121139Sdist /* 2*33457Skarels * Copyright (c) 1983,1988 Regents of the University of California. 3*33457Skarels * All rights reserved. 4*33457Skarels * 5*33457Skarels * Redistribution and use in source and binary forms are permitted 6*33457Skarels * provided that this notice is preserved and that due credit is given 7*33457Skarels * to the University of California at Berkeley. The name of the University 8*33457Skarels * may not be used to endorse or promote products derived from this 9*33457Skarels * software without specific prior written permission. This software 10*33457Skarels * is provided ``as is'' without express or implied warranty. 1121139Sdist */ 1221139Sdist 139198Ssam #ifndef lint 1421139Sdist char copyright[] = 15*33457Skarels "@(#) Copyright (c) 1983,1988 Regents of the University of California.\n\ 1621139Sdist All rights reserved.\n"; 1721139Sdist #endif not lint 186480Ssam 1921139Sdist #ifndef lint 20*33457Skarels static char sccsid[] = "@(#)implogd.c 5.5 (Berkeley) 02/08/88"; 2121139Sdist #endif not lint 2221139Sdist 236480Ssam #include <sgtty.h> 249196Ssam 2513601Ssam #include <sys/time.h> 269251Ssam #include <sys/param.h> 276480Ssam #include <sys/socket.h> 2829429Skarels #include <sys/syslog.h> 299251Ssam #include <sys/file.h> 306480Ssam 3133456Skarels #include <net/if.h> 3233456Skarels 339196Ssam #include <netinet/in.h> 349251Ssam #include <netimp/if_imp.h> 359196Ssam 366480Ssam #define LOGFILE "/usr/adm/implog" 376480Ssam 386480Ssam u_char request[1024]; 396480Ssam int marktime(); 406480Ssam int options; 416480Ssam extern int errno; 426480Ssam int log; 436480Ssam 446480Ssam /* 456480Ssam * Socket address, internet style, with 466480Ssam * unused space taken by timestamp and packet 476480Ssam * size. 486480Ssam */ 496480Ssam struct sockstamp { 506480Ssam short sin_family; 516480Ssam u_short sin_port; 526480Ssam struct in_addr sin_addr; 536480Ssam time_t sin_time; 549196Ssam int sin_len; 556480Ssam }; 566480Ssam 576480Ssam main(argc, argv) 586480Ssam char *argv[]; 596480Ssam { 6029429Skarels int i, s; 616480Ssam time_t t; 629196Ssam struct sockstamp from; 636480Ssam 646480Ssam argc--, argv++; 6529429Skarels openlog("implogd", LOG_PID | LOG_ODELAY, LOG_DAEMON); 666480Ssam if (argc > 0 && !strcmp(argv[0], "-d")) 676480Ssam options |= SO_DEBUG; 6827728Skarels log = open(LOGFILE, O_CREAT|O_WRONLY|O_APPEND, 0644); 6927728Skarels if (log < 0) { 7029429Skarels syslog(LOG_ERR, "%s: %m\n", LOGFILE); 7127728Skarels perror("implogd: open"); 7227728Skarels exit(1); 7327728Skarels } 7427728Skarels from.sin_time = time(0); 7527728Skarels from.sin_len = sizeof (time_t); 7627728Skarels write(log, (char *)&from, sizeof (from)); 7727728Skarels if ((s = socket(AF_IMPLINK, SOCK_RAW, 0)) < 0) { 7829429Skarels syslog(LOG_ERR, "socket: %m\n"); 7927728Skarels perror("implogd: socket"); 8027728Skarels exit(5); 8127728Skarels } 828457Ssam #ifndef DEBUG 838457Ssam if (fork()) 848457Ssam exit(0); 8529429Skarels for (i = 0; i < 10; i++) 8629429Skarels if (i != log && i != s) 8729429Skarels (void) close(i); 888457Ssam (void) open("/", 0); 898457Ssam (void) dup2(0, 1); 908457Ssam (void) dup2(0, 2); 918457Ssam { int tt = open("/dev/tty", 2); 928457Ssam if (tt > 0) { 938457Ssam ioctl(tt, TIOCNOTTY, 0); 948457Ssam close(tt); 958457Ssam } 966480Ssam } 978457Ssam #endif 986480Ssam for (;;) { 9912237Ssam int fromlen = sizeof (from), len; 1009196Ssam 10112237Ssam len = recvfrom(s, request, sizeof (request), 0, 10212237Ssam &from, &fromlen); 10312237Ssam if (len < 0) { 10429429Skarels syslog(LOG_ERR, "recvfrom: %m\n"); 1059251Ssam perror("implogd: recvfrom"); 1066480Ssam continue; 1079251Ssam } 10812237Ssam if (len == 0 || len > IMPMTU) /* sanity */ 1096480Ssam continue; 1109196Ssam from.sin_len = len; 1116480Ssam from.sin_time = time(0); 1129196Ssam write(log, (char *)&from, sizeof (from)); 1139196Ssam write(log, request, len); 1146480Ssam } 1156480Ssam /*NOTREACHED*/ 1166480Ssam } 117