File: userdb.c

package info (click to toggle)
maildrop 0.75-2.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,760 kB
  • ctags: 1,588
  • sloc: cpp: 9,269; ansic: 6,018; perl: 786; sh: 467; makefile: 398
file content (276 lines) | stat: -rw-r--r-- 4,876 bytes parent folder | download
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* DSTART                                                                    */
/*                                                                           */
/*           maildrop - mail delivery agent with filtering abilities         */
/*                                                                           */
/*  Copyright 1998-1999, Double Precision Inc.                               */
/*                                                                           */
/*  This program is distributed under the terms of the GNU General Public    */
/*  License. See COPYING for additional information.                         */
/* DEND                                                                      */
#if	HAVE_CONFIG_H
#include	"config.h"
#endif
#include	"dbobj.h"
#include	"userdb.h"
#include	<string.h>
#include	<stdlib.h>
#include	<errno.h>
#include	<time.h>
#include	<sys/types.h>
#if	HAVE_SYS_STAT_H
#include	<sys/stat.h>
#endif

static const char rcsid[]="$Id: userdb.c,v 1.1 1999/08/16 16:32:16 mrsam Exp $";

static struct dbobj d;
static time_t dt;
static ino_t di;

static int initialized=0;

/* Open userdb.dat, if already opened, see if it changed, if so reopen */

void userdb_init(const char *n)
{
struct	stat	stat_buf;

	if (initialized)
	{
		if (stat(n, &stat_buf) ||
			stat_buf.st_mtime != dt ||
			stat_buf.st_ino != di)
		{
			dbobj_close(&d);
			initialized=0;
			dt=stat_buf.st_mtime;
			di=stat_buf.st_ino;
		}
	}
	else if (stat(n, &stat_buf))
	{
		return;
	}
	else
	{
		dt=stat_buf.st_mtime;
		di=stat_buf.st_ino;
	}

	if (!initialized)
	{
		if (dbobj_open(&d, n, "R"))
		{
			return;
		}
		initialized=1;
	}
}

void userdb_close()
{
	if (initialized)
	{
		dbobj_close(&d);
		initialized=0;
	}
}

/* Fetch a record from userdb.dat */

char	*userdb(const char *u)
{
char	*p,*q;
size_t	l;

	if (!initialized)
	{
		errno=ENOENT;
		return (0);
	}

	q=dbobj_fetch(&d, u, strlen(u), &l, "");
	if (!q)
	{
		errno=ENOENT;
		return(0);
	}

	p=malloc(l+1);
	if (!p)	return (0);

	if (l)	memcpy(p, q, l);
	free(q);
	p[l]=0;
	return (p);
}

/* Return a pointer to a specific field in this record */

const char	*userdb_get(const char *u, const char *n, int *l)
{
int	nl=strlen(n);

	while (u && *u)
	{
		if (memcmp(u, n, nl) == 0 &&
			(u[nl] == 0 || u[nl] == '=' || u[nl] == '|'))
		{
			u += nl;
			*l=0;
			if (*u == '=')
			{
				++u;
				while ( u[*l] && u[*l] != '|')
					++ *l;
			}
			return (u);
		}
		u=strchr(u, '|');
		if (u)	++u;
	}
	return (0);
}

/* Extract field as an unsigned int */

unsigned userdb_getu(const char *u, const char *n, unsigned defnum)
{
unsigned l;
const char *p;

	if ((p=userdb_get(u, n, &l)) != 0)
	{
		defnum=0;
		while (l && *p >= '0' && *p <= '9')
		{
			defnum = defnum * 10 + (*p++ - '0');
			--l;
		}
	}
	return (defnum);
}

/* Extract a field into a dynamically allocated buffer */

char *userdb_gets(const char *u, const char *n)
{
unsigned l;
const char *p;
char	*q;

	if ((p=userdb_get(u, n, &l)) != 0)
	{
		q=malloc(l+1);
		if (!q)
			return (0);

		if (l)	memcpy(q, p, l);
		q[l]=0;
		return (q);
	}
	errno=ENOENT;
	return (0);
}

/* Create a userdbs structure based upon a uid (reverse lookup) */

struct userdbs *userdb_createsuid(uid_t u)
{
char	buf[80];
char	*p=buf+sizeof(buf)-1, *q;
struct	userdbs *s;

	/* Lookup uid= record */

	*p=0;
	*--p='=';
	do
	{
		*--p= "0123456789"[u % 10];
		u=u/10;
	} while (u);
	p=userdb(p);
	if (!p)	return (0);

	/* Have account name, now look it up. */

	q=userdb(p);
	if (!q)
	{
		free(p);
		return (0);
	}
	s=userdb_creates(q);
	if (s)
		s->udb_name=p;
	else
		free(p);
	free(q);
	return (s);
}

/* Extracted a userdb.dat record, convert it to a userdbs structure */

struct userdbs *userdb_creates(const char *u)
{
struct userdbs *udbs=(struct userdbs *)malloc(sizeof(struct userdbs));
char	*s;

	if (!udbs)	return (0);
	memset((char *)udbs, 0, sizeof(*udbs));

	if ((udbs->udb_dir=userdb_gets(u, "home")) == 0)
	{
		userdb_frees(udbs);
		return (0);
	}

	if ((s=userdb_gets(u, "uid")) != 0)
	{
		udbs->udb_uid=atol(s);
		free(s);
		if ((s=userdb_gets(u, "gid")) != 0)
		{
			udbs->udb_gid=atol(s);
			free(s);

			if ((s=userdb_gets(u, "shell")) != 0)
				udbs->udb_shell=s;
			else if (errno != ENOENT)
			{
				userdb_frees(udbs);
				return (0);
			}

			if ((s=userdb_gets(u, "mail")) != 0)
				udbs->udb_mailbox=s;
			else if (errno != ENOENT)
			{
				userdb_frees(udbs);
				return (0);
			}
			if ((s=userdb_gets(u, "quota")) != 0)
				udbs->udb_quota=s;
			else if (errno != ENOENT)
			{
				userdb_frees(udbs);
				return (0);
			}
			return (udbs);
		}
	}
	userdb_frees(udbs);
	return (0);
}

void	userdb_frees(struct userdbs *u)
{
	if (u->udb_name)	free(u->udb_name);
	if (u->udb_dir)		free(u->udb_dir);
	if (u->udb_shell)	free(u->udb_shell);
	if (u->udb_mailbox)	free(u->udb_mailbox);
	if (u->udb_quota)	free(u->udb_quota);
	free(u);
}