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