OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [libc/] [inet/] [rpc/] [ruserpass.c] - Blame information for rev 1325

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.