3b66c3040e6728664670bcab978dcdc9f50e69e1
[kopensolaris-gnu/glibc.git] / inet / ruserpass.c
1 /*
2  * Copyright (c) 1985, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char sccsid[] = "@(#)ruserpass.c 8.3 (Berkeley) 4/2/94";
32 #endif /* not lint */
33
34 #include <sys/types.h>
35 #include <sys/stat.h>
36
37 #include <ctype.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <netdb.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <libintl.h>
46
47 /* #include "ftp_var.h" */
48
49 static  int token (void);
50 static  FILE *cfile;
51
52 #define DEFAULT 1
53 #define LOGIN   2
54 #define PASSWD  3
55 #define ACCOUNT 4
56 #define MACDEF  5
57 #define ID      10
58 #define MACHINE 11
59
60 static char tokval[100];
61
62 static const char tokstr[] =
63 {
64 #define TOK_DEFAULT_IDX 0
65   "default\0"
66 #define TOK_LOGIN_IDX   (TOK_DEFAULT_IDX + sizeof "default")
67   "login\0"
68 #define TOK_PASSWORD_IDX (TOK_LOGIN_IDX + sizeof "login")
69   "password\0"
70 #define TOK_PASSWD_IDX  (TOK_PASSWORD_IDX + sizeof "password")
71   "passwd\0"
72 #define TOK_ACCOUNT_IDX (TOK_PASSWD_IDX + sizeof "passwd")
73   "account\0"
74 #define TOK_MACHINE_IDX (TOK_ACCOUNT_IDX + sizeof "account")
75   "machine\0"
76 #define TOK_MACDEF_IDX  (TOK_MACHINE_IDX + sizeof "machine")
77   "macdef"
78 };
79
80 static const struct toktab {
81         int tokstr_off;
82         int tval;
83 } toktab[]= {
84         { TOK_DEFAULT_IDX,      DEFAULT },
85         { TOK_LOGIN_IDX,        LOGIN },
86         { TOK_PASSWORD_IDX,     PASSWD },
87         { TOK_PASSWD_IDX,       PASSWD },
88         { TOK_ACCOUNT_IDX,      ACCOUNT },
89         { TOK_MACHINE_IDX,      MACHINE },
90         { TOK_MACDEF_IDX,       MACDEF }
91 };
92
93
94
95 int
96 ruserpass(host, aname, apass)
97         const char *host, **aname, **apass;
98 {
99         char *hdir, *buf, *tmp;
100         char myname[1024], *mydomain;
101         int t, usedefault = 0;
102         struct stat64 stb;
103
104         hdir = __secure_getenv("HOME");
105         if (hdir == NULL) {
106                 /* If we can't get HOME, fail instead of trying ".",
107                    which is no improvement. This really should call
108                    getpwuid(getuid()).  */
109                 /*hdir = ".";*/
110                 return -1;
111         }
112
113         buf = alloca (strlen (hdir) + 8);
114
115         __stpcpy (__stpcpy (buf, hdir), "/.netrc");
116         cfile = fopen(buf, "r");
117         if (cfile == NULL) {
118                 if (errno != ENOENT)
119                         warn("%s", buf);
120                 return (0);
121         }
122         if (__gethostname(myname, sizeof(myname)) < 0)
123                 myname[0] = '\0';
124         mydomain = __strchrnul(myname, '.');
125 next:
126         while ((t = token())) switch(t) {
127
128         case DEFAULT:
129                 usedefault = 1;
130                 /* FALL THROUGH */
131
132         case MACHINE:
133                 if (!usedefault) {
134                         if (token() != ID)
135                                 continue;
136                         /*
137                          * Allow match either for user's input host name
138                          * or official hostname.  Also allow match of
139                          * incompletely-specified host in local domain.
140                          */
141                         if (__strcasecmp(host, tokval) == 0)
142                                 goto match;
143 /*                      if (__strcasecmp(hostname, tokval) == 0)
144                                 goto match;
145                         if ((tmp = strchr(hostname, '.')) != NULL &&
146                             __strcasecmp(tmp, mydomain) == 0 &&
147                             __strncasecmp(hostname, tokval, tmp-hostname) == 0 &&
148                             tokval[tmp - hostname] == '\0')
149                                 goto match; */
150                         if ((tmp = strchr(host, '.')) != NULL &&
151                             __strcasecmp(tmp, mydomain) == 0 &&
152                             __strncasecmp(host, tokval, tmp - host) == 0 &&
153                             tokval[tmp - host] == '\0')
154                                 goto match;
155                         continue;
156                 }
157         match:
158                 while ((t = token()) && t != MACHINE && t != DEFAULT) switch(t) {
159
160                 case LOGIN:
161                         if (token()) {
162                                 if (*aname == 0) {
163                                   char *newp;
164                                   newp = malloc((unsigned) strlen(tokval) + 1);
165                                   if (newp == NULL)
166                                     {
167                                       warnx(_("out of memory"));
168                                       goto bad;
169                                     }
170                                   *aname = strcpy(newp, tokval);
171                                 } else {
172                                         if (strcmp(*aname, tokval))
173                                                 goto next;
174                                 }
175                         }
176                         break;
177                 case PASSWD:
178                         if (strcmp(*aname, "anonymous") &&
179                             fstat64(fileno(cfile), &stb) >= 0 &&
180                             (stb.st_mode & 077) != 0) {
181         warnx(_("Error: .netrc file is readable by others."));
182         warnx(_("Remove password or make file unreadable by others."));
183                                 goto bad;
184                         }
185                         if (token() && *apass == 0) {
186                                 char *newp;
187                                 newp = malloc((unsigned) strlen(tokval) + 1);
188                                 if (newp == NULL)
189                                   {
190                                     warnx(_("out of memory"));
191                                     goto bad;
192                                   }
193                                 *apass = strcpy(newp, tokval);
194                         }
195                         break;
196                 case ACCOUNT:
197 #if 0
198                         if (fstat64(fileno(cfile), &stb) >= 0
199                             && (stb.st_mode & 077) != 0) {
200         warnx("Error: .netrc file is readable by others.");
201         warnx("Remove account or make file unreadable by others.");
202                                 goto bad;
203                         }
204                         if (token() && *aacct == 0) {
205                                 *aacct = malloc((unsigned) strlen(tokval) + 1);
206                                 (void) strcpy(*aacct, tokval);
207                         }
208 #endif
209                         break;
210                 case MACDEF:
211 #if 0
212                         if (proxy) {
213                                 (void) fclose(cfile);
214                                 return (0);
215                         }
216                         while ((c=getc_unlocked(cfile)) != EOF && c == ' '
217                                || c == '\t');
218                         if (c == EOF || c == '\n') {
219                                 printf("Missing macdef name argument.\n");
220                                 goto bad;
221                         }
222                         if (macnum == 16) {
223                                 printf("Limit of 16 macros have already been defined\n");
224                                 goto bad;
225                         }
226                         tmp = macros[macnum].mac_name;
227                         *tmp++ = c;
228                         for (i=0; i < 8 && (c=getc_unlocked(cfile)) != EOF &&
229                             !isspace(c); ++i) {
230                                 *tmp++ = c;
231                         }
232                         if (c == EOF) {
233                                 printf("Macro definition missing null line terminator.\n");
234                                 goto bad;
235                         }
236                         *tmp = '\0';
237                         if (c != '\n') {
238                                 while ((c=getc_unlocked(cfile)) != EOF
239                                        && c != '\n');
240                         }
241                         if (c == EOF) {
242                                 printf("Macro definition missing null line terminator.\n");
243                                 goto bad;
244                         }
245                         if (macnum == 0) {
246                                 macros[macnum].mac_start = macbuf;
247                         }
248                         else {
249                                 macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
250                         }
251                         tmp = macros[macnum].mac_start;
252                         while (tmp != macbuf + 4096) {
253                                 if ((c=getc_unlocked(cfile)) == EOF) {
254                                 printf("Macro definition missing null line terminator.\n");
255                                         goto bad;
256                                 }
257                                 *tmp = c;
258                                 if (*tmp == '\n') {
259                                         if (*(tmp-1) == '\0') {
260                                            macros[macnum++].mac_end = tmp - 1;
261                                            break;
262                                         }
263                                         *tmp = '\0';
264                                 }
265                                 tmp++;
266                         }
267                         if (tmp == macbuf + 4096) {
268                                 printf("4K macro buffer exceeded\n");
269                                 goto bad;
270                         }
271 #endif
272                         break;
273                 default:
274                         warnx(_("Unknown .netrc keyword %s"), tokval);
275                         break;
276                 }
277                 goto done;
278         }
279 done:
280         (void) fclose(cfile);
281         return (0);
282 bad:
283         (void) fclose(cfile);
284         return (-1);
285 }
286
287 static int
288 token()
289 {
290         char *cp;
291         int c;
292         int i;
293
294         if (feof_unlocked(cfile) || ferror_unlocked(cfile))
295                 return (0);
296         while ((c = getc_unlocked(cfile)) != EOF &&
297             (c == '\n' || c == '\t' || c == ' ' || c == ','))
298                 continue;
299         if (c == EOF)
300                 return (0);
301         cp = tokval;
302         if (c == '"') {
303                 while ((c = getc_unlocked(cfile)) != EOF && c != '"') {
304                         if (c == '\\')
305                                 c = getc_unlocked(cfile);
306                         *cp++ = c;
307                 }
308         } else {
309                 *cp++ = c;
310                 while ((c = getc_unlocked(cfile)) != EOF
311                     && c != '\n' && c != '\t' && c != ' ' && c != ',') {
312                         if (c == '\\')
313                                 c = getc_unlocked(cfile);
314                         *cp++ = c;
315                 }
316         }
317         *cp = 0;
318         if (tokval[0] == 0)
319                 return (0);
320         for (i = 0; i < sizeof (toktab) / sizeof (toktab[0]); ++i)
321                 if (!strcmp(&tokstr[toktab[i].tokstr_off], tokval))
322                         return toktab[i].tval;
323         return (ID);
324 }