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.
21 static char sccsid
[] = "@(#)ruserpass.c 5.1 (Berkeley) 3/1/89";
24 struct utmp
*getutmp();
27 #ifndef MAXHOSTNAMELEN
28 #define MAXHOSTNAMELEN 64
39 static char tokval
[100];
41 static struct toktab
{
55 extern char *hostname
;
56 static int token(void);
58 int ruserpass(const char *host
, char **aname
, char **apass
, char **aacct
)
60 const char *hdir
, *mydomain
;
61 char buf
[BUFSIZ
], *tmp
;
62 char myname
[MAXHOSTNAMELEN
];
63 int t
, i
, c
, usedefault
= 0;
66 hdir
= getenv("HOME");
69 (void) sprintf(buf
, "%s/.netrc", hdir
);
70 cfile
= fopen(buf
, "r");
78 if (gethostname(myname
, sizeof(myname
)) < 0)
80 if ((mydomain
= index(myname
, '.')) == NULL
)
83 while ((t
= token())) switch(t
) {
94 * Allow match either for user's input host name
95 * or official hostname. Also allow match of
96 * incompletely-specified host in local domain.
98 if (strcasecmp(host
, tokval
) == 0)
100 if (strcasecmp(hostname
, tokval
) == 0)
102 if ((tmp
= index(hostname
, '.')) != NULL
&&
103 strcasecmp(tmp
, mydomain
) == 0 &&
104 strncasecmp(hostname
, tokval
, tmp
- hostname
) == 0 &&
105 tokval
[tmp
- hostname
] == '\0')
107 if ((tmp
= index(host
, '.')) != NULL
&&
108 strcasecmp(tmp
, mydomain
) == 0 &&
109 strncasecmp(host
, tokval
, tmp
- host
) == 0 &&
110 tokval
[tmp
- host
] == '\0')
115 while ((t
= token()) && t
!= MACH
&& t
!= DEFAULT
) switch(t
) {
120 *aname
= malloc((unsigned) strlen(tokval
) + 1);
121 (void) strcpy(*aname
, tokval
);
123 if (strcmp(*aname
, tokval
))
129 if (strcmp(*aname
, "anonymous") &&
130 fstat(fileno(cfile
), &stb
) >= 0 &&
131 (stb
.st_mode
& 077) != 0) {
132 fprintf(stderr
, "Error - .netrc file not correct mode.\n");
133 fprintf(stderr
, "Remove password or correct mode.\n");
136 if (token() && *apass
== 0) {
137 *apass
= malloc((unsigned) strlen(tokval
) + 1);
138 (void) strcpy(*apass
, tokval
);
142 if (fstat(fileno(cfile
), &stb
) >= 0
143 && (stb
.st_mode
& 077) != 0) {
144 fprintf(stderr
, "Error - .netrc file not correct mode.\n");
145 fprintf(stderr
, "Remove account or correct mode.\n");
148 if (token() && *aacct
== 0) {
149 *aacct
= malloc((unsigned) strlen(tokval
) + 1);
150 (void) strcpy(*aacct
, tokval
);
155 (void) fclose(cfile
);
158 while ((c
=getc(cfile
)) != EOF
&& (c
== ' ' || c
== '\t'));
159 if (c
== EOF
|| c
== '\n') {
160 printf("Missing macdef name argument.\n");
164 printf("Limit of 16 macros have already been defined\n");
167 tmp
= macros
[macnum
].mac_name
;
169 for (i
=0; i
< 8 && (c
=getc(cfile
)) != EOF
&&
174 printf("Macro definition missing null line terminator.\n");
179 while ((c
=getc(cfile
)) != EOF
&& c
!= '\n');
182 printf("Macro definition missing null line terminator.\n");
186 macros
[macnum
].mac_start
= macbuf
;
189 macros
[macnum
].mac_start
= macros
[macnum
-1].mac_end
+ 1;
191 tmp
= macros
[macnum
].mac_start
;
192 while (tmp
!= macbuf
+ 4096) {
193 if ((c
=getc(cfile
)) == EOF
) {
194 printf("Macro definition missing null line terminator.\n");
199 if (*(tmp
-1) == '\0') {
200 macros
[macnum
++].mac_end
= tmp
- 1;
207 if (tmp
== macbuf
+ 4096) {
208 printf("4K macro buffer exceeded\n");
213 fprintf(stderr
, "Unknown .netrc keyword %s\n", tokval
);
219 (void) fclose(cfile
);
222 (void) fclose(cfile
);
226 static int token(void)
234 while ((c
= getc(cfile
)) != EOF
&&
235 (c
== '\r' || c
== '\n' || c
== '\t' || c
== ' ' || c
== ','))
241 while ((c
= getc(cfile
)) != EOF
&& c
!= '"') {
248 while ((c
= getc(cfile
)) != EOF
249 && c
!= '\n' && c
!= '\t' && c
!= ' ' && c
!= ',' && c
!= '\r') {
258 for (t
= toktab
; t
->tokstr
; t
++)
259 if (!strcmp(t
->tokstr
, tokval
))