Backport to wheezy
[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
d9898ee8 22
373dd817 23#define TOKEN_CMD "/etc/courier/get-token "
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 */
111 { char uidstr[32] = "<null>";
112 char *token_cmd;
76de405b 113 if (auth.sysuserid)
373dd817 114 {
76de405b 115 snprintf(uidstr, sizeof(uidstr), "%ld", (long)*auth.sysuserid);
373dd817 116 if ((token_cmd=malloc(sizeof(TOKEN_CMD)+strlen(uidstr))))
117 {
118 strcat(strcpy(token_cmd, TOKEN_CMD),uidstr);
119 system(token_cmd);
120 free(token_cmd);
121 }
122 }
123 }
124
76de405b 125 courier_authdebug_authinfo("DEBUG: authuserdb: ", &auth, 0, passwords);
126 rc= (*callback)(&auth, arg);
127 if (passwords) free(passwords);
d9898ee8 128 userdb_frees(udb);
129 return (rc);
130}
131
132void auth_userdb_cleanup()
133{
134 userdb_close();
135}
136
137void auth_userdb_enumerate( void(*cb_func)(const char *name,
138 uid_t uid,
139 gid_t gid,
140 const char *homedir,
141 const char *maildir,
142 const char *options,
143 void *void_arg),
144 void *void_arg)
145{
146 struct userdbs *u;
147
148 userdb_init(USERDB ".dat");
149
150 for (u=userdb_enum_first(); u; u=userdb_enum_next())
151 {
152 (*cb_func)(u->udb_name,
153 u->udb_uid,
154 u->udb_gid,
155 u->udb_dir,
156 u->udb_mailbox,
157 u->udb_options,
158 void_arg);
159 userdb_frees(u);
160 }
161 (*cb_func)(NULL, 0, 0, NULL, NULL, NULL, void_arg);
162}
163