home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / NETWORK / TEL23SRC.ZIP / FINGER / FINGER.C
Encoding:
C/C++ Source or Header  |  1991-06-27  |  9.5 KB  |  342 lines

  1. /*
  2.     finger.c
  3.     Provides a finger client services for NCSA 2.3.
  4.         Reference: RFC 742
  5.  
  6.     By James Nau, College of Engineering,
  7.     University of Nebraska--Lincoln
  8. */
  9.  
  10. #define FINGER_PORT 79
  11.  
  12. #define ALARM 128
  13.  
  14. #ifdef __TURBOC__
  15. #include "turboc.h"
  16. #endif
  17. #include <stdio.h>
  18. #include <conio.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <dos.h>
  22. #include <ctype.h>
  23. #ifdef MSC
  24. #include <signal.h>
  25. #include <time.h>
  26. #endif
  27.  
  28.  
  29. #ifdef MEMORY_DEBUG
  30. #include "memdebug.h"
  31. #endif
  32. #include "netevent.h"
  33. #include "hostform.h"
  34. #include "whatami.h"
  35. #include "externs.h"
  36.  
  37. #include "netutils.h"
  38.  
  39. int debug = 0;          /* enable with -D option */
  40. int timeout=300;        /* timeout period in clicks */
  41. int bypass_passwd=0;    /* whether to bypass the password check, not used */
  42.  
  43. unsigned char path_name[_MAX_DRIVE+_MAX_DIR],        /* character storage for the path name */
  44.     temp_str[20],buf[_MAX_DIR],temp_data[30];
  45.  
  46. /* Function Prototypes */
  47. int main(int argc, char *argv[]);
  48. #ifndef __TURBOC__
  49. static void randomize(void );
  50. #endif
  51. void usage(void);
  52. int index (char *string, char ch);
  53. void do_finger(char *host, char *line);
  54. void filter(char *buffer, int buffer_len);
  55.  
  56. /*
  57.    finger [-h filename] [-l | -w] [user]@hostname
  58.    -h        filename is alternative CONFIG.TEL file
  59.    -l -w     implements longer finger (whois)
  60.    user      username to query
  61.    hostname  the NAME (or Number?) of the host to finger
  62. */
  63.  
  64. int main(int argc, char *argv[])
  65. {
  66.    int i;
  67.    char userinfo[128];  /* Ought not to be > 128 char usernames */
  68.    int longform=0;      /* if 1, then try a long finger (whois) */
  69.    int gotuser=0;       /* if 1, doing lookup on a particular user */
  70.    int gothost=0;       /* if 1, we've found the host */
  71.    int atsign;          /* location of @ */
  72.    char *ptr=0;         /* pointer to CONFIG.TEL file */
  73.  
  74.    char remote_host[256];    /* address of remote host */
  75.  
  76. #ifdef __TURBOC__
  77.     fnsplit(argv[0],path_name,buf,temp_str,temp_data);     /* split path up */
  78. #else
  79.    _splitpath(argv[0],path_name,buf,temp_str,temp_data);   /* split path up */
  80. #endif
  81.    strcat(path_name,buf);         /* append the path name */
  82.    strcat(path_name,temp_str);    /* append filename */
  83.  
  84.    userinfo[0] = '\0';  /* init to a null string */
  85.  
  86. /* get the command line arguments */
  87.    for (i=1; i<argc; i++)
  88.       if (argv[i][0] == '-')
  89.       {
  90.          switch (argv[i][1])
  91.          {
  92.             case 'D':
  93.                debug = 1;
  94.                if (debug) printf("Debugging now ON\n");
  95.                break;
  96.             case 'h':
  97.                if (i+1 < argc)
  98.                   ptr = argv[i+1];
  99.                else
  100.                   usage();
  101.                if (debug) printf("Using config file [%s]\n",ptr);
  102.                break;
  103.             case 'l':
  104.             case 'w':
  105.                longform = 1;
  106.                if (debug) printf("using long finger form\n");
  107.                break;
  108.             case 't':
  109.                i++;
  110.                timeout=atoi(argv[i]);
  111.                printf("Timeout set to %d clicks.\n",timeout);
  112.                break;
  113.             default:
  114.                usage ();   /* Tell user how to use it */
  115.          }
  116.          if (debug) printf("argv[%d][1]=-%c\n", i, argv[i][1]);
  117.       }
  118.  
  119.    if (longform) strcpy(userinfo, "/W ");   /* long finger form, from RFC742 */
  120.  
  121.    if ((atsign = index(argv[argc-1], '@'))) /* find the host */
  122.    {
  123.       if (debug) printf("atsign [%d]\n", atsign);
  124.       strcpy(remote_host,(argv[argc-1]+atsign));
  125.       gothost++;
  126.       if (atsign != 1)  /* Then, we have a username too */
  127.       {
  128.          strncat(userinfo, argv[argc-1], atsign-1); /* username */
  129.          gotuser++;     /* Add 1 to users */
  130.       }
  131.    }
  132.    else
  133.       usage();     /* Tell them how to use it */
  134.  
  135. /* OK, we should have all the info we need to do the finger
  136.    So, now let's go do it */
  137.  
  138.    if (debug) printf("remote_host [%s]\n", remote_host);
  139.    if (debug) printf("userinfo    [%s]\n", userinfo);
  140.    if (debug) printf("gothost [%d]\n", gothost);
  141.    if (debug) printf("gotuser [%d]\n", gotuser);
  142.  
  143. /* exit (0); */  /* For debugging if you have no network! */
  144.  
  145.    signal(SIGINT,SIG_IGN);   /* Microsoft intercept of break */
  146.  
  147. /* Do session initialization.  Snetinit reads config file. */
  148.    /* go find a valid CONFIG.TEL file */
  149.    if (ptr == (char *)NULL) ptr = getenv("CONFIG.TEL");
  150.    if (debug) printf("ptr after getenv [%s]\n",ptr);
  151.    if (ptr != (char *)NULL) Shostfile(ptr);
  152.  
  153.    if (Snetinit()) /* Should return 0 if network is OK */
  154.    {
  155.       printf("network init failed\n");
  156.       exit (1);
  157.    }
  158.  
  159.    strcat(userinfo, "\x0d\x0a");  /* end with <CR><LF> as per RFC 742*/
  160.  
  161.    if (debug) printf("sending [%s] to [%s]\n",userinfo, remote_host);
  162.  
  163. /* Just Do It */
  164.    do_finger(remote_host, userinfo);
  165.  
  166.    netshut();      /* shut down all network stuff */
  167.    exit(0);
  168.    return(0);
  169. }
  170.  
  171.  
  172. void do_finger(char *remote_host, char *sendline)
  173. {
  174.    int from_port;
  175.    int conn_id;
  176.    struct machinfo *finger_host;
  177.    char buff[132]; /* text returned from the server */
  178.    int bufflen=132;
  179.    int len;        /* length of text read from server */
  180.    int i;
  181.  
  182.    int theclass, dat;
  183.    int event;
  184.  
  185.     /* pick a source port at random from the set of privileged ports */
  186.     randomize();
  187.     from_port = rand() % 1023;
  188.  
  189.     /* do name lookup for server */
  190.     finger_host = gethostinfo(remote_host);
  191.    if (finger_host == (struct machinfo *)NULL)     /* couldn't do it, message in gethostinfo */
  192.    {
  193.       printf("Couldn't lookup host [%s]\n", remote_host);
  194.       return;
  195.    }
  196.  
  197. /* print out the IP number of the destination host */
  198.        printf("[");
  199.        for (i=0; i<4; i++) {
  200.           printf("%d",finger_host->hostip[i]);
  201.         if (i < 3)
  202.             printf(".");
  203.     }
  204.        printf("]\n");
  205.  
  206.         /* open the connection */
  207.         conn_id = connect_sock(finger_host, from_port, FINGER_PORT);
  208.         if (conn_id < 0)
  209.         {
  210.            netshut();
  211.            printf("connect_sock returned %d, exiting\n",conn_id);
  212.         }
  213.         if (debug) printf("connection ident [%d]\n",conn_id);
  214.  
  215.         /* send the request */
  216.         netwrite(conn_id, sendline, strlen(sendline));
  217.         if (debug) printf("netwrite succeeded--flushing buffer\n");
  218.  
  219.         netpush(conn_id);
  220.         if (debug) printf("buffer flushing\n");
  221.  
  222.         if (debug)
  223.         if (!netest(conn_id))
  224.            printf("Connection OK\n");
  225.         else
  226.            printf("nettest: %d\n", netest(conn_id));
  227.  
  228. /* OK, now, we'll attempt to set an alarm to time out in timeout seconds
  229.    if we get a connection, but no response */
  230.  
  231.         Stimerset(USERCLASS, ALARM, 0, timeout);
  232.         if (debug) printf("timer set to go off in %d seconds\n", timeout);
  233.         theclass = 0;
  234.         event = 0;
  235.         while ((theclass != USERCLASS || event != ALARM) && !netest(conn_id))
  236.                 /* no alarm and good connection */
  237.         {
  238.            Stask();     /* I think I have to call this to post my alarm? */
  239.            event = Sgetevent(CONCLASS | USERCLASS, &theclass, &dat);
  240.            if (debug)
  241.               printf("event[%d] theclass[%d] dat[%d]\n",event, theclass, dat);
  242.            if (!event) continue;
  243.            if (conn_id != dat) continue;
  244.  
  245.            if (event == CONDATA)
  246.            {
  247.               len = netread(conn_id, buff, bufflen);
  248.               if (len == 0) continue;
  249.               filter(buff, len);
  250.               printf("%.*s", len, buff);
  251.            }
  252.                       
  253.         }
  254.         if (theclass == USERCLASS && event == ALARM)
  255.         {
  256.            printf("Connection timed out in %d clicks\n", timeout);
  257.            netshut();
  258.            exit (2);
  259.         }
  260.  
  261. #ifdef OLD_WAY
  262.   if (debug) printf("returning eof\n");
  263.  return (EOF);
  264.         while ((event = Sgetevent(CONCLASS,&theclass,&dat)) != CONDATA)
  265.         {
  266.            printf("event [%d] [%d] [%d]\n",event, theclass, dat);
  267.            if (conn_id == dat)
  268.            {
  269.         }
  270.         if (debug) printf("got event CONDATA [%d]\n",CONDATA);
  271.         printf("Event [%d] [%d] [%d]\n",event, theclass, dat);
  272.  
  273.            len = netread(conn_id, buff, bufflen);
  274.            if (debug) printf("len is [%d]\n", len);
  275.            if (len <=0 ) break;
  276.            printf("%.*s",len,buff);
  277.    }
  278.    }
  279. #endif
  280.         if (debug) printf("Closing Connection\n");
  281.         netclose(conn_id);
  282. }
  283.  
  284.  
  285. #ifdef MSC
  286. #ifndef __TURBOC__
  287. /******************************************************************
  288. *
  289. * randomize()
  290. *
  291. * replicates the randomize function of Turbo C
  292. * MSC 5.1 does not contain it so we have to write it ourselves.
  293. *
  294. */
  295.  
  296. static void randomize(void )
  297. {
  298.     srand((unsigned)time(NULL));
  299. }
  300. #endif
  301. #endif
  302.  
  303.  
  304. void filter (char *buffer, int buffer_len)
  305. /* filter out control characters, escape sequences, etc
  306.    keeps from reprogramming keys, ...
  307. */
  308. {
  309.    int i;
  310.    char ch;
  311.  
  312.    for (i=0; i<buffer_len; i++)
  313.    {
  314.       ch = *(buffer+i);
  315.       if (!(isprint(ch) || isspace(ch))) *(buffer+i) = '?';
  316.    }
  317. }
  318.  
  319.  
  320. int index(char *string, char ch)
  321. {
  322. /* shouldn't there be one of these somewhere? */
  323.    int i;
  324.  
  325.    for (i=0; i<(int)strlen(string); i++)
  326.       if ( *(string+i) == ch) return (i+1);
  327.    return (0);     /* if not found */
  328. }
  329.  
  330. void usage()
  331. {
  332.    printf("Usage: %s [-h filename] [-t ##] [-l | -w] [user]@hostname\n\n", path_name);
  333.  
  334.    printf("   -h        filename is alternative CONFIG.TEL file\n");
  335.    printf("   -t ##     set timeout period to ## clicks (default 300)\n");
  336.    printf("   -l -w     implements longer finger (whois)\n");
  337.    printf("   user      username to query\n");
  338.    printf("   hostname  the NAME of the host to finger\n");
  339.    exit (-1);
  340. }
  341.  
  342.