xref: /csrg-svn/old/implogd/implogd.c (revision 34772)
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.6 (Berkeley) 06/18/88";
26 #endif /* not lint */
27 
28 #include <sgtty.h>
29 
30 #include <sys/time.h>
31 #include <sys/param.h>
32 #include <sys/socket.h>
33 #include <sys/syslog.h>
34 #include <sys/file.h>
35 
36 #include <net/if.h>
37 
38 #include <netinet/in.h>
39 #include <netimp/if_imp.h>
40 
41 #define	LOGFILE	"/usr/adm/implog"
42 
43 u_char	request[1024];
44 int	marktime();
45 int	options;
46 extern	int errno;
47 int	log;
48 
49 /*
50  * Socket address, internet style, with
51  * unused space taken by timestamp and packet
52  * size.
53  */
54 struct sockstamp {
55 	short	sin_family;
56 	u_short	sin_port;
57 	struct	in_addr sin_addr;
58 	time_t	sin_time;
59 	int	sin_len;
60 };
61 
62 main(argc, argv)
63 	char *argv[];
64 {
65 	int i, s;
66 	time_t t;
67 	struct sockstamp from;
68 
69 	argc--, argv++;
70 	openlog("implogd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
71 	if (argc > 0 && !strcmp(argv[0], "-d"))
72 		options |= SO_DEBUG;
73 	log = open(LOGFILE, O_CREAT|O_WRONLY|O_APPEND, 0644);
74 	if (log < 0) {
75 		syslog(LOG_ERR, "%s: %m\n", LOGFILE);
76 		perror("implogd: open");
77 		exit(1);
78 	}
79 	from.sin_time = time(0);
80 	from.sin_len = sizeof (time_t);
81 	write(log, (char *)&from, sizeof (from));
82 	if ((s = socket(AF_IMPLINK, SOCK_RAW, 0)) < 0) {
83 		syslog(LOG_ERR, "socket: %m\n");
84 		perror("implogd: socket");
85 		exit(5);
86 	}
87 #ifndef DEBUG
88 	if (fork())
89 		exit(0);
90 	for (i = 0; i < 10; i++)
91 		if (i != log && i != s)
92 			(void) close(i);
93 	(void) open("/", 0);
94 	(void) dup2(0, 1);
95 	(void) dup2(0, 2);
96 	{ int tt = open("/dev/tty", 2);
97 	  if (tt > 0) {
98 		ioctl(tt, TIOCNOTTY, 0);
99 		close(tt);
100 	  }
101 	}
102 #endif
103 	for (;;) {
104 		int fromlen = sizeof (from), len;
105 
106 		len = recvfrom(s, request, sizeof (request), 0,
107 			&from, &fromlen);
108 		if (len < 0) {
109 			syslog(LOG_ERR, "recvfrom: %m\n");
110 			perror("implogd: recvfrom");
111 			continue;
112 		}
113 		if (len == 0 || len > IMPMTU)	/* sanity */
114 			continue;
115 		from.sin_len = len;
116 		from.sin_time = time(0);
117 		write(log, (char *)&from, sizeof (from));
118 		write(log, request, len);
119 	}
120 	/*NOTREACHED*/
121 }
122