static void wps_config(char** argv) { char argbuf[256]; char ** arg; int pos; const char* cmd; cmd=*argv; pos=sprintf(argbuf,cmd); arg=argv+1; while (*arg != NULL) { argbuf[pos++]=' '; pos+=sprintf(argbuf+pos,*arg); arg++; } // wpatalk_error("WPS Do cmd <%s>",argbuf); if (wordeq(cmd, "configme")) { if (is_sta) configme_sta(argbuf); else if (is_ap) configme_ap(argbuf); else wpatalk_error("Don't know if we're a STA or an AP"); return; } if (wordeq(cmd, "configthem")) { char* pwd=strstr(argbuf,"pin="); if(pwd != NULL && pin_checksum(pwd+4) != 0) { wpatalk_error("Invalid checksum PIN"); return; } if (is_sta) configthem_sta(argbuf); else if (is_ap) configthem_ap(argbuf); else wpatalk_error("Don't know if we're a STA or an AP"); return; } if (wordeq(cmd, "pin")) { char pin[9]; pin_gen(pin); printf("RANDOMLY GENERATED PIN == %s\n", pin); fflush(stdout); return; } if (interactive) wpatalk_error("Command not found: %s", cmd); else wpatalk_fatal("Command not found: %s", cmd); return; }
/* * Note: cmd must be cleaned up before being passed for execution */ static void wpatalk_command_execute(const char *cmd) { if (wordeq(cmd, "configstop")) { cmd = "CONFIGSTOP"; } if (isupper(*cmd)) { if (wpa_ctrl_command(ctrl_conn, cmd)) { if (interactive || keep_going) wpatalk_error("Command failed: %s", cmd); else wpatalk_fatal("Command failed: %s", cmd); } return; } #if 0 if (wordeq(cmd, "configme")) { if (is_sta) configme_sta(cmd); else if (is_ap) configme_ap(cmd); else wpatalk_error("Don't know if we're a STA or an AP"); return; } if (wordeq(cmd, "configthem")) { if (is_sta) configthem_sta(cmd); else if (is_ap) configthem_ap(cmd); else wpatalk_error("Don't know if we're a STA or an AP"); return; } if (wordeq(cmd, "pin")) { char pin[9]; pin_gen(pin); printf("RANDOMLY GENERATED PIN == %s\n", pin); fflush(stdout); return; } #endif if (interactive) wpatalk_error("Command not found: %s", cmd); else wpatalk_fatal("Command not found: %s", cmd); return; }
int main(int argc, char *argv[]) { const char *arg; progname = *argv++; if (strrchr(progname,'/') ) progname = strrchr(progname,'/')+1; srand(time(0)); /* get random numbers */ wpatalk_read_environment(); /* Options */ while ((arg = *argv++) != NULL) { if (*arg != '-') break; if (!strcmp(arg,"-h") || !strcmp(arg,"-help") || !strcmp(arg,"--help")) { print_help(); exit(0); continue; } if (!strcmp(arg, "-v")) { verbose++; continue; } if (!strcmp(arg, "-q")) { if (verbose > 0) verbose--; continue; } if (!strcmp(arg, "-i")) { interactive = 1; non_interactive = 0; continue; } if (!strcmp(arg, "-noi")) { non_interactive = 1; interactive = 0; continue; } if (!strcmp(arg, "-k")) { keep_going = 1; continue; } wpatalk_fatal("Unknown arg: %s", arg); } if (!arg || ! *arg) { wpatalk_fatal("Missing interface name"); } ctrl_if_spec = arg; if (*argv) { /* default, non-interactive, stop on first error */ } else { /* No args, read stdin, default to interactive mode for terminals */ if (!interactive && !non_interactive) interactive = isatty(0); } /* Open interface */ atexit(wpatalk_cleanup); signal(SIGHUP, wpatalk_terminate); signal(SIGINT, wpatalk_terminate); signal(SIGTERM, wpatalk_terminate); wpatalk_reconnect(); /* Commands */ if (! *argv) { /* No args ... read stdin */ if (interactive) { interactive_from_stdin(); } else { noninteractive_from_stdin(); } } else { const char *cmd; cmd=*argv; if (wordeq(cmd, "configthem") || wordeq(cmd, "configme") || wordeq(cmd, "pin")) { wps_config(argv); return 0; } while ((cmd = *argv++) != NULL) { wpatalk_command_execute(cmd); if ((! keep_going) && wpatalk_nerrors) { wpatalk_fatal("Exiting due to error(s)."); } } } if (wpatalk_nerrors) { wpatalk_fatal("There were error(s)"); } return 0; }
/** Note: the key and val param buffers are modified. */ void cproxy_parse_behavior_key_val(char *key, char *val, proxy_behavior *behavior) { uint32_t ms = 0; uint32_t x = 0; bool ok = false; assert(behavior != NULL); if (key != NULL && val != NULL) { key = trimstr(key); val = trimstr(val); if (wordeq(key, "cycle")) { ok = safe_strtoul(val, &behavior->cycle); } else if (wordeq(key, "downstream_max") || wordeq(key, "concurrency")) { ok = safe_strtoul(val, &behavior->downstream_max); } else if (wordeq(key, "downstream_conn_max")) { ok = safe_strtoul(val, &behavior->downstream_conn_max); } else if (wordeq(key, "weight") || wordeq(key, "downstream_weight")) { ok = safe_strtoul(val, &behavior->downstream_weight); } else if (wordeq(key, "retry") || wordeq(key, "downstream_retry")) { ok = safe_strtoul(val, &behavior->downstream_retry); } else if (wordeq(key, "protocol") || wordeq(key, "downstream_protocol")) { if (wordeq(val, "ascii") || wordeq(val, "memcached-ascii") || wordeq(val, "membase-ascii")) { behavior->downstream_protocol = proxy_downstream_ascii_prot; ok = true; } else if (wordeq(val, "binary") || wordeq(val, "memcached-binary") || wordeq(val, "membase-binary")) { behavior->downstream_protocol = proxy_downstream_binary_prot; ok = true; } else { if (settings.verbose > 1) { moxi_log_write("unknown behavior prot: %s\n", val); } } } else if (wordeq(key, "timeout") || wordeq(key, "downstream_timeout") || wordeq(key, "downstream_conn_timeout")) { ok = safe_strtoul(val, &ms); behavior->downstream_timeout.tv_sec = floor(ms / 1000.0); behavior->downstream_timeout.tv_usec = (ms % 1000) * 1000; } else if (wordeq(key, "downstream_conn_queue_timeout")) { ok = safe_strtoul(val, &ms); behavior->downstream_conn_queue_timeout.tv_sec = floor(ms / 1000.0); behavior->downstream_conn_queue_timeout.tv_usec = (ms % 1000) * 1000; } else if (wordeq(key, "wait_queue_timeout")) { ok = safe_strtoul(val, &ms); behavior->wait_queue_timeout.tv_sec = floor(ms / 1000.0); behavior->wait_queue_timeout.tv_usec = (ms % 1000) * 1000; } else if (wordeq(key, "connect_timeout")) { ok = safe_strtoul(val, &ms); behavior->connect_timeout.tv_sec = floor(ms / 1000.0); behavior->connect_timeout.tv_usec = (ms % 1000) * 1000; } else if (wordeq(key, "auth_timeout")) { ok = safe_strtoul(val, &ms); behavior->auth_timeout.tv_sec = floor(ms / 1000.0); behavior->auth_timeout.tv_usec = (ms % 1000) * 1000; } else if (wordeq(key, "time_stats")) { ok = safe_strtoul(val, &x); behavior->time_stats = x; } else if (wordeq(key, "mcs_opts")) { if (strlen(val) < sizeof(behavior->mcs_opts)) { strcpy(behavior->mcs_opts, val); ok = true; } } else if (wordeq(key, "connect_max_errors")) { ok = safe_strtoul(val, &behavior->connect_max_errors); } else if (wordeq(key, "connect_retry_interval")) { ok = safe_strtoul(val, &behavior->connect_retry_interval); } else if (wordeq(key, "front_cache_max")) { ok = safe_strtoul(val, &behavior->front_cache_max); } else if (wordeq(key, "front_cache_lifespan")) { ok = safe_strtoul(val, &behavior->front_cache_lifespan); } else if (wordeq(key, "front_cache_spec")) { if (strlen(val) < sizeof(behavior->front_cache_spec)) { strcpy(behavior->front_cache_spec, val); ok = true; } } else if (wordeq(key, "front_cache_unspec")) { if (strlen(val) < sizeof(behavior->front_cache_unspec)) { strcpy(behavior->front_cache_unspec, val); ok = true; } } else if (wordeq(key, "key_stats_max")) { ok = safe_strtoul(val, &behavior->key_stats_max); } else if (wordeq(key, "key_stats_lifespan")) { ok = safe_strtoul(val, &behavior->key_stats_lifespan); } else if (wordeq(key, "key_stats_spec")) { if (strlen(val) < sizeof(behavior->key_stats_spec)) { strcpy(behavior->key_stats_spec, val); ok = true; } } else if (wordeq(key, "key_stats_unspec")) { if (strlen(val) < sizeof(behavior->key_stats_unspec)) { strcpy(behavior->key_stats_unspec, val); ok = true; } } else if (wordeq(key, "optimize_set")) { if (strlen(val) < sizeof(behavior->optimize_set)) { strcpy(behavior->optimize_set, val); ok = true; } } else if (wordeq(key, "usr")) { if (strlen(val) < sizeof(behavior->usr)) { strcpy(behavior->usr, val); ok = true; } } else if (wordeq(key, "pwd")) { if (strlen(val) < sizeof(behavior->pwd)) { strcpy(behavior->pwd, val); ok = true; } } else if (wordeq(key, "host")) { if (strlen(val) < sizeof(behavior->host)) { strcpy(behavior->host, val); ok = true; } } else if (wordeq(key, "port")) { ok = safe_strtol(val, &behavior->port); } else if (wordeq(key, "bucket")) { if (strlen(val) < sizeof(behavior->bucket)) { strcpy(behavior->bucket, val); ok = true; } } else if (wordeq(key, "port_listen")) { ok = safe_strtol(val, &behavior->port_listen); } else if (wordeq(key, "default_bucket_name")) { if (strlen(val) < sizeof(behavior->default_bucket_name)) { strcpy(behavior->default_bucket_name, val); ok = true; } } else if (key[0] == '#') { // Comment. ok = true; } else { if (settings.verbose > 1) { moxi_log_write("ERROR: unknown behavior key: %s\n", key); } } } if (ok == false) { moxi_log_write("ERROR: config error in key: %s value: %s\n", key, val); } }
/** The cfg_str looks like... * * apikey=jidname@jhostname%jpassword,config=config,host=host * or... * jidname@jhostname%jpassword,config=config,host=host * * Only the apikey is needed, so it can also look like... * * jidname@jhostname%jpassword */ int cproxy_init_agent(char *cfg_str, proxy_behavior behavior, int nthreads) { init_extensions(); if (cfg_str == NULL) { fprintf(stderr, "missing cfg\n"); exit(EXIT_FAILURE); } int cfg_len = strlen(cfg_str); if (cfg_len <= 0) { fprintf(stderr, "empty cfg\n"); exit(EXIT_FAILURE); } char *buff; if (strncmp(cfg_str, "apikey=", 7) == 0) { buff = trimstrdup(cfg_str); } else { buff = calloc(cfg_len + 50, sizeof(char)); if (buff != NULL) { snprintf(buff, cfg_len + 50, "apikey=%s", cfg_str); } buff = trimstr(buff); } char *next = buff; int rv = 0; while (next != NULL) { char *jid = NULL; char *jpw = NULL; char *jpwmem = NULL; char *config = NULL; char *host = NULL; char *cur = trimstr(strsep(&next, ";")); while (cur != NULL) { char *key_val = trimstr(strsep(&cur, ",\r\n")); if (key_val != NULL) { char *key = trimstr(strsep(&key_val, "=")); char *val = trimstr(key_val); bool handled = true; if (key != NULL && val != NULL) { if (wordeq(key, "apikey")) { jid = strsep(&val, "%"); jpw = val; } else if (wordeq(key, "config")) { config = val; } else if (wordeq(key, "host")) { host = val; } else { handled = false; } } else { handled = false; } if (handled == false && key != NULL && key[0] != '#' && key[0] != '\0') { if (settings.verbose > 0) { fprintf(stderr, "unknown configuration key: %s\n", key); } } } } if (jid == NULL || strlen(jid) <= 0) { fprintf(stderr, "missing conflate id\n"); exit(EXIT_FAILURE); } if (jpw == NULL) { // Handle if jid/jpw is in user:password@fqdn format // instead of user@fqdn%password format. // char *colon = strchr(jid, ':'); char *asign = strchr(jid, '@'); if (colon != NULL && asign != NULL && asign > colon) { *asign = '\0'; jpw = jpwmem = strdup(colon + 1); *asign = '@'; do { *colon = *asign; colon++; asign++; } while (*asign != '\0'); *colon = '\0'; } } if (jpw == NULL || strlen(jpw) <= 0) { fprintf(stderr, "missing conflate password\n"); exit(EXIT_FAILURE); } int config_alloc = 0; if (config == NULL) { config_alloc = strlen(jid) + 100; config = calloc(config_alloc, 1); if (config != NULL) { snprintf(config, config_alloc, CONFLATE_DB_PATH "/%s.cfg", jid); } else { fprintf(stderr, "conflate config buf alloc\n"); exit(EXIT_FAILURE); } } if (settings.verbose > 1) { fprintf(stderr, "cproxy_init jid %s\n", jid); } if (cproxy_init_agent_start(jid, jpw, config, host, behavior, nthreads) != NULL) { rv++; } if (config_alloc > 0 && config != NULL) { free(config); } if (jpwmem) { free(jpwmem); } } free(buff); return rv; }