1 /*
2 ** Copyright 1998 - 1999 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	<stdio.h>
10 #include	<stdlib.h>
11 #include	<string.h>
12 #include	<errno.h>
13 #include	<pwd.h>
14 #if	HAVE_UNISTD_H
15 #include	<unistd.h>
16 #endif
17 
18 #include	"auth.h"
19 #include	"courierauthdebug.h"
20 #include	"userdb/userdb.h"
21 
22 
auth_userdb_pre_common(const char * userid,const char * service,int needpass,int (* callback)(struct authinfo *,void *),void * arg)23 int auth_userdb_pre_common(const char *userid, const char *service,
24 	int needpass,
25 	int (*callback)(struct authinfo *, void *),
26                         void *arg)
27 {
28 char	*u;
29 struct	userdbs *udb;
30 struct authinfo auth;
31 char	*udbs;
32 char	*services;
33 char	*passwords=0;
34 int	rc;
35 
36 	userdb_set_debug(courier_authdebug_login_level);
37 	userdb_init(USERDB ".dat");
38 	/* We rely on DPRINTF doing 'safe' printing */
39 	DPRINTF("userdb: looking up '%s'", userid);
40         if ( (u=userdb(userid)) == 0)
41 	{
42 		userdb_close();
43 		errno=EPERM;
44 		return (-1);
45 	}
46 
47         if ((udb=userdb_creates(u)) == 0)
48         {
49 		free(u);
50                 return (-1);
51         }
52 	free(u);
53 
54         memset(&auth, 0, sizeof(auth));
55 
56 	auth.sysuserid= &udb->udb_uid;
57         auth.sysgroupid=udb->udb_gid;
58         auth.homedir=udb->udb_dir;
59         auth.address=userid;
60         auth.fullname=udb->udb_gecos;
61 	auth.options=udb->udb_options;
62 
63 	if (needpass)
64 	{
65 		udbs=userdbshadow(USERDB "shadow.dat", userid);
66 
67 		if (udbs)
68 		{
69 			if ((services=malloc(strlen(service)+sizeof("pw"))) == 0)
70 			{
71 				perror("malloc");
72 				free(udbs);
73 				userdb_frees(udb);
74 				return (1);
75 			}
76 
77 			strcat(strcpy(services, service), "pw");
78 
79 			passwords=userdb_gets(udbs, services);
80 
81 			if (passwords)
82 			{
83 				DPRINTF("found %s in userdbshadow", services);
84 			}
85 			else
86 			{
87 				passwords=userdb_gets(udbs, "systempw");
88 				if (passwords)
89 				{
90 					DPRINTF("found systempw in userdbshadow");
91 				}
92 				else
93 				{
94 					DPRINTF("no %s or systempw value in userdbshadow for %s",
95 						services, userid);
96 				}
97 			}
98 
99 			free(services);
100 			free(udbs);
101 		}
102 		auth.passwd=passwords;
103 	}
104 
105 	auth.maildir=udb->udb_mailbox;
106 	auth.quota=udb->udb_quota;
107 
108 	courier_authdebug_authinfo("DEBUG: authuserdb: ", &auth, 0, passwords);
109 	rc= (*callback)(&auth, arg);
110 	if (passwords)	free(passwords);
111 	userdb_frees(udb);
112 	return (rc);
113 }
114 
auth_userdb_cleanup()115 void auth_userdb_cleanup()
116 {
117 	userdb_close();
118 }
119 
auth_userdb_enumerate(void (* cb_func)(const char * name,uid_t uid,gid_t gid,const char * homedir,const char * maildir,const char * options,void * void_arg),void * void_arg)120 void auth_userdb_enumerate( void(*cb_func)(const char *name,
121 					   uid_t uid,
122 					   gid_t gid,
123 					   const char *homedir,
124 					   const char *maildir,
125 					   const char *options,
126 					   void *void_arg),
127 			    void *void_arg)
128 {
129 	struct userdbs *u;
130 
131 	userdb_init(USERDB ".dat");
132 
133 	for (u=userdb_enum_first(); u; u=userdb_enum_next())
134 	{
135 		(*cb_func)(u->udb_name,
136 			   u->udb_uid,
137 			   u->udb_gid,
138 			   u->udb_dir,
139 			   u->udb_mailbox,
140 			   u->udb_options,
141 			   void_arg);
142 		userdb_frees(u);
143 	}
144 	(*cb_func)(NULL, 0, 0, NULL, NULL, NULL, void_arg);
145 }
146 
147