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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
/*
* Copyright (c) 1998-2001 Sendmail, Inc. and its suppliers.
* 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.
*
*/
#include <sm/gen.h>
SM_IDSTR(copyright,
"@(#) Copyright (c) 1998-2001 Sendmail, Inc. and its suppliers.\n\
All rights reserved.\n\
Copyright (c) 1983 Eric P. Allman. All rights reserved.\n\
Copyright (c) 1988, 1993\n\
The Regents of the University of California. All rights reserved.\n")
SM_IDSTR(id, "@(#)$Sendmail: praliases.c,v 8.91 2001/03/29 21:15:53 rodney Exp $")
#include <sys/types.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef EX_OK
# undef EX_OK /* unistd.h may have another use for this */
#endif /* EX_OK */
#include <sysexits.h>
#ifndef NOT_SENDMAIL
# define NOT_SENDMAIL
#endif /* ! NOT_SENDMAIL */
#include <sendmail/sendmail.h>
#include <sendmail/pathnames.h>
#include <libsmdb/smdb.h>
static void praliases __P((char *, int, char **));
uid_t RealUid;
gid_t RealGid;
char *RealUserName;
uid_t RunAsUid;
uid_t RunAsGid;
char *RunAsUserName;
int Verbose = 2;
bool DontInitGroups = false;
uid_t TrustedUid = 0;
BITMAP256 DontBlameSendmail;
# define DELIMITERS " ,/"
# define PATH_SEPARATOR ':'
int
main(argc, argv)
int argc;
char **argv;
{
char *cfile;
char *filename = NULL;
SM_FILE_T *cfp;
int ch;
char afilebuf[MAXLINE];
char buf[MAXLINE];
struct passwd *pw;
static char rnamebuf[MAXNAME];
extern char *optarg;
extern int optind;
clrbitmap(DontBlameSendmail);
RunAsUid = RealUid = getuid();
RunAsGid = RealGid = getgid();
pw = getpwuid(RealUid);
if (pw != NULL)
{
if (strlen(pw->pw_name) > MAXNAME - 1)
pw->pw_name[MAXNAME] = 0;
sm_snprintf(rnamebuf, sizeof rnamebuf, "%s", pw->pw_name);
}
else
(void) sm_snprintf(rnamebuf, sizeof rnamebuf,
"Unknown UID %d", (int) RealUid);
RunAsUserName = RealUserName = rnamebuf;
cfile = getcfname(0, 0, SM_GET_SENDMAIL_CF, NULL);
while ((ch = getopt(argc, argv, "C:f:")) != -1)
{
switch ((char)ch) {
case 'C':
cfile = optarg;
break;
case 'f':
filename = optarg;
break;
case '?':
default:
(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
"usage: praliases [-C cffile] [-f aliasfile]\n");
exit(EX_USAGE);
}
}
argc -= optind;
argv += optind;
if (filename != NULL)
{
praliases(filename, argc, argv);
exit(EX_OK);
}
if ((cfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, cfile, SM_IO_RDONLY,
NULL)) == NULL)
{
(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
"praliases: %s: %s\n", cfile,
sm_errstring(errno));
exit(EX_NOINPUT);
}
while (sm_io_fgets(cfp, SM_TIME_DEFAULT, buf, sizeof(buf)) != NULL)
{
register char *b, *p;
b = strchr(buf, '\n');
if (b != NULL)
*b = '\0';
b = buf;
switch (*b++)
{
case 'O': /* option -- see if alias file */
if (sm_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 (sm_strlcpy(afilebuf, b, sizeof afilebuf) >=
sizeof afilebuf)
{
(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
"praliases: AliasFile filename too long: %.30s\n",
b);
(void) sm_io_close(cfp, SM_TIME_DEFAULT);
exit(EX_CONFIG);
}
b = afilebuf;
for (p = b; p != NULL; )
{
while (isascii(*p) && isspace(*p))
p++;
if (*p == '\0')
break;
b = p;
p = strpbrk(p, DELIMITERS);
/* find end of spec */
if (p != NULL)
{
bool quoted = false;
for (; *p != '\0'; p++)
{
/*
** Don't break into a quoted
** string.
*/
if (*p == '"')
quoted = !quoted;
else if (*p == ',' && !quoted)
break;
}
/* No more alias specs follow */
if (*p == '\0')
{
/* chop trailing whitespace */
while (isascii(*p) &&
isspace(*p) &&
p > b)
p--;
*p = '\0';
p = NULL;
}
}
if (p != NULL)
{
char *e = p - 1;
/* chop trailing whitespace */
while (isascii(*e) &&
isspace(*e) &&
e > b)
e--;
*++e = '\0';
*p++ = '\0';
}
praliases(b, argc, argv);
}
default:
continue;
}
}
(void) sm_io_close(cfp, SM_TIME_DEFAULT);
exit(EX_OK);
/* NOTREACHED */
return EX_OK;
}
static void
praliases(filename, argc, argv)
char *filename;
int argc;
char **argv;
{
int result;
char *colon;
char *db_name;
char *db_type;
SMDB_DATABASE *database = NULL;
SMDB_CURSOR *cursor = NULL;
SMDB_DBENT db_key, db_value;
SMDB_DBPARAMS params;
SMDB_USER_INFO user_info;
colon = strchr(filename, PATH_SEPARATOR);
if (colon == NULL)
{
db_name = filename;
db_type = SMDB_TYPE_DEFAULT;
}
else
{
*colon = '\0';
db_name = colon + 1;
db_type = filename;
}
/* clean off arguments */
for (;;)
{
while (isascii(*db_name) && isspace(*db_name))
db_name++;
if (*db_name != '-')
break;
while (*db_name != '\0' &&
!(isascii(*db_name) && isspace(*db_name)))
db_name++;
}
/* Skip non-file based DB types */
if (db_type != NULL && *db_type != '\0')
{
if (db_type != SMDB_TYPE_DEFAULT &&
strcmp(db_type, "hash") != 0 &&
strcmp(db_type, "btree") != 0 &&
strcmp(db_type, "dbm") != 0)
{
sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
"praliases: Skipping non-file based alias type %s\n",
db_type);
return;
}
}
if (*db_name == '\0' || (db_type != NULL && *db_type == '\0'))
{
if (colon != NULL)
*colon = ':';
(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
"praliases: illegal alias specification: %s\n", filename);
goto fatal;
}
memset(¶ms, '\0', sizeof params);
params.smdbp_cache_size = 1024 * 1024;
user_info.smdbu_id = RunAsUid;
user_info.smdbu_group_id = RunAsGid;
(void) sm_strlcpy(user_info.smdbu_name, RunAsUserName,
SMDB_MAX_USER_NAME_LEN);
result = smdb_open_database(&database, db_name, O_RDONLY, 0,
SFF_ROOTOK, db_type, &user_info, ¶ms);
if (result != SMDBE_OK)
{
sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
"praliases: %s: open: %s\n",
db_name, sm_errstring(result));
goto fatal;
}
if (argc == 0)
{
memset(&db_key, '\0', sizeof db_key);
memset(&db_value, '\0', sizeof db_value);
result = database->smdb_cursor(database, &cursor, 0);
if (result != SMDBE_OK)
{
(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
"praliases: %s: set cursor: %s\n", db_name,
sm_errstring(result));
goto fatal;
}
while ((result = cursor->smdbc_get(cursor, &db_key, &db_value,
SMDB_CURSOR_GET_NEXT)) ==
SMDBE_OK)
{
#if 0
/* skip magic @:@ entry */
if (db_key.size == 2 &&
db_key.data[0] == '@' &&
db_key.data[1] == '\0' &&
db_value.size == 2 &&
db_value.data[0] == '@' &&
db_value.data[1] == '\0')
continue;
#endif /* 0 */
(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
"%.*s:%.*s\n",
(int) db_key.size,
(char *) db_key.data,
(int) db_value.size,
(char *) db_value.data);
}
if (result != SMDBE_OK && result != SMDBE_LAST_ENTRY)
{
(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
"praliases: %s: get value at cursor: %s\n",
db_name, sm_errstring(result));
goto fatal;
}
}
else for (; *argv != NULL; ++argv)
{
int get_res;
memset(&db_key, '\0', sizeof db_key);
memset(&db_value, '\0', sizeof db_value);
db_key.data = *argv;
db_key.size = strlen(*argv);
get_res = database->smdb_get(database, &db_key, &db_value, 0);
if (get_res == SMDBE_NOT_FOUND)
{
db_key.size++;
get_res = database->smdb_get(database, &db_key,
&db_value, 0);
}
if (get_res == SMDBE_OK)
{
(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
"%.*s:%.*s\n",
(int) db_key.size,
(char *) db_key.data,
(int) db_value.size,
(char *) db_value.data);
}
else
(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
"%s: No such key\n",
(char *)db_key.data);
}
fatal:
if (cursor != NULL)
(void) cursor->smdbc_close(cursor);
if (database != NULL)
(void) database->smdb_close(database);
if (colon != NULL)
*colon = ':';
return;
}
|