root/source4/dsdb/samdb/ldb_modules/instancetype.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. it_callback
  2. instancetype_add

   1 /* 
   2    ldb database library
   3 
   4    Copyright (C) Simo Sorce  2004-2008
   5    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
   6    Copyright (C) Andrew Tridgell 2005
   7    Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
   8 
   9      ** NOTE! The following LGPL license applies to the ldb
  10      ** library. This does NOT imply that all of Samba is released
  11      ** under the LGPL
  12    
  13    This library is free software; you can redistribute it and/or
  14    modify it under the terms of the GNU Lesser General Public
  15    License as published by the Free Software Foundation; either
  16    version 3 of the License, or (at your option) any later version.
  17 
  18    This library is distributed in the hope that it will be useful,
  19    but WITHOUT ANY WARRANTY; without even the implied warranty of
  20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  21    Lesser General Public License for more details.
  22 
  23    You should have received a copy of the GNU Lesser General Public
  24    License along with this library; if not, see <http://www.gnu.org/licenses/>.
  25 */
  26 
  27 /*
  28  *  Name: ldb
  29  *
  30  *  Component: ldb instancetype module
  31  *
  32  *  Description: add an instanceType onto every new record
  33  *
  34  *  Author: Andrew Bartlett
  35  */
  36 
  37 #include "includes.h"
  38 #include "ldb_module.h"
  39 #include "librpc/gen_ndr/ndr_misc.h"
  40 #include "dsdb/samdb/samdb.h"
  41 #include "dsdb/common/flags.h"
  42 
  43 struct it_context {
  44         struct ldb_module *module;
  45         struct ldb_request *req;
  46 };
  47 
  48 static int it_callback(struct ldb_request *req, struct ldb_reply *ares)
     /* [<][>][^][v][top][bottom][index][help] */
  49 {
  50         struct ldb_context *ldb;
  51         struct it_context *ac;
  52 
  53         ac = talloc_get_type(req->context, struct it_context);
  54         ldb = ldb_module_get_ctx(ac->module);
  55 
  56         if (!ares) {
  57                 return ldb_module_done(ac->req, NULL, NULL,
  58                                         LDB_ERR_OPERATIONS_ERROR);
  59         }
  60         if (ares->error != LDB_SUCCESS) {
  61                 return ldb_module_done(ac->req, ares->controls,
  62                                         ares->response, ares->error);
  63         }
  64 
  65         if (ares->type != LDB_REPLY_DONE) {
  66                 ldb_set_errstring(ldb, "Invalid reply type!");
  67                 return ldb_module_done(ac->req, NULL, NULL,
  68                                         LDB_ERR_OPERATIONS_ERROR);
  69         }
  70 
  71         return ldb_module_done(ac->req, ares->controls,
  72                                         ares->response, ares->error);
  73 }
  74 
  75 /* add_record: add instancetype attribute */
  76 static int instancetype_add(struct ldb_module *module, struct ldb_request *req)
     /* [<][>][^][v][top][bottom][index][help] */
  77 {
  78         struct ldb_context *ldb;
  79         struct ldb_request *down_req;
  80         struct ldb_message *msg;
  81         struct it_context *ac;
  82         uint32_t instance_type;
  83         int ret;
  84         const struct ldb_control *partition_ctrl;
  85         const struct dsdb_control_current_partition *partition;
  86 
  87         ldb = ldb_module_get_ctx(module);
  88 
  89         ldb_debug(ldb, LDB_DEBUG_TRACE, "instancetype_add_record\n");
  90 
  91         /* do not manipulate our control entries */
  92         if (ldb_dn_is_special(req->op.add.message->dn)) {
  93                 return ldb_next_request(module, req);
  94         }
  95 
  96         partition_ctrl = ldb_request_get_control(req, DSDB_CONTROL_CURRENT_PARTITION_OID);
  97         if (!partition_ctrl) {
  98                 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
  99                               "instancetype_add: no current partition control found");
 100                 return LDB_ERR_CONSTRAINT_VIOLATION;
 101         }
 102 
 103         partition = talloc_get_type(partition_ctrl->data,
 104                                     struct dsdb_control_current_partition);
 105         SMB_ASSERT(partition && partition->version == DSDB_CONTROL_CURRENT_PARTITION_VERSION);
 106 
 107         ac = talloc(req, struct it_context);
 108         if (ac == NULL) {
 109                 return LDB_ERR_OPERATIONS_ERROR;
 110         }
 111         ac->module = module;
 112         ac->req = req;
 113 
 114         /* we have to copy the message as the caller might have it as a const */
 115         msg = ldb_msg_copy_shallow(ac, req->op.add.message);
 116         if (msg == NULL) {
 117                 ldb_oom(ldb);
 118                 return LDB_ERR_OPERATIONS_ERROR;
 119         }
 120 
 121         /*
 122          * TODO: calculate correct instance type
 123          */
 124         instance_type = INSTANCE_TYPE_WRITE;
 125         if (ldb_dn_compare(partition->dn, msg->dn) == 0) {
 126                 instance_type |= INSTANCE_TYPE_IS_NC_HEAD;
 127                 if (ldb_dn_compare(msg->dn, samdb_base_dn(ldb)) != 0) {
 128                         instance_type |= INSTANCE_TYPE_NC_ABOVE;
 129                 }
 130         }
 131 
 132         ret = ldb_msg_add_fmt(msg, "instanceType", "%u", instance_type);
 133         if (ret != LDB_SUCCESS) {
 134                 ldb_oom(ldb);
 135                 return LDB_ERR_OPERATIONS_ERROR;
 136         }
 137 
 138         ret = ldb_build_add_req(&down_req, ldb, ac,
 139                                 msg,
 140                                 req->controls,
 141                                 ac, it_callback,
 142                                 req);
 143         if (ret != LDB_SUCCESS) {
 144                 return ret;
 145         }
 146 
 147         /* go on with the call chain */
 148         return ldb_next_request(module, down_req);
 149 }
 150 
 151 _PUBLIC_ const struct ldb_module_ops ldb_instancetype_module_ops = {
 152         .name          = "instancetype",
 153         .add           = instancetype_add,
 154 };

/* [<][>][^][v][top][bottom][index][help] */