first cut at new makefile; file reorg, new depend
[unix-history] / usr / src / usr.bin / ftp / ruserpass.c
index 396b85f..94116f0 100644 (file)
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)ruserpass.c        1.7 (Berkeley) %G%";
+static char sccsid[] = "@(#)ruserpass.c        5.1 (Berkeley) %G%";
 #endif /* not lint */
 
 #endif /* not lint */
 
-struct macel {
-       char mac_name[9];       /* macro name */
-       char *mac_start;        /* start of macro in macbuf */
-       char *mac_end;          /* end of macro in macbuf */
-};
-
-extern int macnum, proxy;                      /* number of defined macros */
-extern struct macel macros[16], *macpt;
-extern char macbuf[4096];
-
 #include <sys/types.h>
 #include <stdio.h>
 #include <utmp.h>
 #include <ctype.h>
 #include <sys/stat.h>
 #include <errno.h>
 #include <sys/types.h>
 #include <stdio.h>
 #include <utmp.h>
 #include <ctype.h>
 #include <sys/stat.h>
 #include <errno.h>
+#include "ftp_var.h"
 
 char   *renvlook(), *malloc(), *index(), *getenv(), *getpass(), *getlogin();
 char   *strcpy();
 struct utmp *getutmp();
 static FILE *cfile;
 
 
 char   *renvlook(), *malloc(), *index(), *getenv(), *getpass(), *getlogin();
 char   *strcpy();
 struct utmp *getutmp();
 static FILE *cfile;
 
-ruserpass(host, aname, apass, aacct)
-       char *host, **aname, **apass, **aacct;
-{
-
-       /* renv(host, aname, apass, aacct);
-       if (*aname == 0 || *apass == 0) */
-               return(rnetrc(host, aname, apass, aacct));
-}
-
 #define        DEFAULT 1
 #define        LOGIN   2
 #define        PASSWD  3
 #define        ACCOUNT 4
 #define MACDEF  5
 #define        ID      10
 #define        DEFAULT 1
 #define        LOGIN   2
 #define        PASSWD  3
 #define        ACCOUNT 4
 #define MACDEF  5
 #define        ID      10
-#define        MACHINE 11
+#define        MACH    11
 
 static char tokval[100];
 
 
 static char tokval[100];
 
@@ -67,17 +49,18 @@ static struct toktab {
        "default",      DEFAULT,
        "login",        LOGIN,
        "password",     PASSWD,
        "default",      DEFAULT,
        "login",        LOGIN,
        "password",     PASSWD,
+       "passwd",       PASSWD,
        "account",      ACCOUNT,
        "account",      ACCOUNT,
-       "machine",      MACHINE,
+       "machine",      MACH,
        "macdef",       MACDEF,
        0,              0
 };
 
        "macdef",       MACDEF,
        0,              0
 };
 
-static
-rnetrc(host, aname, apass, aacct)
+ruserpass(host, aname, apass, aacct)
        char *host, **aname, **apass, **aacct;
 {
        char *hdir, buf[BUFSIZ], *tmp;
        char *host, **aname, **apass, **aacct;
 {
        char *hdir, buf[BUFSIZ], *tmp;
+       char myname[MAXHOSTNAMELEN], *mydomain;
        int t, i, c, usedefault = 0;
        struct stat stb;
        extern int errno;
        int t, i, c, usedefault = 0;
        struct stat stb;
        extern int errno;
@@ -92,6 +75,10 @@ rnetrc(host, aname, apass, aacct)
                        perror(buf);
                return(0);
        }
                        perror(buf);
                return(0);
        }
+       if (gethostname(myname, sizeof(myname)) < 0)
+               myname[0] = '\0';
+       if ((mydomain = index(myname, '.')) == NULL)
+               mydomain = "";
 next:
        while ((t = token())) switch(t) {
 
 next:
        while ((t = token())) switch(t) {
 
@@ -99,10 +86,33 @@ next:
                usedefault = 1;
                /* FALL THROUGH */
 
                usedefault = 1;
                /* FALL THROUGH */
 
-       case MACHINE:
-               if (!usedefault && (token() != ID || strcmp(host, tokval)))
+       case MACH:
+               if (!usedefault) {
+                       if (token() != ID)
+                               continue;
+                       /*
+                        * Allow match either for user's input host name
+                        * or official hostname.  Also allow match of 
+                        * incompletely-specified host in local domain.
+                        */
+                       if (strcasecmp(host, tokval) == 0)
+                               goto match;
+                       if (strcasecmp(hostname, tokval) == 0)
+                               goto match;
+                       if ((tmp = index(hostname, '.')) != NULL &&
+                           strcasecmp(tmp, mydomain) == 0 &&
+                           strncasecmp(hostname, tokval, tmp-hostname) == 0 &&
+                           tokval[tmp - hostname] == '\0')
+                               goto match;
+                       if ((tmp = index(host, '.')) != NULL &&
+                           strcasecmp(tmp, mydomain) == 0 &&
+                           strncasecmp(host, tokval, tmp - host) == 0 &&
+                           tokval[tmp - host] == '\0')
+                               goto match;
                        continue;
                        continue;
-               while ((t = token()) && t != MACHINE) switch(t) {
+               }
+       match:
+               while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
 
                case LOGIN:
                        if (token())
 
                case LOGIN:
                        if (token())
@@ -115,11 +125,12 @@ next:
                                }
                        break;
                case PASSWD:
                                }
                        break;
                case PASSWD:
-                       if (fstat(fileno(cfile), &stb) >= 0
-                           && (stb.st_mode & 077) != 0) {
+                       if (strcmp(*aname, "anonymous") &&
+                           fstat(fileno(cfile), &stb) >= 0 &&
+                           (stb.st_mode & 077) != 0) {
        fprintf(stderr, "Error - .netrc file not correct mode.\n");
        fprintf(stderr, "Remove password or correct mode.\n");
        fprintf(stderr, "Error - .netrc file not correct mode.\n");
        fprintf(stderr, "Remove password or correct mode.\n");
-                               return(-1);
+                               goto bad;
                        }
                        if (token() && *apass == 0) {
                                *apass = malloc((unsigned) strlen(tokval) + 1);
                        }
                        if (token() && *apass == 0) {
                                *apass = malloc((unsigned) strlen(tokval) + 1);
@@ -131,7 +142,7 @@ next:
                            && (stb.st_mode & 077) != 0) {
        fprintf(stderr, "Error - .netrc file not correct mode.\n");
        fprintf(stderr, "Remove account or correct mode.\n");
                            && (stb.st_mode & 077) != 0) {
        fprintf(stderr, "Error - .netrc file not correct mode.\n");
        fprintf(stderr, "Remove account or correct mode.\n");
-                               return(-1);
+                               goto bad;
                        }
                        if (token() && *aacct == 0) {
                                *aacct = malloc((unsigned) strlen(tokval) + 1);
                        }
                        if (token() && *aacct == 0) {
                                *aacct = malloc((unsigned) strlen(tokval) + 1);
@@ -140,16 +151,17 @@ next:
                        break;
                case MACDEF:
                        if (proxy) {
                        break;
                case MACDEF:
                        if (proxy) {
+                               (void) fclose(cfile);
                                return(0);
                        }
                        while ((c=getc(cfile)) != EOF && c == ' ' || c == '\t');
                        if (c == EOF || c == '\n') {
                                printf("Missing macdef name argument.\n");
                                return(0);
                        }
                        while ((c=getc(cfile)) != EOF && c == ' ' || c == '\t');
                        if (c == EOF || c == '\n') {
                                printf("Missing macdef name argument.\n");
-                               return(-1);
+                               goto bad;
                        }
                        if (macnum == 16) {
                                printf("Limit of 16 macros have already been defined\n");
                        }
                        if (macnum == 16) {
                                printf("Limit of 16 macros have already been defined\n");
-                               return(-1);
+                               goto bad;
                        }
                        tmp = macros[macnum].mac_name;
                        *tmp++ = c;
                        }
                        tmp = macros[macnum].mac_name;
                        *tmp++ = c;
@@ -159,7 +171,7 @@ next:
                        }
                        if (c == EOF) {
                                printf("Macro definition missing null line terminator.\n");
                        }
                        if (c == EOF) {
                                printf("Macro definition missing null line terminator.\n");
-                               return(-1);
+                               goto bad;
                        }
                        *tmp = '\0';
                        if (c != '\n') {
                        }
                        *tmp = '\0';
                        if (c != '\n') {
@@ -167,7 +179,7 @@ next:
                        }
                        if (c == EOF) {
                                printf("Macro definition missing null line terminator.\n");
                        }
                        if (c == EOF) {
                                printf("Macro definition missing null line terminator.\n");
-                               return(-1);
+                               goto bad;
                        }
                        if (macnum == 0) {
                                macros[macnum].mac_start = macbuf;
                        }
                        if (macnum == 0) {
                                macros[macnum].mac_start = macbuf;
@@ -179,7 +191,7 @@ next:
                        while (tmp != macbuf + 4096) {
                                if ((c=getc(cfile)) == EOF) {
                                printf("Macro definition missing null line terminator.\n");
                        while (tmp != macbuf + 4096) {
                                if ((c=getc(cfile)) == EOF) {
                                printf("Macro definition missing null line terminator.\n");
-                                       return(-1);
+                                       goto bad;
                                }
                                *tmp = c;
                                if (*tmp == '\n') {
                                }
                                *tmp = c;
                                if (*tmp == '\n') {
@@ -193,7 +205,7 @@ next:
                        }
                        if (tmp == macbuf + 4096) {
                                printf("4K macro buffer exceeded\n");
                        }
                        if (tmp == macbuf + 4096) {
                                printf("4K macro buffer exceeded\n");
-                               return(-1);
+                               goto bad;
                        }
                        break;
                default:
                        }
                        break;
                default:
@@ -205,6 +217,9 @@ next:
 done:
        (void) fclose(cfile);
        return(0);
 done:
        (void) fclose(cfile);
        return(0);
+bad:
+       (void) fclose(cfile);
+       return(-1);
 }
 
 static
 }
 
 static