Beispiel #1
0
/* Simple...								*/
void conn_disconnect( conn_t *conn )
{
	if( conn->proto == PROTO_FTP && !conn->proxy )
		ftp_disconnect( conn->ftp );
	else
		http_disconnect( conn->http );
	conn->fd = -1;
}
Beispiel #2
0
static void
ftp_put_super(struct super_block *sb){
	struct ftp_sb_info *info=(struct ftp_sb_info*)sb->u.generic_sbp;

	ftp_disconnect(info);
	ftp_cache_empty();	
	kfree(info);
	DEBUG(" Super Block discarded!\n");
}
Beispiel #3
0
/* Simple... */
void conn_disconnect(conn_t *conn)
{
	if (conn->proto == PROTO_FTP && !conn->proxy)
		ftp_disconnect(conn->ftp);
	else
		http_disconnect(conn->http);
#if WIN32
	conn->fd = INVALID_SOCKET;
#else
	conn->fd = -1;
#endif
}
Beispiel #4
0
int main(int argc, char** argv){
	if(argc != 2){
		printUsage();
		return 1;
	}

	int val = validateURL(argv[1], sizeof(argv[1]));
	int anon = 1;
	char user[MAX_STRING_SIZE];
	char password[MAX_STRING_SIZE];
	char host[MAX_STRING_SIZE];
	char path[MAX_STRING_SIZE];
	switch(val){
		case 0:
			anon = 0;
		case 1:
			parseURL(argv[1], sizeof(argv[1]), host, user, password, path, anon);
			break;
		case 2:
			printUsage();
			return 1;
		default:
			return 1;
	}

	ftp_t* ftp = ftp_init(host, user, password, path);
	if(ftp == NULL)
		return 1;

	if(ftp_connect(ftp) < 0)
		return 1;

	if(ftp_login_host(ftp) < 0)
		return 1;

	if(ftp_set_passive_mode(ftp) < 0)
		return 1;

	if(ftp_retr_file(ftp) < 0)
		return 1;

	if(ftp_download_file(ftp) < 0)
		return 1;

	if(ftp_disconnect(ftp) < 0)
		return 1;

	ftp_delete(ftp);

	return 0;
}
Beispiel #5
0
void disconnect_from_remote_host(int host)
{
	if (!isconnected) {
		return;
	}

	if (config.access_method[host] == FTP) {
		ftp_disconnect(host);
	}
	remove_temp_files(host);
	isconnected = FALSE;

	message(DISCONNECT, config.host_name[host], 0, host);
}
Beispiel #6
0
void Connection::ftp_connect() {
    ftp_disconnect();
    max_connections = ui->maxConnections->value();

    connect(ftp_conn, SIGNAL(x_commandFinished(QxFtp *,int,bool)), this, SLOT( ftpCommandFinished(QxFtp *,int,bool)));
    connect(ftp_conn, SIGNAL(x_commandStarted(QxFtp *,int)), this, SLOT(ftpCommandStarted(QxFtp *,int)));
    connect(ftp_conn, SIGNAL(x_listInfo(QxFtp *,QUrlInfo)), this, SLOT(addToList(QxFtp *,QUrlInfo)));
    connect(ftp_conn, SIGNAL(x_rawCommandReply(QxFtp *,int, const QString &)), this, SLOT(ftp_rawCommandReply(QxFtp *,int, const QString &)) );
    QUrl url(ui->serverAddress->text());
    ftp_conn->connectToHost(ui->serverAddress->text(), 21);


    if (ui->anonymousConnection->isChecked()) ftp_conn->login();
    else ftp_conn->login(ui->user->text(), ui->password->text());
    ui->statusLabel->setText(tr("Connecting to FTP server '%1'").arg(ui->serverAddress->text()));
}
Beispiel #7
0
int ftp_file_del(const char *file_name) {
	int ret = -1;
	ftp_t ftp;

	if (ftp_connect(&ftp) != 0)
		return -1;
	//发送删除命令
	snprintf(ftp.cmd_buf, BUFSIZE, "DELE %s\r\n", file_name);
	if ((ret = ftp_cmd_tx(&ftp)) == -1) 
		goto _ret;

	ret = 0;
_ret:
	ftp_disconnect(&ftp);
	return ret;
}
Beispiel #8
0
//检查文件大小,文件不存在则返回-1
int ftp_file_size(const char *file_name) {
	int ret = -1, ret_code = 0, file_size = 0;
	ftp_t ftp;

	if (ftp_connect(&ftp) != 0)
		return -1;
	snprintf(ftp.cmd_buf, BUFSIZE, "SIZE %s\r\n", file_name);
	if ((ret = ftp_cmd_tx(&ftp)) == -1) 
		goto _ret;
	//获得文件大小
	sscanf(ftp.cmd_buf, "%d %d", &ret_code, &file_size);
	ret = file_size;
_ret:
	ftp_disconnect(&ftp);
	return ret;
}
Beispiel #9
0
//上传buf内容到ftp的文件中
static int _ftp_file_put(const char *file_name, const uint8_t *buf, int buf_len) {
	int ret = -1;
	ftp_t ftp;

	if (ftp_connect(&ftp) != 0)
		return -1;

	//发送上传命令
	snprintf(ftp.cmd_buf, BUFSIZE, "STOR %s\r\n", file_name);
	if ((ret = ftp_cmd_tx(&ftp)) == -1) 
		goto _ret;
		
	//通过data socket发送数据
	if (sock_write(ftp.data_sd, buf, buf_len) < buf_len)
		goto _ret;

	ret = 0;
_ret:
	ftp_disconnect(&ftp);
	return ret;
}
Beispiel #10
0
//读取ftp文件到buf中,返回文件大小
int ftp_file_get(const char *file_name, uint8_t *buf, int len) {
	int ret = -1, file_size = 0;
	ftp_t ftp;
	//文件不存在或者buf空间不够或者连接失败
	if ((file_size = ftp_file_size(file_name)) == -1 \
		|| len < file_size || ftp_connect(&ftp) != 0)
		return -1;

	//发送获取文件命令
	snprintf(ftp.cmd_buf, BUFSIZE, "RETR %s\r\n", file_name);
	if ((ret = ftp_cmd_tx(&ftp)) == -1)
		goto _ret;

	//通过data socket读取数据
	if (sock_read_wait(ftp.data_sd, buf, file_size) < file_size) 
		goto _ret;

	ret = file_size;
_ret:
	ftp_disconnect(&ftp);
	return ret;
}
Beispiel #11
0
int ftp_open_data(struct ftpconnection *connection) {
    struct sockaddr_in servaddr;
    struct in_addr sin;
    char *line;
    int h1, h2, h3, h4, p1, p2, port;
    int response;
    SOCKLEN len;
    if ((connection->dataconfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("socket");
        return -1;
    }
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = 0;
    if (bind(connection->dataconfd, (SOCKADDR *)&servaddr, sizeof(servaddr)) < 0) {
        perror("bind");
        return -1;
    }
    if (listen(connection->dataconfd, 1) < 0) {
        perror("listen");
        return -1;
    }
    len = 128;
    if (getsockname(connection->dataconfd, (SOCKADDR *)&servaddr, &len) < 0) {
        perror("getsockname");
        return -1;
    }
    line = (char *)malloc(MAX_LINE_SIZE);
    if(NULL == line)
    {
        return -1;
    }
    port = servaddr.sin_port;
    sin.s_addr = connection->localip;
    sprintf(line, "%s.%d\r\n", inet_ntoa(sin), port);
    sscanf(line, "%d.%d.%d.%d.%d", &h1, &h2, &h3, &h4, &port);
    p1 = (port / 256);
    p2 = port - (256 * p1);
    sprintf(line, "PORT %d,%d,%d,%d,%d,%d\r\n", h1, h2, h3, h4, p2, p1);
    connection->status = STATUS_WAITING;
    ftp_sendline(connection, line);
    FREE(line);
    response = ftp_getrc(connection, NULL, 0, 0);
    switch (response) {
    case 200 :
        return 0;
    case 421 :
        printf("Service unavailable\n");
        ftp_disconnect(connection);
        return response;
        break;
    case 500 :
        printf("Server doesn't understand PORT\n");
        return response;
        break;
    case 501 :
        printf("Server doesn't understand PORT parameters\n");
        return response;
        break;
    case 502 :
        printf("Server doesn't understand PORT\n");
        return response;
        break;
    case 530 :
        printf("Not logged in\n");
        return response;
        break;
    default  :
        printf("Unknown response to PORT: %d\n", response);
        return response;
        break;
    }
}
Beispiel #12
0
void Connection::ftpCommandFinished(QxFtp * origin, int id, bool error)
{

    //std::cout<< "finished command id"<<id <<std::endl;

    if (origin->currentCommand() != QxFtp::RawCommand && origin->currentCommand() != QxFtp::Put && origin->currentCommand() != QxFtp::Get) pwd();
    //ta svina se zacyklila...
    if (error) {
        std::cerr<< "State: "<<origin->state() <<std::endl;
        std::cerr<< "Error: "<<origin->errorString().toStdString() <<std::endl;
    }

    if (origin->currentCommand() == QxFtp::ConnectToHost) {
        if (error) {
            QMessageBox::information(this, tr("FTP"),
                                     tr("Unable to connect to the FTP server "
                                        "at '%1'<br />%2")
                                     .arg(ui->serverAddress->text()).arg(origin->errorString()));
            ftp_disconnect();
            return;
        }
        ui->statusLabel->setText(tr("Connected to %1.")
                             .arg(ui->serverAddress->text()));


        panel->show();
        connect( this, SIGNAL(pwdChanged(const QString &)), panel, SLOT(changePwd(const QString &)) );
        this->hide();

        //downloadButton->setDefault(true);
        //connectButton->setEnabled(true);
        return;
    }


//![6]

//![7]
    if (origin->currentCommand() == QxFtp::Login) {
        if (error) {
            QMessageBox::information(this, tr("FTP"),
                                     tr("Unable to login to the FTP server:<br />%1")
                                     .arg(origin->errorString()));
            ui->statusLabel->setText(tr("Disconnected from %1.")
                                 .arg(ui->serverAddress->text()));
            ftp_disconnect();
            return;
        }
        if (origin == ftp_conn)origin->list();

    }
    else if (origin->currentCommand() == QxFtp::Mkdir) {
        //std::cout<<"created"<<std::endl;
        //ftp_conn->list();
    }
    else if (origin->currentCommand() == QxFtp::List) {
    }
    else if (origin->currentCommand() == QxFtp::Rename) {
        if (error) {
            QMessageBox::critical(this, tr("Error!"),
                                     tr("'%1'")
                                     .arg(origin->errorString()));
            origin->list();
        }
    }
    else if (origin->currentCommand() == QxFtp::Put || origin->currentCommand() == QxFtp::Get) {
        for (int i = 0 ; i < pendingQueue.size() ; i++)
        {
            if (origin == pendingQueue.at(i)->conn())
            {
                pendingQueue.at(i)->deleteLater();
                pendingQueue.removeAt(i);
                break;
            }
        }

    }


    emit setupTransfers();


}
Beispiel #13
0
void Connection::on_buttonBox_rejected()
{
    ftp_disconnect();
    exit(0);
}
int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        print_usage(argv[0]);
        return EXIT_FAILURE;
    }

    URL url;
    url_init(&url);
    int error = url_from_string(&url, argv[1]);
    if (error)
    {
        fprintf(stderr, "Could not sucessfully parse FTP url (error code: %d)\n", error);
        url_destroy(&url);
        return EXIT_FAILURE;
    }

    error = url_host_to_ip(&url);
    if (error)
    {
        fprintf(stderr, "Could not get an IPv4 IP address from hostname %s (error code: %d)\n", url.host, error);
        return EXIT_FAILURE;
    }

    FTP ftp;
    error = ftp_connect(&ftp, url.ip, url.port ? url.port : 21);
    if (error)
    {
        fprintf(stderr, "Could not connect to ftp at IP %s, port %d\n", url.ip, url.port ? url.port : 21);
        return EXIT_FAILURE;
    }

    const char* user = strlen(url.user) ? url.user : "******";
    const char* pass = strlen(url.password) ? url.password : "******";

    error = ftp_login(&ftp, user, pass);
    if (error)
    {
        fprintf(stderr, "Could not login with user %s and pass %s\n", user, pass);
        return EXIT_FAILURE;
    }

    char path[1024] = "";
    for (int i = 0; i < url.num_parts - 1; ++i) {
        strcat(path, url.parts[i]);
        strcat(path, "/");
    }

    if (path[0] != 0) {
        error = ftp_cwd(&ftp, path);
        if (error) {
            perror("ftp_cwd");
            return error;
        }
    }

    error = ftp_pasv(&ftp);
    if (error) {
        perror("ftp_pasv");
        return error;
    }

    const char* file_name = url.num_parts ? url.parts[url.num_parts - 1] : "";

    error = ftp_retr(&ftp, file_name);
    if (error) {
        perror("ftp_retr");
        return error;
    }

    error = ftp_download(&ftp, file_name);
    if (error) {
        perror("ftp_download");
        return error;
    }

    error = ftp_disconnect(&ftp);
    if (error) {
        perror("ftp_disconnect");
        return error;
    }

    url_destroy(&url);

    return EXIT_SUCCESS;
}
Beispiel #15
0
int ftp_open_data_pasv(struct ftpconnection *connection, char *address, int port) {
    int response;
    struct sockaddr_in dataaddr;
    char *buffer = NULL, *line, *temp, *temp2;
    int length;
    if (!ftp_check_ready(connection, 1)) return 0;
    if (!address) {
        ftp_sendline(connection, "PASV\r\n");
        line = (char *)malloc(MAX_LINE_SIZE);
        if(NULL == line)
        {
            return 1;
        }
        response = ftp_getrc(connection, line, MAX_LINE_SIZE, 0);
        switch (response) {
        case 227 :
            temp = strstr(line, "(");
            temp++;
            temp2 = strstr(temp, ")");
            length = temp2-temp;
            buffer = (char *)malloc(length + 1);
            if(NULL == buffer)
            {
                return 1;
            }
            memcpy(buffer, temp, length);
            buffer[length] = 0;
            address = (char *)malloc(20);
            if(NULL == address)
            {
                return 1;
            }
            port = ftp_parse_pasv(buffer, address);
            FREE(line);
            FREE(buffer);
            break;
        case 421 :
            printf("Service unavailable\n");
            FREE(line);
            ftp_disconnect(connection);
            return 0;
            break;
        case 500 :
            printf("Server doesn't understand PASV\n");
            FREE(line);
            return 0;
            break;
        case 501 :
            printf("Server doesn't understand PASV parameters\n");
            FREE(line);
            return 0;
            break;
        case 502 :
            printf("Server doesn't understand PASV\n");
            FREE(line);
            return 0;
            break;
        case 530 :
            printf("Not logged in\n");
            FREE(line);
            return 0;
            break;
        default  :
            printf("Unknown response to PASV: %d\n", response);
            FREE(line);
            return 0;
            break;
        }
    }
    if ((connection->datafd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("socket");
        return 1;
    }
    printf("Opening a data connection to %s:%d\n",address, port);
    memset(&dataaddr, 0, sizeof(dataaddr));
    dataaddr.sin_family = AF_INET;
    dataaddr.sin_port = htons(port);
    if (inet_aton(address, &dataaddr.sin_addr) <= 0) {
        perror("inet_aton");
        return 1;
    }
    if (connect(connection->datafd,(SOCKADDR *)&dataaddr, sizeof(dataaddr)) < 0) {
        printf("Can't open a data connection to %s\n", address);
        return 1;
    }
    connection->status = STATUS_TRANSFER;
    FREE(address);
    return 0;
}
Beispiel #16
0
int ftp_list(struct ftpconnection *connection, char *params, char showthem) {
    char *buffer = NULL;
    int response = 0, index = 0;

    char *outputformat = NULL;

    if (!ftp_check_ready(connection, 1)) return -1;
    ftp_sendline(connection, "TYPE A\r\n");	// Go to ASCII mode
    ftp_getrc(connection, NULL, 0, 0);
    buffer = config(connection->variables, "passive");
    if (!buffer || strcasecmp(buffer, "true") == 0)
        response = ftp_open_data_pasv(connection, NULL, 0);
    else response = ftp_open_data(connection);
    if (response) {
        log(LOG_ALERT, "ftp_list: bad response from ftp_open_data (%d)\n",
            response);
        return -response;	// Can't open a data connection
    }
    if (params && *params) {
        buffer = (char *)malloc(MAX_LINE_SIZE);
        sprintf(buffer, "LIST %s\r\n", params);
        ftp_sendline(connection, buffer);
        free(buffer);
    } else ftp_sendline(connection, "LIST\r\n");
    response = ftp_getrc(connection, NULL, 0, 0);
    switch (response) {
    case 125 : /* Already a data connection */
        break;
    case 150 :
        ftp_wait_data(connection);	/* Wait for the data connection */
        break;
    case 250 :
    case 226 :
        log(LOG_INFO, "No files available\n");
        return response;
        break;
    case 425 :
    case 426 :
    case 451 :
    case 450 :
        log(LOG_ALERT, "Could not list directory\n");
        return response;
        break;
    case 421 :
        log(LOG_ALERT, "Service unavailable\n");
        ftp_disconnect(connection);
        return response;
        break;
    case 500 :
        log(LOG_ALERT, "Server doesn't understand LIST\n");
        return response;
        break;
    case 501 :
        log(LOG_ALERT, "Server doesn't understand LIST parameters\n");
        return response;
        break;
    case 502 :
        log(LOG_ALERT, "Server doesn't understand LIST\n");
        return response;
        break;
    case 530 :
        log(LOG_ALERT, "Not logged in\n");
        return 0;
        break;
    default  :
        log(LOG_ALERT, "Unknown response to LIST: %d\n", response);
        return response;
        break;
    }
    log(LOG_MESSAGE, "ftp_list: processing list\n");
    buffer = (char *)malloc(4096);
    outputformat = config(connection->variables, "lsformat");
    while (1) {
        char *temp;
        memset(buffer, 0, 4096);
        if ((response = ftp_read_data_line(connection, buffer, 4096))
                == 0) break; 		/* Read Line */
        if (strlen(buffer) == 0) continue;	/* Got crap? */
        if (ftp_parse_permissions(buffer) < 0) continue;
        /* Parse -rw-r--r-- etc */
        temp = strchr(buffer, '\n');
        if (temp) *temp = 0;
        if (showthem) {
            if (outputformat && *outputformat)
                printf("%s\n", listoutput(buffer, outputformat));
            else
                printf("%s\n", buffer);
        }
        index++;		/* Keep a count of files */
    }
    free(buffer);
    ftp_close_data(connection);		/* Close the data connection */
    response = ftp_getrc(connection, NULL, 0, 0);	/* Server says ok? */
    return index;
}
Beispiel #17
0
int ftp_nlst(struct ftpconnection *connection, char *params, char *list[], int max_items) {
    char *buffer;
    int index, response;
    if (!ftp_check_ready(connection, 0)) return -1;
    if (max_items > 0) memset(list, 0, sizeof(*list) * max_items);
    ftp_sendline(connection, "TYPE A\r\n");
    ftp_getrc(connection, NULL, 0, 0);

    buffer = config(connection->variables, "passive");
    if (!buffer || strcasecmp(buffer, "true") == 0)
        response = ftp_open_data_pasv(connection, NULL, 0);
    else response = ftp_open_data(connection);

    index = 0;
    if (params && *params) {
        buffer = (char *)malloc(MAX_LINE_SIZE);
        sprintf(buffer, "NLST %s\r\n", params);
        ftp_sendline(connection, buffer);
        free(buffer);
    } else ftp_sendline(connection, "NLST\r\n");
    response = ftp_getrc(connection, NULL, 0, 0);
    switch (response) {
    case 125 : /* Already a data connection */
        break;
    case 150 :
        ftp_wait_data(connection);	/* Wait for the data connection */
        break;
    case 250 :
    case 226 :
        printf("No files available\n");
        connection->status = STATUS_IDLE;
        return response;
        break;
    case 425 :
    case 426 :
    case 451 :
    case 450 :
        log(LOG_ALERT, "Could not list directory\n");
        connection->status = STATUS_IDLE;
        return response;
        break;
    case 421 :
        log(LOG_ALERT, "Service unavailable\n");
        ftp_disconnect(connection);
        return response;
        break;
    case 500 :
        log(LOG_ALERT, "Server doesn't understand NLST\n");
        connection->status = STATUS_IDLE;
        return response;
        break;
    case 501 :
        log(LOG_ALERT, "Server doesn't understand NLST parameters\n");
        connection->status = STATUS_IDLE;
        return response;
        break;
    case 502 :
        log(LOG_ALERT, "Server doesn't understand NLST\n");
        connection->status = STATUS_IDLE;
        return response;
        break;
    case 530 :
        log(LOG_ALERT, "Not logged in\n");
        ftp_disconnect(connection);
        return 0;
        break;
    case 550 : /* log(LOG_ALERT, "No files found matching %s\n", params); */
        connection->status = STATUS_IDLE;
        return 0;
        break;
    default  :
        log(LOG_ALERT, "Unknown response to NLST: %d\n", response);
        connection->status = STATUS_IDLE;
        return response;
        break;
    }
    buffer = (char *)malloc(4096);
    while ((response = ftp_read_data_line(connection, buffer, 4096) > 0)) {
        if (strlen(buffer) == 0) continue;
        if (strcmp(buffer, ".") == 0) continue;
        if (strcmp(buffer, "..") == 0) continue;
        if (max_items > 0) list[index] = strdup(buffer);
        index++;
        if (max_items > 0) if (index > max_items) break;
    }
    if (index >= max_items) index = max_items;
    /* else for (temp = index; temp < max_items; temp++) list[temp] = 0; */
    list[index] = NULL;
    ftp_getrc(connection, NULL, 0, 0);
    free(buffer);
    ftp_close_data(connection);
    return index;
}