home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / internet / netlite2 / NET / c / FINGSERV < prev    next >
Encoding:
Text File  |  1993-03-28  |  7.0 KB  |  218 lines

  1. /*
  2.  *
  3.  *      Finger support...
  4.  *
  5.  *      Finger server routines.  Written by Michael T. Horne - KA7AXD.
  6.  *      Copyright 1988 by Michael T. Horne, All Rights Reserved.
  7.  *      Permission granted for non-commercial use and copying, provided
  8.  *      that this notice is retained.
  9.  *
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include "global.h"
  16. #include "mbuf.h"
  17. #include "timer.h"
  18. #include "internet.h"
  19. #include "icmp.h"
  20. #include "netuser.h"
  21. #include "tcp.h"
  22. #include "finger.h"
  23. #include "session.h"
  24. #include "misc.h"
  25. #include "arc.h"
  26.  
  27. static void sndmsg(struct tcb *, char *);
  28.  
  29. struct tcb *fing_tcb = NULLTCB;
  30.  
  31. int finger1(int argc, char **argv)
  32. {
  33.         extern int32    ip_addr;
  34.         struct socket   lsocket;
  35.  
  36.         if (fing_tcb)
  37.                 return(0);
  38.         /* start finger daemon */
  39.         lsocket.address = ip_addr;
  40.         if(argc < 2)
  41.                 lsocket.port = FINGER_PORT;
  42.         else
  43.                 lsocket.port = atoi(argv[1]);
  44.         fing_tcb = open_tcp(&lsocket, NULLSOCK, TCP_SERVER, 0, (void(*)())rcv_fing,
  45.                 NULLVFP, (void(*)())fing_state, 0, NULL);
  46.         return(0);
  47. }
  48. /*
  49.  *      Handle incoming finger connections and closures.
  50.  * 
  51.  */
  52. void fing_state(struct tcb *tcb, char old, char new)
  53. {
  54.         struct finger *fing;
  55.  
  56.         old = old;
  57.  
  58.         switch(new){
  59.         case ESTABLISHED:
  60.                 fing = (struct finger *) malloc(sizeof(struct finger));
  61.  
  62.                 tcb->user = (char *)fing;       /* Upward pointer */
  63.                 fing->tcb = tcb;                /* Downward pointer */
  64.                 return;
  65.         case CLOSED:
  66.                 if (tcb == fing_tcb)
  67.                         fing_tcb = NULLTCB;
  68.                 if (tcb->user != NULLCHAR)
  69.                         free(tcb->user);
  70.                 del_tcp(tcb);
  71.                 break;
  72.         }
  73. }
  74.  
  75. /*
  76.  *      Stop the finger server.
  77.  */
  78.  
  79. int finger0(void)
  80. {
  81.         if (fing_tcb != NULLTCB) {
  82.                 close_tcp(fing_tcb);
  83.                 fing_tcb = NULLTCB;
  84.         }
  85.         return(0);
  86. }
  87.  
  88. /*
  89.  *      Send a short message on a tcb
  90.  */
  91.  
  92. static void sndmsg(struct tcb *tcb, char *msg)
  93. {
  94.         struct mbuf *bp;
  95.  
  96.         bp = qdata(msg,(int16)strlen(msg));
  97.         send_tcp(tcb,bp);
  98. }
  99.  
  100. /*
  101.  *      Finger receive upcall.  This is the guts of the finger server.
  102.  *      The user to finger is read from the socket.  If only a newline
  103.  *      is read, then send the remote host a list of all known 'users' on
  104.  *      this system.
  105.  */
  106.  
  107. void rcv_fing(register struct tcb *tcb, int16 ccnt)
  108. {
  109.         struct finger   *fing;
  110.         FILE            *fuser;
  111.         struct mbuf     *mbuf,
  112.                         *bp;
  113.         char            *buf,
  114.                         *who,
  115.                         *finger_file,
  116.                         *path,
  117.                         ch,
  118.                         temp[80],
  119.                         user[80];
  120.         int             cnt;
  121.         int             size;
  122.  
  123.         ccnt = ccnt;
  124.  
  125.         if ((fing = (struct finger *) tcb->user) == NULLFING)   /* uh oh! */
  126.                 return;
  127.         if(recv_tcp(tcb,&bp,FINGNAMELEN) == 0)
  128.                 return;
  129.         if ((who = malloc(FINGNAMELEN + 1)) == NULL) {
  130.                 free_p(bp);
  131.                 return;
  132.         }
  133.  
  134.         cnt = pullup(&bp, who, FINGNAMELEN);    /* get 'user' name */
  135.         who[cnt] = '\0';                        /* NULL terminate it */
  136.         free_p(bp);                             /* all done with bp */
  137.  
  138.         if (*who == '\015' || *who == '\012') { /* give him a user listing */
  139.                 int found = 0;
  140.  
  141.                 path = (char *) malloc(strlen(fingerpath) + 5);
  142.                 /* create wildcard path to finger files */
  143.                 strcpy(path, fingerpath);
  144.  
  145.                 sndmsg(tcb, "Known users on this system:\015\012");
  146.                 for (filedir(path, 0, user); user[0] != '\0';
  147.                         filedir (path, 1, user))  {
  148.                         found++;
  149.                         sprintf(temp, "        %s\015\012", user);
  150.                         sndmsg(tcb, temp);
  151.                 }
  152.                 if (!found)
  153.                         sndmsg(tcb, "None!\015\012");
  154.  
  155.                 free(path);
  156.         }
  157.         else {
  158.                 buf = who;
  159.                 while (*buf != '\015' && *buf != '\012' && *buf != '\0')
  160.                         buf++;
  161.                 *buf = '\0';
  162.                 /*
  163.                  *      Create path to user's finger file and see if the
  164.                  *      the file exists.
  165.                  */
  166.                 finger_file = malloc(strlen(fingerpath) + strlen(who) + 5);
  167.                 if (finger_file == NULL) {      /* uh oh... */
  168.                         free(who);              /* clean up */
  169.                         close_tcp(tcb);         /* close socket */
  170.                         return;
  171.                 }
  172.                 sprintf(finger_file, "%s.%s", fingerpath, who);
  173.  
  174.                 if ((fuser = fopen(finger_file, "r")) == (FILE *) NULL) {
  175.                         sprintf(temp, "User %s not known\015\012", who);
  176.                         sndmsg(tcb, temp);
  177.                 }
  178.                 else {                          /* valid 'user' */
  179.                         ch = fgetc(fuser);      /* first get must be outside */
  180.                         while (!feof(fuser)) {
  181.                                 size = tcb->window;
  182.                                 if ((mbuf = alloc_mbuf(size)) == NULLBUF) {
  183.                                         fclose(fuser);  /* barf */
  184.                                         free(who);
  185.                                         free(finger_file);
  186.                                         return;
  187.                                 }
  188.                                 buf = mbuf->data;  /* pointer to buffer */
  189.                                 while(!feof(fuser) && size--) { /* loop */
  190.                                         switch(ch) {
  191.                                             case '\032':    /* NO ^Z's! */
  192.                                                     break;
  193.                                             case '\012':    /* EOL */
  194.                                                     *buf++ = '\015';
  195.                                                     *buf++ = '\012';
  196.                                                     mbuf->cnt += 2;
  197.                                                     break;
  198.                                             case '\015':
  199.                                                     break;
  200.                                             default:
  201.                                                     *buf++ = ch;
  202.                                                     mbuf->cnt++;
  203.                                                     break;
  204.                                         }
  205.                                         ch = fgetc(fuser);
  206.                                 }
  207.                                 send_tcp(tcb, mbuf);    /* send info */
  208.                         }
  209.                         fclose(fuser);
  210.                 }
  211.                 free(finger_file);
  212.         }
  213.         free(who);
  214.         close_tcp(tcb);                 /* close socket */
  215.         return;
  216. }
  217.  
  218.