]> git.sesse.net Git - betaftpd/blob - ftpd.c
All header files should now be self-contained (ie. you shouldn't need to do any syste...
[betaftpd] / ftpd.c
1 /*  ftpd.c: BetaFTPD main
2     Copyright (C) 1999-2000 Steinar H. Gunderson
3
4     This program is is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License, version 2 if the
6     License as published by the Free Software Foundation.
7
8     This program is distributed in the hope that it will be useful,
9     but WITHOUT ANY WARRANTY; without even the implied warranty of
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11     GNU General Public License for more details.
12
13     You should have received a copy of the GNU General Public License
14     along with this program; if not, write to the Free Software
15     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 */
17
18 /*
19  * Special note: this file has been overwritten by another (0-byte) file, been
20  * through the dead, and restored (with the help of dd, grep, gpm, vi and less)
21  * with a sucess rate of 99.9%. Show it a little respect -- don't add junk
22  * to it. :-)
23  */
24
25 #define _GNU_SOURCE
26
27 #if HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30
31 #if HAVE_SYS_TYPES_H
32 #include <sys/types.h>
33 #endif
34
35 #if HAVE_ERRNO_H
36 #include <errno.h>
37 #endif
38
39 #if HAVE_STROPTS_H
40 #include <stropts.h>
41 #endif
42
43 #if HAVE_SYS_CONF_H
44 #include <sys/conf.h>
45 #endif
46
47 #if HAVE_FCNTL_H
48 #include <fcntl.h>
49 #endif
50
51 #if HAVE_STDIO_H
52 #include <stdio.h>
53 #endif
54
55 #if HAVE_ASSERT_H
56 #include <assert.h>
57 #endif
58
59 #if HAVE_STRING_H
60 #include <string.h>
61 #endif
62
63 #if HAVE_STRINGS_H
64 #include <strings.h>
65 #endif
66
67 #if HAVE_STDARG_H
68 #include <stdarg.h>
69 #endif
70
71 #if HAVE_STDLIB_H
72 #include <stdlib.h>
73 #endif
74
75 #if HAVE_UNISTD_H
76 #include <unistd.h>
77 #endif
78
79 #if HAVE_NETINET_IN_H
80 #include <netinet/in.h>
81 #endif
82
83 #if HAVE_ARPA_INET_H
84 #include <arpa/inet.h>
85 #endif
86
87 #if HAVE_SYS_STAT_H
88 #include <sys/stat.h>
89 #endif
90
91 #if HAVE_SYS_SOCKET_H
92 #include <sys/socket.h>
93 #endif
94
95 #if HAVE_SYS_IOCTL_H
96 #include <sys/ioctl.h>
97 #endif
98
99 #if HAVE_NETINET_IN_SYSTM_H
100 #include <netinet/in_systm.h>
101 #endif
102
103 #if HAVE_NETINET_IP_H
104 #include <netinet/ip.h>
105 #endif
106
107 #if HAVE_NETINET_TCP_H
108 #include <netinet/tcp.h>
109 #endif
110
111 #if HAVE_LINUX_SOCKET_H
112 #include <linux/socket.h>
113 #endif
114
115 #if HAVE_LINUX_TCP_H
116 #include <linux/tcp.h>
117 #endif
118
119 #if HAVE_MMAP
120 #include <sys/mman.h>
121 #endif
122
123 #if HAVE_TIME_H
124 #include <time.h>
125 #endif
126
127 #if HAVE_SYS_TIME_H
128 #include <sys/time.h>
129 #endif
130
131 #if HAVE_SYS_TIME_H
132 #include <sys/time.h>
133 #endif
134
135 #if HAVE_SYS_FILIO_H
136 #include <sys/filio.h>
137 #endif
138
139 #if HAVE_NETDB_H
140 #include <netdb.h>
141 #endif
142
143 #if HAVE_SIGNAL_H
144 #include <signal.h>
145 #endif
146
147 #if HAVE_GLOB_H
148 #include <glob.h>
149 #endif
150
151 #if HAVE_SYS_SIGNAL_H
152 #include <sys/signal.h>
153 #endif
154
155 #if HAVE_SYS_POLL_H
156 #include <sys/poll.h>
157 #endif
158
159 #if HAVE_SYS_SENDFILE_H
160 #include <sys/sendfile.h>
161 #endif
162
163 /*
164  * <linux/socket.h> does not export this to glibc2 systems, and it isn't
165  * always defined anywhere else.
166  */
167 #if !defined(TCP_CORK) && defined(__linux__)
168 #define TCP_CORK 3
169 #endif
170
171 #include <ftpd.h>
172 #include <cmds.h>
173
174 #if WANT_ASCII
175 #include <ascii.h>
176 #endif
177
178 #if WANT_DCACHE
179 #include <dcache.h>
180 #endif
181
182 #ifndef MAP_FAILED
183 #define MAP_FAILED -1
184 #endif
185
186 struct conn *first_conn = NULL;
187 struct ftran *first_ftran = NULL;
188 #if WANT_DCACHE
189 struct dcache *first_dcache = NULL;
190 #endif
191
192 #if HAVE_POLL
193 unsigned int highest_fds = 0;
194
195 #define FD_MAX 1024
196 #define fds_send fds
197 struct pollfd fds[FD_MAX];
198
199 #define MAXCLIENTS FD_MAX
200 #else
201 fd_set master_fds, master_send_fds;
202 #define MAXCLIENTS FD_SETSIZE
203 #endif
204
205 #if WANT_XFERLOG
206 FILE *xferlog = NULL;
207 #endif
208
209 #if HAVE_LINUX_SENDFILE
210 int sendfile_supported = 1;
211 #endif
212
213 /*
214  * This variable specifies if it's soon time to check for timed out
215  * clients, and timed out directory listing cache entries. It is
216  * set to 1 by a signal handler every minute, and set to 0 when the
217  * checking has been performed.
218  */
219 int time_to_check = 1;
220
221 #ifndef HAVE_SPRINTF
222 /*
223  * snprintf():  snprintf() replacement for systems that miss it. Note
224  *              that this implementation does _not_ necessarily protect
225  *              against all buffer overflows. Get a real snprintf() in
226  *              your C library. That being said, the 8k limit is
227  *              substantially larger than any other string in BetaFTPD,
228  *              which should make such an attack harder.
229  */
230 int snprintf(char *str, size_t n, const char *format, ...)
231 {
232         char buf[8192];
233         va_list args;
234         int err;
235
236         va_start(args, format);
237         err = vsprintf(buf, format, args);
238         va_end(args);
239
240         buf[(int)n] = 0;
241         strcpy(str, buf);
242
243         return err;
244 }
245 #endif
246
247 #ifndef HAVE_VSNPRINTF
248 /*
249  * vsnprintf:   vsnprintf() replacement for systems that miss it. Please
250  *              see snprintf (above) for more information.
251  */
252 int vsnprintf(char *str, size_t n, const char *format, va_list ap)
253 {
254         char buf[8192];
255         int err;
256
257         err = vsprintf(buf, format, ap);
258         buf[(int)n] = 0;
259         strcpy(str, buf);
260         return err;
261 }
262 #endif
263
264 /*
265  * add_fd():    Add an fd to the set we monitor. Return 0 on success.
266  *              This code is shared between poll() and select() versions.
267  */
268 int add_fd(const int fd, const int events)
269 {
270 #if HAVE_POLL
271         if (fd >= FD_MAX) {
272                 printf("add_fd(%d, %x): failed\n", fd, events);
273                 return E2BIG;
274         }
275
276         fds[fd].fd = fd;
277         fds[fd].events = events;
278         if (highest_fds < fd) 
279                 highest_fds = fd;
280 #else 
281         if (fd >= FD_SETSIZE)
282                 return E2BIG;
283         if (events & POLLIN)
284                 FD_SET(fd, &master_fds);
285         if (events & POLLOUT)
286                 FD_SET(fd, &master_send_fds);
287 #endif
288         return 0;
289 }
290
291 /*
292  * del_fd():    Close and remove an fd from the set(s) we monitor. (See also add_fd().)
293  */
294 void del_fd(const int fd)
295 {
296 #if HAVE_POLL
297         if (fd >= FD_MAX)
298                 return;
299
300         fds[fd].fd = -1;
301         fds[fd].events = 0;
302
303         /* Reduce poll()'s workload by not making it watch past end of array */
304         while ((highest_fds > 0) && (fds[highest_fds].fd == -1))
305                 highest_fds--;
306 #else 
307         if (fd >= FD_SETSIZE)
308                 return;
309         FD_CLR(fd, &master_fds);
310         FD_CLR(fd, &master_send_fds);
311 #endif
312
313         close(fd);
314 }
315
316 #if 0
317 void list_clients()
318 {
319         struct conn *c = first_conn;
320         printf("list_clients:\n");
321         while (c && c->next_conn) {
322                 c = c->next_conn;
323                 printf("list_clients: fd %d\n", c->sock);
324         }
325 }
326 #endif
327
328 /*
329  * add_to_linked_list():
330  *              Inserts an element (conn, ftran or dcache) into its linked list.
331  *              The list is placed at the beginning, right after the (bogus)
332  *              first element of the list.
333  */
334 void add_to_linked_list(struct list_element * const first,
335                         struct list_element * const elem)
336 {
337         elem->prev = first;
338
339         if (first) {
340                 elem->next = first->next;
341                 if (elem->next) elem->next->prev = elem;
342                 first->next = elem;
343         } else {
344                 /* this is the bogus head of the list */
345                 elem->next = NULL;
346         }
347 }
348
349 /*
350  * remove_from_linked_list():
351  *              Removes an element (conn, ftran or dcache) from its linked list,
352  *              then frees it.
353  */
354 void remove_from_linked_list(struct list_element * const elem)
355 {
356         if (elem->prev != NULL) elem->prev->next = elem->next;
357         if (elem->next != NULL) elem->next->prev = elem->prev;
358         free(elem);
359 }
360
361 /*
362  * alloc_new_conn():
363  *              Allocates a new control connection (type `struct conn'),
364  *              initializes it, and adds it to the linked list. The connection
365  *              operates on the socket SOCK.
366  */
367 struct conn *alloc_new_conn(const int sock)
368 {
369         const unsigned int one = 1;
370         struct conn *c = (struct conn *)(malloc(sizeof(struct conn)));
371
372         if (c == NULL) return c;
373
374         if (sock != -1) {
375                 ioctl(sock, FIONBIO, &one);
376                 if (add_fd(sock, POLLIN) != 0) {
377                         /* temp unavail */
378                         send(sock, "230 Server too busy, please try again later.\r\n", 46, 0);
379                         close(sock);
380                         return NULL;
381                 }
382
383                 add_to_linked_list((struct list_element *)first_conn,
384                                    (struct list_element *)c);
385         } else {
386                 /* this is the bogus head of the list */
387                 c->next_conn = NULL;
388                 c->prev_conn = NULL;
389         }
390
391         c->transfer = NULL;
392         c->sock = sock;
393         c->buf_len = c->auth = c->rest_pos = 0;
394 #if WANT_ASCII
395         c->ascii_mode = 0;
396 #endif
397
398         /*
399          * equals:
400          * strcpy(c->curr_dir, "/");
401          * strcpy(c->last_cmd, "");
402          * strcpy(c->rename_from, "")
403          */
404         c->curr_dir[0] = '/';
405 #if WANT_FULLSCREEN
406         c->curr_dir[1] = c->last_cmd[0] = c->rename_from[0] = '\0';
407 #else
408         c->curr_dir[1] = c->rename_from[0] = '\0';
409 #endif
410
411         time(&(c->last_transfer));
412
413         /*list_clients();*/
414
415         return c;
416 }
417
418 /*
419  * alloc_new_ftran():
420  *              Allocates a new data connection (type `struct ftran'), and
421  *              adds it to the linked list. The connection operates on the
422  *              socket SOCK, and has the control connection C as its parent.
423  */
424 struct ftran *alloc_new_ftran(const int sock, const struct conn * const c)
425 {
426         struct ftran *f = (struct ftran *)(malloc(sizeof(struct ftran)));
427
428         if (f == NULL) return f;
429         if (c == NULL) {
430                 /* this is the bogus head of the list */
431                 f->next_ftran = NULL;
432                 f->prev_ftran = NULL;
433         } else {
434                 add_to_linked_list((struct list_element *)first_ftran,
435                                    (struct list_element *)f);
436         }
437
438 #if HAVE_MMAP
439         f->file_data = NULL;
440 #endif
441         f->owner = (struct conn * const)c;
442         f->sock = sock;
443         f->state = 0;
444         f->local_file = -1;
445
446 #if WANT_DCACHE
447         f->dir_cache = NULL;
448 #endif
449
450         f->dir_listing = 0;
451         return f;
452 }
453
454 /*
455  * destroy_conn():
456  *              Destroy a control connection, remove it from the linked
457  *              list, and clean up after it.
458  */
459 void destroy_conn(struct conn * const c)
460 {
461         if (c == NULL) return;
462         del_fd(c->sock);
463
464         destroy_ftran(c->transfer);
465         remove_from_linked_list((struct list_element *)c);
466 }
467
468 /*
469  * destroy_ftran():
470  *              Destroy a data connection, remove it from the linked list,
471  *              and clean up after it.
472  *
473  *              For some reason, TCP_CORK (Linux 2.2.x-only) doesn't flush
474  *              even _after_ the socket is closed, so we zero it just before
475  *              closing. We also zero just before sending the last packet,
476  *              as it seems to be needed on some systems.
477  *
478  *              If you wonder why I check for `defined(SOL_TCP)' and don't
479  *              provide an alternative, see the comments on init_file_transfer().
480  */
481 void destroy_ftran(struct ftran * const f)
482 {
483         const unsigned int zero = 0;
484
485         if (f == NULL) return;
486 #if defined(TCP_CORK) && defined(SOL_TCP)
487         setsockopt(f->sock, SOL_TCP, TCP_CORK, (void *)&zero, sizeof(zero));
488 #endif
489         del_fd(f->sock);
490
491 #if WANT_DCACHE
492         if (f->dir_cache) {
493                 time(&(f->dir_cache->last_used));
494                 f->dir_cache->use_count--;
495                 f->dir_cache = NULL;
496         } else
497 #endif
498 #if HAVE_MMAP
499                 if (f->file_data) {
500                         if (f->dir_listing) {
501                                 free(f->file_data);
502                                 f->file_data = NULL;
503                         } else {
504                                 munmap(f->file_data, f->size);
505                         }
506                 }
507
508         if (!f->dir_listing)
509 #endif
510                 if (f->local_file != -1) close(f->local_file);
511
512 #if !HAVE_MMAP
513         if (f->dir_listing) unlink(f->filename);
514 #endif
515
516         f->owner->transfer = NULL;
517
518 #if WANT_DCACHE
519         if (f->dir_cache != NULL) f->dir_cache->use_count--;
520 #endif
521
522         remove_from_linked_list((struct list_element *)f);
523 }
524
525 #if WANT_DCACHE
526 /*
527  * destroy_dcache():
528  *              Destroy a directory listing cache entry, remove it from the
529  *              linked list, and clean up after it.
530  *
531  *              If you free a cache entry that is in use (use_count > 0),
532  *              BetaFTPD will most likely crash (later). The thing you're supposed
533  *              to do when you're done with a dcache entry, is to decrement
534  *              its use_count, and let the timeout functions do the destroying
535  *              when it's time to do so.
536  */
537 void destroy_dcache(struct dcache * const d)
538 {
539         if (d == NULL) return;
540
541         if (d->dir_data != NULL) free(d->dir_data);
542         remove_from_linked_list((struct list_element *)d);
543 }
544 #endif
545
546 /*
547  * process_all_clients():
548  *              Processes all the _control_ connections in active_clients
549  *              (normally returned from a select(), there are at max
550  *              NUM_AC active connections in the set), sending them
551  *              through to the command parser if a command has been
552  *              entered.
553  */
554 #if HAVE_POLL
555 int process_all_clients(const int num_ac)
556 #else
557 int process_all_clients(const fd_set * const active_clients, const int num_ac)
558 #endif
559 {
560         struct conn *c = NULL, *next = first_conn->next_conn;
561         int checked_through = 0;
562
563         /* run through the linked list */
564         while (next != NULL && checked_through < num_ac) {
565                 int bytes_avail;
566
567                 c = next;
568                 next = c->next_conn;
569 #if HAVE_POLL
570                 if ((fds[c->sock].revents & (POLLIN|POLLERR|POLLHUP|POLLNVAL)) == 0) {
571                         continue;
572                 }
573 #else
574                 if (!FD_ISSET(c->sock, active_clients)) {
575                         continue;
576                 }
577 #endif
578
579                 checked_through++;
580
581                 bytes_avail = recv(c->sock, c->recv_buf + c->buf_len,
582                                    255 - c->buf_len, 0);
583                 if (bytes_avail <= 0) {
584                         /*
585                          * select() has already told us there's something about
586                          * this socket, so if we get a return value of zero, the
587                          * client has closed the socket. If we get a return value
588                          * of -1 (error), we close the socket ourselves.
589                          *
590                          * We do the same for poll(), even though we actually have
591                          * bits that tell us what is happening (in case of new 
592                          * input AND error/hangup at the same time, we do an
593                          * explicit check at the bottom of the loop as well).
594                          */
595                         destroy_conn(c);
596                         continue;
597                 }
598
599                 /* overrun = disconnect */
600                 if (c->buf_len + bytes_avail > 254) {
601                         numeric(c, 503, "Buffer overrun; disconnecting.");
602                         destroy_conn(c);
603                         continue;
604                 }
605
606                 c->buf_len += bytes_avail;
607                 parse_command(c);
608
609                 if (fds[c->sock].revents & (POLLERR|POLLHUP|POLLNVAL)) {
610                         destroy_conn(c);
611                 }
612         }
613         return checked_through;
614 }
615
616 /*
617  * process_all_sendfiles():
618  *              Sends data to all clients that are ready to receive it.
619  *              Also checks for data connections that are newly-connected,
620  *              and handler xferlog entries for the files that are finished.
621  */
622 #if HAVE_POLL
623 int process_all_sendfiles(const int num_ac)
624 #else
625 int process_all_sendfiles(fd_set * const active_clients, const int num_ac)
626 #endif
627 {
628         struct ftran *f = NULL, *next = first_ftran->next_ftran;
629         int checked_through = 0;
630         struct sockaddr tempaddr;
631         int tempaddr_len = sizeof(tempaddr);
632  
633         while (next != NULL && checked_through < num_ac) {
634                 f = next;
635                 next = f->next_ftran;
636
637 #if HAVE_POLL
638                 if (fds[f->sock].revents & (POLLHUP|POLLERR|POLLNVAL)) {
639                         destroy_ftran(f);
640                         continue;
641                 }
642 #endif
643
644                 /* state = 2: incoming PASV, state >3: send file */
645 #if HAVE_POLL
646                 if ((f->state < 2) || (f->state == 3) ||  (fds[f->sock].revents & (POLLIN|POLLOUT)) == 0) {
647 #else
648                 if ((f->state < 2) || (f->state == 3) || !FD_ISSET(f->sock, active_clients)) {
649 #endif
650                         continue;
651                 }
652
653                 checked_through++;
654
655 #if HAVE_POLL
656                 /* Nothing is needed for the poll() version? */
657 #else
658                 FD_CLR(f->sock, active_clients);
659 #endif
660
661                 if (f->state == 2) {            /* incoming PASV */
662                         const unsigned int one = 1;
663                         const int tempsock = accept(f->sock, (struct sockaddr *)&tempaddr,
664                                                         &tempaddr_len);
665
666                         del_fd(f->sock);
667
668                         if (tempsock == -1) {
669                                 destroy_ftran(f);
670                                 continue;
671                         }
672
673                         f->sock = tempsock;
674                         ioctl(f->sock, FIONBIO, &one);
675                         init_file_transfer(f);
676 #if WANT_UPLOAD
677                         if (f->upload) continue;
678 #endif
679                 }
680                 if (f->state < 5) {
681                         init_file_transfer(f);
682 #if WANT_UPLOAD
683                         if (f->upload) continue;
684 #endif
685                 }
686
687                 /* for download, we send the first packets right away */
688 #if WANT_UPLOAD
689                 if (f->upload) {
690                         if (do_upload(f)) continue;
691                 } else
692 #endif
693                         if (do_download(f)) continue;
694
695                 /* do_{upload,download} returned 0, the transfer is complete */
696                 numeric(f->owner, 226, "Transfer complete.");
697                 time(&(f->owner->last_transfer));
698
699 #if WANT_XFERLOG
700                 if (!f->dir_listing) {
701                         write_xferlog(f);
702                 }
703 #endif
704
705                 destroy_ftran(f);
706 #if WANT_FULLSCREEN
707                 update_display(first_conn);
708 #endif
709         }
710
711         return checked_through;
712 }
713
714 #if WANT_UPLOAD
715 int do_upload(struct ftran *f)
716 {
717         char upload_buf[16384];
718         int size;
719 #if WANT_ASCII
720         /* keep buffer size small in ascii transfers 
721            to prevent process stalling while filtering
722            data on slower computers */
723
724         /* 
725          * This isn't a big problem, since we won't get
726          * packets this big anyway, the biggest I've seen
727          * was 12kB on 100mbit (but that was from a Windows
728          * machine), so I've reduced the buffer from 64 kB
729          * to 16 kB :-) --Steinar
730          */
731         const int maxlen = (f->ascii_mode == 1) ? 4096 : 16384;
732 #else
733         const int maxlen = 16384;
734 #endif
735
736         errno = 0;
737         size = recv(f->sock, upload_buf, maxlen, 0);
738         if (size >= 0) {
739                 f->pos += size;
740         }
741 #if WANT_ASCII
742         if (size > 0 && f->ascii_mode == 1) {
743                 size = ascii_uploadfilter(upload_buf, size);
744         }
745 #endif
746         if (size > 0 && (write(f->local_file, upload_buf, size) == size)) {
747                 return 1;
748         } else if (size == -1) {
749                 /* don't write xferlog... or? */
750                 numeric(f->owner, 426, strerror(errno));
751                 destroy_ftran(f);
752                 return 1;
753         }
754         return 0;
755
756 #endif
757
758 int do_download(struct ftran *f)
759 {
760 #if defined(TCP_CORK) && defined(SOL_TCP)
761         unsigned int zero = 0;
762 #endif
763         char *sendfrom_buf;
764         int bytes_to_send;
765         int more_to_send = 0;
766
767 #if !HAVE_MMAP
768         char buf[MAX_BLOCK_SIZE];
769 #endif
770 #if WANT_ASCII
771         char buf2[MAX_BLOCK_SIZE * 2];
772 #endif
773         int size;
774
775 #if HAVE_LINUX_SENDFILE
776         /*
777          * We handle the optimal case first, which is sendfile().
778          * Here we use a rather simplified sending `algorithm',
779          * leaving most of the quirks to the system calls.
780          */
781         if (sendfile_supported == 1 && f->dir_listing == 0) {
782                 int err;
783                 size = f->size - f->pos;
784
785                 if (size > f->block_size) size = f->block_size;
786                 if (size < 0) size = 0;
787
788 #ifdef TCP_CORK
789                 if (size != f->block_size) {
790                         setsockopt(f->sock, SOL_TCP, TCP_CORK, (void *)&zero, sizeof(zero));
791                 }       
792 #endif
793
794                 err = sendfile(f->sock, f->local_file, &f->pos, size);
795                 return (f->pos < f->size) && (err > -1);
796         }
797 #endif
798
799 #if HAVE_MMAP
800         size = f->size - f->pos;
801
802         if (size > f->block_size) size = f->block_size;
803         if (size < 0) size = 0;
804
805         bytes_to_send = size;
806         sendfrom_buf = f->file_data + f->pos;
807 #else
808         bytes_to_send = read(f->local_file, buf, f->block_size);
809         sendfrom_buf = buf;
810 #endif
811
812         if (bytes_to_send == f->block_size) more_to_send = 1;
813
814 #if WANT_ASCII
815         if (f->ascii_mode == 1) {
816                 bytes_to_send = ascii_downloadfilter(sendfrom_buf,
817                                                      buf2, bytes_to_send);
818                 sendfrom_buf = buf2;
819         }
820 #endif /* WANT_ASCII */
821
822 #if defined(TCP_CORK) && defined(SOL_TCP)
823         /* if we believe this is the last packet, unset TCP_CORK */
824         if (more_to_send == 0) {
825                 setsockopt(f->sock, SOL_TCP, TCP_CORK, (void *)&zero, sizeof(zero));
826         }
827 #endif
828
829         size = send(f->sock, sendfrom_buf, bytes_to_send, 0);
830         if (size < bytes_to_send) more_to_send = 1;
831
832 #if WANT_ASCII
833         if (f->ascii_mode == 1 && size < bytes_to_send && size > 0) {
834                 size = ascii_findlength(sendfrom_buf, size);
835         }
836 #endif
837
838 #if HAVE_MMAP
839         if (size > 0) f->pos += size;
840 #endif
841
842         return more_to_send;
843 }
844
845 #if WANT_XFERLOG
846 void write_xferlog(struct ftran *f)
847 {
848         char temp[256];
849         time_t now = time(NULL);
850         struct tm *t = localtime(&now);
851
852         if (xferlog == NULL) return;
853
854         strftime(temp, 256, "%a %b %d %H:%M:%S %Y", t);
855 #if WANT_UPLOAD
856         fprintf(xferlog, "%s %u %s %lu %s b _ %c a %s ftp 0 * \n",
857 #else
858         fprintf(xferlog, "%s %u %s %lu %s b _ o a %s ftp 0 *\n",
859 #endif
860                 temp, (int)(difftime(now, f->tran_start)),
861                 inet_ntoa(f->sin.sin_addr), f->size,
862                 f->filename,
863 #if WANT_UPLOAD
864                 (f->upload) ? 'i' : 'o',
865 #endif
866                 f->owner->username);
867         fflush(xferlog);
868
869 #if 0
870         /* vim needs this to work properly :-( */
871         )
872 #endif
873 }
874 #endif
875
876 #if 0
877 /* Reallocate the buggers constantly */
878 void screw_clients()
879 {
880         struct conn *c = first_conn;
881         int maxloops = MAXCLIENTS;
882
883         while (c && c->next_conn) {
884                 struct conn *temp = malloc(sizeof(*temp));
885                 if (!temp) break;
886                 *temp = *(c->next_conn);
887                 if (temp->transfer) temp->transfer->owner = temp;
888                 memset(c->next_conn, 0, sizeof(struct conn));
889                 free(c->next_conn);
890                 temp->prev_conn = c;
891                 c->next_conn = temp;
892                 c = c->next_conn;
893                 maxloops--;
894                 assert(maxloops > 0);
895         }
896 }
897 #endif
898
899 /*
900  * main():      Main function. Does the initialization, and contains
901  *              the main server loop. Takes no command-line arguments
902  *              (see README for justification).
903  */
904 int main(void)
905 {
906         int server_sock;
907
908 #if HAVE_POLL
909         /* the sets are declared globally if we use poll() */
910 #else
911         fd_set fds, fds_send;
912 #endif
913
914         /*setlinebuf(stdout);*/
915         setvbuf(stdout, (char *)NULL, _IOLBF, 0); 
916
917         signal(SIGPIPE, SIG_IGN);
918
919         printf("BetaFTPD version %s, Copyright (C) 1999-2000 Steinar H. Gunderson\n", VERSION);
920         puts("BetaFTPD comes with ABSOLUTELY NO WARRANTY; for details see the file");
921         puts("COPYING. This is free software, and you are welcome to redistribute it");
922         puts("under certain conditions; again see the file COPYING for details.");
923         puts("");
924
925         /* we don't need stdin */
926         close(0);
927
928 #if HAVE_POLL
929         {
930                 int i;
931                 for (i = 0; i < FD_MAX; i++) {
932                         fds[i].fd = -1;
933                         fds[i].events = 0;
934                 }
935         }
936 #else
937         FD_ZERO(&master_fds);
938         FD_ZERO(&master_send_fds);
939 #endif
940
941         server_sock = create_server_socket();
942
943 #if WANT_FULLSCREEN
944         printf("%cc", (char)27);        /* reset and clear the screen */
945 #endif
946
947         /* init dummy first connection */
948         first_conn = alloc_new_conn(-1);
949         first_ftran = alloc_new_ftran(0, NULL);
950 #if WANT_DCACHE
951         first_dcache = alloc_new_dcache();
952 #endif
953
954 #if WANT_XFERLOG
955 #if WANT_NONROOT
956 #warning No xferlog support for nonroot yet
957 #else
958         /* open xferlog */
959         xferlog = fopen("/var/log/xferlog", "r+");
960         if (xferlog == NULL) xferlog = fopen("/usr/adm/xferlog", "r+");
961
962         if (xferlog != NULL) {
963                  fseek(xferlog, 0L, SEEK_END);
964         }
965 #endif
966 #endif
967
968 #if WANT_FORK
969         switch (fork()) {
970         case -1:
971                 perror("fork()");
972                 puts("fork() failed, exiting");
973                 exit(0);
974         case 0:
975                 break;
976         default:
977                 puts("BetaFTPD forked into the background");
978                 exit(0);
979         }
980 #else
981         puts("BetaFTPD active");
982 #endif
983
984         /* set timeout alarm here (after the fork) */
985         alarm(60);
986         signal(SIGALRM, handle_alarm);
987
988 #if HAVE_LINUX_SENDFILE
989         /* check that sendfile() is really implemented (same check as configure does) */
990         {
991                 int out_fd = 1, in_fd = 0;
992                 off_t offset = 0;
993                 size_t size = 1024;
994
995                 errno = 0;
996                 sendfile(out_fd, in_fd, &offset, size);
997                 if (errno == ENOSYS) sendfile_supported = 0;
998         }
999 #endif
1000
1001         for ( ;; ) {
1002                 int i;
1003 #ifndef HAVE_POLL
1004                 struct timeval timeout;
1005 #endif
1006
1007                 /*screw_clients();       //look for memory errors */
1008
1009 #if WANT_FULLSCREEN
1010                 update_display(first_conn);
1011 #endif
1012
1013 #if HAVE_POLL
1014                 i = poll(fds, highest_fds + 1, 60000);
1015 #if 0
1016                 {
1017                         int j;
1018                         for (j=0; j<=highest_fds; j++) {
1019                                 if (fds[j].revents) printf("fds[%d].fd %d, .revents %x\n", j, fds[j].fd, fds[j].revents);
1020                         }
1021                 }
1022 #endif
1023 #else
1024                 /* reset fds (gets changed by select()) */
1025                 fds = master_fds;
1026                 fds_send = master_send_fds;
1027
1028                 /*
1029                  * wait up to 60 secs for any activity 
1030                  */
1031                 timeout.tv_sec = 60;
1032                 timeout.tv_usec = 0;
1033
1034                 i = select(FD_SETSIZE, &fds, &fds_send, NULL, &timeout);
1035 #endif
1036
1037                 if (i == -1) {
1038                         if (errno == EBADF) {
1039 #if !HAVE_POLL
1040                                 /* don't like this, but we have to */
1041                                 clear_bad_fds(&server_sock);
1042 #endif
1043                         } else if (errno != EINTR) {
1044 #if HAVE_POLL
1045                                 perror("poll()");
1046 #else
1047                                 perror("select()");
1048 #endif
1049                                 continue;
1050                         }
1051                 }
1052
1053 #if HAVE_POLL
1054                 /* fix an invalid server socket */
1055                 if (fds[server_sock].revents & POLLERR) {
1056                         del_fd(server_sock);
1057                         server_sock = create_server_socket();
1058                 }
1059 #endif
1060
1061                 /* remove any timed out sockets */
1062                 if (time_to_check) {
1063                         time_out_sockets();
1064 #if WANT_DCACHE
1065                         time_out_dcache();
1066 #endif
1067                         time_to_check = 0;
1068                 }
1069
1070                 if (i <= 0) continue;
1071
1072 #if HAVE_POLL
1073                 i -= process_all_sendfiles(i);
1074                 process_all_clients(i);
1075 #else
1076                 /* sends are given highest `priority' */
1077                 i -= process_all_sendfiles(&fds_send, i);
1078
1079                 /* incoming PASV connections and uploads */
1080                 i -= process_all_sendfiles(&fds, i);
1081
1082                 /*
1083                  * check the incoming PASV connections first, so
1084                  * process_all_clients() won't be confused.
1085                  */ 
1086                 process_all_clients(&fds, i);
1087 #endif
1088
1089 #if HAVE_POLL
1090                 if (fds[server_sock].revents & POLLIN) {
1091 #else
1092                 if (FD_ISSET(server_sock, &fds)) {
1093 #endif
1094                         accept_new_client(&server_sock);
1095                         i--;
1096                 }
1097         }
1098 }
1099
1100 /*
1101  * accept_new_client():
1102  *              Open a socket for the new client, say hello and put it in
1103  *              among the others.
1104  */
1105 void accept_new_client(int * const server_sock)
1106 {
1107         struct sockaddr_in tempaddr;
1108         int tempaddr_len = sizeof(tempaddr);
1109         const int tempsock = accept(*server_sock, (struct sockaddr *)&tempaddr, &tempaddr_len);
1110
1111         static int num_err = 0;
1112
1113         if (tempsock < 0) {
1114 #ifndef WANT_FORK
1115                 perror("accept()");
1116 #endif
1117                 close(tempsock);
1118                 if ((errno == EBADF || errno == EPIPE) && ++num_err >= 3) {
1119                         del_fd(*server_sock);
1120                         *server_sock = create_server_socket();
1121                 }
1122         } else {
1123                 struct conn * const c = alloc_new_conn(tempsock);
1124                 num_err = 0;
1125                 if (c != NULL) {
1126                         numeric(c, 220, "BetaFTPD " VERSION " ready.");
1127 #if WANT_STAT
1128                         memcpy(&(c->addr), &tempaddr, sizeof(struct sockaddr));
1129 #endif
1130                 }
1131         }
1132 }
1133
1134 /*
1135  * time_out_sockets():
1136  *              Times out any socket that has not had any transfer
1137  *              in the last 15 minutes (delay not customizable by FTP
1138  *              user -- you must change it in ftpd.h).
1139  *
1140  *              Note that RFC959 explicitly states that there are no
1141  *              `spontaneous' error replies, yet we have to do it to
1142  *              get the message through at all.
1143  *
1144  *              If we check this list for every accept() call, it's
1145  *              actually eating a lot of CPU time, so we only check
1146  *              it every minute. We used to do a time() call here,
1147  *              but we've changed to do use an alarm() call and set
1148  *              the time_to_check_flag in the SIGALRM handler.
1149  */
1150 RETSIGTYPE handle_alarm(int signum)
1151 {
1152         time_to_check = 1;
1153         alarm(60);
1154
1155         /* for libc5 */
1156         signal(SIGALRM, handle_alarm);
1157 }
1158
1159 void time_out_sockets()
1160 {
1161         struct conn *c = NULL, *next = first_conn->next_conn;
1162         time_t now = time(NULL);  
1163
1164         /* run through the linked list */
1165         while (next != NULL) {
1166                 c = next;
1167                 next = c->next_conn;
1168
1169                 if ((c->transfer == NULL || c->transfer->state != 5) &&
1170                     (now - c->last_transfer > TIMEOUT_SECS)) {
1171                         /* RFC violation? */
1172                         numeric(c, 421, "Timeout (%u minutes): Closing control connection.", TIMEOUT_SECS/60);
1173                         destroy_conn(c);
1174                 }
1175         }
1176 }
1177
1178 /*
1179  * remove_bytes():
1180  *              Remove some bytes from the incoming buffer. This gives
1181  *              room for new data on the control connection, and should
1182  *              be called when the code has finished using the data.
1183  *              (This is done automatically for all commands, so you
1184  *              normally need not worry about it.)
1185  */
1186 void remove_bytes(struct conn * const c, const int num)
1187 {
1188         if (c->buf_len <= num) {
1189                 c->buf_len = 0;
1190         } else {
1191                 c->buf_len -= num;
1192                 memmove(c->recv_buf, c->recv_buf + num, c->buf_len);
1193         }
1194 }
1195
1196 /*
1197  * numeric():   Sends a numeric FTP reply to the client. Note that
1198  *              you can use this command much the same way as you
1199  *              would use a printf() (with all the normal %s, %d,
1200  *              etc.), since it actually uses printf() internally.
1201  */
1202 void numeric(struct conn * const c, const int numeric, const char * const format, ...)
1203 {
1204         char buf[256], fmt[256];
1205         va_list args;
1206         int i, err;
1207
1208         snprintf(fmt, 256, "%03u %s\r\n", numeric, format);
1209
1210         va_start(args, format);
1211         i = vsnprintf(buf, 256, fmt, args);
1212         va_end(args);
1213
1214         err = send(c->sock, buf, i, 0);
1215         if (err == -1 && errno == EPIPE) {
1216                 destroy_conn(c);
1217         }
1218 }
1219
1220 /*
1221  * init_file_transfer():
1222  *              Initiate a data connection for sending. This does not open
1223  *              any files etc., just does whatever is needed for the socket,
1224  *              if needed. It does, however, send the 150 reply to the client,
1225  *              and mmap()s if needed.
1226  *
1227  *              Linux systems (others?) define SOL_TCP right away, which saves us
1228  *              some grief and code size. Perhaps using getprotoent() is the `right'
1229  *              way, but it's bigger :-) (Optionally, we could figure it out at
1230  *              configure time, of course...)
1231  *
1232  *              For optimal speed, we use the Linux 2.2.x-only TCP_CORK flag if
1233  *              possible. Note that this is only defined in the first `arm' --
1234  *              we silently assume that Linux is the only OS supporting this
1235  *              flag. This might be an over-generalization, but I it looks like
1236  *              we'll have to depend on it other places as well, so we might
1237  *              just as well be evil here.
1238  */
1239 void init_file_transfer(struct ftran * const f)
1240 {
1241         struct linger ling;
1242         struct conn * const c = f->owner;
1243         const int mode = IPTOS_THROUGHPUT, zero = 0, one = 1;
1244         struct stat buf;
1245         int events;
1246
1247 #ifdef SOL_TCP
1248         /* we want max throughput */
1249         setsockopt(f->sock, SOL_IP, IP_TOS, (void *)&mode, sizeof(mode));
1250         setsockopt(f->sock, SOL_TCP, TCP_NODELAY, (void *)&zero, sizeof(zero));
1251 #ifdef TCP_CORK
1252         setsockopt(f->sock, SOL_TCP, TCP_CORK, (void *)&one, sizeof(one));
1253 #endif
1254 #else
1255         /* should these pointers be freed afterwards? */
1256         {
1257                 getprotoent();  /* legal? */
1258                 {
1259                         const struct protoent * const pe_ip  = getprotobyname("ip");
1260                         const struct protoent * const pe_tcp = getprotobyname("tcp");
1261                         setsockopt(f->sock, pe_ip->p_proto, IP_TOS, (void *)&mode, sizeof(mode));
1262                         setsockopt(f->sock, pe_tcp->p_proto, TCP_NODELAY, (void *)&zero, sizeof(zero));
1263                 }
1264                 endprotoent();
1265         }
1266 #endif
1267
1268         if (f->dir_listing) {
1269                 f->block_size = MAX_BLOCK_SIZE;
1270         } else {
1271 #if WANT_ASCII
1272                 f->ascii_mode = f->owner->ascii_mode;
1273 #endif
1274
1275                 /* find the preferred block size */
1276                 f->block_size = MAX_BLOCK_SIZE;
1277                 if (fstat(f->local_file, &buf) != -1 &&
1278                     buf.st_blksize < MAX_BLOCK_SIZE) {
1279                         f->block_size = buf.st_blksize;
1280                 }
1281         }
1282
1283         f->state = 5;
1284
1285         events = POLLOUT;
1286 #if WANT_UPLOAD
1287         if (f->upload) {
1288                 events = POLLIN;
1289         }
1290 #endif /* WANT_UPLOAD */
1291
1292         TRAP_ERROR(add_fd(f->sock, events), 500, return);
1293
1294         ling.l_onoff = 0;
1295         ling.l_linger = 0;
1296         setsockopt(f->sock, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling));
1297
1298 #if !HAVE_POLL && WANT_UPLOAD
1299         /*
1300          * if we let an upload socket stay in master_send_fds, we would
1301          * get data that would fool us into closing the socket... (sigh)
1302          */
1303         if (f->upload) {
1304                 FD_CLR(f->sock, &master_send_fds);
1305                 FD_SET(f->sock, &master_fds);
1306         }
1307 #endif
1308
1309         time(&(f->owner->last_transfer));
1310         
1311         if (f->dir_listing) {
1312                 /* include size? */
1313                 numeric(f->owner, 150, "Opening ASCII mode data connection for directory listing.");
1314         } else {
1315                 /*
1316                  * slightly kludged -- perhaps we should kill the second arm,
1317                  * at the expense of code size? Or perhaps we could collapse
1318                  * the two possible replies into one?
1319                  */
1320 #if WANT_ASCII
1321                 if (f->ascii_mode
1322 #if WANT_UPLOAD
1323                         || f->upload
1324 #endif /* WANT_UPLOAD */
1325                 ) {
1326                         numeric(f->owner, 150, "Opening %s mode data connection for '%s'",
1327                                 (f->ascii_mode) ? "ASCII" : "BINARY", f->filename);
1328                 } else {
1329                         numeric(f->owner, 150, "Opening %s mode data connection for '%s' (%u bytes)",
1330                                 (f->ascii_mode) ? "ASCII" : "BINARY", f->filename,
1331                                 f->size); 
1332                 }
1333 #else /* !WANT_ASCII */
1334 #if WANT_UPLOAD
1335                 if (f->upload) {
1336                         numeric(f->owner, 150, "Opening BINARY mode data connection for '%s'", f->filename);
1337                 } else
1338 #endif /* WANT_UPLOAD */
1339                         numeric(f->owner, 150, "Opening BINARY mode data connection for '%s' (%u bytes)", f->filename, f->size);
1340 #endif /* !WANT_ASCII */
1341         }
1342
1343         /*
1344          * This section _could_ in theory be more optimized, but it's
1345          * much easier this way, and hopefully, the compiler will be
1346          * intelligent enough to optimize most of this away. The idea
1347          * is, some modes _require_ use of mmap (or not). The preferred
1348          * thing is using mmap() when we don't have sendfile(), and not
1349          * using mmap() when we have sendfile().
1350          */
1351 #if HAVE_MMAP
1352         if (f->dir_listing == 0) {
1353 #if HAVE_LINUX_SENDFILE
1354                 int do_mmap = (sendfile_supported) ? 0 : 1;
1355 #else
1356                 int do_mmap = 1;
1357 #endif
1358 #if WANT_ASCII
1359                 if (f->ascii_mode == 1) do_mmap = 1;
1360 #endif
1361 #if WANT_UPLOAD
1362                 if (f->upload == 1) do_mmap = 0;
1363 #endif
1364  
1365                 if (do_mmap == 1) {
1366                         f->file_data = mmap(NULL, f->size, PROT_READ, MAP_SHARED, f->local_file, 0);
1367                         if (f->file_data == MAP_FAILED) f->file_data = NULL;
1368                 } else {
1369                         f->file_data = NULL;
1370                 }
1371                 f->pos = f->owner->rest_pos;
1372         }
1373 #else /* !HAVE_MMAP */
1374         lseek(f->local_file, f->owner->rest_pos, SEEK_SET);
1375 #endif
1376 }
1377
1378 /*
1379  * create_server_socket():
1380  *              Create and bind a server socket, that we can use to
1381  *              listen to new clients on.
1382  */
1383 int create_server_socket()
1384 {
1385         int server_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1386         const unsigned int one = 1;
1387         struct sockaddr_in addr;
1388         int err;
1389         
1390         /*
1391          * In the `perfect' world, if an address was in use, we could
1392          * just wait for the kernel to clear everything up, and everybody
1393          * would be happy. But when you just found out your server socket
1394          * was invalid, it has to be `re-made', and 3000 users are trying
1395          * to access your fileserver, I think it's nice that it comes
1396          * up right away... hence this option.
1397          */
1398         setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
1399         ioctl(server_sock, FIONBIO, &one);      /* just in case */
1400
1401         addr.sin_family = AF_INET;
1402         addr.sin_addr.s_addr = INADDR_ANY;
1403         addr.sin_port = htons(FTP_PORT);
1404
1405         do {
1406                 err = bind(server_sock, (struct sockaddr *)&addr, sizeof(struct sockaddr));
1407
1408                 if (err == -1) {
1409                         perror("bind()");
1410                 
1411                         /* try to recover from recoverable errors... */
1412                         if (errno == ENOMEM || errno == EADDRINUSE) {
1413                                 puts("Waiting 1 sec before trying again...");
1414                                 sleep(1);
1415                         } else {
1416                                 puts("Giving up.");
1417                                 exit(1); 
1418                         }
1419                 }
1420         } while (err == -1);
1421
1422         listen(server_sock, 20);
1423
1424         err = add_fd(server_sock, POLLIN);
1425         if (err) {
1426                 perror("add_fd");
1427                 return -1;
1428         }
1429
1430         return server_sock;
1431 }
1432
1433 #if !HAVE_POLL
1434 /*
1435  * clear_bad_fds():
1436  *              Try to find invalid socket descriptors, and clean them.
1437  *              The methods used are rather UGLY, but I can't think of
1438  *              any good way of checking e.g. server_sock without
1439  *              doing anything to it :-(
1440  *
1441  *              poll() is able to do this in a much cleaner way, which 
1442  *              we use if we use poll(). That checking isn't done here,
1443  *              though.
1444  */
1445 void clear_bad_fds(int * const server_sock)
1446 {
1447         {
1448                 fd_set fds;
1449                 struct timeval tv = { 0, 0 };
1450
1451                 FD_ZERO(&fds);
1452                 FD_SET(*server_sock, &fds); 
1453                 if (select(*server_sock, &fds, NULL, NULL, &tv) == -1) {
1454                         FD_CLR(*server_sock, &master_fds);
1455                         close(*server_sock);
1456                         *server_sock = create_server_socket();
1457                 }
1458         }
1459
1460         /* could do this (conn, ftran) in any order */
1461         {
1462                 struct conn *c = NULL, *next = first_conn->next_conn;
1463         
1464                 /* run through the linked list */
1465                 while (next != NULL) {
1466                         char buf[1];
1467
1468                         c = next;
1469                         next = c->next_conn;
1470
1471                         if (read(c->sock, &buf, 0) == -1 &&
1472                             errno == EBADF) {
1473                                 destroy_conn(c);
1474                         }
1475                 }
1476         }
1477
1478         {
1479                 struct ftran *f = NULL, *next = first_ftran->next_ftran;
1480         
1481                 while (next != NULL) {
1482                         char buf[1];
1483
1484                         f = next;
1485                         next = f->next_ftran;
1486
1487                         if (read(f->sock, &buf, 0) == -1 &&
1488                             errno == EBADF) {
1489                                 destroy_ftran(f);
1490                         }
1491                 }
1492         }       
1493 }
1494 #endif
1495
1496 #if WANT_MESSAGE
1497 /*
1498  * dump_file(): Dumps a file on the control connection. Used for
1499  *              welcome messages and the likes. Note that outbuf
1500  *              is so big, to prevent any crashing from users creating
1501  *              weird .message files (like 1024 LFs)... The size of
1502  *              the file is limited to 1024 bytes (by truncation).
1503  */
1504 void dump_file(struct conn * const c, const int num, const char * const filename)
1505 {
1506         char buf[1024], outbuf[5121];
1507         char *ptr = outbuf + 4;
1508         int i, j = -1;
1509
1510         const int dumpfile = open(filename, O_RDONLY);
1511         if (dumpfile == -1) return;
1512
1513         i = read(dumpfile, buf, 1024);
1514         if (i <= 0) {
1515                 close(dumpfile);
1516                 return;
1517         }
1518
1519         sprintf(outbuf, "%03u-", num);
1520         while (++j < i) {
1521                 *ptr++ = buf[j];
1522                 if (buf[j] == '\n') {
1523                         sprintf(ptr, "%03u-", num);
1524                         ptr += 4;
1525                 }
1526         }
1527         *ptr++ = '\n';
1528
1529         send(c->sock, outbuf, ptr - outbuf, 0);
1530         close(dumpfile);
1531 }
1532
1533
1534 /*
1535  * list_readme():
1536  *              Lists all README file in the current (ie. OS current)
1537  *              directory, in a 250- message.
1538  */
1539 void list_readmes(struct conn * const c)
1540 {
1541         glob_t pglob;
1542         const time_t now = time(NULL);
1543         int i;
1544
1545         if (glob("README*", 0, NULL, &pglob) != 0) return;
1546
1547         for (i = 0; i < pglob.gl_pathc; i++) {
1548                 struct stat buf;
1549                 char str[256];
1550                 char *tm;
1551
1552                 if (stat(pglob.gl_pathv[i], &buf) == -1) continue;
1553
1554                 /* remove trailing LF */
1555                 tm = ctime(&buf.st_mtime);
1556                 tm[strlen(tm) - 1] = 0;
1557
1558                 snprintf(str, 256, "250-Please read the file %s\r\n"
1559                                    "250-\tIt was last modified %s - %ld days ago\r\n",
1560                         pglob.gl_pathv[i], tm,
1561                         (now - buf.st_mtime) / 86400);
1562                 send(c->sock, str, strlen(str), 0);
1563         }
1564         globfree(&pglob);
1565 }
1566 #endif
1567