/* Recursively dump the object tree */ static void print_config_tree(confdb_handle_t handle, hdb_handle_t parent_object_handle, int depth) { hdb_handle_t object_handle; char object_name[1024]; size_t object_name_len; char key_name[1024]; size_t key_name_len; char key_value[1024]; size_t key_value_len; int res; int i; /* Show the keys */ res = confdb_key_iter_start(handle, parent_object_handle); if (res != CS_OK) { printf( "error resetting key iterator for object "HDB_X_FORMAT": %d\n", parent_object_handle, res); return; } while ( (res = confdb_key_iter(handle, parent_object_handle, key_name, &key_name_len, key_value, &key_value_len)) == CS_OK) { key_name[key_name_len] = '\0'; key_value[key_value_len] = '\0'; for (i=0; i<depth; i++) printf(" "); printf(" KEY %s=%s\n", key_name, key_value); } /* Show sub-objects */ res = confdb_object_iter_start(handle, parent_object_handle); if (res != CS_OK) { printf( "error resetting object iterator for object "HDB_X_FORMAT": %d\n", parent_object_handle, res); return; } while ( (res = confdb_object_iter(handle, parent_object_handle, &object_handle, object_name, &object_name_len)) == CS_OK) { hdb_handle_t parent; res = confdb_object_parent_get(handle, object_handle, &parent); if (res != CS_OK) { printf( "error getting parent for object "HDB_X_FORMAT": %d\n", object_handle, res); return; } for (i=0; i<depth; i++) printf(" "); object_name[object_name_len] = '\0'; printf("OBJECT: %s ("HDB_X_FORMAT", parent: "HDB_X_FORMAT")\n", object_name, object_handle, parent); /* Down we go ... */ print_config_tree(handle, object_handle, depth+1); } }
/* * return 0 on success * return -1 on errors */ static int path_dive(confdb_handle_t handle, hdb_handle_t *query_handle, char *current_query, int tokens) { char *pos = NULL, *next = NULL; int i; hdb_handle_t new_obj_handle; confdb_value_types_t type; pos = current_query + 1; for (i = 1; i <= tokens; i++) { if (confdb_object_find_start(handle, *query_handle) != CS_OK) goto fail; next = pos + strlen(pos) + 1; if (!strstr(pos, "[")) { /* straight path diving */ if (confdb_object_find (handle, *query_handle, pos, strlen(pos), &new_obj_handle) != CS_OK) goto fail; else { confdb_object_find_destroy(handle, *query_handle); *query_handle = new_obj_handle; } } else { /* * /something[int]/ or /something[@foo="bar"]/ * start and end will identify [] * middle will point to the inside request */ char *start = NULL, *middle = NULL, *end = NULL; char *key_value = NULL; size_t valuelen; char data[PATH_MAX]; size_t datalen = 0; /* * those ones should be always good because * the tokenizer takes care of them */ start = strstr(pos, "["); if (!start) goto fail; end = strstr(pos, "]"); if (!end) goto fail; middle = start + 1; memset(start, 0, 1); memset(end, 0, 1); if (!strcmp(pos, "child::*")) { int val, j; val = atoi(middle); if (val < 1) goto fail; if (confdb_object_iter_start (handle, *query_handle) != CS_OK) goto fail; for (j = 1; j <= val; j++) { if (confdb_object_iter (handle, *query_handle, &new_obj_handle, data, &datalen) != CS_OK) goto fail; } confdb_object_iter_destroy(handle, *query_handle); confdb_object_find_destroy(handle, *query_handle); *query_handle = new_obj_handle; } else if (!strstr(middle, "@")) { /* lookup something with index num = int */ int val, j; val = atoi(middle); if (val < 1) goto fail; for (j = 1; j <= val; j++) { if (confdb_object_find (handle, *query_handle, pos, strlen(pos), &new_obj_handle) != CS_OK) goto fail; } confdb_object_find_destroy(handle, *query_handle); *query_handle = new_obj_handle; } else { /* lookup something with obj foo = bar */ char *equal = NULL, *value = NULL, *tmp = NULL; int goout = 0; equal = strstr(middle, "="); if (!equal) goto fail; memset(equal, 0, 1); value = strstr(equal + 1, "\""); if (!value) goto fail; value = value + 1; tmp = strstr(value, "\""); if (!tmp) goto fail; memset(tmp, 0, 1); middle = strstr(middle, "@") + 1; if (!middle) goto fail; // middle points to foo // value to bar memset(data, 0, PATH_MAX); while (!goout) { if (confdb_object_find (handle, *query_handle, pos, strlen(pos), &new_obj_handle) != CS_OK) goto fail; else { key_value = NULL; if (confdb_key_get_typed2 (handle, new_obj_handle, middle, (void **) &key_value, &valuelen, &type) == CS_OK) { if (!strcmp (key_value, value)) goout = 1; free(key_value); key_value = NULL; } } } free(key_value); key_value = NULL; confdb_object_find_destroy(handle, *query_handle); *query_handle = new_obj_handle; } } pos = next; } return 0; fail: errno = EINVAL; return -1; }
static int get_data(confdb_handle_t handle, hdb_handle_t connection_handle, hdb_handle_t query_handle, hdb_handle_t *list_handle, char **rtn, char *curpos, int list, int is_oldlist) { int cmp; char data[PATH_MAX]; char *resval; char *keyval; hdb_handle_t new_obj_handle; unsigned int value = 0; confdb_value_types_t type; size_t datalen = 0, keyvallen = PATH_MAX; memset(data, 0, PATH_MAX); // we need to handle child::*[int value] in non list mode. cmp = strcmp(curpos, "child::*"); if (cmp >= 0) { char *start = NULL, *end = NULL; // a pure child::* request should come down as list if (!cmp && !list) goto fail; if (confdb_object_iter_start(handle, query_handle) != CS_OK) goto fail; if (!is_oldlist) *list_handle = query_handle; if (cmp) { start = strstr(curpos, "["); if (!start) goto fail; start = start + 1; end = strstr(start, "]"); if (!end) goto fail; memset(end, 0, 1); value = atoi(start); if (value <= 0) goto fail; } else { if (confdb_key_increment (handle, connection_handle, "iterator_tracker", strlen("iterator_tracker"), &value) != CS_OK) value = 1; } while (value != 0) { memset(data, 0, PATH_MAX); if (confdb_object_iter (handle, query_handle, &new_obj_handle, data, &datalen) != CS_OK) { reset_iterator(handle, connection_handle); goto fail; } value--; } resval = malloc(datalen + 2); if (!resval) goto fail; snprintf(resval, datalen + 2, "%s=", data); *rtn = resval; } else if (!strncmp(curpos, "@*", strlen("@*"))) { // this query makes sense only if we are in list mode if (!list) goto fail; if (confdb_key_iter_start(handle, query_handle) != CS_OK) goto fail; *list_handle = query_handle; if (confdb_key_increment (handle, connection_handle, "iterator_tracker", strlen("iterator_tracker"), &value) != CS_OK) value = 1; while (value != 0) { memset(data, 0, PATH_MAX); keyval = NULL; if (confdb_key_iter_typed2 (handle, query_handle, data, (void **)&keyval, &keyvallen, &type) != CS_OK) { reset_iterator(handle, connection_handle); goto fail; } value--; if (value != 0) { free(keyval); keyval = NULL; } } datalen = strlen(data); resval = malloc(datalen + keyvallen + 2); if (!resval) goto fail; snprintf(resval, datalen + keyvallen + 2, "%s=%s", data, keyval); *rtn = resval; free(keyval); } else { /* pure data request */ char *query; // this query doesn't make sense in list mode if (list) goto fail; if (confdb_object_find_start(handle, query_handle) != CS_OK) goto fail; query = strstr(curpos, "@"); if (!query) goto fail; query = query + 1; keyval = NULL; if (confdb_key_get_typed2 (handle, query_handle, query, (void **)&keyval, &keyvallen, &type) != CS_OK) goto fail; *rtn = keyval; } return 0; fail: errno = EINVAL; return -1; }
/* Recursively dump the object tree */ static void print_config_tree(confdb_handle_t handle, hdb_handle_t parent_object_handle, char * parent_name) { hdb_handle_t object_handle; char object_name[OBJ_NAME_SIZE]; size_t object_name_len; char key_name[OBJ_NAME_SIZE]; char key_value[OBJ_NAME_SIZE]; size_t key_value_len; cs_error_t res; int children_printed; confdb_value_types_t type; /* Show the keys */ res = confdb_key_iter_start(handle, parent_object_handle); if (res != CS_OK) { fprintf(stderr, "error resetting key iterator for object "HDB_X_FORMAT" %d\n", parent_object_handle, res); exit(EXIT_FAILURE); } children_printed = 0; while ( (res = confdb_key_iter_typed(handle, parent_object_handle, key_name, key_value, &key_value_len, &type)) == CS_OK) { key_value[key_value_len] = '\0'; if (parent_name != NULL) printf("%s%c", parent_name, SEPERATOR); print_key(key_name, key_value, key_value_len, type); children_printed++; } /* Show sub-objects */ res = confdb_object_iter_start(handle, parent_object_handle); if (res != CS_OK) { fprintf(stderr, "error resetting object iterator for object "HDB_X_FORMAT" %d\n", parent_object_handle, res); exit(EXIT_FAILURE); } while ( (res = confdb_object_iter(handle, parent_object_handle, &object_handle, object_name, &object_name_len)) == CS_OK) { object_name[object_name_len] = '\0'; if (parent_name != NULL) { snprintf(key_value, OBJ_NAME_SIZE, "%s%c%s", parent_name, SEPERATOR, object_name); } else { if ((action == ACTION_PRINT_DEFAULT) && strcmp(object_name, "internal_configuration") == 0) continue; snprintf(key_value, OBJ_NAME_SIZE, "%s", object_name); } print_config_tree(handle, object_handle, key_value); children_printed++; } if (children_printed == 0 && parent_name != NULL) { printf("%s\n", parent_name); } }
/* Recursively dump the object tree */ static void print_config_tree(confdb_handle_t handle, hdb_handle_t parent_object_handle, const char *dn, char *fulldn) { hdb_handle_t object_handle; char object_name[1024]; size_t object_name_len; char key_name[1024]; char *key_value=NULL; confdb_value_types_t type; size_t key_value_len; char cumulative_dn[4096]; int res; int keycount=0; printf("\ndn: %s\n", fulldn); /* Show the keys */ res = confdb_key_iter_start(handle, parent_object_handle); if (res != CS_OK) { printf( "error resetting key iterator for object "HDB_X_FORMAT": %d\n", parent_object_handle, res); return; } while ( (res = confdb_key_iter_typed2(handle, parent_object_handle, key_name, (void**)&key_value, &key_value_len, &type)) == CS_OK) { key_value[key_value_len] = '\0'; printf("%s: %s\n", ldap_attr_name(key_name), key_value); keycount++; free(key_value); key_value=NULL; } if (strncmp(fulldn, "cn=", 3) == 0) { printf("cn: %s\n", dn); } /* Determine objectclass... */ if (keycount == 0) { printf("objectclass: nsContainer\n"); } else { printf("objectclass: %s\n", ldap_attr_name(dn)); } /* Show sub-objects */ res = confdb_object_iter_start(handle, parent_object_handle); if (res != CS_OK) { printf( "error resetting object iterator for object "HDB_X_FORMAT": %d\n", parent_object_handle, res); return; } while ( (res = confdb_object_iter(handle, parent_object_handle, &object_handle, object_name, &object_name_len)) == CS_OK) { hdb_handle_t parent; res = confdb_object_parent_get(handle, object_handle, &parent); if (res != CS_OK) { printf( "error getting parent for object "HDB_X_FORMAT": %d\n", object_handle, res); return; } object_name[object_name_len] = '\0'; /* Check for "name", and create dummy parent object */ res = confdb_key_get_typed2(handle, object_handle, "name", (void **)&key_value, &key_value_len, &type); if (res == CS_OK) { sprintf(cumulative_dn, "cn=%s,%s", object_name, fulldn); printf("\n"); printf("dn: %s\n", cumulative_dn); printf("cn: %s\n", object_name); printf("objectclass: %s\n", "nsContainer"); snprintf(cumulative_dn, sizeof(cumulative_dn) - 1, "name=%s,cn=%s,%s", key_value, object_name, fulldn); free(key_value); key_value = NULL; } else { sprintf(cumulative_dn, "cn=%s,%s", object_name, fulldn); } /* Down we go ... */ print_config_tree(handle, object_handle, object_name, cumulative_dn); } }