Annotation of libwww/Library/src/HTFTP.c, revision 1.104

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

Webmaster