Annotation of libwww/Library/src/HTFTP.c, revision 1.108
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.108 ! kahan 6: ** @(#) $Id: HTFTP.c,v 1.107 2000/02/07 11:19:34 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.73 frystyk 390: HTChunk_clear(ctrl->cmd);
391: HTChunk_ensure(ctrl->cmd, len);
1.60 frystyk 392: if (pars && *pars)
1.73 frystyk 393: sprintf(HTChunk_data(ctrl->cmd), "%s %s%c%c", token, pars, CR, LF);
1.60 frystyk 394: else
1.73 frystyk 395: sprintf(HTChunk_data(ctrl->cmd), "%s%c%c", token, CR, LF);
1.102 frystyk 396: HTTRACE(PROT_TRACE, "FTP Tx...... %s" _ HTChunk_data(ctrl->cmd));
1.84 frystyk 397: return (*input->isa->put_block)(input, HTChunk_data(ctrl->cmd), len);
1.60 frystyk 398: }
399:
400: /* HTFTPParseURL
401: ** -------------
402: ** Scan URL for uid and passwd, and any data type indication. The
403: ** expected format is [user[:password]@]host[:port].
404: ** If no values are found then use defaults.
405: ** Returns YES if OK, else NO
1.22 frystyk 406: */
1.83 frystyk 407: PRIVATE BOOL HTFTPParseURL (HTRequest * request,
408: char *url, ftp_ctrl *ctrl, ftp_data *data)
1.22 frystyk 409: {
1.60 frystyk 410: char *login = HTParse(url, "", PARSE_HOST);
411: char *path = HTParse(url, "", PARSE_PATH+PARSE_PUNCTUATION);
412: char *ptr = strchr(login, '@');
413: if (ptr) { /* Uid and/or passwd specified */
414: char *passwd;
415: *ptr = '\0';
416: if ((passwd = strchr(login, ':'))) { /* Passwd specified */
417: *passwd++ = '\0';
418: HTUnEscape(passwd);
419: StrAllocCopy(ctrl->passwd, passwd);
1.22 frystyk 420: }
1.60 frystyk 421: HTUnEscape(login);
422: StrAllocCopy(ctrl->uid, login);
1.97 frystyk 423: } else if (g_FTPControlMode & FTP_ALWAYS_ASK_UID_PW) {
424: ctrl->uid=NULL;
425: ctrl->passwd=NULL;
426: } else { /* Use anonymous */
1.83 frystyk 427: HTUserProfile * up = HTRequest_userProfile(request);
1.97 frystyk 428: const char * mailaddress = HTUserProfile_email(up);
1.60 frystyk 429: StrAllocCopy(ctrl->uid, "anonymous");
430: if (mailaddress)
431: StrAllocCopy(ctrl->passwd, mailaddress);
432: else
433: StrAllocCopy(ctrl->passwd, WWW_FTP_CLIENT);
1.22 frystyk 434: }
1.102 frystyk 435: HTTRACE(PROT_TRACE, "FTPParse.... uid `%s\' pw `%s\'\n" _
1.104 frystyk 436: ctrl->uid ? ctrl->uid : "<null>" _
437: ctrl->passwd ? ctrl->passwd : "<null>");
1.60 frystyk 438:
1.104 frystyk 439: /*
440: ** Look for any type in the URI. If not 'type' parameter then look for
441: ** trailing slash.
442: */
443: if ((ptr = strchr(path, ';')) != NULL) {
1.60 frystyk 444: *ptr = '\0';
445: if (strncasecomp(ptr, ";type=", 6)) /* Look for type */
446: data->type = TOUPPER(*(ptr+6));
447: else if (*(ptr-1) == '/')
1.104 frystyk 448: data->type = 'D';
1.60 frystyk 449: } else if (*(path+strlen(path)-1) == '/') {
450: *(path+strlen(path)-1) = '\0';
1.104 frystyk 451: data->type = 'D';
1.60 frystyk 452: }
1.102 frystyk 453: HTTRACE(PROT_TRACE, "FTPParse.... Datatype %c\n" _ data->type ? data->type : '?');
1.60 frystyk 454: StrAllocCopy(data->file, path);
455: data->offset = data->file;
1.77 frystyk 456: HT_FREE(login);
457: HT_FREE(path);
1.60 frystyk 458: return YES;
1.22 frystyk 459: }
460:
1.60 frystyk 461: /* Use LIST or NLST
462: ** ----------------
463: ** This function sets the type field for what type of list we can use
464: ** Returns YES if OK, else NO
465: */
1.63 frystyk 466: PRIVATE BOOL FTPListType (ftp_data * data, FTPServerType type)
1.60 frystyk 467: {
468: if (!data) return NO;
469: switch (type) {
470: case FTP_GENERIC: data->type='N'; break;
471: case FTP_MACHTEN: data->type='L'; break;
472: case FTP_UNIX: data->type='L'; break;
473: case FTP_VMS: data->type='L'; break;
474: case FTP_CMS: data->type='N'; break;
475: case FTP_DCTS: data->type='N'; break;
476: case FTP_TCPC: data->type='N'; break;
477: case FTP_PETER_LEWIS: data->type='L'; break;
478: case FTP_NCSA: data->type='N'; break;
479: case FTP_WINNT: data->type='L'; break;
480: default: data->type='N'; break;
481: }
482: return YES;
483: }
484:
485: /* Open a Data socket for listening on
486: ** -----------------------------------
487: ** Set up a port to listen for data
488: ** Returns YES if OK, else NO
1.22 frystyk 489: */
1.104 frystyk 490: PRIVATE BOOL AcceptDataSocket (HTNet *cnet, HTNet *dnet, ftp_data *data)
1.22 frystyk 491: {
1.104 frystyk 492: if (HTHost_listen(NULL, dnet, "ftp://localhost:0") == HT_ERROR)
493: return NO;
1.22 frystyk 494:
1.104 frystyk 495: /*
496: ** Now we must find out who we are to tell the other guy
497: ** We have to get the local IP interface from the control connection as
498: ** this is not yet set in the unaccepted data socket
499: */
1.60 frystyk 500: {
1.104 frystyk 501: SockA local_port, local_host;
502: int addr_size = sizeof(local_port);
503: memset((void *) &local_host, '\0', addr_size);
504: memset((void *) &local_port, '\0', addr_size);
505: if (getsockname(HTNet_socket(cnet),
506: (struct sockaddr *) &local_host, &addr_size) < 0 ||
507: getsockname(HTNet_socket(dnet),
508: (struct sockaddr *) &local_port, &addr_size) < 0) {
1.93 frystyk 509: HTRequest_addSystemError(HTNet_request(dnet), ERR_FATAL, socerrno,
1.68 frystyk 510: NO, "getsockname");
1.60 frystyk 511: return NO;
1.22 frystyk 512: }
1.104 frystyk 513: HTTRACE(PROT_TRACE, "FTP......... This host is `%s\'\n" _ HTInetString(&local_host));
1.60 frystyk 514: {
1.104 frystyk 515: u_long addr = local_host.sin_addr.s_addr;
516: u_short port = local_port.sin_port;
1.60 frystyk 517: sprintf(data->host, "%d,%d,%d,%d,%d,%d",
518: (int)*((unsigned char *)(&addr)+0),
519: (int)*((unsigned char *)(&addr)+1),
520: (int)*((unsigned char *)(&addr)+2),
521: (int)*((unsigned char *)(&addr)+3),
522: (int)*((unsigned char *)(&port)+0),
523: (int)*((unsigned char *)(&port)+1));
1.22 frystyk 524: }
525: }
1.60 frystyk 526: return YES;
1.22 frystyk 527: }
528:
1.60 frystyk 529: /* HTFTPLogin
530: ** -----------
531: ** This function makes a login to a ftp-server. It takes the user name
532: ** and passwd specified in ctrl->user and if that fails or an additional
533: ** account is needed, the user is prompted.
534: ** Returns HT_OK, HT_ERROR, or HT_WOULD_BLOCK
1.22 frystyk 535: */
1.60 frystyk 536: PRIVATE int HTFTPLogin (HTRequest *request, HTNet *cnet, ftp_ctrl *ctrl)
1.22 frystyk 537: {
1.60 frystyk 538: int status;
539: typedef enum _state {
540: SUB_ERROR = -2,
541: SUB_SUCCESS = -1,
542: NEED_SELECT = 0,
543: NEED_GREETING,
544: NEED_REIN,
545: NEED_UID,
546: NEED_PASSWD,
547: NEED_ACCOUNT,
548: PROMPT_USER
549: } state;
550:
551: /* Jump into a second level state machine */
552: while (1) {
553: switch ((state) ctrl->substate) {
1.99 frystyk 554: case NEED_SELECT:
555: {
556: HTAlertCallback * cbf = HTAlert_find(HT_PROG_LOGIN);
557: if (cbf) (*cbf)(request, HT_PROG_LOGIN, HT_MSG_NULL, NULL, NULL, NULL);
1.104 frystyk 558: HTTRACE(PROT_TRACE, "FTP Login... now in state NEED_SELECT\n");
1.99 frystyk 559: ctrl->substate = ctrl->reset ? NEED_REIN : NEED_GREETING;
560: }
1.60 frystyk 561: break;
562:
563: case NEED_GREETING:
1.104 frystyk 564: HTTRACE(PROT_TRACE, "FTP Login... now in state NEED_GREETING\n");
1.93 frystyk 565: status = HTHost_read(HTNet_host(cnet), cnet);
1.60 frystyk 566: if (status == HT_WOULD_BLOCK)
567: return HT_WOULD_BLOCK;
568: else if (status == HT_LOADED) {
1.66 frystyk 569: if (ctrl->repcode/100 == 2) {
570: ctrl->substate = (ctrl->uid && *ctrl->uid) ?
571: NEED_UID : PROMPT_USER;
1.95 frystyk 572: } else {
1.60 frystyk 573: ctrl->substate = SUB_ERROR;
1.95 frystyk 574: }
575: } else {
1.60 frystyk 576: ctrl->substate = SUB_ERROR;
1.95 frystyk 577: }
1.60 frystyk 578: break;
579:
580: case NEED_REIN:
1.104 frystyk 581: HTTRACE(PROT_TRACE, "FTP Login... now in state NEED_REIN\n");
1.60 frystyk 582: if (!ctrl->sent) {
583: status = SendCommand(request, ctrl, "REIN", NULL);
584: if (status == HT_WOULD_BLOCK)
585: return HT_WOULD_BLOCK;
586: else if (status == HT_ERROR)
587: ctrl->substate = SUB_ERROR;
588: ctrl->sent = YES;
589: } else {
1.93 frystyk 590: status = HTHost_read(HTNet_host(cnet), cnet);
1.60 frystyk 591: if (status == HT_WOULD_BLOCK)
592: return HT_WOULD_BLOCK;
593: else if (status == HT_LOADED) {
1.104 frystyk 594: /*
595: ** If the FTP server doesn't support the REIN command, then
596: ** the return code will be 502
597: */
1.95 frystyk 598: if ((ctrl->repcode/100 == 2) || (ctrl->repcode == 502)) {
1.66 frystyk 599: ctrl->substate = (ctrl->uid && *ctrl->uid) ?
600: NEED_UID : PROMPT_USER;
1.95 frystyk 601: } else {
1.60 frystyk 602: ctrl->substate = SUB_SUCCESS; /* hope the best */
1.95 frystyk 603: }
604: } else {
1.60 frystyk 605: ctrl->substate = SUB_ERROR;
1.95 frystyk 606: }
1.60 frystyk 607: ctrl->sent = NO;
1.22 frystyk 608: }
1.60 frystyk 609: break;
610:
611: case NEED_UID:
1.104 frystyk 612: HTTRACE(PROT_TRACE, "FTP Login... now in state NEED_UID\n");
1.60 frystyk 613: if (!ctrl->sent) {
614: status = SendCommand(request, ctrl, "USER", ctrl->uid);
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;
1.22 frystyk 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) {
625: int code = ctrl->repcode/100;
626: if (code == 2) /* Logged in w/o passwd! */
627: ctrl->substate = SUB_SUCCESS;
1.66 frystyk 628: else if (code == 3) { /* Password demanded */
629: ctrl->substate = (ctrl->passwd && *ctrl->passwd) ?
630: NEED_PASSWD : PROMPT_USER;
1.95 frystyk 631: /* begin _GM_ */
632: /* Note: libwww bug ID: GM3 */
633: /* } else if (ctrl->repcode == 530) */
634: /* ctrl->substate = PROMPT_USER;*/ /* User unknown */
635: } else if (ctrl->repcode == 530) {
636: if (ctrl->alreadyLoggedIn == YES) {
637: ctrl->substate = SUB_SUCCESS;
1.104 frystyk 638: HTTRACE(PROT_TRACE, "FTP Login... Already logged in\n");
1.95 frystyk 639: } else {
640: ctrl->substate = PROMPT_USER;
1.104 frystyk 641: HTTRACE(PROT_TRACE, "FTP Login... User Unknown\n");
1.95 frystyk 642: }
643: }
644: /* end _GM_ */
1.60 frystyk 645: else
646: ctrl->substate = SUB_ERROR;
647: } else
648: ctrl->substate = SUB_ERROR;
649: ctrl->sent = NO;
1.22 frystyk 650: }
1.60 frystyk 651: break;
1.1 timbl 652:
1.60 frystyk 653: case NEED_PASSWD:
1.104 frystyk 654: HTTRACE(PROT_TRACE, "FTP Login... now in state NEED_PASSWD\n");
1.60 frystyk 655: if (!ctrl->sent) {
656: status = SendCommand(request, ctrl, "PASS", ctrl->passwd);
657: if (status == HT_WOULD_BLOCK)
658: return HT_WOULD_BLOCK;
659: else if (status == HT_ERROR)
660: ctrl->substate = SUB_ERROR;
661: ctrl->sent = YES;
662: } else {
1.93 frystyk 663: status = HTHost_read(HTNet_host(cnet), cnet);
1.60 frystyk 664: if (status == HT_WOULD_BLOCK)
665: return HT_WOULD_BLOCK;
666: else if (status == HT_LOADED) {
667: int code = ctrl->repcode/100;
668: if (code == 2) /* Logged in with passwd */
669: ctrl->substate = SUB_SUCCESS;
1.70 frystyk 670: else if (code == 3) { /* Account required */
671: HTAlertCallback *cbf = HTAlert_find(HT_A_PROMPT);
672: HTAlertPar * reply = HTAlert_newReply();
673: if (cbf && (*cbf)(request, HT_A_PROMPT,
674: HT_MSG_ACCOUNT, NULL, NULL, reply)) {
675: ctrl->account = HTAlert_replyMessage(reply);
1.60 frystyk 676: ctrl->substate = NEED_ACCOUNT;
1.70 frystyk 677: } else
1.60 frystyk 678: ctrl->substate = SUB_ERROR;
1.70 frystyk 679: HTAlert_deleteReply(reply);
1.60 frystyk 680: } else if (ctrl->repcode == 530)
681: ctrl->substate = PROMPT_USER;
682: else
683: ctrl->substate = SUB_ERROR;
684: } else
685: ctrl->substate = SUB_ERROR;
686: ctrl->sent = NO;
687: }
688: break;
1.1 timbl 689:
1.60 frystyk 690: case NEED_ACCOUNT:
1.104 frystyk 691: HTTRACE(PROT_TRACE, "FTP Login... now in state NEED_ACCOUNT\n");
1.60 frystyk 692: if (!ctrl->sent) {
693: status = SendCommand(request, ctrl, "ACCT", ctrl->account);
694: if (status == HT_WOULD_BLOCK)
695: return HT_WOULD_BLOCK;
696: else if (status == HT_ERROR)
697: ctrl->substate = SUB_ERROR;
698: ctrl->sent = YES;
1.23 frystyk 699: } else {
1.93 frystyk 700: status = HTHost_read(HTNet_host(cnet), cnet);
1.60 frystyk 701: if (status == HT_WOULD_BLOCK)
702: return HT_WOULD_BLOCK;
703: else if (status == HT_LOADED) {
704: int code = ctrl->repcode/100;
705: if (code == 2) /* Logged in with account */
706: ctrl->substate = SUB_SUCCESS;
707: else
708: ctrl->substate = SUB_ERROR; /* hopeless */
709: } else
710: ctrl->substate = SUB_ERROR;
711: ctrl->sent = NO;
1.1 timbl 712: }
1.60 frystyk 713: break;
1.1 timbl 714:
1.60 frystyk 715: case PROMPT_USER:
1.104 frystyk 716: HTTRACE(PROT_TRACE, "FTP Login... now in state PROMPT_USER\n");
1.60 frystyk 717: {
1.70 frystyk 718: HTAlertCallback *cbf = HTAlert_find(HT_A_USER_PW);
719: HTAlertPar * reply = HTAlert_newReply();
1.77 frystyk 720: HT_FREE(ctrl->uid);
721: HT_FREE(ctrl->passwd);
1.87 frystyk 722: if (cbf && (*cbf)(request, HT_A_USER_PW, HT_MSG_FTP_UID,
723: NULL, NULL, reply)){
1.70 frystyk 724: ctrl->uid = HTAlert_replyMessage(reply);
725: ctrl->passwd = HTAlert_replySecret(reply);
1.108 ! kahan 726: }
! 727: else {
! 728: ctrl->uid = NULL;
! 729: ctrl->passwd = NULL;
1.70 frystyk 730: }
731: HTAlert_deleteReply(reply);
1.66 frystyk 732: if (ctrl->uid && *ctrl->uid && ctrl->passwd && *ctrl->passwd)
1.60 frystyk 733: ctrl->substate = NEED_UID;
734: else
735: ctrl->substate = SUB_ERROR;
736: }
737: break;
1.1 timbl 738:
1.60 frystyk 739: case SUB_ERROR:
1.104 frystyk 740: HTTRACE(PROT_TRACE, "FTP Login... now in state SUB_ERROR\n");
1.95 frystyk 741: HTRequest_addError(request, ERR_FATAL, NO,
742: HTERR_FTP_LOGIN_FAILURE, NULL, 0, "HTFTPLogin");
1.102 frystyk 743: HTTRACE(PROT_TRACE, "FTP......... Login failed\n");
1.60 frystyk 744: ctrl->substate = 0;
745: return HT_ERROR;
746: break;
1.23 frystyk 747:
1.60 frystyk 748: case SUB_SUCCESS:
1.104 frystyk 749: HTTRACE(PROT_TRACE, "FTP Login... now in state SUB_SUCCESS\n");
1.102 frystyk 750: HTTRACE(PROT_TRACE, "FTP......... Logged in as `%s\'\n" _ ctrl->uid);
1.60 frystyk 751: ctrl->substate = 0;
752: return HT_OK;
753: break;
1.23 frystyk 754: }
1.22 frystyk 755: }
756: }
757:
1.60 frystyk 758: /* HTFTPDataConnection
759: ** -------------------
760: ** Prepares a data connection to the server and initializes the
761: ** transfer mode.
762: ** Returns HT_OK, HT_ERROR, or HT_WOULD_BLOCK
1.1 timbl 763: */
1.60 frystyk 764: PRIVATE int HTFTPDataConnection (HTRequest * request, HTNet *cnet,
765: ftp_ctrl *ctrl, ftp_data *data)
1.1 timbl 766: {
1.60 frystyk 767: int status;
768: HTNet *dnet = ctrl->dnet;
769: typedef enum _state {
770: SUB_ERROR = -2,
771: SUB_SUCCESS = -1,
772: NEED_TYPE = 0,
773: NEED_SELECT,
774: NEED_PASV,
775: NEED_PORT
776: } state;
777:
778: /* Jump into a second level state machine */
779: while (1) {
780: switch ((state) ctrl->substate) {
781: case NEED_TYPE:
1.102 frystyk 782: HTTRACE(PROT_TRACE, "FTP Data.... now in state NEED_TYPE\n");
1.60 frystyk 783: if(!data->type|| data->pasv || data->type=='N' || data->type=='L'){
784: ctrl->substate = NEED_SELECT;
785: break;
786: }
787: if (!ctrl->sent) {
788: char type[2];
789: *type = data->type;
790: *(type+1) = '\0';
791: status = SendCommand(request, ctrl, "TYPE", type);
792: if (status == HT_WOULD_BLOCK)
793: return HT_WOULD_BLOCK;
794: else if (status == HT_ERROR)
795: ctrl->substate = SUB_ERROR;
796: ctrl->sent = YES;
797: } else {
1.93 frystyk 798: status = HTHost_read(HTNet_host(cnet), cnet);
1.60 frystyk 799: if (status == HT_WOULD_BLOCK)
800: return HT_WOULD_BLOCK;
801: else if (status == HT_LOADED) {
802: if (ctrl->repcode/100 == 2)
803: ctrl->substate = NEED_SELECT;
804: else
805: ctrl->substate = SUB_ERROR;
806: } else
807: ctrl->substate = SUB_ERROR;
808: ctrl->sent = NO;
809: }
810: break;
811:
812: case NEED_SELECT:
1.102 frystyk 813: HTTRACE(PROT_TRACE, "FTP Data.... now in state NEED_SELECT\n");
1.60 frystyk 814: if (FTPMode & FTP_DATA_PASV && !data->pasv)
815: ctrl->substate = NEED_PASV;
1.104 frystyk 816: else if (AcceptDataSocket(cnet, dnet, data))
1.60 frystyk 817: ctrl->substate = NEED_PORT;
818: else
819: ctrl->substate = SUB_ERROR;
820: break;
821:
822: case NEED_PASV:
1.102 frystyk 823: HTTRACE(PROT_TRACE, "FTP Data.... now in state NEED_PASV\n");
1.60 frystyk 824: if (!ctrl->sent) {
825: status = SendCommand(request, ctrl, "PASV", NULL);
826: if (status == HT_WOULD_BLOCK)
827: return HT_WOULD_BLOCK;
828: else if (status == HT_ERROR)
829: ctrl->substate = SUB_ERROR;
830: ctrl->sent = YES;
831: } else {
1.93 frystyk 832: status = HTHost_read(HTNet_host(cnet), cnet);
1.60 frystyk 833: if (status == HT_WOULD_BLOCK)
834: return HT_WOULD_BLOCK;
835: else if (status == HT_LOADED) {
836: if (ctrl->repcode == 227) {
837: /*
838: ** If succes, we have to scan for the returned number.
839: ** As the format for the response isn't standard,
840: ** the best thing to do is to scan for the first digit
841: ** after the status code, see RFC1123
842: */
843: char *host = ctrl->reply;
844: int h0, h1, h2, h3, p0=0, p1=0;
1.94 frystyk 845: while (*host && !isdigit((int) *host++));
1.60 frystyk 846: if (!*host || sscanf(--host, "%d,%d,%d,%d,%d,%d",
847: &h0,&h1,&h2,&h3,&p0,&p1) < 6) {
1.102 frystyk 848: HTTRACE(PROT_TRACE, "FTP Data.... PASV No addr\n");
1.60 frystyk 849: ctrl->substate = SUB_ERROR;
850: break;
851: } else {
852: int port = (p0<<8)+p1;
853: sprintf(data->host, "ftp://%d.%d.%d.%d:%d/",
854: h0, h1, h2, h3, port);
855: data->pasv = YES;
856: ctrl->substate = SUB_SUCCESS;
857: }
858: } else {
1.104 frystyk 859: ctrl->substate = AcceptDataSocket(cnet, dnet, data) ?
1.60 frystyk 860: NEED_PORT : SUB_ERROR;
861: }
862: } else
863: ctrl->substate = SUB_ERROR;
864: ctrl->sent = NO;
865: }
866: break;
1.22 frystyk 867:
1.60 frystyk 868: case NEED_PORT:
1.102 frystyk 869: HTTRACE(PROT_TRACE, "FTP Data.... now in state NEED_PORT\n");
1.60 frystyk 870: if (!ctrl->sent) {
871: status = SendCommand(request, ctrl, "PORT", data->host);
872: if (status == HT_WOULD_BLOCK)
873: return HT_WOULD_BLOCK;
874: else if (status == HT_ERROR)
875: ctrl->substate = SUB_ERROR;
876: ctrl->sent = YES;
877: } else {
1.93 frystyk 878: status = HTHost_read(HTNet_host(cnet), cnet);
1.60 frystyk 879: if (status == HT_WOULD_BLOCK)
880: return HT_WOULD_BLOCK;
881: else if (status == HT_LOADED) {
882: data->pasv = NO;
883: ctrl->substate = (ctrl->repcode/100 == 2) ?
884: SUB_SUCCESS : SUB_ERROR;
885: } else
886: ctrl->substate = SUB_ERROR;
887: ctrl->sent = NO;
888: }
889: break;
1.1 timbl 890:
1.60 frystyk 891: case SUB_ERROR:
1.102 frystyk 892: HTTRACE(PROT_TRACE, "FTP Data.... now in state SUB_ERROR\n");
893: HTTRACE(PROT_TRACE, "FTP Data.... Can't setup data connection\n");
1.60 frystyk 894: ctrl->substate = 0;
895: return HT_ERROR;
896: break;
1.1 timbl 897:
1.60 frystyk 898: case SUB_SUCCESS:
1.102 frystyk 899: HTTRACE(PROT_TRACE, "FTP Data.... now in state SUB_SUCCESS\n");
900: HTTRACE(PROT_TRACE, "FTP Data.... Data connection negotiated\n");
1.60 frystyk 901: ctrl->substate = 0;
902: return HT_OK;
903: break;
1.23 frystyk 904: }
1.58 frystyk 905: }
1.23 frystyk 906: }
1.1 timbl 907:
908:
1.60 frystyk 909: /* HTFTPServerInfo
910: ** ---------------
911: ** This function finds out what server we are talking to.
912: ** Maybe we can upgrade from NLST to LIST.
913: ** Returns HT_OK, HT_ERROR, or HT_WOULD_BLOCK
914: ** Thanks to James.W.Matthews@Dartmouth.EDU (James W. Matthews) for making
915: ** his code available.
1.1 timbl 916: */
1.60 frystyk 917: PRIVATE int HTFTPServerInfo (HTRequest *request, HTNet *cnet,
918: ftp_ctrl *ctrl, ftp_data *data)
1.23 frystyk 919: {
1.60 frystyk 920: int status;
921: typedef enum _state {
922: SUB_ERROR = -2,
923: SUB_SUCCESS = -1,
924: NEED_SYST = 0,
925: CHECK_SYST,
926: NEED_PWD,
927: CHECK_PWD
928: } state;
929:
930: /* Jump into a second level state machine */
931: while (1) {
932: switch ((state) ctrl->substate) {
933: case NEED_SYST:
1.102 frystyk 934: HTTRACE(PROT_TRACE, "FTP Server.. now in state NEED_SYST\n");
1.60 frystyk 935: if (!ctrl->sent) {
1.63 frystyk 936: if (ctrl->server != FTP_UNSURE) {
1.60 frystyk 937: FTPListType(data, ctrl->server);
938: return HT_OK;
939: }
940: status = SendCommand(request, ctrl, "SYST", NULL);
941: if (status == HT_WOULD_BLOCK)
942: return HT_WOULD_BLOCK;
943: else if (status == HT_ERROR)
944: ctrl->substate = SUB_ERROR;
945: ctrl->sent = YES;
946: } else {
1.93 frystyk 947: status = HTHost_read(HTNet_host(cnet), cnet);
1.60 frystyk 948: if (status == HT_WOULD_BLOCK)
949: return HT_WOULD_BLOCK;
950: else if (status == HT_LOADED) {
951: ctrl->substate=ctrl->repcode==215 ? CHECK_SYST : NEED_PWD;
952: } else
953: ctrl->substate = SUB_ERROR;
954: ctrl->sent = NO;
1.23 frystyk 955: }
956: break;
957:
1.60 frystyk 958: case CHECK_SYST:
1.102 frystyk 959: HTTRACE(PROT_TRACE, "FTP Server.. now in state CHECK_SYST\n");
1.23 frystyk 960: {
1.60 frystyk 961: char *reply = ctrl->reply;
962: if (!*reply) {
1.102 frystyk 963: HTTRACE(PROT_TRACE, "FTP Server.. No server info?\n");
1.60 frystyk 964: ctrl->substate = NEED_PWD;
1.23 frystyk 965: break;
966: }
1.60 frystyk 967: if (strncmp(reply, "UNIX Type: L8MAC-OSMachTen", 28) == 0) {
968: ctrl->server = FTP_MACHTEN;
969: } else if (strstr(reply, "UNIX") != NULL) {
970: ctrl->server = FTP_UNIX;
971: } else if (strncmp(reply, "VMS", 3) == 0) {
972: ctrl->server = FTP_VMS;
973: } else if ((strncmp(reply, "VM/CMS", 6) == 0) ||
974: (strncmp(reply, "VM", 2) == 0)) {
975: ctrl->server = FTP_CMS;
976: } else if (strncmp(reply, "DCTS", 4) == 0) {
977: ctrl->server = FTP_DCTS;
978: } else if (strstr(reply, "MAC-OS TCP/ConnectII") != NULL) {
1.23 frystyk 979: /* Check old versions of TCP/C using / in pathnames */
1.63 frystyk 980: ctrl->server = FTP_TCPC + FTP_UNSURE;
1.60 frystyk 981: } else if (strncmp(reply, "MACOS Peter's Server", 20) == 0) {
982: ctrl->server = FTP_PETER_LEWIS;
983: } else if (strncmp(reply, "Windows_NT", 10) == 0) {
984: ctrl->server = FTP_WINNT;
1.23 frystyk 985: }
986:
987: /* If we are unsure, try PWD to get more information */
1.60 frystyk 988: if (ctrl->server & FTP_UNSURE)
989: ctrl->substate = NEED_PWD;
1.23 frystyk 990: else
1.60 frystyk 991: ctrl->substate = SUB_SUCCESS;
1.1 timbl 992: }
1.23 frystyk 993: break;
994:
995: case NEED_PWD:
1.102 frystyk 996: HTTRACE(PROT_TRACE, "FTP Server.. now in state NEED_PWD\n");
1.60 frystyk 997: if (!ctrl->sent) {
998: status = SendCommand(request, ctrl, "PWD", NULL);
999: if (status == HT_WOULD_BLOCK)
1000: return HT_WOULD_BLOCK;
1001: else if (status == HT_ERROR)
1002: ctrl->substate = SUB_ERROR;
1003: ctrl->sent = YES;
1.23 frystyk 1004: } else {
1.93 frystyk 1005: status = HTHost_read(HTNet_host(cnet), cnet);
1.60 frystyk 1006: if (status == HT_WOULD_BLOCK)
1007: return HT_WOULD_BLOCK;
1008: else if (status == HT_LOADED) {
1009: ctrl->substate = (ctrl->repcode/100 == 2) ?
1010: CHECK_PWD : SUB_ERROR;
1011: } else
1012: ctrl->substate = SUB_ERROR;
1013: ctrl->sent = NO;
1.23 frystyk 1014: }
1015: break;
1016:
1.60 frystyk 1017: case CHECK_PWD:
1.102 frystyk 1018: HTTRACE(PROT_TRACE, "FTP Server.. now in state CHECK_PWD\n");
1.60 frystyk 1019: {
1020: char *start = strchr(ctrl->reply, '"');
1021: char *end;
1022: if (!start || (end = strchr(++start, '"')) == NULL) {
1.102 frystyk 1023: HTTRACE(PROT_TRACE, "FTP Server.. No current directory?\n");
1.60 frystyk 1024: ctrl->server = FTP_GENERIC;
1.23 frystyk 1025: } else {
1.60 frystyk 1026: *end = '\0';
1.63 frystyk 1027: if (ctrl->server & FTP_TCPC) {
1.60 frystyk 1028: ctrl->server = *start == '/' ? FTP_NCSA : FTP_TCPC;
1029: } else if (*start == '/') {
1030: /* path names starting with / imply Unix, right? */
1031: ctrl->server = FTP_UNIX;
1032: } else if (*(end-1) == ']') {
1033: /* path names ending with ] imply VMS, right? */
1034: ctrl->server = FTP_VMS;
1035: } else
1036: ctrl->server = FTP_GENERIC;
1.23 frystyk 1037: }
1.60 frystyk 1038: ctrl->substate = SUB_SUCCESS;
1.1 timbl 1039: }
1.23 frystyk 1040: break;
1.1 timbl 1041:
1.60 frystyk 1042: case SUB_ERROR:
1.102 frystyk 1043: HTTRACE(PROT_TRACE, "FTP Server.. now in state SUB_ERROR\n");
1044: HTTRACE(PROT_TRACE, "FTP Server.. Can't get server information\n");
1.60 frystyk 1045: ctrl->substate = 0;
1.63 frystyk 1046: ctrl->server = FTP_GENERIC;
1.60 frystyk 1047: return HT_ERROR;
1.23 frystyk 1048: break;
1.22 frystyk 1049:
1.60 frystyk 1050: case SUB_SUCCESS:
1.102 frystyk 1051: HTTRACE(PROT_TRACE, "FTP Server.. now in state SUB_SUCCESS\n");
1.95 frystyk 1052: {
1.81 frystyk 1053: HTHost * host = HTNet_host(cnet);
1.102 frystyk 1054: HTTRACE(PROT_TRACE, "FTP Server.. Guessed type %d\n" _ ctrl->server);
1.81 frystyk 1055: HTHost_setVersion(host, ctrl->server);
1056: FTPListType(data, ctrl->server);
1057: ctrl->substate = 0;
1058: return HT_OK;
1059: break;
1.95 frystyk 1060: }
1.22 frystyk 1061: }
1.23 frystyk 1062: }
1063: }
1064:
1.60 frystyk 1065: /* HTFTPGetData
1066: ** ------------
1067: ** This function asks for the file or a directory. First we try in one go,
1.23 frystyk 1068: ** but if that doesn't work, then we use CWD for each segment and then
1069: ** try to retrieve it. If that also fails, then we try if it is a
1.60 frystyk 1070: ** directory.
1071: ** Returns HT_OK, HT_LOADED, HT_ERROR, or HT_WOULD_BLOCK
1.23 frystyk 1072: */
1.71 frystyk 1073: PRIVATE int HTFTPGetData (HTRequest *request, HTNet *cnet, SOCKET sockfd,
1.60 frystyk 1074: ftp_ctrl *ctrl, ftp_data *data)
1.23 frystyk 1075: {
1076: int status;
1.60 frystyk 1077: char *segment = NULL;
1078: HTNet *dnet = ctrl->dnet;
1.93 frystyk 1079: BOOL data_is_active = (sockfd == HTNet_socket(dnet));
1.107 kahan 1080: HTPostCallback *pcbf;
1.60 frystyk 1081: typedef enum _state {
1082: SUB_ERROR = -2,
1083: SUB_SUCCESS = -1,
1084: NEED_SELECT = 0,
1085: NEED_CONNECT,
1086: NEED_ACCEPT,
1087: NEED_ACTION,
1088: NEED_CWD,
1089: NEED_SEGMENT,
1090: NEED_STREAM,
1.63 frystyk 1091: NEED_BODY
1.60 frystyk 1092: } state;
1093:
1094: /* Jump into a second level state machine */
1095: while (1) {
1096: switch ((state) ctrl->substate) {
1097: case NEED_SELECT:
1.102 frystyk 1098: HTTRACE(PROT_TRACE, "FTP Get Data now in state NEED_SELECT\n");
1.60 frystyk 1099: ctrl->substate = data->pasv ? NEED_CONNECT : NEED_ACTION;
1100: break;
1101:
1102: case NEED_CONNECT:
1.102 frystyk 1103: HTTRACE(PROT_TRACE, "FTP Get Data now in state NEED_CONNECT\n");
1.104 frystyk 1104: status = HTHost_connect(HTNet_host(dnet), dnet, data->host);
1.60 frystyk 1105: if (status == HT_WOULD_BLOCK)
1106: return HT_WOULD_BLOCK;
1107: else if (status == HT_OK) {
1.104 frystyk 1108: HTTRACE(PROT_TRACE, "FTP Get Data Active data socket %d\n" _
1.95 frystyk 1109: HTNet_socket(dnet));
1.60 frystyk 1110: ctrl->substate = NEED_ACTION;
1.81 frystyk 1111: } else { /* Swap to PORT on the fly */
1.91 frystyk 1112: NETCLOSE(HTNet_socket(dnet));
1113: HTNet_setSocket(dnet, INVSOC);
1.102 frystyk 1114: HTTRACE(PROT_TRACE, "FTP Get Data Swap to PORT on the fly\n");
1.89 frystyk 1115: ctrl->substate = NEED_SELECT;
1.77 frystyk 1116: HT_FREE(segment);
1.60 frystyk 1117: return HT_OK;
1118: }
1.23 frystyk 1119: break;
1120:
1.60 frystyk 1121: case NEED_ACCEPT:
1.102 frystyk 1122: HTTRACE(PROT_TRACE, "FTP Get Data now in state NEED_ACCEPT\n");
1.104 frystyk 1123: status = HTHost_accept(HTNet_host(dnet), dnet, NULL);
1124: if (status == HT_WOULD_BLOCK)
1125: return HT_WOULD_BLOCK;
1126: else if (status == HT_OK) {
1127: HTTRACE(PROT_TRACE, "FTP Get Data Passive data socket %d\n" _
1128: HTNet_socket(dnet));
1129: ctrl->substate = NEED_STREAM;
1130: } else
1131: ctrl->substate = SUB_ERROR;
1.60 frystyk 1132: break;
1.33 frystyk 1133:
1.60 frystyk 1134: case NEED_ACTION:
1.102 frystyk 1135: HTTRACE(PROT_TRACE, "FTP Get Data now in state NEED_ACTION\n");
1.60 frystyk 1136: if (!ctrl->sent) {
1137: char *cmd = (data->type=='L') ? "LIST" :
1138: (data->type=='N') ? "NLST" : "RETR";
1.106 raff 1139: if (HTRequest_method(request) == METHOD_PUT) cmd = "STOR";
1.60 frystyk 1140: StrAllocCopy(segment, data->offset);
1141: HTUnEscape(segment);
1142: HTCleanTelnetString(segment);
1143: status = SendCommand(request, ctrl, cmd, segment);
1.77 frystyk 1144: HT_FREE(segment);
1.60 frystyk 1145: if (status == HT_WOULD_BLOCK)
1146: return HT_WOULD_BLOCK;
1147: else if (status == HT_ERROR)
1148: ctrl->substate = SUB_ERROR;
1149: ctrl->sent = YES;
1150: } else {
1.93 frystyk 1151: status = HTHost_read(HTNet_host(cnet), cnet);
1.60 frystyk 1152: if (status == HT_WOULD_BLOCK)
1153: return HT_WOULD_BLOCK;
1154: else if (status == HT_LOADED) {
1155: int code = ctrl->repcode;
1156: if (code==125 || code==150 || code==225)
1.93 frystyk 1157: ctrl->substate = data->pasv ? NEED_STREAM : NEED_ACCEPT;
1.60 frystyk 1158: else if (code/100==5 && !ctrl->cwd)
1159: ctrl->substate = NEED_SEGMENT;
1.95 frystyk 1160: else {
1161: if (ctrl->repcode == 550) {
1.102 frystyk 1162: HTTRACE(PROT_TRACE, "FTP Get Data no such file or directory\n");
1.95 frystyk 1163: data->stream_error = YES;
1164: }
1165: ctrl->substate = SUB_ERROR;
1166: }
1.60 frystyk 1167: } else
1168: ctrl->substate = SUB_ERROR;
1169: ctrl->sent = NO;
1.23 frystyk 1170: }
1171: break;
1.60 frystyk 1172:
1173: case NEED_SEGMENT:
1.102 frystyk 1174: HTTRACE(PROT_TRACE, "FTP Get Data now in state NEED_SEGMENT\n");
1.23 frystyk 1175: {
1.60 frystyk 1176: char *ptr;
1177: if (data->offset == data->file) {
1.63 frystyk 1178: if (ctrl->server == FTP_VMS) { /* Change to root */
1.77 frystyk 1179: if ((segment = (char *) HT_MALLOC(strlen(ctrl->uid)+3)) == NULL)
1180: HT_OUTOFMEM("segment ");
1.63 frystyk 1181: sprintf(segment, "[%s]", ctrl->uid);
1182: } else
1183: StrAllocCopy(segment, "/");
1.60 frystyk 1184: data->offset++;
1185: ctrl->substate = NEED_CWD;
1186: } else {
1187: if ((ptr = strchr(data->offset, '/'))) {
1188: *ptr='\0';
1189: StrAllocCopy(segment, data->offset);
1190: *ptr='/';
1191: data->offset = ++ptr;
1192: HTUnEscape(segment);
1193: HTCleanTelnetString(segment);
1194: ctrl->substate = NEED_CWD;
1.33 frystyk 1195: } else
1.60 frystyk 1196: ctrl->substate = NEED_ACTION;
1.23 frystyk 1197: }
1198: }
1199: break;
1200:
1.60 frystyk 1201: case NEED_CWD:
1.102 frystyk 1202: HTTRACE(PROT_TRACE, "FTP Get Data now in state NEED_CWD\n");
1.60 frystyk 1203: if (!ctrl->sent) {
1204: status = SendCommand(request, ctrl, "CWD", segment);
1.77 frystyk 1205: HT_FREE(segment);
1.60 frystyk 1206: if (status == HT_WOULD_BLOCK)
1207: return HT_WOULD_BLOCK;
1208: else if (status == HT_ERROR)
1209: ctrl->substate = SUB_ERROR;
1210: ctrl->cwd = YES;
1211: ctrl->sent = YES;
1212: } else {
1.93 frystyk 1213: status = HTHost_read(HTNet_host(cnet), cnet);
1.60 frystyk 1214: if (status == HT_WOULD_BLOCK)
1215: return HT_WOULD_BLOCK;
1216: else if (status == HT_LOADED) {
1217: if (ctrl->repcode/100 == 2)
1218: ctrl->substate = NEED_SEGMENT;
1219: else
1220: ctrl->substate = SUB_ERROR;
1221: } else
1222: ctrl->substate = SUB_ERROR;
1223: ctrl->sent = NO;
1.33 frystyk 1224: }
1.60 frystyk 1225: break;
1.33 frystyk 1226:
1.81 frystyk 1227: case NEED_STREAM:
1.102 frystyk 1228: HTTRACE(PROT_TRACE, "FTP Get Data now in state NEED_STREAM\n");
1.81 frystyk 1229: /*
1230: ** Create the stream pipe FROM the channel to the application.
1231: ** The target for the input stream pipe is set up using the
1232: ** stream stack.
1233: */
1.93 frystyk 1234: {
1235: HTStream * target = FTP_DIR(data) ?
1236: HTFTPDir_new(request, ctrl->server, data->type) :
1237: HTStreamStack(HTAnchor_format(HTRequest_anchor(request)),
1238: HTRequest_outputFormat(request),
1239: HTRequest_outputStream(request),
1240: request, YES);
1241: HTNet_setReadStream(dnet, target);
1.104 frystyk 1242: HTRequest_setOutputConnected(request, YES);
1.81 frystyk 1243: }
1.93 frystyk 1244: data_is_active = YES;
1.81 frystyk 1245: ctrl->substate = NEED_BODY;
1.91 frystyk 1246: break;
1.23 frystyk 1247:
1.60 frystyk 1248: case NEED_BODY:
1.102 frystyk 1249: HTTRACE(PROT_TRACE, "FTP Get Data now in state NEED_BODY\n");
1.93 frystyk 1250: if (data_is_active) {
1.106 raff 1251: if (HTRequest_method(request) == METHOD_PUT) {
1252: HTParentAnchor * entity = HTRequest_entityAnchor(request);
1253: const char * document = (const char *) HTAnchor_document(entity);
1254: int length = (int)HTAnchor_length(entity);
1255: HTStream * output =
1256: (HTStream *)HTChannel_output(HTNet_host(dnet)->channel);
1.107 kahan 1257: pcbf = HTRequest_postCallback(request);
1258: if (pcbf) {
1259: status = (*pcbf)(request, output);
1260: } else {
1261: status = (*output->isa->put_block)(output,
1262: document,
1263: length);
1264: if (status == HT_OK) {
1265: status = HT_LOADED;
1266: }
1267: }
1268:
1.106 raff 1269: if (status == HT_WOULD_BLOCK) {
1270: return HT_WOULD_BLOCK;
1.107 kahan 1271: } else if ( status == HT_LOADED ) {
1272: ctrl->substate = SUB_SUCCESS;
1273: data->complete |= 3;
1.106 raff 1274: } else if ( status == HT_OK ) {
1.107 kahan 1275: return HT_WOULD_BLOCK;
1.106 raff 1276: } else {
1277: ctrl->substate = SUB_ERROR;
1278: data->stream_error = YES;
1279: }
1280: continue;
1281: } else {
1.93 frystyk 1282: status = HTHost_read(HTNet_host(dnet), dnet);
1.106 raff 1283: }
1.93 frystyk 1284: if (status == HT_WOULD_BLOCK)
1285: return HT_WOULD_BLOCK;
1.106 raff 1286: else if (status == HT_LOADED || status == HT_CLOSED || status == HT_OK) {
1.107 kahan 1287: HTDoClose(dnet);
1.93 frystyk 1288: data->complete |= 1;
1289: if (data->complete >= 3)
1290: ctrl->substate = SUB_SUCCESS;
1291: else
1292: data_is_active = NO;
1293: } else {
1294: ctrl->substate = SUB_ERROR;
1295: data->stream_error = YES;
1296: }
1297: } else {
1298: status = HTHost_read(HTNet_host(cnet), cnet);
1299: if (status == HT_WOULD_BLOCK)
1300: return HT_WOULD_BLOCK;
1301: else if (status == HT_LOADED || status == HT_CLOSED) {
1302: if (ctrl->repcode/100 == 2) {
1303: data->complete |= 2;
1304: if (data->complete >= 3)
1305: ctrl->substate = SUB_SUCCESS;
1306: else
1307: data_is_active = YES;
1308: } else
1309: ctrl->substate = SUB_ERROR;
1310: } else
1311: ctrl->substate = SUB_ERROR;
1312: }
1313: break;
1.22 frystyk 1314:
1.60 frystyk 1315: case SUB_ERROR:
1.102 frystyk 1316: HTTRACE(PROT_TRACE, "FTP Get Data now in state SUB_ERROR\n");
1.95 frystyk 1317: HTRequest_addError(request, ERR_FATAL, NO, HTERR_NOT_FOUND,
1318: NULL, 0, "HTFTPGetData");
1.60 frystyk 1319: ctrl->substate = 0;
1.77 frystyk 1320: HT_FREE(segment);
1.60 frystyk 1321: return HT_ERROR;
1.23 frystyk 1322: break;
1323:
1.60 frystyk 1324: case SUB_SUCCESS:
1.102 frystyk 1325: HTTRACE(PROT_TRACE, "FTP Get Data now in state SUB_SUCCESS\n");
1.60 frystyk 1326: ctrl->substate = 0;
1.77 frystyk 1327: HT_FREE(segment);
1.60 frystyk 1328: return HT_LOADED;
1.23 frystyk 1329: break;
1.1 timbl 1330: }
1331: }
1.23 frystyk 1332: }
1.1 timbl 1333:
1.23 frystyk 1334: /* ------------------------------------------------------------------------- */
1335:
1336: /* Retrieve File from Server as an atomic action.
1337: ** -----------------------------------------------
1.58 frystyk 1338: ** Given a hypertext address, this routine loads a document.
1.23 frystyk 1339: **
1340: ** On entry,
1.58 frystyk 1341: ** request This is the request structure
1342: ** returns HT_ERROR Error has occured in call back
1343: ** HT_OK Call back was OK
1344: */
1.91 frystyk 1345: PRIVATE int FTPEvent (SOCKET soc, void * pVoid, HTEventType type);
1346:
1347: PUBLIC int HTLoadFTP (SOCKET soc, HTRequest * request)
1.58 frystyk 1348: {
1.84 frystyk 1349: HTNet * cnet = HTRequest_net(request);
1350: ftp_ctrl * ctrl = NULL;
1351: ftp_data * data = NULL;
1352: HTParentAnchor * anchor = HTRequest_anchor(request);
1353: char * url = HTAnchor_physical(anchor);
1.23 frystyk 1354:
1.58 frystyk 1355: /*
1.60 frystyk 1356: ** Initiate a new FTP ctrl and data structure and bind to request structure
1.58 frystyk 1357: ** This is actually state FTP_BEGIN, but it can't be in the state
1358: ** machine as we need the structure first.
1359: */
1.102 frystyk 1360: HTTRACE(PROT_TRACE, "FTP......... Looking for `%s\'\n" _ url);
1.91 frystyk 1361: if ((ctrl = (ftp_ctrl *) HT_CALLOC(1, sizeof(ftp_ctrl))) == NULL ||
1362: (data = (ftp_data *) HT_CALLOC(1, sizeof(ftp_data))) == NULL)
1363: HT_OUTOFMEM("HTLoadFTP");
1364: ctrl->cmd = HTChunk_new(128);
1365: ctrl->state = FTP_BEGIN;
1366: ctrl->server = FTP_UNSURE;
1367: ctrl->dnet = HTNet_dup(cnet);
1.93 frystyk 1368: ctrl->cnet = cnet;
1.91 frystyk 1369: HTNet_setContext(cnet, ctrl);
1370: HTNet_setEventCallback(cnet, FTPEvent);
1.92 eric 1371: HTNet_setEventParam(cnet, ctrl);
1.98 frystyk 1372: HTNet_setRawBytesCount(ctrl->dnet, YES);
1.97 frystyk 1373:
1.92 eric 1374: /* for now, the dnet comes back to the same place
1375: ** - vestigial from when the callback was from the request object
1376: */
1377: HTNet_setContext(ctrl->dnet, data);
1378: HTNet_setEventCallback(ctrl->dnet, FTPEvent);
1379: HTNet_setEventParam(ctrl->dnet, ctrl);
1.91 frystyk 1380: return FTPEvent(soc, ctrl, HTEvent_BEGIN);
1381: }
1382:
1383: PRIVATE int FTPEvent (SOCKET soc, void * pVoid, HTEventType type)
1384: {
1.93 frystyk 1385: ftp_ctrl * ctrl = (ftp_ctrl *) pVoid;
1386: ftp_data * data = (ftp_data *) HTNet_context(ctrl->dnet);
1.91 frystyk 1387: int status = HT_ERROR;
1.93 frystyk 1388: HTNet * cnet = ctrl->cnet;
1.91 frystyk 1389: HTRequest * request = HTNet_request(cnet);
1390: HTParentAnchor * anchor = HTRequest_anchor(request);
1391: char * url = HTAnchor_physical(anchor);
1392:
1.97 frystyk 1393: HTHost *host = HTNet_host(cnet);
1394:
1395:
1.91 frystyk 1396: if (type == HTEvent_CLOSE) { /* Interrupted */
1.105 frystyk 1397: if (soc == HTNet_socket(cnet) && data->complete<1)
1.98 frystyk 1398: FTPCleanup(request, HT_INTERRUPTED);
1.60 frystyk 1399: else
1.98 frystyk 1400: FTPCleanup(request, HT_LOADED);
1401: return HT_OK;
1402: } else if (type == HTEvent_TIMEOUT) {
1.105 frystyk 1403:
1404: /*
1405: ** Don't time out the control connection if we are actually recieving data
1406: ** on the data connection
1407: */
1408: if (!(soc == HTNet_socket(cnet) && !(data->complete & 1) && HTNet_bytesRead(ctrl->dnet)>0)) {
1409: HTRequest_addError(request, ERR_FATAL, NO, HTERR_TIME_OUT, NULL, 0, "HTLoadHTTP");
1410: FTPCleanup(request, HT_TIMEOUT);
1411: }
1.60 frystyk 1412: return HT_OK;
1.98 frystyk 1413:
1.60 frystyk 1414: } else {
1.93 frystyk 1415: ctrl = (ftp_ctrl *) HTNet_context(cnet); /* Get existing copy */
1416: data = (ftp_data *) HTNet_context(ctrl->dnet);
1.60 frystyk 1417: }
1.58 frystyk 1418:
1419: /* Now jump into the machine. We know the state from the previous run */
1420: while (1) {
1421: switch (ctrl->state) {
1.60 frystyk 1422: case FTP_BEGIN:
1.102 frystyk 1423: HTTRACE(PROT_TRACE, "FTP Event... now in state FTP_BEGIN\n");
1.100 frystyk 1424:
1425: /* Only handle GET requests for now */
1.106 raff 1426: if (HTRequest_method(request) != METHOD_GET &&
1427: HTRequest_method(request) != METHOD_PUT ) {
1428: HTTRACE(PROT_TRACE, "FTP Event... This module only supports the GET or PUT methods\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.104 frystyk 1475: status = HTHost_connect(host, cnet, url);
1.97 frystyk 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.106 raff 1567: else if (HTRequest_method(request) == METHOD_PUT)
1568: ctrl->state = FTP_ERROR;
1.76 frystyk 1569: else if (!FTP_DIR(data) && !data->stream_error) {
1.60 frystyk 1570: FTPListType(data, ctrl->server);
1571: ctrl->state = FTP_NEED_SERVER; /* Try a dir instead? */
1572: } else
1573: ctrl->state = FTP_ERROR;
1574: break;
1.23 frystyk 1575:
1.60 frystyk 1576: case FTP_NEED_SERVER:
1.102 frystyk 1577: HTTRACE(PROT_TRACE, "FTP Event... now in state FTP_NEED_SERVER\n");
1.60 frystyk 1578: status = HTFTPServerInfo(request, cnet, ctrl, data);
1.95 frystyk 1579: if (status == HT_WOULD_BLOCK) return HT_OK;
1.60 frystyk 1580: ctrl->state = FTP_NEED_DATA;
1581: break;
1582:
1583: case FTP_SUCCESS:
1.102 frystyk 1584: HTTRACE(PROT_TRACE, "FTP Event... now in state FTP_SUCCESS\n");
1.95 frystyk 1585: FTPCleanup(request, HT_LOADED);
1.60 frystyk 1586: return HT_OK;
1587: break;
1588:
1589: case FTP_ERROR:
1.102 frystyk 1590: HTTRACE(PROT_TRACE, "FTP Event... now in state FTP_ERROR\n");
1.95 frystyk 1591: FTPCleanup(request, HT_ERROR);
1.60 frystyk 1592: return HT_OK;
1593: break;
1.23 frystyk 1594: }
1.60 frystyk 1595: } /* End of while(1) */
1.23 frystyk 1596: }
1.22 frystyk 1597:
1.95 frystyk 1598: PUBLIC void HTFTP_setTransferMode(FTPTransferMode mode)
1599: {
1600: g_FTPTransferMode = mode;
1601: }
1602:
1603: PUBLIC FTPTransferMode HTFTP_transferMode (void)
1604: {
1605: return g_FTPTransferMode;
1.97 frystyk 1606: }
1607:
1608: PUBLIC void HTFTP_setControlMode (FTPControlMode mode)
1609: {
1610: g_FTPControlMode = mode;
1611: }
1612:
1613: PUBLIC FTPControlMode HTFTP_controlMode (void)
1614: {
1615: return g_FTPControlMode;
1.95 frystyk 1616: }
Webmaster