OSDN Git Service

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