home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / internet / tcpipsrc / h / if / MiscServ / c / ident next >
Text File  |  1995-02-06  |  3KB  |  145 lines

  1. /*
  2.  *
  3.  *     Simple ident support...
  4.  *
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "global.h"
  11. #include "mbuf.h"
  12. #include "timer.h"
  13. #include "internet.h"
  14. #include "icmp.h"
  15. #include "netuser.h"
  16. #include "tcp.h"
  17. #include "ident.h"
  18. #include "session.h"
  19. #include "misc.h"
  20. #include "arc.h"
  21.  
  22. static char idname[16];
  23.  
  24. static void sndmsg(struct tcb *, char *);
  25.  
  26. struct tcb *id_tcb = NULLTCB;
  27.  
  28. int ident1(int argc, char **argv)
  29. {
  30.         extern int32    ip_addr;
  31.         struct socket   lsocket;
  32.  
  33.         if (id_tcb)
  34.                 return(0);
  35.         /* start ident daemon */
  36.         lsocket.address = ip_addr;
  37.         lsocket.port = IDENT_PORT;
  38.         if (argc < 2)
  39.           strcpy(idname, "UNKNOWN");
  40.         else
  41.         {
  42.           strcpy(idname, argv[1]);
  43.           rip(idname);
  44.         }
  45.         id_tcb = open_tcp(&lsocket, NULLSOCK, TCP_SERVER, 0, (void(*)())rcv_ident,
  46.                 NULLVFP, (void(*)())ident_state, 0, NULL);
  47.         return(0);
  48. }
  49.  
  50. /*
  51.  *  Handle incoming ident connections and closures.
  52.  */
  53. void ident_state(struct tcb *tcb, char old, char new)
  54. {
  55.         struct ident *id;
  56.  
  57.         old = old;
  58.  
  59.         switch(new){
  60.         case ESTABLISHED:
  61.                 id = (struct ident *) malloc(sizeof(struct ident));
  62.                 tcb->user = (char *)id;       /* Upward pointer */
  63.                 id->tcb = tcb;                /* Downward pointer */
  64.                 return;
  65.         case CLOSED:
  66.                 if (tcb == id_tcb)
  67.                 {
  68.                         close_tcp(id_tcb);
  69.                         id_tcb = NULLTCB;
  70.                 }
  71.                 if (tcb->user != NULLCHAR)
  72.                         free(tcb->user);
  73.                 del_tcp(tcb);
  74.                 break;
  75.         }
  76. }
  77.  
  78. /*
  79.  * Stop the ident server.
  80.  */
  81. int ident0(void)
  82. {
  83.         if (id_tcb != NULLTCB) {
  84.                 close_tcp(id_tcb);
  85.                 id_tcb = NULLTCB;
  86.         }
  87.         return(0);
  88. }
  89.  
  90. /*
  91.  *      Send a short message on a tcb
  92.  */
  93.  
  94. static void sndmsg(struct tcb *tcb, char *msg)
  95. {
  96.         struct mbuf *bp;
  97.  
  98.         bp = qdata(msg,(int16)strlen(msg));
  99.         send_tcp(tcb,bp);
  100. }
  101.  
  102. /*
  103.  * ident receive upcall.
  104.  */
  105. void rcv_ident(register struct tcb *tcb, int16 ccnt)
  106. {
  107.         struct ident    *id;
  108.         struct mbuf     *mbuf,
  109.                         *bp;
  110.         char            req[80];
  111.         int             cnt;
  112.  
  113.         if ((id = (struct ident *) tcb->user) == NULLID)   /* uh oh! */
  114.                 return;
  115.  
  116.         if(recv_tcp(tcb,&bp,ccnt) == 0)
  117.                 return;
  118.         cnt = pullup(&bp, req, ccnt);
  119.         req[cnt] = '\0';
  120.         free_p(bp);
  121.  
  122.         rip(req);
  123.         strcat(req, " : USERID : UNIX : ");
  124.         strcat(req, idname);
  125.         log_event(tcb,"ID Request: %s",req);
  126.         strcat(req, "\r\n");
  127.  
  128.         sndmsg(tcb, req);
  129.  
  130.         close_tcp(tcb);
  131. }
  132.  
  133.  
  134. int doident(int argc, char **argv)
  135. {
  136.   if (argc>1)
  137.   {
  138.     strcpy(idname, argv[1]);
  139.     rip(idname);
  140.   }
  141.   else
  142.     cwprintf(NULL, "Ident: %s\r\n",idname);
  143.   return 0;
  144. }
  145.