home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NETKIT-B.05 / NETKIT-B / NetKit-B-0.05 / ftp / ruserpass.c,v < prev    next >
Encoding:
Text File  |  1994-08-17  |  7.4 KB  |  304 lines

  1. head    1.1;
  2. access;
  3. symbols;
  4. locks
  5.     florian:1.1; strict;
  6. comment    @ * @;
  7.  
  8.  
  9. 1.1
  10. date    94.08.17.13.52.59;    author florian;    state Exp;
  11. branches;
  12. next    ;
  13.  
  14.  
  15. desc
  16. @@
  17.  
  18.  
  19. 1.1
  20. log
  21. @Initial revision
  22. @
  23. text
  24. @/*
  25.  * Copyright (c) 1985 Regents of the University of California.
  26.  * All rights reserved.
  27.  *
  28.  * Redistribution and use in source and binary forms, with or without
  29.  * modification, are permitted provided that the following conditions
  30.  * are met:
  31.  * 1. Redistributions of source code must retain the above copyright
  32.  *    notice, this list of conditions and the following disclaimer.
  33.  * 2. Redistributions in binary form must reproduce the above copyright
  34.  *    notice, this list of conditions and the following disclaimer in the
  35.  *    documentation and/or other materials provided with the distribution.
  36.  * 3. All advertising materials mentioning features or use of this software
  37.  *    must display the following acknowledgement:
  38.  *    This product includes software developed by the University of
  39.  *    California, Berkeley and its contributors.
  40.  * 4. Neither the name of the University nor the names of its contributors
  41.  *    may be used to endorse or promote products derived from this software
  42.  *    without specific prior written permission.
  43.  *
  44.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  45.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  46.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  47.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  48.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  49.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  50.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  51.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  52.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  53.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  54.  * SUCH DAMAGE.
  55.  */
  56.  
  57. #ifndef lint
  58. /*static char sccsid[] = "from: @@(#)ruserpass.c    5.3 (Berkeley) 3/1/91";*/
  59. static char rcsid[] = "$Id: ruserpass.c,v 1.2 1993/08/01 18:15:24 mycroft Exp $";
  60. #endif /* not lint */
  61.  
  62. #include <sys/types.h>
  63. #include <stdio.h>
  64. #include <utmp.h>
  65. #include <ctype.h>
  66. #include <sys/stat.h>
  67. #include <errno.h>
  68. #include "ftp_var.h"
  69.  
  70. char    *renvlook(), *malloc(), *index(), *getenv(), *getpass(), *getlogin();
  71. char    *strcpy();
  72. struct    utmp *getutmp();
  73. static    FILE *cfile;
  74.  
  75. #define    DEFAULT    1
  76. #define    LOGIN    2
  77. #define    PASSWD    3
  78. #define    ACCOUNT 4
  79. #define MACDEF  5
  80. #define    ID    10
  81. #define    MACH    11
  82.  
  83. static char tokval[100];
  84.  
  85. static struct toktab {
  86.     char *tokstr;
  87.     int tval;
  88. } toktab[]= {
  89.     "default",    DEFAULT,
  90.     "login",    LOGIN,
  91.     "password",    PASSWD,
  92.     "passwd",    PASSWD,
  93.     "account",    ACCOUNT,
  94.     "machine",    MACH,
  95.     "macdef",    MACDEF,
  96.     0,        0
  97. };
  98.  
  99. ruserpass(host, aname, apass, aacct)
  100.     char *host, **aname, **apass, **aacct;
  101. {
  102.     char *hdir, buf[BUFSIZ], *tmp;
  103.     char myname[MAXHOSTNAMELEN], *mydomain;
  104.     int t, i, c, usedefault = 0;
  105.     struct stat stb;
  106.     static int token();
  107.  
  108.     hdir = getenv("HOME");
  109.     if (hdir == NULL)
  110.         hdir = ".";
  111.     (void) sprintf(buf, "%s/.netrc", hdir);
  112.     cfile = fopen(buf, "r");
  113.     if (cfile == NULL) {
  114.         if (errno != ENOENT)
  115.             perror(buf);
  116.         return(0);
  117.     }
  118.     if (gethostname(myname, sizeof(myname)) < 0)
  119.         myname[0] = '\0';
  120.     if ((mydomain = index(myname, '.')) == NULL)
  121.         mydomain = "";
  122. next:
  123.     while ((t = token())) switch(t) {
  124.  
  125.     case DEFAULT:
  126.         usedefault = 1;
  127.         /* FALL THROUGH */
  128.  
  129.     case MACH:
  130.         if (!usedefault) {
  131.             if (token() != ID)
  132.                 continue;
  133.             /*
  134.              * Allow match either for user's input host name
  135.              * or official hostname.  Also allow match of 
  136.              * incompletely-specified host in local domain.
  137.              */
  138.             if (strcasecmp(host, tokval) == 0)
  139.                 goto match;
  140.             if (strcasecmp(hostname, tokval) == 0)
  141.                 goto match;
  142.             if ((tmp = index(hostname, '.')) != NULL &&
  143.                 strcasecmp(tmp, mydomain) == 0 &&
  144.                 strncasecmp(hostname, tokval, tmp-hostname) == 0 &&
  145.                 tokval[tmp - hostname] == '\0')
  146.                 goto match;
  147.             if ((tmp = index(host, '.')) != NULL &&
  148.                 strcasecmp(tmp, mydomain) == 0 &&
  149.                 strncasecmp(host, tokval, tmp - host) == 0 &&
  150.                 tokval[tmp - host] == '\0')
  151.                 goto match;
  152.             continue;
  153.         }
  154.     match:
  155.         while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
  156.  
  157.         case LOGIN:
  158.             if (token())
  159.                 if (*aname == 0) { 
  160.                     *aname = malloc((unsigned) strlen(tokval) + 1);
  161.                     (void) strcpy(*aname, tokval);
  162.                 } else {
  163.                     if (strcmp(*aname, tokval))
  164.                         goto next;
  165.                 }
  166.             break;
  167.         case PASSWD:
  168.             if (strcmp(*aname, "anonymous") &&
  169.                 fstat(fileno(cfile), &stb) >= 0 &&
  170.                 (stb.st_mode & 077) != 0) {
  171.     fprintf(stderr, "Error - .netrc file not correct mode.\n");
  172.     fprintf(stderr, "Remove password or correct mode.\n");
  173.                 goto bad;
  174.             }
  175.             if (token() && *apass == 0) {
  176.                 *apass = malloc((unsigned) strlen(tokval) + 1);
  177.                 (void) strcpy(*apass, tokval);
  178.             }
  179.             break;
  180.         case ACCOUNT:
  181.             if (fstat(fileno(cfile), &stb) >= 0
  182.                 && (stb.st_mode & 077) != 0) {
  183.     fprintf(stderr, "Error - .netrc file not correct mode.\n");
  184.     fprintf(stderr, "Remove account or correct mode.\n");
  185.                 goto bad;
  186.             }
  187.             if (token() && *aacct == 0) {
  188.                 *aacct = malloc((unsigned) strlen(tokval) + 1);
  189.                 (void) strcpy(*aacct, tokval);
  190.             }
  191.             break;
  192.         case MACDEF:
  193.             if (proxy) {
  194.                 (void) fclose(cfile);
  195.                 return(0);
  196.             }
  197.             while ((c=getc(cfile)) != EOF && c == ' ' || c == '\t');
  198.             if (c == EOF || c == '\n') {
  199.                 printf("Missing macdef name argument.\n");
  200.                 goto bad;
  201.             }
  202.             if (macnum == 16) {
  203.                 printf("Limit of 16 macros have already been defined\n");
  204.                 goto bad;
  205.             }
  206.             tmp = macros[macnum].mac_name;
  207.             *tmp++ = c;
  208.             for (i=0; i < 8 && (c=getc(cfile)) != EOF &&
  209.                 !isspace(c); ++i) {
  210.                 *tmp++ = c;
  211.             }
  212.             if (c == EOF) {
  213.                 printf("Macro definition missing null line terminator.\n");
  214.                 goto bad;
  215.             }
  216.             *tmp = '\0';
  217.             if (c != '\n') {
  218.                 while ((c=getc(cfile)) != EOF && c != '\n');
  219.             }
  220.             if (c == EOF) {
  221.                 printf("Macro definition missing null line terminator.\n");
  222.                 goto bad;
  223.             }
  224.             if (macnum == 0) {
  225.                 macros[macnum].mac_start = macbuf;
  226.             }
  227.             else {
  228.                 macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
  229.             }
  230.             tmp = macros[macnum].mac_start;
  231.             while (tmp != macbuf + 4096) {
  232.                 if ((c=getc(cfile)) == EOF) {
  233.                 printf("Macro definition missing null line terminator.\n");
  234.                     goto bad;
  235.                 }
  236.                 *tmp = c;
  237.                 if (*tmp == '\n') {
  238.                     if (*(tmp-1) == '\0') {
  239.                        macros[macnum++].mac_end = tmp - 1;
  240.                        break;
  241.                     }
  242.                     *tmp = '\0';
  243.                 }
  244.                 tmp++;
  245.             }
  246.             if (tmp == macbuf + 4096) {
  247.                 printf("4K macro buffer exceeded\n");
  248.                 goto bad;
  249.             }
  250.             break;
  251.         default:
  252.     fprintf(stderr, "Unknown .netrc keyword %s\n", tokval);
  253.             break;
  254.         }
  255.         goto done;
  256.     }
  257. done:
  258.     (void) fclose(cfile);
  259.     return(0);
  260. bad:
  261.     (void) fclose(cfile);
  262.     return(-1);
  263. }
  264.  
  265. static
  266. token()
  267. {
  268.     char *cp;
  269.     int c;
  270.     struct toktab *t;
  271.  
  272.     if (feof(cfile))
  273.         return (0);
  274.     while ((c = getc(cfile)) != EOF &&
  275.         (c == '\n' || c == '\t' || c == ' ' || c == ','))
  276.         continue;
  277.     if (c == EOF)
  278.         return (0);
  279.     cp = tokval;
  280.     if (c == '"') {
  281.         while ((c = getc(cfile)) != EOF && c != '"') {
  282.             if (c == '\\')
  283.                 c = getc(cfile);
  284.             *cp++ = c;
  285.         }
  286.     } else {
  287.         *cp++ = c;
  288.         while ((c = getc(cfile)) != EOF
  289.             && c != '\n' && c != '\t' && c != ' ' && c != ',') {
  290.             if (c == '\\')
  291.                 c = getc(cfile);
  292.             *cp++ = c;
  293.         }
  294.     }
  295.     *cp = 0;
  296.     if (tokval[0] == 0)
  297.         return (0);
  298.     for (t = toktab; t->tokstr; t++)
  299.         if (!strcmp(t->tokstr, tokval))
  300.             return (t->tval);
  301.     return (ID);
  302. }
  303. @
  304.