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