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 "authmysql.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: authmysql.c,v 1.24 2008/07/10 02:43:55 mrsam Exp $"; |
d9898ee8 |
25 | |
26 | extern void auth_mysql_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_mysql_login(const char *service, char *authdata, |
36 | int (*callback_func)(struct authinfo *, void *), |
37 | void *callback_arg) |
38 | { |
39 | char *user, *pass; |
40 | struct authmysqluserinfo *authinfo; |
41 | struct authinfo aa; |
42 | |
43 | |
44 | if ((user=strtok(authdata, "\n")) == 0 || |
45 | (pass=strtok(0, "\n")) == 0) |
46 | { |
47 | errno=EPERM; |
48 | return (-1); |
49 | } |
50 | |
51 | authinfo=auth_mysql_getuserinfo(user, service); |
52 | |
53 | if (!authinfo) /* Fatal error - such as MySQL being down */ |
54 | { |
55 | errno=EACCES; |
56 | return (1); |
57 | } |
58 | |
59 | if (authinfo->cryptpw) |
60 | { |
61 | if (authcheckpassword(pass,authinfo->cryptpw)) |
62 | { |
63 | errno=EPERM; |
64 | return (-1); /* User/Password not found. */ |
65 | } |
66 | } |
67 | else if (authinfo->clearpw) |
68 | { |
69 | if (strcmp(pass, authinfo->clearpw)) |
70 | { |
71 | if (courier_authdebug_login_level >= 2) |
72 | { |
73 | DPRINTF("supplied password '%s' does not match clearpasswd '%s'", |
74 | pass, authinfo->clearpw); |
75 | } |
76 | else |
77 | { |
78 | DPRINTF("supplied password does not match clearpasswd"); |
79 | } |
80 | errno=EPERM; |
81 | return (-1); |
82 | } |
83 | } |
84 | else |
85 | { |
86 | DPRINTF("no password available to compare"); |
87 | errno=EPERM; |
88 | return (-1); /* Username not found */ |
89 | } |
90 | |
91 | memset(&aa, 0, sizeof(aa)); |
92 | |
93 | aa.sysuserid= &authinfo->uid; |
94 | aa.sysgroupid= authinfo->gid; |
95 | aa.homedir=authinfo->home; |
96 | aa.maildir=authinfo->maildir && authinfo->maildir[0] ? |
97 | authinfo->maildir:0; |
98 | aa.address=authinfo->username; |
99 | aa.quota=authinfo->quota && authinfo->quota[0] ? |
100 | authinfo->quota:0; |
101 | aa.fullname=authinfo->fullname; |
102 | aa.options=authinfo->options; |
103 | aa.clearpasswd=pass; |
104 | aa.passwd=authinfo->cryptpw; |
105 | courier_authdebug_authinfo("DEBUG: authmysql: ", &aa, |
106 | authinfo->clearpw, authinfo->cryptpw); |
107 | |
108 | return (*callback_func)(&aa, callback_arg); |
109 | } |
110 | |
111 | static int auth_mysql_changepw(const char *service, const char *user, |
112 | const char *pass, |
113 | const char *newpass) |
114 | { |
115 | struct authmysqluserinfo *authinfo; |
116 | |
117 | authinfo=auth_mysql_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_mysql_setpass(user, newpass, authinfo->cryptpw)) |
148 | { |
149 | errno=EPERM; |
150 | return (-1); |
151 | } |
152 | return (0); |
153 | } |
154 | |
d9898ee8 |
155 | static int auth_mysql_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_mysql_pre(cci.user, service, &auth_cram_callback, &cci); |
169 | } |
d9898ee8 |
170 | |
171 | int auth_mysql(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_mysql_login(service, authdata, |
177 | callback_func, callback_arg)); |
178 | |
d9898ee8 |
179 | return (auth_mysql_cram(service, authtype, authdata, |
180 | callback_func, callback_arg)); |
d9898ee8 |
181 | } |
182 | |
183 | extern int auth_mysql_pre(const char *user, const char *service, |
184 | int (*callback)(struct authinfo *, void *), |
185 | void *arg); |
186 | |
187 | static struct authstaticinfo authmysql_info={ |
188 | "authmysql", |
189 | auth_mysql, |
190 | auth_mysql_pre, |
191 | auth_mysql_cleanup, |
192 | auth_mysql_changepw, |
193 | auth_mysql_cleanup, |
194 | auth_mysql_enumerate}; |
195 | |
196 | |
197 | struct authstaticinfo *courier_authmysql_init() |
198 | { |
199 | return &authmysql_info; |
200 | } |