2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2004
5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
6 Copyright (C) Brad Henry 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "libnet/libnet.h"
25 #include "librpc/gen_ndr/ndr_drsuapi_c.h"
26 #include "lib/ldb/include/ldb.h"
27 #include "lib/ldb/include/ldb_errors.h"
28 #include "passdb/secrets.h"
29 #include "dsdb/samdb/samdb.h"
31 #include "libcli/security/security.h"
32 #include "auth/credentials/credentials.h"
33 #include "librpc/gen_ndr/ndr_samr_c.h"
34 #include "librpc/gen_ndr/ndr_security.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 const 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
;
75 const char * const attrs
[] = {
76 "msDS-KeyVersionNumber",
77 "servicePrincipalName",
82 r
->out
.error_string
= NULL
;
84 /* We need to convert between a samAccountName and domain to a
85 * DN in the directory. The correct way to do this is with
86 * DRSUAPI CrackNames */
88 /* Fiddle with the bindings, so get to DRSUAPI on
89 * NCACN_IP_TCP, sealed */
90 tmp_ctx
= talloc_named(r
, 0, "libnet_JoinADSDomain temp context");
92 r
->out
.error_string
= NULL
;
93 return NT_STATUS_NO_MEMORY
;
96 drsuapi_binding
= talloc(tmp_ctx
, struct dcerpc_binding
);
97 if (!drsuapi_binding
) {
98 r
->out
.error_string
= NULL
;
100 return NT_STATUS_NO_MEMORY
;
103 *drsuapi_binding
= *samr_binding
;
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
,
115 &dcerpc_table_drsuapi
,
118 if (!NT_STATUS_IS_OK(status
)) {
119 r
->out
.error_string
= talloc_asprintf(r
,
120 "Connection to DRSUAPI pipe of PDC of domain '%s' failed: %s",
123 talloc_free(tmp_ctx
);
127 /* get a DRSUAPI pipe handle */
128 GUID_from_string(DRSUAPI_DS_BIND_GUID
, &drsuapi_bind_guid
);
130 r_drsuapi_bind
.in
.bind_guid
= &drsuapi_bind_guid
;
131 r_drsuapi_bind
.in
.bind_info
= NULL
;
132 r_drsuapi_bind
.out
.bind_handle
= &drsuapi_bind_handle
;
134 status
= dcerpc_drsuapi_DsBind(drsuapi_pipe
, tmp_ctx
, &r_drsuapi_bind
);
135 if (!NT_STATUS_IS_OK(status
)) {
136 if (NT_STATUS_EQUAL(status
, NT_STATUS_NET_WRITE_FAULT
)) {
139 "dcerpc_drsuapi_DsBind failed - %s\n",
140 dcerpc_errstr(tmp_ctx
, drsuapi_pipe
->last_fault_code
));
141 talloc_free(tmp_ctx
);
146 "dcerpc_drsuapi_DsBind failed - %s\n",
148 talloc_free(tmp_ctx
);
151 } else if (!W_ERROR_IS_OK(r_drsuapi_bind
.out
.result
)) {
154 "DsBind failed - %s\n",
155 win_errstr(r_drsuapi_bind
.out
.result
));
156 talloc_free(tmp_ctx
);
157 return NT_STATUS_UNSUCCESSFUL
;
160 /* Actually 'crack' the names */
161 ZERO_STRUCT(r_crack_names
);
162 r_crack_names
.in
.bind_handle
= &drsuapi_bind_handle
;
163 r_crack_names
.in
.level
= 1;
164 r_crack_names
.in
.req
.req1
.unknown1
= 0x000004e4;
165 r_crack_names
.in
.req
.req1
.unknown2
= 0x00000407;
166 r_crack_names
.in
.req
.req1
.count
= 1;
167 r_crack_names
.in
.req
.req1
.names
= names
;
168 r_crack_names
.in
.req
.req1
.format_flags
= DRSUAPI_DS_NAME_FLAG_NO_FLAGS
;
169 r_crack_names
.in
.req
.req1
.format_offered
= DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY
;
170 r_crack_names
.in
.req
.req1
.format_desired
= DRSUAPI_DS_NAME_FORMAT_FQDN_1779
;
171 names
[0].str
= dom_sid_string(tmp_ctx
, r
->out
.account_sid
);
173 r
->out
.error_string
= NULL
;
174 talloc_free(tmp_ctx
);
175 return NT_STATUS_NO_MEMORY
;
178 status
= dcerpc_drsuapi_DsCrackNames(drsuapi_pipe
, tmp_ctx
, &r_crack_names
);
179 if (!NT_STATUS_IS_OK(status
)) {
180 if (NT_STATUS_EQUAL(status
, NT_STATUS_NET_WRITE_FAULT
)) {
183 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s\n",
185 dcerpc_errstr(tmp_ctx
, drsuapi_pipe
->last_fault_code
));
186 talloc_free(tmp_ctx
);
191 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s\n",
194 talloc_free(tmp_ctx
);
197 } else if (!W_ERROR_IS_OK(r_crack_names
.out
.result
)) {
200 "DsCrackNames failed - %s\n", win_errstr(r_crack_names
.out
.result
));
201 talloc_free(tmp_ctx
);
202 return NT_STATUS_UNSUCCESSFUL
;
203 } else if (r_crack_names
.out
.level
!= 1
204 || !r_crack_names
.out
.ctr
.ctr1
205 || r_crack_names
.out
.ctr
.ctr1
->count
!= 1
206 || !r_crack_names
.out
.ctr
.ctr1
->array
[0].result_name
207 || r_crack_names
.out
.ctr
.ctr1
->array
[0].status
!= DRSUAPI_DS_NAME_STATUS_OK
) {
208 r
->out
.error_string
= talloc_asprintf(r
, "DsCrackNames failed\n");
209 talloc_free(tmp_ctx
);
210 return NT_STATUS_UNSUCCESSFUL
;
213 /* Store the DN of our machine account. */
214 account_dn_str
= r_crack_names
.out
.ctr
.ctr1
->array
[0].result_name
;
216 account_dn
= ldb_dn_explode(tmp_ctx
, account_dn_str
);
218 r
->out
.error_string
= talloc_asprintf(r
, "Invalid account dn: %s",
220 talloc_free(tmp_ctx
);
221 return NT_STATUS_UNSUCCESSFUL
;
224 /* Now we know the user's DN, open with LDAP, read and modify a few things */
226 remote_ldb_url
= talloc_asprintf(tmp_ctx
, "ldap://%s",
227 drsuapi_binding
->host
);
228 if (!remote_ldb_url
) {
229 r
->out
.error_string
= NULL
;
230 talloc_free(tmp_ctx
);
231 return NT_STATUS_NO_MEMORY
;
234 remote_ldb
= ldb_wrap_connect(tmp_ctx
, remote_ldb_url
,
235 NULL
, ctx
->cred
, 0, NULL
);
237 r
->out
.error_string
= NULL
;
238 talloc_free(tmp_ctx
);
239 return NT_STATUS_UNSUCCESSFUL
;
242 /* search for the user's record */
243 ret
= ldb_search(remote_ldb
, account_dn
, LDB_SCOPE_BASE
,
245 if (ret
!= LDB_SUCCESS
|| res
->count
!= 1) {
246 r
->out
.error_string
= talloc_asprintf(r
, "ldb_search for %s failed - %s\n",
247 account_dn_str
, ldb_errstring(remote_ldb
));
248 talloc_free(tmp_ctx
);
249 return NT_STATUS_UNSUCCESSFUL
;
252 /* If we have a kvno recorded in AD, we need it locally as well */
253 kvno
= ldb_msg_find_uint(res
->msgs
[0], "msDS-KeyVersionNumber", 0);
255 /* Prepare a new message, for the modify */
256 msg
= ldb_msg_new(tmp_ctx
);
258 r
->out
.error_string
= NULL
;
259 talloc_free(tmp_ctx
);
260 return NT_STATUS_NO_MEMORY
;
262 msg
->dn
= res
->msgs
[0]->dn
;
266 const char *service_principal_name
[6];
267 const char *dns_host_name
= strlower_talloc(tmp_ctx
,
268 talloc_asprintf(tmp_ctx
,
273 if (!dns_host_name
) {
274 r
->out
.error_string
= NULL
;
275 talloc_free(tmp_ctx
);
276 return NT_STATUS_NO_MEMORY
;
279 service_principal_name
[0] = talloc_asprintf(tmp_ctx
, "host/%s", dns_host_name
);
280 service_principal_name
[1] = talloc_asprintf(tmp_ctx
, "host/%s", strlower_talloc(tmp_ctx
, r
->in
.netbios_name
));
281 service_principal_name
[2] = talloc_asprintf(tmp_ctx
, "host/%s/%s", dns_host_name
, realm
);
282 service_principal_name
[3] = talloc_asprintf(tmp_ctx
, "host/%s/%s", strlower_talloc(tmp_ctx
, r
->in
.netbios_name
), realm
);
283 service_principal_name
[4] = talloc_asprintf(tmp_ctx
, "host/%s/%s", dns_host_name
, r
->out
.domain_name
);
284 service_principal_name
[5] = talloc_asprintf(tmp_ctx
, "host/%s/%s", strlower_talloc(tmp_ctx
, r
->in
.netbios_name
), r
->out
.domain_name
);
286 for (i
=0; i
< ARRAY_SIZE(service_principal_name
); i
++) {
287 if (!service_principal_name
[i
]) {
288 r
->out
.error_string
= NULL
;
289 talloc_free(tmp_ctx
);
290 return NT_STATUS_NO_MEMORY
;
292 rtn
= samdb_msg_add_string(remote_ldb
, tmp_ctx
, msg
, "servicePrincipalName", service_principal_name
[i
]);
294 r
->out
.error_string
= NULL
;
295 talloc_free(tmp_ctx
);
296 return NT_STATUS_NO_MEMORY
;
300 rtn
= samdb_msg_add_string(remote_ldb
, tmp_ctx
, msg
, "dNSHostName", dns_host_name
);
302 r
->out
.error_string
= NULL
;
303 talloc_free(tmp_ctx
);
304 return NT_STATUS_NO_MEMORY
;
307 rtn
= samdb_replace(remote_ldb
, tmp_ctx
, msg
);
311 "Failed to replace entries on %s\n",
312 ldb_dn_linearize(tmp_ctx
, msg
->dn
));
313 talloc_free(tmp_ctx
);
314 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
318 /* DsCrackNames to find out the DN of the domain. */
319 r_crack_names
.in
.req
.req1
.format_offered
= DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT
;
320 r_crack_names
.in
.req
.req1
.format_desired
= DRSUAPI_DS_NAME_FORMAT_FQDN_1779
;
321 names
[0].str
= talloc_asprintf(tmp_ctx
, "%s\\", r
->out
.domain_name
);
323 r
->out
.error_string
= NULL
;
324 talloc_free(tmp_ctx
);
325 return NT_STATUS_NO_MEMORY
;
328 status
= dcerpc_drsuapi_DsCrackNames(drsuapi_pipe
, tmp_ctx
, &r_crack_names
);
329 if (!NT_STATUS_IS_OK(status
)) {
330 if (NT_STATUS_EQUAL(status
, NT_STATUS_NET_WRITE_FAULT
)) {
333 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s\n",
335 dcerpc_errstr(tmp_ctx
, drsuapi_pipe
->last_fault_code
));
336 talloc_free(tmp_ctx
);
341 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s\n",
344 talloc_free(tmp_ctx
);
347 } else if (!W_ERROR_IS_OK(r_crack_names
.out
.result
)) {
350 "DsCrackNames failed - %s\n", win_errstr(r_crack_names
.out
.result
));
351 talloc_free(tmp_ctx
);
352 return NT_STATUS_UNSUCCESSFUL
;
353 } else if (r_crack_names
.out
.level
!= 1
354 || !r_crack_names
.out
.ctr
.ctr1
355 || r_crack_names
.out
.ctr
.ctr1
->count
!= 1
356 || !r_crack_names
.out
.ctr
.ctr1
->array
[0].result_name
357 || r_crack_names
.out
.ctr
.ctr1
->array
[0].status
!= DRSUAPI_DS_NAME_STATUS_OK
) {
358 r
->out
.error_string
= talloc_asprintf(r
, "DsCrackNames failed\n");
359 talloc_free(tmp_ctx
);
360 return NT_STATUS_UNSUCCESSFUL
;
363 /* Store the account DN. */
364 r
->out
.account_dn_str
= account_dn_str
;
365 talloc_steal(r
, account_dn_str
);
367 /* Store the domain DN. */
368 r
->out
.domain_dn_str
= r_crack_names
.out
.ctr
.ctr1
->array
[0].result_name
;
369 talloc_steal(r
, r_crack_names
.out
.ctr
.ctr1
->array
[0].result_name
);
373 if (r
->in
.acct_type
== ACB_SVRTRUST
) {
374 status
= libnet_JoinSite(remote_ldb
, r
);
376 talloc_free(tmp_ctx
);
382 * do a domain join using DCERPC/SAMR calls
383 * - connect to the LSA pipe, to try and find out information about the domain
384 * - create a secondary connection to SAMR pipe
385 * - do a samr_Connect to get a policy handle
386 * - do a samr_LookupDomain to get the domain sid
387 * - do a samr_OpenDomain to get a domain handle
388 * - do a samr_CreateAccount to try and get a new account
391 * - do a samr_LookupNames to get the users rid
392 * - do a samr_OpenUser to get a user handle
393 * - potentially delete and recreate the user
394 * - assert the account is of the right type with samrQueryUserInfo
396 * - call libnet_SetPassword_samr_handle to set the password
398 * - do a samrSetUserInfo to set the account flags
399 * - do some ADS specific things when we join as Domain Controller,
400 * look at libnet_joinADSDomain() for the details
402 NTSTATUS
libnet_JoinDomain(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
, struct libnet_JoinDomain
*r
)
406 NTSTATUS status
, cu_status
;
408 struct libnet_RpcConnectDCInfo
*connect_with_info
;
409 struct dcerpc_pipe
*samr_pipe
;
411 struct samr_Connect sc
;
412 struct policy_handle p_handle
;
413 struct samr_OpenDomain od
;
414 struct policy_handle d_handle
;
415 struct samr_LookupNames ln
;
416 struct samr_OpenUser ou
;
417 struct samr_CreateUser2 cu
;
418 struct policy_handle
*u_handle
= NULL
;
419 struct samr_QueryUserInfo qui
;
420 struct samr_SetUserInfo sui
;
421 union samr_UserInfo u_info
;
422 union libnet_SetPassword r2
;
423 struct samr_GetUserPwInfo pwp
;
424 struct lsa_String samr_account_name
;
426 uint32_t acct_flags
, old_acct_flags
;
427 uint32_t rid
, access_granted
;
428 int policy_min_pw_len
= 0;
430 struct dom_sid
*account_sid
= NULL
;
431 const char *password_str
= NULL
;
433 r
->out
.error_string
= NULL
;
434 r2
.samr_handle
.out
.error_string
= NULL
;
436 tmp_ctx
= talloc_named(mem_ctx
, 0, "libnet_Join temp context");
438 r
->out
.error_string
= NULL
;
439 return NT_STATUS_NO_MEMORY
;
442 u_handle
= talloc(tmp_ctx
, struct policy_handle
);
444 r
->out
.error_string
= NULL
;
445 talloc_free(tmp_ctx
);
446 return NT_STATUS_NO_MEMORY
;
449 connect_with_info
= talloc(tmp_ctx
, struct libnet_RpcConnectDCInfo
);
450 if (!connect_with_info
) {
451 r
->out
.error_string
= NULL
;
452 talloc_free(tmp_ctx
);
453 return NT_STATUS_NO_MEMORY
;
456 /* prepare connect to the LSA pipe of PDC */
457 if (r
->in
.level
== LIBNET_JOINDOMAIN_AUTOMATIC
) {
458 connect_with_info
->level
= LIBNET_RPC_CONNECT_PDC
;
459 connect_with_info
->in
.name
= r
->in
.domain_name
;
461 connect_with_info
->level
= LIBNET_RPC_CONNECT_BINDING
;
462 connect_with_info
->in
.binding
= r
->in
.binding
;
465 connect_with_info
->in
.dcerpc_iface
= &dcerpc_table_samr
;
467 establish a SAMR connection, on the same CIFS transport
470 status
= libnet_RpcConnectDCInfo(ctx
, connect_with_info
);
471 if (!NT_STATUS_IS_OK(status
)) {
473 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
474 "Connection to SAMR pipe of DC %s failed: %s",
475 r
->in
.binding
, connect_with_info
->out
.error_string
);
477 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
478 "Connection to SAMR pipe of PDC for %s failed: %s",
479 r
->in
.domain_name
, connect_with_info
->out
.error_string
);
481 talloc_free(tmp_ctx
);
485 samr_pipe
= connect_with_info
->out
.dcerpc_pipe
,
487 status
= dcerpc_pipe_auth(samr_pipe
,
488 connect_with_info
->out
.dcerpc_pipe
->binding
,
489 &dcerpc_table_samr
, ctx
->cred
);
490 if (!NT_STATUS_IS_OK(status
)) {
491 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
492 "SAMR bind failed: %s",
494 talloc_free(tmp_ctx
);
498 /* prepare samr_Connect */
499 ZERO_STRUCT(p_handle
);
500 sc
.in
.system_name
= NULL
;
501 sc
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
502 sc
.out
.connect_handle
= &p_handle
;
504 /* 2. do a samr_Connect to get a policy handle */
505 status
= dcerpc_samr_Connect(samr_pipe
, tmp_ctx
, &sc
);
506 if (!NT_STATUS_IS_OK(status
)) {
507 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
508 "samr_Connect failed: %s",
510 talloc_free(tmp_ctx
);
514 /* If this is a connection on ncacn_ip_tcp to Win2k3 SP1, we don't get back this useful info */
515 if (!connect_with_info
->out
.domain_name
) {
516 if (r
->in
.level
== LIBNET_JOINDOMAIN_AUTOMATIC
) {
517 connect_with_info
->out
.domain_name
= talloc_strdup(tmp_ctx
, r
->in
.domain_name
);
519 /* Bugger, we just lost our way to automaticly find the domain name */
520 connect_with_info
->out
.domain_name
= talloc_strdup(tmp_ctx
, lp_workgroup());
524 /* Perhaps we didn't get a SID above, because we are against ncacn_ip_tcp */
525 if (!connect_with_info
->out
.domain_sid
) {
526 struct lsa_String name
;
527 struct samr_LookupDomain l
;
528 name
.string
= connect_with_info
->out
.domain_name
;
529 l
.in
.connect_handle
= &p_handle
;
530 l
.in
.domain_name
= &name
;
532 status
= dcerpc_samr_LookupDomain(samr_pipe
, tmp_ctx
, &l
);
533 if (!NT_STATUS_IS_OK(status
)) {
534 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
535 "SAMR LookupDomain failed: %s",
537 talloc_free(tmp_ctx
);
540 connect_with_info
->out
.domain_sid
= l
.out
.sid
;
543 /* prepare samr_OpenDomain */
544 ZERO_STRUCT(d_handle
);
545 od
.in
.connect_handle
= &p_handle
;
546 od
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
547 od
.in
.sid
= connect_with_info
->out
.domain_sid
;
548 od
.out
.domain_handle
= &d_handle
;
550 /* do a samr_OpenDomain to get a domain handle */
551 status
= dcerpc_samr_OpenDomain(samr_pipe
, tmp_ctx
, &od
);
552 if (!NT_STATUS_IS_OK(status
)) {
553 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
554 "samr_OpenDomain for [%s] failed: %s",
555 dom_sid_string(tmp_ctx
, connect_with_info
->out
.domain_sid
),
557 talloc_free(tmp_ctx
);
561 /* prepare samr_CreateUser2 */
562 ZERO_STRUCTP(u_handle
);
563 cu
.in
.domain_handle
= &d_handle
;
564 cu
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
565 samr_account_name
.string
= r
->in
.account_name
;
566 cu
.in
.account_name
= &samr_account_name
;
567 cu
.in
.acct_flags
= r
->in
.acct_type
;
568 cu
.out
.user_handle
= u_handle
;
570 cu
.out
.access_granted
= &access_granted
;
572 /* do a samr_CreateUser2 to get an account handle, or an error */
573 cu_status
= dcerpc_samr_CreateUser2(samr_pipe
, tmp_ctx
, &cu
);
575 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
576 /* prepare samr_LookupNames */
577 ln
.in
.domain_handle
= &d_handle
;
579 ln
.in
.names
= talloc_array(tmp_ctx
, struct lsa_String
, 1);
581 r
->out
.error_string
= NULL
;
582 talloc_free(tmp_ctx
);
583 return NT_STATUS_NO_MEMORY
;
585 ln
.in
.names
[0].string
= r
->in
.account_name
;
587 /* 5. do a samr_LookupNames to get the users rid */
588 status
= dcerpc_samr_LookupNames(samr_pipe
, tmp_ctx
, &ln
);
589 if (!NT_STATUS_IS_OK(status
)) {
590 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
591 "samr_LookupNames for [%s] failed: %s",
594 talloc_free(tmp_ctx
);
598 /* check if we got one RID for the user */
599 if (ln
.out
.rids
.count
!= 1) {
600 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
601 "samr_LookupNames for [%s] returns %d RIDs\n",
602 r
->in
.account_name
, ln
.out
.rids
.count
);
603 talloc_free(tmp_ctx
);
604 return NT_STATUS_INVALID_PARAMETER
;
607 /* prepare samr_OpenUser */
608 ZERO_STRUCTP(u_handle
);
609 ou
.in
.domain_handle
= &d_handle
;
610 ou
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
611 ou
.in
.rid
= ln
.out
.rids
.ids
[0];
613 ou
.out
.user_handle
= u_handle
;
615 /* 6. do a samr_OpenUser to get a user handle */
616 status
= dcerpc_samr_OpenUser(samr_pipe
, tmp_ctx
, &ou
);
617 if (!NT_STATUS_IS_OK(status
)) {
618 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
619 "samr_OpenUser for [%s] failed: %s",
622 talloc_free(tmp_ctx
);
626 if (r
->in
.recreate_account
) {
627 struct samr_DeleteUser d
;
628 d
.in
.user_handle
= u_handle
;
629 d
.out
.user_handle
= u_handle
;
630 status
= dcerpc_samr_DeleteUser(samr_pipe
, mem_ctx
, &d
);
631 if (!NT_STATUS_IS_OK(status
)) {
632 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
633 "samr_DeleteUser (for recreate) of [%s] failed: %s",
636 talloc_free(tmp_ctx
);
640 /* We want to recreate, so delete and another samr_CreateUser2 */
642 /* &cu filled in above */
643 status
= dcerpc_samr_CreateUser2(samr_pipe
, tmp_ctx
, &cu
);
644 if (!NT_STATUS_IS_OK(status
)) {
645 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
646 "samr_CreateUser2 (recreate) for [%s] failed: %s\n",
647 r
->in
.domain_name
, nt_errstr(status
));
648 talloc_free(tmp_ctx
);
652 } else if (!NT_STATUS_IS_OK(status
)) {
653 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
654 "samr_CreateUser2 for [%s] failed: %s\n",
655 r
->in
.domain_name
, nt_errstr(status
));
656 talloc_free(tmp_ctx
);
660 /* prepare samr_QueryUserInfo (get flags) */
661 qui
.in
.user_handle
= u_handle
;
664 status
= dcerpc_samr_QueryUserInfo(samr_pipe
, tmp_ctx
, &qui
);
665 if (!NT_STATUS_IS_OK(status
)) {
666 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
667 "samr_QueryUserInfo for [%s] failed: %s",
670 talloc_free(tmp_ctx
);
675 status
= NT_STATUS_INVALID_PARAMETER
;
677 = talloc_asprintf(mem_ctx
,
678 "samr_QueryUserInfo failed to return qui.out.info for [%s]: %s\n",
679 r
->in
.account_name
, nt_errstr(status
));
680 talloc_free(tmp_ctx
);
684 old_acct_flags
= (qui
.out
.info
->info16
.acct_flags
& (ACB_WSTRUST
| ACB_SVRTRUST
| ACB_DOMTRUST
));
685 /* Possibly bail if the account is of the wrong type */
687 != r
->in
.acct_type
) {
688 const char *old_account_type
, *new_account_type
;
689 switch (old_acct_flags
) {
691 old_account_type
= "domain member (member)";
694 old_account_type
= "domain controller (bdc)";
697 old_account_type
= "trusted domain";
700 return NT_STATUS_INVALID_PARAMETER
;
702 switch (r
->in
.acct_type
) {
704 new_account_type
= "domain member (member)";
707 new_account_type
= "domain controller (bdc)";
710 new_account_type
= "trusted domain";
713 return NT_STATUS_INVALID_PARAMETER
;
716 if (!NT_STATUS_EQUAL(cu_status
, NT_STATUS_USER_EXISTS
)) {
717 /* We created a new user, but they didn't come out the right type?!? */
719 = talloc_asprintf(mem_ctx
,
720 "We asked to create a new machine account (%s) of type %s, "
721 "but we got an account of type %s. This is unexpected. "
722 "Perhaps delete the account and try again.\n",
723 r
->in
.account_name
, new_account_type
, old_account_type
);
724 talloc_free(tmp_ctx
);
725 return NT_STATUS_INVALID_PARAMETER
;
727 /* The account is of the wrong type, so bail */
729 /* TODO: We should allow a --force option to override, and redo this from the top setting r.in.recreate_account */
731 = talloc_asprintf(mem_ctx
,
732 "The machine account (%s) already exists in the domain %s, "
733 "but is a %s. You asked to join as a %s. Please delete "
734 "the account and try again.\n",
735 r
->in
.account_name
, connect_with_info
->out
.domain_name
, old_account_type
, new_account_type
);
736 talloc_free(tmp_ctx
);
737 return NT_STATUS_USER_EXISTS
;
740 acct_flags
= qui
.out
.info
->info16
.acct_flags
;
743 acct_flags
= (acct_flags
& ~ACB_DISABLED
);
745 /* Find out what password policy this user has */
746 pwp
.in
.user_handle
= u_handle
;
748 status
= dcerpc_samr_GetUserPwInfo(samr_pipe
, tmp_ctx
, &pwp
);
749 if (NT_STATUS_IS_OK(status
)) {
750 policy_min_pw_len
= pwp
.out
.info
.min_password_length
;
753 /* Grab a password of that minimum length */
755 password_str
= generate_random_str(tmp_ctx
, MAX(8, policy_min_pw_len
));
757 r2
.samr_handle
.level
= LIBNET_SET_PASSWORD_SAMR_HANDLE
;
758 r2
.samr_handle
.in
.account_name
= r
->in
.account_name
;
759 r2
.samr_handle
.in
.newpassword
= password_str
;
760 r2
.samr_handle
.in
.user_handle
= u_handle
;
761 r2
.samr_handle
.in
.dcerpc_pipe
= samr_pipe
;
763 status
= libnet_SetPassword(ctx
, tmp_ctx
, &r2
);
764 if (!NT_STATUS_IS_OK(status
)) {
765 r
->out
.error_string
= talloc_steal(mem_ctx
, r2
.samr_handle
.out
.error_string
);
766 talloc_free(tmp_ctx
);
770 /* reset flags (if required) */
771 if (acct_flags
!= qui
.out
.info
->info16
.acct_flags
) {
773 u_info
.info16
.acct_flags
= acct_flags
;
775 sui
.in
.user_handle
= u_handle
;
776 sui
.in
.info
= &u_info
;
779 dcerpc_samr_SetUserInfo(samr_pipe
, tmp_ctx
, &sui
);
780 if (!NT_STATUS_IS_OK(status
)) {
781 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
782 "samr_SetUserInfo for [%s] failed to remove ACB_DISABLED flag: %s",
785 talloc_free(tmp_ctx
);
790 account_sid
= dom_sid_add_rid(mem_ctx
, connect_with_info
->out
.domain_sid
, rid
);
792 r
->out
.error_string
= NULL
;
793 talloc_free(tmp_ctx
);
794 return NT_STATUS_NO_MEMORY
;
797 /* Finish out by pushing various bits of status data out for the caller to use */
798 r
->out
.join_password
= password_str
;
799 talloc_steal(mem_ctx
, r
->out
.join_password
);
801 r
->out
.domain_sid
= connect_with_info
->out
.domain_sid
;
802 talloc_steal(mem_ctx
, r
->out
.domain_sid
);
804 r
->out
.account_sid
= account_sid
;
805 talloc_steal(mem_ctx
, r
->out
.account_sid
);
807 r
->out
.domain_name
= connect_with_info
->out
.domain_name
;
808 talloc_steal(mem_ctx
, r
->out
.domain_name
);
809 r
->out
.realm
= connect_with_info
->out
.realm
;
810 talloc_steal(mem_ctx
, r
->out
.realm
);
811 r
->out
.samr_pipe
= samr_pipe
;
812 talloc_steal(mem_ctx
, samr_pipe
);
813 r
->out
.samr_binding
= samr_pipe
->binding
;
814 talloc_steal(mem_ctx
, r
->out
.samr_binding
);
815 r
->out
.user_handle
= u_handle
;
816 talloc_steal(mem_ctx
, u_handle
);
817 r
->out
.error_string
= r2
.samr_handle
.out
.error_string
;
818 talloc_steal(mem_ctx
, r2
.samr_handle
.out
.error_string
);
820 r
->out
.server_dn_str
= NULL
;
821 talloc_free(tmp_ctx
);
823 /* Now, if it was AD, then we want to start looking changing a
824 * few more things. Otherwise, we are done. */
826 status
= libnet_JoinADSDomain(ctx
, r
);
833 static NTSTATUS
libnet_Join_primary_domain(struct libnet_context
*ctx
,
835 struct libnet_Join
*r
)
839 struct libnet_JoinDomain
*r2
;
841 struct ldb_context
*ldb
;
842 const struct ldb_dn
*base_dn
;
843 struct ldb_message
**msgs
, *msg
;
845 const char * const attrs
[] = {
854 uint32_t acct_type
= 0;
855 const char *account_name
;
856 const char *netbios_name
;
859 r
->out
.error_string
= NULL
;
861 tmp_mem
= talloc_new(mem_ctx
);
863 return NT_STATUS_NO_MEMORY
;
866 r2
= talloc(tmp_mem
, struct libnet_JoinDomain
);
868 r
->out
.error_string
= NULL
;
869 talloc_free(tmp_mem
);
870 return NT_STATUS_NO_MEMORY
;
873 if (r
->in
.join_type
== SEC_CHAN_BDC
) {
874 acct_type
= ACB_SVRTRUST
;
875 } else if (r
->in
.join_type
== SEC_CHAN_WKSTA
) {
876 acct_type
= ACB_WSTRUST
;
878 r
->out
.error_string
= NULL
;
879 talloc_free(tmp_mem
);
880 return NT_STATUS_INVALID_PARAMETER
;
883 if (r
->in
.netbios_name
!= NULL
) {
884 netbios_name
= r
->in
.netbios_name
;
886 netbios_name
= talloc_reference(tmp_mem
, lp_netbios_name());
888 r
->out
.error_string
= NULL
;
889 talloc_free(tmp_mem
);
890 return NT_STATUS_NO_MEMORY
;
894 account_name
= talloc_asprintf(tmp_mem
, "%s$", netbios_name
);
896 r
->out
.error_string
= NULL
;
897 talloc_free(tmp_mem
);
898 return NT_STATUS_NO_MEMORY
;
902 * Local secrets are stored in secrets.ldb
903 * open it to make sure we can write the info into it after the join
905 ldb
= secrets_db_connect(tmp_mem
);
908 = talloc_asprintf(mem_ctx
,
909 "Could not open secrets database\n");
910 talloc_free(tmp_mem
);
911 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
918 r2
->in
.domain_name
= r
->in
.domain_name
;
919 r2
->in
.account_name
= account_name
;
920 r2
->in
.netbios_name
= netbios_name
;
921 r2
->in
.level
= LIBNET_JOINDOMAIN_AUTOMATIC
;
922 r2
->in
.acct_type
= acct_type
;
923 r2
->in
.recreate_account
= False
;
924 status
= libnet_JoinDomain(ctx
, r2
, r2
);
925 if (!NT_STATUS_IS_OK(status
)) {
926 r
->out
.error_string
= talloc_steal(mem_ctx
, r2
->out
.error_string
);
927 talloc_free(tmp_mem
);
932 * now prepare the record for secrets.ldb
934 sct
= talloc_asprintf(tmp_mem
, "%d", r
->in
.join_type
);
936 r
->out
.error_string
= NULL
;
937 talloc_free(tmp_mem
);
938 return NT_STATUS_NO_MEMORY
;
941 msg
= ldb_msg_new(tmp_mem
);
943 r
->out
.error_string
= NULL
;
944 talloc_free(tmp_mem
);
945 return NT_STATUS_NO_MEMORY
;
948 base_dn
= ldb_dn_explode(tmp_mem
, "cn=Primary Domains");
950 r
->out
.error_string
= NULL
;
951 talloc_free(tmp_mem
);
952 return NT_STATUS_NO_MEMORY
;
955 msg
->dn
= ldb_dn_build_child(tmp_mem
, "flatname", r2
->out
.domain_name
, base_dn
);
957 r
->out
.error_string
= NULL
;
958 talloc_free(tmp_mem
);
959 return NT_STATUS_NO_MEMORY
;
962 rtn
= samdb_msg_add_string(ldb
, tmp_mem
, msg
, "flatname", r2
->out
.domain_name
);
964 r
->out
.error_string
= NULL
;
965 talloc_free(tmp_mem
);
966 return NT_STATUS_NO_MEMORY
;
970 rtn
= samdb_msg_add_string(ldb
, tmp_mem
, msg
, "realm", r2
->out
.realm
);
972 r
->out
.error_string
= NULL
;
973 talloc_free(tmp_mem
);
974 return NT_STATUS_NO_MEMORY
;
977 rtn
= samdb_msg_add_string(ldb
, tmp_mem
, msg
, "objectClass", "primaryDomain");
979 r
->out
.error_string
= NULL
;
980 talloc_free(tmp_mem
);
981 return NT_STATUS_NO_MEMORY
;
985 rtn
= samdb_msg_add_string(ldb
, tmp_mem
, msg
, "objectClass", "primaryDomain");
987 r
->out
.error_string
= NULL
;
988 talloc_free(tmp_mem
);
989 return NT_STATUS_NO_MEMORY
;
992 rtn
= samdb_msg_add_string(ldb
, tmp_mem
, msg
, "secret", r2
->out
.join_password
);
994 r
->out
.error_string
= NULL
;
995 talloc_free(tmp_mem
);
996 return NT_STATUS_NO_MEMORY
;
999 rtn
= samdb_msg_add_string(ldb
, tmp_mem
, msg
, "samAccountName", r2
->in
.account_name
);
1001 r
->out
.error_string
= NULL
;
1002 talloc_free(tmp_mem
);
1003 return NT_STATUS_NO_MEMORY
;
1006 rtn
= samdb_msg_add_string(ldb
, tmp_mem
, msg
, "secureChannelType", sct
);
1008 r
->out
.error_string
= NULL
;
1009 talloc_free(tmp_mem
);
1010 return NT_STATUS_NO_MEMORY
;
1014 rtn
= samdb_msg_add_uint(ldb
, tmp_mem
, msg
, "msDS-KeyVersionNumber",
1017 r
->out
.error_string
= NULL
;
1018 talloc_free(tmp_mem
);
1019 return NT_STATUS_NO_MEMORY
;
1023 if (r2
->out
.domain_sid
) {
1024 rtn
= samdb_msg_add_dom_sid(ldb
, tmp_mem
, msg
, "objectSid",
1025 r2
->out
.domain_sid
);
1027 r
->out
.error_string
= NULL
;
1028 talloc_free(tmp_mem
);
1029 return NT_STATUS_NO_MEMORY
;
1034 * search for the secret record
1035 * - remove the records we find
1036 * - and fetch the old secret and store it under priorSecret
1038 ret
= gendb_search(ldb
,
1041 "(|" SECRETS_PRIMARY_DOMAIN_FILTER
"(realm=%s))",
1042 r2
->out
.domain_name
, r2
->out
.realm
);
1044 rtn
= samdb_msg_set_string(ldb
, tmp_mem
, msg
, "secretsKeytab", "secrets.keytab");
1046 r
->out
.error_string
= NULL
;
1047 talloc_free(tmp_mem
);
1048 return NT_STATUS_NO_MEMORY
;
1050 } else if (ret
== -1) {
1052 = talloc_asprintf(mem_ctx
,
1053 "Search for domain: %s and realm: %s failed: %s",
1054 r2
->out
.domain_name
, r2
->out
.realm
, ldb_errstring(ldb
));
1055 talloc_free(tmp_mem
);
1056 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
1058 const struct ldb_val
*private_keytab
;
1059 const struct ldb_val
*krb5_keytab
;
1060 const struct ldb_val
*prior_secret
;
1061 const struct ldb_val
*prior_modified_time
;
1064 for (i
= 0; i
< ret
; i
++) {
1065 ldb_delete(ldb
, msgs
[i
]->dn
);
1068 prior_secret
= ldb_msg_find_ldb_val(msgs
[0], "secret");
1070 rtn
= samdb_msg_set_value(ldb
, tmp_mem
, msg
, "priorSecret", prior_secret
);
1072 r
->out
.error_string
= NULL
;
1073 talloc_free(tmp_mem
);
1074 return NT_STATUS_NO_MEMORY
;
1077 rtn
= samdb_msg_set_string(ldb
, tmp_mem
, msg
, "secret", r2
->out
.join_password
);
1079 r
->out
.error_string
= NULL
;
1080 talloc_free(tmp_mem
);
1081 return NT_STATUS_NO_MEMORY
;
1084 prior_modified_time
= ldb_msg_find_ldb_val(msgs
[0],
1086 if (prior_modified_time
) {
1087 rtn
= samdb_msg_set_value(ldb
, tmp_mem
, msg
, "priorWhenChanged",
1088 prior_modified_time
);
1090 r
->out
.error_string
= NULL
;
1091 talloc_free(tmp_mem
);
1092 return NT_STATUS_NO_MEMORY
;
1096 rtn
= samdb_msg_set_string(ldb
, tmp_mem
, msg
, "samAccountName", r2
->in
.account_name
);
1098 r
->out
.error_string
= NULL
;
1099 talloc_free(tmp_mem
);
1100 return NT_STATUS_NO_MEMORY
;
1103 rtn
= samdb_msg_set_string(ldb
, tmp_mem
, msg
, "secureChannelType", sct
);
1105 r
->out
.error_string
= NULL
;
1106 talloc_free(tmp_mem
);
1107 return NT_STATUS_NO_MEMORY
;
1110 /* We will want to keep the keytab names */
1111 private_keytab
= ldb_msg_find_ldb_val(msgs
[0], "privateKeytab");
1112 if (private_keytab
) {
1113 rtn
= samdb_msg_set_value(ldb
, tmp_mem
, msg
, "privateKeytab", private_keytab
);
1115 r
->out
.error_string
= NULL
;
1116 talloc_free(tmp_mem
);
1117 return NT_STATUS_NO_MEMORY
;
1120 krb5_keytab
= ldb_msg_find_ldb_val(msgs
[0], "krb5Keytab");
1122 rtn
= samdb_msg_set_value(ldb
, tmp_mem
, msg
, "krb5Keytab", krb5_keytab
);
1124 r
->out
.error_string
= NULL
;
1125 talloc_free(tmp_mem
);
1126 return NT_STATUS_NO_MEMORY
;
1131 /* create the secret */
1132 ret
= samdb_add(ldb
, tmp_mem
, msg
);
1134 r
->out
.error_string
= talloc_asprintf(mem_ctx
, "Failed to create secret record %s\n",
1135 ldb_dn_linearize(ldb
, msg
->dn
));
1136 talloc_free(tmp_mem
);
1137 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
1140 if (r2
->out
.realm
) {
1141 struct cli_credentials
*creds
;
1142 /* Make a credentials structure from it */
1143 creds
= cli_credentials_init(mem_ctx
);
1145 r
->out
.error_string
= NULL
;
1146 talloc_free(tmp_mem
);
1147 return NT_STATUS_NO_MEMORY
;
1149 cli_credentials_set_conf(creds
);
1150 filter
= talloc_asprintf(mem_ctx
, "dn=%s", ldb_dn_linearize(mem_ctx
, msg
->dn
));
1151 status
= cli_credentials_set_secrets(creds
, NULL
, filter
);
1152 if (!NT_STATUS_IS_OK(status
)) {
1153 r
->out
.error_string
= talloc_asprintf(mem_ctx
, "Failed to read secrets for keytab update for %s\n",
1155 talloc_free(tmp_mem
);
1158 ret
= cli_credentials_update_keytab(creds
);
1160 r
->out
.error_string
= talloc_asprintf(mem_ctx
, "Failed to update keytab for %s\n",
1162 talloc_free(tmp_mem
);
1163 return NT_STATUS_UNSUCCESSFUL
;
1167 /* move all out parameter to the callers TALLOC_CTX */
1168 r
->out
.error_string
= NULL
;
1169 r
->out
.join_password
= r2
->out
.join_password
;
1170 talloc_steal(mem_ctx
, r2
->out
.join_password
);
1171 r
->out
.domain_sid
= r2
->out
.domain_sid
;
1172 talloc_steal(mem_ctx
, r2
->out
.domain_sid
);
1173 r
->out
.domain_name
= r2
->out
.domain_name
;
1174 talloc_steal(mem_ctx
, r2
->out
.domain_name
);
1175 talloc_free(tmp_mem
);
1176 return NT_STATUS_OK
;
1179 NTSTATUS
libnet_Join(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
, struct libnet_Join
*r
)
1181 switch (r
->in
.join_type
) {
1182 case SEC_CHAN_WKSTA
:
1183 return libnet_Join_primary_domain(ctx
, mem_ctx
, r
);
1185 return libnet_Join_primary_domain(ctx
, mem_ctx
, r
);
1186 case SEC_CHAN_DOMAIN
:
1190 r
->out
.error_string
= talloc_asprintf(mem_ctx
,
1191 "Invalid join type specified (%08X) attempting to join domain %s",
1192 r
->in
.join_type
, r
->in
.domain_name
);
1193 return NT_STATUS_INVALID_PARAMETER
;