From 6132635cdafa125036ab8d5d5388708cd8d28447 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jorge=20Rodr=C3=ADguez?= Date: Fri, 5 Oct 2007 14:08:41 +0200 Subject: [PATCH] More sophisticated selection of the user jid in Vcard2Ldap. Minor fixes. --- include/v2l_conn.h | 3 + patch/patch-v2l-jabberd-2.1.14.diff | 244 ++++++++++++++++++------------------ src/v2l_config.c | 6 +- src/v2l_conn.c | 200 ++++++++++++++--------------- src/v2l_main.c | 4 +- src/v2l_vcard.c | 17 +-- tools/Vcard2Ldap.pm | 25 +++- 7 files changed, 257 insertions(+), 242 deletions(-) diff --git a/include/v2l_conn.h b/include/v2l_conn.h index 8b7f3d1..88b04d5 100644 --- a/include/v2l_conn.h +++ b/include/v2l_conn.h @@ -81,10 +81,13 @@ typedef void (*v2l_AttrValueFunction) (const char *attr, const char **vals, void *pointer, void *shrdata); /*! \brief Gets a (ephemeral) connection by name. Creates one if doesn't exist. + \warning The name defined by V2L_ADMIN is reserved for master conn. \pre self is valid, user is not null and exists. \post ephermeral connection for user is open. \param self Module config. \param user the username + \sa V2L_ADMIN + \sa v2l_get_master_conn \return the connection if no error, otherwise NULL. */ extern v2l_LdapConn *v2l_get_conn (v2l_Config *self, const char *user); diff --git a/patch/patch-v2l-jabberd-2.1.14.diff b/patch/patch-v2l-jabberd-2.1.14.diff index 30956a2..90fdc1c 100644 --- a/patch/patch-v2l-jabberd-2.1.14.diff +++ b/patch/patch-v2l-jabberd-2.1.14.diff @@ -302,8 +302,8 @@ diff -Naurb jabberd-2.1.14.orig/sm/mod_iq_vcard.c jabberd-2.1.14/sm/mod_iq_vcard DLLEXPORT int module_init(mod_instance_t mi, char *arg) { diff -Naurb jabberd-2.1.14.orig/sm/v2l_config.c jabberd-2.1.14/sm/v2l_config.c --- jabberd-2.1.14.orig/sm/v2l_config.c 1970-01-01 01:00:00.000000000 +0100 -+++ jabberd-2.1.14/sm/v2l_config.c 2007-10-04 19:12:20.000000000 +0200 -@@ -0,0 +1,176 @@ ++++ jabberd-2.1.14/sm/v2l_config.c 2007-10-05 11:02:41.000000000 +0200 +@@ -0,0 +1,178 @@ +/* + * 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 @@ -345,6 +345,8 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_config.c jabberd-2.1.14/sm/v2l_config.c +*/ +static char *_v2l_config_get_tag (T_CONF conn_base, const char *tag); + ++/* public api */ ++ +int +v2l_config_init (v2l_Config *self, T_CONF cfgroot) +{ @@ -353,7 +355,7 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_config.c jabberd-2.1.14/sm/v2l_config.c + + if (!cfgroot) + { -+ log_error(ZONE, "xdb_v2l configuration not present"); ++ log_error (ZONE, "xdb_v2l configuration not present"); + return 0; + } + @@ -363,7 +365,7 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_config.c jabberd-2.1.14/sm/v2l_config.c + + if (conn_base == NULL) + { -+ log_error(ZONE," tag is not present"); ++ log_error (ZONE," tag is not present"); + return 0; + } +#else @@ -482,7 +484,7 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_config.c jabberd-2.1.14/sm/v2l_config.c +} diff -Naurb jabberd-2.1.14.orig/sm/v2l_config.h jabberd-2.1.14/sm/v2l_config.h --- jabberd-2.1.14.orig/sm/v2l_config.h 1970-01-01 01:00:00.000000000 +0100 -+++ jabberd-2.1.14/sm/v2l_config.h 2007-10-04 19:11:46.000000000 +0200 ++++ jabberd-2.1.14/sm/v2l_config.h 2007-10-04 20:40:38.000000000 +0200 @@ -0,0 +1,73 @@ +/* + * This program is free software; you can redistribute it and/or modify @@ -559,8 +561,8 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_config.h jabberd-2.1.14/sm/v2l_config.h +#endif diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.c jabberd-2.1.14/sm/v2l_conn.c --- jabberd-2.1.14.orig/sm/v2l_conn.c 1970-01-01 01:00:00.000000000 +0100 -+++ jabberd-2.1.14/sm/v2l_conn.c 2007-10-04 20:29:08.000000000 +0200 -@@ -0,0 +1,924 @@ ++++ jabberd-2.1.14/sm/v2l_conn.c 2007-10-05 11:50:31.000000000 +0200 +@@ -0,0 +1,916 @@ +/* + * 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 @@ -604,7 +606,7 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.c jabberd-2.1.14/sm/v2l_conn.c +/*! How often thread checks for LDAP results, in seconds */ +#define V2L_POLL_INTERVAL 1 + -+#define LOG_ERROR_MEM log_error(ZONE, "Unable to allocate memory") ++#define LOG_ERROR_MEM log_error (ZONE, "Unable to allocate memory") + +/*! Global hashtable of all currently active LDAP connections */ +static xht V2L_CONN_LIST = NULL; @@ -632,7 +634,7 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.c jabberd-2.1.14/sm/v2l_conn.c + \pre self is valid, user is not NULL + \return 1 if no error, otherwise 0. +*/ -+static int _v2l_ldap_get_credentials(v2l_Config *self, const char *user, ++static int _v2l_ldap_get_credentials (v2l_Config *self, const char *user, + char **passwd, char **cn); + +/*! \brief Closes and frees a connection. @@ -683,13 +685,14 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.c jabberd-2.1.14/sm/v2l_conn.c + +/*! \brief Waits for LDAP results. + Operations on the directory are asynchronous. This is a sync wait function. -+ \param \param[in,out] evt_res Control and info parameter. ++ \param[in,out] evt_res Control and info parameter. +*/ +static void _v2l_ldap_sync (v2l_LdapEvt *evt_res); + +/*! \brief Adds LDAP attr to request. + Utility function, hides OpenLDAP API to upper level. -+ \param[in,out] req The SLL of LDAP requests. ++ \pre Attribute is not NULL. ++ \param[in,out] req The SLL of LDAP requests. If NULL new list is returned. + \param attr The attr, LDAPMod pointer. + \return The list of requests + the last added. The list of request if error. +*/ @@ -739,7 +742,7 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.c jabberd-2.1.14/sm/v2l_conn.c + void *arg); + +/*! A thread for active wait for LDAP results */ -+static int _v2l_ldap_wait_callback(void *arg); ++static int _v2l_ldap_wait_callback (void *arg); + +#ifdef _V2L_JABBER2 +static void *_v2l_ldap_wait_callback_g (void *arg); @@ -754,7 +757,7 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.c jabberd-2.1.14/sm/v2l_conn.c + + user_conn = (v2l_LdapConn *) xhash_get (V2L_CONN_LIST, user); + -+ if(user_conn == NULL) ++ if (user_conn == NULL) + { + char *passwd, *cn, *binddn; + @@ -831,7 +834,7 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.c jabberd-2.1.14/sm/v2l_conn.c + return NULL; + } + -+ _v2l_ldap_sync(evt_res); ++ _v2l_ldap_sync (evt_res); + + if (ldap_count_entries (evt_res->ld, evt_res->result) != 1) + { @@ -849,7 +852,7 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.c jabberd-2.1.14/sm/v2l_conn.c +{ + LDAPMod **attrs; + int i, nbmod, ret; -+ v2l_LdapRequest *cur_req, *cur_temp; ++ v2l_LdapRequest *cur_req; + v2l_LdapEvt *evt_res; + + if (req == NULL) @@ -900,7 +903,7 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.c jabberd-2.1.14/sm/v2l_conn.c + { + memcpy (attrs[i], cur_req->attr, sizeof (LDAPMod)); + log_debug (ZONE, "Element \"%s\" (%d) in the LDAP request: %s", -+ attrs[i]->mod_type, i, attrs[i]->mod_values[0]); ++ attrs[i]->mod_type, i, attrs[i]->mod_values[0]); + } + + attrs[nbmod] = NULL; @@ -925,16 +928,13 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.c jabberd-2.1.14/sm/v2l_conn.c + ldap_msgfree (evt_res->result); + free (evt_res); + -+ for (cur_req = req, i = 0; i < nbmod; i++) ++ for (cur_req = req, i = 0; i < nbmod; cur_req = cur_req->next, i++) + { -+ cur_temp = cur_req; -+ cur_req = cur_req->next; -+ + free (attrs[i]); -+ free (cur_temp->attr->mod_values[0]); -+ free (cur_temp->attr->mod_values); -+ free (cur_temp->attr); -+ free (cur_temp); ++ free (cur_req->attr->mod_values[0]); ++ free (cur_req->attr->mod_values); ++ free (cur_req->attr); ++ free (cur_req); + } + + free (attrs); @@ -980,6 +980,48 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.c jabberd-2.1.14/sm/v2l_conn.c + return _v2l_add_attr (req, mod); +} + ++void ++v2l_ldap_for_all_attrs (v2l_AttrValueFunction value_func, ++ v2l_AttrMatchFunction match_func, void *pointer, v2l_LdapEvt *evt_res) ++{ ++ LDAPMessage *current_result; ++ BerElement *ber; ++ char *current_attr, **vals; ++ void *shrdata; ++ ++ current_result = ldap_first_entry (evt_res->ld, evt_res->result); ++ current_attr = ldap_first_attribute (evt_res->ld, current_result, &ber); ++ ++ /* step through each attribute in objectclass */ ++ for (; ++ current_attr != NULL; ++ current_attr = ldap_next_attribute (evt_res->ld, current_result, ber)) ++ { ++ ++ if (match_func (current_attr, &shrdata)) ++ { ++ vals = ldap_get_values (evt_res->ld, current_result, current_attr); ++ value_func (current_attr, (const char **) vals, pointer, shrdata); ++ ++ if (vals != NULL) ++ { ++ ldap_value_free (vals); ++ } ++ } ++ ++ ldap_memfree (current_attr); ++ } /* attributes loop */ ++ ++ if (ber != NULL) ++ { ++ ber_free (ber, 0); ++ } ++ ++ /* don't forget to free the next attribute */ ++ ldap_memfree (current_attr); ++ ldap_msgfree (evt_res->result); ++} ++ +/* public api ends here */ + +static v2l_LdapConn * @@ -1029,7 +1071,7 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.c jabberd-2.1.14/sm/v2l_conn.c + { + LOG_ERROR_MEM; + ldap_unbind_s (ld); -+ pool_free(poolref); ++ pool_free (poolref); + return NULL; + } + @@ -1043,7 +1085,7 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.c jabberd-2.1.14/sm/v2l_conn.c + { + LOG_ERROR_MEM; + ldap_unbind_s (ld); -+ pool_free(poolref); ++ pool_free (poolref); + return NULL; + } + @@ -1055,7 +1097,7 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.c jabberd-2.1.14/sm/v2l_conn.c + { + LOG_ERROR_MEM; + ldap_unbind_s (ld); -+ pool_free(poolref); ++ pool_free (poolref); + return NULL; + } + @@ -1068,7 +1110,7 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.c jabberd-2.1.14/sm/v2l_conn.c + { + LOG_ERROR_MEM; + ldap_unbind_s (ld); -+ pool_free(poolref); ++ pool_free (poolref); + return NULL; + } + @@ -1089,6 +1131,12 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.c jabberd-2.1.14/sm/v2l_conn.c +{ + char *filter; + ++ if (self->filter == NULL || user == NULL) ++ { ++ log_error (ZONE, "Attempt to make a NULL filter"); ++ return NULL; ++ } ++ + filter = (char *) malloc (strlen (self->filter) + strlen (user)); + + if (filter == NULL) @@ -1097,7 +1145,7 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.c jabberd-2.1.14/sm/v2l_conn.c + return NULL; + } + -+ sprintf(filter, self->filter, user); ++ sprintf (filter, self->filter, user); + + return filter; +} @@ -1111,7 +1159,7 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.c jabberd-2.1.14/sm/v2l_conn.c + char *filter, **vpw, **vcn; + char *attrs[3] = {"userPassword", "cn", NULL}; + -+ evt_res = (v2l_LdapEvt *) malloc(sizeof(v2l_LdapEvt)); ++ evt_res = (v2l_LdapEvt *) malloc (sizeof (v2l_LdapEvt)); + + if (evt_res == NULL) + { @@ -1128,7 +1176,7 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.c jabberd-2.1.14/sm/v2l_conn.c + + if (evt_res->rc != LDAP_SUCCESS) + { -+ log_error(ZONE, ++ log_error (ZONE, + "LDAP error attempting to retrieve \"%s\"'s password: %s", + user, ldap_err2string (evt_res->rc)); + free (evt_res); @@ -1140,9 +1188,9 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.c jabberd-2.1.14/sm/v2l_conn.c + *passwd = NULL; + *cn = NULL; + -+ if (ldap_count_entries(evt_res->ld, evt_res->result) == 1) ++ if (ldap_count_entries (evt_res->ld, evt_res->result) == 1) + { -+ e = ldap_first_entry(evt_res->ld, evt_res->result); ++ e = ldap_first_entry (evt_res->ld, evt_res->result); + + vpw = ldap_get_values (evt_res->ld, e, "userPassword"); + @@ -1165,7 +1213,7 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.c jabberd-2.1.14/sm/v2l_conn.c + return 0; + } + -+ *passwd = (char *) malloc (strlen(vpw[0]) + 1); ++ *passwd = (char *) malloc (strlen (vpw[0]) + 1); + + if (*passwd == NULL) + { @@ -1180,7 +1228,7 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.c jabberd-2.1.14/sm/v2l_conn.c + strcpy (*passwd, vpw[0]); + ldap_value_free (vpw); + -+ *cn = (char *) malloc (strlen(vcn[0]) + 1); ++ *cn = (char *) malloc (strlen (vcn[0]) + 1); + + if (*cn == NULL) + { @@ -1236,7 +1284,7 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.c jabberd-2.1.14/sm/v2l_conn.c + pthread_t thr; + int rc; + -+ rc = pthread_create(&thr, NULL, _v2l_ldap_wait_callback_g, (void *) evt_res); ++ rc = pthread_create (&thr, NULL, _v2l_ldap_wait_callback_g, (void *) evt_res); + + if (rc != 0) + { @@ -1244,7 +1292,7 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.c jabberd-2.1.14/sm/v2l_conn.c + return; + } + -+ pthread_join(thr, NULL); ++ pthread_join (thr, NULL); +#endif +} + @@ -1252,10 +1300,9 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.c jabberd-2.1.14/sm/v2l_conn.c +static int +_v2l_count_attrs (v2l_LdapRequest *req) +{ -+ v2l_LdapRequest *ptr; + int nbmod; + -+ for (nbmod = 0, ptr = req; ptr != NULL; ptr = ptr->next, nbmod++); ++ for (nbmod = 0; req != NULL; req = req->next, nbmod++); + + return nbmod; +} @@ -1263,43 +1310,32 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.c jabberd-2.1.14/sm/v2l_conn.c +static v2l_LdapRequest * +_v2l_add_attr (v2l_LdapRequest *req, LDAPMod *attr) +{ ++ v2l_LdapRequest *new_req, *ptr; ++ + if (attr == NULL) + { + log_warn (ZONE, "LDAP attribute is NULL? I cannot add anything"); + return NULL; + } + -+ if (req == NULL) ++ new_req = (v2l_LdapRequest *) malloc (sizeof (v2l_LdapRequest)); ++ ++ if (new_req == NULL) + { -+ req = (v2l_LdapRequest *) malloc (sizeof (v2l_LdapRequest)); ++ LOG_ERROR_MEM; ++ return NULL; ++ } + -+ if (req == NULL) -+ { -+ LOG_ERROR_MEM; -+ return NULL; -+ } ++ new_req->attr = attr; ++ new_req->next = NULL; + -+ req->attr = attr; -+ req->next = NULL; ++ if (req == NULL) ++ { ++ req = new_req; + } + else + { -+ v2l_LdapRequest *ptr; -+ v2l_LdapRequest *new_req; -+ -+ new_req = (v2l_LdapRequest *) malloc (sizeof (v2l_LdapRequest)); -+ -+ if (new_req == NULL) -+ { -+ LOG_ERROR_MEM; -+ return NULL; -+ } -+ -+ new_req->attr = attr; -+ new_req->next = NULL; -+ + for (ptr = req; ptr->next != NULL; ptr = ptr->next); -+ + ptr->next = new_req; + } + @@ -1307,7 +1343,7 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.c jabberd-2.1.14/sm/v2l_conn.c +} + +static int -+_v2l_ldap_wait_callback(void *arg) ++_v2l_ldap_wait_callback (void *arg) +{ + v2l_LdapEvt *evt_res = (v2l_LdapEvt *) arg; + LDAPMessage *result; @@ -1317,8 +1353,8 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.c jabberd-2.1.14/sm/v2l_conn.c + + if (rc == -1) + { -+ log_error(ZONE, "LDAP result error %s", -+ ldap_err2string(rc)); ++ log_error (ZONE, "LDAP result error %s", ++ ldap_err2string (rc)); + evt_res->result = NULL; + evt_res->rc = -1; + return 1; @@ -1428,11 +1464,11 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.c jabberd-2.1.14/sm/v2l_conn.c + pth_spawn (attr, _v2l_purge_conn_callback, NULL); + pth_attr_destroy (attr); +#else -+ pthread_attr_init(&attr); -+ pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); ++ pthread_attr_init (&attr); ++ pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); + + rc = pthread_create (&thr, &attr, _v2l_purge_conn_callback, NULL); -+ pthread_attr_destroy(&attr); ++ pthread_attr_destroy (&attr); + + if (rc != 0) + { @@ -1443,52 +1479,10 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.c jabberd-2.1.14/sm/v2l_conn.c + + xhash_put (V2L_CONN_LIST, ldap_conn->user, (void *) ldap_conn); +} -+ -+void -+v2l_ldap_for_all_attrs(v2l_AttrValueFunction value_func, -+ v2l_AttrMatchFunction match_func, void *pointer, v2l_LdapEvt *evt_res) -+{ -+ LDAPMessage *current_result; -+ BerElement *ber; -+ char *current_attr, **vals; -+ void *shrdata; -+ -+ current_result = ldap_first_entry (evt_res->ld, evt_res->result); -+ current_attr = ldap_first_attribute (evt_res->ld, current_result, &ber); -+ -+ /* step through each attribute in objectclass */ -+ for (; -+ current_attr != NULL; -+ current_attr = ldap_next_attribute (evt_res->ld, current_result, ber)) -+ { -+ -+ if (match_func (current_attr, &shrdata)) -+ { -+ vals = ldap_get_values (evt_res->ld, current_result, current_attr); -+ value_func (current_attr, (const char **) vals, pointer, shrdata); -+ -+ if (vals != NULL) -+ { -+ ldap_value_free (vals); -+ } -+ } -+ -+ ldap_memfree (current_attr); -+ } /* attributes loop */ -+ -+ if (ber != NULL) -+ { -+ ber_free (ber, 0); -+ } -+ -+ /* don't forget to free the next attribute */ -+ ldap_memfree (current_attr); -+ ldap_msgfree (evt_res->result); -+} diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.h jabberd-2.1.14/sm/v2l_conn.h --- jabberd-2.1.14.orig/sm/v2l_conn.h 1970-01-01 01:00:00.000000000 +0100 -+++ jabberd-2.1.14/sm/v2l_conn.h 2007-09-27 01:03:26.000000000 +0200 -@@ -0,0 +1,143 @@ ++++ jabberd-2.1.14/sm/v2l_conn.h 2007-10-05 12:06:20.000000000 +0200 +@@ -0,0 +1,146 @@ +/* + * 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 @@ -1572,10 +1566,13 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.h jabberd-2.1.14/sm/v2l_conn.h + void *pointer, void *shrdata); + +/*! \brief Gets a (ephemeral) connection by name. Creates one if doesn't exist. ++ \warning The name defined by V2L_ADMIN is reserved for master conn. + \pre self is valid, user is not null and exists. + \post ephermeral connection for user is open. + \param self Module config. + \param user the username ++ \sa V2L_ADMIN ++ \sa v2l_get_master_conn + \return the connection if no error, otherwise NULL. +*/ +extern v2l_LdapConn *v2l_get_conn (v2l_Config *self, const char *user); @@ -1634,8 +1631,8 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_conn.h jabberd-2.1.14/sm/v2l_conn.h +#endif diff -Naurb jabberd-2.1.14.orig/sm/v2l_vcard.c jabberd-2.1.14/sm/v2l_vcard.c --- jabberd-2.1.14.orig/sm/v2l_vcard.c 1970-01-01 01:00:00.000000000 +0100 -+++ jabberd-2.1.14/sm/v2l_vcard.c 2007-09-26 23:46:59.000000000 +0200 -@@ -0,0 +1,588 @@ ++++ jabberd-2.1.14/sm/v2l_vcard.c 2007-10-05 11:03:01.000000000 +0200 +@@ -0,0 +1,589 @@ +/* + * 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 @@ -1671,7 +1668,7 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_vcard.c jabberd-2.1.14/sm/v2l_vcard.c +#endif +#include + -+#define LOG_ERROR_MEM log_error(ZONE, "Unable to allocate memory") ++#define LOG_ERROR_MEM log_error (ZONE, "Unable to allocate memory") + +/*! \brief vCard template item. + SLL. All of relations tag<->LDAP attr read from template @@ -1818,6 +1815,7 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_vcard.c jabberd-2.1.14/sm/v2l_vcard.c +}; + +/* public api */ ++ +xmlnode +v2l_vcard_get (v2l_Config *self, v2l_LdapConn *curr_conn) +{ @@ -1925,7 +1923,7 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_vcard.c jabberd-2.1.14/sm/v2l_vcard.c + + for (stag = (char **) _V2L_MAP_VCARD; *stag != NULL; stag++) + { -+ tmp = strchr(*stag, '/'); ++ tmp = strchr (*stag, '/'); + + if (tmp == NULL) + { @@ -1934,13 +1932,13 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_vcard.c jabberd-2.1.14/sm/v2l_vcard.c + } + else + { -+ sprintf(group, "%.*s", tmp - *stag, *stag); ++ sprintf (group, "%.*s", tmp - *stag, *stag); + tmp++; + } + + tag = xmlnode_get_tag (tpl, *stag); + -+ if (xmlnode_get_data(tag) != NULL) ++ if (xmlnode_get_data (tag) != NULL) + { + int ntags = 0; + char find_attr[30]; @@ -1950,10 +1948,10 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_vcard.c jabberd-2.1.14/sm/v2l_vcard.c + v2l_vCardItem *ptr; + + ptr = (v2l_vCardItem *) pmalloc (self->poolref, -+ sizeof(v2l_vCardItem)); ++ sizeof (v2l_vCardItem)); + + ptr->vcard = tmp; -+ ptr->ldap = pstrdup (self->poolref, xmlnode_get_data(tag)); ++ ptr->ldap = pstrdup (self->poolref, xmlnode_get_data (tag)); + ptr->next = NULL; + ptr->group = group[0] == 0 ? NULL : + pstrdup (self->poolref, group); @@ -1972,7 +1970,7 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_vcard.c jabberd-2.1.14/sm/v2l_vcard.c + sprintf (find_attr, "%s?v2ln=%d", *stag, ++ntags); + tag = xmlnode_get_tag (tpl, find_attr); + } while (tag && xmlnode_get_data (tag) != NULL && ntags < 10); -+ }/* xmlnode_get_data(tag) != NULL */ ++ }/* xmlnode_get_data (tag) != NULL */ + } /* for loop, all tags in template */ + + xmlnode_free (tpl); @@ -2114,7 +2112,7 @@ diff -Naurb jabberd-2.1.14.orig/sm/v2l_vcard.c jabberd-2.1.14/sm/v2l_vcard.c + + if (node == NULL) + { -+ node = xmlnode_insert_tag(res, item->group); ++ node = xmlnode_insert_tag (res, item->group); + } + } + else diff --git a/src/v2l_config.c b/src/v2l_config.c index f9fb12c..31c1bad 100644 --- a/src/v2l_config.c +++ b/src/v2l_config.c @@ -39,6 +39,8 @@ typedef pool_t pool; */ static char *_v2l_config_get_tag (T_CONF conn_base, const char *tag); +/* public api */ + int v2l_config_init (v2l_Config *self, T_CONF cfgroot) { @@ -47,7 +49,7 @@ v2l_config_init (v2l_Config *self, T_CONF cfgroot) if (!cfgroot) { - log_error(ZONE, "xdb_v2l configuration not present"); + log_error (ZONE, "xdb_v2l configuration not present"); return 0; } @@ -57,7 +59,7 @@ v2l_config_init (v2l_Config *self, T_CONF cfgroot) if (conn_base == NULL) { - log_error(ZONE," tag is not present"); + log_error (ZONE," tag is not present"); return 0; } #else diff --git a/src/v2l_conn.c b/src/v2l_conn.c index a4b9c2b..abd8c96 100644 --- a/src/v2l_conn.c +++ b/src/v2l_conn.c @@ -41,7 +41,7 @@ typedef pool_t pool; /*! How often thread checks for LDAP results, in seconds */ #define V2L_POLL_INTERVAL 1 -#define LOG_ERROR_MEM log_error(ZONE, "Unable to allocate memory") +#define LOG_ERROR_MEM log_error (ZONE, "Unable to allocate memory") /*! Global hashtable of all currently active LDAP connections */ static xht V2L_CONN_LIST = NULL; @@ -69,7 +69,7 @@ static v2l_LdapConn *_v2l_create_conn (char *host, int port, const char *binddn, \pre self is valid, user is not NULL \return 1 if no error, otherwise 0. */ -static int _v2l_ldap_get_credentials(v2l_Config *self, const char *user, +static int _v2l_ldap_get_credentials (v2l_Config *self, const char *user, char **passwd, char **cn); /*! \brief Closes and frees a connection. @@ -120,13 +120,14 @@ static char *_v2l_ldap_filter (v2l_Config *self, const char *user); /*! \brief Waits for LDAP results. Operations on the directory are asynchronous. This is a sync wait function. - \param \param[in,out] evt_res Control and info parameter. + \param[in,out] evt_res Control and info parameter. */ static void _v2l_ldap_sync (v2l_LdapEvt *evt_res); /*! \brief Adds LDAP attr to request. Utility function, hides OpenLDAP API to upper level. - \param[in,out] req The SLL of LDAP requests. + \pre Attribute is not NULL. + \param[in,out] req The SLL of LDAP requests. If NULL new list is returned. \param attr The attr, LDAPMod pointer. \return The list of requests + the last added. The list of request if error. */ @@ -176,7 +177,7 @@ static void _v2l_free_expired_walker (xht h, const char *key, void *val, void *arg); /*! A thread for active wait for LDAP results */ -static int _v2l_ldap_wait_callback(void *arg); +static int _v2l_ldap_wait_callback (void *arg); #ifdef _V2L_JABBER2 static void *_v2l_ldap_wait_callback_g (void *arg); @@ -191,7 +192,7 @@ v2l_get_conn (v2l_Config *self, const char *user) user_conn = (v2l_LdapConn *) xhash_get (V2L_CONN_LIST, user); - if(user_conn == NULL) + if (user_conn == NULL) { char *passwd, *cn, *binddn; @@ -268,7 +269,7 @@ v2l_ldap_get_entry (v2l_Config *self, v2l_LdapConn *curr_conn) return NULL; } - _v2l_ldap_sync(evt_res); + _v2l_ldap_sync (evt_res); if (ldap_count_entries (evt_res->ld, evt_res->result) != 1) { @@ -286,7 +287,7 @@ v2l_request_record (v2l_Config *self, v2l_LdapConn *curr_conn, { LDAPMod **attrs; int i, nbmod, ret; - v2l_LdapRequest *cur_req, *cur_temp; + v2l_LdapRequest *cur_req; v2l_LdapEvt *evt_res; if (req == NULL) @@ -337,7 +338,7 @@ v2l_request_record (v2l_Config *self, v2l_LdapConn *curr_conn, { memcpy (attrs[i], cur_req->attr, sizeof (LDAPMod)); log_debug (ZONE, "Element \"%s\" (%d) in the LDAP request: %s", - attrs[i]->mod_type, i, attrs[i]->mod_values[0]); + attrs[i]->mod_type, i, attrs[i]->mod_values[0]); } attrs[nbmod] = NULL; @@ -362,16 +363,13 @@ v2l_request_record (v2l_Config *self, v2l_LdapConn *curr_conn, ldap_msgfree (evt_res->result); free (evt_res); - for (cur_req = req, i = 0; i < nbmod; i++) + for (cur_req = req, i = 0; i < nbmod; cur_req = cur_req->next, i++) { - cur_temp = cur_req; - cur_req = cur_req->next; - free (attrs[i]); - free (cur_temp->attr->mod_values[0]); - free (cur_temp->attr->mod_values); - free (cur_temp->attr); - free (cur_temp); + free (cur_req->attr->mod_values[0]); + free (cur_req->attr->mod_values); + free (cur_req->attr); + free (cur_req); } free (attrs); @@ -417,6 +415,48 @@ v2l_add_attr_str (v2l_LdapRequest *req, const char *attr, const char *str) return _v2l_add_attr (req, mod); } +void +v2l_ldap_for_all_attrs (v2l_AttrValueFunction value_func, + v2l_AttrMatchFunction match_func, void *pointer, v2l_LdapEvt *evt_res) +{ + LDAPMessage *current_result; + BerElement *ber; + char *current_attr, **vals; + void *shrdata; + + current_result = ldap_first_entry (evt_res->ld, evt_res->result); + current_attr = ldap_first_attribute (evt_res->ld, current_result, &ber); + + /* step through each attribute in objectclass */ + for (; + current_attr != NULL; + current_attr = ldap_next_attribute (evt_res->ld, current_result, ber)) + { + + if (match_func (current_attr, &shrdata)) + { + vals = ldap_get_values (evt_res->ld, current_result, current_attr); + value_func (current_attr, (const char **) vals, pointer, shrdata); + + if (vals != NULL) + { + ldap_value_free (vals); + } + } + + ldap_memfree (current_attr); + } /* attributes loop */ + + if (ber != NULL) + { + ber_free (ber, 0); + } + + /* don't forget to free the next attribute */ + ldap_memfree (current_attr); + ldap_msgfree (evt_res->result); +} + /* public api ends here */ static v2l_LdapConn * @@ -466,7 +506,7 @@ _v2l_create_conn (char *host, int port, const char *binddn, const char *user, { LOG_ERROR_MEM; ldap_unbind_s (ld); - pool_free(poolref); + pool_free (poolref); return NULL; } @@ -480,7 +520,7 @@ _v2l_create_conn (char *host, int port, const char *binddn, const char *user, { LOG_ERROR_MEM; ldap_unbind_s (ld); - pool_free(poolref); + pool_free (poolref); return NULL; } @@ -492,7 +532,7 @@ _v2l_create_conn (char *host, int port, const char *binddn, const char *user, { LOG_ERROR_MEM; ldap_unbind_s (ld); - pool_free(poolref); + pool_free (poolref); return NULL; } @@ -505,7 +545,7 @@ _v2l_create_conn (char *host, int port, const char *binddn, const char *user, { LOG_ERROR_MEM; ldap_unbind_s (ld); - pool_free(poolref); + pool_free (poolref); return NULL; } @@ -526,6 +566,12 @@ _v2l_ldap_filter (v2l_Config *self, const char *user) { char *filter; + if (self->filter == NULL || user == NULL) + { + log_error (ZONE, "Attempt to make a NULL filter"); + return NULL; + } + filter = (char *) malloc (strlen (self->filter) + strlen (user)); if (filter == NULL) @@ -534,7 +580,7 @@ _v2l_ldap_filter (v2l_Config *self, const char *user) return NULL; } - sprintf(filter, self->filter, user); + sprintf (filter, self->filter, user); return filter; } @@ -548,7 +594,7 @@ _v2l_ldap_get_credentials (v2l_Config *self, const char *user, char **passwd, char *filter, **vpw, **vcn; char *attrs[3] = {"userPassword", "cn", NULL}; - evt_res = (v2l_LdapEvt *) malloc(sizeof(v2l_LdapEvt)); + evt_res = (v2l_LdapEvt *) malloc (sizeof (v2l_LdapEvt)); if (evt_res == NULL) { @@ -565,7 +611,7 @@ _v2l_ldap_get_credentials (v2l_Config *self, const char *user, char **passwd, if (evt_res->rc != LDAP_SUCCESS) { - log_error(ZONE, + log_error (ZONE, "LDAP error attempting to retrieve \"%s\"'s password: %s", user, ldap_err2string (evt_res->rc)); free (evt_res); @@ -577,9 +623,9 @@ _v2l_ldap_get_credentials (v2l_Config *self, const char *user, char **passwd, *passwd = NULL; *cn = NULL; - if (ldap_count_entries(evt_res->ld, evt_res->result) == 1) + if (ldap_count_entries (evt_res->ld, evt_res->result) == 1) { - e = ldap_first_entry(evt_res->ld, evt_res->result); + e = ldap_first_entry (evt_res->ld, evt_res->result); vpw = ldap_get_values (evt_res->ld, e, "userPassword"); @@ -602,7 +648,7 @@ _v2l_ldap_get_credentials (v2l_Config *self, const char *user, char **passwd, return 0; } - *passwd = (char *) malloc (strlen(vpw[0]) + 1); + *passwd = (char *) malloc (strlen (vpw[0]) + 1); if (*passwd == NULL) { @@ -617,7 +663,7 @@ _v2l_ldap_get_credentials (v2l_Config *self, const char *user, char **passwd, strcpy (*passwd, vpw[0]); ldap_value_free (vpw); - *cn = (char *) malloc (strlen(vcn[0]) + 1); + *cn = (char *) malloc (strlen (vcn[0]) + 1); if (*cn == NULL) { @@ -673,7 +719,7 @@ _v2l_ldap_sync (v2l_LdapEvt *evt_res) pthread_t thr; int rc; - rc = pthread_create(&thr, NULL, _v2l_ldap_wait_callback_g, (void *) evt_res); + rc = pthread_create (&thr, NULL, _v2l_ldap_wait_callback_g, (void *) evt_res); if (rc != 0) { @@ -681,7 +727,7 @@ _v2l_ldap_sync (v2l_LdapEvt *evt_res) return; } - pthread_join(thr, NULL); + pthread_join (thr, NULL); #endif } @@ -689,10 +735,9 @@ _v2l_ldap_sync (v2l_LdapEvt *evt_res) static int _v2l_count_attrs (v2l_LdapRequest *req) { - v2l_LdapRequest *ptr; int nbmod; - for (nbmod = 0, ptr = req; ptr != NULL; ptr = ptr->next, nbmod++); + for (nbmod = 0; req != NULL; req = req->next, nbmod++); return nbmod; } @@ -700,43 +745,32 @@ _v2l_count_attrs (v2l_LdapRequest *req) static v2l_LdapRequest * _v2l_add_attr (v2l_LdapRequest *req, LDAPMod *attr) { + v2l_LdapRequest *new_req, *ptr; + if (attr == NULL) { log_warn (ZONE, "LDAP attribute is NULL? I cannot add anything"); return NULL; } - if (req == NULL) + new_req = (v2l_LdapRequest *) malloc (sizeof (v2l_LdapRequest)); + + if (new_req == NULL) { - req = (v2l_LdapRequest *) malloc (sizeof (v2l_LdapRequest)); + LOG_ERROR_MEM; + return NULL; + } - if (req == NULL) - { - LOG_ERROR_MEM; - return NULL; - } + new_req->attr = attr; + new_req->next = NULL; - req->attr = attr; - req->next = NULL; + if (req == NULL) + { + req = new_req; } else { - v2l_LdapRequest *ptr; - v2l_LdapRequest *new_req; - - new_req = (v2l_LdapRequest *) malloc (sizeof (v2l_LdapRequest)); - - if (new_req == NULL) - { - LOG_ERROR_MEM; - return NULL; - } - - new_req->attr = attr; - new_req->next = NULL; - for (ptr = req; ptr->next != NULL; ptr = ptr->next); - ptr->next = new_req; } @@ -744,7 +778,7 @@ _v2l_add_attr (v2l_LdapRequest *req, LDAPMod *attr) } static int -_v2l_ldap_wait_callback(void *arg) +_v2l_ldap_wait_callback (void *arg) { v2l_LdapEvt *evt_res = (v2l_LdapEvt *) arg; LDAPMessage *result; @@ -754,8 +788,8 @@ _v2l_ldap_wait_callback(void *arg) if (rc == -1) { - log_error(ZONE, "LDAP result error %s", - ldap_err2string(rc)); + log_error (ZONE, "LDAP result error %s", + ldap_err2string (rc)); evt_res->result = NULL; evt_res->rc = -1; return 1; @@ -865,11 +899,11 @@ _v2l_add_conn (v2l_LdapConn *ldap_conn) pth_spawn (attr, _v2l_purge_conn_callback, NULL); pth_attr_destroy (attr); #else - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + pthread_attr_init (&attr); + pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); rc = pthread_create (&thr, &attr, _v2l_purge_conn_callback, NULL); - pthread_attr_destroy(&attr); + pthread_attr_destroy (&attr); if (rc != 0) { @@ -880,45 +914,3 @@ _v2l_add_conn (v2l_LdapConn *ldap_conn) xhash_put (V2L_CONN_LIST, ldap_conn->user, (void *) ldap_conn); } - -void -v2l_ldap_for_all_attrs(v2l_AttrValueFunction value_func, - v2l_AttrMatchFunction match_func, void *pointer, v2l_LdapEvt *evt_res) -{ - LDAPMessage *current_result; - BerElement *ber; - char *current_attr, **vals; - void *shrdata; - - current_result = ldap_first_entry (evt_res->ld, evt_res->result); - current_attr = ldap_first_attribute (evt_res->ld, current_result, &ber); - - /* step through each attribute in objectclass */ - for (; - current_attr != NULL; - current_attr = ldap_next_attribute (evt_res->ld, current_result, ber)) - { - - if (match_func (current_attr, &shrdata)) - { - vals = ldap_get_values (evt_res->ld, current_result, current_attr); - value_func (current_attr, (const char **) vals, pointer, shrdata); - - if (vals != NULL) - { - ldap_value_free (vals); - } - } - - ldap_memfree (current_attr); - } /* attributes loop */ - - if (ber != NULL) - { - ber_free (ber, 0); - } - - /* don't forget to free the next attribute */ - ldap_memfree (current_attr); - ldap_msgfree (evt_res->result); -} diff --git a/src/v2l_main.c b/src/v2l_main.c index 60f505f..cf8cda9 100644 --- a/src/v2l_main.c +++ b/src/v2l_main.c @@ -54,7 +54,7 @@ #include #include -#define LOG_ERROR_MEM log_error(ZONE, "Unable to allocate memory") +#define LOG_ERROR_MEM log_error (ZONE, "Unable to allocate memory") static result _v2l_packets_handler (instance i, dpacket p, void *args); static int _v2l_check_attr_value (xmlnode node, char *attr_name, @@ -193,7 +193,7 @@ _v2l_check_attr_value (xmlnode node, char *attr_name, char *value) { if ((node == NULL) || (attr_name == NULL) || (value == NULL)) { - log_debug (ZONE, "_v2l_check_attr_value() parameters are not valid"); + log_debug (ZONE, "_v2l_check_attr_value () parameters are not valid"); return 0; } diff --git a/src/v2l_vcard.c b/src/v2l_vcard.c index 9fb6037..ca9eef5 100644 --- a/src/v2l_vcard.c +++ b/src/v2l_vcard.c @@ -33,7 +33,7 @@ #endif #include -#define LOG_ERROR_MEM log_error(ZONE, "Unable to allocate memory") +#define LOG_ERROR_MEM log_error (ZONE, "Unable to allocate memory") /*! \brief vCard template item. SLL. All of relations tag<->LDAP attr read from template @@ -180,6 +180,7 @@ static const char *_V2L_MAP_VCARD [] = { }; /* public api */ + xmlnode v2l_vcard_get (v2l_Config *self, v2l_LdapConn *curr_conn) { @@ -287,7 +288,7 @@ _v2l_vcard_map (v2l_Config *self) for (stag = (char **) _V2L_MAP_VCARD; *stag != NULL; stag++) { - tmp = strchr(*stag, '/'); + tmp = strchr (*stag, '/'); if (tmp == NULL) { @@ -296,13 +297,13 @@ _v2l_vcard_map (v2l_Config *self) } else { - sprintf(group, "%.*s", tmp - *stag, *stag); + sprintf (group, "%.*s", tmp - *stag, *stag); tmp++; } tag = xmlnode_get_tag (tpl, *stag); - if (xmlnode_get_data(tag) != NULL) + if (xmlnode_get_data (tag) != NULL) { int ntags = 0; char find_attr[30]; @@ -312,10 +313,10 @@ _v2l_vcard_map (v2l_Config *self) v2l_vCardItem *ptr; ptr = (v2l_vCardItem *) pmalloc (self->poolref, - sizeof(v2l_vCardItem)); + sizeof (v2l_vCardItem)); ptr->vcard = tmp; - ptr->ldap = pstrdup (self->poolref, xmlnode_get_data(tag)); + ptr->ldap = pstrdup (self->poolref, xmlnode_get_data (tag)); ptr->next = NULL; ptr->group = group[0] == 0 ? NULL : pstrdup (self->poolref, group); @@ -334,7 +335,7 @@ _v2l_vcard_map (v2l_Config *self) sprintf (find_attr, "%s?v2ln=%d", *stag, ++ntags); tag = xmlnode_get_tag (tpl, find_attr); } while (tag && xmlnode_get_data (tag) != NULL && ntags < 10); - }/* xmlnode_get_data(tag) != NULL */ + }/* xmlnode_get_data (tag) != NULL */ } /* for loop, all tags in template */ xmlnode_free (tpl); @@ -476,7 +477,7 @@ _v2l_ldap2vcard_generic (v2l_vCardItem *item, const char **vals, xmlnode res) if (node == NULL) { - node = xmlnode_insert_tag(res, item->group); + node = xmlnode_insert_tag (res, item->group); } } else diff --git a/tools/Vcard2Ldap.pm b/tools/Vcard2Ldap.pm index 474175b..3f1deea 100644 --- a/tools/Vcard2Ldap.pm +++ b/tools/Vcard2Ldap.pm @@ -2,7 +2,7 @@ package Vcard2Ldap; =head1 NAME -Vcard2Ldap - Stuff for convert LDAP entries to Jabberd roster ones. +Vcard2Ldap - Stuff for convert LDAP entries to Jabberd roster ones. =head1 SYNOPSIS @@ -55,10 +55,11 @@ Returns the "vCard map". This hash maps XML vCard tags to LDAP attributes. =cut #### CONFIG -my $vcardtpl = "../ldap/vcard.xml"; our $jabberd = "jabber.domain.com"; our $groupname = "Iris"; +my $vcardtpl = "../ldap/vcard.xml"; + my $basedn = "dc=nodomain"; my $ldapserver = "localhost"; my $disname="cn=admin,dc=nodomain"; @@ -66,6 +67,9 @@ my $ldappass= "secret"; my $ldapperson = "irisPerson"; my $ldapgroup = "ou"; +my $ldapjidattr = "irisUserPresenceID"; +my $ldapjidmatch = qr/urn:mace:rediris.es:presence:xmpp:([\w\-_\.]+@.+)/; + ########################## use strict; @@ -115,7 +119,22 @@ sub jabber_get_users { sub jabber_jid { - return $_->get_value ("cn") . '@' . $jabberd; + my $user = shift; + my $jid; + + foreach (@{$user->get_value ($ldapjidattr, asref => 1)}) { + if (length ($ldapjidmatch) == 0) { + return $_; + } else { + $jid = $_ =~ $ldapjidmatch; + + if (length ($jid) != 0) { + return $jid; + } + } + } + + die user->get_value ("cn") . ": user with no jid!"; } sub vcard_get_map { -- 2.11.4.GIT