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, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 static char sccsid[] = "@(#)ruserpass.c 5.3 (Berkeley) 3/1/91"; 36 #endif /* not lint */ 37 38 #include <sys/types.h> 39 #include <stdio.h> 40 #include <utmp.h> 41 #include <ctype.h> 42 #include <sys/stat.h> 43 #include <errno.h> 44 #include "ftp_var.h" 45 46 char *renvlook(), *malloc(), *index(), *getenv(), *getpass(), *getlogin(); 47 char *strcpy(); 48 struct utmp *getutmp(); 49 static FILE *cfile; 50 51 #define DEFAULT 1 52 #define LOGIN 2 53 #define PASSWD 3 54 #define ACCOUNT 4 55 #define MACDEF 5 56 #define ID 10 57 #define MACH 11 58 59 static char tokval[100]; 60 61 static struct toktab { 62 char *tokstr; 63 int tval; 64 } toktab[]= { 65 "default", DEFAULT, 66 "login", LOGIN, 67 "password", PASSWD, 68 "passwd", PASSWD, 69 "account", ACCOUNT, 70 "machine", MACH, 71 "macdef", MACDEF, 72 0, 0 73 }; 74 75 ruserpass(host, aname, apass, aacct) 76 char *host, **aname, **apass, **aacct; 77 { 78 char *hdir, buf[BUFSIZ], *tmp; 79 char myname[MAXHOSTNAMELEN], *mydomain; 80 int t, i, c, usedefault = 0; 81 struct stat stb; 82 static int token(); 83 84 hdir = getenv("HOME"); 85 if (hdir == NULL) 86 hdir = "."; 87 (void) sprintf(buf, "%s/.netrc", hdir); 88 cfile = fopen(buf, "r"); 89 if (cfile == NULL) { 90 if (errno != ENOENT) 91 perror(buf); 92 return(0); 93 } 94 if (gethostname(myname, sizeof(myname)) < 0) 95 myname[0] = '\0'; 96 if ((mydomain = index(myname, '.')) == NULL) 97 mydomain = ""; 98 next: 99 while ((t = token())) switch(t) { 100 101 case DEFAULT: 102 usedefault = 1; 103 /* FALL THROUGH */ 104 105 case MACH: 106 if (!usedefault) { 107 if (token() != ID) 108 continue; 109 /* 110 * Allow match either for user's input host name 111 * or official hostname. Also allow match of 112 * incompletely-specified host in local domain. 113 */ 114 if (strcasecmp(host, tokval) == 0) 115 goto match; 116 if (strcasecmp(hostname, tokval) == 0) 117 goto match; 118 if ((tmp = index(hostname, '.')) != NULL && 119 strcasecmp(tmp, mydomain) == 0 && 120 strncasecmp(hostname, tokval, tmp-hostname) == 0 && 121 tokval[tmp - hostname] == '\0') 122 goto match; 123 if ((tmp = index(host, '.')) != NULL && 124 strcasecmp(tmp, mydomain) == 0 && 125 strncasecmp(host, tokval, tmp - host) == 0 && 126 tokval[tmp - host] == '\0') 127 goto match; 128 continue; 129 } 130 match: 131 while ((t = token()) && t != MACH && t != DEFAULT) switch(t) { 132 133 case LOGIN: 134 if (token()) 135 if (*aname == 0) { 136 *aname = malloc((unsigned) strlen(tokval) + 1); 137 (void) strcpy(*aname, tokval); 138 } else { 139 if (strcmp(*aname, tokval)) 140 goto next; 141 } 142 break; 143 case PASSWD: 144 if (strcmp(*aname, "anonymous") && 145 fstat(fileno(cfile), &stb) >= 0 && 146 (stb.st_mode & 077) != 0) { 147 fprintf(stderr, "Error - .netrc file not correct mode.\n"); 148 fprintf(stderr, "Remove password or correct mode.\n"); 149 goto bad; 150 } 151 if (token() && *apass == 0) { 152 *apass = malloc((unsigned) strlen(tokval) + 1); 153 (void) strcpy(*apass, tokval); 154 } 155 break; 156 case ACCOUNT: 157 if (fstat(fileno(cfile), &stb) >= 0 158 && (stb.st_mode & 077) != 0) { 159 fprintf(stderr, "Error - .netrc file not correct mode.\n"); 160 fprintf(stderr, "Remove account or correct mode.\n"); 161 goto bad; 162 } 163 if (token() && *aacct == 0) { 164 *aacct = malloc((unsigned) strlen(tokval) + 1); 165 (void) strcpy(*aacct, tokval); 166 } 167 break; 168 case MACDEF: 169 if (proxy) { 170 (void) fclose(cfile); 171 return(0); 172 } 173 while ((c=getc(cfile)) != EOF && c == ' ' || c == '\t'); 174 if (c == EOF || c == '\n') { 175 printf("Missing macdef name argument.\n"); 176 goto bad; 177 } 178 if (macnum == 16) { 179 printf("Limit of 16 macros have already been defined\n"); 180 goto bad; 181 } 182 tmp = macros[macnum].mac_name; 183 *tmp++ = c; 184 for (i=0; i < 8 && (c=getc(cfile)) != EOF && 185 !isspace(c); ++i) { 186 *tmp++ = c; 187 } 188 if (c == EOF) { 189 printf("Macro definition missing null line terminator.\n"); 190 goto bad; 191 } 192 *tmp = '\0'; 193 if (c != '\n') { 194 while ((c=getc(cfile)) != EOF && c != '\n'); 195 } 196 if (c == EOF) { 197 printf("Macro definition missing null line terminator.\n"); 198 goto bad; 199 } 200 if (macnum == 0) { 201 macros[macnum].mac_start = macbuf; 202 } 203 else { 204 macros[macnum].mac_start = macros[macnum-1].mac_end + 1; 205 } 206 tmp = macros[macnum].mac_start; 207 while (tmp != macbuf + 4096) { 208 if ((c=getc(cfile)) == EOF) { 209 printf("Macro definition missing null line terminator.\n"); 210 goto bad; 211 } 212 *tmp = c; 213 if (*tmp == '\n') { 214 if (*(tmp-1) == '\0') { 215 macros[macnum++].mac_end = tmp - 1; 216 break; 217 } 218 *tmp = '\0'; 219 } 220 tmp++; 221 } 222 if (tmp == macbuf + 4096) { 223 printf("4K macro buffer exceeded\n"); 224 goto bad; 225 } 226 break; 227 default: 228 fprintf(stderr, "Unknown .netrc keyword %s\n", tokval); 229 break; 230 } 231 goto done; 232 } 233 done: 234 (void) fclose(cfile); 235 return(0); 236 bad: 237 (void) fclose(cfile); 238 return(-1); 239 } 240 241 static 242 token() 243 { 244 char *cp; 245 int c; 246 struct toktab *t; 247 248 if (feof(cfile)) 249 return (0); 250 while ((c = getc(cfile)) != EOF && 251 (c == '\n' || c == '\t' || c == ' ' || c == ',')) 252 continue; 253 if (c == EOF) 254 return (0); 255 cp = tokval; 256 if (c == '"') { 257 while ((c = getc(cfile)) != EOF && c != '"') { 258 if (c == '\\') 259 c = getc(cfile); 260 *cp++ = c; 261 } 262 } else { 263 *cp++ = c; 264 while ((c = getc(cfile)) != EOF 265 && c != '\n' && c != '\t' && c != ' ' && c != ',') { 266 if (c == '\\') 267 c = getc(cfile); 268 *cp++ = c; 269 } 270 } 271 *cp = 0; 272 if (tokval[0] == 0) 273 return (0); 274 for (t = toktab; t->tokstr; t++) 275 if (!strcmp(t->tokstr, tokval)) 276 return (t->tval); 277 return (ID); 278 } 279