syslogd.c revision 1.25 1 /*
2 * Copyright (c) 1983, 1988, 1993, 1994
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.
32 */
33
34 #include <sys/cdefs.h>
35 #ifndef lint
36 __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n");
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)syslogd.c 8.3 (Berkeley) 4/4/94";
43 #else
44 __RCSID("$NetBSD: syslogd.c,v 1.25 1999/02/28 11:30:18 pk Exp $");
45 #endif
46 #endif /* not lint */
47
48 /*
49 * syslogd -- log system messages
50 *
51 * This program implements a system log. It takes a series of lines.
52 * Each line may have a priority, signified as "<n>" as
53 * the first characters of the line. If this is
54 * not present, a default priority is used.
55 *
56 * To kill syslogd, send a signal 15 (terminate). A signal 1 (hup) will
57 * cause it to reread its configuration file.
58 *
59 * Defined Constants:
60 *
61 * MAXLINE -- the maximimum line length that can be handled.
62 * DEFUPRI -- the default priority for user messages
63 * DEFSPRI -- the default priority for kernel messages
64 *
65 * Author: Eric Allman
66 * extensive changes by Ralph Campbell
67 * more extensive changes by Eric Allman (again)
68 */
69
70 #define MAXLINE 1024 /* maximum line length */
71 #define MAXSVLINE 120 /* maximum saved line length */
72 #define DEFUPRI (LOG_USER|LOG_NOTICE)
73 #define DEFSPRI (LOG_KERN|LOG_CRIT)
74 #define TIMERINTVL 30 /* interval for checking flush, mark */
75 #define TTYMSGTIME 1 /* timeout passed to ttymsg */
76
77 #include <sys/param.h>
78 #include <sys/ioctl.h>
79 #include <sys/stat.h>
80 #include <sys/wait.h>
81 #include <sys/socket.h>
82 #include <sys/msgbuf.h>
83 #include <sys/uio.h>
84 #include <sys/poll.h>
85 #include <sys/un.h>
86 #include <sys/time.h>
87 #include <sys/resource.h>
88 #include <sys/sysctl.h>
89
90 #include <netinet/in.h>
91 #include <netdb.h>
92 #include <arpa/inet.h>
93
94 #include <ctype.h>
95 #include <errno.h>
96 #include <fcntl.h>
97 #include <setjmp.h>
98 #include <signal.h>
99 #include <stdio.h>
100 #include <stdlib.h>
101 #include <string.h>
102 #include <unistd.h>
103 #include <utmp.h>
104 #include <util.h>
105 #include "pathnames.h"
106
107 #define SYSLOG_NAMES
108 #include <sys/syslog.h>
109
110 char *ConfFile = _PATH_LOGCONF;
111 char *PidFile = _PATH_LOGPID;
112 char ctty[] = _PATH_CONSOLE;
113
114 #define FDMASK(fd) (1 << (fd))
115
116 #define dprintf if (Debug) printf
117
118 #define MAXUNAMES 20 /* maximum number of user names */
119
120 /*
121 * Flags to logmsg().
122 */
123
124 #define IGN_CONS 0x001 /* don't print on console */
125 #define SYNC_FILE 0x002 /* do fsync on file after printing */
126 #define ADDDATE 0x004 /* add a date to the message */
127 #define MARK 0x008 /* this message is a mark */
128
129 /*
130 * This structure represents the files that will have log
131 * copies printed.
132 */
133
134 struct filed {
135 struct filed *f_next; /* next in linked list */
136 short f_type; /* entry type, see below */
137 short f_file; /* file descriptor */
138 time_t f_time; /* time this was last written */
139 u_char f_pmask[LOG_NFACILITIES+1]; /* priority mask */
140 union {
141 char f_uname[MAXUNAMES][UT_NAMESIZE+1];
142 struct {
143 char f_hname[MAXHOSTNAMELEN+1];
144 struct sockaddr_in f_addr;
145 } f_forw; /* forwarding address */
146 char f_fname[MAXPATHLEN];
147 } f_un;
148 char f_prevline[MAXSVLINE]; /* last message logged */
149 char f_lasttime[16]; /* time of last occurrence */
150 char f_prevhost[MAXHOSTNAMELEN+1]; /* host from which recd. */
151 int f_prevpri; /* pri of f_prevline */
152 int f_prevlen; /* length of f_prevline */
153 int f_prevcount; /* repetition cnt of prevline */
154 int f_repeatcount; /* number of "repeated" msgs */
155 };
156
157 /*
158 * Intervals at which we flush out "message repeated" messages,
159 * in seconds after previous message is logged. After each flush,
160 * we move to the next interval until we reach the largest.
161 */
162 int repeatinterval[] = { 30, 120, 600 }; /* # of secs before flush */
163 #define MAXREPEAT ((sizeof(repeatinterval) / sizeof(repeatinterval[0])) - 1)
164 #define REPEATTIME(f) ((f)->f_time + repeatinterval[(f)->f_repeatcount])
165 #define BACKOFF(f) { if (++(f)->f_repeatcount > MAXREPEAT) \
166 (f)->f_repeatcount = MAXREPEAT; \
167 }
168
169 /* values for f_type */
170 #define F_UNUSED 0 /* unused entry */
171 #define F_FILE 1 /* regular file */
172 #define F_TTY 2 /* terminal */
173 #define F_CONSOLE 3 /* console terminal */
174 #define F_FORW 4 /* remote machine */
175 #define F_USERS 5 /* list of users */
176 #define F_WALL 6 /* everyone logged on */
177
178 char *TypeNames[7] = {
179 "UNUSED", "FILE", "TTY", "CONSOLE",
180 "FORW", "USERS", "WALL"
181 };
182
183 struct filed *Files;
184 struct filed consfile;
185
186 int Debug; /* debug flag */
187 char LocalHostName[MAXHOSTNAMELEN+1]; /* our hostname */
188 char *LocalDomain; /* our local domain name */
189 int InetInuse = 0; /* non-zero if INET sockets are being used */
190 int finet; /* Internet datagram socket */
191 int LogPort; /* port number for INET connections */
192 int Initialized = 0; /* set when we have initialized ourselves */
193 int MarkInterval = 20 * 60; /* interval between marks in seconds */
194 int MarkSeq = 0; /* mark sequence number */
195 int SecureMode = 0; /* when true, speak only unix domain socks */
196 char **LogPaths; /* array of pathnames to read messages from */
197
198 void cfline __P((char *, struct filed *));
199 char *cvthname __P((struct sockaddr_in *));
200 int decode __P((const char *, CODE *));
201 void die __P((int));
202 void domark __P((int));
203 void fprintlog __P((struct filed *, int, char *));
204 int getmsgbufsize __P((void));
205 void init __P((int));
206 void logerror __P((char *));
207 void logmsg __P((int, char *, char *, int));
208 void printline __P((char *, char *));
209 void printsys __P((char *));
210 void reapchild __P((int));
211 void usage __P((void));
212 void wallmsg __P((struct filed *, struct iovec *));
213 int main __P((int, char *[]));
214 void logpath_add __P((char ***, int *, int *, char *));
215 void logpath_fileadd __P((char ***, int *, int *, char *));
216
217 int
218 main(argc, argv)
219 int argc;
220 char *argv[];
221 {
222 int ch, *funix, i, j, fklog, len, linesize;
223 int nfinetix, nfklogix, nfunixbaseix, nfds;
224 int funixsize = 0, funixmaxsize = 0;
225 struct sockaddr_un sunx, fromunix;
226 struct sockaddr_in sin, frominet;
227 FILE *fp;
228 char *p, *line, **pp;
229 struct pollfd *readfds;
230
231 while ((ch = getopt(argc, argv, "dsf:m:p:P:")) != -1)
232 switch(ch) {
233 case 'd': /* debug */
234 Debug++;
235 break;
236 case 'f': /* configuration file */
237 ConfFile = optarg;
238 break;
239 case 'm': /* mark interval */
240 MarkInterval = atoi(optarg) * 60;
241 break;
242 case 'p': /* path */
243 logpath_add(&LogPaths, &funixsize,
244 &funixmaxsize, optarg);
245 break;
246 case 'P': /* file of paths */
247 logpath_fileadd(&LogPaths, &funixsize,
248 &funixmaxsize, optarg);
249 break;
250 case 's': /* no network mode */
251 SecureMode++;
252 break;
253 case '?':
254 default:
255 usage();
256 }
257 if ((argc -= optind) != 0)
258 usage();
259
260 if (!Debug)
261 (void)daemon(0, 0);
262 else
263 setlinebuf(stdout);
264
265 consfile.f_type = F_CONSOLE;
266 (void)strcpy(consfile.f_un.f_fname, ctty);
267 (void)gethostname(LocalHostName, sizeof(LocalHostName));
268 LocalHostName[sizeof(LocalHostName) - 1] = '\0';
269 if ((p = strchr(LocalHostName, '.')) != NULL) {
270 *p++ = '\0';
271 LocalDomain = p;
272 } else
273 LocalDomain = "";
274 linesize = getmsgbufsize();
275 if (linesize < MAXLINE)
276 linesize = MAXLINE;
277 linesize++;
278 line = malloc(linesize);
279 if (line == NULL) {
280 logerror("couldn't allocate line buffer");
281 die(0);
282 }
283 (void)signal(SIGTERM, die);
284 (void)signal(SIGINT, Debug ? die : SIG_IGN);
285 (void)signal(SIGQUIT, Debug ? die : SIG_IGN);
286 (void)signal(SIGCHLD, reapchild);
287 (void)signal(SIGALRM, domark);
288 (void)alarm(TIMERINTVL);
289
290 #ifndef SUN_LEN
291 #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
292 #endif
293 if (funixsize == 0)
294 logpath_add(&LogPaths, &funixsize,
295 &funixmaxsize, _PATH_LOG);
296 funix = (int *)malloc(sizeof(int) * funixsize);
297 if (funix == NULL) {
298 logerror("couldn't allocate funix descriptors");
299 die(0);
300 }
301 for (j = 0, pp = LogPaths; j < funixsize; pp++, j++) {
302 unlink(*pp);
303 memset(&sunx, 0, sizeof(sunx));
304 sunx.sun_family = AF_LOCAL;
305 (void)strncpy(sunx.sun_path, *pp, sizeof(sunx.sun_path));
306 funix[j] = socket(AF_LOCAL, SOCK_DGRAM, 0);
307 if (funix[j] < 0 || bind(funix[j],
308 (struct sockaddr *)&sunx, SUN_LEN(&sunx)) < 0 ||
309 chmod(*pp, 0666) < 0) {
310 (void)snprintf(line, sizeof line,
311 "cannot create %s", *pp);
312 logerror(line);
313 dprintf("cannot create %s (%d)\n", *pp, errno);
314 die(0);
315 }
316 dprintf("listening on unix dgram socket %s\n", *pp);
317 }
318
319 if (!SecureMode)
320 finet = socket(AF_INET, SOCK_DGRAM, 0);
321 else
322 finet = -1;
323
324 if (finet >= 0) {
325 struct servent *sp;
326
327 sp = getservbyname("syslog", "udp");
328 if (sp == NULL) {
329 errno = 0;
330 logerror("syslog/udp: unknown service");
331 die(0);
332 }
333 memset(&sin, 0, sizeof(sin));
334 sin.sin_family = AF_INET;
335 sin.sin_port = LogPort = sp->s_port;
336 if (bind(finet, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
337 logerror("bind");
338 if (!Debug)
339 die(0);
340 } else {
341 InetInuse = 1;
342 }
343 dprintf("listening on inet socket\n");
344 }
345 if ((fklog = open(_PATH_KLOG, O_RDONLY, 0)) < 0) {
346 dprintf("can't open %s (%d)\n", _PATH_KLOG, errno);
347 } else {
348 dprintf("listening on kernel log %s\n", _PATH_KLOG);
349 }
350
351 /* tuck my process id away, if i'm not in debug mode */
352 if (Debug == 0) {
353 fp = fopen(PidFile, "w");
354 if (fp != NULL) {
355 fprintf(fp, "%d\n", getpid());
356 (void) fclose(fp);
357 }
358 }
359
360 dprintf("off & running....\n");
361
362 init(0);
363 (void)signal(SIGHUP, init);
364
365 /* setup pollfd set. */
366 readfds = (struct pollfd *)malloc(sizeof(struct pollfd) *
367 (funixsize + 2));
368 if (readfds == NULL) {
369 logerror("couldn't allocate pollfds");
370 die(0);
371 }
372 nfds = 0;
373 if (fklog >= 0) {
374 nfklogix = nfds++;
375 readfds[nfklogix].fd = fklog;
376 readfds[nfklogix].events = POLLIN | POLLPRI;
377 }
378 if (finet >= 0) {
379 nfinetix = nfds++;
380 readfds[nfinetix].fd = finet;
381 readfds[nfinetix].events = POLLIN | POLLPRI;
382 }
383 nfunixbaseix = nfds;
384 for (j = 0, pp = LogPaths; *pp; pp++) {
385 readfds[nfds].fd = funix[j++];
386 readfds[nfds++].events = POLLIN | POLLPRI;
387 }
388
389 for (;;) {
390 int rv;
391
392 rv = poll(readfds, nfds, INFTIM);
393 if (rv == 0)
394 continue;
395 if (rv < 0) {
396 if (errno != EINTR)
397 logerror("poll");
398 continue;
399 }
400 dprintf("got a message (%d)\n", rv);
401 if (fklog >= 0 &&
402 (readfds[nfklogix].revents & (POLLIN | POLLPRI))) {
403 dprintf("kernel log active\n");
404 i = read(fklog, line, linesize - 1);
405 if (i > 0) {
406 line[i] = '\0';
407 printsys(line);
408 } else if (i < 0 && errno != EINTR) {
409 logerror("klog");
410 fklog = -1;
411 }
412 }
413 for (j = 0, pp = LogPaths; *pp; pp++, j++) {
414 if ((readfds[nfunixbaseix + j].revents &
415 (POLLIN | POLLPRI)) == 0)
416 continue;
417
418 dprintf("unix socket (%s) active\n", *pp);
419 len = sizeof(fromunix);
420 i = recvfrom(funix[j], line, MAXLINE, 0,
421 (struct sockaddr *)&fromunix, &len);
422 if (i > 0) {
423 line[i] = '\0';
424 printline(LocalHostName, line);
425 } else if (i < 0 && errno != EINTR) {
426 char buf[MAXPATHLEN];
427
428 (void)snprintf(buf, sizeof buf,
429 "recvfrom unix %s", *pp);
430 logerror(buf);
431 }
432 }
433 if (finet >= 0 &&
434 (readfds[nfinetix].revents & (POLLIN | POLLPRI))) {
435 dprintf("inet socket active\n");
436 len = sizeof(frominet);
437 i = recvfrom(finet, line, MAXLINE, 0,
438 (struct sockaddr *)&frominet, &len);
439 if (i > 0) {
440 line[i] = '\0';
441 printline(cvthname(&frominet), line);
442 } else if (i < 0 && errno != EINTR)
443 logerror("recvfrom inet");
444 }
445 }
446 }
447
448 void
449 usage()
450 {
451
452 (void)fprintf(stderr,
453 "usage: syslogd [-f conffile] [-m markinterval] [-p logpath1] [-p logpath2 ..]\n");
454 exit(1);
455 }
456
457 /*
458 * given a pointer to an array of char *'s, a pointer to it's current
459 * size and current allocated max size, and a new char * to add, add
460 * it, update everything as necessary, possibly allocating a new array
461 */
462 void
463 logpath_add(lp, szp, maxszp, new)
464 char ***lp;
465 int *szp;
466 int *maxszp;
467 char *new;
468 {
469
470 if (*szp == *maxszp) {
471 if (*maxszp == 0) {
472 *maxszp = 4; /* start of with enough for now */
473 *lp = (char **)malloc(sizeof(char *) * 4);
474 if (*lp == NULL) {
475 logerror("couldn't allocate line buffer");
476 die(0);
477 }
478 } else {
479 *maxszp *= 2;
480 *lp = realloc(*lp, sizeof(char *) * (*maxszp));
481 if (*lp == NULL) {
482 logerror("couldn't allocate line buffer");
483 die(0);
484 }
485 }
486 }
487 (*lp)[(*szp)++] = new;
488 }
489
490 /* do a file of log sockets */
491 void
492 logpath_fileadd(lp, szp, maxszp, file)
493 char ***lp;
494 int *szp;
495 int *maxszp;
496 char *file;
497 {
498 FILE *fp;
499 char *line;
500 size_t len;
501
502 fp = fopen(file, "r");
503 if (fp == NULL) {
504 dprintf("can't open %s (%d)\n", file, errno);
505 logerror("could not open socket file list");
506 die(0);
507 }
508
509 while ((line = fgetln(fp, &len))) {
510 line[len - 1] = 0;
511 logpath_add(lp, szp, maxszp, line);
512 }
513 fclose(fp);
514 }
515
516 /*
517 * Take a raw input line, decode the message, and print the message
518 * on the appropriate log files.
519 */
520 void
521 printline(hname, msg)
522 char *hname;
523 char *msg;
524 {
525 int c, pri;
526 char *p, *q, line[MAXLINE + 1];
527
528 /* test for special codes */
529 pri = DEFUPRI;
530 p = msg;
531 if (*p == '<') {
532 pri = 0;
533 while (isdigit(*++p))
534 pri = 10 * pri + (*p - '0');
535 if (*p == '>')
536 ++p;
537 }
538 if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
539 pri = DEFUPRI;
540
541 /* don't allow users to log kernel messages */
542 if (LOG_FAC(pri) == LOG_KERN)
543 pri = LOG_MAKEPRI(LOG_USER, LOG_PRI(pri));
544
545 q = line;
546
547 while ((c = *p++ & 0177) != '\0' &&
548 q < &line[sizeof(line) - 1])
549 if (iscntrl(c))
550 if (c == '\n')
551 *q++ = ' ';
552 else if (c == '\t')
553 *q++ = '\t';
554 else {
555 *q++ = '^';
556 *q++ = c ^ 0100;
557 }
558 else
559 *q++ = c;
560 *q = '\0';
561
562 logmsg(pri, line, hname, 0);
563 }
564
565 /*
566 * Take a raw input line from /dev/klog, split and format similar to syslog().
567 */
568 void
569 printsys(msg)
570 char *msg;
571 {
572 int c, pri, flags;
573 char *lp, *p, *q, line[MAXLINE + 1];
574
575 (void)strcpy(line, _PATH_UNIX);
576 (void)strcat(line, ": ");
577 lp = line + strlen(line);
578 for (p = msg; *p != '\0'; ) {
579 flags = SYNC_FILE | ADDDATE; /* fsync file after write */
580 pri = DEFSPRI;
581 if (*p == '<') {
582 pri = 0;
583 while (isdigit(*++p))
584 pri = 10 * pri + (*p - '0');
585 if (*p == '>')
586 ++p;
587 } else {
588 /* kernel printf's come out on console */
589 flags |= IGN_CONS;
590 }
591 if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
592 pri = DEFSPRI;
593 q = lp;
594 while (*p != '\0' && (c = *p++) != '\n' &&
595 q < &line[MAXLINE])
596 *q++ = c;
597 *q = '\0';
598 logmsg(pri, line, LocalHostName, flags);
599 }
600 }
601
602 time_t now;
603
604 /*
605 * Log a message to the appropriate log files, users, etc. based on
606 * the priority.
607 */
608 void
609 logmsg(pri, msg, from, flags)
610 int pri;
611 char *msg, *from;
612 int flags;
613 {
614 struct filed *f;
615 int fac, msglen, omask, prilev;
616 char *timestamp;
617
618 dprintf("logmsg: pri 0%o, flags 0x%x, from %s, msg %s\n",
619 pri, flags, from, msg);
620
621 omask = sigblock(sigmask(SIGHUP)|sigmask(SIGALRM));
622
623 /*
624 * Check to see if msg looks non-standard.
625 */
626 msglen = strlen(msg);
627 if (msglen < 16 || msg[3] != ' ' || msg[6] != ' ' ||
628 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ')
629 flags |= ADDDATE;
630
631 (void)time(&now);
632 if (flags & ADDDATE)
633 timestamp = ctime(&now) + 4;
634 else {
635 timestamp = msg;
636 msg += 16;
637 msglen -= 16;
638 }
639
640 /* extract facility and priority level */
641 if (flags & MARK)
642 fac = LOG_NFACILITIES;
643 else
644 fac = LOG_FAC(pri);
645 prilev = LOG_PRI(pri);
646
647 /* log the message to the particular outputs */
648 if (!Initialized) {
649 f = &consfile;
650 f->f_file = open(ctty, O_WRONLY, 0);
651
652 if (f->f_file >= 0) {
653 fprintlog(f, flags, msg);
654 (void)close(f->f_file);
655 }
656 (void)sigsetmask(omask);
657 return;
658 }
659 for (f = Files; f; f = f->f_next) {
660 /* skip messages that are incorrect priority */
661 if (f->f_pmask[fac] < prilev ||
662 f->f_pmask[fac] == INTERNAL_NOPRI)
663 continue;
664
665 if (f->f_type == F_CONSOLE && (flags & IGN_CONS))
666 continue;
667
668 /* don't output marks to recently written files */
669 if ((flags & MARK) && (now - f->f_time) < MarkInterval / 2)
670 continue;
671
672 /*
673 * suppress duplicate lines to this file
674 */
675 if ((flags & MARK) == 0 && msglen == f->f_prevlen &&
676 !strcmp(msg, f->f_prevline) &&
677 !strcmp(from, f->f_prevhost)) {
678 (void)strncpy(f->f_lasttime, timestamp, 15);
679 f->f_prevcount++;
680 dprintf("msg repeated %d times, %ld sec of %d\n",
681 f->f_prevcount, (long)(now - f->f_time),
682 repeatinterval[f->f_repeatcount]);
683 /*
684 * If domark would have logged this by now,
685 * flush it now (so we don't hold isolated messages),
686 * but back off so we'll flush less often
687 * in the future.
688 */
689 if (now > REPEATTIME(f)) {
690 fprintlog(f, flags, (char *)NULL);
691 BACKOFF(f);
692 }
693 } else {
694 /* new line, save it */
695 if (f->f_prevcount)
696 fprintlog(f, 0, (char *)NULL);
697 f->f_repeatcount = 0;
698 f->f_prevpri = pri;
699 (void)strncpy(f->f_lasttime, timestamp, 15);
700 (void)strncpy(f->f_prevhost, from,
701 sizeof(f->f_prevhost));
702 if (msglen < MAXSVLINE) {
703 f->f_prevlen = msglen;
704 (void)strcpy(f->f_prevline, msg);
705 fprintlog(f, flags, (char *)NULL);
706 } else {
707 f->f_prevline[0] = 0;
708 f->f_prevlen = 0;
709 fprintlog(f, flags, msg);
710 }
711 }
712 }
713 (void)sigsetmask(omask);
714 }
715
716 void
717 fprintlog(f, flags, msg)
718 struct filed *f;
719 int flags;
720 char *msg;
721 {
722 struct iovec iov[6];
723 struct iovec *v;
724 int l;
725 char line[MAXLINE + 1], repbuf[80], greetings[200];
726
727 v = iov;
728 if (f->f_type == F_WALL) {
729 v->iov_base = greetings;
730 v->iov_len = snprintf(greetings, sizeof greetings,
731 "\r\n\7Message from syslogd@%s at %.24s ...\r\n",
732 f->f_prevhost, ctime(&now));
733 v++;
734 v->iov_base = "";
735 v->iov_len = 0;
736 v++;
737 } else {
738 v->iov_base = f->f_lasttime;
739 v->iov_len = 15;
740 v++;
741 v->iov_base = " ";
742 v->iov_len = 1;
743 v++;
744 }
745 v->iov_base = f->f_prevhost;
746 v->iov_len = strlen(v->iov_base);
747 v++;
748 v->iov_base = " ";
749 v->iov_len = 1;
750 v++;
751
752 if (msg) {
753 v->iov_base = msg;
754 v->iov_len = strlen(msg);
755 } else if (f->f_prevcount > 1) {
756 v->iov_base = repbuf;
757 v->iov_len = snprintf(repbuf, sizeof repbuf,
758 "last message repeated %d times", f->f_prevcount);
759 } else {
760 v->iov_base = f->f_prevline;
761 v->iov_len = f->f_prevlen;
762 }
763 v++;
764
765 dprintf("Logging to %s", TypeNames[f->f_type]);
766 f->f_time = now;
767
768 switch (f->f_type) {
769 case F_UNUSED:
770 dprintf("\n");
771 break;
772
773 case F_FORW:
774 dprintf(" %s\n", f->f_un.f_forw.f_hname);
775 /* check for local vs remote messages (from FreeBSD PR#bin/7055) */
776 if (strcmp(f->f_prevhost, LocalHostName)) {
777 l = snprintf(line, sizeof(line) - 1,
778 "<%d>%.15s [%s]: %s",
779 f->f_prevpri, (char *) iov[0].iov_base,
780 f->f_prevhost, (char *) iov[4].iov_base);
781 } else {
782 l = snprintf(line, sizeof(line) - 1, "<%d>%.15s %s",
783 f->f_prevpri, (char *) iov[0].iov_base,
784 (char *) iov[4].iov_base);
785 }
786 if (l > MAXLINE)
787 l = MAXLINE;
788 if ((finet >= 0) &&
789 (sendto(finet, line, l, 0,
790 (struct sockaddr *)&f->f_un.f_forw.f_addr,
791 sizeof(f->f_un.f_forw.f_addr)) != l)) {
792 int e = errno;
793 f->f_type = F_UNUSED;
794 errno = e;
795 logerror("sendto");
796 }
797 break;
798
799 case F_CONSOLE:
800 if (flags & IGN_CONS) {
801 dprintf(" (ignored)\n");
802 break;
803 }
804 /* FALLTHROUGH */
805
806 case F_TTY:
807 case F_FILE:
808 dprintf(" %s\n", f->f_un.f_fname);
809 if (f->f_type != F_FILE) {
810 v->iov_base = "\r\n";
811 v->iov_len = 2;
812 } else {
813 v->iov_base = "\n";
814 v->iov_len = 1;
815 }
816 again:
817 if (writev(f->f_file, iov, 6) < 0) {
818 int e = errno;
819 (void)close(f->f_file);
820 /*
821 * Check for errors on TTY's due to loss of tty
822 */
823 if ((e == EIO || e == EBADF) && f->f_type != F_FILE) {
824 f->f_file = open(f->f_un.f_fname,
825 O_WRONLY|O_APPEND, 0);
826 if (f->f_file < 0) {
827 f->f_type = F_UNUSED;
828 logerror(f->f_un.f_fname);
829 } else
830 goto again;
831 } else {
832 f->f_type = F_UNUSED;
833 errno = e;
834 logerror(f->f_un.f_fname);
835 }
836 } else if (flags & SYNC_FILE)
837 (void)fsync(f->f_file);
838 break;
839
840 case F_USERS:
841 case F_WALL:
842 dprintf("\n");
843 v->iov_base = "\r\n";
844 v->iov_len = 2;
845 wallmsg(f, iov);
846 break;
847 }
848 f->f_prevcount = 0;
849 }
850
851 /*
852 * WALLMSG -- Write a message to the world at large
853 *
854 * Write the specified message to either the entire
855 * world, or a list of approved users.
856 */
857 void
858 wallmsg(f, iov)
859 struct filed *f;
860 struct iovec *iov;
861 {
862 static int reenter; /* avoid calling ourselves */
863 FILE *uf;
864 struct utmp ut;
865 int i;
866 char *p;
867 char line[sizeof(ut.ut_line) + 1];
868
869 if (reenter++)
870 return;
871 if ((uf = fopen(_PATH_UTMP, "r")) == NULL) {
872 logerror(_PATH_UTMP);
873 reenter = 0;
874 return;
875 }
876 /* NOSTRICT */
877 while (fread((char *)&ut, sizeof(ut), 1, uf) == 1) {
878 if (ut.ut_name[0] == '\0')
879 continue;
880 strncpy(line, ut.ut_line, sizeof(ut.ut_line));
881 line[sizeof(ut.ut_line)] = '\0';
882 if (f->f_type == F_WALL) {
883 if ((p = ttymsg(iov, 6, line, TTYMSGTIME)) != NULL) {
884 errno = 0; /* already in msg */
885 logerror(p);
886 }
887 continue;
888 }
889 /* should we send the message to this user? */
890 for (i = 0; i < MAXUNAMES; i++) {
891 if (!f->f_un.f_uname[i][0])
892 break;
893 if (!strncmp(f->f_un.f_uname[i], ut.ut_name,
894 UT_NAMESIZE)) {
895 if ((p = ttymsg(iov, 6, line, TTYMSGTIME))
896 != NULL) {
897 errno = 0; /* already in msg */
898 logerror(p);
899 }
900 break;
901 }
902 }
903 }
904 (void)fclose(uf);
905 reenter = 0;
906 }
907
908 void
909 reapchild(signo)
910 int signo;
911 {
912 union wait status;
913
914 while (wait3((int *)&status, WNOHANG, (struct rusage *)NULL) > 0)
915 ;
916 }
917
918 /*
919 * Return a printable representation of a host address.
920 */
921 char *
922 cvthname(f)
923 struct sockaddr_in *f;
924 {
925 struct hostent *hp;
926 char *p;
927
928 dprintf("cvthname(%s)\n", inet_ntoa(f->sin_addr));
929
930 if (f->sin_family != AF_INET) {
931 dprintf("Malformed from address\n");
932 return ("???");
933 }
934 hp = gethostbyaddr((char *)&f->sin_addr,
935 sizeof(struct in_addr), f->sin_family);
936 if (hp == 0) {
937 dprintf("Host name for your address (%s) unknown\n",
938 inet_ntoa(f->sin_addr));
939 return (inet_ntoa(f->sin_addr));
940 }
941 if ((p = strchr(hp->h_name, '.')) && strcmp(p + 1, LocalDomain) == 0)
942 *p = '\0';
943 return (hp->h_name);
944 }
945
946 void
947 domark(signo)
948 int signo;
949 {
950 struct filed *f;
951
952 now = time((time_t *)NULL);
953 MarkSeq += TIMERINTVL;
954 if (MarkSeq >= MarkInterval) {
955 logmsg(LOG_INFO, "-- MARK --", LocalHostName, ADDDATE|MARK);
956 MarkSeq = 0;
957 }
958
959 for (f = Files; f; f = f->f_next) {
960 if (f->f_prevcount && now >= REPEATTIME(f)) {
961 dprintf("flush %s: repeated %d times, %d sec.\n",
962 TypeNames[f->f_type], f->f_prevcount,
963 repeatinterval[f->f_repeatcount]);
964 fprintlog(f, 0, (char *)NULL);
965 BACKOFF(f);
966 }
967 }
968 (void)alarm(TIMERINTVL);
969 }
970
971 /*
972 * Print syslogd errors some place.
973 */
974 void
975 logerror(type)
976 char *type;
977 {
978 char buf[100];
979
980 if (errno)
981 (void)snprintf(buf,
982 sizeof(buf), "syslogd: %s: %s", type, strerror(errno));
983 else
984 (void)snprintf(buf, sizeof(buf), "syslogd: %s", type);
985 errno = 0;
986 dprintf("%s\n", buf);
987 logmsg(LOG_SYSLOG|LOG_ERR, buf, LocalHostName, ADDDATE);
988 }
989
990 void
991 die(signo)
992 int signo;
993 {
994 struct filed *f;
995 char buf[100], **p;
996
997 for (f = Files; f != NULL; f = f->f_next) {
998 /* flush any pending output */
999 if (f->f_prevcount)
1000 fprintlog(f, 0, (char *)NULL);
1001 }
1002 if (signo) {
1003 dprintf("syslogd: exiting on signal %d\n", signo);
1004 (void)snprintf(buf, sizeof buf, "exiting on signal %d", signo);
1005 errno = 0;
1006 logerror(buf);
1007 }
1008 for (p = LogPaths; p && *p; p++)
1009 unlink(*p);
1010 exit(0);
1011 }
1012
1013 /*
1014 * INIT -- Initialize syslogd from configuration table
1015 */
1016 void
1017 init(signo)
1018 int signo;
1019 {
1020 int i;
1021 FILE *cf;
1022 struct filed *f, *next, **nextp;
1023 char *p;
1024 char cline[LINE_MAX];
1025
1026 dprintf("init\n");
1027
1028 /*
1029 * Close all open log files.
1030 */
1031 Initialized = 0;
1032 for (f = Files; f != NULL; f = next) {
1033 /* flush any pending output */
1034 if (f->f_prevcount)
1035 fprintlog(f, 0, (char *)NULL);
1036
1037 switch (f->f_type) {
1038 case F_FILE:
1039 case F_TTY:
1040 case F_CONSOLE:
1041 (void)close(f->f_file);
1042 break;
1043 }
1044 next = f->f_next;
1045 free((char *)f);
1046 }
1047 Files = NULL;
1048 nextp = &Files;
1049
1050 /* open the configuration file */
1051 if ((cf = fopen(ConfFile, "r")) == NULL) {
1052 dprintf("cannot open %s\n", ConfFile);
1053 *nextp = (struct filed *)calloc(1, sizeof(*f));
1054 cfline("*.ERR\t/dev/console", *nextp);
1055 (*nextp)->f_next = (struct filed *)calloc(1, sizeof(*f));
1056 cfline("*.PANIC\t*", (*nextp)->f_next);
1057 Initialized = 1;
1058 return;
1059 }
1060
1061 /*
1062 * Foreach line in the conf table, open that file.
1063 */
1064 f = NULL;
1065 while (fgets(cline, sizeof(cline), cf) != NULL) {
1066 /*
1067 * check for end-of-section, comments, strip off trailing
1068 * spaces and newline character.
1069 */
1070 for (p = cline; isspace(*p); ++p)
1071 continue;
1072 if (*p == '\0' || *p == '#')
1073 continue;
1074 for (p = strchr(cline, '\0'); isspace(*--p);)
1075 continue;
1076 *++p = '\0';
1077 f = (struct filed *)calloc(1, sizeof(*f));
1078 *nextp = f;
1079 nextp = &f->f_next;
1080 cfline(cline, f);
1081 }
1082
1083 /* close the configuration file */
1084 (void)fclose(cf);
1085
1086 Initialized = 1;
1087
1088 if (Debug) {
1089 for (f = Files; f; f = f->f_next) {
1090 for (i = 0; i <= LOG_NFACILITIES; i++)
1091 if (f->f_pmask[i] == INTERNAL_NOPRI)
1092 printf("X ");
1093 else
1094 printf("%d ", f->f_pmask[i]);
1095 printf("%s: ", TypeNames[f->f_type]);
1096 switch (f->f_type) {
1097 case F_FILE:
1098 case F_TTY:
1099 case F_CONSOLE:
1100 printf("%s", f->f_un.f_fname);
1101 break;
1102
1103 case F_FORW:
1104 printf("%s", f->f_un.f_forw.f_hname);
1105 break;
1106
1107 case F_USERS:
1108 for (i = 0; i < MAXUNAMES && *f->f_un.f_uname[i]; i++)
1109 printf("%s, ", f->f_un.f_uname[i]);
1110 break;
1111 }
1112 printf("\n");
1113 }
1114 }
1115
1116 logmsg(LOG_SYSLOG|LOG_INFO, "syslogd: restart", LocalHostName, ADDDATE);
1117 dprintf("syslogd: restarted\n");
1118 }
1119
1120 /*
1121 * Crack a configuration file line
1122 */
1123 void
1124 cfline(line, f)
1125 char *line;
1126 struct filed *f;
1127 {
1128 struct hostent *hp;
1129 int i, pri;
1130 char *bp, *p, *q;
1131 char buf[MAXLINE], ebuf[100];
1132
1133 dprintf("cfline(%s)\n", line);
1134
1135 errno = 0; /* keep strerror() stuff out of logerror messages */
1136
1137 /* clear out file entry */
1138 memset(f, 0, sizeof(*f));
1139 for (i = 0; i <= LOG_NFACILITIES; i++)
1140 f->f_pmask[i] = INTERNAL_NOPRI;
1141
1142 /* scan through the list of selectors */
1143 for (p = line; *p && *p != '\t';) {
1144
1145 /* find the end of this facility name list */
1146 for (q = p; *q && *q != '\t' && *q++ != '.'; )
1147 continue;
1148
1149 /* collect priority name */
1150 for (bp = buf; *q && !strchr("\t,;", *q); )
1151 *bp++ = *q++;
1152 *bp = '\0';
1153
1154 /* skip cruft */
1155 while (strchr(", ;", *q))
1156 q++;
1157
1158 /* decode priority name */
1159 if (*buf == '*')
1160 pri = LOG_PRIMASK + 1;
1161 else {
1162 pri = decode(buf, prioritynames);
1163 if (pri < 0) {
1164 (void)snprintf(ebuf, sizeof ebuf,
1165 "unknown priority name \"%s\"", buf);
1166 logerror(ebuf);
1167 return;
1168 }
1169 }
1170
1171 /* scan facilities */
1172 while (*p && !strchr("\t.;", *p)) {
1173 for (bp = buf; *p && !strchr("\t,;.", *p); )
1174 *bp++ = *p++;
1175 *bp = '\0';
1176 if (*buf == '*')
1177 for (i = 0; i < LOG_NFACILITIES; i++)
1178 f->f_pmask[i] = pri;
1179 else {
1180 i = decode(buf, facilitynames);
1181 if (i < 0) {
1182 (void)snprintf(ebuf, sizeof ebuf,
1183 "unknown facility name \"%s\"",
1184 buf);
1185 logerror(ebuf);
1186 return;
1187 }
1188 f->f_pmask[i >> 3] = pri;
1189 }
1190 while (*p == ',' || *p == ' ')
1191 p++;
1192 }
1193
1194 p = q;
1195 }
1196
1197 /* skip to action part */
1198 while (*p == '\t')
1199 p++;
1200
1201 switch (*p)
1202 {
1203 case '@':
1204 if (!InetInuse)
1205 break;
1206 (void)strcpy(f->f_un.f_forw.f_hname, ++p);
1207 hp = gethostbyname(p);
1208 if (hp == NULL) {
1209 extern int h_errno;
1210
1211 logerror((char *)hstrerror(h_errno));
1212 break;
1213 }
1214 memset(&f->f_un.f_forw.f_addr, 0,
1215 sizeof(f->f_un.f_forw.f_addr));
1216 f->f_un.f_forw.f_addr.sin_family = AF_INET;
1217 f->f_un.f_forw.f_addr.sin_port = LogPort;
1218 memmove(&f->f_un.f_forw.f_addr.sin_addr, hp->h_addr, hp->h_length);
1219 f->f_type = F_FORW;
1220 break;
1221
1222 case '/':
1223 (void)strcpy(f->f_un.f_fname, p);
1224 if ((f->f_file = open(p, O_WRONLY|O_APPEND, 0)) < 0) {
1225 f->f_type = F_UNUSED;
1226 logerror(p);
1227 break;
1228 }
1229 if (isatty(f->f_file))
1230 f->f_type = F_TTY;
1231 else
1232 f->f_type = F_FILE;
1233 if (strcmp(p, ctty) == 0)
1234 f->f_type = F_CONSOLE;
1235 break;
1236
1237 case '*':
1238 f->f_type = F_WALL;
1239 break;
1240
1241 default:
1242 for (i = 0; i < MAXUNAMES && *p; i++) {
1243 for (q = p; *q && *q != ','; )
1244 q++;
1245 (void)strncpy(f->f_un.f_uname[i], p, UT_NAMESIZE);
1246 if ((q - p) > UT_NAMESIZE)
1247 f->f_un.f_uname[i][UT_NAMESIZE] = '\0';
1248 else
1249 f->f_un.f_uname[i][q - p] = '\0';
1250 while (*q == ',' || *q == ' ')
1251 q++;
1252 p = q;
1253 }
1254 f->f_type = F_USERS;
1255 break;
1256 }
1257 }
1258
1259
1260 /*
1261 * Decode a symbolic name to a numeric value
1262 */
1263 int
1264 decode(name, codetab)
1265 const char *name;
1266 CODE *codetab;
1267 {
1268 CODE *c;
1269 char *p, buf[40];
1270
1271 if (isdigit(*name))
1272 return (atoi(name));
1273
1274 for (p = buf; *name && p < &buf[sizeof(buf) - 1]; p++, name++) {
1275 if (isupper(*name))
1276 *p = tolower(*name);
1277 else
1278 *p = *name;
1279 }
1280 *p = '\0';
1281 for (c = codetab; c->c_name; c++)
1282 if (!strcmp(buf, c->c_name))
1283 return (c->c_val);
1284
1285 return (-1);
1286 }
1287
1288 /*
1289 * Retrieve the size of the kernel message buffer, via sysctl.
1290 */
1291 int
1292 getmsgbufsize()
1293 {
1294 int msgbufsize, mib[2];
1295 size_t size;
1296
1297 mib[0] = CTL_KERN;
1298 mib[1] = KERN_MSGBUFSIZE;
1299 size = sizeof msgbufsize;
1300 if (sysctl(mib, 2, &msgbufsize, &size, NULL, 0) == -1) {
1301 dprintf("couldn't get kern.msgbufsize\n");
1302 return (0);
1303 }
1304 return (msgbufsize);
1305 }
1306