4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
40 #pragma ident "%Z%%M% %I% %E% SMI"
44 #include <sys/types.h>
56 static void rnetrc(const char *host
, char **aname
, char **apass
);
72 #define NTOKENS (MAXTOKEN - 1 + 2 + 1) /* two duplicates and null, minus id */
74 static struct ruserdata
{
81 } *ruserdata
, *_ruserdata();
84 static struct ruserdata
*
87 struct ruserdata
*d
= ruserdata
;
91 if ((d
= (struct ruserdata
*)
92 calloc(1, sizeof (struct ruserdata
))) == NULL
) {
97 t
->tokstr
= "default"; t
++->tval
= DEFAULT
;
98 t
->tokstr
= "login"; t
++->tval
= LOGIN
;
99 t
->tokstr
= "password"; t
++->tval
= PASSWD
;
100 t
->tokstr
= "notify"; t
++->tval
= NOTIFY
;
101 t
->tokstr
= "write"; t
++->tval
= WRITE
;
102 t
->tokstr
= "yes"; t
++->tval
= YES
;
103 t
->tokstr
= "y"; t
++->tval
= YES
;
104 t
->tokstr
= "no"; t
++->tval
= NO
;
105 t
->tokstr
= "n"; t
++->tval
= NO
;
106 t
->tokstr
= "command"; t
++->tval
= COMMAND
;
107 t
->tokstr
= "force"; t
++->tval
= FORCE
;
108 t
->tokstr
= "machine"; t
++->tval
= MACHINE
;
109 t
->tokstr
= 0; t
->tval
= 0;
118 _ruserpass(const char *host
, char **aname
, char **apass
)
121 if (*aname
== 0 || *apass
== 0)
122 rnetrc(host
, aname
, apass
);
124 char myname
[L_cuserid
];
126 *aname
= malloc(MAXANAME
+ 1);
127 (void) cuserid(myname
);
128 (void) printf(dgettext(TEXT_DOMAIN
, "Name (%s:%s): "),
130 (void) fflush(stdout
);
131 if (read(2, *aname
, MAXANAME
) <= 0)
133 aname
[0][MAXANAME
] = '\0';
134 if ((*aname
)[0] == '\n')
135 (void) strcpy(*aname
, myname
);
137 if (index(*aname
, '\n'))
138 *index(*aname
, '\n') = 0;
140 if (*aname
&& *apass
== 0) {
141 (void) printf(dgettext(TEXT_DOMAIN
, "Password (%s:%s): "),
143 (void) fflush(stdout
);
144 *apass
= getpass("");
150 rnetrc(const char *host
, char **aname
, char **apass
)
152 struct ruserdata
*d
= _ruserdata();
153 char *hdir
, buf
[BUFSIZ
];
160 hdir
= getenv("HOME");
163 (void) sprintf(buf
, "%s/.netrc", hdir
);
164 d
->cfile
= fopen(buf
, "rF");
165 if (d
->cfile
== NULL
) {
171 while ((t
= token()))
179 if (token() != ID
|| strcmp(host
, d
->tokval
))
181 while ((t
= token()) != 0 && t
!= MACHINE
)
187 *aname
= malloc(strlen(d
->tokval
) + 1);
188 (void) strcpy(*aname
, d
->tokval
);
190 if (strcmp(*aname
, d
->tokval
))
195 if (fstat64(fileno(d
->cfile
), &stb
) >= 0 &&
196 (stb
.st_mode
& 077) != 0) {
197 (void) fprintf(stderr
,
198 dgettext(TEXT_DOMAIN
,
199 "Error - .netrc file not correct mode.\n"));
200 (void) fprintf(stderr
,
201 dgettext(TEXT_DOMAIN
,
202 "Remove password or correct mode.\n"));
205 if (token() && *apass
== 0) {
206 *apass
= malloc(strlen(d
->tokval
) + 1);
207 (void) strcpy(*apass
, d
->tokval
);
217 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
218 "Unknown .netrc option %s\n"), d
->tokval
);
224 (void) fclose(d
->cfile
);
230 struct ruserdata
*d
= _ruserdata();
240 while ((c
= getc(d
->cfile
)) != EOF
&&
241 (c
== '\n' || c
== '\t' || c
== ' ' || c
== ','))
247 while ((c
= getc(d
->cfile
)) != EOF
&& c
!= '"') {
254 while ((c
= getc(d
->cfile
)) != EOF
&&
255 c
!= '\n' && c
!= '\t' && c
!= ' ' && c
!= ',') {
262 if (d
->tokval
[0] == 0)
264 for (t
= d
->toktab
; t
->tokstr
; t
++)
265 if ((strcmp(t
->tokstr
, d
->tokval
) == 0))