X-Git-Url: https://git.subgeniuskitty.com/unix-history/.git/blobdiff_plain/e6be46b8a692a3383ce3be4fde55a29c78b0928b..8b451c8e712b33dd7f66d7fefa21a22b3bb3cb6f:/usr/src/usr.bin/ftp/ruserpass.c diff --git a/usr/src/usr.bin/ftp/ruserpass.c b/usr/src/usr.bin/ftp/ruserpass.c index 9e5e49c289..94116f08a6 100644 --- a/usr/src/usr.bin/ftp/ruserpass.c +++ b/usr/src/usr.bin/ftp/ruserpass.c @@ -1,51 +1,44 @@ /* * Copyright (c) 1985 Regents of the University of California. - * All rights reserved. The Berkeley software License Agreement - * specifies the terms and conditions for redistribution. + * All rights reserved. + * + * Redistribution and use in source and binary forms are permitted + * provided that the above copyright notice and this paragraph are + * duplicated in all such forms and that any documentation, + * advertising materials, and other materials related to such + * distribution and use acknowledge that the software was developed + * by the University of California, Berkeley. The name of the + * University may not be used to endorse or promote products derived + * from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ #ifndef lint -static char sccsid[] = "@(#)ruserpass.c 1.2 (Berkeley) %G%"; -#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]; +static char sccsid[] = "@(#)ruserpass.c 5.1 (Berkeley) %G%"; +#endif /* not lint */ +#include #include #include #include -#include #include #include +#include "ftp_var.h" 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 MACHINE 11 +#define MACH 11 static char tokval[100]; @@ -56,63 +49,92 @@ static struct toktab { "default", DEFAULT, "login", LOGIN, "password", PASSWD, + "passwd", PASSWD, "account", ACCOUNT, - "machine", MACHINE, + "machine", MACH, "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, c; - int t, i; + char *hdir, buf[BUFSIZ], *tmp; + char myname[MAXHOSTNAMELEN], *mydomain; + int t, i, c, usedefault = 0; struct stat stb; extern int errno; hdir = getenv("HOME"); if (hdir == NULL) hdir = "."; - sprintf(buf, "%s/.netrc", hdir); + (void) sprintf(buf, "%s/.netrc", hdir); cfile = fopen(buf, "r"); if (cfile == NULL) { if (errno != ENOENT) 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) { case DEFAULT: - (void) token(); - continue; + usedefault = 1; + /* FALL THROUGH */ - case MACHINE: - if (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; - while ((t = token()) && t != MACHINE) switch(t) { + } + match: + while ((t = token()) && t != MACH && t != DEFAULT) switch(t) { case LOGIN: if (token()) if (*aname == 0) { - *aname = malloc(strlen(tokval) + 1); - strcpy(*aname, tokval); + *aname = malloc((unsigned) strlen(tokval) + 1); + (void) strcpy(*aname, tokval); } else { if (strcmp(*aname, tokval)) goto next; } 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"); - return(-1); + goto bad; } if (token() && *apass == 0) { - *apass = malloc(strlen(tokval) + 1); - strcpy(*apass, tokval); + *apass = malloc((unsigned) strlen(tokval) + 1); + (void) strcpy(*apass, tokval); } break; case ACCOUNT: @@ -120,25 +142,26 @@ next: && (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(strlen(tokval) + 1); - strcpy(*aacct, tokval); + *aacct = malloc((unsigned) strlen(tokval) + 1); + (void) strcpy(*aacct, tokval); } 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(-1); + goto bad; } if (macnum == 16) { printf("Limit of 16 macros have already been defined\n"); - return(-1); + goto bad; } tmp = macros[macnum].mac_name; *tmp++ = c; @@ -148,7 +171,7 @@ next: } if (c == EOF) { printf("Macro definition missing null line terminator.\n"); - return(-1); + goto bad; } *tmp = '\0'; if (c != '\n') { @@ -156,7 +179,7 @@ next: } if (c == EOF) { printf("Macro definition missing null line terminator.\n"); - return(-1); + goto bad; } if (macnum == 0) { macros[macnum].mac_start = macbuf; @@ -168,7 +191,7 @@ next: 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') { @@ -182,7 +205,7 @@ next: } if (tmp == macbuf + 4096) { printf("4K macro buffer exceeded\n"); - return(-1); + goto bad; } break; default: @@ -192,7 +215,11 @@ next: goto done; } done: - fclose(cfile); + (void) fclose(cfile); + return(0); +bad: + (void) fclose(cfile); + return(-1); } static