Reads LDAP<->vCard bindings in an external file.
[vcard2ldap.git] / src / v2l_main.c
blobbbafc0ffc1317e8c373443cf19f88e5173187411
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 #include <stdlib.h>
18 #include <stdio.h>
19 #include <jabberd.h>
20 #include <v2l_config.h>
21 #include <v2l_conn.h>
22 #include <v2l_vcard.h>
24 #if LDAP_VENDOR_VERSION <= 20130
25 #include <sasl/sasl.h>
26 #endif
28 #define LOG_ERROR_MEM log_error(ZONE, "Unable to allocate memory")
30 static result _v2l_packets_handler (instance i, dpacket p, void *args);
31 static short _v2l_check_attr_value (xmlnode node, char *attr_name,
32 char *attr_value);
33 static void _v2l_shutdown (void *arg);
36 * xdb_v2l (), module entry point.
38 #ifdef __cplusplus
39 extern "C"
40 #endif
41 void xdb_v2l (instance i, xmlnode x)
43 xdbcache xc = NULL; /* for config request */
44 v2l_Config *self = NULL;
46 log_debug (ZONE, "Loading xdb_v2l");
48 self = (v2l_Config *) pmalloco (i->p, sizeof (*self));
50 if (self == NULL)
52 return;
55 self->poolref = i->p;
57 /* Load config from xdb */
58 xc = xdb_cache (i);
59 self->config = xdb_get (xc, jid_new (xmlnode_pool (x), "config@-internal"),
60 "jabberd:xdb_v2l:config");
62 /* Parse config */
63 if (!v2l_config_init (self, self->config))
65 /* do no try to go on with a badly configured ldap component */
66 exit (1);
69 register_phandler (i, o_DELIVER, _v2l_packets_handler, (void *) self);
70 register_shutdown (_v2l_shutdown, (void *) self);
72 log_debug (ZONE, "xdb_v2l has been successfully initialized");
75 static void
76 _v2l_shutdown (void *arg)
78 v2l_Config *self = (v2l_Config *) arg;
80 v2l_free_allconn ();
82 /* free admin connection */
83 ldap_unbind (self->master_conn->ld);
84 pool_free (self->master_conn->p);
85 pool_free (self->poolref);
86 #if LDAP_VENDOR_VERSION <= 20130
87 sasl_done ();
88 #endif
92 * Main callback. Handle xdb packets
94 static result
95 _v2l_packets_handler (instance i, dpacket p, void *args)
97 v2l_Config *self = (v2l_Config *) args;
98 v2l_LdapConn *user_conn;
99 dpacket dp;
101 if ((p == NULL) || (p->x == NULL) || (p->id == NULL))
103 log_error (ZONE, "malformed xdb packet");
104 return r_ERR;
107 if (_v2l_check_attr_value (p->x, "type", "error"))
109 xmlnode_free (p->x);
110 log_warn (ZONE, "xdb_v2l received an error packet");
111 return r_DONE;
114 if (!_v2l_check_attr_value (p->x, "ns", NS_VCARD))
116 log_warn (ZONE,
117 "xdb_v2l received a packet for other namespace than NS_VCARD");
118 return r_PASS;
121 user_conn = v2l_get_conn (self, p->id->user);
123 if (user_conn == NULL)
125 log_error (ZONE, "Unable to create connection for \"%s\" user",
126 p->id->user);
127 return r_ERR;
130 if (_v2l_check_attr_value (p->x, "type", "set"))
132 xmlnode child = xmlnode_get_firstchild (p->x);
134 if (v2l_vcard_set (self, user_conn, child) != 1)
136 return r_ERR;
139 else
141 xmlnode data = v2l_vcard_get (self, user_conn);
143 if (data)
145 xmlnode_insert_tag_node (p->x, data);
146 xmlnode_free (data);
150 /* making XML reply */
151 xmlnode_put_attrib (p->x, "type", "result");
152 xmlnode_put_attrib (p->x, "to", xmlnode_get_attrib (p->x, "from"));
153 xmlnode_put_attrib (p->x, "from", jid_full (p->id));
155 dp = dpacket_new (p->x);
157 if (dp == NULL)
159 LOG_ERROR_MEM;
160 return r_ERR;
163 deliver (dp, NULL);
165 return r_DONE;
168 static short
169 _v2l_check_attr_value (xmlnode node, char *attr_name, char *value)
171 if ((node == NULL) || (attr_name == NULL) || (value == NULL))
173 log_debug (ZONE, "_v2l_check_attr_value() parameters are not valid");
174 return 0;
177 return strncmp (xmlnode_get_attrib (node, attr_name), value,
178 sizeof (char) * strlen (value)) == 0;