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