/* add a string element to a message, stealing it into the 'right' place */ int ldb_msg_add_steal_string(struct ldb_message *msg, const char *attr_name, char *str) { struct ldb_val val; val.data = (uint8_t *)str; val.length = strlen(str); if (val.length == 0) { /* allow empty strings as non-existent attributes */ return LDB_SUCCESS; } return ldb_msg_add_steal_value(msg, attr_name, &val); }
/* add a printf formatted element to a message */ int ldb_msg_add_fmt(struct ldb_message *msg, const char *attr_name, const char *fmt, ...) { struct ldb_val val; va_list ap; char *str; va_start(ap, fmt); str = talloc_vasprintf(msg, fmt, ap); va_end(ap); if (str == NULL) return LDB_ERR_OPERATIONS_ERROR; val.data = (uint8_t *)str; val.length = strlen(str); return ldb_msg_add_steal_value(msg, attr_name, &val); }
/* construct the parent GUID for an entry from a message */ static int construct_parent_guid(struct ldb_module *module, struct ldb_message *msg, enum ldb_scope scope, struct ldb_request *parent) { struct ldb_result *res, *parent_res; const struct ldb_val *parent_guid; const char *attrs[] = { "instanceType", NULL }; const char *attrs2[] = { "objectGUID", NULL }; uint32_t instanceType; int ret; struct ldb_dn *parent_dn; struct ldb_val v; /* determine if the object is NC by instance type */ ret = dsdb_module_search_dn(module, msg, &res, msg->dn, attrs, DSDB_FLAG_NEXT_MODULE | DSDB_SEARCH_SHOW_RECYCLED, parent); if (ret != LDB_SUCCESS) { return ret; } instanceType = ldb_msg_find_attr_as_uint(res->msgs[0], "instanceType", 0); talloc_free(res); if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) { DEBUG(4,(__location__ ": Object %s is NC\n", ldb_dn_get_linearized(msg->dn))); return LDB_SUCCESS; } parent_dn = ldb_dn_get_parent(msg, msg->dn); if (parent_dn == NULL) { DEBUG(4,(__location__ ": Failed to find parent for dn %s\n", ldb_dn_get_linearized(msg->dn))); return LDB_SUCCESS; } ret = dsdb_module_search_dn(module, msg, &parent_res, parent_dn, attrs2, DSDB_FLAG_NEXT_MODULE | DSDB_SEARCH_SHOW_RECYCLED, parent); talloc_free(parent_dn); /* not NC, so the object should have a parent*/ if (ret == LDB_ERR_NO_SUCH_OBJECT) { return ldb_error(ldb_module_get_ctx(module), LDB_ERR_OPERATIONS_ERROR, talloc_asprintf(msg, "Parent dn for %s does not exist", ldb_dn_get_linearized(msg->dn))); } else if (ret != LDB_SUCCESS) { return ret; } parent_guid = ldb_msg_find_ldb_val(parent_res->msgs[0], "objectGUID"); if (!parent_guid) { talloc_free(parent_res); return LDB_SUCCESS; } v = data_blob_dup_talloc(parent_res, *parent_guid); if (!v.data) { talloc_free(parent_res); return ldb_oom(ldb_module_get_ctx(module)); } ret = ldb_msg_add_steal_value(msg, "parentGUID", &v); talloc_free(parent_res); return ret; }