📄 ftpdlib.c
字号:
ftpdDebugMsg ("accessing %s\n", (int)pFileName,0,0,0); if( ftpPathAccessVerify(pSlot, pFileName, O_RDONLY) == ERROR ) { if (ftpdCmdSend (pSlot, sock, 550, messages [MSG_GUEST_ACCESS], 0, 0, 0, 0, 0, 0) == ERROR) goto connectionLost; continue; } if(stat(pFileName, &fileStat) == ERROR ) { if (ftpdCmdSend (pSlot, sock, 550, messages [MSG_FILE_ERROR], (int)(&pBuf[5]), 0, 0, 0, 0, 0) == ERROR) goto connectionLost; continue; } if( ! S_ISREG (fileStat.st_mode) ) { if (ftpdCmdSend (pSlot, sock, 550, messages [MSG_FILE_NOTREG], (int)(&pBuf[5]), 0, 0, 0, 0, 0) == ERROR) goto connectionLost; continue; } /* decide what to do with the file according to the actual cmd */ if (strncmp (pBuf, "RETR", 4) == 0) { /* ship it away */ ftpdDataStreamSend (pSlot, pFileName); } else if (strncmp (pBuf, "SIZE", 4) == 0) { if (ftpdCmdSend (pSlot, sock, 213, "%lu", fileStat.st_size, 0, 0, 0, 0, 0) == ERROR) goto connectionLost; } else if (strncmp (pBuf, "MDTM", 4) == 0) { struct tm fileDate; localtime_r (&fileStat.st_mtime, &fileDate); if (ftpdCmdSend (pSlot, sock, 213, "%04d%02d%02d%02d%02d%02d", fileDate.tm_year+1900, fileDate.tm_mon, fileDate.tm_mday, fileDate.tm_hour, fileDate.tm_min, fileDate.tm_sec ) == ERROR) goto connectionLost; } } else if (strncmp (pBuf, "STOR", 4) == 0) { STATUS retVal ; /* store a file */ ftpPathNormalize ( pSlot, &pBuf[5], newPath, &pFileName ); ftpdDebugMsg ("STOR %s\n", (int)pFileName,0,0,0); if( ftpPathAccessVerify(pSlot, pFileName, O_WRONLY) == ERROR ) { if (ftpdCmdSend (pSlot, sock, 550, messages [MSG_GUEST_ACCESS], 0, 0, 0, 0, 0, 0) == ERROR) goto connectionLost; continue; } if ((outStream = fopen (pFileName, "w")) == NULL) { if (ftpdCmdSend (pSlot, sock, 553, messages[MSG_CREATE_ERROR], (int)(&pBuf[5]), 0, 0, 0, 0, 0) == ERROR) goto connectionLost; continue; } /* receive the file */ retVal = ftpdDataStreamReceive (pSlot, outStream); (void) fclose (outStream); if( retVal == ERROR ) { unlink( pFileName ); } } else if (strncmp (pBuf, "RNFR", 4) == 0) { struct stat st; /* for stat() system call */ ftpPathNormalize ( pSlot, &pBuf[5], newPath, &pFileName ); ftpdDebugMsg ("RNFR %s\n", (int)pFileName,0,0,0); if( ftpPathAccessVerify(pSlot, pFileName, O_RDWR) == ERROR ) { if (ftpdCmdSend (pSlot, sock, 550, messages [MSG_GUEST_ACCESS], 0, 0, 0, 0, 0, 0) == ERROR) goto connectionLost; continue; } if( stat( pFileName, &st ) != OK ) { if (ftpdCmdSend (pSlot, sock, 550, messages [MSG_FILE_ERROR], (int) pFileName, 0, 0, 0, 0, 0) == ERROR) goto connectionLost ; renFile[0] = EOS ; continue; } strncpy( renFile, pFileName, MAX_FILENAME_LENGTH ); if (ftpdCmdSend (pSlot, sock, 350, messages [MSG_RNFR_OK], 0, 0, 0, 0, 0, 0) == ERROR) goto connectionLost ; } else if (strncmp (pBuf, "RNTO", 4) == 0) { ftpPathNormalize ( pSlot, &pBuf[5], newPath, &pFileName ); ftpdDebugMsg ("RNTO %s\n", (int)pFileName,0,0,0); if( ftpPathAccessVerify(pSlot, pFileName, O_WRONLY) == ERROR ) { if (ftpdCmdSend (pSlot, sock, 550, messages [MSG_GUEST_ACCESS], 0, 0, 0, 0, 0, 0) == ERROR) goto connectionLost; continue; } if( rename( renFile, pFileName ) != OK ) { if (ftpdCmdSend (pSlot, sock, 550, messages [MSG_DIR_ERROR], (int) &pBuf[5], 0, 0, 0, 0, 0) == ERROR) goto connectionLost ; renFile[0] = EOS ; continue ; } if (ftpdCmdSend (pSlot, sock, 250, messages [MSG_RNTO_OK], 0, 0, 0, 0, 0, 0) == ERROR) goto connectionLost ; renFile[0] = EOS ; } else if (strncmp (pBuf, "DELE", 4) == 0) { ftpPathNormalize ( pSlot, &pBuf[5], newPath, &pFileName ); ftpdDebugMsg ("DELE %s\n", (int)pFileName,0,0,0); if( ftpPathAccessVerify(pSlot, pFileName, O_WRONLY) == ERROR ) { if (ftpdCmdSend (pSlot, sock, 550, messages [MSG_GUEST_ACCESS], 0, 0, 0, 0, 0, 0) == ERROR) goto connectionLost; continue; } if( remove( pFileName ) != OK ) { if (ftpdCmdSend (pSlot, sock, 550, messages [MSG_FILE_ERROR], (int) &pBuf[5], 0, 0, 0, 0, 0) == ERROR) goto connectionLost ; continue; } else { if (ftpdCmdSend (pSlot, sock, 200, messages [MSG_DELE_OK], 0, 0, 0, 0, 0, 0) == ERROR) goto connectionLost ; } } else if ((strncmp (pBuf, "MKD", 3) == 0) || (strncmp (pBuf, "XMKD", 4) == 0) ) { if(strncmp (pBuf, "MKD", 3) == 0) ftpPathNormalize ( pSlot, &pBuf[4], newPath, &pFileName ); else ftpPathNormalize ( pSlot, &pBuf[5], newPath, &pFileName ); ftpdDebugMsg ("MKD %s\n", (int)pFileName,0,0,0); if( ftpPathAccessVerify(pSlot, pFileName, O_WRONLY) == ERROR ) { if (ftpdCmdSend (pSlot, sock, 550, messages [MSG_GUEST_ACCESS], 0, 0, 0, 0, 0, 0) == ERROR) goto connectionLost; continue; } if( mkdir( pFileName ) != OK ) { if (ftpdCmdSend (pSlot, sock, 550, messages [MSG_MKD_ERROR], (int) &pBuf[4], 0, 0, 0, 0, 0) == ERROR) goto connectionLost ; continue; } else { if (ftpdCmdSend (pSlot, sock, 200, messages [MSG_MKD_OK], (int) &pBuf[4], 0, 0, 0, 0, 0) == ERROR) goto connectionLost ; } } else if ((strncmp (pBuf, "RMD", 3) == 0) || (strncmp (pBuf, "XRMD", 4) == 0) ) { char *pDir ; if(strncmp (pBuf, "RMD", 3) == 0) pDir = pBuf+4 ; else pDir = pBuf+5 ; ftpPathNormalize ( pSlot, pDir, newPath, &pFileName ); ftpdDebugMsg ("RMD %s\n", (int)pFileName,0,0,0); if( ftpPathAccessVerify(pSlot, pFileName, O_WRONLY) == ERROR ) { if (ftpdCmdSend (pSlot, sock, 550, messages [MSG_GUEST_ACCESS], 0, 0, 0, 0, 0, 0) == ERROR) goto connectionLost; continue; } if( rmdir( pFileName ) != OK ) { if (ftpdCmdSend (pSlot, sock, 550, messages [MSG_FILE_ERROR], (int) pDir, 0, 0, 0, 0, 0) == ERROR) goto connectionLost ; continue; } else { if (ftpdCmdSend (pSlot, sock, 200, messages [MSG_RMD_OK], (int) pDir, 0, 0, 0, 0, 0) == ERROR) goto connectionLost ; } } else if ((strncmp (pBuf, "CWD", 3) == 0)|| (strncmp (pBuf, "CDUP", 4) == 0)|| (strncmp (pBuf, "XCWD", 4) == 0)|| (strncmp (pBuf, "XCUP", 4) == 0) ) { struct stat st; if(strncmp (pBuf, "CWD", 3) == 0) ftpPathNormalize ( pSlot, &pBuf[4], newPath, &dirName ); else if( strncmp (pBuf, "XCWD", 4) == 0) ftpPathNormalize ( pSlot, &pBuf[5], newPath, &dirName ); else ftpPathNormalize ( pSlot, "..", newPath, &dirName ); if( ftpPathAccessVerify(pSlot, newPath, O_RDONLY) == ERROR ) { if (ftpdCmdSend (pSlot, sock, 550, messages [MSG_GUEST_ACCESS], 0, 0, 0, 0, 0, 0) == ERROR) goto connectionLost; continue; } if( (stat( newPath, &st ) != OK ) || !S_ISDIR (st.st_mode)) { if (ftpdCmdSend (pSlot, sock, 501, messages[MSG_DIR_NOT_PRESENT], 0, 0, 0, 0, 0, 0) == ERROR) goto connectionLost ; continue; } (void) strncpy (pSlot->curDirName, newPath, MAX_FILENAME_LENGTH); if (ftpdCmdSend (pSlot, sock, 250, messages [MSG_CHANGED_DIR], (int)ftpdPathForPrint(pSlot,newPath), 0, 0, 0, 0, 0) == ERROR) goto connectionLost; } else if (strncmp (pBuf, "TYPE", 4) == 0) { if (pBuf [5] == 'I' || pBuf [5] == 'i' || pBuf [5] == 'L' || pBuf [5] == 'l') { pSlot->status |= FTPD_BINARY_TYPE; pSlot->status &= ~FTPD_ASCII_TYPE; if (ftpdCmdSend (pSlot, sock, 200, messages [MSG_TYPE_BINARY], 0, 0, 0, 0, 0, 0) == ERROR) goto connectionLost; } else if (pBuf [5] == 'A' || pBuf [5] == 'a') { pSlot->status |= FTPD_ASCII_TYPE; pSlot->status &= ~FTPD_BINARY_TYPE; if (ftpdCmdSend (pSlot, sock, 200, messages [MSG_TYPE_ASCII], 0, 0, 0, 0, 0, 0) == ERROR) goto connectionLost; } else { if (ftpdCmdSend (pSlot, sock, 504, messages [MSG_PARAM_BAD], 0, 0, 0, 0, 0, 0) == ERROR) goto connectionLost; } } else if (strncmp (pBuf, "PORT", 4) == 0) { (void) sscanf (&pBuf [5], "%d,%d,%d,%d,%d,%d", &portNum [0], &portNum [1], &portNum [2], &portNum [3], &portNum [4], &portNum [5]); pSlot->dataAddr.sin_port = portNum [4] * 256 + portNum [5]; pSlot->dataAddr.sin_port = htons (pSlot->dataAddr.sin_port); value = (portNum [0] << 24) | (portNum [1] << 16) | (portNum [2] << 8) | portNum [3]; pSlot->dataAddr.sin_addr.s_addr = htonl (value); if (ftpdCmdSend (pSlot, sock, 200, messages [MSG_PORT_SET], 0, 0, 0, 0, 0, 0) == ERROR) goto connectionLost; } else if ((strncmp (pBuf, "PWD", 3) == 0) || (strncmp (pBuf, "XPWD", 4) == 0)) { (void) strcpy (pBuf, pSlot->curDirName); if (ftpdCmdSend (pSlot, sock, 257, messages [MSG_CUR_DIR], (int)ftpdPathForPrint(pSlot,pBuf), 0, 0, 0, 0, 0) == ERROR) goto connectionLost; } else if (strncmp (pBuf, "STRU", 4) == 0) { if (pBuf [5] == 'F' || pBuf [5] == 'f') { if (ftpdCmdSend (pSlot, sock, 200, messages [MSG_FILE_STRU], 0, 0, 0, 0, 0, 0) == ERROR) goto connectionLost; } else { if (ftpdCmdSend (pSlot, sock, 504, messages [MSG_PARAM_BAD], 0, 0, 0, 0, 0, 0) == ERROR) goto connectionLost; } } else if (strncmp (pBuf, "MODE", 4) == 0) { /* specify transfer mode */ /* we only support stream mode -- no block or compressed mode */ if (pBuf [5] == 'S' || pBuf [5] == 's') { if (ftpdCmdSend (pSlot, sock, 200, messages [MSG_STREAM_MODE], 0, 0, 0, 0, 0, 0) == ERROR) goto connectionLost; } else { if (ftpdCmdSend (pSlot, sock, 504, messages [MSG_PARAM_BAD], 0, 0, 0, 0, 0, 0) == ERROR) goto connectionLost; } } else if (strncmp (pBuf, "ALLO", 4) == 0 || strncmp (pBuf, "ACCT", 4) == 0) { /* allocate and account commands are not need */ if (ftpdCmdSend (pSlot, sock, 202, messages [MSG_ALLOC_ACCOUNT], 0, 0, 0, 0, 0, 0) == ERROR) goto connectionLost; } else if (strncmp (pBuf, "PASV", 4) == 0) { int outval1; int outval2; int outval3; int outval4; int outval5; int outval6; ftpdSockFree (&pSlot->dataSock); if ((pSlot->dataSock = socket (AF_INET, SOCK_STREAM, 0)) < 0) { if (ftpdCmdSend (pSlot, sock, 425, messages [MSG_PASSIVE_ERROR], 0, 0, 0, 0, 0, 0) == ERROR) goto connectionLost; continue ; } addrLen = sizeof (pSlot->dataAddr); if (getsockname (pSlot->cmdSock, (struct sockaddr *) &pSlot->dataAddr, &addrLen) < 0) { if (ftpdCmdSend (pSlot, sock, 425, messages [MSG_PASSIVE_ERROR], 0, 0, 0, 0, 0, 0) == ERROR) goto connectionLost; } pSlot->dataAddr.sin_port = htons (0); addrLen = sizeof (struct sockaddr_in); if (bind (pSlot->dataSock, (struct sockaddr *)&pSlot->dataAddr, sizeof (struct sockaddr_in)) < 0 || getsockname (pSlot->dataSock, (struct sockaddr *) &pSlot->dataAddr, &addrLen) < 0 || listen (pSlot->dataSock, 1) < 0) { ftpdSockFree (&pSlot->dataSock); if (ftpdCmdSend (pSlot, sock, 425, messages [MSG_PASSIVE_ERROR], 0, 0, 0, 0, 0, 0) == ERROR) goto connectionLost; continue; } pSlot->status |= FTPD_PASSIVE; value = pSlot->dataAddr.sin_addr.s_addr; outval1 = ( (u_char *)&value)[0]; outval2 = ( (u_char *)&value)[1]; outval3 = ( (u_char *)&value)[2]; outval4 = ( (u_char *)&value)[3]; outval5 = ( (u_char *)&pSlot->dataAddr.sin_port)[0]; outval6 = ( (u_char *)&pSlot->dataAddr.sin_port)[1]; if (ftpdCmdSend (pSlot, pSlot->cmdSock, 227, messages [MSG_PASSIVE_MODE], outval1, outval2, outval3, outval4, outval5, outval6) == ERROR) goto connectionLost; } else if (strncmp (pBuf, "NOOP", 4) == 0) { /* don't do anything */ if (ftpdCmdSend (pSlot, sock, 200, messages [MSG_NOOP_OKAY], 0, 0, 0, 0, 0, 0) == ERROR) goto connectionLost; } else { if (ftpdCmdSend (pSlot, sock, 500, messages [MSG_BAD_COMMAND], 0, 0, 0, 0, 0, 0) == ERROR) goto connectionLost; } } ftpdSessionDelete (pSlot); return (OK);connectionLost: ftpdDebugMsg ("FTP Control connection error, closed.\n", 0,0,0,0); ftpdSessionDelete (pSlot); return (ERROR); }/********************************************************************************/LOCAL STATUS ftpdDataConnGet ( FTPD_SESSION_DATA *pSlot ) { FAST int newSock; int addrLen; struct sockaddr_in addr; int on = 1; int retry = 0; if (pSlot->cmdSock == FTPD_SOCK_FREE) return (ERROR); pSlot->byteCount = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -