xref: /original-bsd/old/implogd/implogd.c (revision 7f3e12df)
1 /*
2  * Copyright (c) 1983, 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1983, 1988 Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)implogd.c	5.10 (Berkeley) 03/02/91";
16 #endif /* not lint */
17 
18 #include <sys/param.h>
19 #include <sys/time.h>
20 #include <sys/socket.h>
21 #include <sys/syslog.h>
22 #include <sys/file.h>
23 
24 #include <net/if.h>
25 
26 #include <netinet/in.h>
27 #include <netimp/if_imp.h>
28 
29 #include <sgtty.h>
30 #include "pathnames.h"
31 
32 /*
33  * Socket address, internet style, with
34  * unused space taken by timestamp and packet
35  * size.
36  */
37 struct sockstamp {
38 	short	sin_family;
39 	u_short	sin_port;
40 	struct	in_addr sin_addr;
41 	time_t	sin_time;
42 	int	sin_len;
43 };
44 
45 main()
46 {
47 	register int len, log, s;
48 	struct sockstamp from;
49 	int fromlen;
50 	u_char request[1024];
51 	time_t time();
52 
53 	openlog("implogd", LOG_PID|LOG_ODELAY|LOG_PERROR, LOG_DAEMON);
54 	log = open(_PATH_IMPLOG, O_CREAT|O_WRONLY|O_APPEND, 0644);
55 	if (log < 0) {
56 		syslog(LOG_ERR, "%s: %m\n", _PATH_IMPLOG);
57 		exit(1);
58 	}
59 	from.sin_time = time((time_t *)NULL);
60 	from.sin_len = sizeof(time_t);
61 	(void)write(log, (char *)&from, sizeof(from));
62 	if ((s = socket(AF_IMPLINK, SOCK_RAW, 0)) < 0) {
63 		syslog(LOG_ERR, "socket: %m\n");
64 		exit(1);
65 	}
66 #ifndef DEBUG
67 	{
68 		register int i, tt;
69 
70 		if (fork())
71 			exit(0);
72 		for (i = 0; i < 10; i++)
73 			if (i != log && i != s)
74 				(void) close(i);
75 		(void) open("/", O_RDONLY, 0);
76 		(void) dup2(0, 1);
77 		(void) dup2(0, 2);
78 		tt = open(_PATH_TTY, O_RDWR, 0);
79 		if (tt > 0) {
80 			ioctl(tt, TIOCNOTTY, 0);
81 			(void)close(tt);
82 		}
83 	}
84 #endif
85 	for (fromlen = sizeof(from);;) {
86 		len = recvfrom(s, request, sizeof(request), 0,
87 		    (struct sockaddr *)&from, &fromlen);
88 		if (len < 0) {
89 			syslog(LOG_ERR, "recvfrom: %m\n");
90 			continue;
91 		}
92 		if (len == 0 || len > IMPMTU)		/* sanity */
93 			continue;
94 		from.sin_len = len;
95 		from.sin_time = time((time_t *)NULL);
96 		(void)write(log, (char *)&from, sizeof(from));
97 		(void)write(log, request, len);
98 	}
99 	/*NOTREACHED*/
100 }
101