xref: /original-bsd/old/implogd/implogd.c (revision 4b9b56dc)
1 /*
2  * Copyright (c) 1983, 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 char copyright[] =
20 "@(#) Copyright (c) 1983, 1988 Regents of the University of California.\n\
21  All rights reserved.\n";
22 #endif /* not lint */
23 
24 #ifndef lint
25 static char sccsid[] = "@(#)implogd.c	5.8 (Berkeley) 05/11/89";
26 #endif /* not lint */
27 
28 #include <sys/param.h>
29 #include <sys/time.h>
30 #include <sys/socket.h>
31 #include <sys/syslog.h>
32 #include <sys/file.h>
33 
34 #include <net/if.h>
35 
36 #include <netinet/in.h>
37 #include <netimp/if_imp.h>
38 
39 #include <sgtty.h>
40 #include "pathnames.h"
41 
42 /*
43  * Socket address, internet style, with
44  * unused space taken by timestamp and packet
45  * size.
46  */
47 struct sockstamp {
48 	short	sin_family;
49 	u_short	sin_port;
50 	struct	in_addr sin_addr;
51 	time_t	sin_time;
52 	int	sin_len;
53 };
54 
55 main()
56 {
57 	register int len, log, s;
58 	struct sockstamp from;
59 	int fromlen;
60 	u_char request[1024];
61 	time_t time();
62 
63 	openlog("implogd", LOG_PID|LOG_ODELAY|LOG_PERROR, LOG_DAEMON);
64 	log = open(_PATH_IMPLOG, O_CREAT|O_WRONLY|O_APPEND, 0644);
65 	if (log < 0) {
66 		syslog(LOG_ERR, "%s: %m\n", _PATH_IMPLOG);
67 		exit(1);
68 	}
69 	from.sin_time = time((time_t *)NULL);
70 	from.sin_len = sizeof(time_t);
71 	(void)write(log, (char *)&from, sizeof(from));
72 	if ((s = socket(AF_IMPLINK, SOCK_RAW, 0)) < 0) {
73 		syslog(LOG_ERR, "socket: %m\n");
74 		exit(1);
75 	}
76 #ifndef DEBUG
77 	{
78 		register int i, tt;
79 
80 		if (fork())
81 			exit(0);
82 		for (i = 0; i < 10; i++)
83 			if (i != log && i != s)
84 				(void) close(i);
85 		(void) open("/", O_RDONLY, 0);
86 		(void) dup2(0, 1);
87 		(void) dup2(0, 2);
88 		tt = open(_PATH_TTY, O_RDWR, 0);
89 		if (tt > 0) {
90 			ioctl(tt, TIOCNOTTY, 0);
91 			(void)close(tt);
92 		}
93 	}
94 #endif
95 	for (fromlen = sizeof(from);;) {
96 		len = recvfrom(s, request, sizeof(request), 0,
97 		    &from, &fromlen);
98 		if (len < 0) {
99 			syslog(LOG_ERR, "recvfrom: %m\n");
100 			continue;
101 		}
102 		if (len == 0 || len > IMPMTU)		/* sanity */
103 			continue;
104 		from.sin_len = len;
105 		from.sin_time = time((time_t *)NULL);
106 		(void)write(log, (char *)&from, sizeof(from));
107 		(void)write(log, request, len);
108 	}
109 	/*NOTREACHED*/
110 }
111