1 /* $NetBSD: extended.c,v 1.1.1.6 2018/02/06 01:53:14 christos Exp $ */ 2 3 /* $OpenLDAP$ */ 4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 5 * 6 * Copyright 1999-2017 The OpenLDAP Foundation. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted only as authorized by the OpenLDAP 11 * Public License. 12 * 13 * A copy of this license is available in the file LICENSE in the 14 * top-level directory of the distribution or, alternatively, at 15 * <http://www.OpenLDAP.org/license.html>. 16 */ 17 18 /* 19 * LDAPv3 Extended Operation Request 20 * ExtendedRequest ::= [APPLICATION 23] SEQUENCE { 21 * requestName [0] LDAPOID, 22 * requestValue [1] OCTET STRING OPTIONAL 23 * } 24 * 25 * LDAPv3 Extended Operation Response 26 * ExtendedResponse ::= [APPLICATION 24] SEQUENCE { 27 * COMPONENTS OF LDAPResult, 28 * responseName [10] LDAPOID OPTIONAL, 29 * response [11] OCTET STRING OPTIONAL 30 * } 31 * 32 */ 33 34 #include <sys/cdefs.h> 35 __RCSID("$NetBSD: extended.c,v 1.1.1.6 2018/02/06 01:53:14 christos Exp $"); 36 37 #include "portable.h" 38 39 #include <stdio.h> 40 41 #include <ac/socket.h> 42 #include <ac/string.h> 43 44 #include "slap.h" 45 #include "lber_pvt.h" 46 47 static struct extop_list { 48 struct extop_list *next; 49 struct berval oid; 50 slap_mask_t flags; 51 SLAP_EXTOP_MAIN_FN *ext_main; 52 } *supp_ext_list = NULL; 53 54 static SLAP_EXTOP_MAIN_FN whoami_extop; 55 56 /* This list of built-in extops is for extops that are not part 57 * of backends or in external modules. Essentially, this is 58 * just a way to get built-in extops onto the extop list without 59 * having a separate init routine for each built-in extop. 60 */ 61 static struct { 62 const struct berval *oid; 63 slap_mask_t flags; 64 SLAP_EXTOP_MAIN_FN *ext_main; 65 } builtin_extops[] = { 66 #ifdef LDAP_X_TXN 67 { &slap_EXOP_TXN_START, 0, txn_start_extop }, 68 { &slap_EXOP_TXN_END, 0, txn_end_extop }, 69 #endif 70 { &slap_EXOP_CANCEL, 0, cancel_extop }, 71 { &slap_EXOP_WHOAMI, 0, whoami_extop }, 72 { &slap_EXOP_MODIFY_PASSWD, SLAP_EXOP_WRITES, passwd_extop }, 73 { NULL, 0, NULL } 74 }; 75 76 77 static struct extop_list *find_extop( 78 struct extop_list *list, struct berval *oid ); 79 80 struct berval * 81 get_supported_extop (int index) 82 { 83 struct extop_list *ext; 84 85 /* linear scan is slow, but this way doesn't force a 86 * big change on root_dse.c, where this routine is used. 87 */ 88 for (ext = supp_ext_list; ext != NULL && --index >= 0; ext = ext->next) { 89 ; /* empty */ 90 } 91 92 if (ext == NULL) return NULL; 93 94 return &ext->oid; 95 } 96 97 98 int exop_root_dse_info( Entry *e ) 99 { 100 AttributeDescription *ad_supportedExtension 101 = slap_schema.si_ad_supportedExtension; 102 struct berval vals[2]; 103 struct extop_list *ext; 104 105 vals[1].bv_val = NULL; 106 vals[1].bv_len = 0; 107 108 for (ext = supp_ext_list; ext != NULL; ext = ext->next) { 109 if( ext->flags & SLAP_EXOP_HIDE ) continue; 110 111 vals[0] = ext->oid; 112 113 if( attr_merge( e, ad_supportedExtension, vals, NULL ) ) { 114 return LDAP_OTHER; 115 } 116 } 117 118 return LDAP_SUCCESS; 119 } 120 121 int 122 do_extended( 123 Operation *op, 124 SlapReply *rs 125 ) 126 { 127 struct berval reqdata = {0, NULL}; 128 ber_len_t len; 129 130 Debug( LDAP_DEBUG_TRACE, "%s do_extended\n", 131 op->o_log_prefix, 0, 0 ); 132 133 if( op->o_protocol < LDAP_VERSION3 ) { 134 Debug( LDAP_DEBUG_ANY, "%s do_extended: protocol version (%d) too low\n", 135 op->o_log_prefix, op->o_protocol, 0 ); 136 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "requires LDAPv3" ); 137 rs->sr_err = SLAPD_DISCONNECT; 138 goto done; 139 } 140 141 if ( ber_scanf( op->o_ber, "{m" /*}*/, &op->ore_reqoid ) == LBER_ERROR ) { 142 Debug( LDAP_DEBUG_ANY, "%s do_extended: ber_scanf failed\n", 143 op->o_log_prefix, 0, 0 ); 144 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" ); 145 rs->sr_err = SLAPD_DISCONNECT; 146 goto done; 147 } 148 149 if( ber_peek_tag( op->o_ber, &len ) == LDAP_TAG_EXOP_REQ_VALUE ) { 150 if( ber_scanf( op->o_ber, "m", &reqdata ) == LBER_ERROR ) { 151 Debug( LDAP_DEBUG_ANY, "%s do_extended: ber_scanf failed\n", 152 op->o_log_prefix, 0, 0 ); 153 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" ); 154 rs->sr_err = SLAPD_DISCONNECT; 155 goto done; 156 } 157 } 158 159 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) { 160 Debug( LDAP_DEBUG_ANY, "%s do_extended: get_ctrls failed\n", 161 op->o_log_prefix, 0, 0 ); 162 return rs->sr_err; 163 } 164 165 Statslog( LDAP_DEBUG_STATS, "%s EXT oid=%s\n", 166 op->o_log_prefix, op->ore_reqoid.bv_val, 0, 0, 0 ); 167 168 /* check for controls inappropriate for all extended operations */ 169 if( get_manageDSAit( op ) == SLAP_CONTROL_CRITICAL ) { 170 send_ldap_error( op, rs, 171 LDAP_UNAVAILABLE_CRITICAL_EXTENSION, 172 "manageDSAit control inappropriate" ); 173 goto done; 174 } 175 176 /* FIXME: temporary? */ 177 if ( reqdata.bv_val ) { 178 op->ore_reqdata = &reqdata; 179 } 180 181 op->o_bd = frontendDB; 182 rs->sr_err = frontendDB->be_extended( op, rs ); 183 184 /* clean up in case some overlay set them? */ 185 if ( !BER_BVISNULL( &op->o_req_ndn ) ) { 186 if ( !BER_BVISNULL( &op->o_req_dn ) 187 && op->o_req_ndn.bv_val != op->o_req_dn.bv_val ) 188 { 189 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx ); 190 } 191 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx ); 192 BER_BVZERO( &op->o_req_dn ); 193 BER_BVZERO( &op->o_req_ndn ); 194 } 195 196 done: 197 return rs->sr_err; 198 } 199 200 int 201 fe_extended( Operation *op, SlapReply *rs ) 202 { 203 struct extop_list *ext = NULL; 204 struct berval reqdata = BER_BVNULL; 205 206 if (op->ore_reqdata) { 207 reqdata = *op->ore_reqdata; 208 } 209 210 ext = find_extop(supp_ext_list, &op->ore_reqoid ); 211 if ( ext == NULL ) { 212 Debug( LDAP_DEBUG_ANY, "%s do_extended: unsupported operation \"%s\"\n", 213 op->o_log_prefix, op->ore_reqoid.bv_val, 0 ); 214 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, 215 "unsupported extended operation" ); 216 goto done; 217 } 218 219 op->ore_flags = ext->flags; 220 221 Debug( LDAP_DEBUG_ARGS, "do_extended: oid=%s\n", 222 op->ore_reqoid.bv_val, 0 ,0 ); 223 224 { /* start of OpenLDAP extended operation */ 225 BackendDB *bd = op->o_bd; 226 227 rs->sr_err = (ext->ext_main)( op, rs ); 228 229 if( rs->sr_err != SLAPD_ABANDON ) { 230 if ( rs->sr_err == LDAP_REFERRAL && rs->sr_ref == NULL ) { 231 rs->sr_ref = referral_rewrite( default_referral, 232 NULL, NULL, LDAP_SCOPE_DEFAULT ); 233 if ( !rs->sr_ref ) rs->sr_ref = default_referral; 234 if ( !rs->sr_ref ) { 235 rs->sr_err = LDAP_UNWILLING_TO_PERFORM; 236 rs->sr_text = "referral missing"; 237 } 238 } 239 240 if ( op->o_bd == NULL ) 241 op->o_bd = bd; 242 send_ldap_extended( op, rs ); 243 244 if ( rs->sr_ref != default_referral ) { 245 ber_bvarray_free( rs->sr_ref ); 246 rs->sr_ref = NULL; 247 } 248 } 249 250 if ( rs->sr_rspoid != NULL ) { 251 free( (char *)rs->sr_rspoid ); 252 rs->sr_rspoid = NULL; 253 } 254 255 if ( rs->sr_rspdata != NULL ) { 256 ber_bvfree( rs->sr_rspdata ); 257 rs->sr_rspdata = NULL; 258 } 259 } /* end of OpenLDAP extended operation */ 260 261 done:; 262 return rs->sr_err; 263 } 264 265 int 266 load_extop2( 267 const struct berval *ext_oid, 268 slap_mask_t ext_flags, 269 SLAP_EXTOP_MAIN_FN *ext_main, 270 unsigned flags ) 271 { 272 struct berval oidm = BER_BVNULL; 273 struct extop_list *ext; 274 int insertme = 0; 275 276 if ( !ext_main ) { 277 return -1; 278 } 279 280 if ( ext_oid == NULL || BER_BVISNULL( ext_oid ) || 281 BER_BVISEMPTY( ext_oid ) ) 282 { 283 return -1; 284 } 285 286 if ( numericoidValidate( NULL, (struct berval *)ext_oid ) != 287 LDAP_SUCCESS ) 288 { 289 oidm.bv_val = oidm_find( ext_oid->bv_val ); 290 if ( oidm.bv_val == NULL ) { 291 return -1; 292 } 293 oidm.bv_len = strlen( oidm.bv_val ); 294 ext_oid = &oidm; 295 } 296 297 for ( ext = supp_ext_list; ext; ext = ext->next ) { 298 if ( bvmatch( ext_oid, &ext->oid ) ) { 299 if ( flags == 1 ) { 300 break; 301 } 302 return -1; 303 } 304 } 305 306 if ( flags == 0 || ext == NULL ) { 307 ext = ch_calloc( 1, sizeof(struct extop_list) + ext_oid->bv_len + 1 ); 308 if ( ext == NULL ) { 309 return(-1); 310 } 311 312 ext->oid.bv_val = (char *)(ext + 1); 313 AC_MEMCPY( ext->oid.bv_val, ext_oid->bv_val, ext_oid->bv_len ); 314 ext->oid.bv_len = ext_oid->bv_len; 315 ext->oid.bv_val[ext->oid.bv_len] = '\0'; 316 317 insertme = 1; 318 } 319 320 ext->flags = ext_flags; 321 ext->ext_main = ext_main; 322 323 if ( insertme ) { 324 ext->next = supp_ext_list; 325 supp_ext_list = ext; 326 } 327 328 return(0); 329 } 330 331 int 332 extops_init (void) 333 { 334 int i; 335 336 for ( i = 0; builtin_extops[i].oid != NULL; i++ ) { 337 load_extop( (struct berval *)builtin_extops[i].oid, 338 builtin_extops[i].flags, 339 builtin_extops[i].ext_main ); 340 } 341 return(0); 342 } 343 344 int 345 extops_kill (void) 346 { 347 struct extop_list *ext; 348 349 /* we allocated the memory, so we have to free it, too. */ 350 while ((ext = supp_ext_list) != NULL) { 351 supp_ext_list = ext->next; 352 ch_free(ext); 353 } 354 return(0); 355 } 356 357 static struct extop_list * 358 find_extop( struct extop_list *list, struct berval *oid ) 359 { 360 struct extop_list *ext; 361 362 for (ext = list; ext; ext = ext->next) { 363 if (bvmatch(&ext->oid, oid)) 364 return(ext); 365 } 366 return(NULL); 367 } 368 369 370 const struct berval slap_EXOP_WHOAMI = BER_BVC(LDAP_EXOP_WHO_AM_I); 371 372 static int 373 whoami_extop ( 374 Operation *op, 375 SlapReply *rs ) 376 { 377 struct berval *bv; 378 379 if ( op->ore_reqdata != NULL ) { 380 /* no request data should be provided */ 381 rs->sr_text = "no request data expected"; 382 return LDAP_PROTOCOL_ERROR; 383 } 384 385 Statslog( LDAP_DEBUG_STATS, "%s WHOAMI\n", 386 op->o_log_prefix, 0, 0, 0, 0 ); 387 388 op->o_bd = op->o_conn->c_authz_backend; 389 if( backend_check_restrictions( op, rs, 390 (struct berval *)&slap_EXOP_WHOAMI ) != LDAP_SUCCESS ) 391 { 392 return rs->sr_err; 393 } 394 395 bv = (struct berval *) ch_malloc( sizeof(struct berval) ); 396 if( op->o_dn.bv_len ) { 397 bv->bv_len = op->o_dn.bv_len + STRLENOF( "dn:" ); 398 bv->bv_val = ch_malloc( bv->bv_len + 1 ); 399 AC_MEMCPY( bv->bv_val, "dn:", STRLENOF( "dn:" ) ); 400 AC_MEMCPY( &bv->bv_val[STRLENOF( "dn:" )], op->o_dn.bv_val, 401 op->o_dn.bv_len ); 402 bv->bv_val[bv->bv_len] = '\0'; 403 404 } else { 405 bv->bv_len = 0; 406 bv->bv_val = NULL; 407 } 408 409 rs->sr_rspdata = bv; 410 return LDAP_SUCCESS; 411 } 412