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 "param/secrets.h"
29 #include "dsdb/samdb/samdb.h"
31 #include "libcli/security/security.h"
32 #include "auth/credentials/credentials.h"
33 #include "auth/credentials/credentials_krb5.h"
34 #include "librpc/gen_ndr/ndr_samr_c.h"
37 * complete a domain join, when joining to a AD domain:
38 * 1.) connect and bind to the DRSUAPI pipe
39 * 2.) do a DsCrackNames() to find the machine account dn
41 * 4.) do an ldap search to find the "msDS-KeyVersionNumber" of the machine account
42 * 5.) set the servicePrincipalName's of the machine account via LDAP, (maybe we should use DsWriteAccountSpn()...)
43 * 6.) do a DsCrackNames() to find the domain dn
44 * 7.) find out Site specific stuff, look at libnet_JoinSite() for details
46 static NTSTATUS
libnet_JoinADSDomain(struct libnet_context
*ctx
, struct libnet_JoinDomain
*r
)
52 const char *realm
= r
->out
.realm
;
54 struct dcerpc_binding
*samr_binding
= r
->out
.samr_binding
;
56 struct dcerpc_pipe
*drsuapi_pipe
;
57 struct dcerpc_binding
*drsuapi_binding
;
58 struct drsuapi_DsBind r_drsuapi_bind
;
59 struct drsuapi_DsCrackNames r_crack_names
;
60 struct drsuapi_DsNameString names
[1];
61 struct policy_handle drsuapi_bind_handle
;
62 struct GUID drsuapi_bind_guid
;
64 struct ldb_context
*remote_ldb
;
65 struct ldb_dn
*account_dn
;
66 const char *account_dn_str
;
67 const char *remote_ldb_url
;
68 struct ldb_result
*res
;
69 struct ldb_message
*msg
;
73 const char * const attrs
[] = {
74 "msDS-KeyVersionNumber",
75 "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",
139 dcerpc_errstr(tmp_ctx
, drsuapi_pipe
->last_fault_code
));
140 talloc_free(tmp_ctx
);
145 "dcerpc_drsuapi_DsBind failed - %s",
147 talloc_free(tmp_ctx
);
150 } else if (!W_ERROR_IS_OK(r_drsuapi_bind
.out
.result
)) {
153 "DsBind failed - %s",
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
.codepage
= 1252; /* western european */
164 r_crack_names
.in
.req
.req1
.language
= 0x00000407; /* german */
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",
184 dcerpc_errstr(tmp_ctx
, drsuapi_pipe
->last_fault_code
));
185 talloc_free(tmp_ctx
);
190 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s",
193 talloc_free(tmp_ctx
);
196 } else if (!W_ERROR_IS_OK(r_crack_names
.out
.result
)) {
199 "DsCrackNames failed - %s", 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
->out
.error_string
= talloc_asprintf(r
, "DsCrackNames failed");
206 talloc_free(tmp_ctx
);
207 return NT_STATUS_INVALID_PARAMETER
;
208 } else if (r_crack_names
.out
.ctr
.ctr1
->array
[0].status
!= DRSUAPI_DS_NAME_STATUS_OK
) {
209 r
->out
.error_string
= talloc_asprintf(r
, "DsCrackNames failed: %d", r_crack_names
.out
.ctr
.ctr1
->array
[0].status
);
210 talloc_free(tmp_ctx
);
211 return NT_STATUS_UNSUCCESSFUL
;
212 } else if (r_crack_names
.out
.ctr
.ctr1
->array
[0].result_name
== NULL
) {
213 r
->out
.error_string
= talloc_asprintf(r
, "DsCrackNames failed: no result name");
214 talloc_free(tmp_ctx
);
215 return NT_STATUS_INVALID_PARAMETER
;
218 /* Store the DN of our machine account. */
219 account_dn_str
= r_crack_names
.out
.ctr
.ctr1
->array
[0].result_name
;
221 /* Now we know the user's DN, open with LDAP, read and modify a few things */
223 remote_ldb_url
= talloc_asprintf(tmp_ctx
, "ldap://%s",
224 drsuapi_binding
->target_hostname
);
225 if (!remote_ldb_url
) {
226 r
->out
.error_string
= NULL
;
227 talloc_free(tmp_ctx
);
228 return NT_STATUS_NO_MEMORY
;
231 remote_ldb
= ldb_wrap_connect(tmp_ctx
, remote_ldb_url
,
232 NULL
, ctx
->cred
, 0, NULL
);
234 r
->out
.error_string
= NULL
;
235 talloc_free(tmp_ctx
);
236 return NT_STATUS_UNSUCCESSFUL
;
239 account_dn
= ldb_dn_new(tmp_ctx
, remote_ldb
, account_dn_str
);
240 if (! ldb_dn_validate(account_dn
)) {
241 r
->out
.error_string
= talloc_asprintf(r
, "Invalid account dn: %s",
243 talloc_free(tmp_ctx
);
244 return NT_STATUS_UNSUCCESSFUL
;
247 /* search for the user's record */
248 ret
= ldb_search(remote_ldb
, account_dn
, LDB_SCOPE_BASE
,
250 if (ret
!= LDB_SUCCESS
) {
251 r
->out
.error_string
= talloc_asprintf(r
, "ldb_search for %s failed - %s",
252 account_dn_str
, ldb_errstring(remote_ldb
));
253 talloc_free(tmp_ctx
);
254 return NT_STATUS_UNSUCCESSFUL
;
257 talloc_steal(tmp_ctx
, res
);
259 if (res
->count
!= 1) {
260 r
->out
.error_string
= talloc_asprintf(r
, "ldb_search for %s failed - found %d entries",
261 account_dn_str
, res
->count
);
262 talloc_free(tmp_ctx
);
263 return NT_STATUS_UNSUCCESSFUL
;
266 /* Prepare a new message, for the modify */
267 msg
= ldb_msg_new(tmp_ctx
);
269 r
->out
.error_string
= NULL
;
270 talloc_free(tmp_ctx
);
271 return NT_STATUS_NO_MEMORY
;
273 msg
->dn
= res
->msgs
[0]->dn
;
277 const char *service_principal_name
[6];
278 const char *dns_host_name
= strlower_talloc(tmp_ctx
,
279 talloc_asprintf(tmp_ctx
,
284 if (!dns_host_name
) {
285 r
->out
.error_string
= NULL
;
286 talloc_free(tmp_ctx
);
287 return NT_STATUS_NO_MEMORY
;
290 service_principal_name
[0] = talloc_asprintf(tmp_ctx
, "host/%s", dns_host_name
);
291 service_principal_name
[1] = talloc_asprintf(tmp_ctx
, "host/%s", strlower_talloc(tmp_ctx
, r
->in
.netbios_name
));
292 service_principal_name
[2] = talloc_asprintf(tmp_ctx
, "host/%s/%s", dns_host_name
, realm
);
293 service_principal_name
[3] = talloc_asprintf(tmp_ctx
, "host/%s/%s", strlower_talloc(tmp_ctx
, r
->in
.netbios_name
), realm
);
294 service_principal_name
[4] = talloc_asprintf(tmp_ctx
, "host/%s/%s", dns_host_name
, r
->out
.domain_name
);
295 service_principal_name
[5] = talloc_asprintf(tmp_ctx
, "host/%s/%s", strlower_talloc(tmp_ctx
, r
->in
.netbios_name
), r
->out
.domain_name
);
297 for (i
=0; i
< ARRAY_SIZE(service_principal_name
); i
++) {
298 if (!service_principal_name
[i
]) {
299 r
->out
.error_string
= NULL
;
300 talloc_free(tmp_ctx
);
301 return NT_STATUS_NO_MEMORY
;
303 rtn
= samdb_msg_add_string(remote_ldb
, tmp_ctx
, msg
, "servicePrincipalName", service_principal_name
[i
]);
305 r
->out
.error_string
= NULL
;
306 talloc_free(tmp_ctx
);
307 return NT_STATUS_NO_MEMORY
;
311 rtn
= samdb_msg_add_string(remote_ldb
, tmp_ctx
, msg
, "dNSHostName", dns_host_name
);
313 r
->out
.error_string
= NULL
;
314 talloc_free(tmp_ctx
);
315 return NT_STATUS_NO_MEMORY
;
318 rtn
= samdb_replace(remote_ldb
, tmp_ctx
, msg
);
322 "Failed to replace entries on %s",
323 ldb_dn_get_linearized(msg
->dn
));
324 talloc_free(tmp_ctx
);
325 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
329 /* DsCrackNames to find out the DN of the domain. */
330 r_crack_names
.in
.req
.req1
.format_offered
= DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT
;
331 r_crack_names
.in
.req
.req1
.format_desired
= DRSUAPI_DS_NAME_FORMAT_FQDN_1779
;
332 names
[0].str
= talloc_asprintf(tmp_ctx
, "%s\\", r
->out
.domain_name
);
334 r
->out
.error_string
= NULL
;
335 talloc_free(tmp_ctx
);
336 return NT_STATUS_NO_MEMORY
;
339 status
= dcerpc_drsuapi_DsCrackNames(drsuapi_pipe
, tmp_ctx
, &r_crack_names
);
340 if (!NT_STATUS_IS_OK(status
)) {
341 if (NT_STATUS_EQUAL(status
, NT_STATUS_NET_WRITE_FAULT
)) {
344 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s",
346 dcerpc_errstr(tmp_ctx
, drsuapi_pipe
->last_fault_code
));
347 talloc_free(tmp_ctx
);
352 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s",
355 talloc_free(tmp_ctx
);
358 } else if (!W_ERROR_IS_OK(r_crack_names
.out
.result
)) {
361 "DsCrackNames failed - %s", win_errstr(r_crack_names
.out
.result
));
362 talloc_free(tmp_ctx
);
363 return NT_STATUS_UNSUCCESSFUL
;
364 } else if (r_crack_names
.out
.level
!= 1
365 || !r_crack_names
.out
.ctr
.ctr1
366 || r_crack_names
.out
.ctr
.ctr1
->count
!= 1
367 || !r_crack_names
.out
.ctr
.ctr1
->array
[0].result_name
368 || r_crack_names
.out
.ctr
.ctr1
->array
[0].status
!= DRSUAPI_DS_NAME_STATUS_OK
) {
369 r
->out
.error_string
= talloc_asprintf(r
, "DsCrackNames failed");
370 talloc_free(tmp_ctx
);
371 return NT_STATUS_UNSUCCESSFUL
;
374 /* Store the account DN. */
375 r
->out
.account_dn_str
= account_dn_str
;
376 talloc_steal(r
, account_dn_str
);
378 /* Store the domain DN. */
379 r
->out
.domain_dn_str
= r_crack_names
.out
.ctr
.ctr1
->array
[0].result_name
;
380 talloc_steal(r
, r_crack_names
.out
.ctr
.ctr1
->array
[0].result_name
);
382 /* Store the KVNO of the account, critical for some kerberos
384 r
->out
.kvno
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "msDS-KeyVersionNumber", 0);
386 /* Store the account GUID. */
387 r
->out
.account_guid
= samdb_result_guid(res
->msgs
[0], "objectGUID");
389 if (r
->in
.acct_type
== ACB_SVRTRUST
) {
390 status
= libnet_JoinSite(remote_ldb
, r
);
392 talloc_free(tmp_ctx
);
398 * do a domain join using DCERPC/SAMR calls
399 * - connect to the LSA pipe, to try and find out information about the domain
400 * - create a secondary connection to SAMR pipe
401 * - do a samr_Connect to get a policy handle
402 * - do a samr_LookupDomain to get the domain sid
403 * - do a samr_OpenDomain to get a domain handle
404 * - do a samr_CreateAccount to try and get a new account
407 * - do a samr_LookupNames to get the users rid
408 * - do a samr_OpenUser to get a user handle
409 * - potentially delete and recreate the user
410 * - assert the account is of the right type with samrQueryUserInfo
412 * - call libnet_SetPassword_samr_handle to set the password,
413 * and pass a samr_UserInfo21 struct to set full_name and the account flags
415 * - do some ADS specific things when we join as Domain Controller,
416 * look at libnet_joinADSDomain() for the details
418 NTSTATUS
libnet_JoinDomain(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
, struct libnet_JoinDomain
*r
)
422 NTSTATUS status
, cu_status
;
424 struct libnet_RpcConnect
*connect_with_info
;
425 struct dcerpc_pipe
*samr_pipe
;
427 struct samr_Connect sc
;
428 struct policy_handle p_handle
;
429 struct samr_OpenDomain od
;
430 struct policy_handle d_handle
;
431 struct samr_LookupNames ln
;
432 struct samr_OpenUser ou
;
433 struct samr_CreateUser2 cu
;
434 struct policy_handle
*u_handle
= NULL
;
435 struct samr_QueryUserInfo qui
;
436 struct samr_UserInfo21 u_info21
;
437 union libnet_SetPassword r2
;
438 struct samr_GetUserPwInfo pwp
;
439 struct lsa_String samr_account_name
;
441 uint32_t acct_flags
, old_acct_flags
;
442 uint32_t rid
, access_granted
;
443 int policy_min_pw_len
= 0;
445 struct dom_sid
*account_sid
= NULL
;
446 const char *password_str
= NULL
;
448 r
->out
.error_string
= NULL
;
449 r2
.samr_handle
.out
.error_string
= NULL
;
451 tmp_ctx
= talloc_named(mem_ctx
, 0, "libnet_Join temp context");
453 r
->out
.error_string
= NULL
;
454 return NT_STATUS_NO_MEMORY
;
457 u_handle
= talloc(tmp_ctx
, struct policy_handle
);
459 r
->out
.error_string
= NULL
;
460 talloc_free(tmp_ctx
);
461 return NT_STATUS_NO_MEMORY
;
464 connect_with_info
= talloc(tmp_ctx
, struct libnet_RpcConnect
);
465 if (!connect_with_info
) {
466 r
->out
.error_string
= NULL
;
467 talloc_free(tmp_ctx
);
468 return NT_STATUS_NO_MEMORY
;
471 /* prepare connect to the SAMR pipe of PDC */
472 if (r
->in
.level
== LIBNET_JOINDOMAIN_AUTOMATIC
) {
473 connect_with_info
->in
.binding
= NULL
;
474 connect_with_info
->in
.name
= r
->in
.domain_name
;
476 connect_with_info
->in
.binding
= r
->in
.binding
;
477 connect_with_info
->in
.name
= NULL
;
480 /* This level makes a connection to the LSA pipe on the way,
481 * to get some useful bits of information about the domain */
482 connect_with_info
->level
= LIBNET_RPC_CONNECT_DC_INFO
;
483 connect_with_info
->in
.dcerpc_iface
= &dcerpc_table_samr
;
486 establish the SAMR connection
488 status
= libnet_RpcConnect(ctx
, tmp_ctx
, connect_with_info
);
489 if (!NT_STATUS_IS_OK(status
)) {
491 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
492 "Connection to SAMR pipe of DC %s failed: %s",
493 r
->in
.binding
, connect_with_info
->out
.error_string
);
495 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
496 "Connection to SAMR pipe of PDC for %s failed: %s",
497 r
->in
.domain_name
, connect_with_info
->out
.error_string
);
499 talloc_free(tmp_ctx
);
503 samr_pipe
= connect_with_info
->out
.dcerpc_pipe
;
505 status
= dcerpc_pipe_auth(tmp_ctx
, &samr_pipe
,
506 connect_with_info
->out
.dcerpc_pipe
->binding
,
507 &dcerpc_table_samr
, ctx
->cred
);
508 if (!NT_STATUS_IS_OK(status
)) {
509 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
510 "SAMR bind failed: %s",
512 talloc_free(tmp_ctx
);
516 /* prepare samr_Connect */
517 ZERO_STRUCT(p_handle
);
518 sc
.in
.system_name
= NULL
;
519 sc
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
520 sc
.out
.connect_handle
= &p_handle
;
522 /* 2. do a samr_Connect to get a policy handle */
523 status
= dcerpc_samr_Connect(samr_pipe
, tmp_ctx
, &sc
);
524 if (!NT_STATUS_IS_OK(status
)) {
525 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
526 "samr_Connect failed: %s",
528 talloc_free(tmp_ctx
);
532 /* If this is a connection on ncacn_ip_tcp to Win2k3 SP1, we don't get back this useful info */
533 if (!connect_with_info
->out
.domain_name
) {
534 if (r
->in
.level
== LIBNET_JOINDOMAIN_AUTOMATIC
) {
535 connect_with_info
->out
.domain_name
= talloc_strdup(tmp_ctx
, r
->in
.domain_name
);
537 /* Bugger, we just lost our way to automaticly find the domain name */
538 connect_with_info
->out
.domain_name
= talloc_strdup(tmp_ctx
, lp_workgroup());
539 connect_with_info
->out
.realm
= talloc_strdup(tmp_ctx
, lp_realm());
543 /* Perhaps we didn't get a SID above, because we are against ncacn_ip_tcp */
544 if (!connect_with_info
->out
.domain_sid
) {
545 struct lsa_String name
;
546 struct samr_LookupDomain l
;
547 name
.string
= connect_with_info
->out
.domain_name
;
548 l
.in
.connect_handle
= &p_handle
;
549 l
.in
.domain_name
= &name
;
551 status
= dcerpc_samr_LookupDomain(samr_pipe
, tmp_ctx
, &l
);
552 if (!NT_STATUS_IS_OK(status
)) {
553 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
554 "SAMR LookupDomain failed: %s",
556 talloc_free(tmp_ctx
);
559 connect_with_info
->out
.domain_sid
= l
.out
.sid
;
562 /* prepare samr_OpenDomain */
563 ZERO_STRUCT(d_handle
);
564 od
.in
.connect_handle
= &p_handle
;
565 od
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
566 od
.in
.sid
= connect_with_info
->out
.domain_sid
;
567 od
.out
.domain_handle
= &d_handle
;
569 /* do a samr_OpenDomain to get a domain handle */
570 status
= dcerpc_samr_OpenDomain(samr_pipe
, tmp_ctx
, &od
);
571 if (!NT_STATUS_IS_OK(status
)) {
572 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
573 "samr_OpenDomain for [%s] failed: %s",
574 dom_sid_string(tmp_ctx
, connect_with_info
->out
.domain_sid
),
576 talloc_free(tmp_ctx
);
580 /* prepare samr_CreateUser2 */
581 ZERO_STRUCTP(u_handle
);
582 cu
.in
.domain_handle
= &d_handle
;
583 cu
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
584 samr_account_name
.string
= r
->in
.account_name
;
585 cu
.in
.account_name
= &samr_account_name
;
586 cu
.in
.acct_flags
= r
->in
.acct_type
;
587 cu
.out
.user_handle
= u_handle
;
589 cu
.out
.access_granted
= &access_granted
;
591 /* do a samr_CreateUser2 to get an account handle, or an error */
592 cu_status
= dcerpc_samr_CreateUser2(samr_pipe
, tmp_ctx
, &cu
);
594 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
595 /* prepare samr_LookupNames */
596 ln
.in
.domain_handle
= &d_handle
;
598 ln
.in
.names
= talloc_array(tmp_ctx
, struct lsa_String
, 1);
600 r
->out
.error_string
= NULL
;
601 talloc_free(tmp_ctx
);
602 return NT_STATUS_NO_MEMORY
;
604 ln
.in
.names
[0].string
= r
->in
.account_name
;
606 /* 5. do a samr_LookupNames to get the users rid */
607 status
= dcerpc_samr_LookupNames(samr_pipe
, tmp_ctx
, &ln
);
608 if (!NT_STATUS_IS_OK(status
)) {
609 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
610 "samr_LookupNames for [%s] failed: %s",
613 talloc_free(tmp_ctx
);
617 /* check if we got one RID for the user */
618 if (ln
.out
.rids
.count
!= 1) {
619 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
620 "samr_LookupNames for [%s] returns %d RIDs",
621 r
->in
.account_name
, ln
.out
.rids
.count
);
622 talloc_free(tmp_ctx
);
623 return NT_STATUS_INVALID_PARAMETER
;
626 /* prepare samr_OpenUser */
627 ZERO_STRUCTP(u_handle
);
628 ou
.in
.domain_handle
= &d_handle
;
629 ou
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
630 ou
.in
.rid
= ln
.out
.rids
.ids
[0];
632 ou
.out
.user_handle
= u_handle
;
634 /* 6. do a samr_OpenUser to get a user handle */
635 status
= dcerpc_samr_OpenUser(samr_pipe
, tmp_ctx
, &ou
);
636 if (!NT_STATUS_IS_OK(status
)) {
637 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
638 "samr_OpenUser for [%s] failed: %s",
641 talloc_free(tmp_ctx
);
645 if (r
->in
.recreate_account
) {
646 struct samr_DeleteUser d
;
647 d
.in
.user_handle
= u_handle
;
648 d
.out
.user_handle
= u_handle
;
649 status
= dcerpc_samr_DeleteUser(samr_pipe
, mem_ctx
, &d
);
650 if (!NT_STATUS_IS_OK(status
)) {
651 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
652 "samr_DeleteUser (for recreate) of [%s] failed: %s",
655 talloc_free(tmp_ctx
);
659 /* We want to recreate, so delete and another samr_CreateUser2 */
661 /* &cu filled in above */
662 status
= dcerpc_samr_CreateUser2(samr_pipe
, tmp_ctx
, &cu
);
663 if (!NT_STATUS_IS_OK(status
)) {
664 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
665 "samr_CreateUser2 (recreate) for [%s] failed: %s",
666 r
->in
.account_name
, nt_errstr(status
));
667 talloc_free(tmp_ctx
);
671 } else if (!NT_STATUS_IS_OK(status
)) {
672 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
673 "samr_CreateUser2 for [%s] failed: %s",
674 r
->in
.account_name
, nt_errstr(status
));
675 talloc_free(tmp_ctx
);
679 /* prepare samr_QueryUserInfo (get flags) */
680 qui
.in
.user_handle
= u_handle
;
683 status
= dcerpc_samr_QueryUserInfo(samr_pipe
, tmp_ctx
, &qui
);
684 if (!NT_STATUS_IS_OK(status
)) {
685 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
686 "samr_QueryUserInfo for [%s] failed: %s",
689 talloc_free(tmp_ctx
);
694 status
= NT_STATUS_INVALID_PARAMETER
;
696 = talloc_asprintf(mem_ctx
,
697 "samr_QueryUserInfo failed to return qui.out.info for [%s]: %s",
698 r
->in
.account_name
, nt_errstr(status
));
699 talloc_free(tmp_ctx
);
703 old_acct_flags
= (qui
.out
.info
->info16
.acct_flags
& (ACB_WSTRUST
| ACB_SVRTRUST
| ACB_DOMTRUST
));
704 /* Possibly bail if the account is of the wrong type */
706 != r
->in
.acct_type
) {
707 const char *old_account_type
, *new_account_type
;
708 switch (old_acct_flags
) {
710 old_account_type
= "domain member (member)";
713 old_account_type
= "domain controller (bdc)";
716 old_account_type
= "trusted domain";
719 return NT_STATUS_INVALID_PARAMETER
;
721 switch (r
->in
.acct_type
) {
723 new_account_type
= "domain member (member)";
726 new_account_type
= "domain controller (bdc)";
729 new_account_type
= "trusted domain";
732 return NT_STATUS_INVALID_PARAMETER
;
735 if (!NT_STATUS_EQUAL(cu_status
, NT_STATUS_USER_EXISTS
)) {
736 /* We created a new user, but they didn't come out the right type?!? */
738 = talloc_asprintf(mem_ctx
,
739 "We asked to create a new machine account (%s) of type %s, "
740 "but we got an account of type %s. This is unexpected. "
741 "Perhaps delete the account and try again.",
742 r
->in
.account_name
, new_account_type
, old_account_type
);
743 talloc_free(tmp_ctx
);
744 return NT_STATUS_INVALID_PARAMETER
;
746 /* The account is of the wrong type, so bail */
748 /* TODO: We should allow a --force option to override, and redo this from the top setting r.in.recreate_account */
750 = talloc_asprintf(mem_ctx
,
751 "The machine account (%s) already exists in the domain %s, "
752 "but is a %s. You asked to join as a %s. Please delete "
753 "the account and try again.",
754 r
->in
.account_name
, connect_with_info
->out
.domain_name
, old_account_type
, new_account_type
);
755 talloc_free(tmp_ctx
);
756 return NT_STATUS_USER_EXISTS
;
759 acct_flags
= qui
.out
.info
->info16
.acct_flags
;
762 acct_flags
= (acct_flags
& ~(ACB_DISABLED
|ACB_PWNOTREQ
));
764 /* Find out what password policy this user has */
765 pwp
.in
.user_handle
= u_handle
;
767 status
= dcerpc_samr_GetUserPwInfo(samr_pipe
, tmp_ctx
, &pwp
);
768 if (NT_STATUS_IS_OK(status
)) {
769 policy_min_pw_len
= pwp
.out
.info
.min_password_length
;
772 /* Grab a password of that minimum length */
774 password_str
= generate_random_str(tmp_ctx
, MAX(8, policy_min_pw_len
));
776 /* set full_name and reset flags */
777 ZERO_STRUCT(u_info21
);
778 u_info21
.full_name
.string
= r
->in
.account_name
;
779 u_info21
.acct_flags
= acct_flags
;
780 u_info21
.fields_present
= SAMR_FIELD_FULL_NAME
| SAMR_FIELD_ACCT_FLAGS
;
782 r2
.samr_handle
.level
= LIBNET_SET_PASSWORD_SAMR_HANDLE
;
783 r2
.samr_handle
.in
.account_name
= r
->in
.account_name
;
784 r2
.samr_handle
.in
.newpassword
= password_str
;
785 r2
.samr_handle
.in
.user_handle
= u_handle
;
786 r2
.samr_handle
.in
.dcerpc_pipe
= samr_pipe
;
787 r2
.samr_handle
.in
.info21
= &u_info21
;
789 status
= libnet_SetPassword(ctx
, tmp_ctx
, &r2
);
790 if (!NT_STATUS_IS_OK(status
)) {
791 r
->out
.error_string
= talloc_steal(mem_ctx
, r2
.samr_handle
.out
.error_string
);
792 talloc_free(tmp_ctx
);
796 account_sid
= dom_sid_add_rid(mem_ctx
, connect_with_info
->out
.domain_sid
, rid
);
798 r
->out
.error_string
= NULL
;
799 talloc_free(tmp_ctx
);
800 return NT_STATUS_NO_MEMORY
;
803 /* Finish out by pushing various bits of status data out for the caller to use */
804 r
->out
.join_password
= password_str
;
805 talloc_steal(mem_ctx
, r
->out
.join_password
);
807 r
->out
.domain_sid
= connect_with_info
->out
.domain_sid
;
808 talloc_steal(mem_ctx
, r
->out
.domain_sid
);
810 r
->out
.account_sid
= account_sid
;
811 talloc_steal(mem_ctx
, r
->out
.account_sid
);
813 r
->out
.domain_name
= connect_with_info
->out
.domain_name
;
814 talloc_steal(mem_ctx
, r
->out
.domain_name
);
815 r
->out
.realm
= connect_with_info
->out
.realm
;
816 talloc_steal(mem_ctx
, r
->out
.realm
);
817 r
->out
.samr_pipe
= samr_pipe
;
818 talloc_steal(mem_ctx
, samr_pipe
);
819 r
->out
.samr_binding
= samr_pipe
->binding
;
820 talloc_steal(mem_ctx
, r
->out
.samr_binding
);
821 r
->out
.user_handle
= u_handle
;
822 talloc_steal(mem_ctx
, u_handle
);
823 r
->out
.error_string
= r2
.samr_handle
.out
.error_string
;
824 talloc_steal(mem_ctx
, r2
.samr_handle
.out
.error_string
);
826 r
->out
.server_dn_str
= NULL
;
827 talloc_free(tmp_ctx
);
829 /* Now, if it was AD, then we want to start looking changing a
830 * few more things. Otherwise, we are done. */
832 status
= libnet_JoinADSDomain(ctx
, r
);
839 static NTSTATUS
libnet_Join_primary_domain(struct libnet_context
*ctx
,
841 struct libnet_Join
*r
)
845 struct libnet_JoinDomain
*r2
;
847 struct ldb_context
*ldb
;
848 struct ldb_dn
*base_dn
;
849 struct ldb_message
**msgs
, *msg
;
851 const char * const attrs
[] = {
860 uint32_t acct_type
= 0;
861 const char *account_name
;
862 const char *netbios_name
;
864 r
->out
.error_string
= NULL
;
866 tmp_mem
= talloc_new(mem_ctx
);
868 return NT_STATUS_NO_MEMORY
;
871 r2
= talloc(tmp_mem
, struct libnet_JoinDomain
);
873 r
->out
.error_string
= NULL
;
874 talloc_free(tmp_mem
);
875 return NT_STATUS_NO_MEMORY
;
878 if (r
->in
.join_type
== SEC_CHAN_BDC
) {
879 acct_type
= ACB_SVRTRUST
;
880 } else if (r
->in
.join_type
== SEC_CHAN_WKSTA
) {
881 acct_type
= ACB_WSTRUST
;
883 r
->out
.error_string
= NULL
;
884 talloc_free(tmp_mem
);
885 return NT_STATUS_INVALID_PARAMETER
;
888 if (r
->in
.netbios_name
!= NULL
) {
889 netbios_name
= r
->in
.netbios_name
;
891 netbios_name
= talloc_reference(tmp_mem
, lp_netbios_name());
893 r
->out
.error_string
= NULL
;
894 talloc_free(tmp_mem
);
895 return NT_STATUS_NO_MEMORY
;
899 account_name
= talloc_asprintf(tmp_mem
, "%s$", netbios_name
);
901 r
->out
.error_string
= NULL
;
902 talloc_free(tmp_mem
);
903 return NT_STATUS_NO_MEMORY
;
907 * Local secrets are stored in secrets.ldb
908 * open it to make sure we can write the info into it after the join
910 ldb
= secrets_db_connect(tmp_mem
);
913 = talloc_asprintf(mem_ctx
,
914 "Could not open secrets database");
915 talloc_free(tmp_mem
);
916 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
923 r2
->in
.domain_name
= r
->in
.domain_name
;
924 r2
->in
.account_name
= account_name
;
925 r2
->in
.netbios_name
= netbios_name
;
926 r2
->in
.level
= LIBNET_JOINDOMAIN_AUTOMATIC
;
927 r2
->in
.acct_type
= acct_type
;
928 r2
->in
.recreate_account
= False
;
929 status
= libnet_JoinDomain(ctx
, r2
, r2
);
930 if (!NT_STATUS_IS_OK(status
)) {
931 r
->out
.error_string
= talloc_steal(mem_ctx
, r2
->out
.error_string
);
932 talloc_free(tmp_mem
);
937 * now prepare the record for secrets.ldb
939 sct
= talloc_asprintf(tmp_mem
, "%d", r
->in
.join_type
);
941 r
->out
.error_string
= NULL
;
942 talloc_free(tmp_mem
);
943 return NT_STATUS_NO_MEMORY
;
946 msg
= ldb_msg_new(tmp_mem
);
948 r
->out
.error_string
= NULL
;
949 talloc_free(tmp_mem
);
950 return NT_STATUS_NO_MEMORY
;
953 base_dn
= ldb_dn_new(tmp_mem
, ldb
, "cn=Primary Domains");
955 r
->out
.error_string
= NULL
;
956 talloc_free(tmp_mem
);
957 return NT_STATUS_NO_MEMORY
;
960 msg
->dn
= ldb_dn_copy(tmp_mem
, base_dn
);
961 if ( ! ldb_dn_add_child_fmt(msg
->dn
, "flatname=%s", r2
->out
.domain_name
)) {
962 r
->out
.error_string
= NULL
;
963 talloc_free(tmp_mem
);
964 return NT_STATUS_NO_MEMORY
;
967 rtn
= samdb_msg_add_string(ldb
, tmp_mem
, msg
, "flatname", r2
->out
.domain_name
);
969 r
->out
.error_string
= NULL
;
970 talloc_free(tmp_mem
);
971 return NT_STATUS_NO_MEMORY
;
975 rtn
= samdb_msg_add_string(ldb
, tmp_mem
, msg
, "realm", r2
->out
.realm
);
977 r
->out
.error_string
= NULL
;
978 talloc_free(tmp_mem
);
979 return NT_STATUS_NO_MEMORY
;
982 rtn
= samdb_msg_add_string(ldb
, tmp_mem
, msg
, "objectClass", "primaryDomain");
984 r
->out
.error_string
= NULL
;
985 talloc_free(tmp_mem
);
986 return NT_STATUS_NO_MEMORY
;
990 rtn
= samdb_msg_add_string(ldb
, tmp_mem
, msg
, "objectClass", "primaryDomain");
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
, "secret", r2
->out
.join_password
);
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
, "samAccountName", r2
->in
.account_name
);
1006 r
->out
.error_string
= NULL
;
1007 talloc_free(tmp_mem
);
1008 return NT_STATUS_NO_MEMORY
;
1011 rtn
= samdb_msg_add_string(ldb
, tmp_mem
, msg
, "secureChannelType", sct
);
1013 r
->out
.error_string
= NULL
;
1014 talloc_free(tmp_mem
);
1015 return NT_STATUS_NO_MEMORY
;
1019 rtn
= samdb_msg_add_uint(ldb
, tmp_mem
, msg
, "msDS-KeyVersionNumber",
1022 r
->out
.error_string
= NULL
;
1023 talloc_free(tmp_mem
);
1024 return NT_STATUS_NO_MEMORY
;
1028 if (r2
->out
.domain_sid
) {
1029 rtn
= samdb_msg_add_dom_sid(ldb
, tmp_mem
, msg
, "objectSid",
1030 r2
->out
.domain_sid
);
1032 r
->out
.error_string
= NULL
;
1033 talloc_free(tmp_mem
);
1034 return NT_STATUS_NO_MEMORY
;
1039 * search for the secret record
1040 * - remove the records we find
1041 * - and fetch the old secret and store it under priorSecret
1043 ret
= gendb_search(ldb
,
1046 "(|" SECRETS_PRIMARY_DOMAIN_FILTER
"(realm=%s))",
1047 r2
->out
.domain_name
, r2
->out
.realm
);
1049 rtn
= samdb_msg_set_string(ldb
, tmp_mem
, msg
, "secretsKeytab", "secrets.keytab");
1051 r
->out
.error_string
= NULL
;
1052 talloc_free(tmp_mem
);
1053 return NT_STATUS_NO_MEMORY
;
1055 } else if (ret
== -1) {
1057 = talloc_asprintf(mem_ctx
,
1058 "Search for domain: %s and realm: %s failed: %s",
1059 r2
->out
.domain_name
, r2
->out
.realm
, ldb_errstring(ldb
));
1060 talloc_free(tmp_mem
);
1061 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
1063 const struct ldb_val
*private_keytab
;
1064 const struct ldb_val
*krb5_keytab
;
1065 const struct ldb_val
*prior_secret
;
1066 const struct ldb_val
*prior_modified_time
;
1069 for (i
= 0; i
< ret
; i
++) {
1070 ldb_delete(ldb
, msgs
[i
]->dn
);
1073 prior_secret
= ldb_msg_find_ldb_val(msgs
[0], "secret");
1075 rtn
= samdb_msg_set_value(ldb
, tmp_mem
, msg
, "priorSecret", prior_secret
);
1077 r
->out
.error_string
= NULL
;
1078 talloc_free(tmp_mem
);
1079 return NT_STATUS_NO_MEMORY
;
1082 rtn
= samdb_msg_set_string(ldb
, tmp_mem
, msg
, "secret", r2
->out
.join_password
);
1084 r
->out
.error_string
= NULL
;
1085 talloc_free(tmp_mem
);
1086 return NT_STATUS_NO_MEMORY
;
1089 prior_modified_time
= ldb_msg_find_ldb_val(msgs
[0],
1091 if (prior_modified_time
) {
1092 rtn
= samdb_msg_set_value(ldb
, tmp_mem
, msg
, "priorWhenChanged",
1093 prior_modified_time
);
1095 r
->out
.error_string
= NULL
;
1096 talloc_free(tmp_mem
);
1097 return NT_STATUS_NO_MEMORY
;
1101 rtn
= samdb_msg_set_string(ldb
, tmp_mem
, msg
, "samAccountName", r2
->in
.account_name
);
1103 r
->out
.error_string
= NULL
;
1104 talloc_free(tmp_mem
);
1105 return NT_STATUS_NO_MEMORY
;
1108 rtn
= samdb_msg_set_string(ldb
, tmp_mem
, msg
, "secureChannelType", sct
);
1110 r
->out
.error_string
= NULL
;
1111 talloc_free(tmp_mem
);
1112 return NT_STATUS_NO_MEMORY
;
1115 /* We will want to keep the keytab names */
1116 private_keytab
= ldb_msg_find_ldb_val(msgs
[0], "privateKeytab");
1117 if (private_keytab
) {
1118 rtn
= samdb_msg_set_value(ldb
, tmp_mem
, msg
, "privateKeytab", private_keytab
);
1120 r
->out
.error_string
= NULL
;
1121 talloc_free(tmp_mem
);
1122 return NT_STATUS_NO_MEMORY
;
1125 krb5_keytab
= ldb_msg_find_ldb_val(msgs
[0], "krb5Keytab");
1127 rtn
= samdb_msg_set_value(ldb
, tmp_mem
, msg
, "krb5Keytab", krb5_keytab
);
1129 r
->out
.error_string
= NULL
;
1130 talloc_free(tmp_mem
);
1131 return NT_STATUS_NO_MEMORY
;
1136 /* create the secret */
1137 ret
= samdb_add(ldb
, tmp_mem
, msg
);
1139 r
->out
.error_string
= talloc_asprintf(mem_ctx
, "Failed to create secret record %s",
1140 ldb_dn_get_linearized(msg
->dn
));
1141 talloc_free(tmp_mem
);
1142 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
1145 /* move all out parameter to the callers TALLOC_CTX */
1146 r
->out
.error_string
= NULL
;
1147 r
->out
.join_password
= r2
->out
.join_password
;
1148 talloc_steal(mem_ctx
, r2
->out
.join_password
);
1149 r
->out
.domain_sid
= r2
->out
.domain_sid
;
1150 talloc_steal(mem_ctx
, r2
->out
.domain_sid
);
1151 r
->out
.domain_name
= r2
->out
.domain_name
;
1152 talloc_steal(mem_ctx
, r2
->out
.domain_name
);
1153 talloc_free(tmp_mem
);
1154 return NT_STATUS_OK
;
1157 NTSTATUS
libnet_Join(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
, struct libnet_Join
*r
)
1159 switch (r
->in
.join_type
) {
1160 case SEC_CHAN_WKSTA
:
1161 return libnet_Join_primary_domain(ctx
, mem_ctx
, r
);
1163 return libnet_Join_primary_domain(ctx
, mem_ctx
, r
);
1164 case SEC_CHAN_DOMAIN
:
1168 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
1169 "Invalid join type specified (%08X) attempting to join domain %s",
1170 r
->in
.join_type
, r
->in
.domain_name
);
1171 return NT_STATUS_INVALID_PARAMETER
;