summaryrefslogtreecommitdiff
path: root/plugin/delegateldap.c
blob: 170c6a7d789c2079e93185b4fff8f2392ffc0c34 (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604

#include "config.h"

#include <sasl/sasl.h>
#include <sasl/saslplug.h>

#define LDAP_DEPRECATED 1
#include <ldap.h>
#include <lber.h>

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>

#define CANARY_VALUE "!DL!"

#define OPTION_SERVER  "delegateldap_uri"
#define OPTION_TLS     "delegateldap_tls"
#define OPTION_LDAPRC  "delegateldap_ldaprc"

/*
 * SETTINGS -------------------------------------------------------------------------
 * TODO: Move these to a configuration file or some such
 */

#define SETTINGS_URL "ldap://209.66.100.155"

typedef struct _supported_mechanisms {
	void *mutex;
	int loaded;
	struct berval **mechs;
} supported_mechanisms;

static supported_mechanisms *the_supported;

typedef struct _delegate_context {
	char canary[4];
	const char* mechanism;
	LDAP *ldap;
} delegate_context;

extern sasl_server_plug_init_t sasl_server_plug_init;

static void
log_message (const sasl_utils_t *utils, int level, const char *format, ...)
{
	char buf[1024];
	va_list va;

	va_start (va, format);
	vsnprintf (buf, sizeof (buf), format, va);
	buf[sizeof (buf) - 1] = 0;
	vfprintf (stderr, format, va);
	fputc ('\n', stderr);
	va_end (va);

	(utils->log) (utils->conn, level, "%s", buf);
}

static int
report_ldap_error (const sasl_utils_t *utils, int ldap, const char *format, ...)
{
	char buf[1024];
	va_list va;

	buf[0] = 0;

	if (format) {
		va_start (va, format);
		vsnprintf (buf, sizeof (buf), format, va);
		buf[sizeof (buf) - 1] = 0;
		va_end (va);
	}

	(utils->seterror) (utils->conn, 0, "%s%s%s",
	                   buf, buf[0] ? ":" : "", ldap_err2string (ldap));
	fprintf (stderr, "%s%s%s", buf, buf[0] ? ": " : "", ldap_err2string (ldap));
	fputc ('\n', stderr);

	switch (ldap) {
	case LDAP_MORE_RESULTS_TO_RETURN:
		return SASL_CONTINUE;
	case LDAP_SUCCESS:
		return SASL_OK;
	case LDAP_NO_MEMORY:
		return SASL_NOMEM;
	case LDAP_AUTH_UNKNOWN:
		return SASL_NOMECH;
	case LDAP_DECODING_ERROR:
		return SASL_BADPROT;
	}

	return SASL_FAIL;
}

static void
delegateldap_dispose (void *context, const sasl_utils_t *utils)
{
	delegate_context *ctx = (delegate_context*)context;

	if (!ctx)
		return;

	if (memcmp (ctx->canary, CANARY_VALUE, sizeof (ctx->canary)) != 0) {
		log_message (utils, SASL_LOG_ERR, "trying to dispose of invalid delegateldap mechanism");
		return;
	}

	if (ctx->ldap) {
		log_message (utils, SASL_LOG_NOTE, "closing ldap connection");
		ldap_unbind_ext_s (ctx->ldap, NULL, NULL);
	}
	ctx->ldap = NULL;
	memset (ctx, 0, sizeof (*ctx));

	utils->free (ctx);
}

static int
match_any_value (const char *match, ...)
{
	const char *value;
	va_list va;

	va_start (va, match);
	for (;;) {
		value = va_arg (va, const char*);
		if (!value)
			return 0;
		if (strcasecmp (match, value) == 0)
			return 1;
	}
}

static int
delegateldap_new (void *mech_context, sasl_server_params_t *sparams,
                  const char *challenge, unsigned challen, void **context)
{
	const char *mechanism;
	const char *option;
	delegate_context *ctx;
	int res, protocol, ret;

	*context = NULL;

	/* Make sure we have appropriate options */
	if ((sparams->utils->getopt) (sparams->utils->getopt_context, NULL,
			              OPTION_SERVER, &option, NULL) != SASL_OK) {
		log_message (sparams->utils, SASL_LOG_ERR,
		             "missing '%s' option in config file", OPTION_SERVER);
		return SASL_BADPARAM;
	}

	ctx = sparams->utils->malloc (sizeof (delegate_context));
	if (!ctx)
		return SASL_NOMEM;
	memset (ctx, 0, sizeof (delegate_context));
	memcpy (ctx->canary, CANARY_VALUE, sizeof (ctx->canary));

	/* We use the mech_context to boot strap the mechanism */
	mechanism = (const char*)mech_context;

	log_message (sparams->utils, SASL_LOG_NOTE, "initializing ldap connection to: %s", option);

	res = ldap_initialize (&ctx->ldap, option);
	if (res != LDAP_SUCCESS) {
		delegateldap_dispose (ctx, sparams->utils);
		return report_ldap_error (sparams->utils, res, "couldn't initialize ldap connection: %s", option);
	}

	protocol = LDAP_VERSION3;
	if (ldap_set_option (ctx->ldap, LDAP_OPT_PROTOCOL_VERSION, &protocol) != LDAP_OPT_SUCCESS) {
		delegateldap_dispose (ctx, sparams->utils);
		log_message (sparams->utils, SASL_LOG_ERR, "couldn't setup ldap connection to use version 3 protocol");
		return SASL_FAIL;
	}

	/* See if we should connect with TLS */
	(sparams->utils->getopt) (sparams->utils->getopt_context, NULL, OPTION_TLS, &option, NULL);
	if (option && match_any_value (option, "yes", "y", "on", "try", "demand", "true", NULL)) {

		/* Start the TLS thingy */
		res = ldap_start_tls_s (ctx->ldap, NULL, NULL);
		if (res != LDAP_SUCCESS) {
			ret = report_ldap_error (sparams->utils, res, "couldn't initialize TLS on the ldap connection");

			/* The configure requires that we have tls */
			if (match_any_value (option, "demand", NULL)) {
				delegateldap_dispose (ctx, sparams->utils);
				return ret;
			}
		}
	}

	ctx->mechanism = mechanism;
	*context = ctx;

	return SASL_OK;
}

static int
calculate_user (delegate_context *ctx, sasl_server_params_t *sparams,
                sasl_out_params_t *oparams)
{
	struct berval *auth;
	char *dn;
	char **parts;
	int res, ret;

	res = ldap_whoami_s (ctx->ldap, &auth, NULL, NULL);
	if (res != LDAP_SUCCESS)
		return report_ldap_error (sparams->utils, res, "couldn't determine logged in user name");

	/* Anonmyous authentication */
	if (!auth) {
		log_message (sparams->utils, SASL_LOG_NOTE, "anonymous user login");
		ret = (sparams->canon_user) (sparams->utils->conn, "anonymous", 0,
		                             SASL_CU_AUTHID | SASL_CU_AUTHZID, oparams);

	/* Use RDN as authenticator */
	} else {
		dn = strchr (auth->bv_val, ':');
		if (dn) {
			*dn = 0;
			++dn;
		} else {
			dn = auth->bv_val;
		}

		parts = ldap_explode_dn (dn, 1);
		if (!parts || !parts[0]) {
			log_message (sparams->utils, SASL_LOG_ERR,
			             "couldn't parse the user's dn: %s", auth->bv_val);
			ret = SASL_FAIL;
		} else {
			log_message (sparams->utils, SASL_LOG_NOTE, "user login: %s", parts[0]);
			ret = sparams->canon_user (sparams->utils->conn, parts[0], 0,
			                           SASL_CU_AUTHID | SASL_CU_AUTHZID, oparams);
		}

		if (parts)
			ldap_value_free (parts);

		ber_bvfree (auth);
	}

	return ret;
}

static int
delegateldap_step (void *context, sasl_server_params_t *sparams,
                   const char *clientin, unsigned clientinlen,
                   const char **serverout, unsigned *serveroutlen,
                   sasl_out_params_t *oparams)
{
	delegate_context *ctx = (delegate_context*)context;
	struct berval *scred = NULL;
	struct berval ccred;
	char *buffer;
	int ret;
	int res;

	*serverout = NULL;
	*serveroutlen = 0;

	ccred.bv_len = clientinlen;
	ccred.bv_val = (char*)clientin;

	log_message (sparams->utils, SASL_LOG_NOTE,
	             "%d bytes of client request", ccred.bv_len);

	res = ldap_sasl_bind_s (ctx->ldap, NULL, ctx->mechanism, &ccred, NULL, NULL, &scred);

	/* Authentication was successful! */
	if (res == LDAP_SUCCESS) {
		log_message (sparams->utils, SASL_LOG_NOTE, "successful authentication");
		ret = calculate_user (ctx, sparams, oparams);

	/* We need to keep shuttling information */
	} else if (res == LDAP_SASL_BIND_IN_PROGRESS) {

		ret = SASL_CONTINUE;

		/* Copy the response from the server into SASL allocated memory */
		if (scred && scred->bv_len) {
			buffer = sparams->utils->malloc (scred->bv_len);
			if (buffer) {
				memcpy (buffer, scred->bv_val, scred->bv_len);
				*serveroutlen = scred->bv_len;
				*serverout = buffer;

				log_message (sparams->utils, SASL_LOG_NOTE,
				             "%d bytes of server response", scred->bv_len);

			} else {
				log_message (sparams->utils, SASL_LOG_ERR,
				             "couldn't allocate memory for server response");
				ret = SASL_NOMEM;
			}

		/* No response from the server, strange */
		} else {
			*serverout = NULL;
			*serveroutlen = 0;

			log_message (sparams->utils, SASL_LOG_WARN,
			             "no response from server during sasl step");
		}

	/* Bad login */
	} else if (res == LDAP_INVALID_CREDENTIALS){
		log_message (sparams->utils, SASL_LOG_FAIL, "user login failed: invalid credentials");
		ret = SASL_FAIL;

	/* An error condition */
	} else {

		ret = report_ldap_error (sparams->utils, res, "couldn't do sasl bind step");
	}

	if (scred)
		ber_bvfree (scred);

	if (ret == SASL_OK) {
		oparams->doneflag = 1;
		oparams->mech_ssf = 0;
		oparams->maxoutbuf = 0;
		oparams->encode_context = NULL;
		oparams->encode = NULL;
		oparams->decode_context = NULL;
		oparams->decode = NULL;
		oparams->param_version = 0;
	}

	return ret;
}

static const char*
find_berval_value (const char *match, struct berval **values)
{
	struct berval **v;
	size_t len;

	if (!values)
		return NULL;

	len = strlen (match);
	for (v = values; *v; ++v) {
		if ((*v)->bv_len == len &&
		    strncasecmp (match, (*v)->bv_val, len) == 0) {
			return match;
		}
	}

	return NULL;
}

static int
delegateldap_avail (void *mech_context, sasl_server_params_t *sparams, void **context)
{
	delegate_context *ctx = NULL;
	LDAPMessage *message = NULL;
	int ret = SASL_NOMECH;

	char *attrs[] = { "supportedSASLMechanisms", NULL };
	const char *mechanism;
	LDAPMessage *entry;
	struct berval **values;
	int res, loaded;

	mechanism = (const char*)mech_context;
	*context = NULL;
	ret = SASL_NOMECH;
	loaded = 0;

	/* Lock */
	if (the_supported->mutex)
		(sparams->utils->mutex_lock) (the_supported->mutex);

		/* See if this mechanism is cached */
		loaded = the_supported->loaded;
		if (loaded && find_berval_value (mechanism, the_supported->mechs))
			ret = SASL_OK;

	/* Unlock the globals */
	if (the_supported->mutex)
		(sparams->utils->mutex_unlock) (the_supported->mutex);

	/* We have a cache, no need to go further */
	if (loaded)
		goto cleanup;

	log_message (sparams->utils, SASL_LOG_NOTE, "loading mechanism info from server");

	/* Create a new context, connect to the LDAP server */
	ret = delegateldap_new (mech_context, sparams, NULL, 0, context);
	if (ret != SASL_OK)
		goto cleanup;

	ctx = (delegate_context*)*context;
	values = NULL;

	/* Query the server for supported mechanisms */
	res = ldap_search_ext_s (ctx->ldap, "", LDAP_SCOPE_BASE, NULL, attrs, 0,
	                         NULL, NULL, LDAP_NO_LIMIT, LDAP_NO_LIMIT, &message);

	/* Couldn't find server info */
	if (res == LDAP_NO_SUCH_OBJECT) {
		log_message (sparams->utils, SASL_LOG_WARN, "no base object with LDAP server info");

	/* Some other failure */
	} else if (res != LDAP_SUCCESS) {
		ret = report_ldap_error (sparams->utils, res, "couldn't search server for supported SASL mechanisms");
		goto cleanup;

	/* Found something */
	} else {
		entry = ldap_first_entry (ctx->ldap, message);
		if (entry == NULL) {
			log_message (sparams->utils, SASL_LOG_WARN, "no base object with LDAP server info");
		} else {
			values = ldap_get_values_len (ctx->ldap, entry, "supportedSASLMechanisms");
			if (values == NULL)
				log_message (sparams->utils, SASL_LOG_WARN, "no supportedSASLMechanisms attribute on LDAP server info");
		}
	}

	/* Lock the globals */
	if (the_supported->mutex)
		(sparams->utils->mutex_lock) (the_supported->mutex);

		/* Swap in the mechanisms we just loaded */
		if (the_supported->mechs)
			ldap_value_free_len (the_supported->mechs);
		the_supported->mechs = values;
		the_supported->loaded = 1;

		/* And search through them */
		if (find_berval_value (mechanism, the_supported->mechs))
			ret = SASL_OK;

	/* Unlock the globals */
	if (the_supported->mutex)
		(sparams->utils->mutex_unlock) (the_supported->mutex);

cleanup:

	if (message)
		ldap_msgfree (message);
	if (ret == SASL_OK) {
		log_message (sparams->utils, SASL_LOG_DEBUG,
		             "server supports mechanism: %s", mechanism);
	} else {
		log_message (sparams->utils, SASL_LOG_FAIL,
		             "server doesn't support mechanism: %s", mechanism);
		*context = NULL;
		if (ctx)
			delegateldap_dispose (ctx, sparams->utils);
	}

	return ret;
}

static void
delegateldap_free (void *mech_context, const sasl_utils_t *utils)
{
	void *mutex;

	if (!the_supported)
		return;

	/* Lock globals */
	if (the_supported->mutex)
		(utils->mutex_lock) (the_supported->mutex);

		/* Free mechanisms */
		if (the_supported->mechs)
			ldap_value_free_len (the_supported->mechs);
		the_supported->mechs = NULL;
		the_supported->mechs = 0;

		/* And the main global context */
		mutex = the_supported->mutex;
		(utils->free) (the_supported);
		the_supported = NULL;

	/* Unlock globals */
	if (mutex)
		(utils->mutex_unlock) (mutex);
}

static sasl_server_plug_t delegateldap_server_plugins[] =
{
	{
		"DIGEST-MD5",                /* mech_name */
		0,                           /* max_ssf */
		SASL_SEC_NOPLAINTEXT
		| SASL_SEC_NOANONYMOUS
		| SASL_SEC_MUTUAL_AUTH,      /* security_flags */
		SASL_FEAT_ALLOWS_PROXY,      /* features */
		"DIGEST-MD5",                /* mech_context */
		&delegateldap_new,           /* mech_new */
		&delegateldap_step,          /* mech_step */
		&delegateldap_dispose,       /* mech_dispose */
		&delegateldap_free,          /* mech_free */
		NULL,                        /* setpass */
		NULL,                        /* user_query */
		NULL,                        /* idle */
		&delegateldap_avail,         /* mech avail */
		NULL                         /* spare */
	},
	{
		"CRAM-MD5",                  /* mech_name */
		0,                           /* max_ssf */
		SASL_SEC_NOPLAINTEXT
		| SASL_SEC_NOANONYMOUS,      /* security_flags */
		SASL_FEAT_SERVER_FIRST,      /* features */
		"CRAM-MD5",                  /* mech_context */
		&delegateldap_new,           /* mech_new */
		&delegateldap_step,          /* mech_step */
		&delegateldap_dispose,       /* mech_dispose */
		&delegateldap_free,          /* mech_free */
		NULL,                        /* setpass */
		NULL,                        /* user_query */
		NULL,                        /* idle */
		&delegateldap_avail,         /* mech avail */
		NULL                         /* spare */
	},
	{
		"PLAIN",                     /* mech_name */
		0,                           /* max_ssf */
		SASL_SEC_NOANONYMOUS,        /* security_flags */
		SASL_FEAT_WANT_CLIENT_FIRST
		| SASL_FEAT_ALLOWS_PROXY,    /* features */
		"PLAIN",                     /* mech_context */
		&delegateldap_new,           /* mech_new */
		&delegateldap_step,          /* mech_step */
		&delegateldap_dispose,       /* mech_dispose */
		&delegateldap_free,          /* mech_free */
		NULL,                        /* setpass */
		NULL,                        /* user_query */
		NULL,                        /* idle */
		&delegateldap_avail,         /* mech avail */
		NULL                         /* spare */
	},
	{
		"LOGIN",                     /* mech_name */
		0,                           /* max_ssf */
		SASL_SEC_NOANONYMOUS,        /* security_flags */
		0,                           /* features */
		"LOGIN",                     /* mech_context */
		&delegateldap_new,           /* mech_new */
		&delegateldap_step,          /* mech_step */
		&delegateldap_dispose,       /* mech_dispose */
		&delegateldap_free,          /* mech_free */
		NULL,                        /* setpass */
		NULL,                        /* user_query */
		NULL,                        /* idle */
		&delegateldap_avail,         /* mech avail */
		NULL                         /* spare */
	}
};

extern int
sasl_server_plug_init (const sasl_utils_t *utils, int maxversion, int *out_version,
                       sasl_server_plug_t **pluglist, int *plugcount)
{
	const char *option;

	if (maxversion < SASL_SERVER_PLUG_VERSION) {
		log_message (utils, SASL_LOG_ERR, "bad version: can't load delegateldap plugin");
		return SASL_BADVERS;
	}

	log_message (utils, SASL_LOG_DEBUG, "loading delegateldap plugin");

	if ((utils->getopt) (utils->getopt_context, NULL,
			     OPTION_SERVER, &option, NULL) != SASL_OK || !option) {
		log_message (utils, SASL_LOG_ERR,
		             "missing '%s' option in config file", OPTION_SERVER);
		return SASL_BADPARAM;
	}

	/* From ldapdb plugin */
	if ((utils->getopt) (utils->getopt_context, NULL,
	                     OPTION_LDAPRC, &option, NULL) == SASL_OK && option) {
		setenv ("LDAPRC", option, 1);
	}

	the_supported = (utils->malloc) (sizeof (supported_mechanisms));
	if (!the_supported)
		return SASL_NOMEM;
	memset (the_supported, 0, sizeof (supported_mechanisms));

	if (utils->mutex_alloc)
		the_supported->mutex = (utils->mutex_alloc) ();

	*out_version = SASL_SERVER_PLUG_VERSION;
	*pluglist = delegateldap_server_plugins;
	*plugcount = sizeof (delegateldap_server_plugins) / sizeof (delegateldap_server_plugins[0]);

	return SASL_OK;
}