From: kwang@zeus.calpoly.edu (Kevin Wang aka The Scarecrow)
Subject: Re: Utilities sought...
Organization: The Outland Riders
Date: Fri, 14 Aug 1992 06:42:23 GMT

In article <51742@dime.cs.umass.edu> somebody writes:
>Reply-To: heller@.cs.umass.edu

AUGH!  that's ?????.cs.umass.edu

>I am looking for a couple of utilities to deal with some problems I am
>having:
>
>The first problem is minor, but anoying.  It seems that /etc/utmp
>sometimes gets out of sync with reality.  That it, finger, who, and w
>(which all read this file) report users as being logged in who are not
>really logged in.  This seems to be caused by the various programs that
>create "terminals" and put entries in /etc/utmp, not always managing to
>clean up after themselves (usually when these programs are killed off in
>some abrupt fashion).  Is there a utility which will "fix" /etc/utmp by
>removing records that are bogus.

I don't know if this'll work generically, but try this code
all it does is "simulate" a telnet connection.  it doesn't actually
telnet into the machine, but at least for us, it causes the utmps to
be "prepared" for someone logging into a tty port.

   - Kevin Wang
...And now, for your local station identification, on the hour, every hour...
kwang@hermes.calpoly.edu or kwang@gauss.elee.calpoly.edu
---
                      The Twelve Commandments of Love:
   X. Thou shalt not be a revealer of love affairs.
----- 8< ----- cut here ----- 8< -----
/* ttyclear.c
*  this program will "trick" the system into thinking that someone is
*  telnetting into the system to login.  Since I do 16 of these simultaneously,
*  the "null-and-void" utmp entries will disappear.
*
*  Intial Version Started [ 08/08/92 ]
*  Finished [ 08/08/92 ]
*  Ver 1.0 - KJW - basic functionality
*/
#include <stdio.h>
#include <errno.h> 
#include <netdb.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 
#include <unistd.h>

#define TELNET_PORT 23
#define SOCKET_DOMAIN AF_INET
#define SOCKET_TYPE SOCK_STREAM
#define SOCKET_PROTOCOL 0

#define TELNET_INIT "\377\375\001\377\375\003\377\374\030"  /* telnet init sequence. REQUIRED, else nothing happens */

#define MAXOPEN 16

typedef struct sockaddr inetsockaddr;

/****************************/

inetsockaddr *HostnameToinetsockaddr(char *hostname, int portnum)
{
	static struct sockaddr_in addr;
	struct hostent *host;
	
	host = gethostbyname(hostname);
	if (NULL == host)
		{
		perror("Resolver");
		exit(1);
		}
	
	bzero((char *) &addr, sizeof(addr));
	bcopy(host->h_addr, (char *) &(addr.sin_addr), host->h_length);
	addr.sin_family = SOCKET_DOMAIN;
	addr.sin_port = htons(portnum);
	
	return((inetsockaddr *)&addr);
}

/****************************/

/****************************/

int ConnectSocket(int sock, char *hostname)
{
	int retval;
	int portnum=0;

	portnum = TELNET_PORT;

	retval = connect(sock, HostnameToinetsockaddr(hostname, portnum), sizeof(struct sockaddr_in));
	if (-1 == retval && errno != ECONNREFUSED)
		{
		perror("ConnectSocket");
		exit(1);
		}
	return(retval);
}

/****************************/

int OpenSocket(void)
{
	int sock;

	sock = socket(SOCKET_DOMAIN, SOCKET_TYPE, SOCKET_PROTOCOL);
	if (-1 == sock)
		{
		perror("OpenSocket");
		exit(1);
		}
	return(sock);
}

/****************************/

main(int argc, char *argv[])
{
	int loop;
	int hold[MAXOPEN];
	int retval;
	
	signal(SIGQUIT, SIG_IGN);
	signal(SIGINT, SIG_IGN);
	signal(SIGTERM, SIG_IGN);

	if (argc != 2)
		{
		puts("Usage:  %s <hostname> \n");
		exit(5);
		}
	loop=-1;
	retval = 0;
	while (loop < MAXOPEN && retval != -1)
		{
		loop++;
		hold[loop] = OpenSocket();
		retval = ConnectSocket(hold[loop], argv[1]);
		if (-1 == retval)
			{
			fprintf(stderr, "Connection refused...\n");
			}
		else{
			write(hold[loop], TELNET_INIT, strlen(TELNET_INIT));
			}
		}
	/*sleep(10);*/
	while (loop >= 0)
		{
		close(hold[loop]);
		loop--;
		}
	exit(0);
Newsgroups: comp.unix.wizards