Example #1
0
static int ftp_receive_head(FILE *control_stream, int *pnFdData, int *pnFileSize, ftp_host_info_t *server,
                            char *server_path, char caFtpHead[FTP_HEAD_SIZE])
{
    printf("ftp_receive_head++++++++++++++++++++++\n");
    char buf[512];
    int rd;

    /* Connect to the data socket */
    if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
        ftp_die("PASV", buf);
    }

    *pnFdData = xconnect_ftpdata(server, buf);
    if (*pnFdData < 0)
        return -1;

    if (ftpcmd("SIZE", server_path, control_stream, buf) == 213) {
        *pnFileSize = strtol(buf + 4, NULL, 10);
        if (*pnFileSize < 0)
            ftp_die("SIZE", buf);
    } else {
        do_continue = 0;
    }

    if (ftpcmd("RETR", server_path, control_stream, buf) > 150) {
        ftp_die("RETR", buf);
    }
    rd = safe_read(*pnFdData, caFtpHead, FTP_HEAD_SIZE);
    printf("ftp_receive_head=============\n");
    return rd;
}
Example #2
0
FILE *ftp_login(ftp_host_info_t *server)
{
	FILE *control_stream;
	char buf[512];
	int conn_ret = -1;

	ftp_fd = lib_create_tcp(__FUNCTION__, 0, 0);
	if(ftp_fd < 0){
		lib_error("[%s] lib_create_tcp err!!", __FUNCTION__);	
		return NULL;
	}

	conn_ret = lib_connect_tcp(__FUNCTION__, ftp_fd, server->ip, server->port);
	if(conn_ret < 0){
		lib_error("[%s] lib_connect_tcp err!!", __FUNCTION__);	
		return NULL;
	}

	/* Connect to the command socket */
	control_stream = fdopen(ftp_fd, "r+");
	if (control_stream == NULL) {
		lib_error("[%s] fdopen err!!", __FUNCTION__);	
		return NULL;
	}

	if (ftpcmd(NULL, NULL, control_stream, buf) != 220) {
		ftp_die(NULL, buf);
	}

	//printf("[%s] debug\n", __FUNCTION__);

	/*  Login to the server */
	switch (ftpcmd("USER", server->user, control_stream, buf)) {
	case 230:
		break;
	case 331:
		if (ftpcmd("PASS", server->password, control_stream, buf) != 230) {
			ftp_die("PASS", buf);
			lib_trace("FTP Login Fail.");
			return NULL;
		}
		else
			lib_trace("FTP Login OK.");
		break;
	default:
		ftp_die("USER", buf);
		lib_trace("FTP Login Fail.");
		return NULL;
	}

	ftpcmd("TYPE I", NULL, control_stream, buf);

	if(ftpcmd("CWD", server->dir, control_stream, buf) != 250){
		ftp_die("CWD", buf);
		return NULL;
	}

	return control_stream;
}
Example #3
0
static int ftp_send(ftp_host_info_t *server, FILE *control_stream, const char *server_path, char *local_path)
{
    struct stat sbuf;
    char buf[512];
    int fd_data;
    int fd_local;
    int response;

    /*  Connect to the data socket */
    if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
        ftp_die("PASV", buf);
    }
    fd_data = xconnect_ftpdata(server, buf);

    /* get the local file */
    fd_local = STDIN_FILENO;
    if (NOT_LONE_DASH(local_path)) {
        fd_local = xopen(local_path, O_RDONLY, 0666);
        fstat(fd_local, &sbuf);
        sprintf(buf, "ALLO %lu", (long unsigned int)sbuf.st_size);
        response = ftpcmd(buf, NULL, control_stream, buf);
        switch (response) {
        case 200:
        case 202:
            break;
        default:
            close(fd_local);
            ftp_die("ALLO", buf);
            break;
        }
    }

    response = ftpcmd("STOR", server_path, control_stream, buf);
    switch (response) {
    case 125:
    case 150:
        break;
    default:
        close(fd_local);
        ftp_die("STOR", buf);
    }

    /* transfer the file  */
    if (bb_copyfd_eof(fd_local, fd_data) == -1) {
        exit(EXIT_FAILURE);
    }

    /* close it all down */
    close(fd_data);
    if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
        ftp_die("close", buf);
    }
    ftpcmd("QUIT", NULL, control_stream, buf);

    return EXIT_SUCCESS;
}
Example #4
0
static FILE *ftp_login(ftp_host_info_t *server)
{
    printf("ftp_login++++++++++++++++++++\n");
    FILE *control_stream;
    char buf[512];
    /* Connect to the command socket */
    control_stream = fdopen(xconnect_stream(server->lsa), "r+");
    if (control_stream == NULL) {
        /* fdopen failed - extremely unlikely */
        //bb_error_msg_and_die("ftp login");
        printf("ftp login");
        exit (-1);
    }
    printf("ftp_login++11111111111111111111++++++\n");
    if (ftpcmd(NULL, NULL, control_stream, buf) != 220) {
        printf("ftp_die++\n");
        //ftp_die(NULL, buf);
    }
    printf("ftp_login+++++++2222222222222222222++++\n");
    /*  Login to the server */
    switch (ftpcmd("USER", server->user, control_stream, buf)) {
    case 230:
        printf("ftp_login++++230++++++\n");
        break;
    case 331:
        printf("ftp_login+++++++331++++++++\n");
        if (ftpcmd("PASS", server->password, control_stream, buf) != 230) {
            ftp_die("PASS", buf);
            printf("ftp_login+++ftp_die++++PAS+++++\n");
        }
        break;
    default:
        printf("ftp_login++++++default+++++++\n");
        ftp_die("USER", buf);
    }
    ftpcmd("TYPE I", NULL, control_stream, buf);
    printf("ftp_login==================\n");
    return control_stream;
}
Example #5
0
int ftp_recv_to_ram(int nFdData, FILE *control_stream, char caFtpHead[FTP_HEAD_SIZE], int nHeadSize, char *pcRam)
{
    int fd_local = -1;
    char buf[512];
    int status = -1;
    off_t total = 0;
    char buffer[BUFSIZ];
    int size = 0;
    memcpy(pcRam, caFtpHead, nHeadSize);
    total += nHeadSize;

    while (1) {
        ssize_t rd;
        rd = safe_read(nFdData, buffer, BUFSIZ);
        //printf("-----rd =%d----n", rd);
        if (!rd) { /* eof - all done */
            status = 0;
            break;
        }

        if (rd < 0) {
            //bb_error_msg(bb_msg_read_error);
            break;
        }

        memcpy(pcRam + total, buffer, rd);
        total += rd;
        if (status < 0) { /* if we aren't copying till EOF... */
            size -= rd;
            if (!size) {
                /* 'size' bytes copied - all done */
                status = 0;
                break;
            }
        }
    }

    /* close it all down */
    close(nFdData);
    if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
        ftp_die(NULL, buf);
    }
    ftpcmd("QUIT", NULL, control_stream, buf);

    return status ? -1 : total;
}
Example #6
0
int ftp_recv_to_file(int nFdData, FILE *control_stream, char caFtpHead[FTP_HEAD_SIZE], int nHeadSize, const char *pcLocalFileName)
{
    int fd_local = -1;
    char buf[512];
    int status = -1;
    off_t total = 0;
    char buffer[BUFSIZ];
    int size = 0;
    fd_local = xopen(pcLocalFileName, O_CREAT | O_TRUNC | O_WRONLY, 0666);

    if (fd_local < 0) {
        goto out;
    }

    ssize_t wr = full_write(fd_local, caFtpHead, nHeadSize);
    if (wr < nHeadSize) {
        //bb_error_msg(bb_msg_write_error);
        goto out;
    }

    total += nHeadSize;
    if (!size) {
        size = BUFSIZ;
        status = 1; /* copy until eof */
    }

    while (1) {
        ssize_t rd;
        rd = safe_read(nFdData, buffer, size > BUFSIZ ? BUFSIZ : size);
        //printf("-----rd =%d----n", rd);
        if (!rd) { /* eof - all done */
            status = 0;
            break;
        }

        if (rd < 0) {
            //bb_error_msg(bb_msg_read_error);
            break;
        }

        /* dst_fd == -1 is a fake, else... */
        if (fd_local >= 0) {
            ssize_t wr = full_write(fd_local, buffer, rd);
            if (wr < rd) {
                //bb_error_msg(bb_msg_write_error);
                break;
            }
        }

        total += rd;
        if (status < 0) { /* if we aren't copying till EOF... */
            size -= rd;
            if (!size) {
                /* 'size' bytes copied - all done */
                status = 0;
                break;
            }
        }
    }

    /* close it all down */
    close(fd_local);
    close(nFdData);
    if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
        ftp_die(NULL, buf);
    }
    ftpcmd("QUIT", NULL, control_stream, buf);

out:
    return status ? -1 : total;
}
Example #7
0
static int ftp_receive(ftp_host_info_t *server, FILE *control_stream, const char *local_path, char *server_path)
{
    char buf[512];
    /* I think 'filesize' usage here is bogus. Let's see... */
    //off_t filesize = -1;
#define filesize ((off_t)-1)
    int fd_data;
    int fd_local = -1;
    off_t beg_range = 0;
    /* Connect to the data socket */
    if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
        ftp_die("PASV", buf);
    }

    fd_data = xconnect_ftpdata(server, buf);
    if (ftpcmd("SIZE", server_path, control_stream, buf) == 213) {
        //filesize = BB_STRTOOFF(buf + 4, NULL, 10);
        //if (errno || filesize < 0)
        //  ftp_die("SIZE", buf);
    } else {
        do_continue = 0;
    }

    if (LONE_DASH(local_path)) {
        fd_local = STDOUT_FILENO;
        do_continue = 0;
    }

    if (do_continue) {
        struct stat sbuf;
        if (lstat(local_path, &sbuf) < 0) {
            //bb_error_msg_and_die("lstat");
            printf("lstat error.");
            exit - 1;
        }
        if (sbuf.st_size > 0) {
            beg_range = sbuf.st_size;
        } else {
            do_continue = 0;
        }
    }

    if (do_continue) {
        sprintf(buf, "REST %""1""d", (int)beg_range);
        if (ftpcmd(buf, NULL, control_stream, buf) != 350) {
            do_continue = 0;
        } else {
            //if (filesize != -1)
            //  filesize -= beg_range;
        }
    }

    if (ftpcmd("RETR", server_path, control_stream, buf) > 150) {
        ftp_die("RETR", buf);
    }

    /* only make a local file if we know that one exists on the remote server */
    if (fd_local == -1) {
        if (do_continue) {
            fd_local = xopen(local_path, O_APPEND | O_WRONLY, 0666);
        } else {
            fd_local = xopen(local_path, O_CREAT | O_TRUNC | O_WRONLY, 0666);
        }
    }

    /* Copy the file */
    if (filesize != -1) {
        if (bb_copyfd_size(fd_data, fd_local, filesize) == -1)
            return EXIT_FAILURE;
    } else {
        if (bb_copyfd_eof(fd_data, fd_local) == -1)
            return EXIT_FAILURE;
    }

    /* close it all down */
    close(fd_data);
    if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
        ftp_die(NULL, buf);
    }
    ftpcmd("QUIT", NULL, control_stream, buf);

    return EXIT_SUCCESS;
}
Example #8
0
int ftp_msend2(ftp_host_info_t *server, FILE *control_stream,
		file_path_t *file_path, int file_cnt)
{
	struct stat sbuf;
	char buf[512];
	int fd_data;
	int* fd_local = NULL;
	int response; 
	int i = 0;

	if(file_cnt == 0)
		return -1;

	fd_local = (int*)malloc(sizeof(int)*file_cnt);
	if(fd_local == NULL){
		lib_error("[%s] malloc err!!", __FUNCTION__);
		return -1;
	}

	for(i=0;i<file_cnt;i++){

		memset(buf, 0, sizeof(buf));

		/*  Connect to the data socket */
		if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
			ftp_die("PASV", buf);
		}

		fd_data = connect_ftpdata(server, buf);
		if(fd_data < 0){
			lib_error("[%s] connect_ftpdata err!!", __FUNCTION__);
			return -1;
		}

		/* get the local file */
		fd_local[i] = STDIN_FILENO;
		if (NOT_LONE_DASH(file_path[i].local_path)) {

			//printf("%d: lc_path : %s, rt_path:%s\n",i, file_path[i].local_path, file_path[i].server_path);
			fd_local[i] = open(file_path[i].local_path, O_RDONLY, 0666);
			if(fd_local[i] < 0){
				lib_error("[%s] open err!!", __FUNCTION__);
				close(fd_data);
				return -1;
			}
			fstat(fd_local[i], &sbuf);

			sprintf(buf, "ALLO %"OFF_FMT"u", sbuf.st_size);
			response = ftpcmd(buf, NULL, control_stream, buf);
			switch (response) {
			case 200:
			case 202:
				break;
			default:
				ftp_die("ALLO", buf);
				break;
			}
		}

		response = ftpcmd("STOR", file_path[i].server_path, control_stream, buf);
		switch (response) {
		case 125:
		case 150:
			break;
		default:
			ftp_die("STOR", buf);
			close(fd_local[i]);
			close(fd_data);
			goto ftp_msend_quit_error;
		}

		/* transfer the file  */
		if (copyfd_eof(fd_local[i], fd_data, 0) == -1) {
			close(fd_data);
			close(fd_local[i]);
			goto ftp_msend_quit_error;
		}

		/* close it all down */
		close(fd_local[i]);

		close(fd_data);
		if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
			ftp_die("close", buf);
		}

	}

	ftpcmd("QUIT", NULL, control_stream, buf);
	return 0;

ftp_msend_quit_error:
	ftpcmd("QUIT", NULL, control_stream, buf);
	return -1;
}
Example #9
0
int ftp_send(ftp_host_info_t *server, FILE *control_stream,
		const char *server_path, char *local_path)
{
	struct stat sbuf;
	char buf[512];
	int fd_data;
	int fd_local;
	int response; 
	
	/*  Connect to the data socket */
	if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
		ftp_die("PASV", buf);
	}

	fd_data = connect_ftpdata(server, buf);
	if(fd_data < 0){
		lib_error("[%s] connect_ftpdata err!!", __FUNCTION__);
		return -1;
	}

	/* get the local file */
	fd_local = STDIN_FILENO;
	if (NOT_LONE_DASH(local_path)) {
		fd_local = open(local_path, O_RDONLY, 0666);
		if(fd_local < 0){
			lib_error("[%s] open err!!", __FUNCTION__);
			close(fd_data);
			return -1;
		}
		fstat(fd_local, &sbuf);

		sprintf(buf, "ALLO %"OFF_FMT"u", sbuf.st_size);
		response = ftpcmd(buf, NULL, control_stream, buf);
		switch (response) {
		case 200:
		case 202:
			break;
		default:
			ftp_die("ALLO", buf);
			break;
		}
	}
	response = ftpcmd("STOR", server_path, control_stream, buf);
	switch (response) {
	case 125:
	case 150:
		break;
	default:
		ftp_die("STOR", buf);
		close(fd_local);
		close(fd_data);
		return -1;

	}

	/* transfer the file  */
	if (copyfd_eof(fd_local, fd_data, 0) == -1) {
		close(fd_data);
		close(fd_local);
		return -1;
	}

	/* close it all down */
	close(fd_local);
	close(fd_data);
	if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
		ftp_die("close", buf);
	}
	//ftpcmd("NOOP", NULL, control_stream, buf);
	ftpcmd("QUIT", NULL, control_stream, buf);

	return 0;
}