home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / unix / wizards / 3607 < prev    next >
Encoding:
Text File  |  1992-08-13  |  4.0 KB  |  164 lines

  1. Newsgroups: comp.unix.wizards
  2. Path: sparky!uunet!usc!sol.ctr.columbia.edu!sal.wisc.edu!rat!zeus!kwang
  3. From: kwang@zeus.calpoly.edu (Kevin Wang aka The Scarecrow)
  4. Subject: Re: Utilities sought...
  5. Message-ID: <1992Aug14.064223.132692@zeus.calpoly.edu>
  6. Organization: The Outland Riders
  7. References: <51742@dime.cs.umass.edu>
  8. Date: Fri, 14 Aug 1992 06:42:23 GMT
  9. Lines: 153
  10.  
  11. In article <51742@dime.cs.umass.edu> somebody writes:
  12. >Reply-To: heller@.cs.umass.edu
  13.  
  14. AUGH!  that's ?????.cs.umass.edu
  15.  
  16. >I am looking for a couple of utilities to deal with some problems I am
  17. >having:
  18. >
  19. >The first problem is minor, but anoying.  It seems that /etc/utmp
  20. >sometimes gets out of sync with reality.  That it, finger, who, and w
  21. >(which all read this file) report users as being logged in who are not
  22. >really logged in.  This seems to be caused by the various programs that
  23. >create "terminals" and put entries in /etc/utmp, not always managing to
  24. >clean up after themselves (usually when these programs are killed off in
  25. >some abrupt fashion).  Is there a utility which will "fix" /etc/utmp by
  26. >removing records that are bogus.
  27.  
  28. I don't know if this'll work generically, but try this code
  29. all it does is "simulate" a telnet connection.  it doesn't actually
  30. telnet into the machine, but at least for us, it causes the utmps to
  31. be "prepared" for someone logging into a tty port.
  32.  
  33.    - Kevin Wang
  34. ...And now, for your local station identification, on the hour, every hour...
  35. kwang@hermes.calpoly.edu or kwang@gauss.elee.calpoly.edu
  36. ---
  37.                       The Twelve Commandments of Love:
  38.    X. Thou shalt not be a revealer of love affairs.
  39. ----- 8< ----- cut here ----- 8< -----
  40. /* ttyclear.c
  41. *  this program will "trick" the system into thinking that someone is
  42. *  telnetting into the system to login.  Since I do 16 of these simultaneously,
  43. *  the "null-and-void" utmp entries will disappear.
  44. *
  45. *  Intial Version Started [ 08/08/92 ]
  46. *  Finished [ 08/08/92 ]
  47. *  Ver 1.0 - KJW - basic functionality
  48. */
  49. #include <stdio.h>
  50. #include <errno.h> 
  51. #include <netdb.h> 
  52. #include <sys/types.h> 
  53. #include <sys/socket.h> 
  54. #include <netinet/in.h> 
  55. #include <unistd.h>
  56.  
  57. #define TELNET_PORT 23
  58. #define SOCKET_DOMAIN AF_INET
  59. #define SOCKET_TYPE SOCK_STREAM
  60. #define SOCKET_PROTOCOL 0
  61.  
  62. #define TELNET_INIT "\377\375\001\377\375\003\377\374\030"  /* telnet init sequence. REQUIRED, else nothing happens */
  63.  
  64. #define MAXOPEN 16
  65.  
  66. typedef struct sockaddr inetsockaddr;
  67.  
  68. /****************************/
  69.  
  70. inetsockaddr *HostnameToinetsockaddr(char *hostname, int portnum)
  71. {
  72.     static struct sockaddr_in addr;
  73.     struct hostent *host;
  74.     
  75.     host = gethostbyname(hostname);
  76.     if (NULL == host)
  77.         {
  78.         perror("Resolver");
  79.         exit(1);
  80.         }
  81.     
  82.     bzero((char *) &addr, sizeof(addr));
  83.     bcopy(host->h_addr, (char *) &(addr.sin_addr), host->h_length);
  84.     addr.sin_family = SOCKET_DOMAIN;
  85.     addr.sin_port = htons(portnum);
  86.     
  87.     return((inetsockaddr *)&addr);
  88. }
  89.  
  90. /****************************/
  91.  
  92. /****************************/
  93.  
  94. int ConnectSocket(int sock, char *hostname)
  95. {
  96.     int retval;
  97.     int portnum=0;
  98.  
  99.     portnum = TELNET_PORT;
  100.  
  101.     retval = connect(sock, HostnameToinetsockaddr(hostname, portnum), sizeof(struct sockaddr_in));
  102.     if (-1 == retval && errno != ECONNREFUSED)
  103.         {
  104.         perror("ConnectSocket");
  105.         exit(1);
  106.         }
  107.     return(retval);
  108. }
  109.  
  110. /****************************/
  111.  
  112. int OpenSocket(void)
  113. {
  114.     int sock;
  115.  
  116.     sock = socket(SOCKET_DOMAIN, SOCKET_TYPE, SOCKET_PROTOCOL);
  117.     if (-1 == sock)
  118.         {
  119.         perror("OpenSocket");
  120.         exit(1);
  121.         }
  122.     return(sock);
  123. }
  124.  
  125. /****************************/
  126.  
  127. main(int argc, char *argv[])
  128. {
  129.     int loop;
  130.     int hold[MAXOPEN];
  131.     int retval;
  132.     
  133.     signal(SIGQUIT, SIG_IGN);
  134.     signal(SIGINT, SIG_IGN);
  135.     signal(SIGTERM, SIG_IGN);
  136.  
  137.     if (argc != 2)
  138.         {
  139.         puts("Usage:  %s <hostname> \n");
  140.         exit(5);
  141.         }
  142.     loop=-1;
  143.     retval = 0;
  144.     while (loop < MAXOPEN && retval != -1)
  145.         {
  146.         loop++;
  147.         hold[loop] = OpenSocket();
  148.         retval = ConnectSocket(hold[loop], argv[1]);
  149.         if (-1 == retval)
  150.             {
  151.             fprintf(stderr, "Connection refused...\n");
  152.             }
  153.         else{
  154.             write(hold[loop], TELNET_INIT, strlen(TELNET_INIT));
  155.             }
  156.         }
  157.     /*sleep(10);*/
  158.     while (loop >= 0)
  159.         {
  160.         close(hold[loop]);
  161.         loop--;
  162.         }
  163.     exit(0);
  164.