struct uci_context* ucix_init(const char *config_file) { struct uci_context *ctx = uci_alloc_context(); uci_add_history_path(ctx, "/var/state"); if(uci_load(ctx, config_file, NULL) != UCI_OK) { return NULL; } return ctx; }
int read_config(const char * filename) { struct uci_context * ctx; ctx = uci_alloc_context(); if (!ctx) { cw_log(LOG_ERR,"Fatal: Can't create uci ctx, can't read config file"); return 0; } struct uci_package * pkg; if (filename == NULL) { filename = "wtp_uci.conf"; } cw_dbg(DBG_INFO,"Reading config file %s",filename); int rc = uci_load(ctx, filename, &pkg ); if (rc == UCI_ERR_NOTFOUND) { cw_log(LOG_INFO,"Config file '%s' not found, running without config",filename); return 1; } if (rc) { char * errstr; uci_get_errorstr(ctx, &errstr, ""); cw_log(LOG_ERR,"Fatal: Can't read config file: %s",errstr); return 0; } struct uci_section * section; section = get_anon_section(pkg,"dbg"); read_dbg_options(ctx,section); section = get_anon_section(pkg,"wtp"); if (!section) { cw_dbg(DBG_INFO,"No 'wtp' section found, running without config"); return 1; } read_timers(ctx,section); const char *str; str = uci_lookup_option_string(ctx,section,"name"); if (str) conf_wtpname = strdup(str); str = uci_lookup_option_string(ctx,section,"mtu"); if (str) conf_mtu = atoi(str); str = uci_lookup_option_string(ctx,section,"mtu_discovery"); if (str) conf_mtu_discovery = atoi(str); str = uci_lookup_option_string(ctx,section,"interface"); if (str) conf_primary_if=strdup(str); str = uci_lookup_option_string(ctx,section,"ip"); if (str) conf_ip=strdup(str); str = uci_lookup_option_string(ctx,section,"ssl_key"); if (str) conf_sslkeyfilename=strdup(str); str = uci_lookup_option_string(ctx,section,"ssl_cert"); if (str) conf_sslcertfilename=strdup(str); str = uci_lookup_option_string(ctx,section,"dtls_psk"); if (str) conf_dtls_psk=strdup(str); str = uci_lookup_option_string(ctx,section,"ssl_cipher"); if (str) conf_dtls_cipher=strdup(str); str = uci_lookup_option_string(ctx,section,"vendor_id"); if (str) conf_vendor_id=atoi(str); str = uci_lookup_option_string(ctx,section,"echo_interval"); if (str) conf_echo_interval=atoi(str); str = uci_lookup_option_string(ctx,section,"software_version"); if (str) { uint8_t * s = bstr16_create_from_cfgstr(str); bstr16_replace(&conf_software_version,s); } str = uci_lookup_option_string(ctx,section,"hardware_version"); if (str) { uint8_t * s = bstr16_create_from_cfgstr(str); bstr16_replace(&conf_hardware_version,s); } str = uci_lookup_option_string(ctx,section,"bootloader_version"); if (str) { uint8_t * s = bstr16_create_from_cfgstr(str); bstr16_replace(&conf_bootloader_version,s); } str = uci_lookup_option_string(ctx,section,"board_id"); if (str) { uint8_t * s = bstr16_create_from_cfgstr(str); bstr16_replace(&conf_board_id,s); } str = uci_lookup_option_string(ctx,section,"board_revision"); if (str) { uint8_t * s = bstr16_create_from_cfgstr(str); bstr16_replace(&conf_board_revision,s); } str = uci_lookup_option_string(ctx,section,"serial_no"); if (str) { uint8_t * s = bstr16_create_from_cfgstr(str); bstr16_replace(&conf_serial_no,s); } str = uci_lookup_option_string(ctx,section,"model_no"); if (str) { uint8_t * s = bstr16_create_from_cfgstr(str); bstr16_replace(&conf_model_no,s); } return 1; }
void load_settings(struct settings *settings) { struct uci_context *ctx = uci_alloc_context(); if (!ctx) { fprintf(stderr, "autoupdater: error: failed to allocate UCI context\n"); abort(); } ctx->flags &= ~UCI_FLAG_STRICT; struct uci_package *p; struct uci_section *s; if (uci_load(ctx, "autoupdater", &p) != UCI_OK) { fputs("autoupdater: error: unable to load UCI package\n", stderr); exit(1); } s = uci_lookup_section(ctx, p, "settings"); if (!s || strcmp(s->type, "autoupdater")) { fputs("autoupdater: error: unable to load UCI settings\n", stderr); exit(1); } const char *enabled = uci_lookup_option_string(ctx, s, "enabled"); if ((!enabled || strcmp(enabled, "1")) && !settings->force) { fputs("autoupdater is disabled\n", stderr); exit(0); } const char *version_file = uci_lookup_option_string(ctx, s, "version_file"); if (version_file) settings->old_version = read_one_line(version_file); if (!settings->branch) settings->branch = uci_lookup_option_string(ctx, s, "branch"); if (!settings->branch) { fputs("autoupdater: error: no branch given in settings or command line\n", stderr); exit(1); } struct uci_section *branch = uci_lookup_section(ctx, p, settings->branch); if (!branch || strcmp(branch->type, "branch")) { fprintf(stderr, "autoupdater: error: unable to load branch configuration for branch '%s'\n", settings->branch); exit(1); } settings->good_signatures = load_positive_number(ctx, branch, "good_signatures"); if (settings->n_mirrors == 0) settings->mirrors = load_string_list(ctx, branch, "mirror", &settings->n_mirrors); const char **pubkeys_str = load_string_list(ctx, branch, "pubkey", &settings->n_pubkeys); settings->pubkeys = safe_malloc(settings->n_pubkeys * sizeof(ecc_25519_work_t)); size_t ignored_keys = 0; for (size_t i = 0; i < settings->n_pubkeys; i++) { ecc_int256_t pubkey_packed; if (!pubkeys_str[i]) goto pubkey_fail; if (!parsehex(pubkey_packed.p, pubkeys_str[i], 32)) goto pubkey_fail; if (!ecc_25519_load_packed_legacy(&settings->pubkeys[i-ignored_keys], &pubkey_packed)) goto pubkey_fail; if (!ecdsa_is_valid_pubkey(&settings->pubkeys[i-ignored_keys])) goto pubkey_fail; continue; pubkey_fail: fprintf(stderr, "autoupdater: warning: ignoring invalid public key %s\n", pubkeys_str[i]); ignored_keys++; } settings->n_pubkeys -= ignored_keys; /* Don't free UCI context, we still reference values from it */ }