daemon/lookup.cc
changeset 0 6f7a81934006
child 2 b3afb9f1e801
equal deleted inserted replaced
-1:000000000000 0:6f7a81934006
       
     1 // Copyright (C) 1999,2000 Bruce Guenter <bruceg@em.ca>
       
     2 //
       
     3 // This program is free software; you can redistribute it and/or modify
       
     4 // it under the terms of the GNU General Public License as published by
       
     5 // the Free Software Foundation; either version 2 of the License, or
       
     6 // (at your option) any later version.
       
     7 //
       
     8 // This program is distributed in the hope that it will be useful,
       
     9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    11 // GNU General Public License for more details.
       
    12 //
       
    13 // You should have received a copy of the GNU General Public License
       
    14 // along with this program; if not, write to the Free Software
       
    15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    16 
       
    17 #include <config.h>
       
    18 #include "daemon.h"
       
    19 #include "log.h"
       
    20 #include "misc/lookup.h"
       
    21 #include "misc/pwentry_table.h"
       
    22 
       
    23 response lookup_and_validate(const mystring& fullname,
       
    24 			     pwentry* &pw, vpwentry* &vpw,
       
    25 			     const mystring& password,
       
    26 			     bool mustexist,
       
    27 			     bool userpass)
       
    28 {
       
    29   if(userpass && !mustexist)
       
    30     RETURN(err, "Internal error -- userpass && !mustexist");
       
    31   mystring virtname;
       
    32   if(!lookup_baseuser(fullname, pw, virtname))
       
    33     RETURN(err, "Invalid or unknown base user or domain");
       
    34   if(!password)
       
    35     RETURN(err, "Incorrect password");
       
    36   bool passok = pw->authenticate(password);
       
    37   if(!passok && !userpass)
       
    38     RETURN(err, "Incorrect password");
       
    39   if(virtname.empty())
       
    40     RETURN(err, "User name does not refer to a virtual user");
       
    41   state = new saved_state(pw);
       
    42   if(mustexist) {
       
    43     vpw = state->domain.lookup(virtname, true);
       
    44     if(!vpw)
       
    45       RETURN(err, "Invalid or unknown virtual user");
       
    46     else if(!passok && !vpw->authenticate(password))
       
    47       RETURN(err, "Incorrect password");
       
    48     else
       
    49       RETURN(ok, "");
       
    50   }
       
    51   else {			// user must not already exist
       
    52     vpw = state->domain.lookup(virtname, true);
       
    53     if(vpw)
       
    54       RETURN(err, "Virtual user already exists");
       
    55     else {
       
    56       vpw = new vpwentry(virtname, "*", 0, 0);
       
    57       vpw->set_defaults(true, true);
       
    58       RETURN(ok, "");
       
    59     }
       
    60   }
       
    61 }
       
    62 
       
    63 CMD_FD(lookup)
       
    64   // Usage: lookup username-virtname password
       
    65   // Result: binary vpwentry data
       
    66 {
       
    67   mystring fulluser = args[0];
       
    68   mystring password = args[1];
       
    69   args[1] = LOG_PASSWORD;
       
    70   logcommand(args);
       
    71 
       
    72   pwentry* pw;
       
    73   vpwentry* vpw;
       
    74   OK_RESPONSE(lookup_and_validate(fulluser, pw, vpw, password, true, true));
       
    75 
       
    76   response(response::ok, vpw->to_record()).write(fd);
       
    77   RETURN(ok, "Wrote virtual user data");
       
    78 }