Script for populate rosters. Bug fixed (xmlfile works now in jabberd2)
[vcard2ldap.git] / src / v2l_config.c
blobbf26637a6ea8260da3c72423a1a4115142340351
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 <strings.h>
20 #ifndef _V2L_JABBER2
21 #include <jabberd.h>
22 #else
23 #include "../util/util.h"
25 #define log_warn log_debug
26 #define log_error log_debug
27 typedef pool_t pool;
28 #endif
30 #include <v2l_config.h>
31 #include <v2l_conn.h>
33 #if LDAP_VENDOR_VERSION <= 20130
34 #include <sasl/sasl.h>
35 #endif
37 static char *_v2l_config_get_tag (T_CONF conn_base, const char *tag);
39 int
40 v2l_config_init (v2l_Config *self, T_CONF cfgroot)
42 T_CONF conn_base;
43 const char *portstr;
45 if (!cfgroot)
47 log_error(ZONE, "xdb_v2l configuration not present");
48 return 0;
51 #ifndef _V2L_JABBER2
52 /* Have a look in the XML configuration data for connection information. */
53 conn_base = xmlnode_get_tag (cfgroot, "connection");
55 if (conn_base == NULL)
57 log_error(ZONE,"<connection> tag is not present");
58 return 0;
60 #else
61 conn_base = cfgroot;
62 #endif
64 self->host = _v2l_config_get_tag (conn_base, "host");
66 if (self->host == NULL)
68 log_error (ZONE, "LDAP server is not specified");
69 return 0;
72 portstr = _v2l_config_get_tag (conn_base, "port");
73 self->port = portstr != NULL ? atoi (portstr) : LDAP_PORT;
75 self->suffix = _v2l_config_get_tag (conn_base, "suffix");
77 if (self->suffix == NULL)
79 log_error (ZONE, "LDAP suffix is not specified");
80 return 0;
83 self->uniqattr = _v2l_config_get_tag (conn_base, "uniqattr");
85 if (self->uniqattr == NULL)
87 log_error (ZONE, "LDAP unique attribute is not specified");
88 return 0;
91 self->binddn = _v2l_config_get_tag (conn_base, "binddn");
93 if (self->binddn == NULL)
95 log_error (ZONE, "LDAP jabber admin dn is not specified");
96 return 0;
99 self->bindpw = _v2l_config_get_tag (conn_base, "bindpw");
101 if (self->bindpw == NULL)
103 log_error (ZONE,
104 "LDAP jabber admin password is not specified");
105 return 0;
108 self->confpath = _v2l_config_get_tag (conn_base, "tpl");
110 if (self->confpath == NULL)
112 log_error (ZONE,
113 "Path to vCard <->LDAP is not specified");
114 return 0;
117 self->poolref = pool_new ();
119 if (self->poolref == NULL)
121 log_error (ZONE, "Cannot create pool");
122 return 0;
125 /* Get a handle to an LDAP connection. */
126 log_debug (ZONE, "LDAP server: %s / port : %d", self->host, self->port);
128 self->master_conn = v2l_get_master_conn (self);
130 if (self->master_conn == NULL)
132 log_error (ZONE,
133 "Unable to create the LDAP main connection");
134 return 0;
137 return 1;
140 void
141 v2l_config_shutdown (v2l_Config *self)
143 if (self != NULL && self->master_conn != NULL)
145 v2l_free_allconn ();
146 ldap_unbind (self->master_conn->ld);
147 pool_free (self->master_conn->p);
148 pool_free (self->poolref);
150 #if LDAP_VENDOR_VERSION <= 20130
151 sasl_done ();
152 #endif
156 /* public api ends here */
158 static char *
159 _v2l_config_get_tag (T_CONF conn_base, const char *tag)
161 #ifndef _V2L_JABBER2
162 xmlnode tmp;
164 tmp = xmlnode_get_tag (conn_base, tag);
166 if (tmp == NULL)
168 return NULL;
171 return (char *) xmlnode_get_data (xmlnode_get_firstchild (tmp));
172 #else
173 char str[32] = "v2l.";
175 return config_get_one (conn_base, strncat (str, tag, 27), 0);
176 #endif