commit 9b374f41eee2db5080ef2feb7973228afa3b22aa from: Omar Polo date: Sun Jan 10 14:40:14 2021 UTC macro reordering and while there replace SAFE_SETENV with an inline function. LOG is more difficult to transform into an inline function, given the string concatenations it does. The other LOG* and FATAL macros are fine as they already are. commit - 579e21b35a0ad4cf3540ed474c994ace0e97a286 commit + 9b374f41eee2db5080ef2feb7973228afa3b22aa blob - 346333c39c7b984387caa49b05982a45eac18a26 blob + 287ac7db9022a66dbec587b368b683faa82f3485 --- gmid.c +++ gmid.c @@ -28,6 +28,35 @@ #include "gmid.h" +#define LOG(priority, c, fmt, ...) \ + do { \ + char buf[INET_ADDRSTRLEN]; \ + if (inet_ntop((c)->af, &(c)->addr, \ + buf, sizeof(buf)) == NULL) \ + FATAL("inet_ntop: %s", strerror(errno)); \ + if (foreground) \ + fprintf(stderr, \ + "%s " fmt "\n", buf, __VA_ARGS__); \ + else \ + syslog((priority) | LOG_DAEMON, \ + "%s " fmt, buf, __VA_ARGS__); \ + } while (0) + +#define LOGE(c, fmt, ...) LOG(LOG_ERR, c, fmt, __VA_ARGS__) +#define LOGN(c, fmt, ...) LOG(LOG_NOTICE, c, fmt, __VA_ARGS__) +#define LOGI(c, fmt, ...) LOG(LOG_INFO, c, fmt, __VA_ARGS__) +#define LOGD(c, fmt, ...) LOG(LOG_DEBUG, c, fmt, __VA_ARGS__) + +#define FATAL(fmt, ...) \ + do { \ + if (foreground) \ + fprintf(stderr, fmt "\n", __VA_ARGS__); \ + else \ + syslog(LOG_DAEMON | LOG_CRIT, \ + fmt, __VA_ARGS__); \ + exit(1); \ + } while (0) + const char *dir, *cgi; int dirfd; int port; @@ -56,6 +85,14 @@ struct etm { /* file extension to mime */ {NULL, NULL} }; +static inline void +safe_setenv(const char *name, const char *val) +{ + if (val == NULL) + val = ""; + setenv(name, val, 1); +} + void sig_handler(int sig) { @@ -301,24 +338,24 @@ start_cgi(const char *spath, const char *relpath, cons argv[0] = argv[1] = ex; /* fix the env */ - SAFE_SETENV("GATEWAY_INTERFACE", "CGI/1.1"); - SAFE_SETENV("SERVER_SOFTWARE", "gmid"); - SAFE_SETENV("SERVER_PORT", portno); + safe_setenv("GATEWAY_INTERFACE", "CGI/1.1"); + safe_setenv("SERVER_SOFTWARE", "gmid"); + safe_setenv("SERVER_PORT", portno); /* setenv("SERVER_NAME", "", 1); */ - SAFE_SETENV("SCRIPT_NAME", spath); - SAFE_SETENV("SCRIPT_EXECUTABLE", ex); - SAFE_SETENV("REQUEST_URI", requri); - SAFE_SETENV("REQUEST_RELATIVE", relpath); - SAFE_SETENV("QUERY_STRING", query); - SAFE_SETENV("REMOTE_HOST", addr); - SAFE_SETENV("REMOTE_ADDR", addr); - SAFE_SETENV("DOCUMENT_ROOT", dir); + safe_setenv("SCRIPT_NAME", spath); + safe_setenv("SCRIPT_EXECUTABLE", ex); + safe_setenv("REQUEST_URI", requri); + safe_setenv("REQUEST_RELATIVE", relpath); + safe_setenv("QUERY_STRING", query); + safe_setenv("REMOTE_HOST", addr); + safe_setenv("REMOTE_ADDR", addr); + safe_setenv("DOCUMENT_ROOT", dir); if (tls_peer_cert_provided(c->ctx)) { - SAFE_SETENV("AUTH_TYPE", "Certificate"); - SAFE_SETENV("REMOTE_USER", tls_peer_cert_subject(c->ctx)); - SAFE_SETENV("TLS_CLIENT_ISSUER", tls_peer_cert_issuer(c->ctx)); - SAFE_SETENV("TLS_CLIENT_HASH", tls_peer_cert_hash(c->ctx)); + safe_setenv("AUTH_TYPE", "Certificate"); + safe_setenv("REMOTE_USER", tls_peer_cert_subject(c->ctx)); + safe_setenv("TLS_CLIENT_ISSUER", tls_peer_cert_issuer(c->ctx)); + safe_setenv("TLS_CLIENT_HASH", tls_peer_cert_hash(c->ctx)); } execvp(ex, argv); blob - 3ee5fe4dd4a929a973d8217610e0011d3eb02086 blob + 16d96a7d7dcf40e2f3948b437dac46e5447a9f21 --- gmid.h +++ gmid.h @@ -46,46 +46,8 @@ #define NOT_FOUND 51 #define BAD_REQUEST 59 -#ifndef MAX_USERS #define MAX_USERS 64 -#endif -#define SAFE_SETENV(var, val) do { \ - const char *_tmp = (val); \ - if (_tmp == NULL) \ - _tmp = ""; \ - setenv((var), _tmp, 1); \ - } while(0) - -#define LOG(priority, c, fmt, ...) \ - do { \ - char buf[INET_ADDRSTRLEN]; \ - if (inet_ntop((c)->af, &(c)->addr, \ - buf, sizeof(buf)) == NULL) \ - FATAL("inet_ntop: %s", strerror(errno)); \ - if (foreground) \ - fprintf(stderr, \ - "%s " fmt "\n", buf, __VA_ARGS__); \ - else \ - syslog((priority) | LOG_DAEMON, \ - "%s " fmt, buf, __VA_ARGS__); \ - } while (0) - -#define LOGE(c, fmt, ...) LOG(LOG_ERR, c, fmt, __VA_ARGS__) -#define LOGN(c, fmt, ...) LOG(LOG_NOTICE, c, fmt, __VA_ARGS__) -#define LOGI(c, fmt, ...) LOG(LOG_INFO, c, fmt, __VA_ARGS__) -#define LOGD(c, fmt, ...) LOG(LOG_DEBUG, c, fmt, __VA_ARGS__) - -#define FATAL(fmt, ...) \ - do { \ - if (foreground) \ - fprintf(stderr, fmt "\n", __VA_ARGS__); \ - else \ - syslog(LOG_DAEMON | LOG_CRIT, \ - fmt, __VA_ARGS__); \ - exit(1); \ - } while (0) - enum { S_OPEN, S_INITIALIZING,