]>
Commit | Line | Data |
---|---|---|
6dbe3af9 KZ |
1 | /* |
2 | * Copyright (c) 1983, 1993 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
7eda085c | 32 | * |
b50945d4 | 33 | * 1999-02-22 Arkadiusz MiĆkiewicz <misiek@pld.ORG.PL> |
7eda085c KZ |
34 | * - added Native Language Support |
35 | * Sun Mar 21 1999 - Arnaldo Carvalho de Melo <acme@conectiva.com.br> | |
36 | * - fixed strerr(errno) in gettext calls | |
6dbe3af9 KZ |
37 | */ |
38 | ||
6dbe3af9 | 39 | #include <errno.h> |
047e2888 | 40 | #include <limits.h> |
6dbe3af9 KZ |
41 | #include <unistd.h> |
42 | #include <stdlib.h> | |
5ec85227 | 43 | #include <sys/time.h> |
6dbe3af9 KZ |
44 | #include <stdio.h> |
45 | #include <ctype.h> | |
46 | #include <string.h> | |
7eda085c KZ |
47 | #include <sys/types.h> |
48 | #include <sys/socket.h> | |
66ee8158 | 49 | #include <sys/un.h> |
912d6b98 WJE |
50 | #include <arpa/inet.h> |
51 | #include <netdb.h> | |
b363e86d | 52 | #include <getopt.h> |
019b9702 | 53 | #include <pwd.h> |
27a9eb53 | 54 | #include <signal.h> |
eaac9f88 | 55 | #include <sys/uio.h> |
b363e86d | 56 | |
633493be | 57 | #include "all-io.h" |
b363e86d | 58 | #include "c.h" |
c05a80ca | 59 | #include "closestream.h" |
7eda085c | 60 | #include "nls.h" |
52a49e9a | 61 | #include "pathnames.h" |
b363e86d | 62 | #include "strutils.h" |
4b670c01 | 63 | #include "xalloc.h" |
4299ed1c KZ |
64 | #include "strv.h" |
65 | #include "list.h" | |
6dbe3af9 KZ |
66 | |
67 | #define SYSLOG_NAMES | |
68 | #include <syslog.h> | |
69 | ||
ebff016a | 70 | #ifdef HAVE_LIBSYSTEMD |
03190b73 | 71 | # define SD_JOURNAL_SUPPRESS_LOCATION |
d77dc29e | 72 | # include <systemd/sd-daemon.h> |
4b670c01 SK |
73 | # include <systemd/sd-journal.h> |
74 | #endif | |
75 | ||
87ee2658 ST |
76 | #ifdef HAVE_SYS_TIMEX_H |
77 | # include <sys/timex.h> | |
78 | #endif | |
79 | ||
68265d07 SK |
80 | enum { |
81 | TYPE_UDP = (1 << 1), | |
82 | TYPE_TCP = (1 << 2), | |
83 | ALL_TYPES = TYPE_UDP | TYPE_TCP | |
84 | }; | |
85 | ||
d77dc29e SK |
86 | enum { |
87 | AF_UNIX_ERRORS_OFF = 0, | |
88 | AF_UNIX_ERRORS_ON, | |
89 | AF_UNIX_ERRORS_AUTO | |
90 | }; | |
91 | ||
98920f80 | 92 | enum { |
4b670c01 | 93 | OPT_PRIO_PREFIX = CHAR_MAX + 1, |
4de2e8a0 SK |
94 | OPT_JOURNALD, |
95 | OPT_RFC3164, | |
d77dc29e | 96 | OPT_RFC5424, |
3f51c10b | 97 | OPT_SOCKET_ERRORS, |
55f5bc66 | 98 | OPT_MSGID, |
fd343a05 | 99 | OPT_NOACT, |
b6b67955 | 100 | OPT_ID, |
4299ed1c KZ |
101 | OPT_STRUCTURED_DATA_ID, |
102 | OPT_STRUCTURED_DATA_PARAM, | |
b6b67955 | 103 | OPT_OCTET_COUNT |
98920f80 DJ |
104 | }; |
105 | ||
4299ed1c KZ |
106 | /* rfc5424 structured data */ |
107 | struct structured_data { | |
108 | char *id; /* SD-ID */ | |
109 | char **params; /* array with SD-PARAMs */ | |
110 | ||
111 | struct list_head sds; | |
112 | }; | |
113 | ||
cfa77d26 SK |
114 | struct logger_ctl { |
115 | int fd; | |
cfa77d26 | 116 | int pri; |
59c6ac0b | 117 | pid_t pid; /* zero when unwanted */ |
2b3f40c5 | 118 | char *hdr; /* the syslog header (based on protocol) */ |
c8598d8a | 119 | char const *tag; |
55f5bc66 | 120 | char *msgid; |
c95d3209 | 121 | char *unix_socket; /* -u <path> or default to _PATH_DEVLOG */ |
c68a1cb4 SK |
122 | char *server; |
123 | char *port; | |
124 | int socket_type; | |
f68b8aa7 | 125 | size_t max_message_size; |
4299ed1c KZ |
126 | struct list_head user_sds; /* user defined rfc5424 structured data */ |
127 | struct list_head reserved_sds; /* standard rfc5424 structured data */ | |
128 | ||
2b3f40c5 | 129 | void (*syslogfp)(struct logger_ctl *ctl); |
4299ed1c | 130 | |
4de2e8a0 | 131 | unsigned int |
d77dc29e | 132 | unix_socket_errors:1, /* whether to report or not errors */ |
fd343a05 | 133 | noact:1, /* do not write to sockets */ |
9e930041 | 134 | prio_prefix:1, /* read priority from input */ |
d77dc29e SK |
135 | stderr_printout:1, /* output message to stderr */ |
136 | rfc5424_time:1, /* include time stamp */ | |
137 | rfc5424_tq:1, /* include time quality markup */ | |
ae6846b8 | 138 | rfc5424_host:1, /* include hostname */ |
b6b67955 AB |
139 | skip_empty_lines:1, /* do not send empty lines when processing files */ |
140 | octet_count:1; /* use RFC6587 octet counting */ | |
cfa77d26 SK |
141 | }; |
142 | ||
e92d55e6 | 143 | #define is_connected(_ctl) ((_ctl)->fd >= 0) |
caf6ac6e KZ |
144 | static void logger_reopen(struct logger_ctl *ctl); |
145 | ||
ef5fb280 KZ |
146 | /* |
147 | * For tests we want to be able to control datetime outputs | |
148 | */ | |
149 | #ifdef TEST_LOGGER | |
150 | static inline int logger_gettimeofday(struct timeval *tv, struct timezone *tz) | |
151 | { | |
152 | char *str = getenv("LOGGER_TEST_TIMEOFDAY"); | |
153 | uintmax_t sec, usec; | |
154 | ||
155 | if (str && sscanf(str, "%ju.%ju", &sec, &usec) == 2) { | |
156 | tv->tv_sec = sec; | |
157 | tv->tv_usec = usec; | |
98e90a49 | 158 | return tv->tv_sec >= 0 && tv->tv_usec >= 0 ? 0 : -EINVAL; |
ef5fb280 KZ |
159 | } |
160 | ||
161 | return gettimeofday(tv, tz); | |
162 | } | |
163 | ||
164 | static inline char *logger_xgethostname(void) | |
165 | { | |
166 | char *str = getenv("LOGGER_TEST_HOSTNAME"); | |
167 | return str ? xstrdup(str) : xgethostname(); | |
168 | } | |
169 | ||
170 | static inline pid_t logger_getpid(void) | |
171 | { | |
172 | char *str = getenv("LOGGER_TEST_GETPID"); | |
173 | unsigned int pid; | |
174 | ||
175 | if (str && sscanf(str, "%u", &pid) == 1) | |
176 | return pid; | |
177 | return getpid(); | |
178 | } | |
179 | ||
180 | ||
181 | #undef HAVE_NTP_GETTIME /* force to default non-NTP */ | |
182 | ||
183 | #else /* !TEST_LOGGER */ | |
184 | # define logger_gettimeofday(x, y) gettimeofday(x, y) | |
185 | # define logger_xgethostname xgethostname | |
186 | # define logger_getpid getpid | |
187 | #endif | |
188 | ||
189 | ||
f5fceb40 | 190 | static int decode(const char *name, const CODE *codetab) |
82054b1d | 191 | { |
f5fceb40 | 192 | register const CODE *c; |
82054b1d | 193 | |
4d7d1af6 SK |
194 | if (name == NULL || *name == '\0') |
195 | return -1; | |
196 | if (isdigit(*name)) { | |
197 | int num; | |
198 | char *end = NULL; | |
199 | ||
8d341322 | 200 | errno = 0; |
4d7d1af6 SK |
201 | num = strtol(name, &end, 10); |
202 | if (errno || name == end || (end && *end)) | |
203 | return -1; | |
204 | for (c = codetab; c->c_name; c++) | |
205 | if (num == c->c_val) | |
206 | return num; | |
207 | return -1; | |
208 | } | |
82054b1d DR |
209 | for (c = codetab; c->c_name; c++) |
210 | if (!strcasecmp(name, c->c_name)) | |
211 | return (c->c_val); | |
212 | ||
213 | return -1; | |
214 | } | |
215 | ||
216 | static int pencode(char *s) | |
217 | { | |
2e0fd22d SK |
218 | int facility, level; |
219 | char *separator; | |
220 | ||
7241dda1 KZ |
221 | assert(s); |
222 | ||
2e0fd22d SK |
223 | separator = strchr(s, '.'); |
224 | if (separator) { | |
225 | *separator = '\0'; | |
226 | facility = decode(s, facilitynames); | |
227 | if (facility < 0) | |
228 | errx(EXIT_FAILURE, _("unknown facility name: %s"), s); | |
229 | s = ++separator; | |
230 | } else | |
231 | facility = LOG_USER; | |
232 | level = decode(s, prioritynames); | |
233 | if (level < 0) | |
234 | errx(EXIT_FAILURE, _("unknown priority name: %s"), s); | |
9a13f968 SK |
235 | if (facility == LOG_KERN) |
236 | facility = LOG_USER; /* kern is forbidden */ | |
2e0fd22d | 237 | return ((level & LOG_PRIMASK) | (facility & LOG_FACMASK)); |
82054b1d DR |
238 | } |
239 | ||
fc20393c | 240 | static int unix_socket(struct logger_ctl *ctl, const char *path, int *socket_type) |
fe6999da | 241 | { |
4813a521 | 242 | int fd = -1, i, type = -1; |
fe6999da | 243 | static struct sockaddr_un s_addr; /* AF_UNIX address of local logger */ |
7eda085c | 244 | |
68265d07 SK |
245 | if (strlen(path) >= sizeof(s_addr.sun_path)) |
246 | errx(EXIT_FAILURE, _("openlog %s: pathname too long"), path); | |
7eda085c | 247 | |
fe6999da | 248 | s_addr.sun_family = AF_UNIX; |
68265d07 SK |
249 | strcpy(s_addr.sun_path, path); |
250 | ||
251 | for (i = 2; i; i--) { | |
252 | int st = -1; | |
49999d6a | 253 | |
fc20393c | 254 | if (i == 2 && *socket_type & TYPE_UDP) { |
68265d07 | 255 | st = SOCK_DGRAM; |
fc20393c KZ |
256 | type = TYPE_UDP; |
257 | } | |
258 | if (i == 1 && *socket_type & TYPE_TCP) { | |
68265d07 | 259 | st = SOCK_STREAM; |
fc20393c KZ |
260 | type = TYPE_TCP; |
261 | } | |
68265d07 SK |
262 | if (st == -1 || (fd = socket(AF_UNIX, st, 0)) == -1) |
263 | continue; | |
fe6999da SK |
264 | if (connect(fd, (struct sockaddr *)&s_addr, sizeof(s_addr)) == -1) { |
265 | close(fd); | |
68265d07 | 266 | continue; |
fe6999da | 267 | } |
68265d07 | 268 | break; |
fe6999da | 269 | } |
7eda085c | 270 | |
d77dc29e SK |
271 | if (i == 0) { |
272 | if (ctl->unix_socket_errors) | |
273 | err(EXIT_FAILURE, _("socket %s"), path); | |
28bad822 | 274 | |
e92d55e6 | 275 | /* write_output() will try to reconnect */ |
28bad822 | 276 | return -1; |
d77dc29e | 277 | } |
fc20393c KZ |
278 | |
279 | /* replace ALL_TYPES with the real TYPE_* */ | |
280 | if (type > 0 && type != *socket_type) | |
281 | *socket_type = type; | |
fe6999da | 282 | return fd; |
7eda085c KZ |
283 | } |
284 | ||
fc20393c | 285 | static int inet_socket(const char *servername, const char *port, int *socket_type) |
68265d07 | 286 | { |
fc20393c | 287 | int fd, errcode, i, type = -1; |
24f4db69 | 288 | struct addrinfo hints, *res; |
68265d07 SK |
289 | const char *p = port; |
290 | ||
291 | for (i = 2; i; i--) { | |
292 | memset(&hints, 0, sizeof(hints)); | |
fc20393c | 293 | if (i == 2 && *socket_type & TYPE_UDP) { |
68265d07 | 294 | hints.ai_socktype = SOCK_DGRAM; |
fc20393c | 295 | type = TYPE_UDP; |
68265d07 SK |
296 | if (port == NULL) |
297 | p = "syslog"; | |
298 | } | |
fc20393c | 299 | if (i == 1 && *socket_type & TYPE_TCP) { |
68265d07 | 300 | hints.ai_socktype = SOCK_STREAM; |
fc20393c | 301 | type = TYPE_TCP; |
68265d07 SK |
302 | if (port == NULL) |
303 | p = "syslog-conn"; | |
304 | } | |
305 | if (hints.ai_socktype == 0) | |
306 | continue; | |
307 | hints.ai_family = AF_UNSPEC; | |
308 | errcode = getaddrinfo(servername, p, &hints, &res); | |
309 | if (errcode != 0) | |
4ce393f4 | 310 | errx(EXIT_FAILURE, _("failed to resolve name %s port %s: %s"), |
68265d07 SK |
311 | servername, p, gai_strerror(errcode)); |
312 | if ((fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) { | |
313 | freeaddrinfo(res); | |
314 | continue; | |
315 | } | |
316 | if (connect(fd, res->ai_addr, res->ai_addrlen) == -1) { | |
317 | freeaddrinfo(res); | |
318 | close(fd); | |
319 | continue; | |
320 | } | |
24f4db69 | 321 | |
68265d07 SK |
322 | freeaddrinfo(res); |
323 | break; | |
324 | } | |
912d6b98 | 325 | |
68265d07 | 326 | if (i == 0) |
4ce393f4 | 327 | errx(EXIT_FAILURE, _("failed to connect to %s port %s"), servername, p); |
49999d6a | 328 | |
fc20393c KZ |
329 | /* replace ALL_TYPES with the real TYPE_* */ |
330 | if (type > 0 && type != *socket_type) | |
331 | *socket_type = type; | |
912d6b98 WJE |
332 | return fd; |
333 | } | |
68265d07 | 334 | |
ebff016a | 335 | #ifdef HAVE_LIBSYSTEMD |
fd343a05 | 336 | static int journald_entry(struct logger_ctl *ctl, FILE *fp) |
4b670c01 SK |
337 | { |
338 | struct iovec *iovec; | |
339 | char *buf = NULL; | |
340 | ssize_t sz; | |
1c4a2600 | 341 | int n, lines = 0, vectors = 8, ret = 0, msgline = -1; |
4b670c01 SK |
342 | size_t dummy = 0; |
343 | ||
344 | iovec = xmalloc(vectors * sizeof(struct iovec)); | |
1c4a2600 | 345 | while (1) { |
4b670c01 SK |
346 | buf = NULL; |
347 | sz = getline(&buf, &dummy, fp); | |
f1f5f21e SK |
348 | if (sz == -1 || |
349 | (sz = rtrim_whitespace((unsigned char *) buf)) == 0) { | |
4e5411f6 | 350 | free(buf); |
4b670c01 | 351 | break; |
4e5411f6 | 352 | } |
1c4a2600 KZ |
353 | |
354 | if (strncmp(buf, "MESSAGE=", 8) == 0) { | |
355 | if (msgline == -1) | |
356 | msgline = lines; /* remember the first message */ | |
357 | else { | |
358 | char *p = xrealloc(iovec[msgline].iov_base, | |
359 | iovec[msgline].iov_len + sz - 8 + 2); | |
360 | ||
361 | iovec[msgline].iov_base = p; | |
362 | p += iovec[msgline].iov_len; | |
363 | *p++ = '\n'; | |
364 | memcpy(p, buf + 8, sz - 8); | |
365 | free(buf); | |
366 | ||
367 | iovec[msgline].iov_len += sz - 8 + 1; | |
368 | continue; | |
369 | } | |
370 | } | |
371 | ||
4b670c01 SK |
372 | if (lines == vectors) { |
373 | vectors *= 2; | |
047e2888 SK |
374 | if (IOV_MAX < vectors) |
375 | errx(EXIT_FAILURE, _("maximum input lines (%d) exceeded"), IOV_MAX); | |
4b670c01 SK |
376 | iovec = xrealloc(iovec, vectors * sizeof(struct iovec)); |
377 | } | |
378 | iovec[lines].iov_base = buf; | |
379 | iovec[lines].iov_len = sz; | |
1c4a2600 | 380 | ++lines; |
4b670c01 | 381 | } |
fd343a05 KZ |
382 | |
383 | if (!ctl->noact) | |
384 | ret = sd_journal_sendv(iovec, lines); | |
385 | if (ctl->stderr_printout) { | |
386 | for (n = 0; n < lines; n++) | |
387 | fprintf(stderr, "%s\n", (char *) iovec[n].iov_base); | |
388 | } | |
4b670c01 SK |
389 | for (n = 0; n < lines; n++) |
390 | free(iovec[n].iov_base); | |
391 | free(iovec); | |
392 | return ret; | |
393 | } | |
394 | #endif | |
395 | ||
c8598d8a | 396 | static char const *xgetlogin(void) |
019b9702 | 397 | { |
c8598d8a | 398 | char const *cp; |
019b9702 SK |
399 | struct passwd *pw; |
400 | ||
401 | if (!(cp = getlogin()) || !*cp) | |
402 | cp = (pw = getpwuid(geteuid()))? pw->pw_name : "<someone>"; | |
403 | return cp; | |
404 | } | |
405 | ||
3070ca77 RG |
406 | /* this creates a timestamp based on current time according to the |
407 | * fine rules of RFC3164, most importantly it ensures in a portable | |
408 | * way that the month day is correctly written (with a SP instead | |
409 | * of a leading 0). The function uses a static buffer which is | |
410 | * overwritten on the next call (just like ctime() does). | |
411 | */ | |
c8598d8a | 412 | static char const *rfc3164_current_time(void) |
3070ca77 RG |
413 | { |
414 | static char time[32]; | |
415 | struct timeval tv; | |
416 | struct tm *tm; | |
c8598d8a | 417 | static char const * const monthnames[] = { |
9a13f968 SK |
418 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", |
419 | "Sep", "Oct", "Nov", "Dec" | |
420 | }; | |
3070ca77 | 421 | |
ef5fb280 | 422 | logger_gettimeofday(&tv, NULL); |
3070ca77 RG |
423 | tm = localtime(&tv.tv_sec); |
424 | snprintf(time, sizeof(time),"%s %2d %2.2d:%2.2d:%2.2d", | |
425 | monthnames[tm->tm_mon], tm->tm_mday, | |
0f1c825b | 426 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
3070ca77 RG |
427 | return time; |
428 | } | |
429 | ||
17c8aa1d | 430 | #define next_iovec(ary, idx) __extension__ ({ \ |
b9710f1f | 431 | assert(ARRAY_SIZE(ary) > (size_t)idx); \ |
17c8aa1d KZ |
432 | assert(idx >= 0); \ |
433 | &ary[idx++]; \ | |
434 | }) | |
435 | ||
436 | #define iovec_add_string(ary, idx, str, len) \ | |
437 | do { \ | |
438 | struct iovec *v = next_iovec(ary, idx); \ | |
439 | v->iov_base = (void *) str; \ | |
440 | v->iov_len = len ? len : strlen(str); \ | |
441 | } while (0) | |
442 | ||
443 | #define iovec_memcmp(ary, idx, str, len) \ | |
444 | memcmp((ary)[(idx) - 1].iov_base, str, len) | |
445 | ||
940a14a3 | 446 | /* writes generated buffer to desired destination. For TCP syslog, |
b6b67955 AB |
447 | * we use RFC6587 octet-stuffing (unless octet-counting is selected). |
448 | * This is not great, but doing full blown RFC5425 (TLS) looks like | |
449 | * it is too much for the logger utility. If octet-counting is | |
450 | * selected, we use that. | |
940a14a3 | 451 | */ |
caf6ac6e | 452 | static void write_output(struct logger_ctl *ctl, const char *const msg) |
4be84306 | 453 | { |
17c8aa1d KZ |
454 | struct iovec iov[4]; |
455 | int iovlen = 0; | |
456 | char *octet = NULL; | |
457 | ||
e92d55e6 KZ |
458 | /* initial connect failed? */ |
459 | if (!ctl->noact && !is_connected(ctl)) | |
460 | logger_reopen(ctl); | |
461 | ||
17c8aa1d KZ |
462 | /* 1) octen count */ |
463 | if (ctl->octet_count) { | |
464 | size_t len = xasprintf(&octet, "%zu ", strlen(ctl->hdr) + strlen(msg)); | |
465 | iovec_add_string(iov, iovlen, octet, len); | |
466 | } | |
467 | ||
468 | /* 2) header */ | |
469 | iovec_add_string(iov, iovlen, ctl->hdr, 0); | |
470 | ||
471 | /* 3) message */ | |
472 | iovec_add_string(iov, iovlen, msg, 0); | |
fd343a05 | 473 | |
e92d55e6 | 474 | if (!ctl->noact && is_connected(ctl)) { |
2ec100a1 | 475 | struct msghdr message = { 0 }; |
4199e989 | 476 | #ifdef SCM_CREDENTIALS |
27a9eb53 KZ |
477 | struct cmsghdr *cmhp; |
478 | struct ucred *cred; | |
479 | union { | |
480 | struct cmsghdr cmh; | |
481 | char control[CMSG_SPACE(sizeof(struct ucred))]; | |
482 | } cbuf; | |
4199e989 | 483 | #endif |
17c8aa1d KZ |
484 | |
485 | /* 4) add extra \n to make sure message is terminated */ | |
486 | if ((ctl->socket_type == TYPE_TCP) && !ctl->octet_count) | |
487 | iovec_add_string(iov, iovlen, "\n", 1); | |
94a28496 | 488 | |
2ec100a1 SK |
489 | message.msg_iov = iov; |
490 | message.msg_iovlen = iovlen; | |
94a28496 | 491 | |
4199e989 | 492 | #ifdef SCM_CREDENTIALS |
27a9eb53 KZ |
493 | /* syslog/journald may follow local socket credentials rather |
494 | * than in the message PID. If we use --id as root than we can | |
495 | * force kernel to accept another valid PID than the real logger(1) | |
496 | * PID. | |
497 | */ | |
498 | if (ctl->pid && !ctl->server && ctl->pid != getpid() | |
499 | && geteuid() == 0 && kill(ctl->pid, 0) == 0) { | |
500 | ||
2ec100a1 SK |
501 | message.msg_control = cbuf.control; |
502 | message.msg_controllen = CMSG_SPACE(sizeof(struct ucred)); | |
27a9eb53 | 503 | |
2ec100a1 | 504 | cmhp = CMSG_FIRSTHDR(&message); |
27a9eb53 KZ |
505 | cmhp->cmsg_len = CMSG_LEN(sizeof(struct ucred)); |
506 | cmhp->cmsg_level = SOL_SOCKET; | |
507 | cmhp->cmsg_type = SCM_CREDENTIALS; | |
508 | cred = (struct ucred *) CMSG_DATA(cmhp); | |
509 | ||
510 | cred->pid = ctl->pid; | |
511 | } | |
4199e989 | 512 | #endif |
caf6ac6e KZ |
513 | /* Note that logger(1) maybe executed for long time (as pipe |
514 | * reader) and connection endpoint (syslogd) may be restarted. | |
515 | * | |
516 | * The libc syslog() function reconnects on failed send(). | |
517 | * Let's do the same to be robust. [kzak -- Oct 2017] | |
87ac63c9 KZ |
518 | * |
519 | * MSG_NOSIGNAL is POSIX.1-2008 compatible, but it for example | |
73afd3f8 | 520 | * not supported by apple-darwin15.6.0. |
caf6ac6e | 521 | */ |
87ac63c9 KZ |
522 | #ifndef MSG_NOSIGNAL |
523 | # define MSG_NOSIGNAL 0 | |
524 | #endif | |
caf6ac6e KZ |
525 | if (sendmsg(ctl->fd, &message, MSG_NOSIGNAL) < 0) { |
526 | logger_reopen(ctl); | |
527 | if (sendmsg(ctl->fd, &message, MSG_NOSIGNAL) < 0) | |
528 | warn(_("send message failed")); | |
529 | } | |
fd343a05 | 530 | } |
17c8aa1d KZ |
531 | |
532 | if (ctl->stderr_printout) { | |
533 | /* make sure it's terminated for stderr */ | |
534 | if (iovec_memcmp(iov, iovlen, "\n", 1) != 0) | |
535 | iovec_add_string(iov, iovlen, "\n", 1); | |
536 | ||
537 | ignore_result( writev(STDERR_FILENO, iov, iovlen) ); | |
538 | } | |
539 | ||
540 | free(octet); | |
4be84306 RG |
541 | } |
542 | ||
d5f93061 | 543 | #define NILVALUE "-" |
2b3f40c5 | 544 | static void syslog_rfc3164_header(struct logger_ctl *const ctl) |
195c3603 | 545 | { |
d5f93061 | 546 | char pid[30], *hostname; |
d8b616c2 | 547 | |
77c3bd5b | 548 | *pid = '\0'; |
59c6ac0b KZ |
549 | if (ctl->pid) |
550 | snprintf(pid, sizeof(pid), "[%d]", ctl->pid); | |
852feb72 | 551 | |
ef5fb280 | 552 | if ((hostname = logger_xgethostname())) { |
d5f93061 SK |
553 | char *dot = strchr(hostname, '.'); |
554 | if (dot) | |
555 | *dot = '\0'; | |
556 | } else | |
557 | hostname = xstrdup(NILVALUE); | |
852feb72 | 558 | |
2b3f40c5 RG |
559 | xasprintf(&ctl->hdr, "<%d>%.15s %s %.200s%s: ", |
560 | ctl->pri, rfc3164_current_time(), hostname, ctl->tag, pid); | |
852feb72 KZ |
561 | |
562 | free(hostname); | |
7eda085c KZ |
563 | } |
564 | ||
4299ed1c KZ |
565 | static inline struct list_head *get_user_structured_data(struct logger_ctl *ctl) |
566 | { | |
567 | return &ctl->user_sds; | |
568 | } | |
569 | ||
570 | static inline struct list_head *get_reserved_structured_data(struct logger_ctl *ctl) | |
571 | { | |
572 | return &ctl->reserved_sds; | |
573 | } | |
574 | ||
575 | static int has_structured_data_id(struct list_head *ls, const char *id) | |
576 | { | |
577 | struct list_head *p; | |
578 | ||
579 | if (!ls || list_empty(ls)) | |
580 | return 0; | |
581 | ||
582 | list_for_each(p, ls) { | |
583 | struct structured_data *sd = list_entry(p, struct structured_data, sds); | |
584 | if (sd->id && strcmp(sd->id, id) == 0) | |
585 | return 1; | |
586 | } | |
587 | ||
588 | return 0; | |
589 | } | |
590 | ||
591 | static void add_structured_data_id(struct list_head *ls, const char *id) | |
592 | { | |
593 | struct structured_data *sd; | |
594 | ||
595 | assert(id); | |
596 | ||
597 | if (has_structured_data_id(ls, id)) | |
598 | errx(EXIT_FAILURE, _("structured data ID '%s' is not unique"), id); | |
599 | ||
600 | sd = xcalloc(1, sizeof(*sd)); | |
601 | INIT_LIST_HEAD(&sd->sds); | |
602 | sd->id = xstrdup(id); | |
603 | ||
604 | list_add_tail(&sd->sds, ls); | |
605 | } | |
606 | ||
607 | static void add_structured_data_param(struct list_head *ls, const char *param) | |
608 | { | |
609 | struct structured_data *sd; | |
610 | ||
611 | if (list_empty(ls)) | |
426cdc0a | 612 | errx(EXIT_FAILURE, _("--sd-id was not specified for --sd-param %s"), param); |
4299ed1c KZ |
613 | |
614 | assert(param); | |
615 | ||
616 | sd = list_last_entry(ls, struct structured_data, sds); | |
617 | ||
618 | if (strv_extend(&sd->params, param)) | |
619 | err_oom(); | |
620 | } | |
621 | ||
622 | static void add_structured_data_paramf(struct list_head *ls, const char *fmt, ...) | |
623 | { | |
624 | struct structured_data *sd; | |
625 | va_list ap; | |
626 | int x; | |
627 | ||
628 | assert(!list_empty(ls)); | |
629 | assert(fmt); | |
630 | ||
631 | sd = list_last_entry(ls, struct structured_data, sds); | |
632 | va_start(ap, fmt); | |
633 | x = strv_extendv(&sd->params, fmt, ap); | |
634 | va_end(ap); | |
635 | ||
636 | if (x) | |
637 | err_oom(); | |
638 | } | |
639 | ||
640 | static char *strdup_structured_data(struct structured_data *sd) | |
641 | { | |
642 | char *res, *tmp; | |
643 | ||
644 | if (strv_isempty(sd->params)) | |
645 | return NULL; | |
646 | ||
647 | xasprintf(&res, "[%s %s]", sd->id, | |
648 | (tmp = strv_join(sd->params, " "))); | |
649 | free(tmp); | |
650 | return res; | |
651 | } | |
652 | ||
653 | static char *strdup_structured_data_list(struct list_head *ls) | |
654 | { | |
655 | struct list_head *p; | |
656 | char *res = NULL; | |
657 | ||
658 | list_for_each(p, ls) { | |
659 | struct structured_data *sd = list_entry(p, struct structured_data, sds); | |
660 | char *one = strdup_structured_data(sd); | |
661 | char *tmp = res; | |
662 | ||
663 | if (!one) | |
664 | continue; | |
665 | res = strappend(tmp, one); | |
666 | free(tmp); | |
667 | free(one); | |
668 | } | |
669 | ||
670 | return res; | |
671 | } | |
672 | ||
673 | static char *get_structured_data_string(struct logger_ctl *ctl) | |
674 | { | |
675 | char *sys = NULL, *usr = NULL, *res; | |
676 | ||
677 | if (!list_empty(&ctl->reserved_sds)) | |
678 | sys = strdup_structured_data_list(&ctl->reserved_sds); | |
679 | if (!list_empty(&ctl->user_sds)) | |
680 | usr = strdup_structured_data_list(&ctl->user_sds); | |
681 | ||
682 | if (sys && usr) { | |
683 | res = strappend(sys, usr); | |
684 | free(sys); | |
685 | free(usr); | |
686 | } else | |
687 | res = sys ? sys : usr; | |
688 | ||
689 | return res; | |
690 | } | |
691 | ||
692 | static int valid_structured_data_param(const char *str) | |
693 | { | |
694 | char *eq = strchr(str, '='), | |
695 | *qm1 = strchr(str, '"'), | |
696 | *qm2 = qm1 ? strchr(qm1 + 1, '"') : NULL; | |
697 | ||
698 | if (!eq || !qm1 || !qm2) /* something is missing */ | |
699 | return 0; | |
700 | ||
701 | /* foo="bar" */ | |
702 | return eq > str && eq < qm1 && eq + 1 == qm1 && qm1 < qm2 && *(qm2 + 1) == '\0'; | |
703 | } | |
704 | ||
705 | /* SD-ID format: | |
706 | * name@<private enterprise number>, e.g., "ourSDID@32473" | |
707 | */ | |
708 | static int valid_structured_data_id(const char *str) | |
709 | { | |
710 | char *at = strchr(str, '@'); | |
711 | const char *p; | |
712 | ||
713 | /* standardized IDs without @<digits> */ | |
714 | if (!at && (strcmp(str, "timeQuality") == 0 || | |
715 | strcmp(str, "origin") == 0 || | |
716 | strcmp(str, "meta") == 0)) | |
717 | return 1; | |
718 | ||
719 | if (!at || at == str || !*(at + 1)) | |
720 | return 0; | |
6d8a31f6 KZ |
721 | |
722 | /* <digits> or <digits>.<digits>[...] */ | |
723 | for (p = at + 1; p && *p; p++) { | |
724 | const char *end; | |
725 | ||
726 | if (isdigit_strend(p, &end)) | |
727 | break; /* only digits in the string */ | |
728 | ||
729 | if (end == NULL || end == p || | |
730 | *end != '.' || *(end + 1) == '\0') | |
731 | return 0; | |
732 | p = end; | |
733 | } | |
4299ed1c KZ |
734 | |
735 | /* check for forbidden chars in the <name> */ | |
736 | for (p = str; p < at; p++) { | |
737 | if (*p == '[' || *p == '=' || *p == '"' || *p == '@') | |
738 | return 0; | |
bae57b5a | 739 | if (isblank((unsigned char) *p) || iscntrl((unsigned char) *p)) |
4299ed1c KZ |
740 | return 0; |
741 | } | |
742 | return 1; | |
743 | } | |
744 | ||
745 | ||
746 | /* Some field mappings may be controversial, thus I give the reason | |
4826184b RG |
747 | * why this specific mapping was used: |
748 | * APP-NAME <-- tag | |
749 | * Some may argue that "logger" is a better fit, but we think | |
750 | * this is better inline of what other implementations do. In | |
751 | * rsyslog, for example, the TAG value is populated from APP-NAME. | |
752 | * PROCID <-- pid | |
753 | * This is a relatively straightforward interpretation from | |
754 | * RFC5424, sect. 6.2.6. | |
55f5bc66 | 755 | * MSGID <-- msgid (from --msgid) |
4826184b RG |
756 | * One may argue that the string "logger" would be better suited |
757 | * here so that a receiver can identify the sender process. | |
758 | * However, this does not sound like a good match to RFC5424, | |
55f5bc66 | 759 | * sect. 6.2.7. |
4826184b RG |
760 | * Note that appendix A.1 of RFC5424 does not provide clear guidance |
761 | * of how these fields should be used. This is the case because the | |
762 | * IETF working group couldn't arrive at a clear agreement when we | |
763 | * specified RFC5424. The rest of the field mappings should be | |
764 | * pretty clear from RFC5424. -- Rainer Gerhards, 2015-03-10 | |
765 | */ | |
2b3f40c5 | 766 | static void syslog_rfc5424_header(struct logger_ctl *const ctl) |
4de2e8a0 | 767 | { |
9a13f968 SK |
768 | char *time; |
769 | char *hostname; | |
c8598d8a | 770 | char const *app_name = ctl->tag; |
9a13f968 SK |
771 | char *procid; |
772 | char *const msgid = xstrdup(ctl->msgid ? ctl->msgid : NILVALUE); | |
4299ed1c KZ |
773 | char *structured = NULL; |
774 | struct list_head *sd; | |
9a13f968 | 775 | |
4de2e8a0 | 776 | if (ctl->rfc5424_time) { |
4826184b RG |
777 | struct timeval tv; |
778 | struct tm *tm; | |
9a13f968 | 779 | |
ef5fb280 | 780 | logger_gettimeofday(&tv, NULL); |
4de2e8a0 | 781 | if ((tm = localtime(&tv.tv_sec)) != NULL) { |
852feb72 | 782 | char fmt[64]; |
2f267611 | 783 | const size_t i = strftime(fmt, sizeof(fmt), |
9a13f968 | 784 | "%Y-%m-%dT%H:%M:%S.%%06u%z ", tm); |
2f267611 | 785 | /* patch TZ info to comply with RFC3339 (we left SP at end) */ |
9a13f968 SK |
786 | fmt[i - 1] = fmt[i - 2]; |
787 | fmt[i - 2] = fmt[i - 3]; | |
788 | fmt[i - 3] = ':'; | |
4826184b | 789 | xasprintf(&time, fmt, tv.tv_usec); |
4de2e8a0 SK |
790 | } else |
791 | err(EXIT_FAILURE, _("localtime() failed")); | |
4826184b | 792 | } else |
7ff6948e | 793 | time = xstrdup(NILVALUE); |
852feb72 | 794 | |
4de2e8a0 | 795 | if (ctl->rfc5424_host) { |
ef5fb280 | 796 | if (!(hostname = logger_xgethostname())) |
d5f93061 | 797 | hostname = xstrdup(NILVALUE); |
4de2e8a0 SK |
798 | /* Arbitrary looking 'if (var < strlen()) checks originate from |
799 | * RFC 5424 - 6 Syslog Message Format definition. */ | |
800 | if (255 < strlen(hostname)) | |
801 | errx(EXIT_FAILURE, _("hostname '%s' is too long"), | |
802 | hostname); | |
4826184b | 803 | } else |
7ff6948e | 804 | hostname = xstrdup(NILVALUE); |
852feb72 | 805 | |
2b3f40c5 RG |
806 | if (48 < strlen(ctl->tag)) |
807 | errx(EXIT_FAILURE, _("tag '%s' is too long"), ctl->tag); | |
852feb72 | 808 | |
59c6ac0b | 809 | if (ctl->pid) |
4826184b RG |
810 | xasprintf(&procid, "%d", ctl->pid); |
811 | else | |
7ff6948e | 812 | procid = xstrdup(NILVALUE); |
4826184b | 813 | |
4299ed1c KZ |
814 | sd = get_reserved_structured_data(ctl); |
815 | ||
9e930041 | 816 | /* time quality structured data (maybe overwritten by --sd-id timeQuality) */ |
4299ed1c KZ |
817 | if (ctl->rfc5424_tq && !has_structured_data_id(sd, "timeQuality")) { |
818 | ||
819 | add_structured_data_id(sd, "timeQuality"); | |
820 | add_structured_data_param(sd, "tzKnown=\"1\""); | |
821 | ||
7d3a07d8 KZ |
822 | #ifdef HAVE_NTP_GETTIME |
823 | struct ntptimeval ntptv; | |
9a13f968 | 824 | |
4299ed1c KZ |
825 | if (ntp_gettime(&ntptv) == TIME_OK) { |
826 | add_structured_data_param(sd, "isSynced=\"1\""); | |
827 | add_structured_data_paramf(sd, "syncAccuracy=\"%ld\"", ntptv.maxerror); | |
828 | } else | |
87ee2658 | 829 | #endif |
4299ed1c KZ |
830 | add_structured_data_paramf(sd, "isSynced=\"0\""); |
831 | } | |
832 | ||
833 | /* convert all structured data to string */ | |
834 | structured = get_structured_data_string(ctl); | |
835 | if (!structured) | |
836 | structured = xstrdup(NILVALUE); | |
852feb72 | 837 | |
4826184b RG |
838 | xasprintf(&ctl->hdr, "<%d>1 %s %s %s %s %s %s ", |
839 | ctl->pri, | |
840 | time, | |
841 | hostname, | |
842 | app_name, | |
843 | procid, | |
844 | msgid, | |
4299ed1c | 845 | structured); |
852feb72 | 846 | |
4826184b | 847 | free(time); |
852feb72 | 848 | free(hostname); |
4826184b RG |
849 | /* app_name points to ctl->tag, do NOT free! */ |
850 | free(procid); | |
851 | free(msgid); | |
4299ed1c | 852 | free(structured); |
4de2e8a0 SK |
853 | } |
854 | ||
eb2306e6 | 855 | static void parse_rfc5424_flags(struct logger_ctl *ctl, char *s) |
4de2e8a0 SK |
856 | { |
857 | char *in, *tok; | |
858 | ||
eb2306e6 | 859 | in = s; |
4de2e8a0 SK |
860 | while ((tok = strtok(in, ","))) { |
861 | in = NULL; | |
862 | if (!strcmp(tok, "notime")) { | |
863 | ctl->rfc5424_time = 0; | |
864 | ctl->rfc5424_tq = 0; | |
865 | } else if (!strcmp(tok, "notq")) | |
866 | ctl->rfc5424_tq = 0; | |
867 | else if (!strcmp(tok, "nohost")) | |
868 | ctl->rfc5424_host = 0; | |
869 | else | |
870 | warnx(_("ignoring unknown option argument: %s"), tok); | |
871 | } | |
872 | } | |
873 | ||
eb2306e6 | 874 | static int parse_unix_socket_errors_flags(char *s) |
d77dc29e | 875 | { |
eb2306e6 | 876 | if (!strcmp(s, "off")) |
d77dc29e | 877 | return AF_UNIX_ERRORS_OFF; |
eb2306e6 | 878 | if (!strcmp(s, "on")) |
d77dc29e | 879 | return AF_UNIX_ERRORS_ON; |
eb2306e6 | 880 | if (!strcmp(s, "auto")) |
d77dc29e | 881 | return AF_UNIX_ERRORS_AUTO; |
eb2306e6 | 882 | warnx(_("invalid argument: %s: using automatic errors"), s); |
d77dc29e SK |
883 | return AF_UNIX_ERRORS_AUTO; |
884 | } | |
885 | ||
2b3f40c5 | 886 | static void syslog_local_header(struct logger_ctl *const ctl) |
cfa77d26 | 887 | { |
3070ca77 | 888 | char pid[32]; |
1d575033 | 889 | |
59c6ac0b KZ |
890 | if (ctl->pid) |
891 | snprintf(pid, sizeof(pid), "[%d]", ctl->pid); | |
1d575033 SK |
892 | else |
893 | pid[0] = '\0'; | |
894 | ||
2b3f40c5 RG |
895 | xasprintf(&ctl->hdr, "<%d>%s %s%s: ", ctl->pri, rfc3164_current_time(), |
896 | ctl->tag, pid); | |
897 | } | |
898 | ||
899 | static void generate_syslog_header(struct logger_ctl *const ctl) | |
900 | { | |
901 | free(ctl->hdr); | |
2d4c226d | 902 | ctl->hdr = NULL; |
2b3f40c5 | 903 | ctl->syslogfp(ctl); |
cfa77d26 SK |
904 | } |
905 | ||
caf6ac6e KZ |
906 | /* just open, nothing else */ |
907 | static void __logger_open(struct logger_ctl *ctl) | |
c68a1cb4 SK |
908 | { |
909 | if (ctl->server) { | |
fc20393c | 910 | ctl->fd = inet_socket(ctl->server, ctl->port, &ctl->socket_type); |
4a8919a4 PP |
911 | } else { |
912 | if (!ctl->unix_socket) | |
913 | ctl->unix_socket = _PATH_DEVLOG; | |
7dc20804 | 914 | |
fc20393c | 915 | ctl->fd = unix_socket(ctl, ctl->unix_socket, &ctl->socket_type); |
4a8919a4 | 916 | } |
caf6ac6e KZ |
917 | } |
918 | ||
919 | /* open and initialize relevant @ctl tuff */ | |
920 | static void logger_open(struct logger_ctl *ctl) | |
921 | { | |
922 | __logger_open(ctl); | |
923 | ||
924 | if (!ctl->syslogfp) | |
925 | ctl->syslogfp = ctl->server ? syslog_rfc5424_header : | |
926 | syslog_local_header; | |
9a13f968 SK |
927 | if (!ctl->tag) |
928 | ctl->tag = xgetlogin(); | |
caf6ac6e | 929 | |
2b3f40c5 | 930 | generate_syslog_header(ctl); |
c68a1cb4 SK |
931 | } |
932 | ||
caf6ac6e KZ |
933 | /* re-open; usually after failed connection */ |
934 | static void logger_reopen(struct logger_ctl *ctl) | |
935 | { | |
936 | if (ctl->fd != -1) | |
937 | close(ctl->fd); | |
938 | ctl->fd = -1; | |
939 | ||
940 | __logger_open(ctl); | |
941 | } | |
942 | ||
943 | static void logger_command_line(struct logger_ctl *ctl, char **argv) | |
c68a1cb4 | 944 | { |
2b3f40c5 RG |
945 | /* note: we never re-generate the syslog header here, even if we |
946 | * generate multiple messages. If so, we think it is the right thing | |
947 | * to do to report them with the same timestamp, as the user actually | |
948 | * intended to send a single message. | |
949 | */ | |
f68b8aa7 | 950 | char *const buf = xmalloc(ctl->max_message_size + 1); |
c68a1cb4 | 951 | char *p = buf; |
f68b8aa7 | 952 | const char *endp = buf + ctl->max_message_size - 1; |
c68a1cb4 SK |
953 | size_t len; |
954 | ||
955 | while (*argv) { | |
956 | len = strlen(*argv); | |
957 | if (endp < p + len && p != buf) { | |
2b3f40c5 | 958 | write_output(ctl, buf); |
c68a1cb4 SK |
959 | p = buf; |
960 | } | |
f68b8aa7 RG |
961 | if (ctl->max_message_size < len) { |
962 | (*argv)[ctl->max_message_size] = '\0'; /* truncate */ | |
2b3f40c5 | 963 | write_output(ctl, *argv++); |
c68a1cb4 SK |
964 | continue; |
965 | } | |
966 | if (p != buf) | |
967 | *p++ = ' '; | |
968 | memmove(p, *argv++, len); | |
969 | *(p += len) = '\0'; | |
970 | } | |
971 | if (p != buf) | |
2b3f40c5 | 972 | write_output(ctl, buf); |
c3dd2ecd | 973 | free(buf); |
c68a1cb4 SK |
974 | } |
975 | ||
976 | static void logger_stdin(struct logger_ctl *ctl) | |
977 | { | |
31fb9453 | 978 | /* note: we re-generate the syslog header for each log message to |
7db029e5 KZ |
979 | * update header timestamps and to reflect possible priority changes. |
980 | * The initial header is generated by logger_open(). | |
981 | */ | |
982 | int has_header = 1; | |
c68a1cb4 | 983 | int default_priority = ctl->pri; |
b9ef27f5 RG |
984 | int last_pri = default_priority; |
985 | size_t max_usrmsg_size = ctl->max_message_size - strlen(ctl->hdr); | |
986 | char *const buf = xmalloc(max_usrmsg_size + 2 + 2); | |
987 | int pri; | |
988 | int c; | |
989 | size_t i; | |
990 | ||
991 | c = getchar(); | |
992 | while (c != EOF) { | |
993 | i = 0; | |
da0788fb SK |
994 | if (ctl->prio_prefix && c == '<') { |
995 | pri = 0; | |
996 | buf[i++] = c; | |
997 | while (isdigit(c = getchar()) && pri <= 191) { | |
998 | buf[i++] = c; | |
999 | pri = pri * 10 + c - '0'; | |
1000 | } | |
1001 | if (c != EOF && c != '\n') | |
b9ef27f5 | 1002 | buf[i++] = c; |
da0788fb SK |
1003 | if (c == '>' && 0 <= pri && pri <= 191) { |
1004 | /* valid RFC PRI values */ | |
1005 | i = 0; | |
1006 | if (pri < 8) /* kern facility is forbidden */ | |
1007 | pri |= 8; | |
1008 | ctl->pri = pri; | |
1009 | } else | |
1010 | ctl->pri = default_priority; | |
1011 | ||
1012 | if (ctl->pri != last_pri) { | |
1013 | has_header = 0; | |
1014 | max_usrmsg_size = | |
1015 | ctl->max_message_size - strlen(ctl->hdr); | |
1016 | last_pri = ctl->pri; | |
b9ef27f5 | 1017 | } |
da0788fb SK |
1018 | if (c != EOF && c != '\n') |
1019 | c = getchar(); | |
b9ef27f5 RG |
1020 | } |
1021 | ||
1022 | while (c != EOF && c != '\n' && i < max_usrmsg_size) { | |
1023 | buf[i++] = c; | |
1024 | c = getchar(); | |
1025 | } | |
1026 | buf[i] = '\0'; | |
1027 | ||
7db029e5 KZ |
1028 | if (i > 0 || !ctl->skip_empty_lines) { |
1029 | if (!has_header) | |
1030 | generate_syslog_header(ctl); | |
ae6846b8 | 1031 | write_output(ctl, buf); |
7db029e5 KZ |
1032 | has_header = 0; |
1033 | } | |
b9ef27f5 | 1034 | |
9a13f968 | 1035 | if (c == '\n') /* discard line terminator */ |
b9ef27f5 | 1036 | c = getchar(); |
c68a1cb4 | 1037 | } |
8b318eb2 KZ |
1038 | |
1039 | free(buf); | |
c68a1cb4 SK |
1040 | } |
1041 | ||
46ee14df | 1042 | static void logger_close(const struct logger_ctl *ctl) |
c68a1cb4 | 1043 | { |
9b83e7a4 | 1044 | if (ctl->fd != -1 && close(ctl->fd) != 0) |
1d575033 | 1045 | err(EXIT_FAILURE, _("close failed")); |
2b3f40c5 | 1046 | free(ctl->hdr); |
c68a1cb4 SK |
1047 | } |
1048 | ||
86be6a32 | 1049 | static void __attribute__((__noreturn__)) usage(void) |
b363e86d | 1050 | { |
86be6a32 | 1051 | FILE *out = stdout; |
925aa9e8 | 1052 | fputs(USAGE_HEADER, out); |
4ce393f4 | 1053 | fprintf(out, _(" %s [options] [<message>]\n"), program_invocation_short_name); |
2da49186 | 1054 | |
451dbcfa BS |
1055 | fputs(USAGE_SEPARATOR, out); |
1056 | fputs(_("Enter messages into the system log.\n"), out); | |
1057 | ||
925aa9e8 | 1058 | fputs(USAGE_OPTIONS, out); |
3f51c10b SK |
1059 | fputs(_(" -i log the logger command's PID\n"), out); |
1060 | fputs(_(" --id[=<id>] log the given <id>, or otherwise the PID\n"), out); | |
d0e875ff | 1061 | fputs(_(" -f, --file <file> log the contents of this file\n"), out); |
ae6846b8 | 1062 | fputs(_(" -e, --skip-empty do not log empty lines when processing files\n"), out); |
fd343a05 | 1063 | fputs(_(" --no-act do everything except the write the log\n"), out); |
d0e875ff | 1064 | fputs(_(" -p, --priority <prio> mark given message with this priority\n"), out); |
b6b67955 | 1065 | fputs(_(" --octet-count use rfc6587 octet counting\n"), out); |
d0e875ff KZ |
1066 | fputs(_(" --prio-prefix look for a prefix on every line read from stdin\n"), out); |
1067 | fputs(_(" -s, --stderr output message to standard error as well\n"), out); | |
f68b8aa7 | 1068 | fputs(_(" -S, --size <size> maximum size for a single message\n"), out); |
d0e875ff KZ |
1069 | fputs(_(" -t, --tag <tag> mark every line with this tag\n"), out); |
1070 | fputs(_(" -n, --server <name> write to this remote syslog server\n"), out); | |
1c722759 | 1071 | fputs(_(" -P, --port <port> use this port for UDP or TCP connection\n"), out); |
d0e875ff KZ |
1072 | fputs(_(" -T, --tcp use TCP only\n"), out); |
1073 | fputs(_(" -d, --udp use UDP only\n"), out); | |
1074 | fputs(_(" --rfc3164 use the obsolete BSD syslog protocol\n"), out); | |
2cb40465 | 1075 | fputs(_(" --rfc5424[=<snip>] use the syslog protocol (the default for remote);\n" |
d0e875ff | 1076 | " <snip> can be notime, or notq, and/or nohost\n"), out); |
4299ed1c KZ |
1077 | fputs(_(" --sd-id <id> rfc5424 structured data ID\n"), out); |
1078 | fputs(_(" --sd-param <data> rfc5424 structured data name=value\n"), out); | |
8fce3924 | 1079 | fputs(_(" --msgid <msgid> set rfc5424 message id field\n"), out); |
d0e875ff | 1080 | fputs(_(" -u, --socket <socket> write to this Unix socket\n"), out); |
d77dc29e SK |
1081 | fputs(_(" --socket-errors[=<on|off|auto>]\n" |
1082 | " print connection errors when using Unix sockets\n"), out); | |
ebff016a | 1083 | #ifdef HAVE_LIBSYSTEMD |
4b670c01 SK |
1084 | fputs(_(" --journald[=<file>] write journald entry\n"), out); |
1085 | #endif | |
925aa9e8 KZ |
1086 | |
1087 | fputs(USAGE_SEPARATOR, out); | |
f45f3ec3 RM |
1088 | printf(USAGE_HELP_OPTIONS(26)); |
1089 | printf(USAGE_MAN_TAIL("logger(1)")); | |
b363e86d | 1090 | |
86be6a32 | 1091 | exit(EXIT_SUCCESS); |
b363e86d SK |
1092 | } |
1093 | ||
6dbe3af9 KZ |
1094 | /* |
1095 | * logger -- read and log utility | |
1096 | * | |
1097 | * Reads from an input and arranges to write the result on the system | |
1098 | * log. | |
1099 | */ | |
195c3603 KZ |
1100 | int main(int argc, char **argv) |
1101 | { | |
cfa77d26 SK |
1102 | struct logger_ctl ctl = { |
1103 | .fd = -1, | |
59c6ac0b | 1104 | .pid = 0, |
d0b6c4bf | 1105 | .pri = LOG_USER | LOG_NOTICE, |
c68a1cb4 | 1106 | .prio_prefix = 0, |
cfa77d26 | 1107 | .tag = NULL, |
c68a1cb4 | 1108 | .unix_socket = NULL, |
d77dc29e | 1109 | .unix_socket_errors = 0, |
c68a1cb4 SK |
1110 | .server = NULL, |
1111 | .port = NULL, | |
2b3f40c5 | 1112 | .hdr = NULL, |
55f5bc66 | 1113 | .msgid = NULL, |
4de2e8a0 | 1114 | .socket_type = ALL_TYPES, |
f68b8aa7 | 1115 | .max_message_size = 1024, |
4de2e8a0 SK |
1116 | .rfc5424_time = 1, |
1117 | .rfc5424_tq = 1, | |
1118 | .rfc5424_host = 1, | |
ae6846b8 | 1119 | .skip_empty_lines = 0 |
cfa77d26 | 1120 | }; |
c68a1cb4 | 1121 | int ch; |
3d9f4b1d | 1122 | int stdout_reopened = 0; |
d77dc29e | 1123 | int unix_socket_errors_mode = AF_UNIX_ERRORS_AUTO; |
ebff016a | 1124 | #ifdef HAVE_LIBSYSTEMD |
4b670c01 SK |
1125 | FILE *jfd = NULL; |
1126 | #endif | |
b363e86d | 1127 | static const struct option longopts[] = { |
9a13f968 SK |
1128 | { "id", optional_argument, 0, OPT_ID }, |
1129 | { "stderr", no_argument, 0, 's' }, | |
1130 | { "file", required_argument, 0, 'f' }, | |
fd343a05 | 1131 | { "no-act", no_argument, 0, OPT_NOACT, }, |
9a13f968 SK |
1132 | { "priority", required_argument, 0, 'p' }, |
1133 | { "tag", required_argument, 0, 't' }, | |
1134 | { "socket", required_argument, 0, 'u' }, | |
d77dc29e | 1135 | { "socket-errors", required_argument, 0, OPT_SOCKET_ERRORS }, |
9a13f968 SK |
1136 | { "udp", no_argument, 0, 'd' }, |
1137 | { "tcp", no_argument, 0, 'T' }, | |
1138 | { "server", required_argument, 0, 'n' }, | |
1139 | { "port", required_argument, 0, 'P' }, | |
1140 | { "version", no_argument, 0, 'V' }, | |
1141 | { "help", no_argument, 0, 'h' }, | |
b6b67955 | 1142 | { "octet-count", no_argument, 0, OPT_OCTET_COUNT }, |
9a13f968 SK |
1143 | { "prio-prefix", no_argument, 0, OPT_PRIO_PREFIX }, |
1144 | { "rfc3164", no_argument, 0, OPT_RFC3164 }, | |
1145 | { "rfc5424", optional_argument, 0, OPT_RFC5424 }, | |
1146 | { "size", required_argument, 0, 'S' }, | |
1147 | { "msgid", required_argument, 0, OPT_MSGID }, | |
1148 | { "skip-empty", no_argument, 0, 'e' }, | |
4299ed1c KZ |
1149 | { "sd-id", required_argument, 0, OPT_STRUCTURED_DATA_ID }, |
1150 | { "sd-param", required_argument, 0, OPT_STRUCTURED_DATA_PARAM }, | |
ebff016a | 1151 | #ifdef HAVE_LIBSYSTEMD |
9a13f968 | 1152 | { "journald", optional_argument, 0, OPT_JOURNALD }, |
4b670c01 | 1153 | #endif |
9a13f968 | 1154 | { NULL, 0, 0, 0 } |
b363e86d | 1155 | }; |
7eda085c KZ |
1156 | |
1157 | setlocale(LC_ALL, ""); | |
1158 | bindtextdomain(PACKAGE, LOCALEDIR); | |
1159 | textdomain(PACKAGE); | |
2c308875 | 1160 | close_stdout_atexit(); |
6dbe3af9 | 1161 | |
4299ed1c KZ |
1162 | INIT_LIST_HEAD(&ctl.user_sds); |
1163 | INIT_LIST_HEAD(&ctl.reserved_sds); | |
1164 | ||
ae6846b8 | 1165 | while ((ch = getopt_long(argc, argv, "ef:ip:S:st:u:dTn:P:Vh", |
49999d6a | 1166 | longopts, NULL)) != -1) { |
98920f80 | 1167 | switch (ch) { |
6dbe3af9 | 1168 | case 'f': /* file to log */ |
49999d6a | 1169 | if (freopen(optarg, "r", stdin) == NULL) |
3d9f4b1d SK |
1170 | err(EXIT_FAILURE, _("file %s"), optarg); |
1171 | stdout_reopened = 1; | |
6dbe3af9 | 1172 | break; |
ae6846b8 RG |
1173 | case 'e': |
1174 | ctl.skip_empty_lines = 1; | |
1175 | break; | |
6dbe3af9 | 1176 | case 'i': /* log process id also */ |
ef5fb280 | 1177 | ctl.pid = logger_getpid(); |
3f51c10b SK |
1178 | break; |
1179 | case OPT_ID: | |
aab5b444 | 1180 | if (optarg) { |
e598686d KZ |
1181 | const char *p = optarg; |
1182 | ||
1183 | if (*p == '=') | |
1184 | p++; | |
59c6ac0b KZ |
1185 | ctl.pid = strtoul_or_err(optarg, _("failed to parse id")); |
1186 | } else | |
ef5fb280 | 1187 | ctl.pid = logger_getpid(); |
6dbe3af9 KZ |
1188 | break; |
1189 | case 'p': /* priority */ | |
cfa77d26 | 1190 | ctl.pri = pencode(optarg); |
6dbe3af9 KZ |
1191 | break; |
1192 | case 's': /* log to standard error */ | |
35d36197 | 1193 | ctl.stderr_printout = 1; |
6dbe3af9 KZ |
1194 | break; |
1195 | case 't': /* tag */ | |
cfa77d26 | 1196 | ctl.tag = optarg; |
6dbe3af9 | 1197 | break; |
7eda085c | 1198 | case 'u': /* unix socket */ |
c68a1cb4 | 1199 | ctl.unix_socket = optarg; |
7eda085c | 1200 | break; |
f68b8aa7 RG |
1201 | case 'S': /* max message size */ |
1202 | ctl.max_message_size = strtosize_or_err(optarg, | |
1203 | _("failed to parse message size")); | |
1204 | break; | |
66ee8158 | 1205 | case 'd': |
c68a1cb4 | 1206 | ctl.socket_type = TYPE_UDP; |
68265d07 SK |
1207 | break; |
1208 | case 'T': | |
c68a1cb4 | 1209 | ctl.socket_type = TYPE_TCP; |
66ee8158 | 1210 | break; |
68265d07 | 1211 | case 'n': |
c68a1cb4 | 1212 | ctl.server = optarg; |
912d6b98 | 1213 | break; |
68265d07 | 1214 | case 'P': |
c68a1cb4 | 1215 | ctl.port = optarg; |
912d6b98 | 1216 | break; |
b6b67955 AB |
1217 | case OPT_OCTET_COUNT: |
1218 | ctl.octet_count = 1; | |
1219 | break; | |
98920f80 | 1220 | case OPT_PRIO_PREFIX: |
c68a1cb4 | 1221 | ctl.prio_prefix = 1; |
98920f80 | 1222 | break; |
4de2e8a0 | 1223 | case OPT_RFC3164: |
2b3f40c5 | 1224 | ctl.syslogfp = syslog_rfc3164_header; |
4de2e8a0 SK |
1225 | break; |
1226 | case OPT_RFC5424: | |
2b3f40c5 | 1227 | ctl.syslogfp = syslog_rfc5424_header; |
4de2e8a0 SK |
1228 | if (optarg) |
1229 | parse_rfc5424_flags(&ctl, optarg); | |
1230 | break; | |
55f5bc66 | 1231 | case OPT_MSGID: |
9a13f968 | 1232 | if (strchr(optarg, ' ')) |
8fce3924 | 1233 | errx(EXIT_FAILURE, _("--msgid cannot contain space")); |
55f5bc66 RG |
1234 | ctl.msgid = optarg; |
1235 | break; | |
ebff016a | 1236 | #ifdef HAVE_LIBSYSTEMD |
4b670c01 SK |
1237 | case OPT_JOURNALD: |
1238 | if (optarg) { | |
1239 | jfd = fopen(optarg, "r"); | |
1240 | if (!jfd) | |
1241 | err(EXIT_FAILURE, _("cannot open %s"), | |
1242 | optarg); | |
1243 | } else | |
1244 | jfd = stdin; | |
1245 | break; | |
1246 | #endif | |
d77dc29e SK |
1247 | case OPT_SOCKET_ERRORS: |
1248 | unix_socket_errors_mode = parse_unix_socket_errors_flags(optarg); | |
1249 | break; | |
fd343a05 KZ |
1250 | case OPT_NOACT: |
1251 | ctl.noact = 1; | |
1252 | break; | |
4299ed1c KZ |
1253 | case OPT_STRUCTURED_DATA_ID: |
1254 | if (!valid_structured_data_id(optarg)) | |
1255 | errx(EXIT_FAILURE, _("invalid structured data ID: '%s'"), optarg); | |
1256 | add_structured_data_id(get_user_structured_data(&ctl), optarg); | |
1257 | break; | |
1258 | case OPT_STRUCTURED_DATA_PARAM: | |
1259 | if (!valid_structured_data_param(optarg)) | |
1260 | errx(EXIT_FAILURE, _("invalid structured data parameter: '%s'"), optarg); | |
1261 | add_structured_data_param(get_user_structured_data(&ctl), optarg); | |
1262 | break; | |
2c308875 KZ |
1263 | |
1264 | case 'V': | |
1265 | print_version(EXIT_SUCCESS); | |
1266 | case 'h': | |
1267 | usage(); | |
6dbe3af9 | 1268 | default: |
677ec86c | 1269 | errtryhelp(EXIT_FAILURE); |
6dbe3af9 | 1270 | } |
49999d6a | 1271 | } |
6dbe3af9 KZ |
1272 | argc -= optind; |
1273 | argv += optind; | |
3d9f4b1d SK |
1274 | if (stdout_reopened && argc) |
1275 | warnx(_("--file <file> and <message> are mutually exclusive, message is ignored")); | |
ebff016a | 1276 | #ifdef HAVE_LIBSYSTEMD |
4b670c01 | 1277 | if (jfd) { |
fd343a05 | 1278 | int ret = journald_entry(&ctl, jfd); |
4b670c01 SK |
1279 | if (stdin != jfd) |
1280 | fclose(jfd); | |
047e2888 | 1281 | if (ret) |
54fefa07 | 1282 | errx(EXIT_FAILURE, _("journald entry could not be written")); |
047e2888 | 1283 | return EXIT_SUCCESS; |
4b670c01 SK |
1284 | } |
1285 | #endif | |
4299ed1c | 1286 | |
11026083 | 1287 | /* user overwrites built-in SD-ELEMENT */ |
4299ed1c KZ |
1288 | if (has_structured_data_id(get_user_structured_data(&ctl), "timeQuality")) |
1289 | ctl.rfc5424_tq = 0; | |
1290 | ||
d77dc29e SK |
1291 | switch (unix_socket_errors_mode) { |
1292 | case AF_UNIX_ERRORS_OFF: | |
1293 | ctl.unix_socket_errors = 0; | |
1294 | break; | |
1295 | case AF_UNIX_ERRORS_ON: | |
1296 | ctl.unix_socket_errors = 1; | |
1297 | break; | |
1298 | case AF_UNIX_ERRORS_AUTO: | |
bcf7e149 | 1299 | ctl.unix_socket_errors = ctl.noact || ctl.stderr_printout; |
d77dc29e | 1300 | #ifdef HAVE_LIBSYSTEMD |
bcf7e149 | 1301 | ctl.unix_socket_errors |= !!sd_booted(); |
d77dc29e SK |
1302 | #endif |
1303 | break; | |
1304 | default: | |
1305 | abort(); | |
1306 | } | |
c68a1cb4 SK |
1307 | logger_open(&ctl); |
1308 | if (0 < argc) | |
1309 | logger_command_line(&ctl, argv); | |
7eda085c | 1310 | else |
c68a1cb4 SK |
1311 | /* Note. --file <arg> reopens stdin making the below |
1312 | * function to be used for file inputs. */ | |
1313 | logger_stdin(&ctl); | |
1314 | logger_close(&ctl); | |
49999d6a | 1315 | return EXIT_SUCCESS; |
6dbe3af9 | 1316 | } |