/** * @brief Initialize the cfile system. Called once at application start. * * @param exe_dir Path to a file (not a directory) * @param cdrom_dir Path to a CD drive mount point (may be NULL) * * @return 0 On success * @return 1 On error */ int cfile_init(const char *exe_dir, const char *cdrom_dir) { // initialize encryption encrypt_init(); if (cfile_inited) { return 0; } char buf[CFILE_ROOT_DIRECTORY_LEN]; strncpy(buf, exe_dir, CFILE_ROOT_DIRECTORY_LEN - 1); buf[CFILE_ROOT_DIRECTORY_LEN - 1] = '\0'; size_t i = strlen(buf); // are we in a root directory? if(cfile_in_root_dir(buf)){ os::dialogs::Message(os::dialogs::MESSAGEBOX_ERROR, "FreeSpace2/Fred2 cannot be run from a drive root directory!"); return 1; } // This needs to be set here because cf_build_secondary_filelist assumes it to be true cfile_inited = 1; /* * Determine the executable's directory. Note that DIR_SEPARATOR_CHAR * is guaranteed to be found in the string else cfile_in_root_dir() * would have failed. */ char *p; p = strrchr(buf, DIR_SEPARATOR_CHAR); *p = '\0'; cfile_chdir(buf); // set root directory strncpy(Cfile_root_dir, buf, CFILE_ROOT_DIRECTORY_LEN-1); strncpy(Cfile_user_dir, os_get_config_path().c_str(), CFILE_ROOT_DIRECTORY_LEN-1); #ifdef SCP_UNIX // Initialize path of old pilot files extern const char* Osreg_user_dir_legacy; snprintf(Cfile_user_dir_legacy, CFILE_ROOT_DIRECTORY_LEN-1, "%s/%s/", getenv("HOME"), Osreg_user_dir_legacy); #endif for (i = 0; i < MAX_CFILE_BLOCKS; i++) { Cfile_block_list[i].type = CFILE_BLOCK_UNUSED; } // 32 bit CRC table init cf_chksum_long_init(); Cfile_cdrom_dir = cdrom_dir; cf_build_secondary_filelist(Cfile_cdrom_dir); return 0; }
/* * Function: Initialization routine called kstream encryption system. * * Parameters: * str - kstream to send abort message to. * * data - user data. */ int auth_init(kstream str, kstream_ptr data) { #ifdef ENCRYPTION encrypt_init(str, data); #endif return 0; }
gint nmp_wdd_read_data(char *info) { g_assert(info); wdd *wdd_info; gint head_size, size; guint ver_num; gint ret, err; if (!g_handler) { g_handler = encrypt_init(&g_wdd_passwd, &err); if (!g_handler) { nmp_warning("<NmpModWdd>wdd init failed, err = %d", err); return err; } } head_size = WDD_HEAD_LEN; ret = encrypt_read_data(g_handler, info, DATA_TYPE_1, head_size); if (ret != 0) { nmp_warning("<NmpModWdd>wdd read data error, maybe watchdog has " \ "been removed."); return ret; } wdd_info = (wdd *)info; if (strcmp((char *)(wdd_info->label.label), WDD_LABEL)) { nmp_warning("<NmpModWdd> label error, label:%s", wdd_info->label.label); return -2; } ver_num = ntohl(wdd_info->version.ver_num); if (ver_num == WDD_VERSION_1) { size = WDD_HEAD_LEN + sizeof(wdd_version1); } else { nmp_warning("<NmpModWdd> version error, version=%d", ver_num); return -1; } g_assert(size <= WDD_MAX_BUF_LEN); ret = encrypt_read_data(g_handler, info, DATA_TYPE_1, size); if (ret != 0) { nmp_warning("<NmpModWdd>wdd read data error, maybe watchdog has " \ "been removed."); return ret; } return 0; }
// initialize localization, if no language is passed - use the language specified in the registry void lcl_init(int lang_init) { atexit(lcl_xstr_close); char lang_string[128]; char *ret; int lang, idx; // initialize encryption encrypt_init(); // read the language from the registry if(lang_init < 0){ memset(lang_string, 0, 128); // default to DEFAULT_LANGUAGE (which should be english so we dont have to put German text // in tstrings in the #default section ret = os_config_read_string(NULL, "Language", DEFAULT_LANGUAGE); if(ret == NULL){ Int3(); strcpy(lang_string, DEFAULT_LANGUAGE); } else { strcpy(lang_string, ret); } // look it up lang = -1; for(idx=0; idx<LCL_NUM_LANGUAGES; idx++){ if(!stricmp(Lcl_languages[idx].lang_name, lang_string)){ lang = idx; break; } } if(lang < 0){ lang = 0; } } else { Assert((lang_init >= 0) && (lang_init < LCL_NUM_LANGUAGES)); lang = lang_init; } // language markers Lcl_pointer_count = 0; // associate the table string file lcl_ext_associate(TABLE_STRING_FILENAME); // set the language (this function takes care of setting up file pointers) lcl_set_language(lang); }
void main(int argc, char *argv[]) { switch (argc) { case 2: encrypt_init(); scramble_file(argv[1]); break; case 3: encrypt_init(); if ( !stricmp("-u", argv[1]) ) { unscramble_file(argv[2]); } else if ( !stricmp("-st", argv[1]) ) { scramble_file(argv[2], argv[2], PREPROCESS_SHIPS_TBL); } else if ( !stricmp("-wt", argv[1]) ) { scramble_file(argv[2], argv[2], PREPROCESS_WEAPONS_TBL); } else { scramble_file(argv[1], argv[2]); } break; case 4: encrypt_init(); if ( !stricmp("-u", argv[1]) ) { unscramble_file(argv[2], argv[3]); } else if ( !stricmp("-st", argv[1]) ) { scramble_file(argv[2], argv[3], PREPROCESS_SHIPS_TBL); } else if ( !stricmp("-wt", argv[1]) ) { scramble_file(argv[2], argv[3], PREPROCESS_WEAPONS_TBL); } else { print_instructions(); } break; default: print_instructions(); return; } }
void auth_encrypt_init(const char *local, const char *remote, const char *name, int server) { RemoteHostName = remote; LocalHostName = local; #ifdef AUTHENTICATION auth_init(name, server); #endif #ifdef ENCRYPTION encrypt_init(name, server); #endif if (UserNameRequested) { free(UserNameRequested); UserNameRequested = 0; } }
void auth_encrypt_init (char *local, char *remote, char *principal, char *name, int server) { RemoteHostName = remote; LocalHostName = local; ServerPrincipal = principal; (void) name; (void) server; /* silence gcc */ #if defined AUTHENTICATION auth_init (name, server); #endif #ifdef ENCRYPTION encrypt_init (name, server); #endif /* ENCRYPTION */ free (UserNameRequested); UserNameRequested = NULL; }
// initialize localization, if no language is passed - use the language specified in the registry void lcl_init(int lang_init) { char lang_string[128]; const char *ret; int lang, idx, i; // initialize encryption encrypt_init(); // setup English Lcl_languages.push_back(Lcl_builtin_languages[FS2_OPEN_DEFAULT_LANGUAGE]); // check string.tbl to see which languages we support try { parse_stringstbl_quick("strings.tbl"); } catch (const parse::ParseException& e) { mprintf(("TABLES: Unable to parse '%s'! Error message = %s.\n", "strings.tbl", e.what())); } parse_modular_table(NOX("*-lcl.tbm"), parse_stringstbl_quick); // if the only language we have at this point is English, we need to setup the builtin languages as we might be dealing with an old style strings.tbl // which doesn't support anything beyond the builtin languages. Note, we start at i = 1 because we added English above. if ((int)Lcl_languages.size() == 1) { for (i=1; i<NUM_BUILTIN_LANGUAGES; i++) { Lcl_languages.push_back(Lcl_builtin_languages[i]); } } // read the language from the registry if(lang_init < 0){ memset(lang_string, 0, 128); // default to DEFAULT_LANGUAGE (which should be English so we don't have to put German text // in tstrings in the #default section) ret = os_config_read_string(NULL, "Language", Lcl_languages[FS2_OPEN_DEFAULT_LANGUAGE].lang_name); if(ret == NULL){ Error(LOCATION, "Default language not found."); } strcpy_s(lang_string, ret); // look it up lang = -1; for(idx = 0; idx < (int)Lcl_languages.size(); idx++){ if(!stricmp(Lcl_languages[idx].lang_name, lang_string)){ lang = idx; break; } } if(lang < 0){ lang = 0; } } else { Assert((lang_init >= 0) && (lang_init < (int)Lcl_languages.size())); lang = lang_init; } // set the language (this function takes care of setting up file pointers) lcl_set_language(lang); }
int main(int argc, char* argv[]) { #if 1 // For debug with segment fault struct sigaction sa; sa.sa_handler = backtrace_info; sigaction(SIGSEGV, &sa, NULL); // ignore SIGPIPE signal(SIGPIPE, SIG_IGN); signal(SIGCHLD, SIG_IGN); signal(SIGABRT, SIG_IGN); #endif int opt_g = 0; memset(&cltopt, 0, sizeof(CLT_OPT)); cltopt.C_TYPE = C_USR; while( (opt_g = getopt(argc, argv, "Dh")) != -1 ) { switch(opt_g) { case 'D': cltopt.C_TYPE = C_DAEMON; break; case 'h': default: usage(); exit(EXIT_SUCCESS); } } if(load_settings_client(&cltopt) == RET_NO) { st_d_error("加载配置文件settings.json出错!"); exit(EXIT_FAILURE); } OpenSSL_add_ssl_algorithms(); SSL_load_error_strings(); SSL_library_init(); //SSL_library_init() always returns "1" //int sd_id128_from_string(const char *s, sd_id128_t *ret); sd_id128_get_machine(&cltopt.mach_uuid); gethostname(cltopt.hostname, sizeof(cltopt.hostname)); st_d_print("CURRENT MACH_ID:%s, HOSTNAME:%s", SD_ID128_CONST_STR(cltopt.mach_uuid), cltopt.hostname); if (cltopt.C_TYPE == C_DAEMON) { cltopt.session_uuid = cltopt.mach_uuid; st_d_print("PLEASE REMEMEBER SET MACH_ID FOR USER TYPE!"); } dump_clt_opts(&cltopt); /*带配置产生event_base对象*/ struct event_config *cfg; cfg = event_config_new(); event_config_avoid_method(cfg, "select"); //避免使用select event_config_require_features(cfg, EV_FEATURE_ET); //使用边沿触发类型 base = event_base_new_with_config(cfg); event_config_free(cfg); st_d_print("当前复用Event模式: %s", event_base_get_method(base)); // epoll /*连接服务器*/ int srv_fd = socket(AF_INET, SOCK_STREAM, 0); unsigned int optval = 1; setsockopt(srv_fd, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof(optval));//禁用NAGLE算法 if(sc_connect_srv(srv_fd) != RET_YES) { SYS_ABORT("连接服务器失败!"); } if(cltopt.C_TYPE == C_DAEMON) { if (sc_daemon_init_srv(srv_fd) != RET_YES) SYS_ABORT("(Daemon) 服务器返回错误!"); } else { if (sc_usr_init_srv(srv_fd) != RET_YES) SYS_ABORT("(Usr) 服务器返回错误!"); } st_d_print("客户端连接服务器OK!"); /** * USR 建立本地Listen侦听套接字 */ if (cltopt.C_TYPE == C_USR) { int i = 0; for (i=0; i<MAX_PORT_NUM; i++) { if (cltopt.maps[i].usrport) { struct evconnlistener *listener; struct sockaddr_in sin; memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = htonl(0); sin.sin_port = htons(cltopt.maps[i].usrport); /* Port Num */ listener = evconnlistener_new_bind(base, accept_conn_cb, &cltopt.maps[i], LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE, -1/*backlog 连接无限制*/, (struct sockaddr*)&sin, sizeof(sin)); if (!listener) { st_d_error("[USR]创建侦听套接字失败 %d:%d", cltopt.maps[i].usrport, cltopt.maps[i].daemonport); continue; } evconnlistener_set_error_cb(listener, accept_error_cb); st_d_print("[USR]创建侦听套接字 %d:%d OK", cltopt.maps[i].usrport, cltopt.maps[i].daemonport); } else break; } } encrypt_init(SD_ID128_CONST_STR(cltopt.mach_uuid), cltopt.enc_key); if (cltopt.C_TYPE == C_DAEMON && cltopt.ss5_port ) { /** * 目前只考虑将sockets5代理使用线程池来处理,其它的端口暴露 * 基本都是长连接,不单独处理 */ cltopt.thread_num = 5; cltopt.main_thread_id = pthread_self(); cltopt.thread_objs = (P_THREAD_OBJ)calloc(sizeof(THREAD_OBJ), cltopt.thread_num); if (!cltopt.thread_objs) { SYS_ABORT("申请THREAD_OBJ出错"); } sc_create_ss5_worker_threads(cltopt.thread_num, cltopt.thread_objs); st_d_print("[DAEMON]创建sockets5代理端口:%d", cltopt.ss5_port); struct evconnlistener *listener; struct sockaddr_in sin; memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = htonl(0); sin.sin_port = htons(cltopt.ss5_port); /* Port Num */ listener = evconnlistener_new_bind(base, ss5_accept_conn_cb, NULL, LEV_OPT_LEAVE_SOCKETS_BLOCKING/* 阻塞 */|LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE, -1/*backlog 连接无限制*/, (struct sockaddr*)&sin, sizeof(sin)); if (!listener) { st_d_error("[DAEMON]sockets5代理创建侦听套接字失败 %d", cltopt.ss5_port); exit(EXIT_FAILURE); } evconnlistener_set_error_cb(listener, accept_error_cb); st_d_print("[DAEMON]sockets5代理创建侦听套接字OK %d", cltopt.ss5_port); } if (cltopt.C_TYPE == C_DAEMON && cltopt.dns_port) { st_d_print("[DAEMON]创建DNS代理端口:%d", cltopt.dns_port); if (cltopt.dns_port != 53) { st_d_print("[DAEMON]请注意标准DNS侦听#53端口!"); } int dns_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (dns_socket < 0 ) { st_d_error("Create DNS socket error!"); exit(EXIT_FAILURE); } unsigned int optval = 1; setsockopt(dns_socket, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof(optval));//禁用NAGLE算法 setsockopt(dns_socket, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval)); evutil_make_socket_closeonexec(dns_socket); evutil_make_socket_nonblocking(dns_socket); struct sockaddr_in sin; memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = htonl(0); sin.sin_port = htons(cltopt.dns_port); /* Port Num */ if (bind(dns_socket, (struct sockaddr *)&sin, sizeof(sin))) { st_d_error("Bind DNS socket error!"); exit(EXIT_FAILURE); } cltopt.dns_transid_port_map = (unsigned short*)malloc(sizeof(unsigned short) * 0xFFFF); if (!cltopt.dns_transid_port_map) { st_d_error("Malloc for requestid-port failed!"); exit(EXIT_FAILURE); } P_PORTTRANS p_trans = sc_create_trans(cltopt.dns_port); if (!p_trans) { st_d_error("本地无空闲TRANS!"); exit(EXIT_FAILURE); } p_trans->is_enc = 1; p_trans->l_port = cltopt.dns_port; encrypt_ctx_init(&p_trans->ctx_enc, p_trans->l_port, cltopt.enc_key, 1); encrypt_ctx_init(&p_trans->ctx_dec, p_trans->l_port, cltopt.enc_key, 0); // 建立DNS UDP事件侦听 p_trans->extra_ev = event_new(base, dns_socket, EV_READ | EV_PERSIST, dns_client_to_proxy_cb, p_trans); int dns_srv_fd = socket(AF_INET, SOCK_STREAM, 0); if(sc_connect_srv(dns_srv_fd) != RET_YES) { SYS_ABORT("连接服务器失败!"); } sc_daemon_dns_init_srv(dns_srv_fd, p_trans->l_port, 12333); evutil_make_socket_nonblocking(dns_srv_fd); // later enabled //event_add(p_trans->extra_ev, NULL) != 0); p_trans->srv_bev = bufferevent_socket_new(base, dns_srv_fd, BEV_OPT_CLOSE_ON_FREE); bufferevent_setcb(p_trans->srv_bev, dns_bufferread_cb_enc, NULL, dns_bufferevent_cb, p_trans); st_d_print("[DAEMON]DNS代理创建侦听套接字OK %d", cltopt.dns_port); } sc_set_eventcb_srv(srv_fd, base); /** * Main Loop Here */ event_base_loop(base, 0); event_base_free(base); st_d_print("程序退出!!!!"); return 0; }
int main(int argc, char **argv) { int sd; int rc; int result; data_packet send_packet; int bytes_to_send; char input[MAX_INPUT_BUFFER]; char input_buffer[MAX_INPUT_BUFFER]; char *temp_ptr; char host_name[MAX_HOSTNAME_LENGTH]; char svc_description[MAX_DESCRIPTION_LENGTH]; char plugin_output[MAX_PLUGINOUTPUT_LENGTH]; int total_packets = 0; int16_t return_code; u_int32_t calculated_crc32; char *inputptr, *ptr1, *ptr2, *ptr3, *ptr4; /* process command-line arguments */ result = process_arguments(argc, argv); if (result != OK || show_help == TRUE || show_license == TRUE || show_version == TRUE) { if (result != OK) printf("Incorrect command line arguments supplied\n"); printf("\n"); printf("NSCA Client %s\n", PROGRAM_VERSION); printf("Copyright (c) 2010-2012 Icinga Development Team and Community Contributors (http://www.icinga.org)\n"); printf("Copyright (c) 2000-2007 Ethan Galstad (www.nagios.org)\n"); printf("Last Modified: %s\n", MODIFICATION_DATE); printf("License: GPL v2\n"); printf("Encryption Routines: "); #ifdef HAVE_LIBMCRYPT printf("AVAILABLE"); #else printf("NOT AVAILABLE"); #endif printf("\n"); printf("\n"); } if (result != OK || show_help == TRUE) { printf("Usage: %s -H <host_address> [-p port] [-to to_sec] [-d delim] [-c config_file]\n", argv[0]); printf("\n"); printf("Options:\n"); printf(" <host_address> = The IP address of the host running the NSCA daemon\n"); printf(" [port] = The port on which the daemon is running - default is %s\n", DEFAULT_SERVER_PORT); printf(" [to_sec] = Number of seconds before connection attempt times out.\n"); printf(" (default timeout is %d seconds)\n", DEFAULT_SOCKET_TIMEOUT); printf(" [delim] = Delimiter to use when parsing input (defaults to a tab)\n"); printf(" [config_file] = Name of config file to use\n"); printf("\n"); printf("Note:\n"); printf("This utility is used to send passive check results to the NSCA daemon. Host and\n"); printf("Service check data that is to be sent to the NSCA daemon is read from standard\n"); printf("input. Input should be provided in the following format (tab-delimited unless\n"); printf("overriden with -d command line argument, one entry per line):\n"); printf("\n"); printf("Service Checks:\n"); printf("<host_name>[tab]<svc_description>[tab]<return_code>[tab]<plugin_output>[newline]\n\n"); printf("Host Checks:\n"); printf("<host_name>[tab]<return_code>[tab]<plugin_output>[newline]\n\n"); printf("When submitting multiple simultaneous results, separate each set with the ETB\n"); printf("character (^W or 0x17)\n"); printf("\n"); } if (show_license == TRUE) display_license(); if (result != OK || show_help == TRUE || show_license == TRUE || show_version == TRUE) do_exit(STATE_UNKNOWN); /* read the config file */ result = read_config_file(config_file); /* exit if there are errors... */ if (result == ERROR) { printf("Error: Config file '%s' contained errors...\n", config_file); do_exit(STATE_CRITICAL); } /* generate the CRC 32 table */ generate_crc32_table(); /* initialize alarm signal handling */ signal(SIGALRM, alarm_handler); /* set socket timeout */ alarm(socket_timeout); time(&start_time); /* try to connect to the host at the given port number */ result = my_tcp_connect(server_name, server_port, &sd); /* we couldn't connect */ if (result != STATE_OK) { printf("Error: Could not connect to host %s on port %s\n", server_name, server_port); do_exit(STATE_CRITICAL); } #ifdef DEBUG printf("Connected okay...\n"); #endif /* read the initialization packet containing the IV and timestamp */ result = read_init_packet(sd); if (result != OK) { printf("Error: Could not read init packet from server\n"); close(sd); do_exit(STATE_CRITICAL); } #ifdef DEBUG printf("Got init packet from server\n"); #endif /* initialize encryption/decryption routines with the IV we received from the server */ if (encrypt_init(password, encryption_method, received_iv, &CI) != OK) { printf("Error: Failed to initialize encryption libraries for method %d\n", encryption_method); close(sd); do_exit(STATE_CRITICAL); } #ifdef DEBUG printf("Initialized encryption routines\n"); #endif /**** WE'RE CONNECTED AND READY TO SEND ****/ /* read all data from STDIN until there isn't anymore */ while (!feof(stdin)) { int c = getc(stdin); if (c == -1) { break; } int pos = 0; while (c != 23) { if (c == -1) { // in case we don't terminate properly, or are in single-input mode. break; } input_buffer[pos] = c; c = getc(stdin); pos++; } input_buffer[pos] = 0; strip(input_buffer); if (!strcmp(input_buffer, "")) continue; /* get the host name */ ptr1 = strtok(input_buffer, delimiter); if (ptr1 == NULL) continue; /* get the service description or return code */ ptr2 = strtok(NULL, delimiter); if (ptr2 == NULL) continue; /* get the return code or plugin output */ ptr3 = strtok(NULL, delimiter); if (ptr3 == NULL) continue; /* get the plugin output - if NULL, this is a host check result */ ptr4 = strtok(NULL, "\x0"); strncpy(host_name, ptr1, sizeof(host_name) - 1); host_name[sizeof(host_name) - 1] = '\x0'; if (ptr4 == NULL) { strcpy(svc_description, ""); return_code = atoi(ptr2); ptr3 = escape_newlines(ptr3); strncpy(plugin_output, ptr3, sizeof(plugin_output) - 1); } else { strncpy(svc_description, ptr2, sizeof(svc_description) - 1); return_code = atoi(ptr3); ptr4 = escape_newlines(ptr4); strncpy(plugin_output, ptr4, sizeof(plugin_output) - 1); } svc_description[sizeof(svc_description) - 1] = '\x0'; plugin_output[sizeof(plugin_output) - 1] = '\x0'; /* increment count of packets we're sending */ total_packets++; /* clear the packet buffer */ bzero(&send_packet, sizeof(send_packet)); /* fill the packet with semi-random data */ randomize_buffer((char *)&send_packet, sizeof(send_packet)); /* copy the data we want to send into the packet */ send_packet.packet_version = (int16_t)htons(NSCA_PACKET_VERSION_3); send_packet.return_code = (int16_t)htons(return_code); strcpy(&send_packet.host_name[0], host_name); strcpy(&send_packet.svc_description[0], svc_description); strcpy(&send_packet.plugin_output[0], plugin_output); /* use timestamp provided by the server */ send_packet.timestamp = (u_int32_t)htonl(packet_timestamp); /* calculate the crc 32 value of the packet */ send_packet.crc32_value = (u_int32_t)0L; calculated_crc32 = calculate_crc32((char *)&send_packet, sizeof(send_packet)); send_packet.crc32_value = (u_int32_t)htonl(calculated_crc32); /* encrypt the packet */ encrypt_buffer((char *)&send_packet, sizeof(send_packet), password, encryption_method, CI); /* send the packet */ bytes_to_send = sizeof(send_packet); rc = sendall(sd, (char *)&send_packet, &bytes_to_send); /* there was an error sending the packet */ if (rc == -1) { printf("Error: Could not send data to host\n"); close(sd); do_exit(STATE_UNKNOWN); } /* for some reason we didn't send all the bytes we were supposed to */ else if (bytes_to_send < sizeof(send_packet)) { printf("Warning: Sent only %d of %d bytes to host\n", rc, sizeof(send_packet)); close(sd); return STATE_UNKNOWN; } } #ifdef DEBUG printf("Done sending data\n"); #endif /* close the connection */ close(sd); printf("%d data packet(s) sent to host successfully.\n", total_packets); /* exit cleanly */ do_exit(STATE_OK); /* no compiler complaints here... */ return STATE_OK; }
static void *gui_process(void *d) { gtk_widgets_t *data = d; char *source = _filename_utf8(gtk_file_chooser_get_filename((GtkFileChooser *)data->open_dialog)); char *output = _filename_utf8(gtk_file_chooser_get_filename((GtkFileChooser *)data->save_dialog)); if (!source || !output) *_status = STATUS_FAILED_IO; uint8_t *key = NULL; size_t length = 0; switch (_key_source) { case KEY_SOURCE_FILE: { char *k = _filename_utf8(gtk_file_chooser_get_filename((GtkFileChooser *)data->key_dialog)); length = 0; key = (uint8_t *)strdup(k); g_free(k); } break; case KEY_SOURCE_PASSWORD: { const char *k = gtk_entry_get_text((GtkEntry *)data->password_entry); length = strlen(k); key = (uint8_t *)strndup(k, length); } break; } int c = gtk_combo_box_get_active((GtkComboBox *)data->crypto_combo); int h = gtk_combo_box_get_active((GtkComboBox *)data->hash_combo); int m = gtk_combo_box_get_active((GtkComboBox *)data->mode_combo); const char **ciphers = list_of_ciphers(); const char **hashes = list_of_hashes(); const char **modes = list_of_modes(); crypto_t *x; if (_encrypted) x = decrypt_init(source, output, ciphers[c - 1], hashes[h - 1], modes[m - 1], key, length, _raw); else x = encrypt_init(source, output, ciphers[c - 1], hashes[h - 1], modes[m - 1], key, length, _raw, _compress, _follow, _version); _status = &x->status; g_free(source); g_free(output); free(key); if (x->status == STATUS_INIT) execute(x); gui_display(x, data); if (x->status == STATUS_SUCCESS) { set_progress_bar((GtkProgressBar *)data->progress_bar_total, PERCENT); set_progress_bar((GtkProgressBar *)data->progress_bar_current, PERCENT); } set_status_bar((GtkStatusbar *)data->status_bar, status(x)); set_progress_button((GtkButton *)data->progress_cancel_button, false); set_progress_button((GtkButton *)data->progress_close_button, true); deinit(&x); return NULL; }
//----------------------------------------------------------------------------------- unsigned int AES_encrypt(char *in_buffer, unsigned int in_buffer_size, char *out_buffer, unsigned int out_buffer_size, char *key, unsigned int key_size, bool cbc = false) { if (!En_inited) encrypt_init(key, key_size); return encrypt(in_buffer, in_buffer_size, out_buffer, out_buffer_size, cbc); }
/* handle a client connection */ static void handle_connection(int sock, void *data) { init_packet send_packet; int bytes_to_send; int rc; int flags; time_t packet_send_time; struct crypt_instance *CI; /* log info to syslog facility */ if (debug == TRUE) syslog(LOG_INFO, "Handling the connection..."); /* socket should be non-blocking */ fcntl(sock, F_GETFL, &flags); fcntl(sock, F_SETFL, flags | O_NONBLOCK); /* initialize encryption/decryption routines (server generates the IV to use and send to the client) */ if (encrypt_init(password, decryption_method, NULL, &CI) != OK) { close(sock); if (mode == MULTI_PROCESS_DAEMON) do_exit(STATE_CRITICAL); return; } /* create initial packet to send to client (contains random IV and timestamp) */ memcpy(&send_packet.iv[0], CI->transmitted_iv, TRANSMITTED_IV_SIZE); time(&packet_send_time); send_packet.timestamp = (u_int32_t)htonl(packet_send_time); /* send client the initial packet */ bytes_to_send = sizeof(send_packet); rc = sendall(sock, (char *)&send_packet, &bytes_to_send); /* there was an error sending the packet */ if (rc == -1) { syslog(LOG_ERR, "Could not send init packet to client\n"); encrypt_cleanup(decryption_method, CI); close(sock); if (mode == MULTI_PROCESS_DAEMON) do_exit(STATE_CRITICAL); return; } /* for some reason we didn't send all the bytes we were supposed to */ else if (bytes_to_send < sizeof(send_packet)) { syslog(LOG_ERR, "Only able to send %d of %d bytes of init packet to client\n", rc, sizeof(send_packet)); encrypt_cleanup(decryption_method, CI); close(sock); if (mode == MULTI_PROCESS_DAEMON) do_exit(STATE_CRITICAL); return; } /* open the command file if we're aggregating writes */ if (aggregate_writes == TRUE) { if (open_command_file() == ERROR) { close(sock); if (mode == MULTI_PROCESS_DAEMON) do_exit(STATE_CRITICAL); return; } } if (mode == SINGLE_PROCESS_DAEMON) register_read_handler(sock, handle_connection_read, (void *)CI); else { while (1) handle_connection_read(sock, (void *)CI); } return; }
int main(int argc, char *argv[]) { switch (argc) { case 2: encrypt_init(); if ( !stricmp("-u", argv[1]) || !stricmp("-i", argv[1]) || !stricmp("-st", argv[1]) || !stricmp("-wt", argv[1]) || !stricmp("-fs1", argv[1]) || !stricmp("-8bit", argv[1]) ) { print_instructions(); } else { scramble_file(argv[1]); } break; case 3: encrypt_init(); if ( !stricmp("-u", argv[1]) ) { unscramble_file(argv[2]); } else if ( !stricmp("-i", argv[1]) ) { scramble_identify(argv[2]); } else if ( !stricmp("-st", argv[1]) ) { Use_8bit = 0; fs2 = false; scramble_file(argv[2], argv[2], PREPROCESS_SHIPS_TBL); } else if ( !stricmp("-wt", argv[1]) ) { Use_8bit = 0; fs2 =false; scramble_file(argv[2], argv[2], PREPROCESS_WEAPONS_TBL); } else if ( !stricmp("-fs1", argv[1]) ) { Use_8bit = 0; fs2 = false; scramble_file(argv[2], argv[2]); } else if ( !stricmp("-8bit", argv[1]) ) { Use_8bit = 1; fs2 = false; scramble_file(argv[2], argv[2]); } else { scramble_file(argv[1], argv[2]); } break; case 4: encrypt_init(); if ( !stricmp("-u", argv[1]) ) { unscramble_file(argv[2], argv[3]); } else if ( !stricmp("-st", argv[1]) ) { Use_8bit = 0; fs2 = false; scramble_file(argv[2], argv[3], PREPROCESS_SHIPS_TBL); } else if ( !stricmp("-wt", argv[1]) ) { Use_8bit = 0; fs2 = false; scramble_file(argv[2], argv[3], PREPROCESS_WEAPONS_TBL); } else if ( !stricmp("-fs1", argv[1]) ) { Use_8bit = 0; fs2 = false; scramble_file(argv[2], argv[3]); } else if ( !stricmp("-8bit", argv[1]) ) { Use_8bit = 1; fs2 = false; scramble_file(argv[2], argv[3]); } else { print_instructions(); } break; default: print_instructions(); return 1; } }