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