Build courier-authlib 0.60.2-0hcoop5.
[hcoop/debian/courier-authlib.git] / authmysql.c
CommitLineData
d9898ee8 1/*
2** Copyright 2000-2004 Double Precision, Inc. See COPYING for
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"
21
22static const char rcsid[]="$Id: authmysql.c,v 1.23 2006/10/28 19:22:52 mrsam Exp $";
23
24extern void auth_mysql_enumerate( void(*cb_func)(const char *name,
25 uid_t uid,
26 gid_t gid,
27 const char *homedir,
28 const char *maildir,
29 const char *options,
30 void *void_arg),
31 void *void_arg);
32
33static int auth_mysql_login(const char *service, char *authdata,
34 int (*callback_func)(struct authinfo *, void *),
35 void *callback_arg)
36{
37 char *user, *pass;
38 struct authmysqluserinfo *authinfo;
39 struct authinfo aa;
40
41
42 if ((user=strtok(authdata, "\n")) == 0 ||
43 (pass=strtok(0, "\n")) == 0)
44 {
45 errno=EPERM;
46 return (-1);
47 }
48
49 authinfo=auth_mysql_getuserinfo(user, service);
50
51 if (!authinfo) /* Fatal error - such as MySQL being down */
52 {
53 errno=EACCES;
54 return (1);
55 }
56
57 if (authinfo->cryptpw)
58 {
59 if (authcheckpassword(pass,authinfo->cryptpw))
60 {
61 errno=EPERM;
62 return (-1); /* User/Password not found. */
63 }
64 }
65 else if (authinfo->clearpw)
66 {
67 if (strcmp(pass, authinfo->clearpw))
68 {
69 if (courier_authdebug_login_level >= 2)
70 {
71 DPRINTF("supplied password '%s' does not match clearpasswd '%s'",
72 pass, authinfo->clearpw);
73 }
74 else
75 {
76 DPRINTF("supplied password does not match clearpasswd");
77 }
78 errno=EPERM;
79 return (-1);
80 }
81 }
82 else
83 {
84 DPRINTF("no password available to compare");
85 errno=EPERM;
86 return (-1); /* Username not found */
87 }
88
89 memset(&aa, 0, sizeof(aa));
90
91 aa.sysuserid= &authinfo->uid;
92 aa.sysgroupid= authinfo->gid;
93 aa.homedir=authinfo->home;
94 aa.maildir=authinfo->maildir && authinfo->maildir[0] ?
95 authinfo->maildir:0;
96 aa.address=authinfo->username;
97 aa.quota=authinfo->quota && authinfo->quota[0] ?
98 authinfo->quota:0;
99 aa.fullname=authinfo->fullname;
100 aa.options=authinfo->options;
101 aa.clearpasswd=pass;
102 aa.passwd=authinfo->cryptpw;
103 courier_authdebug_authinfo("DEBUG: authmysql: ", &aa,
104 authinfo->clearpw, authinfo->cryptpw);
105
106 return (*callback_func)(&aa, callback_arg);
107}
108
109static int auth_mysql_changepw(const char *service, const char *user,
110 const char *pass,
111 const char *newpass)
112{
113 struct authmysqluserinfo *authinfo;
114
115 authinfo=auth_mysql_getuserinfo(user, service);
116
117 if (!authinfo)
118 {
119 errno=ENOENT;
120 return (-1);
121 }
122
123 if (authinfo->cryptpw)
124 {
125 if (authcheckpassword(pass,authinfo->cryptpw))
126 {
127 errno=EPERM;
128 return (-1); /* User/Password not found. */
129 }
130 }
131 else if (authinfo->clearpw)
132 {
133 if (strcmp(pass, authinfo->clearpw))
134 {
135 errno=EPERM;
136 return (-1);
137 }
138 }
139 else
140 {
141 errno=EPERM;
142 return (-1);
143 }
144
145 if (auth_mysql_setpass(user, newpass, authinfo->cryptpw))
146 {
147 errno=EPERM;
148 return (-1);
149 }
150 return (0);
151}
152
153#if HAVE_HMACLIB
154
155#include "libhmac/hmac.h"
156#include "cramlib.h"
157
158
159static int auth_mysql_cram(const char *service,
160 const char *authtype, char *authdata,
161 int (*callback_func)(struct authinfo *, void *),
162 void *callback_arg)
163{
164 struct cram_callback_info cci;
165
166 if (auth_get_cram(authtype, authdata, &cci))
167 return (-1);
168
169 cci.callback_func=callback_func;
170 cci.callback_arg=callback_arg;
171
172 return auth_mysql_pre(cci.user, service, &auth_cram_callback, &cci);
173}
174#endif
175
176int auth_mysql(const char *service, const char *authtype, char *authdata,
177 int (*callback_func)(struct authinfo *, void *),
178 void *callback_arg)
179{
180 if (strcmp(authtype, AUTHTYPE_LOGIN) == 0)
181 return (auth_mysql_login(service, authdata,
182 callback_func, callback_arg));
183
184#if HAVE_HMACLIB
185 return (auth_mysql_cram(service, authtype, authdata,
186 callback_func, callback_arg));
187#else
188 errno=EPERM;
189 return (-1);
190#endif
191}
192
193extern int auth_mysql_pre(const char *user, const char *service,
194 int (*callback)(struct authinfo *, void *),
195 void *arg);
196
197static struct authstaticinfo authmysql_info={
198 "authmysql",
199 auth_mysql,
200 auth_mysql_pre,
201 auth_mysql_cleanup,
202 auth_mysql_changepw,
203 auth_mysql_cleanup,
204 auth_mysql_enumerate};
205
206
207struct authstaticinfo *courier_authmysql_init()
208{
209 return &authmysql_info;
210}