1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 28*0Sstevel@tonic-gate /* All Rights Reserved */ 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate /* 31*0Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 32*0Sstevel@tonic-gate * The Regents of the University of California 33*0Sstevel@tonic-gate * All Rights Reserved 34*0Sstevel@tonic-gate * 35*0Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 36*0Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 37*0Sstevel@tonic-gate * contributors. 38*0Sstevel@tonic-gate */ 39*0Sstevel@tonic-gate 40*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 41*0Sstevel@tonic-gate 42*0Sstevel@tonic-gate #include "ftp_var.h" 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate static FILE *cfile; 45*0Sstevel@tonic-gate 46*0Sstevel@tonic-gate static int rnetrc(char *host, char **aname, char **apass, char **aacct); 47*0Sstevel@tonic-gate static int token(void); 48*0Sstevel@tonic-gate 49*0Sstevel@tonic-gate int 50*0Sstevel@tonic-gate ruserpass(char *host, char **aname, char **apass, char **aacct) 51*0Sstevel@tonic-gate { 52*0Sstevel@tonic-gate #if 0 53*0Sstevel@tonic-gate renv(host, aname, apass, aacct); 54*0Sstevel@tonic-gate if (*aname == 0 || *apass == 0) 55*0Sstevel@tonic-gate #endif 56*0Sstevel@tonic-gate return (rnetrc(host, aname, apass, aacct)); 57*0Sstevel@tonic-gate } 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate #define DEFAULT 1 60*0Sstevel@tonic-gate #define LOGIN 2 61*0Sstevel@tonic-gate #define PASSWD 3 62*0Sstevel@tonic-gate #define ACCOUNT 4 63*0Sstevel@tonic-gate #define MACDEF 5 64*0Sstevel@tonic-gate #define ID 10 65*0Sstevel@tonic-gate #define MACHINE 11 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gate static char tokval[100]; 68*0Sstevel@tonic-gate 69*0Sstevel@tonic-gate static struct toktab { 70*0Sstevel@tonic-gate char *tokstr; 71*0Sstevel@tonic-gate int tval; 72*0Sstevel@tonic-gate } toktab[] = { 73*0Sstevel@tonic-gate "default", DEFAULT, 74*0Sstevel@tonic-gate "login", LOGIN, 75*0Sstevel@tonic-gate "password", PASSWD, 76*0Sstevel@tonic-gate "account", ACCOUNT, 77*0Sstevel@tonic-gate "machine", MACHINE, 78*0Sstevel@tonic-gate "macdef", MACDEF, 79*0Sstevel@tonic-gate 0, 0 80*0Sstevel@tonic-gate }; 81*0Sstevel@tonic-gate 82*0Sstevel@tonic-gate static int 83*0Sstevel@tonic-gate rnetrc(char *host, char **aname, char **apass, char **aacct) 84*0Sstevel@tonic-gate { 85*0Sstevel@tonic-gate char *hdir, buf[PATH_MAX+1], *tmp; 86*0Sstevel@tonic-gate int t, i, c; 87*0Sstevel@tonic-gate struct stat stb; 88*0Sstevel@tonic-gate extern int errno; 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gate hdir = getenv("HOME"); 91*0Sstevel@tonic-gate if (hdir == NULL) 92*0Sstevel@tonic-gate hdir = "."; 93*0Sstevel@tonic-gate if (snprintf(buf, sizeof (buf), "%s/.netrc", hdir) >= sizeof (buf)) { 94*0Sstevel@tonic-gate fprintf(stderr, ".netrc: %s\n", strerror(ENAMETOOLONG)); 95*0Sstevel@tonic-gate exit(1); 96*0Sstevel@tonic-gate } 97*0Sstevel@tonic-gate 98*0Sstevel@tonic-gate cfile = fopen(buf, "r"); 99*0Sstevel@tonic-gate if (cfile == NULL) { 100*0Sstevel@tonic-gate if (errno != ENOENT) 101*0Sstevel@tonic-gate perror(buf); 102*0Sstevel@tonic-gate return (0); 103*0Sstevel@tonic-gate } 104*0Sstevel@tonic-gate next: 105*0Sstevel@tonic-gate while ((t = token())) 106*0Sstevel@tonic-gate switch (t) { 107*0Sstevel@tonic-gate 108*0Sstevel@tonic-gate case MACHINE: 109*0Sstevel@tonic-gate if (token() != ID || strcmp(host, tokval)) 110*0Sstevel@tonic-gate continue; 111*0Sstevel@tonic-gate /* "machine name" matches host */ 112*0Sstevel@tonic-gate /* FALLTHROUGH */ 113*0Sstevel@tonic-gate 114*0Sstevel@tonic-gate case DEFAULT: 115*0Sstevel@tonic-gate /* "default" matches any host */ 116*0Sstevel@tonic-gate while (((t = token()) != 0) && t != MACHINE && t != DEFAULT) 117*0Sstevel@tonic-gate switch (t) { 118*0Sstevel@tonic-gate 119*0Sstevel@tonic-gate case LOGIN: 120*0Sstevel@tonic-gate if (token()) 121*0Sstevel@tonic-gate if (*aname == 0) { 122*0Sstevel@tonic-gate *aname = malloc((unsigned) 123*0Sstevel@tonic-gate strlen(tokval) + 1); 124*0Sstevel@tonic-gate if (*aname == NULL) { 125*0Sstevel@tonic-gate fprintf(stderr, 126*0Sstevel@tonic-gate "Error - out of VM\n"); 127*0Sstevel@tonic-gate exit(1); 128*0Sstevel@tonic-gate } 129*0Sstevel@tonic-gate (void) strcpy(*aname, tokval); 130*0Sstevel@tonic-gate } else { 131*0Sstevel@tonic-gate if (strcmp(*aname, tokval)) 132*0Sstevel@tonic-gate goto next; 133*0Sstevel@tonic-gate } 134*0Sstevel@tonic-gate break; 135*0Sstevel@tonic-gate case PASSWD: 136*0Sstevel@tonic-gate if (fstat(fileno(cfile), &stb) >= 0 && 137*0Sstevel@tonic-gate (stb.st_mode & 077) != 0) { 138*0Sstevel@tonic-gate fprintf(stderr, "Error - .netrc file not " 139*0Sstevel@tonic-gate "correct mode.\n"); 140*0Sstevel@tonic-gate fprintf(stderr, "Remove password or correct " 141*0Sstevel@tonic-gate "mode.\n"); 142*0Sstevel@tonic-gate return (-1); 143*0Sstevel@tonic-gate } 144*0Sstevel@tonic-gate if (token() && *apass == 0) { 145*0Sstevel@tonic-gate *apass = malloc((unsigned)strlen(tokval) + 1); 146*0Sstevel@tonic-gate if (*apass == NULL) { 147*0Sstevel@tonic-gate fprintf(stderr, "Error - out of VM\n"); 148*0Sstevel@tonic-gate exit(1); 149*0Sstevel@tonic-gate } 150*0Sstevel@tonic-gate (void) strcpy(*apass, tokval); 151*0Sstevel@tonic-gate } 152*0Sstevel@tonic-gate break; 153*0Sstevel@tonic-gate case ACCOUNT: 154*0Sstevel@tonic-gate if (fstat(fileno(cfile), &stb) >= 0 && 155*0Sstevel@tonic-gate (stb.st_mode & 077) != 0) { 156*0Sstevel@tonic-gate fprintf(stderr, "Error - .netrc file not " 157*0Sstevel@tonic-gate "correct mode.\n"); 158*0Sstevel@tonic-gate fprintf(stderr, "Remove account or correct " 159*0Sstevel@tonic-gate "mode.\n"); 160*0Sstevel@tonic-gate return (-1); 161*0Sstevel@tonic-gate } 162*0Sstevel@tonic-gate if (token() && *aacct == 0) { 163*0Sstevel@tonic-gate *aacct = malloc((unsigned)strlen(tokval) + 1); 164*0Sstevel@tonic-gate if (*aacct == NULL) { 165*0Sstevel@tonic-gate fprintf(stderr, "Error - out of VM\n"); 166*0Sstevel@tonic-gate exit(1); 167*0Sstevel@tonic-gate } 168*0Sstevel@tonic-gate (void) strcpy(*aacct, tokval); 169*0Sstevel@tonic-gate } 170*0Sstevel@tonic-gate break; 171*0Sstevel@tonic-gate case MACDEF: 172*0Sstevel@tonic-gate if (proxy) { 173*0Sstevel@tonic-gate return (0); 174*0Sstevel@tonic-gate } 175*0Sstevel@tonic-gate while ((c = getc(cfile)) != EOF && c == ' ' || 176*0Sstevel@tonic-gate c == '\t'); 177*0Sstevel@tonic-gate if (c == EOF || c == '\n') { 178*0Sstevel@tonic-gate printf("Missing macdef name argument.\n"); 179*0Sstevel@tonic-gate return (-1); 180*0Sstevel@tonic-gate } 181*0Sstevel@tonic-gate if (macnum == 16) { 182*0Sstevel@tonic-gate printf("Limit of 16 macros have already " 183*0Sstevel@tonic-gate "been defined\n"); 184*0Sstevel@tonic-gate return (-1); 185*0Sstevel@tonic-gate } 186*0Sstevel@tonic-gate tmp = macros[macnum].mac_name; 187*0Sstevel@tonic-gate *tmp++ = c; 188*0Sstevel@tonic-gate for (i = 0; i < 8 && (c = getc(cfile)) != EOF && 189*0Sstevel@tonic-gate !isspace(c); ++i) { 190*0Sstevel@tonic-gate *tmp++ = c; 191*0Sstevel@tonic-gate } 192*0Sstevel@tonic-gate if (c == EOF) { 193*0Sstevel@tonic-gate printf("Macro definition for `%s` missing " 194*0Sstevel@tonic-gate "null line terminator.\n", 195*0Sstevel@tonic-gate macros[macnum].mac_name); 196*0Sstevel@tonic-gate return (-1); 197*0Sstevel@tonic-gate } 198*0Sstevel@tonic-gate *tmp = '\0'; 199*0Sstevel@tonic-gate if (c != '\n') { 200*0Sstevel@tonic-gate while ((c = getc(cfile)) != EOF && c != '\n'); 201*0Sstevel@tonic-gate } 202*0Sstevel@tonic-gate if (c == EOF) { 203*0Sstevel@tonic-gate printf("Macro definition for `%s` missing " 204*0Sstevel@tonic-gate "null line terminator.\n", 205*0Sstevel@tonic-gate macros[macnum].mac_name); 206*0Sstevel@tonic-gate return (-1); 207*0Sstevel@tonic-gate } 208*0Sstevel@tonic-gate if (macnum == 0) { 209*0Sstevel@tonic-gate macros[macnum].mac_start = macbuf; 210*0Sstevel@tonic-gate } else { 211*0Sstevel@tonic-gate macros[macnum].mac_start = 212*0Sstevel@tonic-gate macros[macnum-1].mac_end + 1; 213*0Sstevel@tonic-gate } 214*0Sstevel@tonic-gate tmp = macros[macnum].mac_start; 215*0Sstevel@tonic-gate while (tmp != macbuf + 4096) { 216*0Sstevel@tonic-gate if ((c = getc(cfile)) == EOF) { 217*0Sstevel@tonic-gate printf("Macro definition for `%s` missing " 218*0Sstevel@tonic-gate "null line terminator.\n", 219*0Sstevel@tonic-gate macros[macnum].mac_name); 220*0Sstevel@tonic-gate return (-1); 221*0Sstevel@tonic-gate } 222*0Sstevel@tonic-gate *tmp = c; 223*0Sstevel@tonic-gate if (*tmp == '\n') { 224*0Sstevel@tonic-gate if (*(tmp-1) == '\0') { 225*0Sstevel@tonic-gate macros[macnum++].mac_end = 226*0Sstevel@tonic-gate tmp - 1; 227*0Sstevel@tonic-gate break; 228*0Sstevel@tonic-gate } 229*0Sstevel@tonic-gate *tmp = '\0'; 230*0Sstevel@tonic-gate } 231*0Sstevel@tonic-gate tmp++; 232*0Sstevel@tonic-gate } 233*0Sstevel@tonic-gate if (tmp == macbuf + 4096) { 234*0Sstevel@tonic-gate printf("4K macro buffer exceeded\n"); 235*0Sstevel@tonic-gate return (-1); 236*0Sstevel@tonic-gate } 237*0Sstevel@tonic-gate if (*macros[macnum - 1].mac_start == '\n') { 238*0Sstevel@tonic-gate printf("Macro definition for `%s` is empty, " 239*0Sstevel@tonic-gate "macro not stored.\n", 240*0Sstevel@tonic-gate macros[--macnum].mac_name); 241*0Sstevel@tonic-gate } 242*0Sstevel@tonic-gate break; 243*0Sstevel@tonic-gate default: 244*0Sstevel@tonic-gate fprintf(stderr, "Unknown .netrc keyword %s\n", tokval); 245*0Sstevel@tonic-gate break; 246*0Sstevel@tonic-gate } 247*0Sstevel@tonic-gate goto done; 248*0Sstevel@tonic-gate } 249*0Sstevel@tonic-gate done: 250*0Sstevel@tonic-gate (void) fclose(cfile); 251*0Sstevel@tonic-gate return (0); 252*0Sstevel@tonic-gate } 253*0Sstevel@tonic-gate 254*0Sstevel@tonic-gate static int 255*0Sstevel@tonic-gate token(void) 256*0Sstevel@tonic-gate { 257*0Sstevel@tonic-gate char *cp; 258*0Sstevel@tonic-gate int c; 259*0Sstevel@tonic-gate struct toktab *t; 260*0Sstevel@tonic-gate int len; 261*0Sstevel@tonic-gate 262*0Sstevel@tonic-gate if (feof(cfile)) 263*0Sstevel@tonic-gate return (0); 264*0Sstevel@tonic-gate while ((c = fgetwc(cfile)) != EOF && 265*0Sstevel@tonic-gate (c == '\n' || c == '\t' || c == ' ' || c == ',')) 266*0Sstevel@tonic-gate continue; 267*0Sstevel@tonic-gate if (c == EOF) 268*0Sstevel@tonic-gate return (0); 269*0Sstevel@tonic-gate cp = tokval; 270*0Sstevel@tonic-gate if (c == '"') { 271*0Sstevel@tonic-gate while ((c = fgetwc(cfile)) != EOF && c != '"') { 272*0Sstevel@tonic-gate if (c == '\\') 273*0Sstevel@tonic-gate c = fgetwc(cfile); 274*0Sstevel@tonic-gate if ((len = wctomb(cp, c)) <= 0) { 275*0Sstevel@tonic-gate len = 1; 276*0Sstevel@tonic-gate *cp = (unsigned char)c; 277*0Sstevel@tonic-gate } 278*0Sstevel@tonic-gate cp += len; 279*0Sstevel@tonic-gate } 280*0Sstevel@tonic-gate } else { 281*0Sstevel@tonic-gate if ((len = wctomb(cp, c)) <= 0) { 282*0Sstevel@tonic-gate *cp = (unsigned char)c; 283*0Sstevel@tonic-gate len = 1; 284*0Sstevel@tonic-gate } 285*0Sstevel@tonic-gate cp += len; 286*0Sstevel@tonic-gate while ((c = fgetwc(cfile)) != EOF && c != '\n' && c != '\t' && 287*0Sstevel@tonic-gate c != ' ' && c != ',') { 288*0Sstevel@tonic-gate if (c == '\\') 289*0Sstevel@tonic-gate c = fgetwc(cfile); 290*0Sstevel@tonic-gate if ((len = wctomb(cp, c)) <= 0) { 291*0Sstevel@tonic-gate len = 1; 292*0Sstevel@tonic-gate *cp = (unsigned char)c; 293*0Sstevel@tonic-gate } 294*0Sstevel@tonic-gate cp += len; 295*0Sstevel@tonic-gate } 296*0Sstevel@tonic-gate } 297*0Sstevel@tonic-gate *cp = 0; 298*0Sstevel@tonic-gate if (tokval[0] == 0) 299*0Sstevel@tonic-gate return (0); 300*0Sstevel@tonic-gate for (t = toktab; t->tokstr; t++) 301*0Sstevel@tonic-gate if (strcmp(t->tokstr, tokval) == 0) 302*0Sstevel@tonic-gate return (t->tval); 303*0Sstevel@tonic-gate return (ID); 304*0Sstevel@tonic-gate } 305