d9898ee8 |
1 | /* |
0fde1ce3 |
2 | ** Copyright 2000-2008 Double Precision, Inc. See COPYING for |
d9898ee8 |
3 | ** distribution information. |
4 | */ |
5 | #if HAVE_CONFIG_H |
6 | #include "courier_auth_config.h" |
7 | #endif |
8 | #include <stdio.h> |
9 | #include <stdlib.h> |
10 | #include <string.h> |
11 | #include <errno.h> |
12 | #include <pwd.h> |
13 | #if HAVE_UNISTD_H |
14 | #include <unistd.h> |
15 | #endif |
16 | |
17 | #include "auth.h" |
18 | #include "authpgsql.h" |
19 | #include "authstaticlist.h" |
20 | #include "courierauthdebug.h" |
0fde1ce3 |
21 | #include "libhmac/hmac.h" |
22 | #include "cramlib.h" |
d9898ee8 |
23 | |
0fde1ce3 |
24 | static const char rcsid[]="$Id: authpgsql.c,v 1.14 2008/07/10 02:43:55 mrsam Exp $"; |
d9898ee8 |
25 | |
26 | extern void auth_pgsql_enumerate( void(*cb_func)(const char *name, |
27 | uid_t uid, |
28 | gid_t gid, |
29 | const char *homedir, |
30 | const char *maildir, |
31 | const char *options, |
32 | void *void_arg), |
33 | void *void_arg); |
34 | |
35 | static int auth_pgsql_login(const char *service, char *authdata, |
36 | int (*callback_func)(struct authinfo *, void *), |
37 | void *callback_arg) |
38 | { |
39 | char *user, *pass; |
40 | struct authpgsqluserinfo *authinfo; |
41 | struct authinfo aa; |
42 | |
43 | if ((user=strtok(authdata, "\n")) == 0 || |
44 | (pass=strtok(0, "\n")) == 0) |
45 | { |
46 | errno=EPERM; |
47 | return (-1); |
48 | } |
49 | |
50 | authinfo=auth_pgsql_getuserinfo(user, service); |
51 | |
52 | if (!authinfo) /* Fatal error - such as PgSQL being down */ |
53 | { |
54 | errno=EACCES; |
55 | return (1); |
56 | } |
57 | |
58 | if (authinfo->cryptpw) |
59 | { |
60 | if (authcheckpassword(pass,authinfo->cryptpw)) |
61 | { |
62 | errno=EPERM; |
63 | return (-1); /* User/Password not found. */ |
64 | } |
65 | } |
66 | else if (authinfo->clearpw) |
67 | { |
68 | if (strcmp(pass, authinfo->clearpw)) |
69 | { |
70 | if (courier_authdebug_login_level >= 2) |
71 | { |
72 | DPRINTF("supplied password '%s' does not match clearpasswd '%s'", |
73 | pass, authinfo->clearpw); |
74 | } |
75 | else |
76 | { |
77 | DPRINTF("supplied password does not match clearpasswd"); |
78 | } |
79 | errno=EPERM; |
80 | return (-1); |
81 | } |
82 | } |
83 | else |
84 | { |
85 | DPRINTF("no password available to compare"); |
86 | errno=EPERM; |
87 | return (-1); /* Username not found */ |
88 | } |
89 | |
90 | |
91 | memset(&aa, 0, sizeof(aa)); |
92 | |
93 | /*aa.sysusername=user;*/ |
94 | aa.sysuserid= &authinfo->uid; |
95 | aa.sysgroupid= authinfo->gid; |
96 | aa.homedir=authinfo->home; |
97 | aa.maildir=authinfo->maildir && authinfo->maildir[0] ? |
98 | authinfo->maildir:0; |
99 | aa.address=authinfo->username; |
100 | aa.quota=authinfo->quota && authinfo->quota[0] ? |
101 | authinfo->quota:0; |
102 | aa.fullname=authinfo->fullname; |
103 | aa.options=authinfo->options; |
104 | aa.passwd=authinfo->cryptpw; |
105 | aa.clearpasswd=pass; |
106 | courier_authdebug_authinfo("DEBUG: authpgsql: ", &aa, |
107 | authinfo->clearpw, authinfo->cryptpw); |
108 | return (*callback_func)(&aa, callback_arg); |
109 | } |
110 | |
111 | static int auth_pgsql_changepw(const char *service, const char *user, |
112 | const char *pass, |
113 | const char *newpass) |
114 | { |
115 | struct authpgsqluserinfo *authinfo; |
116 | |
117 | authinfo=auth_pgsql_getuserinfo(user, service); |
118 | |
119 | if (!authinfo) |
120 | { |
121 | errno=ENOENT; |
122 | return (-1); |
123 | } |
124 | |
125 | if (authinfo->cryptpw) |
126 | { |
127 | if (authcheckpassword(pass,authinfo->cryptpw)) |
128 | { |
129 | errno=EPERM; |
130 | return (-1); /* User/Password not found. */ |
131 | } |
132 | } |
133 | else if (authinfo->clearpw) |
134 | { |
135 | if (strcmp(pass, authinfo->clearpw)) |
136 | { |
137 | errno=EPERM; |
138 | return (-1); |
139 | } |
140 | } |
141 | else |
142 | { |
143 | errno=EPERM; |
144 | return (-1); |
145 | } |
146 | |
147 | if (auth_pgsql_setpass(user, newpass, authinfo->cryptpw)) |
148 | { |
149 | errno=EPERM; |
150 | return (-1); |
151 | } |
152 | return (0); |
153 | } |
154 | |
d9898ee8 |
155 | static int auth_pgsql_cram(const char *service, |
156 | const char *authtype, char *authdata, |
157 | int (*callback_func)(struct authinfo *, void *), |
158 | void *callback_arg) |
159 | { |
160 | struct cram_callback_info cci; |
161 | |
162 | if (auth_get_cram(authtype, authdata, &cci)) |
163 | return (-1); |
164 | |
165 | cci.callback_func=callback_func; |
166 | cci.callback_arg=callback_arg; |
167 | |
168 | return auth_pgsql_pre(cci.user, service, &auth_cram_callback, &cci); |
169 | } |
d9898ee8 |
170 | |
171 | int auth_pgsql(const char *service, const char *authtype, char *authdata, |
172 | int (*callback_func)(struct authinfo *, void *), |
173 | void *callback_arg) |
174 | { |
175 | if (strcmp(authtype, AUTHTYPE_LOGIN) == 0) |
176 | return (auth_pgsql_login(service, authdata, |
177 | callback_func, callback_arg)); |
178 | |
d9898ee8 |
179 | return (auth_pgsql_cram(service, authtype, authdata, |
180 | callback_func, callback_arg)); |
d9898ee8 |
181 | } |
182 | |
183 | extern int auth_pgsql_pre(const char *user, const char *service, |
184 | int (*callback)(struct authinfo *, void *), |
185 | void *arg); |
186 | |
187 | static struct authstaticinfo authpgsql_info={ |
188 | "authpgsql", |
189 | auth_pgsql, |
190 | auth_pgsql_pre, |
191 | auth_pgsql_cleanup, |
192 | auth_pgsql_changepw, |
193 | auth_pgsql_cleanup, |
194 | auth_pgsql_enumerate}; |
195 | |
196 | struct authstaticinfo *courier_authpgsql_init() |
197 | { |
198 | return &authpgsql_info; |
199 | } |