/
ftpd.c
6263 lines (5860 loc) · 170 KB
/
ftpd.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
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <config.h>
#define DEFINE_GLOBALS
#include "messages.h"
#include "ftpd_p.h"
#include "dynamic.h"
#include "ftpwho-update.h"
#include "ftpwho-read.h"
#include "globals.h"
#include "caps.h"
#include "alt_arc4random.h"
#if defined(WITH_UPLOAD_SCRIPT)
# include "upload-pipe.h"
#endif
#ifdef WITH_ALTLOG
# include "altlog.h"
#endif
#ifdef QUOTAS
# include "quotas.h"
#endif
#ifdef WITH_DIRALIASES
# include "diraliases.h"
#endif
#include "ftpd.h"
#include "bsd-glob.h"
#include "getloadavg.h"
#include "safe_rw.h"
#include "utils.h"
#ifndef MINIMAL
# include "simpleconf.h"
# include "simpleconf_ftpd.h"
#endif
#ifndef WITHOUT_PRIVSEP
# include "privsep.h"
#endif
#ifdef WITH_TLS
# include "tls.h"
# include "tls_extcert.h"
#endif
#ifdef WITH_BONJOUR
# include "bonjour.h"
#endif
#ifdef HAVE_LIBSODIUM
# include <sodium.h>
#endif
#ifdef WITH_DMALLOC
# include <dmalloc.h>
#endif
void disablesignals(void)
{
sigset_t sigs;
sigfillset(&sigs);
if (sigprocmask(SIG_BLOCK, &sigs, &old_sigmask) < 0) {
_EXIT(EXIT_FAILURE);
}
}
static void enablesignals(void)
{
if (sigprocmask(SIG_SETMASK, &old_sigmask, NULL) < 0) {
_EXIT(EXIT_FAILURE);
}
}
void usleep2(const unsigned long microsec)
{
disablesignals();
usleep(microsec);
enablesignals();
}
#ifdef WITH_TLS
ssize_t secure_safe_write(void * const tls_fd, const void *buf_, size_t count)
{
ssize_t written;
const char *buf = (const char *) buf_;
while (count > (size_t) 0U) {
for (;;) {
if ((written = SSL_write(tls_fd, buf, count)) <= (ssize_t) 0) {
if (SSL_get_error(tls_fd, written) != SSL_ERROR_NONE) {
return (ssize_t) -1;
}
continue;
}
break;
}
buf += written;
count -= written;
}
return (ssize_t) (buf - (const char *) buf_);
}
#endif
static ssize_t safe_nonblock_write(const int fd, void * const tls_fd,
const void *buf_, size_t count)
{
ssize_t written;
const char *buf = (const char *) buf_;
struct pollfd pfd;
while (count > (size_t) 0U) {
for (;;) {
if (tls_fd == NULL) {
written = write(fd, buf, count);
} else {
#ifdef WITH_TLS
written = SSL_write(tls_fd, buf, count);
if (SSL_get_error(tls_fd, written) == SSL_ERROR_WANT_WRITE) {
errno = EAGAIN;
}
#else
abort();
#endif
}
if (written > (ssize_t) 0) {
break;
}
if (errno == EAGAIN || errno == EWOULDBLOCK) {
pfd.fd = fd;
pfd.events = POLLOUT | POLLERR | POLLHUP;
pfd.revents = 0;
if (poll(&pfd, 1U, idletime * 1000UL) <= 0 ||
(pfd.revents & (POLLERR | POLLHUP)) != 0 ||
(pfd.revents & POLLOUT) == 0) {
errno = EPIPE;
return -1;
}
} else if (errno != EINTR) {
return -1;
}
}
buf += written;
count -= written;
}
return 0;
}
static void overlapcpy(char *d, const char *s)
{
while (*s != 0) {
*d++ = *s++;
}
*d = 0;
}
static void safe_fd_set(const int fd, fd_set * const fds)
{
if (fd == -1) {
return;
}
FD_SET(fd, fds);
}
static int safe_fd_isset(const int fd, const fd_set * const fds)
{
if (fd == -1) {
return 0;
}
return FD_ISSET(fd, fds);
}
static int init_tz(void)
{
char stbuf[10];
struct tm *tm;
time_t now = time(NULL);
#ifdef HAVE_TZSET
tzset();
#endif
#ifdef HAVE_PUTENV
if ((tm = localtime(&now)) != NULL &&
strftime(stbuf, sizeof stbuf, "%z", tm) == (size_t) 5U) {
snprintf(default_tz_for_putenv, sizeof default_tz_for_putenv,
"TZ=UTC%c%c%c:%c%c", (*stbuf == '-' ? '+' : '-'),
stbuf[1], stbuf[2], stbuf[3], stbuf[4]);
}
putenv(default_tz_for_putenv);
#endif
(void) localtime(&now);
(void) gmtime(&now);
return 0;
}
void simplify(char *subdir)
{
char *a;
if (subdir == NULL || *subdir == 0) {
return;
}
while ((a = strstr(subdir, "//")) != NULL) {
overlapcpy(a, a + 1);
}
while ((a = strstr(subdir, "/./")) != NULL) {
overlapcpy(a, a + 2);
}
while (strncmp(subdir, "../", 3) == 0) {
subdir += 3;
}
a = strstr(subdir, "/../");
if (a != NULL) {
if (a == subdir) {
while (strncmp(subdir, "/../", 4) == 0) {
overlapcpy(subdir, subdir + 3);
}
a = strstr(subdir, "/../");
}
while (a != NULL) {
char *nextcomponent = a + 4;
if (a != subdir && *a == '/') {
a--;
}
while (a != subdir && *a != '/') {
a--;
}
if (*a == '/') {
a++;
}
overlapcpy(a, nextcomponent);
a = strstr(subdir, "/../");
}
}
a = subdir;
if (*a == '.') {
a++;
if (*a == 0) {
return;
}
if (*a == '/') {
while (*a == '/') {
a++;
}
overlapcpy(subdir, a);
}
}
if (*a == 0) {
return;
}
a = subdir + strlen(subdir) - (size_t) 1U;
if (*a != '.' || a == subdir) {
return;
}
a--;
if (*a == '/') {
a[1] = 0;
return;
}
if (*a != '.' || a == subdir) {
return;
}
a--;
if (*a != '/') {
return;
}
*a = 0;
if ((a = strrchr(subdir, '/')) == NULL) {
*subdir = '/';
subdir[1] = 0;
return;
}
a[1] = 0;
}
int checkprintable(const char *s)
{
int ret = 0;
unsigned char c;
while ((c = (unsigned char) *s) != 0U) {
if (ISCTRLCODE(c)) {
ret--;
break;
}
s++;
}
return ret;
}
char *skip_telnet_controls(const char *str)
{
if (str == NULL) {
return NULL;
}
while (*str != 0 && (unsigned char) *str >= 240U) {
str++;
}
return (char *) str;
}
void _EXIT(const int status)
{
delete_atomic_file();
#ifdef FTPWHO
ftpwho_exit();
#endif
_exit(status);
}
static char replybuf[MAX_SERVER_REPLY_LEN * 4U];
static char *replybuf_pos = replybuf;
static size_t replybuf_left;
static void client_init_reply_buf(void)
{
replybuf_pos = replybuf;
replybuf_left = sizeof replybuf - 1U;
}
void client_fflush(void)
{
if (replybuf_pos == replybuf) {
return;
}
safe_write(clientfd, replybuf, (size_t) (replybuf_pos - replybuf), -1);
client_init_reply_buf();
}
void client_printf(const char * const format, ...)
{
va_list va;
char buf[MAX_SERVER_REPLY_LEN];
size_t len;
int vlen;
va_start(va, format);
vlen = vsnprintf(buf, sizeof buf, format, va);
if (vlen < 0 || (size_t) vlen >= sizeof buf) {
buf[MAX_SERVER_REPLY_LEN - 1] = 0;
len = strlen(buf);
} else {
len = (size_t) vlen;
}
if (len >= replybuf_left) {
client_fflush();
}
if (len > replybuf_left) {
va_end(va);
abort();
}
memcpy(replybuf_pos, buf, len);
replybuf_pos += len;
replybuf_left -= len;
va_end(va);
}
void die(const int err, const int priority, const char * const format, ...)
{
va_list va;
char line[MAX_SYSLOG_LINE];
disablesignals();
logging = 0;
va_start(va, format);
vsnprintf(line, sizeof line, format, va);
addreply(err, "%s", line);
va_end(va);
doreply();
logfile(priority, "%s", line);
_EXIT(-priority - 1);
}
void die_mem(void)
{
die(421, LOG_ERR, MSG_OUT_OF_MEMORY);
}
static void sigalarm(int sig)
{
(void) sig;
disablesignals();
die(421, LOG_INFO, MSG_TIMEOUT);
}
#ifndef NO_STANDALONE
static void sigchild(int sig)
{
const int olderrno = errno;
pid_t pid;
(void) sig;
# ifdef HAVE_WAITPID
while ((pid = waitpid((pid_t) -1, NULL, WNOHANG)) > (pid_t) 0) {
if (nb_children > 0U) {
nb_children--;
}
# ifdef FTPWHO
ftpwho_unlinksbfile(pid);
# endif
iptrack_delete_pid(pid);
}
# endif
errno = olderrno;
}
#endif
static void sigterm_client(int sig)
{
(void) sig;
disablesignals();
_EXIT(EXIT_SUCCESS);
}
#ifndef NO_STANDALONE
static void sigterm(int sig)
{
const int olderrno = errno;
(void) sig;
stop_server = 1;
if (listenfd != -1) {
shutdown(listenfd, 2);
(void) close(listenfd);
}
if (listenfd6 != -1) {
shutdown(listenfd6, 2);
(void) close(listenfd6);
}
errno = olderrno;
}
static void set_cloexec_flag(const int fd)
{
fcntl(fd, F_SETFD, FD_CLOEXEC);
}
#endif
static void clearargs(int argc, char **argv)
{
#ifndef NO_PROCNAME_CHANGE
# if defined(__linux__) && !defined(HAVE_SETPROCTITLE)
int i;
char *first = NULL;
char *next = NULL;
for (i = 0; i < argc; i++) {
if (first == NULL) {
first = argv[i];
}
if (next == NULL || argv[i] == next + 1) {
next = argv[i] + strlen(argv[i]);
}
}
for (i = 0; environ[i] != NULL; i++) {
if (first == NULL) {
first = argv[i];
}
if (next == NULL) {
next = argv[i] + strlen(argv[i]);
}
}
if (first == NULL || next == NULL) {
return;
}
argv_lth = next - first;
argv0 = argv;
if (environ != NULL) {
char **new_environ;
unsigned int env_nb = 0U;
while (environ[env_nb] != NULL) {
env_nb++;
}
if ((new_environ = malloc((1U + env_nb) * sizeof (char *))) == NULL) {
abort();
}
new_environ[env_nb] = NULL;
while (env_nb > 0U) {
env_nb--;
new_environ[env_nb] = strdup(environ[env_nb]);
}
environ = new_environ;
}
# else
(void) argc;
(void) argv;
# endif
#endif
}
void setprocessname(const char * const title)
{
#ifndef NO_PROCNAME_CHANGE
# ifdef HAVE_SETPROCTITLE
setproctitle("-%s", title);
# elif defined(__linux__)
if (argv0 != NULL && argv_lth > strlen(title) - 2) {
memset(argv0[0], 0, argv_lth);
strncpy(argv0[0], title, argv_lth - 2);
argv0[1] = NULL;
}
# elif defined(__hpux__)
union pstun pst;
pst.pst_command = title;
pstat(PSTAT_SETCMD, pst, strlen(title), 0, 0);
# endif
#endif
(void) title;
}
/* Check whether an address is valid, return 1 if ok, 0 otherwise.
* Unfortunately, multicasting with the FTP protocol is impossible,
* you have to use things like MTP instead. So prohibit multicast.
*/
static int checkvalidaddr(const struct sockaddr_storage * const addr)
{
if (addr == NULL) {
return 0;
}
/* Some versions of MacOS X have broken IN* macros */
#ifdef __APPLE_CC__
return 1;
#endif
if (STORAGE_FAMILY(*addr) == AF_INET6) {
if (IN6_IS_ADDR_MULTICAST(&STORAGE_SIN_ADDR6_NF_CONST(*addr)) ||
IN6_IS_ADDR_UNSPECIFIED(&STORAGE_SIN_ADDR6_NF_CONST(*addr))) {
return 0;
}
return 1;
} else if (STORAGE_FAMILY(*addr) == AF_INET) {
if (ntohl(STORAGE_SIN_ADDR_CONST(*addr)) == INADDR_ANY ||
ntohl(STORAGE_SIN_ADDR_CONST(*addr)) == INADDR_NONE ||
ntohl(STORAGE_SIN_ADDR_CONST(*addr)) == INADDR_BROADCAST ||
IN_MULTICAST(ntohl(STORAGE_SIN_ADDR_CONST(*addr)))) {
return 0;
}
return 1;
}
return 0;
}
/* Convert a 4-in-6 address to pure IPv4 */
static void fourinsix(struct sockaddr_storage *v6)
{
struct sockaddr_storage v4;
if (v6ready == 0 || STORAGE_FAMILY(*v6) != AF_INET6 ||
IN6_IS_ADDR_V4MAPPED(&STORAGE_SIN_ADDR6_NF_CONST(*v6)) == 0) {
return;
}
memset(&v4, 0, sizeof v4);
STORAGE_FAMILY(v4) = AF_INET;
memcpy(&STORAGE_SIN_ADDR(v4),
(unsigned char *) &STORAGE_SIN_ADDR6_CONST(*v6) + 12,
sizeof STORAGE_SIN_ADDR(v4));
STORAGE_PORT(v4) = STORAGE_PORT6_CONST(*v6);
SET_STORAGE_LEN(v4, sizeof(struct sockaddr_in));
*v6 = v4;
}
/* Return 0 if s1 == s2 , 1 if s1 != s2 , -1 if error */
static int addrcmp(const struct sockaddr_storage * const s1,
const struct sockaddr_storage * const s2)
{
if (STORAGE_FAMILY(*s1) == AF_INET6) {
if (STORAGE_FAMILY(*s2) != AF_INET6) {
return 1;
}
if (IN6_ARE_ADDR_EQUAL(&STORAGE_SIN_ADDR6_NF_CONST(*s1), &STORAGE_SIN_ADDR6_NF_CONST(*s2))) {
return 0;
} else {
return 1;
}
} else if (STORAGE_FAMILY(*s1) == AF_INET) {
if (STORAGE_FAMILY(*s2) != AF_INET) {
return 1;
}
if (STORAGE_SIN_ADDR_CONST(*s1) == STORAGE_SIN_ADDR_CONST(*s2)) {
return 0;
} else {
return 1;
}
}
return -1;
}
static int generic_aton(const char *src, struct sockaddr_storage *a)
{
if (inet_pton(AF_INET6, src, &STORAGE_SIN_ADDR6(*a)) > 0) {
STORAGE_FAMILY(*a) = AF_INET6;
return 0;
}
if (inet_pton(AF_INET, src, &STORAGE_SIN_ADDR(*a)) > 0) {
STORAGE_FAMILY(*a) = AF_INET;
return 0;
}
memset(a, 0, sizeof *a);
return -1;
}
void logfile(const int crit, const char *format, ...)
{
#if defined(NON_ROOT_FTP)
(void) crit;
(void) format;
#else
const char *urgency;
va_list va;
char line[MAX_SYSLOG_LINE];
if (no_syslog != 0) {
va_end(va);
return;
}
va_start(va, format);
vsnprintf(line, sizeof line, format, va);
va_end(va);
switch (crit) {
case LOG_INFO:
urgency = "[INFO] ";
break;
case LOG_WARNING:
urgency = "[WARNING] ";
break;
case LOG_ERR:
urgency = "[ERROR] ";
break;
case LOG_NOTICE:
urgency = "[NOTICE] ";
break;
case LOG_DEBUG:
urgency = "[DEBUG] ";
break;
default:
urgency = "";
}
# ifdef SAVE_DESCRIPTORS
openlog("pure-ftpd", log_pid, syslog_facility);
# endif
syslog(crit, "(%s@%s) %s%s",
((loggedin != 0 && *account != 0) ? account : "?"),
(*host != 0 ? host : "?"),
urgency, line);
# ifdef SAVE_DESCRIPTORS
closelog();
# endif
#endif
}
#ifndef NO_STANDALONE
/* this is taken from the code examples for Stevens' "Advanced
* Programming in the Unix Environment. The code is publicly available
* at ftp://ftp.uu.net/published/books/stevens.advprog.tar.Z */
static unsigned int open_max(void)
{
long z;
if ((z = (long) sysconf(_SC_OPEN_MAX)) < 0L) {
perror("_SC_OPEN_MAX");
_EXIT(EXIT_FAILURE);
}
return (unsigned int) z;
}
#endif
static void addreply_newline(const char * const str, const size_t size)
{
struct reply *newline;
if ((newline = (struct reply *) malloc(offsetof(struct reply, line) +
size)) == NULL) {
die_mem();
}
if (firstreply == NULL) {
firstreply = newline;
} else {
lastreply->next = newline;
}
newline->next = NULL;
lastreply = newline;
memcpy(newline->line, str, size);
}
void addreply_noformat(const int code, const char * const line)
{
if (code != 0) {
replycode = code;
}
addreply_newline(line, strlen(line) + (size_t) 1U);
}
void addreply(const int code, const char * const line, ...)
{
char *a;
char *b;
va_list ap;
int last;
char buf[MAX_SERVER_REPLY_LEN];
if (code != 0) {
replycode = code;
}
va_start(ap, line);
vsnprintf(buf, sizeof buf, line, ap);
va_end(ap);
last = 0;
a = buf;
for (;;) {
b = strchr(a, '\n');
if (b != NULL) {
*b = 0;
} else {
b = a;
while (*b != 0) {
b++;
}
last++;
}
addreply_newline(a, (size_t) (b - a) + (size_t) 1U);
if (last != 0) {
break;
}
a = b + 1;
}
}
void doreply(void)
{
struct reply *scannedentry;
struct reply *nextentry;
if ((scannedentry = firstreply) == NULL) {
return;
}
do {
nextentry = scannedentry->next;
#ifdef WITH_TLS
if (tls_cnx != NULL) {
char buf[MAX_SERVER_REPLY_LEN];
snprintf(buf, sizeof buf, "%3d%c%s\r\n", replycode,
nextentry == NULL ? ' ' : '-', scannedentry->line);
SSL_write(tls_cnx, buf, strlen(buf));
} else
#endif
{
client_printf("%3d%c%s\r\n", replycode,
nextentry == NULL ? ' ' : '-',
scannedentry->line);
}
if (logging > 1) {
logfile(LOG_DEBUG, "%3d%c%s", replycode,
nextentry == NULL ? ' ' : '-', scannedentry->line);
}
} while ((scannedentry = nextentry) != NULL);
client_fflush();
scannedentry = firstreply;
do {
nextentry = scannedentry->next;
free(scannedentry);
} while ((scannedentry = nextentry) != NULL);
firstreply = lastreply = NULL;
}
/* Check whether a file name is valid. Files names starting
* with a dot are only allowed to root and to users
* chroot()ed in their home directories -Jedi. */
static int checknamesanity(const char *name, int dot_ok)
{
const char *namepnt;
#ifdef PARANOID_FILE_NAMES
const char *validchars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefgihjklmnopqrstuvwxyz"
"0123456789./-_";
#endif
if (name == NULL || *name == 0) {
return -1;
}
/* optimize . and .. */
if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0))) {
return 0;
}
namepnt = name;
#ifdef PARANOID_FILE_NAMES
/* we want to make sure we don't get any non-alphanumeric file name */
if (strlen(namepnt) != strspn(namepnt, validchars)) {
return -1;
}
#endif
#ifdef QUOTAS
if (hasquota() == 0) {
if (strstr(namepnt, QUOTA_FILE) != NULL) {
return -1; /* .ftpquota => *NO* */
}
# ifndef ALLOW_DELETION_OF_TEMPORARY_FILES
if (strstr(namepnt, PUREFTPD_TMPFILE_PREFIX) == namepnt) {
return -1;
}
# endif
}
#endif
while (*namepnt != 0) {
#ifndef ALLOW_EVERYTHING_IN_FILE_NAMES
if (ISCTRLCODE(*namepnt) || *namepnt == '\\') {
return -1;
}
#endif
if (dot_ok == 0) {
if (*namepnt == '/') {
namepnt++;
} else if (namepnt != name) {
namepnt++;
continue;
}
if (namepnt[0] == 0) { /* /$ */
return 0;
}
if (namepnt[0] == '.') { /* /. */
if (namepnt[1] == 0) { /* /.$ => ok */
return 0;
}
if (namepnt[1] == '.') { /* /.. */
if (namepnt[2] == 0) { /* /..$ => ok */
return 0;
}
if (namepnt[2] != '/') { /* /..[^/] => *NO* */
return -1;
}
} else if (namepnt[1] != '/') { /* /.[^/]/ => *NO* */
return -1;
}
}
if (namepnt != name) {
continue;
}
}
namepnt++;
}
return 0;
}
static void do_ipv6_port(char *p, char delim)
{
char *deb;
struct sockaddr_storage a;
deb = p;
while (*p && strchr("0123456789abcdefABCDEF:", *p) != NULL) {
p++;
}
if (*p != delim || atoi(p + 1) == 0) {
nope:
(void) close(datafd);
datafd = -1;
addreply_noformat(501, MSG_SYNTAX_ERROR_IP);
return;
}
*p++ = 0;
if (generic_aton(deb, &a) != 0) {
goto nope;
}
doport2(a, (unsigned int) atoi(p));
}
#ifndef MINIMAL
void doesta(void)
{
struct sockaddr_storage dataconn;
socklen_t socksize;
char hbuf[NI_MAXHOST];
char pbuf[NI_MAXSERV];
if (passive != 0 || datafd == -1) {
addreply_noformat(520, MSG_ACTIVE_DISABLED);
return;
}
if (xferfd == -1) {
opendata();
if (xferfd == -1) {
addreply_noformat(425, MSG_CANT_CREATE_DATA_SOCKET);
return;
}
}
socksize = (socklen_t) sizeof dataconn;
if (getsockname(xferfd, (struct sockaddr *) &dataconn, &socksize) < 0 ||
getnameinfo((struct sockaddr *) &dataconn, STORAGE_LEN(dataconn),
hbuf, sizeof hbuf, pbuf, sizeof pbuf,
NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
addreply_noformat(425, MSG_GETSOCKNAME_DATA);
closedata();
return;
}
addreply(225, "Connected from (|%c|%s|%s|)",
STORAGE_FAMILY(dataconn) == AF_INET6 ? '2' : '1', hbuf, pbuf);
}
void doestp(void)
{
struct sockaddr_storage dataconn;
socklen_t socksize;
char hbuf[NI_MAXHOST];
char pbuf[NI_MAXSERV];
if (passive == 0 || datafd == -1) {
addreply_noformat(520, MSG_CANT_PASSIVE);
return;
}
if (xferfd == -1) {
opendata();
if (xferfd == -1) {
addreply_noformat(425, MSG_CANT_CREATE_DATA_SOCKET);
return;
}
}
socksize = (socklen_t) sizeof dataconn;
if (getpeername(xferfd, (struct sockaddr *) &dataconn, &socksize) < 0 ||
getnameinfo((struct sockaddr *) &dataconn, STORAGE_LEN(dataconn),
hbuf, sizeof hbuf, pbuf, sizeof pbuf,
NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
addreply_noformat(425, MSG_GETSOCKNAME_DATA);
closedata();
return;
}
addreply(225, "Connected to (|%c|%s|%s|)",
STORAGE_FAMILY(dataconn) == AF_INET6 ? '2' : '1', hbuf, pbuf);
}
#endif
void doeprt(char *p)
{
char delim;
int family;
delim = *p++;
family = atoi(p);
while (isdigit((unsigned char) *p)) {
p++;
}
if (*p == delim) {
p++;
} else {
addreply_noformat(501, MSG_SYNTAX_ERROR_IP);
return;
}
if (family == 2 && v6ready) {
do_ipv6_port(p, delim);
return;
}
if (family != 1) {
if (v6ready) {
addreply_noformat(522, MSG_ONLY_IPV4V6);
} else {
addreply_noformat(522, MSG_ONLY_IPV4);
}
return;
}
{
unsigned int a1, a2, a3, a4, port = 0U;
/* there should be dot-decimal ip as rfc2428 states,
* but troll used for some reason "comma-decimal" notation
* so I decided to leave it */
if ((sscanf(p, "%u,%u,%u,%u", &a1, &a2, &a3, &a4) != 4 &&
sscanf(p, "%u.%u.%u.%u", &a1, &a2, &a3, &a4) != 4) ||
a1 > 255U || a2 > 255U || a3 > 255U || a4 > 255U ||
(a1 | a2 | a3 | a4) == 0U) {
addreply_noformat(501, MSG_SYNTAX_ERROR_IP);
return;
}
while (*p && strchr("0123456789.,", *p)) {
p++;
}
if (*p == delim) {
port = (unsigned int) atoi(++p);
while (*p && isdigit((unsigned char) *p)) {
p++;
}
}
if (*p != delim || port > 65535U || port <= 0U) {
addreply_noformat(501, MSG_SYNTAX_ERROR_IP);
return;
} else {
struct sockaddr_storage a;
memset(&a, 0, sizeof a);
STORAGE_FAMILY(a) = AF_INET;
STORAGE_SIN_ADDR(a) =
htonl(((uint32_t) a1 << 24) |
((uint32_t) a2 << 16) | (a3 << 8) | a4);
SET_STORAGE_LEN(a, sizeof(struct sockaddr_in));
doport2(a, port);