From 52721b73bb3336c7a7deb8cbd300f04a427e6226 Mon Sep 17 00:00:00 2001 From: Josh Miller Date: Wed, 25 Sep 2013 16:08:59 -0500 Subject: [PATCH] user specification of password and changed the default encryption status to unencrypted --- src/udtcat.cpp | 104 ++++++++++++++++++++++++++++++++++------- src/udtcat_client.cpp | 13 ++---- src/udtcat_server.cpp | 17 ++++--- src/udtcat_threads.cpp | 22 +++++---- 4 files changed, 115 insertions(+), 41 deletions(-) diff --git a/src/udtcat.cpp b/src/udtcat.cpp index 999e069..4f58dc2 100644 --- a/src/udtcat.cpp +++ b/src/udtcat.cpp @@ -30,7 +30,14 @@ using std::cerr; using std::endl; void usage(){ - fprintf(stderr, "usage: udtcat [udtcat options] [server ip] port\n"); + fprintf(stderr, "usage: udtcat [udtcat options] host port\n"); + fprintf(stderr, "options:\n"); + fprintf(stderr, "\t\t%s %s\t%s\n", "-l", "\t", "server"); + fprintf(stderr, "\t\t%s %s\t%s\n", "-n", "\t", "use encryption"); + fprintf(stderr, "\t\t%s %s\t%s\n", "-p", "password", "password string"); + fprintf(stderr, "\t\t%s %s\t%s\n", "-f", "path\t", "path to password"); + fprintf(stderr, "\t\t%s %s\t%s\n", "-v", "\t", "verbose"); + // fprintf(stderr, "\t\t%s %s\t%s\n", "", "", ""); exit(1); } @@ -42,7 +49,7 @@ void initialize_thread_args(thread_args *args){ args->udt_buff = BUFF_SIZE; args->udp_buff = BUFF_SIZE; args->mss = 8400; - args->use_crypto = 1; + args->use_crypto = 0; args->verbose = 0; } @@ -54,51 +61,116 @@ int main(int argc, char *argv[]){ thread_args args; initialize_thread_args(&args); - + int use_crypto = 0; + int verbose = 0; + char* path_to_password = NULL; + char* password = NULL; + // ----------- [ Read in options - while ((opt = getopt (argc, argv, "nlv")) != -1){ + while ((opt = getopt (argc, argv, "hvnlp:f:")) != -1){ switch (opt){ + case 'l': operation = SERVER; break; + case 'v': args.verbose = 1; break; + case 'n': - args.use_crypto = 0; + args.use_crypto = 1; + use_crypto = 1; + break; + + case 'p': + password = strdup(optarg); + break; + + case 'f': + path_to_password = strdup(optarg); break; + + case 'h': + usage(); + break; + default: fprintf(stderr, "Unknown command line arg. -h for help.\n"); usage(); exit(1); + + } + } + + if (use_crypto && (path_to_password && password)){ + fprintf(stderr, "error: Please specify either password or password file, not both.\n"); + exit(1); + } + + if (path_to_password){ + FILE*password_file = fopen(path_to_password, "r"); + if (!password_file){ + fprintf(stderr, "password file: %s.\n", strerror(errno)); + exit(1); } + + fseek(password_file, 0, SEEK_END); + long size = ftell(password_file); + fseek(password_file, 0, SEEK_SET); + password = (char*)malloc(size); + fread(password, 1, size, password_file); + } - // ----------- [ Setup ip + if (!use_crypto && password){ + fprintf(stderr, "warning: You've specified a password, but you don't have encryption turned on.\nProceeding without encryption.\n"); + } + + if (use_crypto && !password){ + fprintf(stderr, "Please either: \n (1) %s\n (2) %s\n (3) %s\n", + "include password in cli [-p password]", + "read on in from file [-f /path/to/password/file]", + "choose not to use encryption, remove [-n]"); + exit(1); + } + + // Setup host if (operation == CLIENT){ if (optind < argc){ args.ip = strdup(argv[optind++]); } else { - cerr << "error: Please specify server ip." << endl; + cerr << "error: Please specify server host." << endl; exit(1); } } - // ----------- [ Check port input + // Check port input if (optind < argc){ args.port = strdup(argv[optind++]); } else { cerr << "error: Please specify port num." << endl; exit(1); } - - // ----------- [ Initialize crypto - unsigned char* password = (unsigned char*) "12345"; - char* cipher = (char*) "aes-128"; - crypto enc(EVP_ENCRYPT, PASSPHRASE_SIZE, password, cipher); - crypto dec(EVP_DECRYPT, PASSPHRASE_SIZE, password, cipher); - args.enc = &enc; - args.dec = &dec; + + // Initialize crypto + if (use_crypto){ + + char* cipher = (char*) "aes-128"; + crypto enc(EVP_ENCRYPT, PASSPHRASE_SIZE, (unsigned char*)password, cipher); + crypto dec(EVP_DECRYPT, PASSPHRASE_SIZE, (unsigned char*)password, cipher); + memset(password, 0, strlen(password)); + args.enc = &enc; + args.dec = &dec; + + } else { + + args.enc = NULL; + args.dec = NULL; + + } + + // ----------- [ Spawn correct process if (operation == SERVER){ diff --git a/src/udtcat_client.cpp b/src/udtcat_client.cpp index ff455e7..43ef36c 100644 --- a/src/udtcat_client.cpp +++ b/src/udtcat_client.cpp @@ -40,7 +40,7 @@ int run_client(thread_args *args) { if (args->verbose) - fprintf(stderr, "Running client...\n"); + fprintf(stderr, "[client] Running client...\n"); char *ip = args->ip; char *port = args->port; @@ -71,7 +71,7 @@ int run_client(thread_args *args) if (args->verbose) - fprintf(stderr, "Creating socket...\n"); + fprintf(stderr, "[client] Creating socket...\n"); UDTSOCKET client; @@ -93,7 +93,7 @@ int run_client(thread_args *args) } if (args->verbose) - fprintf(stderr, "Connecting to server...\n"); + fprintf(stderr, "[client] Connecting to server...\n"); if (UDT::ERROR == UDT::connect(client, peer->ai_addr, peer->ai_addrlen)) { @@ -104,7 +104,7 @@ int run_client(thread_args *args) } if (args->verbose) - fprintf(stderr, "Creating receive thread...\n"); + fprintf(stderr, "[client] Creating receive thread...\n"); pthread_t rcvthread, sndthread; rs_args rcvargs; @@ -118,7 +118,7 @@ int run_client(thread_args *args) if (args->verbose) - fprintf(stderr, "Creating send thread...\n"); + fprintf(stderr, "[client] Creating send thread...\n"); rs_args send_args; @@ -139,9 +139,6 @@ int run_client(thread_args *args) pthread_create(&sndthread, NULL, senddata, &send_args); - if (args->verbose) - fprintf(stderr, "Setup complete.\n"); - void * retval; pthread_join(sndthread, &retval); diff --git a/src/udtcat_server.cpp b/src/udtcat_server.cpp index 2a01fbb..6e14a8f 100644 --- a/src/udtcat_server.cpp +++ b/src/udtcat_server.cpp @@ -36,9 +36,8 @@ int buffer_size; int run_server(thread_args *args){ - if (args->verbose) - fprintf(stderr, "Running server...\n"); + fprintf(stderr, "[server] Running server...\n"); char *port = args->port; int blast = args->blast; @@ -48,7 +47,7 @@ int run_server(thread_args *args){ if (args->verbose) - fprintf(stderr, "Starting UDT...\n"); + fprintf(stderr, "[server] Starting UDT...\n"); UDT::startup(); addrinfo hints; @@ -71,7 +70,7 @@ int run_server(thread_args *args){ if (args->verbose) - fprintf(stderr, "Creating socket...\n"); + fprintf(stderr, "[server] Creating socket...\n"); UDTSOCKET serv; serv = UDT::socket(res->ai_family, res->ai_socktype, res->ai_protocol); @@ -86,7 +85,7 @@ int run_server(thread_args *args){ if (args->verbose) - fprintf(stderr, "Binding socket...\n"); + fprintf(stderr, "[server] Binding socket...\n"); if (UDT::ERROR == UDT::bind(serv, res->ai_addr, res->ai_addrlen)) { @@ -108,7 +107,7 @@ int run_server(thread_args *args){ if (args->verbose) - fprintf(stderr, "Listening for client...\n"); + fprintf(stderr, "[server] Listening for client...\n"); if (UDT::INVALID_SOCK == (recver = UDT::accept(serv, (sockaddr*)&clientaddr, &addrlen))) { @@ -118,7 +117,7 @@ int run_server(thread_args *args){ } if (args->verbose) - fprintf(stderr, "New client connection...\n"); + fprintf(stderr, "[server] New client connection...\n"); char clienthost[NI_MAXHOST]; char clientservice[NI_MAXSERV]; @@ -128,7 +127,7 @@ int run_server(thread_args *args){ if (args->verbose) - fprintf(stderr, "Creating receve thread...\n"); + fprintf(stderr, "[server] Creating receve thread...\n"); rs_args rcvargs; rcvargs.usocket = new UDTSOCKET(recver); @@ -140,7 +139,7 @@ int run_server(thread_args *args){ pthread_detach(rcvthread); if (args->verbose) - fprintf(stderr, "Creating send thread.\n"); + fprintf(stderr, "[server] Creating send thread.\n"); rs_args send_args; send_args.usocket = new UDTSOCKET(recver); diff --git a/src/udtcat_threads.cpp b/src/udtcat_threads.cpp index 2707b72..c2ae039 100644 --- a/src/udtcat_threads.cpp +++ b/src/udtcat_threads.cpp @@ -57,7 +57,10 @@ void* recvdata(void * _args) rs_args * args = (recv_args*)_args; if (args->verbose) - fprintf(stderr, "Initializing receive thread...\n"); + fprintf(stderr, "[recv thread] Initializing receive thread...\n"); + + if (args->verbose && args->use_crypto) + fprintf(stderr, "[recv thread] Receive encryption is on.\n"); UDTSOCKET recver = *args->usocket; @@ -70,9 +73,8 @@ void* recvdata(void * _args) exit(EXIT_FAILURE); } - if (args->verbose) - fprintf(stderr, "Checking encryption...\n"); + fprintf(stderr, "[recv thread] Checking encryption...\n"); long remote_ssl_version = 0; int rs = UDT::recv(recver, (char*)&remote_ssl_version, sizeof(long), 0); @@ -109,7 +111,7 @@ void* recvdata(void * _args) int crypto_cursor; if (args->verbose) - fprintf(stderr, "Listening on receive thread.\n"); + fprintf(stderr, "[recv thread] Listening on receive thread.\n"); if(args->use_crypto) { while(true) { @@ -206,10 +208,14 @@ void* senddata(void* _args) { rs_args * args = (rs_args*) _args; - UDTSOCKET client = *(UDTSOCKET*)args->usocket; if (args->verbose) - fprintf(stderr, "Initializing send thread...\n"); + fprintf(stderr, "[send thread] Initializing send thread...\n"); + + UDTSOCKET client = *(UDTSOCKET*)args->usocket; + + if (args->verbose && args->use_crypto) + fprintf(stderr, "[send thread] Send encryption is on.\n"); char* outdata = (char*)malloc(BUFF_SIZE*sizeof(char)); int crypto_buff_len = BUFF_SIZE / N_CRYPTO_THREADS; @@ -218,7 +224,7 @@ void* senddata(void* _args) int bytes_read; if (args->verbose) - fprintf(stderr, "Sending encryption status...\n"); + fprintf(stderr, "[send thread] Sending encryption status...\n"); long local_openssl_version; if (args->use_crypto) @@ -230,7 +236,7 @@ void* senddata(void* _args) if (args->verbose) - fprintf(stderr, "Send thread listening on stdin.\n"); + fprintf(stderr, "[send thread] Send thread listening on stdin.\n"); if (args->use_crypto){