commit 27dd1f3b8e4e438f1708f7b3505d3ae48c1e2488 from: Martijn van Duren date: Thu Aug 22 11:29:57 2019 UTC Add osmtpd_err{,x}. A drop in replacement for err{,x}, but it does not prepend the __progname commit - 46eecc494ba1995918cbff376ec6c02f3f403836 commit + 27dd1f3b8e4e438f1708f7b3505d3ae48c1e2488 blob - 76638062dd505151f97d71686613490890b4b100 blob + 9e5e758b470e0025530fc870bc6fccc64b5343a6 --- opensmtpd.c +++ opensmtpd.c @@ -21,7 +21,6 @@ #include #include -#include #include #include #include @@ -882,7 +881,7 @@ osmtpd_run(void) if ((io_stdin = io_new()) == NULL || (io_stdout = io_new()) == NULL) - err(1, "io_new"); + osmtpd_err(1, "io_new"); io_set_nonblocking(STDIN_FILENO); io_set_fd(io_stdin, STDIN_FILENO); io_set_callback(io_stdin, osmtpd_newline, NULL); @@ -921,13 +920,37 @@ osmtpd_run(void) } if (!registered) - errx(1, "No events registered"); + osmtpd_errx(1, "No events registered"); io_printf(io_stdout, "register|ready\n"); ready = 1; event_dispatch(); } +__dead void +osmtpd_err(int eval, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + fprintf(stderr, "%s\n", strerror(errno)); + exit(eval); +} + +__dead void +osmtpd_errx(int eval, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + fprintf(stderr, "\n"); + exit(eval); +} + static void osmtpd_newline(struct io *io, int ev, void *arg) { @@ -951,12 +974,12 @@ osmtpd_newline(struct io *io, int ev, void *arg) while ((line = io_getline(io, &linelen)) > 0) { if (dupsize < linelen) { if ((linedup = realloc(linedup, linelen + 1)) == NULL) - err(1, NULL); + osmtpd_err(1, NULL); dupsize = linelen + 1; } strlcpy(linedup, line, dupsize); if ((end = strchr(line, '|')) == NULL) - errx(1, "Invalid line received: missing version: %s", linedup); + osmtpd_errx(1, "Invalid line received: missing version: %s", linedup); end++[0] = '\0'; if (strcmp(line, "filter") == 0) type = OSMTPD_TYPE_FILTER; @@ -967,47 +990,47 @@ osmtpd_newline(struct io *io, int ev, void *arg) if (strcmp(line, "ready") == 0) continue; if ((end = strchr(line, '|')) == NULL) - errx(1, "Invalid line received: missing key: %s", linedup); + osmtpd_errx(1, "Invalid line received: missing key: %s", linedup); end++[0] = '\0'; if (strcmp(line, "smtp-session-timeout") == 0) { session_timeout = strtonum(end, 0, INT_MAX, &errstr); if (errstr != NULL) - errx(1, "Invalid line received: " + osmtpd_errx(1, "Invalid line received: " "invalid smtp-sesion-timeout: %s", linedup); } continue; } else - errx(1, "Invalid line received: unknown message type: %s", linedup); + osmtpd_errx(1, "Invalid line received: unknown message type: %s", linedup); line = end; if ((end = strchr(line, '|')) == NULL) - errx(1, "Invalid line received: missing time: %s", linedup); + osmtpd_errx(1, "Invalid line received: missing time: %s", linedup); end++[0] = '\0'; if (strcmp(line, "0.1") != 0) - errx(1, "Unsupported protocol received: %s", linedup); + osmtpd_errx(1, "Unsupported protocol received: %s", linedup); version_major = 0; version_minor = 1; line = end; if ((end = strchr(line, '.')) == NULL) - errx(1, "Invalid line received: invalid timestamp: %s", linedup); + osmtpd_errx(1, "Invalid line received: invalid timestamp: %s", linedup); end++[0] = '\0'; tm.tv_sec = (time_t) strtonum(line, 0, INT64_MAX, &errstr); if (errstr != NULL) - errx(1, "Invalid line received: invalid timestamp: %s", linedup); + osmtpd_errx(1, "Invalid line received: invalid timestamp: %s", linedup); line = end; if ((end = strchr(line, '|')) == NULL) - errx(1, "Invalid line received: missing direction: %s", linedup); + osmtpd_errx(1, "Invalid line received: missing direction: %s", linedup); end++[0] = '\0'; tm.tv_nsec = (long) strtonum(line, 0, LONG_MAX, &errstr); if (errstr != NULL) - errx(1, "Invalid line received: invalid timestamp: %s", + osmtpd_errx(1, "Invalid line received: invalid timestamp: %s", linedup); tm.tv_nsec *= 10 * (9 - (end - line)); line = end; if ((end = strchr(line, '|')) == NULL) - errx(1, "Invalid line receieved: missing phase: %s", + osmtpd_errx(1, "Invalid line receieved: missing phase: %s", linedup); end++[0] = '\0'; if (strcmp(line, "smtp-in") == 0) @@ -1015,10 +1038,10 @@ osmtpd_newline(struct io *io, int ev, void *arg) else if (strcmp(line, "smtp-out") == 0) incoming = 0; else - errx(1, "Invalid line: invalid direction: %s", linedup); + osmtpd_errx(1, "Invalid line: invalid direction: %s", linedup); line = end; if ((end = strchr(line, '|')) == NULL) - errx(1, "Invalid line received: missing reqid: %s", + osmtpd_errx(1, "Invalid line received: missing reqid: %s", linedup); end++[0] = '\0'; phase = osmtpd_strtophase(line, linedup); @@ -1027,13 +1050,13 @@ osmtpd_newline(struct io *io, int ev, void *arg) search.ctx.reqid = strtoull(line, &end, 16); if ((search.ctx.reqid == ULLONG_MAX && errno != 0) || (end[0] != '|' && end[0] != '\0')) - errx(1, "Invalid line received: invalid reqid: %s", + osmtpd_errx(1, "Invalid line received: invalid reqid: %s", linedup); line = end + 1; ctx = RB_FIND(osmtpd_sessions, &osmtpd_sessions, &search); if (ctx == NULL) { if ((ctx = malloc(sizeof(*ctx))) == NULL) - err(1, NULL); + osmtpd_err(1, NULL); ctx->ctx.reqid = search.ctx.reqid; ctx->ctx.rdns = NULL; ctx->ctx.fcrdns = NULL; @@ -1043,7 +1066,7 @@ osmtpd_newline(struct io *io, int ev, void *arg) ctx->ctx.mailfrom = NULL; ctx->ctx.rcptto = malloc(sizeof(*(ctx->ctx.rcptto))); if (ctx->ctx.rcptto == NULL) - err(1, "malloc"); + osmtpd_err(1, "malloc"); ctx->ctx.rcptto[0] = NULL; memset(&(ctx->ctx.src), 0, sizeof(ctx->ctx.src)); ctx->ctx.src.ss_family = AF_UNSPEC; @@ -1073,14 +1096,14 @@ osmtpd_newline(struct io *io, int ev, void *arg) break; } if (i == NITEMS(osmtpd_callbacks)) { - errx(1, "Invalid line received: received unregistered " + osmtpd_errx(1, "Invalid line received: received unregistered " "line: %s", linedup); } if (ctx->ctx.type == OSMTPD_TYPE_FILTER) { ctx->ctx.token = strtoull(line, &end, 16); if ((ctx->ctx.token == ULLONG_MAX && errno != 0) || end[0] != '|') - errx(1, "Invalid line received: invalid token: %s", linedup); + osmtpd_errx(1, "Invalid line received: invalid token: %s", linedup); line = end + 1; } osmtpd_callbacks[i].osmtpd_cb(&(osmtpd_callbacks[i]), @@ -1097,7 +1120,7 @@ osmtpd_outevt(struct io *io, int evt, void *arg) case IO_DISCONNECTED: exit(0); default: - errx(1, "Unexpectd event"); + osmtpd_errx(1, "Unexpectd event"); } } @@ -1132,7 +1155,7 @@ osmtpd_connect(struct osmtpd_callback *cb, struct osmt hostname = params; if ((address = strchr(params, '|')) == NULL) - errx(1, "Invalid line received: missing address: %s", linedup); + osmtpd_errx(1, "Invalid line received: missing address: %s", linedup); address++[0] = '\0'; osmtpd_addrtoss(address, &ss, 0, linedup); @@ -1151,26 +1174,26 @@ osmtpd_link_connect(struct osmtpd_callback *cb, struct struct sockaddr_storage *, struct sockaddr_storage *); if ((end = strchr(params, '|')) == NULL) - errx(1, "Invalid line received: missing fcrdns: %s", linedup); + osmtpd_errx(1, "Invalid line received: missing fcrdns: %s", linedup); end++[0] = '\0'; rdns = params; params = end; if ((end = strchr(params, '|')) == NULL) - errx(1, "Invalid line received: missing src: %s", linedup); + osmtpd_errx(1, "Invalid line received: missing src: %s", linedup); end++[0] = '\0'; fcrdns = params; params = end; if ((end = strchr(params, '|')) == NULL) - errx(1, "Invalid line received: missing dst: %s", linedup); + osmtpd_errx(1, "Invalid line received: missing dst: %s", linedup); end++[0] = '\0'; osmtpd_addrtoss(params, &src, 1, linedup); params = end; osmtpd_addrtoss(params, &dst, 1, linedup); if (cb->storereport) { if ((ctx->rdns = strdup(params)) == NULL) - err(1, NULL); + osmtpd_err(1, NULL); if ((ctx->fcrdns = strdup(params)) == NULL) - err(1, NULL); + osmtpd_err(1, NULL); memcpy(&(ctx->src), &src, sizeof(ctx->src)); memcpy(&(ctx->dst), &dst, sizeof(ctx->dst)); } @@ -1215,7 +1238,7 @@ osmtpd_link_identify(struct osmtpd_callback *cb, struc if (cb->storereport) { if ((ctx->identity = strdup(identity)) == NULL) - err(1, NULL); + osmtpd_err(1, NULL); } if ((f = cb->cb) != NULL) @@ -1230,7 +1253,7 @@ osmtpd_link_tls(struct osmtpd_callback *cb, struct osm if (cb->storereport) { if ((ctx->ciphers = strdup(ciphers)) == NULL) - err(1, NULL); + osmtpd_err(1, NULL); } if ((f = cb->cb) != NULL) @@ -1248,11 +1271,11 @@ osmtpd_tx_begin(struct osmtpd_callback *cb, struct osm errno = 0; imsgid = strtoul(msgid, &endptr, 16); if ((imsgid == ULONG_MAX && errno != 0) || endptr[0] != '\0') - errx(1, "Invalid line received: invalid msgid: %s", linedup); + osmtpd_errx(1, "Invalid line received: invalid msgid: %s", linedup); ctx->msgid = imsgid; /* Check if we're in range */ if ((unsigned long) ctx->msgid != imsgid) - errx(1, "Invalid line received: invalid msgid: %s", linedup); + osmtpd_errx(1, "Invalid line received: invalid msgid: %s", linedup); if (!cb->storereport) ctx->msgid = 0; @@ -1277,21 +1300,21 @@ osmtpd_tx_mail(struct osmtpd_callback *cb, struct osmt errno = 0; imsgid = strtoul(params, &end, 16); if ((imsgid == ULONG_MAX && errno != 0)) - errx(1, "Invalid line received: invalid msgid: %s", linedup); + osmtpd_errx(1, "Invalid line received: invalid msgid: %s", linedup); if (end[0] != '|') - errx(1, "Invalid line received: missing address: %s", linedup); + osmtpd_errx(1, "Invalid line received: missing address: %s", linedup); msgid = imsgid; if ((unsigned long) msgid != imsgid) - errx(1, "Invalid line received: invalid msgid: %s", linedup); + osmtpd_errx(1, "Invalid line received: invalid msgid: %s", linedup); params = end + 1; if ((end = strchr(params, '|')) == NULL) - errx(1, "Invalid line received: missing status: %s", linedup); + osmtpd_errx(1, "Invalid line received: missing status: %s", linedup); end++[0] = '\0'; mailfrom = params; if (cb->storereport) { if ((ctx->mailfrom = strdup(mailfrom)) == NULL) - err(1, NULL); + osmtpd_err(1, NULL); } params = end; @@ -1313,16 +1336,16 @@ osmtpd_tx_rcpt(struct osmtpd_callback *cb, struct osmt errno = 0; imsgid = strtoul(params, &end, 16); if ((imsgid == ULONG_MAX && errno != 0)) - errx(1, "Invalid line received: invalid msgid: %s", linedup); + osmtpd_errx(1, "Invalid line received: invalid msgid: %s", linedup); if (end[0] != '|') - errx(1, "Invalid line received: missing address: %s", linedup); + osmtpd_errx(1, "Invalid line received: missing address: %s", linedup); msgid = imsgid; if ((unsigned long) msgid != imsgid) - errx(1, "Invalid line received: invalid msgid: %s", linedup); + osmtpd_errx(1, "Invalid line received: invalid msgid: %s", linedup); params = end + 1; if ((end = strchr(params, '|')) == NULL) - errx(1, "Invalid line received: missing status: %s", linedup); + osmtpd_errx(1, "Invalid line received: missing status: %s", linedup); end++[0] = '\0'; rcptto = params; @@ -1334,10 +1357,10 @@ osmtpd_tx_rcpt(struct osmtpd_callback *cb, struct osmt ctx->rcptto = reallocarray(ctx->rcptto, i + 2, sizeof(*(ctx->rcptto))); if (ctx->rcptto == NULL) - err(1, NULL); + osmtpd_err(1, NULL); if ((ctx->rcptto[i] = strdup(rcptto)) == NULL) - err(1, NULL); + osmtpd_err(1, NULL); ctx->rcptto[i + 1] = NULL; } @@ -1359,18 +1382,18 @@ osmtpd_tx_envelope(struct osmtpd_callback *cb, struct errno = 0; imsgid = strtoul(params, &end, 16); if ((imsgid == ULONG_MAX && errno != 0)) - errx(1, "Invalid line received: invalid msgid: %s", linedup); + osmtpd_errx(1, "Invalid line received: invalid msgid: %s", linedup); if (end[0] != '|') - errx(1, "Invalid line received: missing address: %s", linedup); + osmtpd_errx(1, "Invalid line received: missing address: %s", linedup); msgid = imsgid; if ((unsigned long) msgid != imsgid) - errx(1, "Invalid line received: invalid msgid: %s", linedup); + osmtpd_errx(1, "Invalid line received: invalid msgid: %s", linedup); params = end + 1; evpid = strtoull(params, &end, 16); if ((ctx->evpid == ULLONG_MAX && errno != 0) || end[0] != '\0') - errx(1, "Invalid line received: invalid evpid: %s", linedup); + osmtpd_errx(1, "Invalid line received: invalid evpid: %s", linedup); if (cb->storereport) ctx->evpid = evpid; @@ -1390,12 +1413,12 @@ osmtpd_tx_data(struct osmtpd_callback *cb, struct osmt errno = 0; imsgid = strtoul(params, &end, 16); if ((imsgid == ULONG_MAX && errno != 0)) - errx(1, "Invalid line received: invalid msgid: %s", linedup); + osmtpd_errx(1, "Invalid line received: invalid msgid: %s", linedup); if (end[0] != '|') - errx(1, "Invalid line received: missing address: %s", linedup); + osmtpd_errx(1, "Invalid line received: missing address: %s", linedup); msgid = imsgid; if ((unsigned long) msgid != imsgid) - errx(1, "Invalid line received: invalid msgid: %s", linedup); + osmtpd_errx(1, "Invalid line received: invalid msgid: %s", linedup); params = end + 1; if ((f = cb->cb) != NULL) @@ -1416,17 +1439,17 @@ osmtpd_tx_commit(struct osmtpd_callback *cb, struct os errno = 0; imsgid = strtoul(params, &end, 16); if ((imsgid == ULONG_MAX && errno != 0)) - errx(1, "Invalid line received: invalid msgid: %s", linedup); + osmtpd_errx(1, "Invalid line received: invalid msgid: %s", linedup); if (end[0] != '|') - errx(1, "Invalid line received: missing address: %s", linedup); + osmtpd_errx(1, "Invalid line received: missing address: %s", linedup); msgid = imsgid; if ((unsigned long) msgid != imsgid) - errx(1, "Invalid line received: invalid msgid: %s", linedup); + osmtpd_errx(1, "Invalid line received: invalid msgid: %s", linedup); params = end + 1; msgsz = strtonum(params, 0, SIZE_MAX, &errstr); if (errstr != NULL) - errx(1, "Invalid line received: invalid msg size: %s", linedup); + osmtpd_errx(1, "Invalid line received: invalid msg size: %s", linedup); if ((f = cb->cb) != NULL) f(ctx, msgid, msgsz); @@ -1459,12 +1482,12 @@ osmtpd_tx_rollback(struct osmtpd_callback *cb, struct errno = 0; imsgid = strtoul(params, &end, 16); if ((imsgid == ULONG_MAX && errno != 0)) - errx(1, "Invalid line received: invalid msgid: %s", linedup); + osmtpd_errx(1, "Invalid line received: invalid msgid: %s", linedup); if (end[0] != '\0') - errx(1, "Invalid line received: missing address: %s", linedup); + osmtpd_errx(1, "Invalid line received: missing address: %s", linedup); msgid = imsgid; if ((unsigned long) msgid != imsgid) - errx(1, "Invalid line received: invalid msgid: %s", linedup); + osmtpd_errx(1, "Invalid line received: invalid msgid: %s", linedup); if ((f = cb->cb) != NULL) f(ctx, msgid); @@ -1497,7 +1520,7 @@ osmtpd_filter_reject(struct osmtpd_ctx *ctx, int code, va_list ap; if (code < 200 || code > 599) - errx(1, "Invalid reject code"); + osmtpd_errx(1, "Invalid reject code"); io_printf(io_stdout, "filter-result|%016"PRIx64"|%016"PRIx64"|reject|" "%d ", ctx->token, ctx->reqid, code); @@ -1553,14 +1576,14 @@ osmtpd_register(enum osmtpd_type type, enum osmtpd_pha size_t i; if (ready) - errx(1, "Can't register when proc is running"); + osmtpd_errx(1, "Can't register when proc is running"); for (i = 0; i < NITEMS(osmtpd_callbacks); i++) { if (type == osmtpd_callbacks[i].type && phase == osmtpd_callbacks[i].phase && incoming == osmtpd_callbacks[i].incoming) { if (osmtpd_callbacks[i].cb != NULL && cb != NULL) - errx(1, "Event already registered"); + osmtpd_errx(1, "Event already registered"); if (cb != NULL) osmtpd_callbacks[i].cb = cb; osmtpd_callbacks[i].doregister = 1; @@ -1570,7 +1593,7 @@ osmtpd_register(enum osmtpd_type type, enum osmtpd_pha } } /* NOT REACHED */ - errx(1, "Trying to register unknown event"); + osmtpd_errx(1, "Trying to register unknown event"); } static enum osmtpd_phase @@ -1636,7 +1659,7 @@ osmtpd_strtophase(const char *phase, const char *lined return OSMTPD_PHASE_FILTER_RESPONSE; if (strcmp(phase, "timeout") == 0) return OSMTPD_PHASE_TIMEOUT; - errx(1, "Invalid line received: invalid phase: %s", linedup); + osmtpd_errx(1, "Invalid line received: invalid phase: %s", linedup); } static const char * @@ -1648,7 +1671,7 @@ osmtpd_typetostr(enum osmtpd_type type) case OSMTPD_TYPE_REPORT: return "report"; } - errx(1, "In valid type: %d\n", type); + osmtpd_errx(1, "In valid type: %d\n", type); } static const char * @@ -1716,7 +1739,7 @@ osmtpd_phasetostr(enum osmtpd_phase phase) case OSMTPD_PHASE_TIMEOUT: return "timeout"; } - errx(1, "In valid phase: %d\n", phase); + osmtpd_errx(1, "In valid phase: %d\n", phase); } static enum @@ -1728,7 +1751,7 @@ osmtpd_status osmtpd_strtostatus(const char *status, c return OSMTPD_STATUS_TEMPFAIL; else if (strcmp(status, "permfail") == 0) return OSMTPD_STATUS_PERMFAIL; - errx(1, "Invalid line received: invalid status: %s\n", linedup); + osmtpd_errx(1, "Invalid line received: invalid status: %s\n", linedup); } static void @@ -1748,13 +1771,13 @@ osmtpd_addrtoss(char *addr, struct sockaddr_storage *s sin6->sin6_port = 0; if (hasport) { if ((port = strrchr(addr, ':')) == NULL) - errx(1, "Invalid line received: invalid " + osmtpd_errx(1, "Invalid line received: invalid " "address (%s): %s", addr, linedup); port++; sin6->sin6_port = htons(strtonum(port, 0, UINT16_MAX, &errstr)); if (errstr != NULL) - errx(1, "Invalid line received: invalid " + osmtpd_errx(1, "Invalid line received: invalid " "address (%s): %s", addr, linedup); port[-1] = '\0'; } @@ -1763,11 +1786,11 @@ osmtpd_addrtoss(char *addr, struct sockaddr_storage *s break; case 0: port[-1] = ':'; - errx(1, "Invalid line received: invalid address " + osmtpd_errx(1, "Invalid line received: invalid address " "(%s): %s", addr, linedup); default: port[-1] = ':'; - err(1, "Can't parse address (%s): %s", addr, linedup); + osmtpd_err(1, "Can't parse address (%s): %s", addr, linedup); } } else if (strncasecmp(addr, "unix:", 5) == 0) { sun = (struct sockaddr_un *)ss; @@ -1775,7 +1798,7 @@ osmtpd_addrtoss(char *addr, struct sockaddr_storage *s sun->sun_family = AF_UNIX; if (strlcpy(sun->sun_path, addr, sizeof(sun->sun_path)) >= sizeof(sun->sun_path)) { - errx(1, "Invalid line received: address too long (%s): " + osmtpd_errx(1, "Invalid line received: address too long (%s): " "%s", addr, linedup); } } else { @@ -1785,13 +1808,13 @@ osmtpd_addrtoss(char *addr, struct sockaddr_storage *s sin->sin_port = 0; if (hasport) { if ((port = strrchr(addr, ':')) == NULL) - errx(1, "Invalid line received: invalid " + osmtpd_errx(1, "Invalid line received: invalid " "address (%s): %s", addr, linedup); port++; sin->sin_port = htons(strtonum(port, 0, UINT16_MAX, &errstr)); if (errstr != NULL) - errx(1, "Invalid line received: invalid " + osmtpd_errx(1, "Invalid line received: invalid " "address (%s): %s", addr, linedup); port[-1] = '\0'; } @@ -1800,11 +1823,11 @@ osmtpd_addrtoss(char *addr, struct sockaddr_storage *s break; case 0: port[-1] = ':'; - errx(1, "Invalid line received: invalid address " + osmtpd_errx(1, "Invalid line received: invalid address " "(%s): %s", addr, linedup); default: port[-1] = ':'; - err(1, "Can't parse address (%s): %s", addr, linedup); + osmtpd_err(1, "Can't parse address (%s): %s", addr, linedup); } } } blob - d0d91a3b8391364e181b701c212b03bd1421f936 blob + 7733bb1633bbf1b09c266c69e45f8cf169013f63 --- opensmtpd.h +++ opensmtpd.h @@ -155,3 +155,5 @@ void osmtpd_filter_rewrite(struct osmtpd_ctx *, const void osmtpd_filter_dataline(struct osmtpd_ctx *, const char *, ...) __attribute__((__format__ (printf, 2, 3))); void osmtpd_run(void); +__dead void osmtpd_err(int eval, const char *fmt, ...); +__dead void osmtpd_errx(int eval, const char *fmt, ...);