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