forked from MidnightCommander/mc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutilvfs.c
295 lines (254 loc) · 10.1 KB
/
utilvfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* Utilities for VFS modules.
Currently includes login and tcp open socket routines.
Copyright (C) 1995, 1996 Miguel de Icaza
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License
as published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/* Namespace: exports vfs_split_url */
#ifndef test_get_host_and_username
#include <config.h>
#endif
#include <unistd.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <pwd.h>
#include <sys/types.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#include <errno.h>
#include "utilvfs.h"
#include "vfs.h"
/* Extract the hostname and username from the path */
/* path is in the form: [user@]hostname:port/remote-dir, e.g.:
*
* ftp://sunsite.unc.edu/pub/linux
* ftp://[email protected]/c/nc
* ftp://tsx-11.mit.edu:8192/
* ftp://[email protected]:11321/private
* ftp://joe:[email protected]
*
* Returns malloc()ed host, user and pass they are present.
* If the user is empty, e.g. ftp://@roxanne/private, and URL_ALLOW_ANON
* is not set, then the current login name is supplied.
*
* Return value is a malloc()ed string with the pathname relative to the
* host.
*/
char *vfs_split_url (const char *path, char **host, char **user,
int *port, char **pass, int default_port, int flags)
{
struct passwd *passwd_info;
char *dir, *colon, *inner_colon, *at, *rest;
char *retval;
char *pcopy = g_strdup (path);
char *pend = pcopy + strlen (pcopy);
if (pass)
*pass = NULL;
*port = default_port;
*user = NULL;
retval = NULL;
dir = pcopy;
if (!(flags & URL_NOSLASH)) {
/* locate path component */
while (*dir != PATH_SEP && *dir)
dir++;
if (*dir){
retval = g_strdup (dir);
*dir = 0;
} else
retval = g_strdup (PATH_SEP_STR);
}
/* search for any possible user */
at = strchr (pcopy, '@');
/* We have a username */
if (at){
*at = 0;
inner_colon = strchr (pcopy, ':');
if (inner_colon) {
*inner_colon = 0;
inner_colon++;
if (pass)
*pass = g_strdup (inner_colon);
}
if (*pcopy != 0)
*user = g_strdup (pcopy);
if (pend == at+1)
rest = at;
else
rest = at + 1;
} else
rest = pcopy;
if (!*user && !(flags & URL_ALLOW_ANON)) {
passwd_info = getpwuid (geteuid ());
if (passwd_info && passwd_info->pw_name)
*user = g_strdup (passwd_info->pw_name);
else {
/* This is very unlikely to happen */
*user = g_strdup ("anonymous");
}
endpwent ();
}
/* Check if the host comes with a port spec, if so, chop it */
colon = strchr (rest, ':');
if (colon){
*colon = 0;
if (sscanf(colon+1, "%d", port)==1) {
if (*port <= 0 || *port >= 65536)
*port = default_port;
} else {
while(*(++colon)){
switch(*colon) {
case 'C': *port = 1;
break;
case 'r': *port = 2;
break;
}
}
}
}
if (host)
*host = g_strdup (rest);
g_free (pcopy);
return retval;
}
#ifdef test_get_host_and_username
struct tt {
char *url;
char *r_host;
char *r_user;
char *r_pass;
char *r_dir;
int r_port;
};
struct tt tests [] = {
{ "host", "host", "anonymous", NULL, "/", 21 },
{ "host/dir", "host", "anonymous", NULL, "/dir", 21 },
{ "host:40/", "host", "anonymous", NULL, "/", 40 },
{ "host:40/dir", "host", "anonymous", NULL, "/dir", 40 },
{ "user@host", "host", "user", NULL, "/", 21 },
{ "user@host/dir", "host", "user", NULL, "/dir", 21 },
{ "user@host:40/", "host", "user", NULL, "/", 40 },
{ "user@host:40/dir", "host", "user", NULL, "/dir", 40 },
{ "user:pass@host", "host", "user", "pass", "/", 21 },
{ "user:pass@host/dir", "host", "user", "pass", "/dir", 21 },
{ "user:pass@host:40", "host", "user", "pass", "/", 40 },
{ "user:pass@host:40/dir", "host", "user", "pass", "/dir", 40 },
{ "host/a@b", "host", "anonymous", NULL, "/a@b", 21 },
{ "host:40/a@b", "host", "anonymous", NULL, "/a@b", 40 },
{ "user@host/a@b", "host", "user", NULL, "/a@b", 21 },
{ "user@host:40/a@b", "host", "user", NULL, "/a@b", 40 },
{ "user:pass@host/a@b", "host", "user", "pass", "/a@b", 21 },
{ "user:pass@host:40/a@b", "host", "user", "pass", "/a@b", 40 },
{ "host/a:b", "host", "anonymous", NULL, "/a:b", 21 },
{ "host:40/a:b", "host", "anonymous", NULL, "/a:b", 40 },
{ "user@host/a:b", "host", "user", NULL, "/a:b", 21 },
{ "user@host:40/a:b", "host", "user", NULL, "/a:b", 40 },
{ "user:pass@host/a:b", "host", "user", "pass", "/a:b", 21 },
{ "user:pass@host:40/a:b", "host", "user", "pass", "/a:b", 40 },
{ "host/a/b", "host", "anonymous", NULL, "/a/b", 21 },
{ "host:40/a/b", "host", "anonymous", NULL, "/a/b", 40 },
{ "user@host/a/b", "host", "user", NULL, "/a/b", 21 },
{ "user@host:40/a/b", "host", "user", NULL, "/a/b", 40 },
{ "user:pass@host/a/b", "host", "user", "pass", "/a/b", 21 },
{ "user:pass@host:40/a/b", "host", "user", "pass", "/a/b", 40 },
{ NULL }
};
/* tests with implicit login name */
struct tt tests2 [] = {
{ "@host", "host", "user", NULL, "/", 21 },
{ "@host/dir", "host", "user", NULL, "/dir", 21 },
{ "@host:40/", "host", "user", NULL, "/", 40 },
{ "@host:40/dir", "host", "user", NULL, "/dir", 40 },
{ ":pass@host", "host", "user", "pass", "/", 21 },
{ ":pass@host/dir", "host", "user", "pass", "/dir", 21 },
{ ":pass@host:40", "host", "user", "pass", "/", 40 },
{ ":pass@host:40/dir", "host", "user", "pass", "/dir", 40 },
{ "@host/a@b", "host", "user", NULL, "/a@b", 21 },
{ "@host:40/a@b", "host", "user", NULL, "/a@b", 40 },
{ ":pass@host/a@b", "host", "user", "pass", "/a@b", 21 },
{ ":pass@host:40/a@b", "host", "user", "pass", "/a@b", 40 },
{ "@host/a:b", "host", "user", NULL, "/a:b", 21 },
{ "@host:40/a:b", "host", "user", NULL, "/a:b", 40 },
{ ":pass@host/a:b", "host", "user", "pass", "/a:b", 21 },
{ ":pass@host:40/a:b", "host", "user", "pass", "/a:b", 40 },
{ "@host/a/b", "host", "user", NULL, "/a/b", 21 },
{ "@host:40/a/b", "host", "user", NULL, "/a/b", 40 },
{ ":pass@host/a/b", "host", "user", "pass", "/a/b", 21 },
{ ":pass@host:40/a/b", "host", "user", "pass", "/a/b", 40 },
{ NULL }
};
main ()
{
int i, port, err;
char *dir, *host, *user, *pass;
struct passwd *passwd_info;
char *current;
if ((passwd_info = getpwuid (geteuid ())) == NULL)
current = g_strdup ("anonymous");
else {
current= g_strdup (passwd_info->pw_name);
}
endpwent ();
for (i = 0; tests [i].url; i++){
err = 0;
dir = get_host_and_username (tests [i].url, &host, &user, &port, 21, 1, &pass);
if (strcmp (dir, tests [i].r_dir))
err++, printf ("dir: test %d flunked\n", i);
if (!err && strcmp (host, tests [i].r_host))
err++, printf ("host: test %d flunked\n", i);
if (!err && strcmp (user, tests [i].r_user))
err++, printf ("user: test %d flunked\n", i);
if (!err && tests [i].r_pass)
if (strcmp (dir, tests [i].r_dir))
err++, printf ("pass: test %d flunked\n", i);
if (!err & tests [i].r_port != port)
err++, printf ("port: test %d flunked\n", i);
if (err){
printf ("host=[%s] user=[%s] pass=[%s] port=[%d]\n",
host, user, pass, port);
}
}
printf ("%d tests ok\nExtra tests:", i);
for (i = 0; i < 4; i++){
dir = get_host_and_username (tests [i].url, &host, &user, &port, 21, 0, &pass);
if (strcmp (user, current))
printf ("ntest: flunked %d\n", i);
}
printf ("%d tests ok\nTests with implicit login name\n", i);
for (i = 0; tests2 [i].url; i++){
err = 0;
dir = get_host_and_username (tests2 [i].url, &host, &user, &port, 21, 1, &pass);
if (strcmp (dir, tests2 [i].r_dir))
err++, printf ("dir: test %d flunked\n", i);
if (!err && strcmp (host, tests2 [i].r_host))
err++, printf ("host: test %d flunked\n", i);
if (!err && strcmp (user, current))
err++, printf ("user: test %d flunked\n", i);
if (!err && tests2 [i].r_pass)
if (strcmp (dir, tests2 [i].r_dir))
err++, printf ("pass: test %d flunked\n", i);
if (!err & tests2 [i].r_port != port)
err++, printf ("port: test %d flunked\n", i);
if (err){
printf ("host=[%s] user=[%s] pass=[%s] port=[%d]\n",
host, user, pass, port);
}
}
printf ("%d tests ok\n", i);
}
#endif