Example #1
0
/* Get list of files 
 * opt:
 * 1 - NLST
 * 2 - LIST */
void ftp_list(ftp_t *ftp, char *path, int opt) {
	FILE *data;
	char buffer[MAX_STR];

	ftp->dataport = ftp_getdataport(ftp);
		
	switch(opt) {
		case 1:
			fprintf(ftp->FD, "LIST %s\r\n", path);
		case 2:
			fprintf(ftp->FD, "NLST %s\r\n", path);
	}
	/* SIGALRM */
	ftp_getline(ftp);

	data = tcp_connect2(ip, ftp->dataport, "r");

	while ( fgets(buffer, sizeof(buffer), data) != NULL ) {
		/* dir*/
		if ( buffer[0] == 0x64 )
			print(3, "%s%s%s", BLUE,buffer, END);
		/* symlink*/
		if ( buffer[0] == 0x6c )
			print(3, "%s%s%s", CYN,buffer,END);
		/* File */
		else if ( buffer[0] == 0x2d )
			print(3, "%s%s%s", WHT,buffer, END);
	}
	fclose(data);
	close(dfd);
	/* No need to close fd the SIGALRM do it */
}
Example #2
0
File: ftp.c Project: MauriceZ/skod
/* download files/folders */
void ftp_download(ftp_t *ftp, char *path) {
	char *line = NULL;
	char buffer[MAX_STR];
	FILE *data;
	int co = 0;

	ftp->alarm_sec = 3;
	fprintf(ftp->FD, "SIZE %s\r\n", path);
	line = ftp_getline(ftp);
	ftp->code = atoi(line);

	if ( ftp->code == 213 ) {
		ftp_mkcon(ftp);
		ftp_download_single(ftp, path, 1);
	}
	else {
		/* TODO: add download time */
		print(3, ">>> %sStart downloading from%s %s\'%s\'%s ...\n",YEL,END,GREEN,path,END); 
		ftp->dataport = ftp_getdataport(ftp);
		fprintf(ftp->FD, "NLST %s\r\n", path);
		data = tcp_connect(ip, ftp->dataport, "r");
		ftp_getline(ftp);

		while ( fgets(buffer, sizeof(buffer), data)) {
			buffer[strlen(buffer)-2] = '\0';
			ftp_mkcon(ftp);
			ftp_download_single(ftp, buffer, 0);
			co++;
		}
		fclose(data);
		close(dfd);
		print(0, ">>> %sFinished! we have %s%s%d files (:%s", GREEN,END,
			YEL,co,END, path);
	}
}
Example #3
0
/* print contents of single file from the FTP server */
void ftp_cat(ftp_t *ftp, char *path) {
	char *line = NULL;
	FILE *data;
	char buffer[MAX_STR];

	ftp->dataport = ftp_getdataport(ftp);
	fprintf(ftp->FD, "TYPE I\r\n");
	line = ftp_getline(ftp);
	if ( atoi(line) == 500 )
		die("Failed to set TYPE.");
	fprintf(ftp->FD, "RETR %s\r\n", path);
	/* SIGALRM */
	ftp_getline(ftp);

	data = tcp_connect2(ip, ftp->dataport, "r");
	while(( fgets(buffer, sizeof(buffer), data)) != NULL ) {
		printf("%s", buffer);
	}
	fclose(data);
	close(dfd);
}
Example #4
0
/* upload single file to the FTP server */
void ftp_upload_single(ftp_t *ftp, char *path) {
	char *line = NULL;
	char *filename = NULL;
	FILE *data;
	FILE *fp;
	char buffer[MAX_STR];
	long int dsize = 0;
	long int rsize = 0;
	long int fsize = 0;
	data_t d1;
	data_t d2;

	if ( path[0] == 0x2f || path[strlen(path)-1] == 0x2f )
		filename = strrchr(path, '/') + 1;
	else
		filename = path;

	if (( fp = fopen(path, "r")) == NULL )
		die("Cannot read %s.", path);

	fseek(fp, 0L, SEEK_END);
	fsize = ftell(fp);
	fseek(fp, 0L, SEEK_SET);

	ftp->dataport = ftp_getdataport(ftp);
	fprintf(ftp->FD, "TYPE I\r\n");
	line = ftp_getline(ftp);
	if ( atoi(line) == 500 )
		die("Failed to set TYPE.");

	fprintf(ftp->FD, "STOR %s\r\n", path);

	/* SIGALRM */
	line = ftp_getline(ftp);
	data = tcp_connect2(ip, ftp->dataport, "w");

	calc_bytes(&d1, fsize);
	while(( rsize = fread(buffer, 1, sizeof(buffer), fp) ) > 0 ) {
		if ( buffer[rsize +1] == '\r' )
			buffer[rsize +1] = '\0';
		dsize += fwrite(buffer, 1, rsize, data);
		calc_bytes(&d2, dsize);
		fprintf(stdout, "%s:: Uploading %s\'%s\'%s %s(%.2f%c) %.2f%c%s\r",
				WHT,GREEN,
				filename,END,
				YEL,
				d1.bytes,
				d1.bytes_postfix,
				d2.bytes,
				d2.bytes_postfix,
				END);

		fflush(stdout);
	}
	putchar(0x0a);

	if ( fsize == dsize )
		print(0, "File %s\'%s\'%s saved on the server.", GREEN,filename,END);
	else
		print(1, "File %s\'%s\'%s is corrupted, try uploading it again?", RED,filename,END);
		
	fclose(fp);
	fclose(data);
	close(dfd);
}
Example #5
0
/* download single file from the FTP server */
void ftp_download_single(ftp_t *ftp, char *path) {
	char *line = NULL;
	FILE *data;
	FILE *fp;
	char buffer[MAX_STR];
	char *filename = NULL;
	long int dsize = 0;
	int rsize = 0;
	long int fsize = 0;
	long int lsize = 0;
	data_t d1;
	data_t d2;

	/* In case the URL have more then one / */
	if ( path[strlen(path)-1] == 0x2f )
		path[strlen(path)-1] = 0x00;

	/* strrchr return the last string */
	filename = strrchr(path, '/') + 1;

	/* Check if file exists before downloading */
	if (util_file_exists(filename) == 1 ) {
		print(1, "%s\'%s\' already exists.%s", WHT,filename,END);
		exit(0);
	}

	fsize = (int)ftp_size(ftp, path);
	ftp->dataport = ftp_getdataport(ftp);
	fprintf(ftp->FD, "TYPE I\r\n");
	line = ftp_getline(ftp);
	if ( atoi(line) == 500 )
		die("Failed to set TYPE.");
	fprintf(ftp->FD, "RETR %s\r\n", path);

	/* SIGALRM */
	line = ftp_getline(ftp);
	data = tcp_connect2(ip, ftp->dataport, "r");
		
	if (( fp = fopen(filename, "w")) == NULL )
		die("Cannot create %s.", filename);

	calc_bytes(&d1, fsize);
	while(( rsize = fread(buffer, 1, sizeof(buffer), data) ) > 0 ) {
		if ( buffer[rsize +1] == '\r' )
			buffer[rsize +1] = '\0';
		dsize += fwrite(buffer, 1, rsize, fp);
		calc_bytes(&d2, dsize);
		print(3, "%s Downloading %s\'%s\'%s (%.2f%c) %.2f %c\b\b\b\b\b\r", WHT,GREEN,
				filename,END,
				d1.bytes, d1.bytes_postfix,
				d2.bytes, d2.bytes_postfix);
		fflush(stdout);
	}
	putchar(0x0a);
	fseek(fp, 0L, SEEK_END);
	lsize = ftell(fp);
	fseek(fp, 0L, SEEK_SET);
		
	/* All good */
	if ( lsize == fsize )
		print(0, "File %s\'%s\'%s saved.", GREEN,filename,END);
	else {
		/* inetutils <=1.9.4, 
		 * This bug discoverd by me in 05-10-2015,
		 *  SIZE command return file_size+23 bytes*/
		if ( lsize+23 == fsize ) {
			print(0, "File \'%s\' saved.", GREEN,filename,END);
			print(0, "%sYou have unpatched version of inetutils 1.x.x please upgrade.%s", YEL,END);
		}
		/* Bad */
		else
			print(1, "File %s\'%s\'%s is corrupted, try downloading it again?", RED,filename,END);
	}
	fclose(fp);
	fclose(data);
	close(dfd);
}