From 74c280481f7b3087059573dc63b94ac093b17466 Mon Sep 17 00:00:00 2001 From: gshapiro Date: Sat, 12 Aug 2000 22:19:16 +0000 Subject: Fix conflicts from merge of sendmail 8.11.0. PR: bin/11552 misc/18512 bin/15088 --- contrib/sendmail/praliases/praliases.c | 483 ++++++++++++++++++--------------- 1 file changed, 265 insertions(+), 218 deletions(-) (limited to 'contrib/sendmail/praliases/praliases.c') diff --git a/contrib/sendmail/praliases/praliases.c b/contrib/sendmail/praliases/praliases.c index 8b0057a..59a3c00 100644 --- a/contrib/sendmail/praliases/praliases.c +++ b/contrib/sendmail/praliases/praliases.c @@ -1,5 +1,6 @@ /* - * Copyright (c) 1998 Sendmail, Inc. All rights reserved. + * Copyright (c) 1998-2000 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. @@ -12,74 +13,86 @@ #ifndef lint static char copyright[] = -"@(#) Copyright (c) 1988, 1993\n\ +"@(#) Copyright (c) 1998-2000 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"; -#endif /* not lint */ +#endif /* ! lint */ #ifndef lint -static char sccsid[] = "@(#)praliases.c 8.21 (Berkeley) 12/27/1998"; -#endif /* not lint */ +static char id[] = "@(#)$Id: praliases.c,v 8.59.4.10 2000/07/18 05:41:39 gshapiro Exp $"; +#endif /* ! lint */ + +/* $FreeBSD$ */ + +#include +#include +#include +#include +#ifdef EX_OK +# undef EX_OK /* unistd.h may have another use for this */ +#endif /* EX_OK */ +#include -#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 -#endif #ifndef NOT_SENDMAIL # define NOT_SENDMAIL -#endif -#include -#include -#ifdef NEWDB -# include -# 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 +#endif /* ! NOT_SENDMAIL */ +#include +#include +#include static void praliases __P((char *, int, char **)); -#ifdef NDBM -static void praliases_dbm __P((char *, int, char **)); -#endif + +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; + +extern void syserr __P((const char *, ...)); + 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]; + 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; + snprintf(rnamebuf, sizeof rnamebuf, "%s", pw->pw_name); + } + else + (void) snprintf(rnamebuf, sizeof rnamebuf, "Unknown UID %d", + (int) RealUid); + RunAsUserName = RealUserName = rnamebuf; cfile = _PATH_SENDMAILCF; -#if _FFR_GRAB_ALIASFILE_OPTION while ((ch = getopt(argc, argv, "C:f:")) != -1) -#else - while ((ch = getopt(argc, argv, "f:")) != -1) -#endif { switch ((char)ch) { case 'C': @@ -91,11 +104,7 @@ main(argc, argv) case '?': default: (void)fprintf(stderr, -#if _FFR_GRAB_ALIASFILE_OPTION - "usage: praliases [-C cffile] [-f aliasfile]\n"); -#else - "usage: praliases [-f aliasfile]\n"); -#endif + "usage: praliases [-C cffile] [-f aliasfile]\n"); exit(EX_USAGE); } } @@ -110,8 +119,8 @@ main(argc, argv) if ((cfp = fopen(cfile, "r")) == NULL) { - fprintf(stderr, "praliases: "); - perror(cfile); + fprintf(stderr, "praliases: %s: %s\n", + cfile, errstring(errno)); exit(EX_NOINPUT); } @@ -119,6 +128,10 @@ main(argc, argv) { register char *b, *p; + b = strchr(buf, '\n'); + if (b != NULL) + *b = '\0'; + b = buf; switch (*b++) { @@ -140,15 +153,15 @@ main(argc, argv) } /* this is the A or AliasFile option -- save it */ - if (strlen(b) >= sizeof afilebuf) + if (strlcpy(afilebuf, b, sizeof afilebuf) >= + sizeof afilebuf) { fprintf(stderr, - "AliasFile filename too long: %.30s...\n", + "praliases: AliasFile filename too long: %.30s\n", b); (void) fclose(cfp); exit(EX_CONFIG); } - strcpy(afilebuf, b); b = afilebuf; for (p = b; p != NULL; ) @@ -160,12 +173,50 @@ main(argc, argv) b = p; p = strpbrk(p, " ,/"); + /* find end of spec */ if (p != NULL) - p = strpbrk(p, ",\n"); + { + 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) - *p++ = '\0'; + { + char *e = p - 1; + /* chop trailing whitespace */ + while (isascii(*e) && + isspace(*e) && + e > b) + e--; + *++e = '\0'; + *p++ = '\0'; + } praliases(b, argc, argv); } @@ -175,196 +226,192 @@ main(argc, argv) } (void) fclose(cfp); exit(EX_OK); + /* NOTREACHED */ + return EX_OK; } static void praliases(filename, argc, argv) char *filename; - int argc; + int argc; char **argv; { -#ifdef NEWDB - DB *db; - DBT newdbkey, newdbcontent; - char buf[MAXNAME]; -#endif - char *class; - - class = strchr(filename, ':'); - if (class != NULL) + 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, ':'); + if (colon == 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; + db_name = filename; + db_type = SMDB_TYPE_DEFAULT; } -#ifdef NEWDB - if (strlen(filename) + 4 >= sizeof buf) + else { - fprintf(stderr, "Alias filename too long: %.30s...\n", filename); - exit(EX_USAGE); + *colon = '\0'; + db_name = colon + 1; + db_type = filename; } - (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) + + /* clean off arguments */ + for (;;) { - 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 + 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++; } - else + + if (*db_name == '\0' || (db_type != NULL && *db_type == '\0')) { -#endif -#ifdef NDBM - praliases_dbm(filename, argc, argv); -#endif -#ifdef NEWDB + if (colon != NULL) + *colon = ':'; + fprintf(stderr, "praliases: illegal alias specification: %s\n", + filename); + goto fatal; } -#endif -} -#ifdef NDBM -static void -praliases_dbm(filename, argc, argv) - char *filename; - int argc; - char **argv; -{ - DBM *dbp; - datum content, key; + memset(¶ms, '\0', sizeof params); + params.smdbp_cache_size = 1024 * 1024; - if ((dbp = dbm_open(filename, O_RDONLY, 0)) == NULL) + user_info.smdbu_id = RunAsUid; + user_info.smdbu_group_id = RunAsGid; + 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) { - (void)fprintf(stderr, - "praliases: %s: %s\n", filename, strerror(errno)); - exit(EX_OSFILE); + fprintf(stderr, "praliases: %s: open: %s\n", + db_name, errstring(result)); + goto fatal; } - if (!argc) + + if (argc == 0) { - for (key = dbm_firstkey(dbp); - key.dptr != NULL; key = dbm_nextkey(dbp)) + 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) + { + fprintf(stderr, "praliases: %s: set cursor: %s\n", + db_name, 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.data.size == 2 && + db_key.data.data[0] == '@' && + db_key.data.data[1] == '\0' && + db_value.data.size == 2 && + db_value.data.data[0] == '@' && + db_value.data.data[1] == '\0') + continue; +#endif /* 0 */ + + printf("%.*s:%.*s\n", + (int) db_key.data.size, + (char *) db_key.data.data, + (int) db_value.data.size, + (char *) db_value.data.data); + } + + if (result != SMDBE_OK && result != SMDBE_LAST_ENTRY) { - content = dbm_fetch(dbp, key); - (void)printf("%.*s:%.*s\n", - (int) key.dsize, key.dptr, - (int) content.dsize, content.dptr); + fprintf(stderr, + "praliases: %s: get value at cursor: %s\n", + db_name, errstring(result)); + goto fatal; } } - else + else for (; *argv != NULL; ++argv) { - for (; *argv; ++argv) + memset(&db_key, '\0', sizeof db_key); + memset(&db_value, '\0', sizeof db_value); + db_key.data.data = *argv; + db_key.data.size = strlen(*argv) + 1; + if (database->smdb_get(database, &db_key, + &db_value, 0) == SMDBE_OK) { - /* - ** 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); + printf("%.*s:%.*s\n", + (int) db_key.data.size, + (char *) db_key.data.data, + (int) db_value.data.size, + (char *) db_value.data.data); } + else + printf("%s: No such key\n", (char *) db_key.data.data); } - dbm_close(dbp); -} -#endif -#if !HASSTRERROR + fatal: + if (cursor != NULL) + (void) cursor->smdbc_close(cursor); + if (database != NULL) + (void) database->smdb_close(database); + if (colon != NULL) + *colon = ':'; + return; +} -char * -strerror(eno) - int eno; +/*VARARGS1*/ +void +#ifdef __STDC__ +message(const char *msg, ...) +#else /* __STDC__ */ +message(msg, va_alist) + const char *msg; + va_dcl +#endif /* __STDC__ */ { - 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; + const char *m; + VA_LOCAL_DECL + + m = msg; + if (isascii(m[0]) && isdigit(m[0]) && + isascii(m[1]) && isdigit(m[1]) && + isascii(m[2]) && isdigit(m[2]) && m[3] == ' ') + m += 4; + VA_START(msg); + (void) vfprintf(stderr, m, ap); + VA_END; + (void) fprintf(stderr, "\n"); } -#endif /* !HASSTRERROR */ +/*VARARGS1*/ +void +#ifdef __STDC__ +syserr(const char *msg, ...) +#else /* __STDC__ */ +syserr(msg, va_alist) + const char *msg; + va_dcl +#endif /* __STDC__ */ +{ + const char *m; + VA_LOCAL_DECL + + m = msg; + if (isascii(m[0]) && isdigit(m[0]) && + isascii(m[1]) && isdigit(m[1]) && + isascii(m[2]) && isdigit(m[2]) && m[3] == ' ') + m += 4; + VA_START(msg); + (void) vfprintf(stderr, m, ap); + VA_END; + (void) fprintf(stderr, "\n"); +} -- cgit v1.1