17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0b6880ccSsp149894 * Common Development and Distribution License (the "License").
6*0b6880ccSsp149894 * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*0b6880ccSsp149894 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /* All Rights Reserved */
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate * The Regents of the University of California
327c478bd9Sstevel@tonic-gate * All Rights Reserved
337c478bd9Sstevel@tonic-gate *
347c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate * contributors.
377c478bd9Sstevel@tonic-gate */
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate #include "ftp_var.h"
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate static FILE *cfile;
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate static int rnetrc(char *host, char **aname, char **apass, char **aacct);
467c478bd9Sstevel@tonic-gate static int token(void);
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate int
ruserpass(char * host,char ** aname,char ** apass,char ** aacct)497c478bd9Sstevel@tonic-gate ruserpass(char *host, char **aname, char **apass, char **aacct)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate #if 0
527c478bd9Sstevel@tonic-gate renv(host, aname, apass, aacct);
537c478bd9Sstevel@tonic-gate if (*aname == 0 || *apass == 0)
547c478bd9Sstevel@tonic-gate #endif
557c478bd9Sstevel@tonic-gate return (rnetrc(host, aname, apass, aacct));
567c478bd9Sstevel@tonic-gate }
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate #define DEFAULT 1
597c478bd9Sstevel@tonic-gate #define LOGIN 2
607c478bd9Sstevel@tonic-gate #define PASSWD 3
617c478bd9Sstevel@tonic-gate #define ACCOUNT 4
627c478bd9Sstevel@tonic-gate #define MACDEF 5
63*0b6880ccSsp149894 #define SKIPSYST 6
647c478bd9Sstevel@tonic-gate #define ID 10
657c478bd9Sstevel@tonic-gate #define MACHINE 11
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate static char tokval[100];
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate static struct toktab {
707c478bd9Sstevel@tonic-gate char *tokstr;
717c478bd9Sstevel@tonic-gate int tval;
727c478bd9Sstevel@tonic-gate } toktab[] = {
737c478bd9Sstevel@tonic-gate "default", DEFAULT,
747c478bd9Sstevel@tonic-gate "login", LOGIN,
757c478bd9Sstevel@tonic-gate "password", PASSWD,
767c478bd9Sstevel@tonic-gate "account", ACCOUNT,
777c478bd9Sstevel@tonic-gate "machine", MACHINE,
787c478bd9Sstevel@tonic-gate "macdef", MACDEF,
79*0b6880ccSsp149894 "skipsyst", SKIPSYST,
807c478bd9Sstevel@tonic-gate 0, 0
817c478bd9Sstevel@tonic-gate };
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate static int
rnetrc(char * host,char ** aname,char ** apass,char ** aacct)847c478bd9Sstevel@tonic-gate rnetrc(char *host, char **aname, char **apass, char **aacct)
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate char *hdir, buf[PATH_MAX+1], *tmp;
877c478bd9Sstevel@tonic-gate int t, i, c;
887c478bd9Sstevel@tonic-gate struct stat stb;
897c478bd9Sstevel@tonic-gate extern int errno;
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate hdir = getenv("HOME");
927c478bd9Sstevel@tonic-gate if (hdir == NULL)
937c478bd9Sstevel@tonic-gate hdir = ".";
947c478bd9Sstevel@tonic-gate if (snprintf(buf, sizeof (buf), "%s/.netrc", hdir) >= sizeof (buf)) {
957c478bd9Sstevel@tonic-gate fprintf(stderr, ".netrc: %s\n", strerror(ENAMETOOLONG));
967c478bd9Sstevel@tonic-gate exit(1);
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate cfile = fopen(buf, "r");
1007c478bd9Sstevel@tonic-gate if (cfile == NULL) {
1017c478bd9Sstevel@tonic-gate if (errno != ENOENT)
1027c478bd9Sstevel@tonic-gate perror(buf);
1037c478bd9Sstevel@tonic-gate return (0);
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate next:
1067c478bd9Sstevel@tonic-gate while ((t = token()))
1077c478bd9Sstevel@tonic-gate switch (t) {
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate case MACHINE:
1107c478bd9Sstevel@tonic-gate if (token() != ID || strcmp(host, tokval))
1117c478bd9Sstevel@tonic-gate continue;
1127c478bd9Sstevel@tonic-gate /* "machine name" matches host */
1137c478bd9Sstevel@tonic-gate /* FALLTHROUGH */
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate case DEFAULT:
1167c478bd9Sstevel@tonic-gate /* "default" matches any host */
1177c478bd9Sstevel@tonic-gate while (((t = token()) != 0) && t != MACHINE && t != DEFAULT)
1187c478bd9Sstevel@tonic-gate switch (t) {
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate case LOGIN:
1217c478bd9Sstevel@tonic-gate if (token())
1227c478bd9Sstevel@tonic-gate if (*aname == 0) {
1237c478bd9Sstevel@tonic-gate *aname = malloc((unsigned)
1247c478bd9Sstevel@tonic-gate strlen(tokval) + 1);
1257c478bd9Sstevel@tonic-gate if (*aname == NULL) {
1267c478bd9Sstevel@tonic-gate fprintf(stderr,
1277c478bd9Sstevel@tonic-gate "Error - out of VM\n");
1287c478bd9Sstevel@tonic-gate exit(1);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate (void) strcpy(*aname, tokval);
1317c478bd9Sstevel@tonic-gate } else {
1327c478bd9Sstevel@tonic-gate if (strcmp(*aname, tokval))
1337c478bd9Sstevel@tonic-gate goto next;
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate break;
1367c478bd9Sstevel@tonic-gate case PASSWD:
1377c478bd9Sstevel@tonic-gate if (fstat(fileno(cfile), &stb) >= 0 &&
1387c478bd9Sstevel@tonic-gate (stb.st_mode & 077) != 0) {
1397c478bd9Sstevel@tonic-gate fprintf(stderr, "Error - .netrc file not "
1407c478bd9Sstevel@tonic-gate "correct mode.\n");
1417c478bd9Sstevel@tonic-gate fprintf(stderr, "Remove password or correct "
1427c478bd9Sstevel@tonic-gate "mode.\n");
1437c478bd9Sstevel@tonic-gate return (-1);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate if (token() && *apass == 0) {
1467c478bd9Sstevel@tonic-gate *apass = malloc((unsigned)strlen(tokval) + 1);
1477c478bd9Sstevel@tonic-gate if (*apass == NULL) {
1487c478bd9Sstevel@tonic-gate fprintf(stderr, "Error - out of VM\n");
1497c478bd9Sstevel@tonic-gate exit(1);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate (void) strcpy(*apass, tokval);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate break;
1547c478bd9Sstevel@tonic-gate case ACCOUNT:
1557c478bd9Sstevel@tonic-gate if (fstat(fileno(cfile), &stb) >= 0 &&
1567c478bd9Sstevel@tonic-gate (stb.st_mode & 077) != 0) {
1577c478bd9Sstevel@tonic-gate fprintf(stderr, "Error - .netrc file not "
1587c478bd9Sstevel@tonic-gate "correct mode.\n");
1597c478bd9Sstevel@tonic-gate fprintf(stderr, "Remove account or correct "
1607c478bd9Sstevel@tonic-gate "mode.\n");
1617c478bd9Sstevel@tonic-gate return (-1);
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate if (token() && *aacct == 0) {
1647c478bd9Sstevel@tonic-gate *aacct = malloc((unsigned)strlen(tokval) + 1);
1657c478bd9Sstevel@tonic-gate if (*aacct == NULL) {
1667c478bd9Sstevel@tonic-gate fprintf(stderr, "Error - out of VM\n");
1677c478bd9Sstevel@tonic-gate exit(1);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate (void) strcpy(*aacct, tokval);
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate break;
1727c478bd9Sstevel@tonic-gate case MACDEF:
1737c478bd9Sstevel@tonic-gate if (proxy) {
1747c478bd9Sstevel@tonic-gate return (0);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate while ((c = getc(cfile)) != EOF && c == ' ' ||
1777c478bd9Sstevel@tonic-gate c == '\t');
1787c478bd9Sstevel@tonic-gate if (c == EOF || c == '\n') {
1797c478bd9Sstevel@tonic-gate printf("Missing macdef name argument.\n");
1807c478bd9Sstevel@tonic-gate return (-1);
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate if (macnum == 16) {
1837c478bd9Sstevel@tonic-gate printf("Limit of 16 macros have already "
1847c478bd9Sstevel@tonic-gate "been defined\n");
1857c478bd9Sstevel@tonic-gate return (-1);
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate tmp = macros[macnum].mac_name;
1887c478bd9Sstevel@tonic-gate *tmp++ = c;
1897c478bd9Sstevel@tonic-gate for (i = 0; i < 8 && (c = getc(cfile)) != EOF &&
1907c478bd9Sstevel@tonic-gate !isspace(c); ++i) {
1917c478bd9Sstevel@tonic-gate *tmp++ = c;
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate if (c == EOF) {
1947c478bd9Sstevel@tonic-gate printf("Macro definition for `%s` missing "
1957c478bd9Sstevel@tonic-gate "null line terminator.\n",
1967c478bd9Sstevel@tonic-gate macros[macnum].mac_name);
1977c478bd9Sstevel@tonic-gate return (-1);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate *tmp = '\0';
2007c478bd9Sstevel@tonic-gate if (c != '\n') {
2017c478bd9Sstevel@tonic-gate while ((c = getc(cfile)) != EOF && c != '\n');
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate if (c == EOF) {
2047c478bd9Sstevel@tonic-gate printf("Macro definition for `%s` missing "
2057c478bd9Sstevel@tonic-gate "null line terminator.\n",
2067c478bd9Sstevel@tonic-gate macros[macnum].mac_name);
2077c478bd9Sstevel@tonic-gate return (-1);
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate if (macnum == 0) {
2107c478bd9Sstevel@tonic-gate macros[macnum].mac_start = macbuf;
2117c478bd9Sstevel@tonic-gate } else {
2127c478bd9Sstevel@tonic-gate macros[macnum].mac_start =
2137c478bd9Sstevel@tonic-gate macros[macnum-1].mac_end + 1;
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate tmp = macros[macnum].mac_start;
2167c478bd9Sstevel@tonic-gate while (tmp != macbuf + 4096) {
2177c478bd9Sstevel@tonic-gate if ((c = getc(cfile)) == EOF) {
2187c478bd9Sstevel@tonic-gate printf("Macro definition for `%s` missing "
2197c478bd9Sstevel@tonic-gate "null line terminator.\n",
2207c478bd9Sstevel@tonic-gate macros[macnum].mac_name);
2217c478bd9Sstevel@tonic-gate return (-1);
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate *tmp = c;
2247c478bd9Sstevel@tonic-gate if (*tmp == '\n') {
2257c478bd9Sstevel@tonic-gate if (*(tmp-1) == '\0') {
2267c478bd9Sstevel@tonic-gate macros[macnum++].mac_end =
2277c478bd9Sstevel@tonic-gate tmp - 1;
2287c478bd9Sstevel@tonic-gate break;
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate *tmp = '\0';
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate tmp++;
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate if (tmp == macbuf + 4096) {
2357c478bd9Sstevel@tonic-gate printf("4K macro buffer exceeded\n");
2367c478bd9Sstevel@tonic-gate return (-1);
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate if (*macros[macnum - 1].mac_start == '\n') {
2397c478bd9Sstevel@tonic-gate printf("Macro definition for `%s` is empty, "
2407c478bd9Sstevel@tonic-gate "macro not stored.\n",
2417c478bd9Sstevel@tonic-gate macros[--macnum].mac_name);
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate break;
244*0b6880ccSsp149894 case SKIPSYST:
245*0b6880ccSsp149894 skipsyst = 1;
246*0b6880ccSsp149894 break;
2477c478bd9Sstevel@tonic-gate default:
2487c478bd9Sstevel@tonic-gate fprintf(stderr, "Unknown .netrc keyword %s\n", tokval);
2497c478bd9Sstevel@tonic-gate break;
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate goto done;
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate done:
2547c478bd9Sstevel@tonic-gate (void) fclose(cfile);
2557c478bd9Sstevel@tonic-gate return (0);
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate static int
token(void)2597c478bd9Sstevel@tonic-gate token(void)
2607c478bd9Sstevel@tonic-gate {
2617c478bd9Sstevel@tonic-gate char *cp;
2627c478bd9Sstevel@tonic-gate int c;
2637c478bd9Sstevel@tonic-gate struct toktab *t;
2647c478bd9Sstevel@tonic-gate int len;
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate if (feof(cfile))
2677c478bd9Sstevel@tonic-gate return (0);
2687c478bd9Sstevel@tonic-gate while ((c = fgetwc(cfile)) != EOF &&
2697c478bd9Sstevel@tonic-gate (c == '\n' || c == '\t' || c == ' ' || c == ','))
2707c478bd9Sstevel@tonic-gate continue;
2717c478bd9Sstevel@tonic-gate if (c == EOF)
2727c478bd9Sstevel@tonic-gate return (0);
2737c478bd9Sstevel@tonic-gate cp = tokval;
2747c478bd9Sstevel@tonic-gate if (c == '"') {
2757c478bd9Sstevel@tonic-gate while ((c = fgetwc(cfile)) != EOF && c != '"') {
2767c478bd9Sstevel@tonic-gate if (c == '\\')
2777c478bd9Sstevel@tonic-gate c = fgetwc(cfile);
2787c478bd9Sstevel@tonic-gate if ((len = wctomb(cp, c)) <= 0) {
2797c478bd9Sstevel@tonic-gate len = 1;
2807c478bd9Sstevel@tonic-gate *cp = (unsigned char)c;
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate cp += len;
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate } else {
2857c478bd9Sstevel@tonic-gate if ((len = wctomb(cp, c)) <= 0) {
2867c478bd9Sstevel@tonic-gate *cp = (unsigned char)c;
2877c478bd9Sstevel@tonic-gate len = 1;
2887c478bd9Sstevel@tonic-gate }
2897c478bd9Sstevel@tonic-gate cp += len;
2907c478bd9Sstevel@tonic-gate while ((c = fgetwc(cfile)) != EOF && c != '\n' && c != '\t' &&
2917c478bd9Sstevel@tonic-gate c != ' ' && c != ',') {
2927c478bd9Sstevel@tonic-gate if (c == '\\')
2937c478bd9Sstevel@tonic-gate c = fgetwc(cfile);
2947c478bd9Sstevel@tonic-gate if ((len = wctomb(cp, c)) <= 0) {
2957c478bd9Sstevel@tonic-gate len = 1;
2967c478bd9Sstevel@tonic-gate *cp = (unsigned char)c;
2977c478bd9Sstevel@tonic-gate }
2987c478bd9Sstevel@tonic-gate cp += len;
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate *cp = 0;
3027c478bd9Sstevel@tonic-gate if (tokval[0] == 0)
3037c478bd9Sstevel@tonic-gate return (0);
3047c478bd9Sstevel@tonic-gate for (t = toktab; t->tokstr; t++)
3057c478bd9Sstevel@tonic-gate if (strcmp(t->tokstr, tokval) == 0)
3067c478bd9Sstevel@tonic-gate return (t->tval);
3077c478bd9Sstevel@tonic-gate return (ID);
3087c478bd9Sstevel@tonic-gate }
309