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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
/* Ftp filesystem
Copyright (C) 1997 Free Software Foundation, Inc.
Written by Miles Bader <miles@gnu.ai.mit.edu>
This file is part of the GNU Hurd.
The GNU Hurd is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2, or (at
your option) any later version.
The GNU Hurd 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
#include <string.h>
#include <argp.h>
#include <error.h>
#include <argz.h>
#include <netdb.h>
#include <hurd/netfs.h>
#include "ftpfs.h"
static char *args_doc = "REMOTE_FS [SERVER]";
static char *doc = "Hurd ftp filesystem translator"
"\vIf SERVER is not specified, an attempt is made to extract"
" it from REMOTE_FS, using `SERVER:FS' notation."
" SERVER can be a hostname, in which case anonymous ftp is used,"
" or may include a user and password like `USER:PASSWORD@HOST' (the"
" `:PASSWORD' part is optional).";
/* The filesystem. */
struct ftpfs *ftpfs;
/* Parameters describing the server we're connecting to. */
struct ftp_conn_params *ftpfs_ftp_params = 0;
/* customization hooks. */
struct ftp_conn_hooks ftpfs_ftp_hooks = { 0 };
/* The (user-specified) name of the SERVER:FILESYSTEM we're connected too. */
char *ftpfs_remote_fs;
/* The FILESYSTEM component of FTPFS_REMOTE_FS. */
char *ftpfs_remote_root;
/* Random parameters for the filesystem. */
struct ftpfs_params ftpfs_params;
volatile struct mapped_time_value *ftpfs_maptime;
int netfs_maxsymlinks = 0;
extern error_t lookup_server (const char *server,
struct ftp_conn_params **params, int *h_err);
/* Prints ftp connection log to stderr. */
static void
cntl_debug (struct ftp_conn *conn, int type, const char *txt)
{
char *type_str;
static struct mutex debug_lock = MUTEX_INITIALIZER;
switch (type)
{
case FTP_CONN_CNTL_DEBUG_CMD: type_str = ">"; break;
case FTP_CONN_CNTL_DEBUG_REPLY: type_str = "="; break;
default: type_str = "?"; break;
}
mutex_lock (&debug_lock);
fprintf (stderr, "%u.%s%s\n", (unsigned)conn->hook, type_str, txt);
mutex_unlock (&debug_lock);
}
/* Various default parameters. */
#define DEFAULT_DIR_TIMEOUT 300
#define DEFAULT_DIRENT_TIMEOUT 300
#define DEFAULT_STAT_TIMEOUT 120
#define DEFAULT_BULK_STAT_PERIOD 10
#define DEFAULT_BULK_STAT_LIMIT 10
#define DEFAULT_NODE_CACHE_MAX 50
/* Return a string corresponding to the printed rep of DEFAULT_what */
#define ___D(what) #what
#define __D(what) ___D(what)
#define _D(what) __D(DEFAULT_ ## what)
/* Common (runtime & startup) options. */
#define OPT_NO_DEBUG 1
#define OPT_DIR_TIMEOUT 5
#define OPT_DIRENT_TIMEOUT 6
#define OPT_STAT_TIMEOUT 7
#define OPT_NODE_CACHE_MAX 8
/* Options usable both at startup and at runtime. */
static const struct argp_option common_options[] =
{
{"debug", 'D', 0, 0, "Turn on debugging output for ftp connections"},
{"no-debug", OPT_NO_DEBUG, 0, OPTION_HIDDEN },
{0,0,0,0, "Parameters:"},
{"dir-timeout", OPT_DIR_TIMEOUT, "SECS", 0,
"Amount of time directories are cached (default " _D(DIR_TIMEOUT) ")"},
{"dirent-timeout", OPT_DIRENT_TIMEOUT, "SECS", 0,
"Amount of time individual directory entries are cached (default "
_D(DIRENT_TIMEOUT) ")"},
{"stat-timeout", OPT_STAT_TIMEOUT, "SECS", 0,
"Amount of time stat information is cached (default " _D(STAT_TIMEOUT) ")"},
{"node-cache-size", OPT_NODE_CACHE_MAX, "ENTRIES", 0,
"Number of recently used filesystem nodes that are cached (default "
_D(NODE_CACHE_MAX) ")"},
{0, 0}
};
static error_t
parse_common_opt (int key, char *arg, struct argp_state *state)
{
struct ftpfs_params *params = state->input;
switch (key)
{
case 'D':
ftpfs_ftp_hooks.cntl_debug = cntl_debug; break;
case OPT_NO_DEBUG:
ftpfs_ftp_hooks.cntl_debug = 0; break;
case OPT_NODE_CACHE_MAX:
params->node_cache_max = atoi (arg); break;
case OPT_DIR_TIMEOUT:
params->dir_timeout = atoi (arg); break;
case OPT_DIRENT_TIMEOUT:
params->dirent_timeout = atoi (arg); break;
case OPT_STAT_TIMEOUT:
params->stat_timeout = atoi (arg); break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp common_argp = { common_options, parse_common_opt };
/* Startup options. */
static const struct argp_option startup_options[] =
{
{ 0 }
};
/* Parse a single command line option/argument. */
static error_t
parse_startup_opt (int key, char *arg, struct argp_state *state)
{
switch (key)
{
case ARGP_KEY_ARG:
if (state->arg_num > 1)
argp_usage (state);
else if (state->arg_num == 0)
ftpfs_remote_fs = arg;
else
/* If the fs & server are two separate args, glom them together into the
":" notation. */
{
char *rfs = malloc (strlen (ftpfs_remote_fs) + 1 + strlen (arg) + 1);
if (! rfs)
argp_failure (state, 99, ENOMEM, "%s", arg);
stpcpy (stpcpy (stpcpy (rfs, arg), ":"), ftpfs_remote_fs);
ftpfs_remote_fs = rfs;
}
break;
case ARGP_KEY_SUCCESS:
/* Validate the remote fs arg; at this point FTPFS_REMOTE_FS is in
SERVER:FS notation. */
if (state->arg_num == 0)
argp_error (state, "No remote filesystem specified");
else
{
int h_err; /* Host lookup error. */
error_t err;
char *sep = strchr (ftpfs_remote_fs, '@');
if (sep)
/* FTPFS_REMOTE_FS includes a '@', which means that it's in
USER[:PASS]@HOST:FS notation, so we have to be careful not to
choose the wrong `:' as the SERVER-FS separator. */
sep = strchr (sep, ':');
else
sep = strchr (ftpfs_remote_fs, ':');
if (! sep)
argp_error (state, "%s: No server specified", ftpfs_remote_fs);
ftpfs_remote_root = sep + 1;
/* Lookup the ftp server (the part before the `:'). */
*sep = '\0';
err = lookup_server (ftpfs_remote_fs, &ftpfs_ftp_params, &h_err);
if (err == EINVAL)
argp_failure (state, 10, 0, "%s: %s",
ftpfs_remote_fs, hstrerror (h_err));
else if (err)
argp_failure (state, 11, err, "%s", ftpfs_remote_fs);
*sep = ':';
}
case ARGP_KEY_INIT:
/* Setup up state for our first child parser (common options). */
state->child_inputs[0] = &ftpfs_params;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
/* Runtime options. */
static const struct argp_child runtime_argp_children[] =
{ {&common_argp}, {&netfs_std_runtime_argp}, {0} };
static struct argp runtime_argp =
{ 0, 0, 0, 0, runtime_argp_children };
/* Use by netfs_set_options to handle runtime option parsing. */
struct argp *netfs_runtime_argp = &runtime_argp;
/* Return an argz string describing the current options. Fill *ARGZ
with a pointer to newly malloced storage holding the list and *LEN
to the length of that storage. */
error_t
netfs_append_args (char **argz, size_t *argz_len)
{
char buf[80];
error_t err = 0;
#define FOPT(fmt, arg) \
do { \
if (! err) \
{ \
snprintf (buf, sizeof buf, fmt, arg); \
err = argz_add (argz, argz_len, buf); \
} \
} while (0)
if (ftpfs->params.dir_timeout != DEFAULT_DIR_TIMEOUT)
FOPT ("--dir-timeout=%d", ftpfs->params.dir_timeout);
if (ftpfs->params.dirent_timeout != DEFAULT_DIRENT_TIMEOUT)
FOPT ("--dirent-timeout=%d", ftpfs->params.dirent_timeout);
if (ftpfs->params.stat_timeout != DEFAULT_STAT_TIMEOUT)
FOPT ("--stat-timeout=%d", ftpfs->params.stat_timeout);
if (ftpfs->params.node_cache_max != DEFAULT_NODE_CACHE_MAX)
FOPT ("--node-cache-max=%d", ftpfs->params.node_cache_max);
return argz_add (argz, argz_len, ftpfs_remote_fs);
}
/* Program entry point. */
int
main (int argc, char **argv)
{
error_t err;
mach_port_t bootstrap;
const struct argp_child argp_children[] =
{ {&common_argp}, {&netfs_std_startup_argp}, {0} };
struct argp argp =
{ startup_options, parse_startup_opt, args_doc, doc, argp_children };
ftpfs_params.dir_timeout = DEFAULT_DIR_TIMEOUT;
ftpfs_params.dirent_timeout = DEFAULT_DIRENT_TIMEOUT;
ftpfs_params.stat_timeout = DEFAULT_STAT_TIMEOUT;
ftpfs_params.node_cache_max = DEFAULT_NODE_CACHE_MAX;
ftpfs_params.bulk_stat_period = DEFAULT_BULK_STAT_PERIOD;
ftpfs_params.bulk_stat_limit = DEFAULT_BULK_STAT_LIMIT;
argp_parse (&argp, argc, argv, 0, 0, 0);
task_get_bootstrap_port (mach_task_self (), &bootstrap);
netfs_init ();
err = maptime_map (0, 0, &ftpfs_maptime);
if (err)
error (3, err, "mapping time");
err = ftpfs_create (ftpfs_remote_root, ftpfs_ftp_params, &ftpfs_ftp_hooks,
&ftpfs_params, &ftpfs);
if (err)
error (4, err, "%s", ftpfs_remote_fs);
netfs_root_node = ftpfs->root;
netfs_startup (bootstrap, 0);
for (;;)
netfs_server_loop ();
}
|