Merge remote-tracking branch 'origin/debian'
[hcoop/debian/courier-authlib.git] / preauthuserdbcommon.c
CommitLineData
d9898ee8 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
c7c68696
CE
17#include <sys/types.h>
18#include <sys/wait.h>
d9898ee8 19
20#include "auth.h"
21#include "courierauthdebug.h"
22#include "userdb/userdb.h"
c7c68696 23#include "numlib/numlib.h"
373dd817 24
d9898ee8 25int auth_userdb_pre_common(const char *userid, const char *service,
26 int needpass,
27 int (*callback)(struct authinfo *, void *),
28 void *arg)
29{
30char *u;
31struct userdbs *udb;
32struct authinfo auth;
33char *udbs;
34char *services;
35char *passwords=0;
36int 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
373dd817 110 /* Get tokens for AFS */
c7c68696 111 {
76de405b 112 if (auth.sysuserid)
c7c68696
CE
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 }
373dd817 129 }
130
76de405b 131 courier_authdebug_authinfo("DEBUG: authuserdb: ", &auth, 0, passwords);
132 rc= (*callback)(&auth, arg);
133 if (passwords) free(passwords);
d9898ee8 134 userdb_frees(udb);
135 return (rc);
136}
137
138void auth_userdb_cleanup()
139{
140 userdb_close();
141}
142
143void 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