Back to index

openldap  2.4.31
collect.c
Go to the documentation of this file.
00001 /* collect.c - Demonstration of overlay code */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 2003-2012 The OpenLDAP Foundation.
00006  * Portions Copyright 2003 Howard Chu.
00007  * All rights reserved.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted only as authorized by the OpenLDAP
00011  * Public License.
00012  *
00013  * A copy of this license is available in the file LICENSE in the
00014  * top-level directory of the distribution or, alternatively, at
00015  * <http://www.OpenLDAP.org/license.html>.
00016  */
00017 /* ACKNOWLEDGEMENTS:
00018  * This work was initially developed by the Howard Chu for inclusion
00019  * in OpenLDAP Software.
00020  */
00021 
00022 #include "portable.h"
00023 
00024 #ifdef SLAPD_OVER_COLLECT
00025 
00026 #include <stdio.h>
00027 
00028 #include <ac/string.h>
00029 #include <ac/socket.h>
00030 
00031 #include "slap.h"
00032 #include "config.h"
00033 
00034 #include "lutil.h"
00035 
00036 /* This is a cheap hack to implement a collective attribute.
00037  *
00038  * This demonstration overlay looks for a specified attribute in an
00039  * ancestor of a given entry and adds that attribute to the given
00040  * entry when it is returned in a search response. It takes no effect
00041  * for any other operations. If the ancestor does not exist, there
00042  * is no effect. If no attribute was configured, there is no effect.
00043  */
00044 
00045 typedef struct collect_info {
00046        struct collect_info *ci_next;
00047        struct berval ci_dn;
00048        int ci_ad_num;
00049        AttributeDescription *ci_ad[1];
00050 } collect_info;
00051 
00052 static int collect_cf( ConfigArgs *c );
00053 
00054 static ConfigTable collectcfg[] = {
00055        { "collectinfo", "dn> <attribute", 3, 3, 0,
00056          ARG_MAGIC, collect_cf,
00057          "( OLcfgOvAt:19.1 NAME 'olcCollectInfo' "
00058          "DESC 'DN of entry and attribute to distribute' "
00059          "EQUALITY caseIgnoreMatch "
00060          "SYNTAX OMsDirectoryString )", NULL, NULL },
00061        { NULL, NULL, 0, 0, 0, ARG_IGNORED }
00062 };
00063 
00064 static ConfigOCs collectocs[] = {
00065        { "( OLcfgOvOc:19.1 "
00066          "NAME 'olcCollectConfig' "
00067          "DESC 'Collective Attribute configuration' "
00068          "SUP olcOverlayConfig "
00069          "MAY olcCollectInfo )",
00070          Cft_Overlay, collectcfg },
00071        { NULL, 0, NULL }
00072 };
00073 
00074 /*
00075  * inserts a collect_info into on->on_bi.bi_private taking into account
00076  * order. this means longer dn's (i.e. more specific dn's) will be found
00077  * first when searching, allowing some limited overlap of dn's
00078  */
00079 static void
00080 insert_ordered( slap_overinst *on, collect_info *ci ) {
00081        collect_info *find = on->on_bi.bi_private;
00082        collect_info *prev = NULL;
00083        int found = 0;
00084 
00085        while (!found) {
00086               if (find == NULL) {
00087                      if (prev == NULL) {
00088                             /* base case - empty list */
00089                             on->on_bi.bi_private = ci;
00090                             ci->ci_next = NULL;
00091                      } else {
00092                             /* final case - end of list */
00093                             prev->ci_next = ci;
00094                             ci->ci_next = NULL;
00095                      }
00096                      found = 1;
00097               } else if (find->ci_dn.bv_len < ci->ci_dn.bv_len) { 
00098                      /* insert into list here */
00099                      if (prev == NULL) {
00100                             /* entry is head of list */
00101                             ci->ci_next = on->on_bi.bi_private;
00102                             on->on_bi.bi_private = ci;
00103                      } else {
00104                             /* entry is not head of list */
00105                             prev->ci_next = ci;
00106                             ci->ci_next = find;
00107                      }
00108                      found = 1;
00109               } else {
00110                      /* keep looking */
00111                      prev = find;
00112                      find = find->ci_next;
00113               }
00114        }
00115 }
00116 
00117 static int
00118 collect_cf( ConfigArgs *c )
00119 {
00120        slap_overinst *on = (slap_overinst *)c->bi;
00121        int rc = 1, idx;
00122 
00123        switch( c->op ) {
00124        case SLAP_CONFIG_EMIT:
00125               {
00126               collect_info *ci;
00127               for ( ci = on->on_bi.bi_private; ci; ci = ci->ci_next ) {
00128                      struct berval bv;
00129                      char *ptr;
00130                      int len;
00131 
00132                      /* calculate the length & malloc memory */
00133                      bv.bv_len = ci->ci_dn.bv_len + STRLENOF("\"\" ");
00134                      for (idx=0; idx<ci->ci_ad_num; idx++) {
00135                             bv.bv_len += ci->ci_ad[idx]->ad_cname.bv_len;
00136                             if (idx<(ci->ci_ad_num-1)) { 
00137                                    bv.bv_len++;
00138                             }
00139                      }
00140                      bv.bv_val = ch_malloc( bv.bv_len + 1 );
00141 
00142                      /* copy the value and update len */
00143                      len = snprintf( bv.bv_val, bv.bv_len + 1, "\"%s\" ", 
00144                             ci->ci_dn.bv_val);
00145                      ptr = bv.bv_val + len;
00146                      for (idx=0; idx<ci->ci_ad_num; idx++) {
00147                             ptr = lutil_strncopy( ptr,
00148                                    ci->ci_ad[idx]->ad_cname.bv_val,
00149                                    ci->ci_ad[idx]->ad_cname.bv_len);
00150                             if (idx<(ci->ci_ad_num-1)) {
00151                                    *ptr++ = ',';
00152                             }
00153                      }
00154                      *ptr = '\0';
00155                      bv.bv_len = ptr - bv.bv_val;
00156 
00157                      ber_bvarray_add( &c->rvalue_vals, &bv );
00158                      rc = 0;
00159               }
00160               }
00161               break;
00162        case LDAP_MOD_DELETE:
00163               if ( c->valx == -1 ) {
00164               /* Delete entire attribute */
00165                      collect_info *ci;
00166                      while (( ci = on->on_bi.bi_private )) {
00167                             on->on_bi.bi_private = ci->ci_next;
00168                             ch_free( ci->ci_dn.bv_val );
00169                             ch_free( ci );
00170                      }
00171               } else {
00172               /* Delete just one value */
00173                      collect_info **cip, *ci;
00174                      int i;
00175                      cip = (collect_info **)&on->on_bi.bi_private;
00176                      ci = *cip;
00177                      for ( i=0; i < c->valx; i++ ) {
00178                             cip = &ci->ci_next;
00179                             ci = *cip;
00180                      }
00181                      *cip = ci->ci_next;
00182                      ch_free( ci->ci_dn.bv_val );
00183                      ch_free( ci );
00184               }
00185               rc = 0;
00186               break;
00187        case SLAP_CONFIG_ADD:
00188        case LDAP_MOD_ADD:
00189               {
00190               collect_info *ci;
00191               struct berval bv, dn;
00192               const char *text;
00193               int idx, count=0;
00194               char *arg;
00195 
00196               /* count delimiters in attribute argument */
00197               arg = strtok(c->argv[2], ",");
00198               while (arg!=NULL) {
00199                      count++;
00200                      arg = strtok(NULL, ",");
00201               }
00202 
00203               /* validate and normalize dn */
00204               ber_str2bv( c->argv[1], 0, 0, &bv );
00205               if ( dnNormalize( 0, NULL, NULL, &bv, &dn, NULL ) ) {
00206                      snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s invalid DN: \"%s\"",
00207                             c->argv[0], c->argv[1] );
00208                      Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
00209                             "%s: %s\n", c->log, c->cr_msg, 0 );
00210                      return ARG_BAD_CONF;
00211               }
00212 
00213               /* check for duplicate DNs */
00214               for ( ci = (collect_info *)on->on_bi.bi_private; ci;
00215                      ci = ci->ci_next ) {
00216                      /* If new DN is longest, there are no possible matches */
00217                      if ( dn.bv_len > ci->ci_dn.bv_len ) {
00218                             ci = NULL;
00219                             break;
00220                      }
00221                      if ( bvmatch( &dn, &ci->ci_dn )) {
00222                             break;
00223                      }
00224               }
00225               if ( ci ) {
00226                      snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s DN already configured: \"%s\"",
00227                             c->argv[0], c->argv[1] );
00228                      Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
00229                             "%s: %s\n", c->log, c->cr_msg, 0 );
00230                      return ARG_BAD_CONF;
00231               }
00232 
00233               /* allocate config info with room for attribute array */
00234               ci = ch_malloc( sizeof( collect_info ) +
00235                      sizeof( AttributeDescription * ) * count );
00236 
00237               /* load attribute description for attribute list */
00238               arg = c->argv[2];
00239               for( idx=0; idx<count; idx++) {
00240                      ci->ci_ad[idx] = NULL;
00241 
00242                      if ( slap_str2ad( arg, &ci->ci_ad[idx], &text ) ) {
00243                             snprintf( c->cr_msg, sizeof( c->cr_msg ), 
00244                                    "%s attribute description unknown: \"%s\"",
00245                                    c->argv[0], arg);
00246                             Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
00247                                    "%s: %s\n", c->log, c->cr_msg, 0 );
00248                             ch_free( ci );
00249                             return ARG_BAD_CONF;
00250                      }
00251                      while(*arg!='\0') {
00252                             arg++; /* skip to end of argument */
00253                      }
00254                      if (idx<count-1) {
00255                             arg++; /* skip inner delimiters */
00256                      }
00257               }
00258 
00259               /* The on->on_bi.bi_private pointer can be used for
00260                * anything this instance of the overlay needs.
00261                */
00262               ci->ci_ad[count] = NULL;
00263               ci->ci_ad_num = count;
00264               ci->ci_dn = dn;
00265 
00266               /* creates list of ci's ordered by dn length */ 
00267               insert_ordered ( on, ci );
00268 
00269               /* New ci wasn't simply appended to end, adjust its
00270                * position in the config entry's a_vals
00271                */
00272               if ( c->ca_entry && ci->ci_next ) {
00273                      Attribute *a = attr_find( c->ca_entry->e_attrs,
00274                             collectcfg[0].ad );
00275                      if ( a ) {
00276                             struct berval bv, nbv;
00277                             collect_info *c2 = (collect_info *)on->on_bi.bi_private;
00278                             int i, j;
00279                             for ( i=0; c2 != ci; i++, c2 = c2->ci_next );
00280                             bv = a->a_vals[a->a_numvals-1];
00281                             nbv = a->a_nvals[a->a_numvals-1];
00282                             for ( j=a->a_numvals-1; j>i; j-- ) {
00283                                    a->a_vals[j] = a->a_vals[j-1];
00284                                    a->a_nvals[j] = a->a_nvals[j-1];
00285                             }
00286                             a->a_vals[j] = bv;
00287                             a->a_nvals[j] = nbv;
00288                      }
00289               }
00290 
00291               rc = 0;
00292               }
00293        }
00294        return rc;
00295 }
00296 
00297 static int
00298 collect_destroy(
00299        BackendDB *be,
00300        ConfigReply *cr
00301 )
00302 {
00303        slap_overinst *on = (slap_overinst *)be->bd_info;
00304        collect_info *ci;
00305 
00306        while (( ci = on->on_bi.bi_private )) {
00307               on->on_bi.bi_private = ci->ci_next;
00308               ch_free( ci->ci_dn.bv_val );
00309               ch_free( ci );
00310        }
00311        return 0;
00312 }
00313 
00314 static int
00315 collect_modify( Operation *op, SlapReply *rs)
00316 {
00317        slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
00318        collect_info *ci = on->on_bi.bi_private;
00319        Modifications *ml;
00320        char errMsg[100];
00321        int idx;
00322 
00323        for ( ml = op->orm_modlist; ml != NULL; ml = ml->sml_next) {
00324               for (; ci; ci=ci->ci_next ) {
00325                      /* Is this entry an ancestor of this collectinfo ? */
00326                      if (!dnIsSuffix(&op->o_req_ndn, &ci->ci_dn)) {
00327                             /* this collectinfo does not match */
00328                             continue;
00329                      }
00330 
00331                      /* Is this entry the same as the template DN ? */
00332                      if ( dn_match(&op->o_req_ndn, &ci->ci_dn)) {
00333                             /* all changes in this ci are allowed */
00334                             continue;
00335                      }
00336 
00337                      /* check for collect attributes - disallow modify if present */
00338                      for(idx=0; idx<ci->ci_ad_num; idx++) {
00339                             if (ml->sml_desc == ci->ci_ad[idx]) {
00340                                    rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
00341                                    snprintf( errMsg, sizeof( errMsg ), 
00342                                           "cannot change virtual attribute '%s'",
00343                                           ci->ci_ad[idx]->ad_cname.bv_val);
00344                                    rs->sr_text = errMsg;
00345                                    send_ldap_result( op, rs );
00346                                    return rs->sr_err;
00347                             }
00348                      }
00349               }
00350 
00351        }
00352 
00353        return SLAP_CB_CONTINUE;
00354 }
00355 
00356 static int
00357 collect_response( Operation *op, SlapReply *rs )
00358 {
00359        slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
00360        collect_info *ci = on->on_bi.bi_private;
00361 
00362        /* If we've been configured and the current response is
00363         * a search entry
00364         */
00365        if ( ci && rs->sr_type == REP_SEARCH ) {
00366               int rc;
00367 
00368               op->o_bd->bd_info = (BackendInfo *)on->on_info;
00369 
00370               for (; ci; ci=ci->ci_next ) {
00371                      int idx=0;
00372 
00373                      /* Is this entry an ancestor of this collectinfo ? */
00374                      if (!dnIsSuffix(&rs->sr_entry->e_nname, &ci->ci_dn)) {
00375                             /* collectinfo does not match */
00376                             continue;
00377                      }
00378 
00379                      /* Is this entry the same as the template DN ? */
00380                      if ( dn_match(&rs->sr_entry->e_nname, &ci->ci_dn)) {
00381                             /* dont apply change to parent */
00382                             continue;
00383                      }
00384 
00385                      /* The current entry may live in a cache, so
00386                      * don't modify it directly. Make a copy and
00387                      * work with that instead.
00388                      */
00389                      rs_entry2modifiable( op, rs, on );
00390 
00391                      /* Loop for each attribute in this collectinfo */
00392                      for(idx=0; idx<ci->ci_ad_num; idx++) {
00393                             BerVarray vals = NULL;
00394 
00395                             /* Extract the values of the desired attribute from
00396                              * the ancestor entry */
00397                             rc = backend_attribute( op, NULL, &ci->ci_dn, 
00398                                    ci->ci_ad[idx], &vals, ACL_READ );
00399 
00400                             /* If there are any values, merge them into the
00401                              * current search result
00402                              */
00403                             if ( vals ) {
00404                                    attr_merge( rs->sr_entry, ci->ci_ad[idx], 
00405                                           vals, NULL );
00406                                    ber_bvarray_free_x( vals, op->o_tmpmemctx );
00407                             }
00408                      }
00409               }
00410        }
00411 
00412        /* Default is to just fall through to the normal processing */
00413        return SLAP_CB_CONTINUE;
00414 }
00415 
00416 static slap_overinst collect;
00417 
00418 int collect_initialize() {
00419        int code;
00420 
00421        collect.on_bi.bi_type = "collect";
00422        collect.on_bi.bi_db_destroy = collect_destroy;
00423        collect.on_bi.bi_op_modify = collect_modify;
00424        collect.on_response = collect_response;
00425 
00426        collect.on_bi.bi_cf_ocs = collectocs;
00427        code = config_register_schema( collectcfg, collectocs );
00428        if ( code ) return code;
00429 
00430        return overlay_register( &collect );
00431 }
00432 
00433 #if SLAPD_OVER_COLLECT == SLAPD_MOD_DYNAMIC
00434 int init_module(int argc, char *argv[]) {
00435        return collect_initialize();
00436 }
00437 #endif
00438 
00439 #endif /* SLAPD_OVER_COLLECT */