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 struct dcerpc_binding
*samr_binding
= r
->out
.samr_binding
;
58 struct dcerpc_pipe
*drsuapi_pipe
;
59 struct dcerpc_binding
*drsuapi_binding
;
60 struct drsuapi_DsBind r_drsuapi_bind
;
61 struct drsuapi_DsCrackNames r_crack_names
;
62 struct drsuapi_DsNameString names
[1];
63 struct policy_handle drsuapi_bind_handle
;
64 struct GUID drsuapi_bind_guid
;
66 struct ldb_context
*remote_ldb
;
67 struct ldb_dn
*account_dn
;
68 const char *account_dn_str
;
69 const char *remote_ldb_url
;
70 struct ldb_result
*res
;
71 struct ldb_message
*msg
;
75 const char * const attrs
[] = {
76 "msDS-KeyVersionNumber",
77 "servicePrincipalName",
83 r
->out
.error_string
= NULL
;
85 /* We need to convert between a samAccountName and domain to a
86 * DN in the directory. The correct way to do this is with
87 * DRSUAPI CrackNames */
89 /* Fiddle with the bindings, so get to DRSUAPI on
90 * NCACN_IP_TCP, sealed */
91 tmp_ctx
= talloc_named(r
, 0, "libnet_JoinADSDomain temp context");
93 r
->out
.error_string
= NULL
;
94 return NT_STATUS_NO_MEMORY
;
97 drsuapi_binding
= dcerpc_binding_dup(tmp_ctx
, samr_binding
);
98 if (!drsuapi_binding
) {
99 r
->out
.error_string
= NULL
;
100 talloc_free(tmp_ctx
);
101 return NT_STATUS_NO_MEMORY
;
105 /* DRSUAPI is only available on IP_TCP, and locally on NCALRPC */
106 if (drsuapi_binding
->transport
!= NCALRPC
) {
107 drsuapi_binding
->transport
= NCACN_IP_TCP
;
109 drsuapi_binding
->endpoint
= NULL
;
110 drsuapi_binding
->flags
|= DCERPC_SEAL
;
112 status
= dcerpc_pipe_connect_b(tmp_ctx
,
119 if (!NT_STATUS_IS_OK(status
)) {
120 r
->out
.error_string
= talloc_asprintf(r
,
121 "Connection to DRSUAPI pipe of PDC of domain '%s' failed: %s",
124 talloc_free(tmp_ctx
);
128 /* get a DRSUAPI pipe handle */
129 GUID_from_string(DRSUAPI_DS_BIND_GUID
, &drsuapi_bind_guid
);
131 r_drsuapi_bind
.in
.bind_guid
= &drsuapi_bind_guid
;
132 r_drsuapi_bind
.in
.bind_info
= NULL
;
133 r_drsuapi_bind
.out
.bind_handle
= &drsuapi_bind_handle
;
135 status
= dcerpc_drsuapi_DsBind_r(drsuapi_pipe
->binding_handle
, tmp_ctx
, &r_drsuapi_bind
);
136 if (!NT_STATUS_IS_OK(status
)) {
139 "dcerpc_drsuapi_DsBind failed - %s",
141 talloc_free(tmp_ctx
);
143 } else if (!W_ERROR_IS_OK(r_drsuapi_bind
.out
.result
)) {
146 "DsBind failed - %s",
147 win_errstr(r_drsuapi_bind
.out
.result
));
148 talloc_free(tmp_ctx
);
149 return NT_STATUS_UNSUCCESSFUL
;
152 /* Actually 'crack' the names */
153 ZERO_STRUCT(r_crack_names
);
154 r_crack_names
.in
.bind_handle
= &drsuapi_bind_handle
;
155 r_crack_names
.in
.level
= 1;
156 r_crack_names
.in
.req
= talloc(r
, union drsuapi_DsNameRequest
);
157 if (!r_crack_names
.in
.req
) {
158 r
->out
.error_string
= NULL
;
159 talloc_free(tmp_ctx
);
160 return NT_STATUS_NO_MEMORY
;
162 r_crack_names
.in
.req
->req1
.codepage
= 1252; /* western european */
163 r_crack_names
.in
.req
->req1
.language
= 0x00000407; /* german */
164 r_crack_names
.in
.req
->req1
.count
= 1;
165 r_crack_names
.in
.req
->req1
.names
= names
;
166 r_crack_names
.in
.req
->req1
.format_flags
= DRSUAPI_DS_NAME_FLAG_NO_FLAGS
;
167 r_crack_names
.in
.req
->req1
.format_offered
= DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY
;
168 r_crack_names
.in
.req
->req1
.format_desired
= DRSUAPI_DS_NAME_FORMAT_FQDN_1779
;
169 names
[0].str
= dom_sid_string(tmp_ctx
, r
->out
.account_sid
);
171 r
->out
.error_string
= NULL
;
172 talloc_free(tmp_ctx
);
173 return NT_STATUS_NO_MEMORY
;
176 r_crack_names
.out
.ctr
= talloc(r
, union drsuapi_DsNameCtr
);
177 r_crack_names
.out
.level_out
= talloc(r
, uint32_t);
178 if (!r_crack_names
.out
.ctr
|| !r_crack_names
.out
.level_out
) {
179 r
->out
.error_string
= NULL
;
180 talloc_free(tmp_ctx
);
181 return NT_STATUS_NO_MEMORY
;
184 status
= dcerpc_drsuapi_DsCrackNames_r(drsuapi_pipe
->binding_handle
, tmp_ctx
, &r_crack_names
);
185 if (!NT_STATUS_IS_OK(status
)) {
188 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s",
191 talloc_free(tmp_ctx
);
193 } else if (!W_ERROR_IS_OK(r_crack_names
.out
.result
)) {
196 "DsCrackNames failed - %s", win_errstr(r_crack_names
.out
.result
));
197 talloc_free(tmp_ctx
);
198 return NT_STATUS_UNSUCCESSFUL
;
199 } else if (*r_crack_names
.out
.level_out
!= 1
200 || !r_crack_names
.out
.ctr
->ctr1
201 || r_crack_names
.out
.ctr
->ctr1
->count
!= 1) {
202 r
->out
.error_string
= talloc_asprintf(r
, "DsCrackNames failed");
203 talloc_free(tmp_ctx
);
204 return NT_STATUS_INVALID_PARAMETER
;
205 } else if (r_crack_names
.out
.ctr
->ctr1
->array
[0].status
!= DRSUAPI_DS_NAME_STATUS_OK
) {
206 r
->out
.error_string
= talloc_asprintf(r
, "DsCrackNames failed: %d", r_crack_names
.out
.ctr
->ctr1
->array
[0].status
);
207 talloc_free(tmp_ctx
);
208 return NT_STATUS_UNSUCCESSFUL
;
209 } else if (r_crack_names
.out
.ctr
->ctr1
->array
[0].result_name
== NULL
) {
210 r
->out
.error_string
= talloc_asprintf(r
, "DsCrackNames failed: no result name");
211 talloc_free(tmp_ctx
);
212 return NT_STATUS_INVALID_PARAMETER
;
215 /* Store the DN of our machine account. */
216 account_dn_str
= r_crack_names
.out
.ctr
->ctr1
->array
[0].result_name
;
218 /* Now we know the user's DN, open with LDAP, read and modify a few things */
220 remote_ldb_url
= talloc_asprintf(tmp_ctx
, "ldap://%s",
221 drsuapi_binding
->target_hostname
);
222 if (!remote_ldb_url
) {
223 r
->out
.error_string
= NULL
;
224 talloc_free(tmp_ctx
);
225 return NT_STATUS_NO_MEMORY
;
228 remote_ldb
= ldb_wrap_connect(tmp_ctx
, ctx
->event_ctx
, ctx
->lp_ctx
,
232 r
->out
.error_string
= NULL
;
233 talloc_free(tmp_ctx
);
234 return NT_STATUS_UNSUCCESSFUL
;
237 account_dn
= ldb_dn_new(tmp_ctx
, remote_ldb
, account_dn_str
);
238 if (account_dn
== NULL
) {
239 r
->out
.error_string
= talloc_asprintf(r
, "Invalid account dn: %s",
241 talloc_free(tmp_ctx
);
242 return NT_STATUS_UNSUCCESSFUL
;
245 /* search for the user's record */
246 ret
= ldb_search(remote_ldb
, tmp_ctx
, &res
,
247 account_dn
, LDB_SCOPE_BASE
, attrs
, NULL
);
248 if (ret
!= LDB_SUCCESS
) {
249 r
->out
.error_string
= talloc_asprintf(r
, "ldb_search for %s failed - %s",
250 account_dn_str
, ldb_errstring(remote_ldb
));
251 talloc_free(tmp_ctx
);
252 return NT_STATUS_UNSUCCESSFUL
;
255 if (res
->count
!= 1) {
256 r
->out
.error_string
= talloc_asprintf(r
, "ldb_search for %s failed - found %d entries",
257 account_dn_str
, res
->count
);
258 talloc_free(tmp_ctx
);
259 return NT_STATUS_UNSUCCESSFUL
;
262 /* Prepare a new message, for the modify */
263 msg
= ldb_msg_new(tmp_ctx
);
265 r
->out
.error_string
= NULL
;
266 talloc_free(tmp_ctx
);
267 return NT_STATUS_NO_MEMORY
;
269 msg
->dn
= res
->msgs
[0]->dn
;
273 const char *service_principal_name
[2];
274 const char *dns_host_name
= strlower_talloc(msg
,
280 if (!dns_host_name
) {
281 r
->out
.error_string
= NULL
;
282 talloc_free(tmp_ctx
);
283 return NT_STATUS_NO_MEMORY
;
286 service_principal_name
[0] = talloc_asprintf(msg
, "HOST/%s",
288 service_principal_name
[1] = talloc_asprintf(msg
, "HOST/%s",
291 for (i
=0; i
< ARRAY_SIZE(service_principal_name
); i
++) {
292 if (!service_principal_name
[i
]) {
293 r
->out
.error_string
= NULL
;
294 talloc_free(tmp_ctx
);
295 return NT_STATUS_NO_MEMORY
;
297 rtn
= ldb_msg_add_string(msg
, "servicePrincipalName",
298 service_principal_name
[i
]);
299 if (rtn
!= LDB_SUCCESS
) {
300 r
->out
.error_string
= NULL
;
301 talloc_free(tmp_ctx
);
302 return NT_STATUS_NO_MEMORY
;
306 rtn
= ldb_msg_add_string(msg
, "dNSHostName", dns_host_name
);
307 if (rtn
!= LDB_SUCCESS
) {
308 r
->out
.error_string
= NULL
;
309 talloc_free(tmp_ctx
);
310 return NT_STATUS_NO_MEMORY
;
313 rtn
= dsdb_replace(remote_ldb
, msg
, 0);
314 if (rtn
!= LDB_SUCCESS
) {
317 "Failed to replace entries on %s",
318 ldb_dn_get_linearized(msg
->dn
));
319 talloc_free(tmp_ctx
);
320 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
324 msg
= ldb_msg_new(tmp_ctx
);
326 r
->out
.error_string
= NULL
;
327 talloc_free(tmp_ctx
);
328 return NT_STATUS_NO_MEMORY
;
330 msg
->dn
= res
->msgs
[0]->dn
;
332 rtn
= samdb_msg_add_uint(remote_ldb
, msg
, msg
,
333 "msDS-SupportedEncryptionTypes", ENC_ALL_TYPES
);
334 if (rtn
!= LDB_SUCCESS
) {
335 r
->out
.error_string
= NULL
;
336 talloc_free(tmp_ctx
);
337 return NT_STATUS_NO_MEMORY
;
340 rtn
= dsdb_replace(remote_ldb
, msg
, 0);
341 /* The remote server may not support this attribute, if it
342 * isn't a modern schema */
343 if (rtn
!= LDB_SUCCESS
&& rtn
!= LDB_ERR_NO_SUCH_ATTRIBUTE
) {
346 "Failed to replace msDS-SupportedEncryptionTypes on %s",
347 ldb_dn_get_linearized(msg
->dn
));
348 talloc_free(tmp_ctx
);
349 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
352 /* DsCrackNames to find out the DN of the domain. */
353 r_crack_names
.in
.req
->req1
.format_offered
= DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT
;
354 r_crack_names
.in
.req
->req1
.format_desired
= DRSUAPI_DS_NAME_FORMAT_FQDN_1779
;
355 names
[0].str
= talloc_asprintf(tmp_ctx
, "%s\\", r
->out
.domain_name
);
357 r
->out
.error_string
= NULL
;
358 talloc_free(tmp_ctx
);
359 return NT_STATUS_NO_MEMORY
;
362 status
= dcerpc_drsuapi_DsCrackNames_r(drsuapi_pipe
->binding_handle
, tmp_ctx
, &r_crack_names
);
363 if (!NT_STATUS_IS_OK(status
)) {
366 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s",
369 talloc_free(tmp_ctx
);
371 } else if (!W_ERROR_IS_OK(r_crack_names
.out
.result
)) {
374 "DsCrackNames failed - %s", win_errstr(r_crack_names
.out
.result
));
375 talloc_free(tmp_ctx
);
376 return NT_STATUS_UNSUCCESSFUL
;
377 } else if (*r_crack_names
.out
.level_out
!= 1
378 || !r_crack_names
.out
.ctr
->ctr1
379 || r_crack_names
.out
.ctr
->ctr1
->count
!= 1
380 || !r_crack_names
.out
.ctr
->ctr1
->array
[0].result_name
381 || r_crack_names
.out
.ctr
->ctr1
->array
[0].status
!= DRSUAPI_DS_NAME_STATUS_OK
) {
382 r
->out
.error_string
= talloc_asprintf(r
, "DsCrackNames failed");
383 talloc_free(tmp_ctx
);
384 return NT_STATUS_UNSUCCESSFUL
;
387 /* Store the account DN. */
388 r
->out
.account_dn_str
= account_dn_str
;
389 talloc_steal(r
, account_dn_str
);
391 /* Store the domain DN. */
392 r
->out
.domain_dn_str
= r_crack_names
.out
.ctr
->ctr1
->array
[0].result_name
;
393 talloc_steal(r
, r_crack_names
.out
.ctr
->ctr1
->array
[0].result_name
);
395 /* Store the KVNO of the account, critical for some kerberos
397 r
->out
.kvno
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "msDS-KeyVersionNumber", 0);
399 /* Store the account GUID. */
400 r
->out
.account_guid
= samdb_result_guid(res
->msgs
[0], "objectGUID");
402 if (r
->in
.acct_type
== ACB_SVRTRUST
) {
403 status
= libnet_JoinSite(ctx
, remote_ldb
, r
);
405 talloc_free(tmp_ctx
);
411 * do a domain join using DCERPC/SAMR calls
412 * - connect to the LSA pipe, to try and find out information about the domain
413 * - create a secondary connection to SAMR pipe
414 * - do a samr_Connect to get a policy handle
415 * - do a samr_LookupDomain to get the domain sid
416 * - do a samr_OpenDomain to get a domain handle
417 * - do a samr_CreateAccount to try and get a new account
420 * - do a samr_LookupNames to get the users rid
421 * - do a samr_OpenUser to get a user handle
422 * - potentially delete and recreate the user
423 * - assert the account is of the right type with samrQueryUserInfo
425 * - call libnet_SetPassword_samr_handle to set the password,
426 * and pass a samr_UserInfo21 struct to set full_name and the account flags
428 * - do some ADS specific things when we join as Domain Controller,
429 * look at libnet_joinADSDomain() for the details
431 NTSTATUS
libnet_JoinDomain(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
, struct libnet_JoinDomain
*r
)
435 NTSTATUS status
, cu_status
;
437 struct libnet_RpcConnect
*connect_with_info
;
438 struct dcerpc_pipe
*samr_pipe
;
440 struct samr_Connect sc
;
441 struct policy_handle p_handle
;
442 struct samr_OpenDomain od
;
443 struct policy_handle d_handle
;
444 struct samr_LookupNames ln
;
445 struct samr_Ids rids
, types
;
446 struct samr_OpenUser ou
;
447 struct samr_CreateUser2 cu
;
448 struct policy_handle
*u_handle
= NULL
;
449 struct samr_QueryUserInfo qui
;
450 union samr_UserInfo
*uinfo
;
451 struct samr_UserInfo21 u_info21
;
452 union libnet_SetPassword r2
;
453 struct samr_GetUserPwInfo pwp
;
454 struct samr_PwInfo info
;
455 struct lsa_String samr_account_name
;
457 uint32_t acct_flags
, old_acct_flags
;
458 uint32_t rid
, access_granted
;
459 int policy_min_pw_len
= 0;
461 struct dom_sid
*account_sid
= NULL
;
462 const char *password_str
= NULL
;
464 r
->out
.error_string
= NULL
;
465 r2
.samr_handle
.out
.error_string
= NULL
;
467 tmp_ctx
= talloc_named(mem_ctx
, 0, "libnet_Join temp context");
469 r
->out
.error_string
= NULL
;
470 return NT_STATUS_NO_MEMORY
;
473 u_handle
= talloc(tmp_ctx
, struct policy_handle
);
475 r
->out
.error_string
= NULL
;
476 talloc_free(tmp_ctx
);
477 return NT_STATUS_NO_MEMORY
;
480 connect_with_info
= talloc_zero(tmp_ctx
, struct libnet_RpcConnect
);
481 if (!connect_with_info
) {
482 r
->out
.error_string
= NULL
;
483 talloc_free(tmp_ctx
);
484 return NT_STATUS_NO_MEMORY
;
487 /* prepare connect to the SAMR pipe of PDC */
488 if (r
->in
.level
== LIBNET_JOINDOMAIN_AUTOMATIC
) {
489 connect_with_info
->in
.binding
= NULL
;
490 connect_with_info
->in
.name
= r
->in
.domain_name
;
492 connect_with_info
->in
.binding
= r
->in
.binding
;
493 connect_with_info
->in
.name
= NULL
;
496 /* This level makes a connection to the LSA pipe on the way,
497 * to get some useful bits of information about the domain */
498 connect_with_info
->level
= LIBNET_RPC_CONNECT_DC_INFO
;
499 connect_with_info
->in
.dcerpc_iface
= &ndr_table_samr
;
502 establish the SAMR connection
504 status
= libnet_RpcConnect(ctx
, tmp_ctx
, connect_with_info
);
505 if (!NT_STATUS_IS_OK(status
)) {
507 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
508 "Connection to SAMR pipe of DC %s failed: %s",
509 r
->in
.binding
, connect_with_info
->out
.error_string
);
511 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
512 "Connection to SAMR pipe of PDC for %s failed: %s",
513 r
->in
.domain_name
, connect_with_info
->out
.error_string
);
515 talloc_free(tmp_ctx
);
519 samr_pipe
= connect_with_info
->out
.dcerpc_pipe
;
521 status
= dcerpc_pipe_auth(tmp_ctx
, &samr_pipe
,
522 connect_with_info
->out
.dcerpc_pipe
->binding
,
523 &ndr_table_samr
, ctx
->cred
, ctx
->lp_ctx
);
524 if (!NT_STATUS_IS_OK(status
)) {
525 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
526 "SAMR bind failed: %s",
528 talloc_free(tmp_ctx
);
532 /* prepare samr_Connect */
533 ZERO_STRUCT(p_handle
);
534 sc
.in
.system_name
= NULL
;
535 sc
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
536 sc
.out
.connect_handle
= &p_handle
;
538 /* 2. do a samr_Connect to get a policy handle */
539 status
= dcerpc_samr_Connect_r(samr_pipe
->binding_handle
, tmp_ctx
, &sc
);
540 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(sc
.out
.result
)) {
541 status
= sc
.out
.result
;
543 if (!NT_STATUS_IS_OK(status
)) {
544 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
545 "samr_Connect failed: %s",
547 talloc_free(tmp_ctx
);
551 /* If this is a connection on ncacn_ip_tcp to Win2k3 SP1, we don't get back this useful info */
552 if (!connect_with_info
->out
.domain_name
) {
553 if (r
->in
.level
== LIBNET_JOINDOMAIN_AUTOMATIC
) {
554 connect_with_info
->out
.domain_name
= talloc_strdup(tmp_ctx
, r
->in
.domain_name
);
556 /* Bugger, we just lost our way to automatically find the domain name */
557 connect_with_info
->out
.domain_name
= talloc_strdup(tmp_ctx
, lpcfg_workgroup(ctx
->lp_ctx
));
558 connect_with_info
->out
.realm
= talloc_strdup(tmp_ctx
, lpcfg_realm(ctx
->lp_ctx
));
562 /* Perhaps we didn't get a SID above, because we are against ncacn_ip_tcp */
563 if (!connect_with_info
->out
.domain_sid
) {
564 struct lsa_String name
;
565 struct samr_LookupDomain l
;
566 struct dom_sid2
*sid
= NULL
;
567 name
.string
= connect_with_info
->out
.domain_name
;
568 l
.in
.connect_handle
= &p_handle
;
569 l
.in
.domain_name
= &name
;
572 status
= dcerpc_samr_LookupDomain_r(samr_pipe
->binding_handle
, tmp_ctx
, &l
);
573 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(l
.out
.result
)) {
574 status
= l
.out
.result
;
576 if (!NT_STATUS_IS_OK(status
)) {
577 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
578 "SAMR LookupDomain failed: %s",
580 talloc_free(tmp_ctx
);
583 connect_with_info
->out
.domain_sid
= *l
.out
.sid
;
586 /* prepare samr_OpenDomain */
587 ZERO_STRUCT(d_handle
);
588 od
.in
.connect_handle
= &p_handle
;
589 od
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
590 od
.in
.sid
= connect_with_info
->out
.domain_sid
;
591 od
.out
.domain_handle
= &d_handle
;
593 /* do a samr_OpenDomain to get a domain handle */
594 status
= dcerpc_samr_OpenDomain_r(samr_pipe
->binding_handle
, tmp_ctx
, &od
);
595 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(od
.out
.result
)) {
596 status
= od
.out
.result
;
598 if (!NT_STATUS_IS_OK(status
)) {
599 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
600 "samr_OpenDomain for [%s] failed: %s",
601 dom_sid_string(tmp_ctx
, connect_with_info
->out
.domain_sid
),
603 talloc_free(tmp_ctx
);
607 /* prepare samr_CreateUser2 */
608 ZERO_STRUCTP(u_handle
);
609 cu
.in
.domain_handle
= &d_handle
;
610 cu
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
611 samr_account_name
.string
= r
->in
.account_name
;
612 cu
.in
.account_name
= &samr_account_name
;
613 cu
.in
.acct_flags
= r
->in
.acct_type
;
614 cu
.out
.user_handle
= u_handle
;
616 cu
.out
.access_granted
= &access_granted
;
618 /* do a samr_CreateUser2 to get an account handle, or an error */
619 cu_status
= dcerpc_samr_CreateUser2_r(samr_pipe
->binding_handle
, tmp_ctx
, &cu
);
620 if (NT_STATUS_IS_OK(cu_status
) && !NT_STATUS_IS_OK(cu
.out
.result
)) {
621 cu_status
= cu
.out
.result
;
624 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
625 /* prepare samr_LookupNames */
626 ln
.in
.domain_handle
= &d_handle
;
628 ln
.in
.names
= talloc_array(tmp_ctx
, struct lsa_String
, 1);
630 ln
.out
.types
= &types
;
632 r
->out
.error_string
= NULL
;
633 talloc_free(tmp_ctx
);
634 return NT_STATUS_NO_MEMORY
;
636 ln
.in
.names
[0].string
= r
->in
.account_name
;
638 /* 5. do a samr_LookupNames to get the users rid */
639 status
= dcerpc_samr_LookupNames_r(samr_pipe
->binding_handle
, tmp_ctx
, &ln
);
640 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(ln
.out
.result
)) {
641 status
= ln
.out
.result
;
643 if (!NT_STATUS_IS_OK(status
)) {
644 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
645 "samr_LookupNames for [%s] failed: %s",
648 talloc_free(tmp_ctx
);
652 /* check if we got one RID for the user */
653 if (ln
.out
.rids
->count
!= 1) {
654 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
655 "samr_LookupNames for [%s] returns %d RIDs",
656 r
->in
.account_name
, ln
.out
.rids
->count
);
657 talloc_free(tmp_ctx
);
658 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
661 if (ln
.out
.types
->count
!= 1) {
662 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
663 "samr_LookupNames for [%s] returns %d RID TYPEs",
664 r
->in
.account_name
, ln
.out
.types
->count
);
665 talloc_free(tmp_ctx
);
666 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
669 /* prepare samr_OpenUser */
670 ZERO_STRUCTP(u_handle
);
671 ou
.in
.domain_handle
= &d_handle
;
672 ou
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
673 ou
.in
.rid
= ln
.out
.rids
->ids
[0];
675 ou
.out
.user_handle
= u_handle
;
677 /* 6. do a samr_OpenUser to get a user handle */
678 status
= dcerpc_samr_OpenUser_r(samr_pipe
->binding_handle
, tmp_ctx
, &ou
);
679 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(ou
.out
.result
)) {
680 status
= ou
.out
.result
;
682 if (!NT_STATUS_IS_OK(status
)) {
683 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
684 "samr_OpenUser for [%s] failed: %s",
687 talloc_free(tmp_ctx
);
691 if (r
->in
.recreate_account
) {
692 struct samr_DeleteUser d
;
693 d
.in
.user_handle
= u_handle
;
694 d
.out
.user_handle
= u_handle
;
695 status
= dcerpc_samr_DeleteUser_r(samr_pipe
->binding_handle
, mem_ctx
, &d
);
696 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(d
.out
.result
)) {
697 status
= d
.out
.result
;
699 if (!NT_STATUS_IS_OK(status
)) {
700 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
701 "samr_DeleteUser (for recreate) of [%s] failed: %s",
704 talloc_free(tmp_ctx
);
708 /* We want to recreate, so delete and another samr_CreateUser2 */
710 /* &cu filled in above */
711 status
= dcerpc_samr_CreateUser2_r(samr_pipe
->binding_handle
, tmp_ctx
, &cu
);
712 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(cu
.out
.result
)) {
713 status
= cu
.out
.result
;
715 if (!NT_STATUS_IS_OK(status
)) {
716 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
717 "samr_CreateUser2 (recreate) for [%s] failed: %s",
718 r
->in
.account_name
, nt_errstr(status
));
719 talloc_free(tmp_ctx
);
723 } else if (!NT_STATUS_IS_OK(status
)) {
724 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
725 "samr_CreateUser2 for [%s] failed: %s",
726 r
->in
.account_name
, nt_errstr(status
));
727 talloc_free(tmp_ctx
);
731 /* prepare samr_QueryUserInfo (get flags) */
732 qui
.in
.user_handle
= u_handle
;
734 qui
.out
.info
= &uinfo
;
736 status
= dcerpc_samr_QueryUserInfo_r(samr_pipe
->binding_handle
, tmp_ctx
, &qui
);
737 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(qui
.out
.result
)) {
738 status
= qui
.out
.result
;
740 if (!NT_STATUS_IS_OK(status
)) {
741 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
742 "samr_QueryUserInfo for [%s] failed: %s",
745 talloc_free(tmp_ctx
);
750 status
= NT_STATUS_INVALID_PARAMETER
;
752 = talloc_asprintf(mem_ctx
,
753 "samr_QueryUserInfo failed to return qui.out.info for [%s]: %s",
754 r
->in
.account_name
, nt_errstr(status
));
755 talloc_free(tmp_ctx
);
759 old_acct_flags
= (uinfo
->info16
.acct_flags
& (ACB_WSTRUST
| ACB_SVRTRUST
| ACB_DOMTRUST
));
760 /* Possibly bail if the account is of the wrong type */
762 != r
->in
.acct_type
) {
763 const char *old_account_type
, *new_account_type
;
764 switch (old_acct_flags
) {
766 old_account_type
= "domain member (member)";
769 old_account_type
= "domain controller (bdc)";
772 old_account_type
= "trusted domain";
775 return NT_STATUS_INVALID_PARAMETER
;
777 switch (r
->in
.acct_type
) {
779 new_account_type
= "domain member (member)";
782 new_account_type
= "domain controller (bdc)";
785 new_account_type
= "trusted domain";
788 return NT_STATUS_INVALID_PARAMETER
;
791 if (!NT_STATUS_EQUAL(cu_status
, NT_STATUS_USER_EXISTS
)) {
792 /* We created a new user, but they didn't come out the right type?!? */
794 = talloc_asprintf(mem_ctx
,
795 "We asked to create a new machine account (%s) of type %s, "
796 "but we got an account of type %s. This is unexpected. "
797 "Perhaps delete the account and try again.",
798 r
->in
.account_name
, new_account_type
, old_account_type
);
799 talloc_free(tmp_ctx
);
800 return NT_STATUS_INVALID_PARAMETER
;
802 /* The account is of the wrong type, so bail */
804 /* TODO: We should allow a --force option to override, and redo this from the top setting r.in.recreate_account */
806 = talloc_asprintf(mem_ctx
,
807 "The machine account (%s) already exists in the domain %s, "
808 "but is a %s. You asked to join as a %s. Please delete "
809 "the account and try again.",
810 r
->in
.account_name
, connect_with_info
->out
.domain_name
, old_account_type
, new_account_type
);
811 talloc_free(tmp_ctx
);
812 return NT_STATUS_USER_EXISTS
;
815 acct_flags
= uinfo
->info16
.acct_flags
;
818 acct_flags
= (acct_flags
& ~(ACB_DISABLED
|ACB_PWNOTREQ
));
820 /* Find out what password policy this user has */
821 pwp
.in
.user_handle
= u_handle
;
822 pwp
.out
.info
= &info
;
824 status
= dcerpc_samr_GetUserPwInfo_r(samr_pipe
->binding_handle
, tmp_ctx
, &pwp
);
825 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(pwp
.out
.result
)) {
826 status
= pwp
.out
.result
;
828 if (NT_STATUS_IS_OK(status
)) {
829 policy_min_pw_len
= pwp
.out
.info
->min_password_length
;
832 if (r
->in
.account_pass
!= NULL
) {
833 password_str
= talloc_strdup(tmp_ctx
, r
->in
.account_pass
);
835 /* Grab a password of that minimum length */
836 password_str
= generate_random_password(tmp_ctx
,
837 MAX(8, policy_min_pw_len
), 255);
840 r
->out
.error_string
= NULL
;
841 talloc_free(tmp_ctx
);
842 return NT_STATUS_NO_MEMORY
;
845 /* set full_name and reset flags */
846 ZERO_STRUCT(u_info21
);
847 u_info21
.full_name
.string
= r
->in
.account_name
;
848 u_info21
.acct_flags
= acct_flags
;
849 u_info21
.fields_present
= SAMR_FIELD_FULL_NAME
| SAMR_FIELD_ACCT_FLAGS
;
851 r2
.samr_handle
.level
= LIBNET_SET_PASSWORD_SAMR_HANDLE
;
852 r2
.samr_handle
.in
.account_name
= r
->in
.account_name
;
853 r2
.samr_handle
.in
.newpassword
= password_str
;
854 r2
.samr_handle
.in
.user_handle
= u_handle
;
855 r2
.samr_handle
.in
.dcerpc_pipe
= samr_pipe
;
856 r2
.samr_handle
.in
.info21
= &u_info21
;
858 status
= libnet_SetPassword(ctx
, tmp_ctx
, &r2
);
859 if (!NT_STATUS_IS_OK(status
)) {
860 r
->out
.error_string
= talloc_steal(mem_ctx
, r2
.samr_handle
.out
.error_string
);
861 talloc_free(tmp_ctx
);
865 account_sid
= dom_sid_add_rid(mem_ctx
, connect_with_info
->out
.domain_sid
, rid
);
867 r
->out
.error_string
= NULL
;
868 talloc_free(tmp_ctx
);
869 return NT_STATUS_NO_MEMORY
;
872 /* Finish out by pushing various bits of status data out for the caller to use */
873 r
->out
.join_password
= password_str
;
874 talloc_steal(mem_ctx
, r
->out
.join_password
);
876 r
->out
.domain_sid
= connect_with_info
->out
.domain_sid
;
877 talloc_steal(mem_ctx
, r
->out
.domain_sid
);
879 r
->out
.account_sid
= account_sid
;
880 talloc_steal(mem_ctx
, r
->out
.account_sid
);
882 r
->out
.domain_name
= connect_with_info
->out
.domain_name
;
883 talloc_steal(mem_ctx
, r
->out
.domain_name
);
884 r
->out
.realm
= connect_with_info
->out
.realm
;
885 talloc_steal(mem_ctx
, r
->out
.realm
);
886 r
->out
.samr_pipe
= samr_pipe
;
887 talloc_reparent(tmp_ctx
, mem_ctx
, samr_pipe
);
888 r
->out
.samr_binding
= samr_pipe
->binding
;
889 talloc_steal(mem_ctx
, r
->out
.samr_binding
);
890 r
->out
.user_handle
= u_handle
;
891 talloc_steal(mem_ctx
, u_handle
);
892 r
->out
.error_string
= r2
.samr_handle
.out
.error_string
;
893 talloc_steal(mem_ctx
, r2
.samr_handle
.out
.error_string
);
895 r
->out
.server_dn_str
= NULL
;
896 talloc_free(tmp_ctx
);
898 /* Now, if it was AD, then we want to start looking changing a
899 * few more things. Otherwise, we are done. */
901 status
= libnet_JoinADSDomain(ctx
, r
);
908 NTSTATUS
libnet_Join_member(struct libnet_context
*ctx
,
910 struct libnet_Join_member
*r
)
914 struct libnet_JoinDomain
*r2
;
915 struct provision_store_self_join_settings
*set_secrets
;
916 uint32_t acct_type
= 0;
917 const char *account_name
;
918 const char *netbios_name
;
919 const char *error_string
;
921 r
->out
.error_string
= NULL
;
923 tmp_mem
= talloc_new(mem_ctx
);
925 return NT_STATUS_NO_MEMORY
;
928 r2
= talloc_zero(tmp_mem
, struct libnet_JoinDomain
);
930 r
->out
.error_string
= NULL
;
931 talloc_free(tmp_mem
);
932 return NT_STATUS_NO_MEMORY
;
935 acct_type
= ACB_WSTRUST
;
937 if (r
->in
.netbios_name
!= NULL
) {
938 netbios_name
= r
->in
.netbios_name
;
940 netbios_name
= talloc_strdup(tmp_mem
, lpcfg_netbios_name(ctx
->lp_ctx
));
942 r
->out
.error_string
= NULL
;
943 talloc_free(tmp_mem
);
944 return NT_STATUS_NO_MEMORY
;
948 account_name
= talloc_asprintf(tmp_mem
, "%s$", netbios_name
);
950 r
->out
.error_string
= NULL
;
951 talloc_free(tmp_mem
);
952 return NT_STATUS_NO_MEMORY
;
958 r2
->in
.domain_name
= r
->in
.domain_name
;
959 r2
->in
.account_name
= account_name
;
960 r2
->in
.netbios_name
= netbios_name
;
961 r2
->in
.level
= LIBNET_JOINDOMAIN_AUTOMATIC
;
962 r2
->in
.acct_type
= acct_type
;
963 r2
->in
.recreate_account
= false;
964 r2
->in
.account_pass
= r
->in
.account_pass
;
965 status
= libnet_JoinDomain(ctx
, r2
, r2
);
966 if (!NT_STATUS_IS_OK(status
)) {
967 r
->out
.error_string
= talloc_steal(mem_ctx
, r2
->out
.error_string
);
968 talloc_free(tmp_mem
);
972 set_secrets
= talloc(tmp_mem
, struct provision_store_self_join_settings
);
974 r
->out
.error_string
= NULL
;
975 talloc_free(tmp_mem
);
976 return NT_STATUS_NO_MEMORY
;
979 ZERO_STRUCTP(set_secrets
);
980 set_secrets
->domain_name
= r2
->out
.domain_name
;
981 set_secrets
->realm
= r2
->out
.realm
;
982 set_secrets
->netbios_name
= netbios_name
;
983 set_secrets
->secure_channel_type
= SEC_CHAN_WKSTA
;
984 set_secrets
->machine_password
= r2
->out
.join_password
;
985 set_secrets
->key_version_number
= r2
->out
.kvno
;
986 set_secrets
->domain_sid
= r2
->out
.domain_sid
;
988 status
= provision_store_self_join(ctx
, ctx
->lp_ctx
, ctx
->event_ctx
, set_secrets
, &error_string
);
989 if (!NT_STATUS_IS_OK(status
)) {
990 r
->out
.error_string
= talloc_steal(mem_ctx
, error_string
);
991 talloc_free(tmp_mem
);
995 /* move all out parameter to the callers TALLOC_CTX */
996 r
->out
.error_string
= NULL
;
997 r
->out
.join_password
= r2
->out
.join_password
;
998 talloc_reparent(r2
, mem_ctx
, r2
->out
.join_password
);
999 r
->out
.domain_sid
= r2
->out
.domain_sid
;
1000 talloc_reparent(r2
, mem_ctx
, r2
->out
.domain_sid
);
1001 r
->out
.domain_name
= r2
->out
.domain_name
;
1002 talloc_reparent(r2
, mem_ctx
, r2
->out
.domain_name
);
1003 talloc_free(tmp_mem
);
1004 return NT_STATUS_OK
;