diff options
author | Gilles Chehade <gilles@cvs.openbsd.org> | 2012-08-26 11:52:49 +0000 |
---|---|---|
committer | Gilles Chehade <gilles@cvs.openbsd.org> | 2012-08-26 11:52:49 +0000 |
commit | a10ddaa6927e9019fd1ac0a1c5de462b3b90427e (patch) | |
tree | c79842606b932be982e9d495ccd45c799d3f88cd | |
parent | 5f5286de0a0de74ab7e362a34220341aa04be063 (diff) |
- define ZLIB_BUFFER_SIZE instead of hardcoding 8192
- check gzdopen() failure
- call gzclose() whenever a failure occurs after gzdopen()
- simplify slightly some checks in compress/uncompress
- create PATH_TEMPORARY in /var/spool/smtpd, chmod 700, owned by _smtpd
- compress_zlib should use PATH_TEMPORARY instread of /tmp as we're
chrooted and this will otherwise lead to a fatal()
ok chl@
-rw-r--r-- | usr.sbin/smtpd/compress_zlib.c | 62 | ||||
-rw-r--r-- | usr.sbin/smtpd/smtpd.c | 4 | ||||
-rw-r--r-- | usr.sbin/smtpd/smtpd.h | 3 | ||||
-rw-r--r-- | usr.sbin/smtpd/util.c | 10 |
4 files changed, 45 insertions, 34 deletions
diff --git a/usr.sbin/smtpd/compress_zlib.c b/usr.sbin/smtpd/compress_zlib.c index 1e68608017b..de7d8488449 100644 --- a/usr.sbin/smtpd/compress_zlib.c +++ b/usr.sbin/smtpd/compress_zlib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: compress_zlib.c,v 1.3 2012/08/26 11:21:28 gilles Exp $ */ +/* $OpenBSD: compress_zlib.c,v 1.4 2012/08/26 11:52:48 gilles Exp $ */ /* * Copyright (c) 2012 Charles Longeau <chl@openbsd.org> @@ -38,6 +38,8 @@ #include "smtpd.h" #include "log.h" +#define ZLIB_BUFFER_SIZE 8192 + static int compress_file_zlib(int, int); static int uncompress_file_zlib(int, int); static size_t compress_buffer_zlib(const char *, size_t, char *, size_t); @@ -50,56 +52,64 @@ struct compress_backend compress_zlib = { uncompress_buffer_zlib }; - static int compress_file_zlib(int fdin, int fdout) { gzFile gzfd; - char buf[8192]; + char buf[ZLIB_BUFFER_SIZE]; int r, w; + int ret = 0; if (fdin == -1 || fdout == -1) return (0); gzfd = gzdopen(fdout, "wb"); + if (gzfd == NULL) + return (0); - while ((r = read(fdin, buf, sizeof(buf)))) { - if (r == -1) - return (0); - + while ((r = read(fdin, buf, sizeof(buf))) > 0) { w = gzwrite(gzfd, buf, r); - if (w == 0 || w != r) - return (0); + if (w != r) + goto end; } - gzclose(gzfd); + if (r == -1) + goto end; - return (1); + ret = 1; + +end: + gzclose(gzfd); + return (ret); } static int uncompress_file_zlib(int fdin, int fdout) { - gzFile gzfd; - int r, w; - char buf[8192]; + gzFile gzfd; + char buf[ZLIB_BUFFER_SIZE]; + int r, w; + int ret = 0; if (fdin == -1 || fdout == -1) return (0); - + gzfd = gzdopen(fdin, "r"); - while ((r = gzread(gzfd, buf, sizeof(buf)))) { - - if (r == -1) - return (0); + if (gzfd == NULL) + return (0); + while ((r = gzread(gzfd, buf, sizeof(buf))) > 0) { w = write(fdout, buf, r); - - if (w == -1 || w != r) - return (0); + if (w != r) + goto end; } - gzclose(gzfd); + if (r == -1) + goto end; - return (1); + ret = 1; + +end: + gzclose(gzfd); + return (ret); } static size_t @@ -107,14 +117,14 @@ compress_buffer_zlib(const char *inbuf, size_t inbuflen, char *outbuf, size_t ou { uLong compress_bound; int ret; - + compress_bound = compressBound((uLongf) inbuflen); if (compress_bound > outbuflen) return (0); ret = compress((Bytef *) outbuf, (uLongf *) &outbuflen, - (const Bytef *) inbuf, (uLong) inbuflen); + (const Bytef *) inbuf, (uLong) inbuflen); return (ret == Z_OK ? outbuflen : 0); } diff --git a/usr.sbin/smtpd/smtpd.c b/usr.sbin/smtpd/smtpd.c index ac2e94d4d9d..2706358c4ee 100644 --- a/usr.sbin/smtpd/smtpd.c +++ b/usr.sbin/smtpd/smtpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: smtpd.c,v 1.164 2012/08/25 23:35:09 chl Exp $ */ +/* $OpenBSD: smtpd.c,v 1.165 2012/08/26 11:52:48 gilles Exp $ */ /* * Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org> @@ -554,6 +554,8 @@ main(int argc, char *argv[]) errx(1, "error in offline directory setup"); if (ckdir(PATH_SPOOL PATH_PURGE, 0700, env->sc_pw->pw_uid, 0, 1) == 0) errx(1, "error in purge directory setup"); + if (ckdir(PATH_SPOOL PATH_TEMPORARY, 0700, env->sc_pw->pw_uid, 0, 1) == 0) + errx(1, "error in purge directory setup"); mvpurge(PATH_SPOOL PATH_INCOMING, PATH_SPOOL PATH_PURGE); diff --git a/usr.sbin/smtpd/smtpd.h b/usr.sbin/smtpd/smtpd.h index 0a984b122a8..e007395e1e8 100644 --- a/usr.sbin/smtpd/smtpd.h +++ b/usr.sbin/smtpd/smtpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: smtpd.h,v 1.338 2012/08/25 23:35:09 chl Exp $ */ +/* $OpenBSD: smtpd.h,v 1.339 2012/08/26 11:52:48 gilles Exp $ */ /* * Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org> @@ -58,6 +58,7 @@ #define PATH_SPOOL "/var/spool/smtpd" #define PATH_OFFLINE "/offline" #define PATH_PURGE "/purge" +#define PATH_TEMPORARY "/temporary" #define PATH_INCOMING "/incoming" #define PATH_ENVELOPES "/envelopes" #define PATH_MESSAGE "/message" diff --git a/usr.sbin/smtpd/util.c b/usr.sbin/smtpd/util.c index 3469d75d390..05b2050c8da 100644 --- a/usr.sbin/smtpd/util.c +++ b/usr.sbin/smtpd/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.73 2012/08/25 23:35:09 chl Exp $ */ +/* $OpenBSD: util.c,v 1.74 2012/08/26 11:52:48 gilles Exp $ */ /* * Copyright (c) 2000,2001 Markus Friedl. All rights reserved. @@ -336,12 +336,10 @@ mktmpfile(void) char path[MAXPATHLEN]; int fd; -#define PATH_TMP "/tmp" + if (ckdir(PATH_TEMPORARY, 0700, env->sc_pw->pw_uid, 0, 0) == 0) + errx(1, "error in %s directory setup", PATH_TEMPORARY); - if (ckdir(PATH_TMP, 0700, env->sc_pw->pw_uid, 0, 0) == 0) - errx(1, "error in /tmp directory setup"); - - if (! bsnprintf(path, sizeof(path), "%s/zlib.XXXXXXXXXX", PATH_TMP)) + if (! bsnprintf(path, sizeof(path), "%s/smtpd.XXXXXXXXXX", PATH_TEMPORARY)) err(1, "snprintf"); if ((fd = mkstemp(path)) == -1) |