10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate * Copyright 1989 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
27*722Smuffin #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <ctype.h>
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/stat.h>
330Sstevel@tonic-gate #include <errno.h>
34*722Smuffin #include <malloc.h>
35*722Smuffin #include <strings.h>
36*722Smuffin #include <stdlib.h>
37*722Smuffin #include <unistd.h>
380Sstevel@tonic-gate
39*722Smuffin char *getpass();
400Sstevel@tonic-gate
410Sstevel@tonic-gate #define DEFAULT 1
420Sstevel@tonic-gate #define LOGIN 2
430Sstevel@tonic-gate #define PASSWD 3
440Sstevel@tonic-gate #define NOTIFY 4
450Sstevel@tonic-gate #define WRITE 5
460Sstevel@tonic-gate #define YES 6
470Sstevel@tonic-gate #define NO 7
480Sstevel@tonic-gate #define COMMAND 8
490Sstevel@tonic-gate #define FORCE 9
500Sstevel@tonic-gate #define ID 10
510Sstevel@tonic-gate #define MACHINE 11
520Sstevel@tonic-gate
530Sstevel@tonic-gate #define MAXTOKEN 11
540Sstevel@tonic-gate #define NTOKENS (MAXTOKEN - 1 + 2 + 1) /* two duplicates and null, minus id */
550Sstevel@tonic-gate
56*722Smuffin static void rnetrc(char *, char **, char **);
57*722Smuffin static int token(void);
58*722Smuffin
590Sstevel@tonic-gate static struct ruserdata {
600Sstevel@tonic-gate char tokval[100];
610Sstevel@tonic-gate struct toktab {
620Sstevel@tonic-gate char *tokstr;
630Sstevel@tonic-gate int tval;
640Sstevel@tonic-gate } toktab[NTOKENS];
650Sstevel@tonic-gate FILE *cfile;
66*722Smuffin } *ruserdata, *_ruserdata(void);
670Sstevel@tonic-gate
680Sstevel@tonic-gate
690Sstevel@tonic-gate static struct ruserdata *
_ruserdata(void)70*722Smuffin _ruserdata(void)
710Sstevel@tonic-gate {
72*722Smuffin struct ruserdata *d = ruserdata;
730Sstevel@tonic-gate struct toktab *t;
740Sstevel@tonic-gate
750Sstevel@tonic-gate if (d == 0) {
760Sstevel@tonic-gate if ((d = (struct ruserdata *)
770Sstevel@tonic-gate calloc(1, sizeof(struct ruserdata))) == NULL) {
780Sstevel@tonic-gate return(NULL);
790Sstevel@tonic-gate }
800Sstevel@tonic-gate ruserdata = d;
810Sstevel@tonic-gate t = d->toktab;
820Sstevel@tonic-gate t->tokstr = "default"; t++->tval = DEFAULT;
830Sstevel@tonic-gate t->tokstr = "login"; t++->tval = LOGIN;
840Sstevel@tonic-gate t->tokstr = "password"; t++->tval = PASSWD;
850Sstevel@tonic-gate t->tokstr = "notify"; t++->tval = NOTIFY;
860Sstevel@tonic-gate t->tokstr = "write"; t++->tval = WRITE;
870Sstevel@tonic-gate t->tokstr = "yes"; t++->tval = YES;
880Sstevel@tonic-gate t->tokstr = "y"; t++->tval = YES;
890Sstevel@tonic-gate t->tokstr = "no"; t++->tval = NO;
900Sstevel@tonic-gate t->tokstr = "n"; t++->tval = NO;
910Sstevel@tonic-gate t->tokstr = "command"; t++->tval = COMMAND;
920Sstevel@tonic-gate t->tokstr = "force"; t++->tval = FORCE;
930Sstevel@tonic-gate t->tokstr = "machine"; t++->tval = MACHINE;
940Sstevel@tonic-gate t->tokstr = 0; t->tval = 0;
950Sstevel@tonic-gate }
960Sstevel@tonic-gate return(d);
970Sstevel@tonic-gate }
980Sstevel@tonic-gate
99*722Smuffin void
_ruserpass(char * host,char ** aname,char ** apass)100*722Smuffin _ruserpass(char *host, char **aname, char **apass)
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate if (*aname == 0 || *apass == 0)
1040Sstevel@tonic-gate rnetrc(host, aname, apass);
1050Sstevel@tonic-gate if (*aname == 0) {
1060Sstevel@tonic-gate char *myname = getlogin();
1070Sstevel@tonic-gate *aname = malloc(16);
1080Sstevel@tonic-gate printf("Name (%s:%s): ", host, myname);
1090Sstevel@tonic-gate fflush(stdout);
1100Sstevel@tonic-gate if (read(2, *aname, 16) <= 0)
1110Sstevel@tonic-gate exit(1);
1120Sstevel@tonic-gate if ((*aname)[0] == '\n')
1130Sstevel@tonic-gate *aname = myname;
1140Sstevel@tonic-gate else
1150Sstevel@tonic-gate if (index(*aname, '\n'))
1160Sstevel@tonic-gate *index(*aname, '\n') = 0;
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate if (*aname && *apass == 0) {
1190Sstevel@tonic-gate printf("Password (%s:%s): ", host, *aname);
1200Sstevel@tonic-gate fflush(stdout);
1210Sstevel@tonic-gate *apass = getpass("");
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate
126*722Smuffin static void
rnetrc(char * host,char ** aname,char ** apass)127*722Smuffin rnetrc(char *host, char **aname, char **apass)
1280Sstevel@tonic-gate {
129*722Smuffin struct ruserdata *d = _ruserdata();
1300Sstevel@tonic-gate char *hdir, buf[BUFSIZ];
1310Sstevel@tonic-gate int t;
1320Sstevel@tonic-gate struct stat stb;
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate if (d == 0)
1350Sstevel@tonic-gate return;
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate hdir = getenv("HOME");
1380Sstevel@tonic-gate if (hdir == NULL)
1390Sstevel@tonic-gate hdir = ".";
1400Sstevel@tonic-gate sprintf(buf, "%s/.netrc", hdir);
1410Sstevel@tonic-gate d->cfile = fopen(buf, "r");
1420Sstevel@tonic-gate if (d->cfile == NULL) {
1430Sstevel@tonic-gate if (errno != ENOENT)
1440Sstevel@tonic-gate perror(buf);
1450Sstevel@tonic-gate return;
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate next:
1480Sstevel@tonic-gate while ((t = token())) switch(t) {
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate case DEFAULT:
1510Sstevel@tonic-gate (void) token();
1520Sstevel@tonic-gate continue;
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate case MACHINE:
1550Sstevel@tonic-gate if (token() != ID || strcmp(host, d->tokval))
1560Sstevel@tonic-gate continue;
1570Sstevel@tonic-gate while ((t = token()) && t != MACHINE) switch(t) {
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate case LOGIN:
1600Sstevel@tonic-gate if (token())
1610Sstevel@tonic-gate if (*aname == 0) {
1620Sstevel@tonic-gate *aname = malloc(strlen(d->tokval) + 1);
1630Sstevel@tonic-gate strcpy(*aname, d->tokval);
1640Sstevel@tonic-gate } else {
1650Sstevel@tonic-gate if (strcmp(*aname, d->tokval))
1660Sstevel@tonic-gate goto next;
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate break;
1690Sstevel@tonic-gate case PASSWD:
1700Sstevel@tonic-gate if (fstat(fileno(d->cfile), &stb) >= 0
1710Sstevel@tonic-gate && (stb.st_mode & 077) != 0) {
1720Sstevel@tonic-gate fprintf(stderr, "Error - .netrc file not correct mode.\n");
1730Sstevel@tonic-gate fprintf(stderr, "Remove password or correct mode.\n");
1740Sstevel@tonic-gate exit(1);
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate if (token() && *apass == 0) {
1770Sstevel@tonic-gate *apass = malloc(strlen(d->tokval) + 1);
1780Sstevel@tonic-gate strcpy(*apass, d->tokval);
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate break;
1810Sstevel@tonic-gate case COMMAND:
1820Sstevel@tonic-gate case NOTIFY:
1830Sstevel@tonic-gate case WRITE:
1840Sstevel@tonic-gate case FORCE:
1850Sstevel@tonic-gate (void) token();
1860Sstevel@tonic-gate break;
1870Sstevel@tonic-gate default:
1880Sstevel@tonic-gate fprintf(stderr, "Unknown .netrc option %s\n", d->tokval);
1890Sstevel@tonic-gate break;
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate goto done;
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate done:
1940Sstevel@tonic-gate fclose(d->cfile);
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate
197*722Smuffin static int
token(void)198*722Smuffin token(void)
1990Sstevel@tonic-gate {
200*722Smuffin struct ruserdata *d = _ruserdata();
2010Sstevel@tonic-gate char *cp;
2020Sstevel@tonic-gate int c;
2030Sstevel@tonic-gate struct toktab *t;
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate if (d == 0)
2060Sstevel@tonic-gate return(0);
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate if (feof(d->cfile))
2090Sstevel@tonic-gate return (0);
2100Sstevel@tonic-gate while ((c = getc(d->cfile)) != EOF &&
2110Sstevel@tonic-gate (c == '\n' || c == '\t' || c == ' ' || c == ','))
2120Sstevel@tonic-gate continue;
2130Sstevel@tonic-gate if (c == EOF)
2140Sstevel@tonic-gate return (0);
2150Sstevel@tonic-gate cp = d->tokval;
2160Sstevel@tonic-gate if (c == '"') {
2170Sstevel@tonic-gate while ((c = getc(d->cfile)) != EOF && c != '"') {
2180Sstevel@tonic-gate if (c == '\\')
2190Sstevel@tonic-gate c = getc(d->cfile);
2200Sstevel@tonic-gate *cp++ = c;
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate } else {
2230Sstevel@tonic-gate *cp++ = c;
2240Sstevel@tonic-gate while ((c = getc(d->cfile)) != EOF
2250Sstevel@tonic-gate && c != '\n' && c != '\t' && c != ' ' && c != ',') {
2260Sstevel@tonic-gate if (c == '\\')
2270Sstevel@tonic-gate c = getc(d->cfile);
2280Sstevel@tonic-gate *cp++ = c;
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate *cp = 0;
2320Sstevel@tonic-gate if (d->tokval[0] == 0)
2330Sstevel@tonic-gate return (0);
2340Sstevel@tonic-gate for (t = d->toktab; t->tokstr; t++)
2350Sstevel@tonic-gate if (!strcmp(t->tokstr, d->tokval))
2360Sstevel@tonic-gate return (t->tval);
2370Sstevel@tonic-gate return (ID);
2380Sstevel@tonic-gate }
239