xref: /csrg-svn/old/implogd/implogd.c (revision 34772)
121139Sdist /*
2*34772Sbostic  * Copyright (c) 1983, 1988 Regents of the University of California.
333457Skarels  * All rights reserved.
433457Skarels  *
533457Skarels  * Redistribution and use in source and binary forms are permitted
6*34772Sbostic  * provided that the above copyright notice and this paragraph are
7*34772Sbostic  * duplicated in all such forms and that any documentation,
8*34772Sbostic  * advertising materials, and other materials related to such
9*34772Sbostic  * distribution and use acknowledge that the software was developed
10*34772Sbostic  * by the University of California, Berkeley.  The name of the
11*34772Sbostic  * University may not be used to endorse or promote products derived
12*34772Sbostic  * from this software without specific prior written permission.
13*34772Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34772Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34772Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621139Sdist  */
1721139Sdist 
189198Ssam #ifndef lint
1921139Sdist char copyright[] =
20*34772Sbostic "@(#) Copyright (c) 1983, 1988 Regents of the University of California.\n\
2121139Sdist  All rights reserved.\n";
22*34772Sbostic #endif /* not lint */
236480Ssam 
2421139Sdist #ifndef lint
25*34772Sbostic static char sccsid[] = "@(#)implogd.c	5.6 (Berkeley) 06/18/88";
26*34772Sbostic #endif /* not lint */
2721139Sdist 
286480Ssam #include <sgtty.h>
299196Ssam 
3013601Ssam #include <sys/time.h>
319251Ssam #include <sys/param.h>
326480Ssam #include <sys/socket.h>
3329429Skarels #include <sys/syslog.h>
349251Ssam #include <sys/file.h>
356480Ssam 
3633456Skarels #include <net/if.h>
3733456Skarels 
389196Ssam #include <netinet/in.h>
399251Ssam #include <netimp/if_imp.h>
409196Ssam 
416480Ssam #define	LOGFILE	"/usr/adm/implog"
426480Ssam 
436480Ssam u_char	request[1024];
446480Ssam int	marktime();
456480Ssam int	options;
466480Ssam extern	int errno;
476480Ssam int	log;
486480Ssam 
496480Ssam /*
506480Ssam  * Socket address, internet style, with
516480Ssam  * unused space taken by timestamp and packet
526480Ssam  * size.
536480Ssam  */
546480Ssam struct sockstamp {
556480Ssam 	short	sin_family;
566480Ssam 	u_short	sin_port;
576480Ssam 	struct	in_addr sin_addr;
586480Ssam 	time_t	sin_time;
599196Ssam 	int	sin_len;
606480Ssam };
616480Ssam 
626480Ssam main(argc, argv)
636480Ssam 	char *argv[];
646480Ssam {
6529429Skarels 	int i, s;
666480Ssam 	time_t t;
679196Ssam 	struct sockstamp from;
686480Ssam 
696480Ssam 	argc--, argv++;
7029429Skarels 	openlog("implogd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
716480Ssam 	if (argc > 0 && !strcmp(argv[0], "-d"))
726480Ssam 		options |= SO_DEBUG;
7327728Skarels 	log = open(LOGFILE, O_CREAT|O_WRONLY|O_APPEND, 0644);
7427728Skarels 	if (log < 0) {
7529429Skarels 		syslog(LOG_ERR, "%s: %m\n", LOGFILE);
7627728Skarels 		perror("implogd: open");
7727728Skarels 		exit(1);
7827728Skarels 	}
7927728Skarels 	from.sin_time = time(0);
8027728Skarels 	from.sin_len = sizeof (time_t);
8127728Skarels 	write(log, (char *)&from, sizeof (from));
8227728Skarels 	if ((s = socket(AF_IMPLINK, SOCK_RAW, 0)) < 0) {
8329429Skarels 		syslog(LOG_ERR, "socket: %m\n");
8427728Skarels 		perror("implogd: socket");
8527728Skarels 		exit(5);
8627728Skarels 	}
878457Ssam #ifndef DEBUG
888457Ssam 	if (fork())
898457Ssam 		exit(0);
9029429Skarels 	for (i = 0; i < 10; i++)
9129429Skarels 		if (i != log && i != s)
9229429Skarels 			(void) close(i);
938457Ssam 	(void) open("/", 0);
948457Ssam 	(void) dup2(0, 1);
958457Ssam 	(void) dup2(0, 2);
968457Ssam 	{ int tt = open("/dev/tty", 2);
978457Ssam 	  if (tt > 0) {
988457Ssam 		ioctl(tt, TIOCNOTTY, 0);
998457Ssam 		close(tt);
1008457Ssam 	  }
1016480Ssam 	}
1028457Ssam #endif
1036480Ssam 	for (;;) {
10412237Ssam 		int fromlen = sizeof (from), len;
1059196Ssam 
10612237Ssam 		len = recvfrom(s, request, sizeof (request), 0,
10712237Ssam 			&from, &fromlen);
10812237Ssam 		if (len < 0) {
10929429Skarels 			syslog(LOG_ERR, "recvfrom: %m\n");
1109251Ssam 			perror("implogd: recvfrom");
1116480Ssam 			continue;
1129251Ssam 		}
11312237Ssam 		if (len == 0 || len > IMPMTU)	/* sanity */
1146480Ssam 			continue;
1159196Ssam 		from.sin_len = len;
1166480Ssam 		from.sin_time = time(0);
1179196Ssam 		write(log, (char *)&from, sizeof (from));
1189196Ssam 		write(log, request, len);
1196480Ssam 	}
1206480Ssam 	/*NOTREACHED*/
1216480Ssam }
122