static List * menu_delete_item (List *products) { Product *product; int sure; if (products == NULL) { printf ("There are no products to delete\n"); return products; } product = prompt_for_id (products, "Enter the ID of the product to delete: "); printf ("Product: %s\n", product->name); sure = prompt_y_n ("Are you sure you want to delete this product"); if (sure) { products = list_remove (products, product); product_free (product); } else { printf ("The product was not deleted\n"); } return products; }
static int main_with_input (const char *cmd, const char *id_str) { unsigned int id; List *products; Product *product; int error = 0; products = db_load_products (); id = (unsigned int) atoi (id_str); product = product_list_find (products, id); if (product == NULL && toupper (*cmd) != 'N' && toupper (*cmd) != 'L') { printf ("Product with ID %d does not exist\n", id); error = 1; } if (product != NULL && toupper (*cmd) == 'N') { printf ("Product with ID %d already exists\n", id); error = 1; } if (error) { list_foreach (products, LIST_FUNC (product_free)); list_free (products); return 1; } switch (toupper (*cmd)) { case 'N': product = product_new (id, NULL, 0, 0, 0); products = list_append (products, product); /* fall through */ case 'E': products = edit_item (products, product); db_write_products (products); break; case 'V': view_product (product); break; case 'D': view_product (product); if (prompt_y_n ("Delete this item")) { products = list_remove (products, product); product_free (product); db_write_products (products); } break; case 'L': menu_list_items (products); break; } list_foreach (products, LIST_FUNC (product_free)); list_free (products); return 0; }
/* * Edits the given item, with prompts. * * Pass it a NULL product to prompt to create a new item. */ static List * edit_item (List *products, Product *product) { char input[INPUT_BUFFER_SIZE]; unsigned int id; int is_new; int want_edit = 0; while (product == NULL) { printf ("Enter an integer product ID: "); fflush (stdout); fgets (input, INPUT_BUFFER_SIZE, stdin); if (*input == 'Q' || *input == 'q') { return products; } id = (unsigned int) atoi (input); if (product_list_find (products, id) != NULL) { printf ("Product #%u already exists\n", id); printf ("Try another ID or type 'q' to escape\n"); continue; } product = product_new (id, NULL, 0, 0, 0); products = list_append (products, product); } is_new = (product->name == NULL); want_edit = is_new; if (!is_new) { printf ("Name: %s\n", product->name); want_edit = prompt_y_n ("Change name"); } if (want_edit) { printf ("Enter the product's name: "); fflush (stdout); fgets (input, INPUT_BUFFER_SIZE, stdin); product->name = strndup (input, strlen (input)); /* minus \n */ } want_edit = is_new; if (!is_new) { printf ("In stock: %d\n", product->in_stock); want_edit = prompt_y_n ("Change quantity"); } if (want_edit) { printf ("Enter quantity in stock: "); fflush (stdout); fgets (input, INPUT_BUFFER_SIZE, stdin); product->in_stock = atoi (input); } want_edit = is_new; if (!is_new) { printf ("Price: %0.2f\n", product->price); want_edit = prompt_y_n ("Change price"); } if (want_edit) { printf ("Enter price: "); fflush (stdout); fgets (input, INPUT_BUFFER_SIZE, stdin); product->price = (double) atof (input); } want_edit = is_new; if (!is_new) { printf ("Discount: %0.2f%%\n", product->discount); want_edit = prompt_y_n ("Change discount"); } if (want_edit) { printf ("Enter discount in %%: "); fflush (stdout); fgets (input, INPUT_BUFFER_SIZE, stdin); product->discount = (double) atof (input); } return products; }
int main(int argc, char **argv) { struct sockaddr_in srvaddr, cliaddr; socklen_t socketfd,clisocketfd; socklen_t clilen; WOLFSSL_CTX *wsslctx; WOLFSSL *sslconn; int portnum; const char *certpath; const char *privpath; std::string cliipaddr; std::string data; std::string dbpath; pid_t pid; clilen = sizeof(cliaddr); wolfSSL_Init(); if (argc == 4) { if (prompt_y_n("Create new user database?", "")) { do { std::cout << "Please specify a filename for the new database: "; std::getline(std::cin, dbpath); if (!access(dbpath.c_str(), F_OK)) { if (prompt_y_n("File already exists, overwrite?", "")) { break; } } else { break; } } while (true); std::string jsondat = "{ \"users\" : { } }"; std::ofstream outputfile; outputfile.open(dbpath); if (outputfile.is_open()) { outputfile << jsondat; outputfile.close(); std::cout << "Created new database file!" << std::endl; } else { std::cout << "Failed to create new database file!" << std::endl; } } else { std::cout << "Ok, please specify an existing user database" << std::endl; std::cout<<"Usage: "<<argv[0]<<" <port #> <certfile> <privkey> [userdb]"<<std::endl; return -1; } } else if (argc < 5) { std::cout<<"Usage: "<<argv[0]<<" <port #> <certfile> <privkey> [userdb]"<<std::endl; std::cout<<"If [userdb] is unspecified, we will create a new one"<<std::endl; return 1; } portnum = atoi(argv[1]); certpath = argv[2]; privpath = argv[3]; if (argc == 5) dbpath = std::string(argv[4]); if (portnum < 1 || portnum > 65535) { std::cout<<"Please choose a port in the range: 1-65535"<<std::endl; return 1; } /*userdb_file.open(dbpath); if (!userdb_file.is_open()) { std::cout<<"[-] Could not open user database"<<std::endl; return 1; }*/ if ( (socketfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ) { std::cout<<"Failed to initialize socket"<<std::endl; return -1; } memset((void*)&srvaddr,0,sizeof(srvaddr)); srvaddr.sin_family = AF_INET; srvaddr.sin_addr.s_addr = INADDR_ANY; srvaddr.sin_port = htons(portnum); if ( (wsslctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method())) == NULL ) { std::cout<<"Failed to create new WolfSSL CTX"<<std::endl; return -1; } if (wolfSSL_CTX_use_PrivateKey_file(wsslctx,privpath,SSL_FILETYPE_PEM) != SSL_SUCCESS) { std::cout<<"Failed to load SSL private key file"<<std::endl; return -2; } if (wolfSSL_CTX_use_certificate_file(wsslctx,certpath,SSL_FILETYPE_PEM) != SSL_SUCCESS) { std::cout<<"Failed to load SSL certificate file"<<std::endl; return -2; } if (bind(socketfd, (struct sockaddr *)&srvaddr, sizeof(srvaddr)) != 0) { std::cout<<"Failed to bind to port "<<portnum<<std::endl; return -3; } listen(socketfd,10); std::cout<<"[+] KeyLocker server started. Waiting for connections..."<<std::endl; while(1) { if ( (clisocketfd = accept(socketfd,(struct sockaddr *)&cliaddr,&clilen)) == -1 ) { std::cout<<"Failed to accept connection on socket"<<std::endl; //return -3; } if ( (pid=fork()) < 0 ) { std::cout<<"Fork failed"<<std::endl; return -4; } else if (pid > 0) { /* parent */ close(clisocketfd); waitpid(pid, 0, 0); continue; } else { /* child */ close(socketfd); //15 second timeout signal(SIGALRM,sighandler); alarm(15); cliipaddr = std::string(inet_ntoa(cliaddr.sin_addr)); std::cout<<"[+] Client connected from IP address: "<<cliipaddr <<std::endl; sslconn = start_ssl(wsslctx,clisocketfd,cliaddr); data = get_cli_data(sslconn); //shut alarm off alarm(0); process_data(data,dbpath,sslconn); close(clisocketfd); break; } usleep(1000); } //close(clisocketfd); wolfSSL_free(sslconn); wolfSSL_CTX_free(wsslctx); wolfSSL_Cleanup(); return 0; }