name = ldb_msg_find_attr_as_string(ac->msg, "sAMAccountName", NULL);
if (name == NULL) {
- return ldb_operr(ldb);
+ /* The "sAMAccountName" cannot be nothing */
+ ldb_set_errstring(ldb,
+ "samldb: Empty account names aren't allowed!");
+ return LDB_ERR_CONSTRAINT_VIOLATION;
}
ret = samdb_search_count(ldb, ac, NULL, "(sAMAccountName=%s)",
return LDB_SUCCESS;
}
-/*
- * Gets back a single-valued attribute by the rules of the SAM triggers when
- * performing a modify operation
- */
-static int samldb_get_single_valued_attr(struct samldb_ctx *ac,
- const char *attr_name,
- struct ldb_message_element **attr)
-{
- struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
- struct ldb_message_element *el = NULL;
- unsigned int i;
-
- /* We've to walk over all modification entries and consider the
- * "attr_name" ones.
- *
- * 1.) Add operations aren't allowed and there is returned
- * "ATTRIBUTE_OR_VALUE_EXISTS".
- * 2.) Replace operations are allowed but the last one is taken
- * 3.) Delete operations are also not allowed and there is returned
- * "UNWILLING_TO_PERFORM".
- *
- * If "el" is afterwards NULL then that means we've nothing to do here.
- */
- for (i = 0; i < ac->msg->num_elements; i++) {
- if (ldb_attr_cmp(ac->msg->elements[i].name, attr_name) != 0) {
- continue;
- }
-
- el = &ac->msg->elements[i];
- if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD) {
- ldb_asprintf_errstring(ldb,
- "samldb: attribute '%s' already exists!",
- attr_name);
- return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
- }
- if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
- ldb_asprintf_errstring(ldb,
- "samldb: attribute '%s' cannot be deleted!",
- attr_name);
- return LDB_ERR_UNWILLING_TO_PERFORM;
- }
- }
-
- *attr = el;
- return LDB_SUCCESS;
-}
-
/*
* "Objectclass" trigger (MS-SAMR 3.1.1.8.1)
*
sid = samdb_result_dom_sid(ac, ac->msg, "objectSid");
if ((sid != NULL) && (!dsdb_module_am_system(ac->module)) &&
(ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) == NULL)) {
- ldb_asprintf_errstring(ldb,
- "samldb: no SID may be specified in user/group modifications for %s",
- ldb_dn_get_linearized(ac->msg->dn));
+ ldb_set_errstring(ldb,
+ "samldb: objectSid must not be specified!");
return LDB_ERR_UNWILLING_TO_PERFORM;
}
struct ldb_result *res;
struct ldb_message_element *el;
struct ldb_message *msg;
- uint32_t rid;
- struct dom_sid *sid;
+ uint32_t prev_rid, new_rid;
+ struct dom_sid *prev_sid, *new_sid;
struct ldb_dn *prev_prim_group_dn, *new_prim_group_dn;
int ret;
- ret = samldb_get_single_valued_attr(ac, "primaryGroupID", &el);
- if (ret != LDB_SUCCESS) {
- return ret;
- }
+ el = dsdb_get_single_valued_attr(ac->msg, "primaryGroupID");
if (el == NULL) {
/* we are not affected */
return LDB_SUCCESS;
/* Finds out the DN of the old primary group */
- rid = ldb_msg_find_attr_as_uint(res->msgs[0], "primaryGroupID", (uint32_t) -1);
- if (rid == (uint32_t) -1) {
+ prev_rid = ldb_msg_find_attr_as_uint(res->msgs[0], "primaryGroupID",
+ (uint32_t) -1);
+ if (prev_rid == (uint32_t) -1) {
/* User objects do always have a mandatory "primaryGroupID"
* attribute. If this doesn't exist then the object is of the
* wrong type. This is the exact Windows error code */
return LDB_ERR_OBJECT_CLASS_VIOLATION;
}
- sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
- if (sid == NULL) {
- return ldb_operr(ldb);
- }
-
- prev_prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSid=%s)",
- ldap_encode_ndr_dom_sid(ac, sid));
- if (prev_prim_group_dn == NULL) {
+ prev_sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), prev_rid);
+ if (prev_sid == NULL) {
return ldb_operr(ldb);
}
if (ret != LDB_SUCCESS) {
return ret;
}
- rid = ldb_msg_find_attr_as_uint(msg, "primaryGroupID", (uint32_t) -1);
+ new_rid = ldb_msg_find_attr_as_uint(msg, "primaryGroupID", (uint32_t) -1);
talloc_free(msg);
- if (rid == (uint32_t) -1) {
+ if (new_rid == (uint32_t) -1) {
/* we aren't affected of any primary group change */
return LDB_SUCCESS;
}
- sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
- if (sid == NULL) {
+ if (prev_rid == new_rid) {
+ return LDB_SUCCESS;
+ }
+
+ prev_prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSid=%s)",
+ ldap_encode_ndr_dom_sid(ac, prev_sid));
+ if (prev_prim_group_dn == NULL) {
+ return ldb_operr(ldb);
+ }
+
+ new_sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), new_rid);
+ if (new_sid == NULL) {
return ldb_operr(ldb);
}
new_prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSid=%s)",
- ldap_encode_ndr_dom_sid(ac, sid));
+ ldap_encode_ndr_dom_sid(ac, new_sid));
if (new_prim_group_dn == NULL) {
/* Here we know if the specified new primary group candidate is
* valid or not. */
return LDB_ERR_UNWILLING_TO_PERFORM;
}
- /* Only update the "member" attributes when we really do have a change */
- if (ldb_dn_compare(new_prim_group_dn, prev_prim_group_dn) != 0) {
- /* We need to be already a normal member of the new primary
- * group in order to be successful. */
- el = samdb_find_attribute(ldb, res->msgs[0], "memberOf",
- ldb_dn_get_linearized(new_prim_group_dn));
- if (el == NULL) {
- return LDB_ERR_UNWILLING_TO_PERFORM;
- }
-
- /* Remove the "member" attribute on the new primary group */
- msg = ldb_msg_new(ac->msg);
- if (msg == NULL) {
- return ldb_module_oom(ac->module);
- }
- msg->dn = new_prim_group_dn;
+ /* We need to be already a normal member of the new primary
+ * group in order to be successful. */
+ el = samdb_find_attribute(ldb, res->msgs[0], "memberOf",
+ ldb_dn_get_linearized(new_prim_group_dn));
+ if (el == NULL) {
+ return LDB_ERR_UNWILLING_TO_PERFORM;
+ }
- ret = samdb_msg_add_delval(ldb, msg, msg, "member",
- ldb_dn_get_linearized(ac->msg->dn));
- if (ret != LDB_SUCCESS) {
- return ret;
- }
+ /* Remove the "member" attribute on the new primary group */
+ msg = ldb_msg_new(ac->msg);
+ if (msg == NULL) {
+ return ldb_module_oom(ac->module);
+ }
+ msg->dn = new_prim_group_dn;
- ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE);
- if (ret != LDB_SUCCESS) {
- return ret;
- }
- talloc_free(msg);
+ ret = samdb_msg_add_delval(ldb, msg, msg, "member",
+ ldb_dn_get_linearized(ac->msg->dn));
+ if (ret != LDB_SUCCESS) {
+ return ret;
+ }
- /* Add a "member" attribute for the previous primary group */
- msg = ldb_msg_new(ac->msg);
- if (msg == NULL) {
- return ldb_module_oom(ac->module);
- }
- msg->dn = prev_prim_group_dn;
+ ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE);
+ if (ret != LDB_SUCCESS) {
+ return ret;
+ }
+ talloc_free(msg);
- ret = samdb_msg_add_addval(ldb, msg, msg, "member",
- ldb_dn_get_linearized(ac->msg->dn));
- if (ret != LDB_SUCCESS) {
- return ret;
- }
+ /* Add a "member" attribute for the previous primary group */
+ msg = ldb_msg_new(ac->msg);
+ if (msg == NULL) {
+ return ldb_module_oom(ac->module);
+ }
+ msg->dn = prev_prim_group_dn;
- ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE);
- if (ret != LDB_SUCCESS) {
- return ret;
- }
- talloc_free(msg);
+ ret = samdb_msg_add_addval(ldb, msg, msg, "member",
+ ldb_dn_get_linearized(ac->msg->dn));
+ if (ret != LDB_SUCCESS) {
+ return ret;
}
- talloc_free(res);
+ ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE);
+ if (ret != LDB_SUCCESS) {
+ return ret;
+ }
+ talloc_free(msg);
return LDB_SUCCESS;
}
struct ldb_message *tmp_msg;
int ret;
- ret = samldb_get_single_valued_attr(ac, "userAccountControl", &el);
- if (ret != LDB_SUCCESS) {
- return ret;
- }
+ el = dsdb_get_single_valued_attr(ac->msg, "userAccountControl");
if (el == NULL) {
/* we are not affected */
return LDB_SUCCESS;
struct ldb_message *tmp_msg;
int ret;
- ret = samldb_get_single_valued_attr(ac, "groupType", &el);
- if (ret != LDB_SUCCESS) {
- return ret;
- }
+ el = dsdb_get_single_valued_attr(ac->msg, "groupType");
if (el == NULL) {
/* we are not affected */
return LDB_SUCCESS;
struct ldb_message *tmp_msg;
int ret;
- ret = samldb_get_single_valued_attr(ac, "sAMAccountName", &el);
- if (ret != LDB_SUCCESS) {
- return ret;
- }
+ el = dsdb_get_single_valued_attr(ac->msg, "sAMAccountName");
if (el == NULL) {
/* we are not affected */
return LDB_SUCCESS;
if (ret != LDB_SUCCESS) {
return ret;
}
- sam_accountname = ldb_msg_find_attr_as_string(tmp_msg, "sAMAccountName",
- NULL);
+ sam_accountname = talloc_steal(ac,
+ ldb_msg_find_attr_as_string(tmp_msg, "sAMAccountName", NULL));
talloc_free(tmp_msg);
if (sam_accountname == NULL) {
- return ldb_operr(ldb);
+ /* The "sAMAccountName" cannot be nothing */
+ ldb_set_errstring(ldb,
+ "samldb: Empty account names aren't allowed!");
+ return LDB_ERR_UNWILLING_TO_PERFORM;
}
enc_str = ldb_binary_encode_string(ac, sam_accountname);
static int samldb_member_check(struct samldb_ctx *ac)
{
+ static const char * const attrs[] = { "objectSid", "member", NULL };
struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
struct ldb_message_element *el;
- struct ldb_dn *member_dn, *group_dn;
+ struct ldb_dn *member_dn;
uint32_t prim_group_rid;
struct dom_sid *sid;
+ struct ldb_result *res;
+ struct dom_sid *group_sid;
unsigned int i, j;
int cnt;
+ int ret;
+
+ /* Fetch informations from the existing object */
+
+ ret = ldb_search(ldb, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
+ NULL);
+ if (ret != LDB_SUCCESS) {
+ return ret;
+ }
+ if (res->count != 1) {
+ return ldb_operr(ldb);
+ }
+
+ group_sid = samdb_result_dom_sid(res, res->msgs[0], "objectSid");
+ if (group_sid == NULL) {
+ return ldb_operr(ldb);
+ }
/* We've to walk over all modification entries and consider the "member"
* ones. */
el = &ac->msg->elements[i];
for (j = 0; j < el->num_values; j++) {
+ struct ldb_message_element *mo;
+
member_dn = ldb_dn_from_ldb_val(ac, ldb,
&el->values[j]);
if (!ldb_dn_validate(member_dn)) {
* ERR_NO_SUCH_ATTRIBUTE!)
* - primary group check
*/
- cnt = samdb_search_count(ldb, ac, ac->msg->dn,
- "(member=%s)",
- ldb_dn_get_linearized(member_dn));
- if (cnt < 0) {
- return ldb_operr(ldb);
+ mo = samdb_find_attribute(ldb, res->msgs[0], "member",
+ ldb_dn_get_linearized(member_dn));
+ if (mo == NULL) {
+ cnt = 0;
+ } else {
+ cnt = 1;
}
+
if ((cnt > 0) && (LDB_FLAG_MOD_TYPE(el->flags)
== LDB_FLAG_MOD_ADD)) {
return LDB_ERR_ENTRY_ALREADY_EXISTS;
return ldb_operr(ldb);
}
- group_dn = samdb_search_dn(ldb, ac, NULL,
- "(objectSid=%s)",
- ldap_encode_ndr_dom_sid(ac, sid));
- if (group_dn == NULL) {
- return ldb_operr(ldb);
- }
-
- if (ldb_dn_compare(group_dn, ac->msg->dn) == 0) {
+ if (dom_sid_equal(group_sid, sid)) {
return LDB_ERR_ENTRY_ALREADY_EXISTS;
}
}
}
/* This trigger adapts the "servicePrincipalName" attributes if the
- * "dNSHostName" attribute changes */
+ * "dNSHostName" and/or "sAMAccountName" attribute change(s) */
static int samldb_service_principal_names_change(struct samldb_ctx *ac)
{
struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
- struct ldb_message_element *el = NULL;
+ struct ldb_message_element *el = NULL, *el2 = NULL;
struct ldb_message *msg;
const char *attrs[] = { "servicePrincipalName", NULL };
struct ldb_result *res;
- const char *dns_hostname, *old_dns_hostname;
+ const char *dns_hostname = NULL, *old_dns_hostname = NULL,
+ *sam_accountname = NULL, *old_sam_accountname = NULL;
unsigned int i;
int ret;
- /* Here it's not the same logic as with "samldb_get_single_valued_attr".
- * We need to:
- *
- * - consider "add" and "replace" operations - the last value we take
- * - ignore "delete" operations - obviously this attribute isn't
- * write protected
- */
- for (i = 0; i < ac->msg->num_elements; i++) {
- if (ldb_attr_cmp(ac->msg->elements[i].name,
- "dNSHostName") != 0) {
- continue;
+ el = dsdb_get_single_valued_attr(ac->msg, "dNSHostName");
+ el2 = dsdb_get_single_valued_attr(ac->msg, "sAMAccountName");
+ if ((el == NULL) && (el2 == NULL)) {
+ /* we are not affected */
+ return LDB_SUCCESS;
+ }
+
+ /* Create a temporary message for fetching the "dNSHostName" */
+ if (el != NULL) {
+ msg = ldb_msg_new(ac->msg);
+ if (msg == NULL) {
+ return ldb_module_oom(ac->module);
+ }
+ ret = ldb_msg_add(msg, el, 0);
+ if (ret != LDB_SUCCESS) {
+ return ret;
}
+ dns_hostname = talloc_steal(ac,
+ ldb_msg_find_attr_as_string(msg, "dNSHostName", NULL));
+ talloc_free(msg);
+
+ old_dns_hostname = samdb_search_string(ldb, ac, ac->msg->dn,
+ "dNSHostName", NULL);
+ }
- if (LDB_FLAG_MOD_TYPE(ac->msg->elements[i].flags)
- != LDB_FLAG_MOD_DELETE) {
- el = &ac->msg->elements[i];
+ /* Create a temporary message for fetching the "sAMAccountName" */
+ if (el2 != NULL) {
+ char *tempstr, *tempstr2;
+
+ msg = ldb_msg_new(ac->msg);
+ if (msg == NULL) {
+ return ldb_module_oom(ac->module);
+ }
+ ret = ldb_msg_add(msg, el2, 0);
+ if (ret != LDB_SUCCESS) {
+ return ret;
}
+ tempstr = talloc_strdup(ac,
+ ldb_msg_find_attr_as_string(msg, "sAMAccountName", NULL));
+ talloc_free(msg);
+
+ tempstr2 = talloc_strdup(ac,
+ samdb_search_string(ldb, ac, ac->msg->dn, "sAMAccountName", NULL));
+
+ /* The "sAMAccountName" needs some additional trimming: we need
+ * to remove the trailing "$"s if they exist. */
+ if ((tempstr != NULL) && (tempstr[0] != '\0') &&
+ (tempstr[strlen(tempstr) - 1] == '$')) {
+ tempstr[strlen(tempstr) - 1] = '\0';
+ }
+ if ((tempstr2 != NULL) && (tempstr2[0] != '\0') &&
+ (tempstr2[strlen(tempstr2) - 1] == '$')) {
+ tempstr2[strlen(tempstr2) - 1] = '\0';
+ }
+ sam_accountname = tempstr;
+ old_sam_accountname = tempstr2;
}
- if (el == NULL) {
- /* we are not affected */
- return LDB_SUCCESS;
+
+ if (old_dns_hostname == NULL) {
+ /* we cannot change when the old name is unknown */
+ dns_hostname = NULL;
+ }
+ if ((old_dns_hostname != NULL) && (dns_hostname != NULL) &&
+ (strcasecmp(old_dns_hostname, dns_hostname) == 0)) {
+ /* The "dNSHostName" didn't change */
+ dns_hostname = NULL;
}
- /* Create a temporary message for fetching the "dNSHostName" */
- msg = ldb_msg_new(ac->msg);
- if (msg == NULL) {
- return ldb_module_oom(ac->module);
+ if (old_sam_accountname == NULL) {
+ /* we cannot change when the old name is unknown */
+ sam_accountname = NULL;
}
- ret = ldb_msg_add(msg, el, 0);
- if (ret != LDB_SUCCESS) {
- return ret;
+ if ((old_sam_accountname != NULL) && (sam_accountname != NULL) &&
+ (strcasecmp(old_sam_accountname, sam_accountname) == 0)) {
+ /* The "sAMAccountName" didn't change */
+ sam_accountname = NULL;
}
- dns_hostname = ldb_msg_find_attr_as_string(msg, "dNSHostName", "");
- talloc_free(msg);
- old_dns_hostname = samdb_search_string(ldb, ac, ac->msg->dn,
- "dNSHostName", NULL);
- if ((old_dns_hostname == NULL) || (strcasecmp(old_dns_hostname,
- dns_hostname) == 0)) {
- /* Well, if there's no old DNS hostname then we cannot do this.
- * And if old and new DNS name do match we are also finished
- * here. */
+ if ((dns_hostname == NULL) && (sam_accountname == NULL)) {
+ /* Well, there are informations missing (old name(s)) or the
+ * names didn't change. We've nothing to do and can exit here */
return LDB_SUCCESS;
}
if (res->msgs[0]->num_elements == 1) {
/* Yes, we do have "servicePrincipalName"s. First we update them
* locally, that means we do always substitute the current
- * "dNSHostName" with the new one and then we append this to the
+ * "dNSHostName" with the new one and/or "sAMAccountName"
+ * without "$" with the new one and then we append this to the
* modification request (Windows behaviour). */
for (i = 0; i < res->msgs[0]->elements[0].num_values; i++) {
}
while ((tok = strtok_r(NULL, "/", &pos)) != NULL) {
- if (strcasecmp(tok, old_dns_hostname) == 0) {
+ if ((dns_hostname != NULL) &&
+ (strcasecmp(tok, old_dns_hostname) == 0)) {
tok = dns_hostname;
}
+ if ((sam_accountname != NULL) &&
+ (strcasecmp(tok, old_sam_accountname) == 0)) {
+ tok = sam_accountname;
+ }
new_str = talloc_asprintf(ac->msg, "%s/%s",
new_str, tok);
{
struct ldb_context *ldb;
struct samldb_ctx *ac;
- struct ldb_message_element *el;
+ struct ldb_message_element *el, *el2;
bool modified = false;
int ret;
ldb = ldb_module_get_ctx(module);
+ /* make sure that "objectSid" is not specified */
+ el = ldb_msg_find_element(req->op.mod.message, "objectSid");
+ if (el != NULL) {
+ ldb_set_errstring(ldb,
+ "samldb: objectSid must not be specified!");
+ return LDB_ERR_UNWILLING_TO_PERFORM;
+ }
/* make sure that "sAMAccountType" is not specified */
el = ldb_msg_find_element(req->op.mod.message, "sAMAccountType");
if (el != NULL) {
}
el = ldb_msg_find_element(ac->msg, "dNSHostName");
- if (el != NULL) {
+ el2 = ldb_msg_find_element(ac->msg, "sAMAccountName");
+ if ((el != NULL) || (el2 != NULL)) {
modified = true;
ret = samldb_service_principal_names_change(ac);
if (ret != LDB_SUCCESS) {
}
-_PUBLIC_ const struct ldb_module_ops ldb_samldb_module_ops = {
+static const struct ldb_module_ops ldb_samldb_module_ops = {
.name = "samldb",
.add = samldb_add,
.modify = samldb_modify,
.extended = samldb_extended
};
+
+int ldb_samldb_module_init(const char *version)
+{
+ LDB_MODULE_CHECK_VERSION(version);
+ return ldb_register_module(&ldb_samldb_module_ops);
+}