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

1.1       timbl       1: /*                     File Transfer Protocol (FTP) Client
                      2: **                     for a WorldWideWeb browser
                      3: **                     ===================================
                      4: **
                      5: **     A cache of control connections is kept.
                      6: **
                      7: ** Note: Port allocation
                      8: **
                      9: **     It is essential that the port is allocated by the system, rather
                     10: **     than chosen in rotation by us (POLL_PORTS), or the following
                     11: **     problem occurs.
                     12: **
                     13: **     It seems that an attempt by the server to connect to a port which has
                     14: **     been used recently by a listen on the same socket, or by another
                     15: **     socket this or another process causes a hangup of (almost exactly)
                     16: **     one minute. Therefore, we have to use a rotating port number.
                     17: **     The problem remains that if the application is run twice in quick
                     18: **     succession, it will hang for what remains of a minute.
                     19: **
                     20: ** Authors
                     21: **     TBL     Tim Berners-lee <timbl@info.cern.ch>
                     22: **     DD      Denis DeLaRoca 310 825-4580 <CSP1DWD@mvs.oac.ucla.edu>
1.22      frystyk    23: **      LM      Lou Montulli <montulli@ukanaix.cc.ukans.edu>
                     24: **      FM      Foteos Macrides <macrides@sci.wfeb.edu>
1.23      frystyk    25: **     HF      Henrik Frystyk <frystyk@dxcern.cern.ch>
1.30      luotonen   26: **     AL      Ari Luotonen <luotonen@www.cern.ch>
1.23      frystyk    27: **
1.1       timbl      28: ** History:
                     29: **      2 May 91       Written TBL, as a part of the WorldWideWeb project.
                     30: **     15 Jan 92       Bug fix: close() was used for NETCLOSE for control soc
                     31: **     10 Feb 92       Retry if cached connection times out or breaks
                     32: **      8 Dec 92       Bug fix 921208 TBL after DD
                     33: **     17 Dec 92       Anon FTP password now just WWWuser@ suggested by DD
1.2       timbl      34: **                     fails on princeton.edu!
1.22      frystyk    35: **     27 Dec 93 (FM)  Fixed up so FTP now works with VMS hosts.  Path
                     36: **                     must be Unix-style and cannot include the device
                     37: **                     or top directory.
                     38: **      ?? ??? ?? (LM)  Added code to prompt and send passwords for non
                     39: **                     anonymous FTP
                     40: **      25 Mar 94 (LM)  Added code to recognize different ftp server types
                     41: **                      and code to parse dates and sizes on most hosts.
                     42: **     27 Mar 93 (FM)  Added code for getting dates and sizes on VMS hosts.
1.23      frystyk    43: **     27 Apr 94 (HF)  The module is basically rewritten to conform with
                     44: **                     rfc 959, 1123 and 1579 and turned into a state 
                     45: **                     machine. New semantics of ftp URLs are supported.
1.30      luotonen   46: **      2 May 94 (AL)  Fixed possible security hole when the URL contains
                     47: **                     a newline, that could cause multiple commands to be
                     48: **                     sent to an FTP server.
1.1       timbl      49: **
                     50: ** Options:
1.23      frystyk    51: **     LISTEN          The default way to open a dats connection is by using
                     52: **                     PASV, but if that fails, we try PORT. If the PORT part
                     53: **                     is unwanted, it can be disabled by undefine LISTEN.
1.1       timbl      54: **
1.22      frystyk    55: ** Notes:
                     56: **                             Portions Copyright 1994 Trustees of Dartmouth College
                     57: **                     Code for recognizing different FTP servers and
                     58: **                     parsing "ls -l" output taken from Macintosh Fetch
                     59: **                     program with permission from Jim Matthews,
                     60: **                     Dartmouth Software Development Team.
                     61: **
1.23      frystyk    62: **     BUGS:   @@@     Use configuration file for user names
                     63: **
                     64: */
1.1       timbl      65: 
1.22      frystyk    66: /* Implementation dependent include files */
                     67: #include "tcp.h"
1.1       timbl      68: 
1.22      frystyk    69: /* Library include files */
1.1       timbl      70: #include "HTParse.h"
                     71: #include "HTUtils.h"
                     72: #include "HTTCP.h"
                     73: #include "HTAnchor.h"
1.22      frystyk    74: #include "HTFile.h"
1.6       secret     75: #include "HTBTree.h"
                     76: #include "HTChunk.h"
1.22      frystyk    77: #include "HTAlert.h"
1.21      frystyk    78: #include "HTDirBrw.h"
1.33      frystyk    79: #include "HTError.h"
1.22      frystyk    80: #include "HTFTP.h"                                      /* Implemented here */
                     81: 
                     82: /* Macros and other defines */
1.23      frystyk    83: /* If LISTEN is defined, then first 'PASV' then 'PORT' (if error) is tried,
                     84:    else ONLY 'PASV' is used in order to establish a data connection. */
                     85: #define LISTEN
                     86: #ifdef LISTEN
                     87: /* #define REPEAT_LISTEN */                      /* Reuse the portnumber once found */
                     88: /* #define POLL_PORTS */      /* If allocation does not work, poll ourselves.*/
                     89: #endif
1.22      frystyk    90: 
1.33      frystyk    91: #define FTP_DEFAULT_TIMEOUT    1000L                       /* 1/100 seconds */
                     92: 
1.1       timbl      93: #ifndef IPPORT_FTP
1.33      frystyk    94: #define IPPORT_FTP             21
1.1       timbl      95: #endif
                     96: 
1.33      frystyk    97: #define WWW_FTP_CLIENT "WWWuser"         /* If can't get user-info, use this */
                     98: 
1.22      frystyk    99: /* Globals */
1.23      frystyk   100: PUBLIC BOOL HTFTPUserInfo = YES;
1.33      frystyk   101: PUBLIC long HTFTPTimeOut = FTP_DEFAULT_TIMEOUT;
1.22      frystyk   102: 
1.23      frystyk   103: /* Type definitions and global variables etc. local to this module */ 
                    104: PRIVATE user_info *old_user;       /* Only used if HT_REUSE_USER_INFO is on */
                    105: PRIVATE HTList *session;        /* List of control connections in a session */
1.1       timbl     106: 
                    107: #ifdef POLL_PORTS
1.23      frystyk   108: #define FIRST_TCP_PORT  1024          /* Region to try for a listening port */
                    109: #define LAST_TCP_PORT   5999
                    110: PRIVATE        unsigned short   port_number = FIRST_TCP_PORT;
1.1       timbl     111: #endif
                    112: 
                    113: #ifdef LISTEN
1.23      frystyk   114: #ifdef REPEAT_LISTEN
                    115: PRIVATE int     master_socket = -1;           /* Listening socket = invalid */
                    116: #endif
                    117: PRIVATE char * this_addr;                                  /* Local address */
1.1       timbl     118: #endif
                    119: 
1.23      frystyk   120: /* ------------------------------------------------------------------------- */
                    121: /*               ************** TEMPORARY STUFF *************               */
                    122: /* ------------------------------------------------------------------------- */
                    123: #define MAX_ACCEPT_POLL                30
                    124: #define FCNTL(r, s, t) fcntl(r, s, t)
                    125: 
1.26      luotonen  126: 
1.32      frystyk   127: /*                                                             HTDoConnect()
                    128: **
                    129: **     TEMPORARY FUNCTION.
                    130: **     Note: Any port indication in URL, e.g., as `host:port' overwrites
                    131: **     the default_port value.
                    132: **
                    133: **     Returns 0 if OK, -1 on error
                    134: */
1.33      frystyk   135: PUBLIC int HTDoConnect ARGS5(HTRequest *, request, char *, url,
1.31      frystyk   136:                             u_short, default_port, int *, s,
                    137:                             u_long *, addr)
1.23      frystyk   138: {
                    139:     int status;
                    140:     struct sockaddr_in sock_addr;
                    141:     char *p1 = HTParse(url, "", PARSE_HOST);
                    142:     char *at_sign;
                    143:     char *host;
                    144: 
                    145:     /* if theres an @ then use the stuff after it as a hostname */
                    146:     if((at_sign = strchr(p1,'@')) != NULL)
                    147:        host = at_sign+1;
                    148:     else
                    149:        host = p1;
                    150:     if (TRACE) fprintf(stderr, "HTDoConnect. Looking up `%s\'\n", host);
                    151: 
                    152:    /* Set up defaults: */
1.25      frystyk   153:     memset((void *) &sock_addr, '\0', sizeof(sock_addr));
1.23      frystyk   154:     sock_addr.sin_family = AF_INET;
                    155:     sock_addr.sin_port = htons(default_port);
                    156: 
                    157:     /* Get node name */
                    158:     if (HTParseInet(&sock_addr, host)) {
                    159:        if (TRACE) fprintf(stderr, "HTDoConnect. Can't locate remote host `%s\'\n", host);
1.33      frystyk   160:        HTErrorAdd(request, ERR_FATAL, NO, HTERR_NO_REMOTE_HOST,
                    161:                   (void *) host, strlen(host), "HTDoConnect");
1.23      frystyk   162:        free (p1);
                    163:        *s = -1;
                    164:        return -1;
                    165:     }
                    166: 
                    167:     if ((*s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
1.33      frystyk   168:        HTErrorSysAdd(request, ERR_FATAL, NO, "socket");
1.23      frystyk   169:        free (p1);
1.33      frystyk   170:        return -1;
1.23      frystyk   171:     }
                    172:     if (addr)
                    173:        *addr = ntohl(sock_addr.sin_addr.s_addr);
                    174: 
                    175:     if (TRACE)
                    176:        fprintf(stderr, "HTDoConnect. Created socket number %d\n", *s);
                    177: 
                    178:     if ((status = connect(*s, (struct sockaddr *) &sock_addr,
                    179:                          sizeof(sock_addr))) < 0) {
1.33      frystyk   180:        HTErrorSysAdd(request, ERR_FATAL, NO, "connect");
1.23      frystyk   181:        if (NETCLOSE(*s) < 0)
1.33      frystyk   182:            HTErrorSysAdd(request, ERR_FATAL, NO, "close");
1.23      frystyk   183:        free(p1);
                    184:        *s = -1;
                    185:        return -1;
                    186:     }
                    187:     free(p1);
                    188:     return status;
                    189: }
                    190: 
                    191: 
                    192: /*                                                             HTDoAccept()
                    193: **
                    194: **     This function makes a non-blocking accept on a port and polls every
                    195: **     second until MAX_ACCEPT_POLL or interrupted by user.
                    196: **
                    197: **     BUGS Interrupted is not yet implemented!!!
                    198: **
                    199: **     Returns 0 if OK, -1 on error
                    200: */
1.33      frystyk   201: PRIVATE int HTDoAccept ARGS2(HTRequest *, request, int, sockfd)
1.23      frystyk   202: {
                    203:     struct sockaddr_in soc_address;
                    204:     int status;
                    205:     int cnt;
                    206:     int soc_addrlen = sizeof(soc_address);
                    207:     if (sockfd < 0) {
                    208:        if (TRACE) fprintf(stderr, "HTDoAccept.. Bad socket number\n");
                    209:        return -1;
                    210:     }
                    211:        
                    212:     /* First make the socket non-blocking */
                    213:     if((status = FCNTL(sockfd, F_GETFL, 0)) != -1) {
1.33      frystyk   214:        status |= FNDELAY;
1.23      frystyk   215:        status = FCNTL(sockfd, F_SETFL, status);
                    216:     }
                    217:     if (status == -1) {
1.33      frystyk   218:        HTErrorSysAdd(request, ERR_FATAL, NO, "fcntl");
1.23      frystyk   219:        return -1;
                    220:     }
1.22      frystyk   221: 
1.23      frystyk   222:     /* Now poll every sekund */
                    223:     for(cnt=0; cnt<MAX_ACCEPT_POLL; cnt++) {
                    224:        if ((status = accept(sockfd, (struct sockaddr*) &soc_address,
                    225:                             &soc_addrlen)) >= 0) {
                    226:            if (TRACE) fprintf(stderr,
                    227:                               "HTDoAccept.. Accepted new socket %d\n", 
                    228:                               status);
                    229:            return status;
                    230:        } else
1.33      frystyk   231:            HTErrorSysAdd(request, ERR_WARNING, YES, "accept");
1.23      frystyk   232:        sleep(1);
                    233:     }  
                    234:     
1.33      frystyk   235:     /* If nothing has happened */    
1.23      frystyk   236:     if (TRACE)
                    237:        fprintf(stderr, "HTDoAccept.. Timed out, no connection!\n");
1.33      frystyk   238:     HTErrorAdd(request, ERR_FATAL, NO, HTERR_TIME_OUT, NULL, 0, "HTDoAccept");
1.23      frystyk   239:     return -1;
                    240: }
1.22      frystyk   241: 
                    242: /* ------------------------------------------------------------------------- */
                    243: /*                        Directory Specific Functions                      */
                    244: /* ------------------------------------------------------------------------- */
1.25      frystyk   245: 
1.33      frystyk   246: 
                    247: /*                                                           HTFTPParseError
                    248: **
                    249: **     This function parses an (multiple line) error message and takes out
                    250: **     the error codes.
                    251: **
                    252: */
                    253: PRIVATE void HTFTPParseError ARGS1(HTChunk **, error)
                    254: {
                    255:     HTChunk *oldtext = *error;
                    256:     if (!oldtext || !oldtext->data) {
                    257:        if (TRACE) fprintf(stderr, "FTP......... No error message?\n");
                    258:        return;
                    259:     }
                    260:     {
                    261:        int result;                    /* The first return code in the chunk */
                    262:        char *oldp = oldtext->data;
                    263:        HTChunk *newtext = HTChunkCreate(128);
                    264:        if (oldtext->size > 4 && sscanf(oldp, "%d", &result) == 1) {
                    265:            oldp += 4;
                    266:            while (*oldp) {
                    267:                if (*oldp == '\n') {
                    268:                    int tmpres;
                    269:                    if (sscanf(++oldp, "%d", &tmpres) == 1) {
                    270:                        if (tmpres == result) {
                    271:                            HTChunkPutc(newtext, ' ');
                    272:                            oldp += 3;                     /* Skip this code */
                    273:                        }
                    274:                    }
                    275:                } else
                    276:                    HTChunkPutc(newtext, *oldp);
                    277:                oldp++;
                    278:            }
                    279:        }
                    280:        HTChunkFree(oldtext);
                    281:        HTChunkTerminate(newtext);
                    282:        *error = newtext;
                    283:     }
                    284:     return;
                    285: }
                    286: 
                    287: 
1.25      frystyk   288: /*                                                           HTFTPParseWelcome
1.23      frystyk   289: **
                    290: **     This function parses the welcome message stored in ctrl->welcome.
                    291: **     Only multi-line messages are considered interesting, and the starting
                    292: **     return code is removed.
                    293: **
                    294: */
1.25      frystyk   295: PRIVATE void HTFTPParseWelcome ARGS1(HTChunk **, welcome)
1.23      frystyk   296: {
1.25      frystyk   297:     HTChunk *oldtext = *welcome;
1.33      frystyk   298:     if (!oldtext || !oldtext->data) {
1.23      frystyk   299:        if (TRACE) fprintf(stderr, "FTP......... No welcome message?\n");
                    300:        return;
                    301:     }
                    302:     {
1.28      frystyk   303:        int result;                    /* The first return code in the chunk */
                    304:        char cont;                                      /* Either ' ' or '-' */
                    305:        char *oldp = oldtext->data;
1.23      frystyk   306:        HTChunk *newtext = HTChunkCreate(128);
1.28      frystyk   307:        if (oldtext->size > 4 && sscanf(oldp, "%d%c", &result, &cont) == 2) {
                    308:            oldp += 4;
                    309:            while (cont == '-') {
1.23      frystyk   310:                HTChunkPutc(newtext, *oldp);
                    311:                if (*oldp == '\n') {
1.28      frystyk   312:                    int tmpres;
                    313:                    if (isdigit(*++oldp) &&
                    314:                        sscanf(oldp, "%d%c", &tmpres, &cont) == 2) {
                    315:                        if (tmpres == result && cont == ' ')
                    316:                            break;
                    317:                        else
                    318:                            oldp +=3;                      /* Skip this code */
                    319:                    }
1.23      frystyk   320:                }
1.28      frystyk   321:                oldp++;
1.23      frystyk   322:            }
                    323:        }
                    324:        HTChunkTerminate(newtext);
                    325:        HTChunkFree(oldtext);
1.25      frystyk   326:        *welcome = newtext;
                    327:     }
                    328: }
                    329: 
                    330: 
                    331: /*                                                           HTFTPAddWelcome
                    332: **
                    333: **     This function accumulates every welcome messages from the various
                    334: **     states in the login procedure.
                    335: **
                    336: */
                    337: PRIVATE void HTFTPAddWelcome ARGS1(ftp_ctrl_info *, ctrl)
                    338: {
                    339:     if (!ctrl->welcome)                                            /* If first time */
                    340:        ctrl->welcome = HTChunkCreate(128);
                    341: 
                    342:     HTFTPParseWelcome(&ctrl->reply);
                    343:     if (ctrl->reply->size > 1) {
1.28      frystyk   344:        HTChunkPutc(ctrl->welcome, '\n');
1.25      frystyk   345:        HTChunkPuts(ctrl->welcome, ctrl->reply->data);
1.23      frystyk   346:     }
                    347: }
1.22      frystyk   348: 
1.23      frystyk   349: 
                    350: #if 0 /* NOT NEEDED FOR THE MOMENT */
1.22      frystyk   351: /*
                    352:  * is_ls_date() --
                    353:  *      Return TRUE if s points to a string of the form:
                    354:  *              "Sep  1  1990 " or
                    355:  *              "Sep 11 11:59 " or
                    356:  *              "Dec 12 1989  " or
                    357:  *              "FCv 23 1990  " ...
                    358:  *
                    359:  * Thanks to James.W.Matthews@Dartmouth.EDU (James W. Matthews)
                    360:  */
                    361: PRIVATE BOOL is_ls_date ARGS1(char *, s)
                    362: {
                    363:     /* must start with three alpha characget_striters */
                    364:     if (!isalpha(*s++) || !isalpha(*s++) || !isalpha(*s++))
                    365:        return FALSE;
                    366:     
                    367:     /* space */
                    368:     if (*s++ != ' ')
                    369:        return FALSE;
                    370:     
                    371:     /* space or digit */
                    372:     if ((*s != ' ') && !isdigit(*s))
                    373:        return FALSE;
                    374:     s++;
                    375:     
                    376:     /* digit */
                    377:     if (!isdigit(*s++))
                    378:        return FALSE;
                    379:     
                    380:     /* space */
                    381:     if (*s++ != ' ')
                    382:        return FALSE;
                    383:     
                    384:     /* space or digit */
                    385:     if ((*s != ' ') && !isdigit(*s))
                    386:        return FALSE;
                    387:     s++;
                    388:     
                    389:     /* digit */
                    390:     if (!isdigit(*s++))
                    391:        return FALSE;
                    392:     
                    393:     /* colon or digit */
                    394:     if ((*s != ':') && !isdigit(*s))
                    395:        return FALSE;
                    396:     s++;
                    397:     
                    398:     /* digit */
                    399:     if (!isdigit(*s++))
                    400:        return FALSE;
                    401:     
                    402:     /* space or digit */
                    403:     if ((*s != ' ') && !isdigit(*s))
                    404:        return FALSE;
                    405:     s++;
                    406:     
                    407:     /* space */
                    408:     if (*s++ != ' ')
                    409:        return FALSE;
                    410:     
                    411:     return TRUE;
                    412: } /* is_ls_date() */
1.23      frystyk   413: #endif
1.22      frystyk   414: 
                    415: /*                                                             HTStrpMonth()
                    416: **
                    417: **     Returns the number of the month given or -1 on error.
                    418: **
                    419: **     BUG: Handles US dates only!!!
                    420: */
                    421: PRIVATE int HTStrpMonth ARGS1(char *, month)
                    422: {
                    423:     int ret;
                    424:     if (!strncmp(month, "JAN", 3))
                    425:        ret = 0;
                    426:     else if (!strncmp(month, "FEB", 3))
                    427:        ret = 1;
                    428:     else if (!strncmp(month, "MAR", 3))
                    429:        ret = 2;
                    430:     else if (!strncmp(month, "APR", 3))
                    431:        ret = 3;
                    432:     else if (!strncmp(month, "MAY", 3))
                    433:        ret = 4;
                    434:     else if (!strncmp(month, "JUN", 3))
                    435:        ret = 5;
                    436:     else if (!strncmp(month, "JUL", 3))
                    437:        ret = 6;
                    438:     else if (!strncmp(month, "AUG", 3))
                    439:        ret = 7;
                    440:     else if (!strncmp(month, "SEP", 3))
                    441:        ret = 8;
                    442:     else if (!strncmp(month, "OCT", 3))
                    443:        ret = 9;
                    444:     else if (!strncmp(month, "NOV", 3))
                    445:        ret = 10;
                    446:     else if (!strncmp(month, "DEC", 3))
                    447:        ret = 11;
                    448:     else {
                    449:        ret = -1;
1.23      frystyk   450:        if (TRACE) fprintf(stderr, "HTStrpMonth. Couldn't resolve date.\n");
1.22      frystyk   451:     }
                    452:     return ret;
                    453: }
                    454: 
                    455: 
                    456: /*                                                             HTStrpTime()
                    457: **
1.23      frystyk   458: **     Converts a date string from 'ls -l' to a time_t number
1.22      frystyk   459: **     This is needed in order to put out the date using the same format
                    460: **     for all directory listings.
                    461: **
1.23      frystyk   462: **     Returns 0 on error.
1.22      frystyk   463: */
1.23      frystyk   464: PRIVATE time_t HTStrpTime ARGS1(char *, datestr)
1.22      frystyk   465: {
                    466:     struct tm *time_info;                  /* Points to static tm structure */
                    467:     char *bcol = datestr;                                   /* Column begin */
                    468:     char *ecol;                                                       /* Column end */
                    469:     long tval;
                    470:     int cnt;
1.23      frystyk   471:     time_t curtime = time(NULL);
1.22      frystyk   472:     if ((time_info = gmtime(&curtime)) == NULL) {
                    473:        if (TRACE)
1.23      frystyk   474:            fprintf(stderr, "HTStrpTime.. Can't get current time.\n");
                    475:        return (time_t) 0;
1.22      frystyk   476:     }
                    477:     time_info->tm_isdst = -1;                        /* Disable summer time */
                    478:     for (cnt=0; cnt<3; cnt++)                                      /* Month */
                    479:        *bcol++ = toupper(*bcol);
                    480:     if ((time_info->tm_mon = HTStrpMonth(datestr)) < 0)
1.23      frystyk   481:        return (time_t) 0;
1.22      frystyk   482:     ecol = bcol;                                                     /* Day */
                    483:     while (*ecol++ == ' ');                   /* Spool to other side of day */
                    484:     while (*ecol++ != ' ');
                    485:     *--ecol = '\0';
                    486:     time_info->tm_mday = atoi(bcol);
                    487:     time_info->tm_wday = 0;
                    488:     time_info->tm_yday = 0;
                    489:     bcol = ++ecol;                                                  /* Year */
                    490:     if ((ecol = strchr(bcol, ':')) == NULL) {
                    491:        time_info->tm_year = atoi(bcol)-1900;
                    492:        time_info->tm_sec = 0;
                    493:        time_info->tm_min = 0;
                    494:        time_info->tm_hour = 0;
                    495:     } else {                                                        /* Time */
1.23      frystyk   496:        /* If the time is given as hh:mm, then the file is less than 1 year
                    497:           old, but we might shift calandar year. This is avoided by checking 
                    498:           if the date parsed is future or not. */
1.22      frystyk   499:        *ecol = '\0';
                    500:        time_info->tm_sec = 0;
1.23      frystyk   501:        time_info->tm_min = atoi(++ecol);               /* Right side of ':' */
1.22      frystyk   502:        time_info->tm_hour = atoi(bcol);                 /* Left side of ':' */
1.23      frystyk   503:        if (mktime(time_info) > curtime)
                    504:            --time_info->tm_year;
1.22      frystyk   505:     }
1.23      frystyk   506:     return ((tval = mktime(time_info)) == -1 ? (time_t) 0 : tval); 
1.22      frystyk   507: }
                    508: 
                    509: 
                    510: /*                                                             HTVMSStrpTime()
                    511: **
1.23      frystyk   512: **     Converts a date string from vms to a time_t number
1.22      frystyk   513: **     This is needed in order to put out the date using the same format
                    514: **     for all directory listings.
                    515: **
1.23      frystyk   516: **     Returns 0 on error
1.22      frystyk   517: */
1.23      frystyk   518: PRIVATE time_t HTVMSStrpTime ARGS1(char *, datestr)
1.22      frystyk   519: {
                    520:     struct tm *time_info;                  /* Points to static tm structure */
                    521:     char *col;
                    522:     long tval;
1.23      frystyk   523:     time_t curtime = time(NULL);
1.22      frystyk   524:     if ((time_info = gmtime(&curtime)) == NULL)
1.23      frystyk   525:        return (time_t) 0;
1.22      frystyk   526:     time_info->tm_isdst = -1;                        /* Disable summer time */
                    527:     if ((col = strtok(datestr, "-")) == NULL)
1.23      frystyk   528:        return (time_t) 0;
1.22      frystyk   529:     time_info->tm_mday = atoi(col);                                  /* Day */
                    530:     time_info->tm_wday = 0;
                    531:     time_info->tm_yday = 0;
                    532:     if ((col = strtok(NULL, "-")) == NULL ||
                    533:        (time_info->tm_mon = HTStrpMonth(col)) < 0)
1.23      frystyk   534:        return (time_t) 0;
1.22      frystyk   535:     if ((col = strtok(NULL, " ")) == NULL)                          /* Year */
1.23      frystyk   536:        return (time_t) 0;
1.22      frystyk   537:     time_info->tm_year = atoi(col)-1900;
                    538:     if ((col = strtok(NULL, ":")) == NULL)                          /* Hour */
1.23      frystyk   539:        return (time_t) 0;
1.22      frystyk   540:     time_info->tm_hour = atoi(col);
                    541:     if ((col = strtok(NULL, " ")) == NULL)                          /* Mins */
1.23      frystyk   542:        return (time_t) 0;
1.22      frystyk   543:     time_info->tm_min = atoi(col);
                    544:     time_info->tm_sec = 0;
1.23      frystyk   545:     return ((tval = mktime(time_info)) < 0 ? (time_t) 0 : tval);
1.22      frystyk   546: }
                    547: 
                    548: 
                    549: /*                                                             HTFTPFilePerm()
                    550: **
                    551: **     Converts the file type from 'ls -l' into a long. The reason for
                    552: **     doing this is to be able to handle the file type and icon selection
                    553: **     similar to the Unix way used in HTBrowseDirectory().
                    554: **
                    555: */
                    556: PRIVATE long HTFTPFilePerm ARGS1(char *, permission)
                    557: {
                    558:     char *strptr = permission;
                    559:     long mode = 0L;
                    560:     
                    561:     /* Special files etc. are all handled like regular files */
                    562:     switch (*strptr++) {                                       /* File type */
                    563:       case 'd':        mode = S_IFMT & S_IFDIR; break;
                    564:       case 'l': mode = S_IFMT & S_IFLNK; break;
                    565:       default:  mode = S_IFMT & S_IFREG; break;
                    566:     }
                    567:     if (*strptr++ == 'r') mode |= S_IRUSR;                          /* User */
                    568:     if (*strptr++ == 'w') mode |= S_IWUSR;
                    569:     if (*strptr == 'x')
                    570:        mode |= S_IXUSR;
                    571:     else if (*strptr == 's')
                    572:        mode |= (S_IXUSR | S_ISUID);
                    573:     else if (*strptr == 'S')
                    574:        mode |= S_ISUID;
                    575:     strptr++;
                    576:     if (*strptr++ == 'r') mode |= S_IRGRP;                         /* Group */
                    577:     if (*strptr++ == 'w') mode |= S_IWGRP;
                    578:     if (*strptr == 'x')
                    579:        mode |= S_IXGRP;
                    580:     else if (*strptr == 's')
                    581:        mode |= (S_IXGRP | S_ISGID);
                    582:     else if (*strptr == 'S')
                    583:        mode |= S_ISGID;
                    584:     strptr++;
                    585:     if (*strptr++ == 'r') mode |= S_IROTH;                         /* Other */
                    586:     if (*strptr++ == 'w') mode |= S_IWOTH;
                    587:     if (*strptr == 'x')
                    588:        mode |= S_IXOTH;
                    589:     else if (*strptr == 't')
                    590:        mode |= (S_IXOTH | S_ISVTX);
                    591:     else if (*strptr == 'T')
                    592:        mode |= S_ISVTX;
                    593:     strptr++;
                    594:     return mode;
                    595: }
                    596: 
                    597: 
                    598: /*                                                          parse_unix_line()
                    599: **
                    600: **     Extract the name, size, and date from an 'ls'. The function expects
                    601: **     the following format of the ls-line:
                    602: **
                    603: **     <permission> <nlink> <owner> [<group>] <size> <date> <filename>
                    604: **
                    605: **     The group is not always present and is therefore optional. Both owner
                    606: **     and group can be numbers.
                    607: **
                    608: **     Returns YES if OK, NO on error
                    609: */
                    610: PRIVATE BOOL parse_unix_line ARGS2(char *,line, dir_file_info *,f_info)
                    611: {
                    612:     char *column;
                    613:     char *strptr;
                    614:     int ival;
                    615:     unsigned long lval;
                    616:     if (!line || !*line || !f_info)
                    617:        return NO;
                    618: 
                    619:     if ((column = strtok(line, " ")) == NULL)                /* Permissions */
                    620:        return NO;
                    621:     f_info->f_mode = HTFTPFilePerm(column);
                    622:     if ((column = strtok(NULL, " ")) == NULL)                      /* Links */
                    623:        return NO;
                    624:     if (sscanf(column, "%d", &ival) == 1)
                    625:        f_info->f_nlink = ival;
                    626:     if ((column = strtok(NULL, " ")) == NULL)                      /* Owner */
                    627:        return NO;
                    628:     StrAllocCopy(f_info->f_uid, column);
                    629:     if ((column = strtok(NULL, " ")) == NULL)           /* Group and/or Size */
                    630:        return NO;
                    631:     if (sscanf(column, "%lu", &lval) != 1) {
                    632:        StrAllocCopy(f_info->f_gid, column);
                    633:        if ((column = strtok(NULL, " ")) == NULL)
                    634:            return NO;
                    635:        if (sscanf(column, "%lu", &lval) == 1)
                    636:            f_info->f_size = lval;
                    637:     } else {                                      /* Group can be a number! */
                    638:        strptr = column+strlen(column)+1;
                    639:        while (*strptr++ == ' ');
                    640:        if (isdigit(*--strptr)) {          /* Month can't start with a digit */
                    641:            StrAllocCopy(f_info->f_gid, column);
                    642:            if ((column = strtok(NULL, " ")) == NULL)
                    643:                return NO;
                    644:            if (sscanf(column, "%lu", &lval) == 1)
                    645:                f_info->f_size = lval;
                    646:        } else {
1.23      frystyk   647:            StrAllocCopy(f_info->f_gid, "");
1.22      frystyk   648:            f_info->f_size = lval;
                    649:        }
                    650:     }
                    651:     column = column+strlen(column)+1;
                    652:     while (*column++ == ' ');
                    653:     strptr = --column+12;                           /* Find the date column */
                    654:     *strptr++ = '\0';
1.23      frystyk   655:     if ((f_info->f_mtime = HTStrpTime(column)) == (time_t) 0)
1.22      frystyk   656:        return NO;
                    657:     while (*strptr++ == ' ');                          /* Spool to filename */
                    658:     if ((f_info->f_mode & S_IFMT) == S_IFLNK) {        /* Strip any '->' */
                    659:        char *link = strstr(strptr-1, " -> ");
                    660:        if (link)
                    661:            *link = '\0';
                    662:     }
                    663:     StrAllocCopy(f_info->f_name, --strptr);
                    664:     return YES;                                        /* We have a full structure! */
                    665: }
                    666: 
                    667: 
                    668: /*                                                           parse_vms_line()
                    669: **
                    670: **      Format the name, date, and size from a VMS LIST line
                    671: **      into the dir_file_info structure
                    672: **
1.23      frystyk   673: **     BUGS: Group, user and permissions are not parsed!
                    674: **
1.22      frystyk   675: **     Returns YES if OK, NO on error
                    676: */
                    677: PRIVATE BOOL parse_vms_line ARGS2(char *, line, dir_file_info *, f_info)
                    678: {
                    679:     int i, j, ialloc;
                    680:     char *cp, *cpd, *cps, *cdir, *sp = " ";
                    681:     
                    682:     /**  Get rid of information lines by making them blank too **/
                    683:     /**  Valid lines have the semi-colon version number token  **/
                    684:     if (!line || !*line || !f_info || (cp = strchr(line, ';')) == NULL) {
                    685:        return NO;
                    686:     }
                    687: 
                    688:     /** Cut out file or directory name at VMS version number **/
                    689:     *cp++ ='\0';
                    690:     StrAllocCopy(f_info->f_name,line);
                    691:     
                    692:     /** Cast VMS file and directory names to lowercase **/
                    693:     for (i=0; f_info->f_name[i]; i++)
                    694:        f_info->f_name[i] = tolower(f_info->f_name[i]);
                    695:     
                    696:     /** Uppercase terminal .z's or _z's **/
                    697:     if ((--i > 2) && f_info->f_name[i] == 'z' &&
                    698:        (f_info->f_name[i-1] == '.' || f_info->f_name[i-1] == '_'))
                    699:        f_info->f_name[i] = 'Z';
                    700:     
                    701:     /* Trim off VMS directory extensions */
                    702:     if ((cdir = strstr(f_info->f_name, ".dir")) != NULL) { /* Strip any .dir */
                    703:        f_info->f_mode = S_IFMT & S_IFDIR;
                    704:        *cdir = '\0';
                    705:     } else
                    706:        f_info->f_mode = S_IFMT & S_IFREG;
                    707: 
                    708:     /** Convert any tabs in rest of line to spaces **/
                    709:     cps = cp-1;
                    710:     while ((cps=strchr(cps+1, '\t')) != NULL)
                    711:        *cps = ' ';
                    712:     
                    713:     /** Collapse serial spaces **/
                    714:     i = 0; j = 1;
                    715:     cps = cp;
                    716:     while (cps[j] != '\0') {
                    717:        if (cps[i] == ' ' && cps[j] == ' ')
                    718:            j++;
                    719:        else
                    720:            cps[++i] = cps[j++];
                    721:     }
                    722:     cps[++i] = '\0';
                    723:     
                    724:     /** Track down the date **/
                    725:     if ((cpd=strchr(cp, '-')) != NULL) {
1.24      luotonen  726:        if ((int)strlen(cpd) > 9 && isdigit(*(cpd-1)) &&
1.22      frystyk   727:            isalpha(*(cpd+1)) && *(cpd+4) == '-') {
1.23      frystyk   728:            if ((f_info->f_mtime = HTVMSStrpTime(cpd-2)) == (time_t) 0)
1.22      frystyk   729:                return NO;
                    730:        }
                    731:     }
                    732:     
                    733:     /** Track down the size **/
                    734:     if ((cpd = strchr(cp, '/')) != NULL) {
                    735:        /* Appears be in used/allocated format */
                    736:        cps = cpd;
                    737:        while (isdigit(*(cps-1)))
                    738:            cps--;
                    739:        if (cps < cpd)
                    740:            *cpd = '\0';
                    741:        f_info->f_size = atoi(cps);
                    742:        cps = cpd+1;
                    743:        while (isdigit(*cps))
                    744:            cps++;
                    745:        *cps = '\0';
                    746:        ialloc = atoi(cpd+1);
                    747:        /* Check if used is in blocks or bytes */
                    748:        if (f_info->f_size <= ialloc)
                    749:            f_info->f_size *= 512;
                    750:     }
                    751:     else if ((cps=strtok(cp, sp)) != NULL) {
                    752:        /* We just initialized on the version number */
                    753:        /* Now let's hunt for a lone, size number    */
                    754:        while ((cps=strtok(NULL, sp)) != NULL) {
                    755:            cpd = cps;
                    756:            while (isdigit(*cpd))
                    757:                cpd++;
                    758:            if (*cpd == '\0') {
                    759:                /* Assume it's blocks */
                    760:                f_info->f_size = atoi(cps) * 512;
                    761:                break;
                    762:            }
                    763:        }
                    764:     }
                    765:     return YES;                                        /* We have a full structure! */
                    766: }
                    767: 
                    768: 
                    769: /*                                                          parse_dir_entry() 
                    770: **
                    771: **      Given a line of LIST/NLST output in entry, return results 
                    772: **      and a file/dir name in f_info struct
                    773: **
1.23      frystyk   774: **      If first_entry is true, this is the first name in a directory.
1.22      frystyk   775: **     Returns YES if OK, NO on error
                    776: */
1.23      frystyk   777: PRIVATE BOOL parse_dir_entry ARGS4(ftp_data_info *, data, char *, entry,
                    778:                                   BOOL, first_entry, dir_file_info *, f_info)
1.22      frystyk   779: {
                    780:     BOOL status = YES;
1.23      frystyk   781:     switch (data->ctrl->server) {
1.22      frystyk   782:       case UNIX_SERVER:
                    783:       case PETER_LEWIS_SERVER:
                    784:       case MACHTEN_SERVER:
1.32      frystyk   785:       case WINDOWS_NT:                             /* This is an experiment */
1.22      frystyk   786:        /* Interpret and edit LIST output from Unix server */
1.23      frystyk   787:        if (first_entry) {
                    788:            if (data->ctrl->unsure_type == YES &&
                    789:                strncmp(entry, "total ", 6) &&
                    790:                (strstr(entry, "not available") != NULL)) {
1.22      frystyk   791:                /* this isn't really a unix server! */
1.23      frystyk   792:                if (TRACE)
                    793:                    fprintf(stderr, "FTP......... No, this isn't a UNIX server anyway :-(\n");
                    794:                data->ctrl->server = GENERIC_SERVER;
1.22      frystyk   795:            }
1.23      frystyk   796:            /* We might as well say that it is not unsure any more, as we
                    797:               can't (or don't) do anything about it, */
                    798:            data->ctrl->unsure_type = NO;
                    799:            status = NO;
                    800:        } else
                    801:            status = parse_unix_line(entry, f_info);
1.22      frystyk   802:        break;
                    803:        
                    804:       case VMS_SERVER:
                    805:        /* Interpret and edit LIST output from VMS server */
                    806:        /* and convert information lines to zero length.  */
                    807:        status = parse_vms_line(entry, f_info);
                    808:        break;
                    809:        
                    810:       case CMS_SERVER:
                    811:        /* Can't be directory... "entry" already equals the correct f_name */
                    812:        StrAllocCopy(f_info->f_name, entry);
                    813:        f_info->f_mode = S_IFMT & S_IFREG;
                    814:        break;
                    815:        
                    816:       case NCSA_SERVER:
                    817:       case TCPC_SERVER:
                    818:        /* Directories identified by trailing "/" characters */
                    819:        StrAllocCopy(f_info->f_name, entry);
                    820:        {
                    821:            int len = strlen(entry);
                    822:            if (*(entry+len-1) == '/') {
                    823:                *(entry+len-1) = '\0';
                    824:                f_info->f_mode = S_IFMT & S_IFDIR;
                    825:            } else {
                    826:                f_info->f_mode = S_IFMT & S_IFREG;
                    827:            }
                    828:        }
                    829:        break;
                    830:        
                    831:       default:
                    832:        /* We cant tell if it is a directory since we only did an NLST :-( */
                    833:        StrAllocCopy(f_info->f_name, entry);
                    834:        f_info->f_mode = S_IFMT & S_IFREG;
                    835:        break;
                    836:     }
                    837:     return status;
                    838: }
1.1       timbl     839: 
                    840: 
1.23      frystyk   841: PRIVATE int HTFTP_get_dir_string ARGS2(ftp_data_info *, data,
                    842:                                       dir_file_info *, f_info)
1.21      frystyk   843: {
1.23      frystyk   844:     int status = 1;
1.22      frystyk   845:     int ch;                          /* Must be int in order to contain EOF */
1.23      frystyk   846:     BOOL got_line = NO;
1.22      frystyk   847:     static BOOL first = YES;               /* Is it the first time through? */
1.23      frystyk   848:     static HTInputSocket *isoc;
1.22      frystyk   849:     HTChunk *chunk = HTChunkCreate(128);
                    850: 
1.23      frystyk   851:     if (first == YES)
                    852:        isoc = HTInputSocket_new(data->socket);          /* Set up buffering */
                    853:     do {                                       /* Until we have a nice line */
                    854:        while ((ch = HTInputSocket_getCharacter(isoc)) >= 0) {
                    855:            if (ch == CR || ch == LF) {                       /* Terminator? */
1.32      frystyk   856:                first = NO;
1.23      frystyk   857:                if (chunk->size != 0) {                     /* got some text */
                    858:                    if (data->ctrl->server == VMS_SERVER) {
                    859:                        /* Deal with MultiNet's wrapping of long lines - F.M.*/
                    860:                        if (isdigit(*(chunk->data+chunk->size-1)))
                    861:                            continue;
                    862:                    }
                    863:                    HTChunkTerminate(chunk);
                    864:                    if (parse_dir_entry(data, chunk->data, first, f_info)) {
                    865:                        got_line = YES;
                    866:                        break;
                    867:                    } else {
                    868:                        HTChunkClear(chunk);
1.22      frystyk   869:                    }
                    870:                }
1.23      frystyk   871:            } else
                    872:                HTChunkPutc(chunk, ch);
1.21      frystyk   873:        }
1.23      frystyk   874:     } while (got_line == NO && ch >= 0);
                    875:     if (ch < 0) {
                    876:        first = YES;
                    877:        status = (ch == EOF) ? 0 : ch;
                    878:     }
                    879:     if (first) {
                    880:        HTInputSocket_free(isoc);
                    881:        isoc = NULL;
                    882:     }
1.22      frystyk   883:     HTChunkFree(chunk);
1.21      frystyk   884:     return status;
                    885: }
                    886: 
1.22      frystyk   887: /* ------------------------------------------------------------------------- */
1.23      frystyk   888: /*       FTP Client Functions for managing control and data connections     */
1.22      frystyk   889: /* ------------------------------------------------------------------------- */
                    890: 
1.23      frystyk   891: /*                                                             HTFTP_send_cmd
1.22      frystyk   892: **
1.23      frystyk   893: **     This function sends a command through the control connection 
                    894: **     specified. The Telnet terminating end of line code <CRLF> is
                    895: **     appended automaticly. The parameter argument is ignored if NULL.
1.1       timbl     896: **
1.23      frystyk   897: **     Returns 0 on OK, else -1 but does NOT close the connection
1.1       timbl     898: */
1.23      frystyk   899: PRIVATE int HTFTP_send_cmd ARGS3(ftp_ctrl_info *, ctrl_info, char *, cmd,
                    900:                                 char *, pars)
1.1       timbl     901: {
1.23      frystyk   902:     char *command;
                    903:     if (!ctrl_info && ctrl_info->socket < 0) {
                    904:        if (TRACE)
                    905:            fprintf(stderr, "HTFTP_send_cmd: Invalid socket\n");
                    906:        return -1;
                    907:     }
                    908:     if ((command = (char *) malloc(strlen(cmd) + (pars ? strlen(pars)+1 : 0) +
                    909:                                   2 + 1)) == NULL)
                    910:        outofmem(__FILE__, "HTFTP_send_cmd");
                    911:     if (pars && *pars)
                    912:        sprintf(command, "%s %s%c%c", cmd, pars, CR, LF);
                    913:     else
                    914:        sprintf(command, "%s%c%c", cmd, CR, LF);
                    915:     if (TRACE) {
                    916:        if (!strcasecomp(cmd, "pass"))
                    917:            fprintf(stderr, "FTP Tx...... PASS ********\n");
1.22      frystyk   918:        else
1.23      frystyk   919:            fprintf(stderr, "FTP Tx...... %s", command);
                    920:     }
                    921: #ifdef NOT_ASCII
                    922:     {
                    923:        char *sp;
                    924:        for (sp = command; *sp; sp++) {
                    925:            *sp = TOASCII(*sp);
                    926:        }
                    927:     }
                    928: #endif
                    929:     {
                    930:        int status = NETWRITE(ctrl_info->socket, command,
                    931:                              (int) strlen(command));
                    932:        if (status < 0) {
                    933:            if (TRACE)
1.33      frystyk   934:                fprintf(stderr, "FTP......... Error sending command: %s",
                    935:                        command);
                    936:            HTInetStatus("write");
1.23      frystyk   937:            free(command);
                    938:            return -1;
                    939:        }
                    940:        free(command);
                    941:        return 0;
                    942:     }
                    943: }
1.1       timbl     944: 
                    945: 
1.23      frystyk   946: /*                                                        HTFTP_get_response
1.8       timbl     947: **
1.23      frystyk   948: **     This function gets the response from the net to the control connection
                    949: **     specified. If text is not NULL, the response text is put into this 
                    950: **     chunk. In case of OK, the freeing is then left to the callee. The
                    951: **     response is read until a <LF> is encountered, since not all servers
                    952: **     use a full telnet <CRLF> code.
1.1       timbl     953: **
1.23      frystyk   954: **     Returns the 3 digit return code on OK, else -1 but does NOT close
                    955: **     the control connection.
1.1       timbl     956: */
1.23      frystyk   957: PRIVATE int HTFTP_get_response ARGS2(ftp_ctrl_info *, ctrl_info,
                    958:                                     HTChunk **, text)
1.1       timbl     959: {
1.23      frystyk   960:     int result;                                         /* Three-digit decimal code */
                    961:     int offset = 0;                  /* Offset for each newline in response */
                    962:     BOOL first_line = YES;
                    963:     int ch;
                    964:     HTChunk *chunk = HTChunkCreate(128);
                    965: 
                    966:     if (!ctrl_info && ctrl_info->socket < 0) {
                    967:        if (TRACE)
                    968:            fprintf(stderr, "HTFTP_get_response: Invalid socket\n");
                    969:        return -1;
1.1       timbl     970:     }
                    971: 
1.23      frystyk   972:     /* Read response */
                    973:     while ((ch = HTInputSocket_getCharacter(ctrl_info->isoc)) >= 0) {
                    974:        if (ch == LF) {
                    975:            int tmpres;
                    976:            char cont;
                    977:            if (first_line == YES) {
                    978:                if (sscanf(chunk->data, "%d%c", &result, &cont) < 2) {
                    979:                    if (TRACE)
                    980:                        fprintf(stderr,
                    981:                                "FTP Rx...... `%s\' - no code found?\n",
                    982:                                chunk->data);
1.31      frystyk   983:                    HTChunkFree(chunk);
1.23      frystyk   984:                    return -1;
                    985:                }
                    986:                if (cont == '-') {
                    987:                    HTChunkPutc(chunk, '\n');
                    988:                    offset = chunk->size;                 /* Remember offset */
                    989:                    first_line = NO;
                    990:                } else {
                    991:                    HTChunkTerminate(chunk);
                    992:                    break;
                    993:                }
                    994:            } else {
1.28      frystyk   995:                if (isdigit(*(chunk->data+offset)) &&
                    996:                    sscanf(chunk->data+offset, "%d%c", &tmpres, &cont) == 2 &&
1.23      frystyk   997:                    tmpres == result && cont == ' ') {
                    998:                    HTChunkTerminate(chunk);
                    999:                    break;
                   1000:                } else {
                   1001:                    HTChunkPutc(chunk, '\n');
                   1002:                    offset = chunk->size;                   /* Update offset */
                   1003:                }
1.1       timbl    1004:            }
1.23      frystyk  1005:        } else
                   1006:            HTChunkPutc(chunk, (char) ch);
1.1       timbl    1007:     }
1.23      frystyk  1008:     if (!chunk->size || ch < 0) {                      /* No response read? */
1.31      frystyk  1009:        if (TRACE) fprintf(stderr, "FTP Rx...... No response?\n");
1.23      frystyk  1010:        HTChunkFree(chunk);
1.1       timbl    1011:        return -1;
                   1012:     }
1.23      frystyk  1013:     if (TRACE) fprintf(stderr, "FTP Rx...... %s\n", chunk->data);
                   1014:     if (!text)             /* Response text not wanted so we free the chunk */
                   1015:        HTChunkFree(chunk);
                   1016:     else {
                   1017:        if (*text)                                 /* Free old value, if any */
                   1018:            HTChunkFree(*text);
                   1019:        *text = chunk;
                   1020:     }
                   1021:     return result;
1.1       timbl    1022: }
                   1023: 
                   1024: 
1.23      frystyk  1025: 
                   1026: /*                                                     HTFTP_close_data_con
                   1027: **     Closes the data connection and frees memory
                   1028: **     Returns 0 if OK, -1 on error
1.22      frystyk  1029: */
1.23      frystyk  1030: PRIVATE int HTFTP_close_data_con ARGS1(ftp_data_info *, data)
1.22      frystyk  1031: {
1.23      frystyk  1032:     int status = 0;
                   1033:     if (data) {
                   1034:        if (data->socket >= 0) {
                   1035:            if (TRACE)
                   1036:                fprintf(stderr, "FTP......... Closing data socket %d\n",
                   1037:                        data->socket);
                   1038:            if ((status = NETCLOSE(data->socket)) < 0)
                   1039:                HTInetStatus("close data socket");
                   1040: #ifdef REPEAT_LISTEN
                   1041:            if (master_socket == data->socket)
                   1042:                master_socket = -1;
                   1043: #endif
                   1044:        }
                   1045:        FREE(data->datatype);
                   1046:        free(data);
                   1047:     } else {
                   1048:        if (TRACE) fprintf(stderr, "HTFTP_close_data_con: bad argument!");
                   1049:        status = -1;
1.22      frystyk  1050:     }
                   1051:     return status;
                   1052: }
                   1053: 
                   1054: 
1.23      frystyk  1055: /*                                                     HTFTP_close_ctrl_con
                   1056: **     Only if the control connection has no data connection(s) pending
                   1057: **     then it can be closed and the memory freed.
                   1058: **     Returns 0 if OK, -1 on error
1.1       timbl    1059: */
1.23      frystyk  1060: PRIVATE int HTFTP_close_ctrl_con ARGS1(ftp_ctrl_info *, ctrl)
1.1       timbl    1061: {
1.23      frystyk  1062:     int status = 0;
                   1063:     if (ctrl && (!ctrl->data_cons ||
                   1064:                 (ctrl->data_cons && !HTList_count(ctrl->data_cons)))) {
                   1065:        if (TRACE)
                   1066:            fprintf(stderr,
                   1067:                    "FTP......... Closing control socket %d\n", ctrl->socket);
                   1068:        if (ctrl->socket >= 0) {
                   1069:            if ((status = NETCLOSE(ctrl->socket)) < 0)
                   1070:                HTInetStatus("close control socket");
                   1071:        }
                   1072:        if (ctrl->isoc)
                   1073:            HTInputSocket_free(ctrl->isoc);
                   1074:        FREE(ctrl->location);
                   1075:        if (ctrl->user) {
                   1076:            FREE(ctrl->user->domain);
                   1077:            FREE(ctrl->user->id);
                   1078:            FREE(ctrl->user->passwd);
                   1079:            free(ctrl->user);
                   1080:        }
                   1081:        if (ctrl->welcome)
                   1082:            HTChunkFree(ctrl->welcome);
1.25      frystyk  1083:        if (ctrl->reply)
                   1084:            HTChunkFree(ctrl->reply);
1.23      frystyk  1085:        HTList_delete(ctrl->data_cons);
                   1086:        free(ctrl);
                   1087:     }
                   1088:     return status;
                   1089: }
1.22      frystyk  1090: 
1.1       timbl    1091: 
1.23      frystyk  1092: /*                                                     HTFTP_abort_ctrl_con
                   1093: **     Closes the control connection without looking if any data connections
                   1094: **     are pending => they are all removed, so be careful!
                   1095: **     Returns 0 if OK, -1 on error
                   1096: */
                   1097: PRIVATE int HTFTP_abort_ctrl_con ARGS1(ftp_ctrl_info *, ctrl)
                   1098: {
                   1099:     int status = 0;
                   1100:     if (!ctrl) {
                   1101:        if (TRACE)
                   1102:            fprintf(stderr, "HTFTP_abort_ctrl_con called with bad argument\n");
                   1103:        return -1;
                   1104:     }
                   1105:     if (TRACE) fprintf(stderr, "FTP......... Aborting control socket %d\n",
                   1106:                       ctrl->socket);
1.1       timbl    1107: 
1.23      frystyk  1108:     /* Close any pending data connections */
                   1109:     if (ctrl->data_cons && HTList_count(ctrl->data_cons)) {
                   1110:        HTList *cur = ctrl->data_cons;
                   1111:        ftp_data_info *pres;
                   1112:        while ((pres = (ftp_data_info *) HTList_nextObject(cur))) {
                   1113:            HTFTP_close_data_con(pres);
                   1114:        }
                   1115:        HTList_delete(ctrl->data_cons);
                   1116:        ctrl->data_cons = NULL;
                   1117:     }
1.1       timbl    1118: 
1.23      frystyk  1119:     /* If a session is going on, the control connections are closed later */
                   1120:     if (!session) {
                   1121:        if (ctrl->socket >= 0) {
                   1122:            if ((status = NETCLOSE(ctrl->socket)) < 0)
                   1123:                HTInetStatus("abot control socket");
                   1124:        }
                   1125:        if (ctrl->isoc)
                   1126:            HTInputSocket_free(ctrl->isoc);
                   1127:        FREE(ctrl->location);
                   1128:        if (ctrl->user) {
                   1129:            FREE(ctrl->user->domain);
                   1130:            FREE(ctrl->user->id);
                   1131:            FREE(ctrl->user->passwd);
                   1132:            free(ctrl->user);
                   1133:        }
                   1134:        if (ctrl->welcome)
                   1135:            HTChunkFree(ctrl->welcome);
1.25      frystyk  1136:        if (ctrl->reply)
                   1137:            HTChunkFree(ctrl->reply);
1.23      frystyk  1138:        free(ctrl);
1.1       timbl    1139:     }
1.23      frystyk  1140:     return status;
                   1141: }
1.1       timbl    1142: 
                   1143: 
1.23      frystyk  1144: /*                                                        HTFTP_parse_login
                   1145: **
                   1146: **     Scan 'login' part of URL for portnumber, uid and passwd. The
                   1147: **     expected format is [user[:password]@]host[:port]. The 'domain' field
                   1148: **     in the user structure is always filled out together with the
                   1149: **     serv_port. The rest is optional.
                   1150: **
                   1151: **     Returns YES if anything BUT the domain and serv_port is specified,
                   1152: **     else NO 
1.1       timbl    1153: */
1.23      frystyk  1154: PRIVATE BOOL HTFTP_parse_login ARGS3(char *, url, user_info *, user,
                   1155:                                     u_short *, serv_port)
                   1156: {
                   1157:     BOOL status = NO;
                   1158:     char *login = HTParse(url, "", PARSE_HOST);
                   1159:     char *host = strrchr(login, '@');
                   1160:     
                   1161:     if (host) {                                      /* Uid and/or passwd specified */
                   1162:        char *uid = login;
                   1163:        char *passwd;
                   1164:        FREE(user->id);                                   /* Skip old values */
                   1165:        FREE(user->passwd);
                   1166:        *host++ = '\0';
                   1167:        if ((passwd = strrchr(uid, ':')) != NULL) {      /* Passwd specified */
                   1168:            *passwd++ = '\0';    
                   1169:            if (passwd-1 > uid) {                /* Passwd AND uid specified */
                   1170:                StrAllocCopy(user->passwd, passwd);
                   1171:            }
                   1172:        }
                   1173:        StrAllocCopy(user->id, uid);
                   1174:        status = YES;
1.22      frystyk  1175:     } else {
1.23      frystyk  1176:        host = login;
1.22      frystyk  1177:     }
1.23      frystyk  1178:     {
                   1179:        char *portstr;
                   1180:        if ((portstr = strrchr(host, ':')) != NULL) {      /* Port specified */
                   1181:            char *endp = NULL;
                   1182:            *portstr++ = '\0';
                   1183:            *serv_port = (u_short) strtol(portstr, &endp, 10);
                   1184:            if (endp && *endp)   /* If portstr is not good, use default port */
                   1185:                *serv_port = (u_short) IPPORT_FTP;
                   1186:        }
1.22      frystyk  1187:     }
1.23      frystyk  1188:     StrAllocCopy(user->domain, host);                /* This is what's left */
                   1189:     free(login);
                   1190:     return status;
1.22      frystyk  1191: }
1.1       timbl    1192: 
                   1193: 
1.23      frystyk  1194: /*                                                        HTFTP_parse_datatype
1.1       timbl    1195: **
1.23      frystyk  1196: **     Scan 'ftptype' part of URL for the data type with parameters and
                   1197: **     returns the result in accordance with the FTP standard. If nothing is
                   1198: **     found then datatype = NULL.
1.1       timbl    1199: **
1.23      frystyk  1200: **     Returns YES if type is found, else NO 
1.1       timbl    1201: */
1.23      frystyk  1202: PRIVATE BOOL HTFTP_parse_datatype ARGS2(char *, url, char **, datatype)
1.1       timbl    1203: {
1.23      frystyk  1204:     BOOL retour = NO;
                   1205:     char *path = HTParse(url, "", PARSE_PATH);
                   1206:     char *type = strrchr(path, ';');
                   1207:     char dtype[6];
                   1208:     char *tptr = dtype;
                   1209:     
                   1210:     if (type && !strncasecomp(++type, "type=", 5)) {              /* type specified */
                   1211:        *tptr++ = toupper(*(type+5));               /* Look at the type-code */
                   1212:        if (*dtype == 'L') {                 /* We must look for a byte_size */
                   1213:            int cnt;
                   1214:            *tptr++ = ' ';
                   1215:            for (cnt=0; cnt<3 && *(type+6+cnt); cnt++)       /* Max 3 digits */
                   1216:                *tptr++ = *(type+6+cnt);
                   1217:        } else if (*dtype == 'A' || *dtype == 'E') {
                   1218:            *tptr++ = ' ';
                   1219:            *tptr++ = toupper(*(type+6));                   /* Get form-code */
                   1220:        }
                   1221:        *tptr = '\0';
                   1222:        StrAllocCopy(*datatype, dtype);
                   1223:        if (TRACE)
                   1224:            fprintf(stderr, "FTP......... Datatype found: `%s\'\n", *datatype);
                   1225:        retour = YES;
                   1226:     }
                   1227:     free(path);
                   1228:     return retour;
1.1       timbl    1229: }
                   1230: 
                   1231: 
1.23      frystyk  1232: /*                                                        HTFTP_init_con
1.1       timbl    1233: **
1.23      frystyk  1234: **     This function returns a control connection structure linked with a
                   1235: **     data connection structure. The control connection might already be
                   1236: **     open if HTFTPReuseCtrlCon == YES, but that is indicated in the state
                   1237: **     variable. ctrl->user->domain is always filled out but id and passwd
                   1238: **     are optional.
                   1239: **     If error, NULL is returned.
1.1       timbl    1240: */
1.29      frystyk  1241: PRIVATE ftp_ctrl_info *HTFTP_init_con ARGS2(HTRequest *, req, char *, url)
1.1       timbl    1242: {
1.23      frystyk  1243:     int status;
                   1244:     BOOL use_url = NO;    /* YES if uid and passwd are specified in the URL */
                   1245:     u_short serv_port = IPPORT_FTP;            /* Might be changed from URL */
1.1       timbl    1246:     
1.23      frystyk  1247:     ftp_ctrl_info *ctrl;
                   1248:     ftp_data_info *data;
                   1249:     user_info user;                /* Contains userid, passwd etc. from URL */
                   1250: 
                   1251:     if (!url || !*url) {
                   1252:        if (TRACE)
                   1253:            fprintf(stderr, "HTFTP_get_connection: Bad server address!\n");
                   1254:        return NULL;
                   1255:     }
                   1256: 
                   1257:     /* Initiate new data connection structure */
                   1258:     if ((data = (ftp_data_info *) calloc(1, sizeof(ftp_data_info))) == NULL)
                   1259:        outofmem(__FILE__, "HTFTP_get_ctrl_con");
                   1260:     data->socket = -1;                             /* Illigal socket number */
1.33      frystyk  1261:     data->passive = 0;                  /* We do the active open pr default */
1.23      frystyk  1262:     
                   1263:     /* Scan URL for uid, pw and portnumber */
1.25      frystyk  1264:     memset((void *) &user, '\0', sizeof(user_info));
1.23      frystyk  1265:     use_url = HTFTP_parse_login(url, &user, &serv_port);
                   1266: 
                   1267:     {
1.25      frystyk  1268:        char *filename = HTParse(url, "", PARSE_PATH+PARSE_PUNCTUATION);
1.23      frystyk  1269:        char *strptr;
                   1270: 
                   1271:        /* Check if file name is a directory */
                   1272:        if (!*(strptr = filename) || 
                   1273:            *(strptr = filename+strlen(filename)-1) == '/') {
                   1274:            data->directory = YES;
                   1275:        } else {
                   1276:            /* If data type is not specified in URL let's find it ourselves. */
                   1277:            HTUnEscape(filename);
1.34    ! luotonen 1278:            data->fileformat = HTFileFormat(filename,
        !          1279:                                            &req->content_encoding,
        !          1280:                                            &req->content_language);
1.23      frystyk  1281:            if (HTFTP_parse_datatype(filename, &data->datatype) != YES) {
1.34    ! luotonen 1282:                if ((req->content_encoding != HTAtom_for("8bit") &&
        !          1283:                     req->content_encoding != HTAtom_for("7bit"))) {
1.23      frystyk  1284:                    if (TRACE)
1.34    ! luotonen 1285:                        fprintf(stderr, "FTP......... Binary data mode\n");
1.23      frystyk  1286:                    StrAllocCopy(data->datatype, "I");
                   1287:                }
                   1288:            } else {
                   1289:                /* Chop off data type */
                   1290:                strptr = strrchr(filename, ';');
                   1291:                *strptr = '\0';
                   1292:            }
                   1293:        }
                   1294:        FREE(filename);
                   1295:     }
                   1296: 
                   1297:     /* Look if control connection already exists else generate new one */
                   1298:     if (session) {
                   1299:        BOOL found = NO;
                   1300:        HTList *cur = session;
                   1301:        struct sockaddr_in sock_addr;
                   1302:        char *host;
                   1303:            
                   1304:        /* if theres an @ then use the stuff after it as a hostname */
                   1305:        {
                   1306:            char *fullhost = HTParse(url, "", PARSE_HOST);
                   1307:            char *at_sign;
                   1308:            if((at_sign = strchr(fullhost, '@')) != NULL)
                   1309:                host = at_sign+1;
                   1310:            else
                   1311:                host = fullhost;
                   1312:        }
                   1313:        
                   1314:        /* Set up defaults: */
1.25      frystyk  1315:        memset((void *) &sock_addr, '\0', sizeof(sock_addr));
1.23      frystyk  1316:        sock_addr.sin_family = AF_INET;
                   1317:        sock_addr.sin_port = htons(serv_port);
                   1318:        
                   1319:        /* Get node name */
                   1320:        if (HTParseInet(&sock_addr, host)) {
                   1321:            if (TRACE) fprintf(stderr,
                   1322:                               "FTP......... Can't locate remote host `%s\'\n", host);
                   1323:            FREE(user.domain);
                   1324:            FREE(user.id);
                   1325:            FREE(user.passwd);
                   1326:            free(host);
                   1327:            return NULL;
                   1328:        }
                   1329:        {
                   1330:            /* Check if host, port and user info is the same */
                   1331:            u_long new_node = ntohl(sock_addr.sin_addr.s_addr);
                   1332:            while ((ctrl = (ftp_ctrl_info *) HTList_nextObject(cur))) {
                   1333:                if (new_node==ctrl->serv_node && serv_port==ctrl->serv_port) {
                   1334:                    if ((user.id && strcmp(user.id, ctrl->user->id)) ||
                   1335:                        (user.passwd && strcmp(user.id, ctrl->user->passwd))) {
                   1336:                        found = NO;
                   1337:                    } else {
                   1338:                        found = YES;
                   1339:                        break;
                   1340:                    }
                   1341:                }
                   1342:            }
                   1343:        }
                   1344:        FREE(host);
                   1345:        if (found) {
                   1346:            if (TRACE) fprintf(stderr,
                   1347:                               "FTP......... Already have connection for %d.%d.%d.%d. on port %d, socket %d at location `%s\'\n",
                   1348:                               (int)*((unsigned char *)(&ctrl->serv_node)+0),
                   1349:                               (int)*((unsigned char *)(&ctrl->serv_node)+1),
                   1350:                               (int)*((unsigned char *)(&ctrl->serv_node)+2),
                   1351:                               (int)*((unsigned char *)(&ctrl->serv_node)+3),
                   1352:                               ctrl->serv_port,
                   1353:                               ctrl->socket,
                   1354:                               ctrl->location);
                   1355:            data->ctrl = ctrl;                         /* Link them together */
                   1356:            HTList_addObject(ctrl->data_cons, (void *) data); /* Add to list */
                   1357:            FREE(user.domain);
                   1358:            FREE(user.id);
                   1359:            FREE(user.passwd);
                   1360:            return ctrl;           /* This should return the found structure */
                   1361:        } else {
                   1362:            if (TRACE)
                   1363:                fprintf(stderr, "FTP......... No existing connection found, so I build a new\n");
                   1364:        }
                   1365:     }
1.1       timbl    1366:     
1.23      frystyk  1367:     /* Set up data structure for control connection */
                   1368:     if ((ctrl = (ftp_ctrl_info *) calloc(1, sizeof(ftp_ctrl_info))) == NULL ||
                   1369:        (ctrl->user = (user_info *) calloc(1, sizeof(user_info))) == NULL)
                   1370:        outofmem(__FILE__, "HTFTP_init_con");
                   1371:     ctrl->serv_port = serv_port;
                   1372:     ctrl->socket = -1;                             /* Illigal socket number */
                   1373:     StrAllocCopy(ctrl->location, "");                    /* Default is root */
                   1374:     ctrl->server = UNKNOWN;
                   1375:     ctrl->unsure_type = YES;
                   1376:     ctrl->use_list = NO;
1.31      frystyk  1377:     ctrl->state = FTP_IDLE;
1.23      frystyk  1378:     if (session)
                   1379:        HTList_addObject(session, (void *) ctrl);
                   1380:     data->ctrl = ctrl;                                /* Link them together */
                   1381:     ctrl->data_cons = HTList_new();
                   1382:     HTList_addObject(ctrl->data_cons, (void *) data);      /* First element */
                   1383: 
                   1384:     /* Initialize user info */
                   1385:     if (HTFTPUserInfo && !old_user) {   /* If first time */
                   1386:        if ((old_user = (user_info *) calloc(1, sizeof(user_info))) == NULL)
                   1387:            outofmem(__FILE__, "HTFTP_init_con");
                   1388:        StrAllocCopy(old_user->domain, "");/* Can't strcmp with NULL, can I? */
                   1389:     }
                   1390:     if (use_url) {
                   1391:        StrAllocCopy(ctrl->user->domain, user.domain);
                   1392:        StrAllocCopy(ctrl->user->id, user.id);
                   1393:        if (user.passwd) {
                   1394:            StrAllocCopy(ctrl->user->passwd, user.passwd);
                   1395:        }
                   1396:     } else if (HTFTPUserInfo && !strcmp(old_user->domain, user.domain)) {
                   1397:        StrAllocCopy(ctrl->user->domain, user.domain);
                   1398:        if (old_user->id) {
                   1399:            StrAllocCopy(ctrl->user->id, old_user->id);
                   1400:            if (old_user->passwd)
                   1401:                StrAllocCopy(ctrl->user->passwd, old_user->passwd);
                   1402:        }
                   1403:     } else {
                   1404:        char *uid = getenv("USER");
                   1405:        StrAllocCopy(ctrl->user->domain, user.domain);
                   1406:        StrAllocCopy(ctrl->user->id, "anonymous");
                   1407:        if (uid)
                   1408:            StrAllocCopy(ctrl->user->passwd, uid);
                   1409:        else
                   1410:            StrAllocCopy(ctrl->user->passwd, WWW_FTP_CLIENT);
                   1411:        StrAllocCat(ctrl->user->passwd, "@");
                   1412:     }
                   1413:     FREE(user.domain);
                   1414:     FREE(user.id);
                   1415:     FREE(user.passwd);
                   1416: 
                   1417:     /* Now get ready for a connect */
                   1418:     if (TRACE) fprintf(stderr, "FTP......... Looking for `%s\'\n", url);
1.33      frystyk  1419:     if ((status = HTDoConnect (req, url, serv_port, &ctrl->socket,
1.23      frystyk  1420:                               &ctrl->serv_node)) < 0)
                   1421:     {
                   1422:        if (TRACE)
                   1423:            fprintf(stderr, "HTFTP_init_con: Connection not established!\n");
                   1424:        HTFTP_abort_ctrl_con(ctrl);
                   1425:        return NULL;
                   1426:     }
                   1427:     ctrl->isoc = HTInputSocket_new(ctrl->socket);               /* buffering */
1.1       timbl    1428:     
1.23      frystyk  1429:     if (TRACE) 
                   1430:        fprintf(stderr, "FTP......... Control connected, socket %d\n",
                   1431:                ctrl->socket);
                   1432:     return ctrl;
                   1433: }
                   1434: 
                   1435: 
                   1436: #ifdef LISTEN
                   1437: /*     Open a master socket for listening on
                   1438: **     -------------------------------------
                   1439: **
                   1440: **     When data is transferred, we open a port, and wait for the server to
                   1441: **     connect with the data.
                   1442: **
                   1443: ** On entry,
                   1444: **     master_socket   Must be negative if not set up already.
                   1445: ** On exit,
                   1446: **     Returns         The listening data socket if OK
                   1447: **                     Less than zero if error.
                   1448: **     master_socket   is socket number if good, else negative.
                   1449: **     port_number     is valid if good.
                   1450: */
                   1451: PRIVATE BOOL get_listen_socket ARGS1(ftp_data_info *, data)
                   1452: {
                   1453:     struct sockaddr_in local_addr;                /* Binary network address */
                   1454:     int status = -1;
                   1455: 
                   1456: #ifdef REPEAT_LISTEN
                   1457:     if (master_socket >= 0) {                               /* Done already */
                   1458:        data->socket = master_socket;
                   1459:        if (TRACE)
                   1460:            fprintf(stderr, "FTP......... Reusing passive data socket: %d\n",
                   1461:                    data->socket);
                   1462:        return data->socket;
                   1463:     }
1.33      frystyk  1464: #endif /* REPEAT_LISTEN */
1.1       timbl    1465: 
1.23      frystyk  1466:     /* Create internet socket */
                   1467:     if ((data->socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
                   1468:        return HTInetStatus("socket for ftp data");
                   1469:     if (TRACE)
1.32      frystyk  1470:        fprintf(stderr, "HTListen.... Created socket number %d\n",
1.23      frystyk  1471:                data->socket);
1.1       timbl    1472:     
1.23      frystyk  1473:     /* Search for a free port. */
1.25      frystyk  1474:     memset((void *) &local_addr, '\0', sizeof(local_addr));
1.23      frystyk  1475:     local_addr.sin_family = AF_INET;       /* Family = internet, host order */
                   1476:     local_addr.sin_addr.s_addr = htonl(INADDR_ANY); /* For multi homed hosts */
                   1477: 
                   1478:     /* Inherit the local address information from the control socket */
                   1479:     {
                   1480:        int addr_size = sizeof(local_addr);
                   1481:        if (getsockname(data->ctrl->socket, (struct sockaddr *)
                   1482:                        &local_addr, &addr_size) < 0) {
                   1483:            status = HTInetStatus("getsockname");
                   1484:            goto errorfin;
                   1485:        }
                   1486:     }
                   1487: 
1.1       timbl    1488: #ifdef POLL_PORTS
                   1489:     {
                   1490:         unsigned short old_port_number = port_number;
1.23      frystyk  1491:        for (port_number=old_port_number+1;;port_number++) {
1.1       timbl    1492:            if (port_number > LAST_TCP_PORT)
                   1493:                port_number = FIRST_TCP_PORT;
                   1494:            if (port_number == old_port_number) {
1.23      frystyk  1495:                if (TRACE)
                   1496:                    fprintf(stderr, "FTP......... No data port available.\n");
                   1497:                goto errorfin;
1.1       timbl    1498:            }
1.23      frystyk  1499:            local_addr.sin_port = htons(port_number);
                   1500: 
                   1501:            /* The socket address is casted to generic sockaddr */
                   1502:            if (bind(data->socket, (struct sockaddr *) &local_addr,
                   1503:                     sizeof(local_addr)) == 0)
                   1504:                break;                               /* We have found a port */
                   1505:            status = HTInetStatus("bind");    /* else, what error did we get */
                   1506:        }
1.1       timbl    1507:     }
                   1508: #else
                   1509:     {
1.23      frystyk  1510:        local_addr.sin_port = 0;             /* Unspecified: please allocate */
                   1511: 
                   1512:        /* The socket address is casted to a generic address */
                   1513:        if (bind(data->socket, (struct sockaddr *) &local_addr,
                   1514:                 sizeof(local_addr)) < 0) {
                   1515:            status = HTInetStatus("bind");
                   1516:            goto errorfin;
                   1517:        }
                   1518:     }
1.33      frystyk  1519: #endif /* POLL_PORTS */
1.23      frystyk  1520:     /* Now we must find out who we are to tell the other guy. */
                   1521:     {
                   1522:        int addr_size = sizeof(local_addr);
                   1523:        if (getsockname(data->socket, (struct sockaddr *) &local_addr,
                   1524:                        &addr_size) < 0) {
                   1525:            status = HTInetStatus("getsockname");
                   1526:            goto errorfin;
                   1527:        }
                   1528:        if (TRACE) fprintf(stderr, "FTP......... This host is `%s\'\n",
                   1529:                           HTInetString(&local_addr));
                   1530:     }
                   1531:     if (TRACE) fprintf(stderr, "FTP......... Bound to port %d on %s\n",
                   1532:                       (int) ntohs(local_addr.sin_port),
                   1533:                       HTInetString(&local_addr));
                   1534: 
                   1535:     /* this_addr is a static global, we can refer to later */
                   1536:     if (!this_addr && (this_addr = (char *) malloc(24)) == NULL)
                   1537:        outofmem(__FILE__, "get_listen_socket");
                   1538:     {
                   1539:        u_long addr = ntohl(local_addr.sin_addr.s_addr);
                   1540:        u_short port = ntohs(local_addr.sin_port);
                   1541:        sprintf(this_addr, "%d,%d,%d,%d,%d,%d",
                   1542:                (int)*((unsigned char *)(&addr)+0),
                   1543:                (int)*((unsigned char *)(&addr)+1),
                   1544:                (int)*((unsigned char *)(&addr)+2),
                   1545:                (int)*((unsigned char *)(&addr)+3),
                   1546:                (int)*((unsigned char *)(&port)+0),
                   1547:                (int)*((unsigned char *)(&port)+1));
                   1548:     }
                   1549: 
                   1550:     /* Inform TCP that we will accept connections. Backlog is 1 as we only
                   1551:        want (and expect) one connection. If a 3rd host makes a connect
                   1552:        to this port, we have problems! */
                   1553:     if (listen(data->socket, 1) < 0) {
                   1554:        status = HTInetStatus("listen");
                   1555:        goto errorfin;
                   1556:     }
                   1557:     if (TRACE) fprintf(stderr,
                   1558:                       "FTP......... Data socket number %d listening\n",
                   1559:                       data->socket);
1.1       timbl    1560: 
                   1561: #ifdef REPEAT_LISTEN
1.23      frystyk  1562:     master_socket = data->socket;                   /* Update master_socket */
1.33      frystyk  1563: #endif /* REPEAT_LISTEN */
1.23      frystyk  1564:     return data->socket;                                            /* Good */
                   1565: 
                   1566:   errorfin:
                   1567:     NETCLOSE(data->socket);
                   1568:     data->socket = -1;
                   1569:     return -1;
                   1570: }
1.33      frystyk  1571: #endif /* LISTEN */
1.23      frystyk  1572: 
                   1573: 
                   1574: /*                                                             HTFTP_login
                   1575: **
                   1576: **     This function makes a login to a ftp-server. It takes the user name
                   1577: **     and passwd specified in ctrl->user and if that fails or an additional
                   1578: **     account is needed, the user is prompted. As it is difficult, when
                   1579: **     the server sends it's welcome message, we receive them all and choose
                   1580: **     the longest.
                   1581: **
                   1582: **     Returns -2 on ERROR, -1 on FAILURE, 0 on SUCCESS.
1.1       timbl    1583: */
1.23      frystyk  1584: PRIVATE int HTFTP_login ARGS1(ftp_ctrl_info *, ctrl)
                   1585: {
                   1586:     enum _state {
                   1587:        ERROR  = -2,
                   1588:        FAILURE = -1,
                   1589:        SUCCESS = 0,
                   1590:        BEGIN,
                   1591:        SENT_UID,
                   1592:        SENT_PASSWD,
                   1593:        NEED_USER_INFO,
                   1594:        NEED_PASSWD,
                   1595:        NEED_ACCOUNT,
                   1596:        SENT_ACCOUNT
                   1597:     } state = BEGIN;
                   1598:     BOOL asked = YES;              /* Have we already asked for uid/passwd? */
1.25      frystyk  1599:     int status = HTFTP_get_response(ctrl, &ctrl->reply);     /* Get greeting */
1.23      frystyk  1600:     if (status < 0) {
                   1601:         if (TRACE) fprintf (stderr, "FTP......... Interrupted at beginning of login.\n");
                   1602:        return ERROR;
1.25      frystyk  1603:     } else
                   1604:        HTFTPAddWelcome(ctrl);
1.23      frystyk  1605: 
                   1606:     /* This loop only stops if state is ERROR, FAILURE or SUCCESS */
                   1607:     while (state > 0) {
                   1608:        switch (state) {
                   1609:          case BEGIN:
                   1610:            if (!HTFTP_send_cmd(ctrl, "USER", ctrl->user->id))
                   1611:                state = SENT_UID;
                   1612:            else
                   1613:                state = ERROR;
                   1614:            break;
                   1615: 
                   1616:          case SENT_UID:
1.25      frystyk  1617:            status = HTFTP_get_response(ctrl, &ctrl->reply);
                   1618:            if (status/100 == 2) {                  /* Logged in w/o passwd! */
1.28      frystyk  1619:                HTFTPAddWelcome(ctrl);
1.23      frystyk  1620:                state = SUCCESS;
1.28      frystyk  1621:            } else if (status/100 == 3) {               /* Password demanded */
                   1622:                HTFTPAddWelcome(ctrl);
1.23      frystyk  1623:                state = NEED_PASSWD;
1.28      frystyk  1624:            } else if (status == 530 && asked == YES)
1.23      frystyk  1625:                state = NEED_USER_INFO;                      /* User unknown */
                   1626:            else if (status/100 == 4)
                   1627:                state = FAILURE;
                   1628:            else
                   1629:                state = ERROR;
                   1630:            break;
1.1       timbl    1631: 
1.23      frystyk  1632:          case NEED_PASSWD:
                   1633:            if (!ctrl->user->passwd) {                  /* Got to ask for it */
                   1634:                char *prompt = NULL;
                   1635:                StrAllocCopy(prompt, "Enter password for user: ");
                   1636:                StrAllocCat(prompt, ctrl->user->id);
                   1637:                StrAllocCat(prompt, "@");
                   1638:                StrAllocCat(prompt, ctrl->user->domain);
                   1639:                StrAllocCat(prompt, ": ");
                   1640:                if ((ctrl->user->passwd = HTPromptPassword(prompt)) == NULL) {
                   1641:                    state = ERROR;
                   1642:                    free(prompt);
                   1643:                    break;
                   1644:                }
                   1645:                free(prompt);
                   1646:            }
                   1647:            /* If userid = "anonymous" then make sure that there is a '@' at
                   1648:               the end of the passwd */
                   1649:            if (!strcasecomp(ctrl->user->id, "anonymous")) {
                   1650:                if (*(ctrl->user->passwd+strlen(ctrl->user->passwd)-1) != '@')
                   1651:                    StrAllocCat(ctrl->user->passwd, "@");
                   1652:            }
                   1653:            if (!HTFTP_send_cmd(ctrl, "PASS", ctrl->user->passwd))
                   1654:                state = SENT_PASSWD;
                   1655:            else
                   1656:                state = ERROR;
                   1657:            break;
1.1       timbl    1658: 
1.23      frystyk  1659:          case SENT_PASSWD:
1.25      frystyk  1660:            status = HTFTP_get_response(ctrl, &ctrl->reply);
                   1661:            if (status/100 == 2) {                  /* Logged in with passwd */
                   1662:                HTFTPAddWelcome(ctrl);
1.23      frystyk  1663:                state = SUCCESS;
1.28      frystyk  1664:            } else if (status/100 == 3) {                /* Account demanded */
                   1665:                HTFTPAddWelcome(ctrl);
1.23      frystyk  1666:                state = NEED_ACCOUNT;
1.28      frystyk  1667:            } else if (status == 530 && asked == YES)
1.23      frystyk  1668:                state = NEED_USER_INFO;                      /* User unknown */
                   1669:            else if (status/100 == 4)
                   1670:                state = FAILURE;
                   1671:            else
                   1672:                state = ERROR;
                   1673:            break;
                   1674:            
                   1675:          case NEED_ACCOUNT:
                   1676:            {
                   1677:                char *prompt = NULL;
                   1678:                char *account = NULL;
                   1679:                StrAllocCopy(prompt, "Enter account for user: ");
                   1680:                StrAllocCat(prompt, ctrl->user->domain);
                   1681:                StrAllocCat(prompt, "@");
                   1682:                StrAllocCat(prompt, ctrl->user->id);
                   1683:                if ((account = HTPrompt(prompt, NULL)) != NULL &&
                   1684:                    !HTFTP_send_cmd(ctrl, "ACCT", account)) {
                   1685:                    state = SENT_ACCOUNT;
                   1686:                } else {
                   1687:                    state = ERROR;
                   1688:                }
                   1689:                free(prompt);
                   1690:                free(account);
                   1691:            }
                   1692:            break;
1.1       timbl    1693: 
1.23      frystyk  1694:          case SENT_ACCOUNT:
1.25      frystyk  1695:            status = HTFTP_get_response(ctrl, &ctrl->reply);
                   1696:            if (status/100 == 2) {
                   1697:                HTFTPAddWelcome(ctrl);
1.23      frystyk  1698:                state = SUCCESS;
1.25      frystyk  1699:            } else if (status/100 == 4)
1.23      frystyk  1700:                state = FAILURE;
                   1701:            else
                   1702:                state = ERROR;
                   1703:            break;
                   1704:            
                   1705:          case NEED_USER_INFO:
                   1706:            {
                   1707:                char *prompt = NULL;
                   1708:                StrAllocCopy(prompt, "Enter username and password for: ");
                   1709:                StrAllocCat(prompt, ctrl->user->domain);
                   1710:                FREE(ctrl->user->id);
                   1711:                FREE(ctrl->user->passwd);
                   1712:                HTPromptUsernameAndPassword(prompt, &ctrl->user->id,
                   1713:                                             &ctrl->user->passwd);
                   1714:                if (ctrl->user->id && ctrl->user->passwd &&
                   1715:                    !HTFTP_send_cmd(ctrl, "USER", ctrl->user->id))
                   1716:                    state = SENT_UID;
                   1717:                else
                   1718:                    state = ERROR;
                   1719:                free(prompt);
                   1720:            }
                   1721:            asked = NO;
                   1722:            break;
1.1       timbl    1723: 
1.23      frystyk  1724:          case FAILURE:                       /* Otherwise gcc complains :-( */
                   1725:          case ERROR:
                   1726:          case SUCCESS:
                   1727:            break;
                   1728:        } /* end of switch */
                   1729:     }
                   1730:     if (state == SUCCESS) {
                   1731:        if (TRACE)
                   1732:            fprintf(stderr, "FTP......... Logged in at `%s\' as `%s\'\n",
                   1733:                    ctrl->user->domain, ctrl->user->id);
                   1734:     }
1.1       timbl    1735: 
1.23      frystyk  1736:     /* This is a real pain this reuse user stuff :-( */
                   1737:     if (HTFTPUserInfo) {
                   1738:        StrAllocCopy(old_user->domain, ctrl->user->domain);
                   1739:        StrAllocCopy(old_user->id, ctrl->user->id);
                   1740:        StrAllocCopy(old_user->passwd, ctrl->user->passwd);
                   1741:     }
                   1742:     return state;
                   1743: }
1.1       timbl    1744: 
1.23      frystyk  1745: /*                                                             HTFTP_logout
1.1       timbl    1746: **
1.23      frystyk  1747: **     This function logs out from a ftp-server.
                   1748: **
                   1749: **     Returns -2 on ERROR, -1 on FAILURE, 0 on SUCCESS.
1.1       timbl    1750: */
1.23      frystyk  1751: PRIVATE int HTFTP_logout ARGS1(ftp_ctrl_info *, ctrl)
                   1752: {
                   1753:     enum _state {
                   1754:        ERROR  = -2,
                   1755:        FAILURE = -1,
                   1756:        SUCCESS = 0,
                   1757:        BEGIN,
                   1758:        SENT_QUIT
                   1759:     } state = BEGIN;
                   1760:     int status;
                   1761: 
                   1762:     /* This loop only stops if state is ERROR, FAILURE or SUCCESS */
                   1763:     while (state > 0) {
                   1764:        switch (state) {
                   1765:          case BEGIN:
                   1766:            if (!HTFTP_send_cmd(ctrl, "QUIT", NULL))
                   1767:                state = SENT_QUIT;
                   1768:            else
                   1769:                state = ERROR;
                   1770:            break;
                   1771: 
                   1772:          case SENT_QUIT:
1.28      frystyk  1773:            status = HTFTP_get_response(ctrl, &ctrl->reply);
1.23      frystyk  1774:            if (status/100 == 2)
                   1775:                state = SUCCESS;
                   1776:            else if (status/100 == 4)
                   1777:                state = FAILURE;
                   1778:            else
                   1779:                state = ERROR;
                   1780:            break;
                   1781: 
                   1782:          case FAILURE:                       /* Otherwise gcc complains :-( */
                   1783:          case ERROR:
                   1784:          case SUCCESS:
                   1785:            break;
                   1786:        }
                   1787:     }
                   1788:     return state;
                   1789: }
                   1790: 
1.22      frystyk  1791: 
1.23      frystyk  1792: /*                                                         HTFTP_get_data_con
                   1793:  **
                   1794:  **            Gets a valid data connection to the server and initializes the
                   1795:  **    transfer mode.
                   1796:  **
                   1797:  **    Returns -2 on ERROR, -1 on FAILURE, 0 on SUCCESS.
                   1798:  */
1.33      frystyk  1799: PRIVATE int HTFTP_get_data_con ARGS3(HTRequest *, request,
                   1800:                                     ftp_data_info *, data, char *, url)
1.1       timbl    1801: {
1.23      frystyk  1802:     enum _state {
                   1803:        ERROR = -2,
                   1804:        FAILURE = -1,
                   1805:        SUCCESS = 0,
                   1806:        BEGIN,
                   1807:        SENT_TYPE,
                   1808:        SENT_PASV,
                   1809:        SENT_PORT,
1.33      frystyk  1810:        NEED_ACTIVE,             /* We are the active ones in the connection */
                   1811:        NEED_PASSIVE                                       /* We are passive */
1.23      frystyk  1812:     } state = BEGIN;
                   1813:     int serv_port;
1.1       timbl    1814:     int status;
1.28      frystyk  1815:     ftp_ctrl_info *ctrl = data->ctrl;
1.1       timbl    1816:     
1.23      frystyk  1817:     /* This loop only stops if state is ERROR, FAILURE or SUCCESS */
                   1818:     while (state > 0) {
                   1819:        switch (state) {
                   1820:          case BEGIN:
1.33      frystyk  1821: 
1.23      frystyk  1822:            /* First check if it is necessary to send TYPE, else send PASV */
                   1823:            if (data->datatype) {
1.28      frystyk  1824:                if (!HTFTP_send_cmd(ctrl, "TYPE", data->datatype))
1.23      frystyk  1825:                    state = SENT_TYPE;
                   1826:                else
                   1827:                    state = ERROR;
                   1828:            } else {
1.28      frystyk  1829:                if (!HTFTP_send_cmd(ctrl, "PASV", NULL))
1.23      frystyk  1830:                    state = SENT_PASV;
                   1831:                else
                   1832:                    state = ERROR;
                   1833:            }
                   1834:            break;
                   1835:            
                   1836:          case SENT_PASV:
                   1837:            {
1.28      frystyk  1838:                status = HTFTP_get_response(ctrl, &ctrl->reply);
1.23      frystyk  1839:                if (status == 227) {                
                   1840:                    /* If succes, we have to scan for the returned port number.
                   1841:                       However, the format for the response is not standard, so
                   1842:                       the best thing to do is to scan for the first digit
                   1843:                       after the status code, see RFC1123 */
                   1844:                    char *portstr;
                   1845:                    int h0, h1, h2, h3, p0, p1;
1.28      frystyk  1846:                    portstr = ctrl->reply->data+3;
1.23      frystyk  1847:                    while (*portstr && !isdigit(*portstr++));
                   1848:                    if (!*portstr || sscanf(--portstr, "%d,%d,%d,%d,%d,%d",
                   1849:                                            &h0, &h1, &h2, &h3, &p0, &p1)<4) {
                   1850:                        if (TRACE) fprintf(stderr,
                   1851:                            "FTP......... PASV reply has no inet address!\n");
                   1852:                        state = ERROR;
                   1853:                    } else {
                   1854:                        serv_port = (p0<<8)+p1;
                   1855:                        state = NEED_ACTIVE;
                   1856:                    }
                   1857:                } else if (status/100 == 4)
                   1858:                    state = FAILURE;
                   1859:                else if (state == 530)                   /* Not logged in??? */
                   1860:                    state = ERROR;
                   1861:                else
                   1862:                    state = NEED_PASSIVE;      /* If error, try PORT instead */
                   1863:            }
                   1864:            break;
1.22      frystyk  1865: 
1.23      frystyk  1866:          case NEED_ACTIVE:
                   1867:            /* Now get ready for a connect */
                   1868:            if (TRACE) fprintf(stderr,
                   1869:                               "FTP......... Server is listening on port %d\n",
                   1870:                               serv_port);
1.32      frystyk  1871:            
                   1872:            /* Got to strip any host port indication as this is for the control
                   1873:               connection. Thanks to ramey@jello.csc.ti.com (Joe Ramey) */
                   1874:            {
                   1875:                char *host=HTParse(url, "",
                   1876:                                   PARSE_ACCESS+PARSE_HOST+PARSE_PUNCTUATION);
                   1877:                char *portptr = strrchr(host, ':');
                   1878:                if (portptr && strncmp(portptr, "://", 3))
                   1879:                    *portptr = '\0';
1.33      frystyk  1880:                status = HTDoConnect(request, host, serv_port,
1.32      frystyk  1881:                                     &data->socket, (u_long *) NULL);
                   1882:                free(host);
                   1883:            }
1.33      frystyk  1884:            if (status < 0) {
1.23      frystyk  1885:                if (TRACE) fprintf(stderr,
                   1886:                                   "FTP......... Data connection failed using PASV, let's try PORT instead\n");
1.33      frystyk  1887:                HTErrorIgnore(request);      /* Don't generate error message */
1.23      frystyk  1888:                state = NEED_PASSIVE;
                   1889:            } else if (status >= 0) {
                   1890:                if (TRACE) fprintf(stderr, "FTP......... Data connected using PASV, socket %d\n", data->socket);
                   1891:                state = SUCCESS;
                   1892:            } else {
                   1893:                state = ERROR;                                /* Interrupted */
                   1894:            }
                   1895:            break;
1.22      frystyk  1896: 
1.23      frystyk  1897:          case NEED_PASSIVE:
                   1898: #ifdef LISTEN
                   1899:            /* The server didn't accept our PASV so now we try ourselves to be
                   1900:               passive using PORT */
                   1901:            if (get_listen_socket(data) < 0 ||
1.28      frystyk  1902:                HTFTP_send_cmd(ctrl, "PORT", this_addr))
1.23      frystyk  1903:                state = ERROR;
                   1904:            else
                   1905:                state = SENT_PORT;
                   1906: #else
                   1907:            /* If PORT is not compiled, then there is nothing we can do! */
                   1908:            if (TRACE) fprintf(stderr, "FTP......... PORT is not possible!\n");
                   1909:            state = ERROR;
                   1910: #endif
                   1911:            break;
                   1912: 
                   1913:          case SENT_PORT:
1.28      frystyk  1914:            status = HTFTP_get_response(ctrl, &ctrl->reply);
1.23      frystyk  1915:            if (status/100 == 2) {
1.33      frystyk  1916:                data->passive = 1;
1.23      frystyk  1917:                state = SUCCESS;
                   1918:            } else if (status/100 == 4)
                   1919:                state = FAILURE;
                   1920:            else
                   1921:                state = ERROR;
                   1922:            break;
1.1       timbl    1923: 
1.23      frystyk  1924:          case SENT_TYPE:
1.28      frystyk  1925:            status = HTFTP_get_response(ctrl, &ctrl->reply);
1.23      frystyk  1926:            /* If OK, then tell the server to be passive */
                   1927:            if (status/100 == 2) {
1.28      frystyk  1928:                if (!HTFTP_send_cmd(ctrl, "PASV", NULL))
1.23      frystyk  1929:                    state = SENT_PASV;
                   1930:                else
                   1931:                    state = ERROR;
                   1932:            } else if (status/100 == 4)
                   1933:                state = FAILURE;
                   1934:            else
                   1935:                state = ERROR;
                   1936:            break;
                   1937:                        
                   1938:          case ERROR:                         /* Otherwise gcc complains :-( */
                   1939:          case FAILURE:
                   1940:          case SUCCESS:
                   1941:            break;
1.22      frystyk  1942:        }
1.23      frystyk  1943:     }
                   1944:     return state;
                   1945: }
                   1946: 
                   1947: 
1.33      frystyk  1948: #ifdef LISTEN
                   1949: /*                                                       HTFTP_switch_to_port
                   1950: **
                   1951: **     This function changes the current data connection from being PASV to
                   1952: **     PORT. This is to handle servers that doesn't tell if they can handle
                   1953: **     a PASV data connection before very late in the state diagram.
                   1954: **
                   1955: **     Returns -2 on ERROR, -1 on FAILURE, 0 on SUCCESS.
                   1956: */
                   1957: PRIVATE int HTFTP_switch_to_port ARGS2(ftp_data_info *, data,
                   1958:                                       HTRequest *,     req)
                   1959: {
                   1960:     enum _state {
                   1961:        ERROR = -2,
                   1962:        SUCCESS = 0,
                   1963:     } state = ERROR;
                   1964:     int status;
                   1965:     ftp_ctrl_info *ctrl = data->ctrl;
                   1966: 
                   1967:     if (data->passive) {
                   1968:        if (TRACE)
                   1969:            fprintf(stderr, "FTP Switch.. We are already passive, so PORT won't help :-(\n");
                   1970:        return state;
                   1971:     }
                   1972: 
                   1973:     if (TRACE)
                   1974:        fprintf(stderr, "FTP Switch.. Closing PASV data connection number %d, and try to reopen it on the fly using PORT\n",
                   1975:                data->socket);
                   1976:     if ((status = NETCLOSE(data->socket)) < 0) {
                   1977:        HTInetStatus("close data socket");
                   1978:     } else
                   1979:        data->socket = -1;         /* Invalid socket */
                   1980:     
                   1981:     /* Now get new data connection using PORT */
                   1982:     if (status >= 0 && get_listen_socket(data) >= 0 &&
                   1983:        !HTFTP_send_cmd(ctrl, "PORT", this_addr)) {
                   1984:        status = HTFTP_get_response(ctrl,&ctrl->reply);
                   1985:        if (status/100 == 2) {
                   1986:            data->passive = 1;
                   1987:            state = SUCCESS;
                   1988:        }
                   1989:     }
                   1990:     return state;
                   1991: }
                   1992: #endif /* LISTEN */
                   1993: 
                   1994: 
                   1995: /*                                                       HTFTP_look_for_data
                   1996: **
                   1997: **     This function puts up a select on the data connetcion and the control
                   1998: **     connection in order to find out on which one data is transmitted.
                   1999: **
                   2000: **     Returns -1 on ERROR, 0 if data-connection, 1 if control connection.
                   2001: */
                   2002: PRIVATE int HTFTP_look_for_data ARGS2(HTRequest *,     request,
                   2003:                                      ftp_data_info *,  data)
                   2004: {
                   2005:     int status = -1;
                   2006:     fd_set read_socks;
                   2007:     struct timeval max_wait;
                   2008:     ftp_ctrl_info *ctrl = data->ctrl;
                   2009:     int maxfdpl = HTMAX(data->socket, ctrl->socket) + 1;
                   2010:     if (data->socket < 0 || ctrl->socket < 0) {
                   2011:        if (TRACE)
                   2012:            fprintf(stderr, "FTP Select.. Invalid socket\n");
                   2013:        return -1;
                   2014:     }
                   2015: 
                   2016:     /* Initialize the set of sockets */
                   2017:     FD_ZERO(&read_socks);
                   2018:     FD_SET(data->socket, &read_socks);       /* Turn on bit for data socket */
                   2019:     FD_SET(ctrl->socket, &read_socks);    /* Turn on bit for control socket */
                   2020: 
                   2021:     /* Set up timer */
                   2022:     if (HTFTPTimeOut <= 0)
                   2023:        HTFTPTimeOut = FTP_DEFAULT_TIMEOUT;
                   2024:     max_wait.tv_sec = HTFTPTimeOut/100;
                   2025:     max_wait.tv_usec = (HTFTPTimeOut%100)*10000;
                   2026: 
                   2027:     /* This sleep is necessary as data is usually arriving more slow on control
                   2028:        connection than on data connection. Even on an error, the data socket 
                   2029:        might indicate that data is ready, even though they are not :-( */
                   2030:     sleep(1);
                   2031: 
                   2032:     /* Now make the select */
                   2033:     if ((status = select(maxfdpl, &read_socks, (fd_set *) NULL,
                   2034:                         (fd_set *) NULL, &max_wait)) < 0)
                   2035:        HTInetStatus("select");
                   2036:     else if (!status) {
                   2037:        if (TRACE) fprintf(stderr, "FTP Select.. Connections timed out\n");
                   2038:        status = -1;
                   2039:     } else if (status > 1) {
                   2040:        if (TRACE) fprintf(stderr, "FTP Select.. Both data connetion and control data connection has data, let's grab the control\n");
                   2041:        status = 1;
                   2042:     } else if (FD_ISSET(data->socket, &read_socks)) {
                   2043:        if (TRACE)
                   2044:            fprintf(stderr, "FTP Select.. Data connection %d ready\n",
                   2045:                    data->socket);
                   2046:        status = 0;
                   2047:     } else if (FD_ISSET(ctrl->socket, &read_socks)) {
                   2048:        if (TRACE)
                   2049:            fprintf(stderr, "FTP Select.. Control connection %d ready\n",
                   2050:                    ctrl->socket);
                   2051:        status = 1;
                   2052:     } else {
                   2053:        if (TRACE)
                   2054:            fprintf(stderr, "FTP Select.. Unknown socket returned\n");
                   2055:        status = -1;
                   2056:     }
                   2057:     return status;
                   2058: }
                   2059: 
                   2060: 
1.23      frystyk  2061: /*                                                          HTFTPServerInfo()
                   2062: **
                   2063: **     This function finds out what server we are talking to.
                   2064: **     Returns -2 on ERROR, -1 on FAILURE, 0 on SUCCESS.
                   2065: **
                   2066: **     Thanks to James.W.Matthews@Dartmouth.EDU (James W. Matthews) for making
                   2067: **     his code available.
1.1       timbl    2068: */
1.23      frystyk  2069: PRIVATE int HTFTPServerInfo ARGS1(ftp_ctrl_info *, ctrl)
                   2070: {
                   2071:     enum _state {
                   2072:        ERROR  = -2,
                   2073:        FAILURE = -1,
                   2074:        SUCCESS = 0,
                   2075:        BEGIN,
                   2076:        SENT_SYST,
                   2077:        NEED_PWD,
                   2078:        SENT_PWD
                   2079:     } state = BEGIN;
                   2080:     int status;
                   2081: 
                   2082:     /* This loop only stops if state is ERROR, FAILURE or SUCCESS */
                   2083:     while (state > 0) {
                   2084:        switch (state) {
                   2085:          case BEGIN:
                   2086:            if (!HTFTP_send_cmd(ctrl, "SYST", NULL))
                   2087:                state = SENT_SYST;
                   2088:            else
                   2089:                state = ERROR;
                   2090:            break;
                   2091:            
                   2092:          case SENT_SYST:
                   2093:            {
                   2094:                char *info;
1.28      frystyk  2095:                status = HTFTP_get_response(ctrl, &ctrl->reply);
1.23      frystyk  2096:                if (status/100 != 2) {
                   2097:                    state = NEED_PWD;
                   2098:                    break;
                   2099:                }
                   2100: 
                   2101:                /* Got a line - what kind of server are we talking to? */
1.28      frystyk  2102:                info = ctrl->reply->data+3;            /* Skip response code */
1.23      frystyk  2103:                while (*info && *info++ == ' ');
                   2104:                if (!*info) {
                   2105:                    if (TRACE)
                   2106:                        fprintf(stderr, "FTP......... No server info?\n");
                   2107:                    state = NEED_PWD;
                   2108:                    break;
                   2109:                }
                   2110:                --info;
                   2111:                if (strncmp(info, "UNIX Type: L8MAC-OSMachTen", 28) == 0) {
                   2112:                    ctrl->server = MACHTEN_SERVER;
                   2113:                    ctrl->use_list = YES;
                   2114:                    ctrl->unsure_type = NO;
                   2115:                } else if (strstr(info, "UNIX") != NULL) {
                   2116:                    ctrl->server = UNIX_SERVER;
                   2117:                    ctrl->use_list = YES;
                   2118:                    ctrl->unsure_type = NO;
                   2119:                } else if (strncmp(info, "VMS", 3) == 0) {
                   2120:                    ctrl->server = VMS_SERVER;
                   2121:                    ctrl->use_list = YES;
                   2122:                    ctrl->unsure_type = NO;
                   2123:                } else if ((strncmp(info, "VM/CMS", 6) == 0) ||
                   2124:                           (strncmp(info, "VM", 2) == 0)) {
                   2125:                    ctrl->server = CMS_SERVER;
                   2126:                    ctrl->unsure_type = NO;
                   2127:                } else if (strncmp(info, "DCTS", 4) == 0) {
                   2128:                    ctrl->server = DCTS_SERVER;
                   2129:                    ctrl->unsure_type = NO;
                   2130:                } else if (strstr(info, "MAC-OS TCP/ConnectII") != NULL) {
                   2131:                    ctrl->server = TCPC_SERVER;
                   2132:                    /* Check old versions of TCP/C using / in pathnames */
                   2133:                    ctrl->unsure_type = YES;
                   2134:                } else if (strncmp(info, "MACOS Peter's Server", 20) == 0) {
                   2135:                    ctrl->server = PETER_LEWIS_SERVER;
                   2136:                    ctrl->use_list = YES;
                   2137:                    ctrl->unsure_type = NO;
1.32      frystyk  2138:                } else if (strncmp(info, "Windows_NT", 10) == 0) {
                   2139:                    ctrl->server = WINDOWS_NT;
                   2140:                    ctrl->use_list = YES;
                   2141:                    ctrl->unsure_type = NO;
1.23      frystyk  2142:                }
                   2143:                
                   2144:                /* If we are unsure, try PWD to get more information */
                   2145:                if (ctrl->server == UNKNOWN || ctrl->unsure_type == YES)
                   2146:                    state = NEED_PWD;
                   2147:                else
                   2148:                    state = SUCCESS;
1.1       timbl    2149:            }
1.23      frystyk  2150:            break;
                   2151: 
                   2152:          case NEED_PWD:
                   2153:            /* get the working directory (to see what it looks like) */
                   2154:            if (!HTFTP_send_cmd(ctrl, "PWD", NULL))
                   2155:                state = SENT_PWD;
                   2156:            else
                   2157:                state = ERROR;
                   2158:            break;
                   2159: 
                   2160:          case SENT_PWD:
                   2161:            {
1.28      frystyk  2162:                status = HTFTP_get_response(ctrl, &ctrl->reply);
1.23      frystyk  2163:                if (status/100 != 2)
                   2164:                    state = ERROR;
                   2165:                else {
                   2166:                    char *start, *end;
                   2167: 
                   2168:                    /* Now analyze response information between "'s */
1.28      frystyk  2169:                    if ((start = strchr(ctrl->reply->data, '"')) == NULL ||
1.23      frystyk  2170:                        (end = strchr(++start, '"')) == NULL) {
                   2171:                        if (TRACE)
                   2172:                            fprintf(stderr,
                   2173:                                    "FTP......... No current directory?\n");
                   2174:                        ctrl->server = GENERIC_SERVER;
                   2175:                    } else {
                   2176:                        *end = '\0';
                   2177:                        if (ctrl->server == TCPC_SERVER) {
                   2178:                            ctrl->server = *start == '/' ?
                   2179:                                NCSA_SERVER : TCPC_SERVER;
                   2180:                            ctrl->unsure_type = NO;
                   2181:                        } else if (*start == '/') {
                   2182:                            /* path names starting with / imply Unix, right? */
                   2183:                            ctrl->server = UNIX_SERVER;
                   2184:                            ctrl->use_list = YES;
                   2185:                        } else if (*(end-1) == ']') {
                   2186:                            /* path names ending with ] imply VMS, right? */
                   2187:                            ctrl->server = VMS_SERVER;
                   2188:                        } else
                   2189:                            ctrl->server = GENERIC_SERVER;
                   2190:                    }
                   2191:                    state = SUCCESS;
                   2192:                }
                   2193:            }               
                   2194:            break;
                   2195:            
                   2196:          case FAILURE:                       /* Otherwise gcc complains :-( */
                   2197:          case ERROR:
                   2198:          case SUCCESS:
                   2199:            break;
1.1       timbl    2200:        }
1.23      frystyk  2201:     }
                   2202:     if (TRACE) {
                   2203:        static char *servers[] = {
                   2204:            "UNKNOWN",
                   2205:            "GENERIC",
                   2206:            "MACHTEN",
                   2207:            "UNIX",
                   2208:            "VMS",
                   2209:            "CMS",
                   2210:            "DCTS",
                   2211:            "TCPC",
1.32      frystyk  2212:            "PETER LEWIS",
                   2213:            "NCSA",
                   2214:            "WINDOWS NT"
1.23      frystyk  2215:            };
                   2216:        if (ctrl->unsure_type == YES)
                   2217:            fprintf(stderr, "FTP......... This might be a %s server\n",
                   2218:                    *(servers+ctrl->server+1));
                   2219:        else
                   2220:            fprintf(stderr, "FTP......... We are talking to a %s server\n",
                   2221:                    *(servers+ctrl->server+1));
                   2222:     }
                   2223:     return state;
                   2224: }
                   2225: 
                   2226: 
                   2227: /*                                                          HTFTPLocation()
                   2228: **
                   2229: **     This function compares the current directory location in the
                   2230: **     ftp-session to the new URL and returns the relative position. If
                   2231: **     the new URL is at a higher location, the function performs CDUP's
                   2232: **     until this location is reached so that the relative position is '.'
                   2233: **
                   2234: **     Returns relative path name if OK, else current location
                   2235: **
1.1       timbl    2236: */
1.23      frystyk  2237: PRIVATE char *HTFTPLocation ARGS2(ftp_ctrl_info *, ctrl, char *, url)
                   2238: {
                   2239:     unsigned char getup = 0;
                   2240:     char *current;
                   2241:     char *new;
                   2242:     char *relative;
                   2243:     char *result = NULL;
                   2244:     char *strptr;
                   2245: 
                   2246:     /* Make a full URL out of current location */ 
                   2247:     current = HTParse(url, "", PARSE_ACCESS+PARSE_HOST+PARSE_PUNCTUATION);
                   2248:     StrAllocCat(current, "/");
                   2249:     StrAllocCat(current, ctrl->location);
                   2250:     if (*(current+strlen(current)-1) != '/')
                   2251:        StrAllocCat(current, "/");
                   2252: 
                   2253:     /* Make a temporary URL without any type indication */
                   2254:     new = HTParse(url, "", PARSE_ALL);
                   2255:     if ((strptr = strrchr(new, ';')) != NULL)
                   2256:        *strptr = '\0';
                   2257: 
                   2258:     /* Compare those two URLs */
                   2259:     relative = HTRelative(new, current);
                   2260:     {
                   2261:        /* Now send any CDUP if necessary */
                   2262:        char *tail = relative;
                   2263:        int status;
                   2264:        while (tail && (strptr = strstr(tail, "../")) != NULL) {
                   2265:            if (HTFTP_send_cmd(ctrl, "CDUP", NULL)) {
                   2266:                break;
                   2267:            }
1.28      frystyk  2268:            status = HTFTP_get_response(ctrl, &ctrl->reply);
1.23      frystyk  2269:            if (status/100 != 2) {
                   2270:                break;
1.1       timbl    2271:            }
1.23      frystyk  2272:            ++getup;
                   2273:            tail += 3;
                   2274:        }
                   2275:     }
                   2276: 
                   2277:     /* Now update current location if we have used CDUP and make relative 
                   2278:        return value. */
                   2279:     if (getup) {
                   2280:        char *location = HTParse(new, "", PARSE_PATH);
                   2281:        free(ctrl->location);
                   2282:        if (*location == '/')
                   2283:            ctrl->location = ++location;
                   2284:        else
                   2285:            ctrl->location = location;
                   2286:        if (*ctrl->location &&
                   2287:            *(ctrl->location+strlen(ctrl->location)-1) == '/')
                   2288:            *(ctrl->location+strlen(ctrl->location)-1) = '\0';
                   2289:        StrAllocCopy(result, "");
                   2290:        free(relative);
                   2291:     } else {
                   2292:        if (*relative == '/') {
                   2293:            StrAllocCopy(result, relative+1);
                   2294:            free(relative);
                   2295:        } else
                   2296:            result = relative;
1.25      frystyk  2297:        if (*relative && *(relative+strlen(relative)-1) == '/')
1.23      frystyk  2298:            *(relative+strlen(relative)-1) = '\0';
                   2299:     }
                   2300:     if (TRACE)
                   2301:        fprintf(stderr, "FTP......... current location on server: `%s\'\n",
                   2302:                ctrl->location);
                   2303:     free(current);
                   2304:     free(new);
                   2305:     return result;
                   2306: }
1.22      frystyk  2307: 
                   2308: 
1.23      frystyk  2309: /* ------------------------------------------------------------------------- */
                   2310: /*                   FTP Client Functions for receiving data                */
                   2311: /* ------------------------------------------------------------------------- */
1.1       timbl    2312: 
1.23      frystyk  2313: /*                                                             HTFTP_get_dir
                   2314: **
                   2315: **     This function asks for the directory specified and calls
                   2316: **     HTFTPBrowseDirectory. First we try in one go, but if that doesn't
                   2317: **     work, then we use CWD for each segment. The directory is searched
                   2318: **     relative to the login directory, that is without a starting '/'.
                   2319: **
                   2320: **     Returns -2 on ERROR, -1 on FAILURE, 0 on SUCCESS.
1.1       timbl    2321: */
1.33      frystyk  2322: PRIVATE int HTFTP_get_dir ARGS3(ftp_ctrl_info *, ctrl, HTRequest *, req,
                   2323:                                char *, relative)
1.23      frystyk  2324: {
                   2325:     enum _state {
                   2326:        ERROR  = -2,
                   2327:        FAILURE = -1,
                   2328:        SUCCESS = 0,
                   2329:        BEGIN,
                   2330:        NEED_LIST,
                   2331:        SENT_LIST,
                   2332:        SENT_CWD,
                   2333:         MULTIPLE_CWD,
                   2334:        READY_FOR_DATA,
                   2335:        SENT_ABOR,
1.33      frystyk  2336:        WAIT_FOR_CONNECTION,
                   2337:        HANDLE_CTRL,
1.23      frystyk  2338:        GOT_DATA
                   2339:     } state = BEGIN;
1.33      frystyk  2340:     BOOL handle_ctrl = NO;
1.23      frystyk  2341:     ftp_data_info *data = (ftp_data_info *) ctrl->data_cons->next->object; 
                   2342:     int status;
                   2343:     char *unescaped = NULL;
                   2344:     StrAllocCopy(unescaped, relative);
                   2345:     HTUnEscape(unescaped);
1.30      luotonen 2346:     HTCleanTelnetString(unescaped);    /* Prevent security holes */
1.23      frystyk  2347: 
                   2348:     /* This loop only stops if state is ERROR, FAILURE or SUCCESS */
                   2349:     while (state > 0) {
                   2350:        switch (state) {
                   2351:          case BEGIN:
                   2352:            /* Only if the root directory is requested, we can use LIST right
                   2353:               away, else we must first use CWD */
                   2354:            if (!*unescaped || !strcmp(unescaped, "/"))
                   2355:                state = NEED_LIST;
                   2356:            else {
                   2357:                /* We first try to CWD to the location in one go. */
                   2358:                if (!HTFTP_send_cmd(ctrl, "CWD", unescaped))
                   2359:                    state = SENT_CWD;
                   2360:                else
                   2361:                    state = ERROR;
                   2362:            }
                   2363:            break;
                   2364: 
                   2365:          case NEED_LIST:
                   2366:            if (ctrl->use_list == YES) {
                   2367:                if (!HTFTP_send_cmd(ctrl, "LIST", NULL))
                   2368:                    state = SENT_LIST;
                   2369:                else
                   2370:                    state = ERROR;
                   2371:            } else {
                   2372:                if (!HTFTP_send_cmd(ctrl, "NLST", NULL))
                   2373:                    state = SENT_LIST;
                   2374:                else
                   2375:                    state = ERROR;
                   2376:            }
                   2377:            break;
                   2378: 
                   2379:          case SENT_LIST:
1.33      frystyk  2380: #ifdef SEQUENT
                   2381:            
                   2382:            /* If we are listening, do a non-blocking accept now, as the
1.23      frystyk  2383:               accept on some systems (DYNIX) completes the connection. On
                   2384:               BSD systems, the completion is done independently of the
                   2385:               accept. (thanks to Bill Rushka, wcr@aps.org) */
1.33      frystyk  2386:            if (data->passive == 1) {
1.23      frystyk  2387:                int newfd;
1.33      frystyk  2388:                if ((newfd = HTDoAccept(req, data->socket)) >= 0) {
1.23      frystyk  2389: #ifdef REPEAT_LISTEN
                   2390:                    if (TRACE) fprintf(stderr, "FTP......... Passive data channel number %d stays open.\n", data->socket);
                   2391: #else
                   2392:                    if (TRACE) fprintf(stderr, "FTP......... Passive data channel number %d closed.\n", data->socket);
                   2393:                    if (NETCLOSE(data->socket) < 0) {
                   2394:                        HTInetStatus("close");
                   2395:                        state = ERROR;
                   2396:                        break;
                   2397:                    }
1.22      frystyk  2398: #endif
1.23      frystyk  2399:                    data->socket = newfd;        /* Switch to new socket */
1.33      frystyk  2400:                    data->passive = 2;
1.23      frystyk  2401:                    if (TRACE)
                   2402:                        fprintf(stderr, "FTP......... New data socket: %d\n",
                   2403:                                data->socket);
                   2404:                } else {
1.31      frystyk  2405:                    HTChunkClear(ctrl->reply);
1.33      frystyk  2406:                    ctrl->reply = NULL;
1.23      frystyk  2407:                    state = ERROR;
                   2408:                    break;
                   2409:                }
1.1       timbl    2410:            }
1.33      frystyk  2411: #endif /* SEQUENT */
                   2412: 
1.28      frystyk  2413:            status = HTFTP_get_response(ctrl, &ctrl->reply);
1.33      frystyk  2414:            if (status == 150)                   /* About to open connection */
                   2415:                state = WAIT_FOR_CONNECTION;
                   2416:            else if (status == 125)                     /* Transfer starting */
1.23      frystyk  2417:                state = READY_FOR_DATA;
                   2418:            else if (status/100 == 4)
                   2419:                state = FAILURE;
                   2420:            else
                   2421:                state = ERROR;
                   2422:            break;
1.1       timbl    2423:            
1.23      frystyk  2424:          case SENT_CWD:
1.28      frystyk  2425:            status = HTFTP_get_response(ctrl, &ctrl->reply);
1.23      frystyk  2426:            if (status/100 == 2) {
                   2427:                /* Update current location */
                   2428:                if (*ctrl->location)
                   2429:                    StrAllocCat(ctrl->location, "/");
                   2430:                StrAllocCat(ctrl->location, relative);
1.28      frystyk  2431:                HTChunkClear(ctrl->welcome);
                   2432:                HTFTPAddWelcome(ctrl);
1.23      frystyk  2433:                state = NEED_LIST;
                   2434:            } else if (status/100 == 4)
                   2435:                state = FAILURE;
                   2436:            else
                   2437:                state = MULTIPLE_CWD;
                   2438:            break;
1.1       timbl    2439: 
1.23      frystyk  2440:          case MULTIPLE_CWD:
                   2441:            /* We must use the escaped version when looking for '/' as
                   2442:               delimiter between segments, and then unescape each segment */
                   2443:            if (TRACE) fprintf(stderr, "FTP......... Can't jump directly to location, try multiple CD's instead\n");
                   2444:            state = NEED_LIST;               /* This is overwritten if error */
                   2445:            {
                   2446:                char *path = NULL;
                   2447:                char *segment;
                   2448:                StrAllocCopy(path, relative);
                   2449:                segment = strtok(path, "/");
                   2450:                while (segment && *segment) {
                   2451:                    HTUnEscape(segment);
1.30      luotonen 2452:                    HTCleanTelnetString(segment);  /* Prevent security holes */
1.23      frystyk  2453:                    if (HTFTP_send_cmd(ctrl, "CWD", segment)) {
                   2454:                        state = ERROR;
                   2455:                        break;
                   2456:                    }
1.28      frystyk  2457:                    status = HTFTP_get_response(ctrl, &ctrl->reply);
1.23      frystyk  2458:                    if (status/100 != 2) {
                   2459:                        if (status/100 == 4)
                   2460:                            state = FAILURE;
                   2461:                        else
                   2462:                            state = ERROR;
                   2463:                        break;
                   2464:                    } else {                      /* Update current location */
                   2465:                        char *new_seg = HTEscape(segment, URL_XPALPHAS);
                   2466:                        if (*ctrl->location)
                   2467:                            StrAllocCat(ctrl->location, "/");
                   2468:                        StrAllocCat(ctrl->location, new_seg);
                   2469:                        free(new_seg);
1.28      frystyk  2470:                        HTChunkClear(ctrl->welcome);
                   2471:                        HTFTPAddWelcome(ctrl);
1.23      frystyk  2472:                    }
                   2473:                    segment = strtok(NULL, "/");           /* Get next token */
1.22      frystyk  2474:                }
1.23      frystyk  2475:                free(path);
1.22      frystyk  2476:            }
1.23      frystyk  2477:            break;
1.22      frystyk  2478: 
1.23      frystyk  2479:          case READY_FOR_DATA:
1.33      frystyk  2480:            if (data->passive == 1) {
                   2481:                int newfd;
                   2482:                if ((newfd = HTDoAccept(req, data->socket)) >= 0) {
                   2483: #ifdef REPEAT_LISTEN
                   2484:                    if (TRACE) fprintf(stderr, "FTP......... Passive data channel number %d stays open.\n", data->socket);
                   2485: #else
                   2486:                    if (TRACE) fprintf(stderr, "FTP......... Passive data channel number %d closed.\n", data->socket);
                   2487:                    if (NETCLOSE(data->socket) < 0) {
                   2488:                        HTInetStatus("close");
                   2489:                        state = ERROR;
                   2490:                        break;
                   2491:                    }
                   2492: #endif
                   2493:                    data->socket = newfd;            /* Switch to new socket */
                   2494:                    data->passive = 2;
                   2495:                    if (TRACE)
                   2496:                        fprintf(stderr, "FTP......... New data socket: %d\n",
                   2497:                                data->socket);
                   2498:                } else {
                   2499:                    HTChunkClear(ctrl->reply);
                   2500:                    ctrl->reply = NULL;
                   2501:                    state = ERROR;
                   2502:                    break;
                   2503:                }
                   2504:            }
                   2505: 
1.23      frystyk  2506:            /* Now, the browsing module can be called */
                   2507:            {
                   2508:                char *url = HTAnchor_physical(req->anchor);
                   2509:                char *path = HTParse(url, "", PARSE_PATH+PARSE_PUNCTUATION);
                   2510:                HTUnEscape(path);
                   2511:                if (TRACE)
                   2512:                    fprintf(stderr, "FTP......... Receiving directory `%s\'\n",
                   2513:                            path);
                   2514:                status = HTFTPBrowseDirectory(req, path, data,
                   2515:                                              HTFTP_get_dir_string);
                   2516:                if (status == -1)
                   2517:                    state = ERROR;
                   2518:                else if (status == HT_INTERRUPTED) {
                   2519:                    if (!HTFTP_send_cmd(ctrl, "ABOR", NULL))
                   2520:                        state = SENT_ABOR;
                   2521:                    else
                   2522:                        state = ERROR;
                   2523:                } else
                   2524:                    state = GOT_DATA;
                   2525:                free(path);
1.22      frystyk  2526:            }
1.23      frystyk  2527:            break;
                   2528: 
                   2529:          case GOT_DATA:
1.33      frystyk  2530:            if (!handle_ctrl) {
                   2531:                status = HTFTP_get_response(ctrl, &ctrl->reply);
                   2532:                if (status/100 == 2)
                   2533:                    state = SUCCESS;                       /* Directory read */
                   2534:                else if (status/100 == 4)
                   2535:                    state = FAILURE;
                   2536:                else
                   2537:                    state = ERROR;
                   2538:            } else
                   2539:                state = SUCCESS;
                   2540:            break;
                   2541: 
                   2542:          case SENT_ABOR:
1.28      frystyk  2543:            status = HTFTP_get_response(ctrl, &ctrl->reply);
1.23      frystyk  2544:            if (status/100 == 2)
1.33      frystyk  2545:                state = SUCCESS;
1.23      frystyk  2546:            else if (status/100 == 4)
                   2547:                state = FAILURE;
                   2548:            else
                   2549:                state = ERROR;
                   2550:            break;
                   2551: 
1.33      frystyk  2552:          case WAIT_FOR_CONNECTION:
                   2553:            /* Now we have to wait to see whether the FTP-server sends on the
                   2554:               data port or the control port */
                   2555:            status = HTFTP_look_for_data(req, data);
                   2556:            if (!status)
                   2557:                state = READY_FOR_DATA;
                   2558:            else
                   2559:                state = HANDLE_CTRL;
                   2560:            break;
                   2561: 
                   2562:          case HANDLE_CTRL:
1.28      frystyk  2563:            status = HTFTP_get_response(ctrl, &ctrl->reply);
1.33      frystyk  2564:            if (status/100 == 2) {
                   2565:                state = READY_FOR_DATA;
                   2566: #ifdef LISTEN
                   2567:            } else if (status == 425) {    /* Connection could not be opened */
                   2568:                if (HTFTP_switch_to_port(data, req))
                   2569:                    state = ERROR;
                   2570:                else
                   2571:                    state = NEED_LIST;
                   2572: #endif /* LISTEN */
                   2573:            } else if (status/100 == 4)
1.23      frystyk  2574:                state = FAILURE;
1.22      frystyk  2575:            else
1.23      frystyk  2576:                state = ERROR;
1.33      frystyk  2577:            handle_ctrl = YES;
1.23      frystyk  2578:            break;
                   2579: 
                   2580:          case FAILURE:                       /* Otherwise gcc complains :-( */
                   2581:          case ERROR:
                   2582:          case SUCCESS:
                   2583:            break;
1.22      frystyk  2584:        }
1.23      frystyk  2585:     }
                   2586:     FREE(unescaped);
                   2587:     return state;
                   2588: }
                   2589: 
                   2590: 
                   2591: /*                                                             HTFTP_get_file
                   2592: **
                   2593: **     This function asks for the file specified. First we try in one go,
                   2594: **     but if that doesn't work, then we use CWD for each segment and then
                   2595: **     try to retrieve it. If that also fails, then we try if it is a
                   2596: **     directory. This procedure causes that directory links generated in
                   2597: **     HTFTPBrowseDirectory should have a '/' at the end in order to go
                   2598: **     directly to HTFTP_get_dir. The relative is searched relative to
                   2599: **     the login directory, that is without a starting '/'.
                   2600: **
                   2601: **     Returns -2 on ERROR, -1 on FAILURE, 0 on SUCCESS.
                   2602: */
1.33      frystyk  2603: PRIVATE int HTFTP_get_file ARGS3(ftp_ctrl_info *, ctrl, HTRequest *, req,
                   2604:                                 char *, relative)
1.23      frystyk  2605: {
                   2606:     enum _state {
                   2607:        ERROR  = -2,
                   2608:        FAILURE = -1,
                   2609:        SUCCESS = 0,
                   2610:        BEGIN,
                   2611:        SENT_RETR,
                   2612:         MULTIPLE_CWD,
                   2613:        READY_FOR_DATA,
                   2614:        SENT_ABOR,
1.33      frystyk  2615:        WAIT_FOR_CONNECTION,
                   2616:        HANDLE_CTRL,
1.23      frystyk  2617:        GOT_DATA
                   2618:     } state = BEGIN;
1.33      frystyk  2619:     BOOL handle_ctrl = NO;
1.23      frystyk  2620:     BOOL multiple = NO;                      /* Have we already tried multiple CWD? */
                   2621:     ftp_data_info *data = (ftp_data_info *) ctrl->data_cons->next->object; 
                   2622:     int status;
                   2623:     char *unescaped = NULL;
                   2624:     StrAllocCopy(unescaped, relative);
                   2625:     HTUnEscape(unescaped);
1.30      luotonen 2626:     HTCleanTelnetString(unescaped);  /* Prevent security holes */
1.23      frystyk  2627: 
1.33      frystyk  2628:     /* This loop only stops if state is ERROR, FAILURE or SUCCESS */
1.23      frystyk  2629:     while (state > 0) {
                   2630:        switch (state) {
                   2631:          case BEGIN:
                   2632:            /* First we try to retrieve the file in one go. */
                   2633:            if (!HTFTP_send_cmd(ctrl, "RETR", unescaped))
                   2634:                state = SENT_RETR;
                   2635:            else
                   2636:                state = ERROR;
                   2637:            break;
                   2638: 
                   2639:          case SENT_RETR:
1.33      frystyk  2640: #ifdef SEQUENT     
                   2641:            /* If we are listening, do a non-blocking accept now, as the
1.23      frystyk  2642:               accept on some systems (DYNIX) completes the connection. On
                   2643:               BSD systems, the completion is done independently of the
                   2644:               accept. (thanks to Bill Rushka, wcr@aps.org) */
1.33      frystyk  2645:            if (data->passive == 1) {
1.23      frystyk  2646:                int newfd;
1.33      frystyk  2647:                if ((newfd = HTDoAccept(req, data->socket)) >= 0) {
1.23      frystyk  2648: #ifdef REPEAT_LISTEN
                   2649:                    if (TRACE) fprintf(stderr, "FTP......... Passive data channel number %d stays open.\n", data->socket);
                   2650: #else
                   2651:                    if (TRACE) fprintf(stderr, "FTP......... Passive data channel number %d closed.\n", data->socket);
                   2652:                    if (NETCLOSE(data->socket) < 0) {
                   2653:                        HTInetStatus("close");
                   2654:                        state = ERROR;
                   2655:                        break;
                   2656:                    }
1.22      frystyk  2657: #endif
1.33      frystyk  2658:                    data->socket = newfd;            /* Switch to new socket */
                   2659:                    data->passive = 2;
1.23      frystyk  2660:                    if (TRACE)
                   2661:                        fprintf(stderr, "FTP......... New data socket: %d\n",
                   2662:                                data->socket);
                   2663:                } else {
1.31      frystyk  2664:                    HTChunkClear(ctrl->reply);
1.33      frystyk  2665:                    ctrl->reply = NULL;
1.23      frystyk  2666:                    state = ERROR;
                   2667:                    break;
                   2668:                }
                   2669:            }
1.33      frystyk  2670: #endif /* SEQUENT */
                   2671: 
1.28      frystyk  2672:            status = HTFTP_get_response(ctrl, &ctrl->reply);
1.33      frystyk  2673:            if (status == 150)                   /* About to open connection */
                   2674:                state = WAIT_FOR_CONNECTION;
                   2675:            else if (status == 125)
                   2676:                state = READY_FOR_DATA;                 /* Transfer starting */
1.23      frystyk  2677:            else if (status/100 == 4)
                   2678:                state = FAILURE;
1.33      frystyk  2679: 
1.23      frystyk  2680:            /* If there is no '/' in unescaped, it won't help to try
                   2681:               multiple CWD's, as it either doesn't exist or is a directory */
                   2682:            else if (multiple == NO && strchr(unescaped, '/') != NULL)
                   2683:                state = MULTIPLE_CWD;
                   2684:            else {
                   2685:                data->directory = YES;
                   2686:                state = FAILURE;
                   2687:            }
                   2688:            break;
                   2689:            
                   2690:          case MULTIPLE_CWD:
                   2691:            /* We must use the escaped version when looking for '/' as
                   2692:               delimiter between segments, and then unescape each segment */
                   2693:            if (TRACE) fprintf(stderr, "FTP......... Can't jump directly to location, try multiple CD's instead\n");
                   2694:            multiple = YES;
                   2695:            {
                   2696:                char *path = NULL;
                   2697:                char *segment;
                   2698:                char *last_slash;       /* Used to identify the last segment */
                   2699:                StrAllocCopy(path, relative);
                   2700:                if ((last_slash = strrchr(path, '/')) == NULL)
                   2701:                    last_slash = path;
                   2702:                else
                   2703:                    last_slash++;
                   2704:                segment = strtok(path, "/");
                   2705:                while (segment && *segment && segment != last_slash) {
                   2706:                    HTUnEscape(segment);
1.30      luotonen 2707:                    HTCleanTelnetString(segment);  /* Prevent security holes */
1.23      frystyk  2708:                    if (HTFTP_send_cmd(ctrl, "CWD", segment)) {
                   2709:                        state = ERROR;
                   2710:                        break;
                   2711:                    }
1.28      frystyk  2712:                    status = HTFTP_get_response(ctrl, &ctrl->reply);
1.23      frystyk  2713:                    if (status/100 != 2) {
                   2714:                        if (status/100 == 4)
                   2715:                            state = FAILURE;
                   2716:                        else
                   2717:                            state = ERROR;
                   2718:                        break;
                   2719:                    } else {                      /* Update current location */
                   2720:                        char *new_seg = HTEscape(segment, URL_XPALPHAS);
                   2721:                        if (*ctrl->location)
                   2722:                            StrAllocCat(ctrl->location, "/");
                   2723:                        StrAllocCat(ctrl->location, new_seg);
                   2724:                        free(new_seg);
                   2725:                    }
                   2726:                    segment = strtok(NULL, "/");           /* Get next token */
                   2727:                }
                   2728:                /* Now try to retrieve the last segment */
                   2729:                if (segment == last_slash) {
                   2730:                    HTUnEscape(segment);
1.30      luotonen 2731:                    HTCleanTelnetString(segment);  /* Prevent security holes */
1.33      frystyk  2732:                    if (!HTFTP_send_cmd(ctrl, "RETR", segment)) {
                   2733:                        StrAllocCopy(unescaped, segment);
1.23      frystyk  2734:                        state = SENT_RETR;
1.33      frystyk  2735:                    } else
1.23      frystyk  2736:                        state = ERROR;
                   2737:                } else {
                   2738:                    if (TRACE) fprintf(stderr, "FTP......... Strange error, filename not found?\n");
                   2739:                    state = ERROR;
                   2740:                }
                   2741:                free(path);
                   2742:            }
                   2743:            break;
                   2744: 
                   2745:          case READY_FOR_DATA:
1.33      frystyk  2746:            if (data->passive == 1) {
                   2747:                int newfd;
                   2748:                if ((newfd = HTDoAccept(req, data->socket)) >= 0) {
                   2749: #ifdef REPEAT_LISTEN
                   2750:                    if (TRACE) fprintf(stderr, "FTP......... Passive data channel number %d stays open.\n", data->socket);
                   2751: #else
                   2752:                    if (TRACE) fprintf(stderr, "FTP......... Passive data channel number %d closed.\n", data->socket);
                   2753:                    if (NETCLOSE(data->socket) < 0) {
                   2754:                        HTInetStatus("close");
                   2755:                        state = ERROR;
                   2756:                        break;
                   2757:                    }
                   2758: #endif
                   2759:                    data->socket = newfd;        /* Switch to new socket */
                   2760:                    data->passive = 2;
                   2761:                    if (TRACE)
                   2762:                        fprintf(stderr, "FTP......... New data socket: %d\n",
                   2763:                                data->socket);
                   2764:                } else {
                   2765:                    HTChunkClear(ctrl->reply);
                   2766:                    ctrl->reply = NULL;
                   2767:                    state = ERROR;
                   2768:                    break;
                   2769:                }
                   2770:            }
                   2771: 
1.23      frystyk  2772:            /* Now, the browsing module can be called */
                   2773:            if (TRACE) fprintf(stderr, "FTP......... Receiving file `%s\'\n",
                   2774:                               unescaped);
                   2775:            status = HTParseSocket(data->fileformat, data->socket, req);
                   2776:            if (status != HT_LOADED) {
                   2777:                if (status == HT_INTERRUPTED) {
                   2778:                    if (!HTFTP_send_cmd(ctrl, "ABOR", NULL))
                   2779:                        state = SENT_ABOR;
                   2780:                    else
                   2781:                        state = ERROR;
                   2782:                } else
                   2783:                    state = ERROR;
                   2784:            } else
                   2785:                state = GOT_DATA;
                   2786:            break;
                   2787: 
                   2788:          case GOT_DATA:
1.33      frystyk  2789:            if (!handle_ctrl) {
                   2790:                status = HTFTP_get_response(ctrl, &ctrl->reply);
                   2791:                if (status/100 == 2)
                   2792:                    state = SUCCESS;                            /* File read */
                   2793:                else if (status/100 == 4)
                   2794:                    state = FAILURE;
                   2795:                else
                   2796:                    state = ERROR;
                   2797:            } else
                   2798:                state = SUCCESS;
1.23      frystyk  2799:            break;
1.22      frystyk  2800: 
1.23      frystyk  2801:          case SENT_ABOR:
1.28      frystyk  2802:            status = HTFTP_get_response(ctrl, &ctrl->reply);
1.23      frystyk  2803:            if (status/100 == 2)
                   2804:                state = SUCCESS;
                   2805:            else if (status/100 == 4)
                   2806:                state = FAILURE;
1.22      frystyk  2807:            else
1.23      frystyk  2808:                state = ERROR;
                   2809:            break;
                   2810: 
1.33      frystyk  2811:           case WAIT_FOR_CONNECTION:
                   2812:             /* Now we have to wait to see whether the FTP-server sends on the
                   2813:                data port or the control port */
                   2814:             status = HTFTP_look_for_data(req, data);
                   2815:             if (!status)
                   2816:                 state = READY_FOR_DATA;
                   2817:             else
                   2818:                 state = HANDLE_CTRL;
                   2819:             break;
                   2820: 
                   2821:           case HANDLE_CTRL:
                   2822:             status = HTFTP_get_response(ctrl, &ctrl->reply);
                   2823:             if (status/100 == 2) {
                   2824:                 state = READY_FOR_DATA;
                   2825: #ifdef LISTEN
                   2826:             } else if (status == 425) {    /* Connection could not be opened */
                   2827:                 if (HTFTP_switch_to_port(data, req))
                   2828:                     state = ERROR;
                   2829:                 else {
                   2830:                    if (!HTFTP_send_cmd(ctrl, "RETR", unescaped))
                   2831:                        state = SENT_RETR;
                   2832:                    else
                   2833:                        state = ERROR;
                   2834:                }
                   2835: #endif /* LISTEN */
                   2836:             } else if (status/100 == 4)
                   2837:                 state = FAILURE;
                   2838:             else
                   2839:                 state = ERROR;
                   2840:             handle_ctrl = YES;
                   2841:             break;
                   2842: 
1.23      frystyk  2843:          case FAILURE:                       /* Otherwise gcc complains :-( */
                   2844:          case ERROR:
                   2845:          case SUCCESS:
                   2846:            break;
1.1       timbl    2847:        }
                   2848:     }
1.23      frystyk  2849:     FREE(unescaped);
                   2850:     return state;
                   2851: }
1.1       timbl    2852: 
1.23      frystyk  2853: 
                   2854: /* ------------------------------------------------------------------------- */
                   2855: /*                            PUBLIC FTP functions                          */
                   2856: /* ------------------------------------------------------------------------- */
                   2857: 
                   2858: /*                                                        HTFTP_enable_session
                   2859: **
                   2860: **     This function makes it possible to reuse the same control connections
                   2861: **     until they are either timed out by the server, or that the session
1.31      frystyk  2862: **     is closed by HTFTP_end_session. Note that HTLoadFTP can run
1.23      frystyk  2863: **     independently of start and end session, and then each load runs like
                   2864: **     an atomic action.
                   2865: */
                   2866: PUBLIC void HTFTP_enable_session NOARGS
                   2867: {
                   2868:     if (session) {
                   2869:        if (TRACE)
                   2870:            fprintf(stderr, "FTP......... Session is already enabled?\n");
                   2871:        return;
                   2872:     }
                   2873:     session = HTList_new();
                   2874: }
                   2875: 
                   2876: 
                   2877: /*                                                       HTFTP_disable_session
                   2878: **
                   2879: **     This function is the closing function for HTFTP_enable_session.
                   2880: **
                   2881: **     Returns YES if OK, else NO
1.1       timbl    2882: */
1.23      frystyk  2883: PUBLIC BOOL HTFTP_disable_session NOARGS
                   2884: {
                   2885:     BOOL status = YES;
                   2886:     if (!session) {
                   2887:        if (TRACE)
                   2888:            fprintf(stderr, "FTP......... No session to disable?\n");
                   2889:        return NO;
                   2890:     }
1.1       timbl    2891:     {
1.23      frystyk  2892:        HTList *cur = session;
                   2893:        ftp_ctrl_info *pres;
                   2894:        while ((pres = (ftp_ctrl_info *) HTList_nextObject(cur))) {
                   2895:            if (HTFTP_close_ctrl_con(pres))
                   2896:                status = NO;
                   2897:        }
                   2898:        HTList_delete(session);
1.1       timbl    2899:     }
1.23      frystyk  2900:     session = NULL;
                   2901:     return status;
                   2902: }
                   2903: 
                   2904: 
                   2905: /*     Retrieve File from Server as an atomic action. 
                   2906: **     -----------------------------------------------
                   2907: **
                   2908: ** On entry,
                   2909: **     request         This is the request structure
                   2910: ** On exit,
                   2911: **     returns         <0              Error has occured
                   2912: **                     HT_LOADED       OK
                   2913: */
1.31      frystyk  2914: PUBLIC int HTLoadFTP ARGS1(HTRequest *, request)
1.23      frystyk  2915: {
                   2916:     char *url = HTAnchor_physical(request->anchor);
                   2917:     int status = -1;
1.33      frystyk  2918:     int retry;                                     /* How many times tried? */
1.23      frystyk  2919:     ftp_ctrl_info *ctrl;
                   2920: 
1.33      frystyk  2921:     if (!request || !url || !*url) {
                   2922:        if (TRACE) fprintf(stderr, "HTLoadFTP... Bad argument\n");
                   2923:        return -1;
                   2924:     }
                   2925: 
1.23      frystyk  2926:     /* Initiate a (possibly already exsisting) control connection and a
                   2927:        corresponding data connection */
                   2928:     HTSimplify(url);
1.29      frystyk  2929:     if((ctrl = HTFTP_init_con(request, url)) == NULL) {
1.33      frystyk  2930: #ifdef OLD_CODE
1.25      frystyk  2931:        HTLoadError(request, 500,
1.32      frystyk  2932:                    "Could not establish connection to FTP-server");
1.33      frystyk  2933: #endif /* OLD_CODE */
                   2934:        goto endfunc;
1.25      frystyk  2935:     }
1.23      frystyk  2936: 
                   2937:     /* Only if the control connection is in IDLE state, a new
                   2938:        transfer can be started. The control connection can be in another
                   2939:        mode if (session), and then the request is getting queued in
                   2940:        ctrl->data_cons. */
1.31      frystyk  2941:     if (ctrl->state == FTP_IDLE || (session && ctrl->state == FTP_LOGGED_IN)) {
1.23      frystyk  2942:        ftp_data_info *data = ctrl->data_cons->next->object;
1.31      frystyk  2943:        if (ctrl->state == FTP_IDLE)
                   2944:            ctrl->state = FTP_BEGIN;
                   2945:        while (ctrl->state != FTP_IDLE) {               /* Do until finished */
1.23      frystyk  2946:            switch (ctrl->state) {
1.31      frystyk  2947:              case FTP_BEGIN:
1.23      frystyk  2948:                if (!HTFTP_login(ctrl))
1.31      frystyk  2949:                    ctrl->state = FTP_LOGGED_IN;
1.23      frystyk  2950:                else
1.31      frystyk  2951:                    ctrl->state = FTP_ERROR;
1.23      frystyk  2952:                break;
                   2953: 
1.31      frystyk  2954:              case FTP_LOGGED_IN:
1.33      frystyk  2955:                if (!HTFTP_get_data_con(request, data, url))
1.31      frystyk  2956:                    ctrl->state = FTP_GOT_DATA_CON;
1.23      frystyk  2957:                else
1.31      frystyk  2958:                    ctrl->state = FTP_ERROR;
1.23      frystyk  2959:                break;
                   2960: 
1.31      frystyk  2961:              case FTP_GOT_DATA_CON:
1.23      frystyk  2962:                {
                   2963:                    /* Now we must ask for the URL requested. If FAILURE, then
                   2964:                       we try twice to see, if it helps */
1.33      frystyk  2965:                    char *rel = NULL;
1.23      frystyk  2966:                    for (retry=0; retry<2; retry++) {
                   2967:                        if ((rel = HTFTPLocation(ctrl, url)) == NULL) {
1.31      frystyk  2968:                            ctrl->state = FTP_ERROR;
1.23      frystyk  2969:                            break;
                   2970:                        }
1.33      frystyk  2971:                        if (retry == 1 && TRACE)
                   2972:                            fprintf(stderr,
                   2973:                                    "FTP......... First attempt to get URL failed, let's try again\n");
1.23      frystyk  2974: 
                   2975:                        if (data->directory == YES) {
                   2976:                            /* If we haven't already got server-info */
                   2977:                            if (ctrl->server == UNKNOWN) {
                   2978:                                if (HTFTPServerInfo(ctrl)) {
1.31      frystyk  2979:                                    ctrl->state = FTP_ERROR;
1.23      frystyk  2980:                                    break;
                   2981:                                }
                   2982:                            }
1.33      frystyk  2983:                            status = HTFTP_get_dir(ctrl, request, rel);
1.23      frystyk  2984:                        }
                   2985:                        else
1.33      frystyk  2986:                            status = HTFTP_get_file(ctrl, request, rel);
1.23      frystyk  2987:                        if (!status) {
1.31      frystyk  2988:                            ctrl->state = FTP_GOT_DATA;
1.23      frystyk  2989:                            break;
                   2990:                        } else if (status == -2) {                  /* Error */
1.31      frystyk  2991:                            ctrl->state = FTP_ERROR;
1.23      frystyk  2992:                            break;
                   2993:                        } else {
1.33      frystyk  2994:                            FREE(rel);
1.31      frystyk  2995:                            ctrl->state = FTP_FAILURE;          /* Try twice */
1.23      frystyk  2996:                        }
                   2997:                    }
1.33      frystyk  2998:                    FREE(rel);
1.23      frystyk  2999:                }
1.31      frystyk  3000:                if (retry == 2 && ctrl->state == FTP_FAILURE)
                   3001:                    ctrl->state = FTP_ERROR;
1.23      frystyk  3002:                break;
                   3003: 
1.31      frystyk  3004:              case FTP_GOT_DATA:
1.23      frystyk  3005:                if (HTFTP_close_data_con(data))
1.31      frystyk  3006:                    ctrl->state = FTP_ERROR;
1.23      frystyk  3007:                else {
                   3008:                    HTList_removeLastObject(ctrl->data_cons);
                   3009:                    if (!session) {
                   3010:                        if (!HTFTP_logout(ctrl)) {
1.31      frystyk  3011:                            ctrl->state = FTP_IDLE;
1.23      frystyk  3012:                            status = HT_LOADED;
                   3013:                        } else
1.31      frystyk  3014:                            ctrl->state = FTP_ERROR;
1.23      frystyk  3015:                    } else {
1.31      frystyk  3016:                        ctrl->state = FTP_LOGGED_IN; /*Ready for next request*/
1.23      frystyk  3017:                        return HT_LOADED;
                   3018:                    }
                   3019:                    break;
                   3020:                }
                   3021:                break;
                   3022: 
1.31      frystyk  3023:              case FTP_ERROR:
1.30      luotonen 3024:                {
1.32      frystyk  3025:                    if (ctrl->reply && ctrl->reply->data) {
1.33      frystyk  3026:                        HTFTPParseError(&ctrl->reply);
                   3027:                        HTErrorAdd(request, ERR_FATAL, NO, HTERR_FTP_SERVER,
                   3028:                                   (void *) ctrl->reply->data,
                   3029:                                   ctrl->reply->size-1, "HTLoadFTP");
1.32      frystyk  3030:                    } else {
1.33      frystyk  3031:                        char *hoststr = HTParse(url, "", PARSE_HOST);
                   3032:                        HTUnEscape(hoststr);
                   3033:                        HTErrorAdd(request, ERR_FATAL, NO,
                   3034:                                   HTERR_FTP_NO_RESPONSE,
                   3035:                                   (void *) hoststr, strlen(hoststr),
                   3036:                                   "HTLoadFTP");
                   3037:                        free(hoststr);
1.32      frystyk  3038:                    }
1.30      luotonen 3039:                    HTFTP_abort_ctrl_con(ctrl);
1.33      frystyk  3040:                    status = -1;
                   3041:                    goto endfunc;
1.30      luotonen 3042:                }
1.23      frystyk  3043:                break;
                   3044: 
                   3045:              default:
                   3046:                if (TRACE) fprintf(stderr, "FTP......... Unknown state, what happened?\n");
                   3047:                break;
                   3048:            }
                   3049:        }
1.22      frystyk  3050: 
1.23      frystyk  3051:        /* The control connection is only closed if the load is atomic */
                   3052:        if (!session && HTFTP_close_ctrl_con(ctrl))
                   3053:            status = -1;
                   3054:     }
1.33      frystyk  3055: 
                   3056:   endfunc:
1.23      frystyk  3057:     if (status < 0 && status != HT_INTERRUPTED)
1.33      frystyk  3058:        HTErrorAdd(request, ERR_FATAL, NO, HTERR_INTERNAL, NULL, 0, 
                   3059:                   "HTLoadFTP");
                   3060: #ifdef OLD_CODE
1.23      frystyk  3061:        return HTLoadError(request, 500,
1.32      frystyk  3062:               "Document not loaded due to strange behavior from FTP-server");
1.33      frystyk  3063: #endif
                   3064:     if (request->error_stack)
                   3065:        HTErrorMsg(request);
1.23      frystyk  3066:     return status;
                   3067: }
1.31      frystyk  3068: 
                   3069: /* Protocol descriptors */
                   3070: GLOBALDEF PUBLIC HTProtocol HTFTP  = { "ftp", HTLoadFTP, 0 , 0 };
1.22      frystyk  3071: 
1.23      frystyk  3072: /* END OF MODULE */
1.22      frystyk  3073: 
                   3074: 

Webmaster