diff options
author | Nalin Dahyabhai <nalin.dahyabhai@pobox.com> | 2007-11-08 14:07:04 -0500 |
---|---|---|
committer | Nalin Dahyabhai <nalin.dahyabhai@pobox.com> | 2007-11-08 14:07:04 -0500 |
commit | 96254789dcc5015472d280813b10121f494b32e9 (patch) | |
tree | 28b5935854a987971291817c5dc3b1ea7acb8530 /src/plugin.c | |
parent | 226ce762936ee32c02ebf3d6542eb905500ba31f (diff) | |
download | slapi-nis-96254789dcc5015472d280813b10121f494b32e9.tar.gz slapi-nis-96254789dcc5015472d280813b10121f494b32e9.tar.xz slapi-nis-96254789dcc5015472d280813b10121f494b32e9.zip |
- up and answering dummy queries
Diffstat (limited to 'src/plugin.c')
-rw-r--r-- | src/plugin.c | 116 |
1 files changed, 108 insertions, 8 deletions
diff --git a/src/plugin.c b/src/plugin.c index 3d1976b..31067da 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -32,9 +32,14 @@ plugin_description = { struct state { pthread_t tid; + Slapi_ComponentId *plugin_identity; PLArenaPool *arena; int listenfd[2]; }; +struct search_data { + struct state *state; + int client; +}; static int setup_listener(struct state **lstate) @@ -112,6 +117,95 @@ failed: return -1; } +static void +cb_result(int rc, void *callback_data) +{ + struct search_data *data = callback_data; + char buf[sizeof(rc) * 4 + 1]; + slapi_log_error(SLAPI_LOG_PLUGIN, "cb_result", + "returning result %d\n", rc); + sprintf(buf, "%d\n", rc); + write(data->client, buf, strlen(buf)); +} + +static int +cb_entry(Slapi_Entry *e, void *callback_data) +{ + struct search_data *data = callback_data; + Slapi_Attr *attr; + Slapi_ValueSet *values; + Slapi_Value *value; + const char *dn; + const struct berval *val; + int i; + attr = NULL; + if (slapi_entry_attr_find(e, "namingContexts", &attr) == 0) { + dn = slapi_entry_get_dn(e); + slapi_log_error(SLAPI_LOG_PLUGIN, "cb_entry", + "returning entry \"%s\"\n", dn); + write(data->client, "[", 1); + write(data->client, dn, strlen(dn)); + write(data->client, "]\n", 2); + values = NULL; + if (slapi_attr_get_valueset(attr, &values) == 0) { + i = slapi_valueset_count(values); + while (i > 0) { + i--; + value = NULL; + if (slapi_valueset_next_value(values, i, + &value) == 0) { + val = slapi_value_get_berval(value); + if (val != NULL) { + write(data->client, + val->bv_val, val->bv_len); + write(data->client, "\n", 1); + } + } + } + } + } + return 0; +} + +static int +cb_referral(char *referral, void *callback_data) +{ + struct search_data *data = callback_data; + slapi_log_error(SLAPI_LOG_PLUGIN, "cb_referral", + "returning referral to \"%s\"\n", referral); + write(data->client, "See also ", 9); + write(data->client, referral, strlen(referral)); + write(data->client, "\n", 1); + return 0; +} + +static void +handle_client(struct state *state, int client) +{ + Slapi_PBlock *pblock; + int i; + char *attrs[] = { + "namingContexts", + NULL, + }; + struct search_data data; + memset(&data, 0, sizeof(data)); + data.state = state; + data.client = client; + pblock = slapi_pblock_new(); + slapi_search_internal_set_pb(pblock, + "", LDAP_SCOPE_BASE, "(objectclass=*)", + attrs, 0, + NULL, + NULL, + state->plugin_identity, + 0); + i = slapi_search_internal_callback_pb(pblock, &data, + cb_result, cb_entry, cb_referral); + slapi_pblock_destroy(pblock); + close(client); +} + static void * process_requests(void *p) { @@ -141,8 +235,7 @@ process_requests(void *p) plugin_description.spd_id, "answering client request %d\n", client); - write(client, "FOO!\n", 5); - close(client); + handle_client(state, client); } } return state; @@ -153,19 +246,16 @@ static int plugin_start(Slapi_PBlock *pb) { struct state *state; + slapi_pblock_get(pb, SLAPI_PLUGIN_PRIVATE, &state); slapi_log_error(SLAPI_LOG_PLUGIN, "my_init_function", "plugin starting\n"); - if (setup_listener(&state) == -1) { - slapi_log_error(SLAPI_LOG_PLUGIN, plugin_description.spd_id, - "error setting up listening sockets\n"); - return -1; - } - slapi_pblock_set(pb, SLAPI_PLUGIN_PRIVATE, state); if (pthread_create(&state->tid, NULL, &process_requests, state) != 0) { slapi_log_error(SLAPI_LOG_PLUGIN, plugin_description.spd_id, "error starting listener thread\n"); return -1; } + slapi_log_error(SLAPI_LOG_PLUGIN, "my_init_function", + "plugin started\n"); return 0; } /* Prepare for shutdown. */ @@ -180,10 +270,20 @@ plugin_close(Slapi_PBlock *pb) int my_init_function(Slapi_PBlock *pb) { + struct state *state; + if (setup_listener(&state) == -1) { + slapi_log_error(SLAPI_LOG_PLUGIN, plugin_description.spd_id, + "error setting up listening sockets\n"); + return -1; + } slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION, SLAPI_PLUGIN_VERSION_03); slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION, &plugin_description); slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN, &plugin_start); slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN, &plugin_close); + slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &state->plugin_identity); + slapi_pblock_set(pb, SLAPI_PLUGIN_PRIVATE, state); + slapi_log_error(SLAPI_LOG_PLUGIN, plugin_description.spd_id, + "plugin identity %lx\n", state->plugin_identity); slapi_log_error(SLAPI_LOG_PLUGIN, plugin_description.spd_id, "registering plugin hooks\n"); return 0; |