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*004388ebScasper * Common Development and Distribution License (the "License"). 6*004388ebScasper * 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*004388ebScasper * 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) 1984, 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 <stdio.h> 427c478bd9Sstevel@tonic-gate #include <ctype.h> 437c478bd9Sstevel@tonic-gate #include <sys/types.h> 447c478bd9Sstevel@tonic-gate #include <sys/stat.h> 457c478bd9Sstevel@tonic-gate #include <errno.h> 467c478bd9Sstevel@tonic-gate #include <unistd.h> 477c478bd9Sstevel@tonic-gate #include <strings.h> 487c478bd9Sstevel@tonic-gate #include <stdlib.h> 497c478bd9Sstevel@tonic-gate #include <libintl.h> 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate extern char *_dgettext(); 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate #ifdef SYSV 547c478bd9Sstevel@tonic-gate #define index strchr 557c478bd9Sstevel@tonic-gate #endif /* SYSV */ 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate static void rnetrc(const char *host, char **aname, char **apass); 587c478bd9Sstevel@tonic-gate static int token(); 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate #define DEFAULT 1 617c478bd9Sstevel@tonic-gate #define LOGIN 2 627c478bd9Sstevel@tonic-gate #define PASSWD 3 637c478bd9Sstevel@tonic-gate #define NOTIFY 4 647c478bd9Sstevel@tonic-gate #define WRITE 5 657c478bd9Sstevel@tonic-gate #define YES 6 667c478bd9Sstevel@tonic-gate #define NO 7 677c478bd9Sstevel@tonic-gate #define COMMAND 8 687c478bd9Sstevel@tonic-gate #define FORCE 9 697c478bd9Sstevel@tonic-gate #define ID 10 707c478bd9Sstevel@tonic-gate #define MACHINE 11 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate #define MAXTOKEN 11 737c478bd9Sstevel@tonic-gate #define NTOKENS (MAXTOKEN - 1 + 2 + 1) /* two duplicates and null, minus id */ 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate static struct ruserdata { 767c478bd9Sstevel@tonic-gate char tokval[100]; 777c478bd9Sstevel@tonic-gate struct toktab { 787c478bd9Sstevel@tonic-gate char *tokstr; 797c478bd9Sstevel@tonic-gate int tval; 807c478bd9Sstevel@tonic-gate } toktab[NTOKENS]; 817c478bd9Sstevel@tonic-gate FILE *cfile; 827c478bd9Sstevel@tonic-gate } *ruserdata, *_ruserdata(); 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate static struct ruserdata * 867c478bd9Sstevel@tonic-gate _ruserdata() 877c478bd9Sstevel@tonic-gate { 887c478bd9Sstevel@tonic-gate struct ruserdata *d = ruserdata; 897c478bd9Sstevel@tonic-gate struct toktab *t; 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate if (d == 0) { 927c478bd9Sstevel@tonic-gate if ((d = (struct ruserdata *) 937c478bd9Sstevel@tonic-gate calloc(1, sizeof (struct ruserdata))) == NULL) { 947c478bd9Sstevel@tonic-gate return (NULL); 957c478bd9Sstevel@tonic-gate } 967c478bd9Sstevel@tonic-gate ruserdata = d; 977c478bd9Sstevel@tonic-gate t = d->toktab; 987c478bd9Sstevel@tonic-gate t->tokstr = "default"; t++->tval = DEFAULT; 997c478bd9Sstevel@tonic-gate t->tokstr = "login"; t++->tval = LOGIN; 1007c478bd9Sstevel@tonic-gate t->tokstr = "password"; t++->tval = PASSWD; 1017c478bd9Sstevel@tonic-gate t->tokstr = "notify"; t++->tval = NOTIFY; 1027c478bd9Sstevel@tonic-gate t->tokstr = "write"; t++->tval = WRITE; 1037c478bd9Sstevel@tonic-gate t->tokstr = "yes"; t++->tval = YES; 1047c478bd9Sstevel@tonic-gate t->tokstr = "y"; t++->tval = YES; 1057c478bd9Sstevel@tonic-gate t->tokstr = "no"; t++->tval = NO; 1067c478bd9Sstevel@tonic-gate t->tokstr = "n"; t++->tval = NO; 1077c478bd9Sstevel@tonic-gate t->tokstr = "command"; t++->tval = COMMAND; 1087c478bd9Sstevel@tonic-gate t->tokstr = "force"; t++->tval = FORCE; 1097c478bd9Sstevel@tonic-gate t->tokstr = "machine"; t++->tval = MACHINE; 1107c478bd9Sstevel@tonic-gate t->tokstr = 0; t->tval = 0; 1117c478bd9Sstevel@tonic-gate } 1127c478bd9Sstevel@tonic-gate return (d); 1137c478bd9Sstevel@tonic-gate } 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate #define MAXANAME 16 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate void 1197c478bd9Sstevel@tonic-gate _ruserpass(const char *host, char **aname, char **apass) 1207c478bd9Sstevel@tonic-gate { 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate if (*aname == 0 || *apass == 0) 1237c478bd9Sstevel@tonic-gate rnetrc(host, aname, apass); 1247c478bd9Sstevel@tonic-gate if (*aname == 0) { 1257c478bd9Sstevel@tonic-gate char myname[L_cuserid]; 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate *aname = malloc(MAXANAME + 1); 1287c478bd9Sstevel@tonic-gate (void) cuserid(myname); 129*004388ebScasper (void) printf(_dgettext(TEXT_DOMAIN, "Name (%s:%s): "), 130*004388ebScasper host, myname); 1317c478bd9Sstevel@tonic-gate (void) fflush(stdout); 1327c478bd9Sstevel@tonic-gate if (read(2, *aname, MAXANAME) <= 0) 1337c478bd9Sstevel@tonic-gate exit(1); 1347c478bd9Sstevel@tonic-gate aname[0][MAXANAME] = '\0'; 1357c478bd9Sstevel@tonic-gate if ((*aname)[0] == '\n') 1367c478bd9Sstevel@tonic-gate (void) strcpy(*aname, myname); 1377c478bd9Sstevel@tonic-gate else 1387c478bd9Sstevel@tonic-gate if (index(*aname, '\n')) 1397c478bd9Sstevel@tonic-gate *index(*aname, '\n') = 0; 1407c478bd9Sstevel@tonic-gate } 1417c478bd9Sstevel@tonic-gate if (*aname && *apass == 0) { 142*004388ebScasper (void) printf(_dgettext(TEXT_DOMAIN, "Password (%s:%s): "), 1437c478bd9Sstevel@tonic-gate host, *aname); 1447c478bd9Sstevel@tonic-gate (void) fflush(stdout); 1457c478bd9Sstevel@tonic-gate *apass = getpass(""); 1467c478bd9Sstevel@tonic-gate } 1477c478bd9Sstevel@tonic-gate } 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate static void 1517c478bd9Sstevel@tonic-gate rnetrc(const char *host, char **aname, char **apass) 1527c478bd9Sstevel@tonic-gate { 1537c478bd9Sstevel@tonic-gate struct ruserdata *d = _ruserdata(); 1547c478bd9Sstevel@tonic-gate char *hdir, buf[BUFSIZ]; 1557c478bd9Sstevel@tonic-gate int t; 1567c478bd9Sstevel@tonic-gate struct stat64 stb; 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate if (d == 0) 1597c478bd9Sstevel@tonic-gate return; 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate hdir = getenv("HOME"); 1627c478bd9Sstevel@tonic-gate if (hdir == NULL) 1637c478bd9Sstevel@tonic-gate hdir = "."; 1647c478bd9Sstevel@tonic-gate (void) sprintf(buf, "%s/.netrc", hdir); 165*004388ebScasper d->cfile = fopen(buf, "rF"); 1667c478bd9Sstevel@tonic-gate if (d->cfile == NULL) { 1677c478bd9Sstevel@tonic-gate if (errno != ENOENT) 1687c478bd9Sstevel@tonic-gate perror(buf); 1697c478bd9Sstevel@tonic-gate return; 1707c478bd9Sstevel@tonic-gate } 1717c478bd9Sstevel@tonic-gate next: 1727c478bd9Sstevel@tonic-gate while ((t = token())) 1737c478bd9Sstevel@tonic-gate switch (t) { 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate case DEFAULT: 1767c478bd9Sstevel@tonic-gate (void) token(); 1777c478bd9Sstevel@tonic-gate continue; 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate case MACHINE: 1807c478bd9Sstevel@tonic-gate if (token() != ID || strcmp(host, d->tokval)) 1817c478bd9Sstevel@tonic-gate continue; 1827c478bd9Sstevel@tonic-gate while ((t = token()) != 0 && t != MACHINE) 1837c478bd9Sstevel@tonic-gate switch (t) { 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate case LOGIN: 1867c478bd9Sstevel@tonic-gate if (token()) 1877c478bd9Sstevel@tonic-gate if (*aname == 0) { 1887c478bd9Sstevel@tonic-gate *aname = malloc(strlen(d->tokval) + 1); 1897c478bd9Sstevel@tonic-gate (void) strcpy(*aname, d->tokval); 1907c478bd9Sstevel@tonic-gate } else { 1917c478bd9Sstevel@tonic-gate if (strcmp(*aname, d->tokval)) 1927c478bd9Sstevel@tonic-gate goto next; 1937c478bd9Sstevel@tonic-gate } 1947c478bd9Sstevel@tonic-gate break; 1957c478bd9Sstevel@tonic-gate case PASSWD: 1967c478bd9Sstevel@tonic-gate if (fstat64(fileno(d->cfile), &stb) >= 0 && 1977c478bd9Sstevel@tonic-gate (stb.st_mode & 077) != 0) { 1987c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 199*004388ebScasper _dgettext(TEXT_DOMAIN, 200*004388ebScasper "Error - .netrc file not correct mode.\n")); 2017c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 202*004388ebScasper _dgettext(TEXT_DOMAIN, 203*004388ebScasper "Remove password or correct mode.\n")); 2047c478bd9Sstevel@tonic-gate exit(1); 2057c478bd9Sstevel@tonic-gate } 2067c478bd9Sstevel@tonic-gate if (token() && *apass == 0) { 2077c478bd9Sstevel@tonic-gate *apass = malloc(strlen(d->tokval) + 1); 2087c478bd9Sstevel@tonic-gate (void) strcpy(*apass, d->tokval); 2097c478bd9Sstevel@tonic-gate } 2107c478bd9Sstevel@tonic-gate break; 2117c478bd9Sstevel@tonic-gate case COMMAND: 2127c478bd9Sstevel@tonic-gate case NOTIFY: 2137c478bd9Sstevel@tonic-gate case WRITE: 2147c478bd9Sstevel@tonic-gate case FORCE: 2157c478bd9Sstevel@tonic-gate (void) token(); 2167c478bd9Sstevel@tonic-gate break; 2177c478bd9Sstevel@tonic-gate default: 218*004388ebScasper (void) fprintf(stderr, _dgettext(TEXT_DOMAIN, 219*004388ebScasper "Unknown .netrc option %s\n"), d->tokval); 2207c478bd9Sstevel@tonic-gate break; 2217c478bd9Sstevel@tonic-gate } 2227c478bd9Sstevel@tonic-gate goto done; 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate done: 2257c478bd9Sstevel@tonic-gate (void) fclose(d->cfile); 2267c478bd9Sstevel@tonic-gate } 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate static int 2297c478bd9Sstevel@tonic-gate token() 2307c478bd9Sstevel@tonic-gate { 2317c478bd9Sstevel@tonic-gate struct ruserdata *d = _ruserdata(); 2327c478bd9Sstevel@tonic-gate char *cp; 2337c478bd9Sstevel@tonic-gate int c; 2347c478bd9Sstevel@tonic-gate struct toktab *t; 2357c478bd9Sstevel@tonic-gate 2367c478bd9Sstevel@tonic-gate if (d == 0) 2377c478bd9Sstevel@tonic-gate return (0); 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate if (feof(d->cfile)) 2407c478bd9Sstevel@tonic-gate return (0); 2417c478bd9Sstevel@tonic-gate while ((c = getc(d->cfile)) != EOF && 2427c478bd9Sstevel@tonic-gate (c == '\n' || c == '\t' || c == ' ' || c == ',')) 2437c478bd9Sstevel@tonic-gate continue; 2447c478bd9Sstevel@tonic-gate if (c == EOF) 2457c478bd9Sstevel@tonic-gate return (0); 2467c478bd9Sstevel@tonic-gate cp = d->tokval; 2477c478bd9Sstevel@tonic-gate if (c == '"') { 2487c478bd9Sstevel@tonic-gate while ((c = getc(d->cfile)) != EOF && c != '"') { 2497c478bd9Sstevel@tonic-gate if (c == '\\') 2507c478bd9Sstevel@tonic-gate c = getc(d->cfile); 2517c478bd9Sstevel@tonic-gate *cp++ = (char)c; 2527c478bd9Sstevel@tonic-gate } 2537c478bd9Sstevel@tonic-gate } else { 2547c478bd9Sstevel@tonic-gate *cp++ = (char)c; 2557c478bd9Sstevel@tonic-gate while ((c = getc(d->cfile)) != EOF && 2567c478bd9Sstevel@tonic-gate c != '\n' && c != '\t' && c != ' ' && c != ',') { 2577c478bd9Sstevel@tonic-gate if (c == '\\') 2587c478bd9Sstevel@tonic-gate c = getc(d->cfile); 2597c478bd9Sstevel@tonic-gate *cp++ = (char)c; 2607c478bd9Sstevel@tonic-gate } 2617c478bd9Sstevel@tonic-gate } 2627c478bd9Sstevel@tonic-gate *cp = 0; 2637c478bd9Sstevel@tonic-gate if (d->tokval[0] == 0) 2647c478bd9Sstevel@tonic-gate return (0); 2657c478bd9Sstevel@tonic-gate for (t = d->toktab; t->tokstr; t++) 2667c478bd9Sstevel@tonic-gate if ((strcmp(t->tokstr, d->tokval) == 0)) 2677c478bd9Sstevel@tonic-gate return (t->tval); 2687c478bd9Sstevel@tonic-gate return (ID); 2697c478bd9Sstevel@tonic-gate } 270