/* * Print out information for all nodes matching PATH using aug_match and * then aug_get etc. on each of the matches. */ static void dump_match(struct augeas *aug, const char *path) { char **matches; int nmatches; int i; nmatches = aug_match(aug, path, &matches); if (nmatches < 0) { fprintf(stderr, "aug_match for '%s' failed\n", path); fprintf(stderr, "error: %s\n", aug_error_message(aug)); exit(1); } fprintf(stderr, "iterating matches\n"); fprintf(stderr, "%d matches for %s\n", nmatches, path); for (i=0; i < nmatches; i++) { const char *value, *label; char *file; aug_get(aug, matches[i], &value); aug_label(aug, matches[i], &label); aug_source(aug, matches[i], &file); printf("%s: %s %s %s\n", matches[i], label, value, file); free(file); free(matches[i]); } free(matches); }
/* Check that defining a variable with defnode leads to a corresponding * entry in /augeas/variables and that that entry disappears when the * variable is undefined */ static void testDefNodeCreateMeta(CuTest *tc) { int r, created; struct augeas *aug; static const char *const expr = "/augeas/version/save/mode[last()+1]"; static const char *const expr_can = "/augeas/version/save/mode[5]"; const char *value; aug = aug_init(root, loadpath, AUG_NO_STDINC|AUG_NO_LOAD); CuAssertPtrNotNull(tc, aug); CuAssertIntEquals(tc, AUG_NOERROR, aug_error(aug)); r = aug_defnode(aug, "var", expr, "other", &created); CuAssertIntEquals(tc, 1, r); CuAssertIntEquals(tc, 1, created); r = aug_match(aug, "/augeas/variables/*", NULL); CuAssertIntEquals(tc, 1, r); r = aug_get(aug, "/augeas/variables/var", &value); CuAssertStrEquals(tc, expr_can, value); r = aug_defvar(aug, "var", NULL); CuAssertIntEquals(tc, 0, r); r = aug_match(aug, "/augeas/variables/*", NULL); CuAssertIntEquals(tc, 0, r); aug_close(aug); }
/* * call-seq: * exists(PATH) -> boolean * * Return true if there is an entry for this path, false otherwise */ VALUE augeas_exists(VALUE s, VALUE path) { augeas *aug = aug_handle(s); const char *cpath = StringValueCStr(path); int ret = aug_get(aug, cpath, NULL); return (ret == 1) ? Qtrue : Qfalse; }
int dns_rm_search_domain(const char* domain, char** msg) { int i, ret; const char* path = "/files/"AUGEAS_DNS_CONF"/search/domain"; char** matches; const char* value; assert(domain); if ((ret = aug_match(sysaugeas, path, &matches)) == -1) { asprintf(msg, "Augeas match for \"%s\" failed: %s", path, aug_error_message(sysaugeas)); return EXIT_FAILURE; } for (i = 0; i < ret; ++i) { aug_get(sysaugeas, matches[i], &value); if (strcmp(value, domain) == 0) { if (ret == 1) { /* Last search domain, delete the whole search node */ *strrchr(matches[0], '/') = '\0'; } aug_rm(sysaugeas, matches[i]); break; } } /* cleanup */ for (i = 0; i < ret; ++i) { free(matches[i]); } free(matches); return EXIT_SUCCESS; }
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); }
/* Check that defining a variable leads to a corresponding entry in * /augeas/variables and that that entry disappears when the variable is * undefined */ static void testDefVarMeta(CuTest *tc) { int r; struct augeas *aug; static const char *const expr = "/augeas/version/save/mode"; const char *value; aug = aug_init(root, loadpath, AUG_NO_STDINC|AUG_NO_LOAD); CuAssertPtrNotNull(tc, aug); CuAssertIntEquals(tc, AUG_NOERROR, aug_error(aug)); r = aug_defvar(aug, "var", expr); CuAssertIntEquals(tc, 4, r); r = aug_match(aug, "/augeas/variables/*", NULL); CuAssertIntEquals(tc, 1, r); r = aug_get(aug, "/augeas/variables/var", &value); CuAssertStrEquals(tc, expr, value); r = aug_defvar(aug, "var", NULL); CuAssertIntEquals(tc, 0, r); r = aug_match(aug, "/augeas/variables/*", NULL); CuAssertIntEquals(tc, 0, r); aug_close(aug); }
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 void get_login_defs(void) { const char *value; char *endptr; static char method[10] = {'\0','\0','\0','\0','\0','\0','\0','\0','\0','\0'}; if (aug_get(sysaugeas, "/files/"AUGEAS_LOGIN_CONF"/SHA_CRYPT_MIN_ROUNDS", &value) == 1) { sha_crypt_min_rounds = strtol(value, &endptr, 10); if (*endptr != '\0') { /* some characters after number */ nc_verb_warning("SHA_CRYPT_MIN_ROUNDS in %s contains invalid value (%s).", AUGEAS_LOGIN_CONF, value); sha_crypt_min_rounds = -1; } } else { /* default value */ sha_crypt_min_rounds = -1; } if (aug_get(sysaugeas, "/files/etc/"AUGEAS_LOGIN_CONF"/SHA_CRYPT_MAX_ROUNDS", &value) == 1) { sha_crypt_max_rounds = strtol(value, &endptr, 10); if (*endptr != '\0') { /* some characters after number */ nc_verb_warning("SHA_CRYPT_MAX_ROUNDS in %s contains invalid value (%s).", AUGEAS_LOGIN_CONF, value); sha_crypt_max_rounds = -1; } } else { /* default value */ sha_crypt_max_rounds = -1; } if (aug_get(sysaugeas, "/files/etc/"AUGEAS_LOGIN_CONF"/ENCRYPT_METHOD", &value) == 1) { strncpy(method, value, 9); encrypt_method = method; } else { encrypt_method = NULL; } if (aug_get(sysaugeas, "/files/etc/"AUGEAS_LOGIN_CONF"/MD5_CRYPT_ENAB", &value) == 1) { md5_crypt_enab = (strcasecmp(value, "yes") == 0) ? 1 : 0; } else { /* default value */ md5_crypt_enab = 0; } }
int ntp_rm_server(const char* udp_address, const char* association_type, bool iburst, bool prefer, char** msg) { int ret, i, j; char* path; const char* value; char** matches = NULL; assert(udp_address); assert(association_type); path = NULL; asprintf(&path, "/files/%s/%s", AUGEAS_NTP_CONF, association_type); ret = aug_match(sysaugeas, path, &matches); if (ret == -1) { asprintf(msg, "Augeas match for \"%s\" failed: %s", path, aug_error_message(sysaugeas)); free(path); return EXIT_FAILURE; } free(path); for (i = 0; i < ret; ++i) { aug_get(sysaugeas, matches[i], &value); if (value == NULL || strcmp(value, udp_address) != 0) { continue; } path = NULL; asprintf(&path, "/files/%s/%s[%d]/iburst", AUGEAS_NTP_CONF, association_type, i + 1); j = aug_match(sysaugeas, path, NULL); free(path); if ((iburst && j != 1) || (!iburst && j != 0)) { continue; } path = NULL; asprintf(&path, "/files/%s/%s[%d]/prefer", AUGEAS_NTP_CONF, association_type, i + 1); j = aug_match(sysaugeas, path, NULL); free(path); if ((prefer && j != 1) || (!prefer && j != 0)) { continue; } /* remove item and finish */ aug_rm(sysaugeas, matches[i]); break; } /* cleanup */ for (i = 0; i < ret; ++i) { free(matches[i]); } free(matches); return EXIT_SUCCESS; }
static const char * getAugeasError (augeas * augeasHandle) { const char * reason = 0; if (aug_error (augeasHandle) != 0) { reason = aug_error_message (augeasHandle); } else { const char * augeasError; aug_get (augeasHandle, "/augeas/text" AUGEAS_TREE_ROOT "/error", &augeasError); if (augeasError) { const char * lens; const char * line; const char * character; const char * message; aug_get (augeasHandle, "/augeas/text" AUGEAS_TREE_ROOT "/error/lens", &lens); aug_get (augeasHandle, "/augeas/text" AUGEAS_TREE_ROOT "/error/line", &line); aug_get (augeasHandle, "/augeas/text" AUGEAS_TREE_ROOT "/error/char", &character); aug_get (augeasHandle, "/augeas/text" AUGEAS_TREE_ROOT "/error/message", &message); const char * format = "%s\n\tposition: %s:%s\n\tmessage: %s\n\tlens: %s"; size_t messageSize = strlen (lens) + strlen (line) + strlen (character) + strlen (message) + strlen (format); char * buffer = elektraMalloc (messageSize); sprintf (buffer, format, augeasError, line, character, message); reason = buffer; } else { reason = "No specific reason was reported"; } } /* should not happen, but avoid 0 return */ if (!reason) { reason = ""; } return reason; }
static FILE* open_authfile(const char *username, const char *opentype, char **path, char **msg) { struct passwd *pwd; char *filepath = NULL; FILE *file; mode_t mask; int flag; const char *akf = NULL; /* get AuthorizedKeysFile value from sshd_config */ asprintf(&filepath, "/files/%s/AuthorizedKeysFile", NETOPEER_SSHD_CONF); aug_get(sysaugeas, filepath, &akf); free(filepath); filepath = NULL; if (akf == NULL) { *msg = strdup("SSH server doesn't support Authorized Keys files."); return(NULL); } /* get user home */ pwd = getpwnam(username); if (pwd == NULL) { asprintf(msg, "Unable to get user record (%s)", strerror(errno)); return (NULL); } if (pwd->pw_dir == NULL) { asprintf(msg, "Home directory of user \"%s\" not set, unable to set authorized keys.", username); return(NULL); } asprintf(&filepath, "%s/%s", pwd->pw_dir, akf); /* open authorized_keys file in the user's ssh home directory */ flag = access(filepath, F_OK); mask = umask(0600); if ((file = fopen(filepath, opentype)) == NULL) { umask(mask); asprintf(msg, "Opening authorized keys file \"%s\" failed (%s).", filepath, strerror(errno)); free(filepath); return (NULL); } umask(mask); if (flag != 0) { /* change owner of the created file */ chown(filepath, pwd->pw_uid, pwd->pw_gid); } if (path != NULL) { *path = filepath; } else { free(filepath); } return (file); }
/* * call-seq: * get(PATH) -> String * * Lookup the value associated with PATH */ VALUE augeas_get(VALUE s, VALUE path) { augeas *aug = aug_handle(s); const char *cpath = StringValueCStr(path); const char *value; aug_get(aug, cpath, &value); if (value != NULL) { return rb_str_new(value, strlen(value)) ; } else { return Qnil; } }
qpid::types::Variant::Map getConfigTree() { qpid::types::Variant::Map tree; if (augeas==NULL) augeas_init(); if (augeas == NULL) { AGO_ERROR() << "cannot initialize augeas"; return tree; } char **matches; std::stringstream path; path << "/files"; path << getConfigPath(MODULE_CONFDIR).string(); path << "/"; std::string prefix = path.str(); path << "/*"; int num = aug_match(augeas, path.str().c_str(), &matches); for (int i=0; i < num; i++) { const char *val; aug_get(augeas, matches[i], &val); if (val != NULL) { std::vector<std::string> elements; std::string match = matches[i]; AGO_TRACE() << "getConfigTree:augeas match result[" << i << "]:" << match << ": " << val; replaceString(match, prefix, ""); replaceString(match, ".conf", ""); elements = split(match, '/'); if (elements.size() != 3) { AGO_ERROR() << "augeas match ignored: does not split by / in three parts: " << match; continue; } std::string file = elements[0]; std::string section = elements[1]; std::string option = elements[2]; AGO_TRACE() << "File: " << file << " Section: " << section << " Option: " << option; qpid::types::Variant::Map fileMap; qpid::types::Variant::Map sectionMap; if (!(tree[file].isVoid())) { fileMap = tree[file].asMap(); } if (!(fileMap[section].isVoid())) { sectionMap = fileMap[section].asMap(); } sectionMap[option] = val; fileMap[section] = sectionMap; tree[file] = fileMap; } free((void *) matches[i]); } free(matches); return tree; }
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); }
/* * call-seq: * get(PATH) -> String * * Lookup the value associated with PATH */ VALUE augeas_get(VALUE s, VALUE path) { augeas *aug = aug_handle(s); const char *cpath = StringValueCStr(path); const char *value = NULL; int r = aug_get(aug, cpath, &value); /* There used to be a bug in Augeas that would make it not properly set * VALUE to NULL when PATH was invalid. We check RETVAL, too, to avoid * running into that */ if (r == 1 && value != NULL) { return rb_str_new(value, strlen(value)) ; } else { return Qnil; } }
static int Paug_get(lua_State *L) { augeas *a; const char *path; const char *value = NULL; int r; a = Paug_checkarg(L, 1); path = luaL_checkstring(L, 2); r = aug_get(a, path, &value); if (r < 0) return pusherror(L, a, path); lua_pushstring(L, value); return 1; }
static int saveFile(augeas* augeasHandle, FILE* fh) { /* retrieve the file content */ int ret = 0; const char* value = 0; aug_get (augeasHandle, AUGEAS_OUTPUT_ROOT, &value); /* write the file */ if (value) { ret = fwrite (value, sizeof(char), strlen (value), fh); if (feof (fh) || ferror (fh)) return -1; } return ret; }
static void print_version_info(struct augeas *aug) { const char *version; int r; r = aug_get(aug, "/augeas/version", &version); if (r != 1) goto error; fprintf(stderr, "augparse %s <http://augeas.net/>\n", version); fprintf(stderr, "Copyright (C) 2007-2015 David Lutterkort\n"); fprintf(stderr, "License LGPLv2+: GNU LGPL version 2.1 or later\n"); fprintf(stderr, " <http://www.gnu.org/licenses/lgpl-2.1.html>\n"); fprintf(stderr, "This is free software: you are free to change and redistribute it.\n"); fprintf(stderr, "There is NO WARRANTY, to the extent permitted by law.\n\n"); fprintf(stderr, "Written by David Lutterkort\n"); return; error: fprintf(stderr, "Something went terribly wrong internally - please file a bug\n"); }
static int convertToKey(augeas *handle, const char *treePath, void *data) { struct KeyConversion *conversionData = (struct KeyConversion *) data; int result = 0; const char *value = 0; result = aug_get (handle, treePath, &value); if (result < 0) return result; Key *key = createKeyFromPath (conversionData->parentKey, treePath); /* fill key values */ keySetString (key, value); conversionData->currentOrder++; keySetOrderMeta (key, conversionData->currentOrder); result = ksAppendKey (conversionData->ks, key); return result; }
static const char *getAugeasError(augeas* augeasHandle) { const char* message = 0; if (aug_error (augeasHandle) != 0) { message = aug_error_message (augeasHandle); } else { aug_get (augeasHandle, "/augeas/text"AUGEAS_TREE_ROOT"/error/message", &message); if (!message) message = "No specific reason was reported"; } /* should not happen, but avoid 0 return */ if (!message) message = ""; return message; }
char * do_aug_get (const char *path) { #ifdef HAVE_AUGEAS const char *value = NULL; char *v; int r; NEED_AUG (NULL); r = aug_get (aug, path, &value); if (r == 0) { reply_with_error ("no matching node"); return NULL; } if (r != 1) { reply_with_error ("Augeas get failed"); return NULL; } /* value can still be NULL here, eg. try with path == "/augeas". * I don't understand this case, and it seems to contradict the * documentation. */ if (value == NULL) { reply_with_error ("Augeas returned NULL match"); return NULL; } /* The value is an internal Augeas string, so we must copy it. GC FTW. */ v = strdup (value); if (v == NULL) { reply_with_perror ("strdup"); return NULL; } return v; /* Caller frees. */ #else NOT_AVAILABLE (NULL); #endif }
static errno_t sss_config_get_list(TALLOC_CTX *mem_ctx, struct sss_config_ctx *ctx, const char *section, const char *option, char ***_list) { TALLOC_CTX *tmp_ctx = NULL; char *option_path = NULL; const char *value = NULL; char **list = NULL; errno_t ret; int aug_ret; tmp_ctx = talloc_new(NULL); if (tmp_ctx == NULL) { return ENOMEM; } option_path = build_option_path(tmp_ctx, ctx, section, option); if (option_path == NULL) { ret = ENOMEM; goto done; } aug_ret = aug_get(ctx->auges_ctx, option_path, &value); if (aug_ret == 0 || (aug_ret == 1 && (value == NULL || *value == '\0'))) { /* option is not present, return empty list */ list = talloc_zero_array(tmp_ctx, char*, 1); if (list == NULL) { ret = ENOMEM; goto done; } ret = EOK; goto done; } else if (aug_ret != 1) {
std::string getConfigSectionOption(const ConfigNameList §ion, const char *option, const char *defaultValue, const ConfigNameList &app) { if (augeas==NULL){ if(!augeas_init()) { return defaultValue; } } // If app has no values, default to section ConfigNameList app_(app, section); AGO_TRACE() << "Augeas: get " << app_ << " / " << section; BOOST_FOREACH(const std::string & a, app_.names()) { BOOST_FOREACH(const std::string & s, section.names()) { const char *value; std::string aug_path = augeasPath(confPathFromApp(a), s, option); int ret = aug_get(augeas, aug_path.c_str(), &value); if(ret == 1 && value != NULL) { std::stringstream result; AGO_TRACE() << "Augeas: using config value from " << aug_path << ": " << value; result << value; return result.str(); } AGO_TRACE() << "Augeas: config value at " << aug_path << " not found (ret=" << ret << ")"; } } if(defaultValue) { AGO_TRACE() << "Augeas, no match on " << section << " / " << app << ", falling back to default value " << defaultValue; return defaultValue; } // empty return std::string(); }
/** * Creates a new Elektra key from the specified Augeas key. * If any step during key conversion fails, an error is returned * in order to prevent inconsistent keys. */ static int convertToKey (augeas * handle, const char * treePath, void * data) { struct KeyConversion * conversionData = (struct KeyConversion *)data; int result = 0; const char * value = 0; result = aug_get (handle, treePath, &value); /* we were unable to retrieve the augeas value */ if (result < 0) return result; Key * key = createKeyFromPath (conversionData->parentKey, treePath); /* fill key values */ keySetString (key, value); conversionData->currentOrder++; result = keySetOrderMeta (key, conversionData->currentOrder); /* setting the correct key order failed */ if (result < 0) return result; result = ksAppendKey (conversionData->ks, key); return result; }
xmlNodePtr dns_getconfig(xmlNsPtr ns, char** msg) { int i, done; char* path, *content = NULL; const char* value; xmlNodePtr dns_node, server, aux_node; assert(sysaugeas); /* dns-resolver */ dns_node = xmlNewNode(ns, BAD_CAST "dns-resolver"); /* dns-resolver/search[] */ for (i = 1, done = 0; !done; i++) { path = NULL; asprintf(&path, "/files/"AUGEAS_DNS_CONF"/search/domain[%d]", i); switch (aug_match(sysaugeas, path, NULL)) { case -1: asprintf(msg, "Augeas match for \"%s\" failed: %s", path, aug_error_message(sysaugeas)); free(path); xmlFreeNode(dns_node); return (NULL); case 0: /* index out of bounds, continue with next server type */ free(path); done = 1; break; default: /* 1 */ /* dns-resolver/search */ aug_get(sysaugeas, path, &value); xmlNewChild(dns_node, dns_node->ns, BAD_CAST "search", BAD_CAST value); free(path); path = NULL; break; } } /* dns-resolver/server[] */ for (i = 1, done = 0; !done; i++) { path = NULL; asprintf(&path, "/files/"AUGEAS_DNS_CONF"/nameserver[%d]", i); switch (aug_match(sysaugeas, path, NULL)) { case -1: asprintf(msg, "Augeas match for \"%s\" failed: %s", path, aug_error_message(sysaugeas)); free(path); xmlFreeNode(dns_node); return (NULL); case 0: /* index out of bounds, continue with next server type */ free(path); done = 1; break; default: /* 1 */ /* dns-resolver/server */ server = xmlNewChild(dns_node, dns_node->ns, BAD_CAST "server", NULL); /* dns-resolver/server/name */ asprintf(&content, "nameserver-%d", i); xmlNewChild(server, server->ns, BAD_CAST "name", BAD_CAST content); free(content); /* dns-resolver/server/udp-and-tcp/address */ aug_get(sysaugeas, path, &value); aux_node = xmlNewChild(server, server->ns, BAD_CAST "udp-and-tcp", NULL); xmlNewChild(aux_node, aux_node->ns, BAD_CAST "address", BAD_CAST value); free(path); /* port specification is not supported by Linux dns resolver implementation */ break; } } /* dns-resolver/options */ switch (aug_match(sysaugeas, "/files/"AUGEAS_DNS_CONF"/options", NULL)) { case -1: asprintf(msg, "Augeas match for \"%s\" failed: %s", path, aug_error_message(sysaugeas)); free(path); xmlFreeNode(dns_node); return (NULL); case 0: /* No options specified */ break; default: /* 1 */ aux_node = xmlNewChild(dns_node, dns_node->ns, BAD_CAST "options", NULL); /* dns-resolver/options/timeout */ value = NULL; aug_get(sysaugeas, "/files/"AUGEAS_DNS_CONF"/options/timeout", &value); if (value != NULL) { xmlNewChild(aux_node, aux_node->ns, BAD_CAST "timeout", BAD_CAST value); } /* dns-resolver/options/attempts */ value = NULL; aug_get(sysaugeas, "/files/"AUGEAS_DNS_CONF"/options/attempts", &value); if (value != NULL) { xmlNewChild(aux_node, aux_node->ns, BAD_CAST "attempts", BAD_CAST value); } break; } return (dns_node); }
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; }
xmlNodePtr users_getxml(xmlNsPtr ns, char** msg) { xmlNodePtr auth_node, user, aux_node; struct passwd *pwd; struct spwd *spwd; const char* value; char *path = NULL; if (!ncds_feature_isenabled("ietf-system", "local-users")) { return (NULL); } /* authentication */ auth_node = xmlNewNode(ns, BAD_CAST "authentication"); /* authentication/user-authentication-order */ asprintf(&path, "/files/%s/PasswordAuthentication", NETOPEER_SSHD_CONF); aug_get(sysaugeas, path, &value); free(path); if (value != NULL && strcmp(value, "yes") == 0) { xmlNewChild(auth_node, auth_node->ns, BAD_CAST "user-authentication-order", BAD_CAST "local-users"); } /* authentication/user[] */ if (lckpwdf() != 0) { *msg = strdup("Failed to acquire shadow file lock."); xmlFreeNode(auth_node); return (NULL); } setpwent(); while ((pwd = getpwent()) != NULL) { /* authentication/user */ user = xmlNewChild(auth_node, auth_node->ns, BAD_CAST "user", NULL); /* authentication/user/name */ xmlNewChild(user, user->ns, BAD_CAST "name", BAD_CAST pwd->pw_name); /* authentication/user/passwd */ if (pwd->pw_passwd[0] == 'x') { /* get data from /etc/shadow */ setspent(); spwd = getspnam(pwd->pw_name); if (spwd != NULL && /* no record, wtf?!? */ spwd->sp_pwdp[0] != '!' && /* account not initiated or locked */ spwd->sp_pwdp[0] != '*') { /* login disabled */ xmlNewChild(user, user->ns, BAD_CAST "password", BAD_CAST spwd->sp_pwdp); } } else if (pwd->pw_passwd[0] != '*') { /* password is stored in /etc/passwd or refers to something else (e.g., NIS server) */ xmlNewChild(user, user->ns, BAD_CAST "password", BAD_CAST pwd->pw_passwd); } /* else password is disabled */ /* authentication/user/authorized-key[] */ if ((aux_node = authkey_getxml(pwd->pw_name, user->ns, msg)) != NULL) { xmlAddChildList(user, aux_node); } else { /* ignore failures in this case */ free(*msg); *msg = NULL; } } endspent(); endpwent(); ulckpwdf(); return (auth_node); }
static char *readline_path_generator(const char *text, int state) { static int current = 0; static char **children = NULL; static int nchildren = 0; static char *ctx = NULL; char *end = strrchr(text, SEP); if (end != NULL) end += 1; if (state == 0) { char *path; if (end == NULL) { if ((path = strdup("*")) == NULL) return NULL; } else { CALLOC(path, end - text + 2); if (path == NULL) return NULL; strncpy(path, text, end - text); strcat(path, "*"); } for (;current < nchildren; current++) free((void *) children[current]); free((void *) children); nchildren = aug_match(aug, path, &children); current = 0; ctx = NULL; if (path[0] != SEP) aug_get(aug, AUGEAS_CONTEXT, (const char **) &ctx); free(path); } if (end == NULL) end = (char *) text; while (current < nchildren) { char *child = children[current]; current += 1; char *chend = strrchr(child, SEP) + 1; if (STREQLEN(chend, end, strlen(end))) { if (child_count(child) > 0) { char *c = realloc(child, strlen(child)+2); if (c == NULL) return NULL; child = c; strcat(child, "/"); } /* strip off context if the user didn't give it */ if (ctx != NULL) { char *c = realloc(child, strlen(child)-strlen(ctx)+1); if (c == NULL) return NULL; int ctxidx = strlen(ctx); if (child[ctxidx] == SEP) ctxidx++; strcpy(c, &child[ctxidx]); child = c; } rl_filename_completion_desired = 1; rl_completion_append_character = '\0'; return child; } else { free(child); } } return NULL; }
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); }
xmlNodePtr ntp_getconfig(xmlNsPtr ns, char** errmsg) { int i, j; const char* type[2] = {"server", "peer"}; const char* value; char* path, *content = NULL; xmlNodePtr ntp_node, server, aux_node; assert(sysaugeas); /* ntp */ ntp_node = xmlNewNode(ns, BAD_CAST "ntp"); /* ntp/enabled */ xmlNewChild(ntp_node, ntp_node->ns, BAD_CAST "enabled", (ntp_status() == 1) ? BAD_CAST "true" : BAD_CAST "false"); /* ntp/server[] */ j = 0; loop: for (i = 1; j < 2; i++) { path = NULL; asprintf(&path, "/files/"AUGEAS_NTP_CONF"/%s[%d]", type[j], i); switch(aug_match(sysaugeas, path, NULL)) { case -1: asprintf(errmsg, "Augeas match for \"%s\" failed: %s", path, aug_error_message(sysaugeas)); free(path); xmlFreeNode(ntp_node); return (NULL); case 0: /* index out of bounds, continue with next server type */ free(path); j++; goto loop; default: /* 1 */ /* ntp/server/ */ server = xmlNewChild(ntp_node, ntp_node->ns, BAD_CAST "server", NULL); /* ntp/server/name */ asprintf(&content, "%s-%d", type[j], i); xmlNewChild(server, server->ns, BAD_CAST "name", BAD_CAST content); free(content); /* ntp/server/udp/address */ aug_get(sysaugeas, path, &value); aux_node = xmlNewChild(server, server->ns, BAD_CAST "udp", NULL); xmlNewChild(aux_node, aux_node->ns, BAD_CAST "address", BAD_CAST value); /* port specification is not supported by Linux ntp implementation */ free(path); /* ntp/server/association-type */ xmlNewChild(server, server->ns, BAD_CAST "association-type", BAD_CAST type[j]); /* ntp/server/iburst */ path = NULL; asprintf(&path, "/files/"AUGEAS_NTP_CONF"/%s[%d]/iburst", type[j], i); switch(aug_match(sysaugeas, path, NULL)) { case -1: asprintf(errmsg, "Augeas match for \"%s\" failed: %s", path, aug_error_message(sysaugeas)); free(path); xmlFreeNode(ntp_node); return (NULL); case 0: /* iburst not set */ xmlNewChild(server, server->ns, BAD_CAST "iburst", BAD_CAST "false"); break; default: /* 1 */ /* iburst set */ xmlNewChild(server, server->ns, BAD_CAST "iburst", BAD_CAST "true"); break; } free(path); /* ntp/server/prefer */ path = NULL; asprintf(&path, "/files/"AUGEAS_NTP_CONF"/%s[%d]/prefer", type[j], i); switch(aug_match(sysaugeas, path, NULL)) { case -1: asprintf(errmsg, "Augeas match for \"%s\" failed: %s", path, aug_error_message(sysaugeas)); free(path); xmlFreeNode(ntp_node); return (NULL); case 0: /* prefer not set */ xmlNewChild(server, server->ns, BAD_CAST "prefer", BAD_CAST "false"); break; default: /* 1 */ /* prefer set */ xmlNewChild(server, server->ns, BAD_CAST "prefer", BAD_CAST "true"); break; } free(path); } } return (ntp_node); }