1 |
/* |
2 |
* Copyright (c) 1983, 1988, 1993, 1994 |
3 |
* The Regents of the University of California. All rights reserved. |
4 |
* |
5 |
* Redistribution and use in source and binary forms, with or without |
6 |
* modification, are permitted provided that the following conditions |
7 |
* are met: |
8 |
* 1. Redistributions of source code must retain the above copyright |
9 |
* notice, this list of conditions and the following disclaimer. |
10 |
* 2. Redistributions in binary form must reproduce the above copyright |
11 |
* notice, this list of conditions and the following disclaimer in the |
12 |
* documentation and/or other materials provided with the distribution. |
13 |
* 4. Neither the name of the University nor the names of its contributors |
14 |
* may be used to endorse or promote products derived from this software |
15 |
* without specific prior written permission. |
16 |
* |
17 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
18 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
19 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
20 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
21 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
22 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
23 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
24 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
25 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
26 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
27 |
* SUCH DAMAGE. |
28 |
*/ |
29 |
|
30 |
#ifndef lint |
31 |
static const char copyright[] = |
32 |
"@(#) Copyright (c) 1983, 1988, 1993, 1994\n\ |
33 |
The Regents of the University of California. All rights reserved.\n"; |
34 |
#endif /* not lint */ |
35 |
|
36 |
#ifndef lint |
37 |
#if 0 |
38 |
static char sccsid[] = "@(#)syslogd.c 8.3 (Berkeley) 4/4/94"; |
39 |
#endif |
40 |
#endif /* not lint */ |
41 |
|
42 |
#include <sys/cdefs.h> |
43 |
__FBSDID("$FreeBSD$"); |
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 maximum 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 |
* Extension to log by program name as well as facility and priority |
66 |
* by Peter da Silva. |
67 |
* -u and -v by Harlan Stenn. |
68 |
* Priority comparison code by Harlan Stenn. |
69 |
*/ |
70 |
|
71 |
/* Maximum number of characters in time of last occurrence */ |
72 |
#define MAXDATELEN 16 |
73 |
#define MAXLINE 1024 /* maximum line length */ |
74 |
#define MAXSVLINE MAXLINE /* maximum saved line length */ |
75 |
#define DEFUPRI (LOG_USER|LOG_NOTICE) |
76 |
#define DEFSPRI (LOG_KERN|LOG_CRIT) |
77 |
#define TIMERINTVL 30 /* interval for checking flush, mark */ |
78 |
#define TTYMSGTIME 1 /* timeout passed to ttymsg */ |
79 |
#define RCVBUF_MINSIZE (80 * 1024) /* minimum size of dgram rcv buffer */ |
80 |
|
81 |
#include <sys/param.h> |
82 |
#include <sys/ioctl.h> |
83 |
#include <sys/mman.h> |
84 |
#include <sys/queue.h> |
85 |
#include <sys/resource.h> |
86 |
#include <sys/socket.h> |
87 |
#include <sys/stat.h> |
88 |
#include <sys/syslimits.h> |
89 |
#include <sys/time.h> |
90 |
#include <sys/uio.h> |
91 |
#include <sys/un.h> |
92 |
#include <sys/wait.h> |
93 |
#include <sys/types.h> |
94 |
|
95 |
#include <netinet/in.h> |
96 |
#include <netdb.h> |
97 |
#include <arpa/inet.h> |
98 |
|
99 |
#include <ctype.h> |
100 |
#include <dirent.h> |
101 |
#include <err.h> |
102 |
#include <errno.h> |
103 |
#include <fcntl.h> |
104 |
#include <libutil.h> |
105 |
#include <limits.h> |
106 |
#include <paths.h> |
107 |
#include <signal.h> |
108 |
#include <stdio.h> |
109 |
#include <stdlib.h> |
110 |
#include <string.h> |
111 |
#include <sysexits.h> |
112 |
#include <unistd.h> |
113 |
#include <utmpx.h> |
114 |
|
115 |
#include "pathnames.h" |
116 |
#include "ttymsg.h" |
117 |
|
118 |
#define SYSLOG_NAMES |
119 |
#include <sys/syslog.h> |
120 |
|
121 |
const char *ConfFile = _PATH_LOGCONF; |
122 |
const char *PidFile = _PATH_LOGPID; |
123 |
const char ctty[] = _PATH_CONSOLE; |
124 |
static const char include_str[] = "include"; |
125 |
static const char include_ext[] = ".conf"; |
126 |
|
127 |
#define dprintf if (Debug) printf |
128 |
|
129 |
#define MAXUNAMES 20 /* maximum number of user names */ |
130 |
|
131 |
/* |
132 |
* List of hosts for binding. |
133 |
*/ |
134 |
static STAILQ_HEAD(, host) hqueue; |
135 |
struct host { |
136 |
char *name; |
137 |
STAILQ_ENTRY(host) next; |
138 |
}; |
139 |
|
140 |
/* |
141 |
* Unix sockets. |
142 |
* We have two default sockets, one with 666 permissions, |
143 |
* and one for privileged programs. |
144 |
*/ |
145 |
struct funix { |
146 |
int s; |
147 |
const char *name; |
148 |
mode_t mode; |
149 |
STAILQ_ENTRY(funix) next; |
150 |
}; |
151 |
struct funix funix_secure = { -1, _PATH_LOG_PRIV, S_IRUSR | S_IWUSR, |
152 |
{ NULL } }; |
153 |
struct funix funix_default = { -1, _PATH_LOG, DEFFILEMODE, |
154 |
{ &funix_secure } }; |
155 |
|
156 |
STAILQ_HEAD(, funix) funixes = { &funix_default, |
157 |
&(funix_secure.next.stqe_next) }; |
158 |
|
159 |
/* |
160 |
* Flags to logmsg(). |
161 |
*/ |
162 |
|
163 |
#define IGN_CONS 0x001 /* don't print on console */ |
164 |
#define SYNC_FILE 0x002 /* do fsync on file after printing */ |
165 |
#define ADDDATE 0x004 /* add a date to the message */ |
166 |
#define MARK 0x008 /* this message is a mark */ |
167 |
#define ISKERNEL 0x010 /* kernel generated message */ |
168 |
|
169 |
/* |
170 |
* This structure represents the files that will have log |
171 |
* copies printed. |
172 |
* We require f_file to be valid if f_type is F_FILE, F_CONSOLE, F_TTY |
173 |
* or if f_type is F_PIPE and f_pid > 0. |
174 |
*/ |
175 |
|
176 |
struct filed { |
177 |
struct filed *f_next; /* next in linked list */ |
178 |
short f_type; /* entry type, see below */ |
179 |
short f_file; /* file descriptor */ |
180 |
time_t f_time; /* time this was last written */ |
181 |
char *f_host; /* host from which to recd. */ |
182 |
u_char f_pmask[LOG_NFACILITIES+1]; /* priority mask */ |
183 |
u_char f_pcmp[LOG_NFACILITIES+1]; /* compare priority */ |
184 |
#define PRI_LT 0x1 |
185 |
#define PRI_EQ 0x2 |
186 |
#define PRI_GT 0x4 |
187 |
char *f_program; /* program this applies to */ |
188 |
union { |
189 |
char f_uname[MAXUNAMES][MAXLOGNAME]; |
190 |
struct { |
191 |
char f_hname[MAXHOSTNAMELEN]; |
192 |
struct addrinfo *f_addr; |
193 |
|
194 |
} f_forw; /* forwarding address */ |
195 |
char f_fname[MAXPATHLEN]; |
196 |
struct { |
197 |
char f_pname[MAXPATHLEN]; |
198 |
pid_t f_pid; |
199 |
} f_pipe; |
200 |
} f_un; |
201 |
char f_prevline[MAXSVLINE]; /* last message logged */ |
202 |
char f_lasttime[MAXDATELEN]; /* time of last occurrence */ |
203 |
char f_prevhost[MAXHOSTNAMELEN]; /* host from which recd. */ |
204 |
int f_prevpri; /* pri of f_prevline */ |
205 |
int f_prevlen; /* length of f_prevline */ |
206 |
int f_prevcount; /* repetition cnt of prevline */ |
207 |
u_int f_repeatcount; /* number of "repeated" msgs */ |
208 |
int f_flags; /* file-specific flags */ |
209 |
#define FFLAG_SYNC 0x01 |
210 |
#define FFLAG_NEEDSYNC 0x02 |
211 |
}; |
212 |
|
213 |
/* |
214 |
* Queue of about-to-be dead processes we should watch out for. |
215 |
*/ |
216 |
|
217 |
TAILQ_HEAD(stailhead, deadq_entry) deadq_head; |
218 |
struct stailhead *deadq_headp; |
219 |
|
220 |
struct deadq_entry { |
221 |
pid_t dq_pid; |
222 |
int dq_timeout; |
223 |
TAILQ_ENTRY(deadq_entry) dq_entries; |
224 |
}; |
225 |
|
226 |
/* |
227 |
* The timeout to apply to processes waiting on the dead queue. Unit |
228 |
* of measure is `mark intervals', i.e. 20 minutes by default. |
229 |
* Processes on the dead queue will be terminated after that time. |
230 |
*/ |
231 |
|
232 |
#define DQ_TIMO_INIT 2 |
233 |
|
234 |
typedef struct deadq_entry *dq_t; |
235 |
|
236 |
|
237 |
/* |
238 |
* Struct to hold records of network addresses that are allowed to log |
239 |
* to us. |
240 |
*/ |
241 |
struct allowedpeer { |
242 |
int isnumeric; |
243 |
u_short port; |
244 |
union { |
245 |
struct { |
246 |
struct sockaddr_storage addr; |
247 |
struct sockaddr_storage mask; |
248 |
} numeric; |
249 |
char *name; |
250 |
} u; |
251 |
#define a_addr u.numeric.addr |
252 |
#define a_mask u.numeric.mask |
253 |
#define a_name u.name |
254 |
}; |
255 |
|
256 |
|
257 |
/* |
258 |
* Intervals at which we flush out "message repeated" messages, |
259 |
* in seconds after previous message is logged. After each flush, |
260 |
* we move to the next interval until we reach the largest. |
261 |
*/ |
262 |
int repeatinterval[] = { 30, 120, 600 }; /* # of secs before flush */ |
263 |
#define MAXREPEAT ((sizeof(repeatinterval) / sizeof(repeatinterval[0])) - 1) |
264 |
#define REPEATTIME(f) ((f)->f_time + repeatinterval[(f)->f_repeatcount]) |
265 |
#define BACKOFF(f) { if (++(f)->f_repeatcount > MAXREPEAT) \ |
266 |
(f)->f_repeatcount = MAXREPEAT; \ |
267 |
} |
268 |
|
269 |
/* values for f_type */ |
270 |
#define F_UNUSED 0 /* unused entry */ |
271 |
#define F_FILE 1 /* regular file */ |
272 |
#define F_TTY 2 /* terminal */ |
273 |
#define F_CONSOLE 3 /* console terminal */ |
274 |
#define F_FORW 4 /* remote machine */ |
275 |
#define F_USERS 5 /* list of users */ |
276 |
#define F_WALL 6 /* everyone logged on */ |
277 |
#define F_PIPE 7 /* pipe to program */ |
278 |
|
279 |
const char *TypeNames[8] = { |
280 |
"UNUSED", "FILE", "TTY", "CONSOLE", |
281 |
"FORW", "USERS", "WALL", "PIPE" |
282 |
}; |
283 |
|
284 |
static struct filed *Files; /* Log files that we write to */ |
285 |
static struct filed consfile; /* Console */ |
286 |
|
287 |
static int Debug; /* debug flag */ |
288 |
static int Foreground = 0; /* Run in foreground, instead of daemonizing */ |
289 |
static int resolve = 1; /* resolve hostname */ |
290 |
static char LocalHostName[MAXHOSTNAMELEN]; /* our hostname */ |
291 |
static const char *LocalDomain; /* our local domain name */ |
292 |
static int *finet; /* Internet datagram sockets */ |
293 |
static int fklog = -1; /* /dev/klog */ |
294 |
static int Initialized; /* set when we have initialized ourselves */ |
295 |
static int MarkInterval = 20 * 60; /* interval between marks in seconds */ |
296 |
static int MarkSeq; /* mark sequence number */ |
297 |
static int NoBind; /* don't bind() as suggested by RFC 3164 */ |
298 |
static int SecureMode; /* when true, receive only unix domain socks */ |
299 |
#ifdef INET6 |
300 |
static int family = PF_UNSPEC; /* protocol family (IPv4, IPv6 or both) */ |
301 |
#else |
302 |
static int family = PF_INET; /* protocol family (IPv4 only) */ |
303 |
#endif |
304 |
static int mask_C1 = 1; /* mask characters from 0x80 - 0x9F */ |
305 |
static int send_to_all; /* send message to all IPv4/IPv6 addresses */ |
306 |
static int use_bootfile; /* log entire bootfile for every kern msg */ |
307 |
static int no_compress; /* don't compress messages (1=pipes, 2=all) */ |
308 |
static int logflags = O_WRONLY|O_APPEND; /* flags used to open log files */ |
309 |
|
310 |
static char bootfile[MAXLINE+1]; /* booted kernel file */ |
311 |
|
312 |
struct allowedpeer *AllowedPeers; /* List of allowed peers */ |
313 |
static int NumAllowed; /* Number of entries in AllowedPeers */ |
314 |
static int RemoteAddDate; /* Always set the date on remote messages */ |
315 |
|
316 |
static int UniquePriority; /* Only log specified priority? */ |
317 |
static int LogFacPri; /* Put facility and priority in log message: */ |
318 |
/* 0=no, 1=numeric, 2=names */ |
319 |
static int KeepKernFac; /* Keep remotely logged kernel facility */ |
320 |
static int needdofsync = 0; /* Are any file(s) waiting to be fsynced? */ |
321 |
static struct pidfh *pfh; |
322 |
|
323 |
volatile sig_atomic_t MarkSet, WantDie; |
324 |
|
325 |
static int allowaddr(char *); |
326 |
static void cfline(const char *, struct filed *, |
327 |
const char *, const char *); |
328 |
static const char *cvthname(struct sockaddr *); |
329 |
static void deadq_enter(pid_t, const char *); |
330 |
static int deadq_remove(pid_t); |
331 |
static int decode(const char *, const CODE *); |
332 |
static void die(int) __dead2; |
333 |
static void dodie(int); |
334 |
static void dofsync(void); |
335 |
static void domark(int); |
336 |
static void fprintlog(struct filed *, int, const char *); |
337 |
static int *socksetup(int, char *); |
338 |
static void init(int); |
339 |
static void logerror(const char *); |
340 |
static void logmsg(int, const char *, const char *, int); |
341 |
static void log_deadchild(pid_t, int, const char *); |
342 |
static void markit(void); |
343 |
static int skip_message(const char *, const char *, int); |
344 |
static void printline(const char *, char *, int); |
345 |
static void printsys(char *); |
346 |
static int p_open(const char *, pid_t *); |
347 |
static void readklog(void); |
348 |
static void reapchild(int); |
349 |
static const char *ttymsg_check(struct iovec *, int, char *, int); |
350 |
static void usage(void); |
351 |
static int validate(struct sockaddr *, const char *); |
352 |
static void unmapped(struct sockaddr *); |
353 |
static void wallmsg(struct filed *, struct iovec *, const int iovlen); |
354 |
static int waitdaemon(int, int, int); |
355 |
static void timedout(int); |
356 |
static void increase_rcvbuf(int); |
357 |
|
358 |
static void |
359 |
close_filed(struct filed *f) |
360 |
{ |
361 |
|
362 |
if (f == NULL || f->f_file == -1) |
363 |
return; |
364 |
|
365 |
switch (f->f_type) { |
366 |
case F_FORW: |
367 |
if (f->f_un.f_forw.f_addr) { |
368 |
freeaddrinfo(f->f_un.f_forw.f_addr); |
369 |
f->f_un.f_forw.f_addr = NULL; |
370 |
} |
371 |
/*FALLTHROUGH*/ |
372 |
|
373 |
case F_FILE: |
374 |
case F_TTY: |
375 |
case F_CONSOLE: |
376 |
f->f_type = F_UNUSED; |
377 |
break; |
378 |
case F_PIPE: |
379 |
f->f_un.f_pipe.f_pid = 0; |
380 |
break; |
381 |
} |
382 |
(void)close(f->f_file); |
383 |
f->f_file = -1; |
384 |
} |
385 |
|
386 |
int |
387 |
main(int argc, char *argv[]) |
388 |
{ |
389 |
int ch, i, fdsrmax = 0, l; |
390 |
struct sockaddr_un sunx, fromunix; |
391 |
struct sockaddr_storage frominet; |
392 |
fd_set *fdsr = NULL; |
393 |
char line[MAXLINE + 1]; |
394 |
const char *hname; |
395 |
struct timeval tv, *tvp; |
396 |
struct sigaction sact; |
397 |
struct host *host; |
398 |
struct funix *fx, *fx1; |
399 |
sigset_t mask; |
400 |
pid_t ppid = 1, spid; |
401 |
socklen_t len; |
402 |
|
403 |
if (madvise(NULL, 0, MADV_PROTECT) != 0) |
404 |
dprintf("madvise() failed: %s\n", strerror(errno)); |
405 |
|
406 |
STAILQ_INIT(&hqueue); |
407 |
|
408 |
while ((ch = getopt(argc, argv, "468Aa:b:cCdf:Fkl:m:nNop:P:sS:Tuv")) |
409 |
!= -1) |
410 |
switch (ch) { |
411 |
case '4': |
412 |
family = PF_INET; |
413 |
break; |
414 |
#ifdef INET6 |
415 |
case '6': |
416 |
family = PF_INET6; |
417 |
break; |
418 |
#endif |
419 |
case '8': |
420 |
mask_C1 = 0; |
421 |
break; |
422 |
case 'A': |
423 |
send_to_all++; |
424 |
break; |
425 |
case 'a': /* allow specific network addresses only */ |
426 |
if (allowaddr(optarg) == -1) |
427 |
usage(); |
428 |
break; |
429 |
case 'b': |
430 |
{ |
431 |
if ((host = malloc(sizeof(struct host))) == NULL) |
432 |
err(1, "malloc failed"); |
433 |
host->name = optarg; |
434 |
STAILQ_INSERT_TAIL(&hqueue, host, next); |
435 |
break; |
436 |
} |
437 |
case 'c': |
438 |
no_compress++; |
439 |
break; |
440 |
case 'C': |
441 |
logflags |= O_CREAT; |
442 |
break; |
443 |
case 'd': /* debug */ |
444 |
Debug++; |
445 |
break; |
446 |
case 'f': /* configuration file */ |
447 |
ConfFile = optarg; |
448 |
break; |
449 |
case 'F': /* run in foreground instead of daemon */ |
450 |
Foreground++; |
451 |
break; |
452 |
case 'k': /* keep remote kern fac */ |
453 |
KeepKernFac = 1; |
454 |
break; |
455 |
case 'l': |
456 |
{ |
457 |
long perml; |
458 |
mode_t mode; |
459 |
char *name, *ep; |
460 |
|
461 |
if (optarg[0] == '/') { |
462 |
mode = DEFFILEMODE; |
463 |
name = optarg; |
464 |
} else if ((name = strchr(optarg, ':')) != NULL) { |
465 |
*name++ = '\0'; |
466 |
if (name[0] != '/') |
467 |
errx(1, "socket name must be absolute " |
468 |
"path"); |
469 |
if (isdigit(*optarg)) { |
470 |
perml = strtol(optarg, &ep, 8); |
471 |
if (*ep || perml < 0 || |
472 |
perml & ~(S_IRWXU|S_IRWXG|S_IRWXO)) |
473 |
errx(1, "invalid mode %s, exiting", |
474 |
optarg); |
475 |
mode = (mode_t )perml; |
476 |
} else |
477 |
errx(1, "invalid mode %s, exiting", |
478 |
optarg); |
479 |
} else /* doesn't begin with '/', and no ':' */ |
480 |
errx(1, "can't parse path %s", optarg); |
481 |
|
482 |
if (strlen(name) >= sizeof(sunx.sun_path)) |
483 |
errx(1, "%s path too long, exiting", name); |
484 |
if ((fx = malloc(sizeof(struct funix))) == NULL) |
485 |
err(1, "malloc failed"); |
486 |
fx->s = -1; |
487 |
fx->name = name; |
488 |
fx->mode = mode; |
489 |
STAILQ_INSERT_TAIL(&funixes, fx, next); |
490 |
break; |
491 |
} |
492 |
case 'm': /* mark interval */ |
493 |
MarkInterval = atoi(optarg) * 60; |
494 |
break; |
495 |
case 'N': |
496 |
NoBind = 1; |
497 |
SecureMode = 1; |
498 |
break; |
499 |
case 'n': |
500 |
resolve = 0; |
501 |
break; |
502 |
case 'o': |
503 |
use_bootfile = 1; |
504 |
break; |
505 |
case 'p': /* path */ |
506 |
if (strlen(optarg) >= sizeof(sunx.sun_path)) |
507 |
errx(1, "%s path too long, exiting", optarg); |
508 |
funix_default.name = optarg; |
509 |
break; |
510 |
case 'P': /* path for alt. PID */ |
511 |
PidFile = optarg; |
512 |
break; |
513 |
case 's': /* no network mode */ |
514 |
SecureMode++; |
515 |
break; |
516 |
case 'S': /* path for privileged originator */ |
517 |
if (strlen(optarg) >= sizeof(sunx.sun_path)) |
518 |
errx(1, "%s path too long, exiting", optarg); |
519 |
funix_secure.name = optarg; |
520 |
break; |
521 |
case 'T': |
522 |
RemoteAddDate = 1; |
523 |
break; |
524 |
case 'u': /* only log specified priority */ |
525 |
UniquePriority++; |
526 |
break; |
527 |
case 'v': /* log facility and priority */ |
528 |
LogFacPri++; |
529 |
break; |
530 |
default: |
531 |
usage(); |
532 |
} |
533 |
if ((argc -= optind) != 0) |
534 |
usage(); |
535 |
|
536 |
pfh = pidfile_open(PidFile, 0600, &spid); |
537 |
if (pfh == NULL) { |
538 |
if (errno == EEXIST) |
539 |
errx(1, "syslogd already running, pid: %d", spid); |
540 |
warn("cannot open pid file"); |
541 |
} |
542 |
|
543 |
if ((!Foreground) && (!Debug)) { |
544 |
ppid = waitdaemon(0, 0, 30); |
545 |
if (ppid < 0) { |
546 |
warn("could not become daemon"); |
547 |
pidfile_remove(pfh); |
548 |
exit(1); |
549 |
} |
550 |
} else if (Debug) { |
551 |
setlinebuf(stdout); |
552 |
} |
553 |
|
554 |
if (NumAllowed) |
555 |
endservent(); |
556 |
|
557 |
consfile.f_type = F_CONSOLE; |
558 |
(void)strlcpy(consfile.f_un.f_fname, ctty + sizeof _PATH_DEV - 1, |
559 |
sizeof(consfile.f_un.f_fname)); |
560 |
(void)strlcpy(bootfile, getbootfile(), sizeof(bootfile)); |
561 |
(void)signal(SIGTERM, dodie); |
562 |
(void)signal(SIGINT, Debug ? dodie : SIG_IGN); |
563 |
(void)signal(SIGQUIT, Debug ? dodie : SIG_IGN); |
564 |
/* |
565 |
* We don't want the SIGCHLD and SIGHUP handlers to interfere |
566 |
* with each other; they are likely candidates for being called |
567 |
* simultaneously (SIGHUP closes pipe descriptor, process dies, |
568 |
* SIGCHLD happens). |
569 |
*/ |
570 |
sigemptyset(&mask); |
571 |
sigaddset(&mask, SIGHUP); |
572 |
sact.sa_handler = reapchild; |
573 |
sact.sa_mask = mask; |
574 |
sact.sa_flags = SA_RESTART; |
575 |
(void)sigaction(SIGCHLD, &sact, NULL); |
576 |
(void)signal(SIGALRM, domark); |
577 |
(void)signal(SIGPIPE, SIG_IGN); /* We'll catch EPIPE instead. */ |
578 |
(void)alarm(TIMERINTVL); |
579 |
|
580 |
TAILQ_INIT(&deadq_head); |
581 |
|
582 |
#ifndef SUN_LEN |
583 |
#define SUN_LEN(unp) (strlen((unp)->sun_path) + 2) |
584 |
#endif |
585 |
STAILQ_FOREACH_SAFE(fx, &funixes, next, fx1) { |
586 |
(void)unlink(fx->name); |
587 |
memset(&sunx, 0, sizeof(sunx)); |
588 |
sunx.sun_family = AF_LOCAL; |
589 |
(void)strlcpy(sunx.sun_path, fx->name, sizeof(sunx.sun_path)); |
590 |
fx->s = socket(PF_LOCAL, SOCK_DGRAM, 0); |
591 |
if (fx->s < 0 || |
592 |
bind(fx->s, (struct sockaddr *)&sunx, SUN_LEN(&sunx)) < 0 || |
593 |
chmod(fx->name, fx->mode) < 0) { |
594 |
(void)snprintf(line, sizeof line, |
595 |
"cannot create %s", fx->name); |
596 |
logerror(line); |
597 |
dprintf("cannot create %s (%d)\n", fx->name, errno); |
598 |
if (fx == &funix_default || fx == &funix_secure) |
599 |
die(0); |
600 |
else { |
601 |
STAILQ_REMOVE(&funixes, fx, funix, next); |
602 |
continue; |
603 |
} |
604 |
} |
605 |
increase_rcvbuf(fx->s); |
606 |
} |
607 |
if (SecureMode <= 1) { |
608 |
if (STAILQ_EMPTY(&hqueue)) |
609 |
finet = socksetup(family, NULL); |
610 |
STAILQ_FOREACH(host, &hqueue, next) { |
611 |
int *finet0, total; |
612 |
finet0 = socksetup(family, host->name); |
613 |
if (finet0 && !finet) { |
614 |
finet = finet0; |
615 |
} else if (finet0 && finet) { |
616 |
total = *finet0 + *finet + 1; |
617 |
finet = realloc(finet, total * sizeof(int)); |
618 |
if (finet == NULL) |
619 |
err(1, "realloc failed"); |
620 |
for (i = 1; i <= *finet0; i++) { |
621 |
finet[(*finet)+i] = finet0[i]; |
622 |
} |
623 |
*finet = total - 1; |
624 |
free(finet0); |
625 |
} |
626 |
} |
627 |
} |
628 |
|
629 |
if (finet) { |
630 |
if (SecureMode) { |
631 |
for (i = 0; i < *finet; i++) { |
632 |
if (shutdown(finet[i+1], SHUT_RD) < 0 && |
633 |
errno != ENOTCONN) { |
634 |
logerror("shutdown"); |
635 |
if (!Debug) |
636 |
die(0); |
637 |
} |
638 |
} |
639 |
} else { |
640 |
dprintf("listening on inet and/or inet6 socket\n"); |
641 |
} |
642 |
dprintf("sending on inet and/or inet6 socket\n"); |
643 |
} |
644 |
|
645 |
if ((fklog = open(_PATH_KLOG, O_RDONLY|O_NONBLOCK, 0)) < 0) |
646 |
dprintf("can't open %s (%d)\n", _PATH_KLOG, errno); |
647 |
|
648 |
/* tuck my process id away */ |
649 |
pidfile_write(pfh); |
650 |
|
651 |
dprintf("off & running....\n"); |
652 |
|
653 |
init(0); |
654 |
/* prevent SIGHUP and SIGCHLD handlers from running in parallel */ |
655 |
sigemptyset(&mask); |
656 |
sigaddset(&mask, SIGCHLD); |
657 |
sact.sa_handler = init; |
658 |
sact.sa_mask = mask; |
659 |
sact.sa_flags = SA_RESTART; |
660 |
(void)sigaction(SIGHUP, &sact, NULL); |
661 |
|
662 |
tvp = &tv; |
663 |
tv.tv_sec = tv.tv_usec = 0; |
664 |
|
665 |
if (fklog != -1 && fklog > fdsrmax) |
666 |
fdsrmax = fklog; |
667 |
if (finet && !SecureMode) { |
668 |
for (i = 0; i < *finet; i++) { |
669 |
if (finet[i+1] != -1 && finet[i+1] > fdsrmax) |
670 |
fdsrmax = finet[i+1]; |
671 |
} |
672 |
} |
673 |
STAILQ_FOREACH(fx, &funixes, next) |
674 |
if (fx->s > fdsrmax) |
675 |
fdsrmax = fx->s; |
676 |
|
677 |
fdsr = (fd_set *)calloc(howmany(fdsrmax+1, NFDBITS), |
678 |
sizeof(fd_mask)); |
679 |
if (fdsr == NULL) |
680 |
errx(1, "calloc fd_set"); |
681 |
|
682 |
for (;;) { |
683 |
if (MarkSet) |
684 |
markit(); |
685 |
if (WantDie) |
686 |
die(WantDie); |
687 |
|
688 |
bzero(fdsr, howmany(fdsrmax+1, NFDBITS) * |
689 |
sizeof(fd_mask)); |
690 |
|
691 |
if (fklog != -1) |
692 |
FD_SET(fklog, fdsr); |
693 |
if (finet && !SecureMode) { |
694 |
for (i = 0; i < *finet; i++) { |
695 |
if (finet[i+1] != -1) |
696 |
FD_SET(finet[i+1], fdsr); |
697 |
} |
698 |
} |
699 |
STAILQ_FOREACH(fx, &funixes, next) |
700 |
FD_SET(fx->s, fdsr); |
701 |
|
702 |
i = select(fdsrmax+1, fdsr, NULL, NULL, |
703 |
needdofsync ? &tv : tvp); |
704 |
switch (i) { |
705 |
case 0: |
706 |
dofsync(); |
707 |
needdofsync = 0; |
708 |
if (tvp) { |
709 |
tvp = NULL; |
710 |
if (ppid != 1) |
711 |
kill(ppid, SIGALRM); |
712 |
} |
713 |
continue; |
714 |
case -1: |
715 |
if (errno != EINTR) |
716 |
logerror("select"); |
717 |
continue; |
718 |
} |
719 |
if (fklog != -1 && FD_ISSET(fklog, fdsr)) |
720 |
readklog(); |
721 |
if (finet && !SecureMode) { |
722 |
for (i = 0; i < *finet; i++) { |
723 |
if (FD_ISSET(finet[i+1], fdsr)) { |
724 |
len = sizeof(frominet); |
725 |
l = recvfrom(finet[i+1], line, MAXLINE, |
726 |
0, (struct sockaddr *)&frominet, |
727 |
&len); |
728 |
if (l > 0) { |
729 |
line[l] = '\0'; |
730 |
hname = cvthname((struct sockaddr *)&frominet); |
731 |
unmapped((struct sockaddr *)&frominet); |
732 |
if (validate((struct sockaddr *)&frominet, hname)) |
733 |
printline(hname, line, RemoteAddDate ? ADDDATE : 0); |
734 |
} else if (l < 0 && errno != EINTR) |
735 |
logerror("recvfrom inet"); |
736 |
} |
737 |
} |
738 |
} |
739 |
STAILQ_FOREACH(fx, &funixes, next) { |
740 |
if (FD_ISSET(fx->s, fdsr)) { |
741 |
len = sizeof(fromunix); |
742 |
l = recvfrom(fx->s, line, MAXLINE, 0, |
743 |
(struct sockaddr *)&fromunix, &len); |
744 |
if (l > 0) { |
745 |
line[l] = '\0'; |
746 |
printline(LocalHostName, line, 0); |
747 |
} else if (l < 0 && errno != EINTR) |
748 |
logerror("recvfrom unix"); |
749 |
} |
750 |
} |
751 |
} |
752 |
if (fdsr) |
753 |
free(fdsr); |
754 |
} |
755 |
|
756 |
static void |
757 |
unmapped(struct sockaddr *sa) |
758 |
{ |
759 |
struct sockaddr_in6 *sin6; |
760 |
struct sockaddr_in sin4; |
761 |
|
762 |
if (sa->sa_family != AF_INET6) |
763 |
return; |
764 |
if (sa->sa_len != sizeof(struct sockaddr_in6) || |
765 |
sizeof(sin4) > sa->sa_len) |
766 |
return; |
767 |
sin6 = (struct sockaddr_in6 *)sa; |
768 |
if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) |
769 |
return; |
770 |
|
771 |
memset(&sin4, 0, sizeof(sin4)); |
772 |
sin4.sin_family = AF_INET; |
773 |
sin4.sin_len = sizeof(struct sockaddr_in); |
774 |
memcpy(&sin4.sin_addr, &sin6->sin6_addr.s6_addr[12], |
775 |
sizeof(sin4.sin_addr)); |
776 |
sin4.sin_port = sin6->sin6_port; |
777 |
|
778 |
memcpy(sa, &sin4, sin4.sin_len); |
779 |
} |
780 |
|
781 |
static void |
782 |
usage(void) |
783 |
{ |
784 |
|
785 |
fprintf(stderr, "%s\n%s\n%s\n%s\n", |
786 |
"usage: syslogd [-468ACcdFknosTuv] [-a allowed_peer]", |
787 |
" [-b bind_address] [-f config_file]", |
788 |
" [-l [mode:]path] [-m mark_interval]", |
789 |
" [-P pid_file] [-p log_socket]"); |
790 |
exit(1); |
791 |
} |
792 |
|
793 |
/* |
794 |
* Take a raw input line, decode the message, and print the message |
795 |
* on the appropriate log files. |
796 |
*/ |
797 |
static void |
798 |
printline(const char *hname, char *msg, int flags) |
799 |
{ |
800 |
char *p, *q; |
801 |
long n; |
802 |
int c, pri; |
803 |
char line[MAXLINE + 1]; |
804 |
|
805 |
/* test for special codes */ |
806 |
p = msg; |
807 |
pri = DEFUPRI; |
808 |
if (*p == '<') { |
809 |
errno = 0; |
810 |
n = strtol(p + 1, &q, 10); |
811 |
if (*q == '>' && n >= 0 && n < INT_MAX && errno == 0) { |
812 |
p = q + 1; |
813 |
pri = n; |
814 |
} |
815 |
} |
816 |
if (pri &~ (LOG_FACMASK|LOG_PRIMASK)) |
817 |
pri = DEFUPRI; |
818 |
|
819 |
/* |
820 |
* Don't allow users to log kernel messages. |
821 |
* NOTE: since LOG_KERN == 0 this will also match |
822 |
* messages with no facility specified. |
823 |
*/ |
824 |
if ((pri & LOG_FACMASK) == LOG_KERN && !KeepKernFac) |
825 |
pri = LOG_MAKEPRI(LOG_USER, LOG_PRI(pri)); |
826 |
|
827 |
q = line; |
828 |
|
829 |
while ((c = (unsigned char)*p++) != '\0' && |
830 |
q < &line[sizeof(line) - 4]) { |
831 |
if (mask_C1 && (c & 0x80) && c < 0xA0) { |
832 |
c &= 0x7F; |
833 |
*q++ = 'M'; |
834 |
*q++ = '-'; |
835 |
} |
836 |
if (isascii(c) && iscntrl(c)) { |
837 |
if (c == '\n') { |
838 |
*q++ = ' '; |
839 |
} else if (c == '\t') { |
840 |
*q++ = '\t'; |
841 |
} else { |
842 |
*q++ = '^'; |
843 |
*q++ = c ^ 0100; |
844 |
} |
845 |
} else { |
846 |
*q++ = c; |
847 |
} |
848 |
} |
849 |
*q = '\0'; |
850 |
|
851 |
logmsg(pri, line, hname, flags); |
852 |
} |
853 |
|
854 |
/* |
855 |
* Read /dev/klog while data are available, split into lines. |
856 |
*/ |
857 |
static void |
858 |
readklog(void) |
859 |
{ |
860 |
char *p, *q, line[MAXLINE + 1]; |
861 |
int len, i; |
862 |
|
863 |
len = 0; |
864 |
for (;;) { |
865 |
i = read(fklog, line + len, MAXLINE - 1 - len); |
866 |
if (i > 0) { |
867 |
line[i + len] = '\0'; |
868 |
} else { |
869 |
if (i < 0 && errno != EINTR && errno != EAGAIN) { |
870 |
logerror("klog"); |
871 |
fklog = -1; |
872 |
} |
873 |
break; |
874 |
} |
875 |
|
876 |
for (p = line; (q = strchr(p, '\n')) != NULL; p = q + 1) { |
877 |
*q = '\0'; |
878 |
printsys(p); |
879 |
} |
880 |
len = strlen(p); |
881 |
if (len >= MAXLINE - 1) { |
882 |
printsys(p); |
883 |
len = 0; |
884 |
} |
885 |
if (len > 0) |
886 |
memmove(line, p, len + 1); |
887 |
} |
888 |
if (len > 0) |
889 |
printsys(line); |
890 |
} |
891 |
|
892 |
/* |
893 |
* Take a raw input line from /dev/klog, format similar to syslog(). |
894 |
*/ |
895 |
static void |
896 |
printsys(char *msg) |
897 |
{ |
898 |
char *p, *q; |
899 |
long n; |
900 |
int flags, isprintf, pri; |
901 |
|
902 |
flags = ISKERNEL | SYNC_FILE | ADDDATE; /* fsync after write */ |
903 |
p = msg; |
904 |
pri = DEFSPRI; |
905 |
isprintf = 1; |
906 |
if (*p == '<') { |
907 |
errno = 0; |
908 |
n = strtol(p + 1, &q, 10); |
909 |
if (*q == '>' && n >= 0 && n < INT_MAX && errno == 0) { |
910 |
p = q + 1; |
911 |
pri = n; |
912 |
isprintf = 0; |
913 |
} |
914 |
} |
915 |
/* |
916 |
* Kernel printf's and LOG_CONSOLE messages have been displayed |
917 |
* on the console already. |
918 |
*/ |
919 |
if (isprintf || (pri & LOG_FACMASK) == LOG_CONSOLE) |
920 |
flags |= IGN_CONS; |
921 |
if (pri &~ (LOG_FACMASK|LOG_PRIMASK)) |
922 |
pri = DEFSPRI; |
923 |
logmsg(pri, p, LocalHostName, flags); |
924 |
} |
925 |
|
926 |
static time_t now; |
927 |
|
928 |
/* |
929 |
* Match a program or host name against a specification. |
930 |
* Return a non-0 value if the message must be ignored |
931 |
* based on the specification. |
932 |
*/ |
933 |
static int |
934 |
skip_message(const char *name, const char *spec, int checkcase) |
935 |
{ |
936 |
const char *s; |
937 |
char prev, next; |
938 |
int exclude = 0; |
939 |
/* Behaviour on explicit match */ |
940 |
|
941 |
if (spec == NULL) |
942 |
return 0; |
943 |
switch (*spec) { |
944 |
case '-': |
945 |
exclude = 1; |
946 |
/*FALLTHROUGH*/ |
947 |
case '+': |
948 |
spec++; |
949 |
break; |
950 |
default: |
951 |
break; |
952 |
} |
953 |
if (checkcase) |
954 |
s = strstr (spec, name); |
955 |
else |
956 |
s = strcasestr (spec, name); |
957 |
|
958 |
if (s != NULL) { |
959 |
prev = (s == spec ? ',' : *(s - 1)); |
960 |
next = *(s + strlen (name)); |
961 |
|
962 |
if (prev == ',' && (next == '\0' || next == ',')) |
963 |
/* Explicit match: skip iff the spec is an |
964 |
exclusive one. */ |
965 |
return exclude; |
966 |
} |
967 |
|
968 |
/* No explicit match for this name: skip the message iff |
969 |
the spec is an inclusive one. */ |
970 |
return !exclude; |
971 |
} |
972 |
|
973 |
/* |
974 |
* Log a message to the appropriate log files, users, etc. based on |
975 |
* the priority. |
976 |
*/ |
977 |
static void |
978 |
logmsg(int pri, const char *msg, const char *from, int flags) |
979 |
{ |
980 |
struct filed *f; |
981 |
int i, fac, msglen, omask, prilev; |
982 |
const char *timestamp; |
983 |
char prog[NAME_MAX+1]; |
984 |
char buf[MAXLINE+1]; |
985 |
|
986 |
dprintf("logmsg: pri %o, flags %x, from %s, msg %s\n", |
987 |
pri, flags, from, msg); |
988 |
|
989 |
omask = sigblock(sigmask(SIGHUP)|sigmask(SIGALRM)); |
990 |
|
991 |
/* |
992 |
* Check to see if msg looks non-standard. |
993 |
*/ |
994 |
msglen = strlen(msg); |
995 |
if (msglen < MAXDATELEN || msg[3] != ' ' || msg[6] != ' ' || |
996 |
msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') |
997 |
flags |= ADDDATE; |
998 |
|
999 |
(void)time(&now); |
1000 |
if (flags & ADDDATE) { |
1001 |
timestamp = ctime(&now) + 4; |
1002 |
} else { |
1003 |
timestamp = msg; |
1004 |
msg += MAXDATELEN; |
1005 |
msglen -= MAXDATELEN; |
1006 |
} |
1007 |
|
1008 |
/* skip leading blanks */ |
1009 |
while (isspace(*msg)) { |
1010 |
msg++; |
1011 |
msglen--; |
1012 |
} |
1013 |
|
1014 |
/* extract facility and priority level */ |
1015 |
if (flags & MARK) |
1016 |
fac = LOG_NFACILITIES; |
1017 |
else |
1018 |
fac = LOG_FAC(pri); |
1019 |
|
1020 |
/* Check maximum facility number. */ |
1021 |
if (fac > LOG_NFACILITIES) { |
1022 |
(void)sigsetmask(omask); |
1023 |
return; |
1024 |
} |
1025 |
|
1026 |
prilev = LOG_PRI(pri); |
1027 |
|
1028 |
/* extract program name */ |
1029 |
for (i = 0; i < NAME_MAX; i++) { |
1030 |
if (!isprint(msg[i]) || msg[i] == ':' || msg[i] == '[' || |
1031 |
msg[i] == '/' || isspace(msg[i])) |
1032 |
break; |
1033 |
prog[i] = msg[i]; |
1034 |
} |
1035 |
prog[i] = 0; |
1036 |
|
1037 |
/* add kernel prefix for kernel messages */ |
1038 |
if (flags & ISKERNEL) { |
1039 |
snprintf(buf, sizeof(buf), "%s: %s", |
1040 |
use_bootfile ? bootfile : "kernel", msg); |
1041 |
msg = buf; |
1042 |
msglen = strlen(buf); |
1043 |
} |
1044 |
|
1045 |
/* log the message to the particular outputs */ |
1046 |
if (!Initialized) { |
1047 |
f = &consfile; |
1048 |
/* |
1049 |
* Open in non-blocking mode to avoid hangs during open |
1050 |
* and close(waiting for the port to drain). |
1051 |
*/ |
1052 |
f->f_file = open(ctty, O_WRONLY | O_NONBLOCK, 0); |
1053 |
|
1054 |
if (f->f_file >= 0) { |
1055 |
(void)strlcpy(f->f_lasttime, timestamp, |
1056 |
sizeof(f->f_lasttime)); |
1057 |
fprintlog(f, flags, msg); |
1058 |
close(f->f_file); |
1059 |
f->f_file = -1; |
1060 |
} |
1061 |
(void)sigsetmask(omask); |
1062 |
return; |
1063 |
} |
1064 |
for (f = Files; f; f = f->f_next) { |
1065 |
/* skip messages that are incorrect priority */ |
1066 |
if (!(((f->f_pcmp[fac] & PRI_EQ) && (f->f_pmask[fac] == prilev)) |
1067 |
||((f->f_pcmp[fac] & PRI_LT) && (f->f_pmask[fac] < prilev)) |
1068 |
||((f->f_pcmp[fac] & PRI_GT) && (f->f_pmask[fac] > prilev)) |
1069 |
) |
1070 |
|| f->f_pmask[fac] == INTERNAL_NOPRI) |
1071 |
continue; |
1072 |
|
1073 |
/* skip messages with the incorrect hostname */ |
1074 |
if (skip_message(from, f->f_host, 0)) |
1075 |
continue; |
1076 |
|
1077 |
/* skip messages with the incorrect program name */ |
1078 |
if (skip_message(prog, f->f_program, 1)) |
1079 |
continue; |
1080 |
|
1081 |
/* skip message to console if it has already been printed */ |
1082 |
if (f->f_type == F_CONSOLE && (flags & IGN_CONS)) |
1083 |
continue; |
1084 |
|
1085 |
/* don't output marks to recently written files */ |
1086 |
if ((flags & MARK) && (now - f->f_time) < MarkInterval / 2) |
1087 |
continue; |
1088 |
|
1089 |
/* |
1090 |
* suppress duplicate lines to this file |
1091 |
*/ |
1092 |
if (no_compress - (f->f_type != F_PIPE) < 1 && |
1093 |
(flags & MARK) == 0 && msglen == f->f_prevlen && |
1094 |
!strcmp(msg, f->f_prevline) && |
1095 |
!strcasecmp(from, f->f_prevhost)) { |
1096 |
(void)strlcpy(f->f_lasttime, timestamp, |
1097 |
sizeof(f->f_lasttime)); |
1098 |
f->f_prevcount++; |
1099 |
dprintf("msg repeated %d times, %ld sec of %d\n", |
1100 |
f->f_prevcount, (long)(now - f->f_time), |
1101 |
repeatinterval[f->f_repeatcount]); |
1102 |
/* |
1103 |
* If domark would have logged this by now, |
1104 |
* flush it now (so we don't hold isolated messages), |
1105 |
* but back off so we'll flush less often |
1106 |
* in the future. |
1107 |
*/ |
1108 |
if (now > REPEATTIME(f)) { |
1109 |
fprintlog(f, flags, (char *)NULL); |
1110 |
BACKOFF(f); |
1111 |
} |
1112 |
} else { |
1113 |
/* new line, save it */ |
1114 |
if (f->f_prevcount) |
1115 |
fprintlog(f, 0, (char *)NULL); |
1116 |
f->f_repeatcount = 0; |
1117 |
f->f_prevpri = pri; |
1118 |
(void)strlcpy(f->f_lasttime, timestamp, |
1119 |
sizeof(f->f_lasttime)); |
1120 |
(void)strlcpy(f->f_prevhost, from, |
1121 |
sizeof(f->f_prevhost)); |
1122 |
if (msglen < MAXSVLINE) { |
1123 |
f->f_prevlen = msglen; |
1124 |
(void)strlcpy(f->f_prevline, msg, sizeof(f->f_prevline)); |
1125 |
fprintlog(f, flags, (char *)NULL); |
1126 |
} else { |
1127 |
f->f_prevline[0] = 0; |
1128 |
f->f_prevlen = 0; |
1129 |
fprintlog(f, flags, msg); |
1130 |
} |
1131 |
} |
1132 |
} |
1133 |
(void)sigsetmask(omask); |
1134 |
} |
1135 |
|
1136 |
static void |
1137 |
dofsync(void) |
1138 |
{ |
1139 |
struct filed *f; |
1140 |
|
1141 |
for (f = Files; f; f = f->f_next) { |
1142 |
if ((f->f_type == F_FILE) && |
1143 |
(f->f_flags & FFLAG_NEEDSYNC)) { |
1144 |
f->f_flags &= ~FFLAG_NEEDSYNC; |
1145 |
(void)fsync(f->f_file); |
1146 |
} |
1147 |
} |
1148 |
} |
1149 |
|
1150 |
#define IOV_SIZE 7 |
1151 |
static void |
1152 |
fprintlog(struct filed *f, int flags, const char *msg) |
1153 |
{ |
1154 |
struct iovec iov[IOV_SIZE]; |
1155 |
struct iovec *v; |
1156 |
struct addrinfo *r; |
1157 |
int i, l, lsent = 0; |
1158 |
char line[MAXLINE + 1], repbuf[80], greetings[200], *wmsg = NULL; |
1159 |
char nul[] = "", space[] = " ", lf[] = "\n", crlf[] = "\r\n"; |
1160 |
const char *msgret; |
1161 |
|
1162 |
v = iov; |
1163 |
if (f->f_type == F_WALL) { |
1164 |
v->iov_base = greetings; |
1165 |
/* The time displayed is not synchornized with the other log |
1166 |
* destinations (like messages). Following fragment was using |
1167 |
* ctime(&now), which was updating the time every 30 sec. |
1168 |
* With f_lasttime, time is synchronized correctly. |
1169 |
*/ |
1170 |
v->iov_len = snprintf(greetings, sizeof greetings, |
1171 |
"\r\n\7Message from syslogd@%s at %.24s ...\r\n", |
1172 |
f->f_prevhost, f->f_lasttime); |
1173 |
if (v->iov_len >= sizeof greetings) |
1174 |
v->iov_len = sizeof greetings - 1; |
1175 |
v++; |
1176 |
v->iov_base = nul; |
1177 |
v->iov_len = 0; |
1178 |
v++; |
1179 |
} else { |
1180 |
v->iov_base = f->f_lasttime; |
1181 |
v->iov_len = strlen(f->f_lasttime); |
1182 |
v++; |
1183 |
v->iov_base = space; |
1184 |
v->iov_len = 1; |
1185 |
v++; |
1186 |
} |
1187 |
|
1188 |
if (LogFacPri) { |
1189 |
static char fp_buf[30]; /* Hollow laugh */ |
1190 |
int fac = f->f_prevpri & LOG_FACMASK; |
1191 |
int pri = LOG_PRI(f->f_prevpri); |
1192 |
const char *f_s = NULL; |
1193 |
char f_n[5]; /* Hollow laugh */ |
1194 |
const char *p_s = NULL; |
1195 |
char p_n[5]; /* Hollow laugh */ |
1196 |
|
1197 |
if (LogFacPri > 1) { |
1198 |
const CODE *c; |
1199 |
|
1200 |
for (c = facilitynames; c->c_name; c++) { |
1201 |
if (c->c_val == fac) { |
1202 |
f_s = c->c_name; |
1203 |
break; |
1204 |
} |
1205 |
} |
1206 |
for (c = prioritynames; c->c_name; c++) { |
1207 |
if (c->c_val == pri) { |
1208 |
p_s = c->c_name; |
1209 |
break; |
1210 |
} |
1211 |
} |
1212 |
} |
1213 |
if (!f_s) { |
1214 |
snprintf(f_n, sizeof f_n, "%d", LOG_FAC(fac)); |
1215 |
f_s = f_n; |
1216 |
} |
1217 |
if (!p_s) { |
1218 |
snprintf(p_n, sizeof p_n, "%d", pri); |
1219 |
p_s = p_n; |
1220 |
} |
1221 |
snprintf(fp_buf, sizeof fp_buf, "<%s.%s> ", f_s, p_s); |
1222 |
v->iov_base = fp_buf; |
1223 |
v->iov_len = strlen(fp_buf); |
1224 |
} else { |
1225 |
v->iov_base = nul; |
1226 |
v->iov_len = 0; |
1227 |
} |
1228 |
v++; |
1229 |
|
1230 |
v->iov_base = f->f_prevhost; |
1231 |
v->iov_len = strlen(v->iov_base); |
1232 |
v++; |
1233 |
v->iov_base = space; |
1234 |
v->iov_len = 1; |
1235 |
v++; |
1236 |
|
1237 |
if (msg) { |
1238 |
wmsg = strdup(msg); /* XXX iov_base needs a `const' sibling. */ |
1239 |
if (wmsg == NULL) { |
1240 |
logerror("strdup"); |
1241 |
exit(1); |
1242 |
} |
1243 |
v->iov_base = wmsg; |
1244 |
v->iov_len = strlen(msg); |
1245 |
} else if (f->f_prevcount > 1) { |
1246 |
v->iov_base = repbuf; |
1247 |
v->iov_len = snprintf(repbuf, sizeof repbuf, |
1248 |
"last message repeated %d times", f->f_prevcount); |
1249 |
} else { |
1250 |
v->iov_base = f->f_prevline; |
1251 |
v->iov_len = f->f_prevlen; |
1252 |
} |
1253 |
v++; |
1254 |
|
1255 |
dprintf("Logging to %s", TypeNames[f->f_type]); |
1256 |
f->f_time = now; |
1257 |
|
1258 |
switch (f->f_type) { |
1259 |
int port; |
1260 |
case F_UNUSED: |
1261 |
dprintf("\n"); |
1262 |
break; |
1263 |
|
1264 |
case F_FORW: |
1265 |
port = (int)ntohs(((struct sockaddr_in *) |
1266 |
(f->f_un.f_forw.f_addr->ai_addr))->sin_port); |
1267 |
if (port != 514) { |
1268 |
dprintf(" %s:%d\n", f->f_un.f_forw.f_hname, port); |
1269 |
} else { |
1270 |
dprintf(" %s\n", f->f_un.f_forw.f_hname); |
1271 |
} |
1272 |
/* check for local vs remote messages */ |
1273 |
if (strcasecmp(f->f_prevhost, LocalHostName)) |
1274 |
l = snprintf(line, sizeof line - 1, |
1275 |
"<%d>%.15s Forwarded from %s: %s", |
1276 |
f->f_prevpri, (char *)iov[0].iov_base, |
1277 |
f->f_prevhost, (char *)iov[5].iov_base); |
1278 |
else |
1279 |
l = snprintf(line, sizeof line - 1, "<%d>%.15s %s", |
1280 |
f->f_prevpri, (char *)iov[0].iov_base, |
1281 |
(char *)iov[5].iov_base); |
1282 |
if (l < 0) |
1283 |
l = 0; |
1284 |
else if (l > MAXLINE) |
1285 |
l = MAXLINE; |
1286 |
|
1287 |
if (finet) { |
1288 |
for (r = f->f_un.f_forw.f_addr; r; r = r->ai_next) { |
1289 |
for (i = 0; i < *finet; i++) { |
1290 |
#if 0 |
1291 |
/* |
1292 |
* should we check AF first, or just |
1293 |
* trial and error? FWD |
1294 |
*/ |
1295 |
if (r->ai_family == |
1296 |
address_family_of(finet[i+1])) |
1297 |
#endif |
1298 |
lsent = sendto(finet[i+1], line, l, 0, |
1299 |
r->ai_addr, r->ai_addrlen); |
1300 |
if (lsent == l) |
1301 |
break; |
1302 |
} |
1303 |
if (lsent == l && !send_to_all) |
1304 |
break; |
1305 |
} |
1306 |
dprintf("lsent/l: %d/%d\n", lsent, l); |
1307 |
if (lsent != l) { |
1308 |
int e = errno; |
1309 |
logerror("sendto"); |
1310 |
errno = e; |
1311 |
switch (errno) { |
1312 |
case ENOBUFS: |
1313 |
case ENETDOWN: |
1314 |
case ENETUNREACH: |
1315 |
case EHOSTUNREACH: |
1316 |
case EHOSTDOWN: |
1317 |
case EADDRNOTAVAIL: |
1318 |
break; |
1319 |
/* case EBADF: */ |
1320 |
/* case EACCES: */ |
1321 |
/* case ENOTSOCK: */ |
1322 |
/* case EFAULT: */ |
1323 |
/* case EMSGSIZE: */ |
1324 |
/* case EAGAIN: */ |
1325 |
/* case ENOBUFS: */ |
1326 |
/* case ECONNREFUSED: */ |
1327 |
default: |
1328 |
dprintf("removing entry: errno=%d\n", e); |
1329 |
f->f_type = F_UNUSED; |
1330 |
break; |
1331 |
} |
1332 |
} |
1333 |
} |
1334 |
break; |
1335 |
|
1336 |
case F_FILE: |
1337 |
dprintf(" %s\n", f->f_un.f_fname); |
1338 |
v->iov_base = lf; |
1339 |
v->iov_len = 1; |
1340 |
if (writev(f->f_file, iov, IOV_SIZE) < 0) { |
1341 |
/* |
1342 |
* If writev(2) fails for potentially transient errors |
1343 |
* like the filesystem being full, ignore it. |
1344 |
* Otherwise remove this logfile from the list. |
1345 |
*/ |
1346 |
if (errno != ENOSPC) { |
1347 |
int e = errno; |
1348 |
close_filed(f); |
1349 |
errno = e; |
1350 |
logerror(f->f_un.f_fname); |
1351 |
} |
1352 |
} else if ((flags & SYNC_FILE) && (f->f_flags & FFLAG_SYNC)) { |
1353 |
f->f_flags |= FFLAG_NEEDSYNC; |
1354 |
needdofsync = 1; |
1355 |
} |
1356 |
break; |
1357 |
|
1358 |
case F_PIPE: |
1359 |
dprintf(" %s\n", f->f_un.f_pipe.f_pname); |
1360 |
v->iov_base = lf; |
1361 |
v->iov_len = 1; |
1362 |
if (f->f_un.f_pipe.f_pid == 0) { |
1363 |
if ((f->f_file = p_open(f->f_un.f_pipe.f_pname, |
1364 |
&f->f_un.f_pipe.f_pid)) < 0) { |
1365 |
logerror(f->f_un.f_pipe.f_pname); |
1366 |
break; |
1367 |
} |
1368 |
} |
1369 |
if (writev(f->f_file, iov, IOV_SIZE) < 0) { |
1370 |
int e = errno; |
1371 |
|
1372 |
close_filed(f); |
1373 |
deadq_enter(f->f_un.f_pipe.f_pid, |
1374 |
f->f_un.f_pipe.f_pname); |
1375 |
errno = e; |
1376 |
logerror(f->f_un.f_pipe.f_pname); |
1377 |
} |
1378 |
break; |
1379 |
|
1380 |
case F_CONSOLE: |
1381 |
if (flags & IGN_CONS) { |
1382 |
dprintf(" (ignored)\n"); |
1383 |
break; |
1384 |
} |
1385 |
/* FALLTHROUGH */ |
1386 |
|
1387 |
case F_TTY: |
1388 |
dprintf(" %s%s\n", _PATH_DEV, f->f_un.f_fname); |
1389 |
v->iov_base = crlf; |
1390 |
v->iov_len = 2; |
1391 |
|
1392 |
errno = 0; /* ttymsg() only sometimes returns an errno */ |
1393 |
if ((msgret = ttymsg(iov, IOV_SIZE, f->f_un.f_fname, 10))) { |
1394 |
f->f_type = F_UNUSED; |
1395 |
logerror(msgret); |
1396 |
} |
1397 |
break; |
1398 |
|
1399 |
case F_USERS: |
1400 |
case F_WALL: |
1401 |
dprintf("\n"); |
1402 |
v->iov_base = crlf; |
1403 |
v->iov_len = 2; |
1404 |
wallmsg(f, iov, IOV_SIZE); |
1405 |
break; |
1406 |
} |
1407 |
f->f_prevcount = 0; |
1408 |
free(wmsg); |
1409 |
} |
1410 |
|
1411 |
/* |
1412 |
* WALLMSG -- Write a message to the world at large |
1413 |
* |
1414 |
* Write the specified message to either the entire |
1415 |
* world, or a list of approved users. |
1416 |
*/ |
1417 |
static void |
1418 |
wallmsg(struct filed *f, struct iovec *iov, const int iovlen) |
1419 |
{ |
1420 |
static int reenter; /* avoid calling ourselves */ |
1421 |
struct utmpx *ut; |
1422 |
int i; |
1423 |
const char *p; |
1424 |
|
1425 |
if (reenter++) |
1426 |
return; |
1427 |
setutxent(); |
1428 |
/* NOSTRICT */ |
1429 |
while ((ut = getutxent()) != NULL) { |
1430 |
if (ut->ut_type != USER_PROCESS) |
1431 |
continue; |
1432 |
if (f->f_type == F_WALL) { |
1433 |
if ((p = ttymsg(iov, iovlen, ut->ut_line, |
1434 |
TTYMSGTIME)) != NULL) { |
1435 |
errno = 0; /* already in msg */ |
1436 |
logerror(p); |
1437 |
} |
1438 |
continue; |
1439 |
} |
1440 |
/* should we send the message to this user? */ |
1441 |
for (i = 0; i < MAXUNAMES; i++) { |
1442 |
if (!f->f_un.f_uname[i][0]) |
1443 |
break; |
1444 |
if (!strcmp(f->f_un.f_uname[i], ut->ut_user)) { |
1445 |
if ((p = ttymsg_check(iov, iovlen, ut->ut_line, |
1446 |
TTYMSGTIME)) != NULL) { |
1447 |
errno = 0; /* already in msg */ |
1448 |
logerror(p); |
1449 |
} |
1450 |
break; |
1451 |
} |
1452 |
} |
1453 |
} |
1454 |
endutxent(); |
1455 |
reenter = 0; |
1456 |
} |
1457 |
|
1458 |
/* |
1459 |
* Wrapper routine for ttymsg() that checks the terminal for messages enabled. |
1460 |
*/ |
1461 |
static const char * |
1462 |
ttymsg_check(struct iovec *iov, int iovcnt, char *line, int tmout) |
1463 |
{ |
1464 |
static char device[1024]; |
1465 |
static char errbuf[1024]; |
1466 |
struct stat sb; |
1467 |
|
1468 |
(void) snprintf(device, sizeof(device), "%s%s", _PATH_DEV, line); |
1469 |
|
1470 |
if (stat(device, &sb) < 0) { |
1471 |
(void) snprintf(errbuf, sizeof(errbuf), |
1472 |
"%s: %s", device, strerror(errno)); |
1473 |
return (errbuf); |
1474 |
} |
1475 |
if ((sb.st_mode & S_IWGRP) == 0) |
1476 |
/* Messages disabled. */ |
1477 |
return (NULL); |
1478 |
return ttymsg(iov, iovcnt, line, tmout); |
1479 |
} |
1480 |
|
1481 |
static void |
1482 |
reapchild(int signo __unused) |
1483 |
{ |
1484 |
int status; |
1485 |
pid_t pid; |
1486 |
struct filed *f; |
1487 |
|
1488 |
while ((pid = wait3(&status, WNOHANG, (struct rusage *)NULL)) > 0) { |
1489 |
if (!Initialized) |
1490 |
/* Don't tell while we are initting. */ |
1491 |
continue; |
1492 |
|
1493 |
/* First, look if it's a process from the dead queue. */ |
1494 |
if (deadq_remove(pid)) |
1495 |
goto oncemore; |
1496 |
|
1497 |
/* Now, look in list of active processes. */ |
1498 |
for (f = Files; f; f = f->f_next) |
1499 |
if (f->f_type == F_PIPE && |
1500 |
f->f_un.f_pipe.f_pid == pid) { |
1501 |
close_filed(f); |
1502 |
log_deadchild(pid, status, |
1503 |
f->f_un.f_pipe.f_pname); |
1504 |
break; |
1505 |
} |
1506 |
oncemore: |
1507 |
continue; |
1508 |
} |
1509 |
} |
1510 |
|
1511 |
/* |
1512 |
* Return a printable representation of a host address. |
1513 |
*/ |
1514 |
static const char * |
1515 |
cvthname(struct sockaddr *f) |
1516 |
{ |
1517 |
int error, hl; |
1518 |
sigset_t omask, nmask; |
1519 |
static char hname[NI_MAXHOST], ip[NI_MAXHOST]; |
1520 |
|
1521 |
error = getnameinfo((struct sockaddr *)f, |
1522 |
((struct sockaddr *)f)->sa_len, |
1523 |
ip, sizeof ip, NULL, 0, NI_NUMERICHOST); |
1524 |
dprintf("cvthname(%s)\n", ip); |
1525 |
|
1526 |
if (error) { |
1527 |
dprintf("Malformed from address %s\n", gai_strerror(error)); |
1528 |
return ("???"); |
1529 |
} |
1530 |
if (!resolve) |
1531 |
return (ip); |
1532 |
|
1533 |
sigemptyset(&nmask); |
1534 |
sigaddset(&nmask, SIGHUP); |
1535 |
sigprocmask(SIG_BLOCK, &nmask, &omask); |
1536 |
error = getnameinfo((struct sockaddr *)f, |
1537 |
((struct sockaddr *)f)->sa_len, |
1538 |
hname, sizeof hname, NULL, 0, NI_NAMEREQD); |
1539 |
sigprocmask(SIG_SETMASK, &omask, NULL); |
1540 |
if (error) { |
1541 |
dprintf("Host name for your address (%s) unknown\n", ip); |
1542 |
return (ip); |
1543 |
} |
1544 |
hl = strlen(hname); |
1545 |
if (hl > 0 && hname[hl-1] == '.') |
1546 |
hname[--hl] = '\0'; |
1547 |
trimdomain(hname, hl); |
1548 |
return (hname); |
1549 |
} |
1550 |
|
1551 |
static void |
1552 |
dodie(int signo) |
1553 |
{ |
1554 |
|
1555 |
WantDie = signo; |
1556 |
} |
1557 |
|
1558 |
static void |
1559 |
domark(int signo __unused) |
1560 |
{ |
1561 |
|
1562 |
MarkSet = 1; |
1563 |
} |
1564 |
|
1565 |
/* |
1566 |
* Print syslogd errors some place. |
1567 |
*/ |
1568 |
static void |
1569 |
logerror(const char *type) |
1570 |
{ |
1571 |
char buf[512]; |
1572 |
static int recursed = 0; |
1573 |
|
1574 |
/* If there's an error while trying to log an error, give up. */ |
1575 |
if (recursed) |
1576 |
return; |
1577 |
recursed++; |
1578 |
if (errno) |
1579 |
(void)snprintf(buf, |
1580 |
sizeof buf, "syslogd: %s: %s", type, strerror(errno)); |
1581 |
else |
1582 |
(void)snprintf(buf, sizeof buf, "syslogd: %s", type); |
1583 |
errno = 0; |
1584 |
dprintf("%s\n", buf); |
1585 |
logmsg(LOG_SYSLOG|LOG_ERR, buf, LocalHostName, ADDDATE); |
1586 |
recursed--; |
1587 |
} |
1588 |
|
1589 |
static void |
1590 |
die(int signo) |
1591 |
{ |
1592 |
struct filed *f; |
1593 |
struct funix *fx; |
1594 |
int was_initialized; |
1595 |
char buf[100]; |
1596 |
|
1597 |
was_initialized = Initialized; |
1598 |
Initialized = 0; /* Don't log SIGCHLDs. */ |
1599 |
for (f = Files; f != NULL; f = f->f_next) { |
1600 |
/* flush any pending output */ |
1601 |
if (f->f_prevcount) |
1602 |
fprintlog(f, 0, (char *)NULL); |
1603 |
if (f->f_type == F_PIPE && f->f_un.f_pipe.f_pid > 0) |
1604 |
close_filed(f); |
1605 |
} |
1606 |
Initialized = was_initialized; |
1607 |
if (signo) { |
1608 |
dprintf("syslogd: exiting on signal %d\n", signo); |
1609 |
(void)snprintf(buf, sizeof(buf), "exiting on signal %d", signo); |
1610 |
errno = 0; |
1611 |
logerror(buf); |
1612 |
} |
1613 |
STAILQ_FOREACH(fx, &funixes, next) |
1614 |
(void)unlink(fx->name); |
1615 |
pidfile_remove(pfh); |
1616 |
|
1617 |
exit(1); |
1618 |
} |
1619 |
|
1620 |
static int |
1621 |
configfiles(const struct dirent *dp) |
1622 |
{ |
1623 |
const char *p; |
1624 |
size_t ext_len; |
1625 |
|
1626 |
if (dp->d_name[0] == '.') |
1627 |
return (0); |
1628 |
|
1629 |
ext_len = sizeof(include_ext) -1; |
1630 |
|
1631 |
if (dp->d_namlen <= ext_len) |
1632 |
return (0); |
1633 |
|
1634 |
p = &dp->d_name[dp->d_namlen - ext_len]; |
1635 |
if (strcmp(p, include_ext) != 0) |
1636 |
return (0); |
1637 |
|
1638 |
return (1); |
1639 |
} |
1640 |
|
1641 |
static struct filed ** |
1642 |
readconfigfile(FILE *cf, struct filed **nextp, int allow_includes) |
1643 |
{ |
1644 |
FILE *cf2; |
1645 |
struct filed *f; |
1646 |
struct dirent **ent; |
1647 |
char cline[LINE_MAX]; |
1648 |
char host[MAXHOSTNAMELEN]; |
1649 |
char prog[LINE_MAX]; |
1650 |
char file[MAXPATHLEN]; |
1651 |
char *p, *tmp; |
1652 |
int i, nents; |
1653 |
size_t include_len; |
1654 |
|
1655 |
/* |
1656 |
* Foreach line in the conf table, open that file. |
1657 |
*/ |
1658 |
f = NULL; |
1659 |
include_len = sizeof(include_str) -1; |
1660 |
(void)strlcpy(host, "*", sizeof(host)); |
1661 |
(void)strlcpy(prog, "*", sizeof(prog)); |
1662 |
while (fgets(cline, sizeof(cline), cf) != NULL) { |
1663 |
/* |
1664 |
* check for end-of-section, comments, strip off trailing |
1665 |
* spaces and newline character. #!prog is treated specially: |
1666 |
* following lines apply only to that program. |
1667 |
*/ |
1668 |
for (p = cline; isspace(*p); ++p) |
1669 |
continue; |
1670 |
if (*p == 0) |
1671 |
continue; |
1672 |
if (allow_includes && |
1673 |
strncmp(p, include_str, include_len) == 0 && |
1674 |
isspace(p[include_len])) { |
1675 |
p += include_len; |
1676 |
while (isspace(*p)) |
1677 |
p++; |
1678 |
tmp = p; |
1679 |
while (*tmp != '\0' && !isspace(*tmp)) |
1680 |
tmp++; |
1681 |
*tmp = '\0'; |
1682 |
dprintf("Trying to include files in '%s'\n", p); |
1683 |
nents = scandir(p, &ent, configfiles, alphasort); |
1684 |
if (nents == -1) { |
1685 |
dprintf("Unable to open '%s': %s\n", p, |
1686 |
strerror(errno)); |
1687 |
continue; |
1688 |
} |
1689 |
for (i = 0; i < nents; i++) { |
1690 |
if (snprintf(file, sizeof(file), "%s/%s", p, |
1691 |
ent[i]->d_name) >= (int)sizeof(file)) { |
1692 |
dprintf("ignoring path too long: " |
1693 |
"'%s/%s'\n", p, ent[i]->d_name); |
1694 |
free(ent[i]); |
1695 |
continue; |
1696 |
} |
1697 |
free(ent[i]); |
1698 |
cf2 = fopen(file, "r"); |
1699 |
if (cf2 == NULL) |
1700 |
continue; |
1701 |
dprintf("reading %s\n", file); |
1702 |
nextp = readconfigfile(cf2, nextp, 0); |
1703 |
fclose(cf2); |
1704 |
} |
1705 |
free(ent); |
1706 |
continue; |
1707 |
} |
1708 |
if (*p == '#') { |
1709 |
p++; |
1710 |
if (*p != '!' && *p != '+' && *p != '-') |
1711 |
continue; |
1712 |
} |
1713 |
if (*p == '+' || *p == '-') { |
1714 |
host[0] = *p++; |
1715 |
while (isspace(*p)) |
1716 |
p++; |
1717 |
if ((!*p) || (*p == '*')) { |
1718 |
(void)strlcpy(host, "*", sizeof(host)); |
1719 |
continue; |
1720 |
} |
1721 |
if (*p == '@') |
1722 |
p = LocalHostName; |
1723 |
for (i = 1; i < MAXHOSTNAMELEN - 1; i++) { |
1724 |
if (!isalnum(*p) && *p != '.' && *p != '-' |
1725 |
&& *p != ',' && *p != ':' && *p != '%') |
1726 |
break; |
1727 |
host[i] = *p++; |
1728 |
} |
1729 |
host[i] = '\0'; |
1730 |
continue; |
1731 |
} |
1732 |
if (*p == '!') { |
1733 |
p++; |
1734 |
while (isspace(*p)) p++; |
1735 |
if ((!*p) || (*p == '*')) { |
1736 |
(void)strlcpy(prog, "*", sizeof(prog)); |
1737 |
continue; |
1738 |
} |
1739 |
for (i = 0; i < LINE_MAX - 1; i++) { |
1740 |
if (!isprint(p[i]) || isspace(p[i])) |
1741 |
break; |
1742 |
prog[i] = p[i]; |
1743 |
} |
1744 |
prog[i] = 0; |
1745 |
continue; |
1746 |
} |
1747 |
for (p = cline + 1; *p != '\0'; p++) { |
1748 |
if (*p != '#') |
1749 |
continue; |
1750 |
if (*(p - 1) == '\\') { |
1751 |
strcpy(p - 1, p); |
1752 |
p--; |
1753 |
continue; |
1754 |
} |
1755 |
*p = '\0'; |
1756 |
break; |
1757 |
} |
1758 |
for (i = strlen(cline) - 1; i >= 0 && isspace(cline[i]); i--) |
1759 |
cline[i] = '\0'; |
1760 |
f = (struct filed *)calloc(1, sizeof(*f)); |
1761 |
if (f == NULL) { |
1762 |
logerror("calloc"); |
1763 |
exit(1); |
1764 |
} |
1765 |
*nextp = f; |
1766 |
nextp = &f->f_next; |
1767 |
cfline(cline, f, prog, host); |
1768 |
} |
1769 |
return nextp; |
1770 |
} |
1771 |
|
1772 |
/* |
1773 |
* INIT -- Initialize syslogd from configuration table |
1774 |
*/ |
1775 |
static void |
1776 |
init(int signo) |
1777 |
{ |
1778 |
int i; |
1779 |
FILE *cf; |
1780 |
struct filed *f, *next, **nextp; |
1781 |
char *p; |
1782 |
char oldLocalHostName[MAXHOSTNAMELEN]; |
1783 |
char hostMsg[2*MAXHOSTNAMELEN+40]; |
1784 |
char bootfileMsg[LINE_MAX]; |
1785 |
|
1786 |
dprintf("init\n"); |
1787 |
|
1788 |
/* |
1789 |
* Load hostname (may have changed). |
1790 |
*/ |
1791 |
if (signo != 0) |
1792 |
(void)strlcpy(oldLocalHostName, LocalHostName, |
1793 |
sizeof(oldLocalHostName)); |
1794 |
if (gethostname(LocalHostName, sizeof(LocalHostName))) |
1795 |
err(EX_OSERR, "gethostname() failed"); |
1796 |
if ((p = strchr(LocalHostName, '.')) != NULL) { |
1797 |
*p++ = '\0'; |
1798 |
LocalDomain = p; |
1799 |
} else { |
1800 |
LocalDomain = ""; |
1801 |
} |
1802 |
|
1803 |
/* |
1804 |
* Load / reload timezone data (in case it changed). |
1805 |
* |
1806 |
* Just calling tzset() again does not work, the timezone code |
1807 |
* caches the result. However, by setting the TZ variable, one |
1808 |
* can defeat the caching and have the timezone code really |
1809 |
* reload the timezone data. Respect any initial setting of |
1810 |
* TZ, in case the system is configured specially. |
1811 |
*/ |
1812 |
dprintf("loading timezone data via tzset()\n"); |
1813 |
if (getenv("TZ")) { |
1814 |
tzset(); |
1815 |
} else { |
1816 |
setenv("TZ", ":/etc/localtime", 1); |
1817 |
tzset(); |
1818 |
unsetenv("TZ"); |
1819 |
} |
1820 |
|
1821 |
/* |
1822 |
* Close all open log files. |
1823 |
*/ |
1824 |
Initialized = 0; |
1825 |
for (f = Files; f != NULL; f = next) { |
1826 |
/* flush any pending output */ |
1827 |
if (f->f_prevcount) |
1828 |
fprintlog(f, 0, (char *)NULL); |
1829 |
|
1830 |
switch (f->f_type) { |
1831 |
case F_FILE: |
1832 |
case F_FORW: |
1833 |
case F_CONSOLE: |
1834 |
case F_TTY: |
1835 |
close_filed(f); |
1836 |
break; |
1837 |
case F_PIPE: |
1838 |
close_filed(f); |
1839 |
deadq_enter(f->f_un.f_pipe.f_pid, |
1840 |
f->f_un.f_pipe.f_pname); |
1841 |
break; |
1842 |
} |
1843 |
next = f->f_next; |
1844 |
if (f->f_program) free(f->f_program); |
1845 |
if (f->f_host) free(f->f_host); |
1846 |
free((char *)f); |
1847 |
} |
1848 |
Files = NULL; |
1849 |
nextp = &Files; |
1850 |
|
1851 |
/* open the configuration file */ |
1852 |
if ((cf = fopen(ConfFile, "r")) == NULL) { |
1853 |
dprintf("cannot open %s\n", ConfFile); |
1854 |
*nextp = (struct filed *)calloc(1, sizeof(*f)); |
1855 |
if (*nextp == NULL) { |
1856 |
logerror("calloc"); |
1857 |
exit(1); |
1858 |
} |
1859 |
cfline("*.ERR\t/dev/console", *nextp, "*", "*"); |
1860 |
(*nextp)->f_next = (struct filed *)calloc(1, sizeof(*f)); |
1861 |
if ((*nextp)->f_next == NULL) { |
1862 |
logerror("calloc"); |
1863 |
exit(1); |
1864 |
} |
1865 |
cfline("*.PANIC\t*", (*nextp)->f_next, "*", "*"); |
1866 |
Initialized = 1; |
1867 |
return; |
1868 |
} |
1869 |
|
1870 |
readconfigfile(cf, &Files, 1); |
1871 |
|
1872 |
/* close the configuration file */ |
1873 |
(void)fclose(cf); |
1874 |
|
1875 |
Initialized = 1; |
1876 |
|
1877 |
if (Debug) { |
1878 |
int port; |
1879 |
for (f = Files; f; f = f->f_next) { |
1880 |
for (i = 0; i <= LOG_NFACILITIES; i++) |
1881 |
if (f->f_pmask[i] == INTERNAL_NOPRI) |
1882 |
printf("X "); |
1883 |
else |
1884 |
printf("%d ", f->f_pmask[i]); |
1885 |
printf("%s: ", TypeNames[f->f_type]); |
1886 |
switch (f->f_type) { |
1887 |
case F_FILE: |
1888 |
printf("%s", f->f_un.f_fname); |
1889 |
break; |
1890 |
|
1891 |
case F_CONSOLE: |
1892 |
case F_TTY: |
1893 |
printf("%s%s", _PATH_DEV, f->f_un.f_fname); |
1894 |
break; |
1895 |
|
1896 |
case F_FORW: |
1897 |
port = (int)ntohs(((struct sockaddr_in *) |
1898 |
(f->f_un.f_forw.f_addr->ai_addr))->sin_port); |
1899 |
if (port != 514) { |
1900 |
printf("%s:%d", |
1901 |
f->f_un.f_forw.f_hname, port); |
1902 |
} else { |
1903 |
printf("%s", f->f_un.f_forw.f_hname); |
1904 |
} |
1905 |
break; |
1906 |
|
1907 |
case F_PIPE: |
1908 |
printf("%s", f->f_un.f_pipe.f_pname); |
1909 |
break; |
1910 |
|
1911 |
case F_USERS: |
1912 |
for (i = 0; i < MAXUNAMES && *f->f_un.f_uname[i]; i++) |
1913 |
printf("%s, ", f->f_un.f_uname[i]); |
1914 |
break; |
1915 |
} |
1916 |
if (f->f_program) |
1917 |
printf(" (%s)", f->f_program); |
1918 |
printf("\n"); |
1919 |
} |
1920 |
} |
1921 |
|
1922 |
logmsg(LOG_SYSLOG|LOG_INFO, "syslogd: restart", LocalHostName, ADDDATE); |
1923 |
dprintf("syslogd: restarted\n"); |
1924 |
/* |
1925 |
* Log a change in hostname, but only on a restart. |
1926 |
*/ |
1927 |
if (signo != 0 && strcmp(oldLocalHostName, LocalHostName) != 0) { |
1928 |
(void)snprintf(hostMsg, sizeof(hostMsg), |
1929 |
"syslogd: hostname changed, \"%s\" to \"%s\"", |
1930 |
oldLocalHostName, LocalHostName); |
1931 |
logmsg(LOG_SYSLOG|LOG_INFO, hostMsg, LocalHostName, ADDDATE); |
1932 |
dprintf("%s\n", hostMsg); |
1933 |
} |
1934 |
/* |
1935 |
* Log the kernel boot file if we aren't going to use it as |
1936 |
* the prefix, and if this is *not* a restart. |
1937 |
*/ |
1938 |
if (signo == 0 && !use_bootfile) { |
1939 |
(void)snprintf(bootfileMsg, sizeof(bootfileMsg), |
1940 |
"syslogd: kernel boot file is %s", bootfile); |
1941 |
logmsg(LOG_KERN|LOG_INFO, bootfileMsg, LocalHostName, ADDDATE); |
1942 |
dprintf("%s\n", bootfileMsg); |
1943 |
} |
1944 |
} |
1945 |
|
1946 |
/* |
1947 |
* Crack a configuration file line |
1948 |
*/ |
1949 |
static void |
1950 |
cfline(const char *line, struct filed *f, const char *prog, const char *host) |
1951 |
{ |
1952 |
struct addrinfo hints, *res; |
1953 |
int error, i, pri, syncfile; |
1954 |
const char *p, *q; |
1955 |
char *bp; |
1956 |
char buf[MAXLINE], ebuf[100]; |
1957 |
|
1958 |
dprintf("cfline(\"%s\", f, \"%s\", \"%s\")\n", line, prog, host); |
1959 |
|
1960 |
errno = 0; /* keep strerror() stuff out of logerror messages */ |
1961 |
|
1962 |
/* clear out file entry */ |
1963 |
memset(f, 0, sizeof(*f)); |
1964 |
for (i = 0; i <= LOG_NFACILITIES; i++) |
1965 |
f->f_pmask[i] = INTERNAL_NOPRI; |
1966 |
|
1967 |
/* save hostname if any */ |
1968 |
if (host && *host == '*') |
1969 |
host = NULL; |
1970 |
if (host) { |
1971 |
int hl; |
1972 |
|
1973 |
f->f_host = strdup(host); |
1974 |
if (f->f_host == NULL) { |
1975 |
logerror("strdup"); |
1976 |
exit(1); |
1977 |
} |
1978 |
hl = strlen(f->f_host); |
1979 |
if (hl > 0 && f->f_host[hl-1] == '.') |
1980 |
f->f_host[--hl] = '\0'; |
1981 |
trimdomain(f->f_host, hl); |
1982 |
} |
1983 |
|
1984 |
/* save program name if any */ |
1985 |
if (prog && *prog == '*') |
1986 |
prog = NULL; |
1987 |
if (prog) { |
1988 |
f->f_program = strdup(prog); |
1989 |
if (f->f_program == NULL) { |
1990 |
logerror("strdup"); |
1991 |
exit(1); |
1992 |
} |
1993 |
} |
1994 |
|
1995 |
/* scan through the list of selectors */ |
1996 |
for (p = line; *p && *p != '\t' && *p != ' ';) { |
1997 |
int pri_done; |
1998 |
int pri_cmp; |
1999 |
int pri_invert; |
2000 |
|
2001 |
/* find the end of this facility name list */ |
2002 |
for (q = p; *q && *q != '\t' && *q != ' ' && *q++ != '.'; ) |
2003 |
continue; |
2004 |
|
2005 |
/* get the priority comparison */ |
2006 |
pri_cmp = 0; |
2007 |
pri_done = 0; |
2008 |
pri_invert = 0; |
2009 |
if (*q == '!') { |
2010 |
pri_invert = 1; |
2011 |
q++; |
2012 |
} |
2013 |
while (!pri_done) { |
2014 |
switch (*q) { |
2015 |
case '<': |
2016 |
pri_cmp |= PRI_LT; |
2017 |
q++; |
2018 |
break; |
2019 |
case '=': |
2020 |
pri_cmp |= PRI_EQ; |
2021 |
q++; |
2022 |
break; |
2023 |
case '>': |
2024 |
pri_cmp |= PRI_GT; |
2025 |
q++; |
2026 |
break; |
2027 |
default: |
2028 |
pri_done++; |
2029 |
break; |
2030 |
} |
2031 |
} |
2032 |
|
2033 |
/* collect priority name */ |
2034 |
for (bp = buf; *q && !strchr("\t,; ", *q); ) |
2035 |
*bp++ = *q++; |
2036 |
*bp = '\0'; |
2037 |
|
2038 |
/* skip cruft */ |
2039 |
while (strchr(",;", *q)) |
2040 |
q++; |
2041 |
|
2042 |
/* decode priority name */ |
2043 |
if (*buf == '*') { |
2044 |
pri = LOG_PRIMASK; |
2045 |
pri_cmp = PRI_LT | PRI_EQ | PRI_GT; |
2046 |
} else { |
2047 |
/* Ignore trailing spaces. */ |
2048 |
for (i = strlen(buf) - 1; i >= 0 && buf[i] == ' '; i--) |
2049 |
buf[i] = '\0'; |
2050 |
|
2051 |
pri = decode(buf, prioritynames); |
2052 |
if (pri < 0) { |
2053 |
errno = 0; |
2054 |
(void)snprintf(ebuf, sizeof ebuf, |
2055 |
"unknown priority name \"%s\"", buf); |
2056 |
logerror(ebuf); |
2057 |
return; |
2058 |
} |
2059 |
} |
2060 |
if (!pri_cmp) |
2061 |
pri_cmp = (UniquePriority) |
2062 |
? (PRI_EQ) |
2063 |
: (PRI_EQ | PRI_GT) |
2064 |
; |
2065 |
if (pri_invert) |
2066 |
pri_cmp ^= PRI_LT | PRI_EQ | PRI_GT; |
2067 |
|
2068 |
/* scan facilities */ |
2069 |
while (*p && !strchr("\t.; ", *p)) { |
2070 |
for (bp = buf; *p && !strchr("\t,;. ", *p); ) |
2071 |
*bp++ = *p++; |
2072 |
*bp = '\0'; |
2073 |
|
2074 |
if (*buf == '*') { |
2075 |
for (i = 0; i < LOG_NFACILITIES; i++) { |
2076 |
f->f_pmask[i] = pri; |
2077 |
f->f_pcmp[i] = pri_cmp; |
2078 |
} |
2079 |
} else { |
2080 |
i = decode(buf, facilitynames); |
2081 |
if (i < 0) { |
2082 |
errno = 0; |
2083 |
(void)snprintf(ebuf, sizeof ebuf, |
2084 |
"unknown facility name \"%s\"", |
2085 |
buf); |
2086 |
logerror(ebuf); |
2087 |
return; |
2088 |
} |
2089 |
f->f_pmask[i >> 3] = pri; |
2090 |
f->f_pcmp[i >> 3] = pri_cmp; |
2091 |
} |
2092 |
while (*p == ',' || *p == ' ') |
2093 |
p++; |
2094 |
} |
2095 |
|
2096 |
p = q; |
2097 |
} |
2098 |
|
2099 |
/* skip to action part */ |
2100 |
while (*p == '\t' || *p == ' ') |
2101 |
p++; |
2102 |
|
2103 |
if (*p == '-') { |
2104 |
syncfile = 0; |
2105 |
p++; |
2106 |
} else |
2107 |
syncfile = 1; |
2108 |
|
2109 |
switch (*p) { |
2110 |
case '@': |
2111 |
{ |
2112 |
char *tp; |
2113 |
char endkey = ':'; |
2114 |
/* |
2115 |
* scan forward to see if there is a port defined. |
2116 |
* so we can't use strlcpy.. |
2117 |
*/ |
2118 |
i = sizeof(f->f_un.f_forw.f_hname); |
2119 |
tp = f->f_un.f_forw.f_hname; |
2120 |
p++; |
2121 |
|
2122 |
/* |
2123 |
* an ipv6 address should start with a '[' in that case |
2124 |
* we should scan for a ']' |
2125 |
*/ |
2126 |
if (*p == '[') { |
2127 |
p++; |
2128 |
endkey = ']'; |
2129 |
} |
2130 |
while (*p && (*p != endkey) && (i-- > 0)) { |
2131 |
*tp++ = *p++; |
2132 |
} |
2133 |
if (endkey == ']' && *p == endkey) |
2134 |
p++; |
2135 |
*tp = '\0'; |
2136 |
} |
2137 |
/* See if we copied a domain and have a port */ |
2138 |
if (*p == ':') |
2139 |
p++; |
2140 |
else |
2141 |
p = NULL; |
2142 |
|
2143 |
memset(&hints, 0, sizeof(hints)); |
2144 |
hints.ai_family = family; |
2145 |
hints.ai_socktype = SOCK_DGRAM; |
2146 |
error = getaddrinfo(f->f_un.f_forw.f_hname, |
2147 |
p ? p : "syslog", &hints, &res); |
2148 |
if (error) { |
2149 |
logerror(gai_strerror(error)); |
2150 |
break; |
2151 |
} |
2152 |
f->f_un.f_forw.f_addr = res; |
2153 |
f->f_type = F_FORW; |
2154 |
break; |
2155 |
|
2156 |
case '/': |
2157 |
if ((f->f_file = open(p, logflags, 0600)) < 0) { |
2158 |
f->f_type = F_UNUSED; |
2159 |
logerror(p); |
2160 |
break; |
2161 |
} |
2162 |
if (syncfile) |
2163 |
f->f_flags |= FFLAG_SYNC; |
2164 |
if (isatty(f->f_file)) { |
2165 |
if (strcmp(p, ctty) == 0) |
2166 |
f->f_type = F_CONSOLE; |
2167 |
else |
2168 |
f->f_type = F_TTY; |
2169 |
(void)strlcpy(f->f_un.f_fname, p + sizeof(_PATH_DEV) - 1, |
2170 |
sizeof(f->f_un.f_fname)); |
2171 |
} else { |
2172 |
(void)strlcpy(f->f_un.f_fname, p, sizeof(f->f_un.f_fname)); |
2173 |
f->f_type = F_FILE; |
2174 |
} |
2175 |
break; |
2176 |
|
2177 |
case '|': |
2178 |
f->f_un.f_pipe.f_pid = 0; |
2179 |
(void)strlcpy(f->f_un.f_pipe.f_pname, p + 1, |
2180 |
sizeof(f->f_un.f_pipe.f_pname)); |
2181 |
f->f_type = F_PIPE; |
2182 |
break; |
2183 |
|
2184 |
case '*': |
2185 |
f->f_type = F_WALL; |
2186 |
break; |
2187 |
|
2188 |
default: |
2189 |
for (i = 0; i < MAXUNAMES && *p; i++) { |
2190 |
for (q = p; *q && *q != ','; ) |
2191 |
q++; |
2192 |
(void)strncpy(f->f_un.f_uname[i], p, MAXLOGNAME - 1); |
2193 |
if ((q - p) >= MAXLOGNAME) |
2194 |
f->f_un.f_uname[i][MAXLOGNAME - 1] = '\0'; |
2195 |
else |
2196 |
f->f_un.f_uname[i][q - p] = '\0'; |
2197 |
while (*q == ',' || *q == ' ') |
2198 |
q++; |
2199 |
p = q; |
2200 |
} |
2201 |
f->f_type = F_USERS; |
2202 |
break; |
2203 |
} |
2204 |
} |
2205 |
|
2206 |
|
2207 |
/* |
2208 |
* Decode a symbolic name to a numeric value |
2209 |
*/ |
2210 |
static int |
2211 |
decode(const char *name, const CODE *codetab) |
2212 |
{ |
2213 |
const CODE *c; |
2214 |
char *p, buf[40]; |
2215 |
|
2216 |
if (isdigit(*name)) |
2217 |
return (atoi(name)); |
2218 |
|
2219 |
for (p = buf; *name && p < &buf[sizeof(buf) - 1]; p++, name++) { |
2220 |
if (isupper(*name)) |
2221 |
*p = tolower(*name); |
2222 |
else |
2223 |
*p = *name; |
2224 |
} |
2225 |
*p = '\0'; |
2226 |
for (c = codetab; c->c_name; c++) |
2227 |
if (!strcmp(buf, c->c_name)) |
2228 |
return (c->c_val); |
2229 |
|
2230 |
return (-1); |
2231 |
} |
2232 |
|
2233 |
static void |
2234 |
markit(void) |
2235 |
{ |
2236 |
struct filed *f; |
2237 |
dq_t q, next; |
2238 |
|
2239 |
now = time((time_t *)NULL); |
2240 |
MarkSeq += TIMERINTVL; |
2241 |
if (MarkSeq >= MarkInterval) { |
2242 |
logmsg(LOG_INFO, "-- MARK --", |
2243 |
LocalHostName, ADDDATE|MARK); |
2244 |
MarkSeq = 0; |
2245 |
} |
2246 |
|
2247 |
for (f = Files; f; f = f->f_next) { |
2248 |
if (f->f_prevcount && now >= REPEATTIME(f)) { |
2249 |
dprintf("flush %s: repeated %d times, %d sec.\n", |
2250 |
TypeNames[f->f_type], f->f_prevcount, |
2251 |
repeatinterval[f->f_repeatcount]); |
2252 |
fprintlog(f, 0, (char *)NULL); |
2253 |
BACKOFF(f); |
2254 |
} |
2255 |
} |
2256 |
|
2257 |
/* Walk the dead queue, and see if we should signal somebody. */ |
2258 |
for (q = TAILQ_FIRST(&deadq_head); q != NULL; q = next) { |
2259 |
next = TAILQ_NEXT(q, dq_entries); |
2260 |
|
2261 |
switch (q->dq_timeout) { |
2262 |
case 0: |
2263 |
/* Already signalled once, try harder now. */ |
2264 |
if (kill(q->dq_pid, SIGKILL) != 0) |
2265 |
(void)deadq_remove(q->dq_pid); |
2266 |
break; |
2267 |
|
2268 |
case 1: |
2269 |
/* |
2270 |
* Timed out on dead queue, send terminate |
2271 |
* signal. Note that we leave the removal |
2272 |
* from the dead queue to reapchild(), which |
2273 |
* will also log the event (unless the process |
2274 |
* didn't even really exist, in case we simply |
2275 |
* drop it from the dead queue). |
2276 |
*/ |
2277 |
if (kill(q->dq_pid, SIGTERM) != 0) |
2278 |
(void)deadq_remove(q->dq_pid); |
2279 |
/* FALLTHROUGH */ |
2280 |
|
2281 |
default: |
2282 |
q->dq_timeout--; |
2283 |
} |
2284 |
} |
2285 |
MarkSet = 0; |
2286 |
(void)alarm(TIMERINTVL); |
2287 |
} |
2288 |
|
2289 |
/* |
2290 |
* fork off and become a daemon, but wait for the child to come online |
2291 |
* before returning to the parent, or we get disk thrashing at boot etc. |
2292 |
* Set a timer so we don't hang forever if it wedges. |
2293 |
*/ |
2294 |
static int |
2295 |
waitdaemon(int nochdir, int noclose, int maxwait) |
2296 |
{ |
2297 |
int fd; |
2298 |
int status; |
2299 |
pid_t pid, childpid; |
2300 |
|
2301 |
switch (childpid = fork()) { |
2302 |
case -1: |
2303 |
return (-1); |
2304 |
case 0: |
2305 |
break; |
2306 |
default: |
2307 |
signal(SIGALRM, timedout); |
2308 |
alarm(maxwait); |
2309 |
while ((pid = wait3(&status, 0, NULL)) != -1) { |
2310 |
if (WIFEXITED(status)) |
2311 |
errx(1, "child pid %d exited with return code %d", |
2312 |
pid, WEXITSTATUS(status)); |
2313 |
if (WIFSIGNALED(status)) |
2314 |
errx(1, "child pid %d exited on signal %d%s", |
2315 |
pid, WTERMSIG(status), |
2316 |
WCOREDUMP(status) ? " (core dumped)" : |
2317 |
""); |
2318 |
if (pid == childpid) /* it's gone... */ |
2319 |
break; |
2320 |
} |
2321 |
exit(0); |
2322 |
} |
2323 |
|
2324 |
if (setsid() == -1) |
2325 |
return (-1); |
2326 |
|
2327 |
if (!nochdir) |
2328 |
(void)chdir("/"); |
2329 |
|
2330 |
if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { |
2331 |
(void)dup2(fd, STDIN_FILENO); |
2332 |
(void)dup2(fd, STDOUT_FILENO); |
2333 |
(void)dup2(fd, STDERR_FILENO); |
2334 |
if (fd > 2) |
2335 |
(void)close (fd); |
2336 |
} |
2337 |
return (getppid()); |
2338 |
} |
2339 |
|
2340 |
/* |
2341 |
* We get a SIGALRM from the child when it's running and finished doing it's |
2342 |
* fsync()'s or O_SYNC writes for all the boot messages. |
2343 |
* |
2344 |
* We also get a signal from the kernel if the timer expires, so check to |
2345 |
* see what happened. |
2346 |
*/ |
2347 |
static void |
2348 |
timedout(int sig __unused) |
2349 |
{ |
2350 |
int left; |
2351 |
left = alarm(0); |
2352 |
signal(SIGALRM, SIG_DFL); |
2353 |
if (left == 0) |
2354 |
errx(1, "timed out waiting for child"); |
2355 |
else |
2356 |
_exit(0); |
2357 |
} |
2358 |
|
2359 |
/* |
2360 |
* Add `s' to the list of allowable peer addresses to accept messages |
2361 |
* from. |
2362 |
* |
2363 |
* `s' is a string in the form: |
2364 |
* |
2365 |
* [*]domainname[:{servicename|portnumber|*}] |
2366 |
* |
2367 |
* or |
2368 |
* |
2369 |
* netaddr/maskbits[:{servicename|portnumber|*}] |
2370 |
* |
2371 |
* Returns -1 on error, 0 if the argument was valid. |
2372 |
*/ |
2373 |
static int |
2374 |
allowaddr(char *s) |
2375 |
{ |
2376 |
char *cp1, *cp2; |
2377 |
struct allowedpeer ap; |
2378 |
struct servent *se; |
2379 |
int masklen = -1; |
2380 |
struct addrinfo hints, *res; |
2381 |
struct in_addr *addrp, *maskp; |
2382 |
#ifdef INET6 |
2383 |
int i; |
2384 |
u_int32_t *addr6p, *mask6p; |
2385 |
#endif |
2386 |
char ip[NI_MAXHOST]; |
2387 |
|
2388 |
#ifdef INET6 |
2389 |
if (*s != '[' || (cp1 = strchr(s + 1, ']')) == NULL) |
2390 |
#endif |
2391 |
cp1 = s; |
2392 |
if ((cp1 = strrchr(cp1, ':'))) { |
2393 |
/* service/port provided */ |
2394 |
*cp1++ = '\0'; |
2395 |
if (strlen(cp1) == 1 && *cp1 == '*') |
2396 |
/* any port allowed */ |
2397 |
ap.port = 0; |
2398 |
else if ((se = getservbyname(cp1, "udp"))) { |
2399 |
ap.port = ntohs(se->s_port); |
2400 |
} else { |
2401 |
ap.port = strtol(cp1, &cp2, 0); |
2402 |
if (*cp2 != '\0') |
2403 |
return (-1); /* port not numeric */ |
2404 |
} |
2405 |
} else { |
2406 |
if ((se = getservbyname("syslog", "udp"))) |
2407 |
ap.port = ntohs(se->s_port); |
2408 |
else |
2409 |
/* sanity, should not happen */ |
2410 |
ap.port = 514; |
2411 |
} |
2412 |
|
2413 |
if ((cp1 = strchr(s, '/')) != NULL && |
2414 |
strspn(cp1 + 1, "0123456789") == strlen(cp1 + 1)) { |
2415 |
*cp1 = '\0'; |
2416 |
if ((masklen = atoi(cp1 + 1)) < 0) |
2417 |
return (-1); |
2418 |
} |
2419 |
#ifdef INET6 |
2420 |
if (*s == '[') { |
2421 |
cp2 = s + strlen(s) - 1; |
2422 |
if (*cp2 == ']') { |
2423 |
++s; |
2424 |
*cp2 = '\0'; |
2425 |
} else { |
2426 |
cp2 = NULL; |
2427 |
} |
2428 |
} else { |
2429 |
cp2 = NULL; |
2430 |
} |
2431 |
#endif |
2432 |
memset(&hints, 0, sizeof(hints)); |
2433 |
hints.ai_family = PF_UNSPEC; |
2434 |
hints.ai_socktype = SOCK_DGRAM; |
2435 |
hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; |
2436 |
if (getaddrinfo(s, NULL, &hints, &res) == 0) { |
2437 |
ap.isnumeric = 1; |
2438 |
memcpy(&ap.a_addr, res->ai_addr, res->ai_addrlen); |
2439 |
memset(&ap.a_mask, 0, sizeof(ap.a_mask)); |
2440 |
ap.a_mask.ss_family = res->ai_family; |
2441 |
if (res->ai_family == AF_INET) { |
2442 |
ap.a_mask.ss_len = sizeof(struct sockaddr_in); |
2443 |
maskp = &((struct sockaddr_in *)&ap.a_mask)->sin_addr; |
2444 |
addrp = &((struct sockaddr_in *)&ap.a_addr)->sin_addr; |
2445 |
if (masklen < 0) { |
2446 |
/* use default netmask */ |
2447 |
if (IN_CLASSA(ntohl(addrp->s_addr))) |
2448 |
maskp->s_addr = htonl(IN_CLASSA_NET); |
2449 |
else if (IN_CLASSB(ntohl(addrp->s_addr))) |
2450 |
maskp->s_addr = htonl(IN_CLASSB_NET); |
2451 |
else |
2452 |
maskp->s_addr = htonl(IN_CLASSC_NET); |
2453 |
} else if (masklen <= 32) { |
2454 |
/* convert masklen to netmask */ |
2455 |
if (masklen == 0) |
2456 |
maskp->s_addr = 0; |
2457 |
else |
2458 |
maskp->s_addr = htonl(~((1 << (32 - masklen)) - 1)); |
2459 |
} else { |
2460 |
freeaddrinfo(res); |
2461 |
return (-1); |
2462 |
} |
2463 |
/* Lose any host bits in the network number. */ |
2464 |
addrp->s_addr &= maskp->s_addr; |
2465 |
} |
2466 |
#ifdef INET6 |
2467 |
else if (res->ai_family == AF_INET6 && masklen <= 128) { |
2468 |
ap.a_mask.ss_len = sizeof(struct sockaddr_in6); |
2469 |
if (masklen < 0) |
2470 |
masklen = 128; |
2471 |
mask6p = (u_int32_t *)&((struct sockaddr_in6 *)&ap.a_mask)->sin6_addr; |
2472 |
/* convert masklen to netmask */ |
2473 |
while (masklen > 0) { |
2474 |
if (masklen < 32) { |
2475 |
*mask6p = htonl(~(0xffffffff >> masklen)); |
2476 |
break; |
2477 |
} |
2478 |
*mask6p++ = 0xffffffff; |
2479 |
masklen -= 32; |
2480 |
} |
2481 |
/* Lose any host bits in the network number. */ |
2482 |
mask6p = (u_int32_t *)&((struct sockaddr_in6 *)&ap.a_mask)->sin6_addr; |
2483 |
addr6p = (u_int32_t *)&((struct sockaddr_in6 *)&ap.a_addr)->sin6_addr; |
2484 |
for (i = 0; i < 4; i++) |
2485 |
addr6p[i] &= mask6p[i]; |
2486 |
} |
2487 |
#endif |
2488 |
else { |
2489 |
freeaddrinfo(res); |
2490 |
return (-1); |
2491 |
} |
2492 |
freeaddrinfo(res); |
2493 |
} else { |
2494 |
/* arg `s' is domain name */ |
2495 |
ap.isnumeric = 0; |
2496 |
ap.a_name = s; |
2497 |
if (cp1) |
2498 |
*cp1 = '/'; |
2499 |
#ifdef INET6 |
2500 |
if (cp2) { |
2501 |
*cp2 = ']'; |
2502 |
--s; |
2503 |
} |
2504 |
#endif |
2505 |
} |
2506 |
|
2507 |
if (Debug) { |
2508 |
printf("allowaddr: rule %d: ", NumAllowed); |
2509 |
if (ap.isnumeric) { |
2510 |
printf("numeric, "); |
2511 |
getnameinfo((struct sockaddr *)&ap.a_addr, |
2512 |
((struct sockaddr *)&ap.a_addr)->sa_len, |
2513 |
ip, sizeof ip, NULL, 0, NI_NUMERICHOST); |
2514 |
printf("addr = %s, ", ip); |
2515 |
getnameinfo((struct sockaddr *)&ap.a_mask, |
2516 |
((struct sockaddr *)&ap.a_mask)->sa_len, |
2517 |
ip, sizeof ip, NULL, 0, NI_NUMERICHOST); |
2518 |
printf("mask = %s; ", ip); |
2519 |
} else { |
2520 |
printf("domainname = %s; ", ap.a_name); |
2521 |
} |
2522 |
printf("port = %d\n", ap.port); |
2523 |
} |
2524 |
|
2525 |
if ((AllowedPeers = realloc(AllowedPeers, |
2526 |
++NumAllowed * sizeof(struct allowedpeer))) |
2527 |
== NULL) { |
2528 |
logerror("realloc"); |
2529 |
exit(1); |
2530 |
} |
2531 |
memcpy(&AllowedPeers[NumAllowed - 1], &ap, sizeof(struct allowedpeer)); |
2532 |
return (0); |
2533 |
} |
2534 |
|
2535 |
/* |
2536 |
* Validate that the remote peer has permission to log to us. |
2537 |
*/ |
2538 |
static int |
2539 |
validate(struct sockaddr *sa, const char *hname) |
2540 |
{ |
2541 |
int i; |
2542 |
size_t l1, l2; |
2543 |
char *cp, name[NI_MAXHOST], ip[NI_MAXHOST], port[NI_MAXSERV]; |
2544 |
struct allowedpeer *ap; |
2545 |
struct sockaddr_in *sin4, *a4p = NULL, *m4p = NULL; |
2546 |
#ifdef INET6 |
2547 |
int j, reject; |
2548 |
struct sockaddr_in6 *sin6, *a6p = NULL, *m6p = NULL; |
2549 |
#endif |
2550 |
struct addrinfo hints, *res; |
2551 |
u_short sport; |
2552 |
|
2553 |
if (NumAllowed == 0) |
2554 |
/* traditional behaviour, allow everything */ |
2555 |
return (1); |
2556 |
|
2557 |
(void)strlcpy(name, hname, sizeof(name)); |
2558 |
memset(&hints, 0, sizeof(hints)); |
2559 |
hints.ai_family = PF_UNSPEC; |
2560 |
hints.ai_socktype = SOCK_DGRAM; |
2561 |
hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; |
2562 |
if (getaddrinfo(name, NULL, &hints, &res) == 0) |
2563 |
freeaddrinfo(res); |
2564 |
else if (strchr(name, '.') == NULL) { |
2565 |
strlcat(name, ".", sizeof name); |
2566 |
strlcat(name, LocalDomain, sizeof name); |
2567 |
} |
2568 |
if (getnameinfo(sa, sa->sa_len, ip, sizeof ip, port, sizeof port, |
2569 |
NI_NUMERICHOST | NI_NUMERICSERV) != 0) |
2570 |
return (0); /* for safety, should not occur */ |
2571 |
dprintf("validate: dgram from IP %s, port %s, name %s;\n", |
2572 |
ip, port, name); |
2573 |
sport = atoi(port); |
2574 |
|
2575 |
/* now, walk down the list */ |
2576 |
for (i = 0, ap = AllowedPeers; i < NumAllowed; i++, ap++) { |
2577 |
if (ap->port != 0 && ap->port != sport) { |
2578 |
dprintf("rejected in rule %d due to port mismatch.\n", i); |
2579 |
continue; |
2580 |
} |
2581 |
|
2582 |
if (ap->isnumeric) { |
2583 |
if (ap->a_addr.ss_family != sa->sa_family) { |
2584 |
dprintf("rejected in rule %d due to address family mismatch.\n", i); |
2585 |
continue; |
2586 |
} |
2587 |
if (ap->a_addr.ss_family == AF_INET) { |
2588 |
sin4 = (struct sockaddr_in *)sa; |
2589 |
a4p = (struct sockaddr_in *)&ap->a_addr; |
2590 |
m4p = (struct sockaddr_in *)&ap->a_mask; |
2591 |
if ((sin4->sin_addr.s_addr & m4p->sin_addr.s_addr) |
2592 |
!= a4p->sin_addr.s_addr) { |
2593 |
dprintf("rejected in rule %d due to IP mismatch.\n", i); |
2594 |
continue; |
2595 |
} |
2596 |
} |
2597 |
#ifdef INET6 |
2598 |
else if (ap->a_addr.ss_family == AF_INET6) { |
2599 |
sin6 = (struct sockaddr_in6 *)sa; |
2600 |
a6p = (struct sockaddr_in6 *)&ap->a_addr; |
2601 |
m6p = (struct sockaddr_in6 *)&ap->a_mask; |
2602 |
if (a6p->sin6_scope_id != 0 && |
2603 |
sin6->sin6_scope_id != a6p->sin6_scope_id) { |
2604 |
dprintf("rejected in rule %d due to scope mismatch.\n", i); |
2605 |
continue; |
2606 |
} |
2607 |
reject = 0; |
2608 |
for (j = 0; j < 16; j += 4) { |
2609 |
if ((*(u_int32_t *)&sin6->sin6_addr.s6_addr[j] & *(u_int32_t *)&m6p->sin6_addr.s6_addr[j]) |
2610 |
!= *(u_int32_t *)&a6p->sin6_addr.s6_addr[j]) { |
2611 |
++reject; |
2612 |
break; |
2613 |
} |
2614 |
} |
2615 |
if (reject) { |
2616 |
dprintf("rejected in rule %d due to IP mismatch.\n", i); |
2617 |
continue; |
2618 |
} |
2619 |
} |
2620 |
#endif |
2621 |
else |
2622 |
continue; |
2623 |
} else { |
2624 |
cp = ap->a_name; |
2625 |
l1 = strlen(name); |
2626 |
if (*cp == '*') { |
2627 |
/* allow wildmatch */ |
2628 |
cp++; |
2629 |
l2 = strlen(cp); |
2630 |
if (l2 > l1 || memcmp(cp, &name[l1 - l2], l2) != 0) { |
2631 |
dprintf("rejected in rule %d due to name mismatch.\n", i); |
2632 |
continue; |
2633 |
} |
2634 |
} else { |
2635 |
/* exact match */ |
2636 |
l2 = strlen(cp); |
2637 |
if (l2 != l1 || memcmp(cp, name, l1) != 0) { |
2638 |
dprintf("rejected in rule %d due to name mismatch.\n", i); |
2639 |
continue; |
2640 |
} |
2641 |
} |
2642 |
} |
2643 |
dprintf("accepted in rule %d.\n", i); |
2644 |
return (1); /* hooray! */ |
2645 |
} |
2646 |
return (0); |
2647 |
} |
2648 |
|
2649 |
/* |
2650 |
* Fairly similar to popen(3), but returns an open descriptor, as |
2651 |
* opposed to a FILE *. |
2652 |
*/ |
2653 |
static int |
2654 |
p_open(const char *prog, pid_t *rpid) |
2655 |
{ |
2656 |
int pfd[2], nulldesc; |
2657 |
pid_t pid; |
2658 |
sigset_t omask, mask; |
2659 |
char *argv[4]; /* sh -c cmd NULL */ |
2660 |
char errmsg[200]; |
2661 |
|
2662 |
if (pipe(pfd) == -1) |
2663 |
return (-1); |
2664 |
if ((nulldesc = open(_PATH_DEVNULL, O_RDWR)) == -1) |
2665 |
/* we are royally screwed anyway */ |
2666 |
return (-1); |
2667 |
|
2668 |
sigemptyset(&mask); |
2669 |
sigaddset(&mask, SIGALRM); |
2670 |
sigaddset(&mask, SIGHUP); |
2671 |
sigprocmask(SIG_BLOCK, &mask, &omask); |
2672 |
switch ((pid = fork())) { |
2673 |
case -1: |
2674 |
sigprocmask(SIG_SETMASK, &omask, 0); |
2675 |
close(nulldesc); |
2676 |
return (-1); |
2677 |
|
2678 |
case 0: |
2679 |
argv[0] = strdup("sh"); |
2680 |
argv[1] = strdup("-c"); |
2681 |
argv[2] = strdup(prog); |
2682 |
argv[3] = NULL; |
2683 |
if (argv[0] == NULL || argv[1] == NULL || argv[2] == NULL) { |
2684 |
logerror("strdup"); |
2685 |
exit(1); |
2686 |
} |
2687 |
|
2688 |
alarm(0); |
2689 |
(void)setsid(); /* Avoid catching SIGHUPs. */ |
2690 |
|
2691 |
/* |
2692 |
* Throw away pending signals, and reset signal |
2693 |
* behaviour to standard values. |
2694 |
*/ |
2695 |
signal(SIGALRM, SIG_IGN); |
2696 |
signal(SIGHUP, SIG_IGN); |
2697 |
sigprocmask(SIG_SETMASK, &omask, 0); |
2698 |
signal(SIGPIPE, SIG_DFL); |
2699 |
signal(SIGQUIT, SIG_DFL); |
2700 |
signal(SIGALRM, SIG_DFL); |
2701 |
signal(SIGHUP, SIG_DFL); |
2702 |
|
2703 |
dup2(pfd[0], STDIN_FILENO); |
2704 |
dup2(nulldesc, STDOUT_FILENO); |
2705 |
dup2(nulldesc, STDERR_FILENO); |
2706 |
closefrom(3); |
2707 |
|
2708 |
(void)execvp(_PATH_BSHELL, argv); |
2709 |
_exit(255); |
2710 |
} |
2711 |
|
2712 |
sigprocmask(SIG_SETMASK, &omask, 0); |
2713 |
close(nulldesc); |
2714 |
close(pfd[0]); |
2715 |
/* |
2716 |
* Avoid blocking on a hung pipe. With O_NONBLOCK, we are |
2717 |
* supposed to get an EWOULDBLOCK on writev(2), which is |
2718 |
* caught by the logic above anyway, which will in turn close |
2719 |
* the pipe, and fork a new logging subprocess if necessary. |
2720 |
* The stale subprocess will be killed some time later unless |
2721 |
* it terminated itself due to closing its input pipe (so we |
2722 |
* get rid of really dead puppies). |
2723 |
*/ |
2724 |
if (fcntl(pfd[1], F_SETFL, O_NONBLOCK) == -1) { |
2725 |
/* This is bad. */ |
2726 |
(void)snprintf(errmsg, sizeof errmsg, |
2727 |
"Warning: cannot change pipe to PID %d to " |
2728 |
"non-blocking behaviour.", |
2729 |
(int)pid); |
2730 |
logerror(errmsg); |
2731 |
} |
2732 |
*rpid = pid; |
2733 |
return (pfd[1]); |
2734 |
} |
2735 |
|
2736 |
static void |
2737 |
deadq_enter(pid_t pid, const char *name) |
2738 |
{ |
2739 |
dq_t p; |
2740 |
int status; |
2741 |
|
2742 |
if (pid == 0) |
2743 |
return; |
2744 |
/* |
2745 |
* Be paranoid, if we can't signal the process, don't enter it |
2746 |
* into the dead queue (perhaps it's already dead). If possible, |
2747 |
* we try to fetch and log the child's status. |
2748 |
*/ |
2749 |
if (kill(pid, 0) != 0) { |
2750 |
if (waitpid(pid, &status, WNOHANG) > 0) |
2751 |
log_deadchild(pid, status, name); |
2752 |
return; |
2753 |
} |
2754 |
|
2755 |
p = malloc(sizeof(struct deadq_entry)); |
2756 |
if (p == NULL) { |
2757 |
logerror("malloc"); |
2758 |
exit(1); |
2759 |
} |
2760 |
|
2761 |
p->dq_pid = pid; |
2762 |
p->dq_timeout = DQ_TIMO_INIT; |
2763 |
TAILQ_INSERT_TAIL(&deadq_head, p, dq_entries); |
2764 |
} |
2765 |
|
2766 |
static int |
2767 |
deadq_remove(pid_t pid) |
2768 |
{ |
2769 |
dq_t q; |
2770 |
|
2771 |
TAILQ_FOREACH(q, &deadq_head, dq_entries) { |
2772 |
if (q->dq_pid == pid) { |
2773 |
TAILQ_REMOVE(&deadq_head, q, dq_entries); |
2774 |
free(q); |
2775 |
return (1); |
2776 |
} |
2777 |
} |
2778 |
|
2779 |
return (0); |
2780 |
} |
2781 |
|
2782 |
static void |
2783 |
log_deadchild(pid_t pid, int status, const char *name) |
2784 |
{ |
2785 |
int code; |
2786 |
char buf[256]; |
2787 |
const char *reason; |
2788 |
|
2789 |
errno = 0; /* Keep strerror() stuff out of logerror messages. */ |
2790 |
if (WIFSIGNALED(status)) { |
2791 |
reason = "due to signal"; |
2792 |
code = WTERMSIG(status); |
2793 |
} else { |
2794 |
reason = "with status"; |
2795 |
code = WEXITSTATUS(status); |
2796 |
if (code == 0) |
2797 |
return; |
2798 |
} |
2799 |
(void)snprintf(buf, sizeof buf, |
2800 |
"Logging subprocess %d (%s) exited %s %d.", |
2801 |
pid, name, reason, code); |
2802 |
logerror(buf); |
2803 |
} |
2804 |
|
2805 |
static int * |
2806 |
socksetup(int af, char *bindhostname) |
2807 |
{ |
2808 |
struct addrinfo hints, *res, *r; |
2809 |
const char *bindservice; |
2810 |
char *cp; |
2811 |
int error, maxs, *s, *socks; |
2812 |
|
2813 |
/* |
2814 |
* We have to handle this case for backwards compatibility: |
2815 |
* If there are two (or more) colons but no '[' and ']', |
2816 |
* assume this is an inet6 address without a service. |
2817 |
*/ |
2818 |
bindservice = "syslog"; |
2819 |
if (bindhostname != NULL) { |
2820 |
#ifdef INET6 |
2821 |
if (*bindhostname == '[' && |
2822 |
(cp = strchr(bindhostname + 1, ']')) != NULL) { |
2823 |
++bindhostname; |
2824 |
*cp = '\0'; |
2825 |
if (cp[1] == ':' && cp[2] != '\0') |
2826 |
bindservice = cp + 2; |
2827 |
} else { |
2828 |
#endif |
2829 |
cp = strchr(bindhostname, ':'); |
2830 |
if (cp != NULL && strchr(cp + 1, ':') == NULL) { |
2831 |
*cp = '\0'; |
2832 |
if (cp[1] != '\0') |
2833 |
bindservice = cp + 1; |
2834 |
if (cp == bindhostname) |
2835 |
bindhostname = NULL; |
2836 |
} |
2837 |
#ifdef INET6 |
2838 |
} |
2839 |
#endif |
2840 |
} |
2841 |
|
2842 |
memset(&hints, 0, sizeof(hints)); |
2843 |
hints.ai_flags = AI_PASSIVE; |
2844 |
hints.ai_family = af; |
2845 |
hints.ai_socktype = SOCK_DGRAM; |
2846 |
error = getaddrinfo(bindhostname, bindservice, &hints, &res); |
2847 |
if (error) { |
2848 |
logerror(gai_strerror(error)); |
2849 |
errno = 0; |
2850 |
die(0); |
2851 |
} |
2852 |
|
2853 |
/* Count max number of sockets we may open */ |
2854 |
for (maxs = 0, r = res; r; r = r->ai_next, maxs++); |
2855 |
socks = malloc((maxs+1) * sizeof(int)); |
2856 |
if (socks == NULL) { |
2857 |
logerror("couldn't allocate memory for sockets"); |
2858 |
die(0); |
2859 |
} |
2860 |
|
2861 |
*socks = 0; /* num of sockets counter at start of array */ |
2862 |
s = socks + 1; |
2863 |
for (r = res; r; r = r->ai_next) { |
2864 |
int on = 1; |
2865 |
*s = socket(r->ai_family, r->ai_socktype, r->ai_protocol); |
2866 |
if (*s < 0) { |
2867 |
logerror("socket"); |
2868 |
continue; |
2869 |
} |
2870 |
#ifdef INET6 |
2871 |
if (r->ai_family == AF_INET6) { |
2872 |
if (setsockopt(*s, IPPROTO_IPV6, IPV6_V6ONLY, |
2873 |
(char *)&on, sizeof (on)) < 0) { |
2874 |
logerror("setsockopt"); |
2875 |
close(*s); |
2876 |
continue; |
2877 |
} |
2878 |
} |
2879 |
#endif |
2880 |
if (setsockopt(*s, SOL_SOCKET, SO_REUSEADDR, |
2881 |
(char *)&on, sizeof (on)) < 0) { |
2882 |
logerror("setsockopt"); |
2883 |
close(*s); |
2884 |
continue; |
2885 |
} |
2886 |
/* |
2887 |
* RFC 3164 recommends that client side message |
2888 |
* should come from the privileged syslogd port. |
2889 |
* |
2890 |
* If the system administrator choose not to obey |
2891 |
* this, we can skip the bind() step so that the |
2892 |
* system will choose a port for us. |
2893 |
*/ |
2894 |
if (!NoBind) { |
2895 |
if (bind(*s, r->ai_addr, r->ai_addrlen) < 0) { |
2896 |
logerror("bind"); |
2897 |
close(*s); |
2898 |
continue; |
2899 |
} |
2900 |
|
2901 |
if (!SecureMode) |
2902 |
increase_rcvbuf(*s); |
2903 |
} |
2904 |
|
2905 |
(*socks)++; |
2906 |
dprintf("socksetup: new socket fd is %d\n", *s); |
2907 |
s++; |
2908 |
} |
2909 |
|
2910 |
if (*socks == 0) { |
2911 |
free(socks); |
2912 |
if (Debug) |
2913 |
return (NULL); |
2914 |
else |
2915 |
die(0); |
2916 |
} |
2917 |
if (res) |
2918 |
freeaddrinfo(res); |
2919 |
|
2920 |
return (socks); |
2921 |
} |
2922 |
|
2923 |
static void |
2924 |
increase_rcvbuf(int fd) |
2925 |
{ |
2926 |
socklen_t len, slen; |
2927 |
|
2928 |
slen = sizeof(len); |
2929 |
|
2930 |
if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &len, &slen) == 0) { |
2931 |
if (len < RCVBUF_MINSIZE) { |
2932 |
len = RCVBUF_MINSIZE; |
2933 |
setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len)); |
2934 |
} |
2935 |
} |
2936 |
} |