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 3 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, see <http://www.gnu.org/licenses/>.
23 #include "libnet/libnet.h"
24 #include "librpc/gen_ndr/ndr_drsuapi_c.h"
26 #include <ldb_errors.h>
27 #include "dsdb/samdb/samdb.h"
29 #include "libcli/security/security.h"
30 #include "auth/credentials/credentials.h"
31 #include "auth/credentials/credentials_krb5.h"
32 #include "librpc/gen_ndr/ndr_samr_c.h"
33 #include "param/param.h"
34 #include "param/provision.h"
35 #include "system/kerberos.h"
36 #include "auth/kerberos/kerberos.h"
39 * complete a domain join, when joining to a AD domain:
40 * 1.) connect and bind to the DRSUAPI pipe
41 * 2.) do a DsCrackNames() to find the machine account dn
43 * 4.) do an ldap search to find the "msDS-KeyVersionNumber" of the machine account
44 * 5.) set the servicePrincipalName's of the machine account via LDAP, (maybe we should use DsWriteAccountSpn()...)
45 * 6.) do a DsCrackNames() to find the domain dn
46 * 7.) find out Site specific stuff, look at libnet_JoinSite() for details
48 static NTSTATUS
libnet_JoinADSDomain(struct libnet_context
*ctx
, struct libnet_JoinDomain
*r
)
54 const char *realm
= r
->out
.realm
;
56 const struct dcerpc_binding
*samr_binding
= r
->out
.samr_binding
;
58 struct dcerpc_pipe
*drsuapi_pipe
;
59 struct dcerpc_binding
*drsuapi_binding
;
60 enum dcerpc_transport_t transport
;
61 struct drsuapi_DsBind r_drsuapi_bind
;
62 struct drsuapi_DsCrackNames r_crack_names
;
63 struct drsuapi_DsNameString names
[1];
64 struct policy_handle drsuapi_bind_handle
;
65 struct GUID drsuapi_bind_guid
;
67 struct ldb_context
*remote_ldb
;
68 struct ldb_dn
*account_dn
;
69 const char *account_dn_str
;
70 const char *remote_ldb_url
;
71 struct ldb_result
*res
;
72 struct ldb_message
*msg
;
76 const char * const attrs
[] = {
77 "msDS-KeyVersionNumber",
78 "servicePrincipalName",
84 r
->out
.error_string
= NULL
;
86 /* We need to convert between a samAccountName and domain to a
87 * DN in the directory. The correct way to do this is with
88 * DRSUAPI CrackNames */
90 /* Fiddle with the bindings, so get to DRSUAPI on
91 * NCACN_IP_TCP, sealed */
92 tmp_ctx
= talloc_named(r
, 0, "libnet_JoinADSDomain temp context");
94 r
->out
.error_string
= NULL
;
95 return NT_STATUS_NO_MEMORY
;
98 drsuapi_binding
= dcerpc_binding_dup(tmp_ctx
, samr_binding
);
99 if (!drsuapi_binding
) {
100 r
->out
.error_string
= NULL
;
101 talloc_free(tmp_ctx
);
102 return NT_STATUS_NO_MEMORY
;
105 transport
= dcerpc_binding_get_transport(drsuapi_binding
);
107 /* DRSUAPI is only available on IP_TCP, and locally on NCALRPC */
108 if (transport
!= NCALRPC
) {
109 status
= dcerpc_binding_set_transport(drsuapi_binding
, NCACN_IP_TCP
);
110 if (!NT_STATUS_IS_OK(status
)) {
111 r
->out
.error_string
= talloc_asprintf(r
,
112 "dcerpc_binding_set_transport failed: %s",
114 talloc_free(tmp_ctx
);
119 status
= dcerpc_binding_set_string_option(drsuapi_binding
, "endpoint", NULL
);
120 if (!NT_STATUS_IS_OK(status
)) {
121 r
->out
.error_string
= talloc_asprintf(r
,
122 "dcerpc_binding_set_string_option failed: %s",
124 talloc_free(tmp_ctx
);
128 status
= dcerpc_binding_set_flags(drsuapi_binding
, DCERPC_SEAL
, 0);
129 if (!NT_STATUS_IS_OK(status
)) {
130 r
->out
.error_string
= talloc_asprintf(r
,
131 "dcerpc_binding_set_flags failed: %s",
133 talloc_free(tmp_ctx
);
137 status
= dcerpc_pipe_connect_b(tmp_ctx
,
144 if (!NT_STATUS_IS_OK(status
)) {
145 r
->out
.error_string
= talloc_asprintf(r
,
146 "Connection to DRSUAPI pipe of PDC of domain '%s' failed: %s",
149 talloc_free(tmp_ctx
);
153 /* get a DRSUAPI pipe handle */
154 GUID_from_string(DRSUAPI_DS_BIND_GUID
, &drsuapi_bind_guid
);
156 r_drsuapi_bind
.in
.bind_guid
= &drsuapi_bind_guid
;
157 r_drsuapi_bind
.in
.bind_info
= NULL
;
158 r_drsuapi_bind
.out
.bind_handle
= &drsuapi_bind_handle
;
160 status
= dcerpc_drsuapi_DsBind_r(drsuapi_pipe
->binding_handle
, tmp_ctx
, &r_drsuapi_bind
);
161 if (!NT_STATUS_IS_OK(status
)) {
164 "dcerpc_drsuapi_DsBind failed - %s",
166 talloc_free(tmp_ctx
);
168 } else if (!W_ERROR_IS_OK(r_drsuapi_bind
.out
.result
)) {
171 "DsBind failed - %s",
172 win_errstr(r_drsuapi_bind
.out
.result
));
173 talloc_free(tmp_ctx
);
174 return NT_STATUS_UNSUCCESSFUL
;
177 /* Actually 'crack' the names */
178 ZERO_STRUCT(r_crack_names
);
179 r_crack_names
.in
.bind_handle
= &drsuapi_bind_handle
;
180 r_crack_names
.in
.level
= 1;
181 r_crack_names
.in
.req
= talloc(r
, union drsuapi_DsNameRequest
);
182 if (!r_crack_names
.in
.req
) {
183 r
->out
.error_string
= NULL
;
184 talloc_free(tmp_ctx
);
185 return NT_STATUS_NO_MEMORY
;
187 r_crack_names
.in
.req
->req1
.codepage
= 1252; /* western european */
188 r_crack_names
.in
.req
->req1
.language
= 0x00000407; /* german */
189 r_crack_names
.in
.req
->req1
.count
= 1;
190 r_crack_names
.in
.req
->req1
.names
= names
;
191 r_crack_names
.in
.req
->req1
.format_flags
= DRSUAPI_DS_NAME_FLAG_NO_FLAGS
;
192 r_crack_names
.in
.req
->req1
.format_offered
= DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY
;
193 r_crack_names
.in
.req
->req1
.format_desired
= DRSUAPI_DS_NAME_FORMAT_FQDN_1779
;
194 names
[0].str
= dom_sid_string(tmp_ctx
, r
->out
.account_sid
);
196 r
->out
.error_string
= NULL
;
197 talloc_free(tmp_ctx
);
198 return NT_STATUS_NO_MEMORY
;
201 r_crack_names
.out
.ctr
= talloc(r
, union drsuapi_DsNameCtr
);
202 r_crack_names
.out
.level_out
= talloc(r
, uint32_t);
203 if (!r_crack_names
.out
.ctr
|| !r_crack_names
.out
.level_out
) {
204 r
->out
.error_string
= NULL
;
205 talloc_free(tmp_ctx
);
206 return NT_STATUS_NO_MEMORY
;
209 status
= dcerpc_drsuapi_DsCrackNames_r(drsuapi_pipe
->binding_handle
, tmp_ctx
, &r_crack_names
);
210 if (!NT_STATUS_IS_OK(status
)) {
213 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s",
216 talloc_free(tmp_ctx
);
218 } else if (!W_ERROR_IS_OK(r_crack_names
.out
.result
)) {
221 "DsCrackNames failed - %s", win_errstr(r_crack_names
.out
.result
));
222 talloc_free(tmp_ctx
);
223 return NT_STATUS_UNSUCCESSFUL
;
224 } else if (*r_crack_names
.out
.level_out
!= 1
225 || !r_crack_names
.out
.ctr
->ctr1
226 || r_crack_names
.out
.ctr
->ctr1
->count
!= 1) {
227 r
->out
.error_string
= talloc_asprintf(r
, "DsCrackNames failed");
228 talloc_free(tmp_ctx
);
229 return NT_STATUS_INVALID_PARAMETER
;
230 } else if (r_crack_names
.out
.ctr
->ctr1
->array
[0].status
!= DRSUAPI_DS_NAME_STATUS_OK
) {
231 r
->out
.error_string
= talloc_asprintf(r
, "DsCrackNames failed: %d", r_crack_names
.out
.ctr
->ctr1
->array
[0].status
);
232 talloc_free(tmp_ctx
);
233 return NT_STATUS_UNSUCCESSFUL
;
234 } else if (r_crack_names
.out
.ctr
->ctr1
->array
[0].result_name
== NULL
) {
235 r
->out
.error_string
= talloc_asprintf(r
, "DsCrackNames failed: no result name");
236 talloc_free(tmp_ctx
);
237 return NT_STATUS_INVALID_PARAMETER
;
240 /* Store the DN of our machine account. */
241 account_dn_str
= r_crack_names
.out
.ctr
->ctr1
->array
[0].result_name
;
243 /* Now we know the user's DN, open with LDAP, read and modify a few things */
245 remote_ldb_url
= talloc_asprintf(tmp_ctx
, "ldap://%s",
246 dcerpc_binding_get_string_option(drsuapi_binding
, "target_hostname"));
247 if (!remote_ldb_url
) {
248 r
->out
.error_string
= NULL
;
249 talloc_free(tmp_ctx
);
250 return NT_STATUS_NO_MEMORY
;
253 remote_ldb
= ldb_wrap_connect(tmp_ctx
, ctx
->event_ctx
, ctx
->lp_ctx
,
257 r
->out
.error_string
= NULL
;
258 talloc_free(tmp_ctx
);
259 return NT_STATUS_UNSUCCESSFUL
;
262 account_dn
= ldb_dn_new(tmp_ctx
, remote_ldb
, account_dn_str
);
263 if (account_dn
== NULL
) {
264 r
->out
.error_string
= talloc_asprintf(r
, "Invalid account dn: %s",
266 talloc_free(tmp_ctx
);
267 return NT_STATUS_UNSUCCESSFUL
;
270 /* search for the user's record */
271 ret
= ldb_search(remote_ldb
, tmp_ctx
, &res
,
272 account_dn
, LDB_SCOPE_BASE
, attrs
, NULL
);
273 if (ret
!= LDB_SUCCESS
) {
274 r
->out
.error_string
= talloc_asprintf(r
, "ldb_search for %s failed - %s",
275 account_dn_str
, ldb_errstring(remote_ldb
));
276 talloc_free(tmp_ctx
);
277 return NT_STATUS_UNSUCCESSFUL
;
280 if (res
->count
!= 1) {
281 r
->out
.error_string
= talloc_asprintf(r
, "ldb_search for %s failed - found %d entries",
282 account_dn_str
, res
->count
);
283 talloc_free(tmp_ctx
);
284 return NT_STATUS_UNSUCCESSFUL
;
287 /* Prepare a new message, for the modify */
288 msg
= ldb_msg_new(tmp_ctx
);
290 r
->out
.error_string
= NULL
;
291 talloc_free(tmp_ctx
);
292 return NT_STATUS_NO_MEMORY
;
294 msg
->dn
= res
->msgs
[0]->dn
;
298 const char *service_principal_name
[2];
299 const char *dns_host_name
= strlower_talloc(msg
,
305 if (!dns_host_name
) {
306 r
->out
.error_string
= NULL
;
307 talloc_free(tmp_ctx
);
308 return NT_STATUS_NO_MEMORY
;
311 service_principal_name
[0] = talloc_asprintf(msg
, "HOST/%s",
313 service_principal_name
[1] = talloc_asprintf(msg
, "HOST/%s",
316 for (i
=0; i
< ARRAY_SIZE(service_principal_name
); i
++) {
317 if (!service_principal_name
[i
]) {
318 r
->out
.error_string
= NULL
;
319 talloc_free(tmp_ctx
);
320 return NT_STATUS_NO_MEMORY
;
322 rtn
= ldb_msg_add_string(msg
, "servicePrincipalName",
323 service_principal_name
[i
]);
324 if (rtn
!= LDB_SUCCESS
) {
325 r
->out
.error_string
= NULL
;
326 talloc_free(tmp_ctx
);
327 return NT_STATUS_NO_MEMORY
;
331 rtn
= ldb_msg_add_string(msg
, "dNSHostName", dns_host_name
);
332 if (rtn
!= LDB_SUCCESS
) {
333 r
->out
.error_string
= NULL
;
334 talloc_free(tmp_ctx
);
335 return NT_STATUS_NO_MEMORY
;
338 rtn
= dsdb_replace(remote_ldb
, msg
, 0);
339 if (rtn
!= LDB_SUCCESS
) {
342 "Failed to replace entries on %s",
343 ldb_dn_get_linearized(msg
->dn
));
344 talloc_free(tmp_ctx
);
345 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
349 msg
= ldb_msg_new(tmp_ctx
);
351 r
->out
.error_string
= NULL
;
352 talloc_free(tmp_ctx
);
353 return NT_STATUS_NO_MEMORY
;
355 msg
->dn
= res
->msgs
[0]->dn
;
357 rtn
= samdb_msg_add_uint(remote_ldb
, msg
, msg
,
358 "msDS-SupportedEncryptionTypes", ENC_ALL_TYPES
);
359 if (rtn
!= LDB_SUCCESS
) {
360 r
->out
.error_string
= NULL
;
361 talloc_free(tmp_ctx
);
362 return NT_STATUS_NO_MEMORY
;
365 rtn
= dsdb_replace(remote_ldb
, msg
, 0);
366 /* The remote server may not support this attribute, if it
367 * isn't a modern schema */
368 if (rtn
!= LDB_SUCCESS
&& rtn
!= LDB_ERR_NO_SUCH_ATTRIBUTE
) {
371 "Failed to replace msDS-SupportedEncryptionTypes on %s",
372 ldb_dn_get_linearized(msg
->dn
));
373 talloc_free(tmp_ctx
);
374 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
377 /* DsCrackNames to find out the DN of the domain. */
378 r_crack_names
.in
.req
->req1
.format_offered
= DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT
;
379 r_crack_names
.in
.req
->req1
.format_desired
= DRSUAPI_DS_NAME_FORMAT_FQDN_1779
;
380 names
[0].str
= talloc_asprintf(tmp_ctx
, "%s\\", r
->out
.domain_name
);
382 r
->out
.error_string
= NULL
;
383 talloc_free(tmp_ctx
);
384 return NT_STATUS_NO_MEMORY
;
387 status
= dcerpc_drsuapi_DsCrackNames_r(drsuapi_pipe
->binding_handle
, tmp_ctx
, &r_crack_names
);
388 if (!NT_STATUS_IS_OK(status
)) {
391 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s",
394 talloc_free(tmp_ctx
);
396 } else if (!W_ERROR_IS_OK(r_crack_names
.out
.result
)) {
399 "DsCrackNames failed - %s", win_errstr(r_crack_names
.out
.result
));
400 talloc_free(tmp_ctx
);
401 return NT_STATUS_UNSUCCESSFUL
;
402 } else if (*r_crack_names
.out
.level_out
!= 1
403 || !r_crack_names
.out
.ctr
->ctr1
404 || r_crack_names
.out
.ctr
->ctr1
->count
!= 1
405 || !r_crack_names
.out
.ctr
->ctr1
->array
[0].result_name
406 || r_crack_names
.out
.ctr
->ctr1
->array
[0].status
!= DRSUAPI_DS_NAME_STATUS_OK
) {
407 r
->out
.error_string
= talloc_asprintf(r
, "DsCrackNames failed");
408 talloc_free(tmp_ctx
);
409 return NT_STATUS_UNSUCCESSFUL
;
412 /* Store the account DN. */
413 r
->out
.account_dn_str
= account_dn_str
;
414 talloc_steal(r
, account_dn_str
);
416 /* Store the domain DN. */
417 r
->out
.domain_dn_str
= r_crack_names
.out
.ctr
->ctr1
->array
[0].result_name
;
418 talloc_steal(r
, r_crack_names
.out
.ctr
->ctr1
->array
[0].result_name
);
420 /* Store the KVNO of the account, critical for some kerberos
422 r
->out
.kvno
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "msDS-KeyVersionNumber", 0);
424 /* Store the account GUID. */
425 r
->out
.account_guid
= samdb_result_guid(res
->msgs
[0], "objectGUID");
427 if (r
->in
.acct_type
== ACB_SVRTRUST
) {
428 status
= libnet_JoinSite(ctx
, remote_ldb
, r
);
430 talloc_free(tmp_ctx
);
436 * do a domain join using DCERPC/SAMR calls
437 * - connect to the LSA pipe, to try and find out information about the domain
438 * - create a secondary connection to SAMR pipe
439 * - do a samr_Connect to get a policy handle
440 * - do a samr_LookupDomain to get the domain sid
441 * - do a samr_OpenDomain to get a domain handle
442 * - do a samr_CreateAccount to try and get a new account
445 * - do a samr_LookupNames to get the users rid
446 * - do a samr_OpenUser to get a user handle
447 * - potentially delete and recreate the user
448 * - assert the account is of the right type with samrQueryUserInfo
450 * - call libnet_SetPassword_samr_handle to set the password,
451 * and pass a samr_UserInfo21 struct to set full_name and the account flags
453 * - do some ADS specific things when we join as Domain Controller,
454 * look at libnet_joinADSDomain() for the details
456 NTSTATUS
libnet_JoinDomain(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
, struct libnet_JoinDomain
*r
)
460 NTSTATUS status
, cu_status
;
462 struct libnet_RpcConnect
*connect_with_info
;
463 struct dcerpc_pipe
*samr_pipe
;
465 struct samr_Connect sc
;
466 struct policy_handle p_handle
;
467 struct samr_OpenDomain od
;
468 struct policy_handle d_handle
;
469 struct samr_LookupNames ln
;
470 struct samr_Ids rids
, types
;
471 struct samr_OpenUser ou
;
472 struct samr_CreateUser2 cu
;
473 struct policy_handle
*u_handle
= NULL
;
474 struct samr_QueryUserInfo qui
;
475 union samr_UserInfo
*uinfo
;
476 struct samr_UserInfo21 u_info21
;
477 union libnet_SetPassword r2
;
478 struct samr_GetUserPwInfo pwp
;
479 struct samr_PwInfo info
;
480 struct lsa_String samr_account_name
;
482 uint32_t acct_flags
, old_acct_flags
;
483 uint32_t rid
, access_granted
;
484 int policy_min_pw_len
= 0;
486 struct dom_sid
*account_sid
= NULL
;
487 const char *password_str
= NULL
;
489 r
->out
.error_string
= NULL
;
490 r2
.samr_handle
.out
.error_string
= NULL
;
492 tmp_ctx
= talloc_named(mem_ctx
, 0, "libnet_Join temp context");
494 r
->out
.error_string
= NULL
;
495 return NT_STATUS_NO_MEMORY
;
498 u_handle
= talloc(tmp_ctx
, struct policy_handle
);
500 r
->out
.error_string
= NULL
;
501 talloc_free(tmp_ctx
);
502 return NT_STATUS_NO_MEMORY
;
505 connect_with_info
= talloc_zero(tmp_ctx
, struct libnet_RpcConnect
);
506 if (!connect_with_info
) {
507 r
->out
.error_string
= NULL
;
508 talloc_free(tmp_ctx
);
509 return NT_STATUS_NO_MEMORY
;
512 /* prepare connect to the SAMR pipe of PDC */
513 if (r
->in
.level
== LIBNET_JOINDOMAIN_AUTOMATIC
) {
514 connect_with_info
->in
.binding
= NULL
;
515 connect_with_info
->in
.name
= r
->in
.domain_name
;
517 connect_with_info
->in
.binding
= r
->in
.binding
;
518 connect_with_info
->in
.name
= NULL
;
521 /* This level makes a connection to the LSA pipe on the way,
522 * to get some useful bits of information about the domain */
523 connect_with_info
->level
= LIBNET_RPC_CONNECT_DC_INFO
;
524 connect_with_info
->in
.dcerpc_iface
= &ndr_table_samr
;
527 establish the SAMR connection
529 status
= libnet_RpcConnect(ctx
, tmp_ctx
, connect_with_info
);
530 if (!NT_STATUS_IS_OK(status
)) {
532 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
533 "Connection to SAMR pipe of DC %s failed: %s",
534 r
->in
.binding
, connect_with_info
->out
.error_string
);
536 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
537 "Connection to SAMR pipe of PDC for %s failed: %s",
538 r
->in
.domain_name
, connect_with_info
->out
.error_string
);
540 talloc_free(tmp_ctx
);
544 samr_pipe
= connect_with_info
->out
.dcerpc_pipe
;
546 /* prepare samr_Connect */
547 ZERO_STRUCT(p_handle
);
548 sc
.in
.system_name
= NULL
;
549 sc
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
550 sc
.out
.connect_handle
= &p_handle
;
552 /* 2. do a samr_Connect to get a policy handle */
553 status
= dcerpc_samr_Connect_r(samr_pipe
->binding_handle
, tmp_ctx
, &sc
);
554 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(sc
.out
.result
)) {
555 status
= sc
.out
.result
;
557 if (!NT_STATUS_IS_OK(status
)) {
558 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
559 "samr_Connect failed: %s",
561 talloc_free(tmp_ctx
);
565 /* If this is a connection on ncacn_ip_tcp to Win2k3 SP1, we don't get back this useful info */
566 if (!connect_with_info
->out
.domain_name
) {
567 if (r
->in
.level
== LIBNET_JOINDOMAIN_AUTOMATIC
) {
568 connect_with_info
->out
.domain_name
= talloc_strdup(tmp_ctx
, r
->in
.domain_name
);
570 /* Bugger, we just lost our way to automatically find the domain name */
571 connect_with_info
->out
.domain_name
= talloc_strdup(tmp_ctx
, lpcfg_workgroup(ctx
->lp_ctx
));
572 connect_with_info
->out
.realm
= talloc_strdup(tmp_ctx
, lpcfg_realm(ctx
->lp_ctx
));
576 /* Perhaps we didn't get a SID above, because we are against ncacn_ip_tcp */
577 if (!connect_with_info
->out
.domain_sid
) {
578 struct lsa_String name
;
579 struct samr_LookupDomain l
;
580 struct dom_sid2
*sid
= NULL
;
581 name
.string
= connect_with_info
->out
.domain_name
;
582 l
.in
.connect_handle
= &p_handle
;
583 l
.in
.domain_name
= &name
;
586 status
= dcerpc_samr_LookupDomain_r(samr_pipe
->binding_handle
, tmp_ctx
, &l
);
587 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(l
.out
.result
)) {
588 status
= l
.out
.result
;
590 if (!NT_STATUS_IS_OK(status
)) {
591 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
592 "SAMR LookupDomain failed: %s",
594 talloc_free(tmp_ctx
);
597 connect_with_info
->out
.domain_sid
= *l
.out
.sid
;
600 /* prepare samr_OpenDomain */
601 ZERO_STRUCT(d_handle
);
602 od
.in
.connect_handle
= &p_handle
;
603 od
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
604 od
.in
.sid
= connect_with_info
->out
.domain_sid
;
605 od
.out
.domain_handle
= &d_handle
;
607 /* do a samr_OpenDomain to get a domain handle */
608 status
= dcerpc_samr_OpenDomain_r(samr_pipe
->binding_handle
, tmp_ctx
, &od
);
609 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(od
.out
.result
)) {
610 status
= od
.out
.result
;
612 if (!NT_STATUS_IS_OK(status
)) {
613 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
614 "samr_OpenDomain for [%s] failed: %s",
615 dom_sid_string(tmp_ctx
, connect_with_info
->out
.domain_sid
),
617 talloc_free(tmp_ctx
);
621 /* prepare samr_CreateUser2 */
622 ZERO_STRUCTP(u_handle
);
623 cu
.in
.domain_handle
= &d_handle
;
624 cu
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
625 samr_account_name
.string
= r
->in
.account_name
;
626 cu
.in
.account_name
= &samr_account_name
;
627 cu
.in
.acct_flags
= r
->in
.acct_type
;
628 cu
.out
.user_handle
= u_handle
;
630 cu
.out
.access_granted
= &access_granted
;
632 /* do a samr_CreateUser2 to get an account handle, or an error */
633 cu_status
= dcerpc_samr_CreateUser2_r(samr_pipe
->binding_handle
, tmp_ctx
, &cu
);
634 if (NT_STATUS_IS_OK(cu_status
) && !NT_STATUS_IS_OK(cu
.out
.result
)) {
635 cu_status
= cu
.out
.result
;
638 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
639 /* prepare samr_LookupNames */
640 ln
.in
.domain_handle
= &d_handle
;
642 ln
.in
.names
= talloc_array(tmp_ctx
, struct lsa_String
, 1);
644 ln
.out
.types
= &types
;
646 r
->out
.error_string
= NULL
;
647 talloc_free(tmp_ctx
);
648 return NT_STATUS_NO_MEMORY
;
650 ln
.in
.names
[0].string
= r
->in
.account_name
;
652 /* 5. do a samr_LookupNames to get the users rid */
653 status
= dcerpc_samr_LookupNames_r(samr_pipe
->binding_handle
, tmp_ctx
, &ln
);
654 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(ln
.out
.result
)) {
655 status
= ln
.out
.result
;
657 if (!NT_STATUS_IS_OK(status
)) {
658 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
659 "samr_LookupNames for [%s] failed: %s",
662 talloc_free(tmp_ctx
);
666 /* check if we got one RID for the user */
667 if (ln
.out
.rids
->count
!= 1) {
668 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
669 "samr_LookupNames for [%s] returns %d RIDs",
670 r
->in
.account_name
, ln
.out
.rids
->count
);
671 talloc_free(tmp_ctx
);
672 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
675 if (ln
.out
.types
->count
!= 1) {
676 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
677 "samr_LookupNames for [%s] returns %d RID TYPEs",
678 r
->in
.account_name
, ln
.out
.types
->count
);
679 talloc_free(tmp_ctx
);
680 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
683 /* prepare samr_OpenUser */
684 ZERO_STRUCTP(u_handle
);
685 ou
.in
.domain_handle
= &d_handle
;
686 ou
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
687 ou
.in
.rid
= ln
.out
.rids
->ids
[0];
689 ou
.out
.user_handle
= u_handle
;
691 /* 6. do a samr_OpenUser to get a user handle */
692 status
= dcerpc_samr_OpenUser_r(samr_pipe
->binding_handle
, tmp_ctx
, &ou
);
693 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(ou
.out
.result
)) {
694 status
= ou
.out
.result
;
696 if (!NT_STATUS_IS_OK(status
)) {
697 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
698 "samr_OpenUser for [%s] failed: %s",
701 talloc_free(tmp_ctx
);
705 if (r
->in
.recreate_account
) {
706 struct samr_DeleteUser d
;
707 d
.in
.user_handle
= u_handle
;
708 d
.out
.user_handle
= u_handle
;
709 status
= dcerpc_samr_DeleteUser_r(samr_pipe
->binding_handle
, mem_ctx
, &d
);
710 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(d
.out
.result
)) {
711 status
= d
.out
.result
;
713 if (!NT_STATUS_IS_OK(status
)) {
714 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
715 "samr_DeleteUser (for recreate) of [%s] failed: %s",
718 talloc_free(tmp_ctx
);
722 /* We want to recreate, so delete and another samr_CreateUser2 */
724 /* &cu filled in above */
725 status
= dcerpc_samr_CreateUser2_r(samr_pipe
->binding_handle
, tmp_ctx
, &cu
);
726 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(cu
.out
.result
)) {
727 status
= cu
.out
.result
;
729 if (!NT_STATUS_IS_OK(status
)) {
730 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
731 "samr_CreateUser2 (recreate) for [%s] failed: %s",
732 r
->in
.account_name
, nt_errstr(status
));
733 talloc_free(tmp_ctx
);
737 } else if (!NT_STATUS_IS_OK(status
)) {
738 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
739 "samr_CreateUser2 for [%s] failed: %s",
740 r
->in
.account_name
, nt_errstr(status
));
741 talloc_free(tmp_ctx
);
745 /* prepare samr_QueryUserInfo (get flags) */
746 qui
.in
.user_handle
= u_handle
;
748 qui
.out
.info
= &uinfo
;
750 status
= dcerpc_samr_QueryUserInfo_r(samr_pipe
->binding_handle
, tmp_ctx
, &qui
);
751 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(qui
.out
.result
)) {
752 status
= qui
.out
.result
;
754 if (!NT_STATUS_IS_OK(status
)) {
755 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
756 "samr_QueryUserInfo for [%s] failed: %s",
759 talloc_free(tmp_ctx
);
764 status
= NT_STATUS_INVALID_PARAMETER
;
766 = talloc_asprintf(mem_ctx
,
767 "samr_QueryUserInfo failed to return qui.out.info for [%s]: %s",
768 r
->in
.account_name
, nt_errstr(status
));
769 talloc_free(tmp_ctx
);
773 old_acct_flags
= (uinfo
->info16
.acct_flags
& (ACB_WSTRUST
| ACB_SVRTRUST
| ACB_DOMTRUST
));
774 /* Possibly bail if the account is of the wrong type */
776 != r
->in
.acct_type
) {
777 const char *old_account_type
, *new_account_type
;
778 switch (old_acct_flags
) {
780 old_account_type
= "domain member (member)";
783 old_account_type
= "domain controller (bdc)";
786 old_account_type
= "trusted domain";
789 return NT_STATUS_INVALID_PARAMETER
;
791 switch (r
->in
.acct_type
) {
793 new_account_type
= "domain member (member)";
796 new_account_type
= "domain controller (bdc)";
799 new_account_type
= "trusted domain";
802 return NT_STATUS_INVALID_PARAMETER
;
805 if (!NT_STATUS_EQUAL(cu_status
, NT_STATUS_USER_EXISTS
)) {
806 /* We created a new user, but they didn't come out the right type?!? */
808 = talloc_asprintf(mem_ctx
,
809 "We asked to create a new machine account (%s) of type %s, "
810 "but we got an account of type %s. This is unexpected. "
811 "Perhaps delete the account and try again.",
812 r
->in
.account_name
, new_account_type
, old_account_type
);
813 talloc_free(tmp_ctx
);
814 return NT_STATUS_INVALID_PARAMETER
;
816 /* The account is of the wrong type, so bail */
818 /* TODO: We should allow a --force option to override, and redo this from the top setting r.in.recreate_account */
820 = talloc_asprintf(mem_ctx
,
821 "The machine account (%s) already exists in the domain %s, "
822 "but is a %s. You asked to join as a %s. Please delete "
823 "the account and try again.",
824 r
->in
.account_name
, connect_with_info
->out
.domain_name
, old_account_type
, new_account_type
);
825 talloc_free(tmp_ctx
);
826 return NT_STATUS_USER_EXISTS
;
829 acct_flags
= uinfo
->info16
.acct_flags
;
832 acct_flags
= (acct_flags
& ~(ACB_DISABLED
|ACB_PWNOTREQ
));
834 /* Find out what password policy this user has */
835 pwp
.in
.user_handle
= u_handle
;
836 pwp
.out
.info
= &info
;
838 status
= dcerpc_samr_GetUserPwInfo_r(samr_pipe
->binding_handle
, tmp_ctx
, &pwp
);
839 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(pwp
.out
.result
)) {
840 status
= pwp
.out
.result
;
842 if (NT_STATUS_IS_OK(status
)) {
843 policy_min_pw_len
= pwp
.out
.info
->min_password_length
;
846 if (r
->in
.account_pass
!= NULL
) {
847 password_str
= talloc_strdup(tmp_ctx
, r
->in
.account_pass
);
849 /* Grab a password of that minimum length */
850 password_str
= generate_random_password(tmp_ctx
,
851 MAX(8, policy_min_pw_len
), 255);
854 r
->out
.error_string
= NULL
;
855 talloc_free(tmp_ctx
);
856 return NT_STATUS_NO_MEMORY
;
859 /* set full_name and reset flags */
860 ZERO_STRUCT(u_info21
);
861 u_info21
.full_name
.string
= r
->in
.account_name
;
862 u_info21
.acct_flags
= acct_flags
;
863 u_info21
.fields_present
= SAMR_FIELD_FULL_NAME
| SAMR_FIELD_ACCT_FLAGS
;
865 r2
.samr_handle
.level
= LIBNET_SET_PASSWORD_SAMR_HANDLE
;
866 r2
.samr_handle
.in
.account_name
= r
->in
.account_name
;
867 r2
.samr_handle
.in
.newpassword
= password_str
;
868 r2
.samr_handle
.in
.user_handle
= u_handle
;
869 r2
.samr_handle
.in
.dcerpc_pipe
= samr_pipe
;
870 r2
.samr_handle
.in
.info21
= &u_info21
;
872 status
= libnet_SetPassword(ctx
, tmp_ctx
, &r2
);
873 if (!NT_STATUS_IS_OK(status
)) {
874 r
->out
.error_string
= talloc_steal(mem_ctx
, r2
.samr_handle
.out
.error_string
);
875 talloc_free(tmp_ctx
);
879 account_sid
= dom_sid_add_rid(mem_ctx
, connect_with_info
->out
.domain_sid
, rid
);
881 r
->out
.error_string
= NULL
;
882 talloc_free(tmp_ctx
);
883 return NT_STATUS_NO_MEMORY
;
886 /* Finish out by pushing various bits of status data out for the caller to use */
887 r
->out
.join_password
= password_str
;
888 talloc_steal(mem_ctx
, r
->out
.join_password
);
890 r
->out
.domain_sid
= connect_with_info
->out
.domain_sid
;
891 talloc_steal(mem_ctx
, r
->out
.domain_sid
);
893 r
->out
.account_sid
= account_sid
;
894 talloc_steal(mem_ctx
, r
->out
.account_sid
);
896 r
->out
.domain_name
= connect_with_info
->out
.domain_name
;
897 talloc_steal(mem_ctx
, r
->out
.domain_name
);
898 r
->out
.realm
= connect_with_info
->out
.realm
;
899 talloc_steal(mem_ctx
, r
->out
.realm
);
900 r
->out
.samr_pipe
= samr_pipe
;
901 talloc_reparent(tmp_ctx
, mem_ctx
, samr_pipe
);
902 r
->out
.samr_binding
= samr_pipe
->binding
;
903 r
->out
.user_handle
= u_handle
;
904 talloc_steal(mem_ctx
, u_handle
);
905 r
->out
.error_string
= r2
.samr_handle
.out
.error_string
;
906 talloc_steal(mem_ctx
, r2
.samr_handle
.out
.error_string
);
908 r
->out
.server_dn_str
= NULL
;
909 talloc_free(tmp_ctx
);
911 /* Now, if it was AD, then we want to start looking changing a
912 * few more things. Otherwise, we are done. */
914 status
= libnet_JoinADSDomain(ctx
, r
);
921 NTSTATUS
libnet_Join_member(struct libnet_context
*ctx
,
923 struct libnet_Join_member
*r
)
927 struct libnet_JoinDomain
*r2
;
928 struct provision_store_self_join_settings
*set_secrets
;
929 uint32_t acct_type
= 0;
930 const char *account_name
;
931 const char *netbios_name
;
932 const char *error_string
;
934 r
->out
.error_string
= NULL
;
936 tmp_mem
= talloc_new(mem_ctx
);
938 return NT_STATUS_NO_MEMORY
;
941 r2
= talloc_zero(tmp_mem
, struct libnet_JoinDomain
);
943 r
->out
.error_string
= NULL
;
944 talloc_free(tmp_mem
);
945 return NT_STATUS_NO_MEMORY
;
948 acct_type
= ACB_WSTRUST
;
950 if (r
->in
.netbios_name
!= NULL
) {
951 netbios_name
= r
->in
.netbios_name
;
953 netbios_name
= talloc_strdup(tmp_mem
, lpcfg_netbios_name(ctx
->lp_ctx
));
955 r
->out
.error_string
= NULL
;
956 talloc_free(tmp_mem
);
957 return NT_STATUS_NO_MEMORY
;
961 account_name
= talloc_asprintf(tmp_mem
, "%s$", netbios_name
);
963 r
->out
.error_string
= NULL
;
964 talloc_free(tmp_mem
);
965 return NT_STATUS_NO_MEMORY
;
971 r2
->in
.domain_name
= r
->in
.domain_name
;
972 r2
->in
.account_name
= account_name
;
973 r2
->in
.netbios_name
= netbios_name
;
974 r2
->in
.level
= LIBNET_JOINDOMAIN_AUTOMATIC
;
975 r2
->in
.acct_type
= acct_type
;
976 r2
->in
.recreate_account
= false;
977 r2
->in
.account_pass
= r
->in
.account_pass
;
978 status
= libnet_JoinDomain(ctx
, r2
, r2
);
979 if (!NT_STATUS_IS_OK(status
)) {
980 r
->out
.error_string
= talloc_steal(mem_ctx
, r2
->out
.error_string
);
981 talloc_free(tmp_mem
);
985 set_secrets
= talloc(tmp_mem
, struct provision_store_self_join_settings
);
987 r
->out
.error_string
= NULL
;
988 talloc_free(tmp_mem
);
989 return NT_STATUS_NO_MEMORY
;
992 ZERO_STRUCTP(set_secrets
);
993 set_secrets
->domain_name
= r2
->out
.domain_name
;
994 set_secrets
->realm
= r2
->out
.realm
;
995 set_secrets
->netbios_name
= netbios_name
;
996 set_secrets
->secure_channel_type
= SEC_CHAN_WKSTA
;
997 set_secrets
->machine_password
= r2
->out
.join_password
;
998 set_secrets
->key_version_number
= r2
->out
.kvno
;
999 set_secrets
->domain_sid
= r2
->out
.domain_sid
;
1001 status
= provision_store_self_join(ctx
, ctx
->lp_ctx
, ctx
->event_ctx
, set_secrets
, &error_string
);
1002 if (!NT_STATUS_IS_OK(status
)) {
1003 r
->out
.error_string
= talloc_steal(mem_ctx
, error_string
);
1004 talloc_free(tmp_mem
);
1008 /* move all out parameter to the callers TALLOC_CTX */
1009 r
->out
.error_string
= NULL
;
1010 r
->out
.join_password
= r2
->out
.join_password
;
1011 talloc_reparent(r2
, mem_ctx
, r2
->out
.join_password
);
1012 r
->out
.domain_sid
= r2
->out
.domain_sid
;
1013 talloc_reparent(r2
, mem_ctx
, r2
->out
.domain_sid
);
1014 r
->out
.domain_name
= r2
->out
.domain_name
;
1015 talloc_reparent(r2
, mem_ctx
, r2
->out
.domain_name
);
1016 talloc_free(tmp_mem
);
1017 return NT_STATUS_OK
;