// Tests if an options with MyFamily fingerprints missing '$' normalises // them correctly and also ensure it also works with multiple fingerprints static void test_config_fix_my_family(void *arg) { char *err = NULL; const char *family = "$1111111111111111111111111111111111111111, " "1111111111111111111111111111111111111112, " "$1111111111111111111111111111111111111113"; or_options_t* options = options_new(); or_options_t* defaults = options_new(); (void) arg; options_init(options); options_init(defaults); options->MyFamily = tor_strdup(family); options_validate(NULL, options, defaults, 0, &err) ; if (err != NULL) { TT_FAIL(("options_validate failed: %s", err)); } test_streq(options->MyFamily, "$1111111111111111111111111111111111111111, " "$1111111111111111111111111111111111111112, " "$1111111111111111111111111111111111111113"); done: if (err != NULL) { tor_free(err); } or_options_free(options); or_options_free(defaults); }
static int idaapi init(void) { if (!strcmp(inf.procName, "metapc")) { if (inf.is_64bit()) patchdiff_cpu = CPU_X8664; else patchdiff_cpu = CPU_X8632; } else if (!strcmp(inf.procName, "PPC")) { patchdiff_cpu = CPU_PPC; } else patchdiff_cpu = CPU_DEFAULT; d_engine = NULL; // handle IPC ipc_init(NULL, 0, 0); d_opt = options_init(); if (!d_opt) return PLUGIN_SKIP; return PLUGIN_OK; }
int main(int argc, char* argv[]) { gint error = 0; options_init(argc - 1, argv + 1); INFO("Starting up."); descriptor_init(); DEBUG("Disabling SIGPIPE."); signal(SIGPIPE, SIG_IGN); DEBUG("Creating ZeroMQ context."); gpointer zmq_context = zmq_init(options_zmq_io_threads()); if (zmq_context == NULL) { PERROR("main(zmq_init)"); error = 1; goto err0; } DEBUG("Creating server on port %s.", options_port()); gint socket = socket_server(options_port()); if (socket == -1) { error = 1; goto err1; } INFO("Initialising Lua API."); lua_api_init(zmq_context, argc - 1, argv + 1); lua_State* lua = lua_api_get(); DEBUG("Running " LUA_START_FILE); if (luaL_dofile(lua, LUA_START_FILE) == 1) { ERROR("%s", lua_tostring(lua, -1)); error = 1; goto err2; } io_mainloop(socket); err2: DEBUG("Closing server socket."); socket_close(socket); err1: DEBUG("Terminating ZeroMQ context."); /* This is separate from lua_api_deinit() to prevent zmq_term() from blocking forever. */ lua_zmq_deinit(); zmq_term(zmq_context); err0: DEBUG("Enabling SIGPIPE."); signal(SIGPIPE, SIG_DFL); options_deinit(); descriptor_deinit(); if (lua_api_get() != NULL) { DEBUG("Closing Lua state."); lua_api_deinit(); } return error; }
/* * Initializes the ioengine configured for a job, if it has not been done so * already. */ int ioengine_load(struct thread_data *td) { const char *engine; /* * Engine has already been loaded. */ if (td->io_ops) return 0; if (!td->o.ioengine) { log_err("fio: internal fault, no IO engine specified\n"); return 1; } engine = get_engine_name(td->o.ioengine); td->io_ops = load_ioengine(td, engine); if (!td->io_ops) { log_err("fio: failed to load engine %s\n", engine); return 1; } if (td->io_ops->option_struct_size && td->io_ops->options) { /* * In cases where td->eo is set, clone it for a child thread. * This requires that the parent thread has the same ioengine, * but that requirement must be enforced by the code which * cloned the thread. */ void *origeo = td->eo; /* * Otherwise use the default thread options. */ if (!origeo && td != &def_thread && def_thread.eo && def_thread.io_ops->options == td->io_ops->options) origeo = def_thread.eo; options_init(td->io_ops->options); td->eo = malloc(td->io_ops->option_struct_size); /* * Use the default thread as an option template if this uses the * same options structure and there are non-default options * used. */ if (origeo) { memcpy(td->eo, origeo, td->io_ops->option_struct_size); options_mem_dupe(td->eo, td->io_ops->options); } else { memset(td->eo, 0, td->io_ops->option_struct_size); fill_default_options(td->eo, td->io_ops->options); } *(struct thread_data **)td->eo = td; } return 0; }
int GGPROTO::OnEvent(PROTOEVENTTYPE eventType, WPARAM wParam, LPARAM lParam) { switch( eventType ) { case EV_PROTO_ONLOAD: { HookProtoEvent(ME_OPT_INITIALISE, &GGPROTO::options_init); HookProtoEvent(ME_USERINFO_INITIALISE, &GGPROTO::details_init); // Init misc stuff gg_icolib_init(); initpopups(); gc_init(); keepalive_init(); img_init(); block_init(); // Try to fetch user avatar getOwnAvatar(); break; } case EV_PROTO_ONEXIT: // Stop avatar request thread pth_avatar.dwThreadId = 0; // Stop main connection session thread pth_sess.dwThreadId = 0; img_shutdown(); sessions_closedlg(); break; case EV_PROTO_ONOPTIONS: return options_init(wParam, lParam); case EV_PROTO_ONMENU: menus_init(); break; case EV_PROTO_ONRENAME: if (hMenuRoot) { CLISTMENUITEM mi = { sizeof(mi) }; mi.flags = CMIM_NAME | CMIF_TCHAR | CMIF_KEEPUNTRANSLATED; mi.ptszName = m_tszUserName; Menu_ModifyItem(hMenuRoot, &mi); } break; case EV_PROTO_ONCONTACTDELETED: return contactdeleted(wParam, lParam); case EV_PROTO_DBSETTINGSCHANGED: return dbsettingchanged(wParam, lParam); } return TRUE; }
static void init(void) { options_init(); comm_init(); tl_init(); ts_init(); ti_init(); sb_init(); tl_show(); // others... }
/** Main entry point for benchmark code: parse the command line, and run * some benchmarks. */ int main(int argc, const char **argv) { int i; int list=0, n_enabled=0; benchmark_t *b; char *errmsg; or_options_t *options; tor_threads_init(); for (i = 1; i < argc; ++i) { if (!strcmp(argv[i], "--list")) { list = 1; } else { benchmark_t *b = find_benchmark(argv[i]); ++n_enabled; if (b) { b->enabled = 1; } else { printf("No such benchmark as %s\n", argv[i]); } } } reset_perftime(); if (crypto_seed_rng() < 0) { printf("Couldn't seed RNG; exiting.\n"); return 1; } crypto_init_siphash_key(); options = options_new(); init_logging(1); options->command = CMD_RUN_UNITTESTS; options->DataDirectory = tor_strdup(""); options_init(options); if (set_options(options, &errmsg) < 0) { printf("Failed to set initial options: %s\n", errmsg); tor_free(errmsg); return 1; } for (b = benchmarks; b->name; ++b) { if (b->enabled || n_enabled == 0) { printf("===== %s =====\n", b->name); if (!list) b->fn(); } } return 0; }
int main(int argc, char *argv[]) { options_init(); options_deal(argc, argv); video_init(); screen_init(); screen_mainloop(); screen_quit(); video_quit(); exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { struct addrset *set; char line[1024]; int i; #ifdef WIN32 win_init(); #endif set = addrset_new(); options_init(); for (i = 1; i < argc; i++) { if (!addrset_add_spec(set, argv[i], o.af, !o.nodns)) { fprintf(stderr, "Error adding spec \"%s\".\n", argv[i]); exit(1); } } while (fgets(line, sizeof(line), stdin) != NULL) { char *s, *hostname; struct addrinfo *addrs; s = line; while ((hostname = strtok(s, " \t\n")) != NULL) { int rc; s = NULL; rc = resolve_name(hostname, &addrs); if (rc != 0) { fprintf(stderr, "Error resolving \"%s\": %s.\n", hostname, gai_strerror(rc)); continue; } if (addrs == NULL) { fprintf(stderr, "No addresses found for \"%s\".\n", hostname); continue; } /* Check just the first address returned. */ if (addrset_contains(set, addrs->ai_addr)) printf("%s\n", hostname); freeaddrinfo(addrs); } } addrset_free(set); return 0; }
int main(int argc, char *argv[]) { options_init(argc, argv); char *address = opt_address; int port = strtoint(opt_port); char recvBuff[40000]; struct sockaddr_in serv_addr; struct timespec sleep_start = { .tv_sec = 3, .tv_nsec = 0 },
int main(int argc, char **argv) { int ret; options_init(); if (process_options(argc, argv, 1)) { log_err(errno, "Exiting..."); exit(1); } ret = chilliauth(); options_cleanup(); return ret; }
int main(int argc, char **argv) { int ret; options_init(); if (process_options(argc, argv, 1)) { syslog(LOG_ERR, "%s: Exiting...", strerror(errno)); exit(1); } ret = chilliauth(); options_cleanup(); return ret; }
void semantics_instr(char* input, unsigned int in_size, char** output, unsigned int* out_size) { clear_exception(); #ifdef GDSL_X86 struct options options; options_init(&options); #endif state_t state = gdsl_init(); gdsl_set_code(state, input, in_size, 0); if(setjmp(*gdsl_err_tgt(state))) { snprintf(error_message, sizeof(error_message), "decode failed: %s", gdsl_get_error_message(state)); set_exception(); goto cleanup; } #ifdef GDSL_X86 int_t config = 0; config |= gdsl_config_mode64(state)*options.mode64; config |= gdsl_config_default_opnd_sz_32(state)*options.default_opnd_sz_32; obj_t insn = gdsl_decode(state, config); #else obj_t insn = gdsl_decode(state, gdsl_config_default(state)); #endif gdsl_get_ip(state); string_t fmt = gdsl_merge_rope(state, gdsl_pretty(state, insn)); /* string_t is a typedef for char* */ if(setjmp(*gdsl_err_tgt(state))) { snprintf(error_message, sizeof(error_message), "translate failed: %s", gdsl_get_error_message(state)); set_exception(); goto cleanup; } obj_t rreil = gdsl_translate(state, insn); fmt = gdsl_merge_rope(state, gdsl_rreil_pretty(state, rreil)); size_t outputSize = strlen(fmt)+1; *output = malloc(outputSize); strncpy(*output, fmt, outputSize); *out_size = outputSize - 1; // Python expects size without null... cleanup: gdsl_reset_heap(state); gdsl_destroy(state); }
int cli_parse(const int argc, const char **argv) { options_init(); flag_t *flag; const char *arg; int i; for (arg = argv[i=1]; i < argc; arg = argv[++i]) { flag = NULL; if (flag_is_long_flag(arg)) { flag = flag_find_by_name(arg+2); } else if (flag_is_short_flag(arg)) { flag = flag_find(arg[1]); } else if (flag_is_filename(arg)) { flag = flag_find_by_name("in"); options_set_with_arg(flag, arg); continue; } if (!flag) { log_error("unknown flag \'%s\'", arg); return 0; } else if (flag->args > 0) { if (i+1 < argc) options_set_with_arg(flag, argv[++i]); else { log_error("flag \'%s\' needs an argument", arg); return 0; } } else { options_set(flag); } } if (options->out_filename == NULL && options->in_filename == NULL) { log_error("at least one filename (input or output) has to be declared"); return 0; } log_info("parsed arguments from command line"); return 1; }
__declspec(dllexport) int Load(PLUGINLINK * link) { PROTOCOLDESCRIPTOR pd; INITCOMMONCONTROLSEX iccs; /* setup static data */ pluginLink = link; /* get main thread handle */ DuplicateHandle( GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(), &g_hMainThread, THREAD_SET_CONTEXT, FALSE, 0); /* init common controls library (for the IP adress entries to work) */ memset(&iccs, 0, sizeof(iccs)); iccs.dwSize = sizeof(iccs); iccs.dwICC = ICC_INTERNET_CLASSES; InitCommonControlsEx(&iccs); /* init vqp_link module */ vqp_init(NULL, NULL); /* register this module with miranda */ memset(&pd, 0, sizeof(pd)); pd.cbSize = sizeof(pd); pd.szName = VQCHAT_PROTO; pd.type = PROTOTYPE_PROTOCOL; CallService(MS_PROTO_REGISTERMODULE, 0, (LPARAM)&pd); /* init our modules */ options_init(); msghandler_init(); skin_init(); user_init(); userlist_init(); chatroom_init(); /* register protocol services */ service_register_services(); service_hook_all(); return 0; }
int GGPROTO::OnEvent(PROTOEVENTTYPE eventType, WPARAM wParam, LPARAM lParam) { switch( eventType ) { case EV_PROTO_ONLOAD: HookProtoEvent(ME_OPT_INITIALISE, &GGPROTO::options_init); HookProtoEvent(ME_USERINFO_INITIALISE, &GGPROTO::details_init); // Init misc stuff gg_icolib_init(); initpopups(); gc_init(); keepalive_init(); img_init(); block_init(); // Try to fetch user avatar getOwnAvatar(); break; case EV_PROTO_ONEXIT: // Stop avatar request thread pth_avatar.dwThreadId = 0; // Stop main connection session thread pth_sess.dwThreadId = 0; img_shutdown(); sessions_closedlg(); break; case EV_PROTO_ONOPTIONS: return options_init(wParam, lParam); case EV_PROTO_ONMENU: menus_init(); break; case EV_PROTO_ONCONTACTDELETED: return contactdeleted(wParam, lParam); case EV_PROTO_DBSETTINGSCHANGED: return dbsettingchanged(wParam, lParam); } return TRUE; }
DR_EXPORT void dr_init(client_id_t id) { uint i = 0; uint const_arrays_num; drsys_options_t ops = { sizeof(ops), 0, }; dr_set_client_name("Dr. STrace", "http://drmemory.org/issues"); #ifdef WINDOWS dr_enable_console_printing(); #endif options_init(id); drsym_init(0); drmgr_init(); drx_init(); if (drsys_init(id, &ops) != DRMF_SUCCESS) ASSERT(false, "drsys failed to init"); dr_register_exit_event(exit_event); dr_register_filter_syscall_event(event_filter_syscall); drmgr_register_pre_syscall_event(event_pre_syscall); drmgr_register_post_syscall_event(event_post_syscall); if (drsys_filter_all_syscalls() != DRMF_SUCCESS) ASSERT(false, "drsys_filter_all_syscalls should never fail"); open_log_file(); const_arrays_num = get_const_arrays_num(); hashtable_init(&nconsts_table, HASHTABLE_BITSIZE, HASH_STRING, false); while (i < const_arrays_num) { const_values_t *named_consts = const_struct_array[i]; bool res = hashtable_add(&nconsts_table, (void *) named_consts[0].const_name, (void *) named_consts); if (!res) ASSERT(false, "drstrace failed to add to hashtable"); i++; } }
int main(void) { t_options opt_val; t_options *opt = &opt_val; options_init( opt, SR, BS ); t_num *x = sig_init(opt), *y = sig_init(opt); t_phasor phs; dsp_alg_phasor_init( &phs, opt, 0 ); dsp_alg_bias(BS, 441, x, x); dsp_alg_phasor( &phs, x, y ); sig_print( opt, y ); sig_free(x); sig_free(y); return 0; }
int main( int argc, char **argv ) { int a, rc; int ec = 0; options_t options; modbus_t *ctx; uint8_t *tab_bit; uint16_t *tab_reg; if(argc > 1) { options_init(&options); for (a = 1; a < argc; a++) { options_execute(&options, argv[a], strlen(argv[a])+1); rc = options_finish(&options); DEBUG("argument: '%s' end state: %d\n",argv[a], rc); if (rc == -1) { options_err_disp(&options,argv[a]); ec = 1; goto exit; } } if(rc == 0) { fprintf(stderr, "Missing action argument\n"); ec = 1; goto exit; } if(options.action == _UNDEF) goto exit; // Options are valid options_dump(&options); ctx = modbus_init_con(&options); modbus_set_debug(ctx, options.debug); modbus_set_response_timeout(ctx, &options.timeout); if (modbus_connect(ctx) == -1) { fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno)); ec = 1; goto destroy; } switch(options.action) { case _RC: case _RD: tab_bit = (uint8_t *) malloc(options.count * sizeof(uint8_t)); DBG_ASSERT(tab_bit != NULL, "Unable to allocate tab_bit!"); memset(tab_bit, 0, options.count * sizeof(uint8_t)); switch(options.action) { case _RC: rc = modbus_read_bits(ctx, options.address, options.count, tab_bit); break; case _RD: rc = modbus_read_input_bits(ctx, options.address, options.count, tab_bit); break; } if (rc == -1) { fprintf(stderr, "%s\n", modbus_strerror(errno)); ec = 1; } else { display_8bit(&options, tab_bit); } free(tab_bit); break; case _RH: case _RI: tab_reg = (uint16_t *) malloc(options.count * sizeof(uint16_t)); DBG_ASSERT(tab_reg != NULL, "Unable to allocate tab_reg!"); switch(options.action) { case _RH: rc = modbus_read_registers(ctx, options.address, options.count, tab_reg); break; case _RI: rc = modbus_read_input_registers(ctx, options.address, options.count, tab_reg); break; } if (rc == -1) { fprintf(stderr, "%s\n", modbus_strerror(errno)); ec = 1; } else { display_16bit(&options, tab_reg); } free(tab_reg); break; case _WC: if(options.count == 1) rc = modbus_write_bit(ctx, options.address, options.coil_values[0]); else { rc = modbus_write_bits(ctx, options.address, options.count, options.coil_values); } if (rc == -1) { fprintf(stderr, "%s\n", modbus_strerror(errno)); ec = 1; } else { printf("Success\n"); } break; case _WH: if(options.count == 1) rc = modbus_write_register(ctx, options.address, options.reg_values[0]); else { rc = modbus_write_registers(ctx, options.address, options.count, options.reg_values); } if (rc == -1) { fprintf(stderr, "%s\n", modbus_strerror(errno)); ec = 1; } else { printf("Success\n"); } break; default: DBG_ASSERT(0,"Unhandled action enum constant!"); } } else { options_help(); ec = 1; goto exit; } close: modbus_close(ctx); destroy: modbus_free(ctx); exit: return ec; }
int main(int argc, char **argv) { struct passwd *pw; char *s, *path, *label, *home, **var; int opt, flags, quiet, keys; #if defined(DEBUG) && defined(__OpenBSD__) malloc_options = (char *) "AFGJPX"; #endif flags = IDENTIFY_256COLOURS | IDENTIFY_UTF8; quiet = 0; label = path = NULL; login_shell = (**argv == '-'); while ((opt = getopt(argc, argv, "28c:Cdf:lL:qS:uUvV")) != -1) { switch (opt) { case '2': flags |= IDENTIFY_256COLOURS; flags &= ~IDENTIFY_88COLOURS; break; case '8': flags |= IDENTIFY_88COLOURS; flags &= ~IDENTIFY_256COLOURS; break; case 'c': free(shell_cmd); shell_cmd = xstrdup(optarg); break; case 'C': if (flags & IDENTIFY_CONTROL) flags |= IDENTIFY_TERMIOS; else flags |= IDENTIFY_CONTROL; break; case 'V': printf("%s %s\n", __progname, VERSION); exit(0); case 'f': free(cfg_file); cfg_file = xstrdup(optarg); break; case 'l': login_shell = 1; break; case 'L': free(label); label = xstrdup(optarg); break; case 'q': quiet = 1; break; case 'S': free(path); path = xstrdup(optarg); break; case 'u': flags |= IDENTIFY_UTF8; break; case 'v': debug_level++; break; default: usage(); } } argc -= optind; argv += optind; if (shell_cmd != NULL && argc != 0) usage(); if (!(flags & IDENTIFY_UTF8)) { /* * If the user has set whichever of LC_ALL, LC_CTYPE or LANG * exist (in that order) to contain UTF-8, it is a safe * assumption that either they are using a UTF-8 terminal, or * if not they know that output from UTF-8-capable programs may * be wrong. */ if ((s = getenv("LC_ALL")) == NULL || *s == '\0') { if ((s = getenv("LC_CTYPE")) == NULL || *s == '\0') s = getenv("LANG"); } if (s != NULL && (strcasestr(s, "UTF-8") != NULL || strcasestr(s, "UTF8") != NULL)) flags |= IDENTIFY_UTF8; } environ_init(&global_environ); for (var = environ; *var != NULL; var++) environ_put(&global_environ, *var); options_init(&global_options, NULL); options_table_populate_tree(server_options_table, &global_options); options_set_number(&global_options, "quiet", quiet); options_init(&global_s_options, NULL); options_table_populate_tree(session_options_table, &global_s_options); options_set_string(&global_s_options, "default-shell", "%s", getshell()); options_init(&global_w_options, NULL); options_table_populate_tree(window_options_table, &global_w_options); /* Enable UTF-8 if the first client is on UTF-8 terminal. */ if (flags & IDENTIFY_UTF8) { options_set_number(&global_s_options, "status-utf8", 1); options_set_number(&global_s_options, "mouse-utf8", 1); options_set_number(&global_w_options, "utf8", 1); } /* Override keys to vi if VISUAL or EDITOR are set. */ if ((s = getenv("VISUAL")) != NULL || (s = getenv("EDITOR")) != NULL) { if (strrchr(s, '/') != NULL) s = strrchr(s, '/') + 1; if (strstr(s, "vi") != NULL) keys = MODEKEY_VI; else keys = MODEKEY_EMACS; options_set_number(&global_s_options, "status-keys", keys); options_set_number(&global_w_options, "mode-keys", keys); } /* Locate the configuration file. */ if (cfg_file == NULL) { home = getenv("HOME"); if (home == NULL || *home == '\0') { pw = getpwuid(getuid()); if (pw != NULL) home = pw->pw_dir; } xasprintf(&cfg_file, "%s/%s", home, DEFAULT_CFG); if (access(cfg_file, R_OK) != 0 && errno == ENOENT) { free(cfg_file); cfg_file = NULL; } } /* * Figure out the socket path. If specified on the command-line with -S * or -L, use it, otherwise try $TMUX or assume -L default. */ parseenvironment(); if (path == NULL) { /* If no -L, use the environment. */ if (label == NULL) { if (environ_path != NULL) path = xstrdup(environ_path); } /* -L or default set. */ if (!path) { if ((path = makesocketpath(label)) == NULL) { fprintf(stderr, "can't create socket\n"); exit(1); } } } free(label); strlcpy(socket_path, path, sizeof socket_path); free(path); #ifdef HAVE_SETPROCTITLE /* Set process title. */ setproctitle("%s (%s)", __progname, socket_path); #endif /* Pass control to the client. */ ev_base = osdep_event_init(); exit(client_main(argc, argv, flags)); }
int main(int argc, char **argv) { int status; int idx; int active_last = 0; int active = 0; struct redir_t *redir; int keep_going = 1; int reload_config = 0; uint8_t hwaddr[6]; struct ifreq ifr; int selfpipe; int fd = socket(AF_INET, SOCK_DGRAM, 0); options_init(); chilli_signals(&keep_going, &reload_config); process_options(argc, argv, 1); safe_strncpy(ifr.ifr_name, _options.dhcpif, sizeof(ifr.ifr_name)); #ifdef SIOCGIFHWADDR if (ioctl(fd, SIOCGIFHWADDR, (caddr_t)&ifr) == 0) { memcpy(hwaddr, ifr.ifr_hwaddr.sa_data, PKT_ETH_ALEN); } else { log_err(errno, "could not get MAC address"); return -1; } #endif close(fd); /* create an instance of redir */ if (redir_new(&redir, &_options.uamlisten, _options.uamport, #ifdef ENABLE_UAMUIPORT _options.uamuiport #else 0 #endif )) { log_err(0, "Failed to create redir"); return -1; } if (redir_listen(redir)) { log_err(0, "Failed to create redir listen"); return -1; } redir_set(redir, hwaddr, (_options.debug)); redir_set_cb_getstate(redir, sock_redir_getstate); redir->cb_handle_url = redir_handle_url; if (net_select_init(&sctx)) log_err(errno, "select init"); selfpipe = selfpipe_init(); /* epoll */ net_select_addfd(&sctx, selfpipe, SELECT_READ); net_select_addfd(&sctx, redir->fd[0], SELECT_READ); net_select_addfd(&sctx, redir->fd[1], SELECT_READ); if (_options.gid && setgid(_options.gid)) { log_err(errno, "setgid(%d) failed while running with gid = %d\n", _options.gid, getgid()); } if (_options.uid && setuid(_options.uid)) { log_err(errno, "setuid(%d) failed while running with uid = %d\n", _options.uid, getuid()); } while (keep_going) { /* select/poll */ net_select_zero(&sctx); net_select_fd(&sctx, selfpipe, SELECT_READ); net_select_fd(&sctx, redir->fd[0], SELECT_READ); net_select_fd(&sctx, redir->fd[1], SELECT_READ); active = 0; if (reload_config) { reload_options(argc, argv); reload_config = 0; redir_set(redir, hwaddr, _options.debug); } for (idx=0; idx < max_requests; idx++) { conn_select_fd(&requests[idx].conn, &sctx); if (requests[idx].inuse && requests[idx].socket_fd) { time_t now = mainclock_tick(); int fd = requests[idx].socket_fd; int timeout = 60; if (now - requests[idx].last_active > timeout) { log_dbg("timeout connection %d", idx); redir_conn_finish(&requests[idx].conn, &requests[idx]); } else { int evt = SELECT_READ; timeout = 0; if (conn_write_remaining(&requests[idx].conn)) evt |= SELECT_WRITE; net_select_fd(&sctx, fd, evt); active++; } #if(_debug_ > 1) if (_options.debug) { struct sockaddr_in address; socklen_t addrlen = sizeof(address); if (getpeername(fd, (struct sockaddr *)&address, &addrlen) >= 0) { char line[512]; safe_snprintf(line, sizeof(line), "#%d (%d) %d connection from %s %d", timeout ? -1 : active, fd, (int) requests[idx].last_active, inet_ntoa(address.sin_addr), ntohs(address.sin_port)); if (requests[idx].conn.sock) { addrlen = sizeof(address); if (getpeername(requests[idx].conn.sock, (struct sockaddr *)&address, &addrlen) >= 0) { safe_snprintf(line+strlen(line), sizeof(line)-strlen(line), " to %s %d", inet_ntoa(address.sin_addr), ntohs(address.sin_port)); } } if (timeout) { safe_snprintf(line+strlen(line), sizeof(line)-strlen(line), " (timeout)"); } log_dbg("%s", line); } } #endif } } if (active != active_last) { log_dbg("active connections: %d", active); active_last = active; } status = net_select(&sctx); #if defined(USING_POLL) && defined(HAVE_SYS_EPOLL_H) && (_debug_ > 1) if (_options.debug && status > 0) { int i; log_dbg("epoll %d", status); for (i=0; i < status; i++) { log_dbg("epoll fd %d %d", sctx.events[i].data.fd, sctx.events[i].events); } } #endif switch (status) { case -1: log_err(errno, "select() returned -1!"); break; default: if (status > 0) { if (net_select_read_fd(&sctx, selfpipe)==1) { chilli_handle_signal(0, 0); } if (redir->fd[0]) if (net_select_read_fd(&sctx, redir->fd[0])==1 && redir_accept2(redir, 0) < 0) log_err(0, "redir_accept() failed!"); if (redir->fd[1]) if (net_select_read_fd(&sctx, redir->fd[1])==1 && redir_accept2(redir, 1) < 0) log_err(0, "redir_accept() failed!"); for (idx=0; idx < max_requests; idx++) { /* * Update remote connections with activity */ conn_select_update(&requests[idx].conn, &sctx); /* * Check client connections with activity */ if (requests[idx].inuse && requests[idx].socket_fd) { int fd = requests[idx].socket_fd; #ifdef HAVE_SSL if (requests[idx].sslcon) { if (openssl_check_accept(requests[idx].sslcon, 0) < 0) { log_dbg("ssl error %d", errno); redir_conn_finish(&requests[idx].conn, &requests[idx]); continue; } } #endif switch (net_select_write_fd(&sctx, fd)) { case 1: log_dbg("client writeable"); redir_cli_rewrite(&requests[idx], &requests[idx].conn); break; } switch (net_select_read_fd(&sctx, fd)) { case -1: log_dbg("EXCEPTION"); redir_conn_finish(&requests[idx].conn, &requests[idx]); break; case 1: { if (requests[idx].proxy) { char b[PKT_MAX_LEN]; int r; #ifdef HAVE_SSL if (requests[idx].sslcon) { /* log_dbg("proxy_read_ssl"); */ r = openssl_read(requests[idx].sslcon, b, sizeof(b)-1, 0); } else #endif r = safe_read(fd, b, sizeof(b)-1); /* log_dbg("proxy_read: %d %d", fd, r); */ if (r <= 0) { log_dbg("recv %d %d %d", r, requests[idx].conn.read_buf->slen - requests[idx].conn.read_pos, errno); if (!(r == -1 && (errno == EWOULDBLOCK || errno == EAGAIN))) { if (redir_cli_rewrite(&requests[idx], &requests[idx].conn) == 0) { log_dbg("done reading and writing"); redir_conn_finish(&requests[idx].conn, &requests[idx]); } } } else if (r > 0) { int w; requests[idx].last_active = mainclock_tick(); w = net_write(requests[idx].conn.sock, b, r); /* log_dbg("proxy_write: %d", w); */ if (r != w) { log_err(errno, "problem writing what we read from client"); redir_conn_finish(&requests[idx].conn, &requests[idx]); } } } else { #ifdef HAVE_SSL go_again: #endif switch (redir_main(redir, fd, fd, &requests[idx].conn.peer, &requests[idx].baddr, requests[idx].uiidx, &requests[idx])) { case 1: /*log_dbg("redir cont'ed");*/ #ifdef HAVE_SSL if (requests[idx].sslcon && openssl_pending(requests[idx].sslcon) > 0) { log_dbg("ssl_pending, trying again"); goto go_again; } #endif break; case -1: log_dbg("redir error"); default: log_dbg("redir completed"); redir_conn_finish(&requests[idx].conn, &requests[idx]); break; } } } break; } } } } break; } } redir_free(redir); child_killall(SIGKILL); selfpipe_finish(); return 0; }
void startClient() { char *logfile = "/sdcard/client.out"; FILE *fp; if((fp=freopen("/sdcard/freeciv_out_client.log", "w" ,stdout))==NULL) { printf("Cannot open file.\n"); exit(1); } if((fp=freopen("/sdcard/freeciv_err_client.log", "w" ,stderr))==NULL) { printf("Cannot open file.\n"); exit(1); } setenv ("HOME", "/sdcard/FreeCiv", 0); setenv ("USER", "Sparky", 0); LOGI("Hello JNI"); log_init(logfile, LOG_DEBUG , NULL); int i, loglevel; int ui_options = 0; bool ui_separator = FALSE; char *option=NULL; bool user_tileset = FALSE; i_am_client(); /* Tell to libfreeciv that we are client */ /* Ensure that all AIs are initialized to unused state */ ai_type_iterate(ai) { init_ai(ai); } ai_type_iterate_end; init_our_capability(); (void)user_username(default_user_name, MAX_LEN_NAME); if (!is_valid_username(default_user_name)) { char buf[sizeof(default_user_name)]; my_snprintf(buf, sizeof(buf), "_%s", default_user_name); if (is_valid_username(buf)) { sz_strlcpy(default_user_name, buf); } else { my_snprintf(default_user_name, sizeof(default_user_name), "player%d", myrand(10000)); } } game.all_connections = conn_list_new(); game.est_connections = conn_list_new(); ui_init(); fc_init_network(); init_our_capability(); chatline_common_init(); init_player_dlg_common(); init_themes(); options_init(); strcpy(default_sound_plugin_name,"none"); server_port = 9999; sz_strlcpy(server_host, "localhost"); options_load(); mysrand(time(NULL)); helpdata_init(); boot_help_texts(NULL); LOGI("Reading tilespec"); tilespec_try_read("amplio2", TRUE); LOGI("Done reading tilespec"); overview_size_changed(); audio_real_init("stdsounds", "none"); //audio_play_music("music_start", NULL); auto_connect = TRUE; init_mapcanvas_and_overview(); /* run gui-specific client */ ui_main(0, NULL); /* termination */ client_exit(); return; }
int main(int argc, char **argv) { char *s, *path, *label, **var, tmp[PATH_MAX]; int opt, flags, keys; #if defined(DEBUG) && defined(__OpenBSD__) malloc_options = (char *) "AFGJPX"; #endif setlocale(LC_TIME, ""); if (**argv == '-') flags = CLIENT_LOGIN; else flags = 0; label = path = NULL; while ((opt = getopt(argc, argv, "2c:Cdf:lL:qS:uUVv")) != -1) { switch (opt) { case '2': flags |= CLIENT_256COLOURS; break; case 'c': free(shell_cmd); shell_cmd = xstrdup(optarg); break; case 'C': if (flags & CLIENT_CONTROL) flags |= CLIENT_CONTROLCONTROL; else flags |= CLIENT_CONTROL; break; case 'V': printf("%s %s\n", __progname, VERSION); exit(0); case 'f': set_cfg_file(optarg); break; case 'l': flags |= CLIENT_LOGIN; break; case 'L': free(label); label = xstrdup(optarg); break; case 'q': break; case 'S': free(path); path = xstrdup(optarg); break; case 'u': flags |= CLIENT_UTF8; break; case 'v': debug_level++; break; default: usage(); } } argc -= optind; argv += optind; if (shell_cmd != NULL && argc != 0) usage(); if (!(flags & CLIENT_UTF8)) { /* * If the user has set whichever of LC_ALL, LC_CTYPE or LANG * exist (in that order) to contain UTF-8, it is a safe * assumption that either they are using a UTF-8 terminal, or * if not they know that output from UTF-8-capable programs may * be wrong. */ if ((s = getenv("LC_ALL")) == NULL || *s == '\0') { if ((s = getenv("LC_CTYPE")) == NULL || *s == '\0') s = getenv("LANG"); } if (s != NULL && (strcasestr(s, "UTF-8") != NULL || strcasestr(s, "UTF8") != NULL)) flags |= CLIENT_UTF8; } environ_init(&global_environ); for (var = environ; *var != NULL; var++) environ_put(&global_environ, *var); if (getcwd(tmp, sizeof tmp) != NULL) environ_set(&global_environ, "PWD", tmp); options_init(&global_options, NULL); options_table_populate_tree(server_options_table, &global_options); options_init(&global_s_options, NULL); options_table_populate_tree(session_options_table, &global_s_options); options_set_string(&global_s_options, "default-shell", "%s", getshell()); options_init(&global_w_options, NULL); options_table_populate_tree(window_options_table, &global_w_options); /* Enable UTF-8 if the first client is on UTF-8 terminal. */ if (flags & CLIENT_UTF8) { options_set_number(&global_s_options, "status-utf8", 1); options_set_number(&global_s_options, "mouse-utf8", 1); options_set_number(&global_w_options, "utf8", 1); } /* Override keys to vi if VISUAL or EDITOR are set. */ if ((s = getenv("VISUAL")) != NULL || (s = getenv("EDITOR")) != NULL) { if (strrchr(s, '/') != NULL) s = strrchr(s, '/') + 1; if (strstr(s, "vi") != NULL) keys = MODEKEY_VI; else keys = MODEKEY_EMACS; options_set_number(&global_s_options, "status-keys", keys); options_set_number(&global_w_options, "mode-keys", keys); } /* * Figure out the socket path. If specified on the command-line with -S * or -L, use it, otherwise try $TMUX or assume -L default. */ if (path == NULL) { /* If no -L, use the environment. */ if (label == NULL) { s = getenv("TMUX"); if (s != NULL) { path = xstrdup(s); path[strcspn (path, ",")] = '\0'; if (*path == '\0') { free(path); label = xstrdup("default"); } } else label = xstrdup("default"); } /* -L or default set. */ if (label != NULL) { if ((path = makesocketpath(label)) == NULL) { fprintf(stderr, "can't create socket: %s\n", strerror(errno)); exit(1); } } } free(label); if (strlcpy(socket_path, path, sizeof socket_path) >= sizeof socket_path) { fprintf(stderr, "socket path too long: %s\n", path); exit(1); } free(path); #ifdef HAVE_SETPROCTITLE /* Set process title. */ setproctitle("%s (%s)", __progname, socket_path); #endif /* Pass control to the client. */ exit(client_main(event_init(), argc, argv, flags)); }
/** * Program entry point. Constructs and launches the main program object. */ int main(int argc, char *argv[]) { Pool pool = pool_init(0x10000); /* 64K block size */ Options opt = options_init(); Source *in; Scope *scope = scope_new(&pool, 0); ListBuilder code = list_builder_init(&pool); /* Read the options: */ if (!options_parse(&opt, argc, argv)) { options_usage(argv[0]); goto error; } /* Determine output file name: */ if (!string_size(opt.name_out)) { if (string_rmatch(opt.name_in, string_from_k(".ol")) != 3) { fprintf(stderr, "error: If no output file is specified, the input file name must end with \".ol\".\n"); goto error; } opt.name_out = string(opt.name_in.p, opt.name_in.end - 3); } /* Input stream: */ in = source_load(&pool, opt.name_in); if (!in) { fprintf(stderr, "error: Could not open source file \"%s\"\n", opt.name_in.p); goto error; } /* Keywords: */ scope_add(scope, &pool, string_from_k("macro"), dynamic(type_keyword, keyword_new(&pool, parse_macro))); scope_add(scope, &pool, string_from_k("outline"), dynamic(type_keyword, keyword_new(&pool, parse_outline))); scope_add(scope, &pool, string_from_k("union"), dynamic(type_keyword, keyword_new(&pool, parse_union))); scope_add(scope, &pool, string_from_k("map"), dynamic(type_keyword, keyword_new(&pool, parse_map))); scope_add(scope, &pool, string_from_k("for"), dynamic(type_keyword, keyword_new(&pool, parse_for))); scope_add(scope, &pool, string_from_k("include"), dynamic(type_keyword, keyword_new(&pool, parse_include))); /* Do outline2c stuff: */ if (!parse_code(&pool, in, scope, out_list_builder(&code))) goto error; if (opt.debug) { printf("--- AST: ---\n"); dump_code(code.first, 0); printf("\n"); } if (!main_generate(&pool, code.first, &opt)) goto error; /* Clean up: */ pool_free(&pool); return 0; error: pool_free(&pool); return 1; }
int main(int argc, char **argv) { struct stat statbuf; uid_t uid = getuid(); uid_t gid = getgid(); uid_t euid = geteuid(); uid_t egid = getegid(); struct passwd * pwd = getpwuid(uid); struct group * grp = getgrgid(gid); if (argc < 3) usage(argv[0]); options_init(); openlog(PACKAGE, LOG_PID, LOG_DAEMON); memset(&statbuf, 0, sizeof(statbuf)); if (!options_binload(argv[1])) { syslog(LOG_ERR, "invalid binary config file %s", argv[1]); usage(argv[0]); } if (uid != 0) { if (strcmp(pwd->pw_name, CHILLI_USER)) { syslog(LOG_ERR, "has to run as user %s or root", CHILLI_USER); usage(argv[0]); } if (strcmp(grp->gr_name, CHILLI_GROUP)) { syslog(LOG_ERR, "has to run as group %s or root", CHILLI_GROUP); usage(argv[0]); } } syslog(LOG_DEBUG, "USER %s(%d/%d), GROUP %s(%d/%d) CHILLI[UID %d, GID %d]", pwd->pw_name, uid, euid, grp->gr_name, gid, egid, _options.uid, _options.gid); if (stat(argv[2], &statbuf)) { syslog(LOG_ERR, "%s: %s does not exist", strerror(errno), argv[2]); usage(argv[0]); } if (_options.uid && /* chilli is running as non-root */ _options.uid == euid && /* current euid same as chilli uid */ _options.gid == egid && /* current egid same as chilli gid */ statbuf.st_uid == 0 && /* script owned by root */ statbuf.st_gid == _options.gid && /* script group same as chilli gid */ (statbuf.st_mode & 0400) == 0400) { if (setuid(0)) syslog(LOG_ERR, "%s: setuid %s", strerror(errno), argv[0]); } syslog(LOG_INFO, "Running %s (%d/%d)", argv[2], getuid(), geteuid()); if (execv(argv[2], &argv[2])) { syslog(LOG_ERR, "%s: exec %s", strerror(errno), argv[2]); usage(argv[0]); } return 0; }
int main(int argc, char **argv) { struct client_ctx cctx; struct msg_command_data cmddata; struct buffer *b; struct cmd_list *cmdlist; struct cmd *cmd; struct pollfd pfd; struct hdr hdr; const char *shell; struct passwd *pw; char *path, *label, *cause, *home, *pass = NULL; char cwd[MAXPATHLEN]; int retcode, opt, flags, unlock, start_server; unlock = flags = 0; label = path = NULL; while ((opt = getopt(argc, argv, "28df:L:qS:uUVv")) != -1) { switch (opt) { case '2': flags |= IDENTIFY_256COLOURS; flags &= ~IDENTIFY_88COLOURS; break; case '8': flags |= IDENTIFY_88COLOURS; flags &= ~IDENTIFY_256COLOURS; break; case 'f': cfg_file = xstrdup(optarg); break; case 'L': if (path != NULL) { log_warnx("-L and -S cannot be used together"); exit(1); } if (label != NULL) xfree(label); label = xstrdup(optarg); break; case 'S': if (label != NULL) { log_warnx("-L and -S cannot be used together"); exit(1); } if (path != NULL) xfree(path); path = xstrdup(optarg); break; case 'q': be_quiet = 1; break; case 'u': flags |= IDENTIFY_UTF8; break; case 'U': unlock = 1; break; case 'd': flags |= IDENTIFY_HASDEFAULTS; break; case 'v': debug_level++; break; case 'V': printf("%s " BUILD "\n", __progname); exit(0); default: usage(); } } argc -= optind; argv += optind; log_open_tty(debug_level); siginit(); options_init(&global_options, NULL); options_set_number(&global_options, "bell-action", BELL_ANY); options_set_number(&global_options, "buffer-limit", 9); options_set_number(&global_options, "display-time", 750); options_set_number(&global_options, "history-limit", 2000); options_set_number(&global_options, "message-bg", 3); options_set_number(&global_options, "message-fg", 0); options_set_number(&global_options, "message-attr", GRID_ATTR_REVERSE); options_set_number(&global_options, "prefix", META); options_set_number(&global_options, "repeat-time", 500); options_set_number(&global_options, "set-titles", 1); options_set_number(&global_options, "lock-after-time", 0); options_set_number(&global_options, "set-remain-on-exit", 0); options_set_number(&global_options, "status", 1); options_set_number(&global_options, "status-bg", 2); options_set_number(&global_options, "status-fg", 0); options_set_number(&global_options, "status-attr", GRID_ATTR_REVERSE); options_set_number(&global_options, "status-interval", 15); options_set_number(&global_options, "status-left-length", 10); options_set_number(&global_options, "status-right-length", 40); options_set_string(&global_options, "status-left", "[#S]"); options_set_string( &global_options, "status-right", "\"#24T\" %%H:%%M %%d-%%b-%%y"); options_set_number(&global_options, "status-keys", MODEKEY_EMACS); options_init(&global_window_options, NULL); options_set_number(&global_window_options, "aggressive-resize", 0); options_set_number(&global_window_options, "clock-mode-colour", 4); options_set_number(&global_window_options, "clock-mode-style", 1); options_set_number(&global_window_options, "force-height", 0); options_set_number(&global_window_options, "force-width", 0); options_set_number(&global_window_options, "automatic-rename", 1); options_set_number(&global_window_options, "mode-bg", 3); options_set_number(&global_window_options, "mode-fg", 0); options_set_number( &global_window_options, "mode-attr", GRID_ATTR_REVERSE); options_set_number(&global_window_options, "mode-keys", MODEKEY_EMACS); options_set_number(&global_window_options, "monitor-activity", 0); options_set_number(&global_window_options, "utf8", 0); options_set_number(&global_window_options, "xterm-keys", 0); options_set_number(&global_window_options, "remain-on-exit", 0); options_set_number(&global_window_options, "window-status-bg", 8); options_set_number(&global_window_options, "window-status-fg", 8); options_set_number(&global_window_options, "window-status-attr", 0); if (cfg_file == NULL) { home = getenv("HOME"); if (home == NULL || *home == '\0') { pw = getpwuid(getuid()); if (pw != NULL) home = pw->pw_dir; endpwent(); } xasprintf(&cfg_file, "%s/%s", home, DEFAULT_CFG); if (access(cfg_file, R_OK) != 0) { xfree(cfg_file); cfg_file = NULL; } } else { if (access(cfg_file, R_OK) != 0) { log_warn("%s", cfg_file); exit(1); } } if (label == NULL) label = xstrdup("default"); if (path == NULL && (path = makesockpath(label)) == NULL) { log_warn("can't create socket"); exit(1); } xfree(label); shell = getenv("SHELL"); if (shell == NULL || *shell == '\0') { pw = getpwuid(getuid()); if (pw != NULL) shell = pw->pw_shell; endpwent(); if (shell == NULL || *shell == '\0') shell = _PATH_BSHELL; } options_set_string( &global_options, "default-command", "exec %s", shell); if (getcwd(cwd, sizeof cwd) == NULL) { log_warn("getcwd"); exit(1); } options_set_string(&global_options, "default-path", "%s", cwd); if (unlock) { if (argc != 0) { log_warnx("can't specify a command when unlocking"); exit(1); } cmdlist = NULL; if ((pass = getpass("Password: "******"%s", cause); exit(1); } } start_server = 0; TAILQ_FOREACH(cmd, cmdlist, qentry) { if (cmd->entry->flags & CMD_STARTSERVER) { start_server = 1; break; } } } memset(&cctx, 0, sizeof cctx); if (client_init(path, &cctx, start_server, flags) != 0) exit(1); xfree(path); b = buffer_create(BUFSIZ); if (unlock) { cmd_send_string(b, pass); client_write_server( &cctx, MSG_UNLOCK, BUFFER_OUT(b), BUFFER_USED(b)); } else { cmd_list_send(cmdlist, b); cmd_list_free(cmdlist); client_fill_session(&cmddata); client_write_server2(&cctx, MSG_COMMAND, &cmddata, sizeof cmddata, BUFFER_OUT(b), BUFFER_USED(b)); } buffer_destroy(b); retcode = 0; for (;;) { pfd.fd = cctx.srv_fd; pfd.events = POLLIN; if (BUFFER_USED(cctx.srv_out) > 0) pfd.events |= POLLOUT; if (poll(&pfd, 1, INFTIM) == -1) { if (errno == EAGAIN || errno == EINTR) continue; fatal("poll failed"); } if (buffer_poll(&pfd, cctx.srv_in, cctx.srv_out) != 0) goto out; restart: if (BUFFER_USED(cctx.srv_in) < sizeof hdr) continue; memcpy(&hdr, BUFFER_OUT(cctx.srv_in), sizeof hdr); if (BUFFER_USED(cctx.srv_in) < (sizeof hdr) + hdr.size) continue; buffer_remove(cctx.srv_in, sizeof hdr); switch (hdr.type) { case MSG_EXIT: case MSG_SHUTDOWN: goto out; case MSG_ERROR: retcode = 1; /* FALLTHROUGH */ case MSG_PRINT: if (hdr.size > INT_MAX - 1) fatalx("bad MSG_PRINT size"); log_info("%.*s", (int) hdr.size, BUFFER_OUT(cctx.srv_in)); if (hdr.size != 0) buffer_remove(cctx.srv_in, hdr.size); goto restart; case MSG_READY: retcode = client_main(&cctx); goto out; default: fatalx("unexpected command"); } } out: options_free(&global_options); options_free(&global_window_options); close(cctx.srv_fd); buffer_destroy(cctx.srv_in); buffer_destroy(cctx.srv_out); #ifdef DEBUG xmalloc_report(getpid(), "client"); #endif return (retcode); }
/* * Main entry point: */ int MAIN(int argc, char **argv) { // First print GPL information: printf("%s %s [%s] Copyright (C) 2017 basil\n", PROGRAM_NAME_LONG, PROGRAM_VERSION, PLATFORM); puts("License GPLv3+: GNU GPL version 3 or later " "<http://gnu.org/licenses/gpl.html>."); puts("This is free software: you are free to change and redistribute it."); puts("There is NO WARRANTY, to the extent permitted by law."); putchar('\n'); // Process options: options_init(argc, argv); // Initialise various components (order is important!). log_init(); trace("changing to home directory %s", PROGRAM_DIR); chdir_home(); trace("installing files (if required)"); install_files(); trace("initialising user configuration"); config_init(); trace("initialising tunnel management"); tunnel_init(); // Initialise the sockets library (if required on this platform). trace("initialising sockets"); init_sockets(); // Get number of threads. int num_threads = (options_get()->seen_num_threads? options_get()->val_num_threads: NUM_THREADS_DEFAULT); if (num_threads < 1 || num_threads > NUM_THREADS_MAX) { error("unable to spawn %d threads; expected a number within the " "range 1..%u", num_threads, NUM_THREADS_MAX); } // Create configuration server thread. trace("launching configuration server thread"); if (thread_lock_init(&config_lock)) { error("unable to initialise global configuration lock"); } thread_t config_thread; if (!options_get()->seen_no_ui && thread_create(&config_thread, configuration_thread, NULL) != 0) { error("unable to create configuration server thread"); } // Open the packet capture/injection device driver. trace("initialising packet capture"); if (!options_get()->seen_no_capture) { init_capture(); } // Open the tunnels. trace("initialising tunnels"); tunnel_file_read(); tunnel_open(); // Go to sleep if we are not capturing packets. while (options_get()->seen_no_capture) { sleeptime(UINT64_MAX); } // Start worker threads. for (int i = 1; i < num_threads; i++) { thread_t work_thread; if (thread_create(&work_thread, worker_thread, NULL) != 0) { error("unable to create worker thread"); } } worker_thread((void *)0); return EXIT_SUCCESS; }
int main(int argc, char *argv[]) { UpnpDevice_Handle upnp_handle; UpnpDevice_Handle upnp_device; char *device_desc = NULL; int rv = EXIT_FAILURE; struct sigaction sa; shutdown_flag = 0; /* init options, preset some defaults and parse command line arguments */ options = options_init(); options_parse_cli(argc, argv, options); memset(&sa, 0, sizeof(sa)); sa.sa_handler = signal_handler; sa.sa_flags = 0; sigfillset(&sa.sa_mask); if (sigaction(SIGINT, &sa, NULL) < 0) { perror("sigaction"); return EXIT_FAILURE; } if (sigaction(SIGTERM, &sa, NULL) < 0) { perror("sigaction"); return EXIT_FAILURE; } #ifdef UPNP_ENABLE_IPV6 rv = UpnpInit2(options->interface, 0); #else if (options->interface) fprintf(stderr, "Warning: ignoring interface argument, not supported by libupnp.\n"); rv = UpnpInit(NULL, 0); #endif if (rv != UPNP_E_SUCCESS) { fprintf(stderr, "UpnpInit failed: %d\n", rv); return EXIT_FAILURE; } rv = UpnpEnableWebserver(1); if (rv != UPNP_E_SUCCESS) { fprintf(stderr, "Could not enabled UPnP's internal HTTP server.\n"); goto upnp_finish; } device_desc = generate_device_desc(options); if (!device_desc) { fprintf(stderr, "Could not generated the UPnP device description.\n"); goto upnp_finish; } rv = UpnpRegisterRootDevice2(UPNPREG_BUF_DESC, device_desc, strlen(device_desc), 1, upnp_callback, &upnp_device, &upnp_device); if (rv != UPNP_E_SUCCESS) { fprintf(stderr, "Failed to register UPnP root device.\n"); goto free_out; } rv = UpnpSendAdvertisement(upnp_device, UPNP_ALIVE_INTERVAL); if (rv != UPNP_E_SUCCESS) { fprintf(stderr, "Failed to announce UPnP device.\n"); goto upnp_unregister; } while (!shutdown_flag) pause(); rv = EXIT_SUCCESS; upnp_unregister: UpnpUnRegisterRootDevice(upnp_device); free_out: if (device_desc) free(device_desc); upnp_finish: UpnpFinish(); err_out: options_free(options); return rv; }
int main(int argc, char **argv) { struct radius_packet_t radius_pack; struct in_addr radiuslisten; struct timeval timeout; int maxfd = 0; fd_set fdread; fd_set fdwrite; fd_set fdexcep; ssize_t status; int keep_going = 1; int reload_config = 1; int selfpipe; options_init(); selfpipe = selfpipe_init(); chilli_signals(&keep_going, &reload_config); process_options(argc, argv, 1); radiuslisten.s_addr = htonl(INADDR_ANY); memset(&server, 0, sizeof(server)); if (!(server.env = initssl_cli())) { syslog(LOG_ERR, "Failed to create ssl environment"); return -1; } if (radius_new(&server.radius_auth, &radiuslisten, _options.radiusauthport ? _options.radiusauthport : RADIUS_AUTHPORT, 0, 0)) { syslog(LOG_ERR, "Failed to create radius"); return -1; } if (radius_new(&server.radius_acct, &radiuslisten, _options.radiusacctport ? _options.radiusacctport : RADIUS_ACCTPORT, 0, 0)) { syslog(LOG_ERR, "Failed to create radius"); return -1; } if (_options.coaport) { if (radius_new(&server.radius_cli, &radiuslisten, 0, 0, 0) || radius_init_q(server.radius_cli, 8)) { syslog(LOG_ERR, "Failed to create radius"); return -1; } radius_set(server.radius_cli, 0, 0); radius_set_cb_auth_conf(server.radius_cli, cb_radius_auth_conf); } radius_set(server.radius_auth, 0, 0); radius_set(server.radius_acct, 0, 0); if (_options.gid && setgid(_options.gid)) { syslog(LOG_ERR, "%d setgid(%d) failed while running with gid = %d\n", errno, _options.gid, getgid()); } if (_options.uid && setuid(_options.uid)) { syslog(LOG_ERR, "%d setuid(%d) failed while running with uid = %d\n", errno, _options.uid, getuid()); } while (keep_going) { if (reload_config) { reload_options(argc, argv); reload_config = 0; } FD_ZERO(&fdread); FD_ZERO(&fdwrite); FD_ZERO(&fdexcep); FD_SET(selfpipe, &fdread); FD_SET(server.radius_auth->fd, &fdread); FD_SET(server.radius_acct->fd, &fdread); if (server.radius_auth->fd > maxfd) maxfd = server.radius_auth->fd; if (server.radius_acct->fd > maxfd) maxfd = server.radius_acct->fd; if (server.conn.sock) { FD_SET(server.conn.sock, &fdread); if (server.conn.sock > maxfd) maxfd = server.conn.sock; } if (server.radius_cli) { FD_SET(server.radius_cli->fd, &fdread); if (server.radius_cli->fd > maxfd) maxfd = server.radius_cli->fd; } timeout.tv_sec = 1; timeout.tv_usec = 0; status = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout); switch (status) { case -1: if (errno != EINTR) syslog(LOG_ERR, "%s: select() returned -1!", strerror(errno)); break; case 0: default: if (status > 0) { struct sockaddr_in addr; socklen_t fromlen = sizeof(addr); if (FD_ISSET(selfpipe, &fdread)) { chilli_handle_signal(0, 0); } if (FD_ISSET(server.radius_auth->fd, &fdread)) { /* * ---> Authentication */ if ((status = recvfrom(server.radius_auth->fd, &radius_pack, sizeof(radius_pack), 0, (struct sockaddr *) &addr, &fromlen)) <= 0) { syslog(LOG_ERR, "%s: recvfrom() failed", strerror(errno)); return -1; } memcpy(&server.auth_peer, &addr, sizeof(addr)); process_radius(&radius_pack, status); } if (FD_ISSET(server.radius_acct->fd, &fdread)) { /* * ---> Accounting */ syslog(LOG_DEBUG, "received accounting"); if ((status = recvfrom(server.radius_acct->fd, &radius_pack, sizeof(radius_pack), 0, (struct sockaddr *) &addr, &fromlen)) <= 0) { syslog(LOG_ERR, "%s: recvfrom() failed", strerror(errno)); return -1; } memcpy(&server.acct_peer, &addr, sizeof(addr)); process_radius(&radius_pack, status); } if (server.radius_cli) { if (FD_ISSET(server.radius_cli->fd, &fdread)) { radius_decaps(server.radius_cli, 0); } } if (server.conn.sock) { if (FD_ISSET(server.conn.sock, &fdread)) { process_radius_reply(); } } } break; } } selfpipe_finish(); return 0; }
int main(int argc, char **argv) { struct gengetopt_args_info args_info; struct hostent *host; char hostname[USERURLSIZE]; int numargs; int ret = -1; int i; options_init(); memset(&args_info, 0, sizeof(args_info)); if (cmdline_parser2(argc, argv, &args_info, 1, 1, 1) != 0) { log_err(0, "Failed to parse command line options"); goto end_processing; } if (args_info.version_given) { options_print_version(); exit(2); } if (args_info.help_given) { options_print_help(); exit(2); } if (cmdline_parser_configfile(args_info.conf_arg ? args_info.conf_arg : DEFCHILLICONF, &args_info, 0, 0, 0)) { log_err(0, "Failed to parse configuration file: %s!", args_info.conf_arg); if (!args_info.forgiving_flag) goto end_processing; } /* Get the system default DNS entries */ if (res_init()) { log_err(0, "Failed to update system DNS settings (res_init()!"); goto end_processing; } /* Handle each option */ _options.initialized = 1; if (args_info.debug_flag) _options.debug = args_info.debugfacility_arg; else _options.debug = 0; /* pass-throughs */ memset(_options.pass_throughs, 0, sizeof(_options.pass_throughs)); _options.num_pass_throughs = 0; /** simple configuration parameters **/ _options.layer3 = args_info.layer3_flag; #if(_debug_ && !defined(ENABLE_LAYER3)) if (_options.layer3) log_warn(0, "layer3 not implemented. build with --enable-layer3"); #endif _options.uid = args_info.uid_arg; _options.gid = args_info.gid_arg; _options.mtu = args_info.mtu_arg; _options.usetap = args_info.usetap_flag; _options.noarpentries = args_info.noarpentries_flag; #if(_debug_ && !defined(ENABLE_TAP)) if (_options.noarpentries) log_warn(0, "tap not implemented. build with --enable-tap"); #endif #if(_debug_ && !defined(ENABLE_TAP)) if (_options.usetap) log_warn(0, "tap not implemented. build with --enable-tap"); #endif _options.foreground = args_info.fg_flag; _options.interval = args_info.interval_arg; _options.lease = args_info.lease_arg; _options.leaseplus = args_info.leaseplus_arg; _options.dhcpstart = args_info.dhcpstart_arg; _options.dhcpend = args_info.dhcpend_arg; _options.eapolenable = args_info.eapolenable_flag; #if(_debug_ && !defined(ENABLE_EAPOL)) if (_options.eapolenable) log_warn(0, "EAPOL not implemented. build with --enable-eapol"); #endif _options.swapoctets = args_info.swapoctets_flag; _options.logfacility = args_info.logfacility_arg; _options.chillixml = args_info.chillixml_flag; _options.macauth = args_info.macauth_flag; _options.macreauth = args_info.macreauth_flag; _options.macauthdeny = args_info.macauthdeny_flag; _options.uamport = args_info.uamport_arg; #ifdef ENABLE_UAMUIPORT _options.uamuiport = args_info.uamuiport_arg; #endif _options.macallowlocal = args_info.macallowlocal_flag; _options.strictmacauth = args_info.strictmacauth_flag; _options.strictdhcp = args_info.strictdhcp_flag; _options.no_wispr1 = args_info.nowispr1_flag; _options.no_wispr2 = args_info.nowispr2_flag; _options.wpaguests = args_info.wpaguests_flag; _options.openidauth = args_info.openidauth_flag; _options.challengetimeout = args_info.challengetimeout_arg; _options.challengetimeout2 = args_info.challengetimeout2_arg; _options.defsessiontimeout = args_info.defsessiontimeout_arg; _options.definteriminterval = args_info.definteriminterval_arg; _options.defbandwidthmaxdown = args_info.defbandwidthmaxdown_arg; _options.defbandwidthmaxup = args_info.defbandwidthmaxup_arg; _options.defidletimeout = args_info.defidletimeout_arg; _options.radiusnasporttype = args_info.radiusnasporttype_arg; _options.radiusauthport = args_info.radiusauthport_arg; _options.radiusacctport = args_info.radiusacctport_arg; _options.coaport = args_info.coaport_arg; _options.coanoipcheck = args_info.coanoipcheck_flag; _options.radiustimeout = args_info.radiustimeout_arg; _options.radiusretry = args_info.radiusretry_arg; _options.radiusretrysec = args_info.radiusretrysec_arg; #ifdef ENABLE_RADPROXY _options.proxyport = args_info.proxyport_arg; _options.proxymacaccept = args_info.proxymacaccept_flag; _options.proxyonacct = args_info.proxyonacct_flag; #endif #if(_debug_ && !defined(ENABLE_RADPROXY)) if (args_info.proxyport_arg) log_err(0,"radproxy not implemented. build with --enable-radproxy"); #endif _options.txqlen = args_info.txqlen_arg; _options.ringsize = args_info.ringsize_arg; _options.sndbuf = args_info.sndbuf_arg; _options.rcvbuf = args_info.rcvbuf_arg; _options.childmax = args_info.childmax_arg; _options.postauth_proxyport = args_info.postauthproxyport_arg; _options.pap_always_ok = args_info.papalwaysok_flag; _options.mschapv2 = args_info.mschapv2_flag; _options.acct_update = args_info.acctupdate_flag; _options.dhcpradius = args_info.dhcpradius_flag; _options.dhcp_broadcast = args_info.dhcpbroadcast_flag; _options.dhcpgwport = args_info.dhcpgatewayport_arg; _options.noc2c = args_info.noc2c_flag; _options.tcpwin = args_info.tcpwin_arg; _options.tcpmss = args_info.tcpmss_arg; _options.max_clients = args_info.maxclients_arg; _options.radiusqsize = args_info.radiusqsize_arg; _options.dhcphashsize = args_info.dhcphashsize_arg; _options.uamdomain_ttl = args_info.uamdomainttl_arg; _options.seskeepalive = args_info.seskeepalive_flag; _options.uamallowpost = args_info.uamallowpost_flag; _options.redir = args_info.redir_flag; _options.redirurl = args_info.redirurl_flag; _options.statusfilesave = args_info.statusfilesave_flag; _options.dhcpnotidle = args_info.dhcpnotidle_flag; #if(_debug_ && !defined(ENABLE_CHILLIREDIR)) if (_options.redir) log_err(0, "chilli_redir not implemented. build with --enable-chilliredir"); #endif _options.redirssl = args_info.redirssl_flag; _options.uamuissl = args_info.uamuissl_flag; _options.domaindnslocal = args_info.domaindnslocal_flag; _options.framedservice = args_info.framedservice_flag; _options.radsec = args_info.radsec_flag; #if(_debug_ && !defined(ENABLE_CHILLIRADSEC)) if (_options.radsec) log_err(0, "chilli_radsec not implemented. build with --enable-chilliradsec"); #endif _options.noradallow = args_info.noradallow_flag; _options.peerid = args_info.peerid_arg; #if(_debug_ && !defined(ENABLE_CLUSTER)) if (_options.peerid) log_err(0, "clustering not implemented. build with --enable-cluster"); #endif _options.redirdnsreq = args_info.redirdnsreq_flag; #if(_debug_ && !defined(ENABLE_REDIRDNSREQ)) if (_options.redirdnsreq) log_err(0, "redirdnsreq not implemented. build with --enable-redirdnsreq"); #endif #ifdef ENABLE_IPV6 _options.ipv6 = args_info.ipv6_flag; _options.ipv6only = args_info.ipv6only_flag; #endif #ifdef ENABLE_LEAKYBUCKET _options.scalewin = args_info.scalewin_flag; _options.bwbucketupsize = args_info.bwbucketupsize_arg; _options.bwbucketdnsize = args_info.bwbucketdnsize_arg; _options.bwbucketminsize = args_info.bwbucketminsize_arg; #endif #ifdef ENABLE_PROXYVSA _options.vlanlocation = args_info.vlanlocation_flag; _options.location_stop_start = args_info.locationstopstart_flag; _options.location_copy_called = args_info.locationcopycalled_flag; _options.location_immediate_update = args_info.locationimmediateupdate_flag; _options.location_option_82 = args_info.locationopt82_flag; if (args_info.proxylocattr_given) { for (numargs = 0; numargs < args_info.proxylocattr_given && numargs < PROXYVSA_ATTR_CNT; ++numargs) { unsigned int i[2]; switch (sscanf(args_info.proxylocattr_arg[numargs], "%u,%u", &i[0], &i[1])) { case 0: log_err(0, "invalid input %s", args_info.proxylocattr_arg[numargs]); break; case 1: _options.proxy_loc[numargs].attr = i[0]; break; case 2: _options.proxy_loc[numargs].attr_vsa = i[0]; _options.proxy_loc[numargs].attr = i[1]; break; } log_dbg("Proxy location attr %d %d", (int)_options.proxy_loc[numargs].attr_vsa, (int)_options.proxy_loc[numargs].attr); } } #endif if (args_info.dhcpgateway_arg && !inet_aton(args_info.dhcpgateway_arg, &_options.dhcpgwip)) { log_err(0, "Invalid DHCP gateway IP address: %s!", args_info.dhcpgateway_arg); if (!args_info.forgiving_flag) goto end_processing; } if (args_info.dhcprelayagent_arg && !inet_aton(args_info.dhcprelayagent_arg, &_options.dhcprelayip)) { log_err(0, "Invalid DHCP gateway relay IP address: %s!", args_info.dhcprelayagent_arg); if (!args_info.forgiving_flag) goto end_processing; } _options.dhcpif = STRDUP(args_info.dhcpif_arg); #ifdef ENABLE_MULTILAN for (numargs = 0; numargs < args_info.moreif_given && numargs < MAX_MOREIF; ++numargs) { char *nif = STRDUP(args_info.moreif_arg[numargs]); char *vln = strchr(nif, '/'); _options.moreif[numargs].dhcpif = nif; if (vln) { if (strlen(vln) > 1) _options.moreif[numargs].vlan = vln + 1; *vln = 0; } else { vln = strchr(nif, '.'); if (vln && strlen(vln) > 1) _options.moreif[numargs].vlan = vln + 1; } } #endif if (!args_info.radiussecret_arg) { log_err(0, "radiussecret must be specified!"); if (!args_info.forgiving_flag) goto end_processing; } if (!args_info.nexthop_arg) { memset(_options.nexthop, 0, PKT_ETH_ALEN); _options.has_nexthop = 0; } else { unsigned int temp[PKT_ETH_ALEN]; char macstr[RADIUS_ATTR_VLEN]; int macstrlen; int i; if ((macstrlen = strlen(args_info.nexthop_arg)) >= (RADIUS_ATTR_VLEN-1)) { log_err(0, "MAC address too long"); if (!args_info.forgiving_flag) goto end_processing; } memcpy(macstr, args_info.nexthop_arg, macstrlen); macstr[macstrlen] = 0; /* Replace anything but hex with space */ for (i=0; i<macstrlen; i++) if (!isxdigit((int) macstr[i])) macstr[i] = 0x20; if (sscanf (macstr, "%2x %2x %2x %2x %2x %2x", &temp[0], &temp[1], &temp[2], &temp[3], &temp[4], &temp[5]) != 6) { log_err(0, "MAC conversion failed!"); return -1; } for (i = 0; i < PKT_ETH_ALEN; i++) _options.nexthop[i] = temp[i]; _options.has_nexthop = 1; } if (!args_info.dhcpmac_arg) { memset(_options.dhcpmac, 0, PKT_ETH_ALEN); _options.dhcpusemac = 0; _options.dhcpmacset = 0; } else { unsigned int temp[PKT_ETH_ALEN]; char macstr[RADIUS_ATTR_VLEN]; int macstrlen; int i; if ((macstrlen = strlen(args_info.dhcpmac_arg)) >= (RADIUS_ATTR_VLEN-1)) { log_err(0, "MAC address too long"); if (!args_info.forgiving_flag) goto end_processing; } memcpy(macstr, args_info.dhcpmac_arg, macstrlen); macstr[macstrlen] = 0; /* Replace anything but hex with space */ for (i=0; i<macstrlen; i++) if (!isxdigit((int) macstr[i])) macstr[i] = 0x20; if (sscanf (macstr, "%2x %2x %2x %2x %2x %2x", &temp[0], &temp[1], &temp[2], &temp[3], &temp[4], &temp[5]) != 6) { log_err(0, "MAC conversion failed!"); return -1; } for (i = 0; i < PKT_ETH_ALEN; i++) _options.dhcpmac[i] = temp[i]; _options.dhcpusemac = 1; _options.dhcpmacset = args_info.dhcpmacset_flag; } if (args_info.net_arg) { if (option_aton(&_options.net, &_options.mask, args_info.net_arg, 0)) { log_err(0, "Invalid network address: %s!", args_info.net_arg); if (!args_info.forgiving_flag) goto end_processing; } if (!args_info.uamlisten_arg) { _options.uamlisten.s_addr = htonl(ntohl(_options.net.s_addr)+1); } else if (!inet_aton(args_info.uamlisten_arg, &_options.uamlisten)) { log_err(0, "Invalid UAM IP address: %s!", args_info.uamlisten_arg); if (!args_info.forgiving_flag) goto end_processing; } if (!args_info.dhcplisten_arg) { _options.dhcplisten.s_addr = _options.uamlisten.s_addr; } else if (!inet_aton(args_info.dhcplisten_arg, &_options.dhcplisten)) { log_err(0, "Invalid DHCP IP address: %s!", args_info.dhcplisten_arg); if (!args_info.forgiving_flag) goto end_processing; } } else { log_err(0, "Network address must be specified ('net' parameter)!"); if (!args_info.forgiving_flag) goto end_processing; } log_dbg("DHCP Listen: %s", inet_ntoa(_options.dhcplisten)); log_dbg("UAM Listen: %s", inet_ntoa(_options.uamlisten)); if (!args_info.uamserver_arg) { log_err(0, "WARNING: No uamserver defiend!"); } if (args_info.uamserver_arg) { int uamserverport=80; if (_options.debug & DEBUG_CONF) { log_dbg("Uamserver: %s\n", args_info.uamserver_arg); } if (get_urlparts(args_info.uamserver_arg, hostname, USERURLSIZE, &uamserverport, 0)) { log_err(0, "Failed to parse uamserver: %s!", args_info.uamserver_arg); if (!args_info.forgiving_flag) goto end_processing; } if (!args_info.uamaliasname_arg || strncmp(args_info.uamaliasname_arg, hostname, strlen(args_info.uamaliasname_arg))) { if (!(host = gethostbyname(hostname))) { log_err(0, "Could not resolve IP address of uamserver: %s!", args_info.uamserver_arg); } else { int j = 0; pass_through pt; memset(&pt, 0, sizeof(pt)); pt.port = uamserverport; pt.mask.s_addr = ~0; while (host->h_addr_list[j] != NULL) { if (_options.debug & DEBUG_CONF) { log_dbg("Uamserver IP address #%d: %s\n", j, inet_ntoa(*(struct in_addr*) host->h_addr_list[j])); } pt.host.s_addr = ((struct in_addr*) host->h_addr_list[j++])->s_addr; if (pass_through_add(_options.pass_throughs, MAX_PASS_THROUGHS, &_options.num_pass_throughs, &pt, 0 #ifdef HAVE_PATRICIA , 0 #endif )) log_err(0, "Too many pass-throughs! skipped %s:%d", inet_ntoa(pt.host), pt.port); } } } } _options.uamanydns = args_info.uamanydns_flag; #ifdef ENABLE_UAMANYIP _options.uamanyip = args_info.uamanyip_flag; _options.uamnatanyip = args_info.uamnatanyip_flag; #endif _options.dnsparanoia = args_info.dnsparanoia_flag; _options.radiusoriginalurl = args_info.radiusoriginalurl_flag; _options.routeonetone = args_info.routeonetone_flag; #ifdef HAVE_PATRICIA _options.patricia = args_info.patricia_flag; #endif #ifdef ENABLE_GARDENACCOUNTING _options.nousergardendata = args_info.nousergardendata_flag; _options.uamgardendata = args_info.uamgardendata_flag; _options.uamotherdata = args_info.uamotherdata_flag; #endif for (numargs = 0; numargs < args_info.uamallowed_given; ++numargs) { pass_throughs_from_string(_options.pass_throughs, MAX_PASS_THROUGHS, &_options.num_pass_throughs, args_info.uamallowed_arg[numargs], 0, 0 #ifdef HAVE_PATRICIA , 0 #endif ); } #ifdef ENABLE_DHCPOPT _options.dhcp_options_len = 0; for (numargs = 0; numargs < args_info.dhcpopt_given; ++numargs) { unsigned char binopt[128]; int hex_length = strlen(args_info.dhcpopt_arg[numargs]); int bin_length = hex_length / 2; if (hex_length > 0 && (bin_length * 2) == hex_length && bin_length < sizeof(binopt)) { log_dbg("DHCP Options %s", args_info.dhcpopt_arg[numargs]); if (redir_hextochar((unsigned char *)args_info.dhcpopt_arg[numargs], hex_length, binopt, bin_length) == 0) { if (_options.dhcp_options_len + bin_length < sizeof(_options.dhcp_options)) { memcpy(_options.dhcp_options + _options.dhcp_options_len, binopt, bin_length); _options.dhcp_options_len += bin_length; } else { log_dbg("No room for DHCP option %d", (int)binopt[0]); } } else { log_dbg("Bad DHCP option hex encoding"); } } else { log_dbg("DHCP options are hex encoded binary"); } } #endif #ifdef ENABLE_MODULES memset(_options.modules, 0, sizeof(_options.modules)); for (numargs = 0; numargs < args_info.module_given; ++numargs) { if (numargs < MAX_MODULES) { char *n, *sc; int len, nlen; n = args_info.module_arg[numargs]; len = strlen(n); sc = strchr(n, ';'); if (!sc) sc = strchr(n, ':'); nlen = sc ? (sc - n) : len; safe_snprintf(_options.modules[numargs].name, sizeof(_options.modules[numargs].name), "%.*s", nlen, n); if (sc && len > (nlen + 1)) { safe_snprintf(_options.modules[numargs].conf, sizeof(_options.modules[numargs].conf), "%.*s", len - nlen - 1, sc + 1); } } } #endif #ifdef ENABLE_CHILLIREDIR /* for (numargs = 0; numargs < MAX_REGEX_PASS_THROUGHS; ++numargs) { if (_options.regex_pass_throughs[numargs].re_host.allocated) regfree(&_options.regex_pass_throughs[numargs].re_host); if (_options.regex_pass_throughs[numargs].re_path.allocated) regfree(&_options.regex_pass_throughs[numargs].re_path); if (_options.regex_pass_throughs[numargs].re_qs.allocated) regfree(&_options.regex_pass_throughs[numargs].re_qs); } */ memset(_options.regex_pass_throughs, 0, sizeof(_options.regex_pass_throughs)); _options.regex_num_pass_throughs = 0; for (numargs = 0; numargs < args_info.uamregex_given; ++numargs) { regex_pass_throughs_from_string(_options.regex_pass_throughs, MAX_REGEX_PASS_THROUGHS, &_options.regex_num_pass_throughs, args_info.uamregex_arg[numargs], 0); } #endif for (numargs = 0; numargs < MAX_UAM_DOMAINS; ++numargs) { if (_options.uamdomains[numargs]) free(_options.uamdomains[numargs]); _options.uamdomains[numargs] = 0; } if (args_info.uamdomain_given) { for (numargs = 0, i=0; numargs < args_info.uamdomain_given && i < MAX_UAM_DOMAINS; ++numargs) { char *tb = args_info.uamdomain_arg[numargs]; char *tok, *str, *ptr; for (str = tb ; i < MAX_UAM_DOMAINS; str = NULL) { tok = strtok_r(str, ",", &ptr); if (!tok) break; log_dbg("uamdomain %s", tok); _options.uamdomains[i++] = STRDUP(tok); } } } _options.allowdyn = 1; #ifdef ENABLE_UAMANYIP _options.autostatip = args_info.autostatip_arg; if (_options.autostatip) _options.uamanyip = 1; #endif if (args_info.nodynip_flag) { _options.allowdyn = 0; } else { if (!args_info.dynip_arg) { _options.dynip = STRDUP(args_info.net_arg); } else { struct in_addr addr; struct in_addr mask; _options.dynip = STRDUP(args_info.dynip_arg); if (option_aton(&addr, &mask, _options.dynip, 0)) { log_err(0, "Failed to parse dynamic IP address pool!"); if (!args_info.forgiving_flag) goto end_processing; } } } /* statip */ if (args_info.statip_arg) { struct in_addr addr; struct in_addr mask; _options.statip = STRDUP(args_info.statip_arg); if (option_aton(&addr, &mask, _options.statip, 0)) { log_err(0, "Failed to parse static IP address pool!"); return -1; } _options.allowstat = 1; } else { _options.allowstat = 0; } #ifdef ENABLE_UAMANYIP if (args_info.uamnatanyipex_arg) { if (option_aton(&_options.uamnatanyipex_addr, &_options.uamnatanyipex_mask, args_info.uamnatanyipex_arg, 0)) { log_err(0, "Failed to parse uamnatanyipex network!"); return -1; } } if (args_info.uamanyipex_arg) { if (option_aton(&_options.uamanyipex_addr, &_options.uamanyipex_mask, args_info.uamanyipex_arg, 0)) { log_err(0, "Failed to parse uamanyipex network!"); return -1; } } #endif if (args_info.dns1_arg) { if (!inet_aton(args_info.dns1_arg, &_options.dns1)) { log_err(0,"Invalid primary DNS address: %s!", args_info.dns1_arg); if (!args_info.forgiving_flag) goto end_processing; } } else if (_res.nscount >= 1) { _options.dns1 = _res.nsaddr_list[0].sin_addr; } else { _options.dns1.s_addr = 0; } if (args_info.dns2_arg) { if (!inet_aton(args_info.dns2_arg, &_options.dns2)) { log_err(0,"Invalid secondary DNS address: %s!", args_info.dns1_arg); if (!args_info.forgiving_flag) goto end_processing; } } else if (_res.nscount >= 2) { _options.dns2 = _res.nsaddr_list[1].sin_addr; } else { _options.dns2.s_addr = _options.dns1.s_addr; } /* If no listen option is specified listen to any local port */ /* Do hostname lookup to translate hostname to IP address */ if (args_info.radiuslisten_arg) { if (!(host = gethostbyname(args_info.radiuslisten_arg))) { log_err(0, "Invalid listening address: %s! [%s]", args_info.radiuslisten_arg, strerror(errno)); if (!args_info.forgiving_flag) goto end_processing; } else { memcpy(&_options.radiuslisten.s_addr, host->h_addr, host->h_length); } } else { _options.radiuslisten.s_addr = htonl(INADDR_ANY); } #ifdef ENABLE_NETNAT if (args_info.natip_arg) { if (!(host = gethostbyname(args_info.natip_arg))) { log_warn(0, "Invalid natip address: %s! [%s]", args_info.natip_arg, strerror(errno)); } else { memcpy(&_options.natip.s_addr, host->h_addr, host->h_length); } } #endif if (args_info.uamlogoutip_arg) { if (!(host = gethostbyname(args_info.uamlogoutip_arg))) { log_warn(0, "Invalid uamlogoutup address: %s! [%s]", args_info.uamlogoutip_arg, strerror(errno)); } else { memcpy(&_options.uamlogout.s_addr, host->h_addr, host->h_length); } } if (args_info.uamaliasip_arg) { if (!(host = gethostbyname(args_info.uamaliasip_arg))) { log_warn(0, "Invalid uamaliasip address: %s! [%s]", args_info.uamlogoutip_arg, strerror(errno)); } else { memcpy(&_options.uamalias.s_addr, host->h_addr, host->h_length); } } if (args_info.postauthproxy_arg) { if (!(host = gethostbyname(args_info.postauthproxy_arg))) { log_warn(0, "Invalid postauthproxy address: %s! [%s]", args_info.postauthproxy_arg, strerror(errno)); } else { memcpy(&_options.postauth_proxyip.s_addr, host->h_addr, host->h_length); } } /* If no option is specified terminate */ /* Do hostname lookup to translate hostname to IP address */ if (args_info.radiusserver1_arg) { if (!(host = gethostbyname(args_info.radiusserver1_arg))) { log_err(0, "Invalid radiusserver1 address: %s! [%s]", args_info.radiusserver1_arg, strerror(errno)); if (!args_info.forgiving_flag) goto end_processing; } else { memcpy(&_options.radiusserver1.s_addr, host->h_addr, host->h_length); } } else { log_err(0,"No radiusserver1 address given!"); if (!args_info.forgiving_flag) goto end_processing; } /* radiusserver2 */ /* If no option is specified terminate */ /* Do hostname lookup to translate hostname to IP address */ if (args_info.radiusserver2_arg) { if (!(host = gethostbyname(args_info.radiusserver2_arg))) { log_err(0, "Invalid radiusserver2 address: %s! [%s]", args_info.radiusserver2_arg, strerror(errno)); if (!args_info.forgiving_flag) goto end_processing; } else { memcpy(&_options.radiusserver2.s_addr, host->h_addr, host->h_length); } } else { _options.radiusserver2.s_addr = 0; } /* If no listen option is specified listen to any local port */ /* Do hostname lookup to translate hostname to IP address */ if (args_info.proxylisten_arg) { #ifdef ENABLE_RADPROXY if (!(host = gethostbyname(args_info.proxylisten_arg))) { log_err(0, "Invalid listening address: %s! [%s]", args_info.proxylisten_arg, strerror(errno)); if (!args_info.forgiving_flag) goto end_processing; } else { memcpy(&_options.proxylisten.s_addr, host->h_addr, host->h_length); } } else { _options.proxylisten.s_addr = htonl(INADDR_ANY); #elif (_debug_) log_warn(0,"radproxy not implemented. build with --enable-radproxy"); #endif } /* Store proxyclient as in_addr net and mask */ if (args_info.proxyclient_arg) { #ifdef ENABLE_RADPROXY if(option_aton(&_options.proxyaddr, &_options.proxymask, args_info.proxyclient_arg, 0)) { log_err(0,"Invalid proxy client address: %s!", args_info.proxyclient_arg); if (!args_info.forgiving_flag) goto end_processing; } } else { _options.proxyaddr.s_addr = ~0; /* Let nobody through */ _options.proxymask.s_addr = 0; #elif (_debug_) log_warn(0,"radproxy not implemented. build with --enable-radproxy"); #endif } memset(_options.macok, 0, sizeof(_options.macok)); _options.macoklen = 0; for (numargs = 0; numargs < args_info.macallowed_given; ++numargs) { char *p1 = NULL; char *p2 = NULL; char *p3 = malloc(strlen(args_info.macallowed_arg[numargs])+1); int i; unsigned int mac[6]; log_dbg("Macallowed #%d: %s", numargs, args_info.macallowed_arg[numargs]); strcpy(p3, args_info.macallowed_arg[numargs]); p1 = p3; if ((p2 = strchr(p1, ','))) { *p2 = '\0'; } while (p1) { if (_options.macoklen>=MACOK_MAX) { log_err(0,"Too many addresses in macallowed %s!", args_info.macallowed_arg); } else { /* Replace anything but hex and comma with space */ for (i=0; i<strlen(p1); i++) if (!isxdigit((int) p1[i])) p1[i] = 0x20; if (sscanf (p1, "%2x %2x %2x %2x %2x %2x", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]) != 6) { log_err(0, "Failed to convert macallowed option to MAC Address"); } else { log_dbg("Macallowed address #%d: %.2X-%.2X-%.2X-%.2X-%.2X-%.2X", _options.macoklen, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); for (i = 0; i < 6; i++) _options.macok[_options.macoklen][i] = (unsigned char) mac[i]; _options.macoklen++; } } if (p2) { p1 = p2+1; if ((p2 = strchr(p1, ','))) { *p2 = 0; } } else { p1 = NULL; } } free(p3); } /** string parameters **/ #ifdef HAVE_SSL _options.sslkeyfile = STRDUP(args_info.sslkeyfile_arg); _options.sslkeypass = STRDUP(args_info.sslkeypass_arg); _options.sslcertfile = STRDUP(args_info.sslcertfile_arg); _options.sslcafile = STRDUP(args_info.sslcafile_arg); #endif #ifdef USING_IPC_UNIX _options.unixipc = STRDUP(args_info.unixipc_arg); #endif #ifdef HAVE_NETFILTER_COOVA _options.kname = STRDUP(args_info.kname_arg); #endif #ifdef ENABLE_DNSLOG _options.dnslog = STRDUP(args_info.dnslog_arg); #else if (args_info.dnslog_arg) log_err(0, "option dnslog given when no support built-in"); #endif #ifdef ENABLE_IPWHITELIST _options.ipwhitelist = STRDUP(args_info.ipwhitelist_arg); #else if (args_info.ipwhitelist_arg) log_err(0, "option ipwhitelist given when no support built-in"); #endif #ifdef ENABLE_UAMDOMAINFILE _options.uamdomainfile = STRDUP(args_info.uamdomainfile_arg); #else if (args_info.uamdomainfile_arg) log_err(0, "option uamdomainfile given when no support built-in"); #endif #ifdef ENABLE_MODULES _options.moddir = STRDUP(args_info.moddir_arg); #else if (args_info.moddir_arg) log_err(0, "option moddir given when no support built-in"); #endif #ifdef ENABLE_RADPROXY if (!args_info.proxysecret_arg) { _options.proxysecret = STRDUP(args_info.radiussecret_arg); } else { _options.proxysecret = STRDUP(args_info.proxysecret_arg); } #endif #ifdef ENABLE_REDIRINJECT _options.inject = STRDUP(args_info.inject_arg); _options.inject_ext = STRDUP(args_info.injectext_arg); _options.inject_wispr = args_info.injectwispr_flag; #endif #ifdef ENABLE_EXTADMVSA if (args_info.extadmvsa_given) { for (numargs = 0; numargs < args_info.extadmvsa_given && numargs < EXTADMVSA_ATTR_CNT; ++numargs) { int len = strlen(args_info.extadmvsa_arg[numargs]); if (len > 0 && len < 256) { unsigned int i[2]; char s[256]; if (sscanf(args_info.extadmvsa_arg[numargs], "%u,%u:%s", &i[0], &i[1], s) == 3) { char *idx = strchr(s, ':'); _options.extadmvsa[numargs].attr_vsa = i[0]; _options.extadmvsa[numargs].attr = i[1]; if (idx) *idx = 0; safe_strncpy(_options.extadmvsa[numargs].script, s, sizeof(_options.extadmvsa[numargs].script)-1); if (idx) { safe_strncpy(_options.extadmvsa[numargs].data, idx + 1, sizeof(_options.extadmvsa[numargs].data)-1); } } else if (sscanf(args_info.extadmvsa_arg[numargs], "%u:%s", &i[0], s) == 2) { char *idx = strchr(s, ':'); _options.extadmvsa[numargs].attr = i[0]; if (idx) *idx = 0; safe_strncpy(_options.extadmvsa[numargs].script, s, sizeof(_options.extadmvsa[numargs].script)-1); if (idx) { safe_strncpy(_options.extadmvsa[numargs].data, idx + 1, sizeof(_options.extadmvsa[numargs].data)-1); } } else { log_err(0, "invalid input %s", args_info.extadmvsa_arg[numargs]); } } log_dbg("Extended admin-user attr (%d/%d) data=%s script=%s", (int)_options.extadmvsa[numargs].attr_vsa, (int)_options.extadmvsa[numargs].attr, _options.extadmvsa[numargs].data, _options.extadmvsa[numargs].script); } } #endif _options.peerkey = STRDUP(args_info.peerkey_arg); _options.routeif = STRDUP(args_info.routeif_arg); _options.wwwdir = STRDUP(args_info.wwwdir_arg); _options.wwwbin = STRDUP(args_info.wwwbin_arg); _options.uamui = STRDUP(args_info.uamui_arg); _options.localusers = STRDUP(args_info.localusers_arg); _options.uamurl = STRDUP(args_info.uamserver_arg); _options.uamaaaurl = STRDUP(args_info.uamaaaurl_arg); _options.uamhomepage = STRDUP(args_info.uamhomepage_arg); _options.wisprlogin = STRDUP(args_info.wisprlogin_arg); _options.uamsecret = STRDUP(args_info.uamsecret_arg); _options.macsuffix = STRDUP(args_info.macsuffix_arg); _options.macpasswd = STRDUP(args_info.macpasswd_arg); _options.adminuser = STRDUP(args_info.adminuser_arg); _options.adminpasswd = STRDUP(args_info.adminpasswd_arg); _options.adminupdatefile = STRDUP(args_info.adminupdatefile_arg); _options.rtmonfile = STRDUP(args_info.rtmonfile_arg); _options.ssid = STRDUP(args_info.ssid_arg); _options.vlan = STRDUP(args_info.vlan_arg); _options.nasmac = STRDUP(args_info.nasmac_arg); _options.nasip = STRDUP(args_info.nasip_arg); _options.tundev = STRDUP(args_info.tundev_arg); _options.radiusnasid = STRDUP(args_info.radiusnasid_arg); _options.radiuslocationid = STRDUP(args_info.radiuslocationid_arg); _options.radiuslocationname = STRDUP(args_info.radiuslocationname_arg); _options.locationname = STRDUP(args_info.locationname_arg); _options.radiussecret = STRDUP(args_info.radiussecret_arg); #ifdef ENABLE_LARGELIMITS /*_options.radiusacctsecret = STRDUP(args_info.radiusacctsecret_arg); _options.radiusadmsecret = STRDUP(args_info.radiusadmsecret_arg);*/ #endif _options.cmdsocket = STRDUP(args_info.cmdsocket_arg); _options.cmdsocketport = args_info.cmdsocketport_arg; _options.domain = STRDUP(args_info.domain_arg); _options.ipup = STRDUP(args_info.ipup_arg); _options.ipdown = STRDUP(args_info.ipdown_arg); _options.conup = STRDUP(args_info.conup_arg); _options.condown = STRDUP(args_info.condown_arg); _options.macup = STRDUP(args_info.macup_arg); _options.macdown = STRDUP(args_info.macdown_arg); _options.pidfile = STRDUP(args_info.pidfile_arg); _options.statedir = STRDUP(args_info.statedir_arg); _options.usestatusfile = STRDUP(args_info.usestatusfile_arg); _options.uamaliasname = STRDUP(args_info.uamaliasname_arg); _options.uamhostname = STRDUP(args_info.uamhostname_arg); _options.binconfig = STRDUP(args_info.bin_arg); _options.ethers = STRDUP(args_info.ethers_arg); #ifdef ENABLE_IEEE8021Q _options.ieee8021q = args_info.ieee8021q_flag; _options.ieee8021q_only = args_info.only8021q_flag; _options.vlanupdate = STRDUP(args_info.vlanupdate_arg); #endif #ifdef ENABLE_PROXYVSA _options.locationupdate = STRDUP(args_info.locationupdate_arg); #endif #ifdef EX_OPT_MAIN #include EX_OPT_MAIN #endif ret = 0; if (_options.binconfig) { /* save out the configuration */ bstring bt = bfromcstr(""); int ok = options_save(_options.binconfig, bt); if (!ok) log_err(0, "could not save configuration options!"); bdestroy(bt); } if (args_info.reload_flag) { if (execl(SBINDIR "/chilli_query", "chilli_query", args_info.cmdsocket_arg, "reload", (char *) 0) != 0) { log_err(errno, "execl() did not return 0!"); exit(2); } } end_processing: cmdline_parser_free (&args_info); return ret; }