2 ** Copyright 2000-2008 Double Precision, Inc. See COPYING for
3 ** distribution information.
6 #include "courier_auth_config.h"
18 #include "authmysql.h"
21 #include "courierauthstaticlist.h"
22 #include "courierauthdebug.h"
23 #include "courierauth.h"
26 static bool verify(const authmysqluserinfo
&authinfo
,
30 if (authinfo
.home
.size() == 0) /* User not found */
33 return false; /* Username not found */
36 if (authinfo
.cryptpw
.size())
38 if (authcheckpassword(pass
,authinfo
.cryptpw
.c_str()))
41 return false; /* User/Password not found. */
44 else if (authinfo
.clearpw
.size())
46 if (authinfo
.clearpw
!= pass
)
48 if (courier_authdebug_login_level
>= 2)
50 DPRINTF("supplied password '%s' does not match clearpasswd '%s'",
51 pass
, authinfo
.clearpw
.c_str());
55 DPRINTF("supplied password does not match clearpasswd");
63 DPRINTF("no password available to compare for '%s'", user
);
65 return false; /* Username not found */
70 static int auth_mysql_login(const char *service
, char *authdata
,
71 int (*callback_func
)(struct authinfo
*, void *),
77 if ((user
=strtok(authdata
, "\n")) == 0 ||
78 (pass
=strtok(0, "\n")) == 0)
84 authmysqluserinfo authinfo
;
86 if (!auth_mysql_getuserinfo(user
, service
, authinfo
))
87 /* Fatal error - such as MySQL being down */
93 if (!verify(authinfo
, user
, pass
))
96 memset(&aa
, 0, sizeof(aa
));
98 aa
.sysuserid
= &authinfo
.uid
;
99 aa
.sysgroupid
= authinfo
.gid
;
100 aa
.homedir
=authinfo
.home
.c_str();
102 #define STR(z) (authinfo.z.size() ? authinfo.z.c_str():0)
104 aa
.maildir
=STR(maildir
);
105 aa
.address
=STR(username
);
107 aa
.fullname
=STR(fullname
);
108 aa
.options
=STR(options
);
110 aa
.passwd
=STR(cryptpw
);
111 courier_authdebug_authinfo("DEBUG: authmysql: ", &aa
,
112 aa
.clearpasswd
, aa
.passwd
);
114 return (*callback_func
)(&aa
, callback_arg
);
117 static int auth_mysql_changepw(const char *service
, const char *user
,
121 authmysqluserinfo authinfo
;
123 if (!auth_mysql_getuserinfo(user
, service
, authinfo
))
129 if (!verify(authinfo
, user
, pass
))
134 if (!auth_mysql_setpass(user
, newpass
, authinfo
.cryptpw
.c_str()))
142 static int auth_mysql_cram(const char *service
,
143 const char *authtype
, char *authdata
,
144 int (*callback_func
)(struct authinfo
*, void *),
147 struct cram_callback_info cci
;
149 if (auth_get_cram(authtype
, authdata
, &cci
))
152 cci
.callback_func
=callback_func
;
153 cci
.callback_arg
=callback_arg
;
155 return auth_mysql_pre(cci
.user
, service
, &auth_cram_callback
, &cci
);
158 int auth_mysql(const char *service
, const char *authtype
, char *authdata
,
159 int (*callback_func
)(struct authinfo
*, void *),
162 if (strcmp(authtype
, AUTHTYPE_LOGIN
) == 0)
163 return (auth_mysql_login(service
, authdata
,
164 callback_func
, callback_arg
));
166 return (auth_mysql_cram(service
, authtype
, authdata
,
167 callback_func
, callback_arg
));
170 extern int auth_mysql_pre(const char *user
, const char *service
,
171 int (*callback
)(struct authinfo
*, void *),
174 static struct authstaticinfo authmysql_info
={
181 auth_mysql_enumerate
};
185 struct authstaticinfo
*courier_authmysql_init()
187 return &authmysql_info
;