summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSunil Nimmagadda <sunil@cvs.openbsd.org>2016-02-12 03:11:17 +0000
committerSunil Nimmagadda <sunil@cvs.openbsd.org>2016-02-12 03:11:17 +0000
commit7ea1dba1eda07129d63faddde6fa45f6a0167902 (patch)
tree69595d4c02c2a7d8279ee90f6ee95da27f46758b
parent66bd78407199b71aa61c3f5fb876a75053400375 (diff)
Cleanup getopt(3) mis-use in sendmail compat code.
Avoid multiple getopt(3) evaluations without optreset set and unspecified behavior of optind = 0 as per POSIX. Instead, iterate over argv the first time for classification. Fixes -portable without any divergence from -current, and all newaliases opts are handled in one place. Looks good to me millert@ Ok gilles@ jung@ eric@
-rw-r--r--usr.sbin/smtpd/makemap.c24
-rw-r--r--usr.sbin/smtpd/smtpctl.c107
2 files changed, 60 insertions, 71 deletions
diff --git a/usr.sbin/smtpd/makemap.c b/usr.sbin/smtpd/makemap.c
index c1c39d20ad2..ce2c7c5c8b2 100644
--- a/usr.sbin/smtpd/makemap.c
+++ b/usr.sbin/smtpd/makemap.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: makemap.c,v 1.62 2016/02/10 09:23:53 gilles Exp $ */
+/* $OpenBSD: makemap.c,v 1.63 2016/02/12 03:11:16 sunil Exp $ */
/*
* Copyright (c) 2008 Gilles Chehade <gilles@poolp.org>
@@ -50,7 +50,7 @@ static int parse_setentry(DB *, int *, char *, size_t, size_t);
static int make_plain(DBT *, char *);
static int make_aliases(DBT *, char *);
static char *conf_aliases(char *);
-static int dump_db(const char *, DBTYPE);
+static int dump_db(const char *, DBTYPE);
struct smtpd smtpd;
struct smtpd *env = &smtpd;
@@ -94,19 +94,25 @@ makemap(int argc, char *argv[])
int ch, dbputs = 0, Uflag = 0;
DBTYPE dbtype = DB_HASH;
char *p;
- int fd = -1;
+ int fd = -1;
log_init(1);
mode = strcmp(__progname, "newaliases") ? P_MAKEMAP : P_NEWALIASES;
conf = CONF_FILE;
type = T_PLAIN;
- opts = "ho:t:d:U";
+ opts = "b:C:d:ho:O:t:U";
if (mode == P_NEWALIASES)
opts = "f:h";
while ((ch = getopt(argc, argv, opts)) != -1) {
switch (ch) {
+ case 'b':
+ if (optarg && strcmp(optarg, "i") == 0)
+ mode = P_NEWALIASES;
+ break;
+ case 'C':
+ break; /* for compatibility */
case 'd':
if (strcmp(optarg, "hash") == 0)
dbtype = DB_HASH;
@@ -123,6 +129,13 @@ makemap(int argc, char *argv[])
case 'o':
oflag = optarg;
break;
+ case 'O':
+ if (strncmp(optarg, "AliasFile=", 10) != 0)
+ break;
+ type = T_ALIASES;
+ p = strchr(optarg, '=');
+ source = ++p;
+ break;
case 't':
if (strcmp(optarg, "aliases") == 0)
type = T_ALIASES;
@@ -169,7 +182,8 @@ makemap(int argc, char *argv[])
if (argc != 0)
usage();
type = T_ALIASES;
- source = conf_aliases(conf);
+ if (source == NULL)
+ source = conf_aliases(conf);
} else {
if (argc != 1)
usage();
diff --git a/usr.sbin/smtpd/smtpctl.c b/usr.sbin/smtpd/smtpctl.c
index 7d064cab8cb..cb377c52691 100644
--- a/usr.sbin/smtpd/smtpctl.c
+++ b/usr.sbin/smtpd/smtpctl.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: smtpctl.c,v 1.146 2016/02/09 10:38:02 gilles Exp $ */
+/* $OpenBSD: smtpctl.c,v 1.147 2016/02/12 03:11:16 sunil Exp $ */
/*
* Copyright (c) 2013 Eric Faurot <eric@openbsd.org>
@@ -67,6 +67,7 @@ static int is_encrypted_fp(FILE *);
static int is_encrypted_buffer(const char *);
static int is_gzip_buffer(const char *);
static FILE *offline_file(void);
+static void sendmail_compat(int, char **);
extern char *__progname;
int sendmail;
@@ -976,77 +977,14 @@ do_uncorrupt(int argc, struct parameter *argv)
int
main(int argc, char **argv)
{
- arglist args;
gid_t gid;
char *argv_mailq[] = { "show", "queue", NULL };
- char *aliases_path = NULL, *p;
- FILE *offlinefp = NULL;
- int ch, i, sendmail_makemap = 0;
-
- gid = getgid();
- if (strcmp(__progname, "sendmail") == 0 ||
- strcmp(__progname, "send-mail") == 0) {
- /*
- * determine whether we are called with flags
- * that should invoke makemap/newaliases.
- */
- opterr = 0;
- while ((ch = getopt(argc, argv, "b:C:O:")) != -1) {
- switch (ch) {
- case 'b':
- if (strcmp(optarg, "i") == 0)
- sendmail_makemap = 1;
- break;
- case 'C':
- break; /* compatibility, not required */
- case 'O':
- if (strncmp(optarg, "AliasFile=", 10) != 0)
- break;
- p = strchr(optarg, '=');
- aliases_path = ++p;
- break;
- }
- }
- opterr = 1;
-
- if (sendmail_makemap) {
- argc -= optind;
- argv += optind;
- optind = 0;
-
- memset(&args, 0, sizeof args);
- addargs(&args, "%s", "makemap");
- for (i = 0; i < argc; i++)
- addargs(&args, "%s", argv[i]);
-
- addargs(&args, "%s", "-taliases");
- if (aliases_path)
- addargs(&args, "%s", aliases_path);
-
- return makemap(args.num, args.list);
- }
- optind = 0;
-
- if (!srv_connect())
- offlinefp = offline_file();
-
- if (setresgid(gid, gid, gid) == -1)
- err(1, "setresgid");
-
- /* we'll reduce further down the road */
- if (pledge("stdio rpath wpath cpath tmppath flock "
- "dns getpw recvfd", NULL) == -1)
- err(1, "pledge");
-
- sendmail = 1;
- return (enqueue(argc, argv, offlinefp));
- } else if (strcmp(__progname, "makemap") == 0 ||
- strcmp(__progname, "newaliases") == 0)
- return makemap(argc, argv);
+ sendmail_compat(argc, argv);
if (geteuid())
errx(1, "need root privileges");
+ gid = getgid();
if (setresgid(gid, gid, gid) == -1)
err(1, "setresgid");
@@ -1105,7 +1043,44 @@ main(int argc, char **argv)
errx(1, "unsupported mode");
return (0);
+}
+
+void
+sendmail_compat(int argc, char **argv)
+{
+ FILE *offlinefp = NULL;
+ gid_t gid;
+ int i;
+
+ if (strcmp(__progname, "sendmail") == 0 ||
+ strcmp(__progname, "send-mail") == 0) {
+ /*
+ * determine whether we are called with flags
+ * that should invoke makemap/newaliases.
+ */
+ for (i = 1; i < argc; i++)
+ if (strncmp(argv[i], "-bi", 3) == 0) {
+ __progname = "newaliases";
+ exit(makemap(argc, argv));
+ }
+ if (!srv_connect())
+ offlinefp = offline_file();
+
+ gid = getgid();
+ if (setresgid(gid, gid, gid) == -1)
+ err(1, "setresgid");
+
+ /* we'll reduce further down the road */
+ if (pledge("stdio rpath wpath cpath tmppath flock "
+ "dns getpw recvfd", NULL) == -1)
+ err(1, "pledge");
+
+ sendmail = 1;
+ exit(enqueue(argc, argv, offlinefp));
+ } else if (strcmp(__progname, "makemap") == 0 ||
+ strcmp(__progname, "newaliases") == 0)
+ exit(makemap(argc, argv));
}
static void