ruserpass.c (223308) | ruserpass.c (223328) |
---|---|
1/* $NetBSD: ruserpass.c,v 1.29 2003/08/07 11:13:57 agc Exp $ */ | 1/* $NetBSD: ruserpass.c,v 1.8 2007/08/06 04:33:24 lukem Exp $ */ 2/* from NetBSD: ruserpass.c,v 1.33 2007/04/17 05:52:04 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: --- 14 unchanged lines hidden (view full) --- 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 | 3 4/* 5 * Copyright (c) 1985, 1993, 1994 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: --- 14 unchanged lines hidden (view full) --- 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 |
33#include "tnftp.h" 34 35#if 0 /* tnftp */ 36 |
|
32#include <sys/cdefs.h> 33#ifndef lint 34#if 0 35static char sccsid[] = "@(#)ruserpass.c 8.4 (Berkeley) 4/27/95"; 36#else | 37#include <sys/cdefs.h> 38#ifndef lint 39#if 0 40static char sccsid[] = "@(#)ruserpass.c 8.4 (Berkeley) 4/27/95"; 41#else |
37__RCSID("$NetBSD: ruserpass.c,v 1.29 2003/08/07 11:13:57 agc Exp $"); | 42__RCSID(" NetBSD: ruserpass.c,v 1.33 2007/04/17 05:52:04 lukem Exp "); |
38#endif 39#endif /* not lint */ 40 41#include <sys/types.h> 42#include <sys/stat.h> 43 44#include <ctype.h> 45#include <err.h> 46#include <errno.h> 47#include <netdb.h> 48#include <stdio.h> 49#include <stdlib.h> 50#include <string.h> 51#include <unistd.h> 52 | 43#endif 44#endif /* not lint */ 45 46#include <sys/types.h> 47#include <sys/stat.h> 48 49#include <ctype.h> 50#include <err.h> 51#include <errno.h> 52#include <netdb.h> 53#include <stdio.h> 54#include <stdlib.h> 55#include <string.h> 56#include <unistd.h> 57 |
58#endif /* tnftp */ 59 |
|
53#include "ftp_var.h" 54 55static int token(void); 56static FILE *cfile; 57 58#define DEFAULT 1 59#define LOGIN 2 60#define PASSWD 3 61#define ACCOUNT 4 62#define MACDEF 5 63#define ID 10 64#define MACH 11 65 66static char tokval[100]; 67 68static struct toktab { | 60#include "ftp_var.h" 61 62static int token(void); 63static FILE *cfile; 64 65#define DEFAULT 1 66#define LOGIN 2 67#define PASSWD 3 68#define ACCOUNT 4 69#define MACDEF 5 70#define ID 10 71#define MACH 11 72 73static char tokval[100]; 74 75static struct toktab { |
69 char *tokstr; | 76 const char *tokstr; |
70 int tval; 71} toktab[] = { 72 { "default", DEFAULT }, 73 { "login", LOGIN }, 74 { "password", PASSWD }, 75 { "passwd", PASSWD }, 76 { "account", ACCOUNT }, 77 { "machine", MACH }, 78 { "macdef", MACDEF }, 79 { NULL, 0 } 80}; 81 82int | 77 int tval; 78} toktab[] = { 79 { "default", DEFAULT }, 80 { "login", LOGIN }, 81 { "password", PASSWD }, 82 { "passwd", PASSWD }, 83 { "account", ACCOUNT }, 84 { "machine", MACH }, 85 { "macdef", MACDEF }, 86 { NULL, 0 } 87}; 88 89int |
83ruserpass(const char *host, const char **aname, const char **apass, 84 const char **aacct) | 90ruserpass(const char *host, char **aname, char **apass, char **aacct) |
85{ 86 char *tmp; | 91{ 92 char *tmp; |
87 char myname[MAXHOSTNAMELEN + 1], *mydomain; | 93 const char *mydomain; 94 char myname[MAXHOSTNAMELEN + 1]; |
88 int t, i, c, usedefault = 0; 89 struct stat stb; 90 91 if (netrc[0] == '\0') 92 return (0); 93 cfile = fopen(netrc, "r"); 94 if (cfile == NULL) { 95 if (errno != ENOENT) | 95 int t, i, c, usedefault = 0; 96 struct stat stb; 97 98 if (netrc[0] == '\0') 99 return (0); 100 cfile = fopen(netrc, "r"); 101 if (cfile == NULL) { 102 if (errno != ENOENT) |
96 warn("%s", netrc); | 103 warn("Can't read `%s'", netrc); |
97 return (0); 98 } 99 if (gethostname(myname, sizeof(myname)) < 0) 100 myname[0] = '\0'; 101 myname[sizeof(myname) - 1] = '\0'; 102 if ((mydomain = strchr(myname, '.')) == NULL) 103 mydomain = ""; 104 next: | 104 return (0); 105 } 106 if (gethostname(myname, sizeof(myname)) < 0) 107 myname[0] = '\0'; 108 myname[sizeof(myname) - 1] = '\0'; 109 if ((mydomain = strchr(myname, '.')) == NULL) 110 mydomain = ""; 111 next: |
105 while ((t = token())) switch(t) { | 112 while ((t = token()) > 0) switch(t) { |
106 107 case DEFAULT: 108 usedefault = 1; 109 /* FALL THROUGH */ 110 111 case MACH: 112 if (!usedefault) { | 113 114 case DEFAULT: 115 usedefault = 1; 116 /* FALL THROUGH */ 117 118 case MACH: 119 if (!usedefault) { |
113 if (token() != ID) | 120 if ((t = token()) == -1) 121 goto bad; 122 if (t != ID) |
114 continue; 115 /* 116 * Allow match either for user's input host name 117 * or official hostname. Also allow match of 118 * incompletely-specified host in local domain. 119 */ 120 if (strcasecmp(host, tokval) == 0) 121 goto match; --- 7 unchanged lines hidden (view full) --- 129 if ((tmp = strchr(host, '.')) != NULL && 130 strcasecmp(tmp, mydomain) == 0 && 131 strncasecmp(host, tokval, tmp - host) == 0 && 132 tokval[tmp - host] == '\0') 133 goto match; 134 continue; 135 } 136 match: | 123 continue; 124 /* 125 * Allow match either for user's input host name 126 * or official hostname. Also allow match of 127 * incompletely-specified host in local domain. 128 */ 129 if (strcasecmp(host, tokval) == 0) 130 goto match; --- 7 unchanged lines hidden (view full) --- 138 if ((tmp = strchr(host, '.')) != NULL && 139 strcasecmp(tmp, mydomain) == 0 && 140 strncasecmp(host, tokval, tmp - host) == 0 && 141 tokval[tmp - host] == '\0') 142 goto match; 143 continue; 144 } 145 match: |
137 while ((t = token()) && t != MACH && t != DEFAULT) switch(t) { | 146 while ((t = token()) > 0 && 147 t != MACH && t != DEFAULT) switch(t) { |
138 139 case LOGIN: | 148 149 case LOGIN: |
140 if (token()) { | 150 if ((t = token()) == -1) 151 goto bad; 152 if (t) { |
141 if (*aname == NULL) | 153 if (*aname == NULL) |
142 *aname = xstrdup(tokval); | 154 *aname = ftp_strdup(tokval); |
143 else { 144 if (strcmp(*aname, tokval)) 145 goto next; 146 } 147 } 148 break; 149 case PASSWD: 150 if ((*aname == NULL || strcmp(*aname, "anonymous")) && 151 fstat(fileno(cfile), &stb) >= 0 && 152 (stb.st_mode & 077) != 0) { | 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) { |
153 warnx("Error: .netrc file is readable by others."); 154 warnx("Remove password or make file unreadable by others."); | 165 warnx("Error: .netrc file is readable by others"); 166 warnx("Remove password or make file unreadable by others"); |
155 goto bad; 156 } | 167 goto bad; 168 } |
157 if (token() && *apass == NULL) 158 *apass = xstrdup(tokval); | 169 if ((t = token()) == -1) 170 goto bad; 171 if (t && *apass == NULL) 172 *apass = ftp_strdup(tokval); |
159 break; 160 case ACCOUNT: 161 if (fstat(fileno(cfile), &stb) >= 0 162 && (stb.st_mode & 077) != 0) { | 173 break; 174 case ACCOUNT: 175 if (fstat(fileno(cfile), &stb) >= 0 176 && (stb.st_mode & 077) != 0) { |
163 warnx("Error: .netrc file is readable by others."); 164 warnx("Remove account or make file unreadable by others."); | 177 warnx("Error: .netrc file is readable by others"); 178 warnx("Remove account or make file unreadable by others"); |
165 goto bad; 166 } | 179 goto bad; 180 } |
167 if (token() && *aacct == NULL) 168 *aacct = xstrdup(tokval); | 181 if ((t = token()) == -1) 182 goto bad; 183 if (t && *aacct == NULL) 184 *aacct = ftp_strdup(tokval); |
169 break; 170 case MACDEF: 171 if (proxy) { 172 (void)fclose(cfile); 173 return (0); 174 } 175 while ((c = getc(cfile)) != EOF) 176 if (c != ' ' && c != '\t') --- 43 unchanged lines hidden (view full) --- 220 if ((c = getc(cfile)) == EOF) { 221 fputs( 222 "Macro definition missing null line terminator.\n", 223 ttyout); 224 goto bad; 225 } 226 *tmp = c; 227 if (*tmp == '\n') { | 185 break; 186 case MACDEF: 187 if (proxy) { 188 (void)fclose(cfile); 189 return (0); 190 } 191 while ((c = getc(cfile)) != EOF) 192 if (c != ' ' && c != '\t') --- 43 unchanged lines hidden (view full) --- 236 if ((c = getc(cfile)) == EOF) { 237 fputs( 238 "Macro definition missing null line terminator.\n", 239 ttyout); 240 goto bad; 241 } 242 *tmp = c; 243 if (*tmp == '\n') { |
228 if (*(tmp-1) == '\0') { 229 macros[macnum++].mac_end = tmp - 1; 230 break; | 244 if (tmp == macros[macnum].mac_start) { 245 macros[macnum++].mac_end = tmp; 246 break; 247 } else if (*(tmp - 1) == '\0') { 248 macros[macnum++].mac_end = 249 tmp - 1; 250 break; |
231 } 232 *tmp = '\0'; 233 } 234 tmp++; 235 } 236 if (tmp == macbuf + 4096) { 237 fputs("4K macro buffer exceeded.\n", 238 ttyout); 239 goto bad; 240 } 241 break; 242 default: | 251 } 252 *tmp = '\0'; 253 } 254 tmp++; 255 } 256 if (tmp == macbuf + 4096) { 257 fputs("4K macro buffer exceeded.\n", 258 ttyout); 259 goto bad; 260 } 261 break; 262 default: |
243 warnx("Unknown .netrc keyword %s", tokval); | 263 warnx("Unknown .netrc keyword `%s'", tokval); |
244 break; 245 } 246 goto done; 247 } 248 done: | 264 break; 265 } 266 goto done; 267 } 268 done: |
269 if (t == -1) 270 goto bad; |
|
249 (void)fclose(cfile); 250 return (0); 251 bad: 252 (void)fclose(cfile); 253 return (-1); 254} 255 256static int --- 9 unchanged lines hidden (view full) --- 266 (c == '\n' || c == '\t' || c == ' ' || c == ',')) 267 continue; 268 if (c == EOF) 269 return (0); 270 cp = tokval; 271 if (c == '"') { 272 while ((c = getc(cfile)) != EOF && c != '"') { 273 if (c == '\\') | 271 (void)fclose(cfile); 272 return (0); 273 bad: 274 (void)fclose(cfile); 275 return (-1); 276} 277 278static int --- 9 unchanged lines hidden (view full) --- 288 (c == '\n' || c == '\t' || c == ' ' || c == ',')) 289 continue; 290 if (c == EOF) 291 return (0); 292 cp = tokval; 293 if (c == '"') { 294 while ((c = getc(cfile)) != EOF && c != '"') { 295 if (c == '\\') |
274 c = getc(cfile); | 296 if ((c = getc(cfile)) == EOF) 297 break; |
275 *cp++ = c; | 298 *cp++ = c; |
299 if (cp == tokval + sizeof(tokval)) { 300 warnx("Token in .netrc too long"); 301 return (-1); 302 } |
|
276 } 277 } else { 278 *cp++ = c; 279 while ((c = getc(cfile)) != EOF 280 && c != '\n' && c != '\t' && c != ' ' && c != ',') { 281 if (c == '\\') | 303 } 304 } else { 305 *cp++ = c; 306 while ((c = getc(cfile)) != EOF 307 && c != '\n' && c != '\t' && c != ' ' && c != ',') { 308 if (c == '\\') |
282 c = getc(cfile); | 309 if ((c = getc(cfile)) == EOF) 310 break; |
283 *cp++ = c; | 311 *cp++ = c; |
312 if (cp == tokval + sizeof(tokval)) { 313 warnx("Token in .netrc too long"); 314 return (-1); 315 } |
|
284 } 285 } 286 *cp = 0; 287 if (tokval[0] == 0) 288 return (0); 289 for (t = toktab; t->tokstr; t++) 290 if (!strcmp(t->tokstr, tokval)) 291 return (t->tval); 292 return (ID); 293} | 316 } 317 } 318 *cp = 0; 319 if (tokval[0] == 0) 320 return (0); 321 for (t = toktab; t->tokstr; t++) 322 if (!strcmp(t->tokstr, tokval)) 323 return (t->tval); 324 return (ID); 325} |