예제 #1
0
int main( int argc, char *argv[] )
{
	struct ftp_lite_server *source=0, *target=0;
	char *source_host=0, *source_file=0;
	char *target_host=0, *target_file=0;
	FILE *source_fp=0, *target_fp=0, *data=0;
	signed char c;
	int source_port=0, target_port=0;

	debug_config(argv[0]);

	while((c=getopt(argc,argv,"S:s:T:t:P:p:dh")) > -1) {
		switch(c) {
			case 'S':
				source_host = optarg;
				break;
			case 's':
				source_file = optarg;
				break;
			case 'T':
				target_host = optarg;
				break;
			case 't':
				target_file = optarg;
				break;
			case 'P':
				source_port = atoi(optarg);
				break;
			case 'p':
				target_port = atoi(optarg);
				break;
			case 'h':
				show_use(argv[0]);
				return 0;
			case 'd':
				debug_flags_set("ftp");
				break;
		}
	}

	if(source_host&&!source_file) {
		fprintf(stderr," -S requires -s \n");
		show_use(argv[0]);
		return 1;
	}

	if(target_host&&!target_file) {
		fprintf(stderr," -T requires -t \n");
		show_use(argv[0]);
		return 1;
	}

	if(!source_host) {
		if(source_file) {
			source_fp = fopen(source_file,"rb");
			if(!source_fp) {
				fprintf(stderr,"%s: couldn't open %s: %s\n",argv[0],source_file,strerror(errno));
				return 1;
			}
		} else {
			source_fp = stdin;
		}
	}

	if(!target_host) {
		if(target_file) {
			target_fp = fopen(target_file,"rb+");
			if(!target_fp) {
				fprintf(stderr,"%s: couldn't open %s: %s\n",argv[0],target_file,strerror(errno));
				return 1;
			}
		} else {
			target_fp = stdout;
		}
	}

	if(source_host) {
		source = ftp_lite_open_and_auth(source_host,source_port);
		if(!source) {
			fprintf(stderr,"%s: couldn't connect to %s: %s\n",argv[0],source_host,strerror(errno));
			return 1;
		}
	}

	if(target_host) {
		target = ftp_lite_open_and_auth(target_host,target_port);
		if(!target) {
			fprintf(stderr,"%s: couldn't log in to %s: %s\n",argv[0],target_host,strerror(errno));
			return 1;
		}
	}

	if(source&&target) {
		if(!ftp_lite_third_party_transfer(source,source_file,target,target_file)) {
			fprintf(stderr,"%s: transfer failed: %s\n",argv[0],strerror(errno));
			return 1;
		}
	} else if(source) {
		data = ftp_lite_get(source,source_file,0);
		if(!data) {
			fprintf(stderr,"%s: couldn't open %s: %s\n",argv[0],source_file,strerror(errno));
			return 1;
		}

		if(copy_stream_to_stream(data,target_fp)<0) {
			fprintf(stderr,"%s: couldn't copy data: %s\n",argv[0],strerror(errno));
			return 1;
		}
		fclose(data);
		ftp_lite_done(source);
	} else if(target) {
		data = ftp_lite_put(target,target_file,0,FTP_LITE_WHOLE_FILE);
		if(!data) {
			fprintf(stderr,"%s: couldn't open %s: %s\n",argv[0],target_file,strerror(errno));
			return 1;
		}

		if(copy_stream_to_stream(source_fp,data)<0) {
			fprintf(stderr,"%s: couldn't copy data: %s\n",argv[0],strerror(errno));
			return 1;
		}

		fclose(data);
		ftp_lite_done(target);
	} else {
		if(copy_stream_to_stream(source_fp,target_fp)<0) {
			fprintf(stderr,"%s: couldn't copy data: %s\n",argv[0],strerror(errno));
			return 1;
		}
	}

	if(source) ftp_lite_close(source);
	if(target) ftp_lite_close(target);

	if( source_fp && source_file ) fclose(source_fp);
	if( target_fp && target_file ) fclose(target_fp);

	return 0;
}
예제 #2
0
파일: auth_test.c 프로젝트: iheanyi/cctools
int main(int argc, char *argv[])
{
	struct link *link, *master;
	char *subject = 0, *type = 0;
	time_t stoptime;
	char line[1024];
	signed char c;
	int portnum = 30000;
	char *hostname = 0;
	int timeout = 30;

	debug_config(argv[0]);

	static struct option long_options[] = {
		{"auth", required_argument, 0, 'a'},
		{"port", required_argument, 0, 'p'},
		{"host", required_argument, 0, 'r'},
		{"help", required_argument, 0, 'h'},
		{"debug-file", required_argument, 0, 'o'},
		{"debug-rotate-max", required_argument, 0, 'O'},
		{"debug", required_argument, 0, 'd'},
        {0,0,0,0}
	};

	while((c = getopt_long(argc, argv, "a:p:r:d:o:O:", long_options, NULL)) > -1) {
		switch (c) {
		case 'p':
			portnum = atoi(optarg);
			break;
		case 'h':
			show_help(argv[0]);
			exit(0);
			break;
		case 'r':
			hostname = optarg;
			break;
		case 'd':
			debug_flags_set(optarg);
			break;
		case 'o':
			debug_config_file(optarg);
			break;
		case 'O':
			debug_config_file_size(string_metric_parse(optarg));
			break;
		case 'a':
			if(!auth_register_byname(optarg))
				fatal("couldn't register %s authentication", optarg);
			break;
		default:
			show_use(argv[0]);
			exit(1);
			break;
		}
	}

	if(hostname) {
		char addr[LINK_ADDRESS_MAX];

		stoptime = time(0) + timeout;

		if(!domain_name_cache_lookup(hostname, addr))
			fatal("unknown host name: %s", hostname);

		link = link_connect(addr, portnum, stoptime);
		if(!link)
			fatal("couldn't connect to %s:%d: %s", hostname, portnum, strerror(errno));

		if(auth_assert(link, &type, &subject, stoptime)) {
			printf("server thinks I am %s %s\n", type, subject);
			if(link_readline(link, line, sizeof(line), stoptime)) {
				printf("got message: %s\n", line);
			} else {
				printf("lost connection!\n");
			}
		} else {
			printf("unable to authenticate.\n");
		}

		link_close(link);

	} else {
		stoptime = time(0) + timeout;

		master = link_serve(portnum);
		if(!master)
			fatal("couldn't serve port %d: %s\n", portnum, strerror(errno));

		while(time(0) < stoptime) {
			link = link_accept(master, stoptime);
			if(!link)
				continue;

			if(auth_accept(link, &type, &subject, stoptime)) {
				time_t t = time(0);
				link_putfstring(link, "Hello %s:%s, it is now %s", stoptime, type, subject, ctime(&t));	/* ctime ends with newline */
			} else {
				printf("couldn't auth accept\n");
			}
			link_close(link);
		}
	}

	return 0;
}