Exemplo n.º 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);
}
Exemplo n.º 2
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;
}
Exemplo n.º 3
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);
}
Exemplo n.º 4
0
/*
 * call-seq:
 *       save() -> boolean
 *
 * Write all pending changes to disk
 */
VALUE augeas_save(VALUE s) {
    augeas *aug = aug_handle(s);
    int callValue = aug_save(aug) ;
    VALUE returnValue ;

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

    return returnValue ;
}
Exemplo n.º 5
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);
}
Exemplo n.º 6
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);
}
Exemplo n.º 7
0
int
do_aug_save (void)
{
#ifdef HAVE_AUGEAS
  NEED_AUG (-1);

  if (aug_save (aug) == -1) {
    reply_with_error ("Augeas save failed");
    return -1;
  }

  return 0;
#else
  NOT_AVAILABLE (-1);
#endif
}
Exemplo n.º 8
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);
}
Exemplo n.º 9
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);
}
Exemplo n.º 10
0
static int Paug_save(lua_State *L)
{
    augeas *a = Paug_checkarg(L, 1);
    return pushresult(L, aug_save(a), a, NULL);
}
Exemplo n.º 11
0
/*
 * call-seq:
 *       save() -> int
 *
 * Write all pending changes to disk
 */
VALUE facade_save(VALUE s) {
    augeas *aug = aug_handle(s);
    return INT2FIX(aug_save(aug));
}
Exemplo n.º 12
0
/*
 * call-seq:
 *       save() -> boolean
 *
 * Write all pending changes to disk
 */
VALUE augeas_save(VALUE s) {
    augeas *aug = aug_handle(s);

    return (aug_save(aug) == 0) ? Qtrue : Qfalse;
}
Exemplo n.º 13
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;
}