1 /* $NetBSD: config.c,v 1.4 2025/09/05 21:16:25 christos Exp $ */ 2 3 /* config.c - configuration file handling routines */ 4 /* $OpenLDAP$ */ 5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 6 * 7 * Copyright 1998-2024 The OpenLDAP Foundation. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted only as authorized by the OpenLDAP 12 * Public License. 13 * 14 * A copy of this license is available in the file LICENSE in the 15 * top-level directory of the distribution or, alternatively, at 16 * <http://www.OpenLDAP.org/license.html>. 17 */ 18 /* Portions Copyright (c) 1995 Regents of the University of Michigan. 19 * All rights reserved. 20 * 21 * Redistribution and use in source and binary forms are permitted 22 * provided that this notice is preserved and that due credit is given 23 * to the University of Michigan at Ann Arbor. The name of the University 24 * may not be used to endorse or promote products derived from this 25 * software without specific prior written permission. This software 26 * is provided ``as is'' without express or implied warranty. 27 */ 28 29 #include <sys/cdefs.h> 30 __RCSID("$NetBSD: config.c,v 1.4 2025/09/05 21:16:25 christos Exp $"); 31 32 #include "portable.h" 33 34 #include <stdio.h> 35 36 #include <ac/string.h> 37 #include <ac/ctype.h> 38 #include <ac/signal.h> 39 #include <ac/socket.h> 40 #include <ac/errno.h> 41 #include <ac/unistd.h> 42 43 #include <sys/types.h> 44 #include <sys/stat.h> 45 46 #ifndef S_ISREG 47 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG) 48 #endif 49 50 #include "slap.h" 51 #ifdef LDAP_SLAPI 52 #include "slapi/slapi.h" 53 #endif 54 #include "lutil.h" 55 #include "lutil_ldap.h" 56 #include "ldif.h" 57 #include "slap-config.h" 58 59 #ifdef _WIN32 60 #define LUTIL_ATOULX lutil_atoullx 61 #define Z "I" 62 #else 63 #define LUTIL_ATOULX lutil_atoulx 64 #define Z "z" 65 #endif 66 67 #define ARGS_STEP 512 68 69 /* 70 * defaults for various global variables 71 */ 72 slap_mask_t global_allows = 0; 73 slap_mask_t global_disallows = 0; 74 int global_gentlehup = 0; 75 int global_idletimeout = 0; 76 int global_writetimeout = 0; 77 char *global_host = NULL; 78 struct berval global_host_bv = BER_BVNULL; 79 char *global_realm = NULL; 80 char *sasl_host = NULL; 81 char *sasl_cbinding = NULL; 82 char **default_passwd_hash = NULL; 83 struct berval default_search_base = BER_BVNULL; 84 struct berval default_search_nbase = BER_BVNULL; 85 86 ber_len_t sockbuf_max_incoming = SLAP_SB_MAX_INCOMING_DEFAULT; 87 ber_len_t sockbuf_max_incoming_auth= SLAP_SB_MAX_INCOMING_AUTH; 88 89 int slap_conn_max_pending = SLAP_CONN_MAX_PENDING_DEFAULT; 90 int slap_conn_max_pending_auth = SLAP_CONN_MAX_PENDING_AUTH; 91 92 int slap_max_filter_depth = SLAP_MAX_FILTER_DEPTH_DEFAULT; 93 94 char *slapd_pid_file = NULL; 95 char *slapd_args_file = NULL; 96 97 int use_reverse_lookup = 0; 98 99 #ifdef LDAP_SLAPI 100 int slapi_plugins_used = 0; 101 #endif 102 103 static int fp_getline(FILE *fp, ConfigArgs *c); 104 static void fp_getline_init(ConfigArgs *c); 105 106 static char *strtok_quote(char *line, char *sep, char **quote_ptr, int *inquote); 107 static char *strtok_quote_ldif(char **line); 108 109 ConfigArgs * 110 new_config_args( BackendDB *be, const char *fname, int lineno, int argc, char **argv ) 111 { 112 ConfigArgs *c; 113 c = ch_calloc( 1, sizeof( ConfigArgs ) ); 114 if ( c == NULL ) return(NULL); 115 c->be = be; 116 c->fname = fname; 117 c->argc = argc; 118 c->argv = argv; 119 c->lineno = lineno; 120 snprintf( c->log, sizeof( c->log ), "%s: line %d", fname, lineno ); 121 return(c); 122 } 123 124 void 125 init_config_argv( ConfigArgs *c ) 126 { 127 c->argv = ch_calloc( ARGS_STEP + 1, sizeof( *c->argv ) ); 128 c->argv_size = ARGS_STEP + 1; 129 } 130 131 ConfigTable *config_find_keyword(ConfigTable *Conf, ConfigArgs *c) { 132 int i; 133 134 for(i = 0; Conf[i].name; i++) 135 if( (Conf[i].length && (!strncasecmp(c->argv[0], Conf[i].name, Conf[i].length))) || 136 (!strcasecmp(c->argv[0], Conf[i].name)) ) break; 137 if ( !Conf[i].name ) return NULL; 138 if (( Conf[i].arg_type & ARGS_TYPES ) == ARG_BINARY ) { 139 size_t decode_len = LUTIL_BASE64_DECODE_LEN(c->linelen); 140 ch_free( c->tline ); 141 c->tline = ch_malloc( decode_len+1 ); 142 c->linelen = lutil_b64_pton( c->line, c->tline, decode_len ); 143 if ( c->linelen < 0 ) 144 { 145 ch_free( c->tline ); 146 c->tline = NULL; 147 return NULL; 148 } 149 c->line = c->tline; 150 } 151 c->ca_desc = Conf+i; 152 return c->ca_desc; 153 } 154 155 int config_check_vals(ConfigTable *Conf, ConfigArgs *c, int check_only ) { 156 int rc, arg_user, arg_type, arg_syn, iarg; 157 unsigned uiarg; 158 long larg; 159 unsigned long ularg; 160 ber_len_t barg; 161 162 if(Conf->arg_type == ARG_IGNORED) { 163 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n", 164 c->log, Conf->name ); 165 return(0); 166 } 167 arg_type = Conf->arg_type & ARGS_TYPES; 168 arg_user = Conf->arg_type & ARGS_USERLAND; 169 arg_syn = Conf->arg_type & ARGS_SYNTAX; 170 171 if((arg_type == ARG_DN) && c->argc == 1) { 172 c->argc = 2; 173 c->argv[1] = ""; 174 } 175 if(Conf->min_args && (c->argc < Conf->min_args)) { 176 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> missing <%s> argument", 177 c->argv[0], Conf->what ? Conf->what : "" ); 178 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n", c->log, c->cr_msg ); 179 return(ARG_BAD_CONF); 180 } 181 if(Conf->max_args && (c->argc > Conf->max_args)) { 182 char *ignored = " ignored"; 183 184 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> extra cruft after <%s>", 185 c->argv[0], Conf->what ); 186 187 ignored = ""; 188 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s%s.\n", 189 c->log, c->cr_msg, ignored ); 190 return(ARG_BAD_CONF); 191 } 192 if((arg_syn & ARG_DB) && !c->be) { 193 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> only allowed within database declaration", 194 c->argv[0] ); 195 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n", 196 c->log, c->cr_msg ); 197 return(ARG_BAD_CONF); 198 } 199 if((arg_syn & ARG_PRE_BI) && c->bi) { 200 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> must occur before any backend %sdeclaration", 201 c->argv[0], (arg_syn & ARG_PRE_DB) ? "or database " : "" ); 202 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n", 203 c->log, c->cr_msg ); 204 return(ARG_BAD_CONF); 205 } 206 if((arg_syn & ARG_PRE_DB) && c->be && c->be != frontendDB) { 207 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> must occur before any database declaration", 208 c->argv[0] ); 209 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: keyword %s\n", 210 c->log, c->cr_msg ); 211 return(ARG_BAD_CONF); 212 } 213 if((arg_syn & ARG_PAREN) && *c->argv[1] != '(' /*')'*/) { 214 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> old format not supported", c->argv[0] ); 215 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n", 216 c->log, c->cr_msg ); 217 return(ARG_BAD_CONF); 218 } 219 if(arg_type && !Conf->arg_item && !(arg_syn & ARG_OFFSET)) { 220 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid config_table, arg_item is NULL", 221 c->argv[0] ); 222 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n", 223 c->log, c->cr_msg ); 224 return(ARG_BAD_CONF); 225 } 226 c->type = arg_user; 227 memset(&c->values, 0, sizeof(c->values)); 228 if(arg_type == ARG_STRING) { 229 assert( c->argc == 2 ); 230 if ( !check_only ) 231 c->value_string = ch_strdup(c->argv[1]); 232 } else if(arg_type == ARG_BERVAL) { 233 assert( c->argc == 2 ); 234 if ( !check_only ) 235 ber_str2bv( c->argv[1], 0, 1, &c->value_bv ); 236 } else if(arg_type == ARG_BINARY) { 237 assert( c->argc == 2 ); 238 if ( !check_only ) { 239 c->value_bv.bv_len = c->linelen; 240 c->value_bv.bv_val = ch_malloc( c->linelen ); 241 AC_MEMCPY( c->value_bv.bv_val, c->line, c->linelen ); 242 } 243 } else if(arg_type == ARG_DN) { 244 struct berval bv; 245 assert( c->argc == 2 ); 246 ber_str2bv( c->argv[1], 0, 0, &bv ); 247 rc = dnPrettyNormal( NULL, &bv, &c->value_dn, &c->value_ndn, NULL ); 248 if ( rc != LDAP_SUCCESS ) { 249 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid DN %d (%s)", 250 c->argv[0], rc, ldap_err2string( rc )); 251 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n" , c->log, c->cr_msg ); 252 return(ARG_BAD_CONF); 253 } 254 if ( check_only ) { 255 ch_free( c->value_ndn.bv_val ); 256 ch_free( c->value_dn.bv_val ); 257 } 258 } else if(arg_type == ARG_ATDESC) { 259 const char *text = NULL; 260 assert( c->argc == 2 ); 261 c->value_ad = NULL; 262 rc = slap_str2ad( c->argv[1], &c->value_ad, &text ); 263 if ( rc != LDAP_SUCCESS ) { 264 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid AttributeDescription %d (%s)", 265 c->argv[0], rc, text ); 266 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n" , c->log, c->cr_msg ); 267 return(ARG_BAD_CONF); 268 } 269 } else { /* all numeric */ 270 int j; 271 iarg = 0; larg = 0; barg = 0; 272 switch(arg_type) { 273 case ARG_INT: 274 assert( c->argc == 2 ); 275 if ( lutil_atoix( &iarg, c->argv[1], 0 ) != 0 ) { 276 snprintf( c->cr_msg, sizeof( c->cr_msg ), 277 "<%s> unable to parse \"%s\" as int", 278 c->argv[0], c->argv[1] ); 279 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n", 280 c->log, c->cr_msg ); 281 return(ARG_BAD_CONF); 282 } 283 break; 284 case ARG_UINT: 285 assert( c->argc == 2 ); 286 if ( lutil_atoux( &uiarg, c->argv[1], 0 ) != 0 ) { 287 snprintf( c->cr_msg, sizeof( c->cr_msg ), 288 "<%s> unable to parse \"%s\" as unsigned int", 289 c->argv[0], c->argv[1] ); 290 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n", 291 c->log, c->cr_msg ); 292 return(ARG_BAD_CONF); 293 } 294 break; 295 case ARG_LONG: 296 assert( c->argc == 2 ); 297 if ( lutil_atolx( &larg, c->argv[1], 0 ) != 0 ) { 298 snprintf( c->cr_msg, sizeof( c->cr_msg ), 299 "<%s> unable to parse \"%s\" as long", 300 c->argv[0], c->argv[1] ); 301 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n", 302 c->log, c->cr_msg ); 303 return(ARG_BAD_CONF); 304 } 305 break; 306 case ARG_ULONG: 307 assert( c->argc == 2 ); 308 if ( LUTIL_ATOULX( &ularg, c->argv[1], 0 ) != 0 ) { 309 snprintf( c->cr_msg, sizeof( c->cr_msg ), 310 "<%s> unable to parse \"%s\" as unsigned long", 311 c->argv[0], c->argv[1] ); 312 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n", 313 c->log, c->cr_msg ); 314 return(ARG_BAD_CONF); 315 } 316 break; 317 case ARG_BER_LEN_T: { 318 unsigned long l; 319 assert( c->argc == 2 ); 320 if ( lutil_atoulx( &l, c->argv[1], 0 ) != 0 ) { 321 snprintf( c->cr_msg, sizeof( c->cr_msg ), 322 "<%s> unable to parse \"%s\" as ber_len_t", 323 c->argv[0], c->argv[1] ); 324 Debug(LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "%s: %s\n", 325 c->log, c->cr_msg ); 326 return(ARG_BAD_CONF); 327 } 328 barg = (ber_len_t)l; 329 } break; 330 case ARG_ON_OFF: 331 /* note: this is an explicit exception 332 * to the "need exactly 2 args" rule */ 333 if (c->argc == 1) { 334 iarg = 1; 335 } else if ( !strcasecmp(c->argv[1], "on") || 336 !strcasecmp(c->argv[1], "true") || 337 !strcasecmp(c->argv[1], "yes") ) 338 { 339 iarg = 1; 340 } else if ( !strcasecmp(c->argv[1], "off") || 341 !strcasecmp(c->argv[1], "false") || 342 !strcasecmp(c->argv[1], "no") ) 343 { 344 iarg = 0; 345 } else { 346 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid value", 347 c->argv[0] ); 348 Debug(LDAP_DEBUG_ANY|LDAP_DEBUG_NONE, "%s: %s\n", 349 c->log, c->cr_msg ); 350 return(ARG_BAD_CONF); 351 } 352 break; 353 } 354 j = (arg_type & ARG_NONZERO) ? 1 : 0; 355 if(iarg < j && larg < j && barg < (unsigned)j ) { 356 larg = larg ? larg : (barg ? (long)barg : iarg); 357 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid value", 358 c->argv[0] ); 359 Debug(LDAP_DEBUG_ANY|LDAP_DEBUG_NONE, "%s: %s\n", 360 c->log, c->cr_msg ); 361 return(ARG_BAD_CONF); 362 } 363 switch(arg_type) { 364 case ARG_ON_OFF: 365 case ARG_INT: c->value_int = iarg; break; 366 case ARG_UINT: c->value_uint = uiarg; break; 367 case ARG_LONG: c->value_long = larg; break; 368 case ARG_ULONG: c->value_ulong = ularg; break; 369 case ARG_BER_LEN_T: c->value_ber_t = barg; break; 370 } 371 } 372 return 0; 373 } 374 375 int config_set_vals(ConfigTable *Conf, ConfigArgs *c) { 376 int rc, arg_type; 377 void *ptr = NULL; 378 379 arg_type = Conf->arg_type; 380 if(arg_type & ARG_MAGIC) { 381 if(!c->be) c->be = frontendDB; 382 c->cr_msg[0] = '\0'; 383 rc = (*((ConfigDriver*)Conf->arg_item))(c); 384 #if 0 385 if(c->be == frontendDB) c->be = NULL; 386 #endif 387 if(rc) { 388 if ( !c->cr_msg[0] ) { 389 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> handler exited with %d", 390 c->argv[0], rc ); 391 Debug( LDAP_DEBUG_ANY, "%s: %s!\n", 392 c->log, c->cr_msg ); 393 } 394 return(ARG_BAD_CONF); 395 } 396 return(0); 397 } 398 if(arg_type & ARG_OFFSET) { 399 if (c->be && c->table == Cft_Database) 400 ptr = c->be->be_private; 401 else if (c->bi) 402 ptr = c->bi->bi_private; 403 else { 404 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> offset is missing base pointer", 405 c->argv[0] ); 406 Debug( LDAP_DEBUG_ANY, "%s: %s!\n", 407 c->log, c->cr_msg ); 408 return(ARG_BAD_CONF); 409 } 410 ptr = (void *)((char *)ptr + (long)Conf->arg_item); 411 } else if (arg_type & ARGS_TYPES) { 412 ptr = Conf->arg_item; 413 } 414 if(arg_type & ARGS_TYPES) 415 switch(arg_type & ARGS_TYPES) { 416 case ARG_ON_OFF: 417 case ARG_INT: *(int*)ptr = c->value_int; break; 418 case ARG_UINT: *(unsigned*)ptr = c->value_uint; break; 419 case ARG_LONG: *(long*)ptr = c->value_long; break; 420 case ARG_ULONG: *(size_t*)ptr = c->value_ulong; break; 421 case ARG_BER_LEN_T: *(ber_len_t*)ptr = c->value_ber_t; break; 422 case ARG_STRING: { 423 char *cc = *(char**)ptr; 424 if(cc) { 425 if ((arg_type & ARG_UNIQUE) && c->op == SLAP_CONFIG_ADD ) { 426 Debug(LDAP_DEBUG_CONFIG, "%s: already set %s!\n", 427 c->log, Conf->name ); 428 return(ARG_BAD_CONF); 429 } 430 ch_free(cc); 431 } 432 *(char **)ptr = c->value_string; 433 break; 434 } 435 case ARG_BERVAL: 436 case ARG_BINARY: 437 *(struct berval *)ptr = c->value_bv; 438 break; 439 case ARG_ATDESC: 440 *(AttributeDescription **)ptr = c->value_ad; 441 break; 442 } 443 return(0); 444 } 445 446 int config_add_vals(ConfigTable *Conf, ConfigArgs *c) { 447 int rc, arg_type; 448 449 arg_type = Conf->arg_type; 450 if(arg_type == ARG_IGNORED) { 451 Debug(LDAP_DEBUG_CONFIG, "%s: keyword <%s> ignored\n", 452 c->log, Conf->name ); 453 return(0); 454 } 455 rc = config_check_vals( Conf, c, 0 ); 456 if ( rc ) return rc; 457 return config_set_vals( Conf, c ); 458 } 459 460 int 461 config_del_vals(ConfigTable *cf, ConfigArgs *c) 462 { 463 int rc = 0; 464 void *ptr; 465 466 if ( cf->arg_type & ARG_MAGIC ) { 467 c->argv[0] = cf->ad->ad_cname.bv_val; 468 c->op = LDAP_MOD_DELETE; 469 c->type = cf->arg_type & ARGS_USERLAND; 470 rc = (*((ConfigDriver*)cf->arg_item))(c); 471 return rc; 472 } 473 /* If there is no handler, just zero it */ 474 if ( cf->arg_type & ARG_OFFSET ) { 475 if ( c->be && c->table == Cft_Database ) 476 ptr = c->be->be_private; 477 else if ( c->bi ) 478 ptr = c->bi->bi_private; 479 else { 480 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> offset is missing base pointer", 481 c->argv[0] ); 482 Debug( LDAP_DEBUG_CONFIG, "%s: %s!\n", 483 c->log, c->cr_msg ); 484 return ARG_BAD_CONF; 485 } 486 ptr = (void *)((char *)ptr + (long)cf->arg_item); 487 } else if ( cf->arg_type & ARGS_TYPES ) { 488 ptr = cf->arg_item; 489 } 490 if ( cf->arg_type & ARGS_TYPES ) 491 switch ( cf->arg_type & ARGS_TYPES ) { 492 case ARG_ON_OFF: 493 case ARG_INT: *(int *)ptr = cf->arg_default.v_int; break; 494 case ARG_UINT: *(unsigned *)ptr = cf->arg_default.v_uint; break; 495 case ARG_LONG: *(long *)ptr = cf->arg_default.v_long; break; 496 case ARG_ULONG: *(size_t *)ptr = cf->arg_default.v_ulong; break; 497 case ARG_BER_LEN_T: *(ber_len_t *)ptr = cf->arg_default.v_ber_t; break; 498 case ARG_STRING: 499 ch_free( *(char**)ptr ); 500 if ( cf->arg_default.v_string ) { 501 *(char **)ptr = ch_strdup( cf->arg_default.v_string ); 502 } else { 503 *(char **)ptr = NULL; 504 } 505 break; 506 case ARG_BERVAL: 507 case ARG_BINARY: 508 ch_free( ((struct berval *)ptr)->bv_val ); 509 if ( !BER_BVISNULL( &cf->arg_default.v_bv ) ) { 510 ber_dupbv( (struct berval *)ptr, &cf->arg_default.v_bv ); 511 } else { 512 BER_BVZERO( (struct berval *)ptr ); 513 } 514 break; 515 case ARG_ATDESC: 516 *(AttributeDescription **)ptr = cf->arg_default.v_ad; 517 break; 518 } 519 return rc; 520 } 521 522 int 523 config_get_vals(ConfigTable *cf, ConfigArgs *c) 524 { 525 int rc = 0; 526 struct berval bv; 527 void *ptr; 528 529 if ( cf->arg_type & ARG_IGNORED ) { 530 return 1; 531 } 532 533 memset(&c->values, 0, sizeof(c->values)); 534 c->rvalue_vals = NULL; 535 c->rvalue_nvals = NULL; 536 c->op = SLAP_CONFIG_EMIT; 537 c->type = cf->arg_type & ARGS_USERLAND; 538 539 if ( cf->arg_type & ARG_MAGIC ) { 540 rc = (*((ConfigDriver*)cf->arg_item))(c); 541 if ( rc ) return rc; 542 } else { 543 if ( cf->arg_type & ARG_OFFSET ) { 544 if (c->be && c->table == Cft_Database) 545 ptr = c->be->be_private; 546 else if ( c->bi ) 547 ptr = c->bi->bi_private; 548 else 549 return 1; 550 ptr = (void *)((char *)ptr + (long)cf->arg_item); 551 } else { 552 ptr = cf->arg_item; 553 } 554 555 switch(cf->arg_type & ARGS_TYPES) { 556 case ARG_ON_OFF: 557 case ARG_INT: c->value_int = *(int *)ptr; break; 558 case ARG_UINT: c->value_uint = *(unsigned *)ptr; break; 559 case ARG_LONG: c->value_long = *(long *)ptr; break; 560 case ARG_ULONG: c->value_ulong = *(size_t *)ptr; break; 561 case ARG_BER_LEN_T: c->value_ber_t = *(ber_len_t *)ptr; break; 562 case ARG_STRING: 563 if ( *(char **)ptr ) 564 c->value_string = ch_strdup(*(char **)ptr); 565 break; 566 case ARG_BERVAL: 567 c->value_bv = *((struct berval *)ptr); break; 568 case ARG_ATDESC: 569 c->value_ad = *(AttributeDescription **)ptr; break; 570 } 571 } 572 if ( cf->arg_type & ARGS_TYPES) { 573 bv.bv_len = 0; 574 bv.bv_val = c->log; 575 switch(cf->arg_type & ARGS_TYPES) { 576 case ARG_INT: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%d", c->value_int); break; 577 case ARG_UINT: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%u", c->value_uint); break; 578 case ARG_LONG: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_long); break; 579 case ARG_ULONG: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%" Z "u", c->value_ulong); break; 580 case ARG_BER_LEN_T: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%ld", c->value_ber_t); break; 581 case ARG_ON_OFF: bv.bv_len = snprintf(bv.bv_val, sizeof( c->log ), "%s", 582 c->value_int ? "TRUE" : "FALSE"); break; 583 case ARG_STRING: 584 if ( c->value_string && c->value_string[0]) { 585 ber_str2bv( c->value_string, 0, 0, &bv); 586 } else { 587 return 1; 588 } 589 break; 590 case ARG_BERVAL: 591 if ( !BER_BVISEMPTY( &c->value_bv )) { 592 bv = c->value_bv; 593 } else { 594 return 1; 595 } 596 break; 597 case ARG_ATDESC: 598 if ( c->value_ad ) { 599 bv = c->value_ad->ad_cname; 600 } else { 601 return 1; 602 } 603 break; 604 default: 605 bv.bv_val = NULL; 606 break; 607 } 608 if (bv.bv_val == c->log && bv.bv_len >= sizeof( c->log ) ) { 609 return 1; 610 } 611 if (( cf->arg_type & ARGS_TYPES ) == ARG_STRING ) { 612 ber_bvarray_add(&c->rvalue_vals, &bv); 613 } else if ( !BER_BVISNULL( &bv ) ) { 614 value_add_one(&c->rvalue_vals, &bv); 615 } 616 /* else: maybe c->rvalue_vals already set? */ 617 } 618 return rc; 619 } 620 621 int 622 config_push_cleanup(ConfigArgs *ca, ConfigDriver *cleanup) 623 { 624 int i; 625 /* silently ignore redundant push */ 626 for (i=0; i < ca->num_cleanups; i++) { 627 if ( ca->cleanups[i] == cleanup ) 628 return 0; 629 } 630 631 if (ca->num_cleanups >= SLAP_CONFIG_CLEANUP_MAX) 632 return -1; 633 ca->cleanups[ca->num_cleanups++] = cleanup; 634 return 0; 635 } 636 637 int 638 config_run_cleanup(ConfigArgs *ca) 639 { 640 int i, rc = 0; 641 642 for (i=0; i < ca->num_cleanups; i++) { 643 rc = ca->cleanups[i](ca); 644 if (rc) 645 break; 646 } 647 return rc; 648 } 649 650 int 651 init_config_attrs(ConfigTable *ct) { 652 int i, code; 653 654 for (i=0; ct[i].name; i++ ) { 655 if ( !ct[i].attribute ) continue; 656 code = register_at( ct[i].attribute, &ct[i].ad, 1 ); 657 if ( code ) { 658 fprintf( stderr, "init_config_attrs: register_at failed\n" ); 659 return code; 660 } 661 if (( ct[i].arg_type & ARGS_TYPES ) == ARG_BINARY ) { 662 ldif_must_b64_encode_register( ct[i].ad->ad_cname.bv_val, 663 ct[i].ad->ad_type->sat_oid ); 664 } 665 } 666 667 return 0; 668 } 669 670 int 671 init_config_ocs( ConfigOCs *ocs ) { 672 int i, code; 673 674 for (i=0;ocs[i].co_def;i++) { 675 code = register_oc( ocs[i].co_def, &ocs[i].co_oc, 1 ); 676 if ( code ) { 677 fprintf( stderr, "init_config_ocs: register_oc failed\n" ); 678 return code; 679 } 680 } 681 return 0; 682 } 683 684 /* Split an LDIF line into space-separated tokens. Words may be grouped 685 * by quotes. A quoted string may begin in the middle of a word, but must 686 * end at the end of the word (be followed by whitespace or EOS). Any other 687 * quotes are passed through unchanged. All other characters are passed 688 * through unchanged. 689 */ 690 static char * 691 strtok_quote_ldif( char **line ) 692 { 693 char *beg, *ptr, *quote=NULL; 694 int inquote=0; 695 696 ptr = *line; 697 698 if ( !ptr || !*ptr ) 699 return NULL; 700 701 while( isspace( (unsigned char) *ptr )) ptr++; 702 703 if ( *ptr == '"' ) { 704 inquote = 1; 705 ptr++; 706 } 707 708 beg = ptr; 709 710 for (;*ptr;ptr++) { 711 if ( *ptr == '"' ) { 712 if ( inquote && ( !ptr[1] || isspace((unsigned char) ptr[1]))) { 713 *ptr++ = '\0'; 714 break; 715 } 716 inquote = 1; 717 quote = ptr; 718 continue; 719 } 720 if ( inquote ) 721 continue; 722 if ( isspace( (unsigned char) *ptr )) { 723 *ptr++ = '\0'; 724 break; 725 } 726 } 727 if ( quote ) { 728 while ( quote < ptr ) { 729 *quote = quote[1]; 730 quote++; 731 } 732 } 733 if ( !*ptr ) { 734 *line = NULL; 735 } else { 736 while ( isspace( (unsigned char) *ptr )) ptr++; 737 *line = ptr; 738 } 739 return beg; 740 } 741 742 void 743 config_parse_ldif( ConfigArgs *c ) 744 { 745 char *next; 746 c->tline = ch_strdup(c->line); 747 next = c->tline; 748 749 while ((c->argv[c->argc] = strtok_quote_ldif( &next )) != NULL) { 750 c->argc++; 751 if ( c->argc >= c->argv_size ) { 752 char **tmp = ch_realloc( c->argv, (c->argv_size + ARGS_STEP) * 753 sizeof( *c->argv )); 754 c->argv = tmp; 755 c->argv_size += ARGS_STEP; 756 } 757 } 758 c->argv[c->argc] = NULL; 759 } 760 761 int 762 config_parse_vals(ConfigTable *ct, ConfigArgs *c, int valx) 763 { 764 int rc = 0; 765 int arg_type = ct->arg_type & ARGS_TYPES; 766 767 snprintf( c->log, sizeof( c->log ), "%s: value #%d", 768 ct->ad->ad_cname.bv_val, valx ); 769 c->argc = 1; 770 c->argv[0] = ct->ad->ad_cname.bv_val; 771 772 if ( (( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) || 773 (arg_type == ARG_BERVAL || arg_type == ARG_BINARY)) { 774 c->argv[c->argc] = c->line; 775 c->argc++; 776 c->argv[c->argc] = NULL; 777 c->tline = NULL; 778 } else { 779 config_parse_ldif( c ); 780 } 781 rc = config_check_vals( ct, c, 1 ); 782 ch_free( c->tline ); 783 c->tline = NULL; 784 785 if ( rc ) 786 rc = LDAP_CONSTRAINT_VIOLATION; 787 788 return rc; 789 } 790 791 int 792 config_parse_add(ConfigTable *ct, ConfigArgs *c, int valx) 793 { 794 int rc = 0; 795 int arg_type = ct->arg_type & ARGS_TYPES; 796 797 snprintf( c->log, sizeof( c->log ), "%s: value #%d", 798 ct->ad->ad_cname.bv_val, valx ); 799 c->argc = 1; 800 c->argv[0] = ct->ad->ad_cname.bv_val; 801 802 if ( (( ct->arg_type & ARG_QUOTE ) && c->line[ 0 ] != '"' ) || 803 (arg_type == ARG_BERVAL || arg_type == ARG_BINARY)) { 804 c->argv[c->argc] = c->line; 805 c->argc++; 806 c->argv[c->argc] = NULL; 807 c->tline = NULL; 808 } else { 809 config_parse_ldif( c ); 810 } 811 c->op = LDAP_MOD_ADD; 812 rc = config_add_vals( ct, c ); 813 ch_free( c->tline ); 814 815 return rc; 816 } 817 818 int 819 read_config_file(const char *fname, int depth, ConfigArgs *cf, ConfigTable *cft) 820 { 821 FILE *fp; 822 ConfigTable *ct; 823 ConfigArgs *c; 824 int rc; 825 struct stat s; 826 827 c = ch_calloc( 1, sizeof( ConfigArgs ) ); 828 if ( c == NULL ) { 829 return 1; 830 } 831 832 if ( depth ) { 833 memcpy( c, cf, sizeof( ConfigArgs ) ); 834 } else { 835 c->depth = depth; /* XXX */ 836 c->bi = NULL; 837 c->be = NULL; 838 } 839 840 c->valx = -1; 841 c->fname = fname; 842 init_config_argv( c ); 843 844 if ( stat( fname, &s ) != 0 ) { 845 char ebuf[128]; 846 int saved_errno = errno; 847 ldap_syslog = 1; 848 Debug(LDAP_DEBUG_ANY, 849 "could not stat config file \"%s\": %s (%d)\n", 850 fname, AC_STRERROR_R( saved_errno, ebuf, sizeof(ebuf) ), saved_errno); 851 ch_free( c->argv ); 852 ch_free( c ); 853 return(1); 854 } 855 856 if ( !S_ISREG( s.st_mode ) ) { 857 ldap_syslog = 1; 858 Debug(LDAP_DEBUG_ANY, 859 "regular file expected, got \"%s\"\n", 860 fname ); 861 ch_free( c->argv ); 862 ch_free( c ); 863 return(1); 864 } 865 866 fp = fopen( fname, "r" ); 867 if ( fp == NULL ) { 868 char ebuf[128]; 869 int saved_errno = errno; 870 ldap_syslog = 1; 871 Debug(LDAP_DEBUG_ANY, 872 "could not open config file \"%s\": %s (%d)\n", 873 fname, AC_STRERROR_R( saved_errno, ebuf, sizeof(ebuf) ), saved_errno); 874 ch_free( c->argv ); 875 ch_free( c ); 876 return(1); 877 } 878 879 Debug(LDAP_DEBUG_CONFIG, "reading config file %s\n", fname ); 880 881 fp_getline_init(c); 882 883 c->tline = NULL; 884 885 while ( fp_getline( fp, c ) ) { 886 /* skip comments and blank lines */ 887 if ( c->line[0] == '#' || c->line[0] == '\0' ) { 888 continue; 889 } 890 891 snprintf( c->log, sizeof( c->log ), "%s: line %d", 892 c->fname, c->lineno ); 893 894 c->argc = 0; 895 ch_free( c->tline ); 896 if ( config_fp_parse_line( c ) ) { 897 rc = 1; 898 goto done; 899 } 900 901 if ( c->argc < 1 ) { 902 Debug( LDAP_DEBUG_ANY, "%s: bad config line.\n", 903 c->log ); 904 rc = 1; 905 goto done; 906 } 907 908 c->op = SLAP_CONFIG_ADD; 909 910 ct = config_find_keyword( cft, c ); 911 if ( ct ) { 912 c->table = Cft_Global; 913 rc = config_add_vals( ct, c ); 914 if ( !rc ) continue; 915 916 if ( rc & ARGS_USERLAND ) { 917 /* XXX a usertype would be opaque here */ 918 Debug(LDAP_DEBUG_CONFIG, "%s: unknown user type <%s>\n", 919 c->log, c->argv[0] ); 920 rc = 1; 921 goto done; 922 923 } else if ( rc == ARG_BAD_CONF ) { 924 rc = 1; 925 goto done; 926 } 927 928 } else if ( ( c->bi && !c->be ) || ( c->bi && c->bi->bi_flags & SLAP_BFLAG_STANDALONE ) ) { 929 rc = SLAP_CONF_UNKNOWN; 930 if ( c->bi->bi_cf_ocs ) { 931 ct = config_find_keyword( c->bi->bi_cf_ocs->co_table, c ); 932 if ( ct ) { 933 c->table = c->bi->bi_cf_ocs->co_type; 934 rc = config_add_vals( ct, c ); 935 } 936 } 937 if ( c->bi->bi_config && rc == SLAP_CONF_UNKNOWN ) { 938 rc = (*c->bi->bi_config)(c->bi, c->fname, c->lineno, 939 c->argc, c->argv); 940 } 941 if ( rc ) { 942 switch(rc) { 943 case SLAP_CONF_UNKNOWN: 944 Debug( LDAP_DEBUG_ANY, "%s: unknown directive " 945 "<%s> inside backend info definition.\n", 946 c->log, *c->argv ); 947 default: 948 rc = 1; 949 goto done; 950 } 951 } 952 953 } else if ( c->be && c->be != frontendDB ) { 954 rc = SLAP_CONF_UNKNOWN; 955 if ( c->be->be_cf_ocs ) { 956 ct = config_find_keyword( c->be->be_cf_ocs->co_table, c ); 957 if ( ct ) { 958 c->table = c->be->be_cf_ocs->co_type; 959 rc = config_add_vals( ct, c ); 960 } 961 } 962 if ( c->be->be_config && rc == SLAP_CONF_UNKNOWN ) { 963 rc = (*c->be->be_config)(c->be, c->fname, c->lineno, 964 c->argc, c->argv); 965 } 966 if ( rc == SLAP_CONF_UNKNOWN && SLAP_ISGLOBALOVERLAY( frontendDB ) ) 967 { 968 /* global overlays may need 969 * definitions inside other databases... 970 */ 971 rc = (*frontendDB->be_config)( frontendDB, 972 c->fname, (int)c->lineno, c->argc, c->argv ); 973 } 974 975 switch ( rc ) { 976 case 0: 977 break; 978 979 case SLAP_CONF_UNKNOWN: 980 Debug( LDAP_DEBUG_ANY, "%s: unknown directive " 981 "<%s> inside backend database definition.\n", 982 c->log, *c->argv ); 983 984 default: 985 rc = 1; 986 goto done; 987 } 988 989 } else if ( frontendDB->be_config ) { 990 rc = (*frontendDB->be_config)( frontendDB, 991 c->fname, (int)c->lineno, c->argc, c->argv); 992 if ( rc ) { 993 switch(rc) { 994 case SLAP_CONF_UNKNOWN: 995 Debug( LDAP_DEBUG_ANY, "%s: unknown directive " 996 "<%s> inside global database definition.\n", 997 c->log, *c->argv ); 998 999 default: 1000 rc = 1; 1001 goto done; 1002 } 1003 } 1004 1005 } else { 1006 Debug( LDAP_DEBUG_ANY, "%s: unknown directive " 1007 "<%s> outside backend info and database definitions.\n", 1008 c->log, *c->argv ); 1009 rc = 1; 1010 goto done; 1011 } 1012 } 1013 1014 rc = 0; 1015 1016 done: 1017 if ( cf ) { 1018 cf->be = c->be; 1019 cf->bi = c->bi; 1020 } 1021 ch_free(c->tline); 1022 fclose(fp); 1023 ch_free(c->argv); 1024 ch_free(c); 1025 return(rc); 1026 } 1027 1028 /* restrictops, allows, disallows, requires, loglevel */ 1029 1030 static slap_verbmasks slap_ldap_response_code_[] = { 1031 { BER_BVC("success"), LDAP_SUCCESS }, 1032 1033 { BER_BVC("operationsError"), LDAP_OPERATIONS_ERROR }, 1034 { BER_BVC("protocolError"), LDAP_PROTOCOL_ERROR }, 1035 { BER_BVC("timelimitExceeded"), LDAP_TIMELIMIT_EXCEEDED }, 1036 { BER_BVC("sizelimitExceeded"), LDAP_SIZELIMIT_EXCEEDED }, 1037 { BER_BVC("compareFalse"), LDAP_COMPARE_FALSE }, 1038 { BER_BVC("compareTrue"), LDAP_COMPARE_TRUE }, 1039 1040 { BER_BVC("authMethodNotSupported"), LDAP_AUTH_METHOD_NOT_SUPPORTED }, 1041 { BER_BVC("strongAuthNotSupported"), LDAP_STRONG_AUTH_NOT_SUPPORTED }, 1042 { BER_BVC("strongAuthRequired"), LDAP_STRONG_AUTH_REQUIRED }, 1043 { BER_BVC("strongerAuthRequired"), LDAP_STRONGER_AUTH_REQUIRED }, 1044 #if 0 /* not LDAPv3 */ 1045 { BER_BVC("partialResults"), LDAP_PARTIAL_RESULTS }, 1046 #endif 1047 1048 { BER_BVC("referral"), LDAP_REFERRAL }, 1049 { BER_BVC("adminlimitExceeded"), LDAP_ADMINLIMIT_EXCEEDED }, 1050 { BER_BVC("unavailableCriticalExtension"), LDAP_UNAVAILABLE_CRITICAL_EXTENSION }, 1051 { BER_BVC("confidentialityRequired"), LDAP_CONFIDENTIALITY_REQUIRED }, 1052 { BER_BVC("saslBindInProgress"), LDAP_SASL_BIND_IN_PROGRESS }, 1053 1054 { BER_BVC("noSuchAttribute"), LDAP_NO_SUCH_ATTRIBUTE }, 1055 { BER_BVC("undefinedType"), LDAP_UNDEFINED_TYPE }, 1056 { BER_BVC("inappropriateMatching"), LDAP_INAPPROPRIATE_MATCHING }, 1057 { BER_BVC("constraintViolation"), LDAP_CONSTRAINT_VIOLATION }, 1058 { BER_BVC("typeOrValueExists"), LDAP_TYPE_OR_VALUE_EXISTS }, 1059 { BER_BVC("invalidSyntax"), LDAP_INVALID_SYNTAX }, 1060 1061 { BER_BVC("noSuchObject"), LDAP_NO_SUCH_OBJECT }, 1062 { BER_BVC("aliasProblem"), LDAP_ALIAS_PROBLEM }, 1063 { BER_BVC("invalidDnSyntax"), LDAP_INVALID_DN_SYNTAX }, 1064 #if 0 /* not LDAPv3 */ 1065 { BER_BVC("isLeaf"), LDAP_IS_LEAF }, 1066 #endif 1067 { BER_BVC("aliasDerefProblem"), LDAP_ALIAS_DEREF_PROBLEM }, 1068 1069 { BER_BVC("proxyAuthzFailure"), LDAP_X_PROXY_AUTHZ_FAILURE }, 1070 { BER_BVC("inappropriateAuth"), LDAP_INAPPROPRIATE_AUTH }, 1071 { BER_BVC("invalidCredentials"), LDAP_INVALID_CREDENTIALS }, 1072 { BER_BVC("insufficientAccess"), LDAP_INSUFFICIENT_ACCESS }, 1073 1074 { BER_BVC("busy"), LDAP_BUSY }, 1075 { BER_BVC("unavailable"), LDAP_UNAVAILABLE }, 1076 { BER_BVC("unwillingToPerform"), LDAP_UNWILLING_TO_PERFORM }, 1077 { BER_BVC("loopDetect"), LDAP_LOOP_DETECT }, 1078 1079 { BER_BVC("namingViolation"), LDAP_NAMING_VIOLATION }, 1080 { BER_BVC("objectClassViolation"), LDAP_OBJECT_CLASS_VIOLATION }, 1081 { BER_BVC("notAllowedOnNonleaf"), LDAP_NOT_ALLOWED_ON_NONLEAF }, 1082 { BER_BVC("notAllowedOnRdn"), LDAP_NOT_ALLOWED_ON_RDN }, 1083 { BER_BVC("alreadyExists"), LDAP_ALREADY_EXISTS }, 1084 { BER_BVC("noObjectClassMods"), LDAP_NO_OBJECT_CLASS_MODS }, 1085 { BER_BVC("resultsTooLarge"), LDAP_RESULTS_TOO_LARGE }, 1086 { BER_BVC("affectsMultipleDsas"), LDAP_AFFECTS_MULTIPLE_DSAS }, 1087 1088 { BER_BVC("other"), LDAP_OTHER }, 1089 1090 /* extension-specific */ 1091 1092 { BER_BVC("cupResourcesExhausted"), LDAP_CUP_RESOURCES_EXHAUSTED }, 1093 { BER_BVC("cupSecurityViolation"), LDAP_CUP_SECURITY_VIOLATION }, 1094 { BER_BVC("cupInvalidData"), LDAP_CUP_INVALID_DATA }, 1095 { BER_BVC("cupUnsupportedScheme"), LDAP_CUP_UNSUPPORTED_SCHEME }, 1096 { BER_BVC("cupReloadRequired"), LDAP_CUP_RELOAD_REQUIRED }, 1097 1098 { BER_BVC("cancelled"), LDAP_CANCELLED }, 1099 { BER_BVC("noSuchOperation"), LDAP_NO_SUCH_OPERATION }, 1100 { BER_BVC("tooLate"), LDAP_TOO_LATE }, 1101 { BER_BVC("cannotCancel"), LDAP_CANNOT_CANCEL }, 1102 1103 { BER_BVC("assertionFailed"), LDAP_ASSERTION_FAILED }, 1104 1105 { BER_BVC("proxiedAuthorizationDenied"), LDAP_PROXIED_AUTHORIZATION_DENIED }, 1106 1107 { BER_BVC("syncRefreshRequired"), LDAP_SYNC_REFRESH_REQUIRED }, 1108 1109 { BER_BVC("noOperation"), LDAP_X_NO_OPERATION }, 1110 1111 { BER_BVNULL, 0 } 1112 }; 1113 1114 slap_verbmasks *slap_ldap_response_code = slap_ldap_response_code_; 1115 1116 int 1117 slap_ldap_response_code_register( struct berval *bv, int err ) 1118 { 1119 return slap_verbmask_register( slap_ldap_response_code_, 1120 &slap_ldap_response_code, bv, err ); 1121 } 1122 1123 #ifdef HAVE_TLS 1124 static slap_verbmasks tlskey[] = { 1125 { BER_BVC("no"), SB_TLS_OFF }, 1126 { BER_BVC("yes"), SB_TLS_ON }, 1127 { BER_BVC("critical"), SB_TLS_CRITICAL }, 1128 { BER_BVNULL, 0 } 1129 }; 1130 1131 static slap_verbmasks crlkeys[] = { 1132 { BER_BVC("none"), LDAP_OPT_X_TLS_CRL_NONE }, 1133 { BER_BVC("peer"), LDAP_OPT_X_TLS_CRL_PEER }, 1134 { BER_BVC("all"), LDAP_OPT_X_TLS_CRL_ALL }, 1135 { BER_BVNULL, 0 } 1136 }; 1137 1138 static slap_verbmasks vfykeys[] = { 1139 { BER_BVC("never"), LDAP_OPT_X_TLS_NEVER }, 1140 { BER_BVC("allow"), LDAP_OPT_X_TLS_ALLOW }, 1141 { BER_BVC("try"), LDAP_OPT_X_TLS_TRY }, 1142 { BER_BVC("demand"), LDAP_OPT_X_TLS_DEMAND }, 1143 { BER_BVC("hard"), LDAP_OPT_X_TLS_HARD }, 1144 { BER_BVC("true"), LDAP_OPT_X_TLS_HARD }, 1145 { BER_BVNULL, 0 } 1146 }; 1147 #endif 1148 1149 static slap_verbmasks methkey[] = { 1150 { BER_BVC("none"), LDAP_AUTH_NONE }, 1151 { BER_BVC("simple"), LDAP_AUTH_SIMPLE }, 1152 #ifdef HAVE_CYRUS_SASL 1153 { BER_BVC("sasl"), LDAP_AUTH_SASL }, 1154 #endif 1155 { BER_BVNULL, 0 } 1156 }; 1157 1158 static slap_verbmasks versionkey[] = { 1159 { BER_BVC("2"), LDAP_VERSION2 }, 1160 { BER_BVC("3"), LDAP_VERSION3 }, 1161 { BER_BVNULL, 0 } 1162 }; 1163 1164 int 1165 slap_keepalive_parse( 1166 struct berval *val, 1167 void *bc, 1168 slap_cf_aux_table *tab0, 1169 const char *tabmsg, 1170 int unparse ) 1171 { 1172 if ( unparse ) { 1173 slap_keepalive *sk = (slap_keepalive *)bc; 1174 int rc = snprintf( val->bv_val, val->bv_len, "%d:%d:%d", 1175 sk->sk_idle, sk->sk_probes, sk->sk_interval ); 1176 if ( rc < 0 ) { 1177 return -1; 1178 } 1179 1180 if ( (unsigned)rc >= val->bv_len ) { 1181 return -1; 1182 } 1183 1184 val->bv_len = rc; 1185 1186 } else { 1187 char *s = val->bv_val; 1188 char *next; 1189 slap_keepalive *sk = (slap_keepalive *)bc; 1190 slap_keepalive sk2; 1191 1192 if ( s[0] == ':' ) { 1193 sk2.sk_idle = 0; 1194 s++; 1195 1196 } else { 1197 sk2.sk_idle = strtol( s, &next, 10 ); 1198 if ( next == s || next[0] != ':' ) { 1199 return -1; 1200 } 1201 1202 if ( sk2.sk_idle < 0 ) { 1203 return -1; 1204 } 1205 1206 s = ++next; 1207 } 1208 1209 if ( s[0] == ':' ) { 1210 sk2.sk_probes = 0; 1211 s++; 1212 1213 } else { 1214 sk2.sk_probes = strtol( s, &next, 10 ); 1215 if ( next == s || next[0] != ':' ) { 1216 return -1; 1217 } 1218 1219 if ( sk2.sk_probes < 0 ) { 1220 return -1; 1221 } 1222 1223 s = ++next; 1224 } 1225 1226 if ( *s == '\0' ) { 1227 sk2.sk_interval = 0; 1228 1229 } else { 1230 sk2.sk_interval = strtol( s, &next, 10 ); 1231 if ( next == s || next[0] != '\0' ) { 1232 return -1; 1233 } 1234 1235 if ( sk2.sk_interval < 0 ) { 1236 return -1; 1237 } 1238 } 1239 1240 *sk = sk2; 1241 1242 ber_memfree( val->bv_val ); 1243 BER_BVZERO( val ); 1244 } 1245 1246 return 0; 1247 } 1248 1249 static int 1250 slap_sb_uri( 1251 struct berval *val, 1252 void *bcp, 1253 slap_cf_aux_table *tab0, 1254 const char *tabmsg, 1255 int unparse ) 1256 { 1257 slap_bindconf *bc = bcp; 1258 if ( unparse ) { 1259 if ( bc->sb_uri.bv_len >= val->bv_len ) 1260 return -1; 1261 val->bv_len = bc->sb_uri.bv_len; 1262 AC_MEMCPY( val->bv_val, bc->sb_uri.bv_val, val->bv_len ); 1263 } else { 1264 bc->sb_uri = *val; 1265 #ifdef HAVE_TLS 1266 if ( ldap_is_ldaps_url( val->bv_val )) 1267 bc->sb_tls_do_init = 1; 1268 #endif 1269 } 1270 return 0; 1271 } 1272 1273 static slap_cf_aux_table bindkey[] = { 1274 { BER_BVC("uri="), 0, 'x', 1, slap_sb_uri }, 1275 { BER_BVC("version="), offsetof(slap_bindconf, sb_version), 'i', 0, versionkey }, 1276 { BER_BVC("bindmethod="), offsetof(slap_bindconf, sb_method), 'i', 0, methkey }, 1277 { BER_BVC("timeout="), offsetof(slap_bindconf, sb_timeout_api), 'i', 0, NULL }, 1278 { BER_BVC("network-timeout="), offsetof(slap_bindconf, sb_timeout_net), 'i', 0, NULL }, 1279 { BER_BVC("binddn="), offsetof(slap_bindconf, sb_binddn), 'b', 1, (slap_verbmasks *)dnNormalize }, 1280 { BER_BVC("credentials="), offsetof(slap_bindconf, sb_cred), 'b', 1, NULL }, 1281 { BER_BVC("saslmech="), offsetof(slap_bindconf, sb_saslmech), 'b', 0, NULL }, 1282 { BER_BVC("secprops="), offsetof(slap_bindconf, sb_secprops), 's', 0, NULL }, 1283 { BER_BVC("realm="), offsetof(slap_bindconf, sb_realm), 'b', 0, NULL }, 1284 { BER_BVC("authcID="), offsetof(slap_bindconf, sb_authcId), 'b', 1, NULL }, 1285 { BER_BVC("authzID="), offsetof(slap_bindconf, sb_authzId), 'b', 1, (slap_verbmasks *)authzNormalize }, 1286 { BER_BVC("keepalive="), offsetof(slap_bindconf, sb_keepalive), 'x', 0, (slap_verbmasks *)slap_keepalive_parse }, 1287 { BER_BVC("tcp-user-timeout="), offsetof(slap_bindconf, sb_tcp_user_timeout), 'u', 0, NULL }, 1288 #ifdef HAVE_TLS 1289 /* NOTE: replace "14" with the actual index 1290 * of the first TLS-related line */ 1291 #define aux_TLS (bindkey+14) /* beginning of TLS keywords */ 1292 1293 { BER_BVC("starttls="), offsetof(slap_bindconf, sb_tls), 'i', 0, tlskey }, 1294 { BER_BVC("tls_cert="), offsetof(slap_bindconf, sb_tls_cert), 's', 1, NULL }, 1295 { BER_BVC("tls_key="), offsetof(slap_bindconf, sb_tls_key), 's', 1, NULL }, 1296 { BER_BVC("tls_cacert="), offsetof(slap_bindconf, sb_tls_cacert), 's', 1, NULL }, 1297 { BER_BVC("tls_cacertdir="), offsetof(slap_bindconf, sb_tls_cacertdir), 's', 1, NULL }, 1298 { BER_BVC("tls_reqcert="), offsetof(slap_bindconf, sb_tls_reqcert), 's', 0, NULL }, 1299 { BER_BVC("tls_reqsan="), offsetof(slap_bindconf, sb_tls_reqsan), 's', 0, NULL }, 1300 { BER_BVC("tls_cipher_suite="), offsetof(slap_bindconf, sb_tls_cipher_suite), 's', 0, NULL }, 1301 { BER_BVC("tls_protocol_min="), offsetof(slap_bindconf, sb_tls_protocol_min), 's', 0, NULL }, 1302 { BER_BVC("tls_ecname="), offsetof(slap_bindconf, sb_tls_ecname), 's', 0, NULL }, 1303 #ifdef HAVE_OPENSSL 1304 { BER_BVC("tls_crlcheck="), offsetof(slap_bindconf, sb_tls_crlcheck), 's', 0, NULL }, 1305 #endif 1306 #endif 1307 { BER_BVNULL, 0, 0, 0, NULL } 1308 }; 1309 1310 /* 1311 * 's': char * 1312 * 'b': struct berval; if !NULL, normalize using ((slap_mr_normalize_func *)aux) 1313 * 'i': int; if !NULL, compute using ((slap_verbmasks *)aux) 1314 * 'u': unsigned 1315 * 'I': long 1316 * 'U': unsigned long 1317 */ 1318 1319 int 1320 slap_cf_aux_table_parse( const char *word, void *dst, slap_cf_aux_table *tab0, LDAP_CONST char *tabmsg ) 1321 { 1322 int rc = SLAP_CONF_UNKNOWN; 1323 slap_cf_aux_table *tab; 1324 1325 for ( tab = tab0; !BER_BVISNULL( &tab->key ); tab++ ) { 1326 if ( !strncasecmp( word, tab->key.bv_val, tab->key.bv_len ) ) { 1327 char **cptr; 1328 int *iptr, j; 1329 unsigned *uptr; 1330 long *lptr; 1331 unsigned long *ulptr; 1332 struct berval *bptr; 1333 const char *val = word + tab->key.bv_len; 1334 1335 switch ( tab->type ) { 1336 case 's': 1337 cptr = (char **)((char *)dst + tab->off); 1338 *cptr = ch_strdup( val ); 1339 rc = 0; 1340 break; 1341 1342 case 'b': 1343 bptr = (struct berval *)((char *)dst + tab->off); 1344 if ( tab->aux != NULL ) { 1345 struct berval dn; 1346 slap_mr_normalize_func *normalize = (slap_mr_normalize_func *)tab->aux; 1347 1348 ber_str2bv( val, 0, 0, &dn ); 1349 rc = normalize( 0, NULL, NULL, &dn, bptr, NULL ); 1350 1351 } else { 1352 ber_str2bv( val, 0, 1, bptr ); 1353 rc = 0; 1354 } 1355 break; 1356 1357 case 'i': 1358 iptr = (int *)((char *)dst + tab->off); 1359 1360 if ( tab->aux != NULL ) { 1361 slap_verbmasks *aux = (slap_verbmasks *)tab->aux; 1362 1363 assert( aux != NULL ); 1364 1365 rc = 1; 1366 for ( j = 0; !BER_BVISNULL( &aux[j].word ); j++ ) { 1367 if ( !strcasecmp( val, aux[j].word.bv_val ) ) { 1368 *iptr = aux[j].mask; 1369 rc = 0; 1370 break; 1371 } 1372 } 1373 1374 } else { 1375 rc = lutil_atoix( iptr, val, 0 ); 1376 } 1377 break; 1378 1379 case 'u': 1380 uptr = (unsigned *)((char *)dst + tab->off); 1381 1382 rc = lutil_atoux( uptr, val, 0 ); 1383 break; 1384 1385 case 'I': 1386 lptr = (long *)((char *)dst + tab->off); 1387 1388 rc = lutil_atolx( lptr, val, 0 ); 1389 break; 1390 1391 case 'U': 1392 ulptr = (unsigned long *)((char *)dst + tab->off); 1393 1394 rc = lutil_atoulx( ulptr, val, 0 ); 1395 break; 1396 1397 case 'x': 1398 if ( tab->aux != NULL ) { 1399 struct berval value; 1400 slap_cf_aux_table_parse_x *func = (slap_cf_aux_table_parse_x *)tab->aux; 1401 1402 ber_str2bv( val, 0, 1, &value ); 1403 1404 rc = func( &value, (void *)((char *)dst + tab->off), tab, tabmsg, 0 ); 1405 1406 } else { 1407 rc = 1; 1408 } 1409 break; 1410 } 1411 1412 if ( rc ) { 1413 Debug( LDAP_DEBUG_ANY, "invalid %s value %s\n", 1414 tabmsg, word ); 1415 } 1416 1417 return rc; 1418 } 1419 } 1420 1421 return rc; 1422 } 1423 1424 int 1425 slap_cf_aux_table_unparse( void *src, struct berval *bv, slap_cf_aux_table *tab0 ) 1426 { 1427 char buf[AC_LINE_MAX], *ptr; 1428 slap_cf_aux_table *tab; 1429 struct berval tmp; 1430 1431 ptr = buf; 1432 for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) { 1433 char **cptr; 1434 int *iptr, i; 1435 unsigned *uptr; 1436 long *lptr; 1437 unsigned long *ulptr; 1438 struct berval *bptr; 1439 1440 cptr = (char **)((char *)src + tab->off); 1441 1442 switch ( tab->type ) { 1443 case 'b': 1444 bptr = (struct berval *)((char *)src + tab->off); 1445 cptr = &bptr->bv_val; 1446 1447 case 's': 1448 if ( *cptr ) { 1449 *ptr++ = ' '; 1450 ptr = lutil_strcopy( ptr, tab->key.bv_val ); 1451 if ( tab->quote ) *ptr++ = '"'; 1452 ptr = lutil_strcopy( ptr, *cptr ); 1453 if ( tab->quote ) *ptr++ = '"'; 1454 } 1455 break; 1456 1457 case 'i': 1458 iptr = (int *)((char *)src + tab->off); 1459 1460 if ( tab->aux != NULL ) { 1461 slap_verbmasks *aux = (slap_verbmasks *)tab->aux; 1462 1463 for ( i = 0; !BER_BVISNULL( &aux[i].word ); i++ ) { 1464 if ( *iptr == aux[i].mask ) { 1465 *ptr++ = ' '; 1466 ptr = lutil_strcopy( ptr, tab->key.bv_val ); 1467 ptr = lutil_strcopy( ptr, aux[i].word.bv_val ); 1468 break; 1469 } 1470 } 1471 1472 } else { 1473 *ptr++ = ' '; 1474 ptr = lutil_strcopy( ptr, tab->key.bv_val ); 1475 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%d", *iptr ); 1476 } 1477 break; 1478 1479 case 'u': 1480 uptr = (unsigned *)((char *)src + tab->off); 1481 *ptr++ = ' '; 1482 ptr = lutil_strcopy( ptr, tab->key.bv_val ); 1483 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%u", *uptr ); 1484 break; 1485 1486 case 'I': 1487 lptr = (long *)((char *)src + tab->off); 1488 *ptr++ = ' '; 1489 ptr = lutil_strcopy( ptr, tab->key.bv_val ); 1490 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%ld", *lptr ); 1491 break; 1492 1493 case 'U': 1494 ulptr = (unsigned long *)((char *)src + tab->off); 1495 *ptr++ = ' '; 1496 ptr = lutil_strcopy( ptr, tab->key.bv_val ); 1497 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%lu", *ulptr ); 1498 break; 1499 1500 case 'x': 1501 { 1502 char *saveptr=ptr; 1503 *ptr++ = ' '; 1504 ptr = lutil_strcopy( ptr, tab->key.bv_val ); 1505 if ( tab->quote ) *ptr++ = '"'; 1506 if ( tab->aux != NULL ) { 1507 struct berval value; 1508 slap_cf_aux_table_parse_x *func = (slap_cf_aux_table_parse_x *)tab->aux; 1509 int rc; 1510 1511 value.bv_val = ptr; 1512 value.bv_len = buf + sizeof( buf ) - ptr; 1513 1514 rc = func( &value, (void *)((char *)src + tab->off), tab, "(unparse)", 1 ); 1515 if ( rc == 0 ) { 1516 if (value.bv_len) { 1517 ptr += value.bv_len; 1518 } else { 1519 ptr = saveptr; 1520 break; 1521 } 1522 } 1523 } 1524 if ( tab->quote ) *ptr++ = '"'; 1525 } 1526 break; 1527 1528 default: 1529 assert( 0 ); 1530 } 1531 } 1532 tmp.bv_val = buf; 1533 tmp.bv_len = ptr - buf; 1534 ber_dupbv( bv, &tmp ); 1535 return 0; 1536 } 1537 1538 int 1539 slap_tls_get_config( LDAP *ld, int opt, char **val ) 1540 { 1541 #ifdef HAVE_TLS 1542 slap_verbmasks *keys; 1543 int i, ival; 1544 1545 *val = NULL; 1546 switch( opt ) { 1547 case LDAP_OPT_X_TLS_CRLCHECK: 1548 keys = crlkeys; 1549 break; 1550 case LDAP_OPT_X_TLS_REQUIRE_CERT: 1551 keys = vfykeys; 1552 break; 1553 case LDAP_OPT_X_TLS_PROTOCOL_MIN: { 1554 char buf[8]; 1555 ldap_pvt_tls_get_option( ld, opt, &ival ); 1556 snprintf( buf, sizeof( buf ), "%d.%d", 1557 ( ival >> 8 ) & 0xff, ival & 0xff ); 1558 *val = ch_strdup( buf ); 1559 return 0; 1560 } 1561 default: 1562 return -1; 1563 } 1564 ldap_pvt_tls_get_option( ld, opt, &ival ); 1565 for (i=0; !BER_BVISNULL(&keys[i].word); i++) { 1566 if (keys[i].mask == ival) { 1567 *val = ch_strdup( keys[i].word.bv_val ); 1568 return 0; 1569 } 1570 } 1571 #endif 1572 return -1; 1573 } 1574 1575 int 1576 bindconf_tls_parse( const char *word, slap_bindconf *bc ) 1577 { 1578 #ifdef HAVE_TLS 1579 if ( slap_cf_aux_table_parse( word, bc, aux_TLS, "tls config" ) == 0 ) { 1580 bc->sb_tls_do_init = 1; 1581 return 0; 1582 } 1583 #endif 1584 return -1; 1585 } 1586 1587 int 1588 bindconf_tls_unparse( slap_bindconf *bc, struct berval *bv ) 1589 { 1590 #ifdef HAVE_TLS 1591 return slap_cf_aux_table_unparse( bc, bv, aux_TLS ); 1592 #endif 1593 return -1; 1594 } 1595 1596 int 1597 bindconf_parse( const char *word, slap_bindconf *bc ) 1598 { 1599 #ifdef HAVE_TLS 1600 /* Detect TLS config changes explicitly */ 1601 if ( bindconf_tls_parse( word, bc ) == 0 ) { 1602 return 0; 1603 } 1604 #endif 1605 return slap_cf_aux_table_parse( word, bc, bindkey, "bind config" ); 1606 } 1607 1608 int 1609 bindconf_unparse( slap_bindconf *bc, struct berval *bv ) 1610 { 1611 return slap_cf_aux_table_unparse( bc, bv, bindkey ); 1612 } 1613 1614 void bindconf_free( slap_bindconf *bc ) { 1615 if ( !BER_BVISNULL( &bc->sb_uri ) ) { 1616 ch_free( bc->sb_uri.bv_val ); 1617 BER_BVZERO( &bc->sb_uri ); 1618 } 1619 if ( !BER_BVISNULL( &bc->sb_binddn ) ) { 1620 ch_free( bc->sb_binddn.bv_val ); 1621 BER_BVZERO( &bc->sb_binddn ); 1622 } 1623 if ( !BER_BVISNULL( &bc->sb_cred ) ) { 1624 ch_free( bc->sb_cred.bv_val ); 1625 BER_BVZERO( &bc->sb_cred ); 1626 } 1627 if ( !BER_BVISNULL( &bc->sb_saslmech ) ) { 1628 ch_free( bc->sb_saslmech.bv_val ); 1629 BER_BVZERO( &bc->sb_saslmech ); 1630 } 1631 if ( bc->sb_secprops ) { 1632 ch_free( bc->sb_secprops ); 1633 bc->sb_secprops = NULL; 1634 } 1635 if ( !BER_BVISNULL( &bc->sb_realm ) ) { 1636 ch_free( bc->sb_realm.bv_val ); 1637 BER_BVZERO( &bc->sb_realm ); 1638 } 1639 if ( !BER_BVISNULL( &bc->sb_authcId ) ) { 1640 ch_free( bc->sb_authcId.bv_val ); 1641 BER_BVZERO( &bc->sb_authcId ); 1642 } 1643 if ( !BER_BVISNULL( &bc->sb_authzId ) ) { 1644 ch_free( bc->sb_authzId.bv_val ); 1645 BER_BVZERO( &bc->sb_authzId ); 1646 } 1647 #ifdef HAVE_TLS 1648 if ( bc->sb_tls_cert ) { 1649 ch_free( bc->sb_tls_cert ); 1650 bc->sb_tls_cert = NULL; 1651 } 1652 if ( bc->sb_tls_key ) { 1653 ch_free( bc->sb_tls_key ); 1654 bc->sb_tls_key = NULL; 1655 } 1656 if ( bc->sb_tls_cacert ) { 1657 ch_free( bc->sb_tls_cacert ); 1658 bc->sb_tls_cacert = NULL; 1659 } 1660 if ( bc->sb_tls_cacertdir ) { 1661 ch_free( bc->sb_tls_cacertdir ); 1662 bc->sb_tls_cacertdir = NULL; 1663 } 1664 if ( bc->sb_tls_reqcert ) { 1665 ch_free( bc->sb_tls_reqcert ); 1666 bc->sb_tls_reqcert = NULL; 1667 } 1668 if ( bc->sb_tls_reqsan ) { 1669 ch_free( bc->sb_tls_reqsan ); 1670 bc->sb_tls_reqsan = NULL; 1671 } 1672 if ( bc->sb_tls_cipher_suite ) { 1673 ch_free( bc->sb_tls_cipher_suite ); 1674 bc->sb_tls_cipher_suite = NULL; 1675 } 1676 if ( bc->sb_tls_protocol_min ) { 1677 ch_free( bc->sb_tls_protocol_min ); 1678 bc->sb_tls_protocol_min = NULL; 1679 } 1680 if ( bc->sb_tls_ecname ) { 1681 ch_free( bc->sb_tls_ecname ); 1682 bc->sb_tls_ecname = NULL; 1683 } 1684 #ifdef HAVE_OPENSSL 1685 if ( bc->sb_tls_crlcheck ) { 1686 ch_free( bc->sb_tls_crlcheck ); 1687 bc->sb_tls_crlcheck = NULL; 1688 } 1689 #endif 1690 if ( bc->sb_tls_ctx ) { 1691 ldap_pvt_tls_ctx_free( bc->sb_tls_ctx ); 1692 bc->sb_tls_ctx = NULL; 1693 } 1694 #endif 1695 } 1696 1697 void 1698 bindconf_tls_defaults( slap_bindconf *bc ) 1699 { 1700 #ifdef HAVE_TLS 1701 if ( bc->sb_tls_do_init ) { 1702 if ( !bc->sb_tls_cacert ) 1703 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CACERTFILE, 1704 &bc->sb_tls_cacert ); 1705 if ( !bc->sb_tls_cacertdir ) 1706 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CACERTDIR, 1707 &bc->sb_tls_cacertdir ); 1708 if ( !bc->sb_tls_cert ) 1709 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CERTFILE, 1710 &bc->sb_tls_cert ); 1711 if ( !bc->sb_tls_key ) 1712 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_KEYFILE, 1713 &bc->sb_tls_key ); 1714 if ( !bc->sb_tls_cipher_suite ) 1715 ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CIPHER_SUITE, 1716 &bc->sb_tls_cipher_suite ); 1717 if ( !bc->sb_tls_reqcert ) 1718 bc->sb_tls_reqcert = ch_strdup("demand"); 1719 if ( !bc->sb_tls_reqsan ) 1720 bc->sb_tls_reqsan = ch_strdup("allow"); 1721 if ( !bc->sb_tls_ecname ) 1722 slap_tls_get_config( slap_tls_ld, LDAP_OPT_X_TLS_ECNAME, 1723 &bc->sb_tls_ecname ); 1724 #ifdef HAVE_OPENSSL 1725 if ( !bc->sb_tls_crlcheck ) 1726 slap_tls_get_config( slap_tls_ld, LDAP_OPT_X_TLS_CRLCHECK, 1727 &bc->sb_tls_crlcheck ); 1728 #endif 1729 } 1730 #endif 1731 } 1732 1733 #ifdef HAVE_TLS 1734 static struct { 1735 const char *key; 1736 size_t offset; 1737 int opt; 1738 } bindtlsopts[] = { 1739 { "tls_cert", offsetof(slap_bindconf, sb_tls_cert), LDAP_OPT_X_TLS_CERTFILE }, 1740 { "tls_key", offsetof(slap_bindconf, sb_tls_key), LDAP_OPT_X_TLS_KEYFILE }, 1741 { "tls_cacert", offsetof(slap_bindconf, sb_tls_cacert), LDAP_OPT_X_TLS_CACERTFILE }, 1742 { "tls_cacertdir", offsetof(slap_bindconf, sb_tls_cacertdir), LDAP_OPT_X_TLS_CACERTDIR }, 1743 { "tls_cipher_suite", offsetof(slap_bindconf, sb_tls_cipher_suite), LDAP_OPT_X_TLS_CIPHER_SUITE }, 1744 { "tls_ecname", offsetof(slap_bindconf, sb_tls_ecname), LDAP_OPT_X_TLS_ECNAME }, 1745 {0, 0} 1746 }; 1747 1748 int bindconf_tls_set( slap_bindconf *bc, LDAP *ld ) 1749 { 1750 int i, rc, newctx = 0, res = 0; 1751 char *ptr = (char *)bc, **word; 1752 1753 if ( bc->sb_tls_do_init ) { 1754 for (i=0; bindtlsopts[i].opt; i++) { 1755 word = (char **)(ptr + bindtlsopts[i].offset); 1756 if ( *word ) { 1757 rc = ldap_set_option( ld, bindtlsopts[i].opt, *word ); 1758 if ( rc ) { 1759 Debug( LDAP_DEBUG_ANY, 1760 "bindconf_tls_set: failed to set %s to %s\n", 1761 bindtlsopts[i].key, *word ); 1762 res = -1; 1763 } else 1764 newctx = 1; 1765 } 1766 } 1767 if ( bc->sb_tls_reqcert ) { 1768 rc = ldap_pvt_tls_config( ld, LDAP_OPT_X_TLS_REQUIRE_CERT, 1769 bc->sb_tls_reqcert ); 1770 if ( rc ) { 1771 Debug( LDAP_DEBUG_ANY, 1772 "bindconf_tls_set: failed to set tls_reqcert to %s\n", 1773 bc->sb_tls_reqcert ); 1774 res = -1; 1775 } else { 1776 newctx = 1; 1777 /* retrieve the parsed setting for later use */ 1778 ldap_get_option( ld, LDAP_OPT_X_TLS_REQUIRE_CERT, &bc->sb_tls_int_reqcert ); 1779 } 1780 } 1781 if ( bc->sb_tls_reqsan ) { 1782 rc = ldap_pvt_tls_config( ld, LDAP_OPT_X_TLS_REQUIRE_SAN, 1783 bc->sb_tls_reqsan ); 1784 if ( rc ) { 1785 Debug( LDAP_DEBUG_ANY, 1786 "bindconf_tls_set: failed to set tls_reqsan to %s\n", 1787 bc->sb_tls_reqsan ); 1788 res = -1; 1789 } else { 1790 newctx = 1; 1791 /* retrieve the parsed setting for later use */ 1792 ldap_get_option( ld, LDAP_OPT_X_TLS_REQUIRE_SAN, &bc->sb_tls_int_reqsan ); 1793 } 1794 } 1795 if ( bc->sb_tls_protocol_min ) { 1796 rc = ldap_pvt_tls_config( ld, LDAP_OPT_X_TLS_PROTOCOL_MIN, 1797 bc->sb_tls_protocol_min ); 1798 if ( rc ) { 1799 Debug( LDAP_DEBUG_ANY, 1800 "bindconf_tls_set: failed to set tls_protocol_min to %s\n", 1801 bc->sb_tls_protocol_min ); 1802 res = -1; 1803 } else 1804 newctx = 1; 1805 } 1806 #ifdef HAVE_OPENSSL 1807 if ( bc->sb_tls_crlcheck ) { 1808 rc = ldap_pvt_tls_config( ld, LDAP_OPT_X_TLS_CRLCHECK, 1809 bc->sb_tls_crlcheck ); 1810 if ( rc ) { 1811 Debug( LDAP_DEBUG_ANY, 1812 "bindconf_tls_set: failed to set tls_crlcheck to %s\n", 1813 bc->sb_tls_crlcheck ); 1814 res = -1; 1815 } else 1816 newctx = 1; 1817 } 1818 #endif 1819 if ( !res ) 1820 bc->sb_tls_do_init = 0; 1821 } 1822 1823 if ( newctx ) { 1824 int opt = 0; 1825 1826 if ( bc->sb_tls_ctx ) { 1827 ldap_pvt_tls_ctx_free( bc->sb_tls_ctx ); 1828 bc->sb_tls_ctx = NULL; 1829 } 1830 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_NEWCTX, &opt ); 1831 if ( rc ) 1832 res = rc; 1833 else 1834 ldap_get_option( ld, LDAP_OPT_X_TLS_CTX, &bc->sb_tls_ctx ); 1835 } else if ( bc->sb_tls_ctx ) { 1836 rc = ldap_set_option( ld, LDAP_OPT_X_TLS_CTX, bc->sb_tls_ctx ); 1837 if ( rc == LDAP_SUCCESS ) { 1838 /* these options aren't actually inside the ctx, so have to be set again */ 1839 ldap_set_option( ld, LDAP_OPT_X_TLS_REQUIRE_CERT, &bc->sb_tls_int_reqcert ); 1840 ldap_set_option( ld, LDAP_OPT_X_TLS_REQUIRE_SAN, &bc->sb_tls_int_reqsan ); 1841 } else 1842 res = rc; 1843 } 1844 1845 return res; 1846 } 1847 #endif 1848 1849 /* 1850 * set connection keepalive options 1851 */ 1852 void 1853 slap_client_keepalive(LDAP *ld, slap_keepalive *sk) 1854 { 1855 if (!sk) return; 1856 1857 if ( sk->sk_idle ) { 1858 ldap_set_option( ld, LDAP_OPT_X_KEEPALIVE_IDLE, &sk->sk_idle ); 1859 } 1860 1861 if ( sk->sk_probes ) { 1862 ldap_set_option( ld, LDAP_OPT_X_KEEPALIVE_PROBES, &sk->sk_probes ); 1863 } 1864 1865 if ( sk->sk_interval ) { 1866 ldap_set_option( ld, LDAP_OPT_X_KEEPALIVE_INTERVAL, &sk->sk_interval ); 1867 } 1868 1869 return; 1870 } 1871 1872 /* 1873 * connect to a client using the bindconf data 1874 * note: should move "version" into bindconf... 1875 */ 1876 int 1877 slap_client_connect( LDAP **ldp, slap_bindconf *sb ) 1878 { 1879 LDAP *ld = NULL; 1880 int rc; 1881 struct timeval tv; 1882 1883 /* Init connection to provider */ 1884 rc = ldap_initialize( &ld, sb->sb_uri.bv_val ); 1885 if ( rc != LDAP_SUCCESS ) { 1886 Debug( LDAP_DEBUG_ANY, 1887 "slap_client_connect: " 1888 "ldap_initialize(%s) failed (%d)\n", 1889 sb->sb_uri.bv_val, rc ); 1890 return rc; 1891 } 1892 1893 if ( sb->sb_version != 0 ) { 1894 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, 1895 (const void *)&sb->sb_version ); 1896 } 1897 1898 if ( sb->sb_timeout_api ) { 1899 tv.tv_sec = sb->sb_timeout_api; 1900 tv.tv_usec = 0; 1901 ldap_set_option( ld, LDAP_OPT_TIMEOUT, &tv ); 1902 } 1903 1904 if ( sb->sb_timeout_net ) { 1905 tv.tv_sec = sb->sb_timeout_net; 1906 tv.tv_usec = 0; 1907 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, &tv ); 1908 } 1909 1910 /* setting network keepalive options */ 1911 slap_client_keepalive(ld, &sb->sb_keepalive); 1912 1913 #ifdef HAVE_TLS 1914 rc = bindconf_tls_set( sb, ld ); 1915 if ( rc ) { 1916 char *errmsg = NULL; 1917 ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, &errmsg ); 1918 Debug( LDAP_DEBUG_ANY, 1919 "slap_client_connect: " 1920 "URI=%s TLS context initialization failed (%d) %s\n", 1921 sb->sb_uri.bv_val, rc, errmsg ? errmsg : "" ); 1922 ldap_memfree( errmsg ); 1923 goto done; 1924 } 1925 #endif 1926 1927 /* Bind */ 1928 if ( sb->sb_tls ) { 1929 rc = ldap_start_tls_s( ld, NULL, NULL ); 1930 if ( rc != LDAP_SUCCESS ) { 1931 Debug( LDAP_DEBUG_ANY, 1932 "slap_client_connect: URI=%s " 1933 "%s, ldap_start_tls failed (%d)\n", 1934 sb->sb_uri.bv_val, 1935 sb->sb_tls == SB_TLS_CRITICAL ? 1936 "Error" : "Warning", 1937 rc ); 1938 if ( sb->sb_tls == SB_TLS_CRITICAL ) { 1939 goto done; 1940 } 1941 } 1942 } 1943 1944 if ( sb->sb_method == LDAP_AUTH_SASL ) { 1945 #ifdef HAVE_CYRUS_SASL 1946 void *defaults; 1947 1948 if ( sb->sb_secprops != NULL ) { 1949 rc = ldap_set_option( ld, 1950 LDAP_OPT_X_SASL_SECPROPS, sb->sb_secprops); 1951 1952 if( rc != LDAP_OPT_SUCCESS ) { 1953 Debug( LDAP_DEBUG_ANY, 1954 "slap_client_connect: " 1955 "error, ldap_set_option " 1956 "(%s,SECPROPS,\"%s\") failed!\n", 1957 sb->sb_uri.bv_val, sb->sb_secprops ); 1958 goto done; 1959 } 1960 } 1961 1962 defaults = lutil_sasl_defaults( ld, 1963 sb->sb_saslmech.bv_val, 1964 sb->sb_realm.bv_val, 1965 sb->sb_authcId.bv_val, 1966 sb->sb_cred.bv_val, 1967 sb->sb_authzId.bv_val ); 1968 if ( defaults == NULL ) { 1969 rc = LDAP_OTHER; 1970 goto done; 1971 } 1972 1973 rc = ldap_sasl_interactive_bind_s( ld, 1974 sb->sb_binddn.bv_val, 1975 sb->sb_saslmech.bv_val, 1976 NULL, NULL, 1977 LDAP_SASL_QUIET, 1978 lutil_sasl_interact, 1979 defaults ); 1980 1981 lutil_sasl_freedefs( defaults ); 1982 1983 /* FIXME: different error behaviors according to 1984 * 1) return code 1985 * 2) on err policy : exit, retry, backoff ... 1986 */ 1987 if ( rc != LDAP_SUCCESS ) { 1988 static struct berval bv_GSSAPI = BER_BVC( "GSSAPI" ); 1989 1990 Debug( LDAP_DEBUG_ANY, "slap_client_connect: URI=%s " 1991 "ldap_sasl_interactive_bind_s failed (%d)\n", 1992 sb->sb_uri.bv_val, rc ); 1993 1994 /* FIXME (see above comment) */ 1995 /* if Kerberos credentials cache is not active, retry */ 1996 if ( ber_bvcmp( &sb->sb_saslmech, &bv_GSSAPI ) == 0 && 1997 rc == LDAP_LOCAL_ERROR ) 1998 { 1999 rc = LDAP_SERVER_DOWN; 2000 } 2001 2002 goto done; 2003 } 2004 #else /* HAVE_CYRUS_SASL */ 2005 /* Should never get here, we trapped this at config time */ 2006 assert(0); 2007 Debug( LDAP_DEBUG_SYNC, "not compiled with SASL support\n" ); 2008 rc = LDAP_OTHER; 2009 goto done; 2010 #endif 2011 2012 } else if ( sb->sb_method == LDAP_AUTH_SIMPLE ) { 2013 rc = ldap_sasl_bind_s( ld, 2014 sb->sb_binddn.bv_val, LDAP_SASL_SIMPLE, 2015 &sb->sb_cred, NULL, NULL, NULL ); 2016 if ( rc != LDAP_SUCCESS ) { 2017 Debug( LDAP_DEBUG_ANY, "slap_client_connect: " 2018 "URI=%s DN=\"%s\" " 2019 "ldap_sasl_bind_s failed (%d)\n", 2020 sb->sb_uri.bv_val, sb->sb_binddn.bv_val, rc ); 2021 goto done; 2022 } 2023 } 2024 2025 done:; 2026 if ( rc ) { 2027 if ( ld ) { 2028 ldap_unbind_ext( ld, NULL, NULL ); 2029 *ldp = NULL; 2030 } 2031 2032 } else { 2033 *ldp = ld; 2034 } 2035 2036 return rc; 2037 } 2038 2039 /* -------------------------------------- */ 2040 2041 2042 static char * 2043 strtok_quote( char *line, char *sep, char **quote_ptr, int *iqp ) 2044 { 2045 int inquote; 2046 char *tmp; 2047 static char *next; 2048 2049 *quote_ptr = NULL; 2050 if ( line != NULL ) { 2051 next = line; 2052 } 2053 while ( *next && strchr( sep, *next ) ) { 2054 next++; 2055 } 2056 2057 if ( *next == '\0' ) { 2058 next = NULL; 2059 return( NULL ); 2060 } 2061 tmp = next; 2062 2063 for ( inquote = 0; *next; ) { 2064 switch ( *next ) { 2065 case '"': 2066 if ( inquote ) { 2067 inquote = 0; 2068 } else { 2069 inquote = 1; 2070 } 2071 AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 ); 2072 break; 2073 2074 case '\\': 2075 if ( next[1] ) 2076 AC_MEMCPY( next, 2077 next + 1, strlen( next + 1 ) + 1 ); 2078 next++; /* dont parse the escaped character */ 2079 break; 2080 2081 default: 2082 if ( ! inquote ) { 2083 if ( strchr( sep, *next ) != NULL ) { 2084 *quote_ptr = next; 2085 *next++ = '\0'; 2086 return( tmp ); 2087 } 2088 } 2089 next++; 2090 break; 2091 } 2092 } 2093 *iqp = inquote; 2094 2095 return( tmp ); 2096 } 2097 2098 static char buf[AC_LINE_MAX]; 2099 static char *line; 2100 static size_t lmax, lcur; 2101 2102 #define CATLINE( buf ) \ 2103 do { \ 2104 size_t len = strlen( buf ); \ 2105 while ( lcur + len + 1 > lmax ) { \ 2106 lmax += AC_LINE_MAX; \ 2107 line = (char *) ch_realloc( line, lmax ); \ 2108 } \ 2109 strcpy( line + lcur, buf ); \ 2110 lcur += len; \ 2111 } while( 0 ) 2112 2113 static void 2114 fp_getline_init(ConfigArgs *c) { 2115 c->lineno = -1; 2116 buf[0] = '\0'; 2117 } 2118 2119 static int 2120 fp_getline( FILE *fp, ConfigArgs *c ) 2121 { 2122 char *p; 2123 2124 lcur = 0; 2125 CATLINE(buf); 2126 c->lineno++; 2127 2128 /* avoid stack of bufs */ 2129 if ( strncasecmp( line, "include", STRLENOF( "include" ) ) == 0 ) { 2130 buf[0] = '\0'; 2131 c->line = line; 2132 return(1); 2133 } 2134 2135 while ( fgets( buf, sizeof( buf ), fp ) ) { 2136 p = strchr( buf, '\n' ); 2137 if ( p ) { 2138 if ( p > buf && p[-1] == '\r' ) { 2139 --p; 2140 } 2141 *p = '\0'; 2142 } 2143 /* XXX ugly */ 2144 c->line = line; 2145 if ( line[0] 2146 && ( p = line + strlen( line ) - 1 )[0] == '\\' 2147 && p[-1] != '\\' ) 2148 { 2149 p[0] = '\0'; 2150 lcur--; 2151 2152 } else { 2153 if ( !isspace( (unsigned char)buf[0] ) ) { 2154 return(1); 2155 } 2156 buf[0] = ' '; 2157 } 2158 CATLINE(buf); 2159 c->lineno++; 2160 } 2161 2162 buf[0] = '\0'; 2163 c->line = line; 2164 return(line[0] ? 1 : 0); 2165 } 2166 2167 int 2168 config_fp_parse_line(ConfigArgs *c) 2169 { 2170 char *token; 2171 static char *const hide[] = { 2172 "rootpw", "replica", "syncrepl", /* in slapd */ 2173 "acl-bind", "acl-method", "idassert-bind", /* in back-ldap */ 2174 "acl-passwd", "bindpw", /* in back-<ldap/meta> */ 2175 "pseudorootpw", /* in back-meta */ 2176 "dbpasswd", /* in back-sql */ 2177 NULL 2178 }; 2179 static char *const raw[] = { 2180 "attributetype", "objectclass", "ditcontentrule", "ldapsyntax", NULL }; 2181 char *quote_ptr; 2182 int i = (int)(sizeof(hide)/sizeof(hide[0])) - 1; 2183 int inquote = 0; 2184 2185 c->tline = ch_strdup(c->line); 2186 c->linelen = strlen(c->line); 2187 token = strtok_quote(c->tline, " \t", "e_ptr, &inquote); 2188 2189 if(token) for(i = 0; hide[i]; i++) if(!strcasecmp(token, hide[i])) break; 2190 if(quote_ptr) *quote_ptr = ' '; 2191 Debug(LDAP_DEBUG_CONFIG, "%s (%s%s)\n", c->log, 2192 hide[i] ? hide[i] : c->line, hide[i] ? " ***" : ""); 2193 if(quote_ptr) *quote_ptr = '\0'; 2194 2195 for(;; token = strtok_quote(NULL, " \t", "e_ptr, &inquote)) { 2196 if(c->argc >= c->argv_size) { 2197 char **tmp; 2198 tmp = ch_realloc(c->argv, (c->argv_size + ARGS_STEP) * sizeof(*c->argv)); 2199 if(!tmp) { 2200 Debug(LDAP_DEBUG_ANY, "%s: out of memory\n", c->log ); 2201 return -1; 2202 } 2203 c->argv = tmp; 2204 c->argv_size += ARGS_STEP; 2205 } 2206 if(token == NULL) 2207 break; 2208 c->argv[c->argc++] = token; 2209 } 2210 c->argv[c->argc] = NULL; 2211 if (inquote) { 2212 /* these directives parse c->line independently of argv tokenizing */ 2213 for(i = 0; raw[i]; i++) if (!strcasecmp(c->argv[0], raw[i])) return 0; 2214 2215 Debug(LDAP_DEBUG_ANY, "%s: unterminated quoted string \"%s\"\n", c->log, c->argv[c->argc-1] ); 2216 return -1; 2217 } 2218 return(0); 2219 } 2220 2221 void 2222 config_destroy( ) 2223 { 2224 ucdata_unload( UCDATA_ALL ); 2225 if ( frontendDB ) { 2226 /* NOTE: in case of early exit, frontendDB can be NULL */ 2227 if ( frontendDB->be_schemandn.bv_val ) 2228 free( frontendDB->be_schemandn.bv_val ); 2229 if ( frontendDB->be_schemadn.bv_val ) 2230 free( frontendDB->be_schemadn.bv_val ); 2231 if ( frontendDB->be_acl ) 2232 acl_destroy( frontendDB->be_acl ); 2233 } 2234 free( line ); 2235 if ( slapd_args_file ) 2236 free ( slapd_args_file ); 2237 if ( slapd_pid_file ) 2238 free ( slapd_pid_file ); 2239 if ( default_passwd_hash ) 2240 ldap_charray_free( default_passwd_hash ); 2241 } 2242 2243 char ** 2244 slap_str2clist( char ***out, char *in, const char *brkstr ) 2245 { 2246 char *str; 2247 char *s; 2248 char *lasts; 2249 int i, j; 2250 char **new; 2251 2252 /* find last element in list */ 2253 for (i = 0; *out && (*out)[i]; i++); 2254 2255 /* protect the input string from strtok */ 2256 str = ch_strdup( in ); 2257 2258 if ( *str == '\0' ) { 2259 free( str ); 2260 return( *out ); 2261 } 2262 2263 /* Count words in string */ 2264 j=1; 2265 for ( s = str; *s; s++ ) { 2266 if ( strchr( brkstr, *s ) != NULL ) { 2267 j++; 2268 } 2269 } 2270 2271 *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) ); 2272 new = *out + i; 2273 for ( s = ldap_pvt_strtok( str, brkstr, &lasts ); 2274 s != NULL; 2275 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) ) 2276 { 2277 *new = ch_strdup( s ); 2278 new++; 2279 } 2280 2281 *new = NULL; 2282 free( str ); 2283 return( *out ); 2284 } 2285 2286 int config_generic_wrapper( Backend *be, const char *fname, int lineno, 2287 int argc, char **argv ) 2288 { 2289 ConfigArgs c = { 0 }; 2290 ConfigTable *ct; 2291 int rc; 2292 2293 c.be = be; 2294 c.fname = fname; 2295 c.lineno = lineno; 2296 c.argc = argc; 2297 c.argv = argv; 2298 c.valx = -1; 2299 c.line = line; 2300 c.op = SLAP_CONFIG_ADD; 2301 snprintf( c.log, sizeof( c.log ), "%s: line %d", fname, lineno ); 2302 2303 rc = SLAP_CONF_UNKNOWN; 2304 ct = config_find_keyword( be->be_cf_ocs->co_table, &c ); 2305 if ( ct ) { 2306 c.table = be->be_cf_ocs->co_type; 2307 rc = config_add_vals( ct, &c ); 2308 } 2309 return rc; 2310 } 2311 2312 /* See if the given URL (in plain and parsed form) matches 2313 * any of the server's listener addresses. Return matching 2314 * Listener or NULL for no match. 2315 */ 2316 Listener *config_check_my_url( const char *url, LDAPURLDesc *lud ) 2317 { 2318 Listener **l = slapd_get_listeners(); 2319 int i, isMe; 2320 2321 /* Try a straight compare with Listener strings */ 2322 for ( i=0; l && l[i]; i++ ) { 2323 if ( !strcasecmp( url, l[i]->sl_url.bv_val )) { 2324 return l[i]; 2325 } 2326 } 2327 2328 isMe = 0; 2329 /* If hostname is empty, or is localhost, or matches 2330 * our hostname, this url refers to this host. 2331 * Compare it against listeners and ports. 2332 */ 2333 if ( !lud->lud_host || !lud->lud_host[0] || 2334 !strncasecmp("localhost", lud->lud_host, 2335 STRLENOF("localhost")) || 2336 !strcasecmp( global_host, lud->lud_host )) { 2337 2338 for ( i=0; l && l[i]; i++ ) { 2339 LDAPURLDesc *lu2; 2340 ldap_url_parse( l[i]->sl_url.bv_val, &lu2 ); 2341 do { 2342 if ( strcasecmp( lud->lud_scheme, 2343 lu2->lud_scheme )) 2344 break; 2345 if ( lud->lud_port != lu2->lud_port ) 2346 break; 2347 /* Listener on ANY address */ 2348 if ( !lu2->lud_host || !lu2->lud_host[0] ) { 2349 isMe = 1; 2350 break; 2351 } 2352 /* URL on ANY address */ 2353 if ( !lud->lud_host || !lud->lud_host[0] ) { 2354 isMe = 1; 2355 break; 2356 } 2357 /* Listener has specific host, must 2358 * match it 2359 */ 2360 if ( !strcasecmp( lud->lud_host, 2361 lu2->lud_host )) { 2362 isMe = 1; 2363 break; 2364 } 2365 } while(0); 2366 ldap_free_urldesc( lu2 ); 2367 if ( isMe ) { 2368 return l[i]; 2369 } 2370 } 2371 } 2372 return NULL; 2373 } 2374