home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NETKIT-A.06 / NETKIT-A / NetKit-A-0.06 / dip337d-uri / login.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-15  |  6.5 KB  |  261 lines

  1. /*
  2.  * dip        A program for handling dialup IP connecions.
  3.  *        Incoming connection handling module.
  4.  *
  5.  * Version:    @(#)login.c    3.3.6    07/15/94
  6.  *
  7.  * Author:      Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  8.  *        Copyright 1988-1993 MicroWalt Corporation
  9.  *
  10.  * Modified:    Uri Blumenthal, <uri@watson.ibm.com>
  11.  *              (C) 1994
  12.  *
  13.  *        This program is free software; you can redistribute it
  14.  *        and/or  modify it under  the terms of  the GNU General
  15.  *        Public  License as  published  by  the  Free  Software
  16.  *        Foundation;  either  version 2 of the License, or  (at
  17.  *        your option) any later version.
  18.  */
  19. #include "dip.h"
  20.  
  21. static int verify_inet_addr(char *p);
  22.  
  23. static struct dip *
  24. getdipnam(char *who)
  25. {
  26.   static struct dip dip;
  27.   char buff[1024];
  28.   register FILE *fp;
  29.   register char *sp;
  30.   char *bp;
  31.  
  32.   if ((fp = fopen(_PATH_ETC_DIPHOSTS, "r")) == (FILE *)NULL) {
  33.     fprintf(stderr, "dip: cannot open %s\n", _PATH_ETC_DIPHOSTS);
  34.     syslog(LOG_ERR, "cannot open %s", _PATH_ETC_DIPHOSTS);
  35.     return((struct dip *)NULL);
  36.   }
  37.  
  38.   while(fgets(buff, 1024, fp) != (char *)NULL) {
  39.     if ((sp = strchr(buff, '\n')) != (char *)NULL) *sp = '\0';
  40.     if (buff[0] == '#' || buff[0] == '\0') continue;
  41.  
  42.     sp = buff;
  43.     memset((char *) &dip, 0, sizeof(struct dip));
  44.  
  45.     bp = sp;
  46.     while (*sp && *sp != ':') sp++;
  47.     *sp++ = '\0';
  48. #if 1
  49.     /* accept this diphosts record if either id or */
  50.     /* ttyname is in first field                   */
  51.     if (strcmp(bp, who) && strcmp(bp, ttyname(0)) &&
  52.         strcmp(bp, (rindex(ttyname(0), '/')+ 1))
  53.            ) 
  54.       continue;
  55. #else
  56.     if (strcmp(bp, who)) continue;
  57. #endif
  58.     strncpy(dip.name, bp, 16);
  59.  
  60.     bp = sp;
  61.     while (*sp && *sp != ':') sp++;
  62.     *sp++ = '\0';
  63.     strncpy(dip.passwd, bp, 8);
  64.  
  65.     bp = sp;
  66.     while (*sp && *sp != ':') sp++;
  67.     *sp++ = '\0';
  68.     strncpy(dip.remote, bp, 128);
  69.  
  70.     bp = sp;
  71.     while (*sp && *sp != ':') sp++;
  72.     *sp++ = '\0';
  73.     if (*bp)
  74.        strncpy(dip.local, bp, 128);
  75.  
  76.     bp = sp;
  77.     while (*sp && *sp != ':') sp++;
  78.     *sp++ = '\0';
  79.     strncpy(dip.netmask, bp, 128);
  80.     
  81.     bp = sp;
  82.     while (*sp && *sp != ':') sp++;
  83.     *sp++ = '\0';
  84.     strncpy(dip.comment, bp, 128);
  85.  
  86.     bp = sp;
  87.     while (*sp && *sp != ':' && *sp != ',') sp++;
  88.     *sp++ = '\0';
  89.     strncpy(dip.protocol, bp, 16);
  90.  
  91.     bp = sp;
  92.     while (*sp && *sp != ',') sp++;
  93.     *sp++ = '\0';
  94.     dip.mtu = atoi(bp);
  95.  
  96.     (void) fclose(fp);
  97.     return(&dip);
  98.   }
  99.   (void) fclose(fp);
  100.   return((struct dip *)NULL);
  101. }
  102.  
  103. static char buff[256];
  104.  
  105. void
  106. do_login(char *name, char **argp)
  107. {
  108.   struct dip *dip;
  109.   struct hostent *hp;
  110.   int i;
  111.   extern char *h_errlist[];
  112.   extern int h_errno;
  113.  
  114.   (void) openlog("dip", LOG_PID, LOG_DAEMON);
  115.   bzero(buff, sizeof(buff));
  116.  
  117.   dip = getdipnam(name);
  118.   if (dip == (struct dip *)NULL) {
  119.     fprintf(stderr, "You do not have DIP access.  Go away.\n");
  120.     syslog(LOG_WARNING, "%s tried to access DIP: no access!", name);
  121.     exit(-1);
  122.   }
  123.  
  124. #ifdef SKEY
  125.   /* check if the user has S/Key authentication */
  126.   if (strcmp(dip->passwd, SKEY_TOKEN) == 0) {
  127.       int    i;
  128.  
  129.       /* Make sure that the user has an S/Key entry - if not
  130.      log a warning, and kick them off */
  131.       if ((i = skey_haskey(name)) != SKEY_SUCCESS) {
  132.       fprintf(stderr, "Cannot find your S/Key entry.\n");
  133.       syslog(LOG_WARNING, "S/Key lookup failure for %s (%d)", 
  134.          name, i);
  135.       exit(1);
  136.       }
  137.       
  138.       if (skey_authenticate(name) != SKEY_SUCCESS) {
  139.       fprintf(stderr, "S/Key authentication failure.\n");
  140.       syslog(LOG_WARNING, "S/Key authentication failure for %s", name);
  141.       exit(-1);
  142.       }
  143.  
  144.   }
  145. #endif /* SKEY */
  146.  
  147.   /* Resolve this caller's host name to an IP address. */
  148.   if ((hp = gethostbyname(dip->remote)) == (struct hostent *)NULL) {
  149.     syslog(LOG_ERR, "unknown host %s: %s\n",
  150.             dip->remote, h_errlist[h_errno]);
  151.     exit(-1);
  152.   }
  153.   strncpy(dip->remote, hp->h_name, 128);
  154.   memcpy((char *) &dip->rmt_ip, (char *) hp->h_addr_list[0], hp->h_length);
  155.  
  156.   /* Resolve the local name to what IP address server should have */
  157.   if ((hp = gethostbyname(dip->local)) == (struct hostent *)NULL) {
  158.     syslog(LOG_ERR, "unknown local host %s: %s\n",
  159.             dip->local, h_errlist[h_errno]);
  160.     exit(-1);
  161.   }
  162.   strncpy(dip->local, hp->h_name, 128);
  163.   memcpy((char *) &dip->loc_ip, (char *) hp->h_addr_list[0], hp->h_length);
  164.  
  165.   /* Make sure the netmask is reasonably valid. And if not given, */
  166.   /* make it 255.255.255.0, just like what I have in my subnet.   */
  167.   if ((dip->netmask[0] == '\0') || (verify_inet_addr(dip->netmask) != 0))
  168.     strcpy(dip->netmask, "255.255.255.0");
  169.   
  170.   /* Find out which protocol we have to use. */
  171.   if ((i = get_prot(dip->protocol)) == 0) {
  172.     fprintf(stderr, "dip: unknown protocol %s\n", dip->protocol);
  173.     syslog(LOG_ERR, "%s wants unknown protocol %s",
  174.                     dip->remote, dip->protocol);
  175.     exit(-1);
  176.   }
  177.   dip->protonr = i;
  178.  
  179.   /* Show some info. */
  180.   if (opt_v == 1) {
  181.     printf("Hostname: \"%s\" [%s]\n", dip->remote, inet_ntoa(dip->rmt_ip));
  182.     printf("Local   : \"%s\" [%s]\n", dip->local,  inet_ntoa(dip->loc_ip));
  183.     printf("Netmask : \"%s\"\n",      dip->netmask);
  184.     printf("Comments: \"%s\"\n",      dip->comment);
  185.     printf("Protocol: \"%s\" (%d)\n", dip->protocol, dip->protonr);
  186.     printf("IP MTU  : %d\n", dip->mtu);
  187.   }
  188.   syslog(LOG_INFO, "%s connecting %s/%s to local %s/%s  with %s/%d",
  189.     dip->name, 
  190.         dip->remote, inet_ntoa(dip->rmt_ip), 
  191.         dip->local,  inet_ntoa(dip->loc_ip), 
  192.         dip->protocol, dip->mtu);
  193.  
  194.   sprintf(buff, "-dip (%s)", dip->remote);
  195.   *argp = buff;
  196.   printf("Your IP address is %s ",inet_ntoa(dip->rmt_ip));
  197.   printf("Server address is %s\n",inet_ntoa(dip->loc_ip));
  198.   printf("Netmask is %s  Starting %s\n", dip->netmask, dip->protocol);
  199.   (void) fflush(stdout);
  200.  
  201.   /* Initialize this terminal line for 8-bit clean operation. */
  202.   (void) tty_open(NULL);
  203.  
  204.   /* Make tty the control terminal, detect DCD loss from now. */
  205. #if 0
  206.   if ((i = tty_notlocal()) < 0)
  207.      exit(-1);
  208. #else
  209.   if ((i == tty_login()) < 0)
  210.     exit(-1);
  211. #endif
  212.   
  213.   /* Set up for "no messages". We do not want to be disturbed anymore. */
  214.   (void) tty_nomesg();
  215.  
  216.   /* Enter background mode here. */
  217.   (void) dip_login_setup(dip);
  218.  
  219.   exit(0);
  220. }
  221.  
  222. static int verify_inet_addr(char *p)
  223. {
  224.   int  i    = 0;
  225.   int  dots = 0;
  226.   char ch;
  227.  
  228.   for (ch = *p++; ch; ch = *p++) {
  229.     switch (ch) {
  230.     case '.':
  231.       dots++;
  232.       if (dots > 3)   return (-1);
  233.       if ((i > 255) || (i < 0)) return (-1);
  234.       i = 0; 
  235.       break;
  236.     case '0':
  237.     case '1':
  238.     case '2':
  239.     case '3':
  240.     case '4':
  241.     case '5':
  242.     case '6':
  243.     case '7':
  244.     case '8':
  245.     case '9':
  246.       i = (i * 10) + (int) ch - '0';
  247.       break;
  248.     case '\0':
  249.       goto end_of_loop;
  250.     default:
  251.       return (-1);
  252.     }
  253.   }
  254.   
  255.  end_of_loop:
  256.   if ((dots != 3) || (i > 255) || (i < 0))
  257.     return (-1);
  258.   
  259.   return 0;
  260. }
  261.