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