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 
lookup_and_validate(const mystring & fullname,pwentry * & pw,vpwentry * & vpw,const mystring & password,bool mustexist,bool userpass)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);
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);
53     if(vpw)
54       RETURN(err, "Virtual user already exists");
55     else {
56       vpw = new vpwentry(virtname, "*", 0, 0, false);
57       RETURN(ok, "");
58     }
59   }
60 }
61 
CMD_FD(lookup)62 CMD_FD(lookup)
63   // Usage: lookup username-virtname password
64   // Result: binary vpwentry data
65 {
66   mystring fulluser = args[0];
67   mystring password = args[1];
68   args[1] = LOG_PASSWORD;
69   logcommand(args);
70 
71   pwentry* pw;
72   vpwentry* vpw;
73   OK_RESPONSE(lookup_and_validate(fulluser, pw, vpw, password, true, true));
74 
75   response(response::ok, vpw->to_record()).write(fd);
76   RETURN(ok, "Wrote virtual user data");
77 }
78