2 Unix SMB/CIFS implementation.
4 Copyright (C) Guenther Deschner <gd@samba.org> 2008
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "system/passwd.h"
22 #include "libnet/libnet_dssync.h"
23 #include "../libcli/security/security.h"
24 #include "../libds/common/flags.h"
25 #include "../librpc/gen_ndr/ndr_drsuapi.h"
27 #include "dbwrap/dbwrap.h"
28 #include "dbwrap/dbwrap_rbt.h"
29 #include "../libds/common/flag_mapping.h"
31 #include "lib/util/base64.h"
33 /****************************************************************
34 ****************************************************************/
36 struct dssync_passdb {
37 struct pdb_methods *methods;
38 struct db_context *all;
39 struct db_context *aliases;
40 struct db_context *groups;
43 struct dssync_passdb_obj {
44 struct dssync_passdb_obj *self;
46 struct drsuapi_DsReplicaObjectListItemEx *cur;
49 struct db_context *members;
52 struct dssync_passdb_mem {
53 struct dssync_passdb_mem *self;
55 struct drsuapi_DsReplicaObjectIdentifier3 *cur;
56 struct dssync_passdb_obj *obj;
61 static NTSTATUS dssync_insert_obj(struct dssync_passdb *pctx,
62 struct db_context *db,
63 struct dssync_passdb_obj *obj)
66 struct db_record *rec;
69 rec = dbwrap_fetch_locked(db, talloc_tos(), obj->key);
71 return NT_STATUS_NO_MEMORY;
74 value = dbwrap_record_get_value(rec);
75 if (value.dsize != 0) {
79 status = dbwrap_record_store(rec, obj->data, TDB_INSERT);
80 if (!NT_STATUS_IS_OK(status)) {
88 static struct dssync_passdb_obj *dssync_parse_obj(const TDB_DATA data)
90 struct dssync_passdb_obj *obj;
92 if (data.dsize != sizeof(obj)) {
97 * we need to copy the pointer to avoid alignment problems
100 memcpy(&obj, data.dptr, sizeof(obj));
102 return talloc_get_type_abort(obj, struct dssync_passdb_obj);
105 static struct dssync_passdb_obj *dssync_search_obj_by_guid(struct dssync_passdb *pctx,
106 struct db_context *db,
107 const struct GUID *guid)
109 struct dssync_passdb_obj *obj;
114 key = make_tdb_data((const uint8_t *)(const void *)guid,
117 status = dbwrap_fetch(db, talloc_tos(), key, &data);
118 if (!NT_STATUS_IS_OK(status)) {
122 obj = dssync_parse_obj(data);
126 static NTSTATUS dssync_create_obj(struct dssync_passdb *pctx,
127 struct db_context *db,
129 struct drsuapi_DsReplicaObjectListItemEx *cur,
130 struct dssync_passdb_obj **_obj)
133 struct dssync_passdb_obj *obj;
135 obj = talloc_zero(pctx, struct dssync_passdb_obj);
137 return NT_STATUS_NO_MEMORY;
142 obj->key = make_tdb_data((const uint8_t *)(void *)&cur->object.identifier->guid,
143 sizeof(cur->object.identifier->guid));
144 obj->data = make_tdb_data((const uint8_t *)(void *)&obj->self,
147 obj->members = db_open_rbt(obj);
148 if (obj->members == NULL) {
149 return NT_STATUS_NO_MEMORY;
152 status = dssync_insert_obj(pctx, db, obj);
153 if (!NT_STATUS_IS_OK(status)) {
161 static NTSTATUS dssync_insert_mem(struct dssync_passdb *pctx,
162 struct dssync_passdb_obj *obj,
163 struct dssync_passdb_mem *mem)
166 struct db_record *rec;
169 rec = dbwrap_fetch_locked(obj->members, talloc_tos(), mem->key);
171 return NT_STATUS_NO_MEMORY;
174 value = dbwrap_record_get_value(rec);
175 if (value.dsize != 0) {
179 status = dbwrap_record_store(rec, mem->data, TDB_INSERT);
180 if (!NT_STATUS_IS_OK(status)) {
188 static NTSTATUS dssync_create_mem(struct dssync_passdb *pctx,
189 struct dssync_passdb_obj *obj,
191 struct drsuapi_DsReplicaObjectIdentifier3 *cur,
192 struct dssync_passdb_mem **_mem)
195 struct dssync_passdb_mem *mem;
197 mem = talloc_zero(pctx, struct dssync_passdb_mem);
199 return NT_STATUS_NO_MEMORY;
203 mem->active = active;
205 mem->key = make_tdb_data((const uint8_t *)(void *)&cur->guid,
207 mem->data = make_tdb_data((const uint8_t *)(void *)&mem->self,
210 status = dssync_insert_mem(pctx, obj, mem);
211 if (!NT_STATUS_IS_OK(status)) {
219 static struct dssync_passdb_mem *dssync_parse_mem(const TDB_DATA data)
221 struct dssync_passdb_mem *mem;
223 if (data.dsize != sizeof(mem)) {
228 * we need to copy the pointer to avoid alignment problems
231 memcpy(&mem, data.dptr, sizeof(mem));
233 return talloc_get_type_abort(mem, struct dssync_passdb_mem);
236 static NTSTATUS passdb_startup(struct dssync_context *ctx, TALLOC_CTX *mem_ctx,
237 struct replUpToDateVectorBlob **pold_utdv)
240 struct dssync_passdb *pctx;
242 pctx = talloc_zero(mem_ctx, struct dssync_passdb);
244 return NT_STATUS_NO_MEMORY;
247 if (ctx->output_filename) {
248 status = make_pdb_method_name(&pctx->methods, ctx->output_filename);
250 status = make_pdb_method_name(&pctx->methods, lp_passdb_backend());
253 if (!NT_STATUS_IS_OK(status)) {
257 pctx->all = db_open_rbt(pctx);
258 if (pctx->all == NULL) {
259 return NT_STATUS_NO_MEMORY;
261 pctx->aliases = db_open_rbt(pctx);
262 if (pctx->aliases == NULL) {
263 return NT_STATUS_NO_MEMORY;
265 pctx->groups = db_open_rbt(pctx);
266 if (pctx->groups == NULL) {
267 return NT_STATUS_NO_MEMORY;
270 ctx->private_data = pctx;
275 /****************************************************************
276 ****************************************************************/
278 struct dssync_passdb_traverse_amembers {
279 struct dssync_context *ctx;
280 struct dssync_passdb_obj *obj;
285 struct dssync_passdb_traverse_aliases {
286 struct dssync_context *ctx;
291 static int dssync_passdb_traverse_amembers(struct db_record *rec,
294 struct dssync_passdb_traverse_amembers *state =
295 (struct dssync_passdb_traverse_amembers *)private_data;
296 struct dssync_passdb *pctx =
297 talloc_get_type_abort(state->ctx->private_data,
298 struct dssync_passdb);
299 struct dssync_passdb_mem *mem;
301 struct dom_sid alias_sid;
302 struct dom_sid member_sid;
303 const char *member_dn;
306 struct dom_sid *members;
307 bool is_member = false;
313 alias_sid = state->obj->cur->object.identifier->sid;
315 value = dbwrap_record_get_value(rec);
316 mem = dssync_parse_mem(value);
321 member_sid = mem->cur->sid;
322 member_dn = mem->cur->dn;
324 mem->obj = dssync_search_obj_by_guid(pctx, pctx->all, &mem->cur->guid);
325 if (mem->obj == NULL) {
326 DEBUG(0,("alias[%s] member[%s] can't resolve member - ignoring\n",
327 sid_string_dbg(&alias_sid),
328 is_null_sid(&member_sid)?
329 sid_string_dbg(&member_sid):
334 switch (mem->obj->type) {
335 case ATYPE_DISTRIBUTION_LOCAL_GROUP:
336 case ATYPE_DISTRIBUTION_GLOBAL_GROUP:
337 DEBUG(0, ("alias[%s] ignore distribution group [%s]\n",
338 sid_string_dbg(&alias_sid),
345 DEBUG(0,("alias[%s] member[%s]\n",
346 sid_string_dbg(&alias_sid),
347 sid_string_dbg(&member_sid)));
349 status = pdb_enum_aliasmem(&alias_sid, talloc_tos(),
350 &members, &num_members);
351 if (!NT_STATUS_IS_OK(status)) {
352 DEBUG(0, ("Could not find current alias members %s - %s\n",
353 sid_string_dbg(&alias_sid),
358 for (i=0; i < num_members; i++) {
361 match = dom_sid_equal(&members[i], &member_sid);
368 status = NT_STATUS_OK;
370 if (!is_member && mem->active) {
372 pdb_add_aliasmem(&alias_sid, &member_sid);
373 } else if (is_member && !mem->active) {
375 pdb_del_aliasmem(&alias_sid, &member_sid);
377 if (!NT_STATUS_IS_OK(status)) {
378 DEBUG(0, ("Could not %s %s as alias members of %s - %s\n",
380 sid_string_dbg(&member_sid),
381 sid_string_dbg(&alias_sid),
389 static int dssync_passdb_traverse_aliases(struct db_record *rec,
392 struct dssync_passdb_traverse_aliases *state =
393 (struct dssync_passdb_traverse_aliases *)private_data;
394 struct dssync_passdb *pctx =
395 talloc_get_type_abort(state->ctx->private_data,
396 struct dssync_passdb);
397 struct dssync_passdb_traverse_amembers mstate;
398 struct dssync_passdb_obj *obj;
403 if (pctx->methods == NULL) {
407 value = dbwrap_record_get_value(rec);
408 obj = dssync_parse_obj(value);
414 mstate.ctx = state->ctx;
415 mstate.name = "members";
417 status = dbwrap_traverse_read(obj->members,
418 dssync_passdb_traverse_amembers,
420 if (!NT_STATUS_IS_OK(status)) {
427 struct dssync_passdb_traverse_gmembers {
428 struct dssync_context *ctx;
429 struct dssync_passdb_obj *obj;
434 struct dssync_passdb_traverse_groups {
435 struct dssync_context *ctx;
440 static int dssync_passdb_traverse_gmembers(struct db_record *rec,
443 struct dssync_passdb_traverse_gmembers *state =
444 (struct dssync_passdb_traverse_gmembers *)private_data;
445 struct dssync_passdb *pctx =
446 talloc_get_type_abort(state->ctx->private_data,
447 struct dssync_passdb);
448 struct dssync_passdb_mem *mem;
450 char *nt_member = NULL;
452 struct dom_sid group_sid;
453 struct dom_sid member_sid;
454 struct samu *member = NULL;
455 const char *member_dn = NULL;
459 bool is_unix_member = false;
464 group_sid = state->obj->cur->object.identifier->sid;
466 status = dom_sid_split_rid(talloc_tos(), &group_sid, NULL, &rid);
467 if (!NT_STATUS_IS_OK(status)) {
471 value = dbwrap_record_get_value(rec);
473 mem = dssync_parse_mem(value);
478 member_sid = mem->cur->sid;
479 member_dn = mem->cur->dn;
481 mem->obj = dssync_search_obj_by_guid(pctx, pctx->all, &mem->cur->guid);
482 if (mem->obj == NULL) {
483 DEBUG(0,("group[%s] member[%s] can't resolve member - ignoring\n",
484 sid_string_dbg(&group_sid),
485 is_null_sid(&member_sid)?
486 sid_string_dbg(&member_sid):
491 member_sid = mem->obj->cur->object.identifier->sid;
492 member_dn = mem->obj->cur->object.identifier->dn;
494 switch (mem->obj->type) {
495 case ATYPE_SECURITY_LOCAL_GROUP:
496 case ATYPE_SECURITY_GLOBAL_GROUP:
497 DEBUG(0, ("Group[%s] ignore member group [%s]\n",
498 sid_string_dbg(&group_sid),
499 sid_string_dbg(&member_sid)));
502 case ATYPE_DISTRIBUTION_LOCAL_GROUP:
503 case ATYPE_DISTRIBUTION_GLOBAL_GROUP:
504 DEBUG(0, ("Group[%s] ignore distribution group [%s]\n",
505 sid_string_dbg(&group_sid),
512 map = talloc_zero(NULL, GROUP_MAP);
517 if (!get_domain_group_from_sid(group_sid, map)) {
518 DEBUG(0, ("Could not find global group %s\n",
519 sid_string_dbg(&group_sid)));
520 //return NT_STATUS_NO_SUCH_GROUP;
525 if (!(grp = getgrgid(map->gid))) {
526 DEBUG(0, ("Could not find unix group %lu\n",
527 (unsigned long)map->gid));
528 //return NT_STATUS_NO_SUCH_GROUP;
535 DEBUG(0,("Group members of %s: ", grp->gr_name));
537 if ( !(member = samu_new(talloc_tos())) ) {
538 //return NT_STATUS_NO_MEMORY;
542 if (!pdb_getsampwsid(member, &member_sid)) {
543 DEBUG(1, ("Found bogus group member: (member_sid=%s group=%s)\n",
544 sid_string_tos(&member_sid), grp->gr_name));
549 if (pdb_get_group_rid(member) == rid) {
550 DEBUGADD(0,("%s(primary),", pdb_get_username(member)));
555 DEBUGADD(0,("%s,", pdb_get_username(member)));
556 nt_member = talloc_strdup(talloc_tos(), pdb_get_username(member));
561 unix_members = grp->gr_mem;
563 while (*unix_members) {
564 if (strcmp(*unix_members, nt_member) == 0) {
565 is_unix_member = true;
571 if (!is_unix_member && mem->active) {
572 smb_add_user_group(grp->gr_name, nt_member);
573 } else if (is_unix_member && !mem->active) {
574 smb_delete_user_group(grp->gr_name, nt_member);
580 static int dssync_passdb_traverse_groups(struct db_record *rec,
583 struct dssync_passdb_traverse_groups *state =
584 (struct dssync_passdb_traverse_groups *)private_data;
585 struct dssync_passdb *pctx =
586 talloc_get_type_abort(state->ctx->private_data,
587 struct dssync_passdb);
588 struct dssync_passdb_traverse_gmembers mstate;
589 struct dssync_passdb_obj *obj;
594 if (pctx->methods == NULL) {
598 value = dbwrap_record_get_value(rec);
600 obj = dssync_parse_obj(value);
606 mstate.ctx = state->ctx;
607 mstate.name = "members";
609 status = dbwrap_traverse_read(obj->members,
610 dssync_passdb_traverse_gmembers,
612 if (!NT_STATUS_IS_OK(status)) {
619 static NTSTATUS passdb_finish(struct dssync_context *ctx, TALLOC_CTX *mem_ctx,
620 struct replUpToDateVectorBlob *new_utdv)
622 struct dssync_passdb *pctx =
623 talloc_get_type_abort(ctx->private_data,
624 struct dssync_passdb);
625 struct dssync_passdb_traverse_aliases astate;
626 struct dssync_passdb_traverse_groups gstate;
631 astate.name = "aliases";
632 status = dbwrap_traverse_read(pctx->aliases,
633 dssync_passdb_traverse_aliases,
635 if (!NT_STATUS_IS_OK(status)) {
636 return NT_STATUS_INTERNAL_ERROR;
641 gstate.name = "groups";
642 status = dbwrap_traverse_read(pctx->groups,
643 dssync_passdb_traverse_groups,
645 if (!NT_STATUS_IS_OK(status)) {
646 return NT_STATUS_INTERNAL_ERROR;
649 TALLOC_FREE(pctx->methods);
655 /****************************************************************
656 ****************************************************************/
658 static NTSTATUS smb_create_user(TALLOC_CTX *mem_ctx,
661 struct passwd **passwd_p)
663 struct passwd *passwd;
664 char *add_script = NULL;
666 passwd = Get_Pwnam_alloc(mem_ctx, account);
672 /* Create appropriate user */
673 if (acct_flags & ACB_NORMAL) {
674 add_script = lp_add_user_script(mem_ctx);
675 } else if ( (acct_flags & ACB_WSTRUST) ||
676 (acct_flags & ACB_SVRTRUST) ||
677 (acct_flags & ACB_DOMTRUST) ) {
678 add_script = lp_add_machine_script(mem_ctx);
680 DEBUG(1, ("Unknown user type: %s\n",
681 pdb_encode_acct_ctrl(acct_flags, NEW_PW_FORMAT_SPACE_PADDED_LEN)));
682 return NT_STATUS_UNSUCCESSFUL;
686 return NT_STATUS_NO_MEMORY;
691 add_script = talloc_all_string_sub(mem_ctx, add_script,
694 return NT_STATUS_NO_MEMORY;
696 add_ret = smbrun(add_script, NULL);
697 DEBUG(add_ret ? 0 : 1,("fetch_account: Running the command `%s' "
698 "gave %d\n", add_script, add_ret));
700 smb_nscd_flush_user_cache();
704 /* try and find the possible unix account again */
705 passwd = Get_Pwnam_alloc(mem_ctx, account);
707 return NT_STATUS_NO_SUCH_USER;
715 static struct drsuapi_DsReplicaAttribute *find_drsuapi_attr(
716 const struct drsuapi_DsReplicaObjectListItemEx *cur,
721 for (i = 0; i < cur->object.attribute_ctr.num_attributes; i++) {
722 struct drsuapi_DsReplicaAttribute *attr;
724 attr = &cur->object.attribute_ctr.attributes[i];
726 if (attr->attid == attid) {
734 static NTSTATUS find_drsuapi_attr_string(TALLOC_CTX *mem_ctx,
735 const struct drsuapi_DsReplicaObjectListItemEx *cur,
740 struct drsuapi_DsReplicaAttribute *attr;
744 attr = find_drsuapi_attr(cur, attid);
746 return NT_STATUS_PROPSET_NOT_FOUND;
749 array = talloc_array(mem_ctx, char *, attr->value_ctr.num_values);
751 return NT_STATUS_NO_MEMORY;
754 for (a = 0; a < attr->value_ctr.num_values; a++) {
755 const DATA_BLOB *blob;
758 blob = attr->value_ctr.values[a].blob;
761 return NT_STATUS_INTERNAL_DB_CORRUPTION;
764 ret = pull_string_talloc(array, NULL, 0, &array[a],
765 blob->data, blob->length,
769 return NT_STATUS_INTERNAL_ERROR;
773 *_count = attr->value_ctr.num_values;
778 static NTSTATUS find_drsuapi_attr_int32(TALLOC_CTX *mem_ctx,
779 const struct drsuapi_DsReplicaObjectListItemEx *cur,
784 struct drsuapi_DsReplicaAttribute *attr;
788 attr = find_drsuapi_attr(cur, attid);
790 return NT_STATUS_PROPSET_NOT_FOUND;
793 array = talloc_array(mem_ctx, int32_t, attr->value_ctr.num_values);
795 return NT_STATUS_NO_MEMORY;
798 for (a = 0; a < attr->value_ctr.num_values; a++) {
799 const DATA_BLOB *blob;
801 blob = attr->value_ctr.values[a].blob;
804 return NT_STATUS_INTERNAL_DB_CORRUPTION;
807 if (blob->length != 4) {
808 return NT_STATUS_INTERNAL_DB_CORRUPTION;
811 array[a] = IVAL(blob->data, 0);
814 *_count = attr->value_ctr.num_values;
819 static NTSTATUS find_drsuapi_attr_blob(TALLOC_CTX *mem_ctx,
820 const struct drsuapi_DsReplicaObjectListItemEx *cur,
825 struct drsuapi_DsReplicaAttribute *attr;
829 attr = find_drsuapi_attr(cur, attid);
831 return NT_STATUS_PROPSET_NOT_FOUND;
834 array = talloc_array(mem_ctx, DATA_BLOB, attr->value_ctr.num_values);
836 return NT_STATUS_NO_MEMORY;
839 for (a = 0; a < attr->value_ctr.num_values; a++) {
840 const DATA_BLOB *blob;
842 blob = attr->value_ctr.values[a].blob;
845 return NT_STATUS_INTERNAL_DB_CORRUPTION;
848 array[a] = data_blob_talloc(array, blob->data, blob->length);
849 if (array[a].length != blob->length) {
850 return NT_STATUS_NO_MEMORY;
853 *_count = attr->value_ctr.num_values;
858 static NTSTATUS find_drsuapi_attr_int64(TALLOC_CTX *mem_ctx,
859 const struct drsuapi_DsReplicaObjectListItemEx *cur,
864 struct drsuapi_DsReplicaAttribute *attr;
868 attr = find_drsuapi_attr(cur, attid);
870 return NT_STATUS_PROPSET_NOT_FOUND;
873 array = talloc_array(mem_ctx, int64_t, attr->value_ctr.num_values);
875 return NT_STATUS_NO_MEMORY;
878 for (a = 0; a < attr->value_ctr.num_values; a++) {
879 const DATA_BLOB *blob;
881 blob = attr->value_ctr.values[a].blob;
884 return NT_STATUS_INTERNAL_DB_CORRUPTION;
887 if (blob->length != 8) {
888 return NT_STATUS_INTERNAL_DB_CORRUPTION;
891 array[a] = BVAL(blob->data, 0);
893 *_count = attr->value_ctr.num_values;
898 static NTSTATUS find_drsuapi_attr_dn(TALLOC_CTX *mem_ctx,
899 const struct drsuapi_DsReplicaObjectListItemEx *cur,
902 struct drsuapi_DsReplicaObjectIdentifier3 **_array)
904 struct drsuapi_DsReplicaAttribute *attr;
905 struct drsuapi_DsReplicaObjectIdentifier3 *array;
908 attr = find_drsuapi_attr(cur, attid);
910 return NT_STATUS_PROPSET_NOT_FOUND;
913 array = talloc_array(mem_ctx,
914 struct drsuapi_DsReplicaObjectIdentifier3,
915 attr->value_ctr.num_values);
917 return NT_STATUS_NO_MEMORY;
920 for (a = 0; a < attr->value_ctr.num_values; a++) {
921 const DATA_BLOB *blob;
922 enum ndr_err_code ndr_err;
925 blob = attr->value_ctr.values[a].blob;
928 return NT_STATUS_INTERNAL_DB_CORRUPTION;
931 /* windows sometimes sends an extra two pad bytes here */
932 ndr_err = ndr_pull_struct_blob(blob, array, &array[a],
933 (ndr_pull_flags_fn_t)ndr_pull_drsuapi_DsReplicaObjectIdentifier3);
934 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
935 status = ndr_map_error2ntstatus(ndr_err);
939 *_count = attr->value_ctr.num_values;
944 #define GET_BLOB_EX(attr, needed) do { \
947 DATA_BLOB *_vals = NULL; \
948 attr = data_blob_null; \
949 _status = find_drsuapi_attr_blob(mem_ctx, cur, \
950 DRSUAPI_ATTID_ ## attr, \
952 if (NT_STATUS_EQUAL(_status, NT_STATUS_PROPSET_NOT_FOUND)) { \
954 _status = NT_STATUS_OK; \
958 if (!NT_STATUS_IS_OK(_status)) { \
959 DEBUG(0,(__location__ "attr[%s] %s\n", \
960 #attr, nt_errstr(_status))); \
965 talloc_free(_vals); \
966 DEBUG(0,(__location__ "attr[%s] count[%u]\n", #attr, _cnt)); \
967 return NT_STATUS_OBJECT_NAME_NOT_FOUND; \
969 } else if (_cnt > 1) { \
970 talloc_free(_vals); \
971 DEBUG(0,(__location__ "attr[%s] count[%u]\n", #attr, _cnt)); \
972 return NT_STATUS_INTERNAL_DB_CORRUPTION; \
975 (void)talloc_steal(mem_ctx, _vals[0].data); \
977 talloc_free(_vals); \
980 #define GET_STRING_EX(attr, needed) do { \
983 char **_vals = NULL; \
985 _status = find_drsuapi_attr_string(mem_ctx, cur, \
986 DRSUAPI_ATTID_ ## attr, \
988 if (NT_STATUS_EQUAL(_status, NT_STATUS_PROPSET_NOT_FOUND)) { \
990 _status = NT_STATUS_OK; \
994 if (!NT_STATUS_IS_OK(_status)) { \
995 DEBUG(0,(__location__ "attr[%s] %s\n", \
996 #attr, nt_errstr(_status))); \
1001 talloc_free(_vals); \
1002 DEBUG(0,(__location__ "attr[%s] count[%u]\n", #attr, _cnt)); \
1003 return NT_STATUS_OBJECT_NAME_NOT_FOUND; \
1005 } else if (_cnt > 1) { \
1006 talloc_free(_vals); \
1007 DEBUG(0,(__location__ "attr[%s] count[%u]\n", #attr, _cnt)); \
1008 return NT_STATUS_INTERNAL_DB_CORRUPTION; \
1010 attr = talloc_move(mem_ctx, &_vals[0]); \
1012 talloc_free(_vals); \
1015 #define GET_UINT32_EX(attr, needed) do { \
1018 int32_t*_vals = NULL; \
1020 _status = find_drsuapi_attr_int32(mem_ctx, cur, \
1021 DRSUAPI_ATTID_ ## attr, \
1023 if (NT_STATUS_EQUAL(_status, NT_STATUS_PROPSET_NOT_FOUND)) { \
1025 _status = NT_STATUS_OK; \
1029 if (!NT_STATUS_IS_OK(_status)) { \
1030 DEBUG(0,(__location__ "attr[%s] %s\n", \
1031 #attr, nt_errstr(_status))); \
1036 talloc_free(_vals); \
1037 DEBUG(0,(__location__ "attr[%s] count[%u]\n", #attr, _cnt)); \
1038 return NT_STATUS_OBJECT_NAME_NOT_FOUND; \
1040 } else if (_cnt > 1) { \
1041 talloc_free(_vals); \
1042 DEBUG(0,(__location__ "attr[%s] count[%u]\n", #attr, _cnt)); \
1043 return NT_STATUS_INTERNAL_DB_CORRUPTION; \
1045 attr = (uint32_t)_vals[0]; \
1047 talloc_free(_vals); \
1050 #define GET_UINT64_EX(attr, needed) do { \
1053 int64_t *_vals = NULL; \
1055 _status = find_drsuapi_attr_int64(mem_ctx, cur, \
1056 DRSUAPI_ATTID_ ## attr, \
1058 if (NT_STATUS_EQUAL(_status, NT_STATUS_PROPSET_NOT_FOUND)) { \
1060 _status = NT_STATUS_OK; \
1064 if (!NT_STATUS_IS_OK(_status)) { \
1065 DEBUG(0,(__location__ "attr[%s] %s\n", \
1066 #attr, nt_errstr(_status))); \
1071 talloc_free(_vals); \
1072 DEBUG(0,(__location__ "attr[%s] count[%u]\n", #attr, _cnt)); \
1073 return NT_STATUS_OBJECT_NAME_NOT_FOUND; \
1075 } else if (_cnt > 1) { \
1076 talloc_free(_vals); \
1077 DEBUG(0,(__location__ "attr[%s] count[%u]\n", #attr, _cnt)); \
1078 return NT_STATUS_INTERNAL_DB_CORRUPTION; \
1080 attr = (uint64_t)_vals[0]; \
1082 talloc_free(_vals); \
1085 #define GET_BLOB(attr) GET_BLOB_EX(attr, false)
1086 #define GET_STRING(attr) GET_STRING_EX(attr, false)
1087 #define GET_UINT32(attr) GET_UINT32_EX(attr, false)
1088 #define GET_UINT64(attr) GET_UINT64_EX(attr, false)
1090 /* Convert a struct samu_DELTA to a struct samu. */
1091 #define STRING_CHANGED (old_string && !new_string) ||\
1092 (!old_string && new_string) ||\
1093 (old_string && new_string && (strcmp(old_string, new_string) != 0))
1095 #define STRING_CHANGED_NC(s1,s2) ((s1) && !(s2)) ||\
1097 ((s1) && (s2) && (strcmp((s1), (s2)) != 0))
1099 /****************************************************************
1100 ****************************************************************/
1102 static NTSTATUS sam_account_from_object(struct samu *account,
1103 struct drsuapi_DsReplicaObjectListItemEx *cur)
1105 TALLOC_CTX *mem_ctx = account;
1106 const char *old_string, *new_string;
1107 time_t unix_time, stored_time;
1114 NTTIME accountExpires;
1115 const char *sAMAccountName;
1116 const char *displayName;
1117 const char *homeDirectory;
1118 const char *homeDrive;
1119 const char *scriptPath;
1120 const char *profilePath;
1121 const char *description;
1122 const char *userWorkstations;
1123 DATA_BLOB userParameters;
1124 struct dom_sid objectSid;
1125 uint32_t primaryGroupID;
1126 uint32_t userAccountControl;
1127 DATA_BLOB logonHours;
1128 uint32_t badPwdCount;
1129 uint32_t logonCount;
1130 DATA_BLOB unicodePwd;
1134 uint32_t acct_flags;
1135 uint32_t units_per_week;
1137 memset(zero_buf, '\0', sizeof(zero_buf));
1139 objectSid = cur->object.identifier->sid;
1140 GET_STRING_EX(sAMAccountName, true);
1141 DEBUG(0,("sam_account_from_object(%s, %s) start\n",
1142 sAMAccountName, sid_string_dbg(&objectSid)));
1143 GET_UINT64(lastLogon);
1144 GET_UINT64(lastLogoff);
1145 GET_UINT64(pwdLastSet);
1146 GET_UINT64(accountExpires);
1147 GET_STRING(displayName);
1148 GET_STRING(homeDirectory);
1149 GET_STRING(homeDrive);
1150 GET_STRING(scriptPath);
1151 GET_STRING(profilePath);
1152 GET_STRING(description);
1153 GET_STRING(userWorkstations);
1154 GET_BLOB(userParameters);
1155 GET_UINT32(primaryGroupID);
1156 GET_UINT32(userAccountControl);
1157 GET_BLOB(logonHours);
1158 GET_UINT32(badPwdCount);
1159 GET_UINT32(logonCount);
1160 GET_BLOB(unicodePwd);
1163 status = dom_sid_split_rid(mem_ctx, &objectSid, NULL, &rid);
1164 if (!NT_STATUS_IS_OK(status)) {
1167 acct_flags = ds_uf2acb(userAccountControl);
1169 /* Username, fullname, home dir, dir drive, logon script, acct
1170 desc, workstations, profile. */
1172 if (sAMAccountName) {
1173 old_string = pdb_get_nt_username(account);
1174 new_string = sAMAccountName;
1176 if (STRING_CHANGED) {
1177 pdb_set_nt_username(account, new_string, PDB_CHANGED);
1180 /* Unix username is the same - for sanity */
1181 old_string = pdb_get_username( account );
1182 if (STRING_CHANGED) {
1183 pdb_set_username(account, new_string, PDB_CHANGED);
1188 old_string = pdb_get_fullname(account);
1189 new_string = displayName;
1192 pdb_set_fullname(account, new_string, PDB_CHANGED);
1195 if (homeDirectory) {
1196 old_string = pdb_get_homedir(account);
1197 new_string = homeDirectory;
1200 pdb_set_homedir(account, new_string, PDB_CHANGED);
1204 old_string = pdb_get_dir_drive(account);
1205 new_string = homeDrive;
1208 pdb_set_dir_drive(account, new_string, PDB_CHANGED);
1212 old_string = pdb_get_logon_script(account);
1213 new_string = scriptPath;
1216 pdb_set_logon_script(account, new_string, PDB_CHANGED);
1220 old_string = pdb_get_acct_desc(account);
1221 new_string = description;
1224 pdb_set_acct_desc(account, new_string, PDB_CHANGED);
1227 if (userWorkstations) {
1228 old_string = pdb_get_workstations(account);
1229 new_string = userWorkstations;
1232 pdb_set_workstations(account, new_string, PDB_CHANGED);
1236 old_string = pdb_get_profile_path(account);
1237 new_string = profilePath;
1240 pdb_set_profile_path(account, new_string, PDB_CHANGED);
1243 if (userParameters.data) {
1244 char *newstr = NULL;
1245 old_string = pdb_get_munged_dial(account);
1247 if (userParameters.length != 0) {
1248 newstr = base64_encode_data_blob(talloc_tos(),
1250 SMB_ASSERT(newstr != NULL);
1253 if (STRING_CHANGED_NC(old_string, newstr))
1254 pdb_set_munged_dial(account, newstr, PDB_CHANGED);
1255 TALLOC_FREE(newstr);
1258 /* User and group sid */
1259 if (rid != 0 && pdb_get_user_rid(account) != rid) {
1260 pdb_set_user_sid_from_rid(account, rid, PDB_CHANGED);
1262 if (primaryGroupID != 0 && pdb_get_group_rid(account) != primaryGroupID) {
1263 pdb_set_group_sid_from_rid(account, primaryGroupID, PDB_CHANGED);
1266 /* Logon and password information */
1267 if (!nt_time_is_zero(&lastLogon)) {
1268 unix_time = nt_time_to_unix(lastLogon);
1269 stored_time = pdb_get_logon_time(account);
1270 if (stored_time != unix_time)
1271 pdb_set_logon_time(account, unix_time, PDB_CHANGED);
1274 if (!nt_time_is_zero(&lastLogoff)) {
1275 unix_time = nt_time_to_unix(lastLogoff);
1276 stored_time = pdb_get_logoff_time(account);
1277 if (stored_time != unix_time)
1278 pdb_set_logoff_time(account, unix_time,PDB_CHANGED);
1282 units_per_week = logonHours.length * 8;
1284 if (pdb_get_logon_divs(account) != units_per_week) {
1285 pdb_set_logon_divs(account, units_per_week, PDB_CHANGED);
1288 /* Logon Hours Len */
1289 if (units_per_week/8 != pdb_get_hours_len(account)) {
1290 pdb_set_hours_len(account, units_per_week/8, PDB_CHANGED);
1294 if (logonHours.data) {
1295 char oldstr[44], newstr[44];
1296 pdb_sethexhours(oldstr, pdb_get_hours(account));
1297 pdb_sethexhours(newstr, logonHours.data);
1298 if (!strequal(oldstr, newstr)) {
1299 pdb_set_hours(account, logonHours.data,
1300 logonHours.length, PDB_CHANGED);
1304 if (pdb_get_bad_password_count(account) != badPwdCount)
1305 pdb_set_bad_password_count(account, badPwdCount, PDB_CHANGED);
1307 if (pdb_get_logon_count(account) != logonCount)
1308 pdb_set_logon_count(account, logonCount, PDB_CHANGED);
1310 if (!nt_time_is_zero(&pwdLastSet)) {
1311 unix_time = nt_time_to_unix(pwdLastSet);
1312 stored_time = pdb_get_pass_last_set_time(account);
1313 if (stored_time != unix_time)
1314 pdb_set_pass_last_set_time(account, unix_time, PDB_CHANGED);
1316 /* no last set time, make it now */
1317 pdb_set_pass_last_set_time(account, time(NULL), PDB_CHANGED);
1320 if (!nt_time_is_zero(&accountExpires)) {
1321 unix_time = nt_time_to_unix(accountExpires);
1322 stored_time = pdb_get_kickoff_time(account);
1323 if (stored_time != unix_time)
1324 pdb_set_kickoff_time(account, unix_time, PDB_CHANGED);
1327 /* Decode hashes from password hash
1328 Note that win2000 may send us all zeros for the hashes if it doesn't
1329 think this channel is secure enough - don't set the passwords at all
1332 if (dBCSPwd.length == 16 && memcmp(dBCSPwd.data, zero_buf, 16) != 0) {
1333 pdb_set_lanman_passwd(account, dBCSPwd.data, PDB_CHANGED);
1336 if (unicodePwd.length == 16 && memcmp(unicodePwd.data, zero_buf, 16) != 0) {
1337 pdb_set_nt_passwd(account, unicodePwd.data, PDB_CHANGED);
1342 /* TODO: account expiry time */
1344 pdb_set_acct_ctrl(account, acct_flags, PDB_CHANGED);
1346 pdb_set_domain(account, lp_workgroup(), PDB_CHANGED);
1348 DEBUG(0,("sam_account_from_object(%s, %s) done\n",
1349 sAMAccountName, sid_string_dbg(&objectSid)));
1350 return NT_STATUS_OK;
1353 /****************************************************************
1354 ****************************************************************/
1356 static NTSTATUS handle_account_object(struct dssync_passdb *pctx,
1357 TALLOC_CTX *mem_ctx,
1358 struct dssync_passdb_obj *obj)
1360 struct drsuapi_DsReplicaObjectListItemEx *cur = obj->cur;
1363 struct samu *sam_account=NULL;
1366 struct dom_sid user_sid;
1367 struct dom_sid group_sid;
1368 struct passwd *passwd = NULL;
1369 uint32_t acct_flags;
1372 const char *sAMAccountName;
1373 uint32_t userAccountControl;
1375 user_sid = cur->object.identifier->sid;
1376 GET_STRING_EX(sAMAccountName, true);
1377 GET_UINT32_EX(userAccountControl, true);
1379 status = dom_sid_split_rid(mem_ctx, &user_sid, NULL, &rid);
1380 if (!NT_STATUS_IS_OK(status)) {
1384 fstrcpy(account, sAMAccountName);
1385 if (rid == DOMAIN_RID_GUEST) {
1387 * pdb_getsampwsid() has special handling for DOMAIN_RID_GUEST
1388 * that's why we need to ignore it here.
1390 * pdb_smbpasswd.c also has some DOMAIN_RID_GUEST related
1393 DEBUG(0,("Ignore %s - %s\n", account, sid_string_dbg(&user_sid)));
1394 return NT_STATUS_OK;
1396 DEBUG(0,("Creating account: %s\n", account));
1398 if ( !(sam_account = samu_new(mem_ctx)) ) {
1399 return NT_STATUS_NO_MEMORY;
1402 acct_flags = ds_uf2acb(userAccountControl);
1403 status = smb_create_user(sam_account, acct_flags, account, &passwd);
1404 if (!NT_STATUS_IS_OK(status)) {
1405 DEBUG(0,("Could not create posix account info for '%s'- %s\n",
1406 account, nt_errstr(status)));
1407 TALLOC_FREE(sam_account);
1411 DEBUG(3, ("Attempting to find SID %s for user %s in the passdb\n",
1412 sid_string_dbg(&user_sid), account));
1413 if (!pdb_getsampwsid(sam_account, &user_sid)) {
1414 sam_account_from_object(sam_account, cur);
1415 DEBUG(3, ("Attempting to add user SID %s for user %s in the passdb\n",
1416 sid_string_dbg(&user_sid),
1417 pdb_get_username(sam_account)));
1418 if (!NT_STATUS_IS_OK(pdb_add_sam_account(sam_account))) {
1419 DEBUG(1, ("SAM Account for %s failed to be added to the passdb!\n",
1421 TALLOC_FREE(sam_account);
1422 return NT_STATUS_ACCESS_DENIED;
1425 sam_account_from_object(sam_account, cur);
1426 DEBUG(3, ("Attempting to update user SID %s for user %s in the passdb\n",
1427 sid_string_dbg(&user_sid),
1428 pdb_get_username(sam_account)));
1429 if (!NT_STATUS_IS_OK(pdb_update_sam_account(sam_account))) {
1430 DEBUG(1, ("SAM Account for %s failed to be updated in the passdb!\n",
1432 TALLOC_FREE(sam_account);
1433 return NT_STATUS_ACCESS_DENIED;
1437 if (pdb_get_group_sid(sam_account) == NULL) {
1438 TALLOC_FREE(sam_account);
1439 return NT_STATUS_UNSUCCESSFUL;
1442 group_sid = *pdb_get_group_sid(sam_account);
1444 map = talloc_zero(NULL, GROUP_MAP);
1446 return NT_STATUS_NO_MEMORY;
1449 if (!pdb_getgrsid(map, group_sid)) {
1450 DEBUG(0, ("Primary group of %s has no mapping!\n",
1451 pdb_get_username(sam_account)));
1453 if (map->gid != passwd->pw_gid) {
1454 if (!(grp = getgrgid(map->gid))) {
1455 DEBUG(0, ("Could not find unix group %lu for user %s (group SID=%s)\n",
1456 (unsigned long)map->gid, pdb_get_username(sam_account),
1457 sid_string_dbg(&group_sid)));
1459 smb_set_primary_group(grp->gr_name, pdb_get_username(sam_account));
1467 DEBUG(1, ("No unix user for this account (%s), cannot adjust mappings\n",
1468 pdb_get_username(sam_account)));
1471 TALLOC_FREE(sam_account);
1472 return NT_STATUS_OK;
1475 /****************************************************************
1476 ****************************************************************/
1478 static NTSTATUS handle_alias_object(struct dssync_passdb *pctx,
1479 TALLOC_CTX *mem_ctx,
1480 struct dssync_passdb_obj *obj)
1482 struct drsuapi_DsReplicaObjectListItemEx *cur = obj->cur;
1484 struct group *grp = NULL;
1485 struct dom_sid group_sid;
1487 struct dom_sid *dom_sid = NULL;
1492 const char *sAMAccountName;
1493 const char *description;
1495 uint32_t num_members = 0;
1496 struct drsuapi_DsReplicaObjectIdentifier3 *members = NULL;
1498 group_sid = cur->object.identifier->sid;
1499 GET_STRING_EX(sAMAccountName, true);
1500 GET_STRING(description);
1502 status = find_drsuapi_attr_dn(obj, cur, DRSUAPI_ATTID_member,
1503 &num_members, &members);
1504 if (NT_STATUS_EQUAL(status, NT_STATUS_PROPSET_NOT_FOUND)) {
1505 status = NT_STATUS_OK;
1507 if (!NT_STATUS_IS_OK(status)) {
1511 dom_sid_split_rid(mem_ctx, &group_sid, &dom_sid, &rid);
1513 map = talloc_zero(mem_ctx, GROUP_MAP);
1515 status = NT_STATUS_NO_MEMORY;
1519 map->nt_name = talloc_strdup(map, sAMAccountName);
1520 if (map->nt_name == NULL) {
1521 status = NT_STATUS_NO_MEMORY;
1526 map->comment = talloc_strdup(map, description);
1528 map->comment = talloc_strdup(map, "");
1530 if (map->comment == NULL) {
1531 status = NT_STATUS_NO_MEMORY;
1535 sid_to_fstring(sid_string, &group_sid);
1536 DEBUG(0,("Creating alias[%s] - %s members[%u]\n",
1537 map->nt_name, sid_string, num_members));
1539 status = dssync_insert_obj(pctx, pctx->aliases, obj);
1540 if (!NT_STATUS_IS_OK(status)) {
1544 if (pdb_getgrsid(map, group_sid)) {
1545 if (map->gid != -1) {
1546 grp = getgrgid(map->gid);
1554 /* No group found from mapping, find it from its name. */
1555 if ((grp = getgrnam(map->nt_name)) == NULL) {
1557 /* No appropriate group found, create one */
1559 DEBUG(0, ("Creating unix group: '%s'\n",
1562 if (smb_create_group(map->nt_name, &gid) != 0) {
1563 status = NT_STATUS_ACCESS_DENIED;
1567 if ((grp = getgrgid(gid)) == NULL) {
1568 status = NT_STATUS_ACCESS_DENIED;
1574 map->gid = grp->gr_gid;
1575 map->sid = group_sid;
1577 if (dom_sid_equal(dom_sid, &global_sid_Builtin)) {
1579 * pdb_ldap does not like SID_NAME_WKN_GRP...
1581 * map.sid_name_use = SID_NAME_WKN_GRP;
1583 map->sid_name_use = SID_NAME_ALIAS;
1585 map->sid_name_use = SID_NAME_ALIAS;
1589 pdb_add_group_mapping_entry(map);
1591 pdb_update_group_mapping_entry(map);
1594 for (i=0; i < num_members; i++) {
1595 struct dssync_passdb_mem *mem;
1597 status = dssync_create_mem(pctx, obj,
1600 if (!NT_STATUS_IS_OK(status)) {
1605 status = NT_STATUS_OK;
1612 /****************************************************************
1613 ****************************************************************/
1615 static NTSTATUS handle_group_object(struct dssync_passdb *pctx,
1616 TALLOC_CTX *mem_ctx,
1617 struct dssync_passdb_obj *obj)
1619 struct drsuapi_DsReplicaObjectListItemEx *cur = obj->cur;
1621 struct group *grp = NULL;
1622 struct dom_sid group_sid;
1627 const char *sAMAccountName;
1628 const char *description;
1630 uint32_t num_members = 0;
1631 struct drsuapi_DsReplicaObjectIdentifier3 *members = NULL;
1633 group_sid = cur->object.identifier->sid;
1634 GET_STRING_EX(sAMAccountName, true);
1635 GET_STRING(description);
1637 status = find_drsuapi_attr_dn(obj, cur, DRSUAPI_ATTID_member,
1638 &num_members, &members);
1639 if (NT_STATUS_EQUAL(status, NT_STATUS_PROPSET_NOT_FOUND)) {
1640 status = NT_STATUS_OK;
1642 if (!NT_STATUS_IS_OK(status)) {
1646 map = talloc_zero(NULL, GROUP_MAP);
1648 return NT_STATUS_NO_MEMORY;
1651 map->nt_name = talloc_strdup(map, sAMAccountName);
1652 if (!map->nt_name) {
1653 status = NT_STATUS_NO_MEMORY;
1657 map->comment = talloc_strdup(map, description);
1659 map->comment = talloc_strdup(map, "");
1661 if (!map->comment) {
1662 status = NT_STATUS_NO_MEMORY;
1666 sid_to_fstring(sid_string, &group_sid);
1667 DEBUG(0,("Creating group[%s] - %s members [%u]\n",
1668 map->nt_name, sid_string, num_members));
1670 status = dssync_insert_obj(pctx, pctx->groups, obj);
1671 if (!NT_STATUS_IS_OK(status)) {
1675 if (pdb_getgrsid(map, group_sid)) {
1676 if (map->gid != -1) {
1677 grp = getgrgid(map->gid);
1685 /* No group found from mapping, find it from its name. */
1686 if ((grp = getgrnam(map->nt_name)) == NULL) {
1688 /* No appropriate group found, create one */
1690 DEBUG(0, ("Creating unix group: '%s'\n",
1693 if (smb_create_group(map->nt_name, &gid) != 0) {
1694 status = NT_STATUS_ACCESS_DENIED;
1698 if ((grp = getgrnam(map->nt_name)) == NULL) {
1699 status = NT_STATUS_ACCESS_DENIED;
1705 map->gid = grp->gr_gid;
1706 map->sid = group_sid;
1707 map->sid_name_use = SID_NAME_DOM_GRP;
1710 pdb_add_group_mapping_entry(map);
1712 pdb_update_group_mapping_entry(map);
1715 for (i=0; i < num_members; i++) {
1716 struct dssync_passdb_mem *mem;
1718 status = dssync_create_mem(pctx, obj,
1721 if (!NT_STATUS_IS_OK(status)) {
1726 status = NT_STATUS_OK;
1733 /****************************************************************
1734 ****************************************************************/
1736 static NTSTATUS handle_interdomain_trust_object(struct dssync_passdb *pctx,
1737 TALLOC_CTX *mem_ctx,
1738 struct dssync_passdb_obj *obj)
1740 struct drsuapi_DsReplicaObjectListItemEx *cur = obj->cur;
1741 DEBUG(0,("trust: %s\n", cur->object.identifier->dn));
1742 return NT_STATUS_NOT_IMPLEMENTED;
1745 /****************************************************************
1746 ****************************************************************/
1748 struct dssync_object_table_t {
1750 NTSTATUS (*fn) (struct dssync_passdb *pctx,
1751 TALLOC_CTX *mem_ctx,
1752 struct dssync_passdb_obj *obj);
1755 static const struct dssync_object_table_t dssync_object_table[] = {
1756 { ATYPE_NORMAL_ACCOUNT, handle_account_object },
1757 { ATYPE_WORKSTATION_TRUST, handle_account_object },
1758 { ATYPE_SECURITY_LOCAL_GROUP, handle_alias_object },
1759 { ATYPE_SECURITY_GLOBAL_GROUP, handle_group_object },
1760 { ATYPE_INTERDOMAIN_TRUST, handle_interdomain_trust_object },
1763 /****************************************************************
1764 ****************************************************************/
1766 static NTSTATUS parse_object(struct dssync_passdb *pctx,
1767 TALLOC_CTX *mem_ctx,
1768 struct drsuapi_DsReplicaObjectListItemEx *cur)
1770 NTSTATUS status = NT_STATUS_OK;
1774 struct drsuapi_DsReplicaAttribute *attr;
1777 uint32_t sam_type = 0;
1779 DEBUG(3, ("parsing object '%s'\n", cur->object.identifier->dn));
1781 for (i=0; i < cur->object.attribute_ctr.num_attributes; i++) {
1783 attr = &cur->object.attribute_ctr.attributes[i];
1785 if (attr->value_ctr.num_values != 1) {
1789 if (!attr->value_ctr.values[0].blob) {
1793 blob = attr->value_ctr.values[0].blob;
1795 switch (attr->attid) {
1796 case DRSUAPI_ATTID_sAMAccountName:
1797 pull_string_talloc(mem_ctx, NULL, 0, &name,
1798 blob->data, blob->length,
1801 case DRSUAPI_ATTID_sAMAccountType:
1802 sam_type = IVAL(blob->data, 0);
1809 for (a=0; a < ARRAY_SIZE(dssync_object_table); a++) {
1810 if (sam_type == dssync_object_table[a].type) {
1811 if (dssync_object_table[a].fn) {
1812 struct dssync_passdb_obj *obj = NULL;
1813 status = dssync_create_obj(pctx, pctx->all,
1814 sam_type, cur, &obj);
1815 if (!NT_STATUS_IS_OK(status)) {
1818 status = dssync_object_table[a].fn(pctx,
1829 static NTSTATUS parse_link(struct dssync_passdb *pctx,
1830 TALLOC_CTX *mem_ctx,
1831 struct drsuapi_DsReplicaLinkedAttribute *cur)
1833 struct drsuapi_DsReplicaObjectIdentifier3 *id3;
1834 const DATA_BLOB *blob;
1835 enum ndr_err_code ndr_err;
1837 bool active = false;
1838 struct dssync_passdb_mem *mem;
1839 struct dssync_passdb_obj *obj;
1841 if (cur->attid != DRSUAPI_ATTID_member) {
1842 return NT_STATUS_OK;
1845 if (cur->flags & DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE) {
1849 DEBUG(3, ("parsing link '%s' - %s\n",
1850 cur->identifier->dn, active?"adding":"deleting"));
1852 blob = cur->value.blob;
1855 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1858 obj = dssync_search_obj_by_guid(pctx, pctx->all, &cur->identifier->guid);
1860 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1863 id3 = talloc_zero(obj, struct drsuapi_DsReplicaObjectIdentifier3);
1865 return NT_STATUS_NO_MEMORY;
1868 /* windows sometimes sends an extra two pad bytes here */
1869 ndr_err = ndr_pull_struct_blob(blob, id3, id3,
1870 (ndr_pull_flags_fn_t)ndr_pull_drsuapi_DsReplicaObjectIdentifier3);
1871 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1872 status = ndr_map_error2ntstatus(ndr_err);
1876 status = dssync_create_mem(pctx, obj,
1879 if (!NT_STATUS_IS_OK(status)) {
1883 return NT_STATUS_OK;
1886 /****************************************************************
1887 ****************************************************************/
1889 static NTSTATUS passdb_process_objects(struct dssync_context *ctx,
1890 TALLOC_CTX *mem_ctx,
1891 struct drsuapi_DsReplicaObjectListItemEx *cur,
1892 struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr)
1894 NTSTATUS status = NT_STATUS_OK;
1895 struct dssync_passdb *pctx =
1896 talloc_get_type_abort(ctx->private_data,
1897 struct dssync_passdb);
1899 for (; cur; cur = cur->next_object) {
1900 status = parse_object(pctx, mem_ctx, cur);
1901 if (!NT_STATUS_IS_OK(status)) {
1910 /****************************************************************
1911 ****************************************************************/
1913 static NTSTATUS passdb_process_links(struct dssync_context *ctx,
1914 TALLOC_CTX *mem_ctx,
1916 struct drsuapi_DsReplicaLinkedAttribute *links,
1917 struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr)
1919 NTSTATUS status = NT_STATUS_OK;
1920 struct dssync_passdb *pctx =
1921 talloc_get_type_abort(ctx->private_data,
1922 struct dssync_passdb);
1925 for (i = 0; i < count; i++) {
1926 status = parse_link(pctx, mem_ctx, &links[i]);
1927 if (!NT_STATUS_IS_OK(status)) {
1936 /****************************************************************
1937 ****************************************************************/
1939 const struct dssync_ops libnet_dssync_passdb_ops = {
1940 .startup = passdb_startup,
1941 .process_objects = passdb_process_objects,
1942 .process_links = passdb_process_links,
1943 .finish = passdb_finish,