static gboolean terminateProcessIO (GIOChannel *channel, GIOCondition condition, gpointer data) { Client *c; char *str; gsize len; GError *err; c = (Client *) data; g_return_val_if_fail (c != NULL, FALSE); str = NULL; len = 0; err = NULL; if (condition & G_IO_IN) { g_io_channel_read_to_end (channel, &str, &len, &err); if (err) { g_warning (_("Error reading data from child process: %s\n"), err->message); g_error_free (err); } if (len > 0) { if (!g_ascii_strncasecmp (str, "yes", 3)) { clientTerminate (c); } } g_free (str); } terminateCloseDialog (c); return FALSE; }
int main(int argc, char **argv) { BIO *sslbio; SSL_CTX *ctx; SSL *ssl; //SSL_METHOD *meth; unsigned long totl; int i, p; char hostname[BUF_SIZE + 1]; char server[16]; char choice; int ret; if (argc != 2) { printf("Usage: %s ClientName\n", argv[0]); printf("eg: '%s client1'\n", argv[0]); return -1; } if (strlen(argv[1]) >= NAME_SIZE) { fprintf(stderr, "%s is too long! \nPick a shorter client name.\n",argv[1]); } else { strcpy(CLIENT_NAME, argv[1]); } printf("client name: %s\n", CLIENT_NAME); /* Formatting required certificates for client ... certificates are matched to client with file names */ int length = strlen(CLIENT_NAME) + 10; char CLIENT_CERT_FILE2[length]; strcpy(CLIENT_CERT_FILE2, "cert/"); strcat(CLIENT_CERT_FILE2, CLIENT_NAME); strcat(CLIENT_CERT_FILE2, ".pem"); printf("This client CERT file is required: %s\n", CLIENT_CERT_FILE2); // Checking for required certificate if( access( CLIENT_CERT_FILE2, F_OK ) != -1 ) { // file exists printf("CERT file verified present\n"); } else { // file doesn't exist printf("CERT NOT FOUND....\n" "Perhaps this client does not have valid\n" "certificates present at this location\n" ">>> ./%s\n",CLIENT_CERT_FILE2); exit(4); } char CLIENT_KEY_FILE2[length]; strcpy(CLIENT_KEY_FILE2, "cert/"); strcat(CLIENT_KEY_FILE2, CLIENT_NAME); strcat(CLIENT_KEY_FILE2, ".key"); printf("This client KEY file is required: %s\n", CLIENT_KEY_FILE2); // Checking for required certificate if( access( CLIENT_KEY_FILE2, F_OK ) != -1 ) { // file exists printf("KEY file verified present\n\n"); } else { // file doesn't exist printf("KEY NOT FOUND....\n" "Perhaps this client does not have valid" "certificates present at this location\n" ">>> ./%s\n",CLIENT_KEY_FILE2); exit(4); } /* Give initial menu to user; get hostname for connection */ choice = getchoice("Please select an action", imenu); printf("You have chosen: %c\n", choice); if (choice == 'q') { printf("Ending Program... Goodbye.\n"); } else // choice == 'a' { printf("Initializing connection...\n"); // NOTE: 45 is the max length of a IPv4 address getInput(server, "Enter server hostname to connect \n (e.g., '127.0.0.1')", 15); SSL_library_init(); ERR_load_BIO_strings(); ERR_load_SSL_strings(); SSL_load_error_strings(); OpenSSL_add_all_algorithms(); ctx = SSL_CTX_new(SSLv3_client_method()); // ctx = SSL_CTX_new(SSLv3_method()); //ctx = SSL_CTX_new(meth); assert(ctx != NULL); /* Verify the server */ SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL); /* Load CA Certificate */ if (!SSL_CTX_load_verify_locations(ctx, CA_CERT_FILE, NULL)) { printf("Load CA file failed.\r\n"); //goto free_ctx; BIO_free_all(sslbio); SSL_CTX_free(ctx); return 0; } /* Load Client Certificate with Public Key */ if (SSL_CTX_use_certificate_file(ctx, CLIENT_CERT_FILE2, SSL_FILETYPE_PEM) <= 0) { ERR_print_errors_fp(stdout); printf("ssl_ctx_use_certificate_file failed.\r\n"); //goto free_ctx; BIO_free_all(sslbio); SSL_CTX_free(ctx); return 0; } /* Load Private Key */ if (SSL_CTX_use_PrivateKey_file(ctx, CLIENT_KEY_FILE2, SSL_FILETYPE_PEM) <= 0) { ERR_print_errors_fp(stdout); printf("ssl_ctx_use_privatekey_file failed.\r\n"); //goto free_ctx; BIO_free_all(sslbio); SSL_CTX_free(ctx); return 0; } /* Check the validity of Private Key */ if (!SSL_CTX_check_private_key(ctx)) { ERR_print_errors_fp(stdout); printf("ssl_ctx_check_private_key failed.\r\n"); //goto free_ctx; BIO_free_all(sslbio); SSL_CTX_free(ctx); return 0; } /* Create the connection */ sslbio = BIO_new_ssl_connect(ctx); /* Get SSL from sslbio */ BIO_get_ssl(sslbio, &ssl); /* Set the SSL mode into SSL_MODE_AUTO_RETRY */ SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY); ////////////////////////////////////////////////// // NOTE: Port# hardcoded here; change if necessary ////////////////////////////////////////////////// BIO_set_conn_port(sslbio, "7777"); BIO_set_conn_hostname(sslbio, server); /* Request Connection */ if(BIO_do_connect(sslbio) <= 0) { fprintf(stderr, "Error attempting to connect\n"); ERR_print_errors_fp(stderr); BIO_free_all(sslbio); SSL_CTX_free(ctx); return 0; } else { printf("Connection to server successful!\n"); } /* Verify Server Certificate Validity */ if(SSL_get_verify_result(ssl) != X509_V_OK) { printf("Certificate Verification Error: %ld\n", SSL_get_verify_result(ssl)); BIO_free_all(sslbio); SSL_CTX_free(ctx); return 0; } else { printf("verify server cert successful\n"); } //Send hostname to server printf("Sending client name to server.\n"); BIO_write(sslbio, CLIENT_NAME, strlen(CLIENT_NAME)); do { choice = getchoice("Please select an action", menu); printf("You have chosen: %c\n", choice); if (choice == 'a') { printf("Check-in function will be executed\n"); choiceProcess (sslbio, buffer, choice); ret = checkin_file(ssl, sslbio, buffer); } else if (choice == 'b') { printf("Check-out function will be executed\n"); choiceProcess (sslbio, buffer, choice); ret = checkout_file(ssl, sslbio, buffer); } else if (choice == 'c') { printf("Delegate function will be executed\n"); choiceProcess (sslbio, buffer, choice); } else if (choice == 'd') { printf("Safe-delete function will be executed\n"); choiceProcess (sslbio, buffer, choice); } else { printf("Terminate function will be executed\n"); } } while (choice != 'q'); /* Terminate the connection by sending message */ clientTerminate (sslbio, buffer); /* Close the connection and free the context */ BIO_ssl_shutdown(sslbio); BIO_free_all(sslbio); SSL_CTX_free(ctx); } return 0; }