syslogd.c revision 1.56 1 1.56 itojun /* $NetBSD: syslogd.c,v 1.56 2002/09/24 13:53:54 itojun Exp $ */
2 1.32 ad
3 1.1 cgd /*
4 1.5 perry * Copyright (c) 1983, 1988, 1993, 1994
5 1.5 perry * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.11 christos #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.11 christos __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993, 1994\n\
39 1.11 christos The Regents of the University of California. All rights reserved.\n");
40 1.1 cgd #endif /* not lint */
41 1.1 cgd
42 1.1 cgd #ifndef lint
43 1.11 christos #if 0
44 1.11 christos static char sccsid[] = "@(#)syslogd.c 8.3 (Berkeley) 4/4/94";
45 1.11 christos #else
46 1.56 itojun __RCSID("$NetBSD: syslogd.c,v 1.56 2002/09/24 13:53:54 itojun Exp $");
47 1.11 christos #endif
48 1.1 cgd #endif /* not lint */
49 1.1 cgd
50 1.1 cgd /*
51 1.1 cgd * syslogd -- log system messages
52 1.1 cgd *
53 1.1 cgd * This program implements a system log. It takes a series of lines.
54 1.1 cgd * Each line may have a priority, signified as "<n>" as
55 1.1 cgd * the first characters of the line. If this is
56 1.1 cgd * not present, a default priority is used.
57 1.1 cgd *
58 1.1 cgd * To kill syslogd, send a signal 15 (terminate). A signal 1 (hup) will
59 1.1 cgd * cause it to reread its configuration file.
60 1.1 cgd *
61 1.1 cgd * Defined Constants:
62 1.1 cgd *
63 1.1 cgd * MAXLINE -- the maximimum line length that can be handled.
64 1.1 cgd * DEFUPRI -- the default priority for user messages
65 1.1 cgd * DEFSPRI -- the default priority for kernel messages
66 1.1 cgd *
67 1.1 cgd * Author: Eric Allman
68 1.1 cgd * extensive changes by Ralph Campbell
69 1.1 cgd * more extensive changes by Eric Allman (again)
70 1.1 cgd */
71 1.1 cgd
72 1.1 cgd #define MAXLINE 1024 /* maximum line length */
73 1.1 cgd #define MAXSVLINE 120 /* maximum saved line length */
74 1.1 cgd #define DEFUPRI (LOG_USER|LOG_NOTICE)
75 1.1 cgd #define DEFSPRI (LOG_KERN|LOG_CRIT)
76 1.1 cgd #define TIMERINTVL 30 /* interval for checking flush, mark */
77 1.5 perry #define TTYMSGTIME 1 /* timeout passed to ttymsg */
78 1.1 cgd
79 1.1 cgd #include <sys/param.h>
80 1.1 cgd #include <sys/socket.h>
81 1.53 wiz #include <sys/sysctl.h>
82 1.53 wiz #include <sys/types.h>
83 1.1 cgd #include <sys/un.h>
84 1.53 wiz #include <sys/wait.h>
85 1.1 cgd
86 1.56 itojun #include <netinet/in.h>
87 1.56 itojun
88 1.5 perry #include <ctype.h>
89 1.5 perry #include <errno.h>
90 1.5 perry #include <fcntl.h>
91 1.53 wiz #include <grp.h>
92 1.41 tron #include <locale.h>
93 1.53 wiz #include <netdb.h>
94 1.53 wiz #include <poll.h>
95 1.53 wiz #include <pwd.h>
96 1.5 perry #include <signal.h>
97 1.53 wiz #include <stdarg.h>
98 1.1 cgd #include <stdio.h>
99 1.5 perry #include <stdlib.h>
100 1.1 cgd #include <string.h>
101 1.1 cgd #include <unistd.h>
102 1.53 wiz #include <util.h>
103 1.53 wiz
104 1.55 christos #include "utmpentry.h"
105 1.1 cgd #include "pathnames.h"
106 1.1 cgd
107 1.1 cgd #define SYSLOG_NAMES
108 1.1 cgd #include <sys/syslog.h>
109 1.1 cgd
110 1.46 itojun #ifdef LIBWRAP
111 1.46 itojun #include <tcpd.h>
112 1.46 itojun
113 1.46 itojun int allow_severity = LOG_AUTH|LOG_INFO;
114 1.46 itojun int deny_severity = LOG_AUTH|LOG_WARNING;
115 1.46 itojun #endif
116 1.46 itojun
117 1.1 cgd char *ConfFile = _PATH_LOGCONF;
118 1.1 cgd char ctty[] = _PATH_CONSOLE;
119 1.1 cgd
120 1.1 cgd #define FDMASK(fd) (1 << (fd))
121 1.1 cgd
122 1.1 cgd #define dprintf if (Debug) printf
123 1.1 cgd
124 1.1 cgd #define MAXUNAMES 20 /* maximum number of user names */
125 1.1 cgd
126 1.1 cgd /*
127 1.1 cgd * Flags to logmsg().
128 1.1 cgd */
129 1.1 cgd
130 1.1 cgd #define IGN_CONS 0x001 /* don't print on console */
131 1.1 cgd #define SYNC_FILE 0x002 /* do fsync on file after printing */
132 1.1 cgd #define ADDDATE 0x004 /* add a date to the message */
133 1.1 cgd #define MARK 0x008 /* this message is a mark */
134 1.1 cgd
135 1.1 cgd /*
136 1.1 cgd * This structure represents the files that will have log
137 1.1 cgd * copies printed.
138 1.1 cgd */
139 1.1 cgd
140 1.1 cgd struct filed {
141 1.1 cgd struct filed *f_next; /* next in linked list */
142 1.1 cgd short f_type; /* entry type, see below */
143 1.1 cgd short f_file; /* file descriptor */
144 1.1 cgd time_t f_time; /* time this was last written */
145 1.1 cgd u_char f_pmask[LOG_NFACILITIES+1]; /* priority mask */
146 1.1 cgd union {
147 1.1 cgd char f_uname[MAXUNAMES][UT_NAMESIZE+1];
148 1.1 cgd struct {
149 1.1 cgd char f_hname[MAXHOSTNAMELEN+1];
150 1.30 itojun struct addrinfo *f_addr;
151 1.1 cgd } f_forw; /* forwarding address */
152 1.1 cgd char f_fname[MAXPATHLEN];
153 1.1 cgd } f_un;
154 1.1 cgd char f_prevline[MAXSVLINE]; /* last message logged */
155 1.1 cgd char f_lasttime[16]; /* time of last occurrence */
156 1.1 cgd char f_prevhost[MAXHOSTNAMELEN+1]; /* host from which recd. */
157 1.1 cgd int f_prevpri; /* pri of f_prevline */
158 1.1 cgd int f_prevlen; /* length of f_prevline */
159 1.1 cgd int f_prevcount; /* repetition cnt of prevline */
160 1.1 cgd int f_repeatcount; /* number of "repeated" msgs */
161 1.1 cgd };
162 1.1 cgd
163 1.1 cgd /*
164 1.1 cgd * Intervals at which we flush out "message repeated" messages,
165 1.1 cgd * in seconds after previous message is logged. After each flush,
166 1.1 cgd * we move to the next interval until we reach the largest.
167 1.1 cgd */
168 1.1 cgd int repeatinterval[] = { 30, 120, 600 }; /* # of secs before flush */
169 1.1 cgd #define MAXREPEAT ((sizeof(repeatinterval) / sizeof(repeatinterval[0])) - 1)
170 1.1 cgd #define REPEATTIME(f) ((f)->f_time + repeatinterval[(f)->f_repeatcount])
171 1.1 cgd #define BACKOFF(f) { if (++(f)->f_repeatcount > MAXREPEAT) \
172 1.1 cgd (f)->f_repeatcount = MAXREPEAT; \
173 1.1 cgd }
174 1.1 cgd
175 1.1 cgd /* values for f_type */
176 1.1 cgd #define F_UNUSED 0 /* unused entry */
177 1.1 cgd #define F_FILE 1 /* regular file */
178 1.1 cgd #define F_TTY 2 /* terminal */
179 1.1 cgd #define F_CONSOLE 3 /* console terminal */
180 1.1 cgd #define F_FORW 4 /* remote machine */
181 1.1 cgd #define F_USERS 5 /* list of users */
182 1.1 cgd #define F_WALL 6 /* everyone logged on */
183 1.1 cgd
184 1.1 cgd char *TypeNames[7] = {
185 1.1 cgd "UNUSED", "FILE", "TTY", "CONSOLE",
186 1.1 cgd "FORW", "USERS", "WALL"
187 1.1 cgd };
188 1.1 cgd
189 1.1 cgd struct filed *Files;
190 1.1 cgd struct filed consfile;
191 1.1 cgd
192 1.1 cgd int Debug; /* debug flag */
193 1.47 manu int daemonized = 0; /* we are not daemonized yet */
194 1.1 cgd char LocalHostName[MAXHOSTNAMELEN+1]; /* our hostname */
195 1.1 cgd char *LocalDomain; /* our local domain name */
196 1.38 jwise int *finet = NULL; /* Internet datagram sockets */
197 1.1 cgd int Initialized = 0; /* set when we have initialized ourselves */
198 1.1 cgd int MarkInterval = 20 * 60; /* interval between marks in seconds */
199 1.1 cgd int MarkSeq = 0; /* mark sequence number */
200 1.35 jwise int SecureMode = 0; /* listen only on unix domain socks */
201 1.45 mrg int UseNameService = 1; /* make domain name queries */
202 1.36 jwise int NumForwards = 0; /* number of forwarding actions in conf file */
203 1.22 mrg char **LogPaths; /* array of pathnames to read messages from */
204 1.1 cgd
205 1.53 wiz void cfline(char *, struct filed *);
206 1.53 wiz char *cvthname(struct sockaddr_storage *);
207 1.53 wiz int decode(const char *, CODE *);
208 1.53 wiz void die(int);
209 1.53 wiz void domark(int);
210 1.53 wiz void fprintlog(struct filed *, int, char *);
211 1.53 wiz int getmsgbufsize(void);
212 1.53 wiz int* socksetup(int);
213 1.53 wiz void init(int);
214 1.53 wiz void logerror(const char *, ...);
215 1.53 wiz void logmsg(int, char *, char *, int);
216 1.53 wiz void printline(char *, char *);
217 1.53 wiz void printsys(char *);
218 1.53 wiz void reapchild(int);
219 1.53 wiz void usage(void);
220 1.53 wiz void wallmsg(struct filed *, struct iovec *);
221 1.53 wiz int main(int, char *[]);
222 1.53 wiz void logpath_add(char ***, int *, int *, char *);
223 1.53 wiz void logpath_fileadd(char ***, int *, int *, char *);
224 1.1 cgd
225 1.5 perry int
226 1.53 wiz main(int argc, char *argv[])
227 1.1 cgd {
228 1.22 mrg int ch, *funix, i, j, fklog, len, linesize;
229 1.31 itojun int *nfinetix, nfklogix, nfunixbaseix, nfds;
230 1.22 mrg int funixsize = 0, funixmaxsize = 0;
231 1.1 cgd struct sockaddr_un sunx, fromunix;
232 1.30 itojun struct sockaddr_storage frominet;
233 1.22 mrg char *p, *line, **pp;
234 1.22 mrg struct pollfd *readfds;
235 1.47 manu uid_t uid = 0;
236 1.47 manu gid_t gid = 0;
237 1.47 manu char *user = NULL;
238 1.47 manu char *group = NULL;
239 1.47 manu char *root = "/";
240 1.47 manu char *endp;
241 1.47 manu struct group *gr;
242 1.47 manu struct passwd *pw;
243 1.47 manu
244 1.41 tron
245 1.41 tron (void)setlocale(LC_ALL, "");
246 1.1 cgd
247 1.47 manu while ((ch = getopt(argc, argv, "dnsf:m:p:P:u:g:t:")) != -1)
248 1.5 perry switch(ch) {
249 1.53 wiz case 'd': /* debug */
250 1.53 wiz Debug++;
251 1.53 wiz break;
252 1.53 wiz case 'f': /* configuration file */
253 1.53 wiz ConfFile = optarg;
254 1.47 manu break;
255 1.47 manu case 'g':
256 1.47 manu group = optarg;
257 1.47 manu if (*group == '\0')
258 1.47 manu usage();
259 1.47 manu break;
260 1.1 cgd case 'm': /* mark interval */
261 1.1 cgd MarkInterval = atoi(optarg) * 60;
262 1.1 cgd break;
263 1.45 mrg case 'n': /* turn off DNS queries */
264 1.45 mrg UseNameService = 0;
265 1.45 mrg break;
266 1.1 cgd case 'p': /* path */
267 1.22 mrg logpath_add(&LogPaths, &funixsize,
268 1.22 mrg &funixmaxsize, optarg);
269 1.22 mrg break;
270 1.22 mrg case 'P': /* file of paths */
271 1.22 mrg logpath_fileadd(&LogPaths, &funixsize,
272 1.22 mrg &funixmaxsize, optarg);
273 1.1 cgd break;
274 1.35 jwise case 's': /* no network listen mode */
275 1.7 perry SecureMode++;
276 1.7 perry break;
277 1.53 wiz case 't':
278 1.53 wiz root = optarg;
279 1.53 wiz if (*root == '\0')
280 1.53 wiz usage();
281 1.53 wiz break;
282 1.53 wiz case 'u':
283 1.53 wiz user = optarg;
284 1.53 wiz if (*user == '\0')
285 1.53 wiz usage();
286 1.53 wiz break;
287 1.1 cgd case '?':
288 1.1 cgd default:
289 1.1 cgd usage();
290 1.1 cgd }
291 1.5 perry if ((argc -= optind) != 0)
292 1.1 cgd usage();
293 1.1 cgd
294 1.47 manu setlinebuf(stdout);
295 1.47 manu
296 1.47 manu if (user != NULL) {
297 1.47 manu if (isdigit((unsigned char)*user)) {
298 1.47 manu uid = (uid_t)strtoul(user, &endp, 0);
299 1.47 manu if (*endp != '\0')
300 1.47 manu goto getuser;
301 1.47 manu } else {
302 1.47 manu getuser:
303 1.47 manu if ((pw = getpwnam(user)) != NULL) {
304 1.47 manu uid = pw->pw_uid;
305 1.47 manu } else {
306 1.47 manu errno = 0;
307 1.47 manu logerror("Cannot find user `%s'", user);
308 1.47 manu die (0);
309 1.47 manu }
310 1.47 manu }
311 1.47 manu }
312 1.47 manu
313 1.47 manu if (group != NULL) {
314 1.47 manu if (isdigit((unsigned char)*group)) {
315 1.47 manu gid = (gid_t)strtoul(group, &endp, 0);
316 1.47 manu if (*endp != '\0')
317 1.47 manu goto getgroup;
318 1.47 manu } else {
319 1.47 manu getgroup:
320 1.47 manu if ((gr = getgrnam(group)) != NULL) {
321 1.47 manu gid = gr->gr_gid;
322 1.47 manu } else {
323 1.47 manu errno = 0;
324 1.47 manu logerror("Cannot find group `%s'", group);
325 1.47 manu die(0);
326 1.47 manu }
327 1.47 manu }
328 1.47 manu }
329 1.47 manu
330 1.47 manu if (access (root, F_OK | R_OK)) {
331 1.53 wiz logerror("Cannot access `%s'", root);
332 1.47 manu die (0);
333 1.47 manu }
334 1.1 cgd
335 1.1 cgd consfile.f_type = F_CONSOLE;
336 1.5 perry (void)strcpy(consfile.f_un.f_fname, ctty);
337 1.5 perry (void)gethostname(LocalHostName, sizeof(LocalHostName));
338 1.19 mrg LocalHostName[sizeof(LocalHostName) - 1] = '\0';
339 1.5 perry if ((p = strchr(LocalHostName, '.')) != NULL) {
340 1.1 cgd *p++ = '\0';
341 1.1 cgd LocalDomain = p;
342 1.5 perry } else
343 1.1 cgd LocalDomain = "";
344 1.15 leo linesize = getmsgbufsize();
345 1.15 leo if (linesize < MAXLINE)
346 1.15 leo linesize = MAXLINE;
347 1.15 leo linesize++;
348 1.15 leo line = malloc(linesize);
349 1.15 leo if (line == NULL) {
350 1.47 manu logerror("Couldn't allocate line buffer");
351 1.15 leo die(0);
352 1.15 leo }
353 1.5 perry (void)signal(SIGTERM, die);
354 1.5 perry (void)signal(SIGINT, Debug ? die : SIG_IGN);
355 1.5 perry (void)signal(SIGQUIT, Debug ? die : SIG_IGN);
356 1.5 perry (void)signal(SIGCHLD, reapchild);
357 1.5 perry (void)signal(SIGALRM, domark);
358 1.5 perry (void)alarm(TIMERINTVL);
359 1.5 perry
360 1.5 perry #ifndef SUN_LEN
361 1.5 perry #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
362 1.5 perry #endif
363 1.24 tron if (funixsize == 0)
364 1.24 tron logpath_add(&LogPaths, &funixsize,
365 1.24 tron &funixmaxsize, _PATH_LOG);
366 1.22 mrg funix = (int *)malloc(sizeof(int) * funixsize);
367 1.22 mrg if (funix == NULL) {
368 1.47 manu logerror("Couldn't allocate funix descriptors");
369 1.1 cgd die(0);
370 1.1 cgd }
371 1.26 tron for (j = 0, pp = LogPaths; *pp; pp++, j++) {
372 1.47 manu dprintf("Making unix dgram socket `%s'\n", *pp);
373 1.22 mrg unlink(*pp);
374 1.22 mrg memset(&sunx, 0, sizeof(sunx));
375 1.22 mrg sunx.sun_family = AF_LOCAL;
376 1.22 mrg (void)strncpy(sunx.sun_path, *pp, sizeof(sunx.sun_path));
377 1.22 mrg funix[j] = socket(AF_LOCAL, SOCK_DGRAM, 0);
378 1.22 mrg if (funix[j] < 0 || bind(funix[j],
379 1.22 mrg (struct sockaddr *)&sunx, SUN_LEN(&sunx)) < 0 ||
380 1.22 mrg chmod(*pp, 0666) < 0) {
381 1.47 manu logerror("Cannot create `%s'", *pp);
382 1.22 mrg die(0);
383 1.22 mrg }
384 1.47 manu dprintf("Listening on unix dgram socket `%s'\n", *pp);
385 1.22 mrg }
386 1.7 perry
387 1.36 jwise init(0);
388 1.30 itojun
389 1.22 mrg if ((fklog = open(_PATH_KLOG, O_RDONLY, 0)) < 0) {
390 1.47 manu dprintf("Can't open `%s' (%d)\n", _PATH_KLOG, errno);
391 1.22 mrg } else {
392 1.47 manu dprintf("Listening on kernel log `%s'\n", _PATH_KLOG);
393 1.1 cgd }
394 1.1 cgd
395 1.47 manu dprintf("Off & running....\n");
396 1.1 cgd
397 1.5 perry (void)signal(SIGHUP, init);
398 1.1 cgd
399 1.22 mrg /* setup pollfd set. */
400 1.22 mrg readfds = (struct pollfd *)malloc(sizeof(struct pollfd) *
401 1.31 itojun (funixsize + (finet ? *finet : 0) + 1));
402 1.22 mrg if (readfds == NULL) {
403 1.47 manu logerror("Couldn't allocate pollfds");
404 1.22 mrg die(0);
405 1.22 mrg }
406 1.22 mrg nfds = 0;
407 1.22 mrg if (fklog >= 0) {
408 1.22 mrg nfklogix = nfds++;
409 1.22 mrg readfds[nfklogix].fd = fklog;
410 1.22 mrg readfds[nfklogix].events = POLLIN | POLLPRI;
411 1.22 mrg }
412 1.34 lukem if (finet && !SecureMode) {
413 1.31 itojun nfinetix = malloc(*finet * sizeof(*nfinetix));
414 1.31 itojun for (j = 0; j < *finet; j++) {
415 1.31 itojun nfinetix[j] = nfds++;
416 1.31 itojun readfds[nfinetix[j]].fd = finet[j+1];
417 1.31 itojun readfds[nfinetix[j]].events = POLLIN | POLLPRI;
418 1.31 itojun }
419 1.30 itojun }
420 1.22 mrg nfunixbaseix = nfds;
421 1.22 mrg for (j = 0, pp = LogPaths; *pp; pp++) {
422 1.22 mrg readfds[nfds].fd = funix[j++];
423 1.22 mrg readfds[nfds++].events = POLLIN | POLLPRI;
424 1.22 mrg }
425 1.22 mrg
426 1.47 manu /*
427 1.47 manu * All files are open, we can drop privileges and chroot
428 1.47 manu */
429 1.53 wiz dprintf("Attempt to chroot to `%s'\n", root);
430 1.53 wiz if (chroot(root)) {
431 1.53 wiz logerror("Failed to chroot to `%s'", root);
432 1.47 manu die(0);
433 1.47 manu }
434 1.53 wiz dprintf("Attempt to set GID/EGID to `%d'\n", gid);
435 1.53 wiz if (setgid(gid) || setegid(gid)) {
436 1.53 wiz logerror("Failed to set gid to `%d'", gid);
437 1.47 manu die(0);
438 1.47 manu }
439 1.53 wiz dprintf("Attempt to set UID/EUID to `%d'\n", uid);
440 1.53 wiz if (setuid(uid) || seteuid(uid)) {
441 1.53 wiz logerror("Failed to set uid to `%d'", uid);
442 1.47 manu die(0);
443 1.47 manu }
444 1.47 manu
445 1.47 manu /*
446 1.47 manu * We cannot detach from the terminal before we are sure we won't
447 1.47 manu * have a fatal error, because error message would not go to the
448 1.47 manu * terminal and would not be logged because syslogd dies.
449 1.47 manu * All die() calls are behind us, we can call daemon()
450 1.47 manu */
451 1.47 manu if (!Debug) {
452 1.47 manu (void)daemon(0, 0);
453 1.47 manu daemonized = 1;
454 1.48 taca
455 1.48 taca /* tuck my process id away, if i'm not in debug mode */
456 1.48 taca pidfile(NULL);
457 1.47 manu }
458 1.47 manu
459 1.1 cgd for (;;) {
460 1.22 mrg int rv;
461 1.1 cgd
462 1.22 mrg rv = poll(readfds, nfds, INFTIM);
463 1.22 mrg if (rv == 0)
464 1.1 cgd continue;
465 1.22 mrg if (rv < 0) {
466 1.1 cgd if (errno != EINTR)
467 1.47 manu logerror("poll() failed");
468 1.1 cgd continue;
469 1.1 cgd }
470 1.47 manu dprintf("Got a message (%d)\n", rv);
471 1.22 mrg if (fklog >= 0 &&
472 1.22 mrg (readfds[nfklogix].revents & (POLLIN | POLLPRI))) {
473 1.47 manu dprintf("Kernel log active\n");
474 1.15 leo i = read(fklog, line, linesize - 1);
475 1.1 cgd if (i > 0) {
476 1.1 cgd line[i] = '\0';
477 1.1 cgd printsys(line);
478 1.1 cgd } else if (i < 0 && errno != EINTR) {
479 1.47 manu logerror("klog failed");
480 1.1 cgd fklog = -1;
481 1.1 cgd }
482 1.1 cgd }
483 1.22 mrg for (j = 0, pp = LogPaths; *pp; pp++, j++) {
484 1.22 mrg if ((readfds[nfunixbaseix + j].revents &
485 1.22 mrg (POLLIN | POLLPRI)) == 0)
486 1.22 mrg continue;
487 1.22 mrg
488 1.47 manu dprintf("Unix socket (%s) active\n", *pp);
489 1.5 perry len = sizeof(fromunix);
490 1.22 mrg i = recvfrom(funix[j], line, MAXLINE, 0,
491 1.5 perry (struct sockaddr *)&fromunix, &len);
492 1.1 cgd if (i > 0) {
493 1.1 cgd line[i] = '\0';
494 1.1 cgd printline(LocalHostName, line);
495 1.22 mrg } else if (i < 0 && errno != EINTR) {
496 1.47 manu logerror("recvfrom() unix `%s'", *pp);
497 1.22 mrg }
498 1.1 cgd }
499 1.34 lukem if (finet && !SecureMode) {
500 1.31 itojun for (j = 0; j < *finet; j++) {
501 1.34 lukem if (readfds[nfinetix[j]].revents &
502 1.34 lukem (POLLIN | POLLPRI)) {
503 1.46 itojun #ifdef LIBWRAP
504 1.46 itojun struct request_info req;
505 1.46 itojun #endif
506 1.46 itojun int reject = 0;
507 1.46 itojun
508 1.31 itojun dprintf("inet socket active\n");
509 1.46 itojun
510 1.46 itojun #ifdef LIBWRAP
511 1.46 itojun request_init(&req, RQ_DAEMON, "syslogd",
512 1.46 itojun RQ_FILE, finet[j + 1], NULL);
513 1.46 itojun fromhost(&req);
514 1.46 itojun reject = !hosts_access(&req);
515 1.46 itojun if (reject)
516 1.46 itojun dprintf("access denied\n");
517 1.46 itojun #endif
518 1.46 itojun
519 1.31 itojun len = sizeof(frominet);
520 1.34 lukem i = recvfrom(finet[j+1], line, MAXLINE,
521 1.34 lukem 0, (struct sockaddr *)&frominet,
522 1.34 lukem &len);
523 1.46 itojun if (i == 0 || (i < 0 && errno == EINTR))
524 1.46 itojun continue;
525 1.46 itojun else if (i < 0) {
526 1.46 itojun logerror("recvfrom inet");
527 1.46 itojun continue;
528 1.46 itojun }
529 1.46 itojun
530 1.46 itojun line[i] = '\0';
531 1.46 itojun if (!reject)
532 1.34 lukem printline(cvthname(&frominet),
533 1.34 lukem line);
534 1.31 itojun }
535 1.31 itojun }
536 1.30 itojun }
537 1.1 cgd }
538 1.1 cgd }
539 1.1 cgd
540 1.5 perry void
541 1.53 wiz usage(void)
542 1.1 cgd {
543 1.5 perry
544 1.5 perry (void)fprintf(stderr,
545 1.53 wiz "usage: %s [-dns] [-f config_file] [-g group] [-m mark_interval]\n"
546 1.53 wiz "\t[-P file_list] [-p log_socket [-p log_socket2 ...]]\n"
547 1.53 wiz "\t[-t chroot_dir] [-u user]\n", getprogname());
548 1.1 cgd exit(1);
549 1.1 cgd }
550 1.1 cgd
551 1.1 cgd /*
552 1.22 mrg * given a pointer to an array of char *'s, a pointer to it's current
553 1.22 mrg * size and current allocated max size, and a new char * to add, add
554 1.22 mrg * it, update everything as necessary, possibly allocating a new array
555 1.22 mrg */
556 1.22 mrg void
557 1.53 wiz logpath_add(char ***lp, int *szp, int *maxszp, char *new)
558 1.22 mrg {
559 1.22 mrg
560 1.47 manu dprintf("Adding `%s' to the %p logpath list\n", new, *lp);
561 1.22 mrg if (*szp == *maxszp) {
562 1.22 mrg if (*maxszp == 0) {
563 1.22 mrg *maxszp = 4; /* start of with enough for now */
564 1.26 tron *lp = NULL;
565 1.50 lukem } else
566 1.22 mrg *maxszp *= 2;
567 1.26 tron *lp = realloc(*lp, sizeof(char *) * (*maxszp + 1));
568 1.26 tron if (*lp == NULL) {
569 1.47 manu logerror("Couldn't allocate line buffer");
570 1.26 tron die(0);
571 1.22 mrg }
572 1.22 mrg }
573 1.50 lukem if (((*lp)[(*szp)++] = strdup(new)) == NULL) {
574 1.50 lukem logerror("Couldn't allocate logpath");
575 1.50 lukem die(0);
576 1.50 lukem }
577 1.26 tron (*lp)[(*szp)] = NULL; /* always keep it NULL terminated */
578 1.22 mrg }
579 1.22 mrg
580 1.22 mrg /* do a file of log sockets */
581 1.22 mrg void
582 1.53 wiz logpath_fileadd(char ***lp, int *szp, int *maxszp, char *file)
583 1.22 mrg {
584 1.22 mrg FILE *fp;
585 1.22 mrg char *line;
586 1.22 mrg size_t len;
587 1.22 mrg
588 1.22 mrg fp = fopen(file, "r");
589 1.22 mrg if (fp == NULL) {
590 1.47 manu logerror("Could not open socket file list `%s'", file);
591 1.22 mrg die(0);
592 1.22 mrg }
593 1.22 mrg
594 1.22 mrg while ((line = fgetln(fp, &len))) {
595 1.22 mrg line[len - 1] = 0;
596 1.22 mrg logpath_add(lp, szp, maxszp, line);
597 1.22 mrg }
598 1.22 mrg fclose(fp);
599 1.22 mrg }
600 1.22 mrg
601 1.22 mrg /*
602 1.1 cgd * Take a raw input line, decode the message, and print the message
603 1.1 cgd * on the appropriate log files.
604 1.1 cgd */
605 1.5 perry void
606 1.53 wiz printline(char *hname, char *msg)
607 1.1 cgd {
608 1.5 perry int c, pri;
609 1.5 perry char *p, *q, line[MAXLINE + 1];
610 1.1 cgd
611 1.1 cgd /* test for special codes */
612 1.1 cgd pri = DEFUPRI;
613 1.1 cgd p = msg;
614 1.1 cgd if (*p == '<') {
615 1.1 cgd pri = 0;
616 1.1 cgd while (isdigit(*++p))
617 1.1 cgd pri = 10 * pri + (*p - '0');
618 1.1 cgd if (*p == '>')
619 1.1 cgd ++p;
620 1.1 cgd }
621 1.1 cgd if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
622 1.1 cgd pri = DEFUPRI;
623 1.1 cgd
624 1.1 cgd /* don't allow users to log kernel messages */
625 1.1 cgd if (LOG_FAC(pri) == LOG_KERN)
626 1.1 cgd pri = LOG_MAKEPRI(LOG_USER, LOG_PRI(pri));
627 1.1 cgd
628 1.1 cgd q = line;
629 1.1 cgd
630 1.42 sommerfe while ((c = *p++) != '\0' &&
631 1.42 sommerfe q < &line[sizeof(line) - 2]) {
632 1.42 sommerfe c &= 0177;
633 1.1 cgd if (iscntrl(c))
634 1.1 cgd if (c == '\n')
635 1.1 cgd *q++ = ' ';
636 1.1 cgd else if (c == '\t')
637 1.1 cgd *q++ = '\t';
638 1.1 cgd else {
639 1.1 cgd *q++ = '^';
640 1.1 cgd *q++ = c ^ 0100;
641 1.1 cgd }
642 1.1 cgd else
643 1.1 cgd *q++ = c;
644 1.42 sommerfe }
645 1.1 cgd *q = '\0';
646 1.1 cgd
647 1.1 cgd logmsg(pri, line, hname, 0);
648 1.1 cgd }
649 1.1 cgd
650 1.1 cgd /*
651 1.1 cgd * Take a raw input line from /dev/klog, split and format similar to syslog().
652 1.1 cgd */
653 1.5 perry void
654 1.53 wiz printsys(char *msg)
655 1.1 cgd {
656 1.5 perry int c, pri, flags;
657 1.5 perry char *lp, *p, *q, line[MAXLINE + 1];
658 1.1 cgd
659 1.5 perry (void)strcpy(line, _PATH_UNIX);
660 1.5 perry (void)strcat(line, ": ");
661 1.1 cgd lp = line + strlen(line);
662 1.1 cgd for (p = msg; *p != '\0'; ) {
663 1.1 cgd flags = SYNC_FILE | ADDDATE; /* fsync file after write */
664 1.1 cgd pri = DEFSPRI;
665 1.1 cgd if (*p == '<') {
666 1.1 cgd pri = 0;
667 1.1 cgd while (isdigit(*++p))
668 1.1 cgd pri = 10 * pri + (*p - '0');
669 1.1 cgd if (*p == '>')
670 1.1 cgd ++p;
671 1.1 cgd } else {
672 1.1 cgd /* kernel printf's come out on console */
673 1.1 cgd flags |= IGN_CONS;
674 1.1 cgd }
675 1.1 cgd if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
676 1.1 cgd pri = DEFSPRI;
677 1.1 cgd q = lp;
678 1.1 cgd while (*p != '\0' && (c = *p++) != '\n' &&
679 1.1 cgd q < &line[MAXLINE])
680 1.1 cgd *q++ = c;
681 1.1 cgd *q = '\0';
682 1.1 cgd logmsg(pri, line, LocalHostName, flags);
683 1.1 cgd }
684 1.1 cgd }
685 1.1 cgd
686 1.1 cgd time_t now;
687 1.1 cgd
688 1.1 cgd /*
689 1.1 cgd * Log a message to the appropriate log files, users, etc. based on
690 1.1 cgd * the priority.
691 1.1 cgd */
692 1.5 perry void
693 1.53 wiz logmsg(int pri, char *msg, char *from, int flags)
694 1.1 cgd {
695 1.5 perry struct filed *f;
696 1.5 perry int fac, msglen, omask, prilev;
697 1.1 cgd char *timestamp;
698 1.1 cgd
699 1.9 mrg dprintf("logmsg: pri 0%o, flags 0x%x, from %s, msg %s\n",
700 1.1 cgd pri, flags, from, msg);
701 1.1 cgd
702 1.1 cgd omask = sigblock(sigmask(SIGHUP)|sigmask(SIGALRM));
703 1.1 cgd
704 1.1 cgd /*
705 1.1 cgd * Check to see if msg looks non-standard.
706 1.1 cgd */
707 1.1 cgd msglen = strlen(msg);
708 1.1 cgd if (msglen < 16 || msg[3] != ' ' || msg[6] != ' ' ||
709 1.1 cgd msg[9] != ':' || msg[12] != ':' || msg[15] != ' ')
710 1.1 cgd flags |= ADDDATE;
711 1.1 cgd
712 1.5 perry (void)time(&now);
713 1.1 cgd if (flags & ADDDATE)
714 1.1 cgd timestamp = ctime(&now) + 4;
715 1.1 cgd else {
716 1.1 cgd timestamp = msg;
717 1.1 cgd msg += 16;
718 1.1 cgd msglen -= 16;
719 1.1 cgd }
720 1.1 cgd
721 1.1 cgd /* extract facility and priority level */
722 1.1 cgd if (flags & MARK)
723 1.1 cgd fac = LOG_NFACILITIES;
724 1.1 cgd else
725 1.1 cgd fac = LOG_FAC(pri);
726 1.1 cgd prilev = LOG_PRI(pri);
727 1.1 cgd
728 1.1 cgd /* log the message to the particular outputs */
729 1.1 cgd if (!Initialized) {
730 1.1 cgd f = &consfile;
731 1.1 cgd f->f_file = open(ctty, O_WRONLY, 0);
732 1.1 cgd
733 1.1 cgd if (f->f_file >= 0) {
734 1.1 cgd fprintlog(f, flags, msg);
735 1.5 perry (void)close(f->f_file);
736 1.1 cgd }
737 1.5 perry (void)sigsetmask(omask);
738 1.1 cgd return;
739 1.1 cgd }
740 1.1 cgd for (f = Files; f; f = f->f_next) {
741 1.1 cgd /* skip messages that are incorrect priority */
742 1.1 cgd if (f->f_pmask[fac] < prilev ||
743 1.1 cgd f->f_pmask[fac] == INTERNAL_NOPRI)
744 1.1 cgd continue;
745 1.1 cgd
746 1.1 cgd if (f->f_type == F_CONSOLE && (flags & IGN_CONS))
747 1.1 cgd continue;
748 1.1 cgd
749 1.1 cgd /* don't output marks to recently written files */
750 1.1 cgd if ((flags & MARK) && (now - f->f_time) < MarkInterval / 2)
751 1.1 cgd continue;
752 1.1 cgd
753 1.1 cgd /*
754 1.1 cgd * suppress duplicate lines to this file
755 1.1 cgd */
756 1.1 cgd if ((flags & MARK) == 0 && msglen == f->f_prevlen &&
757 1.1 cgd !strcmp(msg, f->f_prevline) &&
758 1.1 cgd !strcmp(from, f->f_prevhost)) {
759 1.5 perry (void)strncpy(f->f_lasttime, timestamp, 15);
760 1.1 cgd f->f_prevcount++;
761 1.47 manu dprintf("Msg repeated %d times, %ld sec of %d\n",
762 1.12 thorpej f->f_prevcount, (long)(now - f->f_time),
763 1.1 cgd repeatinterval[f->f_repeatcount]);
764 1.1 cgd /*
765 1.1 cgd * If domark would have logged this by now,
766 1.1 cgd * flush it now (so we don't hold isolated messages),
767 1.1 cgd * but back off so we'll flush less often
768 1.1 cgd * in the future.
769 1.1 cgd */
770 1.1 cgd if (now > REPEATTIME(f)) {
771 1.1 cgd fprintlog(f, flags, (char *)NULL);
772 1.1 cgd BACKOFF(f);
773 1.1 cgd }
774 1.1 cgd } else {
775 1.1 cgd /* new line, save it */
776 1.1 cgd if (f->f_prevcount)
777 1.1 cgd fprintlog(f, 0, (char *)NULL);
778 1.1 cgd f->f_repeatcount = 0;
779 1.3 cgd f->f_prevpri = pri;
780 1.5 perry (void)strncpy(f->f_lasttime, timestamp, 15);
781 1.5 perry (void)strncpy(f->f_prevhost, from,
782 1.1 cgd sizeof(f->f_prevhost));
783 1.1 cgd if (msglen < MAXSVLINE) {
784 1.1 cgd f->f_prevlen = msglen;
785 1.5 perry (void)strcpy(f->f_prevline, msg);
786 1.1 cgd fprintlog(f, flags, (char *)NULL);
787 1.1 cgd } else {
788 1.1 cgd f->f_prevline[0] = 0;
789 1.1 cgd f->f_prevlen = 0;
790 1.1 cgd fprintlog(f, flags, msg);
791 1.1 cgd }
792 1.1 cgd }
793 1.1 cgd }
794 1.5 perry (void)sigsetmask(omask);
795 1.1 cgd }
796 1.1 cgd
797 1.5 perry void
798 1.53 wiz fprintlog(struct filed *f, int flags, char *msg)
799 1.1 cgd {
800 1.1 cgd struct iovec iov[6];
801 1.5 perry struct iovec *v;
802 1.30 itojun struct addrinfo *r;
803 1.31 itojun int j, l, lsent;
804 1.1 cgd char line[MAXLINE + 1], repbuf[80], greetings[200];
805 1.1 cgd
806 1.1 cgd v = iov;
807 1.1 cgd if (f->f_type == F_WALL) {
808 1.1 cgd v->iov_base = greetings;
809 1.17 mrg v->iov_len = snprintf(greetings, sizeof greetings,
810 1.1 cgd "\r\n\7Message from syslogd@%s at %.24s ...\r\n",
811 1.1 cgd f->f_prevhost, ctime(&now));
812 1.1 cgd v++;
813 1.1 cgd v->iov_base = "";
814 1.1 cgd v->iov_len = 0;
815 1.1 cgd v++;
816 1.1 cgd } else {
817 1.1 cgd v->iov_base = f->f_lasttime;
818 1.1 cgd v->iov_len = 15;
819 1.1 cgd v++;
820 1.1 cgd v->iov_base = " ";
821 1.1 cgd v->iov_len = 1;
822 1.1 cgd v++;
823 1.1 cgd }
824 1.1 cgd v->iov_base = f->f_prevhost;
825 1.1 cgd v->iov_len = strlen(v->iov_base);
826 1.1 cgd v++;
827 1.1 cgd v->iov_base = " ";
828 1.1 cgd v->iov_len = 1;
829 1.1 cgd v++;
830 1.1 cgd
831 1.1 cgd if (msg) {
832 1.1 cgd v->iov_base = msg;
833 1.1 cgd v->iov_len = strlen(msg);
834 1.1 cgd } else if (f->f_prevcount > 1) {
835 1.1 cgd v->iov_base = repbuf;
836 1.17 mrg v->iov_len = snprintf(repbuf, sizeof repbuf,
837 1.17 mrg "last message repeated %d times", f->f_prevcount);
838 1.1 cgd } else {
839 1.1 cgd v->iov_base = f->f_prevline;
840 1.1 cgd v->iov_len = f->f_prevlen;
841 1.1 cgd }
842 1.1 cgd v++;
843 1.1 cgd
844 1.1 cgd dprintf("Logging to %s", TypeNames[f->f_type]);
845 1.1 cgd f->f_time = now;
846 1.1 cgd
847 1.1 cgd switch (f->f_type) {
848 1.1 cgd case F_UNUSED:
849 1.1 cgd dprintf("\n");
850 1.1 cgd break;
851 1.1 cgd
852 1.1 cgd case F_FORW:
853 1.1 cgd dprintf(" %s\n", f->f_un.f_forw.f_hname);
854 1.34 lukem /*
855 1.34 lukem * check for local vs remote messages
856 1.34 lukem * (from FreeBSD PR#bin/7055)
857 1.34 lukem */
858 1.21 tron if (strcmp(f->f_prevhost, LocalHostName)) {
859 1.21 tron l = snprintf(line, sizeof(line) - 1,
860 1.21 tron "<%d>%.15s [%s]: %s",
861 1.21 tron f->f_prevpri, (char *) iov[0].iov_base,
862 1.21 tron f->f_prevhost, (char *) iov[4].iov_base);
863 1.21 tron } else {
864 1.21 tron l = snprintf(line, sizeof(line) - 1, "<%d>%.15s %s",
865 1.21 tron f->f_prevpri, (char *) iov[0].iov_base,
866 1.21 tron (char *) iov[4].iov_base);
867 1.21 tron }
868 1.1 cgd if (l > MAXLINE)
869 1.1 cgd l = MAXLINE;
870 1.34 lukem if (finet) {
871 1.30 itojun for (r = f->f_un.f_forw.f_addr; r; r = r->ai_next) {
872 1.31 itojun for (j = 0; j < *finet; j++) {
873 1.31 itojun #if 0
874 1.34 lukem /*
875 1.34 lukem * should we check AF first, or just
876 1.34 lukem * trial and error? FWD
877 1.34 lukem */
878 1.34 lukem if (r->ai_family ==
879 1.34 lukem address_family_of(finet[j+1]))
880 1.30 itojun #endif
881 1.34 lukem lsent = sendto(finet[j+1], line, l, 0,
882 1.34 lukem r->ai_addr, r->ai_addrlen);
883 1.31 itojun if (lsent == l)
884 1.31 itojun break;
885 1.31 itojun }
886 1.30 itojun }
887 1.30 itojun if (lsent != l) {
888 1.30 itojun f->f_type = F_UNUSED;
889 1.47 manu logerror("sendto() failed");
890 1.30 itojun }
891 1.1 cgd }
892 1.1 cgd break;
893 1.1 cgd
894 1.1 cgd case F_CONSOLE:
895 1.1 cgd if (flags & IGN_CONS) {
896 1.1 cgd dprintf(" (ignored)\n");
897 1.1 cgd break;
898 1.1 cgd }
899 1.1 cgd /* FALLTHROUGH */
900 1.1 cgd
901 1.1 cgd case F_TTY:
902 1.1 cgd case F_FILE:
903 1.1 cgd dprintf(" %s\n", f->f_un.f_fname);
904 1.1 cgd if (f->f_type != F_FILE) {
905 1.1 cgd v->iov_base = "\r\n";
906 1.1 cgd v->iov_len = 2;
907 1.1 cgd } else {
908 1.1 cgd v->iov_base = "\n";
909 1.1 cgd v->iov_len = 1;
910 1.1 cgd }
911 1.1 cgd again:
912 1.1 cgd if (writev(f->f_file, iov, 6) < 0) {
913 1.1 cgd int e = errno;
914 1.5 perry (void)close(f->f_file);
915 1.1 cgd /*
916 1.1 cgd * Check for errors on TTY's due to loss of tty
917 1.1 cgd */
918 1.1 cgd if ((e == EIO || e == EBADF) && f->f_type != F_FILE) {
919 1.1 cgd f->f_file = open(f->f_un.f_fname,
920 1.1 cgd O_WRONLY|O_APPEND, 0);
921 1.1 cgd if (f->f_file < 0) {
922 1.1 cgd f->f_type = F_UNUSED;
923 1.1 cgd logerror(f->f_un.f_fname);
924 1.1 cgd } else
925 1.1 cgd goto again;
926 1.1 cgd } else {
927 1.1 cgd f->f_type = F_UNUSED;
928 1.1 cgd errno = e;
929 1.1 cgd logerror(f->f_un.f_fname);
930 1.1 cgd }
931 1.1 cgd } else if (flags & SYNC_FILE)
932 1.5 perry (void)fsync(f->f_file);
933 1.1 cgd break;
934 1.1 cgd
935 1.1 cgd case F_USERS:
936 1.1 cgd case F_WALL:
937 1.1 cgd dprintf("\n");
938 1.1 cgd v->iov_base = "\r\n";
939 1.1 cgd v->iov_len = 2;
940 1.1 cgd wallmsg(f, iov);
941 1.1 cgd break;
942 1.1 cgd }
943 1.1 cgd f->f_prevcount = 0;
944 1.1 cgd }
945 1.1 cgd
946 1.1 cgd /*
947 1.1 cgd * WALLMSG -- Write a message to the world at large
948 1.1 cgd *
949 1.1 cgd * Write the specified message to either the entire
950 1.1 cgd * world, or a list of approved users.
951 1.1 cgd */
952 1.5 perry void
953 1.53 wiz wallmsg(struct filed *f, struct iovec *iov)
954 1.1 cgd {
955 1.1 cgd static int reenter; /* avoid calling ourselves */
956 1.5 perry int i;
957 1.5 perry char *p;
958 1.55 christos static struct utmpentry *ohead = NULL;
959 1.55 christos struct utmpentry *ep;
960 1.1 cgd
961 1.1 cgd if (reenter++)
962 1.1 cgd return;
963 1.55 christos
964 1.55 christos (void)getutentries(NULL, &ep);
965 1.55 christos if (ep != ohead) {
966 1.55 christos freeutentries(ohead);
967 1.55 christos ohead = ep;
968 1.1 cgd }
969 1.1 cgd /* NOSTRICT */
970 1.55 christos for (; ep; ep = ep->next) {
971 1.1 cgd if (f->f_type == F_WALL) {
972 1.55 christos if ((p = ttymsg(iov, 6, ep->line, TTYMSGTIME))
973 1.55 christos != NULL) {
974 1.1 cgd errno = 0; /* already in msg */
975 1.1 cgd logerror(p);
976 1.1 cgd }
977 1.1 cgd continue;
978 1.1 cgd }
979 1.1 cgd /* should we send the message to this user? */
980 1.1 cgd for (i = 0; i < MAXUNAMES; i++) {
981 1.1 cgd if (!f->f_un.f_uname[i][0])
982 1.1 cgd break;
983 1.55 christos if (strcmp(f->f_un.f_uname[i], ep->name) == 0) {
984 1.55 christos if ((p = ttymsg(iov, 6, ep->line, TTYMSGTIME))
985 1.55 christos != NULL) {
986 1.1 cgd errno = 0; /* already in msg */
987 1.1 cgd logerror(p);
988 1.1 cgd }
989 1.1 cgd break;
990 1.1 cgd }
991 1.1 cgd }
992 1.1 cgd }
993 1.1 cgd reenter = 0;
994 1.1 cgd }
995 1.1 cgd
996 1.1 cgd void
997 1.53 wiz reapchild(int signo)
998 1.1 cgd {
999 1.1 cgd union wait status;
1000 1.1 cgd
1001 1.5 perry while (wait3((int *)&status, WNOHANG, (struct rusage *)NULL) > 0)
1002 1.1 cgd ;
1003 1.1 cgd }
1004 1.1 cgd
1005 1.1 cgd /*
1006 1.1 cgd * Return a printable representation of a host address.
1007 1.1 cgd */
1008 1.1 cgd char *
1009 1.53 wiz cvthname(struct sockaddr_storage *f)
1010 1.1 cgd {
1011 1.30 itojun int error;
1012 1.5 perry char *p;
1013 1.30 itojun const int niflag = NI_DGRAM;
1014 1.30 itojun static char host[NI_MAXHOST], ip[NI_MAXHOST];
1015 1.1 cgd
1016 1.30 itojun error = getnameinfo((struct sockaddr*)f, ((struct sockaddr*)f)->sa_len,
1017 1.33 itojun ip, sizeof ip, NULL, 0, NI_NUMERICHOST|niflag);
1018 1.1 cgd
1019 1.30 itojun dprintf("cvthname(%s)\n", ip);
1020 1.30 itojun
1021 1.30 itojun if (error) {
1022 1.30 itojun dprintf("Malformed from address %s\n", gai_strerror(error));
1023 1.1 cgd return ("???");
1024 1.1 cgd }
1025 1.45 mrg
1026 1.45 mrg if (!UseNameService)
1027 1.45 mrg return (ip);
1028 1.30 itojun
1029 1.30 itojun error = getnameinfo((struct sockaddr*)f, ((struct sockaddr*)f)->sa_len,
1030 1.33 itojun host, sizeof host, NULL, 0, niflag);
1031 1.30 itojun if (error) {
1032 1.30 itojun dprintf("Host name for your address (%s) unknown\n", ip);
1033 1.30 itojun return (ip);
1034 1.1 cgd }
1035 1.30 itojun if ((p = strchr(host, '.')) && strcmp(p + 1, LocalDomain) == 0)
1036 1.1 cgd *p = '\0';
1037 1.30 itojun return (host);
1038 1.1 cgd }
1039 1.1 cgd
1040 1.1 cgd void
1041 1.53 wiz domark(int signo)
1042 1.1 cgd {
1043 1.5 perry struct filed *f;
1044 1.1 cgd
1045 1.1 cgd now = time((time_t *)NULL);
1046 1.1 cgd MarkSeq += TIMERINTVL;
1047 1.1 cgd if (MarkSeq >= MarkInterval) {
1048 1.1 cgd logmsg(LOG_INFO, "-- MARK --", LocalHostName, ADDDATE|MARK);
1049 1.1 cgd MarkSeq = 0;
1050 1.1 cgd }
1051 1.1 cgd
1052 1.1 cgd for (f = Files; f; f = f->f_next) {
1053 1.1 cgd if (f->f_prevcount && now >= REPEATTIME(f)) {
1054 1.47 manu dprintf("Flush %s: repeated %d times, %d sec.\n",
1055 1.1 cgd TypeNames[f->f_type], f->f_prevcount,
1056 1.1 cgd repeatinterval[f->f_repeatcount]);
1057 1.1 cgd fprintlog(f, 0, (char *)NULL);
1058 1.1 cgd BACKOFF(f);
1059 1.1 cgd }
1060 1.1 cgd }
1061 1.5 perry (void)alarm(TIMERINTVL);
1062 1.1 cgd }
1063 1.1 cgd
1064 1.1 cgd /*
1065 1.1 cgd * Print syslogd errors some place.
1066 1.1 cgd */
1067 1.5 perry void
1068 1.47 manu logerror(const char *fmt, ...)
1069 1.1 cgd {
1070 1.47 manu va_list ap;
1071 1.47 manu char tmpbuf[BUFSIZ];
1072 1.47 manu char buf[BUFSIZ];
1073 1.47 manu
1074 1.47 manu va_start(ap, fmt);
1075 1.47 manu
1076 1.47 manu (void)vsnprintf(tmpbuf, sizeof(tmpbuf), fmt, ap);
1077 1.47 manu
1078 1.47 manu va_end(ap);
1079 1.1 cgd
1080 1.1 cgd if (errno)
1081 1.47 manu (void)snprintf(buf, sizeof(buf), "syslogd: %s: %s",
1082 1.47 manu tmpbuf, strerror(errno));
1083 1.1 cgd else
1084 1.47 manu (void)snprintf(buf, sizeof(buf), "syslogd: %s", tmpbuf);
1085 1.47 manu
1086 1.47 manu if (daemonized)
1087 1.47 manu logmsg(LOG_SYSLOG|LOG_ERR, buf, LocalHostName, ADDDATE);
1088 1.47 manu if (!daemonized && Debug)
1089 1.47 manu dprintf("%s\n", buf);
1090 1.47 manu if (!daemonized && !Debug)
1091 1.47 manu printf("%s\n", buf);
1092 1.47 manu
1093 1.47 manu return;
1094 1.1 cgd }
1095 1.1 cgd
1096 1.1 cgd void
1097 1.53 wiz die(int signo)
1098 1.1 cgd {
1099 1.5 perry struct filed *f;
1100 1.47 manu char **p;
1101 1.1 cgd
1102 1.1 cgd for (f = Files; f != NULL; f = f->f_next) {
1103 1.1 cgd /* flush any pending output */
1104 1.1 cgd if (f->f_prevcount)
1105 1.1 cgd fprintlog(f, 0, (char *)NULL);
1106 1.1 cgd }
1107 1.47 manu errno = 0;
1108 1.47 manu if (signo)
1109 1.49 kim logerror("Exiting on signal %d", signo);
1110 1.47 manu else
1111 1.47 manu logerror("Fatal error, exiting");
1112 1.22 mrg for (p = LogPaths; p && *p; p++)
1113 1.22 mrg unlink(*p);
1114 1.1 cgd exit(0);
1115 1.1 cgd }
1116 1.1 cgd
1117 1.1 cgd /*
1118 1.1 cgd * INIT -- Initialize syslogd from configuration table
1119 1.1 cgd */
1120 1.1 cgd void
1121 1.53 wiz init(int signo)
1122 1.1 cgd {
1123 1.5 perry int i;
1124 1.5 perry FILE *cf;
1125 1.5 perry struct filed *f, *next, **nextp;
1126 1.5 perry char *p;
1127 1.5 perry char cline[LINE_MAX];
1128 1.1 cgd
1129 1.1 cgd dprintf("init\n");
1130 1.1 cgd
1131 1.1 cgd /*
1132 1.1 cgd * Close all open log files.
1133 1.1 cgd */
1134 1.1 cgd Initialized = 0;
1135 1.1 cgd for (f = Files; f != NULL; f = next) {
1136 1.1 cgd /* flush any pending output */
1137 1.1 cgd if (f->f_prevcount)
1138 1.1 cgd fprintlog(f, 0, (char *)NULL);
1139 1.1 cgd
1140 1.1 cgd switch (f->f_type) {
1141 1.5 perry case F_FILE:
1142 1.5 perry case F_TTY:
1143 1.5 perry case F_CONSOLE:
1144 1.5 perry (void)close(f->f_file);
1145 1.44 itojun break;
1146 1.44 itojun case F_FORW:
1147 1.44 itojun if (f->f_un.f_forw.f_addr)
1148 1.44 itojun freeaddrinfo(f->f_un.f_forw.f_addr);
1149 1.1 cgd break;
1150 1.1 cgd }
1151 1.1 cgd next = f->f_next;
1152 1.5 perry free((char *)f);
1153 1.1 cgd }
1154 1.1 cgd Files = NULL;
1155 1.1 cgd nextp = &Files;
1156 1.1 cgd
1157 1.38 jwise /*
1158 1.38 jwise * Close all open sockets
1159 1.38 jwise */
1160 1.38 jwise
1161 1.38 jwise if (finet) {
1162 1.38 jwise for (i = 0; i < *finet; i++) {
1163 1.38 jwise if (close(finet[i+1]) < 0) {
1164 1.47 manu logerror("close() failed");
1165 1.38 jwise die(0);
1166 1.38 jwise }
1167 1.38 jwise }
1168 1.38 jwise }
1169 1.39 jwise
1170 1.39 jwise /*
1171 1.39 jwise * Reset counter of forwarding actions
1172 1.39 jwise */
1173 1.39 jwise
1174 1.39 jwise NumForwards=0;
1175 1.38 jwise
1176 1.1 cgd /* open the configuration file */
1177 1.1 cgd if ((cf = fopen(ConfFile, "r")) == NULL) {
1178 1.47 manu dprintf("Cannot open `%s'\n", ConfFile);
1179 1.1 cgd *nextp = (struct filed *)calloc(1, sizeof(*f));
1180 1.1 cgd cfline("*.ERR\t/dev/console", *nextp);
1181 1.1 cgd (*nextp)->f_next = (struct filed *)calloc(1, sizeof(*f));
1182 1.1 cgd cfline("*.PANIC\t*", (*nextp)->f_next);
1183 1.1 cgd Initialized = 1;
1184 1.1 cgd return;
1185 1.1 cgd }
1186 1.1 cgd
1187 1.1 cgd /*
1188 1.1 cgd * Foreach line in the conf table, open that file.
1189 1.1 cgd */
1190 1.1 cgd f = NULL;
1191 1.5 perry while (fgets(cline, sizeof(cline), cf) != NULL) {
1192 1.1 cgd /*
1193 1.1 cgd * check for end-of-section, comments, strip off trailing
1194 1.1 cgd * spaces and newline character.
1195 1.1 cgd */
1196 1.5 perry for (p = cline; isspace(*p); ++p)
1197 1.5 perry continue;
1198 1.10 pk if (*p == '\0' || *p == '#')
1199 1.1 cgd continue;
1200 1.5 perry for (p = strchr(cline, '\0'); isspace(*--p);)
1201 1.5 perry continue;
1202 1.1 cgd *++p = '\0';
1203 1.1 cgd f = (struct filed *)calloc(1, sizeof(*f));
1204 1.1 cgd *nextp = f;
1205 1.1 cgd nextp = &f->f_next;
1206 1.1 cgd cfline(cline, f);
1207 1.1 cgd }
1208 1.1 cgd
1209 1.1 cgd /* close the configuration file */
1210 1.5 perry (void)fclose(cf);
1211 1.1 cgd
1212 1.1 cgd Initialized = 1;
1213 1.1 cgd
1214 1.1 cgd if (Debug) {
1215 1.1 cgd for (f = Files; f; f = f->f_next) {
1216 1.1 cgd for (i = 0; i <= LOG_NFACILITIES; i++)
1217 1.1 cgd if (f->f_pmask[i] == INTERNAL_NOPRI)
1218 1.1 cgd printf("X ");
1219 1.1 cgd else
1220 1.1 cgd printf("%d ", f->f_pmask[i]);
1221 1.1 cgd printf("%s: ", TypeNames[f->f_type]);
1222 1.1 cgd switch (f->f_type) {
1223 1.1 cgd case F_FILE:
1224 1.1 cgd case F_TTY:
1225 1.1 cgd case F_CONSOLE:
1226 1.1 cgd printf("%s", f->f_un.f_fname);
1227 1.1 cgd break;
1228 1.1 cgd
1229 1.1 cgd case F_FORW:
1230 1.1 cgd printf("%s", f->f_un.f_forw.f_hname);
1231 1.1 cgd break;
1232 1.1 cgd
1233 1.1 cgd case F_USERS:
1234 1.34 lukem for (i = 0;
1235 1.34 lukem i < MAXUNAMES && *f->f_un.f_uname[i]; i++)
1236 1.1 cgd printf("%s, ", f->f_un.f_uname[i]);
1237 1.1 cgd break;
1238 1.1 cgd }
1239 1.1 cgd printf("\n");
1240 1.1 cgd }
1241 1.38 jwise }
1242 1.38 jwise
1243 1.38 jwise finet = socksetup(PF_UNSPEC);
1244 1.38 jwise if (finet) {
1245 1.38 jwise if (SecureMode) {
1246 1.38 jwise for (i = 0; i < *finet; i++) {
1247 1.38 jwise if (shutdown(finet[i+1], SHUT_RD) < 0) {
1248 1.47 manu logerror("shutdown() failed");
1249 1.38 jwise die(0);
1250 1.38 jwise }
1251 1.38 jwise }
1252 1.38 jwise } else
1253 1.47 manu dprintf("Listening on inet and/or inet6 socket\n");
1254 1.47 manu dprintf("Sending on inet and/or inet6 socket\n");
1255 1.1 cgd }
1256 1.1 cgd
1257 1.1 cgd logmsg(LOG_SYSLOG|LOG_INFO, "syslogd: restart", LocalHostName, ADDDATE);
1258 1.1 cgd dprintf("syslogd: restarted\n");
1259 1.1 cgd }
1260 1.1 cgd
1261 1.1 cgd /*
1262 1.1 cgd * Crack a configuration file line
1263 1.1 cgd */
1264 1.5 perry void
1265 1.53 wiz cfline(char *line, struct filed *f)
1266 1.1 cgd {
1267 1.30 itojun struct addrinfo hints, *res;
1268 1.30 itojun int error, i, pri;
1269 1.30 itojun char *bp, *p, *q;
1270 1.47 manu char buf[MAXLINE];
1271 1.47 manu int sp_err;
1272 1.1 cgd
1273 1.1 cgd dprintf("cfline(%s)\n", line);
1274 1.1 cgd
1275 1.1 cgd errno = 0; /* keep strerror() stuff out of logerror messages */
1276 1.1 cgd
1277 1.1 cgd /* clear out file entry */
1278 1.5 perry memset(f, 0, sizeof(*f));
1279 1.1 cgd for (i = 0; i <= LOG_NFACILITIES; i++)
1280 1.1 cgd f->f_pmask[i] = INTERNAL_NOPRI;
1281 1.47 manu
1282 1.47 manu /*
1283 1.47 manu * There should not be any space before the log facility.
1284 1.47 manu * Check this is okay, complain and fix if it is not.
1285 1.47 manu */
1286 1.47 manu q = line;
1287 1.47 manu if (isblank((unsigned char)*line)) {
1288 1.47 manu errno = 0;
1289 1.47 manu logerror(
1290 1.47 manu "Warning: `%s' space or tab before the log facility",
1291 1.47 manu line);
1292 1.47 manu /* Fix: strip all spaces/tabs before the log facility */
1293 1.47 manu while (*q++ && isblank((unsigned char)*q));
1294 1.47 manu line = q;
1295 1.47 manu }
1296 1.47 manu
1297 1.47 manu /*
1298 1.47 manu * q is now at the first char of the log facility
1299 1.47 manu * There should be at least one tab after the log facility
1300 1.47 manu * Check this is okay, and complain and fix if it is not.
1301 1.47 manu */
1302 1.47 manu q = line + strlen(line);
1303 1.47 manu while (!isblank((unsigned char)*q) && (q != line))
1304 1.47 manu q--;
1305 1.47 manu if ((q == line) && strlen(line)) {
1306 1.47 manu /* No tabs or space in a non empty line: complain */
1307 1.47 manu errno = 0;
1308 1.47 manu logerror(
1309 1.47 manu "Error: `%s' log facility or log target missing",
1310 1.47 manu line);
1311 1.47 manu }
1312 1.47 manu
1313 1.47 manu /* q is at the end of the blank between the two fields */
1314 1.47 manu sp_err = 0;
1315 1.47 manu while (isblank((unsigned char)*q) && (q != line))
1316 1.47 manu if (*q-- == ' ')
1317 1.47 manu sp_err = 1;
1318 1.47 manu
1319 1.47 manu if (sp_err) {
1320 1.47 manu /*
1321 1.47 manu * A space somewhere between the log facility
1322 1.47 manu * and the log target: complain
1323 1.47 manu */
1324 1.47 manu errno = 0;
1325 1.47 manu logerror(
1326 1.47 manu "Warning: `%s' space found where tab is expected",
1327 1.47 manu line);
1328 1.47 manu /* ... and fix the problem: replace all spaces by tabs */
1329 1.47 manu while (*++q && isblank((unsigned char)*q))
1330 1.47 manu if (*q == ' ')
1331 1.47 manu *q='\t';
1332 1.47 manu }
1333 1.1 cgd
1334 1.1 cgd /* scan through the list of selectors */
1335 1.1 cgd for (p = line; *p && *p != '\t';) {
1336 1.1 cgd
1337 1.1 cgd /* find the end of this facility name list */
1338 1.1 cgd for (q = p; *q && *q != '\t' && *q++ != '.'; )
1339 1.1 cgd continue;
1340 1.1 cgd
1341 1.1 cgd /* collect priority name */
1342 1.5 perry for (bp = buf; *q && !strchr("\t,;", *q); )
1343 1.1 cgd *bp++ = *q++;
1344 1.1 cgd *bp = '\0';
1345 1.1 cgd
1346 1.1 cgd /* skip cruft */
1347 1.5 perry while (strchr(", ;", *q))
1348 1.1 cgd q++;
1349 1.1 cgd
1350 1.1 cgd /* decode priority name */
1351 1.1 cgd if (*buf == '*')
1352 1.1 cgd pri = LOG_PRIMASK + 1;
1353 1.1 cgd else {
1354 1.1 cgd pri = decode(buf, prioritynames);
1355 1.1 cgd if (pri < 0) {
1356 1.47 manu errno = 0;
1357 1.47 manu logerror("Unknown priority name `%s'", buf);
1358 1.1 cgd return;
1359 1.1 cgd }
1360 1.1 cgd }
1361 1.1 cgd
1362 1.1 cgd /* scan facilities */
1363 1.5 perry while (*p && !strchr("\t.;", *p)) {
1364 1.5 perry for (bp = buf; *p && !strchr("\t,;.", *p); )
1365 1.1 cgd *bp++ = *p++;
1366 1.1 cgd *bp = '\0';
1367 1.1 cgd if (*buf == '*')
1368 1.1 cgd for (i = 0; i < LOG_NFACILITIES; i++)
1369 1.1 cgd f->f_pmask[i] = pri;
1370 1.1 cgd else {
1371 1.1 cgd i = decode(buf, facilitynames);
1372 1.1 cgd if (i < 0) {
1373 1.47 manu errno = 0;
1374 1.47 manu logerror("Unknown facility name `%s'",
1375 1.1 cgd buf);
1376 1.1 cgd return;
1377 1.1 cgd }
1378 1.1 cgd f->f_pmask[i >> 3] = pri;
1379 1.1 cgd }
1380 1.1 cgd while (*p == ',' || *p == ' ')
1381 1.1 cgd p++;
1382 1.1 cgd }
1383 1.1 cgd
1384 1.1 cgd p = q;
1385 1.1 cgd }
1386 1.1 cgd
1387 1.1 cgd /* skip to action part */
1388 1.47 manu sp_err = 0;
1389 1.47 manu while ((*p == '\t') || (*p == ' '))
1390 1.1 cgd p++;
1391 1.1 cgd
1392 1.1 cgd switch (*p)
1393 1.1 cgd {
1394 1.1 cgd case '@':
1395 1.5 perry (void)strcpy(f->f_un.f_forw.f_hname, ++p);
1396 1.30 itojun memset(&hints, 0, sizeof(hints));
1397 1.30 itojun hints.ai_family = AF_UNSPEC;
1398 1.30 itojun hints.ai_socktype = SOCK_DGRAM;
1399 1.30 itojun hints.ai_protocol = 0;
1400 1.34 lukem error = getaddrinfo(f->f_un.f_forw.f_hname, "syslog", &hints,
1401 1.34 lukem &res);
1402 1.30 itojun if (error) {
1403 1.30 itojun logerror(gai_strerror(error));
1404 1.1 cgd break;
1405 1.1 cgd }
1406 1.30 itojun f->f_un.f_forw.f_addr = res;
1407 1.1 cgd f->f_type = F_FORW;
1408 1.36 jwise NumForwards++;
1409 1.1 cgd break;
1410 1.1 cgd
1411 1.1 cgd case '/':
1412 1.5 perry (void)strcpy(f->f_un.f_fname, p);
1413 1.1 cgd if ((f->f_file = open(p, O_WRONLY|O_APPEND, 0)) < 0) {
1414 1.13 enami f->f_type = F_UNUSED;
1415 1.1 cgd logerror(p);
1416 1.1 cgd break;
1417 1.1 cgd }
1418 1.1 cgd if (isatty(f->f_file))
1419 1.1 cgd f->f_type = F_TTY;
1420 1.1 cgd else
1421 1.1 cgd f->f_type = F_FILE;
1422 1.1 cgd if (strcmp(p, ctty) == 0)
1423 1.1 cgd f->f_type = F_CONSOLE;
1424 1.1 cgd break;
1425 1.1 cgd
1426 1.1 cgd case '*':
1427 1.1 cgd f->f_type = F_WALL;
1428 1.1 cgd break;
1429 1.1 cgd
1430 1.1 cgd default:
1431 1.1 cgd for (i = 0; i < MAXUNAMES && *p; i++) {
1432 1.1 cgd for (q = p; *q && *q != ','; )
1433 1.1 cgd q++;
1434 1.5 perry (void)strncpy(f->f_un.f_uname[i], p, UT_NAMESIZE);
1435 1.1 cgd if ((q - p) > UT_NAMESIZE)
1436 1.1 cgd f->f_un.f_uname[i][UT_NAMESIZE] = '\0';
1437 1.1 cgd else
1438 1.1 cgd f->f_un.f_uname[i][q - p] = '\0';
1439 1.1 cgd while (*q == ',' || *q == ' ')
1440 1.1 cgd q++;
1441 1.1 cgd p = q;
1442 1.1 cgd }
1443 1.1 cgd f->f_type = F_USERS;
1444 1.1 cgd break;
1445 1.1 cgd }
1446 1.1 cgd }
1447 1.1 cgd
1448 1.1 cgd
1449 1.1 cgd /*
1450 1.1 cgd * Decode a symbolic name to a numeric value
1451 1.1 cgd */
1452 1.5 perry int
1453 1.53 wiz decode(const char *name, CODE *codetab)
1454 1.1 cgd {
1455 1.5 perry CODE *c;
1456 1.5 perry char *p, buf[40];
1457 1.1 cgd
1458 1.1 cgd if (isdigit(*name))
1459 1.1 cgd return (atoi(name));
1460 1.1 cgd
1461 1.5 perry for (p = buf; *name && p < &buf[sizeof(buf) - 1]; p++, name++) {
1462 1.5 perry if (isupper(*name))
1463 1.5 perry *p = tolower(*name);
1464 1.5 perry else
1465 1.5 perry *p = *name;
1466 1.5 perry }
1467 1.5 perry *p = '\0';
1468 1.1 cgd for (c = codetab; c->c_name; c++)
1469 1.1 cgd if (!strcmp(buf, c->c_name))
1470 1.1 cgd return (c->c_val);
1471 1.1 cgd
1472 1.1 cgd return (-1);
1473 1.15 leo }
1474 1.15 leo
1475 1.15 leo /*
1476 1.15 leo * Retrieve the size of the kernel message buffer, via sysctl.
1477 1.15 leo */
1478 1.15 leo int
1479 1.53 wiz getmsgbufsize(void)
1480 1.15 leo {
1481 1.15 leo int msgbufsize, mib[2];
1482 1.15 leo size_t size;
1483 1.15 leo
1484 1.15 leo mib[0] = CTL_KERN;
1485 1.15 leo mib[1] = KERN_MSGBUFSIZE;
1486 1.15 leo size = sizeof msgbufsize;
1487 1.15 leo if (sysctl(mib, 2, &msgbufsize, &size, NULL, 0) == -1) {
1488 1.47 manu dprintf("Couldn't get kern.msgbufsize\n");
1489 1.15 leo return (0);
1490 1.15 leo }
1491 1.15 leo return (msgbufsize);
1492 1.30 itojun }
1493 1.30 itojun
1494 1.31 itojun int *
1495 1.53 wiz socksetup(int af)
1496 1.30 itojun {
1497 1.31 itojun struct addrinfo hints, *res, *r;
1498 1.31 itojun int error, maxs, *s, *socks;
1499 1.56 itojun const int on = 1;
1500 1.35 jwise
1501 1.36 jwise if(SecureMode && !NumForwards)
1502 1.35 jwise return(NULL);
1503 1.30 itojun
1504 1.30 itojun memset(&hints, 0, sizeof(hints));
1505 1.30 itojun hints.ai_flags = AI_PASSIVE;
1506 1.30 itojun hints.ai_family = af;
1507 1.30 itojun hints.ai_socktype = SOCK_DGRAM;
1508 1.30 itojun error = getaddrinfo(NULL, "syslog", &hints, &res);
1509 1.30 itojun if (error) {
1510 1.30 itojun logerror(gai_strerror(error));
1511 1.30 itojun errno = 0;
1512 1.30 itojun die(0);
1513 1.30 itojun }
1514 1.31 itojun
1515 1.31 itojun /* Count max number of sockets we may open */
1516 1.34 lukem for (maxs = 0, r = res; r; r = r->ai_next, maxs++)
1517 1.34 lukem continue;
1518 1.56 itojun socks = malloc((maxs+1) * sizeof(int));
1519 1.31 itojun if (!socks) {
1520 1.47 manu logerror("Couldn't allocate memory for sockets");
1521 1.31 itojun die(0);
1522 1.31 itojun }
1523 1.31 itojun
1524 1.31 itojun *socks = 0; /* num of sockets counter at start of array */
1525 1.56 itojun s = socks + 1;
1526 1.31 itojun for (r = res; r; r = r->ai_next) {
1527 1.31 itojun *s = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
1528 1.31 itojun if (*s < 0) {
1529 1.47 manu logerror("socket() failed");
1530 1.56 itojun continue;
1531 1.56 itojun }
1532 1.56 itojun if (r->ai_family == AF_INET6 && setsockopt(*s, IPPROTO_IPV6,
1533 1.56 itojun IPV6_V6ONLY, &on, sizeof(on)) < 0) {
1534 1.56 itojun logerror("setsockopt(IPV6_V6ONLY) failed");
1535 1.56 itojun close(*s);
1536 1.31 itojun continue;
1537 1.31 itojun }
1538 1.37 jwise if (!SecureMode && bind(*s, r->ai_addr, r->ai_addrlen) < 0) {
1539 1.47 manu logerror("bind() failed");
1540 1.31 itojun close (*s);
1541 1.31 itojun continue;
1542 1.31 itojun }
1543 1.31 itojun
1544 1.31 itojun *socks = *socks + 1;
1545 1.31 itojun s++;
1546 1.31 itojun }
1547 1.31 itojun
1548 1.31 itojun if (*socks == 0) {
1549 1.31 itojun free (socks);
1550 1.31 itojun if(Debug)
1551 1.31 itojun return(NULL);
1552 1.31 itojun else
1553 1.31 itojun die(0);
1554 1.30 itojun }
1555 1.30 itojun if (res)
1556 1.30 itojun freeaddrinfo(res);
1557 1.30 itojun
1558 1.31 itojun return(socks);
1559 1.1 cgd }
1560