summaryrefslogtreecommitdiff
path: root/usr.sbin/sendmail/praliases/praliases.c
blob: 39351918e3babde67b2af82b4305d5457376c9c7 (plain)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
 * Copyright (c) 1998 Sendmail, Inc.  All rights reserved.
 * Copyright (c) 1983 Eric P. Allman.  All rights reserved.
 * Copyright (c) 1988, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * By using this file, you agree to the terms and conditions set
 * forth in the LICENSE file which can be found at the top level of
 * the sendmail distribution.
 *
 */

#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1988, 1993\n\
	The Regents of the University of California.  All rights reserved.\n";
#endif /* not lint */

#ifndef lint
static char sccsid[] = "@(#)praliases.c	8.21 (Berkeley) 12/27/1998";
#endif /* not lint */

#if !defined(NDBM) && !defined(NEWDB)
  ERROR README:	You must define one of NDBM or NEWDB in order to compile
  ERROR README:	praliases.
#endif

#ifdef NDBM
# include <ndbm.h>
#endif
#ifndef NOT_SENDMAIL
# define NOT_SENDMAIL
#endif
#include <sendmail.h>
#include <pathnames.h>
#ifdef NEWDB
# include <db.h>
# ifndef DB_VERSION_MAJOR
#  define DB_VERSION_MAJOR 1
# endif
#endif

#if defined(IRIX64) || defined(IRIX5) || defined(IRIX6) || \
    defined(BSD4_4) || defined(__osf__) || defined(__GNU_LIBRARY__)
# ifndef HASSTRERROR
#  define HASSTRERROR	1	/* has strerror(3) */
# endif
#endif

#if !HASSTRERROR
extern char	*strerror __P((int));
#endif

static void praliases __P((char *, int, char **));
#ifdef NDBM
static void praliases_dbm __P((char *, int, char **));
#endif

int
main(argc, argv)
	int argc;
	char **argv;
{
	extern char *optarg;
	extern int optind;
	char *cfile;
#if _FFR_GRAB_ALIASFILE_OPTION
	char *filename = NULL;
#else
	char *filename = "/etc/aliases";
#endif
	FILE *cfp;
	int ch;
	char afilebuf[MAXLINE];
	char buf[MAXLINE];

	cfile = _PATH_SENDMAILCF;
#if _FFR_GRAB_ALIASFILE_OPTION
	while ((ch = getopt(argc, argv, "C:f:")) != EOF)
#else
	while ((ch = getopt(argc, argv, "f:")) != EOF)
#endif
	{
		switch ((char)ch) {
		case 'C':
			cfile = optarg;
			break;
		case 'f':
			filename = optarg;
			break;
		case '?':
		default:
			(void)fprintf(stderr,
#if _FFR_GRAB_ALIASFILE_OPTION
				"usage: praliases [-C cffile] [-f aliasfile]\n");
#else
				"usage: praliases [-f aliasfile]\n");
#endif
			exit(EX_USAGE);
		}
	}
	argc -= optind;
	argv += optind;

	if (filename != NULL)
	{
		praliases(filename, argc, argv);
		exit(EX_OK);
	}

	if ((cfp = fopen(cfile, "r")) == NULL)
	{
		fprintf(stderr, "praliases: ");
		perror(cfile);
		exit(EX_NOINPUT);
	}

	while (fgets(buf, sizeof(buf), cfp) != NULL)
	{
		register char *b, *p;

		b = buf;
		switch (*b++)
		{
		  case 'O':		/* option -- see if alias file */
			if (strncasecmp(b, " AliasFile", 10) == 0 &&
			    !(isascii(b[10]) && isalnum(b[10])))
			{
				/* new form -- find value */
				b = strchr(b, '=');
				if (b == NULL)
					continue;
				while (isascii(*++b) && isspace(*b))
					continue;
			}
			else if (*b++ != 'A')
			{
				/* something else boring */
				continue;
			}

			/* this is the A or AliasFile option -- save it */
			if (strlen(b) >= sizeof afilebuf)
			{
				fprintf(stderr,
					"AliasFile filename too long: %.30s...\n",
					b);
				(void) fclose(cfp);
				exit(EX_CONFIG);
			}
			strcpy(afilebuf, b);
			b = afilebuf;

			for (p = b; p != NULL; )
			{
				while (isascii(*p) && isspace(*p))
					p++;
				if (*p == '\0')
					break;
				b = p;

				p = strpbrk(p, " ,/");
				/* find end of spec */
				if (p != NULL)
					p = strpbrk(p, ",\n");
				if (p != NULL)
					*p++ = '\0';

				praliases(b, argc, argv);
			}

		  default:
			continue;
		}
	}
	(void) fclose(cfp);
	exit(EX_OK);
}

static void
praliases(filename, argc, argv)
	char *filename;
	int  argc;
	char **argv;
{
#ifdef NEWDB
	DB *db;
	DBT newdbkey, newdbcontent;
	char buf[MAXNAME];
#endif
	char *class;

	class = strchr(filename, ':');
	if (class != NULL)
	{
		if (strncasecmp(filename, "dbm:", 4) == 0)
		{
#ifdef NDBM
			praliases_dbm(class + 1, argc, argv);
			return;
#else
			fprintf(stderr, "class dbm not available\n");
			exit(EX_DATAERR);
#endif
		}
		filename = class + 1;
	}
#ifdef NEWDB
	if (strlen(filename) + 4 >= sizeof buf)
	{
		fprintf(stderr, "Alias filename too long: %.30s...\n", filename);
		exit(EX_USAGE);
	}
	(void) strcpy(buf, filename);
	(void) strcat(buf, ".db");
# if DB_VERSION_MAJOR < 2
	db = dbopen(buf, O_RDONLY, 0444, DB_HASH, NULL);
# else
	db = NULL;
	errno = db_open(buf, DB_HASH, DB_RDONLY, 0444, NULL, NULL, &db);
# endif
	if (db != NULL)
	{
		if (!argc)
		{
# if DB_VERSION_MAJOR > 1
			DBC *dbc;
# endif
			bzero(&newdbkey, sizeof newdbkey);
			bzero(&newdbcontent, sizeof newdbcontent);

# if DB_VERSION_MAJOR < 2
			while(!db->seq(db, &newdbkey, &newdbcontent, R_NEXT))
# else
#  if DB_VERSION_MAJOR > 2 || DB_VERSION_MINOR >=6
			if ((errno = db->cursor(db, NULL, &dbc, 0)) == 0)
#  else
			if ((errno = db->cursor(db, NULL, &dbc)) == 0)
#  endif
			{
				while ((errno = dbc->c_get(dbc, &newdbkey,
							   &newdbcontent,
							   DB_NEXT)) == 0)
# endif
				printf("%.*s:%.*s\n",
					(int) newdbkey.size,
					(char *) newdbkey.data,
					(int) newdbcontent.size,
					(char *) newdbcontent.data);
# if DB_VERSION_MAJOR > 1
				(void) dbc->c_close(dbc);
			}
			else
			{
				fprintf(stderr,
					"praliases: %s: Could not set cursor: %s\n",
					buf, strerror(errno));
				errno = db->close(db, 0);
				exit(EX_DATAERR);
			}
# endif
		}
		else for (; *argv; ++argv)
		{
			bzero(&newdbkey, sizeof newdbkey);
			bzero(&newdbcontent, sizeof newdbcontent);
			newdbkey.data = *argv;
			newdbkey.size = strlen(*argv) + 1;
# if DB_VERSION_MAJOR < 2
			if (!db->get(db, &newdbkey, &newdbcontent, 0))
# else
			if ((errno = db->get(db, NULL, &newdbkey,
					     &newdbcontent, 0)) == 0)
# endif
				printf("%s:%.*s\n", (char *) newdbkey.data,
					(int) newdbcontent.size,
					(char *) newdbcontent.data);
			else
				printf("%s: No such key\n",
					(char *) newdbkey.data);
		}
# if DB_VERSION_MAJOR < 2
		(void)db->close(db);
# else
		errno = db->close(db, 0);
# endif
	}
	else
	{
#endif
#ifdef NDBM
		praliases_dbm(filename, argc, argv);
#endif
#ifdef NEWDB
	}
#endif
}

#ifdef NDBM
static void
praliases_dbm(filename, argc, argv)
	char *filename;
	int  argc;
	char **argv;
{
	DBM *dbp;
	datum content, key;

	if ((dbp = dbm_open(filename, O_RDONLY, 0)) == NULL)
	{
		(void)fprintf(stderr,
		    "praliases: %s: %s\n", filename, strerror(errno));
		exit(EX_OSFILE);
	}
	if (!argc)
	{
		for (key = dbm_firstkey(dbp);
		    key.dptr != NULL; key = dbm_nextkey(dbp))
		{
			content = dbm_fetch(dbp, key);
			(void)printf("%.*s:%.*s\n",
				(int) key.dsize, key.dptr,
				(int) content.dsize, content.dptr);
		}
	}
	else
	{
		for (; *argv; ++argv)
		{
			/*
			**  Use the sendmail adaptive algorithm of trying
			**  the key first without, then if needed with,
			**  the terminating NULL byte.
			*/
			key.dptr = *argv;
			key.dsize = strlen(*argv);
			content = dbm_fetch(dbp, key);
			if (content.dptr == NULL)
			{
				key.dsize++;
				content = dbm_fetch(dbp, key);
			}
			if (content.dptr != NULL)
				(void)printf("%s:%.*s\n", key.dptr,
					(int) content.dsize, content.dptr);
			else
				(void)printf("%s: No such key\n", key.dptr);
		}
	}
	dbm_close(dbp);
}
#endif

#if !HASSTRERROR

char *
strerror(eno)
	int eno;
{
	extern int sys_nerr;
	extern char *sys_errlist[];
	static char ebuf[60];

	if (eno >= 0 && eno < sys_nerr)
		return sys_errlist[eno];
	(void) sprintf(ebuf, "Error %d", eno);
	return ebuf;
}

#endif /* !HASSTRERROR */