Exemple #1
1
int hostnameEdit(const char *option2) {

  struct uci_context *c;
  struct uci_ptr p;

  int length2= strlen("system.@system[0].hostname=")+strlen(option2)+1;
  char *hostname = safe_malloc(length2);
  strcpy(hostname,"system.@system[0].hostname="); 
  strcat(hostname,option2); 

  c = uci_alloc_context();
  if (uci_lookup_ptr (c, &p, hostname, true) != UCI_OK)
    {
      uci_perror (c, "XXX");
      return 1;
    }

  uci_set(c,&p);   
  uci_save(c, p.p);
  uci_commit(c, &p.p, false);
  uci_free_context (c);
 free(hostname);
  return(0); 
}
Exemple #2
0
static void
swconfig_load_uci(struct switch_dev *dev, const char *name)
{
	struct uci_context *ctx;
	struct uci_package *p = NULL;
	struct uci_element *e;
	int ret = -1;

	ctx = uci_alloc_context();
	if (!ctx)
		return;

	uci_load(ctx, name, &p);
	if (!p) {
		uci_perror(ctx, "Failed to load config file: ");
		goto out;
	}

	ret = swlib_apply_from_uci(dev, p);
	if (ret < 0)
		fprintf(stderr, "Failed to apply configuration for switch '%s'\n", dev->dev_name);

out:
	uci_free_context(ctx);
	exit(ret);
}
Exemple #3
0
uint32_t system_helpers_get_nodeid()
{
    char *opt;
    struct uci_ptr uciptr;
    int retval;
    long node_id;
    struct uci_context *ctx = uci_alloc_context();

    if(ctx == NULL)
        return 0;

    opt = malloc(strlen(NODEIDPATH)+1);
    strcpy(opt, NODEIDPATH);
    memset(&uciptr, 0, sizeof(uciptr));

    retval = uci_lookup_ptr(ctx, &uciptr, opt, true);

    if (retval != UCI_OK || uciptr.o == NULL) {
        free(opt);
        uci_free_context(ctx);
        return 0;
    }

    node_id = atol(uciptr.o->v.string);
    free(opt);
    uci_free_context(ctx);

    if (node_id <= 0 || node_id > UINT32_MAX)
        return 0;

    return (uint32_t) node_id;
}
Exemple #4
0
bool config_init(void)
{
	if (conf.uci_ctx != NULL) {
		uci_free_context(conf.uci_ctx);
	}
	return (conf.uci_ctx = uci_alloc_context()) ? true : false;
}
Exemple #5
0
static struct json_object * get_hostname(void) {
	struct json_object *ret = NULL;

	struct uci_context *ctx = uci_alloc_context();
	ctx->flags &= ~UCI_FLAG_STRICT;

	char section[] = "system.@system[0]";
	struct uci_ptr ptr;
	if (uci_lookup_ptr(ctx, &ptr, section, true))
		goto error;

	struct uci_section *s = ptr.s;

	const char *hostname = uci_lookup_option_string(ctx, s, "pretty_hostname");

	if (!hostname)
		hostname = uci_lookup_option_string(ctx, s, "hostname");

	ret = gluonutil_wrap_string(hostname);

error:
	uci_free_context(ctx);

	return ret;
}
Exemple #6
0
int ssidEdit(const char *option1) {

  struct uci_context *c;
  struct uci_ptr p;

  int length1= strlen("wireless.@wifi-iface[0].ssid=")+strlen(option1)+1;
  char *ssid = safe_malloc(length1);
  strcpy(ssid,"wireless.@wifi-iface[0].ssid="); 
  strcat(ssid,option1); 


  c = uci_alloc_context();
  if (uci_lookup_ptr (c, &p, ssid, true) != UCI_OK)
    {
      uci_perror (c, "XXX");
      return 1;
    }

  uci_set(c,&p);   
  uci_save(c, p.p);
  uci_commit(c, &p.p, false);
  uci_free_context (c);
 free(ssid);
  return(0); 
}
Exemple #7
0
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;
}
Exemple #8
0
/*############################## Functions ###################################*/
int check_internet_status(void)
{
	p_debug("access get_wwanip\n");
	char value[64]= "\0";
	char uci_option_str[64]= "\0";
	struct ifreq ifr_wlan1;
	int inet_sock_wlan1;
	int ret = 0;

	ctx=uci_alloc_context();
	memset(uci_option_str, '\0', 64);
	memset(value, '\0', 64);
	strcpy(uci_option_str, "network.wan.workmode"); 
	uci_get_option_value(uci_option_str, value);
	uci_free_context(ctx);

	p_debug("value = %s\n",value);
	if(!strcmp(value, "0"))
		strcpy(ifr_wlan1.ifr_name, "eth0.1");
	else if(!strcmp(value, "1"))
		strcpy(ifr_wlan1.ifr_name, "apcli0");
	else if(!strcmp(value, "2"))
		strcpy(ifr_wlan1.ifr_name, "ppp0");
	else{
		p_debug("unknown workmode\n");
		return -1;
	}
	
	inet_sock_wlan1 = socket(AF_INET, SOCK_DGRAM, 0);
	if(-1 == inet_sock_wlan1)
	{
		p_debug("creat socket error!\n");
		ret = -1;
	}
	if (ioctl(inet_sock_wlan1, SIOCGIFADDR, &ifr_wlan1) <  0)
	{
		p_debug("get no wan ip!\n");
		ret = 0;
	}else
	{
		p_debug("get wan ip success!\n");
		ret = 1;
	}
	if(-1 != inet_sock_wlan1)
		close(inet_sock_wlan1);
	
	return ret;
}
Exemple #9
0
char * ssidRead() {

  struct uci_context *c;
  struct uci_ptr p;

  char *ssid = safe_strdup ("wireless.@wifi-iface[0].ssid"); 
  c = uci_alloc_context();
  if (uci_lookup_ptr (c, &p, ssid, true) != UCI_OK)
    {
      uci_perror (c, "XXX");
      return 1;
    }

  char *str = strdup(p.o->v.string);
  uci_free_context (c);
  free(ssid);
  return str; 
}
Exemple #10
0
char * hostnameRead() {

  struct uci_context *c;
  struct uci_ptr p;

  char *ssid = strdup ("system.@system[0].hostname"); 
  c = uci_alloc_context();
  if (uci_lookup_ptr (c, &p, ssid, true) != UCI_OK)
    {
      uci_perror (c, "XXX");
      return 1;
    }

  char *str = safe_strdup(p.o->v.string);
  uci_free_context (c);
  free(ssid);
  return str; 
}
Exemple #11
0
char * urlRead() {

  struct uci_context *c;
  struct uci_ptr p;

  char *url = safe_strdup ("smartwifi.@smartwifi[0].imageurl"); 
  c = uci_alloc_context();
  if (uci_lookup_ptr (c, &p, url, true) != UCI_OK)
    {
      uci_perror (c, "XXX");
      return 1;
    }

  char *str = strdup(p.o->v.string);
  uci_free_context (c);
  free(url);
  return str; 
}
Exemple #12
0
    Meter
        *meter;

    int
        n=0;


    struct uci_package * pkg = NULL;
    struct uci_element *e;
    char *tmp;
    const char *value;
    int modbus_id;
    struct uci_section *s;


    ctx = uci_alloc_context(); 
    if (UCI_OK != uci_load(ctx, METER_LASTVALUE_UCI_CONFIG_FILE, &pkg))
        goto cleanup; 

    for(lp=head;lp;lp=lp->next)
    {
	meter = (Meter *)lp->data;
	int flag = 0;
	uci_foreach_element(&pkg->sections, e)
	{
	    s = uci_to_section(e);
	

	    if(!strcmp("meter_lastvalue",s->type))
	    {
		printf("section s's type is meter_lastvalue\n");
Exemple #13
0
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;

}
Exemple #14
0
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 */
}
Exemple #15
0
int main(int argc, char *argv[]) {
    const char *lengthStr = getenv("CONTENT_LENGTH"); 
    if (!lengthStr) {
        badRequestResponse("Request Invaild");
        return EXIT_SUCCESS;
    }

    const size_t length = atoi(lengthStr);
    char content[MAX_CONTENT];
    fgets(content, min(length + 1, MAX_CONTENT), stdin);
    
    json_object *json = json_tokener_parse(content);
    UCIContext *ctx = uci_alloc_context();
    if (!json) {
        badRequestResponse("Invaild JSON");
        goto fail;
    }

    json_object *value = NULL;
    json_bool ret = json_object_object_get_ex(json, "mode", &value);
    if (!ret) {
        badRequestResponse("Incomplete Arguments");
        goto fail;
    }
    const char *mode = json_object_get_string(value);
    if (!strcmp(mode, "static")) {
        ret = json_object_object_get_ex(json, "ip", &value);
        if (!ret) {
            badRequestResponse("Incomplete Arguments");
            goto fail;
        }
        const char *ip = json_object_get_string(value);
        if (!isVaildIP(ip)) {
            badRequestResponse("Invaild Argument: ip");
            goto fail;
        }

        ret = json_object_object_get_ex(json, "netmask", &value);
        if (!ret) {
            badRequestResponse("Incomplete Arguments");
            goto fail;
        }
        const char *netmask = json_object_get_string(value);
        if (!isVaildIP(netmask)) {
            badRequestResponse("Invaild Argument: netmask");
            goto fail;
        }

        ret = json_object_object_get_ex(json, "gateway", &value);
        if (!ret) {
            badRequestResponse("Incomplete Arguments");
            goto fail;
        }
        const char *gateway = json_object_get_string(value);
        if (!isVaildIP(gateway)) {
            badRequestResponse("Invaild Argument: gateway");
            goto fail;
        }

        ret = json_object_object_get_ex(json, "dnsA", &value);
        if (!ret) {
            badRequestResponse("Incomplete Arguments");
            goto fail;
        }
        const char *dnsA = json_object_get_string(value);
        if (!isVaildIP(dnsA)) {
            badRequestResponse("Invaild Argument: dnsA");
            goto fail;
        }

        ret = json_object_object_get_ex(json, "dnsB", &value);
        if (!ret) {
            badRequestResponse("Incomplete Arguments");
            goto fail;
        }
        const char *dnsB = json_object_get_string(value);
        if (!isVaildIP(dnsB)) {
            badRequestResponse("Invaild Argument: dnsB");
            goto fail;
        }

        const char *commands[] = { 
                                    "network.lan.proto", 
                                    "network.lan.ipaddr", 
                                    "network.lan.netmask", 
                                    "network.lan.gateway", 
                                    "network.lan.dns", 
                                    NULL
                                 };
        const char *values[] = { mode, ip, netmask, gateway, dnsA, NULL };
        if (statusSetter(ctx, commands, values) < 0 || uci(ctx, UCI_COMMIT, "network.lan")) goto fail;

    } else if (!strcmp(mode, "dhcp")) {
        const char *commands[] = { 
                                    "network.lan.proto", 
                                    NULL
                                 };
        const char *values[] = { mode, NULL };
        if (statusSetter(ctx, commands, values) < 0 || uci(ctx, UCI_COMMIT, "network.lan")) goto fail;

    } else if (!strcmp(mode, "pppoe")) {
        ret = json_object_object_get_ex(json, "username", &value);
        if (!ret) {
            badRequestResponse("Incomplete Arguments");
            goto fail;
        }
        const char *username = json_object_get_string(value);
        if (strlen(username) > MAX_CONTENT) {
            badRequestResponse("Invaild Argument: username");
            goto fail;
        }

        ret = json_object_object_get_ex(json, "password", &value);
        if (!ret) {
            badRequestResponse("Incomplete Arguments");
            goto fail;
        }
        const char *password = json_object_get_string(value);
        if (strlen(password) > MAX_CONTENT) {
            badRequestResponse("Invaild Argument: password");
            goto fail;
        }

        const char *commands[] = { 
                                    "network.lan.proto", 
                                    "network.wan.username", 
                                    "network.wan.password", 
                                    NULL
                                 };
        const char *values[] = { mode, username, password, NULL };
        if (statusSetter(ctx, commands, values) < 0 || uci(ctx, UCI_COMMIT, "network.wan")) goto fail;

    } else {
        badRequestResponse("Invaild Argument: mode");
        goto fail;
    }
    
    printf("Content-Type:application/json\n\n");
    fflush(stdout);

fail:
    uci_free_context(ctx);
    json_object_put(json);

    return EXIT_SUCCESS;
}
/**
 * Nodewatcher agent entry point.
 */
int main(int argc, char **argv)
{
    struct stat s;
    const char *ubus_socket = NULL;
    int log_option = 0;
    int c;

    while ((c = getopt(argc, argv, "s:")) != -1) {
        switch (c) {
        case 's':
            ubus_socket = optarg;
            break;
        case 'f':
            log_option |= LOG_PERROR;
            break;
        default:
            break;
        }
    }

    /* Open the syslog facility */
    openlog("nw-agent", log_option, LOG_DAEMON);

    /* Create directory for temporary run files */
    if (stat("/var/run/nodewatcher-agent", &s))
        mkdir("/var/run/nodewatcher-agent", 0700);

    umask(0077);

    /* Setup signal handlers */
    signal(SIGPIPE, SIG_IGN);
    /* TODO: Handle SIGHUP to reload? */

    /* Seed random generator */
    unsigned int seed;
    int rc = nw_read_random_bytes(&seed, sizeof(seed));
    if (rc < 0) {
        fprintf(stderr, "ERROR: Failed to seed random generator!\n");
        return -1;
    }

    srandom(seed);

    /* Initialize event loop */
    uloop_init();

    /* Attempt to establish connection to ubus daemon */
    for (;;) {
        ubus = ubus_connect(ubus_socket);
        if (!ubus) {
            syslog(LOG_WARNING, "Failed to connect to ubus!");
            sleep(10);
            continue;
        }

        break;
    }

    ubus_add_uloop(ubus);

    /* Initialize UCI context */
    uci = uci_alloc_context();
    if (!uci) {
        syslog(LOG_ERR, "Failed to initialize UCI!");
        return -1;
    }

    /* Discover and initialize modules */
    if (nw_module_init(ubus, uci) != 0) {
        syslog(LOG_ERR, "Unable to initialize modules!");
        return -1;
    }

    /* Initialize the scheduler */
    if (nw_scheduler_init() != 0) {
        syslog(LOG_ERR, "Unable to initialize scheduler!");
        return -1;
    }

    /* Initialize the output exporter */
    if (nw_output_init(uci) != 0) {
        syslog(LOG_ERR, "Unable to initialize output exporter!");
        return -1;
    }

    /* Enter the event loop */
    uloop_run();
    ubus_free(ubus);
    uci_free_context(uci);
    uloop_done();

    return 0;
}