More sophisticated selection of the user jid in Vcard2Ldap. Minor fixes.
[vcard2ldap.git] / src / v2l_config.c
blob31c1bad7889b9b22330e73c3bc536ad0ab87089d
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_config.c
18 \brief Config handling functions. Implementation
21 #include <stdlib.h>
22 #include <strings.h>
24 #ifndef _V2L_JABBER2
25 #include <jabberd.h>
26 #else
27 #include "../util/util.h"
29 #define log_warn log_debug
30 #define log_error log_debug
31 typedef pool_t pool;
32 #endif
34 #include <v2l_config.h>
35 #include <v2l_conn.h>
37 /*! \brief Gets value for tag from config
38 Generic (preprocessor) function for handling Jabberd14/2 config differences
40 static char *_v2l_config_get_tag (T_CONF conn_base, const char *tag);
42 /* public api */
44 int
45 v2l_config_init (v2l_Config *self, T_CONF cfgroot)
47 T_CONF conn_base;
48 const char *portstr;
50 if (!cfgroot)
52 log_error (ZONE, "xdb_v2l configuration not present");
53 return 0;
56 #ifndef _V2L_JABBER2
57 /* Have a look in the XML configuration data for connection information. */
58 conn_base = xmlnode_get_tag (cfgroot, "connection");
60 if (conn_base == NULL)
62 log_error (ZONE,"<connection> tag is not present");
63 return 0;
65 #else
66 conn_base = cfgroot;
67 #endif
69 self->host = _v2l_config_get_tag (conn_base, "host");
71 if (self->host == NULL)
73 log_error (ZONE, "LDAP server is not specified");
74 return 0;
77 portstr = _v2l_config_get_tag (conn_base, "port");
78 self->port = portstr != NULL ? atoi (portstr) : LDAP_PORT;
80 self->suffix = _v2l_config_get_tag (conn_base, "suffix");
82 if (self->suffix == NULL)
84 log_error (ZONE, "LDAP suffix is not specified");
85 return 0;
88 self->filter = _v2l_config_get_tag (conn_base, "filter");
90 if (self->filter == NULL)
92 log_error (ZONE, "LDAP search filter is not specified");
93 return 0;
96 self->binddn = _v2l_config_get_tag (conn_base, "binddn");
98 if (self->binddn == NULL)
100 log_error (ZONE, "LDAP jabber admin dn is not specified");
101 return 0;
104 self->bindpw = _v2l_config_get_tag (conn_base, "bindpw");
106 if (self->bindpw == NULL)
108 log_error (ZONE,
109 "LDAP jabber admin password is not specified");
110 return 0;
113 self->confpath = _v2l_config_get_tag (conn_base, "tpl");
115 if (self->confpath == NULL)
117 log_error (ZONE,
118 "Path to vCard <->LDAP is not specified");
119 return 0;
122 self->poolref = pool_new ();
124 if (self->poolref == NULL)
126 log_error (ZONE, "Cannot create pool");
127 return 0;
130 /* Get a handle to an LDAP connection. */
131 log_debug (ZONE, "LDAP server: %s / port : %d", self->host, self->port);
133 self->master_conn = v2l_get_master_conn (self);
135 if (self->master_conn == NULL)
137 log_error (ZONE,
138 "Unable to create the LDAP main connection");
139 return 0;
142 return 1;
145 void
146 v2l_config_shutdown (v2l_Config *self)
148 if (self != NULL && self->master_conn != NULL)
150 v2l_free_allconn ();
151 ldap_unbind_s (self->master_conn->ld);
152 pool_free (self->master_conn->poolref);
153 pool_free (self->poolref);
157 /* public api ends here */
159 static char *
160 _v2l_config_get_tag (T_CONF conn_base, const char *tag)
162 #ifndef _V2L_JABBER2
163 xmlnode tmp;
165 tmp = xmlnode_get_tag (conn_base, tag);
167 if (tmp == NULL)
169 return NULL;
172 return (char *) xmlnode_get_data (xmlnode_get_firstchild (tmp));
173 #else
174 char str[32] = "v2l.";
176 return config_get_one (conn_base, strncat (str, tag, 27), 0);
177 #endif