2 * Portions of this code are
3 * Copyright (c) 1985 Regents of the University of California.
6 * Redistribution and use in source and binary forms are permitted
7 * provided that the above copyright notice and this paragraph are
8 * duplicated in all such forms and that any documentation,
9 * advertising materials, and other materials related to such
10 * distribution and use acknowledge that the software was developed
11 * by the University of California, Berkeley. The name of the
12 * University may not be used to endorse or promote products derived
13 * from this software without specific prior written permission.
14 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27 #ifndef MAXHOSTNAMELEN
28 # define MAXHOSTNAMELEN 64
39 static char tokval[100];
46 static struct toktab toktabs[] = {
47 { "default", DEFAULT },
49 { "password", PASSWD },
51 { "account", ACCOUNT },
60 static int token(void);
64 ruserpass(char *host, char **aname, char **apass)
66 char *hdir, buf[BUFSIZ];
67 int t, usedefault = 0;
71 hdir = getenv("HOME");
74 snprintf(buf, sizeof(buf), "%s/.netrc", hdir);
75 cfile = fopen(buf, "r");
82 while ((t = token())) {
93 * Allow match either for user's host name.
95 if (strcasecmp(host, tokval) == 0)
100 while ((t = token()) && t != MACH && t != DEFAULT) {
103 if (token() && *aname == 0) {
104 *aname = malloc((size_t) strlen(tokval) + 1);
105 strcpy(*aname, tokval);
109 if (fstat(fileno(cfile), &stb) >= 0 &&
110 (stb.st_mode & 077) != 0) {
111 fprintf(stderr, "Error - .netrc file not correct mode.\n");
112 fprintf(stderr, "Remove password or correct mode.\n");
115 if (token() && *apass == 0) {
116 *apass = malloc((size_t) strlen(tokval) + 1);
117 strcpy(*apass, tokval);
127 fprintf(stderr, "Unknown .netrc keyword %s\n", tokval);
143 if ((myname = getlogin()) == NULL) {
146 if ((pp = getpwuid (getuid())) != NULL)
147 myname = pp->pw_name;
149 printf("Name (%s:%s): ", host, myname);
151 fgets(tmp, sizeof(tmp) - 1, stdin);
152 tmp[strlen(tmp) - 1] = '\0';
157 *aname = malloc((size_t) strlen(myname) + 1);
158 strcpy (*aname, myname);
165 snprintf(prompt, sizeof(prompt), "Password (%s:%s): ", host, *aname);
166 mypass = nmh_getpass(prompt);
168 if (*mypass == '\0') {
172 *apass = malloc((size_t) strlen(mypass) + 1);
173 strcpy (*apass, mypass);
191 while ((c = getc(cfile)) != EOF &&
192 (c == '\n' || c == '\t' || c == ' ' || c == ','))
198 while ((c = getc(cfile)) != EOF && c != '"') {
205 while ((c = getc(cfile)) != EOF
206 && c != '\n' && c != '\t' && c != ' ' && c != ',') {
215 for (t = toktabs; t->tokstr; t++)
216 if (!strcmp(t->tokstr, tokval))