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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [userland/] [ftp/] [ftp/] [ruserpass.c] - Blame information for rev 1771

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

Line No. Rev Author Line
1 763 simons
/*
2
 * Copyright (c) 1985 Regents of the University of California.
3
 * 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
 * 3. All advertising materials mentioning features or use of this software
14
 *    must display the following acknowledgement:
15
 *      This product includes software developed by the University of
16
 *      California, Berkeley and its contributors.
17
 * 4. Neither the name of the University nor the names of its contributors
18
 *    may be used to endorse or promote products derived from this software
19
 *    without specific prior written permission.
20
 *
21
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31
 * SUCH DAMAGE.
32
 */
33
 
34
/*
35
 * from: @(#)ruserpass.c        5.3 (Berkeley) 3/1/91
36
 */
37
char ruserpass_rcsid[] =
38
  "$Id: ruserpass.c,v 1.1 2002-03-25 22:32:01 simons Exp $";
39
 
40
#include <stdio.h>
41
#include <stdlib.h>
42
#include <utmp.h>
43
#include <ctype.h>
44
#include <sys/stat.h>
45
#include <errno.h>
46
#include <string.h>
47
#include <unistd.h>
48
#include "ftp_var.h"
49
 
50
static FILE *cfile;
51
static int token(void);
52
 
53
#define DEFAULT 1
54
#define LOGIN   2
55
#define PASSWD  3
56
#define ACCOUNT 4
57
#define MACDEF  5
58
#define ID      10
59
#define MACH    11
60
 
61
static char tokval[100];
62
 
63
static struct toktab {
64
        const char *tokstr;
65
        int tval;
66
} toktab[]= {
67
        { "default",    DEFAULT },
68
        { "login",      LOGIN },
69
        { "password",   PASSWD },
70
        { "passwd",     PASSWD },
71
        { "account",    ACCOUNT },
72
        { "machine",    MACH },
73
        { "macdef",     MACDEF },
74
        { NULL,         0 }
75
};
76
 
77
int
78
xruserpass(const char *host, char **aname, char **apass, char **aacct)
79
{
80
        const char *hdir;
81
        char buf[BUFSIZ], *tmp;
82
        char myname[MAXHOSTNAMELEN];
83
        const char *mydomain;
84
        int t, i, c, usedefault = 0;
85
        struct stat stb;
86
 
87
        hdir = getenv("HOME");
88
        if (hdir == NULL)
89
                hdir = ".";
90
        snprintf(buf, sizeof(buf), "%s/.netrc", hdir);
91
        cfile = fopen(buf, "r");
92
        if (cfile == NULL) {
93
                if (errno != ENOENT)
94
                        perror(buf);
95
                return(0);
96
        }
97
        if (gethostname(myname, sizeof(myname)) < 0)
98
                myname[0] = '\0';
99
        if ((mydomain = strchr(myname, '.')) == NULL)
100
                mydomain = "";
101
next:
102
        while ((t = token())) switch(t) {
103
 
104
        case DEFAULT:
105
                usedefault = 1;
106
                /* FALL THROUGH */
107
 
108
        case MACH:
109
                if (!usedefault) {
110
                        if (token() != ID)
111
                                continue;
112
                        /*
113
                         * Allow match either for user's input host name
114
                         * or official hostname.  Also allow match of
115
                         * incompletely-specified host in local domain.
116
                         */
117
                        if (strcasecmp(host, tokval) == 0)
118
                                goto match;
119
                        if (strcasecmp(hostname, tokval) == 0)
120
                                goto match;
121
                        if ((tmp = index(hostname, '.')) != NULL &&
122
                            strcasecmp(tmp, mydomain) == 0 &&
123
                            strncasecmp(hostname, tokval, tmp-hostname) == 0 &&
124
                            tokval[tmp - hostname] == '\0')
125
                                goto match;
126
                        if ((tmp = index(host, '.')) != NULL &&
127
                            strcasecmp(tmp, mydomain) == 0 &&
128
                            strncasecmp(host, tokval, tmp - host) == 0 &&
129
                            tokval[tmp - host] == '\0')
130
                                goto match;
131
                        continue;
132
                }
133
        match:
134
                while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
135
 
136
                case LOGIN:
137
                        if (token()) {
138
                                if (*aname == 0) {
139
                                        *aname = malloc((unsigned) strlen(tokval) + 1);
140
                                        (void) strcpy(*aname, tokval);
141
                                } else {
142
                                        if (strcmp(*aname, tokval))
143
                                                goto next;
144
                                }
145
                        }
146
                        break;
147
                case PASSWD:
148
                        if (*aname==NULL) {
149
        fprintf(stderr, "Error: `password' must follow `login' in .netrc\n");
150
                                goto bad;
151
                        }
152
                        if (strcmp(*aname, "anonymous") &&
153
                            fstat(fileno(cfile), &stb) >= 0 &&
154
                            (stb.st_mode & 077) != 0) {
155
        fprintf(stderr, "Error - .netrc file not correct permissions.\n");
156
        fprintf(stderr, "Remove password or correct mode (should be 600).\n");
157
                                goto bad;
158
                        }
159
                        if (token() && *apass == 0) {
160
                                *apass = malloc((unsigned) strlen(tokval) + 1);
161
                                (void) strcpy(*apass, tokval);
162
                        }
163
                        break;
164
                case ACCOUNT:
165
                        if (fstat(fileno(cfile), &stb) >= 0
166
                            && (stb.st_mode & 077) != 0) {
167
        fprintf(stderr, "Error - .netrc file not correct permissions.\n");
168
        fprintf(stderr, "Remove account or correct mode (should be 600).\n");
169
                                goto bad;
170
                        }
171
                        if (token() && *aacct == 0) {
172
                                *aacct = malloc((unsigned) strlen(tokval) + 1);
173
                                (void) strcpy(*aacct, tokval);
174
                        }
175
                        break;
176
                case MACDEF:
177
                        if (proxy) {
178
                                (void) fclose(cfile);
179
                                return(0);
180
                        }
181
                        while ((c=getc(cfile)) != EOF && (c == ' ' || c == '\t'));
182
                        if (c == EOF || c == '\n') {
183
                                printf("Missing macdef name argument.\n");
184
                                goto bad;
185
                        }
186
                        if (macnum == 16) {
187
                                printf("Limit of 16 macros have already been defined\n");
188
                                goto bad;
189
                        }
190
                        tmp = macros[macnum].mac_name;
191
                        *tmp++ = c;
192
                        for (i=0; i < 8 && (c=getc(cfile)) != EOF &&
193
                            !isspace(c); ++i) {
194
                                *tmp++ = c;
195
                        }
196
                        if (c == EOF) {
197
                                printf("Macro definition missing null line terminator.\n");
198
                                goto bad;
199
                        }
200
                        *tmp = '\0';
201
                        if (c != '\n') {
202
                                while ((c=getc(cfile)) != EOF && c != '\n');
203
                        }
204
                        if (c == EOF) {
205
                                printf("Macro definition missing null line terminator.\n");
206
                                goto bad;
207
                        }
208
                        if (macnum == 0) {
209
                                macros[macnum].mac_start = macbuf;
210
                        }
211
                        else {
212
                                macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
213
                        }
214
                        tmp = macros[macnum].mac_start;
215
                        while (tmp != macbuf + MACBUF_SIZE) {
216
                                if ((c=getc(cfile)) == EOF) {
217
                                printf("Macro definition missing null line terminator.\n");
218
                                        goto bad;
219
                                }
220
                                *tmp = c;
221
                                if (*tmp == '\n') {
222
                                        if (*(tmp-1) == '\0') {
223
                                           macros[macnum++].mac_end = tmp - 1;
224
                                           break;
225
                                        }
226
                                        *tmp = '\0';
227
                                }
228
                                tmp++;
229
                        }
230
                        if (tmp == macbuf + MACBUF_SIZE) {
231
                                printf("4K macro buffer exceeded\n");
232
                                goto bad;
233
                        }
234
                        break;
235
                default:
236
        fprintf(stderr, "Unknown .netrc keyword %s\n", tokval);
237
                        break;
238
                }
239
                goto done;
240
        }
241
done:
242
        (void) fclose(cfile);
243
        return(0);
244
bad:
245
        (void) fclose(cfile);
246
        return(-1);
247
}
248
 
249
static
250
int
251
token(void)
252
{
253
        char *cp;
254
        int c;
255
        struct toktab *t;
256
 
257
        if (feof(cfile))
258
                return (0);
259
        while ((c = getc(cfile)) != EOF &&
260
            (c == '\n' || c == '\t' || c == ' ' || c == ','))
261
                continue;
262
        if (c == EOF)
263
                return (0);
264
        cp = tokval;
265
        if (c == '"') {
266
                while ((c = getc(cfile)) != EOF && c != '"') {
267
                        if (c == '\\')
268
                                c = getc(cfile);
269
                        *cp++ = c;
270
                }
271
        } else {
272
                *cp++ = c;
273
                while ((c = getc(cfile)) != EOF
274
                    && c != '\n' && c != '\t' && c != ' ' && c != ',') {
275
                        if (c == '\\')
276
                                c = getc(cfile);
277
                        *cp++ = c;
278
                }
279
        }
280
        *cp = 0;
281
        if (tokval[0] == 0)
282
                return (0);
283
        for (t = toktab; t->tokstr; t++)
284
                if (!strcmp(t->tokstr, tokval))
285
                        return (t->tval);
286
        return (ID);
287
}

powered by: WebSVN 2.1.0

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