Example #1
0
/* Check that loading and saving a file given with a relative path
 * works. Bug #238
 */
static void testRelPath(CuTest *tc) {
    int r;

    r = aug_rm(aug, "/augeas/load/*");
    CuAssertPositive(tc, r);

    r = aug_set(aug, "/augeas/load/Hosts/lens", "Hosts.lns");
    CuAssertRetSuccess(tc, r);
    r = aug_set(aug, "/augeas/load/Hosts/incl", "etc/hosts");
    CuAssertRetSuccess(tc, r);
    r = aug_load(aug);
    CuAssertRetSuccess(tc, r);

    r = aug_match(aug, "/files/etc/hosts/1/alias[ . = 'new']", NULL);
    CuAssertIntEquals(tc, 0, r);

    r = aug_set(aug, "/files/etc/hosts/1/alias[last() + 1]", "new");
    CuAssertRetSuccess(tc, r);

    r = aug_save(aug);
    CuAssertRetSuccess(tc, r);
    r = aug_match(aug, "/augeas//error", NULL);
    CuAssertIntEquals(tc, 0, r);

    /* Force reloading the file */
    r = aug_rm(aug, "/augeas/files//mtime");
    CuAssertPositive(tc, r);

    r = aug_load(aug);
    CuAssertRetSuccess(tc, r);

    r = aug_match(aug, "/files/etc/hosts/1/alias[. = 'new']", NULL);
    CuAssertIntEquals(tc, 1, r);
}
Example #2
0
static void testSet(CuTest *tc) {
    int r;
    const char *value;
    struct augeas *aug;

    aug = aug_init(root, loadpath, AUG_NO_STDINC|AUG_NO_LOAD);
    CuAssertPtrNotNull(tc, aug);
    CuAssertIntEquals(tc, AUG_NOERROR, aug_error(aug));

    /* aug_set returns 0 for a simple set */
    r = aug_set(aug, "/augeas/testSet", "foo");
    CuAssertIntEquals(tc, 0, r);
    CuAssertIntEquals(tc, AUG_NOERROR, aug_error(aug));

    /* aug_set returns -1 when cannot set due to multiple nodes */
    r = aug_set(aug, "/augeas/version/save/*", "foo");
    CuAssertIntEquals(tc, -1, r);
    CuAssertIntEquals(tc, AUG_EMMATCH, aug_error(aug));

    /* aug_set is able to set the context, even when currently invalid */
    r = aug_set(aug, "/augeas/context", "( /files | /augeas )");
    CuAssertIntEquals(tc, 0, r);
    CuAssertIntEquals(tc, AUG_NOERROR, aug_error(aug));
    r = aug_get(aug, "/augeas/version", &value);
    CuAssertIntEquals(tc, -1, r);
    CuAssertIntEquals(tc, AUG_EMMATCH, aug_error(aug));
    r = aug_set(aug, "/augeas/context", "/files");
    CuAssertIntEquals(tc, 0, r);
    CuAssertIntEquals(tc, AUG_NOERROR, aug_error(aug));

    aug_close(aug);
}
Example #3
0
static errno_t
sss_config_set_option(struct sss_config_ctx *ctx,
                      const char *section,
                      const char *option,
                      const char *value)
{
    TALLOC_CTX *tmp_ctx = NULL;
    char *target_path = NULL;
    char *option_path = NULL;
    errno_t ret;
    int aug_ret;

    tmp_ctx = talloc_new(NULL);
    if (tmp_ctx == NULL) {
        return ENOMEM;
    }

    target_path = build_section_path(tmp_ctx, ctx, section);
    if (target_path == NULL) {
        ret = ENOMEM;
        goto done;
    }

    option_path = build_option_path(tmp_ctx, ctx, section, option);
    if (option_path == NULL) {
        ret = ENOMEM;
        goto done;
    }

    /* Set configuration option:
     *
     * # make sure the section exists
     * set /files/$file/target[. = "$section"] $section
     *
     * # set value
     * set /files/$file/target[. = "$section"]/$option $value
     */

    aug_ret = aug_set(ctx->auges_ctx, target_path, section);
    if (aug_ret != 0) {
        ret = EIO;
        goto done;
    }

    aug_ret = aug_set(ctx->auges_ctx, option_path, value);
    if (aug_ret != 0) {
        ret = EIO;
        goto done;
    }

    ret = EOK;

done:
    talloc_free(tmp_ctx);
    return ret;
}
Example #4
0
bool augeas_init()  {
    fs::path incl_path = getConfigPath(fs::path(MODULE_CONFDIR));

    if(!fs::exists(incl_path)) {
        // Complain hard; even if we can create it, any files won't be picked up
        // after we've done aug_load. And user probably wont have any use of an empty
        // dir anyway, so tell him to fix it instead.
        AGO_ERROR() << "Cannot use " << incl_path << " as confdir, does not exist";
        return false;
    } else if (!fs::is_directory(incl_path)) {
        AGO_ERROR() << "Cannot use " << incl_path << " as confdir, not a directory";
        return false;
    }

    incl_path /= "*.conf";

    // Look for augeas lens files in conf path too, in case
    // we have not installed globally
    fs::path extra_loadpath = getConfigPath();

    if(augeas) {
        // Re-init
        aug_close(augeas);
    }

    AGO_TRACE() << "Loading Augeas with extra_loadpath="
        << extra_loadpath.string()
        << " and include path="
        << incl_path.string();

    augeas = aug_init(NULL, extra_loadpath.c_str(), AUG_SAVE_BACKUP | AUG_NO_MODL_AUTOLOAD);
    if (augeas == NULL) {
        AGO_ERROR() << "Can't initalize augeas";
        return false;
    }

    aug_set(augeas, "/augeas/load/Agocontrol/lens", "agocontrol.lns");
    aug_set(augeas, "/augeas/load/Agocontrol/incl", incl_path.c_str());
    if (aug_load(augeas) != 0) {
        // We can get errors below, even if we have 0
        AGO_ERROR() << "Augeas load ret -1";
    }

    if(aug_error(augeas)) {
        std::string err = augeasGetError();
        AGO_ERROR() << "Augeas error: " << err;

        aug_close(augeas);
        augeas = NULL;
        return false;
    }else
        AGO_TRACE() << "Augeas inited without errors";

    return true;
}
Example #5
0
int ntp_add_server(const char* udp_address, const char* association_type, bool iburst, bool prefer, char** msg)
{
	int ret;
	char* path = NULL, *srv_path = NULL;

	assert(udp_address);
	assert(association_type);

	asprintf(&path, "/files/%s/%s", AUGEAS_NTP_CONF, association_type);
	ret = aug_match(sysaugeas, path, NULL);
	if (ret == -1) {
		asprintf(msg, "Augeas match for \"%s\" failed: %s", path, aug_error_message(sysaugeas));
		free(path);
		return EXIT_FAILURE;
	}
	free(path);

	/* add new item after the last one */
	ret++;
	asprintf(&srv_path, "/files/%s/%s[%d]", AUGEAS_NTP_CONF, association_type, ret);
	if (aug_set(sysaugeas, srv_path, udp_address) == -1) {
		asprintf(msg, "Setting NTP %s \"%s\" failed: %s", association_type, udp_address, aug_error_message(sysaugeas));
		free(srv_path);
		return EXIT_FAILURE;
	}

	if (iburst) {
		path = NULL;
		asprintf(&path, "/files/%s/%s[%d]/iburst", AUGEAS_NTP_CONF, association_type, ret);
		if (aug_set(sysaugeas, path, NULL) == -1) {
			asprintf(msg, "Setting iburst option for %s \"%s\" failed: %s", association_type, udp_address, aug_error_message(sysaugeas));
			free(path);
			aug_rm(sysaugeas, srv_path);
			free(srv_path);
			return EXIT_FAILURE;
		}
		free(path);
	}

	if (prefer) {
		path = NULL;
		asprintf(&path, "/files/%s/%s[%d]/prefer", AUGEAS_NTP_CONF, association_type, ret);
		if (aug_set(sysaugeas, path, NULL) == -1) {
			asprintf(msg, "Setting prefer option for %s \"%s\" failed: %s", association_type, udp_address, aug_error_message(sysaugeas));
			free(path);
			aug_rm(sysaugeas, srv_path);
			free(srv_path);
			return EXIT_FAILURE;
		}
		free(path);
	}

	free(srv_path);
	return EXIT_SUCCESS;
}
Example #6
0
static int switch_auth(const char *value, char **msg)
{
	const char* sshdpid_env;
	augeas *augeas_running;
	char *path = NULL;

	asprintf(&path, "/files/%s/PasswordAuthentication", NETOPEER_SSHD_CONF);
	if (aug_set(sysaugeas, path, value) == -1) {
		asprintf(msg, "Unable to set PasswordAuthentication to \"%s\" (%s).", value, aug_error_message(sysaugeas));
		free(path);
		return (EXIT_FAILURE);
	}
	free(path);
	path = NULL;

	/* Save the changes made by children callbacks via augeas */
	if (augeas_save(msg) != 0) {
		return (EXIT_FAILURE);
	}

	asprintf(&path, "/files/%s.running", NETOPEER_SSHD_CONF);
	if ((sshdpid_env = getenv("SSHD_PID")) != NULL && access(path, F_OK) == 0) {
		/* we have info about listening SSH server, update its config and make
		 * it reload the configuration. If something get wrong, still return
		 * success, new settings just will be applied after the SSH server
		 * reboot (if the settings will be stored also into startup datastore).
		 */
		augeas_running = aug_init(NULL, NULL, AUG_NO_MODL_AUTOLOAD | AUG_NO_ERR_CLOSE);
		if (aug_error(augeas_running) != AUG_NOERROR) {
			free(path);
			return EXIT_SUCCESS;
		}
		aug_set(augeas_running, "/augeas/load/Sshd/lens", "Sshd.lns");
		aug_set(augeas_running, "/augeas/load/Sshd/incl", path);
		free(path); path = NULL;
		aug_load(augeas_running);

		if (aug_match(augeas_running, "/augeas//error", NULL) != 0) {
			aug_close(augeas_running);
			return EXIT_SUCCESS;
		}

		asprintf(&path, "/files/%s.running/PasswordAuthentication", NETOPEER_SSHD_CONF);
		if (aug_set(augeas_running, path, value) == 0 &&
				aug_save(augeas_running) == 0) {
			/* make the server to reload configuration */
			kill(atoi(sshdpid_env), SIGHUP);
		}
		free(path);
		aug_close(augeas_running);
	}

	return (EXIT_SUCCESS);
}
Example #7
0
static int loadTree(augeas* augeasHandle, const char *lensPath, char *content)
{
	aug_set (augeasHandle, AUGEAS_CONTENT_ROOT, content);

	return aug_text_store (augeasHandle, lensPath, AUGEAS_CONTENT_ROOT,
	AUGEAS_TREE_ROOT);
}
Example #8
0
static void testMultipleXfm(CuTest *tc) {
    int r;

    r = aug_set(aug, "/augeas/load/Yum2/lens", "Yum.lns");
    CuAssertIntEquals(tc, 0, r);
    r = aug_set(aug, "/augeas/load/Yum2/incl", "/etc/yum.repos.d/*");
    CuAssertIntEquals(tc, 0, r);

    r = aug_set(aug, "/files/etc/yum.repos.d/fedora.repo/fedora/enabled", "0");
    CuAssertIntEquals(tc, 0, r);

    r = aug_save(aug);
    CuAssertIntEquals(tc, -1, r);
    r = aug_error(aug);
    CuAssertIntEquals(tc, AUG_EMXFM, r);
}
Example #9
0
static int set(VALUE s, VALUE path, VALUE value) {
    augeas *aug = aug_handle(s);
    const char *cpath = StringValueCStr(path) ;
    const char *cvalue = StringValueCStrOrNull(value) ;

    return aug_set(aug, cpath, cvalue) ;
}
Example #10
0
bool setConfigSectionOption(const char* section, const char* option, const char* value, const char *app) {
    AGO_DEBUG() << "setConfigSectionOption: section="
        << LOG_STR_OR_NULL(section)
        << " option="
        << LOG_STR_OR_NULL(option)
        << " value="
        << LOG_STR_OR_NULL(value)
        << " app="
        << LOG_STR_OR_NULL(app);

    if (augeas==NULL){
        if(!augeas_init()) {
            AGO_ERROR() << "Save failed: augeas not inited";
            return false;
        }
    }

    if(app == NULL || !strlen(app))
        app = section;

    fs::path file = confPathFromApp(app);

    // Make file is writeable; augeas 1.2.0 (probably others) segfaults,
    // we protect ourselfs from this:
    //   https://github.com/hercules-team/augeas/issues/178
    // When the issue is fixed, we can remove this hack
    if(fs::exists(file)) {
        if(access(file.c_str(), W_OK) != 0) {
            AGO_ERROR() << "Could not write config file "
                << file.string() << ": "
                << strerror(errno);
            return false;
        }
    }

    std::string aug_path = augeasPath(file, section, option);

    if (aug_set(augeas, aug_path.c_str(), value) == -1) {
        AGO_WARNING() << "Could not set value!";
        if(aug_error(augeas)) {
            std::string err = augeasGetError();
            AGO_WARNING() << "Augeas error: " << err;
        }
        return false;
    }

    if (aug_save(augeas) == -1) {
        AGO_WARNING() << "Could not write config file " << file.string();

        if(aug_error(augeas)) {
            std::string err = augeasGetError();
            AGO_ERROR() << "Augeas error: " << err;
        }

        return false;
    }

    return true;
}
Example #11
0
int dns_add_search_domain(const char* domain, int index, char** msg)
{
	int ret;
	char* path;

	assert(domain);
	assert(index >= 1);
	if (domain == NULL || index < 1) {
		asprintf(msg, "NULL arguments.");
		return EXIT_FAILURE;
	}

	switch (ret = aug_match(sysaugeas, "/files/"AUGEAS_DNS_CONF"/search/domain", NULL)) {
	case -1:
		asprintf(msg, "Augeas match for \"%s\" failed: %s", "/files/"AUGEAS_DNS_CONF"/search/domain", aug_error_message(sysaugeas));
		return EXIT_FAILURE;
	case 0:
		/* First domain to be added */
		if (index != 1) {
			asprintf(msg, "Configuration data (dns-resolver search domains) are inconsistent with system configuration file (code 1).");
			return EXIT_FAILURE;
		}
		break;
	default:
		/* Some domains already in the config file */
		if ((index - ret) > 1) {
			asprintf(msg, "Configuration data (dns-resolver search domains) are inconsistent with system configuration file (code 2).");
			return EXIT_FAILURE;
		}

		/* insert new (empty) node */
		if (index == 1) {
			if (aug_insert(sysaugeas, "/files/"AUGEAS_DNS_CONF"/search/domain[1]", "domain", 1) == -1) {
				asprintf(msg, "Inserting DNS search domain configuration before \"%s\" failed (%s)", "/files/"AUGEAS_DNS_CONF"/search/domain[1]", aug_error_message(sysaugeas));
				return (EXIT_FAILURE);
			}
		} else {
			asprintf(&path, "/files/%s/search/domain[%d]", AUGEAS_DNS_CONF, index - 1);
			if (aug_insert(sysaugeas, path, "domain", 0) == -1) {
				asprintf(msg, "Inserting DNS search domain configuration after \"%s\" failed (%s)", path, aug_error_message(sysaugeas));
				free(path);
				return (EXIT_FAILURE);
			}
			free(path); path = NULL;
		}
	}

	/* Set the value of the newly inserted node (or possibly create it, too) */
	asprintf(&path, "/files/%s/search/domain[%d]", AUGEAS_DNS_CONF, index);
	if (aug_set(sysaugeas, path, domain) == -1) {
		aug_rm(sysaugeas, path); /* previously inserted, do rollback */
		asprintf(msg, "Unable to set DNS search domain \"%s\" (%s).", domain, aug_error_message(sysaugeas));
		free(path);
		return EXIT_FAILURE;
	}
	free(path);

	return EXIT_SUCCESS;
}
Example #12
0
static gchar * network_interfaces ()
{
        augeas *aug;
        gchar *value = NULL, **if_match, **option_match, *path, *result, *p, option[128];
        gint if_number, option_number, i, j;

        aug = aug_init (NULL, NULL, AUG_NONE | AUG_NO_ERR_CLOSE | AUG_NO_MODL_AUTOLOAD);
        aug_set (aug, "/augeas/load/Interfaces/lens", "Interfaces.lns");
        aug_set (aug, "/augeas/load/Interfaces/incl", "/etc/network/interfaces");
        aug_load (aug);
        aug_get (aug, "//files/etc/network/interfaces", (const gchar **)&value);
        if_number = aug_match (aug, "//files/etc/network/interfaces/iface[.!='lo']", &if_match);
        result = g_strdup ("[");
        for (i = 0; i < if_number; i++) {
                aug_get (aug, if_match[i], (const gchar **)&value);
                p = result;
                result = g_strdup_printf ("%s{\"name\": \"%s\",", p, value);
                g_free (p);
                p = g_strdup_printf ("//files/etc/network/interfaces/iface[.='%s']/*", value);
                option_number = aug_match (aug, p, &option_match);
                g_free (p);
                for (j = 0; j < option_number; j++) {
                        sscanf (option_match[j], "%*[^]]]/%[^/]", option);
                        if (g_str_has_prefix (option, "#comment")) {
                                continue;
                        }
                        aug_get (aug, option_match[j], (const gchar **)&value);
                        p = result;
                        result = g_strdup_printf ("%s\n\"%s\": \"%s\",", p, option, value);
                        g_free (p);
                        g_free (option_match[j]);
                }
                g_free (option_match);
                g_free (if_match[i]);
                result[strlen (result) - 1] = '}';
                p = result;
                result = g_strdup_printf ("%s,", p);
                g_free (p);
        }
        g_free (if_match);
        aug_close (aug);
        result[strlen (result) - 1] = ']';

        return result;
}
Example #13
0
static void testNonExistentLens(CuTest *tc) {
    int r;

    r = aug_rm(aug, "/augeas/load/*");
    CuAssertTrue(tc, r >= 0);

    r = aug_set(aug, "/augeas/load/Fake/lens", "Fake.lns");
    CuAssertIntEquals(tc, 0, r);
    r = aug_set(aug, "/augeas/load/Fake/incl", "/fake");
    CuAssertIntEquals(tc, 0, r);
    r = aug_set(aug, "/files/fake/entry", "value");
    CuAssertIntEquals(tc, 0, r);

    r = aug_save(aug);
    CuAssertIntEquals(tc, -1, r);
    r = aug_error(aug);
    CuAssertIntEquals(tc, AUG_ENOLENS, r);
}
Example #14
0
static int saveTree (augeas * augeasHandle, KeySet * ks, const char * lensPath, Key * parentKey)
{
	int ret = 0;

	size_t prefixSize = keyGetNameSize (parentKey) - 1;
	size_t arraySize = ksGetSize (ks);
	Key ** keyArray = calloc (ksGetSize (ks), sizeof (Key *));
	ret = elektraKsToMemArray (ks, keyArray);

	if (ret < 0) goto memoryerror;

	qsort (keyArray, arraySize, sizeof (Key *), keyCmpOrderWrapper);

	/* convert the Elektra KeySet to an Augeas tree */
	for (size_t i = 0; i < arraySize; i++)
	{
		Key * key = keyArray[i];
		char * nodeName;
		ret = asprintf (&nodeName, AUGEAS_TREE_ROOT "%s", (keyName (key) + prefixSize));

		if (ret < 0) goto memoryerror;

		aug_set (augeasHandle, nodeName, keyString (key));
		elektraFree (nodeName);
	}

	elektraFree (keyArray);

	/* remove keys not present in the KeySet */
	struct OrphanSearch * data = elektraMalloc (sizeof (struct OrphanSearch));

	if (!data) return -1;

	data->ks = ks;
	data->parentKey = parentKey;

	ret = foreachAugeasNode (augeasHandle, AUGEAS_TREE_ROOT, &removeOrphan, data);

	elektraFree (data);

	/* build the tree */
	ret = aug_text_retrieve (augeasHandle, lensPath, AUGEAS_CONTENT_ROOT, AUGEAS_TREE_ROOT, AUGEAS_OUTPUT_ROOT);

	if (ret < 0)
	{
		/* report the augeas specific error */
		ELEKTRA_SET_ERROR (85, parentKey, getAugeasError (augeasHandle));
	}

	return ret;

memoryerror:
	elektraFree (keyArray);
	ELEKTRA_SET_ERROR (87, parentKey, "Unable to allocate memory while saving the augeas tree");
	return -1;
}
Example #15
0
static int Paug_set(lua_State *L)
{
    augeas *a;
    const char *path, *value;

    a = Paug_checkarg(L, 1);
    path = luaL_checkstring(L, 2);
    value = lua_isnil(L, 3) ? NULL : luaL_checkstring(L, 3);
    return pushresult(L, aug_set(a, path, value), a, path);
}
Example #16
0
int dns_set_opt_attempts(const char* number, char** msg)
{
	const char *path = "/files/"AUGEAS_DNS_CONF"/options/attempts";

	assert(number);

	/* Create or set existing one */
	if (aug_set(sysaugeas, path, number) == -1) {
		asprintf(msg, "Setting DNS attempts option failed (%s)", aug_error_message(sysaugeas));
		return (EXIT_FAILURE);
	}

	return EXIT_SUCCESS;
}
Example #17
0
static int saveTree(augeas* augeasHandle, KeySet* ks, const char* lensPath,
		Key *parentKey)
{
	int ret = 0;

	size_t prefixSize = keyGetNameSize (parentKey) - 1;
	size_t arraySize = ksGetSize (ks);
	Key **keyArray = calloc (ksGetSize(ks), sizeof (Key *));
	ret = elektraKsToMemArray (ks, keyArray);

	if (ret < 0)
	{
		free (keyArray);
		return -1;
	}

	qsort (keyArray, arraySize, sizeof(Key *), keyCmpOrderWrapper);

	/* convert the Elektra KeySet to an Augeas tree */
	for (size_t i = 0; i < arraySize; i++)
	{
		Key *key = keyArray[i];
		char *nodeName;
		asprintf (&nodeName, AUGEAS_TREE_ROOT "%s",
				(keyName (key) + prefixSize));
		aug_set (augeasHandle, nodeName, keyString (key));
		free (nodeName);
	}

	free (keyArray);

	/* remove keys not present in the KeySet */
	struct OrphanSearch *data = malloc (sizeof(struct OrphanSearch));

	if (!data) return -1;

	data->ks = ks;
	data->parentKey = parentKey;

	foreachAugeasNode (augeasHandle, AUGEAS_TREE_ROOT, &removeOrphan, data);

	free (data);

	/* build the tree */
	ret = aug_text_retrieve (augeasHandle, lensPath, AUGEAS_CONTENT_ROOT,
	AUGEAS_TREE_ROOT, AUGEAS_OUTPUT_ROOT);

	return ret;
}
Example #18
0
/*
 * call-seq:
 *   set(PATH, VALUE) -> boolean
 *
 * Set the value associated with PATH to VALUE. VALUE is copied into the
 * internal data structure. Intermediate entries are created if they don't
 * exist.
 */
VALUE augeas_set(VALUE s, VALUE path, VALUE value) {
    augeas *aug = aug_handle(s);
    const char *cpath = StringValueCStr(path) ;
    const char *cvalue = StringValueCStrOrNull(value) ;

    int callValue = aug_set(aug, cpath, cvalue) ;
    VALUE returnValue ;

    if (callValue == 0)
        returnValue = Qtrue ;
    else
        returnValue = Qfalse ;

    return returnValue ;
}
Example #19
0
static void testSaveNewFile(CuTest *tc) {
    int r;

    r = aug_match(aug, "/augeas/files/etc/yum.repos.d/new.repo/path", NULL);
    CuAssertIntEquals(tc, 0, r);

    r = aug_set(aug, "/files/etc/yum.repos.d/new.repo/newrepo/baseurl",
                "http://foo.com/");
    CuAssertIntEquals(tc, 0, r);

    r = aug_save(aug);
    CuAssertIntEquals(tc, 0, r);

    r = aug_match(aug, "/augeas/files/etc/yum.repos.d/new.repo/path", NULL);
    CuAssertIntEquals(tc, 1, r);
}
Example #20
0
int dns_mod_nameserver(const char* address, int index, char** msg)
{
	char *path = NULL;

	assert(address);
	assert(index >= 1);

	asprintf(&path, "/files/%s/nameserver[%d]", AUGEAS_DNS_CONF, index);
	if (aug_set(sysaugeas, path, address) == -1) {
		asprintf(msg, "Changing DNS server failed (%s)", aug_error_message(sysaugeas));
		free(path);
		return (EXIT_FAILURE);
	}
	free(path);

	return EXIT_SUCCESS;
}
Example #21
0
int
do_aug_clear (const char *path)
{
#ifdef HAVE_AUGEAS
  int r;

  NEED_AUG (-1);

  r = aug_set (aug, path, NULL);
  if (r == -1) {
    reply_with_error ("Augeas clear failed");
    return -1;
  }

  return 0;
#else
  NOT_AVAILABLE (-1);
#endif
}
Example #22
0
static void testMtime(CuTest *tc) {
    const char *s, *mtime2;
    char *mtime1;
    int r;

    r = aug_set(aug, "/files/etc/hosts/1/alias[last() + 1]", "new");
    CuAssertIntEquals(tc, 0, r);

    r = aug_get(aug, "/augeas/files/etc/hosts/mtime", &s);
    CuAssertIntEquals(tc, 1, r);
    mtime1 = strdup(s);
    CuAssertPtrNotNull(tc, mtime1);


    r = aug_save(aug);
    CuAssertIntEquals(tc, 0, r);

    r = aug_get(aug, "/augeas/files/etc/hosts/mtime", &mtime2);
    CuAssertIntEquals(tc, 1, r);

    CuAssertStrNotEqual(tc, mtime1, mtime2);
    CuAssertStrNotEqual(tc, "0", mtime2);
}
Example #23
0
static void testGet(CuTest *tc) {
    int r;
    const char *value;
    struct augeas *aug;

    aug = aug_init(root, loadpath, AUG_NO_STDINC|AUG_NO_LOAD);
    CuAssertPtrNotNull(tc, aug);
    CuAssertIntEquals(tc, AUG_NOERROR, aug_error(aug));

    /* Make sure we're looking at the right thing */
    r = aug_match(aug, "/augeas/version/save/*", NULL);
    CuAssertTrue(tc, r > 1);
    CuAssertIntEquals(tc, AUG_NOERROR, aug_error(aug));

    /* aug_get returns 1 and the value if exactly one node matches */
    r = aug_get(aug, "/augeas/version/save/*[1]", &value);
    CuAssertIntEquals(tc, 1, r);
    CuAssertPtrNotNull(tc, value);
    CuAssertIntEquals(tc, AUG_NOERROR, aug_error(aug));

    /* aug_get returns 0 and no value when no node matches */
    r = aug_get(aug, "/augeas/version/save/*[ last() + 1 ]", &value);
    CuAssertIntEquals(tc, 0, r);
    CuAssertPtrEquals(tc, NULL, value);
    CuAssertIntEquals(tc, AUG_NOERROR, aug_error(aug));

    /* aug_get should return an error when multiple nodes match */
    r = aug_get(aug, "/augeas/version/save/*", &value);
    CuAssertIntEquals(tc, -1, r);
    CuAssertPtrEquals(tc, NULL, value);
    CuAssertIntEquals(tc, AUG_EMMATCH, aug_error(aug));

    /* augeas should prepend context if relative path given */
    r = aug_set(aug, "/augeas/context", "/augeas/version");
    r = aug_get(aug, "save/*[1]", &value);
    CuAssertIntEquals(tc, 1, r);
    CuAssertPtrNotNull(tc, value);
    CuAssertIntEquals(tc, AUG_NOERROR, aug_error(aug));

    /* augeas should still work with an empty context */
    r = aug_set(aug, "/augeas/context", "");
    r = aug_get(aug, "/augeas/version", &value);
    CuAssertIntEquals(tc, 1, r);
    CuAssertPtrNotNull(tc, value);
    CuAssertIntEquals(tc, AUG_NOERROR, aug_error(aug));

    /* augeas should ignore trailing slashes in context */
    r = aug_set(aug, "/augeas/context", "/augeas/version/");
    r = aug_get(aug, "save/*[1]", &value);
    CuAssertIntEquals(tc, 1, r);
    CuAssertPtrNotNull(tc, value);
    CuAssertIntEquals(tc, AUG_NOERROR, aug_error(aug));

    /* augeas should create non-existent context path */
    r = aug_set(aug, "/augeas/context", "/context/foo");
    r = aug_set(aug, "bar", "value");
    r = aug_get(aug, "/context/foo/bar", &value);
    CuAssertIntEquals(tc, 1, r);
    CuAssertPtrNotNull(tc, value);
    CuAssertIntEquals(tc, AUG_NOERROR, aug_error(aug));

    aug_close(aug);
}
Example #24
0
static gchar * set_network_interfaces (RequestData *request_data)
{
        gchar *interfaces, *result, *name, *path, *value;
        augeas *aug;
        JSON_Value *val;
        JSON_Array *array;
        JSON_Object *obj;
        gint if_count, i, ret;

        aug = aug_init (NULL, NULL, AUG_NONE | AUG_NO_ERR_CLOSE | AUG_NO_MODL_AUTOLOAD);
        aug_set (aug, "/augeas/load/Interfaces/lens", "Interfaces.lns");
        aug_set (aug, "/augeas/load/Interfaces/incl", "/etc/network/interfaces");
        aug_load (aug);
        interfaces = request_data->raw_request + request_data->header_size;
        val = json_parse_string (interfaces);
        if (val == NULL) {
                GST_ERROR ("parse json type interfaces failure");
                result = g_strdup_printf ("{\n    \"result\": \"failure\",\n    \"reason\": \"invalid data\"\n}");
                aug_close (aug);
                return result;
        }
        array = json_value_get_array (val);
        if (array == NULL) {
                GST_ERROR ("get interfaces array failure");
                result = g_strdup_printf ("{\n    \"result\": \"failure\",\n    \"reason\": \"not array type interfaces\"\n}");
                aug_close (aug);
                json_value_free (val);
                return result;
        }
        if_count = json_array_get_count (array);
        for (i = 0; i < if_count; i++) {
                obj = json_array_get_object (array, i);
                name = (gchar *)json_object_get_string (obj, "name");
                value = (gchar *)json_object_get_string (obj, "method");
                if (value != NULL) {
                        path = g_strdup_printf ("//files/etc/network/interfaces/iface[.='%s']/method", name);
                        aug_set (aug, path, value);
                        g_free (path);
                }
                value = (gchar *)json_object_get_string (obj, "address");
                if (value != NULL) {
                        path = g_strdup_printf ("//files/etc/network/interfaces/iface[.='%s']/address", name);
                        ret = aug_set (aug, path, value);
                        g_free (path);
                }
                value = (gchar *)json_object_get_string (obj, "netmask");
                if (value != NULL) {
                        path = g_strdup_printf ("//files/etc/network/interfaces/iface[.='%s']/netmask", name);
                        aug_set (aug, path, value);
                        g_free (path);
                }
                value = (gchar *)json_object_get_string (obj, "network");
                if (value != NULL) {
                        path = g_strdup_printf ("//files/etc/network/interfaces/iface[.='%s']/network", name);
                        aug_set (aug, path, value);
                        g_free (path);
                }
                value = (gchar *)json_object_get_string (obj, "broadcast");
                if (value != NULL) {
                        path = g_strdup_printf ("//files/etc/network/interfaces/iface[.='%s']/broadcast", name);
                        aug_set (aug, path, value);
                        g_free (path);
                }
                value = (gchar *)json_object_get_string (obj, "gateway");
                if (value != NULL) {
                        path = g_strdup_printf ("//files/etc/network/interfaces/iface[.='%s']/gateway", name);
                        aug_set (aug, path, value);
                        g_free (path);
                }
        }
        if (aug_save (aug) == -1) {
                aug_get (aug, "/augeas//error", (const gchar **)&value);
                GST_ERROR ("set /etc/network/interface failure: %s", value);
                result = g_strdup_printf ("{\n    \"result\": \"failure\",\n    \"reason\": \"%s\"\n}", value);

        } else {
                result = g_strdup ("{\n    \"result\": \"success\"\n}");
        }
        json_value_free (val);
        aug_close (aug);

        return result;
}