summaryrefslogtreecommitdiffstats
path: root/ldap/servers/plugins/passthru/ptconfig.c
blob: b7bb138632c98070bfb4cad9cf0ce129fe51c603 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/** BEGIN COPYRIGHT BLOCK
 * This Program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; version 2 of the License.
 * 
 * This Program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA.
 * 
 * In addition, as a special exception, Red Hat, Inc. gives You the additional
 * right to link the code of this Program with code not covered under the GNU
 * General Public License ("Non-GPL Code") and to distribute linked combinations
 * including the two, subject to the limitations in this paragraph. Non-GPL Code
 * permitted under this exception must only link to the code of this Program
 * through those well defined interfaces identified in the file named EXCEPTION
 * found in the source code files (the "Approved Interfaces"). The files of
 * Non-GPL Code may instantiate templates or use macros or inline functions from
 * the Approved Interfaces without causing the resulting work to be covered by
 * the GNU General Public License. Only Red Hat, Inc. may make changes or
 * additions to the list of Approved Interfaces. You must obey the GNU General
 * Public License in all respects for all of the Program code and other code used
 * in conjunction with the Program except the Non-GPL Code covered by this
 * exception. If you modify this file, you may extend this exception to your
 * version of the file, but you are not obligated to do so. If you do not wish to
 * provide this exception without modification, you must delete this exception
 * statement from your version and license this file solely under the GPL without
 * exception. 
 * 
 * 
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

/*
 * ptconfig.c - configuration-related code for Pass Through Authentication
 *
 */

#include "passthru.h"

/*
 * Configuration is a bit complicated to fit into a single slapd config file
 * line, but for now that's how it works.  The format is:
 *
 *   plugin preoperation on PTA NSHOME/passthru-plugin.so passthruauth_init ARGS
 *
 * where each ARGS provides configuration for one host.  Each ARG should
 * be of the form:
 *
 *  "ldap://hosts/suffixes maxconns,maxconcurrency,timeout,ldver,connlifetime"
 * OR
 *  "ldaps://hosts/suffixes maxconns,maxconcurrency,timeout,ldver,connlifetime"
 *
 * where:
 *    hosts is a space-separated list of remote servers (with optional port
 *      numbers) to be used.  Each one is tried in order when opening an
 *      LDAP connection.
 *    suffixes is a semicolon separated list of DNs (if a DN contains a
 *	semicolon it must be represented \3B),
 *    maxconns is a limit on how many connections will be made,
 *    maxconcurrency is a limit on how many operations can share a connection, 
 *    timeout is a time limit in seconds for bind operations to complete (use
 *	0 to specify an infinite limit).
 *    ldver is the LDAP protocol version to use to talk to the server (2 or 3)
 *    connlifetime is a time limit time in seconds for a connection to be
 *      used before it is closed and reopened (use 0 to specify an infinite
 *      limit).  connlifetime can be omitted in which case a default value
 *	is used; this is for compatibility with DS 4.0 which did not support
 *      connlifetime.
 */


/*
 * function prototypes
 */ 

/*
 * static variables
 */
/* for now, there is only one configuration and it is global to the plugin  */
static PassThruConfig	theConfig;
static int		inited = 0;


/*
 * Read configuration and create a configuration data structure.
 * This is called after the server has configured itself so we can check
 *   for things like collisions between our suffixes and backend's suffixes.
 * Returns an LDAP error code (LDAP_SUCCESS if all goes well).
 * XXXmcs: this function leaks memory if any errors occur.
 */
int
passthru_config( int argc, char **argv )
{
    int			i, j, rc, tosecs, using_def_connlifetime, starttls = 0;
    char		**suffixarray;
    PassThruServer	*prevsrvr, *srvr;
    PassThruSuffix	*suffix, *prevsuffix;
    LDAPURLDesc		*ludp;

    if ( inited ) {
	slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
		"only one pass through plugin instance can be used\n" );
	return( LDAP_PARAM_ERROR );
    }

    inited = 1;

    /*
     * It doesn't make sense to configure a pass through plugin without
     * providing at least one remote server.  Return an error if attempted.
     */
    if ( argc < 1 ) {
	slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
		"no pass through servers found in configuration"
		" (at least one must be listed)\n" );
	return( LDAP_PARAM_ERROR );
    }

    /*
     * Parse argv[] values.
     */
    prevsrvr = NULL;
    for ( i = 0; i < argc; ++i ) {
	char *p = NULL;
	srvr = (PassThruServer *)slapi_ch_calloc( 1, sizeof( PassThruServer ));
	srvr->ptsrvr_url = slapi_ch_strdup( argv[i] );

	/* since the ldap url may contain both spaces (to delimit multiple hosts)
	   and commas (in suffixes), we have to search for the first space
	   after the last /, then look for any commas after that
	   This assumes the ldap url looks like this:
	   ldap(s)://host:port host:port .... host:port/suffixes
	   That is, it assumes there is always a trailing slash on the ldapurl
	   and that the url does not look like this: ldap://host
	   also assumes suffixes do not have any / in them
	*/
	if ((p = strrchr(srvr->ptsrvr_url, '/'))) { /* look for last / */
	    p = strchr(p, ' '); /* look for first space after last / */
	    if (p) {
		if (!strchr(p, ',')) { /* no comma */
		    p = NULL; /* just use defaults */
		}
	    }
	}

	if (!p) {
	    /*
	     * use defaults for maxconnections, maxconcurrency, timeout,
	     * LDAP version, and connlifetime.
	     */
	    srvr->ptsrvr_maxconnections = PASSTHRU_DEF_SRVR_MAXCONNECTIONS;
	    srvr->ptsrvr_maxconcurrency = PASSTHRU_DEF_SRVR_MAXCONCURRENCY;
	    srvr->ptsrvr_timeout = (struct timeval *)slapi_ch_calloc( 1,
		    sizeof( struct timeval ));
	    srvr->ptsrvr_timeout->tv_sec = PASSTHRU_DEF_SRVR_TIMEOUT;
	    srvr->ptsrvr_ldapversion = PASSTHRU_DEF_SRVR_PROTOCOL_VERSION;
	    using_def_connlifetime = 1;
	} else {
	    /*
	     * parse parameters.  format is:
	     *     maxconnections,maxconcurrency,timeout,ldapversion
	     * OR  maxconnections,maxconcurrency,timeout,ldapversion,lifetime
	     * OR  maxconnections,maxconcurrency,timeout,ldapversion,lifetime,starttls
	     */
	    *p++ = '\0'; /* p points at space preceding optional arguments */
	    rc = sscanf( p, "%d,%d,%d,%d,%d,%d", &srvr->ptsrvr_maxconnections,
		    &srvr->ptsrvr_maxconcurrency, &tosecs,
		    &srvr->ptsrvr_ldapversion, &srvr->ptsrvr_connlifetime,
		    &starttls);
	    if ( rc < 4 ) {
		slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
			"server parameters should be in the form "
			"\"maxconnections,maxconcurrency,timeout,ldapversion,"
			"connlifetime\" (got \"%s\")\n", p );
		return( LDAP_PARAM_ERROR );
	    } else if ( rc < 5 ) {
		using_def_connlifetime = 1;
		srvr->ptsrvr_connlifetime = PASSTHRU_DEF_SRVR_CONNLIFETIME;
		starttls = 0;
	    } else if ( rc < 6 ) {
		using_def_connlifetime = 0; /* lifetime specified */
		starttls = 0; /* but not starttls */
	    } else { /* all 6 args supplied */
		using_def_connlifetime = 0; /* lifetime specified */
		/* and starttls */
	    }

	    if ( srvr->ptsrvr_ldapversion != LDAP_VERSION2
		    && srvr->ptsrvr_ldapversion != LDAP_VERSION3 ) {
		slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
			"LDAP protocol version should be %d or %d (got %d)\n",
			LDAP_VERSION2, LDAP_VERSION3,
			srvr->ptsrvr_ldapversion );
		return( LDAP_PARAM_ERROR );
	    }

	    if ( srvr->ptsrvr_maxconnections <= 0 ) {
		slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
			"maximum connections must be greater than "
			"zero (got %d)\n", srvr->ptsrvr_maxconnections );
		return( LDAP_PARAM_ERROR );
	    }

	    if ( srvr->ptsrvr_maxconcurrency <= 0 ) {
		slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
			"maximum concurrency must be greater than "
			"zero (got %d)\n", srvr->ptsrvr_maxconcurrency );
		return( LDAP_PARAM_ERROR );
	    }

	    if ( tosecs <= 0 ) {
		srvr->ptsrvr_timeout = NULL;
	    } else {
		srvr->ptsrvr_timeout = (struct timeval *)slapi_ch_calloc( 1,
			sizeof( struct timeval ));
		srvr->ptsrvr_timeout->tv_sec = tosecs;
	    }
	}

	/*
	 * parse the LDAP URL
	 */
	if (( rc = ldap_url_parse( srvr->ptsrvr_url, &ludp )) != 0 ) {
	    slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
		    "unable to parse LDAP URL \"%s\" (%s)\n",
		    srvr->ptsrvr_url, passthru_urlparse_err2string( rc ));
	    return( LDAP_PARAM_ERROR );
	}

	if ( ludp->lud_dn == NULL || *ludp->lud_dn == '\0' ) {
	    slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
		    "missing suffix in LDAP URL \"%s\"\n",
		    srvr->ptsrvr_url );
	    return( LDAP_PARAM_ERROR );
	}
	
	srvr->ptsrvr_hostname = slapi_ch_strdup( ludp->lud_host );
	srvr->ptsrvr_port = ludp->lud_port;
	srvr->ptsrvr_secure =
		(( ludp->lud_options & LDAP_URL_OPT_SECURE ) != 0 );
	if (starttls) {
	    srvr->ptsrvr_secure = 2;
	}

	/*
	 * If a space-separated list of hosts is configured for failover,
	 * use a different (non infinite) default for connection lifetime.
	 */
	if ( using_def_connlifetime &&
	    strchr( srvr->ptsrvr_hostname, ' ' ) != NULL ) {
		srvr->ptsrvr_connlifetime =
			PASSTHRU_DEF_SRVR_FAILOVERCONNLIFETIME;
	}

	/*
	 * split the DN into multiple suffixes (separated by ';')
	 */
	if (( suffixarray = ldap_str2charray( ludp->lud_dn, ";" )) == NULL ) {
	    slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
		"unable to parse suffix string \"%s\" within \"%s\"\n",
		ludp->lud_dn, srvr->ptsrvr_url );
	    return( LDAP_PARAM_ERROR );
	}

	/*
	 * free our LDAP URL descriptor
	 */
	ldap_free_urldesc( ludp );
	ludp = NULL;

	/*
	 * reorganize the suffixes into a linked list and normalize them
	 */
	prevsuffix = NULL;
	for ( j = 0; suffixarray[ j ] != NULL; ++j ) {

	    /*
	     * allocate a new PassThruSuffix structure and fill it in.
	     */
	    suffix = (PassThruSuffix *)slapi_ch_malloc(
		    sizeof( PassThruSuffix ));
	    suffix->ptsuffix_normsuffix =
		    slapi_dn_normalize( suffixarray[ j ] );
	    suffixarray[ j ] = NULL;
	    suffix->ptsuffix_len = strlen( suffix->ptsuffix_normsuffix );
	    suffix->ptsuffix_next = NULL;

	    /*
	     * add to end of list
	     */
	    if ( prevsuffix == NULL ) {
		srvr->ptsrvr_suffixes = suffix;
	    } else {
		prevsuffix->ptsuffix_next = suffix;
	    }
	    prevsuffix = suffix;
	}
	ldap_memfree( suffixarray );

	/*
	 * create mutexes and condition variables for this server
	 */
	if (( srvr->ptsrvr_connlist_mutex = slapi_new_mutex()) == NULL ||
		( srvr->ptsrvr_connlist_cv = slapi_new_condvar(
		srvr->ptsrvr_connlist_mutex )) == NULL ) {
	    return( LDAP_LOCAL_ERROR );
	}

	/*
	 * add this server to the end of our list
	 */
	if ( prevsrvr == NULL ) {
	    theConfig.ptconfig_serverlist = srvr;
	} else {
	    prevsrvr->ptsrvr_next = srvr;
	}
	prevsrvr = srvr;

#ifdef PASSTHRU_VERBOSE_LOGGING
	/*
	 * log configuration for debugging purposes
	 */
	slapi_log_error( SLAPI_LOG_PLUGIN, PASSTHRU_PLUGIN_SUBSYSTEM,
		"PTA server host: \"%s\", port: %d, secure: %d,"
		" maxconnections: %d, maxconcurrency: %d, timeout: %d,"
		" ldversion: %d, connlifetime: %d\n",
		srvr->ptsrvr_hostname, srvr->ptsrvr_port,
		srvr->ptsrvr_secure, srvr->ptsrvr_maxconnections,
		srvr->ptsrvr_maxconcurrency,
		srvr->ptsrvr_timeout == NULL ? -1
		: srvr->ptsrvr_timeout->tv_sec, srvr->ptsrvr_ldapversion,
		srvr->ptsrvr_connlifetime );
	for ( prevsuffix = srvr->ptsrvr_suffixes; prevsuffix != NULL;
		prevsuffix = prevsuffix->ptsuffix_next ) {
	    slapi_log_error( SLAPI_LOG_FATAL, PASSTHRU_PLUGIN_SUBSYSTEM,
		    "   normalized suffix: \"%s\"\n",
		    prevsuffix->ptsuffix_normsuffix );
	}
#endif

    }

    return( LDAP_SUCCESS );
}


/*
 * Get the pass though configuration data.  For now, there is only one
 * configuration and it is global to the plugin.
 */
PassThruConfig *
passthru_get_config( void )
{
    return( &theConfig );
}