d9898ee8 |
1 | /* |
ac40fd9e |
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 "authmysql.h" |
b0322a85 |
19 | #include "courierauthstaticlist.h" |
d9898ee8 |
20 | #include "courierauthdebug.h" |
ac40fd9e |
21 | #include "libhmac/hmac.h" |
22 | #include "cramlib.h" |
d9898ee8 |
23 | |
d9898ee8 |
24 | |
25 | extern void auth_mysql_enumerate( void(*cb_func)(const char *name, |
26 | uid_t uid, |
27 | gid_t gid, |
28 | const char *homedir, |
29 | const char *maildir, |
30 | const char *options, |
31 | void *void_arg), |
32 | void *void_arg); |
33 | |
34 | static int auth_mysql_login(const char *service, char *authdata, |
35 | int (*callback_func)(struct authinfo *, void *), |
36 | void *callback_arg) |
37 | { |
38 | char *user, *pass; |
39 | struct authmysqluserinfo *authinfo; |
40 | struct authinfo aa; |
41 | |
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_mysql_getuserinfo(user, service); |
51 | |
52 | if (!authinfo) /* Fatal error - such as MySQL being down */ |
53 | { |
54 | errno=EACCES; |
b0322a85 |
55 | return (-1); |
d9898ee8 |
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 | memset(&aa, 0, sizeof(aa)); |
91 | |
92 | aa.sysuserid= &authinfo->uid; |
93 | aa.sysgroupid= authinfo->gid; |
94 | aa.homedir=authinfo->home; |
95 | aa.maildir=authinfo->maildir && authinfo->maildir[0] ? |
96 | authinfo->maildir:0; |
97 | aa.address=authinfo->username; |
98 | aa.quota=authinfo->quota && authinfo->quota[0] ? |
99 | authinfo->quota:0; |
100 | aa.fullname=authinfo->fullname; |
101 | aa.options=authinfo->options; |
102 | aa.clearpasswd=pass; |
103 | aa.passwd=authinfo->cryptpw; |
104 | courier_authdebug_authinfo("DEBUG: authmysql: ", &aa, |
105 | authinfo->clearpw, authinfo->cryptpw); |
106 | |
107 | return (*callback_func)(&aa, callback_arg); |
108 | } |
109 | |
110 | static int auth_mysql_changepw(const char *service, const char *user, |
111 | const char *pass, |
112 | const char *newpass) |
113 | { |
114 | struct authmysqluserinfo *authinfo; |
115 | |
116 | authinfo=auth_mysql_getuserinfo(user, service); |
117 | |
118 | if (!authinfo) |
119 | { |
120 | errno=ENOENT; |
121 | return (-1); |
122 | } |
123 | |
124 | if (authinfo->cryptpw) |
125 | { |
126 | if (authcheckpassword(pass,authinfo->cryptpw)) |
127 | { |
128 | errno=EPERM; |
129 | return (-1); /* User/Password not found. */ |
130 | } |
131 | } |
132 | else if (authinfo->clearpw) |
133 | { |
134 | if (strcmp(pass, authinfo->clearpw)) |
135 | { |
136 | errno=EPERM; |
137 | return (-1); |
138 | } |
139 | } |
140 | else |
141 | { |
142 | errno=EPERM; |
143 | return (-1); |
144 | } |
145 | |
146 | if (auth_mysql_setpass(user, newpass, authinfo->cryptpw)) |
147 | { |
148 | errno=EPERM; |
149 | return (-1); |
150 | } |
151 | return (0); |
152 | } |
153 | |
d9898ee8 |
154 | static int auth_mysql_cram(const char *service, |
155 | const char *authtype, char *authdata, |
156 | int (*callback_func)(struct authinfo *, void *), |
157 | void *callback_arg) |
158 | { |
159 | struct cram_callback_info cci; |
160 | |
161 | if (auth_get_cram(authtype, authdata, &cci)) |
162 | return (-1); |
163 | |
164 | cci.callback_func=callback_func; |
165 | cci.callback_arg=callback_arg; |
166 | |
167 | return auth_mysql_pre(cci.user, service, &auth_cram_callback, &cci); |
168 | } |
d9898ee8 |
169 | |
170 | int auth_mysql(const char *service, const char *authtype, char *authdata, |
171 | int (*callback_func)(struct authinfo *, void *), |
172 | void *callback_arg) |
173 | { |
174 | if (strcmp(authtype, AUTHTYPE_LOGIN) == 0) |
175 | return (auth_mysql_login(service, authdata, |
176 | callback_func, callback_arg)); |
177 | |
d9898ee8 |
178 | return (auth_mysql_cram(service, authtype, authdata, |
179 | callback_func, callback_arg)); |
d9898ee8 |
180 | } |
181 | |
182 | extern int auth_mysql_pre(const char *user, const char *service, |
183 | int (*callback)(struct authinfo *, void *), |
184 | void *arg); |
185 | |
186 | static struct authstaticinfo authmysql_info={ |
187 | "authmysql", |
188 | auth_mysql, |
189 | auth_mysql_pre, |
190 | auth_mysql_cleanup, |
191 | auth_mysql_changepw, |
192 | auth_mysql_cleanup, |
193 | auth_mysql_enumerate}; |
194 | |
195 | |
196 | struct authstaticinfo *courier_authmysql_init() |
197 | { |
198 | return &authmysql_info; |
199 | } |