|
|
1.1 root 1: /*
2: * Copyright (c) 1985 Regents of the University of California.
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms are permitted
6: * provided that the above copyright notice and this paragraph are
7: * duplicated in all such forms and that any documentation,
8: * advertising materials, and other materials related to such
9: * distribution and use acknowledge that the software was developed
10: * by the University of California, Berkeley. The name of the
11: * University may not be used to endorse or promote products derived
12: * from this software without specific prior written permission.
13: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16: */
17:
18: #ifndef lint
19: static char sccsid[] = "@(#)ruserpass.c 5.1 (Berkeley) 3/1/89";
20: #endif /* not lint */
21:
22: #include <sys/types.h>
23: #include <stdio.h>
24: #include <utmp.h>
25: #include <ctype.h>
26: #include <sys/stat.h>
27: #include <string.h>
28: #include <errno.h>
29: #include "ftp_var.h"
30:
31: char *renvlook(), *malloc(), *getenv(), *getpass(), *getlogin();
32: char *strcpy();
33: struct utmp *getutmp();
34: static FILE *cfile;
35:
36: #define DEFAULT 1
37: #define LOGIN 2
38: #define PASSWD 3
39: #define ACCOUNT 4
40: #define MACDEF 5
41: #define ID 10
42: #define MACH 11
43:
44: static char tokval[100];
45:
46: static struct toktab {
47: char *tokstr;
48: int tval;
49: } toktab[]= {
50: "default", DEFAULT,
51: "login", LOGIN,
52: "password", PASSWD,
53: "passwd", PASSWD,
54: "account", ACCOUNT,
55: "machine", MACH,
56: "macdef", MACDEF,
57: 0, 0
58: };
59:
60: ruserpass(host, aname, apass, aacct)
61: char *host, **aname, **apass, **aacct;
62: {
63: char *hdir, buf[BUFSIZ], *tmp;
64: char myname[MAXHOSTNAMELEN], *mydomain;
65: int t, i, c, usedefault = 0;
66: struct stat stb;
67: extern int errno;
68:
69: hdir = getenv("HOME");
70: if (hdir == NULL)
71: hdir = ".";
72: (void) sprintf(buf, "%s/.netrc", hdir);
73: cfile = fopen(buf, "r");
74: if (cfile == NULL) {
75: if (errno != ENOENT)
76: perror(buf);
77: return(0);
78: }
79: #ifdef REMOVE
80: if (gethostname(myname, sizeof(myname)) < 0)
81: #endif
82: myname[0] = '\0';
83: if ((mydomain = strchr(myname, '.')) == NULL)
84: mydomain = "";
85: next:
86: while ((t = token())) switch(t) {
87:
88: case DEFAULT:
89: usedefault = 1;
90: /* FALL THROUGH */
91:
92: case MACH:
93: if (!usedefault) {
94: if (token() != ID)
95: continue;
96: /*
97: * Allow match either for user's input host name
98: * or official hostname. Also allow match of
99: * incompletely-specified host in local domain.
100: */
101: if (strcasecmp(host, tokval) == 0)
102: goto match;
103: if (strcasecmp(hostname, tokval) == 0)
104: goto match;
105: if ((tmp = strchr(hostname, '.')) != NULL &&
106: strcasecmp(tmp, mydomain) == 0 &&
107: strncasecmp(hostname, tokval, tmp-hostname) == 0 &&
108: tokval[tmp - hostname] == '\0')
109: goto match;
110: if ((tmp = strchr(host, '.')) != NULL &&
111: strcasecmp(tmp, mydomain) == 0 &&
112: strncasecmp(host, tokval, tmp - host) == 0 &&
113: tokval[tmp - host] == '\0')
114: goto match;
115: continue;
116: }
117: match:
118: while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
119:
120: case LOGIN:
121: if (token())
122: if (*aname == 0) {
123: *aname = malloc((unsigned) strlen(tokval) + 1);
124: (void) strcpy(*aname, tokval);
125: } else {
126: if (strcmp(*aname, tokval))
127: goto next;
128: }
129: break;
130: case PASSWD:
131: if (strcmp(*aname, "anonymous") &&
132: fstat(fileno(cfile), &stb) >= 0 &&
133: (stb.st_mode & 077) != 0) {
134: fprintf(stderr, "Error - .netrc file not correct mode.\n");
135: fprintf(stderr, "Remove password or correct mode.\n");
136: goto bad;
137: }
138: if (token() && *apass == 0) {
139: *apass = malloc((unsigned) strlen(tokval) + 1);
140: (void) strcpy(*apass, tokval);
141: }
142: break;
143: case ACCOUNT:
144: if (fstat(fileno(cfile), &stb) >= 0
145: && (stb.st_mode & 077) != 0) {
146: fprintf(stderr, "Error - .netrc file not correct mode.\n");
147: fprintf(stderr, "Remove account or correct mode.\n");
148: goto bad;
149: }
150: if (token() && *aacct == 0) {
151: *aacct = malloc((unsigned) strlen(tokval) + 1);
152: (void) strcpy(*aacct, tokval);
153: }
154: break;
155: case MACDEF:
156: if (proxy) {
157: (void) fclose(cfile);
158: return(0);
159: }
160: while ((c=getc(cfile)) != EOF && c == ' ' || c == '\t');
161: if (c == EOF || c == '\n') {
162: printf("Missing macdef name argument.\n");
163: goto bad;
164: }
165: if (macnum == 16) {
166: printf("Limit of 16 macros have already been defined\n");
167: goto bad;
168: }
169: tmp = macros[macnum].mac_name;
170: *tmp++ = c;
171: for (i=0; i < 8 && (c=getc(cfile)) != EOF &&
172: !isspace(c); ++i) {
173: *tmp++ = c;
174: }
175: if (c == EOF) {
176: printf("Macro definition missing null line terminator.\n");
177: goto bad;
178: }
179: *tmp = '\0';
180: if (c != '\n') {
181: while ((c=getc(cfile)) != EOF && c != '\n');
182: }
183: if (c == EOF) {
184: printf("Macro definition missing null line terminator.\n");
185: goto bad;
186: }
187: if (macnum == 0) {
188: macros[macnum].mac_start = macbuf;
189: }
190: else {
191: macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
192: }
193: tmp = macros[macnum].mac_start;
194: while (tmp != macbuf + 4096) {
195: if ((c=getc(cfile)) == EOF) {
196: printf("Macro definition missing null line terminator.\n");
197: goto bad;
198: }
199: *tmp = c;
200: if (*tmp == '\n') {
201: if (*(tmp-1) == '\0') {
202: macros[macnum++].mac_end = tmp - 1;
203: break;
204: }
205: *tmp = '\0';
206: }
207: tmp++;
208: }
209: if (tmp == macbuf + 4096) {
210: printf("4K macro buffer exceeded\n");
211: goto bad;
212: }
213: break;
214: default:
215: fprintf(stderr, "Unknown .netrc keyword %s\n", tokval);
216: break;
217: }
218: goto done;
219: }
220: done:
221: (void) fclose(cfile);
222: return(0);
223: bad:
224: (void) fclose(cfile);
225: return(-1);
226: }
227:
228: static
229: token()
230: {
231: char *cp;
232: int c;
233: struct toktab *t;
234:
235: if (feof(cfile))
236: return (0);
237: while ((c = getc(cfile)) != EOF &&
238: (c == '\n' || c == '\t' || c == ' ' || c == ','))
239: continue;
240: if (c == EOF)
241: return (0);
242: cp = tokval;
243: if (c == '"') {
244: while ((c = getc(cfile)) != EOF && c != '"') {
245: if (c == '\\')
246: c = getc(cfile);
247: *cp++ = c;
248: }
249: } else {
250: *cp++ = c;
251: while ((c = getc(cfile)) != EOF
252: && c != '\n' && c != '\t' && c != ' ' && c != ',') {
253: if (c == '\\')
254: c = getc(cfile);
255: *cp++ = c;
256: }
257: }
258: *cp = 0;
259: if (tokval[0] == 0)
260: return (0);
261: for (t = toktab; t->tokstr; t++)
262: if (!strcmp(t->tokstr, tokval))
263: return (t->tval);
264: return (ID);
265: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.