2 ** Copyright 2000-2008 Double Precision, Inc. See COPYING for
3 ** distribution information.
6 #include "courier_auth_config.h"
19 #include "courierauthstaticlist.h"
20 #include "courierauth.h"
21 #include "courierauthdebug.h"
22 #include "libhmac/hmac.h"
25 #include "authsqlite.h"
27 static int auth_sqlite_pre(const char *user
, const char *service
,
28 int (*callback
)(struct authinfo
*, void *), void *arg
)
30 authsqliteuserinfo authinfo
;
33 if (!auth_sqlite_getuserinfo(user
, service
, authinfo
))
34 // Fatal error - such as Sqlite being down
37 if (authinfo
.home
.empty()) // User not found
40 memset(&aa
, 0, sizeof(aa
));
42 /*aa.sysusername=user;*/
43 aa
.sysuserid
= &authinfo
.uid
;
44 aa
.sysgroupid
= authinfo
.gid
;
45 aa
.homedir
=authinfo
.home
.c_str();
46 aa
.maildir
=authinfo
.maildir
.empty() ? NULL
:authinfo
.maildir
.c_str();
47 aa
.address
=authinfo
.username
.c_str();
48 aa
.passwd
=authinfo
.cryptpw
.c_str();
49 aa
.clearpasswd
=authinfo
.clearpw
.c_str();
50 aa
.fullname
=authinfo
.fullname
.c_str();
51 aa
.quota
=authinfo
.quota
.empty() ? NULL
:authinfo
.quota
.c_str();
52 aa
.options
=authinfo
.options
.c_str();
53 return ((*callback
)(&aa
, arg
));
56 extern void auth_sqlite_enumerate( void(*cb_func
)(const char *name
,
65 static bool docheckpw(authsqliteuserinfo
&authinfo
, const char *pass
)
67 if (!authinfo
.cryptpw
.empty())
69 if (authcheckpassword(pass
, authinfo
.cryptpw
.c_str()))
72 return false; /* User/Password not found. */
75 else if (!authinfo
.clearpw
.empty())
77 if (authinfo
.clearpw
!= pass
)
79 if (courier_authdebug_login_level
>= 2)
81 DPRINTF("supplied password '%s' does not match clearpasswd '%s'",
82 pass
, authinfo
.clearpw
.c_str());
86 DPRINTF("supplied password does not match clearpasswd");
94 DPRINTF("no password available to compare");
96 return false; /* Username not found */
101 static int auth_sqlite_login(const char *service
, char *authdata
,
102 int (*callback_func
)(struct authinfo
*, void *),
106 authsqliteuserinfo authinfo
;
110 if ((user
=strtok(authdata
, "\n")) == 0 ||
111 (pass
=strtok(0, "\n")) == 0)
117 if (!auth_sqlite_getuserinfo(user
, service
, authinfo
))
118 // Fatal error - such as Sqlite being down
124 if (!docheckpw(authinfo
, pass
))
127 memset(&aa
, 0, sizeof(aa
));
129 aa
.sysuserid
= &authinfo
.uid
;
130 aa
.sysgroupid
= authinfo
.gid
;
131 aa
.homedir
=authinfo
.home
.c_str();
132 aa
.maildir
=authinfo
.maildir
.empty() ? NULL
:authinfo
.maildir
.c_str();
133 aa
.address
=authinfo
.username
.c_str();
134 aa
.quota
=authinfo
.quota
.empty() ? NULL
:authinfo
.quota
.c_str();
135 aa
.fullname
=authinfo
.fullname
.c_str();
136 aa
.options
=authinfo
.options
.c_str();
138 aa
.passwd
=authinfo
.cryptpw
.c_str();
139 courier_authdebug_authinfo("DEBUG: authsqlite: ", &aa
,
140 authinfo
.clearpw
.c_str(),
141 authinfo
.cryptpw
.c_str());
143 return (*callback_func
)(&aa
, callback_arg
);
146 static int auth_sqlite_changepw(const char *service
, const char *user
,
150 authsqliteuserinfo authinfo
;
152 if (!auth_sqlite_getuserinfo(user
, service
, authinfo
))
158 if (!docheckpw(authinfo
, pass
))
161 return (-1); /* User/Password not found. */
164 if (auth_sqlite_setpass(user
, newpass
, authinfo
.cryptpw
.c_str()))
172 static int auth_sqlite_cram(const char *service
,
173 const char *authtype
, char *authdata
,
174 int (*callback_func
)(struct authinfo
*, void *),
177 struct cram_callback_info cci
;
179 if (auth_get_cram(authtype
, authdata
, &cci
))
182 cci
.callback_func
=callback_func
;
183 cci
.callback_arg
=callback_arg
;
185 return auth_sqlite_pre(cci
.user
, service
, &auth_cram_callback
, &cci
);
188 int auth_sqlite(const char *service
, const char *authtype
, char *authdata
,
189 int (*callback_func
)(struct authinfo
*, void *),
192 if (strcmp(authtype
, AUTHTYPE_LOGIN
) == 0)
193 return (auth_sqlite_login(service
, authdata
,
194 callback_func
, callback_arg
));
196 return (auth_sqlite_cram(service
, authtype
, authdata
,
197 callback_func
, callback_arg
));
200 extern int auth_sqlite_pre(const char *user
, const char *service
,
201 int (*callback
)(struct authinfo
*, void *),
204 static struct authstaticinfo authsqlite_info
={
209 auth_sqlite_changepw
,
211 auth_sqlite_enumerate
};
214 struct authstaticinfo
*courier_authsqlite_init()
216 return &authsqlite_info
;