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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
|
/* Unix-specific ftpconn hooks
Copyright (C) 1997 Free Software Foundation, Inc.
Written by Miles Bader <miles@gnu.ai.mit.edu>
This program 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.
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
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <errno.h>
#include <pwd.h>
#include <grp.h>
#include <sys/time.h>
#include <netinet/in.h>
#ifdef HAVE_HURD_HURD_TYPES_H
#include <hurd/hurd_types.h>
#endif
#include <ftpconn.h>
/* Uid/gid to use when we don't know about a particular user name. */
#define DEFAULT_UID 65535
#define DEFAULT_GID 65535
struct ftp_conn_syshooks ftp_conn_unix_syshooks = {
ftp_conn_unix_pasv_addr, ftp_conn_unix_interp_err,
ftp_conn_unix_start_get_stats, ftp_conn_unix_cont_get_stats,
ftp_conn_unix_append_name, ftp_conn_unix_basename
};
/* Try to get an internet address out of the reply to a PASV command.
Unfortunately, the format of the reply such isn't standardized. */
error_t
ftp_conn_unix_pasv_addr (struct ftp_conn *conn, const char *txt,
struct sockaddr **addr)
{
unsigned a0, a1, a2, a3; /* Parts of the inet address */
unsigned p0, p1; /* Parts of the prot (msb, lsb) */
if (sscanf (txt, "%*[^0-9]%d,%d,%d,%d,%d,%d", &a0,&a1,&a2,&a3, &p0,&p1) != 6)
return EGRATUITOUS;
else
{
unsigned char *a, *p;
*addr = malloc (sizeof (struct sockaddr_in));
if (! *addr)
return ENOMEM;
(*addr)->sa_len = sizeof (struct sockaddr_in);
(*addr)->sa_family = AF_INET;
a = (unsigned char *)&((struct sockaddr_in *)*addr)->sin_addr.s_addr;
a[0] = a0 & 0xff;
a[1] = a1 & 0xff;
a[2] = a2 & 0xff;
a[3] = a3 & 0xff;
p = (unsigned char *)&((struct sockaddr_in *)*addr)->sin_port;
p[0] = p0 & 0xff;
p[1] = p1 & 0xff;
return 0;
}
}
/* Compare strings P & Q in a most forgiving manner, ignoring case and
everything but alphanumeric characters. */
static int
strlaxcmp (const char *p, const char *q)
{
for (;;)
{
int ch1, ch2;
while (*p && !isalnum (*p))
p++;
while (*q && !isalnum (*q))
q++;
if (!*p || !*q)
break;
ch1 = tolower (*p);
ch2 = tolower (*q);
if (ch1 != ch2)
break;
p++;
q++;
}
return *p - *q;
}
/* Try to convert an error message in TXT into an error code. POSS_ERRS
contains a list of likely errors to try; if no other clue is found, the
first thing in poss_errs is returned. */
error_t
ftp_conn_unix_interp_err (struct ftp_conn *conn, const char *txt,
const error_t *poss_errs)
{
const char *p;
const error_t *e;
if (!poss_errs || !poss_errs[0])
return EIO;
/* ignore everything before the last colon. */
p = strrchr (txt, ':');
if (p)
p++;
else
p = txt;
/* Now, for each possible error, do a string compare ignoring case and
anything non-alphanumberic. */
for (e = poss_errs; *e; e++)
if (strlaxcmp (p, strerror (*e)) == 0)
return *e;
return poss_errs[0];
}
struct get_stats_state
{
char *name; /* Last read (maybe partial) name. */
size_t name_len; /* Valid length of NAME, *not including* '\0'. */
size_t name_alloced; /* Allocated size of NAME (>= NAME_LEN). */
int name_partial; /* True if NAME isn't complete. */
int contents; /* Are we looking for directory contents? */
int added_slash; /* Did we prefix the name with `./'? */
struct stat stat; /* Last read stat info. */
int start; /* True if at beginning of output. */
size_t buf_len; /* Length of contents in BUF. */
char buf[7000];
};
/* Start an operation to get a list of file-stat structures for NAME (this is
often similar to ftp_conn_start_dir, but with OS-specific flags), and
return a file-descriptor for reading on, and a state structure in STATE
suitable for passing to cont_get_stats. If CONTENTS is true, NAME must
refer to a directory, and the contents will be returned, otherwise, the
(single) result will refer to NAME. */
error_t
ftp_conn_unix_start_get_stats (struct ftp_conn *conn,
const char *name, int contents,
int *fd, void **state)
{
error_t err;
size_t req_len;
char *req;
struct get_stats_state *s = malloc (sizeof (struct get_stats_state));
const char *flags = contents ? "-A" : "-Ad";
const char *slash = strchr (name, '/');
if (! s)
return ENOMEM;
if (strcspn (name, "*? \t\n{}$`\\\"'") < strlen (name))
/* NAME contains some metacharacters, which makes the behavior of various
ftp servers unpredictable, so punt. */
{
free (s);
return EINVAL;
}
/* We pack the ls options and the name into the list argument, in REQ,
which will do the right thing on most unix ftp servers. */
/* Space needed for REQ. */
req_len = strlen (flags) + 1 + strlen (name) + 1;
/* If NAME doesn't contain a slash, we prepend `./' to it so that we can
tell from the results whether it's a directory or not. */
if (! slash)
req_len += 2;
req = malloc (req_len);
if (! req)
return ENOMEM;
snprintf (req, req_len, "%s %s%s", flags, slash ? "" : "./", name);
/* Make the actual request. */
err = ftp_conn_start_dir (conn, req, fd);
free (req);
if (err)
free (s);
else
{
s->contents = contents;
s->added_slash = !slash;
s->name = 0;
s->name_len = s->name_alloced = 0;
s->name_partial = 0;
s->buf_len = 0;
s->start = 1;
*state = s;
}
return err;
}
static char *months[] =
{
"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct",
"nov", "dec", 0
};
/* Translate the information in the ls output in *LINE as best we can into
STAT, and update *LINE to point to the filename at the end of the line.
If *LINE should be ignored, EAGAIN is returned. */
static error_t
parse_dir_entry (char **line, struct stat *stat)
{
char **m;
struct tm tm;
char *p = *line, *e;
/*
drwxrwxrwt 3 root wheel 1024 May 1 16:58 /tmp
drwxrwxrwt 5 root daemon 4096 May 1 17:15 /tmp
drwxrwxrwt 4 root 0 1024 May 1 14:34 /tmp
drwxrwxrwt 6 root wheel 284 May 1 12:46 /tmp
drwxrwxrwt 4 sys sys 482 May 1 17:11 /tmp
drwxrwxrwt 7 34 archive 512 May 1 14:28 /tmp
*/
if (strncasecmp (p, "total ", 6) == 0)
return EAGAIN;
bzero (stat, sizeof *stat);
#ifdef FSTYPE_FTP
stat->st_fstype = FSTYPE_FTP;
#endif
/* File format (S_IFMT) bits. */
switch (*p++)
{
case '-': stat->st_mode |= S_IFREG; break;
case 'd': stat->st_mode |= S_IFDIR; break;
case 'c': stat->st_mode |= S_IFCHR; break;
case 'b': stat->st_mode |= S_IFBLK; break;
case 'l': stat->st_mode |= S_IFLNK; break;
case 's': stat->st_mode |= S_IFSOCK; break;
case 'p': stat->st_mode |= S_IFIFO; break;
default: return EGRATUITOUS;
}
/* User perm bits. */
switch (*p++)
{
case '-': break;
case 'r': stat->st_mode |= S_IRUSR; break;
default: return EGRATUITOUS;
}
switch (*p++)
{
case '-': break;
case 'w': stat->st_mode |= S_IWUSR; break;
default: return EGRATUITOUS;
}
switch (*p++)
{
case '-': break;
case 'x': stat->st_mode |= S_IXUSR; break;
case 's': stat->st_mode |= S_IXUSR | S_ISUID; break;
case 'S': stat->st_mode |= S_ISUID; break;
default: return EGRATUITOUS;
}
/* Group perm bits. */
switch (*p++)
{
case '-': break;
case 'r': stat->st_mode |= S_IRGRP; break;
default: return EGRATUITOUS;
}
switch (*p++)
{
case '-': break;
case 'w': stat->st_mode |= S_IWGRP; break;
default: return EGRATUITOUS;
}
switch (*p++)
{
case '-': break;
case 'x': stat->st_mode |= S_IXGRP; break;
case 's': stat->st_mode |= S_IXGRP | S_ISGID; break;
case 'S': stat->st_mode |= S_ISGID; break;
default: return EGRATUITOUS;
}
/* `Other' perm bits. */
switch (*p++)
{
case '-': break;
case 'r': stat->st_mode |= S_IROTH; break;
default: return EGRATUITOUS;
}
switch (*p++)
{
case '-': break;
case 'w': stat->st_mode |= S_IWOTH; break;
default: return EGRATUITOUS;
}
switch (*p++)
{
case '-': break;
case 'x': stat->st_mode |= S_IXOTH; break;
case 't': stat->st_mode |= S_IXOTH | S_ISVTX; break;
case 'T': stat->st_mode |= S_ISVTX; break;
default: return EGRATUITOUS;
}
#define SKIP_WS() \
while (isspace (*p)) p++;
#define PARSE_INT() ({ \
unsigned u = strtoul (p, &e, 10); \
if (e == p || isalnum (*e)) \
return EGRATUITOUS; \
p = e; \
u; \
})
/* Link count. */
SKIP_WS ();
stat->st_nlink = PARSE_INT ();
/* File owner. */
SKIP_WS ();
if (isdigit (*p))
stat->st_uid = PARSE_INT ();
else
{
struct passwd *pw;
e = p + strcspn (p, " \t\n");
*e++ = '\0';
pw = getpwnam (p);
if (pw)
stat->st_uid = pw->pw_uid;
else
stat->st_uid = DEFAULT_UID;
p = e;
}
#ifdef HAVE_STAT_ST_AUTHOR
stat->st_author = stat->st_uid;
#endif
/* File group. */
SKIP_WS ();
if (isdigit (*p))
stat->st_gid = PARSE_INT ();
else
{
struct group *gr;
e = p + strcspn (p, " \t\n");
*e++ = '\0';
gr = getgrnam (p);
if (gr)
stat->st_gid = gr->gr_gid;
else
stat->st_gid = DEFAULT_GID;
p = e;
}
/* File size / device numbers. */
SKIP_WS ();
if (S_ISCHR (stat->st_mode) || S_ISBLK (stat->st_mode))
/* Block and character devices show the block params instead of the file
size. */
{
stat->st_dev = PARSE_INT ();
if (*p != ',')
return EGRATUITOUS;
stat->st_dev = (stat->st_dev << 8) | PARSE_INT ();
stat->st_size = 0;
}
else
/* File size. */
stat->st_size = PARSE_INT ();
stat->st_blocks = stat->st_size >> 9;
/* Date. Ick. */
/* Formats: MONTH DAY HH:MM and MONTH DAY YEAR */
bzero (&tm, sizeof tm);
SKIP_WS ();
e = p + strcspn (p, " \t\n");
for (m = months; *m; m++)
if (strncasecmp (*m, p, e - p) == 0)
{
tm.tm_mon = m - months;
break;
}
if (! *m)
return EGRATUITOUS;
p = e;
SKIP_WS ();
tm.tm_mday = PARSE_INT ();
SKIP_WS ();
if (p[1] == ':' || p[2] == ':')
{
struct tm *now_tm;
struct timeval now_tv;
tm.tm_hour = PARSE_INT ();
p++;
tm.tm_min = PARSE_INT ();
if (gettimeofday (&now_tv, 0) != 0)
return errno;
now_tm = localtime (&now_tv.tv_sec);
if (now_tm->tm_mon < tm.tm_mon)
tm.tm_year = now_tm->tm_year - 1;
else
tm.tm_year = now_tm->tm_year;
}
else
tm.tm_year = PARSE_INT () - 1900;
stat->st_mtime = mktime (&tm);
if (stat->st_mtime == (time_t)-1)
return EGRATUITOUS;
/* atime and ctime are the same as mtime. */
stat->st_atime = stat->st_ctime = stat->st_mtime;
/* Update *LINE to point to the filename. */
SKIP_WS ();
*line = p;
return 0;
}
/* Read stats information from FD, calling ADD_STAT for each new stat (HOOK
is passed to ADD_STAT). FD and STATE should be returned from
start_get_stats. If this function returns EAGAIN, then it should be
called again to finish the job (possibly after calling select on FD); if
it returns 0, then it is finishe,d and FD and STATE are deallocated. */
error_t
ftp_conn_unix_cont_get_stats (struct ftp_conn *conn, int fd, void *state,
ftp_conn_add_stat_fun_t add_stat, void *hook)
{
char *p, *nl;
ssize_t rd;
size_t name_len;
error_t err = 0;
struct get_stats_state *s = state;
int (*icheck) (struct ftp_conn *conn) = conn->hooks->interrupt_check;
/* We always consume full lines, so we know that we have to read more when
we first get called. */
rd = read (fd, s->buf + s->buf_len, sizeof (s->buf) - s->buf_len);
if (rd < 0)
{
err = errno;
goto finished;
}
if (icheck && (*icheck) (conn))
{
err = EINTR;
goto finished;
}
if (rd == 0)
/* EOF */
if (s->buf_len == 0)
/* We're done! Clean up and return the result in STATS. */
{
if (s->start)
/* No output at all. From many ftp servers, this means that the
specified file wasn't found. */
err = ENOENT;
goto finished;
}
else
/* Partial line at end of file? */
nl = s->buf + s->buf_len;
else
/* Look for a new line in what we read (we know that there weren't any in
the buffer before that). */
{
nl = memchr (s->buf + s->buf_len, '\n', rd);
s->buf_len += rd;
}
s->start = 0; /* We've read past the start. */
if (!nl && s->buf_len < sizeof (s->buf))
/* We didn't find any newlines (which implies we didn't hit EOF), and we
still have room to grow the buffer, so just wait until next time to do
anything. */
return EAGAIN;
/* Where we start parsing. */
p = s->buf;
do
{
if (! s->name_partial)
/* We aren't continuing to read an overflowed name from the previous
call, so we know that we are at the start of a line, and can parse
the info here as a directory entry. */
{
/* Parse the directory entry info, updating P to point to the
beginning of the name. */
err = parse_dir_entry (&p, &s->stat);
if (err == EAGAIN)
/* This line isn't a real entry and should be ignored. */
goto skip_line;
if (err)
goto finished;
}
/* Now fill in S->last_stat's name field, possibly extending it from a
previous buffer. */
name_len = (nl ? nl - p : s->buf + s->buf_len - p);
if (name_len > 0 && p[name_len - 1] == '\r')
name_len--;
if (name_len > 0)
/* Extending s->name. */
{
size_t old_len = s->name_len;
size_t total_len = old_len + name_len + 1;
if (total_len > s->name_alloced)
{
char *new_name = realloc (s->name, total_len);
if (! new_name)
goto enomem;
s->name = new_name;
s->name_alloced = total_len;
}
strncpy (s->name + old_len, p, name_len);
s->name[old_len + name_len] = '\0';
s->name_len = total_len - 1;
}
if (nl)
{
char *name = s->name;
char *symlink_target = 0;
if (S_ISLNK (s->stat.st_mode))
/* A symlink, see if we can find the link target. */
{
symlink_target = strstr (name, " -> ");
if (symlink_target)
{
*symlink_target = '\0';
symlink_target += 4;
}
}
if (strchr (name, '/'))
if (s->contents)
/* We know that the name originally request had a slash in it
(because we added one if necessary), so if a name in the
listing has one too, it can't be the contents of a
directory; if this is the case and we wanted the contents,
this must not be a directory. */
{
err = ENOTDIR;
goto finished;
}
else if (s->added_slash)
/* S->name must be the same name we passed; if we added a `./'
prefix, removed it so the client gets back what it passed. */
name += 2;
/* Pass only directory-relative names to the callback function. */
name = basename (name);
/* Call the callback function to process the current entry; it is
responsible for freeing S->name and SYMLINK_TARGET. */
err = (*add_stat) (name, &s->stat, symlink_target, hook);
if (err)
goto finished;
s->name_len = 0;
s->name_partial = 0;
skip_line:
p = nl + 1;
nl = memchr (p, '\n', s->buf + s->buf_len - p);
}
else
/* We found no newline, so the name extends past what we read; we'll
try to read more next time. */
{
s->name_partial = 1;
/* Skip over the partial name for the next iteration. */
p += name_len;
}
}
while (nl);
/* Move any remaining characters in the buffer to the beginning for the
next call. */
s->buf_len -= (p - s->buf);
if (s->buf_len > 0)
memmove (s->buf, p, s->buf_len);
/* Try again later. */
return EAGAIN;
enomem:
/* Some memory allocation failed. */
err = ENOMEM;
finished:
/* We're finished (with an error if ERR != 0), deallocate everything &
return. */
if (s->name)
free (s->name);
free (s);
close (fd);
if (err && rd > 0)
ftp_conn_abort (conn);
else if (err)
ftp_conn_finish_transfer (conn);
else
err = ftp_conn_finish_transfer (conn);
return err;
}
/* Give a name which refers to a directory file, and a name in that
directory, this should return in COMPOSITE the composite name refering to
that name in that directory, in malloced storage. */
error_t
ftp_conn_unix_append_name (struct ftp_conn *conn,
const char *dir, const char *name,
char **composite)
{
char *path = malloc (strlen (dir) + 1 + strlen (name) + 1);
if (! path)
return ENOMEM;
/* Form the path name. */
if (name && *name)
if (dir[0] == '/' && dir[1] == '\0')
stpcpy (stpcpy (path, dir), name);
else
stpcpy (stpcpy (stpcpy (path, dir), "/"), name);
else
strcpy (path, dir);
*composite = path;
return 0;
}
/* If the name of a file *NAME is a composite name (containing both a
filename and a directory name), this function should change *NAME to be
the name component only; if the result is shorter than the original
*NAME, the storage pointed to it may be modified, otherwise, *NAME
should be changed to point to malloced storage holding the result, which
will be freed by the caller. */
error_t
ftp_conn_unix_basename (struct ftp_conn *conn, char **name)
{
*name = basename (*name);
return 0;
}
|