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
, 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
;
1113 NTTIME accountExpires
;
1114 const char *sAMAccountName
;
1115 const char *displayName
;
1116 const char *homeDirectory
;
1117 const char *homeDrive
;
1118 const char *scriptPath
;
1119 const char *profilePath
;
1120 const char *description
;
1121 const char *userWorkstations
;
1122 DATA_BLOB userParameters
;
1123 struct dom_sid objectSid
;
1124 uint32_t primaryGroupID
;
1125 uint32_t userAccountControl
;
1126 DATA_BLOB logonHours
;
1127 uint32_t badPwdCount
;
1128 uint32_t logonCount
;
1129 DATA_BLOB unicodePwd
;
1133 uint32_t acct_flags
;
1134 uint32_t units_per_week
;
1136 objectSid
= cur
->object
.identifier
->sid
;
1137 GET_STRING_EX(sAMAccountName
, true);
1138 DEBUG(0,("sam_account_from_object(%s, %s) start\n",
1139 sAMAccountName
, sid_string_dbg(&objectSid
)));
1140 GET_UINT64(lastLogon
);
1141 GET_UINT64(lastLogoff
);
1142 GET_UINT64(pwdLastSet
);
1143 GET_UINT64(accountExpires
);
1144 GET_STRING(displayName
);
1145 GET_STRING(homeDirectory
);
1146 GET_STRING(homeDrive
);
1147 GET_STRING(scriptPath
);
1148 GET_STRING(profilePath
);
1149 GET_STRING(description
);
1150 GET_STRING(userWorkstations
);
1151 GET_BLOB(userParameters
);
1152 GET_UINT32(primaryGroupID
);
1153 GET_UINT32(userAccountControl
);
1154 GET_BLOB(logonHours
);
1155 GET_UINT32(badPwdCount
);
1156 GET_UINT32(logonCount
);
1157 GET_BLOB(unicodePwd
);
1160 status
= dom_sid_split_rid(mem_ctx
, &objectSid
, NULL
, &rid
);
1161 if (!NT_STATUS_IS_OK(status
)) {
1164 acct_flags
= ds_uf2acb(userAccountControl
);
1166 /* Username, fullname, home dir, dir drive, logon script, acct
1167 desc, workstations, profile. */
1169 if (sAMAccountName
) {
1170 old_string
= pdb_get_nt_username(account
);
1171 new_string
= sAMAccountName
;
1173 if (STRING_CHANGED
) {
1174 pdb_set_nt_username(account
, new_string
, PDB_CHANGED
);
1177 /* Unix username is the same - for sanity */
1178 old_string
= pdb_get_username( account
);
1179 if (STRING_CHANGED
) {
1180 pdb_set_username(account
, new_string
, PDB_CHANGED
);
1185 old_string
= pdb_get_fullname(account
);
1186 new_string
= displayName
;
1189 pdb_set_fullname(account
, new_string
, PDB_CHANGED
);
1192 if (homeDirectory
) {
1193 old_string
= pdb_get_homedir(account
);
1194 new_string
= homeDirectory
;
1197 pdb_set_homedir(account
, new_string
, PDB_CHANGED
);
1201 old_string
= pdb_get_dir_drive(account
);
1202 new_string
= homeDrive
;
1205 pdb_set_dir_drive(account
, new_string
, PDB_CHANGED
);
1209 old_string
= pdb_get_logon_script(account
);
1210 new_string
= scriptPath
;
1213 pdb_set_logon_script(account
, new_string
, PDB_CHANGED
);
1217 old_string
= pdb_get_acct_desc(account
);
1218 new_string
= description
;
1221 pdb_set_acct_desc(account
, new_string
, PDB_CHANGED
);
1224 if (userWorkstations
) {
1225 old_string
= pdb_get_workstations(account
);
1226 new_string
= userWorkstations
;
1229 pdb_set_workstations(account
, new_string
, PDB_CHANGED
);
1233 old_string
= pdb_get_profile_path(account
);
1234 new_string
= profilePath
;
1237 pdb_set_profile_path(account
, new_string
, PDB_CHANGED
);
1240 if (userParameters
.data
) {
1241 char *newstr
= NULL
;
1242 old_string
= pdb_get_munged_dial(account
);
1244 if (userParameters
.length
!= 0) {
1245 newstr
= base64_encode_data_blob(talloc_tos(),
1247 SMB_ASSERT(newstr
!= NULL
);
1250 if (STRING_CHANGED_NC(old_string
, newstr
))
1251 pdb_set_munged_dial(account
, newstr
, PDB_CHANGED
);
1252 TALLOC_FREE(newstr
);
1255 /* User and group sid */
1256 if (rid
!= 0 && pdb_get_user_rid(account
) != rid
) {
1257 pdb_set_user_sid_from_rid(account
, rid
, PDB_CHANGED
);
1259 if (primaryGroupID
!= 0 && pdb_get_group_rid(account
) != primaryGroupID
) {
1260 pdb_set_group_sid_from_rid(account
, primaryGroupID
, PDB_CHANGED
);
1263 /* Logon and password information */
1264 if (!nt_time_is_zero(&lastLogon
)) {
1265 unix_time
= nt_time_to_unix(lastLogon
);
1266 stored_time
= pdb_get_logon_time(account
);
1267 if (stored_time
!= unix_time
)
1268 pdb_set_logon_time(account
, unix_time
, PDB_CHANGED
);
1271 if (!nt_time_is_zero(&lastLogoff
)) {
1272 unix_time
= nt_time_to_unix(lastLogoff
);
1273 stored_time
= pdb_get_logoff_time(account
);
1274 if (stored_time
!= unix_time
)
1275 pdb_set_logoff_time(account
, unix_time
,PDB_CHANGED
);
1279 units_per_week
= logonHours
.length
* 8;
1281 if (pdb_get_logon_divs(account
) != units_per_week
) {
1282 pdb_set_logon_divs(account
, units_per_week
, PDB_CHANGED
);
1285 /* Logon Hours Len */
1286 if (units_per_week
/8 != pdb_get_hours_len(account
)) {
1287 pdb_set_hours_len(account
, units_per_week
/8, PDB_CHANGED
);
1291 if (logonHours
.data
) {
1292 char oldstr
[44], newstr
[44];
1293 pdb_sethexhours(oldstr
, pdb_get_hours(account
));
1294 pdb_sethexhours(newstr
, logonHours
.data
);
1295 if (!strequal(oldstr
, newstr
)) {
1296 pdb_set_hours(account
, logonHours
.data
,
1297 logonHours
.length
, PDB_CHANGED
);
1301 if (pdb_get_bad_password_count(account
) != badPwdCount
)
1302 pdb_set_bad_password_count(account
, badPwdCount
, PDB_CHANGED
);
1304 if (pdb_get_logon_count(account
) != logonCount
)
1305 pdb_set_logon_count(account
, logonCount
, PDB_CHANGED
);
1307 if (!nt_time_is_zero(&pwdLastSet
)) {
1308 unix_time
= nt_time_to_unix(pwdLastSet
);
1309 stored_time
= pdb_get_pass_last_set_time(account
);
1310 if (stored_time
!= unix_time
)
1311 pdb_set_pass_last_set_time(account
, unix_time
, PDB_CHANGED
);
1313 /* no last set time, make it now */
1314 pdb_set_pass_last_set_time(account
, time(NULL
), PDB_CHANGED
);
1317 if (!nt_time_is_zero(&accountExpires
)) {
1318 unix_time
= nt_time_to_unix(accountExpires
);
1319 stored_time
= pdb_get_kickoff_time(account
);
1320 if (stored_time
!= unix_time
)
1321 pdb_set_kickoff_time(account
, unix_time
, PDB_CHANGED
);
1324 /* Decode hashes from password hash
1325 Note that win2000 may send us all zeros for the hashes if it doesn't
1326 think this channel is secure enough - don't set the passwords at all
1329 if (dBCSPwd
.length
== 16 && !all_zero(dBCSPwd
.data
, 16)) {
1330 pdb_set_lanman_passwd(account
, dBCSPwd
.data
, PDB_CHANGED
);
1333 if (unicodePwd
.length
== 16 && !all_zero(unicodePwd
.data
, 16)) {
1334 pdb_set_nt_passwd(account
, unicodePwd
.data
, PDB_CHANGED
);
1339 /* TODO: account expiry time */
1341 pdb_set_acct_ctrl(account
, acct_flags
, PDB_CHANGED
);
1343 pdb_set_domain(account
, lp_workgroup(), PDB_CHANGED
);
1345 DEBUG(0,("sam_account_from_object(%s, %s) done\n",
1346 sAMAccountName
, sid_string_dbg(&objectSid
)));
1347 return NT_STATUS_OK
;
1350 /****************************************************************
1351 ****************************************************************/
1353 static NTSTATUS
handle_account_object(struct dssync_passdb
*pctx
,
1354 TALLOC_CTX
*mem_ctx
,
1355 struct dssync_passdb_obj
*obj
)
1357 struct drsuapi_DsReplicaObjectListItemEx
*cur
= obj
->cur
;
1360 struct samu
*sam_account
=NULL
;
1363 struct dom_sid user_sid
;
1364 struct dom_sid group_sid
;
1365 struct passwd
*passwd
= NULL
;
1366 uint32_t acct_flags
;
1369 const char *sAMAccountName
;
1370 uint32_t userAccountControl
;
1372 user_sid
= cur
->object
.identifier
->sid
;
1373 GET_STRING_EX(sAMAccountName
, true);
1374 GET_UINT32_EX(userAccountControl
, true);
1376 status
= dom_sid_split_rid(mem_ctx
, &user_sid
, NULL
, &rid
);
1377 if (!NT_STATUS_IS_OK(status
)) {
1381 fstrcpy(account
, sAMAccountName
);
1382 if (rid
== DOMAIN_RID_GUEST
) {
1384 * pdb_getsampwsid() has special handling for DOMAIN_RID_GUEST
1385 * that's why we need to ignore it here.
1387 * pdb_smbpasswd.c also has some DOMAIN_RID_GUEST related
1390 DEBUG(0,("Ignore %s - %s\n", account
, sid_string_dbg(&user_sid
)));
1391 return NT_STATUS_OK
;
1393 DEBUG(0,("Creating account: %s\n", account
));
1395 if ( !(sam_account
= samu_new(mem_ctx
)) ) {
1396 return NT_STATUS_NO_MEMORY
;
1399 acct_flags
= ds_uf2acb(userAccountControl
);
1400 status
= smb_create_user(sam_account
, acct_flags
, account
, &passwd
);
1401 if (!NT_STATUS_IS_OK(status
)) {
1402 DEBUG(0,("Could not create posix account info for '%s'- %s\n",
1403 account
, nt_errstr(status
)));
1404 TALLOC_FREE(sam_account
);
1408 DEBUG(3, ("Attempting to find SID %s for user %s in the passdb\n",
1409 sid_string_dbg(&user_sid
), account
));
1410 if (!pdb_getsampwsid(sam_account
, &user_sid
)) {
1411 sam_account_from_object(sam_account
, cur
);
1412 DEBUG(3, ("Attempting to add user SID %s for user %s in the passdb\n",
1413 sid_string_dbg(&user_sid
),
1414 pdb_get_username(sam_account
)));
1415 if (!NT_STATUS_IS_OK(pdb_add_sam_account(sam_account
))) {
1416 DEBUG(1, ("SAM Account for %s failed to be added to the passdb!\n",
1418 TALLOC_FREE(sam_account
);
1419 return NT_STATUS_ACCESS_DENIED
;
1422 sam_account_from_object(sam_account
, cur
);
1423 DEBUG(3, ("Attempting to update user SID %s for user %s in the passdb\n",
1424 sid_string_dbg(&user_sid
),
1425 pdb_get_username(sam_account
)));
1426 if (!NT_STATUS_IS_OK(pdb_update_sam_account(sam_account
))) {
1427 DEBUG(1, ("SAM Account for %s failed to be updated in the passdb!\n",
1429 TALLOC_FREE(sam_account
);
1430 return NT_STATUS_ACCESS_DENIED
;
1434 if (pdb_get_group_sid(sam_account
) == NULL
) {
1435 TALLOC_FREE(sam_account
);
1436 return NT_STATUS_UNSUCCESSFUL
;
1439 group_sid
= *pdb_get_group_sid(sam_account
);
1441 map
= talloc_zero(NULL
, GROUP_MAP
);
1443 return NT_STATUS_NO_MEMORY
;
1446 if (!pdb_getgrsid(map
, group_sid
)) {
1447 DEBUG(0, ("Primary group of %s has no mapping!\n",
1448 pdb_get_username(sam_account
)));
1450 if (map
->gid
!= passwd
->pw_gid
) {
1451 if (!(grp
= getgrgid(map
->gid
))) {
1452 DEBUG(0, ("Could not find unix group %lu for user %s (group SID=%s)\n",
1453 (unsigned long)map
->gid
, pdb_get_username(sam_account
),
1454 sid_string_dbg(&group_sid
)));
1456 smb_set_primary_group(grp
->gr_name
, pdb_get_username(sam_account
));
1464 DEBUG(1, ("No unix user for this account (%s), cannot adjust mappings\n",
1465 pdb_get_username(sam_account
)));
1468 TALLOC_FREE(sam_account
);
1469 return NT_STATUS_OK
;
1472 /****************************************************************
1473 ****************************************************************/
1475 static NTSTATUS
handle_alias_object(struct dssync_passdb
*pctx
,
1476 TALLOC_CTX
*mem_ctx
,
1477 struct dssync_passdb_obj
*obj
)
1479 struct drsuapi_DsReplicaObjectListItemEx
*cur
= obj
->cur
;
1481 struct group
*grp
= NULL
;
1482 struct dom_sid group_sid
;
1484 struct dom_sid
*dom_sid
= NULL
;
1489 const char *sAMAccountName
;
1490 const char *description
;
1492 uint32_t num_members
= 0;
1493 struct drsuapi_DsReplicaObjectIdentifier3
*members
= NULL
;
1495 group_sid
= cur
->object
.identifier
->sid
;
1496 GET_STRING_EX(sAMAccountName
, true);
1497 GET_STRING(description
);
1499 status
= find_drsuapi_attr_dn(obj
, cur
, DRSUAPI_ATTID_member
,
1500 &num_members
, &members
);
1501 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROPSET_NOT_FOUND
)) {
1502 status
= NT_STATUS_OK
;
1504 if (!NT_STATUS_IS_OK(status
)) {
1508 dom_sid_split_rid(mem_ctx
, &group_sid
, &dom_sid
, &rid
);
1510 map
= talloc_zero(mem_ctx
, GROUP_MAP
);
1512 status
= NT_STATUS_NO_MEMORY
;
1516 map
->nt_name
= talloc_strdup(map
, sAMAccountName
);
1517 if (map
->nt_name
== NULL
) {
1518 status
= NT_STATUS_NO_MEMORY
;
1523 map
->comment
= talloc_strdup(map
, description
);
1525 map
->comment
= talloc_strdup(map
, "");
1527 if (map
->comment
== NULL
) {
1528 status
= NT_STATUS_NO_MEMORY
;
1532 sid_to_fstring(sid_string
, &group_sid
);
1533 DEBUG(0,("Creating alias[%s] - %s members[%u]\n",
1534 map
->nt_name
, sid_string
, num_members
));
1536 status
= dssync_insert_obj(pctx
, pctx
->aliases
, obj
);
1537 if (!NT_STATUS_IS_OK(status
)) {
1541 if (pdb_getgrsid(map
, group_sid
)) {
1542 if (map
->gid
!= -1) {
1543 grp
= getgrgid(map
->gid
);
1551 /* No group found from mapping, find it from its name. */
1552 if ((grp
= getgrnam(map
->nt_name
)) == NULL
) {
1554 /* No appropriate group found, create one */
1556 DEBUG(0, ("Creating unix group: '%s'\n",
1559 if (smb_create_group(map
->nt_name
, &gid
) != 0) {
1560 status
= NT_STATUS_ACCESS_DENIED
;
1564 if ((grp
= getgrgid(gid
)) == NULL
) {
1565 status
= NT_STATUS_ACCESS_DENIED
;
1571 map
->gid
= grp
->gr_gid
;
1572 map
->sid
= group_sid
;
1574 if (dom_sid_equal(dom_sid
, &global_sid_Builtin
)) {
1576 * pdb_ldap does not like SID_NAME_WKN_GRP...
1578 * map.sid_name_use = SID_NAME_WKN_GRP;
1580 map
->sid_name_use
= SID_NAME_ALIAS
;
1582 map
->sid_name_use
= SID_NAME_ALIAS
;
1586 pdb_add_group_mapping_entry(map
);
1588 pdb_update_group_mapping_entry(map
);
1591 for (i
=0; i
< num_members
; i
++) {
1592 struct dssync_passdb_mem
*mem
;
1594 status
= dssync_create_mem(pctx
, obj
,
1597 if (!NT_STATUS_IS_OK(status
)) {
1602 status
= NT_STATUS_OK
;
1609 /****************************************************************
1610 ****************************************************************/
1612 static NTSTATUS
handle_group_object(struct dssync_passdb
*pctx
,
1613 TALLOC_CTX
*mem_ctx
,
1614 struct dssync_passdb_obj
*obj
)
1616 struct drsuapi_DsReplicaObjectListItemEx
*cur
= obj
->cur
;
1618 struct group
*grp
= NULL
;
1619 struct dom_sid group_sid
;
1624 const char *sAMAccountName
;
1625 const char *description
;
1627 uint32_t num_members
= 0;
1628 struct drsuapi_DsReplicaObjectIdentifier3
*members
= NULL
;
1630 group_sid
= cur
->object
.identifier
->sid
;
1631 GET_STRING_EX(sAMAccountName
, true);
1632 GET_STRING(description
);
1634 status
= find_drsuapi_attr_dn(obj
, cur
, DRSUAPI_ATTID_member
,
1635 &num_members
, &members
);
1636 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROPSET_NOT_FOUND
)) {
1637 status
= NT_STATUS_OK
;
1639 if (!NT_STATUS_IS_OK(status
)) {
1643 map
= talloc_zero(NULL
, GROUP_MAP
);
1645 return NT_STATUS_NO_MEMORY
;
1648 map
->nt_name
= talloc_strdup(map
, sAMAccountName
);
1649 if (!map
->nt_name
) {
1650 status
= NT_STATUS_NO_MEMORY
;
1654 map
->comment
= talloc_strdup(map
, description
);
1656 map
->comment
= talloc_strdup(map
, "");
1658 if (!map
->comment
) {
1659 status
= NT_STATUS_NO_MEMORY
;
1663 sid_to_fstring(sid_string
, &group_sid
);
1664 DEBUG(0,("Creating group[%s] - %s members [%u]\n",
1665 map
->nt_name
, sid_string
, num_members
));
1667 status
= dssync_insert_obj(pctx
, pctx
->groups
, obj
);
1668 if (!NT_STATUS_IS_OK(status
)) {
1672 if (pdb_getgrsid(map
, group_sid
)) {
1673 if (map
->gid
!= -1) {
1674 grp
= getgrgid(map
->gid
);
1682 /* No group found from mapping, find it from its name. */
1683 if ((grp
= getgrnam(map
->nt_name
)) == NULL
) {
1685 /* No appropriate group found, create one */
1687 DEBUG(0, ("Creating unix group: '%s'\n",
1690 if (smb_create_group(map
->nt_name
, &gid
) != 0) {
1691 status
= NT_STATUS_ACCESS_DENIED
;
1695 if ((grp
= getgrnam(map
->nt_name
)) == NULL
) {
1696 status
= NT_STATUS_ACCESS_DENIED
;
1702 map
->gid
= grp
->gr_gid
;
1703 map
->sid
= group_sid
;
1704 map
->sid_name_use
= SID_NAME_DOM_GRP
;
1707 pdb_add_group_mapping_entry(map
);
1709 pdb_update_group_mapping_entry(map
);
1712 for (i
=0; i
< num_members
; i
++) {
1713 struct dssync_passdb_mem
*mem
;
1715 status
= dssync_create_mem(pctx
, obj
,
1718 if (!NT_STATUS_IS_OK(status
)) {
1723 status
= NT_STATUS_OK
;
1730 /****************************************************************
1731 ****************************************************************/
1733 static NTSTATUS
handle_interdomain_trust_object(struct dssync_passdb
*pctx
,
1734 TALLOC_CTX
*mem_ctx
,
1735 struct dssync_passdb_obj
*obj
)
1737 struct drsuapi_DsReplicaObjectListItemEx
*cur
= obj
->cur
;
1738 DEBUG(0,("trust: %s\n", cur
->object
.identifier
->dn
));
1739 return NT_STATUS_NOT_IMPLEMENTED
;
1742 /****************************************************************
1743 ****************************************************************/
1745 struct dssync_object_table_t
{
1747 NTSTATUS (*fn
) (struct dssync_passdb
*pctx
,
1748 TALLOC_CTX
*mem_ctx
,
1749 struct dssync_passdb_obj
*obj
);
1752 static const struct dssync_object_table_t dssync_object_table
[] = {
1753 { ATYPE_NORMAL_ACCOUNT
, handle_account_object
},
1754 { ATYPE_WORKSTATION_TRUST
, handle_account_object
},
1755 { ATYPE_SECURITY_LOCAL_GROUP
, handle_alias_object
},
1756 { ATYPE_SECURITY_GLOBAL_GROUP
, handle_group_object
},
1757 { ATYPE_INTERDOMAIN_TRUST
, handle_interdomain_trust_object
},
1760 /****************************************************************
1761 ****************************************************************/
1763 static NTSTATUS
parse_object(struct dssync_passdb
*pctx
,
1764 TALLOC_CTX
*mem_ctx
,
1765 struct drsuapi_DsReplicaObjectListItemEx
*cur
)
1767 NTSTATUS status
= NT_STATUS_OK
;
1771 struct drsuapi_DsReplicaAttribute
*attr
;
1774 uint32_t sam_type
= 0;
1776 DEBUG(3, ("parsing object '%s'\n", cur
->object
.identifier
->dn
));
1778 for (i
=0; i
< cur
->object
.attribute_ctr
.num_attributes
; i
++) {
1780 attr
= &cur
->object
.attribute_ctr
.attributes
[i
];
1782 if (attr
->value_ctr
.num_values
!= 1) {
1786 if (!attr
->value_ctr
.values
[0].blob
) {
1790 blob
= attr
->value_ctr
.values
[0].blob
;
1792 switch (attr
->attid
) {
1793 case DRSUAPI_ATTID_sAMAccountName
:
1794 pull_string_talloc(mem_ctx
, NULL
, 0, &name
,
1795 blob
->data
, blob
->length
,
1798 case DRSUAPI_ATTID_sAMAccountType
:
1799 sam_type
= IVAL(blob
->data
, 0);
1806 for (a
=0; a
< ARRAY_SIZE(dssync_object_table
); a
++) {
1807 if (sam_type
== dssync_object_table
[a
].type
) {
1808 if (dssync_object_table
[a
].fn
) {
1809 struct dssync_passdb_obj
*obj
= NULL
;
1810 status
= dssync_create_obj(pctx
, pctx
->all
,
1811 sam_type
, cur
, &obj
);
1812 if (!NT_STATUS_IS_OK(status
)) {
1815 status
= dssync_object_table
[a
].fn(pctx
,
1826 static NTSTATUS
parse_link(struct dssync_passdb
*pctx
,
1827 TALLOC_CTX
*mem_ctx
,
1828 struct drsuapi_DsReplicaLinkedAttribute
*cur
)
1830 struct drsuapi_DsReplicaObjectIdentifier3
*id3
;
1831 const DATA_BLOB
*blob
;
1832 enum ndr_err_code ndr_err
;
1834 bool active
= false;
1835 struct dssync_passdb_mem
*mem
;
1836 struct dssync_passdb_obj
*obj
;
1838 if (cur
->attid
!= DRSUAPI_ATTID_member
) {
1839 return NT_STATUS_OK
;
1842 if (cur
->flags
& DRSUAPI_DS_LINKED_ATTRIBUTE_FLAG_ACTIVE
) {
1846 DEBUG(3, ("parsing link '%s' - %s\n",
1847 cur
->identifier
->dn
, active
?"adding":"deleting"));
1849 blob
= cur
->value
.blob
;
1852 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
1855 obj
= dssync_search_obj_by_guid(pctx
, pctx
->all
, &cur
->identifier
->guid
);
1857 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
1860 id3
= talloc_zero(obj
, struct drsuapi_DsReplicaObjectIdentifier3
);
1862 return NT_STATUS_NO_MEMORY
;
1865 /* windows sometimes sends an extra two pad bytes here */
1866 ndr_err
= ndr_pull_struct_blob(blob
, id3
, id3
,
1867 (ndr_pull_flags_fn_t
)ndr_pull_drsuapi_DsReplicaObjectIdentifier3
);
1868 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1869 status
= ndr_map_error2ntstatus(ndr_err
);
1873 status
= dssync_create_mem(pctx
, obj
,
1876 if (!NT_STATUS_IS_OK(status
)) {
1880 return NT_STATUS_OK
;
1883 /****************************************************************
1884 ****************************************************************/
1886 static NTSTATUS
passdb_process_objects(struct dssync_context
*ctx
,
1887 TALLOC_CTX
*mem_ctx
,
1888 struct drsuapi_DsReplicaObjectListItemEx
*cur
,
1889 struct drsuapi_DsReplicaOIDMapping_Ctr
*mapping_ctr
)
1891 NTSTATUS status
= NT_STATUS_OK
;
1892 struct dssync_passdb
*pctx
=
1893 talloc_get_type_abort(ctx
->private_data
,
1894 struct dssync_passdb
);
1896 for (; cur
; cur
= cur
->next_object
) {
1897 status
= parse_object(pctx
, mem_ctx
, cur
);
1898 if (!NT_STATUS_IS_OK(status
)) {
1907 /****************************************************************
1908 ****************************************************************/
1910 static NTSTATUS
passdb_process_links(struct dssync_context
*ctx
,
1911 TALLOC_CTX
*mem_ctx
,
1913 struct drsuapi_DsReplicaLinkedAttribute
*links
,
1914 struct drsuapi_DsReplicaOIDMapping_Ctr
*mapping_ctr
)
1916 NTSTATUS status
= NT_STATUS_OK
;
1917 struct dssync_passdb
*pctx
=
1918 talloc_get_type_abort(ctx
->private_data
,
1919 struct dssync_passdb
);
1922 for (i
= 0; i
< count
; i
++) {
1923 status
= parse_link(pctx
, mem_ctx
, &links
[i
]);
1924 if (!NT_STATUS_IS_OK(status
)) {
1933 /****************************************************************
1934 ****************************************************************/
1936 const struct dssync_ops libnet_dssync_passdb_ops
= {
1937 .startup
= passdb_startup
,
1938 .process_objects
= passdb_process_objects
,
1939 .process_links
= passdb_process_links
,
1940 .finish
= passdb_finish
,