Build courier-authlib 0.59.3-1hcoop1.
[hcoop/debian/courier-authlib.git] / preauthshadow.c
1 /*
2 ** Copyright 1998 - 2005 Double Precision, Inc. See COPYING for
3 ** distribution information.
4 */
5
6 #if HAVE_CONFIG_H
7 #include "courier_auth_config.h"
8 #endif
9 #include <time.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <errno.h>
14 #include <pwd.h>
15 #if HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif
18 #if HAVE_SHADOW_H
19 #include <shadow.h>
20 #endif
21
22
23 #include "auth.h"
24 #include "courierauthdebug.h"
25
26 static const char rcsid[]="$Id: preauthshadow.c,v 1.9 2005/11/17 01:29:03 mrsam Exp $";
27
28 int auth_shadow_pre(const char *userid, const char *service,
29 int (*callback)(struct authinfo *, void *),
30 void *arg)
31 {
32 struct authinfo auth;
33 struct passwd *pw;
34 struct spwd *spw;
35 long today;
36
37 memset(&auth, 0, sizeof(auth));
38
39 if ((pw=getpwnam(userid)) == NULL)
40 {
41 if (errno == ENOMEM) return 1;
42 return -1;
43 }
44
45 if ((spw=getspnam(userid)) == NULL)
46 {
47 if (errno == ENOMEM) return 1;
48 return -1;
49 }
50
51 today = (long)time(NULL)/(24L*60*60);
52
53 if ((spw->sp_expire > 0) && (today > spw->sp_expire))
54 {
55 DPRINTF("authshadow: %s - account expired", userid);
56 return -1; /* account expired */
57 }
58
59 if ((spw->sp_lstchg != -1) && (spw->sp_max != -1) &&
60 ((spw->sp_lstchg + spw->sp_max) < today))
61 {
62 DPRINTF("authshadow: %s - password expired", userid);
63 return -1; /* password expired */
64 }
65
66 auth.sysusername=userid;
67 auth.sysgroupid=pw->pw_gid;
68 auth.homedir=pw->pw_dir;
69 auth.address=userid;
70 auth.fullname=pw->pw_gecos;
71 auth.passwd=spw->sp_pwdp;
72
73 courier_authdebug_authinfo("DEBUG: authshadow: ", &auth, 0, pw->pw_passwd);
74 return ((*callback)(&auth, arg));
75 }