diff options
author | Nalin Dahyabhai <nalin@redhat.com> | 2010-11-22 13:39:46 -0500 |
---|---|---|
committer | Nalin Dahyabhai <nalin@redhat.com> | 2010-11-22 13:39:46 -0500 |
commit | 608d05efbdab5269d8c372594be59a63cd385799 (patch) | |
tree | 0bfd3029def65c35028a08d43437fe6bb4b855e9 /src/format.c | |
parent | c067125f9150e452fdfe9885d757224bd5ec5795 (diff) | |
download | slapi-nis-608d05efbdab5269d8c372594be59a63cd385799.tar.gz slapi-nis-608d05efbdab5269d8c372594be59a63cd385799.tar.xz slapi-nis-608d05efbdab5269d8c372594be59a63cd385799.zip |
- try to factor deref_r and its corresponding update code into
optionally handling filters at each step of the way
Diffstat (limited to 'src/format.c')
-rw-r--r-- | src/format.c | 217 |
1 files changed, 171 insertions, 46 deletions
diff --git a/src/format.c b/src/format.c index 95ee384..6285f5b 100644 --- a/src/format.c +++ b/src/format.c @@ -1,5 +1,5 @@ /* - * Copyright 2008 Red Hat, Inc. + * Copyright 2008,2010 Red Hat, Inc. * * This Program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -291,15 +291,18 @@ format_find_ref_attr_list(struct format_ref_attr_list **list, break; } if (j < item->n_links - 1) { - if ((filters == NULL) && + if (((filters == NULL) || + (filters[j] == NULL)) && (link->filter_str != NULL)) { break; } if ((filters != NULL) && + (filters[j] != NULL) && (link->filter_str == NULL)) { break; } if ((filters != NULL) && + (filters[j] != NULL) && (link->filter_str != NULL) && (strcmp(filters[j], link->filter_str) != 0)) { @@ -360,7 +363,7 @@ format_add_ref_attr_list(struct format_ref_attr_list ***list, format_free_ref_attr_list(ret); return NULL; } - if ((filters != NULL) && (j < ret[i]->n_links - 1)) { + if ((filters != NULL) && (filters[j] != NULL)) { ftmp = strdup(filters[j]); if (ftmp == NULL) { format_free_ref_attr_list(ret); @@ -1060,14 +1063,15 @@ format_deref(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e, * at last, pull out the values for the attribute named by the last argument, * and return a list of those values. */ static int -format_deref_r(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e, - const char *group, const char *set, - const char *args, const char *disallowed, - char *outbuf, int outbuf_len, - struct format_choice **outbuf_choices, - char ***ref_attrs, struct format_inref_attr ***inref_attrs, - struct format_ref_attr_list ***ref_attr_list, - struct format_ref_attr_list ***inref_attr_list) +format_deref_rx(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e, + const char *fname, const char *group, const char *set, + const char **attributes, const char **filters, + const char *disallowed, + char *outbuf, int outbuf_len, + struct format_choice **outbuf_choices, + char ***ref_attrs, struct format_inref_attr ***inref_attrs, + struct format_ref_attr_list ***ref_attr_list, + struct format_ref_attr_list ***inref_attr_list) { int i, j, k, ret, argc; Slapi_Entry *entry; @@ -1075,37 +1079,17 @@ format_deref_r(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e, Slapi_ValueSet *values; Slapi_Value *value; int disposition, buffer_flags; - char **argv, *attrs[2], *actual_attr; + char *attrs[2], *actual_attr; const char *dn, *cvalue; const struct berval *bval; struct berval **choices; struct format_ref_attr_list *list; - ret = format_parse_args(state, args, &argc, &argv); - if (ret != 0) { - slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id, - "deref_r: error parsing arguments\n"); - return -EINVAL; - } - if (argc < 2) { - slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id, - "deref_r: requires at least two arguments\n"); - format_free_parsed_args(argv); - return -EINVAL; - } - if (outbuf_choices == NULL) { - slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id, - "deref_r: returns a list, but a list " - "would not be appropriate\n"); - format_free_parsed_args(argv); - return -EINVAL; - } - /* Note that this list of attributes is used for pulling up data. */ format_add_ref_attr_list(ref_attr_list, group, set, - (const char **) argv, NULL); + attributes, filters); list = format_find_ref_attr_list(*ref_attr_list, group, set, - (const char **) argv, NULL); + attributes, filters); /* Follow the chain: set up the first link. */ these = NULL; @@ -1129,9 +1113,9 @@ format_deref_r(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e, dn = slapi_sdn_get_ndn(these[j]); slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id, - "deref_r: noting parent " + "%s: noting parent " "\"%s\" for \"%s\"\n", - slapi_dn_parent(dn), attrs[0]); + fname, slapi_dn_parent(dn), attrs[0]); format_add_sdn_list(&list->links[i].base_sdn_list, slapi_dn_parent(dn)); /* Pull up the named entry. */ @@ -1141,17 +1125,44 @@ format_deref_r(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e, if (entry == NULL) { slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id, - "deref_r: error reading entry " - "\"%s\"\n", + "%s: error reading entry " + "\"%s\"\n", fname, slapi_sdn_get_dn(these[j])); continue; } else { slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id, - "deref_r: reading entry " - "\"%s\" (%d)\n", + "%s: reading entry " + "\"%s\" (%d)\n", fname, slapi_sdn_get_dn(these[j]), i); } + /* If we were given filters, test this entry against + * the right filter. */ + if (list->links[i].filter != NULL) { + if (slapi_filter_test(pb, entry, + list->links[i].filter, + 0) != 0) { + slapi_log_error(SLAPI_LOG_PLUGIN, + state->plugin_desc->spd_id, + "%s: entry \"%s\" does " + "not match filter " + "\"%s\"\n", + fname, + slapi_entry_get_dn(entry), + filters[i]); + slapi_entry_free(entry); + continue; + } else { + slapi_log_error(SLAPI_LOG_PLUGIN, + state->plugin_desc->spd_id, + "%s: entry \"%s\" " + "matches filter " + "\"%s\"\n", + fname, + slapi_entry_get_dn(entry), + filters[i]); + } + } /* Pull up the value set. */ if (slapi_vattr_values_get(entry, attrs[0], &values, &disposition, @@ -1159,9 +1170,9 @@ format_deref_r(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e, 0, &buffer_flags) != 0) { slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id, - "deref_r: entry \"%s\" has no " - "values for \"%s\"\n", - slapi_sdn_get_dn(these[j]), + "%s: entry \"%s\" has no " + "values for \"%s\"\n", fname, + slapi_entry_get_dn(entry), attrs[0]); slapi_entry_free(entry); continue; @@ -1192,9 +1203,9 @@ format_deref_r(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e, format_add_bv_list(&choices, bval); slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id, - "deref_r: found value " + "%s: found value " "\"%.*s\" in \"%s\"\n", - bval->bv_len, + fname, bval->bv_len, bval->bv_val, dn); } } @@ -1211,7 +1222,6 @@ format_deref_r(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e, /* Clean up and return any values we found. */ format_free_sdn_list(these); - format_free_parsed_args(argv); if (choices != NULL) { format_add_choice(outbuf_choices, outbuf, choices); format_free_bv_list(choices); @@ -1221,6 +1231,120 @@ format_deref_r(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e, } } +static int +format_deref_r(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e, + const char *group, const char *set, + const char *args, const char *disallowed, + char *outbuf, int outbuf_len, + struct format_choice **outbuf_choices, + char ***ref_attrs, struct format_inref_attr ***inref_attrs, + struct format_ref_attr_list ***ref_attr_list, + struct format_ref_attr_list ***inref_attr_list) +{ + int ret, argc; + char **argv; + ret = format_parse_args(state, args, &argc, &argv); + if (ret != 0) { + slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id, + "deref_r: error parsing arguments\n"); + return -EINVAL; + } + if (argc < 2) { + slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id, + "deref_r: requires at least two arguments\n"); + format_free_parsed_args(argv); + return -EINVAL; + } + if (outbuf_choices == NULL) { + slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id, + "deref_r: returns a list, but a list " + "would not be appropriate\n"); + format_free_parsed_args(argv); + return -EINVAL; + } + ret = format_deref_rx(state, pb, e, "deref_r", + group, set, + (const char **) argv, NULL, + disallowed, + outbuf, outbuf_len, + outbuf_choices, + ref_attrs, inref_attrs, + ref_attr_list, inref_attr_list); + format_free_parsed_args(argv); + return ret; +} + +static int +format_deref_rf(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e, + const char *group, const char *set, + const char *args, const char *disallowed, + char *outbuf, int outbuf_len, + struct format_choice **outbuf_choices, + char ***ref_attrs, struct format_inref_attr ***inref_attrs, + struct format_ref_attr_list ***ref_attr_list, + struct format_ref_attr_list ***inref_attr_list) +{ + int ret, argc, n, i; + char **argv, **attrs, **filters; + ret = format_parse_args(state, args, &argc, &argv); + if (ret != 0) { + slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id, + "deref_rf: error parsing arguments\n"); + return -EINVAL; + } + if (argc < 3) { + slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id, + "deref_rf: requires at least three " + "arguments\n"); + format_free_parsed_args(argv); + return -EINVAL; + } + if (outbuf_choices == NULL) { + slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id, + "deref_rf: returns a list, but a list " + "would not be appropriate\n"); + format_free_parsed_args(argv); + return -EINVAL; + } + /* Build the lists of attributes and filters. */ + n = (argc + 1) / 2; + attrs = malloc(sizeof(char *) * (n + 1)); + if (attrs == NULL) { + slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id, + "deref_rf: out of memory\n"); + format_free_parsed_args(argv); + return -ENOMEM; + } + memset(attrs, 0, sizeof(char *) * (n + 1)); + filters = malloc(sizeof(char *) * (n + 1)); + if (filters == NULL) { + slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id, + "deref_rf: out of memory\n"); + free(attrs); + format_free_parsed_args(argv); + return -ENOMEM; + } + memset(filters, 0, sizeof(char *) * (n + 1)); + for (i = 0; i < n; i++) { + attrs[i] = argv[i * 2]; + if (i < (n - 1)) { + filters[i + 1] = argv[i * 2 + 1]; + } + } + ret = format_deref_rx(state, pb, e, "deref_rf", + group, set, + (const char **) attrs, (const char **) filters, + disallowed, + outbuf, outbuf_len, + outbuf_choices, + ref_attrs, inref_attrs, + ref_attr_list, inref_attr_list); + free(filters); + free(attrs); + format_free_parsed_args(argv); + return ret; +} + /* Look up entries in the set named by the first argument, which have this * entry's DN stored in the attribute named by the second argument, pull out * the values for the attribute named by the third argument, and return a list @@ -2564,6 +2688,7 @@ format_lookup_fn(const char *fnname) {"first", format_first}, {"deref", format_deref}, {"deref_r", format_deref_r}, + {"deref_rf", format_deref_rf}, {"referred", format_referred}, {"referred_r", format_referred_r}, {"merge", format_merge}, |