commit 27541da87a2e82efb16eaac56bb5c10e12859b29 from: Martijn van Duren date: Sun Jun 30 14:22:25 2019 UTC Remove log.[ch] now that we have stderr logging commit - c826412d86123fb2346635710aabec82f8e33946 commit + 27541da87a2e82efb16eaac56bb5c10e12859b29 blob - 7aadff031bac46191b5530f5a06610c9f867a240 blob + e621fb18c804aeddd0113a789eeeb8e34d0e7423 --- Makefile +++ Makefile @@ -2,7 +2,7 @@ PROG= filter-dkim BINDIR= /usr/libexec/smtpd/ -SRCS+= main.c log.c smtp_proc.c +SRCS+= main.c smtp_proc.c CFLAGS+= -g3 -O0 LDADD+= -levent -lcrypto blob - 7ec8ca42e18d1c57b84e27a564c23ff352c267cf (mode 644) blob + /dev/null --- log.c +++ /dev/null @@ -1,218 +0,0 @@ -/* $OpenBSD: log.c,v 1.20 2017/03/21 12:06:56 bluhm Exp $ */ - -/* - * Copyright (c) 2003, 2004 Henning Brauer - * - * 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 -#include -#include -#include -#include -#include -#include - -static int debug; -static int verbose; -const char *log_procname; - -void log_init(int, int); -void log_procinit(const char *); -void log_setverbose(int); -int log_getverbose(void); -void log_warn(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); -void log_warnx(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); -void log_info(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); -void log_debug(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); -void logit(int, const char *, ...) - __attribute__((__format__ (printf, 2, 3))); -void vlog(int, const char *, va_list) - __attribute__((__format__ (printf, 2, 0))); -__dead void fatal(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); -__dead void fatalx(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); - -void -log_init(int n_debug, int facility) -{ - extern char *__progname; - - debug = n_debug; - verbose = n_debug; - log_procinit(__progname); - - if (!debug) - openlog(__progname, LOG_PID | LOG_NDELAY, facility); - - tzset(); -} - -void -log_procinit(const char *procname) -{ - if (procname != NULL) - log_procname = procname; -} - -void -log_setverbose(int v) -{ - verbose = v; -} - -int -log_getverbose(void) -{ - return (verbose); -} - -void -logit(int pri, const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - vlog(pri, fmt, ap); - va_end(ap); -} - -void -vlog(int pri, const char *fmt, va_list ap) -{ - char *nfmt; - int saved_errno = errno; - - if (debug) { - /* best effort in out of mem situations */ - if (asprintf(&nfmt, "%s\n", fmt) == -1) { - vfprintf(stderr, fmt, ap); - fprintf(stderr, "\n"); - } else { - vfprintf(stderr, nfmt, ap); - free(nfmt); - } - fflush(stderr); - } else - vsyslog(pri, fmt, ap); - - errno = saved_errno; -} - -void -log_warn(const char *emsg, ...) -{ - char *nfmt; - va_list ap; - int saved_errno = errno; - - /* best effort to even work in out of memory situations */ - if (emsg == NULL) - logit(LOG_ERR, "%s", strerror(saved_errno)); - else { - va_start(ap, emsg); - - if (asprintf(&nfmt, "%s: %s", emsg, - strerror(saved_errno)) == -1) { - /* we tried it... */ - vlog(LOG_ERR, emsg, ap); - logit(LOG_ERR, "%s", strerror(saved_errno)); - } else { - vlog(LOG_ERR, nfmt, ap); - free(nfmt); - } - va_end(ap); - } - - errno = saved_errno; -} - -void -log_warnx(const char *emsg, ...) -{ - va_list ap; - - va_start(ap, emsg); - vlog(LOG_ERR, emsg, ap); - va_end(ap); -} - -void -log_info(const char *emsg, ...) -{ - va_list ap; - - va_start(ap, emsg); - vlog(LOG_INFO, emsg, ap); - va_end(ap); -} - -void -log_debug(const char *emsg, ...) -{ - va_list ap; - - if (verbose > 1) { - va_start(ap, emsg); - vlog(LOG_DEBUG, emsg, ap); - va_end(ap); - } -} - -static void -vfatalc(int code, const char *emsg, va_list ap) -{ - static char s[BUFSIZ]; - const char *sep; - - if (emsg != NULL) { - (void)vsnprintf(s, sizeof(s), emsg, ap); - sep = ": "; - } else { - s[0] = '\0'; - sep = ""; - } - if (code) - logit(LOG_CRIT, "%s: %s%s%s", - log_procname, s, sep, strerror(code)); - else - logit(LOG_CRIT, "%s%s%s", log_procname, sep, s); -} - -void -fatal(const char *emsg, ...) -{ - va_list ap; - - va_start(ap, emsg); - vfatalc(errno, emsg, ap); - va_end(ap); - exit(1); -} - -void -fatalx(const char *emsg, ...) -{ - va_list ap; - - va_start(ap, emsg); - vfatalc(0, emsg, ap); - va_end(ap); - exit(1); -} blob - 22bb416439467fde6a3c3e58209877edd7fdf468 (mode 644) blob + /dev/null --- log.h +++ /dev/null @@ -1,46 +0,0 @@ -/* $OpenBSD: log.h,v 1.8 2018/04/26 20:57:59 eric Exp $ */ - -/* - * Copyright (c) 2003, 2004 Henning Brauer - * - * 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. - */ - -#ifndef LOG_H -#define LOG_H - -#include -#include - -void log_init(int, int); -void log_procinit(const char *); -void log_setverbose(int); -int log_getverbose(void); -void log_warn(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); -void log_warnx(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); -void log_info(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); -void log_debug(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); -void logit(int, const char *, ...) - __attribute__((__format__ (printf, 2, 3))); -void vlog(int, const char *, va_list) - __attribute__((__format__ (printf, 2, 0))); -__dead void fatal(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); -__dead void fatalx(const char *, ...) - __attribute__((__format__ (printf, 1, 2))); - -#endif /* LOG_H */ blob - be71a9bd0e73e19d1841154579e31e506362fa4e blob + 0d1c69f72c27f74d37a5e6d2083227dea8b7cf05 --- main.c +++ main.c @@ -30,7 +30,6 @@ #include #include -#include "log.h" #include "smtp_proc.h" struct dkim_signature { @@ -202,18 +201,17 @@ main(int argc, char *argv[]) usage(); } } - log_init(debug, LOG_MAIL); OpenSSL_add_all_digests(); if ((hash_md = EVP_get_digestbyname(hashalg)) == NULL) - fatalx("Can't find hash: %s", hashalg); + errx(1, "Can't find hash: %s", hashalg); /* * fattr required for tmpfile. * Can hopefully be removed in the future */ if (pledge("fattr tmppath stdio", NULL) == -1) - fatal("pledge"); + err(1, "pledge"); if (domain == NULL || selector == NULL || pkey == NULL) usage(); @@ -251,7 +249,7 @@ dkim_dataline(char *type, int version, struct timespec return; session->token = token; } else if (session->token != token) - fatalx("Token incorrect"); + errx(1, "Token incorrect"); if (session->err) return; @@ -284,7 +282,7 @@ dkim_commit(char *type, int version, struct timespec * search.reqid = reqid; if ((session = RB_FIND(dkim_sessions, &dkim_sessions, &search)) == NULL) - fatalx("Commit on undefined session"); + errx(1, "Commit on undefined session"); if (session->err) smtp_filter_disconnect(session->reqid, session->token, @@ -302,7 +300,7 @@ dkim_session_new(uint64_t reqid) struct dkim_signature *signature; if ((session = calloc(1, sizeof(*session))) == NULL) - fatal(NULL); + err(1, NULL); session->reqid = reqid; if ((session->origf = tmpfile()) == NULL) { @@ -344,7 +342,7 @@ dkim_session_new(uint64_t reqid) return NULL; } if (RB_INSERT(dkim_sessions, &dkim_sessions, session) != NULL) - fatalx("session already registered"); + errx(1, "session already registered"); return session; } @@ -414,14 +412,14 @@ void dkim_err(struct dkim_session *session, char *msg) { session->err = 1; - log_warn("%s", msg); + warn("%s", msg); } void dkim_errx(struct dkim_session *session, char *msg) { session->err = 1; - log_warnx("%s", msg); + warnx("%s", msg); } void @@ -524,14 +522,14 @@ dkim_parse_header(struct dkim_session *session, char * session->headers[lastheader] = htmp; if (canonheader == CANON_SIMPLE) { if (strlcat(htmp, "\r\n", linelen) >= linelen) - fatalx("Missized header"); + errx(1, "Missized header"); } else if (canonheader == CANON_RELAXED && (tmp = strchr(session->headers[lastheader], ':')) != NULL && tmp[1] == '\0') line++; if (strlcat(htmp, line, linelen) >= linelen) - fatalx("Missized header"); + errx(1, "Missized header"); } } @@ -829,7 +827,7 @@ dkim_signature_printf(struct dkim_session *session, ch va_start(ap, fmt); if ((len = vsnprintf(sig->signature + sig->len, sig->size - sig->len, fmt, ap)) >= sig->size - sig->len) - fatalx("Miscalculated header size"); + errx(1, "Miscalculated header size"); } sig->len += len; va_end(ap); blob - e522c7d28e3357f5421e4b30c01507bb1c7b64ae blob + e6708b1ce7cb46802b1c4c47df2df0f9dc92c4a7 --- smtp_proc.c +++ smtp_proc.c @@ -17,6 +17,7 @@ #include #include +#include #include #include #include @@ -28,7 +29,6 @@ #include #include -#include "log.h" #include "smtp_proc.h" #define NITEMS(x) (sizeof(x) / sizeof(*x)) @@ -125,13 +125,12 @@ smtp_run(int debug) smtp_printf("register|ready\n"); ready = 1; - log_init(debug, LOG_MAIL); event_set(&stdinev, STDIN_FILENO, EV_READ | EV_PERSIST, smtp_newline, &stdinev); event_add(&stdinev, NULL); if (fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK) == -1) - fatal("fcntl"); + err(1, "fcntl"); event_dispatch(); } @@ -156,12 +155,12 @@ smtp_getline(char ** restrict buf, size_t * restrict s *size = sepoff - rsoff + 1; *buf = realloc(*buf, sepoff - rsoff + 1); if (*buf == NULL) - fatal(NULL); + err(1, NULL); } sep[0] = '\0'; strlen = strlcpy(*buf, rbuf + rsoff, *size); if (strlen >= *size) - fatalx("copy buffer too small"); + errx(1, "copy buffer too small"); rsoff = sepoff + 1; return strlen; } @@ -175,7 +174,7 @@ smtp_getline(char ** restrict buf, size_t * restrict s /* If we still can't fill alloc some new memory. */ if (rbsize - reoff < 1500) { if ((rbuf = realloc(rbuf, rbsize + 4096)) == NULL) - fatal(NULL); + err(1, NULL); rbsize += 4096; } nread = read(STDIN_FILENO, rbuf + reoff, rbsize - reoff); @@ -202,46 +201,46 @@ smtp_newline(int fd, short event, void *arg) while ((linelen = smtp_getline(&line, &linesize)) > 0) { if (dupsize < linesize) { if ((linedup = realloc(linedup, linesize)) == NULL) - fatal(NULL); + err(1, NULL); dupsize = linesize; } strlcpy(linedup, line, dupsize); type = line; if ((start = strchr(type, '|')) == NULL) - fatalx("Invalid line received: missing version: %s", linedup); + errx(1, "Invalid line received: missing version: %s", linedup); start++[0] = '\0'; if ((end = strchr(start, '|')) == NULL) - fatalx("Invalid line received: missing time: %s", linedup); + errx(1, "Invalid line received: missing time: %s", linedup); end++[0] = '\0'; if (strcmp(start, "1") != 0) - fatalx("Unsupported protocol received: %s: %s", start, linedup); + errx(1, "Unsupported protocol received: %s: %s", start, linedup); version = 1; start = end; if ((direction = strchr(start, '|')) == NULL) - fatalx("Invalid line received: missing direction: %s", linedup); + errx(1, "Invalid line received: missing direction: %s", linedup); direction++[0] = '\0'; tm.tv_sec = (time_t) strtoull(start, &end, 10); tm.tv_nsec = 0; if (start[0] == '\0' || (end[0] != '\0' && end[0] != '.')) - fatalx("Invalid line received: invalid timestamp: %s", linedup); + errx(1, "Invalid line received: invalid timestamp: %s", linedup); if (end[0] == '.') { start = end + 1; tm.tv_nsec = strtol(start, &end, 10); if (start[0] == '\0' || end[0] != '\0') - fatalx("Invalid line received: invalid " + errx(1, "Invalid line received: invalid " "timestamp: %s", linedup); for (i = 9 - (end - start); i > 0; i--) tm.tv_nsec *= 10; } if ((phase = strchr(direction, '|')) == NULL) - fatalx("Invalid line receieved: missing phase: %s", linedup); + errx(1, "Invalid line receieved: missing phase: %s", linedup); phase++[0] = '\0'; if ((start = strchr(phase, '|')) == NULL) - fatalx("Invalid line received: missing reqid: %s", linedup); + errx(1, "Invalid line received: missing reqid: %s", linedup); start++[0] = '\0'; reqid = strtoull(start, ¶ms, 16); if (start[0] == '|' || (params[0] != '|' & params[0] != '\0')) - fatalx("Invalid line received: invalid reqid: %s", linedup); + errx(1, "Invalid line received: invalid reqid: %s", linedup); params++; for (i = 0; i < NITEMS(smtp_callbacks); i++) { @@ -251,14 +250,14 @@ smtp_newline(int fd, short event, void *arg) break; } if (i == NITEMS(smtp_callbacks)) { - fatalx("Invalid line received: received unregistered " + errx(1, "Invalid line received: received unregistered " "%s: %s: %s", type, phase, linedup); } if (strcmp(type, "filter") == 0) { start = params; token = strtoull(start, ¶ms, 16); if (start[0] == '|' || params[0] != '|') - fatalx("Invalid line received: invalid token: %s", linedup); + errx(1, "Invalid line received: invalid token: %s", linedup); params++; smtp_callbacks[i].smtp_filter(&(smtp_callbacks[i]), version, &tm, reqid, token, params); @@ -283,7 +282,7 @@ smtp_connect(struct smtp_callback *cb, int version, st hostname = params; if ((address = strchr(params, '|')) == NULL) - fatalx("Invalid line received: missing address: %s", params); + errx(1, "Invalid line received: missing address: %s", params); address++[0] = '\0'; addrx.af = AF_INET; @@ -295,9 +294,9 @@ smtp_connect(struct smtp_callback *cb, int version, st ret = inet_pton(addrx.af, address, addrx.af == AF_INET ? (void *)&(addrx.addr) : (void *)&(addrx.addr6)); if (ret == 0) - fatalx("Invalid line received: Couldn't parse address: %s", params); + errx(1, "Invalid line received: Couldn't parse address: %s", params); if (ret == -1) - fatal("Couldn't convert address: %s", params); + err(1, "Couldn't convert address: %s", params); f = cb->cb; f(cb->type, version, tm, cb->direction, cb->phase, reqid, token, @@ -365,17 +364,17 @@ smtp_vprintf(const char *fmt, va_list ap) fmtlen = vsnprintf(buf.buf + buf.buflen, buf.bufsize - buf.buflen, fmt, ap); if (fmtlen == -1) - fatal("vsnprintf"); + err(1, "vsnprintf"); if (fmtlen >= buf.bufsize - buf.buflen) { buf.bufsize = buf.buflen + fmtlen + 1; buf.buf = reallocarray(buf.buf, buf.bufsize, sizeof(*(buf.buf))); if (buf.buf == NULL) - fatal(NULL); + err(1, NULL); fmtlen = vsnprintf(buf.buf + buf.buflen, buf.bufsize - buf.buflen, fmt, cap); if (fmtlen == -1) - fatal("vsnprintf"); + err(1, "vsnprintf"); } va_end(cap); buf.buflen += fmtlen; @@ -401,7 +400,7 @@ smtp_write(int fd, short event, void *arg) wlen = write(fd, buf->buf, buf->buflen); if (wlen == -1) { if (errno != EAGAIN && errno != EINTR) - fatal("Failed to write to smtpd"); + err(1, "Failed to write to smtpd"); event_add(&stdoutev, NULL); return; } @@ -421,7 +420,7 @@ smtp_filter_reject(uint64_t reqid, uint64_t token, int va_list ap; if (code < 200 || code > 599) - fatalx("Invalid reject code"); + errx(1, "Invalid reject code"); smtp_printf("filter-result|%016"PRIx64"|%016"PRIx64"|reject|%d ", token, reqid, code); @@ -463,7 +462,7 @@ smtp_register(char *type, char *phase, char *direction static int evinit = 0; if (ready) - fatalx("Can't register when proc is running"); + errx(1, "Can't register when proc is running"); if (!evinit) { event_init();