OSDN Git Service

7a839c3a8b4925b560641e06c26c042095a5e128
[uclinux-h8/uClibc.git] / libc / inet / rpc / 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 #define __FORCE_GLIBC
31 #include <features.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34
35 #include <ctype.h>
36 #include <err.h>
37 #include <errno.h>
38 #include <netdb.h>
39 #include <stdio.h>
40 #include <stdio_ext.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <malloc.h>
44 #include <strings.h>
45 #include <unistd.h>
46
47 /* Experimentally off - libc_hidden_proto(strcat) */
48 /* Experimentally off - libc_hidden_proto(strchr) */
49 /* Experimentally off - libc_hidden_proto(strcmp) */
50 /* Experimentally off - libc_hidden_proto(strcpy) */
51 /* Experimentally off - libc_hidden_proto(strlen) */
52 /* Experimentally off - libc_hidden_proto(strcasecmp) */
53 /* Experimentally off - libc_hidden_proto(strncasecmp) */
54 libc_hidden_proto(getenv)
55 libc_hidden_proto(printf)
56 libc_hidden_proto(fstat)
57 libc_hidden_proto(__fsetlocking)
58 libc_hidden_proto(getgid)
59 libc_hidden_proto(getuid)
60 libc_hidden_proto(getegid)
61 libc_hidden_proto(geteuid)
62 libc_hidden_proto(gethostname)
63 libc_hidden_proto(fileno)
64 libc_hidden_proto(fopen)
65 libc_hidden_proto(fclose)
66 libc_hidden_proto(getc_unlocked)
67 libc_hidden_proto(__fgetc_unlocked)
68 libc_hidden_proto(__uc_malloc)
69
70 #define _(X)  (X)
71 /* #include "ftp_var.h" */
72
73 static  int token (void);
74 static  FILE *cfile;
75
76 #define DEFAULT 1
77 #define LOGIN   2
78 #define PASSWD  3
79 #define ACCOUNT 4
80 #define MACDEF  5
81 #define ID      10
82 #define MACHINE 11
83
84 static char *tokval; /* [100] */
85
86 static const char tokstr[] =
87 {
88 #define TOK_DEFAULT_IDX 0
89   "default\0"
90 #define TOK_LOGIN_IDX   (TOK_DEFAULT_IDX + sizeof "default")
91   "login\0"
92 #define TOK_PASSWORD_IDX (TOK_LOGIN_IDX + sizeof "login")
93   "password\0"
94 #define TOK_PASSWD_IDX  (TOK_PASSWORD_IDX + sizeof "password")
95   "passwd\0"
96 #define TOK_ACCOUNT_IDX (TOK_PASSWD_IDX + sizeof "passwd")
97   "account\0"
98 #define TOK_MACHINE_IDX (TOK_ACCOUNT_IDX + sizeof "account")
99   "machine\0"
100 #define TOK_MACDEF_IDX  (TOK_MACHINE_IDX + sizeof "machine")
101   "macdef"
102 };
103
104 static const struct toktab {
105         int tokstr_off;
106         int tval;
107 } toktab[]= {
108         { TOK_DEFAULT_IDX,      DEFAULT },
109         { TOK_LOGIN_IDX,        LOGIN },
110         { TOK_PASSWORD_IDX,     PASSWD },
111         { TOK_PASSWD_IDX,       PASSWD },
112         { TOK_ACCOUNT_IDX,      ACCOUNT },
113         { TOK_MACHINE_IDX,      MACHINE },
114         { TOK_MACDEF_IDX,       MACDEF }
115 };
116
117
118 /* ruserpass - remote password check.
119    This function also exists in glibc but is undocumented */
120 libc_hidden_proto(ruserpass)
121 int ruserpass(const char *host, const char **aname, const char **apass)
122 {
123         char *hdir, *buf, *tmp;
124         char myname[1024], *mydomain;
125         int t, usedefault = 0;
126         struct stat stb;
127
128         /* Give up when running a setuid or setgid app. */
129         if ((getuid() != geteuid()) || getgid() != getegid())
130             return -1;
131         hdir = getenv("HOME");
132         if (hdir == NULL) {
133                 /* If we can't get HOME, fail instead of trying ".",
134                    which is no improvement. */
135                 return -1;
136         }
137
138         buf = alloca (strlen(hdir) + 8);
139         strcpy(buf, hdir);
140         strcat(buf, "/.netrc");
141         cfile = fopen(buf, "r");
142         if (cfile == NULL) {
143                 if (errno != ENOENT)
144                         printf("%s", buf);
145                 return (0);
146         }
147         /* No threads use this stream.  */
148 #ifdef __UCLIBC_HAS_THREADS__
149         __fsetlocking (cfile, FSETLOCKING_BYCALLER);
150 #endif
151         if (gethostname(myname, sizeof(myname)) < 0)
152                 myname[0] = '\0';
153         mydomain = strchr(myname, '.');
154         if (mydomain==NULL) {
155             mydomain=myname + strlen(myname);
156         }
157
158         if (!tokval)
159                 tokval = __uc_malloc(100);
160 next:
161         while ((t = token())) switch(t) {
162
163         case DEFAULT:
164                 usedefault = 1;
165                 /* FALL THROUGH */
166
167         case MACHINE:
168                 if (!usedefault) {
169                         if (token() != ID)
170                                 continue;
171                         /*
172                          * Allow match either for user's input host name
173                          * or official hostname.  Also allow match of
174                          * incompletely-specified host in local domain.
175                          */
176                         if (strcasecmp(host, tokval) == 0)
177                                 goto match;
178                         if ((tmp = strchr(host, '.')) != NULL &&
179                             strcasecmp(tmp, mydomain) == 0 &&
180                             strncasecmp(host, tokval, tmp - host) == 0 &&
181                             tokval[tmp - host] == '\0')
182                                 goto match;
183                         continue;
184                 }
185         match:
186                 while ((t = token()) && t != MACHINE && t != DEFAULT) switch(t) {
187
188                 case LOGIN:
189                         if (token()) {
190                                 if (*aname == 0) {
191                                   char *newp;
192                                   newp = malloc((unsigned) strlen(tokval) + 1);
193                                   if (newp == NULL)
194                                     {
195                                       printf(_("out of memory"));
196                                       goto bad;
197                                     }
198                                   *aname = strcpy(newp, tokval);
199                                 } else {
200                                         if (strcmp(*aname, tokval))
201                                                 goto next;
202                                 }
203                         }
204                         break;
205                 case PASSWD:
206                         if (strcmp(*aname, "anonymous") &&
207                             fstat(fileno(cfile), &stb) >= 0 &&
208                             (stb.st_mode & 077) != 0) {
209         printf(_("Error: .netrc file is readable by others."));
210         printf(_("Remove password or make file unreadable by others."));
211                                 goto bad;
212                         }
213                         if (token() && *apass == 0) {
214                                 char *newp;
215                                 newp = malloc((unsigned) strlen(tokval) + 1);
216                                 if (newp == NULL)
217                                   {
218                                     printf(_("out of memory"));
219                                     goto bad;
220                                   }
221                                 *apass = strcpy(newp, tokval);
222                         }
223                         break;
224                 case ACCOUNT:
225 #if 0
226                         if (fstat(fileno(cfile), &stb) >= 0
227                             && (stb.st_mode & 077) != 0) {
228         printf("Error: .netrc file is readable by others.");
229         printf("Remove account or make file unreadable by others.");
230                                 goto bad;
231                         }
232                         if (token() && *aacct == 0) {
233                                 *aacct = malloc((unsigned) strlen(tokval) + 1);
234                                 (void) strcpy(*aacct, tokval);
235                         }
236 #endif
237                         break;
238                 case MACDEF:
239 #if 0
240                         if (proxy) {
241                                 (void) fclose(cfile);
242                                 return (0);
243                         }
244                         while ((c=getc_unlocked(cfile)) != EOF && c == ' '
245                                || c == '\t');
246                         if (c == EOF || c == '\n') {
247                                 printf("Missing macdef name argument.\n");
248                                 goto bad;
249                         }
250                         if (macnum == 16) {
251                                 printf("Limit of 16 macros have already been defined\n");
252                                 goto bad;
253                         }
254                         tmp = macros[macnum].mac_name;
255                         *tmp++ = c;
256                         for (i=0; i < 8 && (c=getc_unlocked(cfile)) != EOF &&
257                             !isspace(c); ++i) {
258                                 *tmp++ = c;
259                         }
260                         if (c == EOF) {
261                                 printf("Macro definition missing null line terminator.\n");
262                                 goto bad;
263                         }
264                         *tmp = '\0';
265                         if (c != '\n') {
266                                 while ((c=getc_unlocked(cfile)) != EOF
267                                        && c != '\n');
268                         }
269                         if (c == EOF) {
270                                 printf("Macro definition missing null line terminator.\n");
271                                 goto bad;
272                         }
273                         if (macnum == 0) {
274                                 macros[macnum].mac_start = macbuf;
275                         }
276                         else {
277                                 macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
278                         }
279                         tmp = macros[macnum].mac_start;
280                         while (tmp != macbuf + 4096) {
281                                 if ((c=getc_unlocked(cfile)) == EOF) {
282                                 printf("Macro definition missing null line terminator.\n");
283                                         goto bad;
284                                 }
285                                 *tmp = c;
286                                 if (*tmp == '\n') {
287                                         if (*(tmp-1) == '\0') {
288                                            macros[macnum++].mac_end = tmp - 1;
289                                            break;
290                                         }
291                                         *tmp = '\0';
292                                 }
293                                 tmp++;
294                         }
295                         if (tmp == macbuf + 4096) {
296                                 printf("4K macro buffer exceeded\n");
297                                 goto bad;
298                         }
299 #endif
300                         break;
301                 default:
302                         printf(_("Unknown .netrc keyword %s"), tokval);
303                         break;
304                 }
305                 goto done;
306         }
307 done:
308         (void) fclose(cfile);
309         return (0);
310 bad:
311         (void) fclose(cfile);
312         return (-1);
313 }
314 libc_hidden_def(ruserpass)
315
316 static int
317 token()
318 {
319         char *cp;
320         int c;
321         int i;
322
323         if (feof_unlocked(cfile) || ferror_unlocked(cfile))
324                 return (0);
325         while ((c = getc_unlocked(cfile)) != EOF &&
326             (c == '\n' || c == '\t' || c == ' ' || c == ','))
327                 continue;
328         if (c == EOF)
329                 return (0);
330         cp = tokval;
331         if (c == '"') {
332                 while ((c = getc_unlocked(cfile)) != EOF && c != '"') {
333                         if (c == '\\')
334                                 c = getc_unlocked(cfile);
335                         *cp++ = c;
336                 }
337         } else {
338                 *cp++ = c;
339                 while ((c = getc_unlocked(cfile)) != EOF
340                     && c != '\n' && c != '\t' && c != ' ' && c != ',') {
341                         if (c == '\\')
342                                 c = getc_unlocked(cfile);
343                         *cp++ = c;
344                 }
345         }
346         *cp = 0;
347         if (tokval[0] == 0)
348                 return (0);
349         for (i = 0; i < (int) (sizeof (toktab) / sizeof (toktab[0])); ++i)
350                 if (!strcmp(&tokstr[toktab[i].tokstr_off], tokval))
351                         return toktab[i].tval;
352         return (ID);
353 }