2 ** Copyright 2000-2008 Double Precision, Inc. See COPYING for
3 ** distribution information.
6 #include "courier_auth_config.h"
18 #include "authmysql.h"
19 #include "courierauthstaticlist.h"
20 #include "courierauthdebug.h"
21 #include "libhmac/hmac.h"
25 extern void auth_mysql_enumerate( void(*cb_func
)(const char *name
,
34 static int auth_mysql_login(const char *service
, char *authdata
,
35 int (*callback_func
)(struct authinfo
*, void *),
39 struct authmysqluserinfo
*authinfo
;
43 if ((user
=strtok(authdata
, "\n")) == 0 ||
44 (pass
=strtok(0, "\n")) == 0)
50 authinfo
=auth_mysql_getuserinfo(user
, service
);
52 if (!authinfo
) /* Fatal error - such as MySQL being down */
58 if (authinfo
->cryptpw
)
60 if (authcheckpassword(pass
,authinfo
->cryptpw
))
63 return (-1); /* User/Password not found. */
66 else if (authinfo
->clearpw
)
68 if (strcmp(pass
, authinfo
->clearpw
))
70 if (courier_authdebug_login_level
>= 2)
72 DPRINTF("supplied password '%s' does not match clearpasswd '%s'",
73 pass
, authinfo
->clearpw
);
77 DPRINTF("supplied password does not match clearpasswd");
85 DPRINTF("no password available to compare");
87 return (-1); /* Username not found */
90 memset(&aa
, 0, sizeof(aa
));
92 aa
.sysuserid
= &authinfo
->uid
;
93 aa
.sysgroupid
= authinfo
->gid
;
94 aa
.homedir
=authinfo
->home
;
95 aa
.maildir
=authinfo
->maildir
&& authinfo
->maildir
[0] ?
97 aa
.address
=authinfo
->username
;
98 aa
.quota
=authinfo
->quota
&& authinfo
->quota
[0] ?
100 aa
.fullname
=authinfo
->fullname
;
101 aa
.options
=authinfo
->options
;
103 aa
.passwd
=authinfo
->cryptpw
;
104 courier_authdebug_authinfo("DEBUG: authmysql: ", &aa
,
105 authinfo
->clearpw
, authinfo
->cryptpw
);
107 return (*callback_func
)(&aa
, callback_arg
);
110 static int auth_mysql_changepw(const char *service
, const char *user
,
114 struct authmysqluserinfo
*authinfo
;
116 authinfo
=auth_mysql_getuserinfo(user
, service
);
124 if (authinfo
->cryptpw
)
126 if (authcheckpassword(pass
,authinfo
->cryptpw
))
129 return (-1); /* User/Password not found. */
132 else if (authinfo
->clearpw
)
134 if (strcmp(pass
, authinfo
->clearpw
))
146 if (auth_mysql_setpass(user
, newpass
, authinfo
->cryptpw
))
154 static int auth_mysql_cram(const char *service
,
155 const char *authtype
, char *authdata
,
156 int (*callback_func
)(struct authinfo
*, void *),
159 struct cram_callback_info cci
;
161 if (auth_get_cram(authtype
, authdata
, &cci
))
164 cci
.callback_func
=callback_func
;
165 cci
.callback_arg
=callback_arg
;
167 return auth_mysql_pre(cci
.user
, service
, &auth_cram_callback
, &cci
);
170 int auth_mysql(const char *service
, const char *authtype
, char *authdata
,
171 int (*callback_func
)(struct authinfo
*, void *),
174 if (strcmp(authtype
, AUTHTYPE_LOGIN
) == 0)
175 return (auth_mysql_login(service
, authdata
,
176 callback_func
, callback_arg
));
178 return (auth_mysql_cram(service
, authtype
, authdata
,
179 callback_func
, callback_arg
));
182 extern int auth_mysql_pre(const char *user
, const char *service
,
183 int (*callback
)(struct authinfo
*, void *),
186 static struct authstaticinfo authmysql_info
={
193 auth_mysql_enumerate
};
196 struct authstaticinfo
*courier_authmysql_init()
198 return &authmysql_info
;