Build courier-authlib 0.60.2-0hcoop5.
[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
18 #include "auth.h"
19 #include "courierauthdebug.h"
20 #include "userdb/userdb.h"
21
22 static const char rcsid[]="$Id: preauthuserdbcommon.c,v 1.21 2006/10/28 19:22:52 mrsam Exp $";
23
24 #define TOKEN_CMD "/etc/courier/get-token "
25
26 int auth_userdb_pre_common(const char *userid, const char *service,
27 int needpass,
28 int (*callback)(struct authinfo *, void *),
29 void *arg)
30 {
31 char *u;
32 struct userdbs *udb;
33 struct authinfo auth;
34 char *udbs;
35 char *services;
36 char *passwords=0;
37 int rc;
38
39 userdb_set_debug(courier_authdebug_login_level);
40 userdb_init(USERDB ".dat");
41 /* We rely on DPRINTF doing 'safe' printing */
42 DPRINTF("userdb: looking up '%s'", userid);
43 if ( (u=userdb(userid)) == 0)
44 {
45 userdb_close();
46 errno=EPERM;
47 return (-1);
48 }
49
50 if ((udb=userdb_creates(u)) == 0)
51 {
52 free(u);
53 return (-1);
54 }
55 free(u);
56
57 memset(&auth, 0, sizeof(auth));
58
59 auth.sysuserid= &udb->udb_uid;
60 auth.sysgroupid=udb->udb_gid;
61 auth.homedir=udb->udb_dir;
62 auth.address=userid;
63 auth.fullname=udb->udb_gecos;
64 auth.options=udb->udb_options;
65
66 if (needpass)
67 {
68 udbs=userdbshadow(USERDB "shadow.dat", userid);
69
70 if (udbs)
71 {
72 if ((services=malloc(strlen(service)+sizeof("pw"))) == 0)
73 {
74 perror("malloc");
75 free(udbs);
76 userdb_frees(udb);
77 return (1);
78 }
79
80 strcat(strcpy(services, service), "pw");
81
82 passwords=userdb_gets(udbs, services);
83
84 if (passwords)
85 {
86 DPRINTF("found %s in userdbshadow", services);
87 }
88 else
89 {
90 passwords=userdb_gets(udbs, "systempw");
91 if (passwords)
92 {
93 DPRINTF("found systempw in userdbshadow");
94 }
95 else
96 {
97 DPRINTF("no %s or systempw value in userdbshadow for %s",
98 services, userid);
99 }
100 }
101
102 free(services);
103 free(udbs);
104 }
105 auth.passwd=passwords;
106 }
107
108 auth.maildir=udb->udb_mailbox;
109 auth.quota=udb->udb_quota;
110
111 /* Get tokens for AFS */
112 { char uidstr[32] = "<null>";
113 char *token_cmd;
114 if (auth.sysuserid)
115 {
116 snprintf(uidstr, sizeof(uidstr), "%ld", (long)*auth.sysuserid);
117 if ((token_cmd=malloc(sizeof(TOKEN_CMD)+strlen(uidstr))))
118 {
119 strcat(strcpy(token_cmd, TOKEN_CMD),uidstr);
120 system(token_cmd);
121 free(token_cmd);
122 }
123 }
124 }
125
126 courier_authdebug_authinfo("DEBUG: authuserdb: ", &auth, 0, passwords);
127 rc= (*callback)(&auth, arg);
128 if (passwords) free(passwords);
129 userdb_frees(udb);
130 return (rc);
131 }
132
133 void auth_userdb_cleanup()
134 {
135 userdb_close();
136 }
137
138 void auth_userdb_enumerate( void(*cb_func)(const char *name,
139 uid_t uid,
140 gid_t gid,
141 const char *homedir,
142 const char *maildir,
143 const char *options,
144 void *void_arg),
145 void *void_arg)
146 {
147 struct userdbs *u;
148
149 userdb_init(USERDB ".dat");
150
151 for (u=userdb_enum_first(); u; u=userdb_enum_next())
152 {
153 (*cb_func)(u->udb_name,
154 u->udb_uid,
155 u->udb_gid,
156 u->udb_dir,
157 u->udb_mailbox,
158 u->udb_options,
159 void_arg);
160 userdb_frees(u);
161 }
162 (*cb_func)(NULL, 0, 0, NULL, NULL, NULL, void_arg);
163 }
164