2 Unix SMB/CIFS implementation.
4 Extract the user/system database from a remote SamSync server
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
7 Copyright (C) Andrew Tridgell 2004
8 Copyright (C) Volker Lendecke 2004
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "libnet/libnet.h"
27 #include "libcli/ldap/ldap_ndr.h"
28 #include "dsdb/samdb/samdb.h"
29 #include "auth/auth.h"
30 #include "../lib/util/util_ldb.h"
31 #include "librpc/gen_ndr/ndr_misc.h"
33 #include "libcli/security/security.h"
34 #include "librpc/rpc/dcerpc.h"
35 #include "param/param.h"
37 struct samsync_ldb_secret
{
38 struct samsync_ldb_secret
*prev
, *next
;
44 struct samsync_ldb_trusted_domain
{
45 struct samsync_ldb_trusted_domain
*prev
, *next
;
50 struct samsync_ldb_state
{
51 /* Values from the LSA lookup */
52 const struct libnet_SamSync_state
*samsync_state
;
54 struct dom_sid
*dom_sid
[3];
55 struct ldb_context
*sam_ldb
, *remote_ldb
, *pdb
;
56 struct ldb_dn
*base_dn
[3];
57 struct samsync_ldb_secret
*secrets
;
58 struct samsync_ldb_trusted_domain
*trusted_domains
;
61 static NTSTATUS
samsync_ldb_add_foreignSecurityPrincipal(TALLOC_CTX
*mem_ctx
,
62 struct samsync_ldb_state
*state
,
64 struct ldb_dn
**fsp_dn
,
67 const char *sidstr
= dom_sid_string(mem_ctx
, sid
);
68 /* We assume that ForeignSecurityPrincipals are under the BASEDN of the main domain */
69 struct ldb_dn
*basedn
= samdb_search_dn(state
->sam_ldb
, mem_ctx
,
70 state
->base_dn
[SAM_DATABASE_DOMAIN
],
71 "(&(objectClass=container)(cn=ForeignSecurityPrincipals))");
72 struct ldb_message
*msg
;
76 return NT_STATUS_NO_MEMORY
;
80 *error_string
= talloc_asprintf(mem_ctx
,
81 "Failed to find DN for "
82 "ForeignSecurityPrincipal container under %s",
83 ldb_dn_get_linearized(state
->base_dn
[SAM_DATABASE_DOMAIN
]));
84 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
87 msg
= ldb_msg_new(mem_ctx
);
89 return NT_STATUS_NO_MEMORY
;
92 /* add core elements to the ldb_message for the alias */
94 if ( ! ldb_dn_add_child_fmt(msg
->dn
, "CN=%s", sidstr
))
95 return NT_STATUS_UNSUCCESSFUL
;
97 samdb_msg_add_string(state
->sam_ldb
, mem_ctx
, msg
,
99 "foreignSecurityPrincipal");
103 /* create the alias */
104 ret
= ldb_add(state
->sam_ldb
, msg
);
106 *error_string
= talloc_asprintf(mem_ctx
, "Failed to create foreignSecurityPrincipal "
108 ldb_dn_get_linearized(msg
->dn
),
109 ldb_errstring(state
->sam_ldb
));
110 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
115 static NTSTATUS
samsync_ldb_handle_domain(TALLOC_CTX
*mem_ctx
,
116 struct samsync_ldb_state
*state
,
117 enum netr_SamDatabaseID database
,
118 struct netr_DELTA_ENUM
*delta
,
121 struct netr_DELTA_DOMAIN
*domain
= delta
->delta_union
.domain
;
122 const char *domain_name
= domain
->domain_name
.string
;
123 struct ldb_message
*msg
;
126 msg
= ldb_msg_new(mem_ctx
);
128 return NT_STATUS_NO_MEMORY
;
131 if (database
== SAM_DATABASE_DOMAIN
) {
132 struct ldb_dn
*partitions_basedn
;
133 const char *domain_attrs
[] = {"nETBIOSName", "nCName", NULL
};
134 struct ldb_message
**msgs_domain
;
137 partitions_basedn
= samdb_partitions_dn(state
->sam_ldb
, mem_ctx
);
139 ret_domain
= gendb_search(state
->sam_ldb
, mem_ctx
, partitions_basedn
, &msgs_domain
, domain_attrs
,
140 "(&(&(nETBIOSName=%s)(objectclass=crossRef))(ncName=*))",
142 if (ret_domain
== -1) {
143 *error_string
= talloc_asprintf(mem_ctx
, "gendb_search for domain failed: %s", ldb_errstring(state
->sam_ldb
));
144 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
147 if (ret_domain
!= 1) {
148 *error_string
= talloc_asprintf(mem_ctx
, "Failed to find existing domain record for %s: %d results", domain_name
,
150 return NT_STATUS_NO_SUCH_DOMAIN
;
153 state
->base_dn
[database
] = samdb_result_dn(state
->sam_ldb
, state
, msgs_domain
[0], "nCName", NULL
);
155 if (state
->dom_sid
[database
]) {
156 /* Update the domain sid with the incoming
157 * domain (found on LSA pipe, database sid may
159 samdb_msg_add_dom_sid(state
->sam_ldb
, mem_ctx
,
160 msg
, "objectSid", state
->dom_sid
[database
]);
162 /* Well, we will have to use the one from the database */
163 state
->dom_sid
[database
] = samdb_search_dom_sid(state
->sam_ldb
, state
,
164 state
->base_dn
[database
],
168 if (state
->samsync_state
->domain_guid
) {
171 status
= GUID_to_ndr_blob(state
->samsync_state
->domain_guid
, msg
, &v
);
172 if (!NT_STATUS_IS_OK(status
)) {
173 *error_string
= talloc_asprintf(mem_ctx
, "ndr_push of domain GUID failed!");
177 ldb_msg_add_value(msg
, "objectGUID", &v
, NULL
);
179 } else if (database
== SAM_DATABASE_BUILTIN
) {
180 /* work out the builtin_dn - useful for so many calls its worth
182 const char *dnstring
= samdb_search_string(state
->sam_ldb
, mem_ctx
, NULL
,
183 "distinguishedName", "objectClass=builtinDomain");
184 state
->base_dn
[database
] = ldb_dn_new(state
, state
->sam_ldb
, dnstring
);
185 if ( ! ldb_dn_validate(state
->base_dn
[database
])) {
186 return NT_STATUS_INTERNAL_ERROR
;
190 return NT_STATUS_INVALID_PARAMETER
;
193 msg
->dn
= talloc_reference(mem_ctx
, state
->base_dn
[database
]);
195 return NT_STATUS_NO_MEMORY
;
198 samdb_msg_add_string(state
->sam_ldb
, mem_ctx
,
199 msg
, "oEMInformation", domain
->oem_information
.string
);
201 samdb_msg_add_int64(state
->sam_ldb
, mem_ctx
,
202 msg
, "forceLogoff", domain
->force_logoff_time
);
204 samdb_msg_add_uint(state
->sam_ldb
, mem_ctx
,
205 msg
, "minPwdLen", domain
->min_password_length
);
207 samdb_msg_add_int64(state
->sam_ldb
, mem_ctx
,
208 msg
, "maxPwdAge", domain
->max_password_age
);
210 samdb_msg_add_int64(state
->sam_ldb
, mem_ctx
,
211 msg
, "minPwdAge", domain
->min_password_age
);
213 samdb_msg_add_uint(state
->sam_ldb
, mem_ctx
,
214 msg
, "pwdHistoryLength", domain
->password_history_length
);
216 samdb_msg_add_uint64(state
->sam_ldb
, mem_ctx
,
217 msg
, "modifiedCount",
218 domain
->sequence_num
);
220 samdb_msg_add_uint64(state
->sam_ldb
, mem_ctx
,
221 msg
, "creationTime", domain
->domain_create_time
);
223 /* TODO: Account lockout, password properties */
225 ret
= dsdb_replace(state
->sam_ldb
, msg
, 0);
228 return NT_STATUS_INTERNAL_ERROR
;
233 static NTSTATUS
samsync_ldb_handle_user(TALLOC_CTX
*mem_ctx
,
234 struct samsync_ldb_state
*state
,
235 enum netr_SamDatabaseID database
,
236 struct netr_DELTA_ENUM
*delta
,
239 uint32_t rid
= delta
->delta_id_union
.rid
;
240 struct netr_DELTA_USER
*user
= delta
->delta_union
.user
;
241 const char *container
, *obj_class
;
244 const struct dom_sid
*user_sid
;
245 struct ldb_message
*msg
;
246 struct ldb_message
**msgs
;
247 struct ldb_message
**remote_msgs
= NULL
;
252 const char *attrs
[] = { NULL
};
253 /* we may change this to a global search, then fill in only the things not in ldap later */
254 const char *remote_attrs
[] = { "userPrincipalName", "servicePrincipalName",
255 "msDS-KeyVersionNumber", "objectGUID", NULL
};
257 user_sid
= dom_sid_add_rid(mem_ctx
, state
->dom_sid
[database
], rid
);
259 return NT_STATUS_NO_MEMORY
;
262 msg
= ldb_msg_new(mem_ctx
);
264 return NT_STATUS_NO_MEMORY
;
268 /* search for the user, by rid */
269 ret
= gendb_search(state
->sam_ldb
, mem_ctx
, state
->base_dn
[database
],
270 &msgs
, attrs
, "(&(objectClass=user)(objectSid=%s))",
271 ldap_encode_ndr_dom_sid(mem_ctx
, user_sid
));
274 *error_string
= talloc_asprintf(mem_ctx
, "LDB for user %s failed: %s",
275 dom_sid_string(mem_ctx
, user_sid
),
276 ldb_errstring(state
->sam_ldb
));
277 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
278 } else if (ret
== 0) {
280 } else if (ret
> 1) {
281 *error_string
= talloc_asprintf(mem_ctx
, "More than one user with SID: %s in local LDB",
282 dom_sid_string(mem_ctx
, user_sid
));
283 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
285 msg
->dn
= msgs
[0]->dn
;
286 talloc_steal(msg
, msgs
[0]->dn
);
289 /* and do the same on the remote database */
290 if (state
->remote_ldb
) {
291 ret
= gendb_search(state
->remote_ldb
, mem_ctx
, state
->base_dn
[database
],
292 &remote_msgs
, remote_attrs
, "(&(objectClass=user)(objectSid=%s))",
293 ldap_encode_ndr_dom_sid(mem_ctx
, user_sid
));
296 *error_string
= talloc_asprintf(mem_ctx
, "remote LDAP for user %s failed: %s",
297 dom_sid_string(mem_ctx
, user_sid
),
298 ldb_errstring(state
->remote_ldb
));
299 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
300 } else if (ret
== 0) {
301 *error_string
= talloc_asprintf(mem_ctx
, "User exists in samsync but not in remote LDAP domain! (base: %s, SID: %s)",
302 ldb_dn_get_linearized(state
->base_dn
[database
]),
303 dom_sid_string(mem_ctx
, user_sid
));
304 return NT_STATUS_NO_SUCH_USER
;
305 } else if (ret
> 1) {
306 *error_string
= talloc_asprintf(mem_ctx
, "More than one user in remote LDAP domain with SID: %s",
307 dom_sid_string(mem_ctx
, user_sid
));
308 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
310 /* Try to put things in the same location as the remote server */
312 msg
->dn
= remote_msgs
[0]->dn
;
313 talloc_steal(msg
, remote_msgs
[0]->dn
);
317 cn_name
= talloc_strdup(mem_ctx
, user
->account_name
.string
);
318 NT_STATUS_HAVE_NO_MEMORY(cn_name
);
319 cn_name_len
= strlen(cn_name
);
321 #define ADD_OR_DEL(type, attrib, field) do { \
323 samdb_msg_add_ ## type(state->sam_ldb, mem_ctx, msg, \
324 attrib, user->field); \
326 samdb_msg_add_delete(state->sam_ldb, mem_ctx, msg, \
331 ADD_OR_DEL(string
, "samAccountName", account_name
.string
);
332 ADD_OR_DEL(string
, "displayName", full_name
.string
);
334 if (samdb_msg_add_dom_sid(state
->sam_ldb
, mem_ctx
, msg
,
335 "objectSid", dom_sid_add_rid(mem_ctx
, state
->dom_sid
[database
], rid
))) {
336 return NT_STATUS_NO_MEMORY
;
339 ADD_OR_DEL(uint
, "primaryGroupID", primary_gid
);
340 ADD_OR_DEL(string
, "homeDirectory", home_directory
.string
);
341 ADD_OR_DEL(string
, "homeDrive", home_drive
.string
);
342 ADD_OR_DEL(string
, "scriptPath", logon_script
.string
);
343 ADD_OR_DEL(string
, "description", description
.string
);
344 ADD_OR_DEL(string
, "userWorkstations", workstations
.string
);
346 ADD_OR_DEL(uint64
, "lastLogon", last_logon
);
347 ADD_OR_DEL(uint64
, "lastLogoff", last_logoff
);
349 if (samdb_msg_add_logon_hours(state
->sam_ldb
, mem_ctx
, msg
, "logonHours", &user
->logon_hours
) != 0) {
350 return NT_STATUS_NO_MEMORY
;
353 ADD_OR_DEL(uint
, "badPwdCount", bad_password_count
);
354 ADD_OR_DEL(uint
, "logonCount", logon_count
);
356 ADD_OR_DEL(uint64
, "pwdLastSet", last_password_change
);
357 ADD_OR_DEL(uint64
, "accountExpires", acct_expiry
);
359 if (samdb_msg_add_acct_flags(state
->sam_ldb
, mem_ctx
, msg
,
360 "userAccountControl", user
->acct_flags
) != 0) {
361 return NT_STATUS_NO_MEMORY
;
365 /* Passwords. Ensure there is no plaintext stored against
366 * this entry, as we only have hashes */
367 samdb_msg_add_delete(state
->sam_ldb
, mem_ctx
, msg
,
370 if (user
->lm_password_present
) {
371 samdb_msg_add_hash(state
->sam_ldb
, mem_ctx
, msg
,
372 "dBCSPwd", &user
->lmpassword
);
374 samdb_msg_add_delete(state
->sam_ldb
, mem_ctx
, msg
,
377 if (user
->nt_password_present
) {
378 samdb_msg_add_hash(state
->sam_ldb
, mem_ctx
, msg
,
379 "unicodePwd", &user
->ntpassword
);
381 samdb_msg_add_delete(state
->sam_ldb
, mem_ctx
, msg
,
385 ADD_OR_DEL(string
, "comment", comment
.string
);
387 if (samdb_msg_add_parameters(state
->sam_ldb
, mem_ctx
, msg
, "userParameters", &user
->parameters
) != 0) {
388 return NT_STATUS_NO_MEMORY
;
391 ADD_OR_DEL(uint
, "countryCode", country_code
);
392 ADD_OR_DEL(uint
, "codePage", code_page
);
394 ADD_OR_DEL(string
, "profilePath", profile_path
.string
);
398 for (i
=0; remote_attrs
[i
]; i
++) {
399 struct ldb_message_element
*el
= ldb_msg_find_element(remote_msgs
[0], remote_attrs
[i
]);
401 samdb_msg_add_delete(state
->sam_ldb
, mem_ctx
, msg
,
404 ldb_msg_add(msg
, el
, LDB_FLAG_MOD_REPLACE
);
408 acb
= user
->acct_flags
;
409 if (acb
& (ACB_WSTRUST
)) {
410 cn_name
[cn_name_len
- 1] = '\0';
411 container
= "Computers";
412 obj_class
= "computer";
414 } else if (acb
& ACB_SVRTRUST
) {
415 if (cn_name
[cn_name_len
- 1] != '$') {
416 return NT_STATUS_FOOBAR
;
418 cn_name
[cn_name_len
- 1] = '\0';
419 container
= "Domain Controllers";
420 obj_class
= "computer";
426 samdb_msg_add_string(state
->sam_ldb
, mem_ctx
, msg
,
427 "objectClass", obj_class
);
429 msg
->dn
= ldb_dn_copy(mem_ctx
, state
->base_dn
[database
]);
430 ldb_dn_add_child_fmt(msg
->dn
, "CN=%s,CN=%s", cn_name
, container
);
432 return NT_STATUS_NO_MEMORY
;
436 ret
= ldb_add(state
->sam_ldb
, msg
);
438 struct ldb_dn
*first_try_dn
= msg
->dn
;
439 /* Try again with the default DN */
441 *error_string
= talloc_asprintf(mem_ctx
, "Failed to create user record. Tried %s: %s",
442 ldb_dn_get_linearized(first_try_dn
),
443 ldb_errstring(state
->sam_ldb
));
444 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
446 msg
->dn
= talloc_steal(msg
, remote_msgs
[0]->dn
);
447 ret
= ldb_add(state
->sam_ldb
, msg
);
449 *error_string
= talloc_asprintf(mem_ctx
, "Failed to create user record. Tried both %s and %s: %s",
450 ldb_dn_get_linearized(first_try_dn
),
451 ldb_dn_get_linearized(msg
->dn
),
452 ldb_errstring(state
->sam_ldb
));
453 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
458 ret
= dsdb_replace(state
->sam_ldb
, msg
, 0);
460 *error_string
= talloc_asprintf(mem_ctx
, "Failed to modify user record %s: %s",
461 ldb_dn_get_linearized(msg
->dn
),
462 ldb_errstring(state
->sam_ldb
));
463 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
470 static NTSTATUS
samsync_ldb_delete_user(TALLOC_CTX
*mem_ctx
,
471 struct samsync_ldb_state
*state
,
472 enum netr_SamDatabaseID database
,
473 struct netr_DELTA_ENUM
*delta
,
476 uint32_t rid
= delta
->delta_id_union
.rid
;
477 struct ldb_message
**msgs
;
479 const char *attrs
[] = { NULL
};
481 /* search for the user, by rid */
482 ret
= gendb_search(state
->sam_ldb
, mem_ctx
, state
->base_dn
[database
],
483 &msgs
, attrs
, "(&(objectClass=user)(objectSid=%s))",
484 ldap_encode_ndr_dom_sid(mem_ctx
, dom_sid_add_rid(mem_ctx
, state
->dom_sid
[database
], rid
)));
487 *error_string
= talloc_asprintf(mem_ctx
, "gendb_search failed: %s", ldb_errstring(state
->sam_ldb
));
488 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
489 } else if (ret
== 0) {
490 return NT_STATUS_NO_SUCH_USER
;
491 } else if (ret
> 1) {
492 *error_string
= talloc_asprintf(mem_ctx
, "More than one user with SID: %s",
493 dom_sid_string(mem_ctx
,
494 dom_sid_add_rid(mem_ctx
,
495 state
->dom_sid
[database
],
497 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
500 ret
= ldb_delete(state
->sam_ldb
, msgs
[0]->dn
);
502 *error_string
= talloc_asprintf(mem_ctx
, "Failed to delete user record %s: %s",
503 ldb_dn_get_linearized(msgs
[0]->dn
),
504 ldb_errstring(state
->sam_ldb
));
505 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
511 static NTSTATUS
samsync_ldb_handle_group(TALLOC_CTX
*mem_ctx
,
512 struct samsync_ldb_state
*state
,
513 enum netr_SamDatabaseID database
,
514 struct netr_DELTA_ENUM
*delta
,
517 uint32_t rid
= delta
->delta_id_union
.rid
;
518 struct netr_DELTA_GROUP
*group
= delta
->delta_union
.group
;
519 const char *container
, *obj_class
;
522 struct ldb_message
*msg
;
523 struct ldb_message
**msgs
;
526 const char *attrs
[] = { NULL
};
528 msg
= ldb_msg_new(mem_ctx
);
530 return NT_STATUS_NO_MEMORY
;
533 /* search for the group, by rid */
534 ret
= gendb_search(state
->sam_ldb
, mem_ctx
, state
->base_dn
[database
], &msgs
, attrs
,
535 "(&(objectClass=group)(objectSid=%s))",
536 ldap_encode_ndr_dom_sid(mem_ctx
, dom_sid_add_rid(mem_ctx
, state
->dom_sid
[database
], rid
)));
539 *error_string
= talloc_asprintf(mem_ctx
, "gendb_search failed: %s", ldb_errstring(state
->sam_ldb
));
540 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
541 } else if (ret
== 0) {
543 } else if (ret
> 1) {
544 *error_string
= talloc_asprintf(mem_ctx
, "More than one group/alias with SID: %s",
545 dom_sid_string(mem_ctx
,
546 dom_sid_add_rid(mem_ctx
,
547 state
->dom_sid
[database
],
549 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
551 msg
->dn
= talloc_steal(msg
, msgs
[0]->dn
);
554 cn_name
= group
->group_name
.string
;
556 #define ADD_OR_DEL(type, attrib, field) do { \
557 if (group->field) { \
558 samdb_msg_add_ ## type(state->sam_ldb, mem_ctx, msg, \
559 attrib, group->field); \
561 samdb_msg_add_delete(state->sam_ldb, mem_ctx, msg, \
566 ADD_OR_DEL(string
, "samAccountName", group_name
.string
);
568 if (samdb_msg_add_dom_sid(state
->sam_ldb
, mem_ctx
, msg
,
569 "objectSid", dom_sid_add_rid(mem_ctx
, state
->dom_sid
[database
], rid
))) {
570 return NT_STATUS_NO_MEMORY
;
573 ADD_OR_DEL(string
, "description", description
.string
);
581 samdb_msg_add_string(state
->sam_ldb
, mem_ctx
, msg
,
582 "objectClass", obj_class
);
583 msg
->dn
= ldb_dn_copy(mem_ctx
, state
->base_dn
[database
]);
584 ldb_dn_add_child_fmt(msg
->dn
, "CN=%s,CN=%s", cn_name
, container
);
586 return NT_STATUS_NO_MEMORY
;
589 ret
= ldb_add(state
->sam_ldb
, msg
);
591 *error_string
= talloc_asprintf(mem_ctx
, "Failed to create group record %s: %s",
592 ldb_dn_get_linearized(msg
->dn
),
593 ldb_errstring(state
->sam_ldb
));
594 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
597 ret
= dsdb_replace(state
->sam_ldb
, msg
, 0);
599 *error_string
= talloc_asprintf(mem_ctx
, "Failed to modify group record %s: %s",
600 ldb_dn_get_linearized(msg
->dn
),
601 ldb_errstring(state
->sam_ldb
));
602 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
609 static NTSTATUS
samsync_ldb_delete_group(TALLOC_CTX
*mem_ctx
,
610 struct samsync_ldb_state
*state
,
611 enum netr_SamDatabaseID database
,
612 struct netr_DELTA_ENUM
*delta
,
615 uint32_t rid
= delta
->delta_id_union
.rid
;
616 struct ldb_message
**msgs
;
618 const char *attrs
[] = { NULL
};
620 /* search for the group, by rid */
621 ret
= gendb_search(state
->sam_ldb
, mem_ctx
, state
->base_dn
[database
], &msgs
, attrs
,
622 "(&(objectClass=group)(objectSid=%s))",
623 ldap_encode_ndr_dom_sid(mem_ctx
, dom_sid_add_rid(mem_ctx
, state
->dom_sid
[database
], rid
)));
626 *error_string
= talloc_asprintf(mem_ctx
, "gendb_search failed: %s", ldb_errstring(state
->sam_ldb
));
627 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
628 } else if (ret
== 0) {
629 return NT_STATUS_NO_SUCH_GROUP
;
630 } else if (ret
> 1) {
631 *error_string
= talloc_asprintf(mem_ctx
, "More than one group/alias with SID: %s",
632 dom_sid_string(mem_ctx
,
633 dom_sid_add_rid(mem_ctx
,
634 state
->dom_sid
[database
],
636 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
639 ret
= ldb_delete(state
->sam_ldb
, msgs
[0]->dn
);
641 *error_string
= talloc_asprintf(mem_ctx
, "Failed to delete group record %s: %s",
642 ldb_dn_get_linearized(msgs
[0]->dn
),
643 ldb_errstring(state
->sam_ldb
));
644 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
650 static NTSTATUS
samsync_ldb_handle_group_member(TALLOC_CTX
*mem_ctx
,
651 struct samsync_ldb_state
*state
,
652 enum netr_SamDatabaseID database
,
653 struct netr_DELTA_ENUM
*delta
,
656 uint32_t rid
= delta
->delta_id_union
.rid
;
657 struct netr_DELTA_GROUP_MEMBER
*group_member
= delta
->delta_union
.group_member
;
658 struct ldb_message
*msg
;
659 struct ldb_message
**msgs
;
661 const char *attrs
[] = { NULL
};
664 msg
= ldb_msg_new(mem_ctx
);
666 return NT_STATUS_NO_MEMORY
;
669 /* search for the group, by rid */
670 ret
= gendb_search(state
->sam_ldb
, mem_ctx
, state
->base_dn
[database
], &msgs
, attrs
,
671 "(&(objectClass=group)(objectSid=%s))",
672 ldap_encode_ndr_dom_sid(mem_ctx
, dom_sid_add_rid(mem_ctx
, state
->dom_sid
[database
], rid
)));
675 *error_string
= talloc_asprintf(mem_ctx
, "gendb_search failed: %s", ldb_errstring(state
->sam_ldb
));
676 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
677 } else if (ret
== 0) {
678 return NT_STATUS_NO_SUCH_GROUP
;
679 } else if (ret
> 1) {
680 *error_string
= talloc_asprintf(mem_ctx
, "More than one group/alias with SID: %s",
681 dom_sid_string(mem_ctx
,
682 dom_sid_add_rid(mem_ctx
,
683 state
->dom_sid
[database
],
685 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
687 msg
->dn
= talloc_steal(msg
, msgs
[0]->dn
);
692 for (i
=0; i
<group_member
->num_rids
; i
++) {
693 /* search for the group, by rid */
694 ret
= gendb_search(state
->sam_ldb
, mem_ctx
, state
->base_dn
[database
], &msgs
, attrs
,
695 "(&(objectClass=user)(objectSid=%s))",
696 ldap_encode_ndr_dom_sid(mem_ctx
, dom_sid_add_rid(mem_ctx
, state
->dom_sid
[database
], group_member
->rids
[i
])));
699 *error_string
= talloc_asprintf(mem_ctx
, "gendb_search failed: %s", ldb_errstring(state
->sam_ldb
));
700 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
701 } else if (ret
== 0) {
702 return NT_STATUS_NO_SUCH_USER
;
703 } else if (ret
> 1) {
704 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
706 samdb_msg_add_string(state
->sam_ldb
, mem_ctx
, msg
, "member", ldb_dn_alloc_linearized(mem_ctx
, msgs
[0]->dn
));
712 ret
= dsdb_replace(state
->sam_ldb
, msg
, 0);
714 *error_string
= talloc_asprintf(mem_ctx
, "Failed to modify group record %s: %s",
715 ldb_dn_get_linearized(msg
->dn
),
716 ldb_errstring(state
->sam_ldb
));
717 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
723 static NTSTATUS
samsync_ldb_handle_alias(TALLOC_CTX
*mem_ctx
,
724 struct samsync_ldb_state
*state
,
725 enum netr_SamDatabaseID database
,
726 struct netr_DELTA_ENUM
*delta
,
729 uint32_t rid
= delta
->delta_id_union
.rid
;
730 struct netr_DELTA_ALIAS
*alias
= delta
->delta_union
.alias
;
731 const char *container
, *obj_class
;
734 struct ldb_message
*msg
;
735 struct ldb_message
**msgs
;
738 const char *attrs
[] = { NULL
};
740 msg
= ldb_msg_new(mem_ctx
);
742 return NT_STATUS_NO_MEMORY
;
745 /* search for the alias, by rid */
746 ret
= gendb_search(state
->sam_ldb
, mem_ctx
, state
->base_dn
[database
], &msgs
, attrs
,
747 "(&(objectClass=group)(objectSid=%s))",
748 ldap_encode_ndr_dom_sid(mem_ctx
, dom_sid_add_rid(mem_ctx
, state
->dom_sid
[database
], rid
)));
751 *error_string
= talloc_asprintf(mem_ctx
, "gendb_search failed: %s", ldb_errstring(state
->sam_ldb
));
752 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
753 } else if (ret
== 0) {
755 } else if (ret
> 1) {
756 *error_string
= talloc_asprintf(mem_ctx
, "More than one group/alias with SID: %s",
757 dom_sid_string(mem_ctx
,
758 dom_sid_add_rid(mem_ctx
,
759 state
->dom_sid
[database
],
761 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
763 msg
->dn
= talloc_steal(mem_ctx
, msgs
[0]->dn
);
766 cn_name
= alias
->alias_name
.string
;
768 #define ADD_OR_DEL(type, attrib, field) do { \
769 if (alias->field) { \
770 samdb_msg_add_ ## type(state->sam_ldb, mem_ctx, msg, \
771 attrib, alias->field); \
773 samdb_msg_add_delete(state->sam_ldb, mem_ctx, msg, \
778 ADD_OR_DEL(string
, "samAccountName", alias_name
.string
);
780 if (samdb_msg_add_dom_sid(state
->sam_ldb
, mem_ctx
, msg
,
781 "objectSid", dom_sid_add_rid(mem_ctx
, state
->dom_sid
[database
], rid
))) {
782 return NT_STATUS_NO_MEMORY
;
785 ADD_OR_DEL(string
, "description", description
.string
);
789 samdb_msg_add_uint(state
->sam_ldb
, mem_ctx
, msg
, "groupType", 0x80000004);
795 samdb_msg_add_string(state
->sam_ldb
, mem_ctx
, msg
,
796 "objectClass", obj_class
);
797 msg
->dn
= ldb_dn_copy(mem_ctx
, state
->base_dn
[database
]);
798 ldb_dn_add_child_fmt(msg
->dn
, "CN=%s,CN=%s", cn_name
, container
);
800 return NT_STATUS_NO_MEMORY
;
803 ret
= ldb_add(state
->sam_ldb
, msg
);
805 *error_string
= talloc_asprintf(mem_ctx
, "Failed to create alias record %s: %s",
806 ldb_dn_get_linearized(msg
->dn
),
807 ldb_errstring(state
->sam_ldb
));
808 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
811 ret
= dsdb_replace(state
->sam_ldb
, msg
, 0);
813 *error_string
= talloc_asprintf(mem_ctx
, "Failed to modify alias record %s: %s",
814 ldb_dn_get_linearized(msg
->dn
),
815 ldb_errstring(state
->sam_ldb
));
816 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
823 static NTSTATUS
samsync_ldb_delete_alias(TALLOC_CTX
*mem_ctx
,
824 struct samsync_ldb_state
*state
,
825 enum netr_SamDatabaseID database
,
826 struct netr_DELTA_ENUM
*delta
,
829 uint32_t rid
= delta
->delta_id_union
.rid
;
830 struct ldb_message
**msgs
;
832 const char *attrs
[] = { NULL
};
834 /* search for the alias, by rid */
835 ret
= gendb_search(state
->sam_ldb
, mem_ctx
, state
->base_dn
[database
], &msgs
, attrs
,
836 "(&(objectClass=group)(objectSid=%s))",
837 ldap_encode_ndr_dom_sid(mem_ctx
, dom_sid_add_rid(mem_ctx
, state
->dom_sid
[database
], rid
)));
840 *error_string
= talloc_asprintf(mem_ctx
, "gendb_search failed: %s", ldb_errstring(state
->sam_ldb
));
841 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
842 } else if (ret
== 0) {
843 return NT_STATUS_NO_SUCH_ALIAS
;
844 } else if (ret
> 1) {
845 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
848 ret
= ldb_delete(state
->sam_ldb
, msgs
[0]->dn
);
850 *error_string
= talloc_asprintf(mem_ctx
, "Failed to delete alias record %s: %s",
851 ldb_dn_get_linearized(msgs
[0]->dn
),
852 ldb_errstring(state
->sam_ldb
));
853 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
859 static NTSTATUS
samsync_ldb_handle_alias_member(TALLOC_CTX
*mem_ctx
,
860 struct samsync_ldb_state
*state
,
861 enum netr_SamDatabaseID database
,
862 struct netr_DELTA_ENUM
*delta
,
865 uint32_t rid
= delta
->delta_id_union
.rid
;
866 struct netr_DELTA_ALIAS_MEMBER
*alias_member
= delta
->delta_union
.alias_member
;
867 struct ldb_message
*msg
;
868 struct ldb_message
**msgs
;
870 const char *attrs
[] = { NULL
};
873 msg
= ldb_msg_new(mem_ctx
);
875 return NT_STATUS_NO_MEMORY
;
878 /* search for the alias, by rid */
879 ret
= gendb_search(state
->sam_ldb
, mem_ctx
, state
->base_dn
[database
], &msgs
, attrs
,
880 "(&(objectClass=group)(objectSid=%s))",
881 ldap_encode_ndr_dom_sid(mem_ctx
, dom_sid_add_rid(mem_ctx
, state
->dom_sid
[database
], rid
)));
884 *error_string
= talloc_asprintf(mem_ctx
, "gendb_search failed: %s", ldb_errstring(state
->sam_ldb
));
885 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
886 } else if (ret
== 0) {
887 return NT_STATUS_NO_SUCH_GROUP
;
888 } else if (ret
> 1) {
889 *error_string
= talloc_asprintf(mem_ctx
, "More than one group/alias with SID: %s",
890 dom_sid_string(mem_ctx
,
891 dom_sid_add_rid(mem_ctx
,
892 state
->dom_sid
[database
],
894 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
896 msg
->dn
= talloc_steal(msg
, msgs
[0]->dn
);
901 for (i
=0; i
<alias_member
->sids
.num_sids
; i
++) {
902 struct ldb_dn
*alias_member_dn
;
903 /* search for members, in the top basedn (normal users are builtin aliases) */
904 ret
= gendb_search(state
->sam_ldb
, mem_ctx
, state
->base_dn
[SAM_DATABASE_DOMAIN
], &msgs
, attrs
,
906 ldap_encode_ndr_dom_sid(mem_ctx
, alias_member
->sids
.sids
[i
].sid
));
909 *error_string
= talloc_asprintf(mem_ctx
, "gendb_search failed: %s", ldb_errstring(state
->sam_ldb
));
910 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
911 } else if (ret
== 0) {
913 nt_status
= samsync_ldb_add_foreignSecurityPrincipal(mem_ctx
, state
,
914 alias_member
->sids
.sids
[i
].sid
,
917 if (!NT_STATUS_IS_OK(nt_status
)) {
920 } else if (ret
> 1) {
921 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
923 alias_member_dn
= msgs
[0]->dn
;
925 samdb_msg_add_string(state
->sam_ldb
, mem_ctx
, msg
, "member", ldb_dn_alloc_linearized(mem_ctx
, alias_member_dn
));
930 ret
= dsdb_replace(state
->sam_ldb
, msg
, 0);
932 *error_string
= talloc_asprintf(mem_ctx
, "Failed to modify group record %s: %s",
933 ldb_dn_get_linearized(msg
->dn
),
934 ldb_errstring(state
->sam_ldb
));
935 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
941 static NTSTATUS
samsync_ldb_handle_account(TALLOC_CTX
*mem_ctx
,
942 struct samsync_ldb_state
*state
,
943 enum netr_SamDatabaseID database
,
944 struct netr_DELTA_ENUM
*delta
,
947 struct dom_sid
*sid
= delta
->delta_id_union
.sid
;
948 struct netr_DELTA_ACCOUNT
*account
= delta
->delta_union
.account
;
950 struct ldb_message
*msg
;
953 char *dnstr
, *sidstr
;
955 msg
= ldb_msg_new(mem_ctx
);
957 return NT_STATUS_NO_MEMORY
;
960 sidstr
= dom_sid_string(msg
, sid
);
961 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(sidstr
, msg
);
963 dnstr
= talloc_asprintf(msg
, "sid=%s", sidstr
);
964 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(dnstr
, msg
);
966 msg
->dn
= ldb_dn_new(msg
, state
->pdb
, dnstr
);
967 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(msg
->dn
, msg
);
969 for (i
=0; i
< account
->privilege_entries
; i
++) {
970 samdb_msg_add_string(state
->pdb
, mem_ctx
, msg
, "privilege",
971 account
->privilege_name
[i
].string
);
974 ret
= dsdb_replace(state
->pdb
, msg
, 0);
975 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
976 if (samdb_msg_add_dom_sid(state
->pdb
, msg
, msg
, "objectSid", sid
) != LDB_SUCCESS
) {
978 return NT_STATUS_NO_MEMORY
;
980 samdb_msg_add_string(state
->pdb
, msg
, msg
, "comment", "added via samsync");
981 ret
= ldb_add(state
->pdb
, msg
);
985 *error_string
= talloc_asprintf(mem_ctx
, "Failed to modify privilege record %s",
986 ldb_dn_get_linearized(msg
->dn
));
987 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
993 static NTSTATUS
samsync_ldb_delete_account(TALLOC_CTX
*mem_ctx
,
994 struct samsync_ldb_state
*state
,
995 enum netr_SamDatabaseID database
,
996 struct netr_DELTA_ENUM
*delta
,
999 struct dom_sid
*sid
= delta
->delta_id_union
.sid
;
1001 struct ldb_message
*msg
;
1002 struct ldb_message
**msgs
;
1004 const char *attrs
[] = { NULL
};
1006 msg
= ldb_msg_new(mem_ctx
);
1008 return NT_STATUS_NO_MEMORY
;
1011 /* search for the account, by sid, in the top basedn */
1012 ret
= gendb_search(state
->sam_ldb
, mem_ctx
, state
->base_dn
[SAM_DATABASE_DOMAIN
], &msgs
, attrs
,
1014 ldap_encode_ndr_dom_sid(mem_ctx
, sid
));
1017 *error_string
= talloc_asprintf(mem_ctx
, "gendb_search failed: %s", ldb_errstring(state
->sam_ldb
));
1018 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
1019 } else if (ret
== 0) {
1020 return NT_STATUS_NO_SUCH_USER
;
1021 } else if (ret
> 1) {
1022 *error_string
= talloc_asprintf(mem_ctx
, "More than one account with SID: %s",
1023 dom_sid_string(mem_ctx
, sid
));
1024 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
1026 msg
->dn
= talloc_steal(msg
, msgs
[0]->dn
);
1029 samdb_msg_add_delete(state
->sam_ldb
, mem_ctx
, msg
,
1032 ret
= dsdb_replace(state
->sam_ldb
, msg
, 0);
1034 *error_string
= talloc_asprintf(mem_ctx
, "Failed to modify privilege record %s",
1035 ldb_dn_get_linearized(msg
->dn
));
1036 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
1039 return NT_STATUS_OK
;
1042 static NTSTATUS
libnet_samsync_ldb_fn(TALLOC_CTX
*mem_ctx
,
1044 enum netr_SamDatabaseID database
,
1045 struct netr_DELTA_ENUM
*delta
,
1046 char **error_string
)
1048 NTSTATUS nt_status
= NT_STATUS_OK
;
1049 struct samsync_ldb_state
*state
= talloc_get_type(private_data
, struct samsync_ldb_state
);
1051 *error_string
= NULL
;
1052 switch (delta
->delta_type
) {
1053 case NETR_DELTA_DOMAIN
:
1055 nt_status
= samsync_ldb_handle_domain(mem_ctx
,
1062 case NETR_DELTA_USER
:
1064 nt_status
= samsync_ldb_handle_user(mem_ctx
,
1071 case NETR_DELTA_DELETE_USER
:
1073 nt_status
= samsync_ldb_delete_user(mem_ctx
,
1080 case NETR_DELTA_GROUP
:
1082 nt_status
= samsync_ldb_handle_group(mem_ctx
,
1089 case NETR_DELTA_DELETE_GROUP
:
1091 nt_status
= samsync_ldb_delete_group(mem_ctx
,
1098 case NETR_DELTA_GROUP_MEMBER
:
1100 nt_status
= samsync_ldb_handle_group_member(mem_ctx
,
1107 case NETR_DELTA_ALIAS
:
1109 nt_status
= samsync_ldb_handle_alias(mem_ctx
,
1116 case NETR_DELTA_DELETE_ALIAS
:
1118 nt_status
= samsync_ldb_delete_alias(mem_ctx
,
1125 case NETR_DELTA_ALIAS_MEMBER
:
1127 nt_status
= samsync_ldb_handle_alias_member(mem_ctx
,
1134 case NETR_DELTA_ACCOUNT
:
1136 nt_status
= samsync_ldb_handle_account(mem_ctx
,
1143 case NETR_DELTA_DELETE_ACCOUNT
:
1145 nt_status
= samsync_ldb_delete_account(mem_ctx
,
1153 /* Can't dump them all right now */
1156 if (!NT_STATUS_IS_OK(nt_status
) && !*error_string
) {
1157 *error_string
= talloc_asprintf(mem_ctx
, "Failed to handle samsync delta: %s", nt_errstr(nt_status
));
1162 static NTSTATUS
libnet_samsync_ldb_init(TALLOC_CTX
*mem_ctx
,
1164 struct libnet_SamSync_state
*samsync_state
,
1165 char **error_string
)
1167 struct samsync_ldb_state
*state
= talloc_get_type(private_data
, struct samsync_ldb_state
);
1168 const char *server
= dcerpc_server_name(samsync_state
->netlogon_pipe
);
1171 state
->samsync_state
= samsync_state
;
1173 ZERO_STRUCT(state
->dom_sid
);
1174 if (state
->samsync_state
->domain_sid
) {
1175 state
->dom_sid
[SAM_DATABASE_DOMAIN
] = dom_sid_dup(state
, state
->samsync_state
->domain_sid
);
1178 state
->dom_sid
[SAM_DATABASE_BUILTIN
] = dom_sid_parse_talloc(state
, SID_BUILTIN
);
1180 if (state
->samsync_state
->realm
) {
1181 if (!server
|| !*server
) {
1182 /* huh? how do we not have a server name? */
1183 *error_string
= talloc_strdup(mem_ctx
, "No DCE/RPC server name available. How did we connect?");
1184 return NT_STATUS_INVALID_PARAMETER
;
1186 ldap_url
= talloc_asprintf(state
, "ldap://%s", server
);
1188 state
->remote_ldb
= ldb_wrap_connect(mem_ctx
,
1189 state
->samsync_state
->machine_net_ctx
->event_ctx
,
1190 state
->samsync_state
->machine_net_ctx
->lp_ctx
,
1192 NULL
, state
->samsync_state
->machine_net_ctx
->cred
,
1194 if (!state
->remote_ldb
) {
1195 *error_string
= talloc_asprintf(mem_ctx
, "Failed to connect to remote LDAP server at %s (used to extract additional data in SamSync replication)", ldap_url
);
1196 return NT_STATUS_NO_LOGON_SERVERS
;
1199 state
->remote_ldb
= NULL
;
1201 return NT_STATUS_OK
;
1204 NTSTATUS
libnet_samsync_ldb(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
, struct libnet_samsync_ldb
*r
)
1207 struct libnet_SamSync r2
;
1208 struct samsync_ldb_state
*state
= talloc(mem_ctx
, struct samsync_ldb_state
);
1211 return NT_STATUS_NO_MEMORY
;
1214 state
->secrets
= NULL
;
1215 state
->trusted_domains
= NULL
;
1217 state
->sam_ldb
= samdb_connect(mem_ctx
,
1220 r
->in
.session_info
);
1221 if (!state
->sam_ldb
) {
1222 return NT_STATUS_INTERNAL_DB_ERROR
;
1225 state
->pdb
= privilege_connect(mem_ctx
,
1229 return NT_STATUS_INTERNAL_DB_ERROR
;
1232 r2
.out
.error_string
= NULL
;
1233 r2
.in
.binding_string
= r
->in
.binding_string
;
1234 r2
.in
.init_fn
= libnet_samsync_ldb_init
;
1235 r2
.in
.delta_fn
= libnet_samsync_ldb_fn
;
1236 r2
.in
.fn_ctx
= state
;
1237 r2
.in
.machine_account
= NULL
; /* TODO: Create a machine account, fill this in, and the delete it */
1238 nt_status
= libnet_SamSync_netlogon(ctx
, state
, &r2
);
1239 r
->out
.error_string
= r2
.out
.error_string
;
1240 talloc_steal(mem_ctx
, r
->out
.error_string
);
1242 if (!NT_STATUS_IS_OK(nt_status
)) {