More sophisticated selection of the user jid in Vcard2Ldap. Minor fixes.
[vcard2ldap.git] / src / v2l_main.c
blobcf8cda9e7685c53d7cbe7bb9ebad6d1e00649013
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 /*! \file v2l_main.c
18 \brief Module skeleton.
19 \mainpage
20 \dot
21 digraph Packages {
22 node [shape = plaintext, fontname = Helvetica, fontsize = 12]
23 server [shape = egg, label = jabberd ]
24 vcard [
25 label =<<table href="v2l__vcard_8h.html">
26 <tr><td bgcolor="gray">v2l_vcard</td></tr>
27 <tr><td>XML vCard Handling</td></tr>
28 </table>>
30 config [
31 label =<<table href="v2l__config_8h.html">
32 <tr><td bgcolor="gray">v2l_config</td></tr>
33 <tr><td>Config handling package</td></tr>
34 </table>>
36 conn [
37 label =<<table href="v2l__conn_8h.html">
38 <tr><td bgcolor="gray">v2l_conn</td></tr>
39 <tr><td>LDAP-level package</td></tr>
40 </table>>
42 config -> server
43 vcard -> config
44 vcard -> conn
45 conn -> config
46 server -> vcard
48 \enddot
51 #include <stdlib.h>
52 #include <jabberd.h>
53 #include <v2l_config.h>
54 #include <v2l_conn.h>
55 #include <v2l_vcard.h>
57 #define LOG_ERROR_MEM log_error (ZONE, "Unable to allocate memory")
59 static result _v2l_packets_handler (instance i, dpacket p, void *args);
60 static int _v2l_check_attr_value (xmlnode node, char *attr_name,
61 char *attr_value);
62 static void _v2l_shutdown (void *arg);
64 /*! \brief Module entry point.
65 See jabberd14 API docs for details.
67 #ifdef __cplusplus
68 extern "C"
69 #endif
70 void
71 xdb_v2l (instance i, xmlnode x)
73 xdbcache xc = NULL; /* for config request */
74 v2l_Config *self = NULL;
76 log_debug (ZONE, "Loading xdb_v2l");
78 self = (v2l_Config *) pmalloco (i->p, sizeof (*self));
80 if (self == NULL)
82 return;
85 self->poolref = i->p;
87 /* Load config from xdb */
88 xc = xdb_cache (i);
89 self->config = xdb_get (xc, jid_new (xmlnode_pool (x), "config@-internal"),
90 "jabberd:xdb_v2l:config");
92 /* Parse config */
93 if (!v2l_config_init (self, self->config))
95 /* do no try to go on with a badly configured ldap component */
96 exit (1);
99 register_phandler (i, o_DELIVER, _v2l_packets_handler, (void *) self);
100 register_shutdown (_v2l_shutdown, (void *) self);
102 log_debug (ZONE, "xdb_v2l has been successfully initialized");
105 /*! \brief shutdown and cleanup callback
106 See jabberd14 API docs for details.
108 static void
109 _v2l_shutdown (void *arg)
111 v2l_config_shutdown ((v2l_Config *) arg);
114 /*! \brief Main callback. Handle xdb packets
115 See jabberd14 API docs for details.
117 static result
118 _v2l_packets_handler (instance i, dpacket p, void *args)
120 v2l_Config *self = (v2l_Config *) args;
121 v2l_LdapConn *user_conn;
122 dpacket dp;
124 if ((p == NULL) || (p->x == NULL) || (p->id == NULL))
126 log_error (ZONE, "malformed xdb packet");
127 return r_ERR;
130 if (_v2l_check_attr_value (p->x, "type", "error"))
132 xmlnode_free (p->x);
133 log_warn (ZONE, "xdb_v2l received an error packet");
134 return r_DONE;
137 if (!_v2l_check_attr_value (p->x, "ns", NS_VCARD))
139 log_warn (ZONE,
140 "xdb_v2l received a packet for other namespace than NS_VCARD");
141 return r_PASS;
144 user_conn = v2l_get_conn (self, p->id->user);
146 if (user_conn == NULL)
148 log_error (ZONE, "Unable to create connection for \"%s\" user",
149 p->id->user);
150 return r_ERR;
153 if (_v2l_check_attr_value (p->x, "type", "set"))
155 xmlnode child = xmlnode_get_firstchild (p->x);
157 if (v2l_vcard_set (self, user_conn, child) != 1)
159 return r_ERR;
162 else
164 xmlnode data = v2l_vcard_get (self, user_conn);
166 if (data)
168 xmlnode_insert_tag_node (p->x, data);
169 xmlnode_free (data);
173 /* making XML reply */
174 xmlnode_put_attrib (p->x, "type", "result");
175 xmlnode_put_attrib (p->x, "to", xmlnode_get_attrib (p->x, "from"));
176 xmlnode_put_attrib (p->x, "from", jid_full (p->id));
178 dp = dpacket_new (p->x);
180 if (dp == NULL)
182 LOG_ERROR_MEM;
183 return r_ERR;
186 deliver (dp, NULL);
188 return r_DONE;
191 static int
192 _v2l_check_attr_value (xmlnode node, char *attr_name, char *value)
194 if ((node == NULL) || (attr_name == NULL) || (value == NULL))
196 log_debug (ZONE, "_v2l_check_attr_value () parameters are not valid");
197 return 0;
200 return strncmp (xmlnode_get_attrib (node, attr_name), value,
201 strlen (value)) == 0;