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";
22 #include <sys/types.h>
29 #include "prototypes.h"
32 struct utmp
*getutmp();
35 #ifndef MAXHOSTNAMELEN
36 #define MAXHOSTNAMELEN 64
47 static char tokval
[100];
49 static struct toktab
{
63 extern char *hostname
;
64 static int token(void);
66 int ruserpass(const char *host
, char **aname
, char **apass
, char **aacct
)
68 const char *hdir
, *mydomain
;
69 char buf
[BUFSIZ
], *tmp
;
70 char myname
[MAXHOSTNAMELEN
];
71 int t
, i
, c
, usedefault
= 0;
74 hdir
= getenv("HOME");
77 (void) sprintf(buf
, "%s/.netrc", hdir
);
78 cfile
= fopen(buf
, "r");
86 if (gethostname(myname
, sizeof(myname
)) < 0)
88 if ((mydomain
= index(myname
, '.')) == NULL
)
91 while ((t
= token())) switch(t
) {
102 * Allow match either for user's input host name
103 * or official hostname. Also allow match of
104 * incompletely-specified host in local domain.
106 if (strcasecmp(host
, tokval
) == 0)
108 if (strcasecmp(hostname
, tokval
) == 0)
110 if ((tmp
= index(hostname
, '.')) != NULL
&&
111 strcasecmp(tmp
, mydomain
) == 0 &&
112 strncasecmp(hostname
, tokval
, tmp
- hostname
) == 0 &&
113 tokval
[tmp
- hostname
] == '\0')
115 if ((tmp
= index(host
, '.')) != NULL
&&
116 strcasecmp(tmp
, mydomain
) == 0 &&
117 strncasecmp(host
, tokval
, tmp
- host
) == 0 &&
118 tokval
[tmp
- host
] == '\0')
123 while ((t
= token()) && t
!= MACH
&& t
!= DEFAULT
) switch(t
) {
128 *aname
= malloc((unsigned) strlen(tokval
) + 1);
129 (void) strcpy(*aname
, tokval
);
131 if (strcmp(*aname
, tokval
))
137 if (strcmp(*aname
, "anonymous") &&
138 fstat(fileno(cfile
), &stb
) >= 0 &&
139 (stb
.st_mode
& 077) != 0) {
140 fprintf(stderr
, "Error - .netrc file not correct mode.\n");
141 fprintf(stderr
, "Remove password or correct mode.\n");
144 if (token() && *apass
== 0) {
145 *apass
= malloc((unsigned) strlen(tokval
) + 1);
146 (void) strcpy(*apass
, tokval
);
150 if (fstat(fileno(cfile
), &stb
) >= 0
151 && (stb
.st_mode
& 077) != 0) {
152 fprintf(stderr
, "Error - .netrc file not correct mode.\n");
153 fprintf(stderr
, "Remove account or correct mode.\n");
156 if (token() && *aacct
== 0) {
157 *aacct
= malloc((unsigned) strlen(tokval
) + 1);
158 (void) strcpy(*aacct
, tokval
);
163 (void) fclose(cfile
);
166 while ((c
=getc(cfile
)) != EOF
&& (c
== ' ' || c
== '\t'));
167 if (c
== EOF
|| c
== '\n') {
168 printf("Missing macdef name argument.\n");
172 printf("Limit of 16 macros have already been defined\n");
175 tmp
= macros
[macnum
].mac_name
;
177 for (i
=0; i
< 8 && (c
=getc(cfile
)) != EOF
&&
182 printf("Macro definition missing null line terminator.\n");
187 while ((c
=getc(cfile
)) != EOF
&& c
!= '\n');
190 printf("Macro definition missing null line terminator.\n");
194 macros
[macnum
].mac_start
= macbuf
;
197 macros
[macnum
].mac_start
= macros
[macnum
-1].mac_end
+ 1;
199 tmp
= macros
[macnum
].mac_start
;
200 while (tmp
!= macbuf
+ 4096) {
201 if ((c
=getc(cfile
)) == EOF
) {
202 printf("Macro definition missing null line terminator.\n");
207 if (*(tmp
-1) == '\0') {
208 macros
[macnum
++].mac_end
= tmp
- 1;
215 if (tmp
== macbuf
+ 4096) {
216 printf("4K macro buffer exceeded\n");
221 fprintf(stderr
, "Unknown .netrc keyword %s\n", tokval
);
227 (void) fclose(cfile
);
230 (void) fclose(cfile
);
234 static int token(void)
242 while ((c
= getc(cfile
)) != EOF
&&
243 (c
== '\r' || c
== '\n' || c
== '\t' || c
== ' ' || c
== ','))
249 while ((c
= getc(cfile
)) != EOF
&& c
!= '"') {
256 while ((c
= getc(cfile
)) != EOF
257 && c
!= '\n' && c
!= '\t' && c
!= ' ' && c
!= ',' && c
!= '\r') {
266 for (t
= toktab
; t
->tokstr
; t
++)
267 if (!strcmp(t
->tokstr
, tokval
))