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