2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2004
5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
6 Copyright (C) Brad Henry 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "libnet/libnet.h"
25 #include "librpc/gen_ndr/ndr_drsuapi_c.h"
26 #include "lib/ldb/include/ldb.h"
27 #include "lib/ldb/include/ldb_errors.h"
28 #include "passdb/secrets.h"
29 #include "dsdb/samdb/samdb.h"
31 #include "libcli/security/security.h"
32 #include "auth/credentials/credentials.h"
33 #include "librpc/gen_ndr/ndr_samr_c.h"
36 * complete a domain join, when joining to a AD domain:
37 * 1.) connect and bind to the DRSUAPI pipe
38 * 2.) do a DsCrackNames() to find the machine account dn
40 * 4.) do an ldap search to find the "msDS-KeyVersionNumber" of the machine account
41 * 5.) set the servicePrincipalName's of the machine account via LDAP, (maybe we should use DsWriteAccountSpn()...)
42 * 6.) do a DsCrackNames() to find the domain dn
43 * 7.) find out Site specific stuff, look at libnet_JoinSite() for details
45 static NTSTATUS
libnet_JoinADSDomain(struct libnet_context
*ctx
, struct libnet_JoinDomain
*r
)
51 const char *realm
= r
->out
.realm
;
53 struct dcerpc_binding
*samr_binding
= r
->out
.samr_binding
;
55 struct dcerpc_pipe
*drsuapi_pipe
;
56 struct dcerpc_binding
*drsuapi_binding
;
57 struct drsuapi_DsBind r_drsuapi_bind
;
58 struct drsuapi_DsCrackNames r_crack_names
;
59 struct drsuapi_DsNameString names
[1];
60 struct policy_handle drsuapi_bind_handle
;
61 struct GUID drsuapi_bind_guid
;
63 struct ldb_context
*remote_ldb
;
64 const struct ldb_dn
*account_dn
;
65 const char *account_dn_str
;
66 const char *remote_ldb_url
;
67 struct ldb_result
*res
;
68 struct ldb_message
*msg
;
74 const char * const attrs
[] = {
75 "msDS-KeyVersionNumber",
76 "servicePrincipalName",
81 r
->out
.error_string
= NULL
;
83 /* We need to convert between a samAccountName and domain to a
84 * DN in the directory. The correct way to do this is with
85 * DRSUAPI CrackNames */
87 /* Fiddle with the bindings, so get to DRSUAPI on
88 * NCACN_IP_TCP, sealed */
89 tmp_ctx
= talloc_named(r
, 0, "libnet_JoinADSDomain temp context");
91 r
->out
.error_string
= NULL
;
92 return NT_STATUS_NO_MEMORY
;
95 drsuapi_binding
= talloc(tmp_ctx
, struct dcerpc_binding
);
96 if (!drsuapi_binding
) {
97 r
->out
.error_string
= NULL
;
99 return NT_STATUS_NO_MEMORY
;
102 *drsuapi_binding
= *samr_binding
;
104 /* DRSUAPI is only available on IP_TCP, and locally on NCALRPC */
105 if (drsuapi_binding
->transport
!= NCALRPC
) {
106 drsuapi_binding
->transport
= NCACN_IP_TCP
;
108 drsuapi_binding
->endpoint
= NULL
;
109 drsuapi_binding
->flags
|= DCERPC_SEAL
;
111 status
= dcerpc_pipe_connect_b(tmp_ctx
,
114 &dcerpc_table_drsuapi
,
117 if (!NT_STATUS_IS_OK(status
)) {
118 r
->out
.error_string
= talloc_asprintf(r
,
119 "Connection to DRSUAPI pipe of PDC of domain '%s' failed: %s",
122 talloc_free(tmp_ctx
);
126 /* get a DRSUAPI pipe handle */
127 GUID_from_string(DRSUAPI_DS_BIND_GUID
, &drsuapi_bind_guid
);
129 r_drsuapi_bind
.in
.bind_guid
= &drsuapi_bind_guid
;
130 r_drsuapi_bind
.in
.bind_info
= NULL
;
131 r_drsuapi_bind
.out
.bind_handle
= &drsuapi_bind_handle
;
133 status
= dcerpc_drsuapi_DsBind(drsuapi_pipe
, tmp_ctx
, &r_drsuapi_bind
);
134 if (!NT_STATUS_IS_OK(status
)) {
135 if (NT_STATUS_EQUAL(status
, NT_STATUS_NET_WRITE_FAULT
)) {
138 "dcerpc_drsuapi_DsBind failed - %s\n",
139 dcerpc_errstr(tmp_ctx
, drsuapi_pipe
->last_fault_code
));
140 talloc_free(tmp_ctx
);
145 "dcerpc_drsuapi_DsBind failed - %s\n",
147 talloc_free(tmp_ctx
);
150 } else if (!W_ERROR_IS_OK(r_drsuapi_bind
.out
.result
)) {
153 "DsBind failed - %s\n",
154 win_errstr(r_drsuapi_bind
.out
.result
));
155 talloc_free(tmp_ctx
);
156 return NT_STATUS_UNSUCCESSFUL
;
159 /* Actually 'crack' the names */
160 ZERO_STRUCT(r_crack_names
);
161 r_crack_names
.in
.bind_handle
= &drsuapi_bind_handle
;
162 r_crack_names
.in
.level
= 1;
163 r_crack_names
.in
.req
.req1
.unknown1
= 0x000004e4;
164 r_crack_names
.in
.req
.req1
.unknown2
= 0x00000407;
165 r_crack_names
.in
.req
.req1
.count
= 1;
166 r_crack_names
.in
.req
.req1
.names
= names
;
167 r_crack_names
.in
.req
.req1
.format_flags
= DRSUAPI_DS_NAME_FLAG_NO_FLAGS
;
168 r_crack_names
.in
.req
.req1
.format_offered
= DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY
;
169 r_crack_names
.in
.req
.req1
.format_desired
= DRSUAPI_DS_NAME_FORMAT_FQDN_1779
;
170 names
[0].str
= dom_sid_string(tmp_ctx
, r
->out
.account_sid
);
172 r
->out
.error_string
= NULL
;
173 talloc_free(tmp_ctx
);
174 return NT_STATUS_NO_MEMORY
;
177 status
= dcerpc_drsuapi_DsCrackNames(drsuapi_pipe
, tmp_ctx
, &r_crack_names
);
178 if (!NT_STATUS_IS_OK(status
)) {
179 if (NT_STATUS_EQUAL(status
, NT_STATUS_NET_WRITE_FAULT
)) {
182 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s\n",
184 dcerpc_errstr(tmp_ctx
, drsuapi_pipe
->last_fault_code
));
185 talloc_free(tmp_ctx
);
190 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s\n",
193 talloc_free(tmp_ctx
);
196 } else if (!W_ERROR_IS_OK(r_crack_names
.out
.result
)) {
199 "DsCrackNames failed - %s\n", win_errstr(r_crack_names
.out
.result
));
200 talloc_free(tmp_ctx
);
201 return NT_STATUS_UNSUCCESSFUL
;
202 } else if (r_crack_names
.out
.level
!= 1
203 || !r_crack_names
.out
.ctr
.ctr1
204 || r_crack_names
.out
.ctr
.ctr1
->count
!= 1
205 || !r_crack_names
.out
.ctr
.ctr1
->array
[0].result_name
206 || r_crack_names
.out
.ctr
.ctr1
->array
[0].status
!= DRSUAPI_DS_NAME_STATUS_OK
) {
207 r
->out
.error_string
= talloc_asprintf(r
, "DsCrackNames failed\n");
208 talloc_free(tmp_ctx
);
209 return NT_STATUS_UNSUCCESSFUL
;
212 /* Store the DN of our machine account. */
213 account_dn_str
= r_crack_names
.out
.ctr
.ctr1
->array
[0].result_name
;
215 account_dn
= ldb_dn_explode(tmp_ctx
, account_dn_str
);
217 r
->out
.error_string
= talloc_asprintf(r
, "Invalid account dn: %s",
219 talloc_free(tmp_ctx
);
220 return NT_STATUS_UNSUCCESSFUL
;
223 /* Now we know the user's DN, open with LDAP, read and modify a few things */
225 remote_ldb_url
= talloc_asprintf(tmp_ctx
, "ldap://%s",
226 drsuapi_binding
->host
);
227 if (!remote_ldb_url
) {
228 r
->out
.error_string
= NULL
;
229 talloc_free(tmp_ctx
);
230 return NT_STATUS_NO_MEMORY
;
233 remote_ldb
= ldb_wrap_connect(tmp_ctx
, remote_ldb_url
,
234 NULL
, ctx
->cred
, 0, NULL
);
236 r
->out
.error_string
= NULL
;
237 talloc_free(tmp_ctx
);
238 return NT_STATUS_UNSUCCESSFUL
;
241 /* search for the user's record */
242 ret
= ldb_search(remote_ldb
, account_dn
, LDB_SCOPE_BASE
,
244 if (ret
!= LDB_SUCCESS
|| res
->count
!= 1) {
245 r
->out
.error_string
= talloc_asprintf(r
, "ldb_search for %s failed - %s\n",
246 account_dn_str
, ldb_errstring(remote_ldb
));
247 talloc_free(tmp_ctx
);
248 return NT_STATUS_UNSUCCESSFUL
;
251 /* If we have a kvno recorded in AD, we need it locally as well */
252 kvno
= ldb_msg_find_uint(res
->msgs
[0], "msDS-KeyVersionNumber", 0);
254 /* Prepare a new message, for the modify */
255 msg
= ldb_msg_new(tmp_ctx
);
257 r
->out
.error_string
= NULL
;
258 talloc_free(tmp_ctx
);
259 return NT_STATUS_NO_MEMORY
;
261 msg
->dn
= res
->msgs
[0]->dn
;
265 const char *service_principal_name
[6];
266 const char *dns_host_name
= strlower_talloc(tmp_ctx
,
267 talloc_asprintf(tmp_ctx
,
272 if (!dns_host_name
) {
273 r
->out
.error_string
= NULL
;
274 talloc_free(tmp_ctx
);
275 return NT_STATUS_NO_MEMORY
;
278 service_principal_name
[0] = talloc_asprintf(tmp_ctx
, "host/%s", dns_host_name
);
279 service_principal_name
[1] = talloc_asprintf(tmp_ctx
, "host/%s", strlower_talloc(tmp_ctx
, r
->in
.netbios_name
));
280 service_principal_name
[2] = talloc_asprintf(tmp_ctx
, "host/%s/%s", dns_host_name
, realm
);
281 service_principal_name
[3] = talloc_asprintf(tmp_ctx
, "host/%s/%s", strlower_talloc(tmp_ctx
, r
->in
.netbios_name
), realm
);
282 service_principal_name
[4] = talloc_asprintf(tmp_ctx
, "host/%s/%s", dns_host_name
, r
->out
.domain_name
);
283 service_principal_name
[5] = talloc_asprintf(tmp_ctx
, "host/%s/%s", strlower_talloc(tmp_ctx
, r
->in
.netbios_name
), r
->out
.domain_name
);
285 for (i
=0; i
< ARRAY_SIZE(service_principal_name
); i
++) {
286 if (!service_principal_name
[i
]) {
287 r
->out
.error_string
= NULL
;
288 talloc_free(tmp_ctx
);
289 return NT_STATUS_NO_MEMORY
;
291 rtn
= samdb_msg_add_string(remote_ldb
, tmp_ctx
, msg
, "servicePrincipalName", service_principal_name
[i
]);
293 r
->out
.error_string
= NULL
;
294 talloc_free(tmp_ctx
);
295 return NT_STATUS_NO_MEMORY
;
299 rtn
= samdb_msg_add_string(remote_ldb
, tmp_ctx
, msg
, "dNSHostName", dns_host_name
);
301 r
->out
.error_string
= NULL
;
302 talloc_free(tmp_ctx
);
303 return NT_STATUS_NO_MEMORY
;
306 rtn
= samdb_replace(remote_ldb
, tmp_ctx
, msg
);
310 "Failed to replace entries on %s\n",
311 ldb_dn_linearize(tmp_ctx
, msg
->dn
));
312 talloc_free(tmp_ctx
);
313 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
317 /* DsCrackNames to find out the DN of the domain. */
318 r_crack_names
.in
.req
.req1
.format_offered
= DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT
;
319 r_crack_names
.in
.req
.req1
.format_desired
= DRSUAPI_DS_NAME_FORMAT_FQDN_1779
;
320 names
[0].str
= talloc_asprintf(tmp_ctx
, "%s\\", r
->out
.domain_name
);
322 r
->out
.error_string
= NULL
;
323 talloc_free(tmp_ctx
);
324 return NT_STATUS_NO_MEMORY
;
327 status
= dcerpc_drsuapi_DsCrackNames(drsuapi_pipe
, tmp_ctx
, &r_crack_names
);
328 if (!NT_STATUS_IS_OK(status
)) {
329 if (NT_STATUS_EQUAL(status
, NT_STATUS_NET_WRITE_FAULT
)) {
332 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s\n",
334 dcerpc_errstr(tmp_ctx
, drsuapi_pipe
->last_fault_code
));
335 talloc_free(tmp_ctx
);
340 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s\n",
343 talloc_free(tmp_ctx
);
346 } else if (!W_ERROR_IS_OK(r_crack_names
.out
.result
)) {
349 "DsCrackNames failed - %s\n", win_errstr(r_crack_names
.out
.result
));
350 talloc_free(tmp_ctx
);
351 return NT_STATUS_UNSUCCESSFUL
;
352 } else if (r_crack_names
.out
.level
!= 1
353 || !r_crack_names
.out
.ctr
.ctr1
354 || r_crack_names
.out
.ctr
.ctr1
->count
!= 1
355 || !r_crack_names
.out
.ctr
.ctr1
->array
[0].result_name
356 || r_crack_names
.out
.ctr
.ctr1
->array
[0].status
!= DRSUAPI_DS_NAME_STATUS_OK
) {
357 r
->out
.error_string
= talloc_asprintf(r
, "DsCrackNames failed\n");
358 talloc_free(tmp_ctx
);
359 return NT_STATUS_UNSUCCESSFUL
;
362 /* Store the account DN. */
363 r
->out
.account_dn_str
= account_dn_str
;
364 talloc_steal(r
, account_dn_str
);
366 /* Store the domain DN. */
367 r
->out
.domain_dn_str
= r_crack_names
.out
.ctr
.ctr1
->array
[0].result_name
;
368 talloc_steal(r
, r_crack_names
.out
.ctr
.ctr1
->array
[0].result_name
);
372 if (r
->in
.acct_type
== ACB_SVRTRUST
) {
373 status
= libnet_JoinSite(remote_ldb
, r
);
375 talloc_free(tmp_ctx
);
381 * do a domain join using DCERPC/SAMR calls
382 * - connect to the LSA pipe, to try and find out information about the domain
383 * - create a secondary connection to SAMR pipe
384 * - do a samr_Connect to get a policy handle
385 * - do a samr_LookupDomain to get the domain sid
386 * - do a samr_OpenDomain to get a domain handle
387 * - do a samr_CreateAccount to try and get a new account
390 * - do a samr_LookupNames to get the users rid
391 * - do a samr_OpenUser to get a user handle
392 * - potentially delete and recreate the user
393 * - assert the account is of the right type with samrQueryUserInfo
395 * - call libnet_SetPassword_samr_handle to set the password
397 * - do a samrSetUserInfo to set the account flags
398 * - do some ADS specific things when we join as Domain Controller,
399 * look at libnet_joinADSDomain() for the details
401 NTSTATUS
libnet_JoinDomain(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
, struct libnet_JoinDomain
*r
)
405 NTSTATUS status
, cu_status
;
407 struct libnet_RpcConnect
*connect_with_info
;
408 struct dcerpc_pipe
*samr_pipe
;
410 struct samr_Connect sc
;
411 struct policy_handle p_handle
;
412 struct samr_OpenDomain od
;
413 struct policy_handle d_handle
;
414 struct samr_LookupNames ln
;
415 struct samr_OpenUser ou
;
416 struct samr_CreateUser2 cu
;
417 struct policy_handle
*u_handle
= NULL
;
418 struct samr_QueryUserInfo qui
;
419 struct samr_SetUserInfo sui
;
420 union samr_UserInfo u_info
;
421 union libnet_SetPassword r2
;
422 struct samr_GetUserPwInfo pwp
;
423 struct lsa_String samr_account_name
;
425 uint32_t acct_flags
, old_acct_flags
;
426 uint32_t rid
, access_granted
;
427 int policy_min_pw_len
= 0;
429 struct dom_sid
*account_sid
= NULL
;
430 const char *password_str
= NULL
;
432 r
->out
.error_string
= NULL
;
433 r2
.samr_handle
.out
.error_string
= NULL
;
435 tmp_ctx
= talloc_named(mem_ctx
, 0, "libnet_Join temp context");
437 r
->out
.error_string
= NULL
;
438 return NT_STATUS_NO_MEMORY
;
441 u_handle
= talloc(tmp_ctx
, struct policy_handle
);
443 r
->out
.error_string
= NULL
;
444 talloc_free(tmp_ctx
);
445 return NT_STATUS_NO_MEMORY
;
448 connect_with_info
= talloc(tmp_ctx
, struct libnet_RpcConnect
);
449 if (!connect_with_info
) {
450 r
->out
.error_string
= NULL
;
451 talloc_free(tmp_ctx
);
452 return NT_STATUS_NO_MEMORY
;
455 /* prepare connect to the LSA pipe of PDC */
456 if (r
->in
.level
== LIBNET_JOINDOMAIN_AUTOMATIC
) {
457 connect_with_info
->in
.name
= r
->in
.domain_name
;
459 connect_with_info
->in
.binding
= r
->in
.binding
;
462 connect_with_info
->level
= LIBNET_RPC_CONNECT_DC_INFO
;
463 connect_with_info
->in
.dcerpc_iface
= &dcerpc_table_samr
;
466 establish a SAMR connection, on the same CIFS transport
468 status
= libnet_RpcConnect(ctx
, tmp_ctx
, connect_with_info
);
469 if (!NT_STATUS_IS_OK(status
)) {
471 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
472 "Connection to SAMR pipe of DC %s failed: %s",
473 r
->in
.binding
, connect_with_info
->out
.error_string
);
475 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
476 "Connection to SAMR pipe of PDC for %s failed: %s",
477 r
->in
.domain_name
, connect_with_info
->out
.error_string
);
479 talloc_free(tmp_ctx
);
483 samr_pipe
= connect_with_info
->out
.dcerpc_pipe
;
485 status
= dcerpc_pipe_auth(tmp_ctx
, &samr_pipe
,
486 connect_with_info
->out
.dcerpc_pipe
->binding
,
487 &dcerpc_table_samr
, ctx
->cred
);
488 if (!NT_STATUS_IS_OK(status
)) {
489 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
490 "SAMR bind failed: %s",
492 talloc_free(tmp_ctx
);
496 /* prepare samr_Connect */
497 ZERO_STRUCT(p_handle
);
498 sc
.in
.system_name
= NULL
;
499 sc
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
500 sc
.out
.connect_handle
= &p_handle
;
502 /* 2. do a samr_Connect to get a policy handle */
503 status
= dcerpc_samr_Connect(samr_pipe
, tmp_ctx
, &sc
);
504 if (!NT_STATUS_IS_OK(status
)) {
505 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
506 "samr_Connect failed: %s",
508 talloc_free(tmp_ctx
);
512 /* If this is a connection on ncacn_ip_tcp to Win2k3 SP1, we don't get back this useful info */
513 if (!connect_with_info
->out
.domain_name
) {
514 if (r
->in
.level
== LIBNET_JOINDOMAIN_AUTOMATIC
) {
515 connect_with_info
->out
.domain_name
= talloc_strdup(tmp_ctx
, r
->in
.domain_name
);
517 /* Bugger, we just lost our way to automaticly find the domain name */
518 connect_with_info
->out
.domain_name
= talloc_strdup(tmp_ctx
, lp_workgroup());
522 /* Perhaps we didn't get a SID above, because we are against ncacn_ip_tcp */
523 if (!connect_with_info
->out
.domain_sid
) {
524 struct lsa_String name
;
525 struct samr_LookupDomain l
;
526 name
.string
= connect_with_info
->out
.domain_name
;
527 l
.in
.connect_handle
= &p_handle
;
528 l
.in
.domain_name
= &name
;
530 status
= dcerpc_samr_LookupDomain(samr_pipe
, tmp_ctx
, &l
);
531 if (!NT_STATUS_IS_OK(status
)) {
532 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
533 "SAMR LookupDomain failed: %s",
535 talloc_free(tmp_ctx
);
538 connect_with_info
->out
.domain_sid
= l
.out
.sid
;
541 /* prepare samr_OpenDomain */
542 ZERO_STRUCT(d_handle
);
543 od
.in
.connect_handle
= &p_handle
;
544 od
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
545 od
.in
.sid
= connect_with_info
->out
.domain_sid
;
546 od
.out
.domain_handle
= &d_handle
;
548 /* do a samr_OpenDomain to get a domain handle */
549 status
= dcerpc_samr_OpenDomain(samr_pipe
, tmp_ctx
, &od
);
550 if (!NT_STATUS_IS_OK(status
)) {
551 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
552 "samr_OpenDomain for [%s] failed: %s",
553 dom_sid_string(tmp_ctx
, connect_with_info
->out
.domain_sid
),
555 talloc_free(tmp_ctx
);
559 /* prepare samr_CreateUser2 */
560 ZERO_STRUCTP(u_handle
);
561 cu
.in
.domain_handle
= &d_handle
;
562 cu
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
563 samr_account_name
.string
= r
->in
.account_name
;
564 cu
.in
.account_name
= &samr_account_name
;
565 cu
.in
.acct_flags
= r
->in
.acct_type
;
566 cu
.out
.user_handle
= u_handle
;
568 cu
.out
.access_granted
= &access_granted
;
570 /* do a samr_CreateUser2 to get an account handle, or an error */
571 cu_status
= dcerpc_samr_CreateUser2(samr_pipe
, tmp_ctx
, &cu
);
573 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
574 /* prepare samr_LookupNames */
575 ln
.in
.domain_handle
= &d_handle
;
577 ln
.in
.names
= talloc_array(tmp_ctx
, struct lsa_String
, 1);
579 r
->out
.error_string
= NULL
;
580 talloc_free(tmp_ctx
);
581 return NT_STATUS_NO_MEMORY
;
583 ln
.in
.names
[0].string
= r
->in
.account_name
;
585 /* 5. do a samr_LookupNames to get the users rid */
586 status
= dcerpc_samr_LookupNames(samr_pipe
, tmp_ctx
, &ln
);
587 if (!NT_STATUS_IS_OK(status
)) {
588 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
589 "samr_LookupNames for [%s] failed: %s",
592 talloc_free(tmp_ctx
);
596 /* check if we got one RID for the user */
597 if (ln
.out
.rids
.count
!= 1) {
598 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
599 "samr_LookupNames for [%s] returns %d RIDs\n",
600 r
->in
.account_name
, ln
.out
.rids
.count
);
601 talloc_free(tmp_ctx
);
602 return NT_STATUS_INVALID_PARAMETER
;
605 /* prepare samr_OpenUser */
606 ZERO_STRUCTP(u_handle
);
607 ou
.in
.domain_handle
= &d_handle
;
608 ou
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
609 ou
.in
.rid
= ln
.out
.rids
.ids
[0];
611 ou
.out
.user_handle
= u_handle
;
613 /* 6. do a samr_OpenUser to get a user handle */
614 status
= dcerpc_samr_OpenUser(samr_pipe
, tmp_ctx
, &ou
);
615 if (!NT_STATUS_IS_OK(status
)) {
616 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
617 "samr_OpenUser for [%s] failed: %s",
620 talloc_free(tmp_ctx
);
624 if (r
->in
.recreate_account
) {
625 struct samr_DeleteUser d
;
626 d
.in
.user_handle
= u_handle
;
627 d
.out
.user_handle
= u_handle
;
628 status
= dcerpc_samr_DeleteUser(samr_pipe
, mem_ctx
, &d
);
629 if (!NT_STATUS_IS_OK(status
)) {
630 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
631 "samr_DeleteUser (for recreate) of [%s] failed: %s",
634 talloc_free(tmp_ctx
);
638 /* We want to recreate, so delete and another samr_CreateUser2 */
640 /* &cu filled in above */
641 status
= dcerpc_samr_CreateUser2(samr_pipe
, tmp_ctx
, &cu
);
642 if (!NT_STATUS_IS_OK(status
)) {
643 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
644 "samr_CreateUser2 (recreate) for [%s] failed: %s\n",
645 r
->in
.domain_name
, nt_errstr(status
));
646 talloc_free(tmp_ctx
);
650 } else if (!NT_STATUS_IS_OK(status
)) {
651 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
652 "samr_CreateUser2 for [%s] failed: %s\n",
653 r
->in
.domain_name
, nt_errstr(status
));
654 talloc_free(tmp_ctx
);
658 /* prepare samr_QueryUserInfo (get flags) */
659 qui
.in
.user_handle
= u_handle
;
662 status
= dcerpc_samr_QueryUserInfo(samr_pipe
, tmp_ctx
, &qui
);
663 if (!NT_STATUS_IS_OK(status
)) {
664 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
665 "samr_QueryUserInfo for [%s] failed: %s",
668 talloc_free(tmp_ctx
);
673 status
= NT_STATUS_INVALID_PARAMETER
;
675 = talloc_asprintf(mem_ctx
,
676 "samr_QueryUserInfo failed to return qui.out.info for [%s]: %s\n",
677 r
->in
.account_name
, nt_errstr(status
));
678 talloc_free(tmp_ctx
);
682 old_acct_flags
= (qui
.out
.info
->info16
.acct_flags
& (ACB_WSTRUST
| ACB_SVRTRUST
| ACB_DOMTRUST
));
683 /* Possibly bail if the account is of the wrong type */
685 != r
->in
.acct_type
) {
686 const char *old_account_type
, *new_account_type
;
687 switch (old_acct_flags
) {
689 old_account_type
= "domain member (member)";
692 old_account_type
= "domain controller (bdc)";
695 old_account_type
= "trusted domain";
698 return NT_STATUS_INVALID_PARAMETER
;
700 switch (r
->in
.acct_type
) {
702 new_account_type
= "domain member (member)";
705 new_account_type
= "domain controller (bdc)";
708 new_account_type
= "trusted domain";
711 return NT_STATUS_INVALID_PARAMETER
;
714 if (!NT_STATUS_EQUAL(cu_status
, NT_STATUS_USER_EXISTS
)) {
715 /* We created a new user, but they didn't come out the right type?!? */
717 = talloc_asprintf(mem_ctx
,
718 "We asked to create a new machine account (%s) of type %s, "
719 "but we got an account of type %s. This is unexpected. "
720 "Perhaps delete the account and try again.\n",
721 r
->in
.account_name
, new_account_type
, old_account_type
);
722 talloc_free(tmp_ctx
);
723 return NT_STATUS_INVALID_PARAMETER
;
725 /* The account is of the wrong type, so bail */
727 /* TODO: We should allow a --force option to override, and redo this from the top setting r.in.recreate_account */
729 = talloc_asprintf(mem_ctx
,
730 "The machine account (%s) already exists in the domain %s, "
731 "but is a %s. You asked to join as a %s. Please delete "
732 "the account and try again.\n",
733 r
->in
.account_name
, connect_with_info
->out
.domain_name
, old_account_type
, new_account_type
);
734 talloc_free(tmp_ctx
);
735 return NT_STATUS_USER_EXISTS
;
738 acct_flags
= qui
.out
.info
->info16
.acct_flags
;
741 acct_flags
= (acct_flags
& ~ACB_DISABLED
);
743 /* Find out what password policy this user has */
744 pwp
.in
.user_handle
= u_handle
;
746 status
= dcerpc_samr_GetUserPwInfo(samr_pipe
, tmp_ctx
, &pwp
);
747 if (NT_STATUS_IS_OK(status
)) {
748 policy_min_pw_len
= pwp
.out
.info
.min_password_length
;
751 /* Grab a password of that minimum length */
753 password_str
= generate_random_str(tmp_ctx
, MAX(8, policy_min_pw_len
));
755 r2
.samr_handle
.level
= LIBNET_SET_PASSWORD_SAMR_HANDLE
;
756 r2
.samr_handle
.in
.account_name
= r
->in
.account_name
;
757 r2
.samr_handle
.in
.newpassword
= password_str
;
758 r2
.samr_handle
.in
.user_handle
= u_handle
;
759 r2
.samr_handle
.in
.dcerpc_pipe
= samr_pipe
;
761 status
= libnet_SetPassword(ctx
, tmp_ctx
, &r2
);
762 if (!NT_STATUS_IS_OK(status
)) {
763 r
->out
.error_string
= talloc_steal(mem_ctx
, r2
.samr_handle
.out
.error_string
);
764 talloc_free(tmp_ctx
);
768 /* reset flags (if required) */
769 if (acct_flags
!= qui
.out
.info
->info16
.acct_flags
) {
771 u_info
.info16
.acct_flags
= acct_flags
;
773 sui
.in
.user_handle
= u_handle
;
774 sui
.in
.info
= &u_info
;
777 dcerpc_samr_SetUserInfo(samr_pipe
, tmp_ctx
, &sui
);
778 if (!NT_STATUS_IS_OK(status
)) {
779 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
780 "samr_SetUserInfo for [%s] failed to remove ACB_DISABLED flag: %s",
783 talloc_free(tmp_ctx
);
788 account_sid
= dom_sid_add_rid(mem_ctx
, connect_with_info
->out
.domain_sid
, rid
);
790 r
->out
.error_string
= NULL
;
791 talloc_free(tmp_ctx
);
792 return NT_STATUS_NO_MEMORY
;
795 /* Finish out by pushing various bits of status data out for the caller to use */
796 r
->out
.join_password
= password_str
;
797 talloc_steal(mem_ctx
, r
->out
.join_password
);
799 r
->out
.domain_sid
= connect_with_info
->out
.domain_sid
;
800 talloc_steal(mem_ctx
, r
->out
.domain_sid
);
802 r
->out
.account_sid
= account_sid
;
803 talloc_steal(mem_ctx
, r
->out
.account_sid
);
805 r
->out
.domain_name
= connect_with_info
->out
.domain_name
;
806 talloc_steal(mem_ctx
, r
->out
.domain_name
);
807 r
->out
.realm
= connect_with_info
->out
.realm
;
808 talloc_steal(mem_ctx
, r
->out
.realm
);
809 r
->out
.samr_pipe
= samr_pipe
;
810 talloc_steal(mem_ctx
, samr_pipe
);
811 r
->out
.samr_binding
= samr_pipe
->binding
;
812 talloc_steal(mem_ctx
, r
->out
.samr_binding
);
813 r
->out
.user_handle
= u_handle
;
814 talloc_steal(mem_ctx
, u_handle
);
815 r
->out
.error_string
= r2
.samr_handle
.out
.error_string
;
816 talloc_steal(mem_ctx
, r2
.samr_handle
.out
.error_string
);
818 r
->out
.server_dn_str
= NULL
;
819 talloc_free(tmp_ctx
);
821 /* Now, if it was AD, then we want to start looking changing a
822 * few more things. Otherwise, we are done. */
824 status
= libnet_JoinADSDomain(ctx
, r
);
831 static NTSTATUS
libnet_Join_primary_domain(struct libnet_context
*ctx
,
833 struct libnet_Join
*r
)
837 struct libnet_JoinDomain
*r2
;
839 struct ldb_context
*ldb
;
840 const struct ldb_dn
*base_dn
;
841 struct ldb_message
**msgs
, *msg
;
843 const char * const attrs
[] = {
852 uint32_t acct_type
= 0;
853 const char *account_name
;
854 const char *netbios_name
;
857 r
->out
.error_string
= NULL
;
859 tmp_mem
= talloc_new(mem_ctx
);
861 return NT_STATUS_NO_MEMORY
;
864 r2
= talloc(tmp_mem
, struct libnet_JoinDomain
);
866 r
->out
.error_string
= NULL
;
867 talloc_free(tmp_mem
);
868 return NT_STATUS_NO_MEMORY
;
871 if (r
->in
.join_type
== SEC_CHAN_BDC
) {
872 acct_type
= ACB_SVRTRUST
;
873 } else if (r
->in
.join_type
== SEC_CHAN_WKSTA
) {
874 acct_type
= ACB_WSTRUST
;
876 r
->out
.error_string
= NULL
;
877 talloc_free(tmp_mem
);
878 return NT_STATUS_INVALID_PARAMETER
;
881 if (r
->in
.netbios_name
!= NULL
) {
882 netbios_name
= r
->in
.netbios_name
;
884 netbios_name
= talloc_reference(tmp_mem
, lp_netbios_name());
886 r
->out
.error_string
= NULL
;
887 talloc_free(tmp_mem
);
888 return NT_STATUS_NO_MEMORY
;
892 account_name
= talloc_asprintf(tmp_mem
, "%s$", netbios_name
);
894 r
->out
.error_string
= NULL
;
895 talloc_free(tmp_mem
);
896 return NT_STATUS_NO_MEMORY
;
900 * Local secrets are stored in secrets.ldb
901 * open it to make sure we can write the info into it after the join
903 ldb
= secrets_db_connect(tmp_mem
);
906 = talloc_asprintf(mem_ctx
,
907 "Could not open secrets database\n");
908 talloc_free(tmp_mem
);
909 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
916 r2
->in
.domain_name
= r
->in
.domain_name
;
917 r2
->in
.account_name
= account_name
;
918 r2
->in
.netbios_name
= netbios_name
;
919 r2
->in
.level
= LIBNET_JOINDOMAIN_AUTOMATIC
;
920 r2
->in
.acct_type
= acct_type
;
921 r2
->in
.recreate_account
= False
;
922 status
= libnet_JoinDomain(ctx
, r2
, r2
);
923 if (!NT_STATUS_IS_OK(status
)) {
924 r
->out
.error_string
= talloc_steal(mem_ctx
, r2
->out
.error_string
);
925 talloc_free(tmp_mem
);
930 * now prepare the record for secrets.ldb
932 sct
= talloc_asprintf(tmp_mem
, "%d", r
->in
.join_type
);
934 r
->out
.error_string
= NULL
;
935 talloc_free(tmp_mem
);
936 return NT_STATUS_NO_MEMORY
;
939 msg
= ldb_msg_new(tmp_mem
);
941 r
->out
.error_string
= NULL
;
942 talloc_free(tmp_mem
);
943 return NT_STATUS_NO_MEMORY
;
946 base_dn
= ldb_dn_explode(tmp_mem
, "cn=Primary Domains");
948 r
->out
.error_string
= NULL
;
949 talloc_free(tmp_mem
);
950 return NT_STATUS_NO_MEMORY
;
953 msg
->dn
= ldb_dn_build_child(tmp_mem
, "flatname", r2
->out
.domain_name
, base_dn
);
955 r
->out
.error_string
= NULL
;
956 talloc_free(tmp_mem
);
957 return NT_STATUS_NO_MEMORY
;
960 rtn
= samdb_msg_add_string(ldb
, tmp_mem
, msg
, "flatname", r2
->out
.domain_name
);
962 r
->out
.error_string
= NULL
;
963 talloc_free(tmp_mem
);
964 return NT_STATUS_NO_MEMORY
;
968 rtn
= samdb_msg_add_string(ldb
, tmp_mem
, msg
, "realm", r2
->out
.realm
);
970 r
->out
.error_string
= NULL
;
971 talloc_free(tmp_mem
);
972 return NT_STATUS_NO_MEMORY
;
975 rtn
= samdb_msg_add_string(ldb
, tmp_mem
, msg
, "objectClass", "primaryDomain");
977 r
->out
.error_string
= NULL
;
978 talloc_free(tmp_mem
);
979 return NT_STATUS_NO_MEMORY
;
983 rtn
= samdb_msg_add_string(ldb
, tmp_mem
, msg
, "objectClass", "primaryDomain");
985 r
->out
.error_string
= NULL
;
986 talloc_free(tmp_mem
);
987 return NT_STATUS_NO_MEMORY
;
990 rtn
= samdb_msg_add_string(ldb
, tmp_mem
, msg
, "secret", r2
->out
.join_password
);
992 r
->out
.error_string
= NULL
;
993 talloc_free(tmp_mem
);
994 return NT_STATUS_NO_MEMORY
;
997 rtn
= samdb_msg_add_string(ldb
, tmp_mem
, msg
, "samAccountName", r2
->in
.account_name
);
999 r
->out
.error_string
= NULL
;
1000 talloc_free(tmp_mem
);
1001 return NT_STATUS_NO_MEMORY
;
1004 rtn
= samdb_msg_add_string(ldb
, tmp_mem
, msg
, "secureChannelType", sct
);
1006 r
->out
.error_string
= NULL
;
1007 talloc_free(tmp_mem
);
1008 return NT_STATUS_NO_MEMORY
;
1012 rtn
= samdb_msg_add_uint(ldb
, tmp_mem
, msg
, "msDS-KeyVersionNumber",
1015 r
->out
.error_string
= NULL
;
1016 talloc_free(tmp_mem
);
1017 return NT_STATUS_NO_MEMORY
;
1021 if (r2
->out
.domain_sid
) {
1022 rtn
= samdb_msg_add_dom_sid(ldb
, tmp_mem
, msg
, "objectSid",
1023 r2
->out
.domain_sid
);
1025 r
->out
.error_string
= NULL
;
1026 talloc_free(tmp_mem
);
1027 return NT_STATUS_NO_MEMORY
;
1032 * search for the secret record
1033 * - remove the records we find
1034 * - and fetch the old secret and store it under priorSecret
1036 ret
= gendb_search(ldb
,
1039 "(|" SECRETS_PRIMARY_DOMAIN_FILTER
"(realm=%s))",
1040 r2
->out
.domain_name
, r2
->out
.realm
);
1042 rtn
= samdb_msg_set_string(ldb
, tmp_mem
, msg
, "secretsKeytab", "secrets.keytab");
1044 r
->out
.error_string
= NULL
;
1045 talloc_free(tmp_mem
);
1046 return NT_STATUS_NO_MEMORY
;
1048 } else if (ret
== -1) {
1050 = talloc_asprintf(mem_ctx
,
1051 "Search for domain: %s and realm: %s failed: %s",
1052 r2
->out
.domain_name
, r2
->out
.realm
, ldb_errstring(ldb
));
1053 talloc_free(tmp_mem
);
1054 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
1056 const struct ldb_val
*private_keytab
;
1057 const struct ldb_val
*krb5_keytab
;
1058 const struct ldb_val
*prior_secret
;
1059 const struct ldb_val
*prior_modified_time
;
1062 for (i
= 0; i
< ret
; i
++) {
1063 ldb_delete(ldb
, msgs
[i
]->dn
);
1066 prior_secret
= ldb_msg_find_ldb_val(msgs
[0], "secret");
1068 rtn
= samdb_msg_set_value(ldb
, tmp_mem
, msg
, "priorSecret", prior_secret
);
1070 r
->out
.error_string
= NULL
;
1071 talloc_free(tmp_mem
);
1072 return NT_STATUS_NO_MEMORY
;
1075 rtn
= samdb_msg_set_string(ldb
, tmp_mem
, msg
, "secret", r2
->out
.join_password
);
1077 r
->out
.error_string
= NULL
;
1078 talloc_free(tmp_mem
);
1079 return NT_STATUS_NO_MEMORY
;
1082 prior_modified_time
= ldb_msg_find_ldb_val(msgs
[0],
1084 if (prior_modified_time
) {
1085 rtn
= samdb_msg_set_value(ldb
, tmp_mem
, msg
, "priorWhenChanged",
1086 prior_modified_time
);
1088 r
->out
.error_string
= NULL
;
1089 talloc_free(tmp_mem
);
1090 return NT_STATUS_NO_MEMORY
;
1094 rtn
= samdb_msg_set_string(ldb
, tmp_mem
, msg
, "samAccountName", r2
->in
.account_name
);
1096 r
->out
.error_string
= NULL
;
1097 talloc_free(tmp_mem
);
1098 return NT_STATUS_NO_MEMORY
;
1101 rtn
= samdb_msg_set_string(ldb
, tmp_mem
, msg
, "secureChannelType", sct
);
1103 r
->out
.error_string
= NULL
;
1104 talloc_free(tmp_mem
);
1105 return NT_STATUS_NO_MEMORY
;
1108 /* We will want to keep the keytab names */
1109 private_keytab
= ldb_msg_find_ldb_val(msgs
[0], "privateKeytab");
1110 if (private_keytab
) {
1111 rtn
= samdb_msg_set_value(ldb
, tmp_mem
, msg
, "privateKeytab", private_keytab
);
1113 r
->out
.error_string
= NULL
;
1114 talloc_free(tmp_mem
);
1115 return NT_STATUS_NO_MEMORY
;
1118 krb5_keytab
= ldb_msg_find_ldb_val(msgs
[0], "krb5Keytab");
1120 rtn
= samdb_msg_set_value(ldb
, tmp_mem
, msg
, "krb5Keytab", krb5_keytab
);
1122 r
->out
.error_string
= NULL
;
1123 talloc_free(tmp_mem
);
1124 return NT_STATUS_NO_MEMORY
;
1129 /* create the secret */
1130 ret
= samdb_add(ldb
, tmp_mem
, msg
);
1132 r
->out
.error_string
= talloc_asprintf(mem_ctx
, "Failed to create secret record %s\n",
1133 ldb_dn_linearize(ldb
, msg
->dn
));
1134 talloc_free(tmp_mem
);
1135 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
1138 if (r2
->out
.realm
) {
1139 struct cli_credentials
*creds
;
1140 /* Make a credentials structure from it */
1141 creds
= cli_credentials_init(mem_ctx
);
1143 r
->out
.error_string
= NULL
;
1144 talloc_free(tmp_mem
);
1145 return NT_STATUS_NO_MEMORY
;
1147 cli_credentials_set_conf(creds
);
1148 filter
= talloc_asprintf(mem_ctx
, "dn=%s", ldb_dn_linearize(mem_ctx
, msg
->dn
));
1149 status
= cli_credentials_set_secrets(creds
, NULL
, filter
);
1150 if (!NT_STATUS_IS_OK(status
)) {
1151 r
->out
.error_string
= talloc_asprintf(mem_ctx
, "Failed to read secrets for keytab update for %s\n",
1153 talloc_free(tmp_mem
);
1156 ret
= cli_credentials_update_keytab(creds
);
1158 r
->out
.error_string
= talloc_asprintf(mem_ctx
, "Failed to update keytab for %s\n",
1160 talloc_free(tmp_mem
);
1161 return NT_STATUS_UNSUCCESSFUL
;
1165 /* move all out parameter to the callers TALLOC_CTX */
1166 r
->out
.error_string
= NULL
;
1167 r
->out
.join_password
= r2
->out
.join_password
;
1168 talloc_steal(mem_ctx
, r2
->out
.join_password
);
1169 r
->out
.domain_sid
= r2
->out
.domain_sid
;
1170 talloc_steal(mem_ctx
, r2
->out
.domain_sid
);
1171 r
->out
.domain_name
= r2
->out
.domain_name
;
1172 talloc_steal(mem_ctx
, r2
->out
.domain_name
);
1173 talloc_free(tmp_mem
);
1174 return NT_STATUS_OK
;
1177 NTSTATUS
libnet_Join(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
, struct libnet_Join
*r
)
1179 switch (r
->in
.join_type
) {
1180 case SEC_CHAN_WKSTA
:
1181 return libnet_Join_primary_domain(ctx
, mem_ctx
, r
);
1183 return libnet_Join_primary_domain(ctx
, mem_ctx
, r
);
1184 case SEC_CHAN_DOMAIN
:
1188 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
1189 "Invalid join type specified (%08X) attempting to join domain %s",
1190 r
->in
.join_type
, r
->in
.domain_name
);
1191 return NT_STATUS_INVALID_PARAMETER
;