r19266: Add a target_hostname element to the binding struct. This allows us
[Samba/ekacnet.git] / source4 / libnet / libnet_join.c
blob57ecddd9daaff906f2220c385ce4c30401235939
1 /*
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.
23 #include "includes.h"
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"
30 #include "db_wrap.h"
31 #include "libcli/security/security.h"
32 #include "auth/credentials/credentials.h"
33 #include "librpc/gen_ndr/ndr_samr_c.h"
36 * complete a domain join, when joining to a AD domain:
37 * 1.) connect and bind to the DRSUAPI pipe
38 * 2.) do a DsCrackNames() to find the machine account dn
39 * 3.) connect to LDAP
40 * 4.) do an ldap search to find the "msDS-KeyVersionNumber" of the machine account
41 * 5.) set the servicePrincipalName's of the machine account via LDAP, (maybe we should use DsWriteAccountSpn()...)
42 * 6.) do a DsCrackNames() to find the domain dn
43 * 7.) find out Site specific stuff, look at libnet_JoinSite() for details
45 static NTSTATUS libnet_JoinADSDomain(struct libnet_context *ctx, struct libnet_JoinDomain *r)
47 NTSTATUS status;
49 TALLOC_CTX *tmp_ctx;
51 const char *realm = r->out.realm;
53 struct dcerpc_binding *samr_binding = r->out.samr_binding;
55 struct dcerpc_pipe *drsuapi_pipe;
56 struct dcerpc_binding *drsuapi_binding;
57 struct drsuapi_DsBind r_drsuapi_bind;
58 struct drsuapi_DsCrackNames r_crack_names;
59 struct drsuapi_DsNameString names[1];
60 struct policy_handle drsuapi_bind_handle;
61 struct GUID drsuapi_bind_guid;
63 struct ldb_context *remote_ldb;
64 const struct ldb_dn *account_dn;
65 const char *account_dn_str;
66 const char *remote_ldb_url;
67 struct ldb_result *res;
68 struct ldb_message *msg;
70 int ret, rtn;
72 unsigned int kvno;
74 const char * const attrs[] = {
75 "msDS-KeyVersionNumber",
76 "servicePrincipalName",
77 "dNSHostName",
78 NULL,
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");
90 if (!tmp_ctx) {
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;
98 talloc_free(tmp_ctx);
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,
112 &drsuapi_pipe,
113 drsuapi_binding,
114 &dcerpc_table_drsuapi,
115 ctx->cred,
116 ctx->event_ctx);
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",
120 r->in.domain_name,
121 nt_errstr(status));
122 talloc_free(tmp_ctx);
123 return status;
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)) {
136 r->out.error_string
137 = talloc_asprintf(r,
138 "dcerpc_drsuapi_DsBind failed - %s",
139 dcerpc_errstr(tmp_ctx, drsuapi_pipe->last_fault_code));
140 talloc_free(tmp_ctx);
141 return status;
142 } else {
143 r->out.error_string
144 = talloc_asprintf(r,
145 "dcerpc_drsuapi_DsBind failed - %s",
146 nt_errstr(status));
147 talloc_free(tmp_ctx);
148 return status;
150 } else if (!W_ERROR_IS_OK(r_drsuapi_bind.out.result)) {
151 r->out.error_string
152 = talloc_asprintf(r,
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.unknown1 = 0x000004e4;
164 r_crack_names.in.req.req1.unknown2 = 0x00000407;
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);
171 if (!names[0].str) {
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)) {
180 r->out.error_string
181 = talloc_asprintf(r,
182 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s",
183 names[0].str,
184 dcerpc_errstr(tmp_ctx, drsuapi_pipe->last_fault_code));
185 talloc_free(tmp_ctx);
186 return status;
187 } else {
188 r->out.error_string
189 = talloc_asprintf(r,
190 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s",
191 names[0].str,
192 nt_errstr(status));
193 talloc_free(tmp_ctx);
194 return status;
196 } else if (!W_ERROR_IS_OK(r_crack_names.out.result)) {
197 r->out.error_string
198 = talloc_asprintf(r,
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 account_dn = ldb_dn_explode(tmp_ctx, account_dn_str);
222 if (!account_dn) {
223 r->out.error_string = talloc_asprintf(r, "Invalid account dn: %s",
224 account_dn_str);
225 talloc_free(tmp_ctx);
226 return NT_STATUS_UNSUCCESSFUL;
229 /* Now we know the user's DN, open with LDAP, read and modify a few things */
231 remote_ldb_url = talloc_asprintf(tmp_ctx, "ldap://%s",
232 drsuapi_binding->target_hostname);
233 if (!remote_ldb_url) {
234 r->out.error_string = NULL;
235 talloc_free(tmp_ctx);
236 return NT_STATUS_NO_MEMORY;
239 remote_ldb = ldb_wrap_connect(tmp_ctx, remote_ldb_url,
240 NULL, ctx->cred, 0, NULL);
241 if (!remote_ldb) {
242 r->out.error_string = NULL;
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,
249 NULL, attrs, &res);
250 if (ret != LDB_SUCCESS || res->count != 1) {
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 /* If we have a kvno recorded in AD, we need it locally as well */
258 kvno = ldb_msg_find_attr_as_uint(res->msgs[0], "msDS-KeyVersionNumber", 0);
260 /* Prepare a new message, for the modify */
261 msg = ldb_msg_new(tmp_ctx);
262 if (!msg) {
263 r->out.error_string = NULL;
264 talloc_free(tmp_ctx);
265 return NT_STATUS_NO_MEMORY;
267 msg->dn = res->msgs[0]->dn;
270 int i;
271 const char *service_principal_name[6];
272 const char *dns_host_name = strlower_talloc(tmp_ctx,
273 talloc_asprintf(tmp_ctx,
274 "%s.%s",
275 r->in.netbios_name,
276 realm));
278 if (!dns_host_name) {
279 r->out.error_string = NULL;
280 talloc_free(tmp_ctx);
281 return NT_STATUS_NO_MEMORY;
284 service_principal_name[0] = talloc_asprintf(tmp_ctx, "host/%s", dns_host_name);
285 service_principal_name[1] = talloc_asprintf(tmp_ctx, "host/%s", strlower_talloc(tmp_ctx, r->in.netbios_name));
286 service_principal_name[2] = talloc_asprintf(tmp_ctx, "host/%s/%s", dns_host_name, realm);
287 service_principal_name[3] = talloc_asprintf(tmp_ctx, "host/%s/%s", strlower_talloc(tmp_ctx, r->in.netbios_name), realm);
288 service_principal_name[4] = talloc_asprintf(tmp_ctx, "host/%s/%s", dns_host_name, r->out.domain_name);
289 service_principal_name[5] = talloc_asprintf(tmp_ctx, "host/%s/%s", strlower_talloc(tmp_ctx, r->in.netbios_name), r->out.domain_name);
291 for (i=0; i < ARRAY_SIZE(service_principal_name); i++) {
292 if (!service_principal_name[i]) {
293 r->out.error_string = NULL;
294 talloc_free(tmp_ctx);
295 return NT_STATUS_NO_MEMORY;
297 rtn = samdb_msg_add_string(remote_ldb, tmp_ctx, msg, "servicePrincipalName", service_principal_name[i]);
298 if (rtn == -1) {
299 r->out.error_string = NULL;
300 talloc_free(tmp_ctx);
301 return NT_STATUS_NO_MEMORY;
305 rtn = samdb_msg_add_string(remote_ldb, tmp_ctx, msg, "dNSHostName", dns_host_name);
306 if (rtn == -1) {
307 r->out.error_string = NULL;
308 talloc_free(tmp_ctx);
309 return NT_STATUS_NO_MEMORY;
312 rtn = samdb_replace(remote_ldb, tmp_ctx, msg);
313 if (rtn != 0) {
314 r->out.error_string
315 = talloc_asprintf(r,
316 "Failed to replace entries on %s",
317 ldb_dn_linearize(tmp_ctx, msg->dn));
318 talloc_free(tmp_ctx);
319 return NT_STATUS_INTERNAL_DB_CORRUPTION;
323 /* DsCrackNames to find out the DN of the domain. */
324 r_crack_names.in.req.req1.format_offered = DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT;
325 r_crack_names.in.req.req1.format_desired = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
326 names[0].str = talloc_asprintf(tmp_ctx, "%s\\", r->out.domain_name);
327 if (!names[0].str) {
328 r->out.error_string = NULL;
329 talloc_free(tmp_ctx);
330 return NT_STATUS_NO_MEMORY;
333 status = dcerpc_drsuapi_DsCrackNames(drsuapi_pipe, tmp_ctx, &r_crack_names);
334 if (!NT_STATUS_IS_OK(status)) {
335 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
336 r->out.error_string
337 = talloc_asprintf(r,
338 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s",
339 r->in.domain_name,
340 dcerpc_errstr(tmp_ctx, drsuapi_pipe->last_fault_code));
341 talloc_free(tmp_ctx);
342 return status;
343 } else {
344 r->out.error_string
345 = talloc_asprintf(r,
346 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s",
347 r->in.domain_name,
348 nt_errstr(status));
349 talloc_free(tmp_ctx);
350 return status;
352 } else if (!W_ERROR_IS_OK(r_crack_names.out.result)) {
353 r->out.error_string
354 = talloc_asprintf(r,
355 "DsCrackNames failed - %s", win_errstr(r_crack_names.out.result));
356 talloc_free(tmp_ctx);
357 return NT_STATUS_UNSUCCESSFUL;
358 } else if (r_crack_names.out.level != 1
359 || !r_crack_names.out.ctr.ctr1
360 || r_crack_names.out.ctr.ctr1->count != 1
361 || !r_crack_names.out.ctr.ctr1->array[0].result_name
362 || r_crack_names.out.ctr.ctr1->array[0].status != DRSUAPI_DS_NAME_STATUS_OK) {
363 r->out.error_string = talloc_asprintf(r, "DsCrackNames failed");
364 talloc_free(tmp_ctx);
365 return NT_STATUS_UNSUCCESSFUL;
368 /* Store the account DN. */
369 r->out.account_dn_str = account_dn_str;
370 talloc_steal(r, account_dn_str);
372 /* Store the domain DN. */
373 r->out.domain_dn_str = r_crack_names.out.ctr.ctr1->array[0].result_name;
374 talloc_steal(r, r_crack_names.out.ctr.ctr1->array[0].result_name);
376 r->out.kvno = kvno;
378 if (r->in.acct_type == ACB_SVRTRUST) {
379 status = libnet_JoinSite(remote_ldb, r);
381 talloc_free(tmp_ctx);
383 return status;
387 * do a domain join using DCERPC/SAMR calls
388 * - connect to the LSA pipe, to try and find out information about the domain
389 * - create a secondary connection to SAMR pipe
390 * - do a samr_Connect to get a policy handle
391 * - do a samr_LookupDomain to get the domain sid
392 * - do a samr_OpenDomain to get a domain handle
393 * - do a samr_CreateAccount to try and get a new account
395 * If that fails, do:
396 * - do a samr_LookupNames to get the users rid
397 * - do a samr_OpenUser to get a user handle
398 * - potentially delete and recreate the user
399 * - assert the account is of the right type with samrQueryUserInfo
401 * - call libnet_SetPassword_samr_handle to set the password
403 * - do a samrSetUserInfo to set the account flags
404 * - do some ADS specific things when we join as Domain Controller,
405 * look at libnet_joinADSDomain() for the details
407 NTSTATUS libnet_JoinDomain(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_JoinDomain *r)
409 TALLOC_CTX *tmp_ctx;
411 NTSTATUS status, cu_status;
413 struct libnet_RpcConnect *connect_with_info;
414 struct dcerpc_pipe *samr_pipe;
416 struct samr_Connect sc;
417 struct policy_handle p_handle;
418 struct samr_OpenDomain od;
419 struct policy_handle d_handle;
420 struct samr_LookupNames ln;
421 struct samr_OpenUser ou;
422 struct samr_CreateUser2 cu;
423 struct policy_handle *u_handle = NULL;
424 struct samr_QueryUserInfo qui;
425 struct samr_SetUserInfo sui;
426 union samr_UserInfo u_info;
427 union libnet_SetPassword r2;
428 struct samr_GetUserPwInfo pwp;
429 struct lsa_String samr_account_name;
431 uint32_t acct_flags, old_acct_flags;
432 uint32_t rid, access_granted;
433 int policy_min_pw_len = 0;
435 struct dom_sid *account_sid = NULL;
436 const char *password_str = NULL;
438 r->out.error_string = NULL;
439 r2.samr_handle.out.error_string = NULL;
441 tmp_ctx = talloc_named(mem_ctx, 0, "libnet_Join temp context");
442 if (!tmp_ctx) {
443 r->out.error_string = NULL;
444 return NT_STATUS_NO_MEMORY;
447 u_handle = talloc(tmp_ctx, struct policy_handle);
448 if (!u_handle) {
449 r->out.error_string = NULL;
450 talloc_free(tmp_ctx);
451 return NT_STATUS_NO_MEMORY;
454 connect_with_info = talloc(tmp_ctx, struct libnet_RpcConnect);
455 if (!connect_with_info) {
456 r->out.error_string = NULL;
457 talloc_free(tmp_ctx);
458 return NT_STATUS_NO_MEMORY;
461 /* prepare connect to the LSA pipe of PDC */
462 if (r->in.level == LIBNET_JOINDOMAIN_AUTOMATIC) {
463 connect_with_info->in.binding = NULL;
464 connect_with_info->in.name = r->in.domain_name;
465 } else {
466 connect_with_info->in.binding = r->in.binding;
467 connect_with_info->in.name = NULL;
470 connect_with_info->level = LIBNET_RPC_CONNECT_DC_INFO;
471 connect_with_info->in.dcerpc_iface = &dcerpc_table_samr;
474 establish a SAMR connection, on the same CIFS transport
476 status = libnet_RpcConnect(ctx, tmp_ctx, connect_with_info);
477 if (!NT_STATUS_IS_OK(status)) {
478 if (r->in.binding) {
479 r->out.error_string = talloc_asprintf(mem_ctx,
480 "Connection to SAMR pipe of DC %s failed: %s",
481 r->in.binding, connect_with_info->out.error_string);
482 } else {
483 r->out.error_string = talloc_asprintf(mem_ctx,
484 "Connection to SAMR pipe of PDC for %s failed: %s",
485 r->in.domain_name, connect_with_info->out.error_string);
487 talloc_free(tmp_ctx);
488 return status;
491 samr_pipe = connect_with_info->out.dcerpc_pipe;
493 status = dcerpc_pipe_auth(tmp_ctx, &samr_pipe,
494 connect_with_info->out.dcerpc_pipe->binding,
495 &dcerpc_table_samr, ctx->cred);
496 if (!NT_STATUS_IS_OK(status)) {
497 r->out.error_string = talloc_asprintf(mem_ctx,
498 "SAMR bind failed: %s",
499 nt_errstr(status));
500 talloc_free(tmp_ctx);
501 return status;
504 /* prepare samr_Connect */
505 ZERO_STRUCT(p_handle);
506 sc.in.system_name = NULL;
507 sc.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
508 sc.out.connect_handle = &p_handle;
510 /* 2. do a samr_Connect to get a policy handle */
511 status = dcerpc_samr_Connect(samr_pipe, tmp_ctx, &sc);
512 if (!NT_STATUS_IS_OK(status)) {
513 r->out.error_string = talloc_asprintf(mem_ctx,
514 "samr_Connect failed: %s",
515 nt_errstr(status));
516 talloc_free(tmp_ctx);
517 return status;
520 /* If this is a connection on ncacn_ip_tcp to Win2k3 SP1, we don't get back this useful info */
521 if (!connect_with_info->out.domain_name) {
522 if (r->in.level == LIBNET_JOINDOMAIN_AUTOMATIC) {
523 connect_with_info->out.domain_name = talloc_strdup(tmp_ctx, r->in.domain_name);
524 } else {
525 /* Bugger, we just lost our way to automaticly find the domain name */
526 connect_with_info->out.domain_name = talloc_strdup(tmp_ctx, lp_workgroup());
530 /* Perhaps we didn't get a SID above, because we are against ncacn_ip_tcp */
531 if (!connect_with_info->out.domain_sid) {
532 struct lsa_String name;
533 struct samr_LookupDomain l;
534 name.string = connect_with_info->out.domain_name;
535 l.in.connect_handle = &p_handle;
536 l.in.domain_name = &name;
538 status = dcerpc_samr_LookupDomain(samr_pipe, tmp_ctx, &l);
539 if (!NT_STATUS_IS_OK(status)) {
540 r->out.error_string = talloc_asprintf(mem_ctx,
541 "SAMR LookupDomain failed: %s",
542 nt_errstr(status));
543 talloc_free(tmp_ctx);
544 return status;
546 connect_with_info->out.domain_sid = l.out.sid;
549 /* prepare samr_OpenDomain */
550 ZERO_STRUCT(d_handle);
551 od.in.connect_handle = &p_handle;
552 od.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
553 od.in.sid = connect_with_info->out.domain_sid;
554 od.out.domain_handle = &d_handle;
556 /* do a samr_OpenDomain to get a domain handle */
557 status = dcerpc_samr_OpenDomain(samr_pipe, tmp_ctx, &od);
558 if (!NT_STATUS_IS_OK(status)) {
559 r->out.error_string = talloc_asprintf(mem_ctx,
560 "samr_OpenDomain for [%s] failed: %s",
561 dom_sid_string(tmp_ctx, connect_with_info->out.domain_sid),
562 nt_errstr(status));
563 talloc_free(tmp_ctx);
564 return status;
567 /* prepare samr_CreateUser2 */
568 ZERO_STRUCTP(u_handle);
569 cu.in.domain_handle = &d_handle;
570 cu.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
571 samr_account_name.string = r->in.account_name;
572 cu.in.account_name = &samr_account_name;
573 cu.in.acct_flags = r->in.acct_type;
574 cu.out.user_handle = u_handle;
575 cu.out.rid = &rid;
576 cu.out.access_granted = &access_granted;
578 /* do a samr_CreateUser2 to get an account handle, or an error */
579 cu_status = dcerpc_samr_CreateUser2(samr_pipe, tmp_ctx, &cu);
580 status = cu_status;
581 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
582 /* prepare samr_LookupNames */
583 ln.in.domain_handle = &d_handle;
584 ln.in.num_names = 1;
585 ln.in.names = talloc_array(tmp_ctx, struct lsa_String, 1);
586 if (!ln.in.names) {
587 r->out.error_string = NULL;
588 talloc_free(tmp_ctx);
589 return NT_STATUS_NO_MEMORY;
591 ln.in.names[0].string = r->in.account_name;
593 /* 5. do a samr_LookupNames to get the users rid */
594 status = dcerpc_samr_LookupNames(samr_pipe, tmp_ctx, &ln);
595 if (!NT_STATUS_IS_OK(status)) {
596 r->out.error_string = talloc_asprintf(mem_ctx,
597 "samr_LookupNames for [%s] failed: %s",
598 r->in.account_name,
599 nt_errstr(status));
600 talloc_free(tmp_ctx);
601 return status;
604 /* check if we got one RID for the user */
605 if (ln.out.rids.count != 1) {
606 r->out.error_string = talloc_asprintf(mem_ctx,
607 "samr_LookupNames for [%s] returns %d RIDs",
608 r->in.account_name, ln.out.rids.count);
609 talloc_free(tmp_ctx);
610 return NT_STATUS_INVALID_PARAMETER;
613 /* prepare samr_OpenUser */
614 ZERO_STRUCTP(u_handle);
615 ou.in.domain_handle = &d_handle;
616 ou.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
617 ou.in.rid = ln.out.rids.ids[0];
618 rid = ou.in.rid;
619 ou.out.user_handle = u_handle;
621 /* 6. do a samr_OpenUser to get a user handle */
622 status = dcerpc_samr_OpenUser(samr_pipe, tmp_ctx, &ou);
623 if (!NT_STATUS_IS_OK(status)) {
624 r->out.error_string = talloc_asprintf(mem_ctx,
625 "samr_OpenUser for [%s] failed: %s",
626 r->in.account_name,
627 nt_errstr(status));
628 talloc_free(tmp_ctx);
629 return status;
632 if (r->in.recreate_account) {
633 struct samr_DeleteUser d;
634 d.in.user_handle = u_handle;
635 d.out.user_handle = u_handle;
636 status = dcerpc_samr_DeleteUser(samr_pipe, mem_ctx, &d);
637 if (!NT_STATUS_IS_OK(status)) {
638 r->out.error_string = talloc_asprintf(mem_ctx,
639 "samr_DeleteUser (for recreate) of [%s] failed: %s",
640 r->in.account_name,
641 nt_errstr(status));
642 talloc_free(tmp_ctx);
643 return status;
646 /* We want to recreate, so delete and another samr_CreateUser2 */
648 /* &cu filled in above */
649 status = dcerpc_samr_CreateUser2(samr_pipe, tmp_ctx, &cu);
650 if (!NT_STATUS_IS_OK(status)) {
651 r->out.error_string = talloc_asprintf(mem_ctx,
652 "samr_CreateUser2 (recreate) for [%s] failed: %s",
653 r->in.account_name, nt_errstr(status));
654 talloc_free(tmp_ctx);
655 return status;
658 } else if (!NT_STATUS_IS_OK(status)) {
659 r->out.error_string = talloc_asprintf(mem_ctx,
660 "samr_CreateUser2 for [%s] failed: %s",
661 r->in.account_name, nt_errstr(status));
662 talloc_free(tmp_ctx);
663 return status;
666 /* prepare samr_QueryUserInfo (get flags) */
667 qui.in.user_handle = u_handle;
668 qui.in.level = 16;
670 status = dcerpc_samr_QueryUserInfo(samr_pipe, tmp_ctx, &qui);
671 if (!NT_STATUS_IS_OK(status)) {
672 r->out.error_string = talloc_asprintf(mem_ctx,
673 "samr_QueryUserInfo for [%s] failed: %s",
674 r->in.account_name,
675 nt_errstr(status));
676 talloc_free(tmp_ctx);
677 return status;
680 if (!qui.out.info) {
681 status = NT_STATUS_INVALID_PARAMETER;
682 r->out.error_string
683 = talloc_asprintf(mem_ctx,
684 "samr_QueryUserInfo failed to return qui.out.info for [%s]: %s",
685 r->in.account_name, nt_errstr(status));
686 talloc_free(tmp_ctx);
687 return status;
690 old_acct_flags = (qui.out.info->info16.acct_flags & (ACB_WSTRUST | ACB_SVRTRUST | ACB_DOMTRUST));
691 /* Possibly bail if the account is of the wrong type */
692 if (old_acct_flags
693 != r->in.acct_type) {
694 const char *old_account_type, *new_account_type;
695 switch (old_acct_flags) {
696 case ACB_WSTRUST:
697 old_account_type = "domain member (member)";
698 break;
699 case ACB_SVRTRUST:
700 old_account_type = "domain controller (bdc)";
701 break;
702 case ACB_DOMTRUST:
703 old_account_type = "trusted domain";
704 break;
705 default:
706 return NT_STATUS_INVALID_PARAMETER;
708 switch (r->in.acct_type) {
709 case ACB_WSTRUST:
710 new_account_type = "domain member (member)";
711 break;
712 case ACB_SVRTRUST:
713 new_account_type = "domain controller (bdc)";
714 break;
715 case ACB_DOMTRUST:
716 new_account_type = "trusted domain";
717 break;
718 default:
719 return NT_STATUS_INVALID_PARAMETER;
722 if (!NT_STATUS_EQUAL(cu_status, NT_STATUS_USER_EXISTS)) {
723 /* We created a new user, but they didn't come out the right type?!? */
724 r->out.error_string
725 = talloc_asprintf(mem_ctx,
726 "We asked to create a new machine account (%s) of type %s, "
727 "but we got an account of type %s. This is unexpected. "
728 "Perhaps delete the account and try again.",
729 r->in.account_name, new_account_type, old_account_type);
730 talloc_free(tmp_ctx);
731 return NT_STATUS_INVALID_PARAMETER;
732 } else {
733 /* The account is of the wrong type, so bail */
735 /* TODO: We should allow a --force option to override, and redo this from the top setting r.in.recreate_account */
736 r->out.error_string
737 = talloc_asprintf(mem_ctx,
738 "The machine account (%s) already exists in the domain %s, "
739 "but is a %s. You asked to join as a %s. Please delete "
740 "the account and try again.",
741 r->in.account_name, connect_with_info->out.domain_name, old_account_type, new_account_type);
742 talloc_free(tmp_ctx);
743 return NT_STATUS_USER_EXISTS;
745 } else {
746 acct_flags = qui.out.info->info16.acct_flags;
749 acct_flags = (acct_flags & ~ACB_DISABLED);
751 /* Find out what password policy this user has */
752 pwp.in.user_handle = u_handle;
754 status = dcerpc_samr_GetUserPwInfo(samr_pipe, tmp_ctx, &pwp);
755 if (NT_STATUS_IS_OK(status)) {
756 policy_min_pw_len = pwp.out.info.min_password_length;
759 /* Grab a password of that minimum length */
761 password_str = generate_random_str(tmp_ctx, MAX(8, policy_min_pw_len));
763 r2.samr_handle.level = LIBNET_SET_PASSWORD_SAMR_HANDLE;
764 r2.samr_handle.in.account_name = r->in.account_name;
765 r2.samr_handle.in.newpassword = password_str;
766 r2.samr_handle.in.user_handle = u_handle;
767 r2.samr_handle.in.dcerpc_pipe = samr_pipe;
769 status = libnet_SetPassword(ctx, tmp_ctx, &r2);
770 if (!NT_STATUS_IS_OK(status)) {
771 r->out.error_string = talloc_steal(mem_ctx, r2.samr_handle.out.error_string);
772 talloc_free(tmp_ctx);
773 return status;
776 /* reset flags (if required) */
777 if (acct_flags != qui.out.info->info16.acct_flags) {
778 ZERO_STRUCT(u_info);
779 u_info.info16.acct_flags = acct_flags;
781 sui.in.user_handle = u_handle;
782 sui.in.info = &u_info;
783 sui.in.level = 16;
785 dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
786 if (!NT_STATUS_IS_OK(status)) {
787 r->out.error_string = talloc_asprintf(mem_ctx,
788 "samr_SetUserInfo for [%s] failed to remove ACB_DISABLED flag: %s",
789 r->in.account_name,
790 nt_errstr(status));
791 talloc_free(tmp_ctx);
792 return status;
796 account_sid = dom_sid_add_rid(mem_ctx, connect_with_info->out.domain_sid, rid);
797 if (!account_sid) {
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);
825 r->out.kvno = 0;
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. */
831 if (r->out.realm) {
832 status = libnet_JoinADSDomain(ctx, r);
833 return status;
836 return status;
839 static NTSTATUS libnet_Join_primary_domain(struct libnet_context *ctx,
840 TALLOC_CTX *mem_ctx,
841 struct libnet_Join *r)
843 NTSTATUS status;
844 TALLOC_CTX *tmp_mem;
845 struct libnet_JoinDomain *r2;
846 int ret, rtn;
847 struct ldb_context *ldb;
848 const struct ldb_dn *base_dn;
849 struct ldb_message **msgs, *msg;
850 const char *sct;
851 const char * const attrs[] = {
852 "whenChanged",
853 "secret",
854 "priorSecret",
855 "priorChanged",
856 "krb5Keytab",
857 "privateKeytab",
858 NULL
860 uint32_t acct_type = 0;
861 const char *account_name;
862 const char *netbios_name;
863 char *filter;
865 r->out.error_string = NULL;
867 tmp_mem = talloc_new(mem_ctx);
868 if (!tmp_mem) {
869 return NT_STATUS_NO_MEMORY;
872 r2 = talloc(tmp_mem, struct libnet_JoinDomain);
873 if (!r2) {
874 r->out.error_string = NULL;
875 talloc_free(tmp_mem);
876 return NT_STATUS_NO_MEMORY;
879 if (r->in.join_type == SEC_CHAN_BDC) {
880 acct_type = ACB_SVRTRUST;
881 } else if (r->in.join_type == SEC_CHAN_WKSTA) {
882 acct_type = ACB_WSTRUST;
883 } else {
884 r->out.error_string = NULL;
885 talloc_free(tmp_mem);
886 return NT_STATUS_INVALID_PARAMETER;
889 if (r->in.netbios_name != NULL) {
890 netbios_name = r->in.netbios_name;
891 } else {
892 netbios_name = talloc_reference(tmp_mem, lp_netbios_name());
893 if (!netbios_name) {
894 r->out.error_string = NULL;
895 talloc_free(tmp_mem);
896 return NT_STATUS_NO_MEMORY;
900 account_name = talloc_asprintf(tmp_mem, "%s$", netbios_name);
901 if (!account_name) {
902 r->out.error_string = NULL;
903 talloc_free(tmp_mem);
904 return NT_STATUS_NO_MEMORY;
908 * Local secrets are stored in secrets.ldb
909 * open it to make sure we can write the info into it after the join
911 ldb = secrets_db_connect(tmp_mem);
912 if (!ldb) {
913 r->out.error_string
914 = talloc_asprintf(mem_ctx,
915 "Could not open secrets database");
916 talloc_free(tmp_mem);
917 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
921 * join the domain
923 ZERO_STRUCTP(r2);
924 r2->in.domain_name = r->in.domain_name;
925 r2->in.account_name = account_name;
926 r2->in.netbios_name = netbios_name;
927 r2->in.level = LIBNET_JOINDOMAIN_AUTOMATIC;
928 r2->in.acct_type = acct_type;
929 r2->in.recreate_account = False;
930 status = libnet_JoinDomain(ctx, r2, r2);
931 if (!NT_STATUS_IS_OK(status)) {
932 r->out.error_string = talloc_steal(mem_ctx, r2->out.error_string);
933 talloc_free(tmp_mem);
934 return status;
938 * now prepare the record for secrets.ldb
940 sct = talloc_asprintf(tmp_mem, "%d", r->in.join_type);
941 if (!sct) {
942 r->out.error_string = NULL;
943 talloc_free(tmp_mem);
944 return NT_STATUS_NO_MEMORY;
947 msg = ldb_msg_new(tmp_mem);
948 if (!msg) {
949 r->out.error_string = NULL;
950 talloc_free(tmp_mem);
951 return NT_STATUS_NO_MEMORY;
954 base_dn = ldb_dn_explode(tmp_mem, "cn=Primary Domains");
955 if (!base_dn) {
956 r->out.error_string = NULL;
957 talloc_free(tmp_mem);
958 return NT_STATUS_NO_MEMORY;
961 msg->dn = ldb_dn_build_child(tmp_mem, "flatname", r2->out.domain_name, base_dn);
962 if (!msg->dn) {
963 r->out.error_string = NULL;
964 talloc_free(tmp_mem);
965 return NT_STATUS_NO_MEMORY;
968 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "flatname", r2->out.domain_name);
969 if (rtn == -1) {
970 r->out.error_string = NULL;
971 talloc_free(tmp_mem);
972 return NT_STATUS_NO_MEMORY;
975 if (r2->out.realm) {
976 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "realm", r2->out.realm);
977 if (rtn == -1) {
978 r->out.error_string = NULL;
979 talloc_free(tmp_mem);
980 return NT_STATUS_NO_MEMORY;
983 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "objectClass", "primaryDomain");
984 if (rtn == -1) {
985 r->out.error_string = NULL;
986 talloc_free(tmp_mem);
987 return NT_STATUS_NO_MEMORY;
991 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "objectClass", "primaryDomain");
992 if (rtn == -1) {
993 r->out.error_string = NULL;
994 talloc_free(tmp_mem);
995 return NT_STATUS_NO_MEMORY;
998 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "secret", r2->out.join_password);
999 if (rtn == -1) {
1000 r->out.error_string = NULL;
1001 talloc_free(tmp_mem);
1002 return NT_STATUS_NO_MEMORY;
1005 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "samAccountName", r2->in.account_name);
1006 if (rtn == -1) {
1007 r->out.error_string = NULL;
1008 talloc_free(tmp_mem);
1009 return NT_STATUS_NO_MEMORY;
1012 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "secureChannelType", sct);
1013 if (rtn == -1) {
1014 r->out.error_string = NULL;
1015 talloc_free(tmp_mem);
1016 return NT_STATUS_NO_MEMORY;
1019 if (r2->out.kvno) {
1020 rtn = samdb_msg_add_uint(ldb, tmp_mem, msg, "msDS-KeyVersionNumber",
1021 r2->out.kvno);
1022 if (rtn == -1) {
1023 r->out.error_string = NULL;
1024 talloc_free(tmp_mem);
1025 return NT_STATUS_NO_MEMORY;
1029 if (r2->out.domain_sid) {
1030 rtn = samdb_msg_add_dom_sid(ldb, tmp_mem, msg, "objectSid",
1031 r2->out.domain_sid);
1032 if (rtn == -1) {
1033 r->out.error_string = NULL;
1034 talloc_free(tmp_mem);
1035 return NT_STATUS_NO_MEMORY;
1040 * search for the secret record
1041 * - remove the records we find
1042 * - and fetch the old secret and store it under priorSecret
1044 ret = gendb_search(ldb,
1045 tmp_mem, base_dn,
1046 &msgs, attrs,
1047 "(|" SECRETS_PRIMARY_DOMAIN_FILTER "(realm=%s))",
1048 r2->out.domain_name, r2->out.realm);
1049 if (ret == 0) {
1050 rtn = samdb_msg_set_string(ldb, tmp_mem, msg, "secretsKeytab", "secrets.keytab");
1051 if (rtn == -1) {
1052 r->out.error_string = NULL;
1053 talloc_free(tmp_mem);
1054 return NT_STATUS_NO_MEMORY;
1056 } else if (ret == -1) {
1057 r->out.error_string
1058 = talloc_asprintf(mem_ctx,
1059 "Search for domain: %s and realm: %s failed: %s",
1060 r2->out.domain_name, r2->out.realm, ldb_errstring(ldb));
1061 talloc_free(tmp_mem);
1062 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1063 } else {
1064 const struct ldb_val *private_keytab;
1065 const struct ldb_val *krb5_keytab;
1066 const struct ldb_val *prior_secret;
1067 const struct ldb_val *prior_modified_time;
1068 int i;
1070 for (i = 0; i < ret; i++) {
1071 ldb_delete(ldb, msgs[i]->dn);
1074 prior_secret = ldb_msg_find_ldb_val(msgs[0], "secret");
1075 if (prior_secret) {
1076 rtn = samdb_msg_set_value(ldb, tmp_mem, msg, "priorSecret", prior_secret);
1077 if (rtn == -1) {
1078 r->out.error_string = NULL;
1079 talloc_free(tmp_mem);
1080 return NT_STATUS_NO_MEMORY;
1083 rtn = samdb_msg_set_string(ldb, tmp_mem, msg, "secret", r2->out.join_password);
1084 if (rtn == -1) {
1085 r->out.error_string = NULL;
1086 talloc_free(tmp_mem);
1087 return NT_STATUS_NO_MEMORY;
1090 prior_modified_time = ldb_msg_find_ldb_val(msgs[0],
1091 "whenChanged");
1092 if (prior_modified_time) {
1093 rtn = samdb_msg_set_value(ldb, tmp_mem, msg, "priorWhenChanged",
1094 prior_modified_time);
1095 if (rtn == -1) {
1096 r->out.error_string = NULL;
1097 talloc_free(tmp_mem);
1098 return NT_STATUS_NO_MEMORY;
1102 rtn = samdb_msg_set_string(ldb, tmp_mem, msg, "samAccountName", r2->in.account_name);
1103 if (rtn == -1) {
1104 r->out.error_string = NULL;
1105 talloc_free(tmp_mem);
1106 return NT_STATUS_NO_MEMORY;
1109 rtn = samdb_msg_set_string(ldb, tmp_mem, msg, "secureChannelType", sct);
1110 if (rtn == -1) {
1111 r->out.error_string = NULL;
1112 talloc_free(tmp_mem);
1113 return NT_STATUS_NO_MEMORY;
1116 /* We will want to keep the keytab names */
1117 private_keytab = ldb_msg_find_ldb_val(msgs[0], "privateKeytab");
1118 if (private_keytab) {
1119 rtn = samdb_msg_set_value(ldb, tmp_mem, msg, "privateKeytab", private_keytab);
1120 if (rtn == -1) {
1121 r->out.error_string = NULL;
1122 talloc_free(tmp_mem);
1123 return NT_STATUS_NO_MEMORY;
1126 krb5_keytab = ldb_msg_find_ldb_val(msgs[0], "krb5Keytab");
1127 if (krb5_keytab) {
1128 rtn = samdb_msg_set_value(ldb, tmp_mem, msg, "krb5Keytab", krb5_keytab);
1129 if (rtn == -1) {
1130 r->out.error_string = NULL;
1131 talloc_free(tmp_mem);
1132 return NT_STATUS_NO_MEMORY;
1137 /* create the secret */
1138 ret = samdb_add(ldb, tmp_mem, msg);
1139 if (ret != 0) {
1140 r->out.error_string = talloc_asprintf(mem_ctx, "Failed to create secret record %s",
1141 ldb_dn_linearize(ldb, msg->dn));
1142 talloc_free(tmp_mem);
1143 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1146 if (r2->out.realm) {
1147 struct cli_credentials *creds;
1148 /* Make a credentials structure from it */
1149 creds = cli_credentials_init(mem_ctx);
1150 if (!creds) {
1151 r->out.error_string = NULL;
1152 talloc_free(tmp_mem);
1153 return NT_STATUS_NO_MEMORY;
1155 cli_credentials_set_conf(creds);
1156 filter = talloc_asprintf(mem_ctx, "dn=%s", ldb_dn_linearize(mem_ctx, msg->dn));
1157 status = cli_credentials_set_secrets(creds, NULL, filter);
1158 if (!NT_STATUS_IS_OK(status)) {
1159 r->out.error_string = talloc_asprintf(mem_ctx, "Failed to read secrets for keytab update for %s",
1160 filter);
1161 talloc_free(tmp_mem);
1162 return status;
1164 ret = cli_credentials_update_keytab(creds);
1165 if (ret != 0) {
1166 r->out.error_string = talloc_asprintf(mem_ctx, "Failed to update keytab for %s",
1167 filter);
1168 talloc_free(tmp_mem);
1169 return NT_STATUS_UNSUCCESSFUL;
1173 /* move all out parameter to the callers TALLOC_CTX */
1174 r->out.error_string = NULL;
1175 r->out.join_password = r2->out.join_password;
1176 talloc_steal(mem_ctx, r2->out.join_password);
1177 r->out.domain_sid = r2->out.domain_sid;
1178 talloc_steal(mem_ctx, r2->out.domain_sid);
1179 r->out.domain_name = r2->out.domain_name;
1180 talloc_steal(mem_ctx, r2->out.domain_name);
1181 talloc_free(tmp_mem);
1182 return NT_STATUS_OK;
1185 NTSTATUS libnet_Join(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_Join *r)
1187 switch (r->in.join_type) {
1188 case SEC_CHAN_WKSTA:
1189 return libnet_Join_primary_domain(ctx, mem_ctx, r);
1190 case SEC_CHAN_BDC:
1191 return libnet_Join_primary_domain(ctx, mem_ctx, r);
1192 case SEC_CHAN_DOMAIN:
1193 break;
1196 r->out.error_string = talloc_asprintf(mem_ctx,
1197 "Invalid join type specified (%08X) attempting to join domain %s",
1198 r->in.join_type, r->in.domain_name);
1199 return NT_STATUS_INVALID_PARAMETER;