diff options
author | Jacek Masiulaniec <jacekm@cvs.openbsd.org> | 2008-12-17 22:59:45 +0000 |
---|---|---|
committer | Jacek Masiulaniec <jacekm@cvs.openbsd.org> | 2008-12-17 22:59:45 +0000 |
commit | a1ec3abf9b11ef5ffcc78d91109fb89fe3c8eb05 (patch) | |
tree | 3d6844ea863f54680b74bae797e3699454b0e119 /usr.sbin | |
parent | 4b9d3a5b4f2e3fce34e8d068a7852c162598c370 (diff) |
Merge newaliases into makemap.
ok gilles@
Diffstat (limited to 'usr.sbin')
-rw-r--r-- | usr.sbin/smtpd/Makefile | 4 | ||||
-rw-r--r-- | usr.sbin/smtpd/makemap.8 | 60 | ||||
-rw-r--r-- | usr.sbin/smtpd/makemap.c | 258 | ||||
-rw-r--r-- | usr.sbin/smtpd/makemap/Makefile | 5 | ||||
-rw-r--r-- | usr.sbin/smtpd/newaliases.8 | 48 | ||||
-rw-r--r-- | usr.sbin/smtpd/newaliases.c | 219 | ||||
-rw-r--r-- | usr.sbin/smtpd/newaliases/Makefile | 15 |
7 files changed, 187 insertions, 422 deletions
diff --git a/usr.sbin/smtpd/Makefile b/usr.sbin/smtpd/Makefile index 4159055631e..58ecb1aed87 100644 --- a/usr.sbin/smtpd/Makefile +++ b/usr.sbin/smtpd/Makefile @@ -1,8 +1,8 @@ -# $OpenBSD: Makefile,v 1.7 2008/12/05 03:28:37 gilles Exp $ +# $OpenBSD: Makefile,v 1.8 2008/12/17 22:59:36 jacekm Exp $ .include <bsd.own.mk> -SUBDIR = makemap newaliases smtpd smtpctl +SUBDIR = makemap smtpd smtpctl distribution: ${INSTALL} -C -o root -g wheel -m 0644 ${.CURDIR}/smtpd.conf \ diff --git a/usr.sbin/smtpd/makemap.8 b/usr.sbin/smtpd/makemap.8 index 110a32a948a..ae3b737d799 100644 --- a/usr.sbin/smtpd/makemap.8 +++ b/usr.sbin/smtpd/makemap.8 @@ -1,4 +1,4 @@ -.\" $OpenBSD: makemap.8,v 1.2 2008/12/04 06:22:05 jmc Exp $ +.\" $OpenBSD: makemap.8,v 1.3 2008/12/17 22:59:36 jacekm Exp $ .\" Copyright (c) 1998-2002 Sendmail, Inc. and its suppliers. .\" All rights reserved. .\" Copyright (c) 1988, 1991, 1993 @@ -11,46 +11,58 @@ .\" .\" $Sendmail: makemap.8,v 8.30 2002/06/27 23:41:04 gshapiro Exp $ .\" -.Dd $Mdocdate: December 4 2008 $ +.Dd $Mdocdate: December 17 2008 $ .Dt MAKEMAP 8 .Os .Sh NAME -.Nm makemap +.Nm makemap , +.Nm newaliases .Nd create database maps for smtpd .Sh SYNOPSIS .Nm makemap -.Ar mapdesc +.Ar file +.Nm newaliases .Sh DESCRIPTION .Nm creates the database maps used by the keyed map lookups in .Xr smtpd 8 . -It reads input from the -.Ar mapdesc -file and outputs them to -.Ar mapdesc.db . +It reads input from +.Ar file +and outputs them to +.Ar file.db . .Pp In all cases, .Nm -reads lines from the standard input consisting of two -words separated by whitespace. +reads lines from +.Ar file +consisting of words separated by whitespace. The first is the database key, -the second is the value. +the remaining specify mapped value. +.Pp +.Ex -std makemap .Pp -Note: -use -.Xr newaliases 8 -to create the aliases database, not -.Nm , -as the input formats differ for historical reasons. +.Nm newaliases +rebuilds the random access database for the mail aliases file +.Pa /etc/mail/aliases . +It must be run each time this file is changed +in order for the change to take effect. +.Pp +.Ex -std newaliases +.Sh FILES +.Bl -tag -width "/etc/mail/aliasesXXX" -compact +.It Pa /etc/mail/aliases +The mail aliases file. +.El .Sh SEE ALSO .Xr editmap 8 , -.Xr newaliases 8 , -.Xr smtpd 8 +.Xr smtpd 8 , +.Xr aliases 5 , +.Xr mailer.conf 5 .Sh HISTORY -This +These .Nm -command first appeared in +and +.Nm newaliases +commands first appeared in .Ox 4.5 -as a replacement for the -.Nm -command that was shipped with sendmail. +as the replacement for equivalent commands shipped with sendmail. diff --git a/usr.sbin/smtpd/makemap.c b/usr.sbin/smtpd/makemap.c index a291b805301..d8870293d9e 100644 --- a/usr.sbin/smtpd/makemap.c +++ b/usr.sbin/smtpd/makemap.c @@ -1,3 +1,5 @@ +/* $OpenBSD: makemap.c,v 1.3 2008/12/17 22:59:36 jacekm Exp $ */ + /* * Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org> * @@ -32,7 +34,6 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <sysexits.h> #include <unistd.h> #include <util.h> @@ -40,85 +41,85 @@ extern char *__progname; -static int usage(void); -int parse_map(const char *); -int parse_entry(char *, size_t, size_t); +__dead void usage(void); +int parse_map(char *); +int parse_entry(char *, size_t, size_t); DB *db; +char *source; + +enum program { + P_MAKEMAP, + P_NEWALIASES +} mode; int main(int argc, char *argv[]) { - int ch; - char pathname[MAXPATHLEN]; char dbname[MAXPATHLEN]; - char output[MAXPATHLEN]; - - while ((ch = getopt(argc, argv, "")) != -1) { - switch (ch) { - default: - return usage(); - } + char dest[MAXPATHLEN]; + + mode = strcmp(__progname, "newaliases") ? P_MAKEMAP : P_NEWALIASES; + + switch (mode) { + case P_MAKEMAP: + if (argc != 2) + usage(); + source = argv[1]; + break; + case P_NEWALIASES: + if (argc != 1) + usage(); + if (geteuid()) + errx(1, "need root privileges"); + source = PATH_ALIASES; + break; + default: + abort(); } - argc -= optind; - argv += optind; - if (argc != 1) - return usage(); + if (! bsnprintf(dbname, MAXPATHLEN, "%s.db.XXXXXXXXXXX", source)) + errx(1, "path too long"); + if (mkstemp(dbname) == -1) + err(1, "mkstemp"); - bzero(pathname, MAXPATHLEN); - snprintf(pathname, MAXPATHLEN, "%s.XXXXX", argv[0]); - if (mkdtemp(pathname) == NULL) - errx(1, "failed to create temporary directory"); - - bzero(dbname, MAXPATHLEN); - snprintf(dbname, MAXPATHLEN, "%s/map", pathname); - db = dbopen(dbname, O_CREAT|O_EXLOCK|O_RDWR|O_SYNC, 0644, DB_HASH, - NULL); + db = dbopen(dbname, O_EXLOCK|O_RDWR|O_SYNC, 0644, DB_HASH, NULL); if (db == NULL) { - warn("dbopen"); + warn("dbopen: %s", dbname); goto bad; } - if (! parse_map(argv[0])) { - warnx("syntax error in aliases file"); + if (! parse_map(source)) + goto bad; + + if (db->close(db) == -1) { + warn("dbclose: %s", dbname); goto bad; } - db->close(db); + if (chmod(dbname, 0644) == -1) { + warn("chmod: %s", dbname); + goto bad; + } - snprintf(output, MAXPATHLEN, "%s.db", argv[0]); + if (! bsnprintf(dest, MAXPATHLEN, "%s.db", source)) { + warnx("path too long"); + goto bad; + } - if (rename(dbname, output) == -1) { + if (rename(dbname, dest) == -1) { warn("rename"); goto bad; } - if (chmod(output, 0644) == -1) - err(1, "chmod"); - - if (rmdir(pathname) == -1) - err(1, "rmdir"); - - return EX_OK; + return 0; bad: - if (dbname[0] != '\0') - if (unlink(dbname) == -1) - err(1, "unlink: %s", dbname); - if (rmdir(pathname) == -1) - err(1, "rmdir: %s", pathname); + unlink(dbname); return 1; } -static int -usage(void) -{ - fprintf(stderr, "usage: %s filename\n", __progname); - return EX_USAGE; -} - int -parse_map(const char *filename) +parse_map(char *filename) { FILE *fp; char *line; @@ -127,14 +128,19 @@ parse_map(const char *filename) char delim[] = { '\\', '\\', '#' }; fp = fopen(filename, "r"); - if (fp == NULL) - errx(1, "failed to open aliases file"); - + if (fp == NULL) { + warn("%s", filename); + return 0; + } while ((line = fparseln(fp, &len, &lineno, delim, 0)) != NULL) { if (len == 0) continue; - parse_entry(line, len, lineno); + if (! parse_entry(line, len, lineno)) { + free(line); + fclose(fp); + return 0; + } free(line); } @@ -146,90 +152,116 @@ int parse_entry(char *line, size_t len, size_t lineno) { char *name; - char *delim; char *rcpt; + char *endp; char *subrcpt; - struct alias alias; - int ret; DBT key; DBT val; name = line; - while (*name && isspace(*name)) - ++name; - - rcpt = delim = strchr(name, ' '); + switch (mode) { + case P_MAKEMAP: + rcpt = strchr(line, ' '); + if (rcpt == NULL) + rcpt = strchr(line, '\t'); + break; + case P_NEWALIASES: + rcpt = strchr(line, ':'); + break; + default: + abort(); + } if (rcpt == NULL) - rcpt = delim = strchr(name, '\t'); - if (rcpt == NULL || name == rcpt) goto bad; + *rcpt++ = '\0'; - - *delim-- = 0; - while (isspace(*delim)) - *delim-- = '\0'; - rcpt++; - while (*rcpt && isspace(*rcpt)) - ++rcpt; - if (*rcpt == '\0') + /* name: strip initial whitespace. */ + while (isspace(*name)) + ++name; + if (*name == '\0') goto bad; - /* At this point, name points to nul-terminate name */ - for (; (subrcpt = strsep(&rcpt, ",")) != NULL;) { - while (*subrcpt && isspace(*subrcpt)) - ++subrcpt; - if (*subrcpt == '\0') - continue; - - delim = subrcpt + strlen(subrcpt); - delim--; - while (isspace(*delim)) - *delim-- = '\0'; + /* name: strip trailing whitespace. */ + endp = name + strlen(name) - 1; + while (name < endp && isspace(*endp)) + *endp-- = '\0'; + + /* Check for dups. */ + key.data = name; + key.size = strlen(name) + 1; + if (db->get(db, &key, &val, 0) == 0) { + warnx("%s:%zd: duplicate entry for %s", source, lineno, + key.data); + return 0; + } - key.data = name; - key.size = strlen(name) + 1; + /* At this point name and rcpt are non-zero nul-terminated strings. */ + while ((subrcpt = strsep(&rcpt, ",")) != NULL) { + struct alias alias; + void *p; - if ((ret = db->get(db, &key, &val, 0)) == -1) - errx(1, "db->get()"); + /* subrcpt: strip initial whitespace. */ + while (isspace(*subrcpt)) + ++subrcpt; + if (*subrcpt == '\0') + goto bad; - if (ret == 1) { - val.data = NULL; - val.size = 0; - } + /* subrcpt: strip trailing whitespace. */ + endp = subrcpt + strlen(subrcpt) - 1; + while (subrcpt < endp && isspace(*endp)) + *endp-- = '\0'; if (! alias_parse(&alias, subrcpt)) goto bad; - if (val.size == 0) { - val.size = sizeof(struct alias); - val.data = &alias; - - if ((ret = db->put(db, &key, &val, 0)) == -1) - errx(1, "db->get()"); + key.data = name; + key.size = strlen(name) + 1; + val.data = NULL; + val.size = 0; + if (db->get(db, &key, &val, 0) == -1) { + warn("dbget"); + return 0; } - else { - void *p; - p = calloc(val.size + sizeof(alias), 1); - if (p == NULL) - errx(1, "calloc: memory exhausted"); - memcpy(p, val.data, val.size); - memcpy((u_int8_t *)p + val.size, &alias, sizeof(alias)); - - val.data = p; - val.size += sizeof(alias); - - if ((ret = db->put(db, &key, &val, 0)) == -1) - errx(1, "db->get()"); + p = calloc(1, val.size + sizeof(struct alias)); + if (p == NULL) { + warn("calloc"); + return 0; + } + memcpy(p, val.data, val.size); + memcpy((u_int8_t *)p + val.size, &alias, sizeof(struct alias)); + val.data = p; + val.size += sizeof(struct alias); + if (db->put(db, &key, &val, 0) == -1) { + warn("dbput"); free(p); + return 0; } - db->sync(db, 0); + + free(p); } return 1; bad: - warnx("line %zd: invalid entry: %s", lineno, line); + /* The actual line is not printed; it may be mangled by above code. */ + warnx("%s:%zd: invalid entry", source, lineno); return 0; } + +void +usage(void) +{ + switch (mode) { + case P_MAKEMAP: + fprintf(stderr, "usage: %s file\n", __progname); + break; + case P_NEWALIASES: + fprintf(stderr, "usage: %s\n", __progname); + break; + default: + abort(); + } + exit(1); +} diff --git a/usr.sbin/smtpd/makemap/Makefile b/usr.sbin/smtpd/makemap/Makefile index 00bb6db2290..052439431f1 100644 --- a/usr.sbin/smtpd/makemap/Makefile +++ b/usr.sbin/smtpd/makemap/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.2 2008/12/11 23:04:45 gilles Exp $ +# $OpenBSD: Makefile,v 1.3 2008/12/17 22:59:39 jacekm Exp $ .PATH: ${.CURDIR}/.. @@ -10,6 +10,9 @@ BINMODE?=555 BINDIR= /usr/bin MAN= makemap.8 +LINKS= ${BINDIR}/makemap ${BINDIR}/newaliases +MLINKS= makemap.8 newaliases.8 + SRCS= makemap.c aliases.c map.c log.c util.c LDFLAGS= -lutil .include <bsd.prog.mk> diff --git a/usr.sbin/smtpd/newaliases.8 b/usr.sbin/smtpd/newaliases.8 deleted file mode 100644 index a5b5b99135c..00000000000 --- a/usr.sbin/smtpd/newaliases.8 +++ /dev/null @@ -1,48 +0,0 @@ -.\" $OpenBSD: newaliases.8,v 1.2 2008/12/04 06:22:05 jmc Exp $ -.\" Copyright (c) 1998-2001 Sendmail, Inc. and its suppliers. -.\" All rights reserved. -.\" Copyright (c) 1983, 1997 Eric P. Allman. All rights reserved. -.\" Copyright (c) 1985, 1990, 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. -.\" -.\" -.\" $Sendmail: newaliases.1,v 8.19 2001/10/10 03:23:17 ca Exp $ -.\" -.Dd $Mdocdate: December 4 2008 $ -.Dt NEWALIASES 8 -.Os -.Sh NAME -.Nm newaliases -.Nd rebuild the database for the mail aliases file -.Sh SYNOPSIS -.Nm newaliases -.Sh DESCRIPTION -.Nm -rebuilds the random access database for the mail aliases file -.Pa /etc/mail/aliases . -It must be run each time this file is changed -in order for the change to take effect. -.Pp -.Ex -std newaliases -.Sh FILES -.Bl -tag -width "/etc/mail/aliasesXXX" -compact -.It Pa /etc/mail/aliases -The mail aliases file. -.El -.Sh SEE ALSO -.Xr aliases 5 , -.Xr mailer.conf 5 , -.Xr makemap 8 , -.Xr smtpd 8 -.Sh HISTORY -This -.Nm -command first appeared in -.Ox 4.5 -as a replacement for the -.Nm -command that was shipped with sendmail. diff --git a/usr.sbin/smtpd/newaliases.c b/usr.sbin/smtpd/newaliases.c deleted file mode 100644 index 5634541074a..00000000000 --- a/usr.sbin/smtpd/newaliases.c +++ /dev/null @@ -1,219 +0,0 @@ -/* - * Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org> - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <sys/types.h> -#include <sys/stat.h> -#include <sys/tree.h> -#include <sys/queue.h> -#include <sys/param.h> - -#include <sys/socket.h> - -#include <ctype.h> -#include <db.h> -#include <err.h> -#include <errno.h> -#include <event.h> -#include <fcntl.h> -#include <limits.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> -#include <util.h> - -#include "smtpd.h" - -extern char *__progname; - -__dead void usage(void); -int parse_aliases(const char *); -int parse_entry(char *, size_t, size_t); - -DB *db; - -int -main(int argc, char *argv[]) -{ - char dbname[MAXPATHLEN]; - int ch; - - if (argc != 1) - usage(); - - if (strlcpy(dbname, PATH_ALIASESDB ".XXXXXXXXXXX", MAXPATHLEN) - >= MAXPATHLEN) - errx(1, "path truncation"); - if (mkstemp(dbname) == -1) - err(1, "mkstemp"); - - db = dbopen(dbname, O_EXLOCK|O_RDWR|O_SYNC, 0644, DB_HASH, NULL); - if (db == NULL) { - warn("dbopen: %s", dbname); - goto bad; - } - - if (! parse_aliases(PATH_ALIASES)) - goto bad; - - if (db->close(db) == -1) { - warn("dbclose: %s", dbname); - goto bad; - } - - if (chmod(dbname, 0644) == -1) { - warn("chmod: %s", dbname); - goto bad; - } - - if (rename(dbname, PATH_ALIASESDB) == -1) { - warn("rename"); - goto bad; - } - - return 0; -bad: - unlink(dbname); - return 1; -} - -int -parse_aliases(const char *filename) -{ - FILE *fp; - char *line; - size_t len; - size_t lineno = 0; - char delim[] = { '\\', '\\', '#' }; - - fp = fopen(filename, "r"); - if (fp == NULL) { - warn("%s", filename); - return 0; - } - - while ((line = fparseln(fp, &len, &lineno, delim, 0)) != NULL) { - if (len == 0) - continue; - if (! parse_entry(line, len, lineno)) { - free(line); - fclose(fp); - return 0; - } - free(line); - } - - fclose(fp); - return 1; -} - -int -parse_entry(char *line, size_t len, size_t lineno) -{ - char *name; - char *rcpt; - char *endp; - char *subrcpt; - DBT key; - DBT val; - - name = line; - rcpt = strchr(line, ':'); - if (rcpt == NULL) - goto bad; - *rcpt++ = '\0'; - - /* name: strip initial whitespace. */ - while (isspace(*name)) - ++name; - if (*name == '\0') - goto bad; - - /* name: strip trailing whitespace. */ - endp = name + strlen(name) - 1; - while (name < endp && isspace(*endp)) - *endp-- = '\0'; - - /* Check for dups. */ - key.data = name; - key.size = strlen(name) + 1; - if (db->get(db, &key, &val, 0) == 0) { - warnx("%s:%zd: duplicate entry for %s", PATH_ALIASES, lineno, - key.data); - return 0; - } - - /* At this point name and rcpt are non-zero nul-terminated strings. */ - while ((subrcpt = strsep(&rcpt, ",")) != NULL) { - struct alias alias; - void *p; - - /* subrcpt: strip initial whitespace. */ - while (isspace(*subrcpt)) - ++subrcpt; - if (*subrcpt == '\0') - goto bad; - - /* subrcpt: strip trailing whitespace. */ - endp = subrcpt + strlen(subrcpt) - 1; - while (subrcpt < endp && isspace(*endp)) - *endp-- = '\0'; - - if (! alias_parse(&alias, subrcpt)) - goto bad; - - key.data = name; - key.size = strlen(name) + 1; - val.data = NULL; - val.size = 0; - if (db->get(db, &key, &val, 0) == -1) { - warn("dbget"); - return 0; - } - - p = calloc(1, val.size + sizeof(struct alias)); - if (p == NULL) { - warn("calloc"); - return 0; - } - memcpy(p, val.data, val.size); - memcpy((u_int8_t *)p + val.size, &alias, sizeof(struct alias)); - - val.data = p; - val.size += sizeof(struct alias); - if (db->put(db, &key, &val, 0) == -1) { - warn("dbput"); - free(p); - return 0; - } - - free(p); - } - - return 1; - -bad: - /* The actual line is not printed; it may be mangled by above code. */ - warnx("%s:%zd: invalid entry", PATH_ALIASES, lineno); - return 0; -} - -void -usage(void) -{ - fprintf(stderr, "usage: %s\n", __progname); - exit(1); -} diff --git a/usr.sbin/smtpd/newaliases/Makefile b/usr.sbin/smtpd/newaliases/Makefile deleted file mode 100644 index 9322dc11cda..00000000000 --- a/usr.sbin/smtpd/newaliases/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -# $OpenBSD: Makefile,v 1.2 2008/12/11 23:04:45 gilles Exp $ - -.PATH: ${.CURDIR}/.. - -PROG= newaliases -BINOWN= root - -BINMODE?=555 - -BINDIR= /usr/bin -MAN= newaliases.8 - -SRCS= newaliases.c aliases.c map.c log.c util.c -LDFLAGS= -lutil -.include <bsd.prog.mk> |