From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10669a,25457602a18c64ce X-Google-Attributes: gid10669a,public X-Google-Thread: fa528,25457602a18c64ce X-Google-Attributes: gidfa528,public X-Google-ArrivalTime: 2002-07-17 13:17:26 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news.ems.psu.edu!news.cis.ohio-state.edu!news.maxwell.syr.edu!newsswitch.lcs.mit.edu!snoopy.risq.qc.ca!newsflash.concordia.ca!not-for-mail From: syl@alcor.concordia.ca (Sylvain Robitaille) Newsgroups: comp.unix.tru64,comp.os.linux.networking Subject: Re: Associate standard tty devices with ip adrs? Date: 17 Jul 2002 20:17:25 GMT Organization: Concordia University, Montreal, Canada Message-ID: References: <4687034c.0207170756.69da076b@posting.google.com> NNTP-Posting-Host: alcor.concordia.ca NNTP-Posting-User: syl User-Agent: slrn/0.9.5.7 (UNIX) Xref: archiver1.google.com comp.unix.tru64:8162 comp.os.linux.networking:102084 On 17 Jul 2002 08:56:27 -0700, Martin Ejdestig wrote: > Is it possible to associate a /dev/* file with an ip address and port. Check out logutmpd. It might be sufficient to get the information you're after: http://home.inreach.com/jaguiar/logutmpd-1.1.tgz I use that program with the appended patch. I hope this helps. -- ---------------------------------------------------------------------- Sylvain Robitaille syl@alcor.concordia.ca Systems analyst Concordia University Instructional & Information Technology Montreal, Quebec, Canada ---------------------------------------------------------------------- diff -uwr Makefile.original Makefile --- Makefile.original Tue Jan 2 12:24:47 2001 +++ Makefile Fri Jul 27 16:13:45 2001 @@ -15,6 +15,7 @@ # Linux # Alt_Linux # OSF +# NetBSD # # To set syslog priority of messages: # UT_FACILITY=????? diff -uwr function.c.original function.c --- function.c.original Tue Jan 2 12:18:41 2001 +++ function.c Fri Jul 27 16:17:28 2001 @@ -1,4 +1,5 @@ #include "logutmpd.h" +#define PID_FILE "/var/run/logutmpd.pid" /*------------------- subroutines ------------------------------*/ @@ -34,11 +35,25 @@ void custom_utmpname( char* filename) { - fd = open( filename, O_RDWR); + fd = open( filename, O_RDONLY); + if( fd < 0 ) { + fprintf(stderr, "could not open file %s", filename); + perror(""); + exit( -1); + } lseek( fd, 0, SEEK_SET); } /*--------------------------------------------------------------*/ +/* sub custom_endutent() - close utmp file: */ +/*--------------------------------------------------------------*/ + +void custom_endutent( void) +{ + close( fd); +} + +/*--------------------------------------------------------------*/ /* sub custom_getutent() - get utmp record */ /*--------------------------------------------------------------*/ @@ -65,6 +80,20 @@ } #endif + +/*--------------------------------------------------------------*/ +/* sub exit_cleaner() - cleans up when the process exits */ +/*--------------------------------------------------------------*/ + +static void exit_cleaner( void) +{ + if( unlink( PID_FILE) < 0) { + fprintf( stderr, "Unable to remove file %s", PID_FILE); + perror(""); + } +} + + /*--------------------------------------------------------------*/ /* sub daemon_init() - make child a little daemon */ /*--------------------------------------------------------------*/ @@ -72,11 +101,33 @@ int daemon_init() { pid_t pid; + int fd; + char mypid[12]; /* It's going to hold a PID */ + + /* First create our pid file */ + fd = open(PID_FILE,O_CREAT|O_WRONLY|O_EXCL,0666); + if( fd < 0 ) { + fprintf(stderr, "Error creating %s", PID_FILE); + perror(""); + exit(-1); + } + if ( ( pid = fork()) < 0) { return( -1); } else if ( pid != 0) { + /* Parent -- Write the PID and exit */ + sprintf(mypid, "%d", pid); + write(fd, mypid, strlen(mypid)); + close( fd); exit( 0); } + /* We can close the PID file in the child as well: */ + close( fd); + /* Child needs to set up exit handler */ + if( atexit(exit_cleaner) != 0) { + fprintf(stderr, "Error registering exit_cleaner()!\n"); + } + freopen( "/dev/null", "a", stdout); freopen( "/dev/null", "a", stderr); setsid(); @@ -97,10 +148,10 @@ } /*--------------------------------------------------------------*/ -/* sub sig_handle() - generic signal handler */ +/* sub sig_handler() - generic signal handler */ /*--------------------------------------------------------------*/ -static void sig_handle( int signo) +static void sig_handler( int signo) { close( fd); ENDUTENT(); @@ -111,6 +162,30 @@ /*--------------------------------------------------------------*/ +/* sub hup_handler() - HUP signal handler */ +/* */ +/* Closes just the wtmp file upon receipt of HUP signal. */ +/* The next call to getutent() will cause the file to be */ +/* reopened. */ +/*--------------------------------------------------------------*/ + +static void hup_handler( int signo) +{ + fprintf( stderr, "signal %d received, closing wtmp file\n",signo ); + syslog( UT_FACILITY | UT_LEVEL, "signal %d received, closing wtmp file.", + signo ); + ENDUTENT(); +# ifdef CUSTOM + /* + * On systems which use the custom_* calls, we need to reopen the + * wtmp file: + */ + UTMPNAME( utmpfile); +# endif +} + + +/*--------------------------------------------------------------*/ /* sub timestr() - return a formated time string */ /*--------------------------------------------------------------*/ diff -uwr logutmpd.c.original logutmpd.c --- logutmpd.c.original Tue Jan 2 12:18:58 2001 +++ logutmpd.c Fri Jul 27 16:20:21 2001 @@ -2,9 +2,19 @@ *--------------------------------------------------------------------* Filename: logutmpd.c Date: 03/25/2000 - Filename: Joe Aguiar + Author: Joe Aguiar Descr: Forward all utmp/wtmp/utmpx/wtmpx entries to the syslog or a specified text file. + Modifications: 07/10/2001 Sylvain Robitaille: Add entry type + descriptions for those entries which don't + have a user name (DEAD_PROCESS, etc.) + 07/26/2001 Sylvain Robitaille: Add a signal + handler to close and re-open wtmp file upon + receipt of HUP signal; write a pid file. + 07/27/2001 Sylvain Robitaille: Catch the TERM + signals, so that the pid file is cleaned up + when we're killed; Now works on NetBSD. + Copyright(c)1999 Joe Aguiar. All rights reserved. This program is free software *--------------------------------------------------------------------* @@ -24,15 +34,48 @@ int i, nfds, sfd, k; fd_set rfds; time_t us; - char seconds[11], utmpfile[PATH_MAX], filename[PATH_MAX]; + char seconds[11], filename[PATH_MAX]; +# ifndef CUSTOM + /* + * NOTE: for systems using custom_*() routines, this is globally + * defined. + */ + char utmpfile[PATH_MAX]; +# endif int debug, log; - UTMP_TYPE *myu; + UTMP_TYPE *u; + /* Entry type descriptions */ + char *types[] = {"EMPTY","RUN_LVL","BOOT_TIME","OLD_TIME","NEW_TIME", + "INIT_PROCESS","LOGIN_PROCESS","USER_PROCESS", + "DEAD_PROCESS","ACCOUNTING","PROCESSOR"}; + struct sigaction sh; debug = FALSE; log = TRUE; us = 0; strcpy( utmpfile, XTMP_FILE); + /* Setup signal handlers */ + sh.sa_flags = SA_RESTART; + sigemptyset(&sh.sa_mask); + sh.sa_handler = hup_handler; + if( sigaction(SIGHUP, &sh, NULL) ) { + /* Something's wrong! */ + fprintf(stderr, "Error setting signal handler: "); + perror(""); + exit(-1); + } + sh.sa_flags = SA_RESTART; + sigemptyset(&sh.sa_mask); + sh.sa_handler = sig_handler; + if( sigaction(SIGTERM, &sh, NULL) ) { + /* Something's wrong! */ + fprintf(stderr, "Error setting signal handler: "); + perror(""); + exit(-1); + } + + /* Read command line args */ for ( i = 1; i < argc; ++i) { if ( 0 == strncmp( "-f", argv[i], 3)) { @@ -90,8 +133,14 @@ setuid( NOBODY); /* read until end of file and print to screen */ - while ( ( (UTMP_TYPE*)myu = GETUTENT()) != NULL ) { - if ( debug == TRUE) { tostderr( myu);} + while ( ( (UTMP_TYPE*)u = GETUTENT()) != NULL ) { +# ifndef NetBSD + /* NOTE: NetBSD doesn't have a ut_type element */ + if ( ! strlen(u->ut_user) ) { + strcpy(u->ut_user, types[u->ut_type]); + } +# endif + if ( debug == TRUE) { tostderr( u);} } /* fudge to make custom_... subs work right */ @@ -113,9 +162,9 @@ nfds = select( fd + 1, &rfds, (fd_set*)NULL, (fd_set*)NULL, (struct timeval*)NULL); if ( FD_ISSET( fd, &rfds)) { - if ( ( (UTMP_TYPE*)myu = GETUTENT()) != NULL) { + if ( ( (UTMP_TYPE*)u = GETUTENT()) != NULL) { if ( debug == TRUE) { - tostdout( (UTMP_TYPE*)myu); + tostdout( (UTMP_TYPE*)u); } if ( log == TRUE) { syslog( UT_LEVEL, T_UT_FORMAT, T_OLIST); @@ -123,9 +172,19 @@ } } #else - while ( ( (UTMP_TYPE*)myu = GETUTENT()) != NULL) { + while ( ( (UTMP_TYPE*)u = GETUTENT()) != NULL) { +# ifndef NetBSD + /* NOTE: NetBSD doesn't have a ut_type element */ + /* + * If we don't have a user name, fill in at least with a + * description. + */ + if ( ! strlen(u->ut_user) ) { + strcpy(u->ut_user, types[u->ut_type]); + } +# endif if ( debug == TRUE) { - tostdout( (UTMP_TYPE*)myu); + tostdout( (UTMP_TYPE*)u); } if ( log == TRUE) { syslog( UT_LEVEL, T_UT_FORMAT, T_OLIST); diff -uwr logutmpd.h.original logutmpd.h --- logutmpd.h.original Tue Jan 2 12:19:08 2001 +++ logutmpd.h Fri Jul 27 16:21:46 2001 @@ -2,9 +2,12 @@ *--------------------------------------------------------------------* Filename: logutmpd.h Date: 11/21/2000 - Filename: Joe Aguiar + Author: Joe Aguiar Descr: Forward all utmp/wtmp/utmpx/wtmpx entries to the syslog or a specified text file. + Modifications: 07/27/2001 Sylvain Robitaille: OSF/DU-4.0x, + NetBSD + Copyright(c)1999 Joe Aguiar. All rights reserved. This program is free software *--------------------------------------------------------------------* @@ -24,6 +27,7 @@ #include #include #include +#include /* Just in case */ #ifndef TRUE #define TRUE 1 @@ -160,7 +164,7 @@ #endif #endif -/* Works with old DEC Unix, others?*/ +/* Works with old DEC Unix, others? 4.0d, 4.0e, 4.0f, 4.0g */ #ifdef OSF #include @@ -181,10 +185,64 @@ #define SETUTENT setutent #define GETUTENT getutent #define UTMPNAME utmpname +#ifndef PUTUTENT +#define PUTUTENT pututline +#endif #define ENDUTENT endutent #ifndef NOBODY #define NOBODY 60001 #endif +/* + * 2001/07/10 Sylvain Robitaille: struct element sizes from utmp.h: + */ +#ifndef UT_LINESIZE +#define UT_LINESIZE 32 +#endif +#ifndef UT_HOSTSIZE +#define UT_HOSTSIZE 64 +#endif +#ifndef UT_NAMESIZE +#define UT_NAMESIZE 32 +#endif +#ifndef UT_UNKNOWN +#define UT_UNKNOWN EMPTY +#endif +#endif + +/* NetBSD -- simple utmp */ + +#ifdef NetBSD +#include +#define XTMP_FILE _PATH_WTMP +#define OLIST u->ut_name, u->ut_line, u->ut_host +/* + * Note: on our NetBSD-1.5 systems, ut_host seems to not be + * nul-terminated, resulting in extraneous characters being written + * following ut_host in the output. We can fix that by ensuring + * that only UT_HOSTSIZE characters are written, (a way to do this + * without hard-coding the actual constant in here would be really + * nice!) + */ +#define UT_FORMAT "%s, %s, %0.16s\n" +#ifdef UT_TIME +#define T_OLIST timestr(u->ut_time), u->ut_name, u->ut_line, u->ut_host +#define T_UT_FORMAT "%s: %s, %s, %0.16s\n" +#else +#define T_OLIST OLIST +#define T_UT_FORMAT UT_FORMAT +#endif +#define UTMP_TYPE struct utmp +#define SETUTENT custom_setutent +#define GETUTENT custom_getutent +#define UTMPNAME custom_utmpname +#define ENDUTENT custom_endutent +#define PUTUTENT custom_pututent +#ifndef NOBODY +#define NOBODY 60001 +#define ut_user ut_name +/* need to use custom accessor subs */ +#define CUSTOM +#endif #endif /* Log Priority to use */ @@ -207,6 +265,8 @@ UTMP_TYPE* custom_getutent(); void custom_setutent(); void custom_pututent( UTMP_TYPE*); +void custom_endutent(); +char utmpfile[PATH_MAX]; #endif UTMP_TYPE ut, *u = &ut; diff -uwr ulogger.c.original ulogger.c --- ulogger.c.original Tue Jan 2 12:19:58 2001 +++ ulogger.c Fri Jul 27 15:28:14 2001 @@ -65,10 +65,11 @@ strncpy( ut.ut_line, line, UT_LINESIZE); strncpy( ut.ut_user, username, UT_NAMESIZE); strncpy( ut.ut_host, hostname, UT_HOSTSIZE); - ut.ut_pid = getpid(); time( &ut.ut_time); - +# ifndef NetBSD + ut.ut_pid = getpid(); ut.ut_type = UT_UNKNOWN; +# endif PUTUTENT( &ut); ENDUTENT();