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
5004388ebScasper * Common Development and Distribution License (the "License").
6004388ebScasper * 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 */
21*7257d1b4Sraf
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate * The Regents of the University of California
337c478bd9Sstevel@tonic-gate * All Rights Reserved
347c478bd9Sstevel@tonic-gate *
357c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate * contributors.
387c478bd9Sstevel@tonic-gate */
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate #include <stdio.h>
437c478bd9Sstevel@tonic-gate #include <ctype.h>
447c478bd9Sstevel@tonic-gate #include <sys/types.h>
457c478bd9Sstevel@tonic-gate #include <sys/stat.h>
467c478bd9Sstevel@tonic-gate #include <errno.h>
477c478bd9Sstevel@tonic-gate #include <unistd.h>
487c478bd9Sstevel@tonic-gate #include <strings.h>
497c478bd9Sstevel@tonic-gate #include <stdlib.h>
507c478bd9Sstevel@tonic-gate #include <libintl.h>
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate #ifdef SYSV
537c478bd9Sstevel@tonic-gate #define index strchr
547c478bd9Sstevel@tonic-gate #endif /* SYSV */
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate static void rnetrc(const char *host, char **aname, char **apass);
577c478bd9Sstevel@tonic-gate static int token();
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate #define DEFAULT 1
607c478bd9Sstevel@tonic-gate #define LOGIN 2
617c478bd9Sstevel@tonic-gate #define PASSWD 3
627c478bd9Sstevel@tonic-gate #define NOTIFY 4
637c478bd9Sstevel@tonic-gate #define WRITE 5
647c478bd9Sstevel@tonic-gate #define YES 6
657c478bd9Sstevel@tonic-gate #define NO 7
667c478bd9Sstevel@tonic-gate #define COMMAND 8
677c478bd9Sstevel@tonic-gate #define FORCE 9
687c478bd9Sstevel@tonic-gate #define ID 10
697c478bd9Sstevel@tonic-gate #define MACHINE 11
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate #define MAXTOKEN 11
727c478bd9Sstevel@tonic-gate #define NTOKENS (MAXTOKEN - 1 + 2 + 1) /* two duplicates and null, minus id */
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate static struct ruserdata {
757c478bd9Sstevel@tonic-gate char tokval[100];
767c478bd9Sstevel@tonic-gate struct toktab {
777c478bd9Sstevel@tonic-gate char *tokstr;
787c478bd9Sstevel@tonic-gate int tval;
797c478bd9Sstevel@tonic-gate } toktab[NTOKENS];
807c478bd9Sstevel@tonic-gate FILE *cfile;
817c478bd9Sstevel@tonic-gate } *ruserdata, *_ruserdata();
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate static struct ruserdata *
_ruserdata()857c478bd9Sstevel@tonic-gate _ruserdata()
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate struct ruserdata *d = ruserdata;
887c478bd9Sstevel@tonic-gate struct toktab *t;
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate if (d == 0) {
917c478bd9Sstevel@tonic-gate if ((d = (struct ruserdata *)
927c478bd9Sstevel@tonic-gate calloc(1, sizeof (struct ruserdata))) == NULL) {
937c478bd9Sstevel@tonic-gate return (NULL);
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate ruserdata = d;
967c478bd9Sstevel@tonic-gate t = d->toktab;
977c478bd9Sstevel@tonic-gate t->tokstr = "default"; t++->tval = DEFAULT;
987c478bd9Sstevel@tonic-gate t->tokstr = "login"; t++->tval = LOGIN;
997c478bd9Sstevel@tonic-gate t->tokstr = "password"; t++->tval = PASSWD;
1007c478bd9Sstevel@tonic-gate t->tokstr = "notify"; t++->tval = NOTIFY;
1017c478bd9Sstevel@tonic-gate t->tokstr = "write"; t++->tval = WRITE;
1027c478bd9Sstevel@tonic-gate t->tokstr = "yes"; t++->tval = YES;
1037c478bd9Sstevel@tonic-gate t->tokstr = "y"; t++->tval = YES;
1047c478bd9Sstevel@tonic-gate t->tokstr = "no"; t++->tval = NO;
1057c478bd9Sstevel@tonic-gate t->tokstr = "n"; t++->tval = NO;
1067c478bd9Sstevel@tonic-gate t->tokstr = "command"; t++->tval = COMMAND;
1077c478bd9Sstevel@tonic-gate t->tokstr = "force"; t++->tval = FORCE;
1087c478bd9Sstevel@tonic-gate t->tokstr = "machine"; t++->tval = MACHINE;
1097c478bd9Sstevel@tonic-gate t->tokstr = 0; t->tval = 0;
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate return (d);
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate #define MAXANAME 16
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate void
_ruserpass(const char * host,char ** aname,char ** apass)1187c478bd9Sstevel@tonic-gate _ruserpass(const char *host, char **aname, char **apass)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate if (*aname == 0 || *apass == 0)
1227c478bd9Sstevel@tonic-gate rnetrc(host, aname, apass);
1237c478bd9Sstevel@tonic-gate if (*aname == 0) {
1247c478bd9Sstevel@tonic-gate char myname[L_cuserid];
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate *aname = malloc(MAXANAME + 1);
1277c478bd9Sstevel@tonic-gate (void) cuserid(myname);
128*7257d1b4Sraf (void) printf(dgettext(TEXT_DOMAIN, "Name (%s:%s): "),
129004388ebScasper host, myname);
1307c478bd9Sstevel@tonic-gate (void) fflush(stdout);
1317c478bd9Sstevel@tonic-gate if (read(2, *aname, MAXANAME) <= 0)
1327c478bd9Sstevel@tonic-gate exit(1);
1337c478bd9Sstevel@tonic-gate aname[0][MAXANAME] = '\0';
1347c478bd9Sstevel@tonic-gate if ((*aname)[0] == '\n')
1357c478bd9Sstevel@tonic-gate (void) strcpy(*aname, myname);
1367c478bd9Sstevel@tonic-gate else
1377c478bd9Sstevel@tonic-gate if (index(*aname, '\n'))
1387c478bd9Sstevel@tonic-gate *index(*aname, '\n') = 0;
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate if (*aname && *apass == 0) {
141*7257d1b4Sraf (void) printf(dgettext(TEXT_DOMAIN, "Password (%s:%s): "),
1427c478bd9Sstevel@tonic-gate host, *aname);
1437c478bd9Sstevel@tonic-gate (void) fflush(stdout);
1447c478bd9Sstevel@tonic-gate *apass = getpass("");
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate static void
rnetrc(const char * host,char ** aname,char ** apass)1507c478bd9Sstevel@tonic-gate rnetrc(const char *host, char **aname, char **apass)
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate struct ruserdata *d = _ruserdata();
1537c478bd9Sstevel@tonic-gate char *hdir, buf[BUFSIZ];
1547c478bd9Sstevel@tonic-gate int t;
1557c478bd9Sstevel@tonic-gate struct stat64 stb;
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate if (d == 0)
1587c478bd9Sstevel@tonic-gate return;
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate hdir = getenv("HOME");
1617c478bd9Sstevel@tonic-gate if (hdir == NULL)
1627c478bd9Sstevel@tonic-gate hdir = ".";
1637c478bd9Sstevel@tonic-gate (void) sprintf(buf, "%s/.netrc", hdir);
164004388ebScasper d->cfile = fopen(buf, "rF");
1657c478bd9Sstevel@tonic-gate if (d->cfile == NULL) {
1667c478bd9Sstevel@tonic-gate if (errno != ENOENT)
1677c478bd9Sstevel@tonic-gate perror(buf);
1687c478bd9Sstevel@tonic-gate return;
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate next:
1717c478bd9Sstevel@tonic-gate while ((t = token()))
1727c478bd9Sstevel@tonic-gate switch (t) {
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate case DEFAULT:
1757c478bd9Sstevel@tonic-gate (void) token();
1767c478bd9Sstevel@tonic-gate continue;
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate case MACHINE:
1797c478bd9Sstevel@tonic-gate if (token() != ID || strcmp(host, d->tokval))
1807c478bd9Sstevel@tonic-gate continue;
1817c478bd9Sstevel@tonic-gate while ((t = token()) != 0 && t != MACHINE)
1827c478bd9Sstevel@tonic-gate switch (t) {
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate case LOGIN:
1857c478bd9Sstevel@tonic-gate if (token())
1867c478bd9Sstevel@tonic-gate if (*aname == 0) {
1877c478bd9Sstevel@tonic-gate *aname = malloc(strlen(d->tokval) + 1);
1887c478bd9Sstevel@tonic-gate (void) strcpy(*aname, d->tokval);
1897c478bd9Sstevel@tonic-gate } else {
1907c478bd9Sstevel@tonic-gate if (strcmp(*aname, d->tokval))
1917c478bd9Sstevel@tonic-gate goto next;
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate break;
1947c478bd9Sstevel@tonic-gate case PASSWD:
1957c478bd9Sstevel@tonic-gate if (fstat64(fileno(d->cfile), &stb) >= 0 &&
1967c478bd9Sstevel@tonic-gate (stb.st_mode & 077) != 0) {
1977c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
198*7257d1b4Sraf dgettext(TEXT_DOMAIN,
1997c478bd9Sstevel@tonic-gate "Error - .netrc file not correct mode.\n"));
2007c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
201*7257d1b4Sraf dgettext(TEXT_DOMAIN,
2027c478bd9Sstevel@tonic-gate "Remove password or correct mode.\n"));
2037c478bd9Sstevel@tonic-gate exit(1);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate if (token() && *apass == 0) {
2067c478bd9Sstevel@tonic-gate *apass = malloc(strlen(d->tokval) + 1);
2077c478bd9Sstevel@tonic-gate (void) strcpy(*apass, d->tokval);
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate break;
2107c478bd9Sstevel@tonic-gate case COMMAND:
2117c478bd9Sstevel@tonic-gate case NOTIFY:
2127c478bd9Sstevel@tonic-gate case WRITE:
2137c478bd9Sstevel@tonic-gate case FORCE:
2147c478bd9Sstevel@tonic-gate (void) token();
2157c478bd9Sstevel@tonic-gate break;
2167c478bd9Sstevel@tonic-gate default:
217*7257d1b4Sraf (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
218004388ebScasper "Unknown .netrc option %s\n"), d->tokval);
2197c478bd9Sstevel@tonic-gate break;
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate goto done;
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate done:
2247c478bd9Sstevel@tonic-gate (void) fclose(d->cfile);
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate static int
token()2287c478bd9Sstevel@tonic-gate token()
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate struct ruserdata *d = _ruserdata();
2317c478bd9Sstevel@tonic-gate char *cp;
2327c478bd9Sstevel@tonic-gate int c;
2337c478bd9Sstevel@tonic-gate struct toktab *t;
2347c478bd9Sstevel@tonic-gate
2357c478bd9Sstevel@tonic-gate if (d == 0)
2367c478bd9Sstevel@tonic-gate return (0);
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate if (feof(d->cfile))
2397c478bd9Sstevel@tonic-gate return (0);
2407c478bd9Sstevel@tonic-gate while ((c = getc(d->cfile)) != EOF &&
2417c478bd9Sstevel@tonic-gate (c == '\n' || c == '\t' || c == ' ' || c == ','))
2427c478bd9Sstevel@tonic-gate continue;
2437c478bd9Sstevel@tonic-gate if (c == EOF)
2447c478bd9Sstevel@tonic-gate return (0);
2457c478bd9Sstevel@tonic-gate cp = d->tokval;
2467c478bd9Sstevel@tonic-gate if (c == '"') {
2477c478bd9Sstevel@tonic-gate while ((c = getc(d->cfile)) != EOF && c != '"') {
2487c478bd9Sstevel@tonic-gate if (c == '\\')
2497c478bd9Sstevel@tonic-gate c = getc(d->cfile);
2507c478bd9Sstevel@tonic-gate *cp++ = (char)c;
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate } else {
2537c478bd9Sstevel@tonic-gate *cp++ = (char)c;
2547c478bd9Sstevel@tonic-gate while ((c = getc(d->cfile)) != EOF &&
2557c478bd9Sstevel@tonic-gate c != '\n' && c != '\t' && c != ' ' && c != ',') {
2567c478bd9Sstevel@tonic-gate if (c == '\\')
2577c478bd9Sstevel@tonic-gate c = getc(d->cfile);
2587c478bd9Sstevel@tonic-gate *cp++ = (char)c;
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate *cp = 0;
2627c478bd9Sstevel@tonic-gate if (d->tokval[0] == 0)
2637c478bd9Sstevel@tonic-gate return (0);
2647c478bd9Sstevel@tonic-gate for (t = d->toktab; t->tokstr; t++)
2657c478bd9Sstevel@tonic-gate if ((strcmp(t->tokstr, d->tokval) == 0))
2667c478bd9Sstevel@tonic-gate return (t->tval);
2677c478bd9Sstevel@tonic-gate return (ID);
2687c478bd9Sstevel@tonic-gate }
269