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
42 #include <sys/types.h>
55 static void rnetrc(const char *host
, char **aname
, char **apass
);
71 #define NTOKENS (MAXTOKEN - 1 + 2 + 1) /* two duplicates and null, minus id */
73 static struct ruserdata
{
80 } *ruserdata
, *_ruserdata();
83 static struct ruserdata
*
86 struct ruserdata
*d
= ruserdata
;
90 if ((d
= (struct ruserdata
*)
91 calloc(1, sizeof (struct ruserdata
))) == NULL
) {
96 t
->tokstr
= "default"; t
++->tval
= DEFAULT
;
97 t
->tokstr
= "login"; t
++->tval
= LOGIN
;
98 t
->tokstr
= "password"; t
++->tval
= PASSWD
;
99 t
->tokstr
= "notify"; t
++->tval
= NOTIFY
;
100 t
->tokstr
= "write"; t
++->tval
= WRITE
;
101 t
->tokstr
= "yes"; t
++->tval
= YES
;
102 t
->tokstr
= "y"; t
++->tval
= YES
;
103 t
->tokstr
= "no"; t
++->tval
= NO
;
104 t
->tokstr
= "n"; t
++->tval
= NO
;
105 t
->tokstr
= "command"; t
++->tval
= COMMAND
;
106 t
->tokstr
= "force"; t
++->tval
= FORCE
;
107 t
->tokstr
= "machine"; t
++->tval
= MACHINE
;
108 t
->tokstr
= 0; t
->tval
= 0;
117 _ruserpass(const char *host
, char **aname
, char **apass
)
120 if (*aname
== 0 || *apass
== 0)
121 rnetrc(host
, aname
, apass
);
123 char myname
[L_cuserid
];
125 *aname
= malloc(MAXANAME
+ 1);
126 (void) cuserid(myname
);
127 (void) printf(dgettext(TEXT_DOMAIN
, "Name (%s:%s): "),
129 (void) fflush(stdout
);
130 if (read(2, *aname
, MAXANAME
) <= 0)
132 aname
[0][MAXANAME
] = '\0';
133 if ((*aname
)[0] == '\n')
134 (void) strcpy(*aname
, myname
);
136 if (index(*aname
, '\n'))
137 *index(*aname
, '\n') = 0;
139 if (*aname
&& *apass
== 0) {
140 (void) printf(dgettext(TEXT_DOMAIN
, "Password (%s:%s): "),
142 (void) fflush(stdout
);
143 *apass
= getpass("");
149 rnetrc(const char *host
, char **aname
, char **apass
)
151 struct ruserdata
*d
= _ruserdata();
152 char *hdir
, buf
[PATH_MAX
];
159 hdir
= getenv("HOME");
162 t
= snprintf(buf
, sizeof (buf
), "%s/.netrc", hdir
);
163 if (t
< 0 || t
>= sizeof (buf
)) {
167 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
168 "HOME directory name is too long: %s\n"), hdir
);
173 d
->cfile
= fopen(buf
, "rF");
174 if (d
->cfile
== NULL
) {
180 while ((t
= token()))
188 if (token() != ID
|| strcmp(host
, d
->tokval
))
190 while ((t
= token()) != 0 && t
!= MACHINE
)
196 *aname
= malloc(strlen(d
->tokval
) + 1);
197 (void) strcpy(*aname
, d
->tokval
);
199 if (strcmp(*aname
, d
->tokval
))
204 if (fstat64(fileno(d
->cfile
), &stb
) >= 0 &&
205 (stb
.st_mode
& 077) != 0) {
206 (void) fprintf(stderr
,
207 dgettext(TEXT_DOMAIN
,
208 "Error - .netrc file not correct mode.\n"));
209 (void) fprintf(stderr
,
210 dgettext(TEXT_DOMAIN
,
211 "Remove password or correct mode.\n"));
214 if (token() && *apass
== 0) {
215 *apass
= malloc(strlen(d
->tokval
) + 1);
216 (void) strcpy(*apass
, d
->tokval
);
226 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
227 "Unknown .netrc option %s\n"), d
->tokval
);
233 (void) fclose(d
->cfile
);
239 struct ruserdata
*d
= _ruserdata();
249 while ((c
= getc(d
->cfile
)) != EOF
&&
250 (c
== '\n' || c
== '\t' || c
== ' ' || c
== ','))
256 while ((c
= getc(d
->cfile
)) != EOF
&& c
!= '"') {
263 while ((c
= getc(d
->cfile
)) != EOF
&&
264 c
!= '\n' && c
!= '\t' && c
!= ' ' && c
!= ',') {
271 if (d
->tokval
[0] == 0)
273 for (t
= d
->toktab
; t
->tokstr
; t
++)
274 if ((strcmp(t
->tokstr
, d
->tokval
) == 0))