xref: /csrg-svn/old/implogd/implogd.c (revision 37965)
121139Sdist /*
234772Sbostic  * 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
634772Sbostic  * provided that the above copyright notice and this paragraph are
734772Sbostic  * duplicated in all such forms and that any documentation,
834772Sbostic  * advertising materials, and other materials related to such
934772Sbostic  * distribution and use acknowledge that the software was developed
1034772Sbostic  * by the University of California, Berkeley.  The name of the
1134772Sbostic  * University may not be used to endorse or promote products derived
1234772Sbostic  * from this software without specific prior written permission.
1334772Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434772Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534772Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621139Sdist  */
1721139Sdist 
189198Ssam #ifndef lint
1921139Sdist char copyright[] =
2034772Sbostic "@(#) Copyright (c) 1983, 1988 Regents of the University of California.\n\
2121139Sdist  All rights reserved.\n";
2234772Sbostic #endif /* not lint */
236480Ssam 
2421139Sdist #ifndef lint
25*37965Sbostic static char sccsid[] = "@(#)implogd.c	5.8 (Berkeley) 05/11/89";
2634772Sbostic #endif /* not lint */
2721139Sdist 
2837279Sbostic #include <sys/param.h>
2913601Ssam #include <sys/time.h>
306480Ssam #include <sys/socket.h>
3129429Skarels #include <sys/syslog.h>
329251Ssam #include <sys/file.h>
336480Ssam 
3433456Skarels #include <net/if.h>
3533456Skarels 
369196Ssam #include <netinet/in.h>
379251Ssam #include <netimp/if_imp.h>
389196Ssam 
3937279Sbostic #include <sgtty.h>
4037279Sbostic #include "pathnames.h"
416480Ssam 
426480Ssam /*
436480Ssam  * Socket address, internet style, with
446480Ssam  * unused space taken by timestamp and packet
456480Ssam  * size.
466480Ssam  */
476480Ssam struct sockstamp {
486480Ssam 	short	sin_family;
496480Ssam 	u_short	sin_port;
506480Ssam 	struct	in_addr sin_addr;
516480Ssam 	time_t	sin_time;
529196Ssam 	int	sin_len;
536480Ssam };
546480Ssam 
5537279Sbostic main()
566480Ssam {
5737279Sbostic 	register int len, log, s;
589196Ssam 	struct sockstamp from;
5937279Sbostic 	int fromlen;
6037279Sbostic 	u_char request[1024];
6137279Sbostic 	time_t time();
626480Ssam 
6337279Sbostic 	openlog("implogd", LOG_PID|LOG_ODELAY|LOG_PERROR, LOG_DAEMON);
6437279Sbostic 	log = open(_PATH_IMPLOG, O_CREAT|O_WRONLY|O_APPEND, 0644);
6527728Skarels 	if (log < 0) {
6637279Sbostic 		syslog(LOG_ERR, "%s: %m\n", _PATH_IMPLOG);
6727728Skarels 		exit(1);
6827728Skarels 	}
6937279Sbostic 	from.sin_time = time((time_t *)NULL);
7037279Sbostic 	from.sin_len = sizeof(time_t);
7137279Sbostic 	(void)write(log, (char *)&from, sizeof(from));
7227728Skarels 	if ((s = socket(AF_IMPLINK, SOCK_RAW, 0)) < 0) {
7329429Skarels 		syslog(LOG_ERR, "socket: %m\n");
7437279Sbostic 		exit(1);
7527728Skarels 	}
768457Ssam #ifndef DEBUG
7737279Sbostic 	{
7837279Sbostic 		register int i, tt;
7937279Sbostic 
8037279Sbostic 		if (fork())
8137279Sbostic 			exit(0);
8237279Sbostic 		for (i = 0; i < 10; i++)
8337279Sbostic 			if (i != log && i != s)
8437279Sbostic 				(void) close(i);
8537279Sbostic 		(void) open("/", O_RDONLY, 0);
8637279Sbostic 		(void) dup2(0, 1);
8737279Sbostic 		(void) dup2(0, 2);
88*37965Sbostic 		tt = open(_PATH_TTY, O_RDWR, 0);
8937279Sbostic 		if (tt > 0) {
9037279Sbostic 			ioctl(tt, TIOCNOTTY, 0);
9137279Sbostic 			(void)close(tt);
9237279Sbostic 		}
936480Ssam 	}
948457Ssam #endif
9537279Sbostic 	for (fromlen = sizeof(from);;) {
9637279Sbostic 		len = recvfrom(s, request, sizeof(request), 0,
9737279Sbostic 		    &from, &fromlen);
9812237Ssam 		if (len < 0) {
9929429Skarels 			syslog(LOG_ERR, "recvfrom: %m\n");
1006480Ssam 			continue;
1019251Ssam 		}
10237279Sbostic 		if (len == 0 || len > IMPMTU)		/* sanity */
1036480Ssam 			continue;
1049196Ssam 		from.sin_len = len;
10537279Sbostic 		from.sin_time = time((time_t *)NULL);
10637279Sbostic 		(void)write(log, (char *)&from, sizeof(from));
10737279Sbostic 		(void)write(log, request, len);
1086480Ssam 	}
1096480Ssam 	/*NOTREACHED*/
1106480Ssam }
111