/* 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); }
static void add_transforms(char *ts, size_t tslen) { char *command; int r; char *t = NULL; bool added_transform = false; while ((t = argz_next(ts, tslen, t))) { r = xasprintf(&command, "transform %s", t); if (r < 0) fprintf(stderr, "error: Failed to add transform %s: could not allocate memory\n", t); r = aug_srun(aug, stdout, command); if (r < 0) fprintf(stderr, "error: Failed to add transform %s: %s\n", t, aug_error_message(aug)); free(command); added_transform = true; } if (added_transform) { r = aug_load(aug); if (r < 0) fprintf(stderr, "error: Failed to load with new transforms: %s\n", aug_error_message(aug)); } }
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; }
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); }
/* * call-seq: * load() -> boolean * * Load files from disk according to the transforms under +/augeas/load+ */ VALUE augeas_load(VALUE s) { augeas *aug = aug_handle(s); int callValue = aug_load(aug); VALUE returnValue ; if (callValue == 0) returnValue = Qtrue ; else returnValue = Qfalse ; return returnValue ; }
int do_aug_load (void) { #ifdef HAVE_AUG_LOAD NEED_AUG (-1); if (aug_load (aug) == -1) { reply_with_error ("Augeas load failed"); return -1; } return 0; #else NOT_AVAILABLE (-1); #endif }
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; }
static int Paug_load(lua_State *L) { augeas *a = Paug_checkarg(L, 1); return pushresult(L, aug_load(a), a, NULL); }
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; }