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 "authstaticlist.h"
20 #include "courierauthdebug.h"
21 #include "libhmac/hmac.h"
24 static const char rcsid
[]="$Id: authmysql.c,v 1.24 2008/07/10 02:43:55 mrsam Exp $";
26 extern void auth_mysql_enumerate( void(*cb_func
)(const char *name
,
35 static int auth_mysql_login(const char *service
, char *authdata
,
36 int (*callback_func
)(struct authinfo
*, void *),
40 struct authmysqluserinfo
*authinfo
;
44 if ((user
=strtok(authdata
, "\n")) == 0 ||
45 (pass
=strtok(0, "\n")) == 0)
51 authinfo
=auth_mysql_getuserinfo(user
, service
);
53 if (!authinfo
) /* Fatal error - such as MySQL being down */
59 if (authinfo
->cryptpw
)
61 if (authcheckpassword(pass
,authinfo
->cryptpw
))
64 return (-1); /* User/Password not found. */
67 else if (authinfo
->clearpw
)
69 if (strcmp(pass
, authinfo
->clearpw
))
71 if (courier_authdebug_login_level
>= 2)
73 DPRINTF("supplied password '%s' does not match clearpasswd '%s'",
74 pass
, authinfo
->clearpw
);
78 DPRINTF("supplied password does not match clearpasswd");
86 DPRINTF("no password available to compare");
88 return (-1); /* Username not found */
91 memset(&aa
, 0, sizeof(aa
));
93 aa
.sysuserid
= &authinfo
->uid
;
94 aa
.sysgroupid
= authinfo
->gid
;
95 aa
.homedir
=authinfo
->home
;
96 aa
.maildir
=authinfo
->maildir
&& authinfo
->maildir
[0] ?
98 aa
.address
=authinfo
->username
;
99 aa
.quota
=authinfo
->quota
&& authinfo
->quota
[0] ?
101 aa
.fullname
=authinfo
->fullname
;
102 aa
.options
=authinfo
->options
;
104 aa
.passwd
=authinfo
->cryptpw
;
105 courier_authdebug_authinfo("DEBUG: authmysql: ", &aa
,
106 authinfo
->clearpw
, authinfo
->cryptpw
);
108 return (*callback_func
)(&aa
, callback_arg
);
111 static int auth_mysql_changepw(const char *service
, const char *user
,
115 struct authmysqluserinfo
*authinfo
;
117 authinfo
=auth_mysql_getuserinfo(user
, service
);
125 if (authinfo
->cryptpw
)
127 if (authcheckpassword(pass
,authinfo
->cryptpw
))
130 return (-1); /* User/Password not found. */
133 else if (authinfo
->clearpw
)
135 if (strcmp(pass
, authinfo
->clearpw
))
147 if (auth_mysql_setpass(user
, newpass
, authinfo
->cryptpw
))
155 static int auth_mysql_cram(const char *service
,
156 const char *authtype
, char *authdata
,
157 int (*callback_func
)(struct authinfo
*, void *),
160 struct cram_callback_info cci
;
162 if (auth_get_cram(authtype
, authdata
, &cci
))
165 cci
.callback_func
=callback_func
;
166 cci
.callback_arg
=callback_arg
;
168 return auth_mysql_pre(cci
.user
, service
, &auth_cram_callback
, &cci
);
171 int auth_mysql(const char *service
, const char *authtype
, char *authdata
,
172 int (*callback_func
)(struct authinfo
*, void *),
175 if (strcmp(authtype
, AUTHTYPE_LOGIN
) == 0)
176 return (auth_mysql_login(service
, authdata
,
177 callback_func
, callback_arg
));
179 return (auth_mysql_cram(service
, authtype
, authdata
,
180 callback_func
, callback_arg
));
183 extern int auth_mysql_pre(const char *user
, const char *service
,
184 int (*callback
)(struct authinfo
*, void *),
187 static struct authstaticinfo authmysql_info
={
194 auth_mysql_enumerate
};
197 struct authstaticinfo
*courier_authmysql_init()
199 return &authmysql_info
;