X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/1691e80890e5d8ba258c51c214a3e91880e1db2b..5977791e005446f4cd8b2f04bfb14c8cd265df8a:/sbr/ruserpass.c diff --git a/sbr/ruserpass.c b/sbr/ruserpass.c index 68809df4..1e43571e 100644 --- a/sbr/ruserpass.c +++ b/sbr/ruserpass.c @@ -1,4 +1,6 @@ -/* +/* ruserpass.c -- parse .netrc-format file. + * + * Portions of this code are * Copyright (c) 1985 Regents of the University of California. * All rights reserved. * @@ -14,19 +16,18 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id$ + * Portions of this code are Copyright (c) 2013, by the authors of + * nmh. See the COPYRIGHT file in the root directory of the nmh + * distribution for complete copyright information. */ #include +#include #include -#include static FILE *cfile; -#ifndef MAXHOSTNAMELEN -# define MAXHOSTNAMELEN 64 -#endif - +#define TOK_EOF 0 #define DEFAULT 1 #define LOGIN 2 #define PASSWD 3 @@ -35,7 +36,7 @@ static FILE *cfile; #define ID 10 #define MACH 11 -static char tokval[100]; +#define MAX_TOKVAL_SIZE 1024 /* Including terminating NUL. */ struct toktab { char *tokstr; @@ -43,6 +44,7 @@ struct toktab { }; static struct toktab toktabs[] = { + { "", TOK_EOF }, { "default", DEFAULT }, { "login", LOGIN }, { "password", PASSWD }, @@ -56,86 +58,85 @@ static struct toktab toktabs[] = { /* * prototypes */ -static int token(void); +static int token(char *); -int -ruserpass(char *host, char **aname, char **apass) +void +ruserpass(const char *host, char **aname, char **apass, int flags) { - char *hdir, buf[BUFSIZ]; int t, usedefault = 0; struct stat stb; - extern int errno; - hdir = getenv("HOME"); - if (hdir == NULL) - hdir = "."; - snprintf(buf, sizeof(buf), "%s/.netrc", hdir); - cfile = fopen(buf, "r"); + init_credentials_file (); + + cfile = fopen (credentials_file, "r"); if (cfile == NULL) { if (errno != ENOENT) - perror(buf); - goto done; - } - - while ((t = token())) { - switch(t) { - case DEFAULT: - usedefault = 1; - /* FALL THROUGH */ - - case MACH: - if (!usedefault) { - if (token() != ID) + perror (credentials_file); + } else { + char tokval[MAX_TOKVAL_SIZE]; + tokval[0] = '\0'; + + while ((t = token(tokval))) { + switch(t) { + case DEFAULT: + usedefault = 1; + /* FALLTHRU */ + + case MACH: + if (!usedefault) { + if (token(tokval) != ID) + continue; + /* + * Allow match either for user's host name. + */ + if (strcasecmp(host, tokval) == 0) + goto match; continue; - /* - * Allow match either for user's host name. - */ - if (strcasecmp(host, tokval) == 0) - goto match; - continue; - } -match: - while ((t = token()) && t != MACH && t != DEFAULT) { - switch(t) { - case LOGIN: - if (token() && *aname == 0) { - *aname = malloc((size_t) strlen(tokval) + 1); - strcpy(*aname, tokval); - } - break; - case PASSWD: - if (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"); - goto bad; - } - if (token() && *apass == 0) { - *apass = malloc((size_t) strlen(tokval) + 1); - strcpy(*apass, tokval); + } + match: + while ((t = token(tokval)) && t != MACH && t != DEFAULT) { + switch(t) { + case LOGIN: + if (token(tokval) && *aname == 0) + *aname = mh_xstrdup(tokval); + break; + + case PASSWD: + if (!credentials_no_perm_check && + fstat(fileno(cfile), &stb) >= 0 && + (stb.st_mode & 077) != 0) { + /* We make this a fatal error to force the + user to correct it. */ + inform("group or other permissions, %#o, " + "forbidden: %s", stb.st_mode, credentials_file); + adios(NULL, "Remove password or correct file " + "permissions."); + } + if (token(tokval) && *apass == 0) + *apass = mh_xstrdup(tokval); + break; + + case ACCOUNT: + break; + + case MACDEF: + fclose(cfile); + return; + + default: + fprintf(stderr, + "Unknown keyword %s in credentials file %s\n", + tokval, credentials_file); + break; } - break; - case ACCOUNT: - break; - - case MACDEF: - goto done_close; - break; - default: - fprintf(stderr, "Unknown .netrc keyword %s\n", tokval); - break; } + return; } - goto done; } } -done_close: - fclose(cfile); - -done: - if (!*aname) { + if (!*aname && ! (flags & RUSERPASS_NO_PROMPT_USER)) { char tmp[80]; char *myname; @@ -147,72 +148,77 @@ done: } printf("Name (%s:%s): ", host, myname); - fgets(tmp, sizeof(tmp) - 1, stdin); - tmp[strlen(tmp) - 1] = '\0'; - if (*tmp != '\0') { + if (fgets(tmp, sizeof tmp, stdin) == NULL) { + advise ("tmp", "fgets"); + } + trim_suffix_c(tmp, '\n'); + if (*tmp != '\0' || myname == NULL) { myname = tmp; } - *aname = malloc((size_t) strlen(myname) + 1); - strcpy (*aname, myname); + *aname = mh_xstrdup(myname); } - if (!*apass) { + if (!*apass && ! (flags & RUSERPASS_NO_PROMPT_PASSWORD)) { char prompt[256]; char *mypass; snprintf(prompt, sizeof(prompt), "Password (%s:%s): ", host, *aname); - mypass = getpass (prompt); - + mypass = nmh_getpass(prompt); + if (*mypass == '\0') { mypass = *aname; } - *apass = malloc((size_t) strlen(mypass) + 1); - strcpy (*apass, mypass); + *aname = mh_xstrdup(mypass); } - return(0); -bad: - fclose(cfile); - return(-1); } static int -token(void) +token(char *tokval) { - char *cp; int c; + const char normalStop[] = "\t\n ,"; /* Each breaks a word. */ + const char *stop; + char *cp; struct toktab *t; - if (feof(cfile)) - return (0); - while ((c = getc(cfile)) != EOF && - (c == '\n' || c == '\t' || c == ' ' || c == ',')) - continue; + if (feof(cfile) || ferror(cfile)) + return TOK_EOF; + + stop = normalStop; + while ((c = getc(cfile)) != EOF && c && strchr(stop, c)) + ; if (c == EOF) - return (0); + return TOK_EOF; + cp = tokval; - if (c == '"') { - while ((c = getc(cfile)) != EOF && c != '"') { - if (c == '\\') - c = getc(cfile); - *cp++ = c; - } - } else { - *cp++ = c; - while ((c = getc(cfile)) != EOF - && c != '\n' && c != '\t' && c != ' ' && c != ',') { - if (c == '\\') - c = getc(cfile); - *cp++ = c; - } + if (c == '"') + /* FIXME: Where is the quoted-string syntax of netrc documented? + * This code treats «"foo""bar"» as two tokens without further + * separators. */ + stop = "\""; + else + /* Might be backslash. Get it again later. It's handled then. */ + if (ungetc(c, cfile) == EOF) + return TOK_EOF; + + while ((c = getc(cfile)) != EOF && c && !strchr(stop, c)) { + if (c == '\\' && (c = getc(cfile)) == EOF) + return TOK_EOF; /* Discard whole token. */ + + *cp++ = c; + if (cp - tokval > MAX_TOKVAL_SIZE-1) { + adios(NULL, "credential tokens restricted to length %d", + MAX_TOKVAL_SIZE - 1); + } } - *cp = 0; - if (tokval[0] == 0) - return (0); + *cp = '\0'; + for (t = toktabs; t->tokstr; t++) if (!strcmp(t->tokstr, tokval)) return (t->tval); + return (ID); }