File: preauthuserdbcommon.c

package info (click to toggle)
courier-authlib 0.72.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,824 kB
  • sloc: ansic: 15,828; sh: 5,047; cpp: 4,205; makefile: 853; perl: 761
file content (146 lines) | stat: -rw-r--r-- 2,840 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
** Copyright 1998 - 1999 Double Precision, Inc.  See COPYING for
** distribution information.
*/

#if	HAVE_CONFIG_H
#include	"courier_auth_config.h"
#endif
#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<errno.h>
#include	<pwd.h>
#if	HAVE_UNISTD_H
#include	<unistd.h>
#endif

#include	"auth.h"
#include	"courierauthdebug.h"
#include	"userdb/userdb.h"


int auth_userdb_pre_common(const char *userid, const char *service,
	int needpass,
	int (*callback)(struct authinfo *, void *),
                        void *arg)
{
char	*u;
struct	userdbs *udb;
struct authinfo auth;
char	*udbs;
char	*services;
char	*passwords=0;
int	rc;

	userdb_set_debug(courier_authdebug_login_level);
	userdb_init(USERDB ".dat");
	/* We rely on DPRINTF doing 'safe' printing */
	DPRINTF("userdb: looking up '%s'", userid);
        if ( (u=userdb(userid)) == 0)
	{
		userdb_close();
		errno=EPERM;
		return (-1);
	}

        if ((udb=userdb_creates(u)) == 0)
        {
		free(u);
                return (-1);
        }
	free(u);

        memset(&auth, 0, sizeof(auth));

	auth.sysuserid= &udb->udb_uid;
        auth.sysgroupid=udb->udb_gid;
        auth.homedir=udb->udb_dir;
        auth.address=userid;
        auth.fullname=udb->udb_gecos;
	auth.options=udb->udb_options;

	if (needpass)
	{
		udbs=userdbshadow(USERDB "shadow.dat", userid);

		if (udbs)
		{
			if ((services=malloc(strlen(service)+sizeof("pw"))) == 0)
			{
				perror("malloc");
				free(udbs);
				userdb_frees(udb);
				return (1);
			}

			strcat(strcpy(services, service), "pw");

			passwords=userdb_gets(udbs, services);

			if (passwords)
			{
				DPRINTF("found %s in userdbshadow", services);
			}
			else
			{
				passwords=userdb_gets(udbs, "systempw");
				if (passwords)
				{
					DPRINTF("found systempw in userdbshadow");
				}
				else
				{
					DPRINTF("no %s or systempw value in userdbshadow for %s",
						services, userid);
				}
			}

			free(services);
			free(udbs);
		}
		auth.passwd=passwords;
	}

	auth.maildir=udb->udb_mailbox;
	auth.quota=udb->udb_quota;

	courier_authdebug_authinfo("DEBUG: authuserdb: ", &auth, 0, passwords);
	rc= (*callback)(&auth, arg);
	if (passwords)	free(passwords);
	userdb_frees(udb);
	return (rc);
}

void auth_userdb_cleanup()
{
	userdb_close();
}

void 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)
{
	struct userdbs *u;

	userdb_init(USERDB ".dat");

	for (u=userdb_enum_first(); u; u=userdb_enum_next())
	{
		(*cb_func)(u->udb_name,
			   u->udb_uid,
			   u->udb_gid,
			   u->udb_dir,
			   u->udb_mailbox,
			   u->udb_options,
			   void_arg);
		userdb_frees(u);
	}
	(*cb_func)(NULL, 0, 0, NULL, NULL, NULL, void_arg);
}