rebuild for hcoop
[hcoop/debian/courier-authlib.git] / preauthuserdbcommon.c
1 /*
2 ** Copyright 1998 - 1999 Double Precision, Inc. See COPYING for
3 ** distribution information.
4 */
5
6 #if HAVE_CONFIG_H
7 #include "courier_auth_config.h"
8 #endif
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <errno.h>
13 #include <pwd.h>
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #include <sys/types.h>
18 #include <sys/wait.h>
19
20 #include "auth.h"
21 #include "courierauthdebug.h"
22 #include "userdb/userdb.h"
23 #include "numlib/numlib.h"
24
25 int auth_userdb_pre_common(const char *userid, const char *service,
26 int needpass,
27 int (*callback)(struct authinfo *, void *),
28 void *arg)
29 {
30 char *u;
31 struct userdbs *udb;
32 struct authinfo auth;
33 char *udbs;
34 char *services;
35 char *passwords=0;
36 int rc;
37
38 userdb_set_debug(courier_authdebug_login_level);
39 userdb_init(USERDB ".dat");
40 /* We rely on DPRINTF doing 'safe' printing */
41 DPRINTF("userdb: looking up '%s'", userid);
42 if ( (u=userdb(userid)) == 0)
43 {
44 userdb_close();
45 errno=EPERM;
46 return (-1);
47 }
48
49 if ((udb=userdb_creates(u)) == 0)
50 {
51 free(u);
52 return (-1);
53 }
54 free(u);
55
56 memset(&auth, 0, sizeof(auth));
57
58 auth.sysuserid= &udb->udb_uid;
59 auth.sysgroupid=udb->udb_gid;
60 auth.homedir=udb->udb_dir;
61 auth.address=userid;
62 auth.fullname=udb->udb_gecos;
63 auth.options=udb->udb_options;
64
65 if (needpass)
66 {
67 udbs=userdbshadow(USERDB "shadow.dat", userid);
68
69 if (udbs)
70 {
71 if ((services=malloc(strlen(service)+sizeof("pw"))) == 0)
72 {
73 perror("malloc");
74 free(udbs);
75 userdb_frees(udb);
76 return (1);
77 }
78
79 strcat(strcpy(services, service), "pw");
80
81 passwords=userdb_gets(udbs, services);
82
83 if (passwords)
84 {
85 DPRINTF("found %s in userdbshadow", services);
86 }
87 else
88 {
89 passwords=userdb_gets(udbs, "systempw");
90 if (passwords)
91 {
92 DPRINTF("found systempw in userdbshadow");
93 }
94 else
95 {
96 DPRINTF("no %s or systempw value in userdbshadow for %s",
97 services, userid);
98 }
99 }
100
101 free(services);
102 free(udbs);
103 }
104 auth.passwd=passwords;
105 }
106
107 auth.maildir=udb->udb_mailbox;
108 auth.quota=udb->udb_quota;
109
110 /* Get tokens for AFS */
111 {
112 if (auth.sysuserid)
113 {
114 pid_t pid = fork ();
115
116 if (pid == 0)
117 {
118 char uidstr[32] = "<null>";
119 snprintf(uidstr, sizeof(uidstr), "%ld", (long)*auth.sysuserid);
120
121 libmail_changeuidgid (*auth.sysuserid, auth.sysgroupid);
122 execl ("/etc/courier/get-token", "get-token", uidstr, NULL);
123 }
124 else
125 {
126 waitpid (pid, NULL, 0);
127 }
128 }
129 }
130
131 courier_authdebug_authinfo("DEBUG: authuserdb: ", &auth, 0, passwords);
132 rc= (*callback)(&auth, arg);
133 if (passwords) free(passwords);
134 userdb_frees(udb);
135 return (rc);
136 }
137
138 void auth_userdb_cleanup()
139 {
140 userdb_close();
141 }
142
143 void auth_userdb_enumerate( void(*cb_func)(const char *name,
144 uid_t uid,
145 gid_t gid,
146 const char *homedir,
147 const char *maildir,
148 const char *options,
149 void *void_arg),
150 void *void_arg)
151 {
152 struct userdbs *u;
153
154 userdb_init(USERDB ".dat");
155
156 for (u=userdb_enum_first(); u; u=userdb_enum_next())
157 {
158 (*cb_func)(u->udb_name,
159 u->udb_uid,
160 u->udb_gid,
161 u->udb_dir,
162 u->udb_mailbox,
163 u->udb_options,
164 void_arg);
165 userdb_frees(u);
166 }
167 (*cb_func)(NULL, 0, 0, NULL, NULL, NULL, void_arg);
168 }
169