src/v2l_main.c

Go to the documentation of this file.
00001 /*
00002  *  This program is free software; you can redistribute it and/or modify
00003  *  it under the terms of the GNU General Public License as published by
00004  *  the Free Software Foundation; either version 2 of the License, or
00005  *  (at your option) any later version.
00006  *
00007  *  This program is distributed in the hope that it will be useful,
00008  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00009  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010  *  GNU General Public License for more details.
00011  *
00012  *  You should have received a copy of the GNU General Public License
00013  *  along with this program; if not, write to the Free Software
00014  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00015  */
00016 
00051 #include <stdlib.h>
00052 #include <jabberd.h>
00053 #include <v2l_config.h>
00054 #include <v2l_conn.h>
00055 #include <v2l_vcard.h>
00056 
00057 #define LOG_ERROR_MEM log_error (ZONE, "Unable to allocate memory")
00058 
00059 static result _v2l_packets_handler (instance i, dpacket p, void *args);
00060 static int _v2l_check_attr_value (xmlnode node, char *attr_name,
00061   char *attr_value);
00062 static void _v2l_shutdown (void *arg);
00063 
00067 #ifdef __cplusplus
00068 extern "C"
00069 #endif
00070 void
00071 xdb_v2l (instance i, xmlnode x)
00072 {
00073   xdbcache xc = NULL;    /* for config request */
00074   v2l_Config *self = NULL;
00075 
00076   log_debug (ZONE, "Loading xdb_v2l");
00077 
00078   self = (v2l_Config *) pmalloco (i->p, sizeof (*self));
00079 
00080   if (self == NULL)
00081     {
00082       return;
00083     }
00084 
00085   self->poolref = i->p;
00086 
00087   /* Load config from xdb */
00088   xc = xdb_cache (i);
00089   self->config = xdb_get (xc, jid_new (xmlnode_pool (x), "config@-internal"),
00090     "jabberd:xdb_v2l:config");
00091 
00092   /* Parse config */
00093   if (!v2l_config_init (self, self->config))
00094     {
00095       /* do no try to go on with a badly configured ldap component */
00096       exit (1);
00097     }
00098 
00099   register_phandler (i, o_DELIVER, _v2l_packets_handler, (void *) self);
00100   register_shutdown (_v2l_shutdown, (void *) self);
00101 
00102   log_debug (ZONE, "xdb_v2l has been successfully initialized");
00103 }
00104 
00108 static void
00109 _v2l_shutdown (void *arg)
00110 {
00111   v2l_config_shutdown ((v2l_Config *) arg);
00112 }
00113 
00117 static result
00118 _v2l_packets_handler (instance i, dpacket p, void *args)
00119 {
00120   v2l_Config *self = (v2l_Config *) args;
00121   v2l_LdapConn *user_conn;
00122   dpacket dp;
00123 
00124   if ((p == NULL) || (p->x == NULL) || (p->id == NULL))
00125     {
00126       log_error (ZONE, "malformed xdb packet");
00127       return r_ERR;
00128     }
00129 
00130   if (_v2l_check_attr_value (p->x, "type", "error"))
00131     {
00132       xmlnode_free (p->x);
00133       log_warn (ZONE, "xdb_v2l received an error packet");
00134       return r_DONE;
00135     }
00136 
00137   if (!_v2l_check_attr_value (p->x, "ns", NS_VCARD))
00138     {
00139       log_warn (ZONE,
00140         "xdb_v2l received a packet for other namespace than NS_VCARD");
00141       return r_PASS;
00142     }
00143 
00144   user_conn = v2l_get_conn (self, p->id->user);
00145 
00146   if (user_conn == NULL)
00147     {
00148       log_error (ZONE, "Unable to create connection for \"%s\" user",
00149         p->id->user);
00150       return r_ERR;
00151     }
00152 
00153   if (_v2l_check_attr_value (p->x, "type", "set"))
00154     {
00155       xmlnode child = xmlnode_get_firstchild (p->x);
00156 
00157       if (v2l_vcard_set (self, user_conn, child) != 1)
00158         {
00159           return r_ERR;
00160         }
00161     }
00162   else
00163     {
00164       xmlnode data = v2l_vcard_get (self, user_conn);
00165 
00166       if (data)
00167         {
00168           xmlnode_insert_tag_node (p->x, data);
00169           xmlnode_free (data);
00170         }
00171     }
00172 
00173   /* making XML reply */
00174   xmlnode_put_attrib (p->x, "type", "result");
00175   xmlnode_put_attrib (p->x, "to", xmlnode_get_attrib (p->x, "from"));
00176   xmlnode_put_attrib (p->x, "from", jid_full (p->id));
00177 
00178   dp = dpacket_new (p->x);
00179 
00180   if (dp == NULL)
00181     {
00182       LOG_ERROR_MEM;
00183       return r_ERR;
00184     }
00185 
00186   deliver (dp, NULL);
00187 
00188   return r_DONE;
00189 }
00190 
00191 static int
00192 _v2l_check_attr_value (xmlnode node, char *attr_name, char *value)
00193 {
00194   if ((node == NULL) || (attr_name == NULL) || (value == NULL))
00195     {
00196       log_debug (ZONE, "_v2l_check_attr_value () parameters are not valid");
00197       return 0;
00198     }
00199 
00200   return strncmp (xmlnode_get_attrib (node, attr_name), value,
00201       strlen (value)) == 0;
00202 }

Generated on Sun Oct 7 14:38:12 2007 for vCard2LDAP by  doxygen 1.5.1