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

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

Webmaster