int dsdb_user_obj_set_defaults(struct ldb_context *ldb,
			       struct ldb_message *usr_obj,
			       struct ldb_request *req)
{
	size_t i;
	int ret;
	const struct attribute_values {
		const char *name;
		const char *value;
		const char *add_value;
		const char *mod_value;
		const char *control;
		unsigned add_flags;
		unsigned mod_flags;
	} map[] = {
		{
			.name = "accountExpires",
			.add_value = "9223372036854775807",
			.mod_value = "0",
		},
		{
			.name = "badPasswordTime",
			.value = "0"
		},
		{
			.name = "badPwdCount",
			.value = "0"
		},
		{
			.name = "codePage",
			.value = "0"
		},
		{
			.name = "countryCode",
			.value = "0"
		},
		{
			.name = "lastLogoff",
			.value = "0"
		},
		{
			.name = "lastLogon",
			.value = "0"
		},
		{
			.name = "logonCount",
			.value = "0"
		},
		{
			.name = "logonHours",
			.add_flags = DSDB_FLAG_INTERNAL_FORCE_META_DATA,
		},
		{
			.name = "pwdLastSet",
			.value = "0",
			.control = DSDB_CONTROL_PASSWORD_DEFAULT_LAST_SET_OID,
		},
		{
			.name = "adminCount",
			.mod_value = "0",
		},
		{
			.name = "operatorCount",
			.mod_value = "0",
		},
	};

	for (i = 0; i < ARRAY_SIZE(map); i++) {
		bool added = false;
		const char *value = NULL;
		unsigned flags = 0;

		if (req != NULL && req->operation == LDB_ADD) {
			value = map[i].add_value;
			flags = map[i].add_flags;
		} else {
			value = map[i].mod_value;
			flags = map[i].mod_flags;
		}

		if (value == NULL) {
			value = map[i].value;
		}

		if (value != NULL) {
			flags |= LDB_FLAG_MOD_ADD;
		}

		if (flags == 0) {
			continue;
		}

		ret = samdb_find_or_add_attribute_ex(ldb, usr_obj,
						     map[i].name,
						     value, flags,
						     &added);
		if (ret != LDB_SUCCESS) {
			return ret;
		}

		if (req != NULL && added && map[i].control != NULL) {
			ret = ldb_request_add_control(req,
						      map[i].control,
						      false, NULL);
			if (ret != LDB_SUCCESS) {
				return ret;
			}
		}
	}

	return LDB_SUCCESS;
}
int dsdb_user_obj_set_defaults(struct ldb_context *ldb,
			       struct ldb_message *usr_obj,
			       struct ldb_request *req)
{
	size_t i;
	int ret;
	const struct attribute_values {
		const char *name;
		const char *value;
		const char *add_value;
		const char *mod_value;
		const char *control;
		unsigned add_flags;
		unsigned mod_flags;
	} map[] = {
		{
			.name = "accountExpires",
			.add_value = "9223372036854775807",
			.mod_value = "0",
		},
		{
			.name = "badPasswordTime",
			.value = "0"
		},
		{
			.name = "badPwdCount",
			.value = "0"
		},
		{
			.name = "codePage",
			.value = "0"
		},
		{
			.name = "countryCode",
			.value = "0"
		},
		{
			.name = "lastLogoff",
			.value = "0"
		},
		{
			.name = "lastLogon",
			.value = "0"
		},
		{
			.name = "logonCount",
			.value = "0"
		},
		{
			.name = "logonHours",
			.add_flags = DSDB_FLAG_INTERNAL_FORCE_META_DATA,
		},
		{
			.name = "pwdLastSet",
			.value = "0",
			.control = DSDB_CONTROL_PASSWORD_DEFAULT_LAST_SET_OID,
		},
		{
			.name = "adminCount",
			.mod_value = "0",
		},
		{
			.name = "operatorCount",
			.mod_value = "0",
		},
	};

	for (i = 0; i < ARRAY_SIZE(map); i++) {
		bool added = false;
		const char *value = NULL;
		unsigned flags = 0;

		if (req != NULL && req->operation == LDB_ADD) {
			value = map[i].add_value;
			flags = map[i].add_flags;
		} else {
			value = map[i].mod_value;
			flags = map[i].mod_flags;
		}

		if (value == NULL) {
			value = map[i].value;
		}

		if (value != NULL) {
			flags |= LDB_FLAG_MOD_ADD;
		}

		if (flags == 0) {
			continue;
		}

		ret = samdb_find_or_add_attribute_ex(ldb, usr_obj,
						     map[i].name,
						     value, flags,
						     &added);
		if (ret != LDB_SUCCESS) {
			return ret;
		}

		if (req != NULL && added && map[i].control != NULL) {
			ret = ldb_request_add_control(req,
						      map[i].control,
						      false, NULL);
			if (ret != LDB_SUCCESS) {
				return ret;
			}
		}
	}

	return LDB_SUCCESS;
}