1 /* $NetBSD: ruserpass.c,v 1.21 1998/08/08 03:17:40 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.21 1998/08/08 03:17:40 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 __P((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(host, aname, apass, aacct) 88 const char *host; 89 const char **aname, **apass, **aacct; 90 { 91 char *hdir, buf[BUFSIZ], *tmp; 92 char myname[MAXHOSTNAMELEN + 1], *mydomain; 93 int t, i, c, usedefault = 0; 94 struct stat stb; 95 96 hdir = getenv("HOME"); 97 if (hdir == NULL) 98 hdir = "."; 99 if (strlen(hdir) + sizeof(".netrc") < sizeof(buf)) { 100 (void)snprintf(buf, sizeof(buf), "%s/.netrc", hdir); 101 } else { 102 warnx("%s/.netrc: %s", hdir, strerror(ENAMETOOLONG)); 103 return (0); 104 } 105 cfile = fopen(buf, "r"); 106 if (cfile == NULL) { 107 if (errno != ENOENT) 108 warn("%s", buf); 109 return (0); 110 } 111 if (gethostname(myname, sizeof(myname)) < 0) 112 myname[0] = '\0'; 113 myname[sizeof(myname) - 1] = '\0'; 114 if ((mydomain = strchr(myname, '.')) == NULL) 115 mydomain = ""; 116 next: 117 while ((t = token())) switch(t) { 118 119 case DEFAULT: 120 usedefault = 1; 121 /* FALL THROUGH */ 122 123 case MACH: 124 if (!usedefault) { 125 if (token() != ID) 126 continue; 127 /* 128 * Allow match either for user's input host name 129 * or official hostname. Also allow match of 130 * incompletely-specified host in local domain. 131 */ 132 if (strcasecmp(host, tokval) == 0) 133 goto match; 134 if (strcasecmp(hostname, tokval) == 0) 135 goto match; 136 if ((tmp = strchr(hostname, '.')) != NULL && 137 strcasecmp(tmp, mydomain) == 0 && 138 strncasecmp(hostname, tokval, tmp-hostname) == 0 && 139 tokval[tmp - hostname] == '\0') 140 goto match; 141 if ((tmp = strchr(host, '.')) != NULL && 142 strcasecmp(tmp, mydomain) == 0 && 143 strncasecmp(host, tokval, tmp - host) == 0 && 144 tokval[tmp - host] == '\0') 145 goto match; 146 continue; 147 } 148 match: 149 while ((t = token()) && t != MACH && t != DEFAULT) switch(t) { 150 151 case LOGIN: 152 if (token()) { 153 if (*aname == NULL) 154 *aname = xstrdup(tokval); 155 else { 156 if (strcmp(*aname, tokval)) 157 goto next; 158 } 159 } 160 break; 161 case PASSWD: 162 if ((*aname == NULL || strcmp(*aname, "anonymous")) && 163 fstat(fileno(cfile), &stb) >= 0 && 164 (stb.st_mode & 077) != 0) { 165 warnx("Error: .netrc file is readable by others."); 166 warnx("Remove password or make file unreadable by others."); 167 goto bad; 168 } 169 if (token() && *apass == NULL) 170 *apass = xstrdup(tokval); 171 break; 172 case ACCOUNT: 173 if (fstat(fileno(cfile), &stb) >= 0 174 && (stb.st_mode & 077) != 0) { 175 warnx("Error: .netrc file is readable by others."); 176 warnx("Remove account or make file unreadable by others."); 177 goto bad; 178 } 179 if (token() && *aacct == NULL) 180 *aacct = xstrdup(tokval); 181 break; 182 case MACDEF: 183 if (proxy) { 184 (void)fclose(cfile); 185 return (0); 186 } 187 while ((c = getc(cfile)) != EOF) 188 if (c != ' ' && c != '\t') 189 break; 190 if (c == EOF || c == '\n') { 191 fputs("Missing macdef name argument.\n", 192 ttyout); 193 goto bad; 194 } 195 if (macnum == 16) { 196 fputs( 197 "Limit of 16 macros have already been defined.\n", 198 ttyout); 199 goto bad; 200 } 201 tmp = macros[macnum].mac_name; 202 *tmp++ = c; 203 for (i = 0; i < 8 && (c = getc(cfile)) != EOF && 204 !isspace(c); ++i) { 205 *tmp++ = c; 206 } 207 if (c == EOF) { 208 fputs( 209 "Macro definition missing null line terminator.\n", 210 ttyout); 211 goto bad; 212 } 213 *tmp = '\0'; 214 if (c != '\n') { 215 while ((c = getc(cfile)) != EOF && c != '\n'); 216 } 217 if (c == EOF) { 218 fputs( 219 "Macro definition missing null line terminator.\n", 220 ttyout); 221 goto bad; 222 } 223 if (macnum == 0) { 224 macros[macnum].mac_start = macbuf; 225 } 226 else { 227 macros[macnum].mac_start = 228 macros[macnum-1].mac_end + 1; 229 } 230 tmp = macros[macnum].mac_start; 231 while (tmp != macbuf + 4096) { 232 if ((c = getc(cfile)) == EOF) { 233 fputs( 234 "Macro definition missing null line terminator.\n", 235 ttyout); 236 goto bad; 237 } 238 *tmp = c; 239 if (*tmp == '\n') { 240 if (*(tmp-1) == '\0') { 241 macros[macnum++].mac_end = tmp - 1; 242 break; 243 } 244 *tmp = '\0'; 245 } 246 tmp++; 247 } 248 if (tmp == macbuf + 4096) { 249 fputs("4K macro buffer exceeded.\n", 250 ttyout); 251 goto bad; 252 } 253 break; 254 default: 255 warnx("Unknown .netrc keyword %s", tokval); 256 break; 257 } 258 goto done; 259 } 260 done: 261 (void)fclose(cfile); 262 return (0); 263 bad: 264 (void)fclose(cfile); 265 return (-1); 266 } 267 268 static int 269 token() 270 { 271 char *cp; 272 int c; 273 struct toktab *t; 274 275 if (feof(cfile) || ferror(cfile)) 276 return (0); 277 while ((c = getc(cfile)) != EOF && 278 (c == '\n' || c == '\t' || c == ' ' || c == ',')) 279 continue; 280 if (c == EOF) 281 return (0); 282 cp = tokval; 283 if (c == '"') { 284 while ((c = getc(cfile)) != EOF && c != '"') { 285 if (c == '\\') 286 c = getc(cfile); 287 *cp++ = c; 288 } 289 } else { 290 *cp++ = c; 291 while ((c = getc(cfile)) != EOF 292 && c != '\n' && c != '\t' && c != ' ' && c != ',') { 293 if (c == '\\') 294 c = getc(cfile); 295 *cp++ = c; 296 } 297 } 298 *cp = 0; 299 if (tokval[0] == 0) 300 return (0); 301 for (t = toktab; t->tokstr; t++) 302 if (!strcmp(t->tokstr, tokval)) 303 return (t->tval); 304 return (ID); 305 } 306