xref: /original-bsd/old/implogd/implogd.c (revision 241757c4)
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 this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #ifndef lint
14 char copyright[] =
15 "@(#) Copyright (c) 1983,1988 Regents of the University of California.\n\
16  All rights reserved.\n";
17 #endif not lint
18 
19 #ifndef lint
20 static char sccsid[] = "@(#)implogd.c	5.5 (Berkeley) 02/08/88";
21 #endif not lint
22 
23 #include <sgtty.h>
24 
25 #include <sys/time.h>
26 #include <sys/param.h>
27 #include <sys/socket.h>
28 #include <sys/syslog.h>
29 #include <sys/file.h>
30 
31 #include <net/if.h>
32 
33 #include <netinet/in.h>
34 #include <netimp/if_imp.h>
35 
36 #define	LOGFILE	"/usr/adm/implog"
37 
38 u_char	request[1024];
39 int	marktime();
40 int	options;
41 extern	int errno;
42 int	log;
43 
44 /*
45  * Socket address, internet style, with
46  * unused space taken by timestamp and packet
47  * size.
48  */
49 struct sockstamp {
50 	short	sin_family;
51 	u_short	sin_port;
52 	struct	in_addr sin_addr;
53 	time_t	sin_time;
54 	int	sin_len;
55 };
56 
57 main(argc, argv)
58 	char *argv[];
59 {
60 	int i, s;
61 	time_t t;
62 	struct sockstamp from;
63 
64 	argc--, argv++;
65 	openlog("implogd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
66 	if (argc > 0 && !strcmp(argv[0], "-d"))
67 		options |= SO_DEBUG;
68 	log = open(LOGFILE, O_CREAT|O_WRONLY|O_APPEND, 0644);
69 	if (log < 0) {
70 		syslog(LOG_ERR, "%s: %m\n", LOGFILE);
71 		perror("implogd: open");
72 		exit(1);
73 	}
74 	from.sin_time = time(0);
75 	from.sin_len = sizeof (time_t);
76 	write(log, (char *)&from, sizeof (from));
77 	if ((s = socket(AF_IMPLINK, SOCK_RAW, 0)) < 0) {
78 		syslog(LOG_ERR, "socket: %m\n");
79 		perror("implogd: socket");
80 		exit(5);
81 	}
82 #ifndef DEBUG
83 	if (fork())
84 		exit(0);
85 	for (i = 0; i < 10; i++)
86 		if (i != log && i != s)
87 			(void) close(i);
88 	(void) open("/", 0);
89 	(void) dup2(0, 1);
90 	(void) dup2(0, 2);
91 	{ int tt = open("/dev/tty", 2);
92 	  if (tt > 0) {
93 		ioctl(tt, TIOCNOTTY, 0);
94 		close(tt);
95 	  }
96 	}
97 #endif
98 	for (;;) {
99 		int fromlen = sizeof (from), len;
100 
101 		len = recvfrom(s, request, sizeof (request), 0,
102 			&from, &fromlen);
103 		if (len < 0) {
104 			syslog(LOG_ERR, "recvfrom: %m\n");
105 			perror("implogd: recvfrom");
106 			continue;
107 		}
108 		if (len == 0 || len > IMPMTU)	/* sanity */
109 			continue;
110 		from.sin_len = len;
111 		from.sin_time = time(0);
112 		write(log, (char *)&from, sizeof (from));
113 		write(log, request, len);
114 	}
115 	/*NOTREACHED*/
116 }
117