Skip to content
This repository was archived by the owner on May 24, 2022. It is now read-only.

Commit e393b07

Browse files
committed
Logging: make syslog facility configurable.
1 parent 337b2e4 commit e393b07

File tree

5 files changed

+104
-1
lines changed

5 files changed

+104
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ Detail about the entire set of options can be found by invoking `stud -h`:
118118
LOGGING:
119119
-q --quiet Be quiet; emit only error messages
120120
-s --syslog Send log message to syslog in addition to stderr/stdout
121+
--syslog-facility=FACILITY Syslog facility to use (Default: "daemon")
121122

122123
OTHER OPTIONS:
123124
--daemon Fork into background and become a daemon (Default: off)

configuration.c

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <pwd.h>
1818
#include <grp.h>
1919
#include <sys/stat.h>
20+
#include <syslog.h>
2021

2122
#include "configuration.h"
2223
#include "version.h"
@@ -39,6 +40,8 @@
3940
#define CFG_GROUP "group"
4041
#define CFG_QUIET "quiet"
4142
#define CFG_SYSLOG "syslog"
43+
#define CFG_SYSLOG_FACILITY "syslog-facility"
44+
#define CFG_PARAM_SYSLOG_FACILITY 11015
4245
#define CFG_DAEMON "daemon"
4346
#define CFG_WRITE_IP "write-ip"
4447
#define CFG_WRITE_PROXY "write-proxy"
@@ -137,6 +140,7 @@ stud_config * config_new (void) {
137140

138141
r->QUIET = 0;
139142
r->SYSLOG = 0;
143+
r->SYSLOG_FACILITY = LOG_DAEMON;
140144
r->TCP_KEEPALIVE_TIME = 3600;
141145
r->DAEMONIZE = 0;
142146
r->PREFER_SERVER_CIPHERS = 0;
@@ -620,6 +624,47 @@ void config_param_validate (char *k, char *v, stud_config *cfg, char *file, int
620624
else if (strcmp(k, CFG_SYSLOG) == 0) {
621625
r = config_param_val_bool(v, &cfg->SYSLOG);
622626
}
627+
else if (strcmp(k, CFG_SYSLOG_FACILITY) == 0) {
628+
r = 1;
629+
if (!strcmp(v, "auth") || !strcmp(v, "authpriv"))
630+
cfg->SYSLOG_FACILITY = LOG_AUTHPRIV;
631+
else if (!strcmp(v, "cron"))
632+
cfg->SYSLOG_FACILITY = LOG_CRON;
633+
else if (!strcmp(v, "daemon"))
634+
cfg->SYSLOG_FACILITY = LOG_DAEMON;
635+
else if (!strcmp(v, "ftp"))
636+
cfg->SYSLOG_FACILITY = LOG_FTP;
637+
else if (!strcmp(v, "local0"))
638+
cfg->SYSLOG_FACILITY = LOG_LOCAL0;
639+
else if (!strcmp(v, "local1"))
640+
cfg->SYSLOG_FACILITY = LOG_LOCAL1;
641+
else if (!strcmp(v, "local2"))
642+
cfg->SYSLOG_FACILITY = LOG_LOCAL2;
643+
else if (!strcmp(v, "local3"))
644+
cfg->SYSLOG_FACILITY = LOG_LOCAL3;
645+
else if (!strcmp(v, "local4"))
646+
cfg->SYSLOG_FACILITY = LOG_LOCAL4;
647+
else if (!strcmp(v, "local5"))
648+
cfg->SYSLOG_FACILITY = LOG_LOCAL5;
649+
else if (!strcmp(v, "local6"))
650+
cfg->SYSLOG_FACILITY = LOG_LOCAL6;
651+
else if (!strcmp(v, "local7"))
652+
cfg->SYSLOG_FACILITY = LOG_LOCAL7;
653+
else if (!strcmp(v, "lpr"))
654+
cfg->SYSLOG_FACILITY = LOG_LPR;
655+
else if (!strcmp(v, "mail"))
656+
cfg->SYSLOG_FACILITY = LOG_MAIL;
657+
else if (!strcmp(v, "news"))
658+
cfg->SYSLOG_FACILITY = LOG_NEWS;
659+
else if (!strcmp(v, "user"))
660+
cfg->SYSLOG_FACILITY = LOG_USER;
661+
else if (!strcmp(v, "uucp"))
662+
cfg->SYSLOG_FACILITY = LOG_UUCP;
663+
else {
664+
config_error_set("Invalid facility '%s'.", v);
665+
r = 0;
666+
}
667+
}
623668
else if (strcmp(k, CFG_DAEMON) == 0) {
624669
r = config_param_val_bool(v, &cfg->DAEMONIZE);
625670
}
@@ -748,6 +793,48 @@ char * config_disp_hostport (char *host, char *port) {
748793
return tmp_buf;
749794
}
750795

796+
const char * config_disp_log_facility (int facility) {
797+
switch (facility)
798+
{
799+
case LOG_AUTHPRIV:
800+
return "authpriv";
801+
case LOG_CRON:
802+
return "cron";
803+
case LOG_DAEMON:
804+
return "daemon";
805+
case LOG_FTP:
806+
return "ftp";
807+
case LOG_LOCAL0:
808+
return "local0";
809+
case LOG_LOCAL1:
810+
return "local1";
811+
case LOG_LOCAL2:
812+
return "local2";
813+
case LOG_LOCAL3:
814+
return "local3";
815+
case LOG_LOCAL4:
816+
return "local4";
817+
case LOG_LOCAL5:
818+
return "local5";
819+
case LOG_LOCAL6:
820+
return "local6";
821+
case LOG_LOCAL7:
822+
return "local7";
823+
case LOG_LPR:
824+
return "lpr";
825+
case LOG_MAIL:
826+
return "mail";
827+
case LOG_NEWS:
828+
return "news";
829+
case LOG_USER:
830+
return "user";
831+
case LOG_UUCP:
832+
return "uucp";
833+
default:
834+
return "UNKNOWN";
835+
}
836+
}
837+
751838
void config_print_usage_fd (char *prog, stud_config *cfg, FILE *out) {
752839
if (out == NULL) out = stderr;
753840
fprintf(out, "Usage: %s [OPTIONS] PEM\n\n", basename(prog));
@@ -806,6 +893,7 @@ void config_print_usage_fd (char *prog, stud_config *cfg, FILE *out) {
806893
fprintf(out, "LOGGING:\n");
807894
fprintf(out, " -q --quiet Be quiet; emit only error messages\n");
808895
fprintf(out, " -s --syslog Send log message to syslog in addition to stderr/stdout\n");
896+
fprintf(out, " --syslog-facility=FACILITY Syslog facility to use (Default: \"%s\")\n", config_disp_log_facility(cfg->SYSLOG_FACILITY));
809897
fprintf(out, "\n");
810898
fprintf(out, "OTHER OPTIONS:\n");
811899
fprintf(out, " --daemon Fork into background and become a daemon (Default: %s)\n", config_disp_bool(cfg->DAEMONIZE));
@@ -968,6 +1056,12 @@ void config_print_default (FILE *fd, stud_config *cfg) {
9681056
fprintf(fd, FMT_STR, CFG_SYSLOG, config_disp_bool(cfg->SYSLOG));
9691057
fprintf(fd, "\n");
9701058

1059+
fprintf(fd, "# Syslog facility to use\n");
1060+
fprintf(fd, "#\n");
1061+
fprintf(fd, "# type: string\n");
1062+
fprintf(fd, FMT_QSTR, CFG_SYSLOG_FACILITY, config_disp_log_facility(cfg->SYSLOG_FACILITY));
1063+
fprintf(fd, "\n");
1064+
9711065
fprintf(fd, "# Run as daemon\n");
9721066
fprintf(fd, "#\n");
9731067
fprintf(fd, "# type: boolean\n");
@@ -1032,6 +1126,7 @@ void config_parse_cli(int argc, char **argv, stud_config *cfg) {
10321126
{ CFG_GROUP, 1, NULL, 'g' },
10331127
{ CFG_QUIET, 0, NULL, 'q' },
10341128
{ CFG_SYSLOG, 0, NULL, 's' },
1129+
{ CFG_SYSLOG_FACILITY, 1, NULL, CFG_PARAM_SYSLOG_FACILITY },
10351130
{ CFG_DAEMON, 0, &cfg->DAEMONIZE, 1 },
10361131
{ CFG_WRITE_IP, 0, &cfg->WRITE_IP_OCTET, 1 },
10371132
{ CFG_WRITE_PROXY, 0, &cfg->WRITE_PROXY_LINE, 1 },
@@ -1066,6 +1161,9 @@ void config_parse_cli(int argc, char **argv, stud_config *cfg) {
10661161
exit(0);
10671162
break;
10681163
#endif
1164+
case CFG_PARAM_SYSLOG_FACILITY:
1165+
config_param_validate(CFG_SYSLOG_FACILITY, optarg, cfg, NULL, 0);
1166+
break;
10691167
case 'c':
10701168
config_param_validate(CFG_CIPHERS, optarg, cfg, NULL, 0);
10711169
break;

configuration.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ struct __stud_config {
5353
#endif
5454
int QUIET;
5555
int SYSLOG;
56+
int SYSLOG_FACILITY;
5657
int TCP_KEEPALIVE_TIME;
5758
int DAEMONIZE;
5859
int PREFER_SERVER_CIPHERS;

stud.8

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ Send messages to syslog in addition to
107107
.Em stderr
108108
and
109109
.Em stdout .
110+
.It Fl -syslog-facility Ar facility
111+
Syslog facility to use. Default is
112+
.Ar daemon .
110113
.It Fl -write-ip
111114
Write 1 octet with the IP family followed by the IP address in 4
112115
(IPv4) or 16 (IPv6) octets little-endian to backend before the actual

stud.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1262,7 +1262,7 @@ void init_globals() {
12621262
fail("calloc");
12631263

12641264
if (CONFIG->SYSLOG)
1265-
openlog("stud", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_DAEMON);
1265+
openlog("stud", LOG_CONS | LOG_PID | LOG_NDELAY, CONFIG->SYSLOG_FACILITY);
12661266
}
12671267

12681268
/* Forks COUNT children starting with START_INDEX.

0 commit comments

Comments
 (0)