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