Annotation of libwww/Library/src/HTFTP.c, revision 1.103
1.46 frystyk 1: /* HTFTP.c
2: ** FILE TRANSFER PROTOCOL (FTP) CLIENT
3: **
1.52 frystyk 4: ** (c) COPYRIGHT MIT 1995.
1.46 frystyk 5: ** Please first read the full copyright statement in the file COPYRIGH.
1.103 ! frystyk 6: ** @(#) $Id: HTFTP.c,v 1.102 1999/02/22 22:10:11 frystyk Exp $
1.1 timbl 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
1.79 frystyk 13: ** than chosen in rotation by us (FTP_POLL_PORTS), or the following
1.1 timbl 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
1.54 frystyk 24: ** TBL Tim Berners-lee <timbl@w3.org>
1.1 timbl 25: ** DD Denis DeLaRoca 310 825-4580 <CSP1DWD@mvs.oac.ucla.edu>
1.22 frystyk 26: ** LM Lou Montulli <montulli@ukanaix.cc.ukans.edu>
27: ** FM Foteos Macrides <macrides@sci.wfeb.edu>
1.54 frystyk 28: ** HF Henrik Frystyk <frystyk@w3.org>
1.30 luotonen 29: ** AL Ari Luotonen <luotonen@www.cern.ch>
1.23 frystyk 30: **
1.1 timbl 31: ** History:
32: ** 2 May 91 Written TBL, as a part of the WorldWideWeb project.
33: ** 15 Jan 92 Bug fix: close() was used for NETCLOSE for control soc
34: ** 10 Feb 92 Retry if cached connection times out or breaks
35: ** 8 Dec 92 Bug fix 921208 TBL after DD
36: ** 17 Dec 92 Anon FTP password now just WWWuser@ suggested by DD
1.2 timbl 37: ** fails on princeton.edu!
1.22 frystyk 38: ** 27 Dec 93 (FM) Fixed up so FTP now works with VMS hosts. Path
39: ** must be Unix-style and cannot include the device
40: ** or top directory.
41: ** ?? ??? ?? (LM) Added code to prompt and send passwords for non
42: ** anonymous FTP
43: ** 25 Mar 94 (LM) Added code to recognize different ftp server types
44: ** and code to parse dates and sizes on most hosts.
45: ** 27 Mar 93 (FM) Added code for getting dates and sizes on VMS hosts.
1.23 frystyk 46: ** 27 Apr 94 (HF) The module is basically rewritten to conform with
47: ** rfc 959, 1123 and 1579 and turned into a state
48: ** machine. New semantics of ftp URLs are supported.
1.30 luotonen 49: ** 2 May 94 (AL) Fixed possible security hole when the URL contains
50: ** a newline, that could cause multiple commands to be
51: ** sent to an FTP server.
1.60 frystyk 52: ** Sep 95 HFN Rewritten to support streams and persistent conenctions
53: ** and multiplexed IO
1.95 frystyk 54: ** Mar 1998 NG Neil Griffin - Bug fixes and additions for FTP support on NT.
1.22 frystyk 55: ** Notes:
56: ** Portions Copyright 1994 Trustees of Dartmouth College
57: ** Code for recognizing different FTP servers and
58: ** parsing "ls -l" output taken from Macintosh Fetch
59: ** program with permission from Jim Matthews,
60: ** Dartmouth Software Development Team.
1.23 frystyk 61: */
1.1 timbl 62:
1.22 frystyk 63: /* Library include files */
1.96 frystyk 64: #include "wwwsys.h"
1.81 frystyk 65: #include "WWWUtil.h"
66: #include "WWWCore.h"
1.93 frystyk 67: #include "WWWStream.h"
68: #include "WWWTrans.h"
1.101 frystyk 69: #include "WWWFile.h"
1.58 frystyk 70: #include "HTReqMan.h"
1.98 frystyk 71: #include "HTNetMan.h"
1.63 frystyk 72: #include "HTFTPDir.h"
1.22 frystyk 73: #include "HTFTP.h" /* Implemented here */
74:
75: /* Macros and other defines */
1.60 frystyk 76: #if 0
77: /* Only use this if ABSOLUTELY necessary! */
1.79 frystyk 78: #define FTP_POLL_PORTS /* If allocation does not work, poll ourselves.*/
1.1 timbl 79: #endif
80:
1.79 frystyk 81: #ifdef FTP_POLL_PORTS
1.23 frystyk 82: #define FIRST_TCP_PORT 1024 /* Region to try for a listening port */
83: #define LAST_TCP_PORT 5999
1.60 frystyk 84: PRIVATE int DataPort = FIRST_TCP_PORT;
1.1 timbl 85: #endif
86:
1.60 frystyk 87: #ifndef FTP_PORT
88: #define FTP_PORT 21
89: #define FTP_DATA 20
1.1 timbl 90: #endif
1.60 frystyk 91:
92: #define WWW_FTP_CLIENT "libwww@" /* If can't get user-info, use this */
93: #define FTP_DIR(me) ((me)->type=='L' || (me)->type=='N')
1.1 timbl 94:
1.58 frystyk 95: /*
96: ** Local context structure used in the HTNet object.
97: */
1.45 frystyk 98: typedef enum _HTFTPState {
1.60 frystyk 99: FTP_SUCCESS = -2,
100: FTP_ERROR = -1,
1.58 frystyk 101: FTP_BEGIN = 0,
102: FTP_NEED_CCON, /* Control connection */
1.60 frystyk 103: FTP_NEED_LOGIN,
1.58 frystyk 104: FTP_NEED_DCON, /* Data connection */
1.60 frystyk 105: FTP_NEED_DATA,
106: FTP_NEED_SERVER /* For directory listings */
1.39 frystyk 107: } HTFTPState;
108:
1.58 frystyk 109: typedef struct _ftp_ctrl {
1.60 frystyk 110: HTChunk * cmd;
111: int repcode;
112: char * reply;
113: char * uid;
1.58 frystyk 114: char * passwd;
1.60 frystyk 115: char * account;
1.58 frystyk 116: HTFTPState state; /* State of the connection */
1.60 frystyk 117: int substate; /* For hierarchical states */
118: BOOL sent; /* Read or write command */
119: BOOL cwd; /* Done cwd */
120: BOOL reset; /* Expect greeting */
1.63 frystyk 121: FTPServerType server; /* Type of server */
1.93 frystyk 122: HTNet * cnet; /* Control connection */
1.60 frystyk 123: HTNet * dnet; /* Data connection */
1.95 frystyk 124: /* begin _GM_ */
125: /* Note: libwww bug ID: GM3 */
126: BOOL alreadyLoggedIn;
127: /* end _GM_ */
1.58 frystyk 128: } ftp_ctrl;
1.39 frystyk 129:
1.58 frystyk 130: typedef struct _ftp_data {
1.60 frystyk 131: char host[30]; /* Host to contact for data */
132: char * file; /* File or dir name */
133: char * offset; /* offset into file */
134: BOOL pasv; /* Active or passive */
135: char type; /* 'A', 'I', 'L'(IST), 'N'(LST) */
1.93 frystyk 136: int complete; /* Check if both ctrl and data is loaded */
1.76 frystyk 137: BOOL stream_error;
1.58 frystyk 138: } ftp_data;
1.39 frystyk 139:
1.60 frystyk 140: struct _HTStream {
1.79 frystyk 141: const HTStreamClass * isa;
1.60 frystyk 142: HTStream * target;
143: HTRequest * request;
144: ftp_ctrl * ctrl;
1.80 frystyk 145: HTEOLState state;
1.60 frystyk 146: HTChunk * welcome;
1.62 frystyk 147: BOOL junk; /* For too long lines */
1.60 frystyk 148: BOOL first_line;
1.63 frystyk 149: char buffer[MAX_FTP_LINE+1];
1.60 frystyk 150: int buflen;
1.92 eric 151: HTHost * host;
1.60 frystyk 152: };
153:
1.81 frystyk 154: struct _HTInputStream {
155: const HTInputStreamClass * isa;
156: };
157:
1.60 frystyk 158: typedef enum _FTPDataCon {
159: FTP_DATA_PASV = 0x1,
160: FTP_DATA_PORT = 0x2
161: } FTPDataCon;
162:
163: PRIVATE FTPDataCon FTPMode = FTP_DATA_PASV;
1.39 frystyk 164:
1.95 frystyk 165: /* Added by Neil Griffin */
166: PRIVATE FTPTransferMode g_FTPTransferMode = FTP_DEFAULT_TRANSFER_MODE;
1.97 frystyk 167: PRIVATE FTPControlMode g_FTPControlMode = FTP_DEFAULT_CONTROL_MODE;
1.95 frystyk 168:
1.23 frystyk 169: /* ------------------------------------------------------------------------- */
1.60 frystyk 170: /* FTP Status Line Stream */
1.22 frystyk 171: /* ------------------------------------------------------------------------- */
1.60 frystyk 172:
173: /* FTPCleanup
174: ** ----------
175: ** This function closes the connection and frees memory.
176: ** Returns YES on OK, else NO
177: */
1.65 frystyk 178: PRIVATE int FTPCleanup (HTRequest * request, int status)
1.60 frystyk 179: {
1.75 frystyk 180: if (request) {
1.84 frystyk 181: HTNet * cnet = HTRequest_net(request);
182: ftp_ctrl * ctrl = (ftp_ctrl *) HTNet_context(cnet);
183: HTStream * input = HTRequest_inputStream(request);
1.98 frystyk 184:
185: if (status == HT_INTERRUPTED) {
186: HTAlertCallback * cbf = HTAlert_find(HT_PROG_INTERRUPT);
187: if (cbf) (*cbf)(request, HT_PROG_INTERRUPT,
188: HT_MSG_NULL, NULL, NULL, NULL);
189: } else if (status == HT_TIMEOUT) {
190: HTAlertCallback * cbf = HTAlert_find(HT_PROG_TIMEOUT);
191: if (cbf) (*cbf)(request, HT_PROG_TIMEOUT,
192: HT_MSG_NULL, NULL, NULL, NULL);
193: } else if (status == HT_LOADED) {
194: HTAlertCallback * cbf = HTAlert_find(HT_PROG_DONE);
195: if (cbf) (*cbf)(request, HT_PROG_DONE,
196: HT_MSG_NULL, NULL, NULL, NULL);
197: }
198:
1.93 frystyk 199: /* Free control stream with data TO network */
1.84 frystyk 200: if (!HTRequest_isDestination(request) && input) {
1.75 frystyk 201: if (status == HT_INTERRUPTED)
1.84 frystyk 202: (*input->isa->abort)(input, NULL);
1.75 frystyk 203: else
1.84 frystyk 204: (*input->isa->_free)(input);
1.75 frystyk 205: }
206:
207: /* Remove the request object and our own context structure for http */
1.84 frystyk 208: if (cnet && ctrl) {
1.75 frystyk 209: HTNet * dnet = ctrl->dnet;
1.84 frystyk 210: ftp_data * data = (ftp_data *) HTNet_context(dnet);
1.75 frystyk 211: HTChunk_delete(ctrl->cmd);
1.77 frystyk 212: HT_FREE(ctrl->reply);
213: HT_FREE(ctrl->uid);
214: HT_FREE(ctrl->passwd);
215: HT_FREE(ctrl->account);
216: HT_FREE(ctrl);
1.84 frystyk 217: if (dnet && data) {
1.77 frystyk 218: HT_FREE(data->file);
219: HT_FREE(data);
1.75 frystyk 220: }
1.93 frystyk 221: #if 0
1.88 frystyk 222: HTNet_setPersistent(dnet, NO, HT_TP_SINGLE);
1.93 frystyk 223: #endif
1.98 frystyk 224: /* See if we got a content length */
225: if (status == HT_LOADED)
226: HTAnchor_setLength(HTRequest_anchor(request), HTNet_bytesRead(dnet));
227:
228: HTNet_delete(dnet, HT_IGNORE);
1.75 frystyk 229: }
230: HTNet_delete(cnet, status);
231: return YES;
1.60 frystyk 232: }
1.75 frystyk 233: return NO;
1.60 frystyk 234: }
235:
236: /* ScanResponse
237: ** ------------
238: ** Analyzes the response from the FTP server.
239: ** Returns HT_LOADED if OK, HT_OK if more, HT_ERROR if error
240: ** the control connection.
1.33 frystyk 241: */
1.65 frystyk 242: PRIVATE int ScanResponse (HTStream * me)
1.33 frystyk 243: {
1.60 frystyk 244: int reply = 0;
245: char cont = '\0';
246: char *ptr = me->buffer+4;
247: *(me->buffer+me->buflen) = '\0';
1.95 frystyk 248: /* begin _GM_ */
249: /* Note: libwww bug ID: GM3 */
250: me->ctrl->alreadyLoggedIn = NO;
251: /* end _GM_ */
1.94 frystyk 252: if (isdigit((int) *(me->buffer))) sscanf(me->buffer, "%d%c", &reply, &cont);
1.60 frystyk 253: if (me->first_line) {
1.102 frystyk 254: HTTRACE(PROT_TRACE, "FTP Rx...... `%s\'\n" _ me->buffer);
1.60 frystyk 255: if (!reply) return HT_ERROR;
256: me->first_line = NO;
257: me->ctrl->repcode = reply;
258: StrAllocCopy(me->ctrl->reply, ptr);
1.95 frystyk 259: /* begin _GM_ */
260: /* Note: libwww bug ID: GM3 */
1.103 ! frystyk 261: if ( (reply == 530) && (HTStrCaseStr(me->buffer, "already") != NULL) ) {
1.95 frystyk 262: me->ctrl->alreadyLoggedIn = YES;
263: } else {
264: me->ctrl->alreadyLoggedIn = NO;
265: }
266: /* end _GM_ */
1.60 frystyk 267: } else {
1.73 frystyk 268: HTChunk_puts(me->welcome, ptr);
269: HTChunk_putc(me->welcome, '\n');
1.33 frystyk 270: }
1.60 frystyk 271: me->buflen = 0;
1.68 frystyk 272: me->state = EOL_BEGIN;
1.60 frystyk 273: if (cont != '-') {
274: me->first_line = YES;
275: return HT_LOADED;
1.33 frystyk 276: }
1.60 frystyk 277: return HT_OK;
1.33 frystyk 278: }
279:
1.60 frystyk 280: /*
281: ** Searches for FTP header line until buffer fills up or a CRLF or LF
282: ** is found
1.23 frystyk 283: */
1.79 frystyk 284: PRIVATE int FTPStatus_put_block (HTStream * me, const char * b, int l)
1.92 eric 285: {
1.60 frystyk 286: int status;
1.93 frystyk 287: HTHost_setConsumed(me->host, l);
1.60 frystyk 288: while (l-- > 0) {
289: if (me->state == EOL_FCR) {
290: if (*b == LF) {
1.62 frystyk 291: if (!me->junk) {
1.93 frystyk 292: if ((status = ScanResponse(me)) != HT_OK) return status;
1.62 frystyk 293: } else {
294: me->buflen = 0;
295: me->junk = NO;
296: }
1.60 frystyk 297: }
298: } else if (*b == CR) {
299: me->state = EOL_FCR;
300: } else if (*b == LF) {
1.62 frystyk 301: if (!me->junk) {
1.93 frystyk 302: if ((status = ScanResponse(me)) != HT_OK) return status;
1.62 frystyk 303: } else {
304: me->buflen = 0;
305: me->junk = NO;
306: }
1.60 frystyk 307: } else {
308: *(me->buffer+me->buflen++) = *b;
1.63 frystyk 309: if (me->buflen >= MAX_FTP_LINE) {
1.102 frystyk 310: HTTRACE(PROT_TRACE, "FTP Status.. Line too long - chopped\n");
1.62 frystyk 311: me->junk = YES;
312: if ((status = ScanResponse(me)) != HT_OK) {
313: me->junk = NO;
314: return status;
315: }
1.23 frystyk 316: }
317: }
1.60 frystyk 318: b++;
1.25 frystyk 319: }
1.60 frystyk 320: return HT_OK;
1.25 frystyk 321: }
322:
1.79 frystyk 323: PRIVATE int FTPStatus_put_string (HTStream * me, const char * s)
1.60 frystyk 324: {
325: return FTPStatus_put_block(me, s, (int) strlen(s));
326: }
1.25 frystyk 327:
1.65 frystyk 328: PRIVATE int FTPStatus_put_character (HTStream * me, char c)
1.25 frystyk 329: {
1.60 frystyk 330: return FTPStatus_put_block(me, &c, 1);
331: }
1.25 frystyk 332:
1.65 frystyk 333: PRIVATE int FTPStatus_flush (HTStream * me)
1.60 frystyk 334: {
335: return (*me->target->isa->flush)(me->target);
1.23 frystyk 336: }
1.22 frystyk 337:
1.65 frystyk 338: PRIVATE int FTPStatus_free (HTStream * me)
1.22 frystyk 339: {
1.60 frystyk 340: int status = HT_OK;
341: if (me->target) {
342: if ((status = (*me->target->isa->_free)(me->target)) == HT_WOULD_BLOCK)
343: return HT_WOULD_BLOCK;
1.22 frystyk 344: }
1.73 frystyk 345: HTChunk_delete(me->welcome);
1.77 frystyk 346: HT_FREE(me);
1.60 frystyk 347: return HT_OK;
1.22 frystyk 348: }
349:
1.68 frystyk 350: PRIVATE int FTPStatus_abort (HTStream * me, HTList * e)
1.22 frystyk 351: {
1.60 frystyk 352: if (me->target)
353: (*me->target->isa->abort)(me->target, e);
1.73 frystyk 354: HTChunk_delete(me->welcome);
1.77 frystyk 355: HT_FREE(me);
1.102 frystyk 356: HTTRACE(PROT_TRACE, "FTPStatus... ABORTING...\n");
1.60 frystyk 357: return HT_ERROR;
1.22 frystyk 358: }
359:
1.60 frystyk 360: /* FTPStatus Stream
361: ** -----------------
1.22 frystyk 362: */
1.79 frystyk 363: PRIVATE const HTStreamClass FTPStatusClass =
1.60 frystyk 364: {
365: "FTPStatus",
366: FTPStatus_flush,
367: FTPStatus_free,
368: FTPStatus_abort,
369: FTPStatus_put_character,
370: FTPStatus_put_string,
371: FTPStatus_put_block
372: };
373:
1.92 eric 374: PRIVATE HTStream * FTPStatus_new (HTRequest * request, ftp_ctrl * ctrl, HTHost * host)
1.60 frystyk 375: {
1.77 frystyk 376: HTStream * me;
377: if ((me = (HTStream *) HT_CALLOC(1, sizeof(HTStream))) == NULL)
378: HT_OUTOFMEM("FTPStatus_new");
1.60 frystyk 379: me->isa = &FTPStatusClass;
380: me->request = request;
381: me->first_line = YES;
1.73 frystyk 382: me->welcome = HTChunk_new(256);
1.60 frystyk 383: me->ctrl = ctrl;
384: me->state = EOL_BEGIN;
1.92 eric 385: me->host = host;
1.60 frystyk 386: return me;
1.22 frystyk 387: }
388:
1.60 frystyk 389: /* ------------------------------------------------------------------------- */
390: /* FTP Client Functions for managing control and data connections */
391: /* ------------------------------------------------------------------------- */
1.22 frystyk 392:
1.60 frystyk 393: PRIVATE int SendCommand (HTRequest *request, ftp_ctrl *ctrl,
394: char *token, char *pars)
395: {
396: int len = strlen(token) + (pars ? strlen(pars)+1:0) + 2;
1.84 frystyk 397: HTStream * input = HTRequest_inputStream(request);
1.73 frystyk 398: HTChunk_clear(ctrl->cmd);
399: HTChunk_ensure(ctrl->cmd, len);
1.60 frystyk 400: if (pars && *pars)
1.73 frystyk 401: sprintf(HTChunk_data(ctrl->cmd), "%s %s%c%c", token, pars, CR, LF);
1.60 frystyk 402: else
1.73 frystyk 403: sprintf(HTChunk_data(ctrl->cmd), "%s%c%c", token, CR, LF);
1.102 frystyk 404: HTTRACE(PROT_TRACE, "FTP Tx...... %s" _ HTChunk_data(ctrl->cmd));
1.84 frystyk 405: return (*input->isa->put_block)(input, HTChunk_data(ctrl->cmd), len);
1.60 frystyk 406: }
407:
408: /* HTFTPParseURL
409: ** -------------
410: ** Scan URL for uid and passwd, and any data type indication. The
411: ** expected format is [user[:password]@]host[:port].
412: ** If no values are found then use defaults.
413: ** Returns YES if OK, else NO
1.22 frystyk 414: */
1.83 frystyk 415: PRIVATE BOOL HTFTPParseURL (HTRequest * request,
416: char *url, ftp_ctrl *ctrl, ftp_data *data)
1.22 frystyk 417: {
1.60 frystyk 418: char *login = HTParse(url, "", PARSE_HOST);
419: char *path = HTParse(url, "", PARSE_PATH+PARSE_PUNCTUATION);
420: char *ptr = strchr(login, '@');
421: if (ptr) { /* Uid and/or passwd specified */
422: char *passwd;
423: *ptr = '\0';
424: if ((passwd = strchr(login, ':'))) { /* Passwd specified */
425: *passwd++ = '\0';
426: HTUnEscape(passwd);
427: StrAllocCopy(ctrl->passwd, passwd);
1.22 frystyk 428: }
1.60 frystyk 429: HTUnEscape(login);
430: StrAllocCopy(ctrl->uid, login);
1.97 frystyk 431: } else if (g_FTPControlMode & FTP_ALWAYS_ASK_UID_PW) {
432: /* Added by Marek Nagy. */
433: ctrl->uid=NULL;
434: ctrl->passwd=NULL;
435: /* End of adding. */
436: } else { /* Use anonymous */
1.83 frystyk 437: HTUserProfile * up = HTRequest_userProfile(request);
1.97 frystyk 438: const char * mailaddress = HTUserProfile_email(up);
1.60 frystyk 439: StrAllocCopy(ctrl->uid, "anonymous");
440: if (mailaddress)
441: StrAllocCopy(ctrl->passwd, mailaddress);
442: else
443: StrAllocCopy(ctrl->passwd, WWW_FTP_CLIENT);
1.22 frystyk 444: }
1.95 frystyk 445: /* begin _GM_ */
446: /* Note: libwww bug ID: GM6 */
447: {
1.97 frystyk 448: /* char tempParams[512];
1.95 frystyk 449: sprintf(tempParams, "Username='%s', Password='%s'", ctrl->uid, ctrl->passwd);
450: HTRequest_addError(request, ERR_INFO, NO, HTERR_OK,
451: tempParams, strlen(tempParams), "HTFTPParseURL");
1.97 frystyk 452: */ }
1.95 frystyk 453: /* end _GM_ */
1.102 frystyk 454: HTTRACE(PROT_TRACE, "FTPParse.... uid `%s\' pw `%s\'\n" _
455: ctrl->uid ? ctrl->uid : "<null>" _
1.66 frystyk 456: ctrl->passwd ? ctrl->passwd : "<null>");
1.60 frystyk 457:
458: ptr = strchr(path, ';');
459: if (ptr) {
460: *ptr = '\0';
461: if (strncasecomp(ptr, ";type=", 6)) /* Look for type */
462: data->type = TOUPPER(*(ptr+6));
463: else if (*(ptr-1) == '/')
464: data->type = 'N';
465: } else if (*(path+strlen(path)-1) == '/') {
466: *(path+strlen(path)-1) = '\0';
467: data->type = 'N';
468: }
1.102 frystyk 469: HTTRACE(PROT_TRACE, "FTPParse.... Datatype %c\n" _ data->type ? data->type : '?');
1.60 frystyk 470: StrAllocCopy(data->file, path);
471: data->offset = data->file;
1.77 frystyk 472: HT_FREE(login);
473: HT_FREE(path);
1.60 frystyk 474: return YES;
1.22 frystyk 475: }
476:
1.60 frystyk 477: /* Use LIST or NLST
478: ** ----------------
479: ** This function sets the type field for what type of list we can use
480: ** Returns YES if OK, else NO
481: */
1.63 frystyk 482: PRIVATE BOOL FTPListType (ftp_data * data, FTPServerType type)
1.60 frystyk 483: {
484: if (!data) return NO;
485: switch (type) {
486: case FTP_GENERIC: data->type='N'; break;
487: case FTP_MACHTEN: data->type='L'; break;
488: case FTP_UNIX: data->type='L'; break;
489: case FTP_VMS: data->type='L'; break;
490: case FTP_CMS: data->type='N'; break;
491: case FTP_DCTS: data->type='N'; break;
492: case FTP_TCPC: data->type='N'; break;
493: case FTP_PETER_LEWIS: data->type='L'; break;
494: case FTP_NCSA: data->type='N'; break;
495: case FTP_WINNT: data->type='L'; break;
496: default: data->type='N'; break;
497: }
498: return YES;
499: }
500:
501: /* Open a Data socket for listening on
502: ** -----------------------------------
503: ** Set up a port to listen for data
504: ** Returns YES if OK, else NO
1.22 frystyk 505: */
1.60 frystyk 506: PRIVATE BOOL ListenSocket (HTNet *cnet, HTNet *dnet, ftp_data *data)
1.22 frystyk 507: {
1.79 frystyk 508: #ifdef FTP_POLL_PORTS
1.60 frystyk 509: unsigned short old_DataPort = DataPort;
510: for (DataPort=old_DataPort+1;; DataPort++) {
511: if (DataPort > LAST_TCP_PORT)
512: DataPort = FIRST_TCP_PORT;
513: if (DataPort == old_DataPort) {
1.102 frystyk 514: HTTRACE(PROT_TRACE, "FTP......... No data port found\n");
1.60 frystyk 515: return NO;
516: }
1.71 frystyk 517: if (HTDoListen(dnet, DataPort, 1) == HT_OK)
1.60 frystyk 518: break;
1.85 frystyk 519: #if 0
1.91 frystyk 520: if (HTNet_socket(dnet) != INVSOC) {
521: NETCLOSE(HTNet_socket(dnet));
522: HTNet_socket(dnet) = INVSOC;
1.60 frystyk 523: }
1.85 frystyk 524: #else
525: HTDoClose(dnet);
526: #endif
1.22 frystyk 527: }
1.60 frystyk 528: #else
1.91 frystyk 529: if (HTDoListen(dnet, 0, HTNet_socket(cnet), 1) != HT_OK) return NO;
1.79 frystyk 530: #endif /* FTP_POLL_PORTS */
1.22 frystyk 531:
1.60 frystyk 532: /* Now we must find out who we are to tell the other guy */
533: {
534: SockA local_addr;
535: int addr_size = sizeof(local_addr);
536: memset((void *) &local_addr, '\0', sizeof(local_addr));
1.91 frystyk 537: if (getsockname(HTNet_socket(dnet), (struct sockaddr *) &local_addr,
1.60 frystyk 538: &addr_size) < 0) {
1.93 frystyk 539: HTRequest_addSystemError(HTNet_request(dnet), ERR_FATAL, socerrno,
1.68 frystyk 540: NO, "getsockname");
1.60 frystyk 541: return NO;
1.22 frystyk 542: }
1.102 frystyk 543: HTTRACE(PROT_TRACE, "FTP......... This host is `%s\'\n" _
1.60 frystyk 544: HTInetString(&local_addr));
545: {
546: u_long addr = local_addr.sin_addr.s_addr;
547: u_short port = local_addr.sin_port;
548: sprintf(data->host, "%d,%d,%d,%d,%d,%d",
549: (int)*((unsigned char *)(&addr)+0),
550: (int)*((unsigned char *)(&addr)+1),
551: (int)*((unsigned char *)(&addr)+2),
552: (int)*((unsigned char *)(&addr)+3),
553: (int)*((unsigned char *)(&port)+0),
554: (int)*((unsigned char *)(&port)+1));
1.22 frystyk 555: }
556: }
1.60 frystyk 557: return YES;
1.22 frystyk 558: }
559:
1.60 frystyk 560: /* HTFTPLogin
561: ** -----------
562: ** This function makes a login to a ftp-server. It takes the user name
563: ** and passwd specified in ctrl->user and if that fails or an additional
564: ** account is needed, the user is prompted.
565: ** Returns HT_OK, HT_ERROR, or HT_WOULD_BLOCK
1.22 frystyk 566: */
1.60 frystyk 567: PRIVATE int HTFTPLogin (HTRequest *request, HTNet *cnet, ftp_ctrl *ctrl)
1.22 frystyk 568: {
1.60 frystyk 569: int status;
570: typedef enum _state {
571: SUB_ERROR = -2,
572: SUB_SUCCESS = -1,
573: NEED_SELECT = 0,
574: NEED_GREETING,
575: NEED_REIN,
576: NEED_UID,
577: NEED_PASSWD,
578: NEED_ACCOUNT,
579: PROMPT_USER
580: } state;
581:
582: /* Jump into a second level state machine */
583: while (1) {
584: switch ((state) ctrl->substate) {
1.99 frystyk 585: case NEED_SELECT:
586: {
587: HTAlertCallback * cbf = HTAlert_find(HT_PROG_LOGIN);
588: if (cbf) (*cbf)(request, HT_PROG_LOGIN, HT_MSG_NULL, NULL, NULL, NULL);
1.102 frystyk 589: HTTRACE(PROT_TRACE, "FTP Login.. now in state NEED_SELECT\n");
1.99 frystyk 590: ctrl->substate = ctrl->reset ? NEED_REIN : NEED_GREETING;
591: }
1.60 frystyk 592: break;
593:
594: case NEED_GREETING:
1.102 frystyk 595: HTTRACE(PROT_TRACE, "FTP Login.. now in state NEED_GREETING\n");
1.93 frystyk 596: status = HTHost_read(HTNet_host(cnet), cnet);
1.60 frystyk 597: if (status == HT_WOULD_BLOCK)
598: return HT_WOULD_BLOCK;
599: else if (status == HT_LOADED) {
1.66 frystyk 600: if (ctrl->repcode/100 == 2) {
601: ctrl->substate = (ctrl->uid && *ctrl->uid) ?
602: NEED_UID : PROMPT_USER;
1.95 frystyk 603: } else {
1.60 frystyk 604: ctrl->substate = SUB_ERROR;
1.95 frystyk 605: }
606: } else {
1.60 frystyk 607: ctrl->substate = SUB_ERROR;
1.95 frystyk 608: }
1.60 frystyk 609: break;
610:
611: case NEED_REIN:
1.102 frystyk 612: HTTRACE(PROT_TRACE, "FTP Login.. now in state NEED_REIN\n");
1.60 frystyk 613: if (!ctrl->sent) {
614: status = SendCommand(request, ctrl, "REIN", NULL);
615: if (status == HT_WOULD_BLOCK)
616: return HT_WOULD_BLOCK;
617: else if (status == HT_ERROR)
618: ctrl->substate = SUB_ERROR;
619: ctrl->sent = YES;
620: } else {
1.93 frystyk 621: status = HTHost_read(HTNet_host(cnet), cnet);
1.60 frystyk 622: if (status == HT_WOULD_BLOCK)
623: return HT_WOULD_BLOCK;
624: else if (status == HT_LOADED) {
1.95 frystyk 625: /* begin _GM_ */
626: /* Note: libwww bug ID: GM8 */
627: /* if (ctrl->repcode/100 == 2) { */
628: /* If the FTP server doesn't support the REIN command, then
629: the return code will be 502 */
630: if ((ctrl->repcode/100 == 2) || (ctrl->repcode == 502)) {
631: /* end _GM_ */
1.66 frystyk 632: ctrl->substate = (ctrl->uid && *ctrl->uid) ?
633: NEED_UID : PROMPT_USER;
1.95 frystyk 634: } else {
1.60 frystyk 635: ctrl->substate = SUB_SUCCESS; /* hope the best */
1.95 frystyk 636: }
637: } else {
1.60 frystyk 638: ctrl->substate = SUB_ERROR;
1.95 frystyk 639: }
1.60 frystyk 640: ctrl->sent = NO;
1.22 frystyk 641: }
1.60 frystyk 642: break;
643:
644: case NEED_UID:
1.102 frystyk 645: HTTRACE(PROT_TRACE, "FTP Login.. now in state NEED_UID\n");
1.60 frystyk 646: if (!ctrl->sent) {
647: status = SendCommand(request, ctrl, "USER", ctrl->uid);
648: if (status == HT_WOULD_BLOCK)
649: return HT_WOULD_BLOCK;
650: else if (status == HT_ERROR)
651: ctrl->substate = SUB_ERROR;
652: ctrl->sent = YES;
1.22 frystyk 653: } else {
1.93 frystyk 654: status = HTHost_read(HTNet_host(cnet), cnet);
1.60 frystyk 655: if (status == HT_WOULD_BLOCK)
656: return HT_WOULD_BLOCK;
657: else if (status == HT_LOADED) {
658: int code = ctrl->repcode/100;
659: if (code == 2) /* Logged in w/o passwd! */
660: ctrl->substate = SUB_SUCCESS;
1.66 frystyk 661: else if (code == 3) { /* Password demanded */
662: ctrl->substate = (ctrl->passwd && *ctrl->passwd) ?
663: NEED_PASSWD : PROMPT_USER;
1.95 frystyk 664: /* begin _GM_ */
665: /* Note: libwww bug ID: GM3 */
666: /* } else if (ctrl->repcode == 530) */
667: /* ctrl->substate = PROMPT_USER;*/ /* User unknown */
668: } else if (ctrl->repcode == 530) {
669: if (ctrl->alreadyLoggedIn == YES) {
670: ctrl->substate = SUB_SUCCESS;
1.102 frystyk 671: HTTRACE(PROT_TRACE, "FTP Login.. Already logged in\n");
1.95 frystyk 672: } else {
673: ctrl->substate = PROMPT_USER;
1.102 frystyk 674: HTTRACE(PROT_TRACE, "FTP Login.. User Unknown\n");
1.95 frystyk 675: }
676: }
677: /* end _GM_ */
1.60 frystyk 678: else
679: ctrl->substate = SUB_ERROR;
680: } else
681: ctrl->substate = SUB_ERROR;
682: ctrl->sent = NO;
1.22 frystyk 683: }
1.60 frystyk 684: break;
1.1 timbl 685:
1.60 frystyk 686: case NEED_PASSWD:
1.102 frystyk 687: HTTRACE(PROT_TRACE, "FTP Login.. now in state NEED_PASSWD\n");
1.60 frystyk 688: if (!ctrl->sent) {
689: status = SendCommand(request, ctrl, "PASS", ctrl->passwd);
690: if (status == HT_WOULD_BLOCK)
691: return HT_WOULD_BLOCK;
692: else if (status == HT_ERROR)
693: ctrl->substate = SUB_ERROR;
694: ctrl->sent = YES;
695: } else {
1.93 frystyk 696: status = HTHost_read(HTNet_host(cnet), cnet);
1.60 frystyk 697: if (status == HT_WOULD_BLOCK)
698: return HT_WOULD_BLOCK;
699: else if (status == HT_LOADED) {
700: int code = ctrl->repcode/100;
701: if (code == 2) /* Logged in with passwd */
702: ctrl->substate = SUB_SUCCESS;
1.70 frystyk 703: else if (code == 3) { /* Account required */
704: HTAlertCallback *cbf = HTAlert_find(HT_A_PROMPT);
705: HTAlertPar * reply = HTAlert_newReply();
706: if (cbf && (*cbf)(request, HT_A_PROMPT,
707: HT_MSG_ACCOUNT, NULL, NULL, reply)) {
708: ctrl->account = HTAlert_replyMessage(reply);
1.60 frystyk 709: ctrl->substate = NEED_ACCOUNT;
1.70 frystyk 710: } else
1.60 frystyk 711: ctrl->substate = SUB_ERROR;
1.70 frystyk 712: HTAlert_deleteReply(reply);
1.60 frystyk 713: } else if (ctrl->repcode == 530)
714: ctrl->substate = PROMPT_USER;
715: else
716: ctrl->substate = SUB_ERROR;
717: } else
718: ctrl->substate = SUB_ERROR;
719: ctrl->sent = NO;
720: }
721: break;
1.1 timbl 722:
1.60 frystyk 723: case NEED_ACCOUNT:
1.102 frystyk 724: HTTRACE(PROT_TRACE, "FTP Login.. now in state NEED_ACCOUNT\n");
1.60 frystyk 725: if (!ctrl->sent) {
726: status = SendCommand(request, ctrl, "ACCT", ctrl->account);
727: if (status == HT_WOULD_BLOCK)
728: return HT_WOULD_BLOCK;
729: else if (status == HT_ERROR)
730: ctrl->substate = SUB_ERROR;
731: ctrl->sent = YES;
1.23 frystyk 732: } else {
1.93 frystyk 733: status = HTHost_read(HTNet_host(cnet), cnet);
1.60 frystyk 734: if (status == HT_WOULD_BLOCK)
735: return HT_WOULD_BLOCK;
736: else if (status == HT_LOADED) {
737: int code = ctrl->repcode/100;
738: if (code == 2) /* Logged in with account */
739: ctrl->substate = SUB_SUCCESS;
740: else
741: ctrl->substate = SUB_ERROR; /* hopeless */
742: } else
743: ctrl->substate = SUB_ERROR;
744: ctrl->sent = NO;
1.1 timbl 745: }
1.60 frystyk 746: break;
1.1 timbl 747:
1.60 frystyk 748: case PROMPT_USER:
1.102 frystyk 749: HTTRACE(PROT_TRACE, "FTP Login.. now in state PROMPT_USER\n");
1.60 frystyk 750: {
1.70 frystyk 751: HTAlertCallback *cbf = HTAlert_find(HT_A_USER_PW);
752: HTAlertPar * reply = HTAlert_newReply();
1.77 frystyk 753: HT_FREE(ctrl->uid);
754: HT_FREE(ctrl->passwd);
1.87 frystyk 755: if (cbf && (*cbf)(request, HT_A_USER_PW, HT_MSG_FTP_UID,
756: NULL, NULL, reply)){
1.70 frystyk 757: ctrl->uid = HTAlert_replyMessage(reply);
758: ctrl->passwd = HTAlert_replySecret(reply);
759: }
760: HTAlert_deleteReply(reply);
1.66 frystyk 761: if (ctrl->uid && *ctrl->uid && ctrl->passwd && *ctrl->passwd)
1.60 frystyk 762: ctrl->substate = NEED_UID;
763: else
764: ctrl->substate = SUB_ERROR;
765: }
766: break;
1.1 timbl 767:
1.60 frystyk 768: case SUB_ERROR:
1.102 frystyk 769: HTTRACE(PROT_TRACE, "FTP Login.. now in state SUB_ERROR\n");
1.95 frystyk 770: HTRequest_addError(request, ERR_FATAL, NO,
771: HTERR_FTP_LOGIN_FAILURE, NULL, 0, "HTFTPLogin");
1.102 frystyk 772: HTTRACE(PROT_TRACE, "FTP......... Login failed\n");
1.60 frystyk 773: ctrl->substate = 0;
774: return HT_ERROR;
775: break;
1.23 frystyk 776:
1.60 frystyk 777: case SUB_SUCCESS:
1.102 frystyk 778: HTTRACE(PROT_TRACE, "FTP Login.. now in state SUB_SUCCESS\n");
779: HTTRACE(PROT_TRACE, "FTP......... Logged in as `%s\'\n" _ ctrl->uid);
1.60 frystyk 780: ctrl->substate = 0;
781: return HT_OK;
782: break;
1.23 frystyk 783: }
1.22 frystyk 784: }
785: }
786:
1.60 frystyk 787: /* HTFTPDataConnection
788: ** -------------------
789: ** Prepares a data connection to the server and initializes the
790: ** transfer mode.
791: ** Returns HT_OK, HT_ERROR, or HT_WOULD_BLOCK
1.1 timbl 792: */
1.60 frystyk 793: PRIVATE int HTFTPDataConnection (HTRequest * request, HTNet *cnet,
794: ftp_ctrl *ctrl, ftp_data *data)
1.1 timbl 795: {
1.60 frystyk 796: int status;
797: HTNet *dnet = ctrl->dnet;
798: typedef enum _state {
799: SUB_ERROR = -2,
800: SUB_SUCCESS = -1,
801: NEED_TYPE = 0,
802: NEED_SELECT,
803: NEED_PASV,
804: NEED_PORT
805: } state;
806:
807: /* Jump into a second level state machine */
808: while (1) {
809: switch ((state) ctrl->substate) {
810: case NEED_TYPE:
1.102 frystyk 811: HTTRACE(PROT_TRACE, "FTP Data.... now in state NEED_TYPE\n");
1.60 frystyk 812: if(!data->type|| data->pasv || data->type=='N' || data->type=='L'){
813: ctrl->substate = NEED_SELECT;
814: break;
815: }
816: if (!ctrl->sent) {
817: char type[2];
818: *type = data->type;
819: *(type+1) = '\0';
820: status = SendCommand(request, ctrl, "TYPE", type);
821: if (status == HT_WOULD_BLOCK)
822: return HT_WOULD_BLOCK;
823: else if (status == HT_ERROR)
824: ctrl->substate = SUB_ERROR;
825: ctrl->sent = YES;
826: } else {
1.93 frystyk 827: status = HTHost_read(HTNet_host(cnet), cnet);
1.60 frystyk 828: if (status == HT_WOULD_BLOCK)
829: return HT_WOULD_BLOCK;
830: else if (status == HT_LOADED) {
831: if (ctrl->repcode/100 == 2)
832: ctrl->substate = NEED_SELECT;
833: else
834: ctrl->substate = SUB_ERROR;
835: } else
836: ctrl->substate = SUB_ERROR;
837: ctrl->sent = NO;
838: }
839: break;
840:
841: case NEED_SELECT:
1.102 frystyk 842: HTTRACE(PROT_TRACE, "FTP Data.... now in state NEED_SELECT\n");
1.60 frystyk 843: if (FTPMode & FTP_DATA_PASV && !data->pasv)
844: ctrl->substate = NEED_PASV;
845: else if (ListenSocket(cnet, dnet, data))
846: ctrl->substate = NEED_PORT;
847: else
848: ctrl->substate = SUB_ERROR;
849: break;
850:
851: case NEED_PASV:
1.102 frystyk 852: HTTRACE(PROT_TRACE, "FTP Data.... now in state NEED_PASV\n");
1.60 frystyk 853: if (!ctrl->sent) {
854: status = SendCommand(request, ctrl, "PASV", NULL);
855: if (status == HT_WOULD_BLOCK)
856: return HT_WOULD_BLOCK;
857: else if (status == HT_ERROR)
858: ctrl->substate = SUB_ERROR;
859: ctrl->sent = YES;
860: } else {
1.93 frystyk 861: status = HTHost_read(HTNet_host(cnet), cnet);
1.60 frystyk 862: if (status == HT_WOULD_BLOCK)
863: return HT_WOULD_BLOCK;
864: else if (status == HT_LOADED) {
865: if (ctrl->repcode == 227) {
866: /*
867: ** If succes, we have to scan for the returned number.
868: ** As the format for the response isn't standard,
869: ** the best thing to do is to scan for the first digit
870: ** after the status code, see RFC1123
871: */
872: char *host = ctrl->reply;
873: int h0, h1, h2, h3, p0=0, p1=0;
1.94 frystyk 874: while (*host && !isdigit((int) *host++));
1.60 frystyk 875: if (!*host || sscanf(--host, "%d,%d,%d,%d,%d,%d",
876: &h0,&h1,&h2,&h3,&p0,&p1) < 6) {
1.102 frystyk 877: HTTRACE(PROT_TRACE, "FTP Data.... PASV No addr\n");
1.60 frystyk 878: ctrl->substate = SUB_ERROR;
879: break;
880: } else {
881: int port = (p0<<8)+p1;
882: sprintf(data->host, "ftp://%d.%d.%d.%d:%d/",
883: h0, h1, h2, h3, port);
884: data->pasv = YES;
885: ctrl->substate = SUB_SUCCESS;
886: }
887: } else {
888: ctrl->substate = ListenSocket(cnet, dnet, data) ?
889: NEED_PORT : SUB_ERROR;
890: }
891: } else
892: ctrl->substate = SUB_ERROR;
893: ctrl->sent = NO;
894: }
895: break;
1.22 frystyk 896:
1.60 frystyk 897: case NEED_PORT:
1.102 frystyk 898: HTTRACE(PROT_TRACE, "FTP Data.... now in state NEED_PORT\n");
1.60 frystyk 899: if (!ctrl->sent) {
900: status = SendCommand(request, ctrl, "PORT", data->host);
901: if (status == HT_WOULD_BLOCK)
902: return HT_WOULD_BLOCK;
903: else if (status == HT_ERROR)
904: ctrl->substate = SUB_ERROR;
905: ctrl->sent = YES;
906: } else {
1.93 frystyk 907: status = HTHost_read(HTNet_host(cnet), cnet);
1.60 frystyk 908: if (status == HT_WOULD_BLOCK)
909: return HT_WOULD_BLOCK;
910: else if (status == HT_LOADED) {
911: data->pasv = NO;
912: ctrl->substate = (ctrl->repcode/100 == 2) ?
913: SUB_SUCCESS : SUB_ERROR;
914: } else
915: ctrl->substate = SUB_ERROR;
916: ctrl->sent = NO;
917: }
918: break;
1.1 timbl 919:
1.60 frystyk 920: case SUB_ERROR:
1.102 frystyk 921: HTTRACE(PROT_TRACE, "FTP Data.... now in state SUB_ERROR\n");
922: HTTRACE(PROT_TRACE, "FTP Data.... Can't setup data connection\n");
1.60 frystyk 923: ctrl->substate = 0;
924: return HT_ERROR;
925: break;
1.1 timbl 926:
1.60 frystyk 927: case SUB_SUCCESS:
1.102 frystyk 928: HTTRACE(PROT_TRACE, "FTP Data.... now in state SUB_SUCCESS\n");
929: HTTRACE(PROT_TRACE, "FTP Data.... Data connection negotiated\n");
1.60 frystyk 930: ctrl->substate = 0;
931: return HT_OK;
932: break;
1.23 frystyk 933: }
1.58 frystyk 934: }
1.23 frystyk 935: }
1.1 timbl 936:
937:
1.60 frystyk 938: /* HTFTPServerInfo
939: ** ---------------
940: ** This function finds out what server we are talking to.
941: ** Maybe we can upgrade from NLST to LIST.
942: ** Returns HT_OK, HT_ERROR, or HT_WOULD_BLOCK
943: ** Thanks to James.W.Matthews@Dartmouth.EDU (James W. Matthews) for making
944: ** his code available.
1.1 timbl 945: */
1.60 frystyk 946: PRIVATE int HTFTPServerInfo (HTRequest *request, HTNet *cnet,
947: ftp_ctrl *ctrl, ftp_data *data)
1.23 frystyk 948: {
1.60 frystyk 949: int status;
950: typedef enum _state {
951: SUB_ERROR = -2,
952: SUB_SUCCESS = -1,
953: NEED_SYST = 0,
954: CHECK_SYST,
955: NEED_PWD,
956: CHECK_PWD
957: } state;
958:
959: /* Jump into a second level state machine */
960: while (1) {
961: switch ((state) ctrl->substate) {
962: case NEED_SYST:
1.102 frystyk 963: HTTRACE(PROT_TRACE, "FTP Server.. now in state NEED_SYST\n");
1.60 frystyk 964: if (!ctrl->sent) {
1.63 frystyk 965: if (ctrl->server != FTP_UNSURE) {
1.60 frystyk 966: FTPListType(data, ctrl->server);
967: return HT_OK;
968: }
969: status = SendCommand(request, ctrl, "SYST", NULL);
970: if (status == HT_WOULD_BLOCK)
971: return HT_WOULD_BLOCK;
972: else if (status == HT_ERROR)
973: ctrl->substate = SUB_ERROR;
974: ctrl->sent = YES;
975: } else {
1.93 frystyk 976: status = HTHost_read(HTNet_host(cnet), cnet);
1.60 frystyk 977: if (status == HT_WOULD_BLOCK)
978: return HT_WOULD_BLOCK;
979: else if (status == HT_LOADED) {
980: ctrl->substate=ctrl->repcode==215 ? CHECK_SYST : NEED_PWD;
981: } else
982: ctrl->substate = SUB_ERROR;
983: ctrl->sent = NO;
1.23 frystyk 984: }
985: break;
986:
1.60 frystyk 987: case CHECK_SYST:
1.102 frystyk 988: HTTRACE(PROT_TRACE, "FTP Server.. now in state CHECK_SYST\n");
1.23 frystyk 989: {
1.60 frystyk 990: char *reply = ctrl->reply;
991: if (!*reply) {
1.102 frystyk 992: HTTRACE(PROT_TRACE, "FTP Server.. No server info?\n");
1.60 frystyk 993: ctrl->substate = NEED_PWD;
1.23 frystyk 994: break;
995: }
1.60 frystyk 996: if (strncmp(reply, "UNIX Type: L8MAC-OSMachTen", 28) == 0) {
997: ctrl->server = FTP_MACHTEN;
998: } else if (strstr(reply, "UNIX") != NULL) {
999: ctrl->server = FTP_UNIX;
1000: } else if (strncmp(reply, "VMS", 3) == 0) {
1001: ctrl->server = FTP_VMS;
1002: } else if ((strncmp(reply, "VM/CMS", 6) == 0) ||
1003: (strncmp(reply, "VM", 2) == 0)) {
1004: ctrl->server = FTP_CMS;
1005: } else if (strncmp(reply, "DCTS", 4) == 0) {
1006: ctrl->server = FTP_DCTS;
1007: } else if (strstr(reply, "MAC-OS TCP/ConnectII") != NULL) {
1.23 frystyk 1008: /* Check old versions of TCP/C using / in pathnames */
1.63 frystyk 1009: ctrl->server = FTP_TCPC + FTP_UNSURE;
1.60 frystyk 1010: } else if (strncmp(reply, "MACOS Peter's Server", 20) == 0) {
1011: ctrl->server = FTP_PETER_LEWIS;
1012: } else if (strncmp(reply, "Windows_NT", 10) == 0) {
1013: ctrl->server = FTP_WINNT;
1.23 frystyk 1014: }
1015:
1016: /* If we are unsure, try PWD to get more information */
1.60 frystyk 1017: if (ctrl->server & FTP_UNSURE)
1018: ctrl->substate = NEED_PWD;
1.23 frystyk 1019: else
1.60 frystyk 1020: ctrl->substate = SUB_SUCCESS;
1.1 timbl 1021: }
1.23 frystyk 1022: break;
1023:
1024: case NEED_PWD:
1.102 frystyk 1025: HTTRACE(PROT_TRACE, "FTP Server.. now in state NEED_PWD\n");
1.60 frystyk 1026: if (!ctrl->sent) {
1027: status = SendCommand(request, ctrl, "PWD", NULL);
1028: if (status == HT_WOULD_BLOCK)
1029: return HT_WOULD_BLOCK;
1030: else if (status == HT_ERROR)
1031: ctrl->substate = SUB_ERROR;
1032: ctrl->sent = YES;
1.23 frystyk 1033: } else {
1.93 frystyk 1034: status = HTHost_read(HTNet_host(cnet), cnet);
1.60 frystyk 1035: if (status == HT_WOULD_BLOCK)
1036: return HT_WOULD_BLOCK;
1037: else if (status == HT_LOADED) {
1038: ctrl->substate = (ctrl->repcode/100 == 2) ?
1039: CHECK_PWD : SUB_ERROR;
1040: } else
1041: ctrl->substate = SUB_ERROR;
1042: ctrl->sent = NO;
1.23 frystyk 1043: }
1044: break;
1045:
1.60 frystyk 1046: case CHECK_PWD:
1.102 frystyk 1047: HTTRACE(PROT_TRACE, "FTP Server.. now in state CHECK_PWD\n");
1.60 frystyk 1048: {
1049: char *start = strchr(ctrl->reply, '"');
1050: char *end;
1051: if (!start || (end = strchr(++start, '"')) == NULL) {
1.102 frystyk 1052: HTTRACE(PROT_TRACE, "FTP Server.. No current directory?\n");
1.60 frystyk 1053: ctrl->server = FTP_GENERIC;
1.23 frystyk 1054: } else {
1.60 frystyk 1055: *end = '\0';
1.63 frystyk 1056: if (ctrl->server & FTP_TCPC) {
1.60 frystyk 1057: ctrl->server = *start == '/' ? FTP_NCSA : FTP_TCPC;
1058: } else if (*start == '/') {
1059: /* path names starting with / imply Unix, right? */
1060: ctrl->server = FTP_UNIX;
1061: } else if (*(end-1) == ']') {
1062: /* path names ending with ] imply VMS, right? */
1063: ctrl->server = FTP_VMS;
1064: } else
1065: ctrl->server = FTP_GENERIC;
1.23 frystyk 1066: }
1.60 frystyk 1067: ctrl->substate = SUB_SUCCESS;
1.1 timbl 1068: }
1.23 frystyk 1069: break;
1.1 timbl 1070:
1.60 frystyk 1071: case SUB_ERROR:
1.102 frystyk 1072: HTTRACE(PROT_TRACE, "FTP Server.. now in state SUB_ERROR\n");
1073: HTTRACE(PROT_TRACE, "FTP Server.. Can't get server information\n");
1.60 frystyk 1074: ctrl->substate = 0;
1.63 frystyk 1075: ctrl->server = FTP_GENERIC;
1.60 frystyk 1076: return HT_ERROR;
1.23 frystyk 1077: break;
1.22 frystyk 1078:
1.60 frystyk 1079: case SUB_SUCCESS:
1.102 frystyk 1080: HTTRACE(PROT_TRACE, "FTP Server.. now in state SUB_SUCCESS\n");
1.95 frystyk 1081: {
1.81 frystyk 1082: HTHost * host = HTNet_host(cnet);
1.102 frystyk 1083: HTTRACE(PROT_TRACE, "FTP Server.. Guessed type %d\n" _ ctrl->server);
1.81 frystyk 1084: HTHost_setVersion(host, ctrl->server);
1085: FTPListType(data, ctrl->server);
1086: ctrl->substate = 0;
1087: return HT_OK;
1088: break;
1.95 frystyk 1089: }
1.22 frystyk 1090: }
1.23 frystyk 1091: }
1092: }
1093:
1.60 frystyk 1094: /* HTFTPGetData
1095: ** ------------
1096: ** This function asks for the file or a directory. First we try in one go,
1.23 frystyk 1097: ** but if that doesn't work, then we use CWD for each segment and then
1098: ** try to retrieve it. If that also fails, then we try if it is a
1.60 frystyk 1099: ** directory.
1100: ** Returns HT_OK, HT_LOADED, HT_ERROR, or HT_WOULD_BLOCK
1.23 frystyk 1101: */
1.71 frystyk 1102: PRIVATE int HTFTPGetData (HTRequest *request, HTNet *cnet, SOCKET sockfd,
1.60 frystyk 1103: ftp_ctrl *ctrl, ftp_data *data)
1.23 frystyk 1104: {
1105: int status;
1.60 frystyk 1106: char *segment = NULL;
1107: HTNet *dnet = ctrl->dnet;
1.93 frystyk 1108: BOOL data_is_active = (sockfd == HTNet_socket(dnet));
1.60 frystyk 1109: typedef enum _state {
1110: SUB_ERROR = -2,
1111: SUB_SUCCESS = -1,
1112: NEED_SELECT = 0,
1113: NEED_CONNECT,
1114: NEED_ACCEPT,
1115: NEED_ACTION,
1116: NEED_CWD,
1117: NEED_SEGMENT,
1118: NEED_STREAM,
1.63 frystyk 1119: NEED_BODY
1.60 frystyk 1120: } state;
1121:
1122: /* Jump into a second level state machine */
1123: while (1) {
1124: switch ((state) ctrl->substate) {
1125: case NEED_SELECT:
1.102 frystyk 1126: HTTRACE(PROT_TRACE, "FTP Get Data now in state NEED_SELECT\n");
1.60 frystyk 1127: ctrl->substate = data->pasv ? NEED_CONNECT : NEED_ACTION;
1128: break;
1129:
1130: case NEED_CONNECT:
1.102 frystyk 1131: HTTRACE(PROT_TRACE, "FTP Get Data now in state NEED_CONNECT\n");
1.93 frystyk 1132: status = HTHost_connect(HTNet_host(dnet), dnet, data->host, FTP_DATA);
1.60 frystyk 1133: if (status == HT_WOULD_BLOCK)
1134: return HT_WOULD_BLOCK;
1135: else if (status == HT_OK) {
1.102 frystyk 1136: HTTRACE(PROT_TRACE, "FTP Get Data.... Active data socket %d\n" _
1.95 frystyk 1137: HTNet_socket(dnet));
1.93 frystyk 1138: #if 0
1.92 eric 1139: /* HTNet_setPersistent(dnet, YES, HT_TP_INTERLEAVE); */
1140: HTNet_setPersistent(dnet, YES, HT_TP_SINGLE);
1.93 frystyk 1141: #endif
1.60 frystyk 1142: ctrl->substate = NEED_ACTION;
1.81 frystyk 1143: } else { /* Swap to PORT on the fly */
1.91 frystyk 1144: NETCLOSE(HTNet_socket(dnet));
1145: HTNet_setSocket(dnet, INVSOC);
1.102 frystyk 1146: HTTRACE(PROT_TRACE, "FTP Get Data Swap to PORT on the fly\n");
1.89 frystyk 1147: ctrl->substate = NEED_SELECT;
1.77 frystyk 1148: HT_FREE(segment);
1.60 frystyk 1149: return HT_OK;
1150: }
1.23 frystyk 1151: break;
1152:
1.60 frystyk 1153: case NEED_ACCEPT:
1.102 frystyk 1154: HTTRACE(PROT_TRACE, "FTP Get Data now in state NEED_ACCEPT\n");
1.60 frystyk 1155: {
1.86 frystyk 1156: status = HTDoAccept(ctrl->dnet, &ctrl->dnet);
1157: dnet = ctrl->dnet;
1.60 frystyk 1158: if (status == HT_WOULD_BLOCK)
1159: return HT_WOULD_BLOCK;
1160: else if (status == HT_OK) {
1.102 frystyk 1161: HTTRACE(PROT_TRACE, "FTP Get Data.... Passive data socket %d\n" _
1.91 frystyk 1162: HTNet_socket(dnet));
1.60 frystyk 1163: ctrl->substate = NEED_STREAM;
1164: } else
1165: ctrl->substate = SUB_ERROR;
1.23 frystyk 1166: }
1.60 frystyk 1167: break;
1.33 frystyk 1168:
1.60 frystyk 1169: case NEED_ACTION:
1.102 frystyk 1170: HTTRACE(PROT_TRACE, "FTP Get Data now in state NEED_ACTION\n");
1.60 frystyk 1171: if (!ctrl->sent) {
1172: char *cmd = (data->type=='L') ? "LIST" :
1173: (data->type=='N') ? "NLST" : "RETR";
1174: StrAllocCopy(segment, data->offset);
1175: HTUnEscape(segment);
1176: HTCleanTelnetString(segment);
1177: status = SendCommand(request, ctrl, cmd, segment);
1.77 frystyk 1178: HT_FREE(segment);
1.60 frystyk 1179: if (status == HT_WOULD_BLOCK)
1180: return HT_WOULD_BLOCK;
1181: else if (status == HT_ERROR)
1182: ctrl->substate = SUB_ERROR;
1183: ctrl->sent = YES;
1184: } else {
1.93 frystyk 1185: status = HTHost_read(HTNet_host(cnet), cnet);
1.60 frystyk 1186: if (status == HT_WOULD_BLOCK)
1187: return HT_WOULD_BLOCK;
1188: else if (status == HT_LOADED) {
1189: int code = ctrl->repcode;
1190: if (code==125 || code==150 || code==225)
1.93 frystyk 1191: ctrl->substate = data->pasv ? NEED_STREAM : NEED_ACCEPT;
1.60 frystyk 1192: else if (code/100==5 && !ctrl->cwd)
1193: ctrl->substate = NEED_SEGMENT;
1.95 frystyk 1194: /* begin _GM_ */
1195: /* Note: libwww bug ID: GM9 */
1196: /* else */
1197: /* ctrl->substate = SUB_ERROR; */
1198: else {
1199: if (ctrl->repcode == 550) {
1.102 frystyk 1200: HTTRACE(PROT_TRACE, "FTP Get Data no such file or directory\n");
1.95 frystyk 1201: data->stream_error = YES;
1202: }
1203: ctrl->substate = SUB_ERROR;
1204: }
1205: /* end _GM_ */
1.60 frystyk 1206: } else
1207: ctrl->substate = SUB_ERROR;
1208: ctrl->sent = NO;
1.23 frystyk 1209: }
1210: break;
1.60 frystyk 1211:
1212: case NEED_SEGMENT:
1.102 frystyk 1213: HTTRACE(PROT_TRACE, "FTP Get Data now in state NEED_SEGMENT\n");
1.23 frystyk 1214: {
1.60 frystyk 1215: char *ptr;
1216: if (data->offset == data->file) {
1.63 frystyk 1217: if (ctrl->server == FTP_VMS) { /* Change to root */
1.77 frystyk 1218: if ((segment = (char *) HT_MALLOC(strlen(ctrl->uid)+3)) == NULL)
1219: HT_OUTOFMEM("segment ");
1.63 frystyk 1220: sprintf(segment, "[%s]", ctrl->uid);
1221: } else
1222: StrAllocCopy(segment, "/");
1.60 frystyk 1223: data->offset++;
1224: ctrl->substate = NEED_CWD;
1225: } else {
1226: if ((ptr = strchr(data->offset, '/'))) {
1227: *ptr='\0';
1228: StrAllocCopy(segment, data->offset);
1229: *ptr='/';
1230: data->offset = ++ptr;
1231: HTUnEscape(segment);
1232: HTCleanTelnetString(segment);
1233: ctrl->substate = NEED_CWD;
1.33 frystyk 1234: } else
1.60 frystyk 1235: ctrl->substate = NEED_ACTION;
1.23 frystyk 1236: }
1237: }
1238: break;
1239:
1.60 frystyk 1240: case NEED_CWD:
1.102 frystyk 1241: HTTRACE(PROT_TRACE, "FTP Get Data now in state NEED_CWD\n");
1.60 frystyk 1242: if (!ctrl->sent) {
1243: status = SendCommand(request, ctrl, "CWD", segment);
1.77 frystyk 1244: HT_FREE(segment);
1.60 frystyk 1245: if (status == HT_WOULD_BLOCK)
1246: return HT_WOULD_BLOCK;
1247: else if (status == HT_ERROR)
1248: ctrl->substate = SUB_ERROR;
1249: ctrl->cwd = YES;
1250: ctrl->sent = YES;
1251: } else {
1.93 frystyk 1252: status = HTHost_read(HTNet_host(cnet), cnet);
1.60 frystyk 1253: if (status == HT_WOULD_BLOCK)
1254: return HT_WOULD_BLOCK;
1255: else if (status == HT_LOADED) {
1256: if (ctrl->repcode/100 == 2)
1257: ctrl->substate = NEED_SEGMENT;
1258: else
1259: ctrl->substate = SUB_ERROR;
1260: } else
1261: ctrl->substate = SUB_ERROR;
1262: ctrl->sent = NO;
1.33 frystyk 1263: }
1.60 frystyk 1264: break;
1.33 frystyk 1265:
1.81 frystyk 1266: case NEED_STREAM:
1.102 frystyk 1267: HTTRACE(PROT_TRACE, "FTP Get Data now in state NEED_STREAM\n");
1.81 frystyk 1268: /*
1269: ** Create the stream pipe FROM the channel to the application.
1270: ** The target for the input stream pipe is set up using the
1271: ** stream stack.
1272: */
1.93 frystyk 1273: {
1274: HTStream * target = FTP_DIR(data) ?
1275: HTFTPDir_new(request, ctrl->server, data->type) :
1276: HTStreamStack(HTAnchor_format(HTRequest_anchor(request)),
1277: HTRequest_outputFormat(request),
1278: HTRequest_outputStream(request),
1279: request, YES);
1280: HTNet_setReadStream(dnet, target);
1.81 frystyk 1281: }
1.86 frystyk 1282: HTRequest_setOutputConnected(request, YES);
1.93 frystyk 1283: data_is_active = YES;
1.81 frystyk 1284: ctrl->substate = NEED_BODY;
1.91 frystyk 1285: break;
1.23 frystyk 1286:
1.60 frystyk 1287: case NEED_BODY:
1.102 frystyk 1288: HTTRACE(PROT_TRACE, "FTP Get Data now in state NEED_BODY\n");
1.93 frystyk 1289: if (data_is_active) {
1290: status = HTHost_read(HTNet_host(dnet), dnet);
1291: if (status == HT_WOULD_BLOCK)
1292: return HT_WOULD_BLOCK;
1293: else if (status == HT_LOADED || status == HT_CLOSED) {
1294: data->complete |= 1;
1295: if (data->complete >= 3)
1296: ctrl->substate = SUB_SUCCESS;
1297: else
1298: data_is_active = NO;
1299: } else {
1300: ctrl->substate = SUB_ERROR;
1301: data->stream_error = YES;
1302: }
1303: } else {
1304: status = HTHost_read(HTNet_host(cnet), cnet);
1305: if (status == HT_WOULD_BLOCK)
1306: return HT_WOULD_BLOCK;
1307: else if (status == HT_LOADED || status == HT_CLOSED) {
1308: if (ctrl->repcode/100 == 2) {
1309: data->complete |= 2;
1310: if (data->complete >= 3)
1311: ctrl->substate = SUB_SUCCESS;
1312: else
1313: data_is_active = YES;
1314: } else
1315: ctrl->substate = SUB_ERROR;
1316: } else
1317: ctrl->substate = SUB_ERROR;
1318: }
1319: break;
1.22 frystyk 1320:
1.60 frystyk 1321: case SUB_ERROR:
1.102 frystyk 1322: HTTRACE(PROT_TRACE, "FTP Get Data now in state SUB_ERROR\n");
1.95 frystyk 1323: HTRequest_addError(request, ERR_FATAL, NO, HTERR_NOT_FOUND,
1324: NULL, 0, "HTFTPGetData");
1.60 frystyk 1325: ctrl->substate = 0;
1.77 frystyk 1326: HT_FREE(segment);
1.60 frystyk 1327: return HT_ERROR;
1.23 frystyk 1328: break;
1329:
1.60 frystyk 1330: case SUB_SUCCESS:
1.102 frystyk 1331: HTTRACE(PROT_TRACE, "FTP Get Data now in state SUB_SUCCESS\n");
1.60 frystyk 1332: ctrl->substate = 0;
1.77 frystyk 1333: HT_FREE(segment);
1.60 frystyk 1334: return HT_LOADED;
1.23 frystyk 1335: break;
1.1 timbl 1336: }
1337: }
1.23 frystyk 1338: }
1.1 timbl 1339:
1.23 frystyk 1340: /* ------------------------------------------------------------------------- */
1341:
1342: /* Retrieve File from Server as an atomic action.
1343: ** -----------------------------------------------
1.58 frystyk 1344: ** Given a hypertext address, this routine loads a document.
1.23 frystyk 1345: **
1346: ** On entry,
1.58 frystyk 1347: ** request This is the request structure
1348: ** returns HT_ERROR Error has occured in call back
1349: ** HT_OK Call back was OK
1350: */
1.91 frystyk 1351: PRIVATE int FTPEvent (SOCKET soc, void * pVoid, HTEventType type);
1352:
1353: PUBLIC int HTLoadFTP (SOCKET soc, HTRequest * request)
1.58 frystyk 1354: {
1.84 frystyk 1355: HTNet * cnet = HTRequest_net(request);
1356: ftp_ctrl * ctrl = NULL;
1357: ftp_data * data = NULL;
1358: HTParentAnchor * anchor = HTRequest_anchor(request);
1359: char * url = HTAnchor_physical(anchor);
1.23 frystyk 1360:
1.97 frystyk 1361:
1.58 frystyk 1362: /*
1.60 frystyk 1363: ** Initiate a new FTP ctrl and data structure and bind to request structure
1.58 frystyk 1364: ** This is actually state FTP_BEGIN, but it can't be in the state
1365: ** machine as we need the structure first.
1366: */
1.102 frystyk 1367: HTTRACE(PROT_TRACE, "FTP......... Looking for `%s\'\n" _ url);
1.91 frystyk 1368: if ((ctrl = (ftp_ctrl *) HT_CALLOC(1, sizeof(ftp_ctrl))) == NULL ||
1369: (data = (ftp_data *) HT_CALLOC(1, sizeof(ftp_data))) == NULL)
1370: HT_OUTOFMEM("HTLoadFTP");
1371: ctrl->cmd = HTChunk_new(128);
1372: ctrl->state = FTP_BEGIN;
1373: ctrl->server = FTP_UNSURE;
1374: ctrl->dnet = HTNet_dup(cnet);
1.93 frystyk 1375: ctrl->cnet = cnet;
1.91 frystyk 1376: HTNet_setContext(cnet, ctrl);
1377: HTNet_setEventCallback(cnet, FTPEvent);
1.92 eric 1378: HTNet_setEventParam(cnet, ctrl);
1.98 frystyk 1379: HTNet_setRawBytesCount(ctrl->dnet, YES);
1.97 frystyk 1380:
1.92 eric 1381: /* for now, the dnet comes back to the same place
1382: ** - vestigial from when the callback was from the request object
1383: */
1384: HTNet_setContext(ctrl->dnet, data);
1385: HTNet_setEventCallback(ctrl->dnet, FTPEvent);
1386: HTNet_setEventParam(ctrl->dnet, ctrl);
1.91 frystyk 1387: return FTPEvent(soc, ctrl, HTEvent_BEGIN);
1388: }
1389:
1390: PRIVATE int FTPEvent (SOCKET soc, void * pVoid, HTEventType type)
1391: {
1.93 frystyk 1392: ftp_ctrl * ctrl = (ftp_ctrl *) pVoid;
1393: ftp_data * data = (ftp_data *) HTNet_context(ctrl->dnet);
1.91 frystyk 1394: int status = HT_ERROR;
1.93 frystyk 1395: HTNet * cnet = ctrl->cnet;
1.91 frystyk 1396: HTRequest * request = HTNet_request(cnet);
1397: HTParentAnchor * anchor = HTRequest_anchor(request);
1398: char * url = HTAnchor_physical(anchor);
1399:
1.97 frystyk 1400: HTHost *host = HTNet_host(cnet);
1401:
1402:
1.91 frystyk 1403: if (type == HTEvent_CLOSE) { /* Interrupted */
1.98 frystyk 1404: if (soc == HTNet_socket(cnet))
1405: FTPCleanup(request, HT_INTERRUPTED);
1.60 frystyk 1406: else
1.98 frystyk 1407: FTPCleanup(request, HT_LOADED);
1408: return HT_OK;
1409: } else if (type == HTEvent_TIMEOUT) {
1410: HTRequest_addError(request, ERR_FATAL, NO, HTERR_TIME_OUT,
1411: NULL, 0, "HTLoadHTTP");
1412: FTPCleanup(request, HT_TIMEOUT);
1.60 frystyk 1413: return HT_OK;
1.98 frystyk 1414:
1.60 frystyk 1415: } else {
1.93 frystyk 1416: ctrl = (ftp_ctrl *) HTNet_context(cnet); /* Get existing copy */
1417: data = (ftp_data *) HTNet_context(ctrl->dnet);
1.60 frystyk 1418: }
1.58 frystyk 1419:
1420: /* Now jump into the machine. We know the state from the previous run */
1421: while (1) {
1422: switch (ctrl->state) {
1.60 frystyk 1423: case FTP_BEGIN:
1.102 frystyk 1424: HTTRACE(PROT_TRACE, "FTP Event... now in state FTP_BEGIN\n");
1.100 frystyk 1425:
1426: /* Only handle GET requests for now */
1427: if (HTRequest_method(request) != METHOD_GET) {
1.102 frystyk 1428: HTTRACE(PROT_TRACE, "FTP Event... This module only supports the GET method\n");
1.100 frystyk 1429: ctrl->state = FTP_ERROR;
1430: break;
1431: }
1432:
1.95 frystyk 1433: HTFTPParseURL(request, url, ctrl, data);
1.97 frystyk 1434:
1.95 frystyk 1435: /* The following is added by Neil Griffin, GAIN Software */
1436:
1437: /*
1438: ** If the user hasn't specified a permanent transfer type, then
1439: ** use the transfer type specified at the end of the URL.
1440: */
1441: if (g_FTPTransferMode == FTP_DEFAULT_TRANSFER_MODE) {
1442: switch (data->type) {
1443: case 'a' : data->type = 'A'; break;
1444: case 'A' : data->type = 'A'; break;
1445: case 'i' : data->type = 'I'; break;
1446: case 'I' : data->type = 'I'; break;
1447: case 'd' : FTPListType(data, ctrl->server); break;
1448: case 'D' : FTPListType(data, ctrl->server); break;
1449: default : data->type = 'I'; break;
1450: }
1451:
1452: /* Otherwise, use the permanent transfer type specified by the user. */
1453: } else {
1454: switch (g_FTPTransferMode) {
1455: case FTP_ASCII_TRANSFER_MODE : data->type = 'A'; break;
1456: case FTP_BINARY_TRANSFER_MODE : data->type = 'I'; break;
1457: case FTP_DIR_TRANSFER_MODE : FTPListType(data, ctrl->server); break;
1458: default : data->type = 'I'; break;
1459: }
1460: }
1461:
1.102 frystyk 1462: HTTRACE(PROT_TRACE, "FTP Event... Transfer mode set to '%c'\n" _ data->type);
1.95 frystyk 1463:
1464: /*
1465: ** See if we can get any hints to what we might expect content wise.
1466: */
1467: if (!FTP_DIR(data)) HTBind_getAnchorBindings(anchor);
1.98 frystyk 1468:
1469: /* Ready for next state */
1470: ctrl->state = FTP_NEED_CCON;
1471: break;
1.60 frystyk 1472:
1.95 frystyk 1473: case FTP_NEED_CCON:
1.102 frystyk 1474: HTTRACE(PROT_TRACE, "FTP Event... now in state FTP_NEED_CONN\n");
1.97 frystyk 1475: status = HTHost_connect(host, cnet, url, FTP_PORT);
1476: host = HTNet_host(cnet);
1.60 frystyk 1477: if (status == HT_OK) {
1.81 frystyk 1478:
1479: /*
1480: ** Check the protocol class to see if we have connected to a
1481: ** the right class of server, in this case HTTP.
1482: */
1483: {
1484: char * s_class = HTHost_class(host);
1485: if (s_class && strcasecomp(s_class, "ftp")) {
1486: HTRequest_addError(request, ERR_FATAL, NO, HTERR_CLASS,
1487: NULL, 0, "HTLoadNews");
1488: ctrl->state = FTP_ERROR;
1489: break;
1490: }
1491: HTHost_setClass(host, "ftp");
1492: }
1493:
1494: /* Check persistent connection */
1495: if (HTNet_persistent(cnet)) {
1496: ctrl->server = HTHost_version(host);
1.102 frystyk 1497: HTTRACE(PROT_TRACE, "FTP Server.. Cache says type %d server\n" _
1.81 frystyk 1498: ctrl->server);
1499: ctrl->reset = 1;
1500: } else
1.88 frystyk 1501: HTNet_setPersistent(cnet, YES, HT_TP_SINGLE);
1.23 frystyk 1502:
1.81 frystyk 1503: /*
1504: ** Create the stream pipe FROM the channel to the application.
1505: ** The target for the input stream pipe is set up using the
1506: ** stream stack.
1507: */
1.93 frystyk 1508: {
1509: HTStream * readstream = FTPStatus_new(request, ctrl, host);
1510: HTNet_setReadStream(cnet, readstream);
1511: }
1.23 frystyk 1512:
1.60 frystyk 1513: /*
1.81 frystyk 1514: ** Create the stream pipe TO the channel from the application
1515: ** and hook it up to the request object
1516: */
1517: {
1518: HTOutputStream * output = HTNet_getOutput(cnet, NULL, 0);
1519: HTRequest_setInputStream(request, (HTStream *) output);
1520: }
1521:
1522: /*
1.60 frystyk 1523: ** Set up concurrent read/write if this request isn't the
1524: ** source for a PUT or POST. As source we don't start reading
1525: ** before all destinations are ready. If destination then
1526: ** register the input stream and get ready for read
1527: */
1528: if (HTRequest_isPostWeb(request)) {
1.93 frystyk 1529: HTEvent * event = HTNet_event(cnet);
1530: HTEvent_register(HTNet_socket(cnet), HTEvent_READ, event);
1.60 frystyk 1531: HTRequest_linkDestination(request);
1532: }
1533:
1534: ctrl->state = FTP_NEED_LOGIN;
1.88 frystyk 1535: } else if (status == HT_WOULD_BLOCK || status == HT_PENDING)
1.60 frystyk 1536: return HT_OK;
1537: else
1538: ctrl->state = FTP_ERROR; /* Error or interrupt */
1539: break;
1540:
1541: case FTP_NEED_LOGIN:
1.102 frystyk 1542: HTTRACE(PROT_TRACE, "FTP Event... now in state FTP_NEED_LOGIN\n");
1.60 frystyk 1543: status = HTFTPLogin(request, cnet, ctrl);
1544: if (status == HT_WOULD_BLOCK) return HT_OK;
1545: ctrl->state = (status == HT_OK) ? FTP_NEED_DCON : FTP_ERROR;
1546: break;
1547:
1548: case FTP_NEED_DCON:
1.102 frystyk 1549: HTTRACE(PROT_TRACE, "FTP Event... now in state FTP_NEED_DCON\n");
1.60 frystyk 1550: status = HTFTPDataConnection(request, cnet, ctrl, data);
1551: if (status == HT_WOULD_BLOCK) return HT_OK;
1552: if (status == HT_OK)
1553: ctrl->state = (data->type=='N') ?
1554: FTP_NEED_SERVER : FTP_NEED_DATA;
1555: else
1556: ctrl->state = FTP_ERROR;
1557: break;
1558:
1559: case FTP_NEED_DATA:
1.102 frystyk 1560: HTTRACE(PROT_TRACE, "FTP Event... now in state FTP_NEED_DATA\n");
1.63 frystyk 1561: status = HTFTPGetData(request, cnet, soc, ctrl, data);
1.60 frystyk 1562: if (status == HT_WOULD_BLOCK) return HT_OK;
1563: if (status == HT_LOADED)
1564: ctrl->state = FTP_SUCCESS;
1565: else if (status == HT_OK)
1566: ctrl->state = FTP_NEED_DCON;
1.76 frystyk 1567: else if (!FTP_DIR(data) && !data->stream_error) {
1.60 frystyk 1568: FTPListType(data, ctrl->server);
1569: ctrl->state = FTP_NEED_SERVER; /* Try a dir instead? */
1570: } else
1571: ctrl->state = FTP_ERROR;
1572: break;
1.23 frystyk 1573:
1.60 frystyk 1574: case FTP_NEED_SERVER:
1.102 frystyk 1575: HTTRACE(PROT_TRACE, "FTP Event... now in state FTP_NEED_SERVER\n");
1.60 frystyk 1576: status = HTFTPServerInfo(request, cnet, ctrl, data);
1.95 frystyk 1577: if (status == HT_WOULD_BLOCK) return HT_OK;
1.60 frystyk 1578: ctrl->state = FTP_NEED_DATA;
1579: break;
1580:
1581: case FTP_SUCCESS:
1.102 frystyk 1582: HTTRACE(PROT_TRACE, "FTP Event... now in state FTP_SUCCESS\n");
1.95 frystyk 1583: FTPCleanup(request, HT_LOADED);
1.60 frystyk 1584: return HT_OK;
1585: break;
1586:
1587: case FTP_ERROR:
1.102 frystyk 1588: HTTRACE(PROT_TRACE, "FTP Event... now in state FTP_ERROR\n");
1.95 frystyk 1589: FTPCleanup(request, HT_ERROR);
1.60 frystyk 1590: return HT_OK;
1591: break;
1.23 frystyk 1592: }
1.60 frystyk 1593: } /* End of while(1) */
1.23 frystyk 1594: }
1.22 frystyk 1595:
1.95 frystyk 1596: PUBLIC void HTFTP_setTransferMode(FTPTransferMode mode)
1597: {
1598: g_FTPTransferMode = mode;
1599: }
1600:
1601: PUBLIC FTPTransferMode HTFTP_transferMode (void)
1602: {
1603: return g_FTPTransferMode;
1.97 frystyk 1604: }
1605:
1606: PUBLIC void HTFTP_setControlMode (FTPControlMode mode)
1607: {
1608: g_FTPControlMode = mode;
1609: }
1610:
1611: PUBLIC FTPControlMode HTFTP_controlMode (void)
1612: {
1613: return g_FTPControlMode;
1.95 frystyk 1614: }
Webmaster