2 * Copyright (c) 1985 Regents of the University of California.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 static char sccsid
[] = "@(#)ruserpass.c 5.1 (Berkeley) 3/1/89";
25 struct utmp
*getutmp();
28 #ifndef MAXHOSTNAMELEN
29 #define MAXHOSTNAMELEN 64
40 static char tokval
[100];
42 static struct toktab
{
56 extern char *hostname
;
57 static int token(void);
59 int ruserpass(const char *host
, char **aname
, char **apass
, char **aacct
)
61 const char *hdir
, *mydomain
;
62 char buf
[BUFSIZ
], *tmp
;
63 char myname
[MAXHOSTNAMELEN
];
64 int t
, i
, c
, usedefault
= 0;
67 hdir
= getenv("HOME");
70 (void) sprintf(buf
, "%s/.netrc", hdir
);
71 cfile
= fopen(buf
, "r");
79 if (gethostname(myname
, sizeof(myname
)) < 0)
81 if ((mydomain
= index(myname
, '.')) == NULL
)
84 while ((t
= token())) switch(t
) {
95 * Allow match either for user's input host name
96 * or official hostname. Also allow match of
97 * incompletely-specified host in local domain.
99 if (strcasecmp(host
, tokval
) == 0)
101 if (strcasecmp(hostname
, tokval
) == 0)
103 if ((tmp
= index(hostname
, '.')) != NULL
&&
104 strcasecmp(tmp
, mydomain
) == 0 &&
105 strncasecmp(hostname
, tokval
, tmp
- hostname
) == 0 &&
106 tokval
[tmp
- hostname
] == '\0')
108 if ((tmp
= index(host
, '.')) != NULL
&&
109 strcasecmp(tmp
, mydomain
) == 0 &&
110 strncasecmp(host
, tokval
, tmp
- host
) == 0 &&
111 tokval
[tmp
- host
] == '\0')
116 while ((t
= token()) && t
!= MACH
&& t
!= DEFAULT
) switch(t
) {
121 *aname
= malloc((unsigned) strlen(tokval
) + 1);
122 (void) strcpy(*aname
, tokval
);
124 if (strcmp(*aname
, tokval
))
130 if (strcmp(*aname
, "anonymous") &&
131 fstat(fileno(cfile
), &stb
) >= 0 &&
132 (stb
.st_mode
& 077) != 0) {
133 fprintf(stderr
, "Error - .netrc file not correct mode.\n");
134 fprintf(stderr
, "Remove password or correct mode.\n");
137 if (token() && *apass
== 0) {
138 *apass
= malloc((unsigned) strlen(tokval
) + 1);
139 (void) strcpy(*apass
, tokval
);
143 if (fstat(fileno(cfile
), &stb
) >= 0
144 && (stb
.st_mode
& 077) != 0) {
145 fprintf(stderr
, "Error - .netrc file not correct mode.\n");
146 fprintf(stderr
, "Remove account or correct mode.\n");
149 if (token() && *aacct
== 0) {
150 *aacct
= malloc((unsigned) strlen(tokval
) + 1);
151 (void) strcpy(*aacct
, tokval
);
156 (void) fclose(cfile
);
159 while ((c
=getc(cfile
)) != EOF
&& (c
== ' ' || c
== '\t'));
160 if (c
== EOF
|| c
== '\n') {
161 printf("Missing macdef name argument.\n");
165 printf("Limit of 16 macros have already been defined\n");
168 tmp
= macros
[macnum
].mac_name
;
170 for (i
=0; i
< 8 && (c
=getc(cfile
)) != EOF
&&
175 printf("Macro definition missing null line terminator.\n");
180 while ((c
=getc(cfile
)) != EOF
&& c
!= '\n');
183 printf("Macro definition missing null line terminator.\n");
187 macros
[macnum
].mac_start
= macbuf
;
190 macros
[macnum
].mac_start
= macros
[macnum
-1].mac_end
+ 1;
192 tmp
= macros
[macnum
].mac_start
;
193 while (tmp
!= macbuf
+ 4096) {
194 if ((c
=getc(cfile
)) == EOF
) {
195 printf("Macro definition missing null line terminator.\n");
200 if (*(tmp
-1) == '\0') {
201 macros
[macnum
++].mac_end
= tmp
- 1;
208 if (tmp
== macbuf
+ 4096) {
209 printf("4K macro buffer exceeded\n");
214 fprintf(stderr
, "Unknown .netrc keyword %s\n", tokval
);
220 (void) fclose(cfile
);
223 (void) fclose(cfile
);
227 static int token(void)
235 while ((c
= getc(cfile
)) != EOF
&&
236 (c
== '\r' || c
== '\n' || c
== '\t' || c
== ' ' || c
== ','))
242 while ((c
= getc(cfile
)) != EOF
&& c
!= '"') {
249 while ((c
= getc(cfile
)) != EOF
250 && c
!= '\n' && c
!= '\t' && c
!= ' ' && c
!= ',' && c
!= '\r') {
259 for (t
= toktab
; t
->tokstr
; t
++)
260 if (!strcmp(t
->tokstr
, tokval
))