summaryrefslogtreecommitdiff
path: root/usr.sbin
diff options
context:
space:
mode:
authorJacek Masiulaniec <jacekm@cvs.openbsd.org>2008-12-13 19:02:34 +0000
committerJacek Masiulaniec <jacekm@cvs.openbsd.org>2008-12-13 19:02:34 +0000
commitcb5d68f0b67082e9df46e63e2f1c4a70e25687b8 (patch)
treeda58bf5d08252aff32edbbcd340f852138f424af /usr.sbin
parent6cfc00daa1693736af1599785e114026ee99cc0f (diff)
Use mkstemp instead of mkdtemp; also, add more X's.
Do chmod before rename to eliminate short time window when aliases.db is "official" but not readable. Use PATH_ALIASESDB instead of hardcoding "/etc/mail/.." Check db->close return code. ok gilles@ henning@
Diffstat (limited to 'usr.sbin')
-rw-r--r--usr.sbin/smtpd/newaliases.c41
1 files changed, 18 insertions, 23 deletions
diff --git a/usr.sbin/smtpd/newaliases.c b/usr.sbin/smtpd/newaliases.c
index 86ebd77d74d..dacc33f7eca 100644
--- a/usr.sbin/smtpd/newaliases.c
+++ b/usr.sbin/smtpd/newaliases.c
@@ -48,24 +48,21 @@ DB *db;
int
main(int argc, char *argv[])
{
- int ch;
- char pathname[MAXPATHLEN];
char dbname[MAXPATHLEN];
+ int ch;
if (argc != 1)
usage();
- bzero(pathname, MAXPATHLEN);
- snprintf(pathname, MAXPATHLEN, "/etc/mail/aliases.XXXXX");
- if (mkdtemp(pathname) == NULL)
- errx(1, "failed to create temporary directory");
+ if (strlcpy(dbname, PATH_ALIASESDB ".XXXXXXXXXXX", MAXPATHLEN)
+ >= MAXPATHLEN)
+ errx(1, "path truncation");
+ if (mkstemp(dbname) == -1)
+ err(1, "mkstemp");
- bzero(dbname, MAXPATHLEN);
- snprintf(dbname, MAXPATHLEN, "%s/aliases.db", 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;
}
@@ -74,26 +71,24 @@ main(int argc, char *argv[])
goto bad;
}
- db->close(db);
+ 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;
}
- if (chmod(PATH_ALIASESDB, 0644) == -1)
- err(1, "chmod");
-
- if (rmdir(pathname) == -1)
- err(1, "rmdir");
-
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;
}