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"
25
#include "lib/ldb/include/ldb.h"
26
#include "lib/ldb/include/ldb_errors.h"
27
#include "param/secrets.h"
28
#include "dsdb/samdb/samdb.h"
30
#include "../lib/util/util_ldb.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"
35
#include "param/param.h"
38
* complete a domain join, when joining to a AD domain:
39
* 1.) connect and bind to the DRSUAPI pipe
40
* 2.) do a DsCrackNames() to find the machine account dn
42
* 4.) do an ldap search to find the "msDS-KeyVersionNumber" of the machine account
43
* 5.) set the servicePrincipalName's of the machine account via LDAP, (maybe we should use DsWriteAccountSpn()...)
44
* 6.) do a DsCrackNames() to find the domain dn
45
* 7.) find out Site specific stuff, look at libnet_JoinSite() for details
47
static NTSTATUS libnet_JoinADSDomain(struct libnet_context *ctx, struct libnet_JoinDomain *r)
53
const char *realm = r->out.realm;
55
struct dcerpc_binding *samr_binding = r->out.samr_binding;
57
struct dcerpc_pipe *drsuapi_pipe;
58
struct dcerpc_binding *drsuapi_binding;
59
struct drsuapi_DsBind r_drsuapi_bind;
60
struct drsuapi_DsCrackNames r_crack_names;
61
struct drsuapi_DsNameString names[1];
62
struct policy_handle drsuapi_bind_handle;
63
struct GUID drsuapi_bind_guid;
65
struct ldb_context *remote_ldb;
66
struct ldb_dn *account_dn;
67
const char *account_dn_str;
68
const char *remote_ldb_url;
69
struct ldb_result *res;
70
struct ldb_message *msg;
74
const char * const attrs[] = {
75
"msDS-KeyVersionNumber",
76
"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,
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(drsuapi_pipe, tmp_ctx, &r_drsuapi_bind);
136
if (!NT_STATUS_IS_OK(status)) {
137
if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
140
"dcerpc_drsuapi_DsBind failed - %s",
141
dcerpc_errstr(tmp_ctx, drsuapi_pipe->last_fault_code));
142
talloc_free(tmp_ctx);
147
"dcerpc_drsuapi_DsBind failed - %s",
149
talloc_free(tmp_ctx);
152
} else if (!W_ERROR_IS_OK(r_drsuapi_bind.out.result)) {
155
"DsBind failed - %s",
156
win_errstr(r_drsuapi_bind.out.result));
157
talloc_free(tmp_ctx);
158
return NT_STATUS_UNSUCCESSFUL;
161
/* Actually 'crack' the names */
162
ZERO_STRUCT(r_crack_names);
163
r_crack_names.in.bind_handle = &drsuapi_bind_handle;
164
r_crack_names.in.level = 1;
165
r_crack_names.in.req = talloc(r, union drsuapi_DsNameRequest);
166
if (!r_crack_names.in.req) {
167
r->out.error_string = NULL;
168
talloc_free(tmp_ctx);
169
return NT_STATUS_NO_MEMORY;
171
r_crack_names.in.req->req1.codepage = 1252; /* western european */
172
r_crack_names.in.req->req1.language = 0x00000407; /* german */
173
r_crack_names.in.req->req1.count = 1;
174
r_crack_names.in.req->req1.names = names;
175
r_crack_names.in.req->req1.format_flags = DRSUAPI_DS_NAME_FLAG_NO_FLAGS;
176
r_crack_names.in.req->req1.format_offered = DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY;
177
r_crack_names.in.req->req1.format_desired = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
178
names[0].str = dom_sid_string(tmp_ctx, r->out.account_sid);
180
r->out.error_string = NULL;
181
talloc_free(tmp_ctx);
182
return NT_STATUS_NO_MEMORY;
185
r_crack_names.out.ctr = talloc(r, union drsuapi_DsNameCtr);
186
r_crack_names.out.level_out = talloc(r, int32_t);
187
if (!r_crack_names.out.ctr || !r_crack_names.out.level_out) {
188
r->out.error_string = NULL;
189
talloc_free(tmp_ctx);
190
return NT_STATUS_NO_MEMORY;
193
status = dcerpc_drsuapi_DsCrackNames(drsuapi_pipe, tmp_ctx, &r_crack_names);
194
if (!NT_STATUS_IS_OK(status)) {
195
if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
198
"dcerpc_drsuapi_DsCrackNames for [%s] failed - %s",
200
dcerpc_errstr(tmp_ctx, drsuapi_pipe->last_fault_code));
201
talloc_free(tmp_ctx);
206
"dcerpc_drsuapi_DsCrackNames for [%s] failed - %s",
209
talloc_free(tmp_ctx);
212
} else if (!W_ERROR_IS_OK(r_crack_names.out.result)) {
215
"DsCrackNames failed - %s", win_errstr(r_crack_names.out.result));
216
talloc_free(tmp_ctx);
217
return NT_STATUS_UNSUCCESSFUL;
218
} else if (*r_crack_names.out.level_out != 1
219
|| !r_crack_names.out.ctr->ctr1
220
|| r_crack_names.out.ctr->ctr1->count != 1) {
221
r->out.error_string = talloc_asprintf(r, "DsCrackNames failed");
222
talloc_free(tmp_ctx);
223
return NT_STATUS_INVALID_PARAMETER;
224
} else if (r_crack_names.out.ctr->ctr1->array[0].status != DRSUAPI_DS_NAME_STATUS_OK) {
225
r->out.error_string = talloc_asprintf(r, "DsCrackNames failed: %d", r_crack_names.out.ctr->ctr1->array[0].status);
226
talloc_free(tmp_ctx);
227
return NT_STATUS_UNSUCCESSFUL;
228
} else if (r_crack_names.out.ctr->ctr1->array[0].result_name == NULL) {
229
r->out.error_string = talloc_asprintf(r, "DsCrackNames failed: no result name");
230
talloc_free(tmp_ctx);
231
return NT_STATUS_INVALID_PARAMETER;
234
/* Store the DN of our machine account. */
235
account_dn_str = r_crack_names.out.ctr->ctr1->array[0].result_name;
237
/* Now we know the user's DN, open with LDAP, read and modify a few things */
239
remote_ldb_url = talloc_asprintf(tmp_ctx, "ldap://%s",
240
drsuapi_binding->target_hostname);
241
if (!remote_ldb_url) {
242
r->out.error_string = NULL;
243
talloc_free(tmp_ctx);
244
return NT_STATUS_NO_MEMORY;
247
remote_ldb = ldb_wrap_connect(tmp_ctx, ctx->event_ctx, ctx->lp_ctx,
249
NULL, ctx->cred, 0, NULL);
251
r->out.error_string = NULL;
252
talloc_free(tmp_ctx);
253
return NT_STATUS_UNSUCCESSFUL;
256
account_dn = ldb_dn_new(tmp_ctx, remote_ldb, account_dn_str);
257
if (! ldb_dn_validate(account_dn)) {
258
r->out.error_string = talloc_asprintf(r, "Invalid account dn: %s",
260
talloc_free(tmp_ctx);
261
return NT_STATUS_UNSUCCESSFUL;
264
/* search for the user's record */
265
ret = ldb_search(remote_ldb, tmp_ctx, &res,
266
account_dn, LDB_SCOPE_BASE, attrs, NULL);
267
if (ret != LDB_SUCCESS) {
268
r->out.error_string = talloc_asprintf(r, "ldb_search for %s failed - %s",
269
account_dn_str, ldb_errstring(remote_ldb));
270
talloc_free(tmp_ctx);
271
return NT_STATUS_UNSUCCESSFUL;
274
if (res->count != 1) {
275
r->out.error_string = talloc_asprintf(r, "ldb_search for %s failed - found %d entries",
276
account_dn_str, res->count);
277
talloc_free(tmp_ctx);
278
return NT_STATUS_UNSUCCESSFUL;
281
/* Prepare a new message, for the modify */
282
msg = ldb_msg_new(tmp_ctx);
284
r->out.error_string = NULL;
285
talloc_free(tmp_ctx);
286
return NT_STATUS_NO_MEMORY;
288
msg->dn = res->msgs[0]->dn;
292
const char *service_principal_name[6];
293
const char *dns_host_name = strlower_talloc(tmp_ctx,
294
talloc_asprintf(tmp_ctx,
299
if (!dns_host_name) {
300
r->out.error_string = NULL;
301
talloc_free(tmp_ctx);
302
return NT_STATUS_NO_MEMORY;
305
service_principal_name[0] = talloc_asprintf(tmp_ctx, "host/%s", dns_host_name);
306
service_principal_name[1] = talloc_asprintf(tmp_ctx, "host/%s", strlower_talloc(tmp_ctx, r->in.netbios_name));
307
service_principal_name[2] = talloc_asprintf(tmp_ctx, "host/%s/%s", dns_host_name, realm);
308
service_principal_name[3] = talloc_asprintf(tmp_ctx, "host/%s/%s", strlower_talloc(tmp_ctx, r->in.netbios_name), realm);
309
service_principal_name[4] = talloc_asprintf(tmp_ctx, "host/%s/%s", dns_host_name, r->out.domain_name);
310
service_principal_name[5] = talloc_asprintf(tmp_ctx, "host/%s/%s", strlower_talloc(tmp_ctx, r->in.netbios_name), r->out.domain_name);
312
for (i=0; i < ARRAY_SIZE(service_principal_name); i++) {
313
if (!service_principal_name[i]) {
314
r->out.error_string = NULL;
315
talloc_free(tmp_ctx);
316
return NT_STATUS_NO_MEMORY;
318
rtn = samdb_msg_add_string(remote_ldb, tmp_ctx, msg, "servicePrincipalName", service_principal_name[i]);
320
r->out.error_string = NULL;
321
talloc_free(tmp_ctx);
322
return NT_STATUS_NO_MEMORY;
326
rtn = samdb_msg_add_string(remote_ldb, tmp_ctx, msg, "dNSHostName", dns_host_name);
328
r->out.error_string = NULL;
329
talloc_free(tmp_ctx);
330
return NT_STATUS_NO_MEMORY;
333
rtn = samdb_replace(remote_ldb, tmp_ctx, msg);
337
"Failed to replace entries on %s",
338
ldb_dn_get_linearized(msg->dn));
339
talloc_free(tmp_ctx);
340
return NT_STATUS_INTERNAL_DB_CORRUPTION;
344
/* DsCrackNames to find out the DN of the domain. */
345
r_crack_names.in.req->req1.format_offered = DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT;
346
r_crack_names.in.req->req1.format_desired = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
347
names[0].str = talloc_asprintf(tmp_ctx, "%s\\", r->out.domain_name);
349
r->out.error_string = NULL;
350
talloc_free(tmp_ctx);
351
return NT_STATUS_NO_MEMORY;
354
status = dcerpc_drsuapi_DsCrackNames(drsuapi_pipe, tmp_ctx, &r_crack_names);
355
if (!NT_STATUS_IS_OK(status)) {
356
if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
359
"dcerpc_drsuapi_DsCrackNames for [%s] failed - %s",
361
dcerpc_errstr(tmp_ctx, drsuapi_pipe->last_fault_code));
362
talloc_free(tmp_ctx);
367
"dcerpc_drsuapi_DsCrackNames for [%s] failed - %s",
370
talloc_free(tmp_ctx);
373
} else if (!W_ERROR_IS_OK(r_crack_names.out.result)) {
376
"DsCrackNames failed - %s", win_errstr(r_crack_names.out.result));
377
talloc_free(tmp_ctx);
378
return NT_STATUS_UNSUCCESSFUL;
379
} else if (*r_crack_names.out.level_out != 1
380
|| !r_crack_names.out.ctr->ctr1
381
|| r_crack_names.out.ctr->ctr1->count != 1
382
|| !r_crack_names.out.ctr->ctr1->array[0].result_name
383
|| r_crack_names.out.ctr->ctr1->array[0].status != DRSUAPI_DS_NAME_STATUS_OK) {
384
r->out.error_string = talloc_asprintf(r, "DsCrackNames failed");
385
talloc_free(tmp_ctx);
386
return NT_STATUS_UNSUCCESSFUL;
389
/* Store the account DN. */
390
r->out.account_dn_str = account_dn_str;
391
talloc_steal(r, account_dn_str);
393
/* Store the domain DN. */
394
r->out.domain_dn_str = r_crack_names.out.ctr->ctr1->array[0].result_name;
395
talloc_steal(r, r_crack_names.out.ctr->ctr1->array[0].result_name);
397
/* Store the KVNO of the account, critical for some kerberos
399
r->out.kvno = ldb_msg_find_attr_as_uint(res->msgs[0], "msDS-KeyVersionNumber", 0);
401
/* Store the account GUID. */
402
r->out.account_guid = samdb_result_guid(res->msgs[0], "objectGUID");
404
if (r->in.acct_type == ACB_SVRTRUST) {
405
status = libnet_JoinSite(ctx, remote_ldb, r);
407
talloc_free(tmp_ctx);
413
* do a domain join using DCERPC/SAMR calls
414
* - connect to the LSA pipe, to try and find out information about the domain
415
* - create a secondary connection to SAMR pipe
416
* - do a samr_Connect to get a policy handle
417
* - do a samr_LookupDomain to get the domain sid
418
* - do a samr_OpenDomain to get a domain handle
419
* - do a samr_CreateAccount to try and get a new account
422
* - do a samr_LookupNames to get the users rid
423
* - do a samr_OpenUser to get a user handle
424
* - potentially delete and recreate the user
425
* - assert the account is of the right type with samrQueryUserInfo
427
* - call libnet_SetPassword_samr_handle to set the password,
428
* and pass a samr_UserInfo21 struct to set full_name and the account flags
430
* - do some ADS specific things when we join as Domain Controller,
431
* look at libnet_joinADSDomain() for the details
433
NTSTATUS libnet_JoinDomain(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_JoinDomain *r)
437
NTSTATUS status, cu_status;
439
struct libnet_RpcConnect *connect_with_info;
440
struct dcerpc_pipe *samr_pipe;
442
struct samr_Connect sc;
443
struct policy_handle p_handle;
444
struct samr_OpenDomain od;
445
struct policy_handle d_handle;
446
struct samr_LookupNames ln;
447
struct samr_Ids rids, types;
448
struct samr_OpenUser ou;
449
struct samr_CreateUser2 cu;
450
struct policy_handle *u_handle = NULL;
451
struct samr_QueryUserInfo qui;
452
union samr_UserInfo *uinfo;
453
struct samr_UserInfo21 u_info21;
454
union libnet_SetPassword r2;
455
struct samr_GetUserPwInfo pwp;
456
struct samr_PwInfo info;
457
struct lsa_String samr_account_name;
459
uint32_t acct_flags, old_acct_flags;
460
uint32_t rid, access_granted;
461
int policy_min_pw_len = 0;
463
struct dom_sid *account_sid = NULL;
464
const char *password_str = NULL;
466
r->out.error_string = NULL;
467
r2.samr_handle.out.error_string = NULL;
469
tmp_ctx = talloc_named(mem_ctx, 0, "libnet_Join temp context");
471
r->out.error_string = NULL;
472
return NT_STATUS_NO_MEMORY;
475
u_handle = talloc(tmp_ctx, struct policy_handle);
477
r->out.error_string = NULL;
478
talloc_free(tmp_ctx);
479
return NT_STATUS_NO_MEMORY;
482
connect_with_info = talloc(tmp_ctx, struct libnet_RpcConnect);
483
if (!connect_with_info) {
484
r->out.error_string = NULL;
485
talloc_free(tmp_ctx);
486
return NT_STATUS_NO_MEMORY;
489
/* prepare connect to the SAMR pipe of PDC */
490
if (r->in.level == LIBNET_JOINDOMAIN_AUTOMATIC) {
491
connect_with_info->in.binding = NULL;
492
connect_with_info->in.name = r->in.domain_name;
494
connect_with_info->in.binding = r->in.binding;
495
connect_with_info->in.name = NULL;
498
/* This level makes a connection to the LSA pipe on the way,
499
* to get some useful bits of information about the domain */
500
connect_with_info->level = LIBNET_RPC_CONNECT_DC_INFO;
501
connect_with_info->in.dcerpc_iface = &ndr_table_samr;
504
establish the SAMR connection
506
status = libnet_RpcConnect(ctx, tmp_ctx, connect_with_info);
507
if (!NT_STATUS_IS_OK(status)) {
509
r->out.error_string = talloc_asprintf(mem_ctx,
510
"Connection to SAMR pipe of DC %s failed: %s",
511
r->in.binding, connect_with_info->out.error_string);
513
r->out.error_string = talloc_asprintf(mem_ctx,
514
"Connection to SAMR pipe of PDC for %s failed: %s",
515
r->in.domain_name, connect_with_info->out.error_string);
517
talloc_free(tmp_ctx);
521
samr_pipe = connect_with_info->out.dcerpc_pipe;
523
status = dcerpc_pipe_auth(tmp_ctx, &samr_pipe,
524
connect_with_info->out.dcerpc_pipe->binding,
525
&ndr_table_samr, ctx->cred, ctx->lp_ctx);
526
if (!NT_STATUS_IS_OK(status)) {
527
r->out.error_string = talloc_asprintf(mem_ctx,
528
"SAMR bind failed: %s",
530
talloc_free(tmp_ctx);
534
/* prepare samr_Connect */
535
ZERO_STRUCT(p_handle);
536
sc.in.system_name = NULL;
537
sc.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
538
sc.out.connect_handle = &p_handle;
540
/* 2. do a samr_Connect to get a policy handle */
541
status = dcerpc_samr_Connect(samr_pipe, tmp_ctx, &sc);
542
if (!NT_STATUS_IS_OK(status)) {
543
r->out.error_string = talloc_asprintf(mem_ctx,
544
"samr_Connect failed: %s",
546
talloc_free(tmp_ctx);
550
/* If this is a connection on ncacn_ip_tcp to Win2k3 SP1, we don't get back this useful info */
551
if (!connect_with_info->out.domain_name) {
552
if (r->in.level == LIBNET_JOINDOMAIN_AUTOMATIC) {
553
connect_with_info->out.domain_name = talloc_strdup(tmp_ctx, r->in.domain_name);
555
/* Bugger, we just lost our way to automaticly find the domain name */
556
connect_with_info->out.domain_name = talloc_strdup(tmp_ctx, lp_workgroup(ctx->lp_ctx));
557
connect_with_info->out.realm = talloc_strdup(tmp_ctx, lp_realm(ctx->lp_ctx));
561
/* Perhaps we didn't get a SID above, because we are against ncacn_ip_tcp */
562
if (!connect_with_info->out.domain_sid) {
563
struct lsa_String name;
564
struct samr_LookupDomain l;
565
struct dom_sid2 *sid = NULL;
566
name.string = connect_with_info->out.domain_name;
567
l.in.connect_handle = &p_handle;
568
l.in.domain_name = &name;
571
status = dcerpc_samr_LookupDomain(samr_pipe, tmp_ctx, &l);
572
if (!NT_STATUS_IS_OK(status)) {
573
r->out.error_string = talloc_asprintf(mem_ctx,
574
"SAMR LookupDomain failed: %s",
576
talloc_free(tmp_ctx);
579
connect_with_info->out.domain_sid = *l.out.sid;
582
/* prepare samr_OpenDomain */
583
ZERO_STRUCT(d_handle);
584
od.in.connect_handle = &p_handle;
585
od.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
586
od.in.sid = connect_with_info->out.domain_sid;
587
od.out.domain_handle = &d_handle;
589
/* do a samr_OpenDomain to get a domain handle */
590
status = dcerpc_samr_OpenDomain(samr_pipe, tmp_ctx, &od);
591
if (!NT_STATUS_IS_OK(status)) {
592
r->out.error_string = talloc_asprintf(mem_ctx,
593
"samr_OpenDomain for [%s] failed: %s",
594
dom_sid_string(tmp_ctx, connect_with_info->out.domain_sid),
596
talloc_free(tmp_ctx);
600
/* prepare samr_CreateUser2 */
601
ZERO_STRUCTP(u_handle);
602
cu.in.domain_handle = &d_handle;
603
cu.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
604
samr_account_name.string = r->in.account_name;
605
cu.in.account_name = &samr_account_name;
606
cu.in.acct_flags = r->in.acct_type;
607
cu.out.user_handle = u_handle;
609
cu.out.access_granted = &access_granted;
611
/* do a samr_CreateUser2 to get an account handle, or an error */
612
cu_status = dcerpc_samr_CreateUser2(samr_pipe, tmp_ctx, &cu);
614
if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
615
/* prepare samr_LookupNames */
616
ln.in.domain_handle = &d_handle;
618
ln.in.names = talloc_array(tmp_ctx, struct lsa_String, 1);
620
ln.out.types = &types;
622
r->out.error_string = NULL;
623
talloc_free(tmp_ctx);
624
return NT_STATUS_NO_MEMORY;
626
ln.in.names[0].string = r->in.account_name;
628
/* 5. do a samr_LookupNames to get the users rid */
629
status = dcerpc_samr_LookupNames(samr_pipe, tmp_ctx, &ln);
630
if (!NT_STATUS_IS_OK(status)) {
631
r->out.error_string = talloc_asprintf(mem_ctx,
632
"samr_LookupNames for [%s] failed: %s",
635
talloc_free(tmp_ctx);
639
/* check if we got one RID for the user */
640
if (ln.out.rids->count != 1) {
641
r->out.error_string = talloc_asprintf(mem_ctx,
642
"samr_LookupNames for [%s] returns %d RIDs",
643
r->in.account_name, ln.out.rids->count);
644
talloc_free(tmp_ctx);
645
return NT_STATUS_INVALID_PARAMETER;
648
/* prepare samr_OpenUser */
649
ZERO_STRUCTP(u_handle);
650
ou.in.domain_handle = &d_handle;
651
ou.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
652
ou.in.rid = ln.out.rids->ids[0];
654
ou.out.user_handle = u_handle;
656
/* 6. do a samr_OpenUser to get a user handle */
657
status = dcerpc_samr_OpenUser(samr_pipe, tmp_ctx, &ou);
658
if (!NT_STATUS_IS_OK(status)) {
659
r->out.error_string = talloc_asprintf(mem_ctx,
660
"samr_OpenUser for [%s] failed: %s",
663
talloc_free(tmp_ctx);
667
if (r->in.recreate_account) {
668
struct samr_DeleteUser d;
669
d.in.user_handle = u_handle;
670
d.out.user_handle = u_handle;
671
status = dcerpc_samr_DeleteUser(samr_pipe, mem_ctx, &d);
672
if (!NT_STATUS_IS_OK(status)) {
673
r->out.error_string = talloc_asprintf(mem_ctx,
674
"samr_DeleteUser (for recreate) of [%s] failed: %s",
677
talloc_free(tmp_ctx);
681
/* We want to recreate, so delete and another samr_CreateUser2 */
683
/* &cu filled in above */
684
status = dcerpc_samr_CreateUser2(samr_pipe, tmp_ctx, &cu);
685
if (!NT_STATUS_IS_OK(status)) {
686
r->out.error_string = talloc_asprintf(mem_ctx,
687
"samr_CreateUser2 (recreate) for [%s] failed: %s",
688
r->in.account_name, nt_errstr(status));
689
talloc_free(tmp_ctx);
693
} else if (!NT_STATUS_IS_OK(status)) {
694
r->out.error_string = talloc_asprintf(mem_ctx,
695
"samr_CreateUser2 for [%s] failed: %s",
696
r->in.account_name, nt_errstr(status));
697
talloc_free(tmp_ctx);
701
/* prepare samr_QueryUserInfo (get flags) */
702
qui.in.user_handle = u_handle;
704
qui.out.info = &uinfo;
706
status = dcerpc_samr_QueryUserInfo(samr_pipe, tmp_ctx, &qui);
707
if (!NT_STATUS_IS_OK(status)) {
708
r->out.error_string = talloc_asprintf(mem_ctx,
709
"samr_QueryUserInfo for [%s] failed: %s",
712
talloc_free(tmp_ctx);
717
status = NT_STATUS_INVALID_PARAMETER;
719
= talloc_asprintf(mem_ctx,
720
"samr_QueryUserInfo failed to return qui.out.info for [%s]: %s",
721
r->in.account_name, nt_errstr(status));
722
talloc_free(tmp_ctx);
726
old_acct_flags = (uinfo->info16.acct_flags & (ACB_WSTRUST | ACB_SVRTRUST | ACB_DOMTRUST));
727
/* Possibly bail if the account is of the wrong type */
729
!= r->in.acct_type) {
730
const char *old_account_type, *new_account_type;
731
switch (old_acct_flags) {
733
old_account_type = "domain member (member)";
736
old_account_type = "domain controller (bdc)";
739
old_account_type = "trusted domain";
742
return NT_STATUS_INVALID_PARAMETER;
744
switch (r->in.acct_type) {
746
new_account_type = "domain member (member)";
749
new_account_type = "domain controller (bdc)";
752
new_account_type = "trusted domain";
755
return NT_STATUS_INVALID_PARAMETER;
758
if (!NT_STATUS_EQUAL(cu_status, NT_STATUS_USER_EXISTS)) {
759
/* We created a new user, but they didn't come out the right type?!? */
761
= talloc_asprintf(mem_ctx,
762
"We asked to create a new machine account (%s) of type %s, "
763
"but we got an account of type %s. This is unexpected. "
764
"Perhaps delete the account and try again.",
765
r->in.account_name, new_account_type, old_account_type);
766
talloc_free(tmp_ctx);
767
return NT_STATUS_INVALID_PARAMETER;
769
/* The account is of the wrong type, so bail */
771
/* TODO: We should allow a --force option to override, and redo this from the top setting r.in.recreate_account */
773
= talloc_asprintf(mem_ctx,
774
"The machine account (%s) already exists in the domain %s, "
775
"but is a %s. You asked to join as a %s. Please delete "
776
"the account and try again.",
777
r->in.account_name, connect_with_info->out.domain_name, old_account_type, new_account_type);
778
talloc_free(tmp_ctx);
779
return NT_STATUS_USER_EXISTS;
782
acct_flags = uinfo->info16.acct_flags;
785
acct_flags = (acct_flags & ~(ACB_DISABLED|ACB_PWNOTREQ));
787
/* Find out what password policy this user has */
788
pwp.in.user_handle = u_handle;
789
pwp.out.info = &info;
791
status = dcerpc_samr_GetUserPwInfo(samr_pipe, tmp_ctx, &pwp);
792
if (NT_STATUS_IS_OK(status)) {
793
policy_min_pw_len = pwp.out.info->min_password_length;
796
/* Grab a password of that minimum length */
798
password_str = generate_random_str(tmp_ctx, MAX(8, policy_min_pw_len));
800
/* set full_name and reset flags */
801
ZERO_STRUCT(u_info21);
802
u_info21.full_name.string = r->in.account_name;
803
u_info21.acct_flags = acct_flags;
804
u_info21.fields_present = SAMR_FIELD_FULL_NAME | SAMR_FIELD_ACCT_FLAGS;
806
r2.samr_handle.level = LIBNET_SET_PASSWORD_SAMR_HANDLE;
807
r2.samr_handle.in.account_name = r->in.account_name;
808
r2.samr_handle.in.newpassword = password_str;
809
r2.samr_handle.in.user_handle = u_handle;
810
r2.samr_handle.in.dcerpc_pipe = samr_pipe;
811
r2.samr_handle.in.info21 = &u_info21;
813
status = libnet_SetPassword(ctx, tmp_ctx, &r2);
814
if (!NT_STATUS_IS_OK(status)) {
815
r->out.error_string = talloc_steal(mem_ctx, r2.samr_handle.out.error_string);
816
talloc_free(tmp_ctx);
820
account_sid = dom_sid_add_rid(mem_ctx, connect_with_info->out.domain_sid, rid);
822
r->out.error_string = NULL;
823
talloc_free(tmp_ctx);
824
return NT_STATUS_NO_MEMORY;
827
/* Finish out by pushing various bits of status data out for the caller to use */
828
r->out.join_password = password_str;
829
talloc_steal(mem_ctx, r->out.join_password);
831
r->out.domain_sid = connect_with_info->out.domain_sid;
832
talloc_steal(mem_ctx, r->out.domain_sid);
834
r->out.account_sid = account_sid;
835
talloc_steal(mem_ctx, r->out.account_sid);
837
r->out.domain_name = connect_with_info->out.domain_name;
838
talloc_steal(mem_ctx, r->out.domain_name);
839
r->out.realm = connect_with_info->out.realm;
840
talloc_steal(mem_ctx, r->out.realm);
841
r->out.samr_pipe = samr_pipe;
842
talloc_steal(mem_ctx, samr_pipe);
843
r->out.samr_binding = samr_pipe->binding;
844
talloc_steal(mem_ctx, r->out.samr_binding);
845
r->out.user_handle = u_handle;
846
talloc_steal(mem_ctx, u_handle);
847
r->out.error_string = r2.samr_handle.out.error_string;
848
talloc_steal(mem_ctx, r2.samr_handle.out.error_string);
850
r->out.server_dn_str = NULL;
851
talloc_free(tmp_ctx);
853
/* Now, if it was AD, then we want to start looking changing a
854
* few more things. Otherwise, we are done. */
856
status = libnet_JoinADSDomain(ctx, r);
863
NTSTATUS libnet_set_join_secrets(struct libnet_context *ctx,
865
struct libnet_set_join_secrets *r)
869
struct ldb_context *ldb;
870
struct ldb_dn *base_dn;
871
struct ldb_message **msgs, *msg;
873
const char * const attrs[] = {
883
tmp_mem = talloc_new(mem_ctx);
885
return NT_STATUS_NO_MEMORY;
888
/* Open the secrets database */
889
ldb = secrets_db_connect(tmp_mem, ctx->event_ctx, ctx->lp_ctx);
892
= talloc_asprintf(mem_ctx,
893
"Could not open secrets database");
894
talloc_free(tmp_mem);
895
return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
899
* now prepare the record for secrets.ldb
901
sct = talloc_asprintf(tmp_mem, "%d", r->in.join_type);
903
r->out.error_string = NULL;
904
talloc_free(tmp_mem);
905
return NT_STATUS_NO_MEMORY;
908
msg = ldb_msg_new(tmp_mem);
910
r->out.error_string = NULL;
911
talloc_free(tmp_mem);
912
return NT_STATUS_NO_MEMORY;
915
base_dn = ldb_dn_new(tmp_mem, ldb, "cn=Primary Domains");
917
r->out.error_string = NULL;
918
talloc_free(tmp_mem);
919
return NT_STATUS_NO_MEMORY;
922
msg->dn = ldb_dn_copy(tmp_mem, base_dn);
923
if ( ! ldb_dn_add_child_fmt(msg->dn, "flatname=%s", r->in.domain_name)) {
924
r->out.error_string = NULL;
925
talloc_free(tmp_mem);
926
return NT_STATUS_NO_MEMORY;
929
rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "flatname", r->in.domain_name);
931
r->out.error_string = NULL;
932
talloc_free(tmp_mem);
933
return NT_STATUS_NO_MEMORY;
937
rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "realm", r->in.realm);
939
r->out.error_string = NULL;
940
talloc_free(tmp_mem);
941
return NT_STATUS_NO_MEMORY;
944
rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "objectClass", "primaryDomain");
946
r->out.error_string = NULL;
947
talloc_free(tmp_mem);
948
return NT_STATUS_NO_MEMORY;
952
rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "objectClass", "primaryDomain");
954
r->out.error_string = NULL;
955
talloc_free(tmp_mem);
956
return NT_STATUS_NO_MEMORY;
959
rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "secret", r->in.join_password);
961
r->out.error_string = NULL;
962
talloc_free(tmp_mem);
963
return NT_STATUS_NO_MEMORY;
966
rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "samAccountName", r->in.account_name);
968
r->out.error_string = NULL;
969
talloc_free(tmp_mem);
970
return NT_STATUS_NO_MEMORY;
973
rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "secureChannelType", sct);
975
r->out.error_string = NULL;
976
talloc_free(tmp_mem);
977
return NT_STATUS_NO_MEMORY;
981
rtn = samdb_msg_add_uint(ldb, tmp_mem, msg, "msDS-KeyVersionNumber",
984
r->out.error_string = NULL;
985
talloc_free(tmp_mem);
986
return NT_STATUS_NO_MEMORY;
990
if (r->in.domain_sid) {
991
rtn = samdb_msg_add_dom_sid(ldb, tmp_mem, msg, "objectSid",
994
r->out.error_string = NULL;
995
talloc_free(tmp_mem);
996
return NT_STATUS_NO_MEMORY;
1001
* search for the secret record
1002
* - remove the records we find
1003
* - and fetch the old secret and store it under priorSecret
1005
ret = gendb_search(ldb,
1008
"(|" SECRETS_PRIMARY_DOMAIN_FILTER "(realm=%s))",
1009
r->in.domain_name, r->in.realm);
1011
rtn = samdb_msg_set_string(ldb, tmp_mem, msg, "secretsKeytab", "secrets.keytab");
1013
r->out.error_string = NULL;
1014
talloc_free(tmp_mem);
1015
return NT_STATUS_NO_MEMORY;
1017
} else if (ret == -1) {
1019
= talloc_asprintf(mem_ctx,
1020
"Search for domain: %s and realm: %s failed: %s",
1021
r->in.domain_name, r->in.realm, ldb_errstring(ldb));
1022
talloc_free(tmp_mem);
1023
return NT_STATUS_INTERNAL_DB_CORRUPTION;
1025
const struct ldb_val *private_keytab;
1026
const struct ldb_val *krb5_main_keytab;
1027
const struct ldb_val *prior_secret;
1028
const struct ldb_val *prior_modified_time;
1031
for (i = 0; i < ret; i++) {
1032
ldb_delete(ldb, msgs[i]->dn);
1035
prior_secret = ldb_msg_find_ldb_val(msgs[0], "secret");
1037
rtn = samdb_msg_set_value(ldb, tmp_mem, msg, "priorSecret", prior_secret);
1039
r->out.error_string = NULL;
1040
talloc_free(tmp_mem);
1041
return NT_STATUS_NO_MEMORY;
1044
rtn = samdb_msg_set_string(ldb, tmp_mem, msg, "secret", r->in.join_password);
1046
r->out.error_string = NULL;
1047
talloc_free(tmp_mem);
1048
return NT_STATUS_NO_MEMORY;
1051
prior_modified_time = ldb_msg_find_ldb_val(msgs[0],
1053
if (prior_modified_time) {
1054
rtn = samdb_msg_set_value(ldb, tmp_mem, msg, "priorWhenChanged",
1055
prior_modified_time);
1057
r->out.error_string = NULL;
1058
talloc_free(tmp_mem);
1059
return NT_STATUS_NO_MEMORY;
1063
rtn = samdb_msg_set_string(ldb, tmp_mem, msg, "samAccountName", r->in.account_name);
1065
r->out.error_string = NULL;
1066
talloc_free(tmp_mem);
1067
return NT_STATUS_NO_MEMORY;
1070
rtn = samdb_msg_set_string(ldb, tmp_mem, msg, "secureChannelType", sct);
1072
r->out.error_string = NULL;
1073
talloc_free(tmp_mem);
1074
return NT_STATUS_NO_MEMORY;
1077
/* We will want to keep the keytab names */
1078
private_keytab = ldb_msg_find_ldb_val(msgs[0], "privateKeytab");
1079
if (private_keytab) {
1080
rtn = samdb_msg_set_value(ldb, tmp_mem, msg, "privateKeytab", private_keytab);
1082
r->out.error_string = NULL;
1083
talloc_free(tmp_mem);
1084
return NT_STATUS_NO_MEMORY;
1087
krb5_main_keytab = ldb_msg_find_ldb_val(msgs[0], "krb5Keytab");
1088
if (krb5_main_keytab) {
1089
rtn = samdb_msg_set_value(ldb, tmp_mem, msg,
1090
"krb5Keytab", krb5_main_keytab);
1092
r->out.error_string = NULL;
1093
talloc_free(tmp_mem);
1094
return NT_STATUS_NO_MEMORY;
1099
/* create the secret */
1100
ret = ldb_add(ldb, msg);
1102
r->out.error_string = talloc_asprintf(mem_ctx, "Failed to create secret record %s",
1103
ldb_dn_get_linearized(msg->dn));
1104
talloc_free(tmp_mem);
1105
return NT_STATUS_INTERNAL_DB_CORRUPTION;
1108
return NT_STATUS_OK;
1111
static NTSTATUS libnet_Join_primary_domain(struct libnet_context *ctx,
1112
TALLOC_CTX *mem_ctx,
1113
struct libnet_Join *r)
1116
TALLOC_CTX *tmp_mem;
1117
struct libnet_JoinDomain *r2;
1118
struct libnet_set_join_secrets *r3;
1119
uint32_t acct_type = 0;
1120
const char *account_name;
1121
const char *netbios_name;
1123
r->out.error_string = NULL;
1125
tmp_mem = talloc_new(mem_ctx);
1127
return NT_STATUS_NO_MEMORY;
1130
r2 = talloc(tmp_mem, struct libnet_JoinDomain);
1132
r->out.error_string = NULL;
1133
talloc_free(tmp_mem);
1134
return NT_STATUS_NO_MEMORY;
1137
if (r->in.join_type == SEC_CHAN_BDC) {
1138
acct_type = ACB_SVRTRUST;
1139
} else if (r->in.join_type == SEC_CHAN_WKSTA) {
1140
acct_type = ACB_WSTRUST;
1142
r->out.error_string = NULL;
1143
talloc_free(tmp_mem);
1144
return NT_STATUS_INVALID_PARAMETER;
1147
if (r->in.netbios_name != NULL) {
1148
netbios_name = r->in.netbios_name;
1150
netbios_name = talloc_reference(tmp_mem, lp_netbios_name(ctx->lp_ctx));
1151
if (!netbios_name) {
1152
r->out.error_string = NULL;
1153
talloc_free(tmp_mem);
1154
return NT_STATUS_NO_MEMORY;
1158
account_name = talloc_asprintf(tmp_mem, "%s$", netbios_name);
1159
if (!account_name) {
1160
r->out.error_string = NULL;
1161
talloc_free(tmp_mem);
1162
return NT_STATUS_NO_MEMORY;
1169
r2->in.domain_name = r->in.domain_name;
1170
r2->in.account_name = account_name;
1171
r2->in.netbios_name = netbios_name;
1172
r2->in.level = LIBNET_JOINDOMAIN_AUTOMATIC;
1173
r2->in.acct_type = acct_type;
1174
r2->in.recreate_account = false;
1175
status = libnet_JoinDomain(ctx, r2, r2);
1176
if (!NT_STATUS_IS_OK(status)) {
1177
r->out.error_string = talloc_steal(mem_ctx, r2->out.error_string);
1178
talloc_free(tmp_mem);
1182
r3 = talloc(tmp_mem, struct libnet_set_join_secrets);
1184
r->out.error_string = NULL;
1185
talloc_free(tmp_mem);
1186
return NT_STATUS_NO_MEMORY;
1190
r3->in.domain_name = r2->out.domain_name;
1191
r3->in.realm = r2->out.realm;
1192
r3->in.account_name = account_name;
1193
r3->in.netbios_name = netbios_name;
1194
r3->in.join_type = r->in.join_type;
1195
r3->in.join_password = r2->out.join_password;
1196
r3->in.kvno = r2->out.kvno;
1197
r3->in.domain_sid = r2->out.domain_sid;
1199
status = libnet_set_join_secrets(ctx, r3, r3);
1200
if (!NT_STATUS_IS_OK(status)) {
1201
r->out.error_string = talloc_steal(mem_ctx, r3->out.error_string);
1202
talloc_free(tmp_mem);
1206
/* move all out parameter to the callers TALLOC_CTX */
1207
r->out.error_string = NULL;
1208
r->out.join_password = r2->out.join_password;
1209
talloc_steal(mem_ctx, r2->out.join_password);
1210
r->out.domain_sid = r2->out.domain_sid;
1211
talloc_steal(mem_ctx, r2->out.domain_sid);
1212
r->out.domain_name = r2->out.domain_name;
1213
talloc_steal(mem_ctx, r2->out.domain_name);
1214
talloc_free(tmp_mem);
1215
return NT_STATUS_OK;
1218
NTSTATUS libnet_Join(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_Join *r)
1220
switch (r->in.join_type) {
1221
case SEC_CHAN_WKSTA:
1222
return libnet_Join_primary_domain(ctx, mem_ctx, r);
1224
return libnet_Join_primary_domain(ctx, mem_ctx, r);
1225
case SEC_CHAN_DOMAIN:
1226
case SEC_CHAN_DNS_DOMAIN:
1231
r->out.error_string = talloc_asprintf(mem_ctx,
1232
"Invalid join type specified (%08X) attempting to join domain %s",
1233
r->in.join_type, r->in.domain_name);
1234
return NT_STATUS_INVALID_PARAMETER;