2 ** Copyright 2000-2008 Double Precision, Inc. See COPYING for
3 ** distribution information.
6 #include "courier_auth_config.h"
18 #include "authpgsql.h"
19 #include "authstaticlist.h"
20 #include "courierauthdebug.h"
21 #include "libhmac/hmac.h"
24 static const char rcsid
[]="$Id: authpgsql.c,v 1.14 2008/07/10 02:43:55 mrsam Exp $";
26 extern void auth_pgsql_enumerate( void(*cb_func
)(const char *name
,
35 static int auth_pgsql_login(const char *service
, char *authdata
,
36 int (*callback_func
)(struct authinfo
*, void *),
40 struct authpgsqluserinfo
*authinfo
;
43 if ((user
=strtok(authdata
, "\n")) == 0 ||
44 (pass
=strtok(0, "\n")) == 0)
50 authinfo
=auth_pgsql_getuserinfo(user
, service
);
52 if (!authinfo
) /* Fatal error - such as PgSQL 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 */
91 memset(&aa
, 0, sizeof(aa
));
93 /*aa.sysusername=user;*/
94 aa
.sysuserid
= &authinfo
->uid
;
95 aa
.sysgroupid
= authinfo
->gid
;
96 aa
.homedir
=authinfo
->home
;
97 aa
.maildir
=authinfo
->maildir
&& authinfo
->maildir
[0] ?
99 aa
.address
=authinfo
->username
;
100 aa
.quota
=authinfo
->quota
&& authinfo
->quota
[0] ?
102 aa
.fullname
=authinfo
->fullname
;
103 aa
.options
=authinfo
->options
;
104 aa
.passwd
=authinfo
->cryptpw
;
106 courier_authdebug_authinfo("DEBUG: authpgsql: ", &aa
,
107 authinfo
->clearpw
, authinfo
->cryptpw
);
108 return (*callback_func
)(&aa
, callback_arg
);
111 static int auth_pgsql_changepw(const char *service
, const char *user
,
115 struct authpgsqluserinfo
*authinfo
;
117 authinfo
=auth_pgsql_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_pgsql_setpass(user
, newpass
, authinfo
->cryptpw
))
155 static int auth_pgsql_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_pgsql_pre(cci
.user
, service
, &auth_cram_callback
, &cci
);
171 int auth_pgsql(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_pgsql_login(service
, authdata
,
177 callback_func
, callback_arg
));
179 return (auth_pgsql_cram(service
, authtype
, authdata
,
180 callback_func
, callback_arg
));
183 extern int auth_pgsql_pre(const char *user
, const char *service
,
184 int (*callback
)(struct authinfo
*, void *),
187 static struct authstaticinfo authpgsql_info
={
194 auth_pgsql_enumerate
};
196 struct authstaticinfo
*courier_authpgsql_init()
198 return &authpgsql_info
;