1 /* 2 * Copyright (c) 1985, 1993, 1994 3 * The Regents of the University of California. 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[] = "from: @(#)ruserpass.c 8.3 (Berkeley) 4/2/94";*/ 36 static char *rcsid = "$Id: ruserpass.c,v 1.5 1994/09/17 00:51:10 mycroft Exp $"; 37 #endif /* not lint */ 38 39 #include <sys/types.h> 40 #include <sys/stat.h> 41 42 #include <ctype.h> 43 #include <err.h> 44 #include <errno.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include <unistd.h> 49 50 #include "ftp_var.h" 51 52 static int token __P((void)); 53 static FILE *cfile; 54 55 #define DEFAULT 1 56 #define LOGIN 2 57 #define PASSWD 3 58 #define ACCOUNT 4 59 #define MACDEF 5 60 #define ID 10 61 #define MACH 11 62 63 static char tokval[100]; 64 65 static struct toktab { 66 char *tokstr; 67 int tval; 68 } toktab[]= { 69 { "default", DEFAULT }, 70 { "login", LOGIN }, 71 { "password", PASSWD }, 72 { "passwd", PASSWD }, 73 { "account", ACCOUNT }, 74 { "machine", MACH }, 75 { "macdef", MACDEF }, 76 { NULL, 0 } 77 }; 78 79 int 80 ruserpass(host, aname, apass, aacct) 81 char *host, **aname, **apass, **aacct; 82 { 83 char *hdir, buf[BUFSIZ], *tmp; 84 char myname[MAXHOSTNAMELEN], *mydomain; 85 int t, i, c, usedefault = 0; 86 struct stat stb; 87 88 hdir = getenv("HOME"); 89 if (hdir == NULL) 90 hdir = "."; 91 (void) sprintf(buf, "%s/.netrc", hdir); 92 cfile = fopen(buf, "r"); 93 if (cfile == NULL) { 94 if (errno != ENOENT) 95 warn("%s", buf); 96 return (0); 97 } 98 if (gethostname(myname, sizeof(myname)) < 0) 99 myname[0] = '\0'; 100 if ((mydomain = strchr(myname, '.')) == NULL) 101 mydomain = ""; 102 next: 103 while ((t = token())) switch(t) { 104 105 case DEFAULT: 106 usedefault = 1; 107 /* FALL THROUGH */ 108 109 case MACH: 110 if (!usedefault) { 111 if (token() != ID) 112 continue; 113 /* 114 * Allow match either for user's input host name 115 * or official hostname. Also allow match of 116 * incompletely-specified host in local domain. 117 */ 118 if (strcasecmp(host, tokval) == 0) 119 goto match; 120 if (strcasecmp(hostname, tokval) == 0) 121 goto match; 122 if ((tmp = strchr(hostname, '.')) != NULL && 123 strcasecmp(tmp, mydomain) == 0 && 124 strncasecmp(hostname, tokval, tmp-hostname) == 0 && 125 tokval[tmp - hostname] == '\0') 126 goto match; 127 if ((tmp = strchr(host, '.')) != NULL && 128 strcasecmp(tmp, mydomain) == 0 && 129 strncasecmp(host, tokval, tmp - host) == 0 && 130 tokval[tmp - host] == '\0') 131 goto match; 132 continue; 133 } 134 match: 135 while ((t = token()) && t != MACH && t != DEFAULT) switch(t) { 136 137 case LOGIN: 138 if (token()) 139 if (*aname == 0) { 140 *aname = malloc((unsigned) strlen(tokval) + 1); 141 (void) strcpy(*aname, tokval); 142 } else { 143 if (strcmp(*aname, tokval)) 144 goto next; 145 } 146 break; 147 case PASSWD: 148 if ((*aname == 0 || strcmp(*aname, "anonymous")) && 149 fstat(fileno(cfile), &stb) >= 0 && 150 (stb.st_mode & 077) != 0) { 151 warnx("Error: .netrc file is readable by others."); 152 warnx("Remove password or make file unreadable by others."); 153 goto bad; 154 } 155 if (token() && *apass == 0) { 156 *apass = malloc((unsigned) strlen(tokval) + 1); 157 (void) strcpy(*apass, tokval); 158 } 159 break; 160 case ACCOUNT: 161 if (fstat(fileno(cfile), &stb) >= 0 162 && (stb.st_mode & 077) != 0) { 163 warnx("Error: .netrc file is readable by others."); 164 warnx("Remove account or make file unreadable by others."); 165 goto bad; 166 } 167 if (token() && *aacct == 0) { 168 *aacct = malloc((unsigned) strlen(tokval) + 1); 169 (void) strcpy(*aacct, tokval); 170 } 171 break; 172 case MACDEF: 173 if (proxy) { 174 (void) fclose(cfile); 175 return (0); 176 } 177 while ((c=getc(cfile)) != EOF && c == ' ' || c == '\t'); 178 if (c == EOF || c == '\n') { 179 printf("Missing macdef name argument.\n"); 180 goto bad; 181 } 182 if (macnum == 16) { 183 printf("Limit of 16 macros have already been defined\n"); 184 goto bad; 185 } 186 tmp = macros[macnum].mac_name; 187 *tmp++ = c; 188 for (i=0; i < 8 && (c=getc(cfile)) != EOF && 189 !isspace(c); ++i) { 190 *tmp++ = c; 191 } 192 if (c == EOF) { 193 printf("Macro definition missing null line terminator.\n"); 194 goto bad; 195 } 196 *tmp = '\0'; 197 if (c != '\n') { 198 while ((c=getc(cfile)) != EOF && c != '\n'); 199 } 200 if (c == EOF) { 201 printf("Macro definition missing null line terminator.\n"); 202 goto bad; 203 } 204 if (macnum == 0) { 205 macros[macnum].mac_start = macbuf; 206 } 207 else { 208 macros[macnum].mac_start = macros[macnum-1].mac_end + 1; 209 } 210 tmp = macros[macnum].mac_start; 211 while (tmp != macbuf + 4096) { 212 if ((c=getc(cfile)) == EOF) { 213 printf("Macro definition missing null line terminator.\n"); 214 goto bad; 215 } 216 *tmp = c; 217 if (*tmp == '\n') { 218 if (*(tmp-1) == '\0') { 219 macros[macnum++].mac_end = tmp - 1; 220 break; 221 } 222 *tmp = '\0'; 223 } 224 tmp++; 225 } 226 if (tmp == macbuf + 4096) { 227 printf("4K macro buffer exceeded\n"); 228 goto bad; 229 } 230 break; 231 default: 232 warnx("Unknown .netrc keyword %s", tokval); 233 break; 234 } 235 goto done; 236 } 237 done: 238 (void) fclose(cfile); 239 return (0); 240 bad: 241 (void) fclose(cfile); 242 return (-1); 243 } 244 245 static int 246 token() 247 { 248 char *cp; 249 int c; 250 struct toktab *t; 251 252 if (feof(cfile) || ferror(cfile)) 253 return (0); 254 while ((c = getc(cfile)) != EOF && 255 (c == '\n' || c == '\t' || c == ' ' || c == ',')) 256 continue; 257 if (c == EOF) 258 return (0); 259 cp = tokval; 260 if (c == '"') { 261 while ((c = getc(cfile)) != EOF && c != '"') { 262 if (c == '\\') 263 c = getc(cfile); 264 *cp++ = c; 265 } 266 } else { 267 *cp++ = c; 268 while ((c = getc(cfile)) != EOF 269 && c != '\n' && c != '\t' && c != ' ' && c != ',') { 270 if (c == '\\') 271 c = getc(cfile); 272 *cp++ = c; 273 } 274 } 275 *cp = 0; 276 if (tokval[0] == 0) 277 return (0); 278 for (t = toktab; t->tokstr; t++) 279 if (!strcmp(t->tokstr, tokval)) 280 return (t->tval); 281 return (ID); 282 } 283