Debian Codegraph @

servers/slapd/overlays/remoteauth.c

raw
1 /* $OpenLDAP$ */
2 /* remoteauth.c - Overlay to delegate bind processing to a remote server */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 2004-2024 The OpenLDAP Foundation.
6 * Portions Copyright 2017-2021 Ondřej Kuzník, Symas Corporation.
7 * Portions Copyright 2004-2017 Howard Chu, Symas Corporation.
8 * Portions Copyright 2004 Hewlett-Packard Company.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted only as authorized by the OpenLDAP
13 * Public License.
14 *
15 * A copy of this license is available in the file LICENSE in the
16 * top-level directory of the distribution or, alternatively, at
17 * <http://www.OpenLDAP.org/license.html>.
18 */
19
20 #include "portable.h"
21
22 #include <ldap.h>
23 #if SLAPD_MODULES
24 #define LIBLTDL_DLL_IMPORT /* Win32: don't re-export libltdl's symbols */
25 #include <ltdl.h>
26 #endif
27 #include <ac/errno.h>
28 #include <ac/time.h>
29 #include <ac/string.h>
30 #include <ac/ctype.h>
31 #include "lutil.h"
32 #include "slap.h"
33 #include "slap-config.h"
34
35 #ifndef UP_STR
36 #define UP_STR "userPassword"
37 #endif /* UP_STR */
38
39 #ifndef LDAP_PREFIX
40 #define LDAP_PREFIX "ldap://"
41 #endif /* LDAP_PREFIX */
42
43 #ifndef FILE_PREFIX
44 #define FILE_PREFIX "file://"
45 #endif /* LDAP_PREFIX */
46
47 typedef struct _ad_info {
48 struct _ad_info *next;
49 char *domain;
50 char *realm;
51 } ad_info;
52
53 typedef struct _ad_pin {
54 struct _ad_pin *next;
55 char *hostname;
56 char *pin;
57 } ad_pin;
58
59 typedef struct _ad_private {
60 char *dn;
61 AttributeDescription *dn_ad;
62 char *domain_attr;
63 AttributeDescription *domain_ad;
64
65 AttributeDescription *up_ad;
66 ad_info *mappings;
67
68 char *default_realm;
69 char *default_domain;
70
71 int up_set;
72 int retry_count;
73 int store_on_success;
74
75 ad_pin *pins;
76 slap_bindconf ad_tls;
77 } ad_private;
78
79 enum {
80 REMOTE_AUTH_MAPPING = 1,
81 REMOTE_AUTH_DN_ATTRIBUTE,
82 REMOTE_AUTH_DOMAIN_ATTRIBUTE,
83 REMOTE_AUTH_DEFAULT_DOMAIN,
84 REMOTE_AUTH_DEFAULT_REALM,
85 REMOTE_AUTH_CACERT_DIR,
86 REMOTE_AUTH_CACERT_FILE,
87 REMOTE_AUTH_VALIDATE_CERTS,
88 REMOTE_AUTH_RETRY_COUNT,
89 REMOTE_AUTH_TLS,
90 REMOTE_AUTH_TLS_PIN,
91 REMOTE_AUTH_STORE_ON_SUCCESS,
92 };
93
94 static ConfigDriver remoteauth_cf_gen;
95
96 static ConfigTable remoteauthcfg[] = {
97 { "remoteauth_mapping", "mapping between domain and realm", 2, 3, 0,
98 ARG_MAGIC|REMOTE_AUTH_MAPPING,
99 remoteauth_cf_gen,
100 "( OLcfgOvAt:24.1 NAME 'olcRemoteAuthMapping' "
101 "DESC 'Mapping from domain name to server' "
102 "SYNTAX OMsDirectoryString )",
103 NULL, NULL
104 },
105 { "remoteauth_dn_attribute", "Attribute to use as AD bind DN", 2, 2, 0,
106 ARG_MAGIC|REMOTE_AUTH_DN_ATTRIBUTE,
107 remoteauth_cf_gen,
108 "( OLcfgOvAt:24.2 NAME 'olcRemoteAuthDNAttribute' "
109 "DESC 'Attribute in entry to use as bind DN for AD' "
110 "SYNTAX OMsDirectoryString SINGLE-VALUE )",
111 NULL, NULL
112 },
113 { "remoteauth_domain_attribute", "Attribute to use as domain determinant", 2, 2, 0,
114 ARG_MAGIC|REMOTE_AUTH_DOMAIN_ATTRIBUTE,
115 remoteauth_cf_gen,
116 "( OLcfgOvAt:24.3 NAME 'olcRemoteAuthDomainAttribute' "
117 "DESC 'Attribute in entry to determine windows domain' "
118 "SYNTAX OMsDirectoryString SINGLE-VALUE )",
119 NULL, NULL
120 },
121 { "remoteauth_default_domain", "Default Windows domain", 2, 2, 0,
122 ARG_MAGIC|REMOTE_AUTH_DEFAULT_DOMAIN,
123 remoteauth_cf_gen,
124 "( OLcfgOvAt:24.4 NAME 'olcRemoteAuthDefaultDomain' "
125 "DESC 'Default Windows domain to use' "
126 "SYNTAX OMsDirectoryString SINGLE-VALUE )",
127 NULL, NULL
128 },
129 { "remoteauth_default_realm", "Default AD realm", 2, 2, 0,
130 ARG_MAGIC|REMOTE_AUTH_DEFAULT_REALM,
131 remoteauth_cf_gen,
132 "( OLcfgOvAt:24.5 NAME 'olcRemoteAuthDefaultRealm' "
133 "DESC 'Default AD realm to use' "
134 "SYNTAX OMsDirectoryString SINGLE-VALUE )",
135 NULL, NULL
136 },
137 { "remoteauth_store", "on|off", 1, 2, 0,
138 ARG_OFFSET|ARG_ON_OFF|REMOTE_AUTH_STORE_ON_SUCCESS,
139 (void *)offsetof(ad_private, store_on_success),
140 "( OLcfgOvAt:24.6 NAME 'olcRemoteAuthStore' "
141 "DESC 'Store password locally on success' "
142 "SYNTAX OMsBoolean SINGLE-VALUE )",
143 NULL, NULL
144 },
145 { "remoteauth_retry_count", "integer", 2, 2, 0,
146 ARG_OFFSET|ARG_UINT|REMOTE_AUTH_RETRY_COUNT,
147 (void *)offsetof(ad_private, retry_count),
148 "( OLcfgOvAt:24.7 NAME 'olcRemoteAuthRetryCount' "
149 "DESC 'Number of retries attempted' "
150 "SYNTAX OMsInteger SINGLE-VALUE )",
151 NULL, { .v_uint = 3 }
152 },
153 { "remoteauth_tls", "tls settings", 2, 0, 0,
154 ARG_MAGIC|REMOTE_AUTH_TLS,
155 remoteauth_cf_gen,
156 "( OLcfgOvAt:24.8 NAME 'olcRemoteAuthTLS' "
157 "DESC 'StartTLS settings' "
158 "SYNTAX OMsDirectoryString SINGLE-VALUE )",
159 NULL, NULL
160 },
161 { "remoteauth_tls_peerkey_hash", "mapping between hostnames and their public key hash", 3, 3, 0,
162 ARG_MAGIC|REMOTE_AUTH_TLS_PIN,
163 remoteauth_cf_gen,
164 "( OLcfgOvAt:24.9 NAME 'olcRemoteAuthTLSPeerkeyHash' "
165 "DESC 'StartTLS hostname to public key pin mapping file' "
166 "SYNTAX OMsDirectoryString )",
167 NULL, NULL
168 },
169
170 { NULL, NULL, 0, 0, 0, ARG_IGNORED, NULL }
171 };
172
173 static ConfigOCs remoteauthocs[] = {
174 { "( OLcfgOvOc:24.1 "
175 "NAME 'olcRemoteAuthCfg' "
176 "DESC 'Remote Directory passthough authentication configuration' "
177 "SUP olcOverlayConfig "
178 "MUST olcRemoteAuthTLS "
179 "MAY ( olcRemoteAuthMapping $ olcRemoteAuthDNAttribute $ "
180 " olcRemoteAuthDomainAttribute $ olcRemoteAuthDefaultDomain $ "
181 " olcRemoteAuthDefaultRealm $ olcRemoteAuthStore $ "
182 " olcRemoteAuthRetryCount $ olcRemoteAuthTLSPeerkeyHash ) )",
183 Cft_Overlay, remoteauthcfg },
184 { NULL, 0, NULL }
185 };
186
187 static int
188 remoteauth_cf_gen( ConfigArgs *c )
189 {
190 slap_overinst *on = (slap_overinst *)c->bi;
191 ad_private *ad = (ad_private *)on->on_bi.bi_private;
192 struct berval bv;
193 int i, rc = 0;
194 ad_info *map;
195 const char *text = NULL;
196
197 switch ( c->op ) {
198 case SLAP_CONFIG_EMIT:
199 switch ( c->type ) {
200 case REMOTE_AUTH_MAPPING:
201 for ( map = ad->mappings; map; map = map->next ) {
202 char *str;
203
204 str = ch_malloc( strlen( map->domain ) +
205 strlen( map->realm ) + 2 );
206 sprintf( str, "%s %s", map->domain, map->realm );
207 ber_str2bv( str, 0, 0, &bv );
208 rc = value_add_one( &c->rvalue_vals, &bv );
209 if ( !rc )
210 rc = value_add_one( &c->rvalue_nvals, &bv );
211 ch_free( str );
212 if ( rc ) break;
213 }
214 break;
215 case REMOTE_AUTH_DN_ATTRIBUTE:
216 if ( ad->dn )
217 value_add_one( &c->rvalue_vals, &ad->dn_ad->ad_cname );
218 break;
219 case REMOTE_AUTH_DOMAIN_ATTRIBUTE:
220 if ( ad->domain_attr )
221 value_add_one(
222 &c->rvalue_vals, &ad->domain_ad->ad_cname );
223 break;
224 case REMOTE_AUTH_DEFAULT_DOMAIN:
225 if ( ad->default_domain ) {
226 ber_str2bv( ad->default_domain, 0, 0, &bv );
227 value_add_one( &c->rvalue_vals, &bv );
228 }
229 break;
230 case REMOTE_AUTH_DEFAULT_REALM:
231 if ( ad->default_realm ) {
232 ber_str2bv( ad->default_realm, 0, 0, &bv );
233 value_add_one( &c->rvalue_vals, &bv );
234 }
235 break;
236 case REMOTE_AUTH_TLS:
237 bindconf_tls_unparse( &ad->ad_tls, &bv );
238
239 for ( i = 0; isspace( (unsigned char) bv.bv_val[ i ] ); i++ )
240 /* count spaces */ ;
241
242 if ( i ) {
243 bv.bv_len -= i;
244 AC_MEMCPY( bv.bv_val, &bv.bv_val[ i ],
245 bv.bv_len + 1 );
246 }
247
248 value_add_one( &c->rvalue_vals, &bv );
249 ch_free( bv.bv_val );
250 break;
251 case REMOTE_AUTH_TLS_PIN: {
252 ad_pin *pin = ad->pins;
253 for ( pin = ad->pins; pin; pin = pin->next ) {
254 bv.bv_val = ch_malloc( strlen( pin->hostname ) +
255 strlen( pin->pin ) + 2 );
256 bv.bv_len = sprintf(
257 bv.bv_val, "%s %s", pin->hostname, pin->pin );
258 rc = value_add_one( &c->rvalue_vals, &bv );
259 if ( rc ) return rc;
260 rc = value_add_one( &c->rvalue_nvals, &bv );
261 if ( rc ) return rc;
262 }
263 } break;
264
265 default:
266 abort();
267 }
268 break;
269 case LDAP_MOD_DELETE:
270 switch ( c->type ) {
271 case REMOTE_AUTH_MAPPING:
272 if ( c->valx < 0 ) {
273 /* delete all mappings */
274 while ( ad->mappings ) {
275 map = ad->mappings;
276 ad->mappings = ad->mappings->next;
277 ch_free( map->domain );
278 ch_free( map->realm );
279 ch_free( map );
280 }
281 } else {
282 /* delete a specific mapping indicated by 'valx'*/
283 ad_info *pmap = NULL;
284
285 for ( map = ad->mappings, i = 0;
286 ( map ) && ( i < c->valx );
287 pmap = map, map = map->next, i++ )
288 ;
289
290 if ( pmap ) {
291 pmap->next = map->next;
292 map->next = NULL;
293
294 ch_free( map->domain );
295 ch_free( map->realm );
296 ch_free( map );
297 } else if ( ad->mappings ) {
298 /* delete the first item in the list */
299 map = ad->mappings;
300 ad->mappings = map->next;
301 ch_free( map->domain );
302 ch_free( map->realm );
303 ch_free( map );
304 }
305 }
306 break;
307 case REMOTE_AUTH_DN_ATTRIBUTE:
308 if ( ad->dn ) {
309 ch_free( ad->dn );
310 ad->dn = NULL; /* Don't free AttributeDescription */
311 }
312 break;
313 case REMOTE_AUTH_DOMAIN_ATTRIBUTE:
314 if ( ad->domain_attr ) {
315 ch_free( ad->domain_attr );
316 /* Don't free AttributeDescription */
317 ad->domain_attr = NULL;
318 }
319 break;
320 case REMOTE_AUTH_DEFAULT_DOMAIN:
321 if ( ad->default_domain ) {
322 ch_free( ad->default_domain );
323 ad->default_domain = NULL;
324 }
325 break;
326 case REMOTE_AUTH_DEFAULT_REALM:
327 if ( ad->default_realm ) {
328 ch_free( ad->default_realm );
329 ad->default_realm = NULL;
330 }
331 break;
332 case REMOTE_AUTH_TLS:
333 /* MUST + SINGLE-VALUE -> this is a replace */
334 bindconf_free( &ad->ad_tls );
335 break;
336 case REMOTE_AUTH_TLS_PIN:
337 while ( ad->pins ) {
338 ad_pin *pin = ad->pins;
339 ad->pins = ad->pins->next;
340 ch_free( pin->hostname );
341 ch_free( pin->pin );
342 ch_free( pin );
343 }
344 break;
345 /* ARG_OFFSET */
346 case REMOTE_AUTH_STORE_ON_SUCCESS:
347 case REMOTE_AUTH_RETRY_COUNT:
348 abort();
349 break;
350 default:
351 abort();
352 }
353 break;
354 case SLAP_CONFIG_ADD:
355 case LDAP_MOD_ADD:
356 switch ( c->type ) {
357 case REMOTE_AUTH_MAPPING:
358 /* add mapping to head of list */
359 map = ch_malloc( sizeof(ad_info) );
360 map->domain = ber_strdup( c->argv[1] );
361 map->realm = ber_strdup( c->argv[2] );
362 map->next = ad->mappings;
363 ad->mappings = map;
364
365 break;
366 case REMOTE_AUTH_DN_ATTRIBUTE:
367 if ( slap_str2ad( c->argv[1], &ad->dn_ad, &text ) ==
368 LDAP_SUCCESS ) {
369 ad->dn = ber_strdup( ad->dn_ad->ad_cname.bv_val );
370 } else {
371 strncpy( c->cr_msg, text, sizeof(c->cr_msg) );
372 c->cr_msg[sizeof(c->cr_msg) - 1] = '\0';
373 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg );
374 rc = ARG_BAD_CONF;
375 }
376 break;
377 case REMOTE_AUTH_DOMAIN_ATTRIBUTE:
378 if ( slap_str2ad( c->argv[1], &ad->domain_ad, &text ) ==
379 LDAP_SUCCESS ) {
380 ad->domain_attr =
381 ber_strdup( ad->domain_ad->ad_cname.bv_val );
382 } else {
383 strncpy( c->cr_msg, text, sizeof(c->cr_msg) );
384 c->cr_msg[sizeof(c->cr_msg) - 1] = '\0';
385 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg );
386 rc = ARG_BAD_CONF;
387 }
388 break;
389 case REMOTE_AUTH_DEFAULT_DOMAIN:
390 if ( ad->default_domain ) {
391 ch_free( ad->default_domain );
392 ad->default_domain = NULL;
393 }
394 ad->default_domain = ber_strdup( c->argv[1] );
395 break;
396 case REMOTE_AUTH_DEFAULT_REALM:
397 if ( ad->default_realm ) {
398 ch_free( ad->default_realm );
399 ad->default_realm = NULL;
400 }
401 ad->default_realm = ber_strdup( c->argv[1] );
402 break;
403 case REMOTE_AUTH_TLS:
404 for ( i=1; i < c->argc; i++ ) {
405 if ( bindconf_tls_parse( c->argv[i], &ad->ad_tls ) ) {
406 rc = 1;
407 break;
408 }
409 }
410 bindconf_tls_defaults( &ad->ad_tls );
411 break;
412 case REMOTE_AUTH_TLS_PIN: {
413 ad_pin *pin = ch_calloc( 1, sizeof(ad_pin) );
414
415 pin->hostname = ber_strdup( c->argv[1] );
416 pin->pin = ber_strdup( c->argv[2] );
417 pin->next = ad->pins;
418 ad->pins = pin;
419 } break;
420 /* ARG_OFFSET */
421 case REMOTE_AUTH_STORE_ON_SUCCESS:
422 case REMOTE_AUTH_RETRY_COUNT:
423 abort();
424 break;
425 default:
426 abort();
427 }
428 break;
429 default:
430 abort();
431 }
432
433 return rc;
434 }
435
436 static char *
437 get_realm(
438 const char *domain,
439 ad_info *mappings,
440 const char *default_realm,
441 int *isfile )
442 {
443 ad_info *ai;
444 char *dom = NULL, *ch, *ret = NULL;
445
446 if ( isfile ) *isfile = 0;
447
448 if ( !domain ) {
449 ret = default_realm ? ch_strdup( default_realm ) : NULL;
450 goto exit;
451 }
452
453 /* munge any DOMAIN\user or DOMAIN:user values into just DOMAIN */
454
455 ch = strchr( domain, '\\' );
456 if ( !ch ) ch = strchr( domain, ':' );
457
458 if ( ch ) {
459 dom = ch_malloc( ch - domain + 1 );
460 strncpy( dom, domain, ch - domain );
461 dom[ch - domain] = '\0';
462 } else {
463 dom = ch_strdup( domain );
464 }
465
466 for ( ai = mappings; ai; ai = ai->next )
467 if ( strcasecmp( ai->domain, dom ) == 0 ) {
468 ret = ch_strdup( ai->realm );
469 break;
470 }
471
472 if ( !ai )
473 ret = default_realm ? ch_strdup( default_realm ) :
474 NULL; /* no mapping found */
475 exit:
476 if ( dom ) ch_free( dom );
477 if ( ret &&
478 ( strncasecmp( ret, FILE_PREFIX, strlen( FILE_PREFIX ) ) == 0 ) ) {
479 char *p;
480
481 p = ret;
482 ret = ch_strdup( p + strlen( FILE_PREFIX ) );
483 ch_free( p );
484 if ( isfile ) *isfile = 1;
485 }
486
487 return ret;
488 }
489
490 static char *
491 get_ldap_url( const char *realm, int isfile )
492 {
493 char *ldap_url = NULL;
494 FILE *fp;
495
496 if ( !realm ) return NULL;
497
498 if ( !isfile ) {
499 if ( strstr( realm, "://" ) ) {
500 return ch_strdup( realm );
501 }
502
503 ldap_url = ch_malloc( 1 + strlen( LDAP_PREFIX ) + strlen( realm ) );
504 sprintf( ldap_url, "%s%s", LDAP_PREFIX, realm );
505 return ldap_url;
506 }
507
508 fp = fopen( realm, "r" );
509 if ( !fp ) {
510 char ebuf[128];
511 int saved_errno = errno;
512 Debug( LDAP_DEBUG_TRACE, "remoteauth: "
513 "Unable to open realm file (%s)\n",
514 sock_errstr( saved_errno, ebuf, sizeof(ebuf) ) );
515 return NULL;
516 }
517 /*
518 * Read each line in the file and return a URL of the form
519 * "ldap://<line1> ldap://<line2> ... ldap://<lineN>"
520 * which can be passed to ldap_initialize.
521 */
522 while ( !feof( fp ) ) {
523 char line[512], *p;
524
525 p = fgets( line, sizeof(line), fp );
526 if ( !p ) continue;
527
528 /* terminate line at first whitespace */
529 for ( p = line; *p; p++ )
530 if ( isspace( *p ) ) {
531 *p = '\0';
532 break;
533 }
534
535 if ( ldap_url ) {
536 char *nu;
537
538 nu = ch_malloc( strlen( ldap_url ) + 2 + strlen( LDAP_PREFIX ) +
539 strlen( line ) );
540
541 if ( strstr( line, "://" ) ) {
542 sprintf( nu, "%s %s", ldap_url, line );
543 } else {
544 sprintf( nu, "%s %s%s", ldap_url, LDAP_PREFIX, line );
545 }
546 ch_free( ldap_url );
547 ldap_url = nu;
548 } else {
549 ldap_url = ch_malloc( 1 + strlen( line ) + strlen( LDAP_PREFIX ) );
550 if ( strstr( line, "://" ) ) {
551 strcpy( ldap_url, line );
552 } else {
553 sprintf( ldap_url, "%s%s", LDAP_PREFIX, line );
554 }
555 }
556 }
557
558 fclose( fp );
559
560 return ldap_url;
561 }
562
563 static void
564 trace_remoteauth_parameters( ad_private *ap )
565 {
566 ad_info *pad_info;
567 struct berval bv;
568
569 if ( !ap ) return;
570
571 Debug( LDAP_DEBUG_TRACE, "remoteauth_dn_attribute: %s\n",
572 ap->dn ? ap->dn : "NULL" );
573
574 Debug( LDAP_DEBUG_TRACE, "remoteauth_domain_attribute: %s\n",
575 ap->domain_attr ? ap->domain_attr : "NULL" );
576
577 Debug( LDAP_DEBUG_TRACE, "remoteauth_default_realm: %s\n",
578 ap->default_realm ? ap->default_realm : "NULL" );
579
580 Debug( LDAP_DEBUG_TRACE, "remoteauth_default_domain: %s\n",
581 ap->default_domain ? ap->default_domain : "NULL" );
582
583 Debug( LDAP_DEBUG_TRACE, "remoteauth_retry_count: %d\n", ap->retry_count );
584
585 bindconf_tls_unparse( &ap->ad_tls, &bv );
586 Debug( LDAP_DEBUG_TRACE, "remoteauth_tls:%s\n", bv.bv_val );
587 ch_free( bv.bv_val );
588
589 pad_info = ap->mappings;
590 while ( pad_info ) {
591 Debug( LDAP_DEBUG_TRACE, "remoteauth_mappings(%s,%s)\n",
592 pad_info->domain ? pad_info->domain : "NULL",
593 pad_info->realm ? pad_info->realm : "NULL" );
594 pad_info = pad_info->next;
595 }
596
597 return;
598 }
599
600 static int
601 remoteauth_conn_cb(
602 LDAP *ld,
603 Sockbuf *sb,
604 LDAPURLDesc *srv,
605 struct sockaddr *addr,
606 struct ldap_conncb *ctx )
607 {
608 ad_private *ap = ctx->lc_arg;
609 ad_pin *pin = NULL;
610 char *host;
611
612 host = srv->lud_host;
613 if ( !host || !*host ) {
614 host = "localhost";
615 }
616
617 for ( pin = ap->pins; pin; pin = pin->next ) {
618 if ( !strcasecmp( host, pin->hostname ) ) break;
619 }
620
621 if ( pin ) {
622 int rc = ldap_set_option( ld, LDAP_OPT_X_TLS_PEERKEY_HASH, pin->pin );
623 if ( rc == LDAP_SUCCESS ) {
624 return 0;
625 }
626
627 Debug( LDAP_DEBUG_TRACE, "remoteauth_conn_cb: "
628 "TLS Peerkey hash could not be set to '%s': %d\n",
629 pin->pin, rc );
630 } else {
631 Debug( LDAP_DEBUG_TRACE, "remoteauth_conn_cb: "
632 "No TLS Peerkey hash found for host '%s'\n",
633 host );
634 }
635
636 return -1;
637 }
638
639 static void
640 remoteauth_conn_delcb( LDAP *ld, Sockbuf *sb, struct ldap_conncb *ctx )
641 {
642 return;
643 }
644
645 static int
646 remoteauth_bind( Operation *op, SlapReply *rs )
647 {
648 Entry *e;
649 int rc;
650 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
651 ad_private *ap = (ad_private *)on->on_bi.bi_private;
652 Attribute *a_dom, *a_dn;
653 struct ldap_conncb ad_conncb = { .lc_add = remoteauth_conn_cb,
654 .lc_del = remoteauth_conn_delcb,
655 .lc_arg = ap };
656 struct berval dn = { 0 };
657 char *dom_val, *realm = NULL;
658 char *ldap_url = NULL;
659 LDAP *ld = NULL;
660 int protocol = LDAP_VERSION3, isfile = 0;
661 int tries = 0;
662
663 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
664 trace_remoteauth_parameters( ap );
665 }
666
667 if ( op->orb_method != LDAP_AUTH_SIMPLE )
668 return SLAP_CB_CONTINUE; /* only do password auth */
669
670 /* Can't handle root via this mechanism */
671 if ( be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) return SLAP_CB_CONTINUE;
672
673 if ( !ap->up_set ) {
674 const char *txt = NULL;
675
676 if ( slap_str2ad( UP_STR, &ap->up_ad, &txt ) )
677 Debug( LDAP_DEBUG_TRACE, "remoteauth_bind: "
678 "userPassword attr undefined: %s\n",
679 txt ? txt : "" );
680 ap->up_set = 1;
681 }
682
683 if ( !ap->up_ad ) {
684 Debug( LDAP_DEBUG_TRACE, "remoteauth_bind: "
685 "password attribute not configured\n" );
686 return SLAP_CB_CONTINUE; /* userPassword not defined */
687 }
688
689 if ( !ap->dn ) {
690 Debug( LDAP_DEBUG_TRACE, "remoteauth_bind: "
691 "remote DN attribute not configured\n" );
692 return SLAP_CB_CONTINUE; /* no mapped DN attribute */
693 }
694
695 if ( !ap->domain_attr ) {
696 Debug( LDAP_DEBUG_TRACE, "remoteauth_bind: "
697 "domain attribute not configured\n" );
698 return SLAP_CB_CONTINUE; /* no way to know domain */
699 }
700
701 op->o_bd->bd_info = (BackendInfo *)on->on_info;
702 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
703 if ( rc != LDAP_SUCCESS ) return SLAP_CB_CONTINUE;
704
705 rc = SLAP_CB_CONTINUE;
706 /* if userPassword is defined in entry, skip to the end */
707 if ( attr_find( e->e_attrs, ap->up_ad ) ) {
708 Debug( LDAP_DEBUG_TRACE, "%s remoteauth_bind: "
709 "user has a password, skipping\n",
710 op->o_log_prefix );
711 goto exit;
712 }
713
714 a_dom = attr_find( e->e_attrs, ap->domain_ad );
715 if ( !a_dom )
716 dom_val = ap->default_domain;
717 else {
718 dom_val = a_dom->a_vals[0].bv_val;
719 }
720
721 if ( !dom_val ) {
722 Debug( LDAP_DEBUG_TRACE, "%s remoteauth_bind: "
723 "user has no domain nor do we have a default, skipping\n",
724 op->o_log_prefix );
725 goto exit; /* user has no domain */
726 }
727
728 realm = get_realm( dom_val, ap->mappings, ap->default_realm, &isfile );
729 if ( !realm ) goto exit;
730
731 a_dn = attr_find( e->e_attrs, ap->dn_ad );
732 if ( !a_dn ) {
733 Debug( LDAP_DEBUG_TRACE, "%s remoteauth_bind: "
734 "no remote DN found on user\n",
735 op->o_log_prefix );
736 goto exit; /* user has no DN for the other directory */
737 }
738
739 ber_dupbv_x( &dn, a_dn->a_vals, op->o_tmpmemctx );
740 be_entry_release_r( op, e );
741 e = NULL;
742
743 Debug( LDAP_DEBUG_TRACE, "%s remoteauth_bind: "
744 "(realm, dn) = (%s, %s)\n",
745 op->o_log_prefix, realm, dn.bv_val );
746
747 ldap_url = get_ldap_url( realm, isfile );
748 if ( !ldap_url ) {
749 Debug( LDAP_DEBUG_TRACE, "%s remoteauth_bind: "
750 "No LDAP URL obtained\n",
751 op->o_log_prefix );
752 goto exit;
753 }
754
755 retry:
756 rc = ldap_initialize( &ld, ldap_url );
757 if ( rc ) {
758 Debug( LDAP_DEBUG_TRACE, "%s remoteauth_bind: "
759 "Cannot initialize %s: %s\n",
760 op->o_log_prefix, ldap_url, ldap_err2string( rc ) );
761 goto exit; /* user has no DN for the other directory */
762 }
763
764 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol );
765
766 #ifdef HAVE_TLS
767 rc = bindconf_tls_set( &ap->ad_tls, ld );
768 if ( rc ) {
769 Debug( LDAP_DEBUG_TRACE, "%s remoteauth_bind: "
770 "bindconf_tls_set failed\n",
771 op->o_log_prefix );
772 goto exit;
773 }
774
775 if ( ap->pins ) {
776 if ( (rc = ldap_set_option( ld, LDAP_OPT_CONNECT_CB, &ad_conncb )) !=
777 LDAP_SUCCESS ) {
778 goto exit;
779 }
780 }
781
782 if ( (rc = ldap_connect( ld )) != LDAP_SUCCESS ) {
783 Debug( LDAP_DEBUG_TRACE, "%s remoteauth_bind: "
784 "Cannot connect to %s: %s\n",
785 op->o_log_prefix, ldap_url, ldap_err2string( rc ) );
786 goto exit;
787 }
788
789 if ( ap->ad_tls.sb_tls && !ldap_tls_inplace( ld ) ) {
790 if ( (rc = ldap_start_tls_s( ld, NULL, NULL )) != LDAP_SUCCESS ) {
791 Debug( LDAP_DEBUG_TRACE, "%s remoteauth_bind: "
792 "LDAP TLS failed %s: %s\n",
793 op->o_log_prefix, ldap_url, ldap_err2string( rc ) );
794 goto exit;
795 }
796 }
797
798 #endif /* HAVE_TLS */
799
800 rc = ldap_sasl_bind_s( ld, dn.bv_val, LDAP_SASL_SIMPLE,
801 &op->oq_bind.rb_cred, NULL, NULL, NULL );
802 if ( rc == LDAP_SUCCESS ) {
803 if ( ap->store_on_success ) {
804 const char *txt;
805
806 Operation op2 = *op;
807 SlapReply r2 = { REP_RESULT };
808 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
809 Modifications m = {};
810
811 op2.o_tag = LDAP_REQ_MODIFY;
812 op2.o_callback = &cb;
813 op2.orm_modlist = &m;
814 op2.orm_no_opattrs = 0;
815 op2.o_dn = op->o_bd->be_rootdn;
816 op2.o_ndn = op->o_bd->be_rootndn;
817
818 m.sml_op = LDAP_MOD_ADD;
819 m.sml_flags = 0;
820 m.sml_next = NULL;
821 m.sml_type = ap->up_ad->ad_cname;
822 m.sml_desc = ap->up_ad;
823 m.sml_numvals = 1;
824 m.sml_values = op->o_tmpcalloc(
825 sizeof(struct berval), 2, op->o_tmpmemctx );
826
827 slap_passwd_hash( &op->oq_bind.rb_cred, &m.sml_values[0], &txt );
828 if ( m.sml_values[0].bv_val == NULL ) {
829 Debug( LDAP_DEBUG_ANY, "%s remoteauth_bind: "
830 "password hashing for '%s' failed, storing password in "
831 "plain text\n",
832 op->o_log_prefix, op->o_req_dn.bv_val );
833 ber_dupbv( &m.sml_values[0], &op->oq_bind.rb_cred );
834 }
835
836 /*
837 * If this server is a shadow use the frontend to perform this
838 * modify. That will trigger the update referral, which can then be
839 * forwarded by the chain overlay. Obviously the updateref and
840 * chain overlay must be configured appropriately for this to be
841 * useful.
842 */
843 if ( SLAP_SHADOW(op->o_bd) ) {
844 op2.o_bd = frontendDB;
845 } else {
846 op2.o_bd->bd_info = (BackendInfo *)on->on_info;
847 }
848
849 if ( op2.o_bd->be_modify( &op2, &r2 ) != LDAP_SUCCESS ) {
850 Debug( LDAP_DEBUG_ANY, "%s remoteauth_bind: "
851 "attempt to store password in entry '%s' failed, "
852 "ignoring\n",
853 op->o_log_prefix, op->o_req_dn.bv_val );
854 }
855 ch_free( m.sml_values[0].bv_val );
856 }
857 goto exit;
858 }
859
860 if ( rc == LDAP_INVALID_CREDENTIALS ) {
861 Debug( LDAP_DEBUG_TRACE, "%s remoteauth_bind: "
862 "ldap_sasl_bind_s (%s) failed: invalid credentials\n",
863 op->o_log_prefix, ldap_url );
864 goto exit;
865 }
866
867 if ( tries < ap->retry_count ) {
868 Debug( LDAP_DEBUG_TRACE, "%s remoteauth_bind: "
869 "ldap_sasl_bind_s failed %s: %s (try #%d)\n",
870 op->o_log_prefix, ldap_url, ldap_err2string( rc ), tries );
871 if ( ld ) ldap_unbind_ext_s( ld, NULL, NULL );
872 tries++;
873 goto retry;
874 } else
875 goto exit;
876
877 exit:
878 if ( dn.bv_val ) {
879 op->o_tmpfree( dn.bv_val, op->o_tmpmemctx );
880 }
881 if ( e ) {
882 be_entry_release_r( op, e );
883 }
884 if ( ld ) ldap_unbind_ext_s( ld, NULL, NULL );
885 if ( ldap_url ) ch_free( ldap_url );
886 if ( realm ) ch_free( realm );
887 if ( rc == SLAP_CB_CONTINUE ) {
888 Debug( LDAP_DEBUG_TRACE, "%s remoteauth_bind: "
889 "continue\n", op->o_log_prefix );
890 return rc;
891 } else {
892 /* for rc == 0, frontend sends result */
893 if ( rc ) {
894 if ( rc > 0 ) {
895 Debug( LDAP_DEBUG_TRACE, "%s remoteauth_bind: "
896 "failed\n", op->o_log_prefix );
897 send_ldap_error( op, rs, rc, "remoteauth_bind failed" );
898 } else {
899 Debug( LDAP_DEBUG_TRACE, "%s remoteauth_bind: "
900 "operations error\n", op->o_log_prefix );
901 send_ldap_error( op, rs, LDAP_OPERATIONS_ERROR,
902 "remoteauth_bind operations error" );
903 }
904 }
905
906 return rs->sr_err;
907 }
908 }
909
910 static int
911 remoteauth_db_init( BackendDB *be, ConfigReply *cr )
912 {
913 slap_overinst *on = (slap_overinst *)be->bd_info;
914 ad_private *ap;
915
916 if ( SLAP_ISGLOBALOVERLAY(be) ) {
917 Debug( LDAP_DEBUG_ANY, "remoteauth_db_init: "
918 "remoteauth overlay must be instantiated within a "
919 "database.\n" );
920 return 1;
921 }
922
923 ap = ch_calloc( 1, sizeof(ad_private) );
924
925 ap->dn = NULL;
926 ap->dn_ad = NULL;
927 ap->domain_attr = NULL;
928 ap->domain_ad = NULL;
929
930 ap->up_ad = NULL;
931 ap->mappings = NULL;
932
933 ap->default_realm = NULL;
934 ap->default_domain = NULL;
935
936 ap->pins = NULL;
937
938 ap->up_set = 0;
939 ap->retry_count = 3;
940
941 on->on_bi.bi_private = ap;
942
943 return LDAP_SUCCESS;
944 }
945
946 static int
947 remoteauth_db_destroy( BackendDB *be, ConfigReply *cr )
948 {
949 slap_overinst *on = (slap_overinst *)be->bd_info;
950 ad_private *ap = (ad_private *)on->on_bi.bi_private;
951 ad_info *ai = ap->mappings;
952
953 while ( ai ) {
954 ad_info *next = ai->next;
955
956 if ( ai->domain ) ch_free( ai->domain );
957 if ( ai->realm ) ch_free( ai->realm );
958
959 ch_free( ai );
960 ai = next;
961 }
962
963 if ( ap->dn ) ch_free( ap->dn );
964 if ( ap->default_domain ) ch_free( ap->default_domain );
965 if ( ap->default_realm ) ch_free( ap->default_realm );
966 if ( ap->domain_attr ) ch_free( ap->domain_attr );
967
968 bindconf_free( &ap->ad_tls );
969
970 ch_free( ap );
971
972 return 0;
973 }
974
975 static slap_overinst remoteauth;
976
977 int
978 remoteauth_initialize( void )
979 {
980 int rc;
981
982 remoteauth.on_bi.bi_type = "remoteauth";
983 remoteauth.on_bi.bi_flags = SLAPO_BFLAG_SINGLE;
984
985 remoteauth.on_bi.bi_cf_ocs = remoteauthocs;
986 rc = config_register_schema( remoteauthcfg, remoteauthocs );
987 if ( rc ) return rc;
988
989 remoteauth.on_bi.bi_db_init = remoteauth_db_init;
990 remoteauth.on_bi.bi_db_destroy = remoteauth_db_destroy;
991 remoteauth.on_bi.bi_op_bind = remoteauth_bind;
992
993 return overlay_register( &remoteauth );
994 }
995
996 #if SLAPD_OVER_ACCESSLOG == SLAPD_MOD_DYNAMIC
997 int
998 init_module( int argc, char *argv[] )
999 {
1000 return remoteauth_initialize();
1001 }
1002 #endif