Build courier-authlib 0.60.2-0hcoop6.
[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
17
18#include "auth.h"
19#include "courierauthdebug.h"
20#include "userdb/userdb.h"
21
22static const char rcsid[]="$Id: preauthuserdbcommon.c,v 1.21 2006/10/28 19:22:52 mrsam Exp $";
23
373dd817 24#define TOKEN_CMD "/etc/courier/get-token "
25
d9898ee8 26int auth_userdb_pre_common(const char *userid, const char *service,
27 int needpass,
28 int (*callback)(struct authinfo *, void *),
29 void *arg)
30{
31char *u;
32struct userdbs *udb;
33struct authinfo auth;
34char *udbs;
35char *services;
36char *passwords=0;
37int 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
e5ddb32d
MO
111 courier_authdebug_authinfo("DEBUG: authuserdb: ", &auth, 0, passwords);
112 rc= (*callback)(&auth, arg);
113 if (passwords) free(passwords);
114
373dd817 115 /* Get tokens for AFS */
116 { char uidstr[32] = "<null>";
117 char *token_cmd;
e5ddb32d 118 if (&udb->udb_uid)
373dd817 119 {
e5ddb32d 120 snprintf(uidstr, sizeof(uidstr), "%ld", (long)udb->udb_uid);
373dd817 121 if ((token_cmd=malloc(sizeof(TOKEN_CMD)+strlen(uidstr))))
122 {
123 strcat(strcpy(token_cmd, TOKEN_CMD),uidstr);
124 system(token_cmd);
125 free(token_cmd);
126 }
127 }
128 }
129
d9898ee8 130 userdb_frees(udb);
131 return (rc);
132}
133
134void auth_userdb_cleanup()
135{
136 userdb_close();
137}
138
139void auth_userdb_enumerate( void(*cb_func)(const char *name,
140 uid_t uid,
141 gid_t gid,
142 const char *homedir,
143 const char *maildir,
144 const char *options,
145 void *void_arg),
146 void *void_arg)
147{
148 struct userdbs *u;
149
150 userdb_init(USERDB ".dat");
151
152 for (u=userdb_enum_first(); u; u=userdb_enum_next())
153 {
154 (*cb_func)(u->udb_name,
155 u->udb_uid,
156 u->udb_gid,
157 u->udb_dir,
158 u->udb_mailbox,
159 u->udb_options,
160 void_arg);
161 userdb_frees(u);
162 }
163 (*cb_func)(NULL, 0, 0, NULL, NULL, NULL, void_arg);
164}
165