Debian Codegraph @

WWW/Library/Implementation/HTFTP.c

raw
1 /*
2 * $LynxId: HTFTP.c,v 1.154 2026/04/19 22:44:53 tom Exp $
3 *
4 * File Transfer Protocol (FTP) Client
5 * for a WorldWideWeb browser
6 * ===================================
7 *
8 * A cache of control connections is kept.
9 *
10 * Note: Port allocation
11 *
12 * It is essential that the port is allocated by the system, rather
13 * than chosen in rotation by us (POLL_PORTS), or the following
14 * problem occurs.
15 *
16 * It seems that an attempt by the server to connect to a port which has
17 * been used recently by a listen on the same socket, or by another
18 * socket this or another process causes a hangup of (almost exactly)
19 * one minute. Therefore, we have to use a rotating port number.
20 * The problem remains that if the application is run twice in quick
21 * succession, it will hang for what remains of a minute.
22 *
23 * Authors
24 * TBL Tim Berners-lee <[email protected]>
25 * DD Denis DeLaRoca 310 825-4580 <[email protected]>
26 * LM Lou Montulli <[email protected]>
27 * FM Foteos Macrides <[email protected]>
28 * History:
29 * 2 May 91 Written TBL, as a part of the WorldWideWeb project.
30 * 15 Jan 92 Bug fix: close() was used for NETCLOSE for control soc
31 * 10 Feb 92 Retry if cached connection times out or breaks
32 * 8 Dec 92 Bug fix 921208 TBL after DD
33 * 17 Dec 92 Anon FTP password now just WWWuser@ suggested by DD
34 * fails on princeton.edu!
35 * 27 Dec 93 (FM) Fixed up so FTP now works with VMS hosts. Path
36 * must be Unix-style and cannot include the device
37 * or top directory.
38 * ?? ??? ?? (LM) Added code to prompt and send passwords for non
39 * anonymous FTP
40 * 25 Mar 94 (LM) Added code to recognize different ftp server types
41 * and code to parse dates and sizes on most hosts.
42 * 27 Mar 93 (FM) Added code for getting dates and sizes on VMS hosts.
43 *
44 * Notes:
45 * Portions Copyright 1994 Trustees of Dartmouth College
46 * Code for recognizing different FTP servers and
47 * parsing "ls -l" output taken from Macintosh Fetch
48 * program with permission from Jim Matthews,
49 * Dartmouth Software Development Team.
50 */
51
52 /*
53 * BUGS: @@@ Limit connection cache size!
54 * Error reporting to user.
55 * 400 & 500 errors are ack'ed by user with windows.
56 * Use configuration file for user names
57 *
58 * Note for portability this version does not use select() and
59 * so does not watch the control and data channels at the
60 * same time.
61 */
62
63 #include <HTUtils.h>
64
65 #include <HTAlert.h>
66
67 #include <HTFTP.h> /* Implemented here */
68 #include <HTTCP.h>
69 #include <HTTP.h>
70 #include <HTFont.h>
71
72 #define REPEAT_PORT /* Give the port number for each file */
73 #define REPEAT_LISTEN /* Close each listen socket and open a new one */
74
75 /* define POLL_PORTS If allocation does not work, poll ourselves.*/
76 #define LISTEN_BACKLOG 2 /* Number of pending connect requests (TCP) */
77
78 #define FIRST_TCP_PORT 1024 /* Region to try for a listening port */
79 #define LAST_TCP_PORT 5999
80
81 #define LINE_LENGTH 256
82
83 #include <HTParse.h>
84 #include <HTAnchor.h>
85 #include <HTFile.h> /* For HTFileFormat() */
86 #include <HTBTree.h>
87 #include <HTChunk.h>
88 #ifndef IPPORT_FTP
89 #define IPPORT_FTP 21
90 #endif /* !IPORT_FTP */
91
92 #include <LYUtils.h>
93 #include <LYGlobalDefs.h>
94 #include <LYStrings.h>
95 #include <LYLeaks.h>
96
97 typedef struct _connection {
98 struct _connection *next; /* Link on list */
99 int socket; /* Socket number for communication */
100 BOOL is_binary; /* Binary mode? */
101 } connection;
102
103 /* Hypertext object building machinery
104 */
105 #include <HTML.h>
106
107 /*
108 * socklen_t is the standard, but there are many pre-standard variants.
109 * This ifdef works around a few of those cases.
110 *
111 * Information was obtained from header files on these platforms:
112 * AIX 4.3.2, 5.1
113 * HPUX 10.20, 11.00, 11.11
114 * IRIX64 6.5
115 * Tru64 4.0G, 4.0D, 5.1
116 */
117 #if defined(SYS_IRIX64)
118 /* IRIX64 6.5 socket.h may use socklen_t if SGI_SOURCE is not defined */
119 # if _NO_XOPEN4 && _NO_XOPEN5
120 # define LY_SOCKLEN socklen_t
121 # elif _ABIAPI
122 # define LY_SOCKLEN int
123 # elif _XOPEN5
124 # if (_MIPS_SIM != _ABIO32)
125 # define LY_SOCKLEN socklen_t
126 # else
127 # define LY_SOCKLEN int
128 # endif
129 # else
130 # define LY_SOCKLEN size_t
131 # endif
132 #elif defined(SYS_HPUX)
133 # if defined(_XOPEN_SOURCE_EXTENDED) && defined(SO_PROTOTYPE)
134 # define LY_SOCKLEN socklen_t
135 # else /* HPUX 10.20, etc. */
136 # define LY_SOCKLEN int
137 # endif
138 #elif defined(SYS_TRU64)
139 # if defined(_POSIX_PII_SOCKET)
140 # define LY_SOCKLEN socklen_t
141 # elif defined(_XOPEN_SOURCE_EXTENDED)
142 # define LY_SOCKLEN size_t
143 # else
144 # define LY_SOCKLEN int
145 # endif
146 #else
147 # define LY_SOCKLEN socklen_t
148 #endif
149
150 #define PUTC(c) (*target->isa->put_character) (target, c)
151 #define PUTS(s) (*target->isa->put_string) (target, s)
152 #define START(e) (*target->isa->start_element) (target, e, NULL, NULL, -1, NULL)
153 #define END(e) (*target->isa->end_element) (target, e, NULL)
154 #define FREE_TARGET (*target->isa->_free) (target)
155 #define ABORT_TARGET (*target->isa->_free) (target)
156
157 #define TRACE_ENTRY(tag, entry_info) \
158 CTRACE((tfp, "HTFTP: %s filename: %s date: %s size: %" PRI_off_t "\n", \
159 tag, \
160 entry_info->filename, \
161 NonNull(entry_info->date), \
162 CAST_off_t(entry_info->size)))
163
164 struct _HTStructured {
165 const HTStructuredClass *isa;
166 /* ... */
167 };
168
169 /* Global Variables
170 * ---------------------
171 */
172 int HTfileSortMethod = FILE_BY_NAME;
173
174 #ifndef DISABLE_FTP /*This disables everything to end-of-file */
175 static char ThisYear[8];
176 static char LastYear[8];
177 static int TheDate;
178 static BOOLEAN HaveYears = FALSE;
179
180 /* Module-Wide Variables
181 * ---------------------
182 */
183 static connection *connections = NULL; /* Linked list of connections */
184 static char response_text[LINE_LENGTH + 1]; /* Last response from ftp host */
185 static connection *control = NULL; /* Current connection */
186 static int data_soc = -1; /* Socket for data transfer =invalid */
187 static char *user_entered_password = NULL;
188 static char *last_username_and_host = NULL;
189
190 /*
191 * Some ftp servers are known to have a broken implementation of RETR. If
192 * asked to retrieve a directory, they get confused and fail subsequent
193 * commands such as CWD and LIST.
194 */
195 static int Broken_RETR = FALSE;
196
197 /*
198 * Some ftp servers are known to have a broken implementation of EPSV. The
199 * server will hang for a long time when we attempt to connect after issuing
200 * this command.
201 */
202 #ifdef INET6
203 static int Broken_EPSV = FALSE;
204 #endif
205
206 typedef enum {
207 GENERIC_SERVER
208 ,MACHTEN_SERVER
209 ,UNIX_SERVER
210 ,VMS_SERVER
211 ,CMS_SERVER
212 ,DCTS_SERVER
213 ,TCPC_SERVER
214 ,PETER_LEWIS_SERVER
215 ,NCSA_SERVER
216 ,WINDOWS_NT_SERVER
217 ,WINDOWS_2K_SERVER
218 ,MS_WINDOWS_SERVER
219 ,MSDOS_SERVER
220 ,APPLESHARE_SERVER
221 ,NETPRESENZ_SERVER
222 ,DLS_SERVER
223 } eServerType;
224
225 static eServerType server_type = GENERIC_SERVER; /* the type of ftp host */
226 static int unsure_type = FALSE; /* sure about the type? */
227 static BOOLEAN use_list = FALSE; /* use the LIST command? */
228
229 static int interrupted_in_next_data_char = FALSE;
230
231 #ifdef POLL_PORTS
232 static PortNumber port_number = FIRST_TCP_PORT;
233 #endif /* POLL_PORTS */
234
235 static BOOL have_socket = FALSE; /* true if master_socket is valid */
236 static LYNX_FD master_socket; /* Listening socket = invalid */
237
238 static char *port_command; /* Command for setting the port */
239 static fd_set open_sockets; /* Mask of active channels */
240 static LYNX_FD num_sockets; /* Number of sockets to scan */
241 static PortNumber passive_port; /* Port server specified for data */
242
243 #define NEXT_CHAR HTGetCharacter() /* Use function in HTFormat.c */
244
245 #define DATA_BUFFER_SIZE 2048
246 static char data_buffer[DATA_BUFFER_SIZE]; /* Input data buffer */
247 static char *data_read_pointer;
248 static char *data_write_pointer;
249
250 #define NEXT_DATA_CHAR next_data_char()
251 static int close_connection(connection * con);
252
253 #ifndef HAVE_ATOLL
254 off_t LYatoll(const char *value)
255 {
256 off_t result = 0;
257
258 while (*value != '\0') {
259 result = (result * 10) + (off_t) (*value++ - '0');
260 }
261 return result;
262 }
263 #endif
264
265 #ifdef LY_FIND_LEAKS
266 /*
267 * This function frees module globals. - FM
268 */
269 static void free_FTPGlobals(void)
270 {
271 FREE(user_entered_password);
272 FREE(last_username_and_host);
273 if (control) {
274 if (control->socket != -1)
275 close_connection(control);
276 FREE(control);
277 }
278 }
279 #endif /* LY_FIND_LEAKS */
280
281 /* PUBLIC HTVMS_name()
282 * CONVERTS WWW name into a VMS name
283 * ON ENTRY:
284 * nn Node Name (optional)
285 * fn WWW file name
286 *
287 * ON EXIT:
288 * returns vms file specification
289 *
290 * Bug: Returns pointer to static -- non-reentrant
291 */
292 char *HTVMS_name(const char *nn,
293 const char *fn)
294 {
295 /* We try converting the filename into Files-11 syntax. That is, we assume
296 * first that the file is, like us, on a VMS node. We try remote (or
297 * local) DECnet access. Files-11, VMS, VAX and DECnet are trademarks of
298 * Digital Equipment Corporation. The node is assumed to be local if the
299 * hostname WITHOUT DOMAIN matches the local one. @@@
300 */
301 static char *vmsname;
302 char *filename = (char *) malloc(strlen(fn) + 1);
303 char *nodename = (char *) malloc(strlen(nn) + 2 + 1); /* Copies to hack */
304 char *second; /* 2nd slash */
305 char *last; /* last slash */
306
307 const char *hostname = HTHostName();
308
309 if (!filename || !nodename)
310 outofmem(__FILE__, "HTVMSname");
311
312 strcpy(filename, fn);
313 strcpy(nodename, ""); /* On same node? Yes if node names match */
314 if (StrNCmp(nn, "localhost", 9)) {
315 const char *p;
316 const char *q;
317
318 for (p = hostname, q = nn;
319 *p && *p != '.' && *q && *q != '.'; p++, q++) {
320 if (TOUPPER(*p) != TOUPPER(*q)) {
321 char *r;
322
323 strcpy(nodename, nn);
324 r = StrChr(nodename, '.'); /* Mismatch */
325 if (r)
326 *r = '\0'; /* Chop domain */
327 strcat(nodename, "::"); /* Try decnet anyway */
328 break;
329 }
330 }
331 }
332
333 second = StrChr(filename + 1, '/'); /* 2nd slash */
334 last = strrchr(filename, '/'); /* last slash */
335
336 if (!second) { /* Only one slash */
337 HTSprintf0(&vmsname, "%s%s", nodename, filename + 1);
338 } else if (second == last) { /* Exactly two slashes */
339 *second = '\0'; /* Split filename from disk */
340 HTSprintf0(&vmsname, "%s%s:%s", nodename, filename + 1, second + 1);
341 *second = '/'; /* restore */
342 } else { /* More than two slashes */
343 char *p;
344
345 *second = '\0'; /* Split disk from directories */
346 *last = '\0'; /* Split dir from filename */
347 HTSprintf0(&vmsname, "%s%s:[%s]%s",
348 nodename, filename + 1, second + 1, last + 1);
349 *second = *last = '/'; /* restore filename */
350 if ((p = StrChr(vmsname, '[')) != NULL) {
351 while (*p != '\0' && *p != ']') {
352 if (*p == '/')
353 *p = '.'; /* Convert dir sep. to dots */
354 ++p;
355 }
356 }
357 }
358 FREE(nodename);
359 FREE(filename);
360 return vmsname;
361 }
362
363 /* Procedure: Read a character from the data connection
364 * ----------------------------------------------------
365 */
366 static int next_data_char(void)
367 {
368 int status;
369
370 if (data_read_pointer >= data_write_pointer) {
371 status = NETREAD(data_soc, data_buffer, DATA_BUFFER_SIZE);
372 if (status == HT_INTERRUPTED)
373 interrupted_in_next_data_char = 1;
374 if (status <= 0)
375 return EOF;
376 data_write_pointer = data_buffer + status;
377 data_read_pointer = data_buffer;
378 }
379 #ifdef NOT_ASCII
380 {
381 char c = *data_read_pointer++;
382
383 return FROMASCII(c);
384 }
385 #else
386 return UCH(*data_read_pointer++);
387 #endif /* NOT_ASCII */
388 }
389
390 /* Close an individual connection
391 *
392 */
393 static int close_connection(connection * con)
394 {
395 connection *scan;
396 int status;
397
398 CTRACE((tfp, "HTFTP: Closing control socket %d\n", con->socket));
399 status = NETCLOSE(con->socket);
400 if (TRACE && status != 0) {
401 #ifdef UNIX
402 CTRACE((tfp, "HTFTP:close_connection: %s", LYStrerror(errno)));
403 #else
404 if (con->socket != INVSOC)
405 HTInetStatus("HTFTP:close_connection");
406 #endif
407 }
408 con->socket = -1;
409 if (connections == con) {
410 connections = con->next;
411 return status;
412 }
413 for (scan = connections; scan; scan = scan->next) {
414 if (scan->next == con) {
415 scan->next = con->next; /* Unlink */
416 if (control == con)
417 control = (connection *) 0;
418 return status;
419 } /*if */
420 } /* for */
421 return -1; /* very strange -- was not on list. */
422 }
423
424 static char *help_message_buffer = NULL; /* global :( */
425
426 static void init_help_message_cache(void)
427 {
428 FREE(help_message_buffer);
429 }
430
431 static void help_message_cache_add(char *string)
432 {
433 if (help_message_buffer)
434 StrAllocCat(help_message_buffer, string);
435 else
436 StrAllocCopy(help_message_buffer, string);
437
438 CTRACE((tfp, "Adding message to help cache: %s\n", string));
439 }
440
441 static char *help_message_cache_non_empty(void)
442 {
443 return (help_message_buffer);
444 }
445
446 static char *help_message_cache_contents(void)
447 {
448 return (help_message_buffer);
449 }
450
451 /* Send One Command
452 * ----------------
453 *
454 * This function checks whether we have a control connection, and sends
455 * one command if given.
456 *
457 * On entry,
458 * control points to the connection which is established.
459 * cmd points to a command, or is zero to just get the response.
460 *
461 * The command should already be terminated with the CRLF pair.
462 *
463 * On exit,
464 * returns: 1 for success,
465 * or negative for communication failure (in which case
466 * the control connection will be closed).
467 */
468 static int write_cmd(const char *cmd)
469 {
470 int status;
471
472 if (!control) {
473 CTRACE((tfp, "HTFTP: No control connection set up!!\n"));
474 return HT_NO_CONNECTION;
475 }
476
477 if (cmd) {
478 CTRACE((tfp, " Tx: %s", cmd));
479 #ifdef NOT_ASCII
480 {
481 char *p;
482
483 for (p = cmd; *p; p++) {
484 *p = TOASCII(*p);
485 }
486 }
487 #endif /* NOT_ASCII */
488 status = (int) NETWRITE(control->socket, cmd, (unsigned) strlen(cmd));
489 if (status < 0) {
490 CTRACE((tfp,
491 "HTFTP: Error %d sending command: closing socket %d\n",
492 status, control->socket));
493 close_connection(control);
494 return status;
495 }
496 }
497 return 1;
498 }
499
500 /*
501 * For each string in the list, check if it is found in the response text.
502 * If so, return TRUE.
503 */
504 static BOOL find_response(HTList *list)
505 {
506 BOOL result = FALSE;
507 HTList *p = list;
508 char *value;
509
510 while ((value = (char *) HTList_nextObject(p)) != NULL) {
511 if (LYstrstr(response_text, value)) {
512 result = TRUE;
513 break;
514 }
515 }
516 return result;
517 }
518
519 /* Execute Command and get Response
520 * --------------------------------
521 *
522 * See the state machine illustrated in RFC959, p57. This implements
523 * one command/reply sequence. It also interprets lines which are to
524 * be continued, which are marked with a "-" immediately after the
525 * status code.
526 *
527 * Continuation then goes on until a line with a matching reply code
528 * an a space after it.
529 *
530 * On entry,
531 * control points to the connection which is established.
532 * cmd points to a command, or is zero to just get the response.
533 *
534 * The command must already be terminated with the CRLF pair.
535 *
536 * On exit,
537 * returns: The first digit of the reply type,
538 * or negative for communication failure.
539 */
540 static int response(const char *cmd)
541 {
542 int result; /* Three-digit decimal code */
543 int continuation_response = -1;
544 int status;
545
546 if ((status = write_cmd(cmd)) < 0)
547 return status;
548
549 do {
550 char *p = response_text;
551
552 for (;;) {
553 int ich = NEXT_CHAR;
554
555 if (((*p++ = (char) ich) == LF)
556 || (p == &response_text[LINE_LENGTH])) {
557
558 char continuation;
559
560 if (interrupted_in_htgetcharacter) {
561 CTRACE((tfp,
562 "HTFTP: Interrupted in HTGetCharacter, apparently.\n"));
563 NETCLOSE(control->socket);
564 control->socket = -1;
565 return HT_INTERRUPTED;
566 }
567
568 *p = '\0'; /* Terminate the string */
569 CTRACE((tfp, " Rx: %s", response_text));
570
571 /* Check for login or help messages */
572 if (!StrNCmp(response_text, "230-", 4) ||
573 !StrNCmp(response_text, "250-", 4) ||
574 !StrNCmp(response_text, "220-", 4))
575 help_message_cache_add(response_text + 4);
576
577 sscanf(response_text, "%d%c", &result, &continuation);
578 if (continuation_response == -1) {
579 if (continuation == '-') /* start continuation */
580 continuation_response = result;
581 } else { /* continuing */
582 if (continuation_response == result &&
583 continuation == ' ')
584 continuation_response = -1; /* ended */
585 }
586 if (result == 220 && find_response(broken_ftp_retr)) {
587 Broken_RETR = TRUE;
588 CTRACE((tfp, "This server is broken (RETR)\n"));
589 }
590 #ifdef INET6
591 if (result == 220 && find_response(broken_ftp_epsv)) {
592 Broken_EPSV = TRUE;
593 CTRACE((tfp, "This server is broken (EPSV)\n"));
594 }
595 #endif
596 break;
597 }
598 /* if end of line */
599 if (interrupted_in_htgetcharacter) {
600 CTRACE((tfp,
601 "HTFTP: Interrupted in HTGetCharacter, apparently.\n"));
602 NETCLOSE(control->socket);
603 control->socket = -1;
604 return HT_INTERRUPTED;
605 }
606
607 if (ich == EOF) {
608 CTRACE((tfp, "Error on rx: closing socket %d\n",
609 control->socket));
610 strcpy(response_text, "000 *** TCP read error on response\n");
611 close_connection(control);
612 return -1; /* End of file on response */
613 }
614 } /* Loop over characters */
615
616 } while (continuation_response != -1);
617
618 if (result == 421) {
619 CTRACE((tfp, "HTFTP: They close so we close socket %d\n",
620 control->socket));
621 close_connection(control);
622 return -1;
623 }
624 if ((result == 255 && server_type == CMS_SERVER) &&
625 (0 == strncasecomp(cmd, "CWD", 3) ||
626 0 == strcasecomp(cmd, "CDUP"))) {
627 /*
628 * Alas, CMS returns 255 on failure to CWD to parent of root. - PG
629 */
630 result = 555;
631 }
632 return result / 100;
633 }
634
635 static int send_cmd_1(const char *verb)
636 {
637 char command[80];
638
639 sprintf(command, "%.*s%c%c", (int) sizeof(command) - 4, verb, CR, LF);
640 return response(command);
641 }
642
643 static int send_cmd_2(const char *verb, const char *param)
644 {
645 char *command = NULL;
646 int status;
647
648 HTSprintf0(&command, "%s %s%c%c", verb, param, CR, LF);
649 status = response(command);
650 FREE(command);
651
652 return status;
653 }
654
655 #define send_cwd(path) send_cmd_2("CWD", path)
656
657 /*
658 * This function should try to set the macintosh server into binary mode. Some
659 * servers need an additional letter after the MACB command.
660 */
661 static int set_mac_binary(eServerType ServerType)
662 {
663 /* try to set mac binary mode */
664 if (ServerType == APPLESHARE_SERVER ||
665 ServerType == NETPRESENZ_SERVER) {
666 /*
667 * Presumably E means "Enable". - KW
668 */
669 return (2 == response("MACB E\r\n"));
670 } else {
671 return (2 == response("MACB\r\n"));
672 }
673 }
674
675 /* This function gets the current working directory to help
676 * determine what kind of host it is
677 */
678
679 static void get_ftp_pwd(eServerType *ServerType, BOOLEAN *UseList)
680 {
681 char *cp;
682
683 /* get the working directory (to see what it looks like) */
684 int status = response("PWD\r\n");
685
686 if (status < 0) {
687 return;
688 } else {
689 cp = StrChr(response_text + 5, '"');
690 if (cp)
691 *cp = '\0';
692 if (*ServerType == TCPC_SERVER) {
693 *ServerType = ((response_text[5] == '/') ?
694 NCSA_SERVER : TCPC_SERVER);
695 CTRACE((tfp, "HTFTP: Treating as %s server.\n",
696 ((*ServerType == NCSA_SERVER) ?
697 "NCSA" : "TCPC")));
698 } else if (response_text[5] == '/') {
699 /* path names beginning with / imply Unix,
700 * right?
701 */
702 if (set_mac_binary(*ServerType)) {
703 *ServerType = NCSA_SERVER;
704 CTRACE((tfp, "HTFTP: Treating as NCSA server.\n"));
705 } else {
706 *ServerType = UNIX_SERVER;
707 *UseList = TRUE;
708 CTRACE((tfp, "HTFTP: Treating as Unix server.\n"));
709 }
710 return;
711 } else if (response_text[strlen(response_text) - 1] == ']') {
712 /* path names ending with ] imply VMS, right? */
713 *ServerType = VMS_SERVER;
714 *UseList = TRUE;
715 CTRACE((tfp, "HTFTP: Treating as VMS server.\n"));
716 } else {
717 *ServerType = GENERIC_SERVER;
718 CTRACE((tfp, "HTFTP: Treating as Generic server.\n"));
719 }
720
721 if ((*ServerType == NCSA_SERVER) ||
722 (*ServerType == TCPC_SERVER) ||
723 (*ServerType == PETER_LEWIS_SERVER) ||
724 (*ServerType == NETPRESENZ_SERVER))
725 set_mac_binary(*ServerType);
726 }
727 }
728
729 /* This function turns MSDOS-like directory output off for
730 * Windows NT servers.
731 */
732
733 static void set_unix_dirstyle(eServerType *ServerType, BOOLEAN *UseList)
734 {
735 char *cp;
736
737 /* This is a toggle. It seems we have to toggle in order to see
738 * the current state (after toggling), so we may end up toggling
739 * twice. - kw
740 */
741 int status = response("SITE DIRSTYLE\r\n");
742
743 if (status != 2) {
744 *ServerType = GENERIC_SERVER;
745 CTRACE((tfp, "HTFTP: DIRSTYLE failed, treating as Generic server.\n"));
746 return;
747 } else {
748 *UseList = TRUE;
749 /* Expecting one of:
750 * 200 MSDOS-like directory output is off
751 * 200 MSDOS-like directory output is on
752 * The following code doesn't look for the full exact string -
753 * who knows how the wording may change in some future version.
754 * If the first response isn't recognized, we toggle again
755 * anyway, under the assumption that it's more likely that
756 * the MSDOS setting was "off" originally. - kw
757 */
758 cp = strstr(response_text + 4, "MSDOS");
759 if (cp && strstr(cp, " off")) {
760 return; /* already off now. */
761 } else {
762 response("SITE DIRSTYLE\r\n");
763 }
764 }
765 }
766
767 #define CheckForInterrupt(msg) \
768 if (status == HT_INTERRUPTED) { \
769 CTRACE((tfp, "HTFTP: Interrupted %s.\n", msg)); \
770 _HTProgress(CONNECTION_INTERRUPTED); \
771 NETCLOSE(control->socket); \
772 control->socket = -1; \
773 return HT_INTERRUPTED; \
774 }
775
776 /* Get a valid connection to the host
777 * ----------------------------------
778 *
779 * On entry,
780 * arg points to the name of the host in a hypertext address
781 * On exit,
782 * returns <0 if error
783 * socket number if success
784 *
785 * This routine takes care of managing timed-out connections, and
786 * limiting the number of connections in use at any one time.
787 *
788 * It ensures that all connections are logged in if they exist.
789 * It ensures they have the port number transferred.
790 */
791 static int get_connection(const char *arg,
792 HTParentAnchor *anchor)
793 {
794 int status;
795 char *command = NULL;
796 connection *con;
797 char *username = NULL;
798 char *password = NULL;
799 static BOOLEAN firstuse = TRUE;
800
801 if (firstuse) {
802 /*
803 * Set up freeing at exit. - FM
804 */
805 #ifdef LY_FIND_LEAKS
806 atexit(free_FTPGlobals);
807 #endif
808 firstuse = FALSE;
809 }
810
811 if (control != NULL) {
812 connection *next = control->next;
813
814 if (control->socket != -1) {
815 NETCLOSE(control->socket);
816 }
817 memset(con = control, 0, sizeof(*con));
818 con->next = next;
819 } else {
820 con = typecalloc(connection);
821 if (con == NULL)
822 outofmem(__FILE__, "get_connection");
823 }
824 con->socket = -1;
825
826 if (isEmpty(arg)) {
827 free(con);
828 return -1; /* Bad if no name specified */
829 }
830
831 /* Get node name:
832 */
833 CTRACE((tfp, "get_connection(%s)\n", arg));
834 {
835 char *p1 = HTParse(arg, "", PARSE_HOST);
836 char *p2 = strrchr(p1, '@'); /* user? */
837 char *pw = NULL;
838
839 if (p2 != NULL) {
840 username = p1;
841 *p2 = '\0'; /* terminate */
842 p1 = p2 + 1; /* point to host */
843 pw = StrChr(username, ':');
844 if (pw != NULL) {
845 *pw++ = '\0';
846 password = HTUnEscape(pw);
847 }
848 if (*username)
849 HTUnEscape(username);
850
851 /*
852 * If the password doesn't exist then we are going to have to ask
853 * the user for it. The only problem is that we don't want to ask
854 * for it every time, so we will store away in a primitive fashion.
855 */
856 if (!password) {
857 char *tmp = NULL;
858
859 HTSprintf0(&tmp, "%s@%s", username, p1);
860 /*
861 * If the user@host is not equal to the last time through or
862 * user_entered_password has no data then we need to ask the
863 * user for the password.
864 */
865 if (!last_username_and_host ||
866 strcmp(tmp, last_username_and_host) ||
867 !user_entered_password) {
868
869 StrAllocCopy(last_username_and_host, tmp);
870 HTSprintf0(&tmp, LY_MSG("Enter password for user %s@%s:"),
871 username, p1);
872 FREE(user_entered_password);
873 user_entered_password = HTPromptPassword(tmp, NULL);
874
875 } /* else we already know the password */
876 password = user_entered_password;
877 FREE(tmp);
878 }
879 }
880
881 if (!username)
882 FREE(p1);
883 } /* scope of p1 */
884
885 status = HTDoConnect(arg, "FTP", IPPORT_FTP, (int *) &con->socket);
886
887 if (status < 0) {
888 if (status == HT_INTERRUPTED) {
889 CTRACE((tfp, "HTFTP: Interrupted on connect\n"));
890 } else {
891 CTRACE((tfp, "HTFTP: Unable to connect to remote host for `%s'.\n",
892 arg));
893 }
894 if (status == HT_INTERRUPTED) {
895 _HTProgress(CONNECTION_INTERRUPTED);
896 status = HT_NOT_LOADED;
897 } else {
898 HTAlert(gettext("Unable to connect to FTP host."));
899 }
900 if (con->socket != -1) {
901 NETCLOSE(con->socket);
902 }
903
904 FREE(username);
905 if (control == con)
906 control = NULL;
907 FREE(con);
908 return status; /* Bad return */
909 }
910
911 CTRACE((tfp, "FTP connected, socket %d control %p\n",
912 con->socket, (void *) con));
913 control = con; /* Current control connection */
914
915 /* Initialise buffering for control connection */
916 HTInitInput(control->socket);
917 init_help_message_cache(); /* Clear the login message buffer. */
918
919 /* Now we log in Look up username, prompt for pw.
920 */
921 status = response(NULL); /* Get greeting */
922 CheckForInterrupt("at beginning of login");
923
924 server_type = GENERIC_SERVER; /* reset */
925 if (status == 2) { /* Send username */
926 char *cp; /* look at greeting text */
927
928 /* don't gettext() this -- incoming text: */
929 if (strlen(response_text) > 4) {
930 if ((cp = strstr(response_text, " awaits your command")) ||
931 (cp = strstr(response_text, " ready."))) {
932 *cp = '\0';
933 }
934 cp = response_text + 4;
935 if (!strncasecomp(cp, "NetPresenz", 10))
936 server_type = NETPRESENZ_SERVER;
937 } else {
938 cp = response_text;
939 }
940 StrAllocCopy(anchor->server, cp);
941
942 status = send_cmd_2("USER", (username && *username)
943 ? username
944 : "anonymous");
945
946 CheckForInterrupt("while sending username");
947 }
948 if (status == 3) { /* Send password */
949 if (non_empty(password)) {
950 HTSprintf0(&command, "PASS %s%c%c", password, CR, LF);
951 } else {
952 /*
953 * No password was given; use mail-address.
954 */
955 const char *the_address;
956 char *user = NULL;
957 const char *host = NULL;
958 char *cp;
959
960 the_address = anonftp_password;
961 if (isEmpty(the_address))
962 the_address = personal_mail_address;
963 if (isEmpty(the_address))
964 the_address = LYGetEnv("USER");
965 if (isEmpty(the_address))
966 the_address = "WWWuser";
967
968 StrAllocCopy(user, the_address);
969 if ((cp = StrChr(user, '@')) != NULL) {
970 *cp++ = '\0';
971 if (*cp == '\0')
972 host = HTHostName();
973 else
974 host = cp;
975 } else {
976 host = HTHostName();
977 }
978
979 /*
980 * If host is not fully qualified, suppress it
981 * as ftp.uu.net prefers a blank to a bad name
982 */
983 if (!(host) || StrChr(host, '.') == NULL)
984 host = "";
985
986 HTSprintf0(&command, "PASS %s@%s%c%c", user, host, CR, LF);
987 FREE(user);
988 }
989 status = response(command);
990 FREE(command);
991 CheckForInterrupt("while sending password");
992 }
993 FREE(username);
994
995 if (status == 3) {
996 status = send_cmd_1("ACCT noaccount");
997 CheckForInterrupt("while sending password");
998 }
999 if (status != 2) {
1000 CTRACE((tfp, "HTFTP: Login fail: %s", response_text));
1001 /* if (control->socket > 0) close_connection(control->socket); */
1002 return -1; /* Bad return */
1003 }
1004 CTRACE((tfp, "HTFTP: Logged in.\n"));
1005
1006 /* Check for host type */
1007 if (server_type != NETPRESENZ_SERVER)
1008 server_type = GENERIC_SERVER; /* reset */
1009 use_list = FALSE; /* reset */
1010 if (response("SYST\r\n") == 2) {
1011 /* we got a line -- what kind of server are we talking to? */
1012 if (StrNCmp(response_text + 4,
1013 "UNIX Type: L8 MAC-OS MachTen", 28) == 0) {
1014 server_type = MACHTEN_SERVER;
1015 use_list = TRUE;
1016 CTRACE((tfp, "HTFTP: Treating as MachTen server.\n"));
1017
1018 } else if (strstr(response_text + 4, "UNIX") != NULL ||
1019 strstr(response_text + 4, "Unix") != NULL) {
1020 server_type = UNIX_SERVER;
1021 unsure_type = FALSE; /* to the best of out knowledge... */
1022 use_list = TRUE;
1023 CTRACE((tfp, "HTFTP: Treating as Unix server.\n"));
1024
1025 } else if (strstr(response_text + 4, "MSDOS") != NULL) {
1026 server_type = MSDOS_SERVER;
1027 use_list = TRUE;
1028 CTRACE((tfp, "HTFTP: Treating as MSDOS (Unix emulation) server.\n"));
1029
1030 } else if (StrNCmp(response_text + 4, "VMS", 3) == 0) {
1031 const char *tilde = strstr(arg, "/~");
1032
1033 use_list = TRUE;
1034 if (tilde != NULL
1035 && tilde[2] != 0
1036 && strstr(response_text + 4, "MadGoat") != NULL) {
1037 server_type = UNIX_SERVER;
1038 CTRACE((tfp, "HTFTP: Treating VMS as UNIX server.\n"));
1039 } else {
1040 server_type = VMS_SERVER;
1041 CTRACE((tfp, "HTFTP: Treating as VMS server.\n"));
1042 }
1043
1044 } else if ((StrNCmp(response_text + 4, "VM/CMS", 6) == 0) ||
1045 (StrNCmp(response_text + 4, "VM ", 3) == 0)) {
1046 server_type = CMS_SERVER;
1047 use_list = TRUE;
1048 CTRACE((tfp, "HTFTP: Treating as CMS server.\n"));
1049
1050 } else if (StrNCmp(response_text + 4, "DCTS", 4) == 0) {
1051 server_type = DCTS_SERVER;
1052 CTRACE((tfp, "HTFTP: Treating as DCTS server.\n"));
1053
1054 } else if (strstr(response_text + 4, "MAC-OS TCP/Connect II") != NULL) {
1055 server_type = TCPC_SERVER;
1056 CTRACE((tfp, "HTFTP: Looks like a TCPC server.\n"));
1057 get_ftp_pwd(&server_type, &use_list);
1058 unsure_type = TRUE;
1059
1060 } else if (server_type == NETPRESENZ_SERVER) { /* already set above */
1061 use_list = TRUE;
1062 set_mac_binary(server_type);
1063 CTRACE((tfp, "HTFTP: Treating as NetPresenz (MACOS) server.\n"));
1064
1065 } else if (StrNCmp(response_text + 4, "MACOS Peter's Server", 20) == 0) {
1066 server_type = PETER_LEWIS_SERVER;
1067 use_list = TRUE;
1068 set_mac_binary(server_type);
1069 CTRACE((tfp, "HTFTP: Treating as Peter Lewis (MACOS) server.\n"));
1070
1071 } else if (StrNCmp(response_text + 4, "Windows_NT", 10) == 0) {
1072 server_type = WINDOWS_NT_SERVER;
1073 CTRACE((tfp, "HTFTP: Treating as Window_NT server.\n"));
1074 set_unix_dirstyle(&server_type, &use_list);
1075
1076 } else if (StrNCmp(response_text + 4, "Windows2000", 11) == 0) {
1077 server_type = WINDOWS_2K_SERVER;
1078 CTRACE((tfp, "HTFTP: Treating as Window_2K server.\n"));
1079 set_unix_dirstyle(&server_type, &use_list);
1080
1081 } else if (StrNCmp(response_text + 4, "MS Windows", 10) == 0) {
1082 server_type = MS_WINDOWS_SERVER;
1083 use_list = TRUE;
1084 CTRACE((tfp, "HTFTP: Treating as MS Windows server.\n"));
1085
1086 } else if (StrNCmp(response_text + 4,
1087 "MACOS AppleShare IP FTP Server", 30) == 0) {
1088 server_type = APPLESHARE_SERVER;
1089 use_list = TRUE;
1090 set_mac_binary(server_type);
1091 CTRACE((tfp, "HTFTP: Treating as AppleShare server.\n"));
1092
1093 } else {
1094 server_type = GENERIC_SERVER;
1095 CTRACE((tfp, "HTFTP: Ugh! A Generic server.\n"));
1096 get_ftp_pwd(&server_type, &use_list);
1097 unsure_type = TRUE;
1098 }
1099 } else {
1100 /* SYST fails :( try to get the type from the PWD command */
1101 get_ftp_pwd(&server_type, &use_list);
1102 }
1103
1104 return con->socket; /* Good return */
1105 }
1106
1107 static void reset_master_socket(void)
1108 {
1109 have_socket = FALSE;
1110 }
1111
1112 static void set_master_socket(int value)
1113 {
1114 have_socket = (BOOLEAN) (value >= 0);
1115 if (have_socket)
1116 master_socket = (LYNX_FD) value;
1117 }
1118
1119 /* Close Master (listening) socket
1120 * -------------------------------
1121 *
1122 *
1123 */
1124 static int close_master_socket(void)
1125 {
1126 int status;
1127
1128 if (have_socket)
1129 FD_CLR(master_socket, &open_sockets);
1130
1131 status = NETCLOSE((int) master_socket);
1132 CTRACE((tfp, "HTFTP: Closed master socket %u\n", (unsigned) master_socket));
1133
1134 reset_master_socket();
1135
1136 if (status < 0)
1137 return HTInetStatus(gettext("close master socket"));
1138 else
1139 return status;
1140 }
1141
1142 /* Open a master socket for listening on
1143 * -------------------------------------
1144 *
1145 * When data is transferred, we open a port, and wait for the server to
1146 * connect with the data.
1147 *
1148 * On entry,
1149 * have_socket Must be false, if master_socket is not setup already
1150 * master_socket Must be negative if not set up already.
1151 * On exit,
1152 * Returns socket number if good
1153 * less than zero if error.
1154 * master_socket is socket number if good, else negative.
1155 * port_number is valid if good.
1156 */
1157 static int get_listen_socket(void)
1158 {
1159 LY_SOCKADDR soc_A;
1160
1161 #ifdef INET6
1162 unsigned short af;
1163 LY_SOCKLEN slen;
1164 #endif /* INET6 */
1165 int new_socket; /* Will be master_socket */
1166
1167 FD_ZERO(&open_sockets); /* Clear our record of open sockets */
1168 num_sockets = 0;
1169
1170 FREE(port_command);
1171 #ifndef REPEAT_LISTEN
1172 if (have_socket)
1173 return master_socket; /* Done already */
1174 #endif /* !REPEAT_LISTEN */
1175
1176 #ifdef INET6
1177 /* query address family of control connection */
1178 memset(&soc_A, 0, sizeof(soc_A));
1179 slen = (LY_SOCKLEN) sizeof(soc_A);
1180 if (getsockname(control->socket, SOCKADDR_OF(soc_A), &slen) < 0) {
1181 return HTInetStatus("getsockname failed");
1182 }
1183 af = SOCKADDR_OF(soc_A)->sa_family;
1184 #endif /* INET6 */
1185
1186 /* Create internet socket
1187 */
1188 #ifdef INET6
1189 new_socket = socket(af, SOCK_STREAM, IPPROTO_TCP);
1190 #else
1191 new_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1192 #endif /* INET6 */
1193
1194 if (new_socket < 0)
1195 return HTInetStatus(gettext("socket for master socket"));
1196
1197 CTRACE((tfp, "HTFTP: Opened master socket number %d\n", new_socket));
1198
1199 /* Search for a free port.
1200 */
1201 #ifdef INET6
1202 memset(&soc_A, 0, sizeof(soc_A));
1203 SOCKADDR_OF(soc_A)->sa_family = (unsigned short) af;
1204 switch (af) {
1205 case AF_INET:
1206 #ifdef SIN6_LEN
1207 SOCKADDR_OF(soc_A)->sa_len = sizeof(struct sockaddr_in);
1208 #endif /* SIN6_LEN */
1209 break;
1210 case AF_INET6:
1211 #ifdef SIN6_LEN
1212 SOCKADDR_OF(soc_A)->sa_len = sizeof(struct sockaddr_in6);
1213 #endif /* SIN6_LEN */
1214 break;
1215 default:
1216 HTInetStatus("AF");
1217 }
1218 #else
1219 soc_A.soc_in.sin_family = AF_INET; /* Family = internet, host order */
1220 soc_A.soc_in.sin_addr.s_addr = INADDR_ANY; /* Any peer address */
1221 #endif /* INET6 */
1222 #ifdef POLL_PORTS
1223 {
1224 PortNumber old_port_number = port_number;
1225
1226 for (port_number = (old_port_number + 1);; port_number++) {
1227 int status;
1228
1229 if (port_number > LAST_TCP_PORT)
1230 port_number = FIRST_TCP_PORT;
1231 if (port_number == old_port_number) {
1232 return HTInetStatus("bind");
1233 }
1234 #ifdef INET6
1235 soc_A.soc_in.sin_port = htons(port_number);
1236 #else
1237 soc_A.sin_port = htons(port_number);
1238 #endif /* INET6 */
1239 #ifdef SOCKS
1240 if (socks_flag)
1241 if ((status = Rbind(new_socket,
1242 SOCKADDR_OF(soc_A),
1243 SOCKADDR_LEN(soc_A))) == 0) {
1244 break;
1245 } else
1246 #endif /* SOCKS */
1247 if ((status = bind(new_socket,
1248 SOCKADDR_OF(soc_A),
1249 SOCKADDR_LEN(soc_A)
1250 )) == 0) {
1251 break;
1252 }
1253 CTRACE((tfp, "TCP bind attempt to port %d yields %d, errno=%d\n",
1254 port_number, status, SOCKET_ERRNO));
1255 } /* for */
1256 }
1257 #else
1258 {
1259 int status;
1260 LY_SOCKLEN address_length = (LY_SOCKLEN) sizeof(soc_A);
1261
1262 #ifdef SOCKS
1263 if (socks_flag)
1264 status = Rgetsockname(control->socket,
1265 SOCKADDR_OF(soc_A),
1266 &address_length);
1267 else
1268 #endif /* SOCKS */
1269 status = getsockname(control->socket,
1270 SOCKADDR_OF(soc_A),
1271 &address_length);
1272 if (status < 0) {
1273 close(new_socket);
1274 return HTInetStatus("getsockname");
1275 }
1276 CTRACE((tfp, "HTFTP: This host is %s\n",
1277 HTInetString((void *) &soc_A.soc_in)));
1278
1279 soc_A.soc_in.sin_port = 0; /* Unspecified: please allocate */
1280 #ifdef SOCKS
1281 if (socks_flag)
1282 status = Rbind(new_socket,
1283 SOCKADDR_OF(soc_A),
1284 sizeof(soc_A));
1285 else
1286 #endif /* SOCKS */
1287 status = bind(new_socket,
1288 SOCKADDR_OF(soc_A),
1289 SOCKADDR_LEN(soc_A));
1290 if (status < 0) {
1291 close(new_socket);
1292 return HTInetStatus("bind");
1293 }
1294
1295 address_length = sizeof(soc_A);
1296 #ifdef SOCKS
1297 if (socks_flag)
1298 status = Rgetsockname(new_socket,
1299 SOCKADDR_OF(soc_A),
1300 &address_length);
1301 else
1302 #endif /* SOCKS */
1303 status = getsockname(new_socket,
1304 SOCKADDR_OF(soc_A),
1305 &address_length);
1306 if (status < 0) {
1307 close(new_socket);
1308 return HTInetStatus("getsockname");
1309 }
1310 }
1311 #endif /* POLL_PORTS */
1312
1313 CTRACE((tfp, "HTFTP: bound to port %d on %s\n",
1314 (int) ntohs(soc_A.soc_in.sin_port),
1315 HTInetString((void *) &soc_A.soc_in)));
1316
1317 #ifdef REPEAT_LISTEN
1318 if (have_socket)
1319 (void) close_master_socket();
1320 #endif /* REPEAT_LISTEN */
1321
1322 set_master_socket(new_socket);
1323
1324 /* Now we must find out who we are to tell the other guy
1325 */
1326 (void) HTHostName(); /* Make address valid - doesn't work */
1327 #ifdef INET6
1328 switch (SOCKADDR_OF(soc_A)->sa_family) {
1329 case AF_INET:
1330 #endif /* INET6 */
1331 HTSprintf0(&port_command, "PORT %d,%d,%d,%d,%d,%d%c%c",
1332 (int) *((unsigned char *) (&soc_A.soc_in.sin_addr) + 0),
1333 (int) *((unsigned char *) (&soc_A.soc_in.sin_addr) + 1),
1334 (int) *((unsigned char *) (&soc_A.soc_in.sin_addr) + 2),
1335 (int) *((unsigned char *) (&soc_A.soc_in.sin_addr) + 3),
1336 (int) *((unsigned char *) (&soc_A.soc_in.sin_port) + 0),
1337 (int) *((unsigned char *) (&soc_A.soc_in.sin_port) + 1),
1338 CR, LF);
1339
1340 #ifdef INET6
1341 break;
1342
1343 case AF_INET6:
1344 {
1345 char hostbuf[MAXHOSTNAMELEN];
1346 char portbuf[MAXHOSTNAMELEN];
1347
1348 getnameinfo(SOCKADDR_OF(soc_A),
1349 SOCKADDR_LEN(soc_A),
1350 hostbuf,
1351 (socklen_t) sizeof(hostbuf),
1352 portbuf,
1353 (socklen_t) sizeof(portbuf),
1354 NI_NUMERICHOST | NI_NUMERICSERV);
1355 HTSprintf0(&port_command, "EPRT |%d|%s|%s|%c%c", 2, hostbuf, portbuf,
1356 CR, LF);
1357 break;
1358 }
1359 default:
1360 HTSprintf0(&port_command, "JUNK%c%c", CR, LF);
1361 break;
1362 }
1363 #endif /* INET6 */
1364 if (port_command == NULL)
1365 return -1;
1366
1367 /* Inform TCP that we will accept connections
1368 */
1369 {
1370 int status;
1371
1372 #ifdef SOCKS
1373 if (socks_flag)
1374 status = Rlisten((int) master_socket, 1);
1375 else
1376 #endif /* SOCKS */
1377 status = listen((int) master_socket, 1);
1378 if (status < 0) {
1379 reset_master_socket();
1380 return HTInetStatus("listen");
1381 }
1382 }
1383 CTRACE((tfp, "TCP: Master socket(), bind() and listen() all OK\n"));
1384 FD_SET(master_socket, &open_sockets);
1385 if ((master_socket + 1) > num_sockets)
1386 num_sockets = master_socket + 1;
1387
1388 return (int) master_socket; /* Good */
1389
1390 } /* get_listen_socket */
1391
1392 static const char *months[12] =
1393 {
1394 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1395 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
1396 };
1397
1398 /* Procedure: Set the current and last year strings and date integer
1399 * -----------------------------------------------------------------
1400 *
1401 * Bug:
1402 * This code is for sorting listings by date, if that option
1403 * is selected in Lynx, and doesn't take into account time
1404 * zones or ensure resetting at midnight, so the sort may not
1405 * be perfect, but the actual date isn't changed in the display,
1406 * i.e., the date is still correct. - FM
1407 */
1408 static void set_years_and_date(void)
1409 {
1410 char day[8], month[8], date[12];
1411 time_t NowTime;
1412 int i;
1413 char *printable;
1414
1415 NowTime = time(NULL);
1416 printable = ctime(&NowTime);
1417 LYStrNCpy(day, printable + 8, 2);
1418 if (day[0] == ' ') {
1419 day[0] = '0';
1420 }
1421 LYStrNCpy(month, printable + 4, 3);
1422 for (i = 0; i < 12; i++) {
1423 if (!strcasecomp(month, months[i])) {
1424 break;
1425 }
1426 }
1427 i++;
1428 sprintf(date, "9999%02d%.2s", i % 100, day);
1429 TheDate = atoi(date);
1430 LYStrNCpy(ThisYear, printable + 20, 4);
1431 sprintf(LastYear, "%d", (atoi(ThisYear) - 1) % 10000);
1432 HaveYears = TRUE;
1433 }
1434
1435 typedef struct _EntryInfo {
1436 char *filename;
1437 char *linkname; /* symbolic link, if any */
1438 char *type;
1439 char *date;
1440 off_t size;
1441 BOOLEAN display; /* show this entry? */
1442 #ifdef LONG_LIST
1443 unsigned long file_links;
1444 char *file_mode;
1445 char *file_user;
1446 char *file_group;
1447 #endif
1448 } EntryInfo;
1449
1450 static void free_entryinfo_struct_contents(EntryInfo *entry_info)
1451 {
1452 if (entry_info) {
1453 #ifdef LONG_LIST
1454 FREE(entry_info->file_mode);
1455 FREE(entry_info->file_user);
1456 FREE(entry_info->file_group);
1457 #endif
1458 FREE(entry_info->filename);
1459 FREE(entry_info->linkname);
1460 FREE(entry_info->type);
1461 FREE(entry_info->date);
1462 }
1463 /* don't free the struct */
1464 }
1465
1466 /*
1467 * is_ls_date() --
1468 * Return TRUE if s points to a string of the form:
1469 * "Sep 1 1990 " or
1470 * "Sep 11 11:59 " or
1471 * "Dec 12 1989 " or
1472 * "FCv 23 1990 " ...
1473 */
1474 static BOOLEAN is_ls_date(char *s)
1475 {
1476 /* must start with three alpha characters */
1477 if (!isalpha(UCH(*s++)) || !isalpha(UCH(*s++)) || !isalpha(UCH(*s++)))
1478 return FALSE;
1479
1480 /* space or HT_NON_BREAK_SPACE */
1481 if (!(*s == ' ' || *s == HT_NON_BREAK_SPACE)) {
1482 return FALSE;
1483 }
1484 s++;
1485
1486 /* space or digit */
1487 if (!(*s == ' ' || isdigit(UCH(*s)))) {
1488 return FALSE;
1489 }
1490 s++;
1491
1492 /* digit */
1493 if (!isdigit(UCH(*s++)))
1494 return FALSE;
1495
1496 /* space */
1497 if (*s++ != ' ')
1498 return FALSE;
1499
1500 /* space or digit */
1501 if (!(*s == ' ' || isdigit(UCH(*s)))) {
1502 return FALSE;
1503 }
1504 s++;
1505
1506 /* digit */
1507 if (!isdigit(UCH(*s++)))
1508 return FALSE;
1509
1510 /* colon or digit */
1511 if (!(*s == ':' || isdigit(UCH(*s)))) {
1512 return FALSE;
1513 }
1514 s++;
1515
1516 /* digit */
1517 if (!isdigit(UCH(*s++)))
1518 return FALSE;
1519
1520 /* space or digit */
1521 if (!(*s == ' ' || isdigit(UCH(*s)))) {
1522 return FALSE;
1523 }
1524 s++;
1525
1526 /* space */
1527 if (*s != ' ')
1528 return FALSE;
1529
1530 return TRUE;
1531 } /* is_ls_date() */
1532
1533 /*
1534 * Extract the name, size, and date from an EPLF line. - 08-06-96 DJB
1535 */
1536 static void parse_eplf_line(char *line,
1537 EntryInfo *info)
1538 {
1539 char *cp = line;
1540 char ct[26];
1541 off_t size;
1542 time_t secs;
1543 static time_t base; /* time() value on this OS in 1970 */
1544 static int flagbase = 0;
1545
1546 if (!flagbase) {
1547 struct tm t;
1548
1549 t.tm_year = 70;
1550 t.tm_mon = 0;
1551 t.tm_mday = 0;
1552 t.tm_hour = 0;
1553 t.tm_min = 0;
1554 t.tm_sec = 0;
1555 t.tm_isdst = -1;
1556 base = mktime(&t); /* could return -1 */
1557 flagbase = 1;
1558 }
1559
1560 while (*cp) {
1561 switch (*cp) {
1562 case '\t':
1563 StrAllocCopy(info->filename, cp + 1);
1564 return;
1565 case 's':
1566 size = 0;
1567 while (*(++cp) && (*cp != ','))
1568 size = (size * 10) + (off_t) (*cp - '0');
1569 info->size = size;
1570 break;
1571 case 'm':
1572 secs = 0;
1573 while (*(++cp) && (*cp != ','))
1574 secs = (secs * 10) + (*cp - '0');
1575 secs += base; /* assumes that time_t is #seconds */
1576 LYStrNCpy(ct, ctime(&secs), 24);
1577 StrAllocCopy(info->date, ct);
1578 break;
1579 case '/':
1580 StrAllocCopy(info->type, ENTRY_IS_DIRECTORY);
1581 /* FALLTHRU */
1582 default:
1583 while (*cp) {
1584 if (*cp++ == ',')
1585 break;
1586 }
1587 break;
1588 }
1589 }
1590 } /* parse_eplf_line */
1591
1592 /*
1593 * Extract the name, size, and date from an ls -l line.
1594 */
1595 static void parse_ls_line(char *line,
1596 EntryInfo *entry)
1597 {
1598 #ifdef LONG_LIST
1599 char *next;
1600 char *cp;
1601 #endif
1602 int i, j;
1603 off_t base = 1;
1604 off_t size_num = 0;
1605
1606 for (i = (int) strlen(line) - 1;
1607 (i > 13) && (!isspace(UCH(line[i])) || !is_ls_date(&line[i - 12]));
1608 i--) {
1609 ; /* null body */
1610 }
1611 line[i] = '\0';
1612 if (i > 13) {
1613 StrAllocCopy(entry->date, &line[i - 12]);
1614 /* replace the 4th location with nbsp if it is a space or zero */
1615 if (entry->date[4] == ' ' || entry->date[4] == '0')
1616 entry->date[4] = HT_NON_BREAK_SPACE;
1617 /* make sure year or time is flush right */
1618 if (entry->date[11] == ' ') {
1619 for (j = 11; j > 6; j--) {
1620 entry->date[j] = entry->date[j - 1];
1621 }
1622 }
1623 }
1624 j = i - 14;
1625 while (isdigit(UCH(line[j]))) {
1626 size_num += ((off_t) (line[j] - '0') * base);
1627 base *= 10;
1628 j--;
1629 }
1630 entry->size = size_num;
1631 StrAllocCopy(entry->filename, &line[i + 1]);
1632
1633 #ifdef LONG_LIST
1634 line[j] = '\0';
1635
1636 /*
1637 * Extract the file-permissions, as a string.
1638 */
1639 if ((cp = StrChr(line, ' ')) != NULL) {
1640 if ((cp - line) == 10) {
1641 *cp = '\0';
1642 StrAllocCopy(entry->file_mode, line);
1643 *cp = ' ';
1644 }
1645
1646 /*
1647 * Next is the link-count.
1648 */
1649 next = NULL;
1650 entry->file_links = (unsigned long) strtol(cp, &next, 10);
1651 if (next == NULL || *next != ' ') {
1652 entry->file_links = 0;
1653 next = cp;
1654 } else {
1655 cp = next;
1656 }
1657 /*
1658 * Next is the user-name.
1659 */
1660 while (isspace(UCH(*cp)))
1661 ++cp;
1662 if ((next = StrChr(cp, ' ')) != NULL)
1663 *next = '\0';
1664 if (*cp != '\0')
1665 StrAllocCopy(entry->file_user, cp);
1666 /*
1667 * Next is the group-name (perhaps).
1668 */
1669 if (next != NULL) {
1670 cp = (next + 1);
1671 while (isspace(UCH(*cp)))
1672 ++cp;
1673 if ((next = StrChr(cp, ' ')) != NULL)
1674 *next = '\0';
1675 if (*cp != '\0')
1676 StrAllocCopy(entry->file_group, cp);
1677 }
1678 }
1679 #endif
1680 }
1681
1682 /*
1683 * Extract the name and size info and whether it refers to a directory from a
1684 * LIST line in "dls" format.
1685 */
1686 static void parse_dls_line(char *line,
1687 EntryInfo *entry_info,
1688 char **pspilledname)
1689 {
1690 short j;
1691 int base = 1;
1692 off_t size_num = 0;
1693 int len;
1694 char *cps = NULL;
1695
1696 /* README 763 Information about this server\0
1697 bin/ - \0
1698 etc/ = \0
1699 ls-lR 0 \0
1700 ls-lR.Z 3 \0
1701 pub/ = Public area\0
1702 usr/ - \0
1703 morgan 14 -> ../real/morgan\0
1704 TIMIT.mostlikely.Z\0
1705 79215 \0
1706 */
1707
1708 len = (int) strlen(line);
1709 if (len == 0) {
1710 FREE(*pspilledname);
1711 entry_info->display = FALSE;
1712 return;
1713 }
1714 cps = LYSkipNonBlanks(line);
1715 if (*cps == '\0') { /* only a filename, save it and return. */
1716 StrAllocCopy(*pspilledname, line);
1717 entry_info->display = FALSE;
1718 return;
1719 }
1720 if (len < 24 || line[23] != ' ' ||
1721 (isspace(UCH(line[0])) && !*pspilledname)) {
1722 /* this isn't the expected "dls" format! */
1723 if (!isspace(UCH(line[0])))
1724 *cps = '\0';
1725 if (*pspilledname && !*line) {
1726 entry_info->filename = *pspilledname;
1727 *pspilledname = NULL;
1728 if (entry_info->filename[strlen(entry_info->filename) - 1] == '/')
1729 StrAllocCopy(entry_info->type, ENTRY_IS_DIRECTORY);
1730 else
1731 StrAllocCopy(entry_info->type, "");
1732 } else {
1733 StrAllocCopy(entry_info->filename, line);
1734 if (cps != line && *(cps - 1) == '/')
1735 StrAllocCopy(entry_info->type, ENTRY_IS_DIRECTORY);
1736 else
1737 StrAllocCopy(entry_info->type, "");
1738 FREE(*pspilledname);
1739 }
1740 return;
1741 }
1742
1743 j = 22;
1744 if (line[j] == '=' || line[j] == '-') {
1745 StrAllocCopy(entry_info->type, ENTRY_IS_DIRECTORY);
1746 } else {
1747 while (isdigit(UCH(line[j]))) {
1748 size_num += (line[j] - '0') * base;
1749 base *= 10;
1750 j--;
1751 }
1752 }
1753 entry_info->size = size_num;
1754
1755 cps = LYSkipBlanks(&line[23]);
1756 if (!StrNCmp(cps, "-> ", 3) && cps[3] != '\0' && cps[3] != ' ') {
1757 StrAllocCopy(entry_info->type, ENTRY_IS_SYMBOLIC_LINK);
1758 StrAllocCopy(entry_info->linkname, LYSkipBlanks(cps + 3));
1759 entry_info->size = 0; /* don't display size */
1760 }
1761
1762 if (j > 0)
1763 line[j] = '\0';
1764
1765 LYTrimTrailing(line);
1766
1767 len = (int) strlen(line);
1768 if (len == 0 && *pspilledname && **pspilledname) {
1769 line = *pspilledname;
1770 len = (int) strlen(*pspilledname);
1771 }
1772 if (len > 0 && line[len - 1] == '/') {
1773 /*
1774 * It's a dir, remove / and mark it as such.
1775 */
1776 if (len > 1)
1777 line[len - 1] = '\0';
1778 if (!entry_info->type)
1779 StrAllocCopy(entry_info->type, ENTRY_IS_DIRECTORY);
1780 }
1781
1782 StrAllocCopy(entry_info->filename, line);
1783 FREE(*pspilledname);
1784 } /* parse_dls_line() */
1785
1786 /*
1787 * parse_vms_dir_entry()
1788 * Format the name, date, and size from a VMS LIST line
1789 * into the EntryInfo structure - FM
1790 */
1791 static void parse_vms_dir_entry(char *line,
1792 EntryInfo *entry_info)
1793 {
1794 int i, j;
1795 off_t ialloc;
1796 char *cp, *cpd, *cps, date[16];
1797 const char *sp = " ";
1798
1799 /* Get rid of blank lines, and information lines. Valid lines have the ';'
1800 * version number token.
1801 */
1802 if (!strlen(line) || (cp = StrChr(line, ';')) == NULL) {
1803 entry_info->display = FALSE;
1804 return;
1805 }
1806
1807 /* Cut out file or directory name at VMS version number. */
1808 *cp++ = '\0';
1809 StrAllocCopy(entry_info->filename, line);
1810
1811 /* Cast VMS non-README file and directory names to lowercase. */
1812 if (strstr(entry_info->filename, "READ") == NULL) {
1813 LYLowerCase(entry_info->filename);
1814 i = (int) strlen(entry_info->filename);
1815 } else {
1816 i = (int) ((strstr(entry_info->filename, "READ")
1817 - entry_info->filename)
1818 + 4);
1819 if (!StrNCmp(&entry_info->filename[i], "ME", 2)) {
1820 i += 2;
1821 while (entry_info->filename[i] && entry_info->filename[i] != '.') {
1822 i++;
1823 }
1824 } else if (!StrNCmp(&entry_info->filename[i], ".ME", 3)) {
1825 i = (int) strlen(entry_info->filename);
1826 } else {
1827 i = 0;
1828 }
1829 LYLowerCase(entry_info->filename + i);
1830 }
1831
1832 /* Uppercase terminal .z's or _z's. */
1833 if ((--i > 2) &&
1834 entry_info->filename[i] == 'z' &&
1835 (entry_info->filename[i - 1] == '.' ||
1836 entry_info->filename[i - 1] == '_'))
1837 entry_info->filename[i] = 'Z';
1838
1839 /* Convert any tabs in rest of line to spaces. */
1840 cps = cp - 1;
1841 while ((cps = StrChr(cps + 1, '\t')) != NULL)
1842 *cps = ' ';
1843
1844 /* Collapse serial spaces. */
1845 i = 0;
1846 j = 1;
1847 cps = cp;
1848 while (cps[j] != '\0') {
1849 if (cps[i] == ' ' && cps[j] == ' ')
1850 j++;
1851 else
1852 cps[++i] = cps[j++];
1853 }
1854 cps[++i] = '\0';
1855
1856 /* Set the years and date, if we don't have them yet. * */
1857 if (!HaveYears) {
1858 set_years_and_date();
1859 }
1860
1861 /* Track down the date. */
1862 if ((cpd = StrChr(cp, '-')) != NULL &&
1863 strlen(cpd) > 9 && isdigit(UCH(*(cpd - 1))) &&
1864 isalpha(UCH(*(cpd + 1))) && *(cpd + 4) == '-') {
1865
1866 /* Month */
1867 *(cpd + 2) = (char) TOLOWER(*(cpd + 2));
1868 *(cpd + 3) = (char) TOLOWER(*(cpd + 3));
1869 sprintf(date, "%.3s ", cpd + 1);
1870
1871 /* Day */
1872 if (isdigit(UCH(*(cpd - 2))))
1873 sprintf(date + 4, "%.2s ", cpd - 2);
1874 else
1875 sprintf(date + 4, "%c%.1s ", HT_NON_BREAK_SPACE, cpd - 1);
1876
1877 /* Time or Year */
1878 if (!StrNCmp(ThisYear, cpd + 5, 4) &&
1879 strlen(cpd) > 15 && *(cpd + 12) == ':') {
1880 sprintf(date + 7, "%.5s", cpd + 10);
1881 } else {
1882 sprintf(date + 7, " %.4s", cpd + 5);
1883 }
1884
1885 StrAllocCopy(entry_info->date, date);
1886 }
1887
1888 /* Track down the size */
1889 if ((cpd = StrChr(cp, '/')) != NULL) {
1890 /* Appears be in used/allocated format */
1891 cps = cpd;
1892 while (isdigit(UCH(*(cps - 1))))
1893 cps--;
1894 if (cps < cpd)
1895 *cpd = '\0';
1896 entry_info->size = LYatoll(cps);
1897 cps = cpd + 1;
1898 while (isdigit(UCH(*cps)))
1899 cps++;
1900 *cps = '\0';
1901 ialloc = LYatoll(cpd + 1);
1902 /* Check if used is in blocks or bytes */
1903 if (entry_info->size <= ialloc)
1904 entry_info->size *= 512;
1905
1906 } else if (strtok(cp, sp) != NULL) {
1907 /* We just initialized on the version number */
1908 /* Now let's hunt for a lone, size number */
1909 while ((cps = strtok(NULL, sp)) != NULL) {
1910 cpd = cps;
1911 while (isdigit(UCH(*cpd)))
1912 cpd++;
1913 if (*cpd == '\0') {
1914 /* Assume it's blocks */
1915 entry_info->size = (LYatoll(cps) * 512);
1916 break;
1917 }
1918 }
1919 }
1920
1921 TRACE_ENTRY("VMS", entry_info);
1922 return;
1923 } /* parse_vms_dir_entry() */
1924
1925 /*
1926 * parse_ms_windows_dir_entry() --
1927 * Format the name, date, and size from an MS_WINDOWS LIST line into
1928 * the EntryInfo structure (assumes Chameleon NEWT format). - FM
1929 */
1930 static void parse_ms_windows_dir_entry(char *line,
1931 EntryInfo *entry_info)
1932 {
1933 char *cp = line;
1934 char *cps, *cpd, date[16];
1935 char *end = line + strlen(line);
1936
1937 /* Get rid of blank or junk lines. */
1938 cp = LYSkipBlanks(cp);
1939 if (!(*cp)) {
1940 entry_info->display = FALSE;
1941 return;
1942 }
1943
1944 /* Cut out file or directory name. */
1945 cps = LYSkipNonBlanks(cp);
1946 *cps++ = '\0';
1947 cpd = cps;
1948 StrAllocCopy(entry_info->filename, cp);
1949
1950 /* Track down the size */
1951 if (cps < end) {
1952 cps = LYSkipBlanks(cps);
1953 cpd = LYSkipNonBlanks(cps);
1954 *cpd++ = '\0';
1955 if (isdigit(UCH(*cps))) {
1956 entry_info->size = LYatoll(cps);
1957 } else {
1958 StrAllocCopy(entry_info->type, ENTRY_IS_DIRECTORY);
1959 }
1960 } else {
1961 StrAllocCopy(entry_info->type, "");
1962 }
1963
1964 /* Set the years and date, if we don't have them yet. * */
1965 if (!HaveYears) {
1966 set_years_and_date();
1967 }
1968
1969 /* Track down the date. */
1970 if (cpd < end) {
1971 cpd = LYSkipBlanks(cpd);
1972 if (strlen(cpd) > 17) {
1973 *(cpd + 6) = '\0'; /* Month and Day */
1974 *(cpd + 11) = '\0'; /* Year */
1975 *(cpd + 17) = '\0'; /* Time */
1976 if (strcmp(ThisYear, cpd + 7))
1977 /* Not this year, so show the year */
1978 sprintf(date, "%.6s %.4s", cpd, (cpd + 7));
1979 else
1980 /* Is this year, so show the time */
1981 sprintf(date, "%.6s %.5s", cpd, (cpd + 12));
1982 StrAllocCopy(entry_info->date, date);
1983 if (entry_info->date[4] == ' ' || entry_info->date[4] == '0') {
1984 entry_info->date[4] = HT_NON_BREAK_SPACE;
1985 }
1986 }
1987 }
1988
1989 TRACE_ENTRY("MS Windows", entry_info);
1990 return;
1991 } /* parse_ms_windows_dir_entry */
1992
1993 /*
1994 * parse_windows_nt_dir_entry() --
1995 * Format the name, date, and size from a WINDOWS_NT LIST line into
1996 * the EntryInfo structure (assumes Chameleon NEWT format). - FM
1997 */
1998 #ifdef NOTDEFINED
1999 static void parse_windows_nt_dir_entry(char *line,
2000 EntryInfo *entry_info)
2001 {
2002 char *cp = line;
2003 char *cps, *cpd, date[16];
2004 char *end = line + strlen(line);
2005 int i;
2006
2007 /* Get rid of blank or junk lines. */
2008 cp = LYSkipBlanks(cp);
2009 if (!(*cp)) {
2010 entry_info->display = FALSE;
2011 return;
2012 }
2013
2014 /* Cut out file or directory name. */
2015 cpd = cp;
2016 cps = LYSkipNonBlanks(end - 1);
2017 cp = (cps + 1);
2018 if (!strcmp(cp, ".") || !strcmp(cp, "..")) {
2019 entry_info->display = FALSE;
2020 return;
2021 }
2022 StrAllocCopy(entry_info->filename, cp);
2023 if (cps < cpd)
2024 return;
2025 *cp = '\0';
2026 end = cp;
2027
2028 /* Set the years and date, if we don't have them yet. * */
2029 if (!HaveYears) {
2030 set_years_and_date();
2031 }
2032
2033 /* Cut out the date. */
2034 cp = cps = cpd;
2035 cps = LYSkipNonBlanks(cps);
2036 *cps++ = '\0';
2037 if (cps > end) {
2038 entry_info->display = FALSE;
2039 return;
2040 }
2041 cps = LYSkipBlanks(cps);
2042 cpd = LYSkipNonBlanks(cps);
2043 *cps++ = '\0';
2044 if (cps > end || cpd == cps || strlen(cpd) < 7) {
2045 entry_info->display = FALSE;
2046 return;
2047 }
2048 if (strlen(cp) == 8 &&
2049 isdigit(*cp) && isdigit(*(cp + 1)) && *(cp + 2) == '-' &&
2050 isdigit(*(cp + 3)) && isdigit(*(cp + 4)) && *(cp + 5) == '-') {
2051 *(cp + 2) = '\0'; /* Month */
2052 i = atoi(cp) - 1;
2053 *(cp + 5) = '\0'; /* Day */
2054 sprintf(date, "%.3s %.2s", months[i], (cp + 3));
2055 if (date[4] == '0')
2056 date[4] = ' ';
2057 cp += 6; /* Year */
2058 if (strcmp((ThisYear + 2), cp)) {
2059 /* Not this year, so show the year */
2060 if (atoi(cp) < 70) {
2061 sprintf(&date[6], " 20%.2s", cp);
2062 } else {
2063 sprintf(&date[6], " 19%.2s", cp);
2064 }
2065 } else {
2066 /* Is this year, so show the time */
2067 *(cpd + 2) = '\0'; /* Hour */
2068 i = atoi(cpd);
2069 if (*(cpd + 5) == 'P' || *(cpd + 5) == 'p')
2070 i += 12;
2071 sprintf(&date[6], " %02d:%.2s", i, (cpd + 3));
2072 }
2073 StrAllocCopy(entry_info->date, date);
2074 if (entry_info->date[4] == ' ' || entry_info->date[4] == '0') {
2075 entry_info->date[4] = HT_NON_BREAK_SPACE;
2076 }
2077 }
2078
2079 /* Track down the size */
2080 if (cps < end) {
2081 cps = LYSkipBlanks(cps);
2082 cpd = LYSkipNonBlanks(cps);
2083 *cpd = '\0';
2084 if (isdigit(*cps)) {
2085 entry_info->size = LYatoll(cps);
2086 } else {
2087 StrAllocCopy(entry_info->type, ENTRY_IS_DIRECTORY);
2088 }
2089 } else {
2090 StrAllocCopy(entry_info->type, "");
2091 }
2092
2093 /* Wrap it up */
2094 CTRACE((tfp, "HTFTP: Windows NT filename: %s date: %s size: %d\n",
2095 entry_info->filename,
2096 NonNull(entry_info->date),
2097 entry_info->size));
2098 return;
2099 } /* parse_windows_nt_dir_entry */
2100 #endif /* NOTDEFINED */
2101
2102 /*
2103 * parse_cms_dir_entry() --
2104 * Format the name, date, and size from a VM/CMS line into
2105 * the EntryInfo structure. - FM
2106 */
2107 static void parse_cms_dir_entry(char *line,
2108 EntryInfo *entry_info)
2109 {
2110 char *cp = line;
2111 char *cps, *cpd, date[16];
2112 char *end = line + strlen(line);
2113 int RecordLength = 0;
2114 int Records = 0;
2115 int i;
2116
2117 /* Get rid of blank or junk lines. */
2118 cp = LYSkipBlanks(cp);
2119 if (!(*cp)) {
2120 entry_info->display = FALSE;
2121 return;
2122 }
2123
2124 /* Cut out file or directory name. */
2125 cps = LYSkipNonBlanks(cp);
2126 *cps++ = '\0';
2127 StrAllocCopy(entry_info->filename, cp);
2128 if (StrChr(entry_info->filename, '.') != NULL)
2129 /* If we already have a dot, we did an NLST. */
2130 return;
2131 cp = LYSkipBlanks(cps);
2132 if (!(*cp)) {
2133 /* If we don't have more, we've misparsed. */
2134 FREE(entry_info->filename);
2135 FREE(entry_info->type);
2136 entry_info->display = FALSE;
2137 return;
2138 }
2139 cps = LYSkipNonBlanks(cp);
2140 *cps++ = '\0';
2141 if ((0 == strcasecomp(cp, "DIR")) && (cp - line) > 17) {
2142 /* It's an SFS directory. */
2143 StrAllocCopy(entry_info->type, ENTRY_IS_DIRECTORY);
2144 entry_info->size = 0;
2145 } else {
2146 /* It's a file. */
2147 cp--;
2148 *cp = '.';
2149 StrAllocCat(entry_info->filename, cp);
2150
2151 /* Track down the VM/CMS RECFM or type. */
2152 cp = cps;
2153 if (cp < end) {
2154 cp = LYSkipBlanks(cp);
2155 cps = LYSkipNonBlanks(cp);
2156 *cps++ = '\0';
2157 /* Check cp here, if it's relevant someday. */
2158 }
2159 }
2160
2161 /* Track down the record length or dash. */
2162 cp = cps;
2163 if (cp < end) {
2164 cp = LYSkipBlanks(cp);
2165 cps = LYSkipNonBlanks(cp);
2166 *cps++ = '\0';
2167 if (isdigit(UCH(*cp))) {
2168 RecordLength = atoi(cp);
2169 }
2170 }
2171
2172 /* Track down the number of records or the dash. */
2173 cp = cps;
2174 if (cps < end) {
2175 cp = LYSkipBlanks(cp);
2176 cps = LYSkipNonBlanks(cp);
2177 *cps++ = '\0';
2178 if (isdigit(UCH(*cp))) {
2179 Records = atoi(cp);
2180 }
2181 if (Records > 0 && RecordLength > 0) {
2182 /* Compute an approximate size. */
2183 entry_info->size = ((off_t) Records * (off_t) RecordLength);
2184 }
2185 }
2186
2187 /* Set the years and date, if we don't have them yet. */
2188 if (!HaveYears) {
2189 set_years_and_date();
2190 }
2191
2192 /* Track down the date using the ":" separating hours/minutes:
2193 * mm/dd/yy hh:mm
2194 * 01234567890123
2195 */
2196 cpd = cps;
2197 if (((cps < end) &&
2198 (cps = StrChr(cpd, ':')) != NULL) &&
2199 (cps < (end - 3) &&
2200 isdigit(UCH(*(cps + 1))) && isdigit(UCH(*(cps + 2))) && *(cps + 3) == ':')) {
2201 cps += 3;
2202 *cps = '\0';
2203 if ((cps - cpd) >= 14) {
2204 cpd = (cps - 14);
2205 *(cpd + 2) = '\0'; /* Month */
2206 *(cpd + 5) = '\0'; /* Day */
2207 *(cpd + 8) = '\0'; /* Year */
2208 cps -= 5; /* Time */
2209 if (*cpd == ' ')
2210 *cpd = '0';
2211 i = atoi(cpd) - 1;
2212 sprintf(date, "%.3s %.2s", months[i], (cpd + 3));
2213 if (date[4] == '0')
2214 date[4] = ' ';
2215 cpd += 6; /* Year */
2216 if (strcmp((ThisYear + 2), cpd)) {
2217 /* Not this year, so show the year. */
2218 if (atoi(cpd) < 70) {
2219 sprintf(&date[6], " 20%.2s", cpd);
2220 } else {
2221 sprintf(&date[6], " 19%.2s", cpd);
2222 }
2223 } else {
2224 /* Is this year, so show the time. */
2225 *(cps + 2) = '\0'; /* Hour */
2226 i = atoi(cps);
2227 sprintf(&date[6], " %02d:%.2s", i, (cps + 3));
2228 }
2229 StrAllocCopy(entry_info->date, date);
2230 if (entry_info->date[4] == ' ' || entry_info->date[4] == '0') {
2231 entry_info->date[4] = HT_NON_BREAK_SPACE;
2232 }
2233 }
2234 }
2235
2236 TRACE_ENTRY("VM/CMS", entry_info);
2237 return;
2238 } /* parse_cms_dir_entry */
2239
2240 /*
2241 * Given a line of LIST/NLST output in entry, return results and a file/dir
2242 * name in entry_info struct
2243 *
2244 * If first is true, this is the first name in a directory.
2245 */
2246 static EntryInfo *parse_dir_entry(char *entry,
2247 BOOLEAN *first,
2248 char **pspilledname)
2249 {
2250 EntryInfo *entry_info;
2251 int i;
2252 int len;
2253 BOOLEAN remove_size = FALSE;
2254 char *cp;
2255
2256 entry_info = typecalloc(EntryInfo);
2257
2258 if (entry_info == NULL)
2259 outofmem(__FILE__, "parse_dir_entry");
2260
2261 entry_info->display = TRUE;
2262
2263 switch (server_type) {
2264 case DLS_SERVER:
2265
2266 /*
2267 * Interpret and edit LIST output from a Unix server in "dls" format.
2268 * This one must have claimed to be Unix in order to get here; if the
2269 * first line looks fishy, we revert to Unix and hope that fits better
2270 * (this recovery is untested). - kw
2271 */
2272
2273 if (*first) {
2274 len = (int) strlen(entry);
2275 if (!len || entry[0] == ' ' ||
2276 (len >= 24 && entry[23] != ' ') ||
2277 (len < 24 && StrChr(entry, ' '))) {
2278 server_type = UNIX_SERVER;
2279 CTRACE((tfp,
2280 "HTFTP: Falling back to treating as Unix server.\n"));
2281 } else {
2282 *first = FALSE;
2283 }
2284 }
2285
2286 if (server_type == DLS_SERVER) {
2287 /* if still unchanged... */
2288 parse_dls_line(entry, entry_info, pspilledname);
2289
2290 if (isEmpty(entry_info->filename)) {
2291 entry_info->display = FALSE;
2292 return (entry_info);
2293 }
2294 if (!strcmp(entry_info->filename, "..") ||
2295 !strcmp(entry_info->filename, "."))
2296 entry_info->display = FALSE;
2297 if (entry_info->type && *entry_info->type == '\0') {
2298 FREE(entry_info->type);
2299 return (entry_info);
2300 }
2301 /*
2302 * Goto the bottom and get real type.
2303 */
2304 break;
2305 }
2306 /* fall through if server_type changed for *first == TRUE ! */
2307 /* FALLTHRU */
2308 case UNIX_SERVER:
2309 case PETER_LEWIS_SERVER:
2310 case MACHTEN_SERVER:
2311 case MSDOS_SERVER:
2312 case WINDOWS_NT_SERVER:
2313 case WINDOWS_2K_SERVER:
2314 case APPLESHARE_SERVER:
2315 case NETPRESENZ_SERVER:
2316 /*
2317 * Check for EPLF output (local times).
2318 */
2319 if (*entry == '+') {
2320 parse_eplf_line(entry, entry_info);
2321 break;
2322 }
2323
2324 /*
2325 * Interpret and edit LIST output from Unix server.
2326 */
2327 len = (int) strlen(entry);
2328 if (*first) {
2329 /* don't gettext() this -- incoming text: */
2330 if (!strcmp(entry, "can not access directory .")) {
2331 /*
2332 * Don't reset *first, nothing real will follow. - KW
2333 */
2334 entry_info->display = FALSE;
2335 return (entry_info);
2336 }
2337 *first = FALSE;
2338 if (!StrNCmp(entry, "total ", 6) ||
2339 strstr(entry, "not available") != NULL) {
2340 entry_info->display = FALSE;
2341 return (entry_info);
2342 } else if (unsure_type) {
2343 /* this isn't really a unix server! */
2344 server_type = GENERIC_SERVER;
2345 entry_info->display = FALSE;
2346 return (entry_info);
2347 }
2348 }
2349
2350 /*
2351 * Check first character of ls -l output.
2352 */
2353 if (TOUPPER(entry[0]) == 'D') {
2354 /*
2355 * It's a directory.
2356 */
2357 StrAllocCopy(entry_info->type, ENTRY_IS_DIRECTORY);
2358 remove_size = TRUE; /* size is not useful */
2359 } else if (entry[0] == 'l') {
2360 /*
2361 * It's a symbolic link, does the user care about knowing if it is
2362 * symbolic? I think so since it might be a directory.
2363 */
2364 StrAllocCopy(entry_info->type, ENTRY_IS_SYMBOLIC_LINK);
2365 remove_size = TRUE; /* size is not useful */
2366
2367 /*
2368 * Strip off " -> pathname".
2369 */
2370 for (i = len - 1; (i > 3) &&
2371 (!isspace(UCH(entry[i])) ||
2372 (entry[i - 1] != '>') ||
2373 (entry[i - 2] != '-') ||
2374 (entry[i - 3] != ' ')); i--) ; /* null body */
2375 if (i > 3) {
2376 entry[i - 3] = '\0';
2377 StrAllocCopy(entry_info->linkname, LYSkipBlanks(entry + i));
2378 }
2379 }
2380 /* link */
2381 parse_ls_line(entry, entry_info);
2382
2383 if (!strcmp(entry_info->filename, "..") ||
2384 !strcmp(entry_info->filename, "."))
2385 entry_info->display = FALSE;
2386 /*
2387 * Goto the bottom and get real type.
2388 */
2389 break;
2390
2391 case VMS_SERVER:
2392 /*
2393 * Interpret and edit LIST output from VMS server and convert
2394 * information lines to zero length.
2395 */
2396 parse_vms_dir_entry(entry, entry_info);
2397
2398 /*
2399 * Get rid of any junk lines.
2400 */
2401 if (!entry_info->display)
2402 return (entry_info);
2403
2404 /*
2405 * Trim off VMS directory extensions.
2406 */
2407 len = (int) strlen(entry_info->filename);
2408 if ((len > 4) && !strcmp(&entry_info->filename[len - 4], ".dir")) {
2409 entry_info->filename[len - 4] = '\0';
2410 StrAllocCopy(entry_info->type, ENTRY_IS_DIRECTORY);
2411 remove_size = TRUE; /* size is not useful */
2412 }
2413 /*
2414 * Goto the bottom and get real type.
2415 */
2416 break;
2417
2418 case MS_WINDOWS_SERVER:
2419 /*
2420 * Interpret and edit LIST output from MS_WINDOWS server and convert
2421 * information lines to zero length.
2422 */
2423 parse_ms_windows_dir_entry(entry, entry_info);
2424
2425 /*
2426 * Get rid of any junk lines.
2427 */
2428 if (!entry_info->display)
2429 return (entry_info);
2430 if (entry_info->type && *entry_info->type == '\0') {
2431 FREE(entry_info->type);
2432 return (entry_info);
2433 }
2434 /*
2435 * Goto the bottom and get real type.
2436 */
2437 break;
2438
2439 #ifdef NOTDEFINED
2440 case WINDOWS_NT_SERVER:
2441 /*
2442 * Interpret and edit LIST output from MS_WINDOWS server and convert
2443 * information lines to zero length.
2444 */
2445 parse_windows_nt_dir_entry(entry, entry_info);
2446
2447 /*
2448 * Get rid of any junk lines.
2449 */
2450 if (!entry_info->display)
2451 return (entry_info);
2452 if (entry_info->type && *entry_info->type == '\0') {
2453 FREE(entry_info->type);
2454 return (entry_info);
2455 }
2456 /*
2457 * Goto the bottom and get real type.
2458 */
2459 break;
2460 #endif /* NOTDEFINED */
2461
2462 case CMS_SERVER:
2463 {
2464 /*
2465 * Interpret and edit LIST output from VM/CMS server and convert
2466 * any information lines to zero length.
2467 */
2468 parse_cms_dir_entry(entry, entry_info);
2469
2470 /*
2471 * Get rid of any junk lines.
2472 */
2473 if (!entry_info->display)
2474 return (entry_info);
2475 if (entry_info->type && *entry_info->type == '\0') {
2476 FREE(entry_info->type);
2477 return (entry_info);
2478 }
2479 /*
2480 * Goto the bottom and get real type.
2481 */
2482 break;
2483 }
2484
2485 case NCSA_SERVER:
2486 case TCPC_SERVER:
2487 /*
2488 * Directories identified by trailing "/" characters.
2489 */
2490 StrAllocCopy(entry_info->filename, entry);
2491 len = (int) strlen(entry);
2492 if (entry[len - 1] == '/') {
2493 /*
2494 * It's a dir, remove / and mark it as such.
2495 */
2496 entry[len - 1] = '\0';
2497 StrAllocCopy(entry_info->type, ENTRY_IS_DIRECTORY);
2498 remove_size = TRUE; /* size is not useful */
2499 }
2500 /*
2501 * Goto the bottom and get real type.
2502 */
2503 break;
2504
2505 default:
2506 /*
2507 * We can't tell if it is a directory since we only did an NLST :( List
2508 * bad file types anyways? NOT!
2509 */
2510 StrAllocCopy(entry_info->filename, entry);
2511 return (entry_info); /* mostly empty info */
2512
2513 } /* switch (server_type) */
2514
2515 #ifdef LONG_LIST
2516 (void) remove_size;
2517 #else
2518 if (remove_size && entry_info->size) {
2519 entry_info->size = 0;
2520 }
2521 #endif
2522
2523 if (isEmpty(entry_info->filename)) {
2524 entry_info->display = FALSE;
2525 return (entry_info);
2526 }
2527 if (strlen(entry_info->filename) > 3) {
2528 if (((cp = strrchr(entry_info->filename, '.')) != NULL &&
2529 0 == strncasecomp(cp, ".me", 3)) &&
2530 (cp[3] == '\0' || cp[3] == ';')) {
2531 /*
2532 * Don't treat this as application/x-Troff-me if it's a Unix server
2533 * but has the string "read.me", or if it's not a Unix server. -
2534 * FM
2535 */
2536 if ((server_type != UNIX_SERVER) ||
2537 (cp > (entry_info->filename + 3) &&
2538 0 == strncasecomp((cp - 4), "read.me", 7))) {
2539 StrAllocCopy(entry_info->type, STR_PLAINTEXT);
2540 }
2541 }
2542 }
2543
2544 /*
2545 * Get real types eventually.
2546 */
2547 if (!entry_info->type) {
2548 const char *cp2;
2549 HTFormat format;
2550 HTAtom *encoding; /* @@ not used at all */
2551
2552 format = HTFileFormat(entry_info->filename, &encoding, &cp2);
2553
2554 if (cp2 == NULL) {
2555 if (!StrNCmp(HTAtom_name(format), "application", 11)) {
2556 cp2 = HTAtom_name(format) + 12;
2557 if (!StrNCmp(cp2, "x-", 2))
2558 cp2 += 2;
2559 } else {
2560 cp2 = HTAtom_name(format);
2561 }
2562 }
2563
2564 StrAllocCopy(entry_info->type, cp2);
2565 }
2566
2567 return (entry_info);
2568 }
2569
2570 static void formatDate(char target[16], EntryInfo *entry)
2571 {
2572 char temp[8], month[4];
2573 int i;
2574
2575 /*
2576 * Set up for sorting in reverse chronological order. - FM
2577 */
2578 if (entry->date[9] == ':') {
2579 strcpy(target, "9999");
2580 LYStrNCpy(temp, &entry->date[7], 5);
2581 if (temp[0] == ' ') {
2582 temp[0] = '0';
2583 }
2584 } else {
2585 LYStrNCpy(target, &entry->date[8], 4);
2586 strcpy(temp, "00:00");
2587 }
2588 LYStrNCpy(month, entry->date, 3);
2589 for (i = 0; i < 12; i++) {
2590 if (!strcasecomp(month, months[i])) {
2591 break;
2592 }
2593 }
2594 i++;
2595 sprintf(month, "%02d", i % 100);
2596 strcat(target, month);
2597 StrNCat(target, &entry->date[4], 2);
2598 if (target[6] == ' ' || target[6] == HT_NON_BREAK_SPACE) {
2599 target[6] = '0';
2600 }
2601
2602 /* If no year given, assume last year if it would otherwise be in the
2603 * future by more than one day. The one day tolerance is to account for a
2604 * possible timezone difference. - kw
2605 */
2606 if (target[0] == '9' && atoi(target) > TheDate + 1) {
2607 for (i = 0; i < 4; i++) {
2608 target[i] = LastYear[i];
2609 }
2610 }
2611 strcat(target, temp);
2612 }
2613
2614 static int compare_EntryInfo_structs(EntryInfo *entry1, EntryInfo *entry2)
2615 {
2616 int status;
2617 char date1[16], date2[16];
2618 int result = strcmp(entry1->filename, entry2->filename);
2619
2620 switch (HTfileSortMethod) {
2621 case FILE_BY_SIZE:
2622 /* both equal or both 0 */
2623 if (entry1->size > entry2->size)
2624 result = 1;
2625 else if (entry1->size < entry2->size)
2626 result = -1;
2627 break;
2628
2629 case FILE_BY_TYPE:
2630 if (entry1->type && entry2->type) {
2631 status = strcasecomp(entry1->type, entry2->type);
2632 if (status)
2633 result = status;
2634 }
2635 break;
2636
2637 case FILE_BY_DATE:
2638 if (entry1->date && entry2->date &&
2639 strlen(entry1->date) == 12 &&
2640 strlen(entry2->date) == 12) {
2641 /*
2642 * Set the years and date, if we don't have them yet.
2643 */
2644 if (!HaveYears) {
2645 set_years_and_date();
2646 }
2647 formatDate(date1, entry1);
2648 formatDate(date2, entry2);
2649 /*
2650 * Do the comparison. - FM
2651 */
2652 status = strcasecomp(date2, date1);
2653 if (status)
2654 result = status;
2655 }
2656 break;
2657
2658 case FILE_BY_NAME:
2659 default:
2660 break;
2661 }
2662 return result;
2663 }
2664
2665 #ifdef LONG_LIST
2666 static char *FormatStr(char **bufp,
2667 char *start,
2668 const char *value)
2669 {
2670 char fmt[512];
2671
2672 if (*start) {
2673 sprintf(fmt, "%%%.*ss", (int) sizeof(fmt) - 3, start);
2674 HTSprintf(bufp, HT_FMT("%s", fmt), value);
2675 } else if (*bufp && !(value && *value)) {
2676 ;
2677 } else if (value) {
2678 StrAllocCat(*bufp, value);
2679 }
2680 return *bufp;
2681 }
2682
2683 static char *FormatSize(char **bufp,
2684 char *start,
2685 off_t value)
2686 {
2687 char fmt[512];
2688
2689 if (*start) {
2690 sprintf(fmt, "%%%.*s" PRI_off_t,
2691 (int) sizeof(fmt) - DigitsOf(start) - 3, start);
2692
2693 HTSprintf(bufp, HT_FMT("%" PRI_off_t, fmt), value);
2694 } else {
2695 sprintf(fmt, "%" PRI_off_t, CAST_off_t (value));
2696
2697 StrAllocCat(*bufp, fmt);
2698 }
2699 return *bufp;
2700 }
2701
2702 static char *FormatNum(char **bufp,
2703 char *start,
2704 unsigned long value)
2705 {
2706 char fmt[512];
2707
2708 if (*start) {
2709 sprintf(fmt, "%%%.*sld",
2710 (int) sizeof(fmt) - DigitsOf(start) - 3, start);
2711 HTSprintf(bufp, HT_FMT("%ld", fmt), value);
2712 } else {
2713 sprintf(fmt, "%lu", value);
2714 StrAllocCat(*bufp, fmt);
2715 }
2716 return *bufp;
2717 }
2718
2719 static void FlushParse(HTStructured * target, char **buf)
2720 {
2721 if (*buf && **buf) {
2722 PUTS(*buf);
2723 **buf = '\0';
2724 }
2725 }
2726
2727 static void LYListFmtParse(const char *fmtstr,
2728 EntryInfo *data,
2729 HTStructured * target,
2730 char *tail)
2731 {
2732 char c;
2733 char *s;
2734 char *end;
2735 char *start;
2736 char *str = NULL;
2737 char *buf = NULL;
2738 BOOL is_directory = (BOOL) (data->file_mode != NULL &&
2739 (TOUPPER(data->file_mode[0]) == 'D'));
2740 BOOL is_symlinked = (BOOL) (data->file_mode != NULL &&
2741 (TOUPPER(data->file_mode[0]) == 'L'));
2742 BOOL remove_size = (BOOL) (is_directory || is_symlinked);
2743
2744 StrAllocCopy(str, fmtstr);
2745 s = str;
2746 end = str + strlen(str);
2747 while (*s) {
2748 start = s;
2749 while (*s) {
2750 if (*s == '%') {
2751 if (*(s + 1) == '%') /* literal % */
2752 s++;
2753 else
2754 break;
2755 }
2756 s++;
2757 }
2758 /* s is positioned either at a % or at \0 */
2759 *s = '\0';
2760 if (s > start) { /* some literal chars. */
2761 StrAllocCat(buf, start);
2762 }
2763 if (s == end)
2764 break;
2765 start = ++s;
2766 while (isdigit(UCH(*s)) || *s == '.' || *s == '-' || *s == ' ' ||
2767 *s == '#' || *s == '+' || *s == '\'')
2768 s++;
2769 c = *s; /* the format char. or \0 */
2770 *s = '\0';
2771
2772 switch (c) {
2773 case '\0':
2774 StrAllocCat(buf, start);
2775 continue;
2776
2777 case 'A':
2778 case 'a': /* anchor */
2779 FlushParse(target, &buf);
2780 HTDirEntry(target, tail, data->filename);
2781 FormatStr(&buf, start, data->filename);
2782 PUTS(buf);
2783 END(HTML_A);
2784 if (buf != NULL)
2785 *buf = '\0';
2786 if (c != 'A' && data->linkname != NULL) {
2787 PUTS(" -> ");
2788 PUTS(data->linkname);
2789 }
2790 break;
2791
2792 case 'T': /* MIME type */
2793 case 't': /* MIME type description */
2794 if (is_directory) {
2795 if (c != 'T') {
2796 FormatStr(&buf, start, ENTRY_IS_DIRECTORY);
2797 } else {
2798 FormatStr(&buf, start, "");
2799 }
2800 } else if (is_symlinked) {
2801 if (c != 'T') {
2802 FormatStr(&buf, start, ENTRY_IS_SYMBOLIC_LINK);
2803 } else {
2804 FormatStr(&buf, start, "");
2805 }
2806 } else {
2807 const char *cp2;
2808 HTFormat format;
2809
2810 format = HTFileFormat(data->filename, NULL, &cp2);
2811
2812 if (c != 'T') {
2813 if (cp2 == NULL) {
2814 if (!StrNCmp(HTAtom_name(format),
2815 "application", 11)) {
2816 cp2 = HTAtom_name(format) + 12;
2817 if (!StrNCmp(cp2, "x-", 2))
2818 cp2 += 2;
2819 } else {
2820 cp2 = HTAtom_name(format);
2821 }
2822 }
2823 FormatStr(&buf, start, cp2);
2824 } else {
2825 FormatStr(&buf, start, HTAtom_name(format));
2826 }
2827 }
2828 break;
2829
2830 case 'd': /* date */
2831 if (data->date) {
2832 FormatStr(&buf, start, data->date);
2833 } else {
2834 FormatStr(&buf, start, " * ");
2835 }
2836 break;
2837
2838 case 's': /* size in bytes */
2839 FormatSize(&buf, start, data->size);
2840 break;
2841
2842 case 'K': /* size in Kilobytes but not for directories */
2843 if (remove_size) {
2844 FormatStr(&buf, start, "");
2845 StrAllocCat(buf, " ");
2846 break;
2847 }
2848 /* FALL THROUGH */
2849 case 'k': /* size in Kilobytes */
2850 /* FIXME - this is inconsistent with HTFile.c, but historical */
2851 if (data->size < 1024) {
2852 FormatSize(&buf, start, data->size);
2853 StrAllocCat(buf, " bytes");
2854 } else {
2855 FormatSize(&buf, start, data->size / 1024);
2856 StrAllocCat(buf, "Kb");
2857 }
2858 break;
2859
2860 #ifdef LONG_LIST
2861 case 'p': /* unix-style permission bits */
2862 FormatStr(&buf, start, NonNull(data->file_mode));
2863 break;
2864
2865 case 'o': /* owner */
2866 FormatStr(&buf, start, NonNull(data->file_user));
2867 break;
2868
2869 case 'g': /* group */
2870 FormatStr(&buf, start, NonNull(data->file_group));
2871 break;
2872
2873 case 'l': /* link count */
2874 FormatNum(&buf, start, data->file_links);
2875 break;
2876 #endif
2877
2878 case '%': /* literal % with flags/width */
2879 FormatStr(&buf, start, "%");
2880 break;
2881
2882 default:
2883 fprintf(stderr,
2884 "Unknown format character `%c' in list format\n", c);
2885 break;
2886 }
2887
2888 s++;
2889 }
2890 if (buf) {
2891 LYTrimTrailing(buf);
2892 FlushParse(target, &buf);
2893 FREE(buf);
2894 }
2895 PUTC('\n');
2896 FREE(str);
2897 }
2898 #endif /* LONG_LIST */
2899
2900 /* Read a directory into an hypertext object from the data socket
2901 * --------------------------------------------------------------
2902 *
2903 * On entry,
2904 * anchor Parent anchor to link the this node to
2905 * address Address of the directory
2906 * On exit,
2907 * returns HT_LOADED if OK
2908 * <0 if error.
2909 */
2910 static int read_directory(HTParentAnchor *parent,
2911 const char *address,
2912 HTFormat format_out,
2913 HTStream *sink)
2914 {
2915 int status;
2916 BOOLEAN WasInterrupted = FALSE;
2917 HTStructured *target = HTML_new(parent, format_out, sink);
2918 char *filename = HTParse(address, "", PARSE_PATH + PARSE_PUNCTUATION);
2919 EntryInfo *entry_info;
2920 BOOLEAN first = TRUE;
2921 char *lastpath = NULL; /* prefix for link, either "" (for root) or xxx */
2922 BOOL tildeIsTop = FALSE;
2923
2924 #ifndef LONG_LIST
2925 char string_buffer[64];
2926 #endif
2927
2928 _HTProgress(gettext("Receiving FTP directory."));
2929
2930 /*
2931 * Force the current Date and Year (TheDate, ThisYear, and LastYear) to be
2932 * recalculated for each directory request. Otherwise we have a problem
2933 * with long-running sessions assuming the wrong date for today. - kw
2934 */
2935 HaveYears = FALSE;
2936 /*
2937 * Check whether we always want the home directory treated as Welcome. -
2938 * FM
2939 */
2940 if (server_type == VMS_SERVER)
2941 tildeIsTop = TRUE;
2942
2943 /*
2944 * This should always come back FALSE, since the flag is set only for local
2945 * directory listings if LONG_LIST was defined on compilation, but we could
2946 * someday set up an equivalent listing for Unix ftp servers. - FM
2947 */
2948 (void) HTDirTitles(target, parent, format_out, tildeIsTop);
2949
2950 data_read_pointer = data_write_pointer = data_buffer;
2951
2952 if (*filename == '\0') { /* Empty filename: use root. */
2953 StrAllocCopy(lastpath, "/");
2954 } else if (!strcmp(filename, "/")) { /* Root path. */
2955 StrAllocCopy(lastpath, "/foo/..");
2956 } else {
2957 char *p = strrchr(filename, '/'); /* Find the lastslash. */
2958 char *cp;
2959
2960 if (server_type == CMS_SERVER) {
2961 StrAllocCopy(lastpath, filename); /* Use absolute path for CMS. */
2962 } else {
2963 StrAllocCopy(lastpath, p + 1); /* Take slash off the beginning. */
2964 }
2965 if ((cp = strrchr(lastpath, ';')) != NULL) { /* Trim type= param. */
2966 if (!strncasecomp((cp + 1), "type=", 5)) {
2967 if (TOUPPER(*(cp + 6)) == 'D' ||
2968 TOUPPER(*(cp + 6)) == 'A' ||
2969 TOUPPER(*(cp + 6)) == 'I')
2970 *cp = '\0';
2971 }
2972 }
2973 }
2974 FREE(filename);
2975
2976 {
2977 HTBTree *bt = HTBTree_new((HTComparer) compare_EntryInfo_structs);
2978 int ic;
2979 HTChunk *chunk = HTChunkCreate(128);
2980 int BytesReceived = 0;
2981 int BytesReported = 0;
2982 char NumBytes[64];
2983 char *spilledname = NULL;
2984
2985 PUTC('\n'); /* prettier LJM */
2986 for (ic = 0; ic != EOF;) { /* For each entry in the directory */
2987 HTChunkClear(chunk);
2988
2989 if (HTCheckForInterrupt()) {
2990 CTRACE((tfp,
2991 "read_directory: interrupted after %d bytes\n",
2992 BytesReceived));
2993 WasInterrupted = TRUE;
2994 if (BytesReceived) {
2995 goto unload_btree; /* unload btree */
2996 } else {
2997 ABORT_TARGET;
2998 HTBTreeAndObject_free(bt);
2999 FREE(spilledname);
3000 HTChunkFree(chunk);
3001 return HT_INTERRUPTED;
3002 }
3003 }
3004
3005 /* read directory entry
3006 */
3007 interrupted_in_next_data_char = FALSE;
3008 for (;;) { /* Read in one line as filename */
3009 ic = NEXT_DATA_CHAR;
3010 AgainForMultiNet:
3011 if (interrupted_in_next_data_char) {
3012 CTRACE((tfp,
3013 "read_directory: interrupted_in_next_data_char after %d bytes\n",
3014 BytesReceived));
3015 WasInterrupted = TRUE;
3016 if (BytesReceived) {
3017 goto unload_btree; /* unload btree */
3018 } else {
3019 ABORT_TARGET;
3020 HTBTreeAndObject_free(bt);
3021 FREE(spilledname);
3022 HTChunkFree(chunk);
3023 return HT_INTERRUPTED;
3024 }
3025 } else if ((char) ic == CR || (char) ic == LF) { /* Terminator? */
3026 if (chunk->size != 0) { /* got some text */
3027 /* Deal with MultiNet's wrapping of long lines */
3028 if (server_type == VMS_SERVER) {
3029 /* Deal with MultiNet's wrapping of long lines - F.M. */
3030 if (data_read_pointer < data_write_pointer &&
3031 *(data_read_pointer + 1) == ' ')
3032 data_read_pointer++;
3033 else if (data_read_pointer >= data_write_pointer) {
3034 status = NETREAD(data_soc, data_buffer,
3035 DATA_BUFFER_SIZE);
3036 if (status == HT_INTERRUPTED) {
3037 interrupted_in_next_data_char = 1;
3038 goto AgainForMultiNet;
3039 }
3040 if (status <= 0) {
3041 ic = EOF;
3042 break;
3043 }
3044 data_write_pointer = data_buffer + status;
3045 data_read_pointer = data_buffer;
3046 if (*data_read_pointer == ' ')
3047 data_read_pointer++;
3048 else
3049 break;
3050 } else
3051 break;
3052 } else
3053 break; /* finish getting one entry */
3054 }
3055 } else if (ic == EOF) {
3056 break; /* End of file */
3057 } else {
3058 HTChunkPutc(chunk, UCH(ic));
3059 }
3060 }
3061 HTChunkTerminate(chunk);
3062
3063 BytesReceived += chunk->size;
3064 if (BytesReceived > BytesReported + 1024) {
3065 #ifdef _WINDOWS
3066 sprintf(NumBytes, LY_MSG("Transferred %d bytes (%5d)"),
3067 BytesReceived, ws_read_per_sec);
3068 #else
3069 sprintf(NumBytes, TRANSFERRED_X_BYTES, BytesReceived);
3070 #endif
3071 HTProgress(NumBytes);
3072 BytesReported = BytesReceived;
3073 }
3074
3075 if (ic == EOF && chunk->size == 1)
3076 /* 1 means empty: includes terminating 0 */
3077 break;
3078 CTRACE((tfp, "HTFTP: Line in %s is %s\n",
3079 lastpath, chunk->data));
3080
3081 entry_info = parse_dir_entry(chunk->data, &first, &spilledname);
3082 if (entry_info->display) {
3083 FREE(spilledname);
3084 CTRACE((tfp, "Adding file to BTree: %s\n",
3085 entry_info->filename));
3086 HTBTree_add(bt, entry_info);
3087 } else {
3088 free_entryinfo_struct_contents(entry_info);
3089 FREE(entry_info);
3090 }
3091
3092 } /* next entry */
3093
3094 unload_btree:
3095
3096 HTChunkFree(chunk);
3097 FREE(spilledname);
3098
3099 /* print out the handy help message if it exists :) */
3100 if (help_message_cache_non_empty()) {
3101 START(HTML_PRE);
3102 START(HTML_HR);
3103 PUTC('\n');
3104 PUTS(help_message_cache_contents());
3105 init_help_message_cache(); /* to free memory */
3106 START(HTML_HR);
3107 PUTC('\n');
3108 } else {
3109 START(HTML_PRE);
3110 PUTC('\n');
3111 }
3112
3113 /* Run through tree printing out in order
3114 */
3115 {
3116 #ifndef LONG_LIST
3117 #ifdef SH_EX /* 1997/10/18 (Sat) 14:14:28 */
3118 char *p, name_buff[256];
3119 int name_len, dot_len;
3120
3121 #define FNAME_WIDTH 30
3122 #define FILE_GAP 1
3123
3124 #endif
3125 int i;
3126 #endif
3127 HTBTElement *ele;
3128
3129 for (ele = HTBTree_next(bt, NULL);
3130 ele != NULL;
3131 ele = HTBTree_next(bt, ele)) {
3132 entry_info = (EntryInfo *) HTBTree_object(ele);
3133
3134 #ifdef LONG_LIST
3135 LYListFmtParse(ftp_format,
3136 entry_info,
3137 target,
3138 lastpath);
3139 #else
3140 if (entry_info->date) {
3141 PUTS(entry_info->date);
3142 PUTS(" ");
3143 } else {
3144 PUTS(" * ");
3145 }
3146
3147 if (entry_info->type) {
3148 for (i = 0; entry_info->type[i] != '\0' && i < 16; i++)
3149 PUTC(entry_info->type[i]);
3150 for (; i < 17; i++)
3151 PUTC(' ');
3152 }
3153 /* start the anchor */
3154 HTDirEntry(target, lastpath, entry_info->filename);
3155 #ifdef SH_EX /* 1997/10/18 (Sat) 16:00 */
3156 name_len = strlen(entry_info->filename);
3157
3158 sprintf(name_buff, "%-*s", FNAME_WIDTH, entry_info->filename);
3159
3160 if (name_len < FNAME_WIDTH) {
3161 dot_len = FNAME_WIDTH - FILE_GAP - name_len;
3162 if (dot_len > 0) {
3163 p = name_buff + name_len + 1;
3164 while (dot_len-- > 0)
3165 *p++ = '.';
3166 }
3167 } else {
3168 name_buff[FNAME_WIDTH] = '\0';
3169 }
3170
3171 PUTS(name_buff);
3172 #else
3173 PUTS(entry_info->filename);
3174 #endif
3175 END(HTML_A);
3176
3177 if (entry_info->size) {
3178 #ifdef SH_EX /* 1998/02/02 (Mon) 16:34:52 */
3179 if (entry_info->size < 1024)
3180 sprintf(string_buffer, "%6ld bytes",
3181 entry_info->size);
3182 else
3183 sprintf(string_buffer, "%6ld Kb",
3184 entry_info->size / 1024);
3185 #else
3186 if (entry_info->size < 1024)
3187 sprintf(string_buffer, " %lu bytes",
3188 (unsigned long) entry_info->size);
3189 else
3190 sprintf(string_buffer, " %luKb",
3191 (unsigned long) entry_info->size / 1024);
3192 #endif
3193 PUTS(string_buffer);
3194 } else if (entry_info->linkname != 0) {
3195 PUTS(" -> ");
3196 PUTS(entry_info->linkname);
3197 }
3198
3199 PUTC('\n'); /* end of this entry */
3200 #endif
3201
3202 free_entryinfo_struct_contents(entry_info);
3203 }
3204 }
3205 END(HTML_PRE);
3206 END(HTML_BODY);
3207 FREE_TARGET;
3208 HTBTreeAndObject_free(bt);
3209 }
3210
3211 FREE(lastpath);
3212
3213 if (WasInterrupted || data_soc != -1) { /* should always be true */
3214 /*
3215 * Without closing the data socket first, the response(NULL) later may
3216 * hang. Some servers expect the client to fin/ack the close of the
3217 * data connection before proceeding with the conversation on the
3218 * control connection. - kw
3219 */
3220 CTRACE((tfp, "HTFTP: Closing data socket %d\n", data_soc));
3221 status = NETCLOSE(data_soc);
3222 if (status == -1)
3223 HTInetStatus("close"); /* Comment only */
3224 data_soc = -1;
3225 }
3226
3227 if (WasInterrupted || HTCheckForInterrupt()) {
3228 _HTProgress(TRANSFER_INTERRUPTED);
3229 }
3230 return HT_LOADED;
3231 }
3232
3233 /*
3234 * Setup an FTP connection.
3235 */
3236 static int setup_connection(const char *name,
3237 HTParentAnchor *anchor)
3238 {
3239 int retry; /* How many times tried? */
3240 int status = HT_NO_CONNECTION;
3241
3242 CTRACE((tfp, "setup_connection(%s)\n", name));
3243
3244 /* set use_list to NOT since we don't know what kind of server
3245 * this is yet. And set the type to GENERIC
3246 */
3247 use_list = FALSE;
3248 server_type = GENERIC_SERVER;
3249 Broken_RETR = FALSE;
3250
3251 #ifdef INET6
3252 Broken_EPSV = FALSE;
3253 #endif
3254
3255 for (retry = 0; retry < 2; retry++) { /* For timed out/broken connections */
3256 status = get_connection(name, anchor);
3257 if (status < 0) {
3258 break;
3259 }
3260
3261 if (!ftp_local_passive) {
3262 status = get_listen_socket();
3263 if (status < 0) {
3264 NETCLOSE(control->socket);
3265 control->socket = -1;
3266 #ifdef INET6
3267 if (have_socket)
3268 (void) close_master_socket();
3269 #else
3270 close_master_socket();
3271 #endif /* INET6 */
3272 /* HT_INTERRUPTED would fall through, if we could interrupt
3273 somehow in the middle of it, which we currently can't. */
3274 break;
3275 }
3276 #ifdef REPEAT_PORT
3277 /* Inform the server of the port number we will listen on
3278 */
3279 status = response(port_command);
3280 FREE(port_command);
3281 if (status == HT_INTERRUPTED) {
3282 CTRACE((tfp, "HTFTP: Interrupted in response (port_command)\n"));
3283 _HTProgress(CONNECTION_INTERRUPTED);
3284 NETCLOSE(control->socket);
3285 control->socket = -1;
3286 close_master_socket();
3287 status = HT_INTERRUPTED;
3288 break;
3289 }
3290 if (status != 2) { /* Could have timed out */
3291 if (status < 0)
3292 continue; /* try again - net error */
3293 status = -status; /* bad reply */
3294 break;
3295 }
3296 CTRACE((tfp, "HTFTP: Port defined.\n"));
3297 #endif /* REPEAT_PORT */
3298 } else { /* Tell the server to be passive */
3299 char *command = NULL;
3300 const char *p = "?";
3301 int h0, h1, h2, h3, p0, p1; /* Parts of reply */
3302
3303 #ifdef INET6
3304 char dst[LINE_LENGTH + 1];
3305 #endif
3306
3307 data_soc = status;
3308
3309 #ifdef INET6
3310 /* see RFC 2428 */
3311 if (Broken_EPSV)
3312 status = 1;
3313 else
3314 status = send_cmd_1(p = "EPSV");
3315 if (status < 0) /* retry or Bad return */
3316 continue;
3317 else if (status != 2) {
3318 status = send_cmd_1(p = "PASV");
3319 if (status < 0) { /* retry or Bad return */
3320 continue;
3321 } else if (status != 2) {
3322 status = -status; /* bad reply */
3323 break;
3324 }
3325 }
3326
3327 if (strcmp(p, "PASV") == 0) {
3328 for (p = response_text; *p && *p != ','; p++) {
3329 ; /* null body */
3330 }
3331
3332 while (--p > response_text && '0' <= *p && *p <= '9') {
3333 ; /* null body */
3334 }
3335 status = sscanf(p + 1, "%d,%d,%d,%d,%d,%d",
3336 &h0, &h1, &h2, &h3, &p0, &p1);
3337 if (status < 4) {
3338 fprintf(tfp, "HTFTP: PASV reply has no inet address!\n");
3339 status = HT_NO_CONNECTION;
3340 break;
3341 }
3342 passive_port = (PortNumber) ((p0 << 8) + p1);
3343 sprintf(dst, "%d.%d.%d.%d", h0, h1, h2, h3);
3344 } else if (strcmp(p, "EPSV") == 0) {
3345 char c0, c1, c2, c3;
3346 LY_SOCKADDR ss;
3347 LY_SOCKLEN sslen;
3348
3349 /*
3350 * EPSV bla (|||port|)
3351 */
3352 for (p = response_text; *p && !isspace(UCH(*p)); p++) {
3353 ; /* null body */
3354 }
3355 for ( /*nothing */ ;
3356 *p && *p != '(';
3357 p++) { /*) */
3358 ; /* null body */
3359 }
3360 status = sscanf(p, "(%c%c%c%d%c)", &c0, &c1, &c2, &p0, &c3);
3361 if (status != 5) {
3362 fprintf(tfp, "HTFTP: EPSV reply has invalid format!\n");
3363 status = HT_NO_CONNECTION;
3364 break;
3365 }
3366 passive_port = (PortNumber) p0;
3367
3368 sslen = (LY_SOCKLEN) sizeof(ss);
3369 if (getpeername(control->socket, SOCKADDR_OF(ss), &sslen) < 0) {
3370 fprintf(tfp, "HTFTP: getpeername(control) failed\n");
3371 status = HT_NO_CONNECTION;
3372 break;
3373 }
3374 if (getnameinfo(SOCKADDR_OF(ss),
3375 sslen,
3376 dst,
3377 (socklen_t) sizeof(dst),
3378 NULL, 0, NI_NUMERICHOST)) {
3379 fprintf(tfp, "HTFTP: getnameinfo failed\n");
3380 status = HT_NO_CONNECTION;
3381 break;
3382 }
3383 }
3384 #else
3385 status = send_cmd_1("PASV");
3386 if (status != 2) {
3387 if (status < 0)
3388 continue; /* retry or Bad return */
3389 status = -status; /* bad reply */
3390 break;
3391 }
3392 for (p = response_text; *p && *p != ','; p++) {
3393 ; /* null body */
3394 }
3395
3396 while (--p > response_text && '0' <= *p && *p <= '9') {
3397 ; /* null body */
3398 }
3399
3400 status = sscanf(p + 1, "%d,%d,%d,%d,%d,%d",
3401 &h0, &h1, &h2, &h3, &p0, &p1);
3402 if (status < 4) {
3403 fprintf(tfp, "HTFTP: PASV reply has no inet address!\n");
3404 status = HT_NO_CONNECTION;
3405 break;
3406 }
3407 passive_port = (PortNumber) ((p0 << 8) + p1);
3408 #endif /* INET6 */
3409 CTRACE((tfp, "HTFTP: Server is listening on port %d\n",
3410 passive_port));
3411
3412 /* Open connection for data: */
3413
3414 #ifdef INET6
3415 HTSprintf0(&command, "%s//%s:%d/", STR_FTP_URL, dst, passive_port);
3416 #else
3417 HTSprintf0(&command, "%s//%d.%d.%d.%d:%d/",
3418 STR_FTP_URL, h0, h1, h2, h3, passive_port);
3419 #endif
3420 status = HTDoConnect(command, "FTP data", passive_port, &data_soc);
3421 FREE(command);
3422
3423 if (status < 0) {
3424 (void) HTInetStatus(gettext("connect for data"));
3425 NETCLOSE(data_soc);
3426 break;
3427 }
3428
3429 CTRACE((tfp, "FTP data connected, socket %d\n", data_soc));
3430 }
3431 status = 0;
3432 break; /* No more retries */
3433
3434 } /* for retries */
3435 CTRACE((tfp, "setup_connection returns %d\n", status));
3436 return status;
3437 }
3438
3439 /* Retrieve File from Server
3440 * -------------------------
3441 *
3442 * On entry,
3443 * name WWW address of a file: document, including hostname
3444 * On exit,
3445 * returns Socket number for file if good.
3446 * <0 if bad.
3447 */
3448 int HTFTPLoad(const char *name,
3449 HTParentAnchor *anchor,
3450 HTFormat format_out,
3451 HTStream *sink)
3452 {
3453 BOOL isDirectory = NO;
3454 HTAtom *encoding = NULL;
3455 int status, final_status;
3456 int outstanding = 1; /* outstanding control connection responses
3457
3458 that we are willing to wait for, if we
3459 get to the point of reading data - kw */
3460 HTFormat format;
3461
3462 CTRACE((tfp, "HTFTPLoad(%s) %s connection\n",
3463 name,
3464 (ftp_local_passive
3465 ? "passive"
3466 : "normal")));
3467
3468 HTReadProgress((off_t) 0, (off_t) 0);
3469
3470 status = setup_connection(name, anchor);
3471 if (status < 0)
3472 return status; /* Failed with this code */
3473
3474 /* Ask for the file:
3475 */
3476 {
3477 char *filename = HTParse(name, "", PARSE_PATH + PARSE_PUNCTUATION);
3478 char *fname = filename; /* Save for subsequent free() */
3479 char *vmsname = NULL;
3480 BOOL binary;
3481 const char *type = NULL;
3482 char *types = NULL;
3483 char *cp;
3484
3485 if (server_type == CMS_SERVER) {
3486 /* If the unescaped path has a %2f, reject it as illegal. - FM */
3487 if (((cp = strstr(filename, "%2")) != NULL) &&
3488 TOUPPER(cp[2]) == 'F') {
3489 FREE(fname);
3490 init_help_message_cache(); /* to free memory */
3491 NETCLOSE(control->socket);
3492 control->socket = -1;
3493 CTRACE((tfp,
3494 "HTFTP: Rejecting path due to illegal escaped slash.\n"));
3495 return -1;
3496 }
3497 }
3498
3499 if (!*filename) {
3500 StrAllocCopy(filename, "/");
3501 type = "D";
3502 } else if ((type = types = strrchr(filename, ';')) != NULL) {
3503 /*
3504 * Check and trim the type= parameter. - FM
3505 */
3506 if (!strncasecomp((type + 1), "type=", 5)) {
3507 switch (TOUPPER(*(type + 6))) {
3508 case 'D':
3509 *types = '\0';
3510 type = "D";
3511 break;
3512 case 'A':
3513 *types = '\0';
3514 type = "A";
3515 break;
3516 case 'I':
3517 *types = '\0';
3518 type = "I";
3519 break;
3520 default:
3521 type = "";
3522 break;
3523 }
3524 if (!*filename) {
3525 *filename = '/';
3526 *(filename + 1) = '\0';
3527 }
3528 }
3529 if (*type != '\0') {
3530 CTRACE((tfp, "HTFTP: type=%s\n", type));
3531 }
3532 }
3533 HTUnEscape(filename);
3534 CTRACE((tfp, "HTFTP: UnEscaped %s\n", filename));
3535 if (filename[1] == '~') {
3536 /*
3537 * Check if translation of HOME as tilde is supported,
3538 * and adjust filename if so. - FM
3539 */
3540 char *cp2 = NULL;
3541 char *fn = NULL;
3542
3543 if ((cp2 = StrChr((filename + 1), '/')) != NULL) {
3544 *cp2 = '\0';
3545 }
3546 status = send_cmd_1("PWD");
3547 if (status == 2 && response_text[5] == '/') {
3548 status = send_cwd(filename + 1);
3549 if (status == 2) {
3550 StrAllocCopy(fn, (filename + 1));
3551 if (cp2) {
3552 *cp2 = '/';
3553 if (fn[strlen(fn) - 1] != '/') {
3554 StrAllocCat(fn, cp2);
3555 } else {
3556 StrAllocCat(fn, (cp2 + 1));
3557 }
3558 cp2 = NULL;
3559 }
3560 FREE(fname);
3561 fname = filename = fn;
3562 }
3563 }
3564 if (cp2) {
3565 *cp2 = '/';
3566 }
3567 }
3568 if (strlen(filename) > 3) {
3569 char *cp2;
3570
3571 if (((cp2 = strrchr(filename, '.')) != NULL &&
3572 0 == strncasecomp(cp2, ".me", 3)) &&
3573 (cp2[3] == '\0' || cp2[3] == ';')) {
3574 /*
3575 * Don't treat this as application/x-Troff-me if it's a Unix
3576 * server but has the string "read.me", or if it's not a Unix
3577 * server. - FM
3578 */
3579 if ((server_type != UNIX_SERVER) ||
3580 (cp2 > (filename + 3) &&
3581 0 == strncasecomp((cp2 - 4), "read.me", 7))) {
3582 *cp2 = '\0';
3583 format = HTFileFormat(filename, &encoding, NULL);
3584 *cp2 = '.';
3585 } else {
3586 format = HTFileFormat(filename, &encoding, NULL);
3587 }
3588 } else {
3589 format = HTFileFormat(filename, &encoding, NULL);
3590 }
3591 } else {
3592 format = HTFileFormat(filename, &encoding, NULL);
3593 }
3594 format = HTCharsetFormat(format, anchor, -1);
3595 binary = (BOOL) (encoding != WWW_ENC_8BIT &&
3596 encoding != WWW_ENC_7BIT);
3597 if (!binary &&
3598 /*
3599 * Force binary if we're in source, download or dump mode and this is
3600 * not a VM/CMS server, so we don't get CRLF instead of LF (or CR) for
3601 * newlines in text files. Can't do this for VM/CMS or we'll get raw
3602 * EBCDIC. - FM
3603 */
3604 (format_out == WWW_SOURCE ||
3605 format_out == WWW_DOWNLOAD ||
3606 format_out == WWW_DUMP) &&
3607 (server_type != CMS_SERVER))
3608 binary = TRUE;
3609 if (!binary && type && *type == 'I') {
3610 /*
3611 * Force binary if we had ;type=I - FM
3612 */
3613 binary = TRUE;
3614 } else if (binary && type && *type == 'A') {
3615 /*
3616 * Force ASCII if we had ;type=A - FM
3617 */
3618 binary = FALSE;
3619 }
3620 if (binary != control->is_binary) {
3621 /*
3622 * Act on our setting if not already set. - FM
3623 */
3624 const char *mode = binary ? "I" : "A";
3625
3626 status = send_cmd_2("TYPE", mode);
3627 if (status != 2) {
3628 init_help_message_cache(); /* to free memory */
3629 return ((status < 0) ? status : -status);
3630 }
3631 control->is_binary = binary;
3632 }
3633 switch (server_type) {
3634 /*
3635 * Handle what for Lynx are special case servers, e.g., for which
3636 * we respect RFC 1738, or which have known conflicts in suffix
3637 * mappings. - FM
3638 */
3639 case VMS_SERVER:
3640 {
3641 char *cp1, *cp2;
3642 BOOL included_device = FALSE;
3643 BOOL found_tilde = FALSE;
3644
3645 /* Accept only Unix-style filename */
3646 if (StrChr(filename, ':') != NULL ||
3647 StrChr(filename, '[') != NULL) {
3648 FREE(fname);
3649 init_help_message_cache(); /* to free memory */
3650 NETCLOSE(control->socket);
3651 control->socket = -1;
3652 CTRACE((tfp,
3653 "HTFTP: Rejecting path due to non-Unix-style syntax.\n"));
3654 return -1;
3655 }
3656 /* Handle any unescaped "/%2F" path */
3657 if (!StrNCmp(filename, "//", 2)) {
3658 int i;
3659
3660 included_device = TRUE;
3661 for (i = 0; filename[(i + 1)]; i++)
3662 filename[i] = filename[(i + 1)];
3663 filename[i] = '\0';
3664 CTRACE((tfp, "HTFTP: Trimmed '%s'\n", filename));
3665 cp = HTVMS_name("", filename);
3666 CTRACE((tfp, "HTFTP: VMSized '%s'\n", cp));
3667 if ((cp1 = strrchr(cp, ']')) != NULL) {
3668 strcpy(filename, ++cp1);
3669 CTRACE((tfp, "HTFTP: Filename '%s'\n", filename));
3670 *cp1 = '\0';
3671 status = send_cwd(cp);
3672 if (status != 2) {
3673 char *dotslash = NULL;
3674
3675 if ((cp1 = StrChr(cp, '[')) != NULL) {
3676 *cp1++ = '\0';
3677 status = send_cwd(cp);
3678 if (status != 2) {
3679 FREE(fname);
3680 init_help_message_cache(); /* to free memory */
3681 NETCLOSE(control->socket);
3682 control->socket = -1;
3683 return ((status < 0) ? status : -status);
3684 }
3685 HTSprintf0(&dotslash, "[.%s", cp1);
3686 status = send_cwd(dotslash);
3687 FREE(dotslash);
3688 if (status != 2) {
3689 FREE(fname);
3690 init_help_message_cache(); /* to free memory */
3691 NETCLOSE(control->socket);
3692 control->socket = -1;
3693 return ((status < 0) ? status : -status);
3694 }
3695 } else {
3696 FREE(fname);
3697 init_help_message_cache(); /* to free memory */
3698 NETCLOSE(control->socket);
3699 control->socket = -1;
3700 return ((status < 0) ? status : -status);
3701 }
3702 }
3703 } else if ((cp1 = StrChr(cp, ':')) != NULL &&
3704 StrChr(cp, '[') == NULL &&
3705 StrChr(cp, ']') == NULL) {
3706 cp1++;
3707 if (*cp1 != '\0') {
3708 int cplen = (int) (cp1 - cp);
3709
3710 strcpy(filename, cp1);
3711 CTRACE((tfp, "HTFTP: Filename '%s'\n", filename));
3712 HTSprintf0(&vmsname, "%.*s[%s]", cplen, cp, filename);
3713 status = send_cwd(vmsname);
3714 if (status != 2) {
3715 HTSprintf(&vmsname, "%.*s[000000]", cplen, cp);
3716 status = send_cwd(vmsname);
3717 if (status != 2) {
3718 HTSprintf(&vmsname, "%.*s", cplen, cp);
3719 status = send_cwd(vmsname);
3720 if (status != 2) {
3721 FREE(fname);
3722 init_help_message_cache();
3723 NETCLOSE(control->socket);
3724 control->socket = -1;
3725 return ((status < 0) ? status : -status);
3726 }
3727 }
3728 } else {
3729 HTSprintf0(&vmsname, "000000");
3730 filename = vmsname;
3731 }
3732 }
3733 } else if (0 == strcmp(cp, (filename + 1))) {
3734 status = send_cwd(cp);
3735 if (status != 2) {
3736 HTSprintf0(&vmsname, "%s:", cp);
3737 status = send_cwd(vmsname);
3738 if (status != 2) {
3739 FREE(fname);
3740 init_help_message_cache(); /* to free memory */
3741 NETCLOSE(control->socket);
3742 control->socket = -1;
3743 return ((status < 0) ? status : -status);
3744 }
3745 }
3746 HTSprintf0(&vmsname, "000000");
3747 filename = vmsname;
3748 }
3749 }
3750 /* Trim trailing slash if filename is not the top directory */
3751 if (strlen(filename) > 1 && filename[strlen(filename) - 1] == '/')
3752 filename[strlen(filename) - 1] = '\0';
3753
3754 #ifdef MAINTAIN_CONNECTION /* Don't need this if always new connection - F.M. */
3755 if (!included_device) {
3756 /* Get the current default VMS device:[directory] */
3757 status = send_cmd_1("PWD");
3758 if (status != 2) {
3759 FREE(fname);
3760 init_help_message_cache(); /* to free memory */
3761 NETCLOSE(control->socket);
3762 control->socket = -1;
3763 return ((status < 0) ? status : -status);
3764 }
3765 /* Go to the VMS account's top directory */
3766 if ((cp = StrChr(response_text, '[')) != NULL &&
3767 (cp1 = strrchr(response_text, ']')) != NULL) {
3768 char *tmp = 0;
3769 unsigned len = 4;
3770
3771 StrAllocCopy(tmp, cp);
3772 if ((cp2 = StrChr(cp, '.')) != NULL && cp2 < cp1) {
3773 len += (cp2 - cp);
3774 } else {
3775 len += (cp1 - cp);
3776 }
3777 tmp[len] = 0;
3778 StrAllocCat(tmp, "]");
3779
3780 status = send_cwd(tmp);
3781 FREE(tmp);
3782
3783 if (status != 2) {
3784 FREE(fname);
3785 init_help_message_cache(); /* to free memory */
3786 NETCLOSE(control->socket);
3787 control->socket = -1;
3788 return ((status < 0) ? status : -status);
3789 }
3790 }
3791 }
3792 #endif /* MAINTAIN_CONNECTION */
3793
3794 /* If we want the VMS account's top directory, list it now */
3795 if (!(strcmp(filename, "/~")) ||
3796 (included_device && 0 == strcmp(filename, "000000")) ||
3797 (strlen(filename) == 1 && *filename == '/')) {
3798 isDirectory = YES;
3799 status = send_cmd_1("LIST");
3800 FREE(fname);
3801 if (status != 1) {
3802 /* Action not started */
3803 init_help_message_cache(); /* to free memory */
3804 NETCLOSE(control->socket);
3805 control->socket = -1;
3806 return ((status < 0) ? status : -status);
3807 }
3808 /* Big goto! */
3809 goto listen;
3810 }
3811 /* Otherwise, go to appropriate directory and doctor filename */
3812 if (!StrNCmp(filename, "/~", 2)) {
3813 filename += 2;
3814 found_tilde = TRUE;
3815 }
3816 CTRACE((tfp, "check '%s' to translate x/y/ to [.x.y]\n", filename));
3817 if (!included_device &&
3818 (cp = StrChr(filename, '/')) != NULL &&
3819 (cp1 = strrchr(cp, '/')) != NULL &&
3820 (cp1 - cp) > 1) {
3821 char *tmp = NULL;
3822
3823 HTSprintf0(&tmp, "[.%.*s]", (int) (cp1 - cp - 1), cp + 1);
3824
3825 CTRACE((tfp, "change path '%s'\n", tmp));
3826 while ((cp2 = strrchr(tmp, '/')) != NULL)
3827 *cp2 = '.';
3828 CTRACE((tfp, "...to path '%s'\n", tmp));
3829
3830 status = send_cwd(tmp);
3831 FREE(tmp);
3832
3833 if (status != 2) {
3834 FREE(fname);
3835 init_help_message_cache(); /* to free memory */
3836 NETCLOSE(control->socket);
3837 control->socket = -1;
3838 return ((status < 0) ? status : -status);
3839 }
3840 filename = cp1 + 1;
3841 } else {
3842 if (!included_device && !found_tilde) {
3843 filename += 1;
3844 }
3845 }
3846 break;
3847 }
3848 case CMS_SERVER:
3849 {
3850 /*
3851 * If we want the CMS account's top directory, or a base SFS or
3852 * anonymous directory path (i.e., without a slash), list it
3853 * now. FM
3854 */
3855 if ((strlen(filename) == 1 && *filename == '/') ||
3856 ((0 == strncasecomp((filename + 1), "vmsysu:", 7)) &&
3857 (cp = StrChr((filename + 1), '.')) != NULL &&
3858 StrChr(cp, '/') == NULL) ||
3859 (0 == strncasecomp(filename + 1, "anonymou.", 9) &&
3860 StrChr(filename + 1, '/') == NULL)) {
3861 if (filename[1] != '\0') {
3862 status = send_cwd(filename + 1);
3863 if (status != 2) {
3864 /* Action not started */
3865 init_help_message_cache(); /* to free memory */
3866 NETCLOSE(control->socket);
3867 control->socket = -1;
3868 return ((status < 0) ? status : -status);
3869 }
3870 }
3871 isDirectory = YES;
3872 if (use_list)
3873 status = send_cmd_1("LIST");
3874 else
3875 status = send_cmd_1("NLST");
3876 FREE(fname);
3877 if (status != 1) {
3878 /* Action not started */
3879 init_help_message_cache(); /* to free memory */
3880 NETCLOSE(control->socket);
3881 control->socket = -1;
3882 return ((status < 0) ? status : -status);
3883 }
3884 /* Big goto! */
3885 goto listen;
3886 }
3887 filename++;
3888
3889 /* Otherwise, go to appropriate directory and adjust filename */
3890 while ((cp = StrChr(filename, '/')) != NULL) {
3891 *cp++ = '\0';
3892 status = send_cwd(filename);
3893 if (status == 2) {
3894 if (*cp == '\0') {
3895 isDirectory = YES;
3896 if (use_list)
3897 status = send_cmd_1("LIST");
3898 else
3899 status = send_cmd_1("NLST");
3900 FREE(fname);
3901 if (status != 1) {
3902 /* Action not started */
3903 init_help_message_cache(); /* to free memory */
3904 NETCLOSE(control->socket);
3905 control->socket = -1;
3906 return ((status < 0) ? status : -status);
3907 }
3908 /* Clear any messages from the login directory */
3909 init_help_message_cache();
3910 /* Big goto! */
3911 goto listen;
3912 }
3913 filename = cp;
3914 }
3915 }
3916 break;
3917 }
3918 default:
3919 /* Shift for any unescaped "/%2F" path */
3920 if (!StrNCmp(filename, "//", 2))
3921 filename++;
3922 break;
3923 }
3924 /*
3925 * Act on a file or listing request, or try to figure out which we're
3926 * dealing with if we don't know yet. - FM
3927 */
3928 if (!(type) || (type && *type != 'D')) {
3929 /*
3930 * If we are retrieving a file we will (except for CMS) use
3931 * binary mode, which lets us use the size command supported by
3932 * ftp servers which implement RFC 3659. Knowing the size lets
3933 * us in turn display ETA in the progress message -TD
3934 */
3935 if (control->is_binary) {
3936 int code;
3937
3938 status = send_cmd_2("SIZE", filename);
3939 if (status == 2) {
3940 #if !defined(HAVE_LONG_LONG) && defined(GUESS_PRI_off_t)
3941 long size;
3942
3943 if (sscanf(response_text, "%d %ld", &code, &size) == 2) {
3944 anchor->content_length = (off_t) size;
3945 }
3946 #else
3947 off_t size;
3948 if (sscanf(response_text, "%d %" SCN_off_t, &code, &size)
3949 == 2) {
3950 anchor->content_length = size;
3951 }
3952 #endif
3953 }
3954 }
3955 status = send_cmd_2("RETR", filename);
3956 if (status >= 5) {
3957 int check;
3958
3959 if (Broken_RETR) {
3960 CTRACE((tfp, "{{reconnecting...\n"));
3961 close_connection(control);
3962 check = setup_connection(name, anchor);
3963 CTRACE((tfp, "...done }}reconnecting\n"));
3964 if (check < 0)
3965 return check;
3966 }
3967 }
3968 } else {
3969 status = 5; /* Failed status set as flag. - FM */
3970 }
3971 if (status != 1) { /* Failed : try to CWD to it */
3972 /* Clear any login messages if this isn't the login directory */
3973 if (strcmp(filename, "/"))
3974 init_help_message_cache();
3975
3976 status = send_cwd(filename);
3977 if (status == 2) { /* Succeeded : let's NAME LIST it */
3978 isDirectory = YES;
3979 if (use_list)
3980 status = send_cmd_1("LIST");
3981 else
3982 status = send_cmd_1("NLST");
3983 }
3984 }
3985 FREE(fname);
3986 FREE(vmsname);
3987 if (status != 1) {
3988 init_help_message_cache(); /* to free memory */
3989 NETCLOSE(control->socket);
3990 control->socket = -1;
3991 if (status < 0)
3992 return status;
3993 else
3994 return -status;
3995 }
3996 }
3997
3998 listen:
3999 if (!ftp_local_passive) {
4000 /* Wait for the connection */
4001 LY_SOCKADDR soc_A;
4002 LY_SOCKLEN soc_addrlen = (LY_SOCKLEN) sizeof(soc_A);
4003
4004 #ifdef SOCKS
4005 if (socks_flag)
4006 status = Raccept((int) master_socket,
4007 SOCKADDR_OF(soc_A),
4008 &soc_addrlen);
4009 else
4010 #endif /* SOCKS */
4011 status = accept((int) master_socket,
4012 SOCKADDR_OF(soc_A),
4013 &soc_addrlen);
4014 if (status < 0) {
4015 init_help_message_cache(); /* to free memory */
4016 return HTInetStatus("accept");
4017 }
4018 CTRACE((tfp, "TCP: Accepted new socket %d\n", status));
4019 data_soc = status;
4020 }
4021
4022 if (isDirectory) {
4023 if (server_type == UNIX_SERVER && !unsure_type &&
4024 !strcmp(response_text,
4025 "150 Opening ASCII mode data connection for /bin/dl.\n")) {
4026 CTRACE((tfp, "HTFTP: Treating as \"dls\" server.\n"));
4027 server_type = DLS_SERVER;
4028 }
4029 final_status = read_directory(anchor, name, format_out, sink);
4030 if (final_status > 0) {
4031 if (server_type != CMS_SERVER)
4032 if (outstanding-- > 0) {
4033 status = response(NULL);
4034 if (status < 0 ||
4035 (status == 2 && !StrNCmp(response_text, "221", 3)))
4036 outstanding = 0;
4037 }
4038 } else { /* HT_INTERRUPTED */
4039 /* User may have pressed 'z' to give up because no
4040 packets got through, so let's not make them wait
4041 any longer - kw */
4042 outstanding = 0;
4043 }
4044
4045 if (data_soc != -1) { /* normally done in read_directory */
4046 CTRACE((tfp, "HTFTP: Closing data socket %d\n", data_soc));
4047 status = NETCLOSE(data_soc);
4048 if (status == -1)
4049 HTInetStatus("close"); /* Comment only */
4050 }
4051 status = final_status;
4052 } else {
4053 int rv;
4054 char *FileName = HTParse(name, "", PARSE_PATH + PARSE_PUNCTUATION);
4055
4056 /* Clear any login messages */
4057 init_help_message_cache();
4058
4059 /* Fake a Content-Encoding for compressed files. - FM */
4060 HTUnEscape(FileName);
4061 if (!IsUnityEnc(encoding)) {
4062 /*
4063 * We already know from the call to HTFileFormat above that this is
4064 * a compressed file, no need to look at the filename again. - kw
4065 */
4066 StrAllocCopy(anchor->content_type, format->name);
4067 StrAllocCopy(anchor->content_encoding, HTAtom_name(encoding));
4068 format = HTAtom_for("www/compressed");
4069
4070 } else {
4071 int rootlen;
4072 CompressFileType cft = HTCompressFileType(FileName, "._-", &rootlen);
4073
4074 if (cft != cftNone) {
4075 FileName[rootlen] = '\0';
4076 format = HTFileFormat(FileName, &encoding, NULL);
4077 format = HTCharsetFormat(format, anchor, -1);
4078 StrAllocCopy(anchor->content_type, format->name);
4079 format = HTAtom_for("www/compressed");
4080 }
4081
4082 switch (cft) {
4083 case cftCompress:
4084 StrAllocCopy(anchor->content_encoding, "x-compress");
4085 break;
4086 case cftGzip:
4087 StrAllocCopy(anchor->content_encoding, "x-gzip");
4088 break;
4089 case cftDeflate:
4090 StrAllocCopy(anchor->content_encoding, "x-deflate");
4091 break;
4092 case cftBzip2:
4093 StrAllocCopy(anchor->content_encoding, "x-bzip2");
4094 break;
4095 case cftBrotli:
4096 StrAllocCopy(anchor->content_encoding, "x-brotli");
4097 break;
4098 case cftZstd:
4099 StrAllocCopy(anchor->content_encoding, "zstd");
4100 break;
4101 case cftNone:
4102 break;
4103 }
4104 }
4105 FREE(FileName);
4106
4107 _HTProgress(gettext("Receiving FTP file."));
4108 rv = HTParseSocket(format, format_out, anchor, data_soc, sink);
4109
4110 HTInitInput(control->socket);
4111 /* Reset buffering to control connection DD 921208 */
4112
4113 if (rv < 0) {
4114 if (rv == -2) /* weird error, don't expect much response */
4115 outstanding--;
4116 else if (rv == HT_INTERRUPTED || rv == -1)
4117 /* User may have pressed 'z' to give up because no
4118 packets got through, so let's not make them wait
4119 longer - kw */
4120 outstanding = 0;
4121 CTRACE((tfp, "HTFTP: Closing data socket %d\n", data_soc));
4122 status = NETCLOSE(data_soc);
4123 } else {
4124 status = 2; /* data_soc already closed in HTCopy - kw */
4125 }
4126
4127 if (status < 0 && rv != HT_INTERRUPTED && rv != -1) {
4128 (void) HTInetStatus("close"); /* Comment only */
4129 } else {
4130 if (rv != HT_LOADED && outstanding--) {
4131 status = response(NULL); /* Pick up final reply */
4132 if (status != 2 && rv != HT_INTERRUPTED && rv != -1) {
4133 data_soc = -1; /* invalidate it */
4134 init_help_message_cache(); /* to free memory */
4135 return HTLoadError(sink, 500, response_text);
4136 } else if (status == 2 && !StrNCmp(response_text, "221", 3)) {
4137 outstanding = 0;
4138 }
4139 }
4140 }
4141 final_status = HT_LOADED;
4142 }
4143 while (outstanding-- > 0 &&
4144 (status > 0)) {
4145 status = response(NULL);
4146 if (status == 2 && !StrNCmp(response_text, "221", 3))
4147 break;
4148 }
4149 data_soc = -1; /* invalidate it */
4150 CTRACE((tfp, "HTFTPLoad: normal end; "));
4151 if (control->socket < 0) {
4152 CTRACE((tfp, "control socket is %d\n", control->socket));
4153 } else {
4154 CTRACE((tfp, "closing control socket %d\n", control->socket));
4155 status = NETCLOSE(control->socket);
4156 if (status == -1)
4157 HTInetStatus("control connection close"); /* Comment only */
4158 }
4159 control->socket = -1;
4160 init_help_message_cache(); /* to free memory */
4161 /* returns HT_LOADED (always for file if we get here) or error */
4162 return final_status;
4163 } /* open_file_read */
4164
4165 /*
4166 * This function frees any user entered password, so that
4167 * it must be entered again for a future request. - FM
4168 */
4169 void HTClearFTPPassword(void)
4170 {
4171 /*
4172 * Need code to check cached documents from non-anonymous ftp accounts and
4173 * do something to ensure that they no longer can be accessed without a new
4174 * retrieval. - FM
4175 */
4176
4177 /*
4178 * Now free the current user entered password, if any. - FM
4179 */
4180 FREE(user_entered_password);
4181 }
4182
4183 #endif /* ifndef DISABLE_FTP */