s4:operational LDB module - fix warnings (missing parameters, unused variable)
[Samba/ekacnet.git] / source4 / libnet / libnet_join.c
blob289756c76fe9800fad817fea3b603769a48e7af8
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 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/>.
22 #include "includes.h"
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"
29 #include "ldb_wrap.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"
36 #include "param/provision.h"
39 * complete a domain join, when joining to a AD domain:
40 * 1.) connect and bind to the DRSUAPI pipe
41 * 2.) do a DsCrackNames() to find the machine account dn
42 * 3.) connect to LDAP
43 * 4.) do an ldap search to find the "msDS-KeyVersionNumber" of the machine account
44 * 5.) set the servicePrincipalName's of the machine account via LDAP, (maybe we should use DsWriteAccountSpn()...)
45 * 6.) do a DsCrackNames() to find the domain dn
46 * 7.) find out Site specific stuff, look at libnet_JoinSite() for details
48 static NTSTATUS libnet_JoinADSDomain(struct libnet_context *ctx, struct libnet_JoinDomain *r)
50 NTSTATUS status;
52 TALLOC_CTX *tmp_ctx;
54 const char *realm = r->out.realm;
56 struct dcerpc_binding *samr_binding = r->out.samr_binding;
58 struct dcerpc_pipe *drsuapi_pipe;
59 struct dcerpc_binding *drsuapi_binding;
60 struct drsuapi_DsBind r_drsuapi_bind;
61 struct drsuapi_DsCrackNames r_crack_names;
62 struct drsuapi_DsNameString names[1];
63 struct policy_handle drsuapi_bind_handle;
64 struct GUID drsuapi_bind_guid;
66 struct ldb_context *remote_ldb;
67 struct ldb_dn *account_dn;
68 const char *account_dn_str;
69 const char *remote_ldb_url;
70 struct ldb_result *res;
71 struct ldb_message *msg;
73 int ret, rtn;
75 const char * const attrs[] = {
76 "msDS-KeyVersionNumber",
77 "servicePrincipalName",
78 "dNSHostName",
79 "objectGUID",
80 NULL,
83 r->out.error_string = NULL;
85 /* We need to convert between a samAccountName and domain to a
86 * DN in the directory. The correct way to do this is with
87 * DRSUAPI CrackNames */
89 /* Fiddle with the bindings, so get to DRSUAPI on
90 * NCACN_IP_TCP, sealed */
91 tmp_ctx = talloc_named(r, 0, "libnet_JoinADSDomain temp context");
92 if (!tmp_ctx) {
93 r->out.error_string = NULL;
94 return NT_STATUS_NO_MEMORY;
97 drsuapi_binding = talloc(tmp_ctx, struct dcerpc_binding);
98 if (!drsuapi_binding) {
99 r->out.error_string = NULL;
100 talloc_free(tmp_ctx);
101 return NT_STATUS_NO_MEMORY;
104 *drsuapi_binding = *samr_binding;
106 /* DRSUAPI is only available on IP_TCP, and locally on NCALRPC */
107 if (drsuapi_binding->transport != NCALRPC) {
108 drsuapi_binding->transport = NCACN_IP_TCP;
110 drsuapi_binding->endpoint = NULL;
111 drsuapi_binding->flags |= DCERPC_SEAL;
113 status = dcerpc_pipe_connect_b(tmp_ctx,
114 &drsuapi_pipe,
115 drsuapi_binding,
116 &ndr_table_drsuapi,
117 ctx->cred,
118 ctx->event_ctx,
119 ctx->lp_ctx);
120 if (!NT_STATUS_IS_OK(status)) {
121 r->out.error_string = talloc_asprintf(r,
122 "Connection to DRSUAPI pipe of PDC of domain '%s' failed: %s",
123 r->out.domain_name,
124 nt_errstr(status));
125 talloc_free(tmp_ctx);
126 return status;
129 /* get a DRSUAPI pipe handle */
130 GUID_from_string(DRSUAPI_DS_BIND_GUID, &drsuapi_bind_guid);
132 r_drsuapi_bind.in.bind_guid = &drsuapi_bind_guid;
133 r_drsuapi_bind.in.bind_info = NULL;
134 r_drsuapi_bind.out.bind_handle = &drsuapi_bind_handle;
136 status = dcerpc_drsuapi_DsBind_r(drsuapi_pipe->binding_handle, tmp_ctx, &r_drsuapi_bind);
137 if (!NT_STATUS_IS_OK(status)) {
138 r->out.error_string
139 = talloc_asprintf(r,
140 "dcerpc_drsuapi_DsBind failed - %s",
141 nt_errstr(status));
142 talloc_free(tmp_ctx);
143 return status;
144 } else if (!W_ERROR_IS_OK(r_drsuapi_bind.out.result)) {
145 r->out.error_string
146 = talloc_asprintf(r,
147 "DsBind failed - %s",
148 win_errstr(r_drsuapi_bind.out.result));
149 talloc_free(tmp_ctx);
150 return NT_STATUS_UNSUCCESSFUL;
153 /* Actually 'crack' the names */
154 ZERO_STRUCT(r_crack_names);
155 r_crack_names.in.bind_handle = &drsuapi_bind_handle;
156 r_crack_names.in.level = 1;
157 r_crack_names.in.req = talloc(r, union drsuapi_DsNameRequest);
158 if (!r_crack_names.in.req) {
159 r->out.error_string = NULL;
160 talloc_free(tmp_ctx);
161 return NT_STATUS_NO_MEMORY;
163 r_crack_names.in.req->req1.codepage = 1252; /* western european */
164 r_crack_names.in.req->req1.language = 0x00000407; /* german */
165 r_crack_names.in.req->req1.count = 1;
166 r_crack_names.in.req->req1.names = names;
167 r_crack_names.in.req->req1.format_flags = DRSUAPI_DS_NAME_FLAG_NO_FLAGS;
168 r_crack_names.in.req->req1.format_offered = DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY;
169 r_crack_names.in.req->req1.format_desired = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
170 names[0].str = dom_sid_string(tmp_ctx, r->out.account_sid);
171 if (!names[0].str) {
172 r->out.error_string = NULL;
173 talloc_free(tmp_ctx);
174 return NT_STATUS_NO_MEMORY;
177 r_crack_names.out.ctr = talloc(r, union drsuapi_DsNameCtr);
178 r_crack_names.out.level_out = talloc(r, uint32_t);
179 if (!r_crack_names.out.ctr || !r_crack_names.out.level_out) {
180 r->out.error_string = NULL;
181 talloc_free(tmp_ctx);
182 return NT_STATUS_NO_MEMORY;
185 status = dcerpc_drsuapi_DsCrackNames_r(drsuapi_pipe->binding_handle, tmp_ctx, &r_crack_names);
186 if (!NT_STATUS_IS_OK(status)) {
187 r->out.error_string
188 = talloc_asprintf(r,
189 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s",
190 names[0].str,
191 nt_errstr(status));
192 talloc_free(tmp_ctx);
193 return status;
194 } else if (!W_ERROR_IS_OK(r_crack_names.out.result)) {
195 r->out.error_string
196 = talloc_asprintf(r,
197 "DsCrackNames failed - %s", win_errstr(r_crack_names.out.result));
198 talloc_free(tmp_ctx);
199 return NT_STATUS_UNSUCCESSFUL;
200 } else if (*r_crack_names.out.level_out != 1
201 || !r_crack_names.out.ctr->ctr1
202 || r_crack_names.out.ctr->ctr1->count != 1) {
203 r->out.error_string = talloc_asprintf(r, "DsCrackNames failed");
204 talloc_free(tmp_ctx);
205 return NT_STATUS_INVALID_PARAMETER;
206 } else if (r_crack_names.out.ctr->ctr1->array[0].status != DRSUAPI_DS_NAME_STATUS_OK) {
207 r->out.error_string = talloc_asprintf(r, "DsCrackNames failed: %d", r_crack_names.out.ctr->ctr1->array[0].status);
208 talloc_free(tmp_ctx);
209 return NT_STATUS_UNSUCCESSFUL;
210 } else if (r_crack_names.out.ctr->ctr1->array[0].result_name == NULL) {
211 r->out.error_string = talloc_asprintf(r, "DsCrackNames failed: no result name");
212 talloc_free(tmp_ctx);
213 return NT_STATUS_INVALID_PARAMETER;
216 /* Store the DN of our machine account. */
217 account_dn_str = r_crack_names.out.ctr->ctr1->array[0].result_name;
219 /* Now we know the user's DN, open with LDAP, read and modify a few things */
221 remote_ldb_url = talloc_asprintf(tmp_ctx, "ldap://%s",
222 drsuapi_binding->target_hostname);
223 if (!remote_ldb_url) {
224 r->out.error_string = NULL;
225 talloc_free(tmp_ctx);
226 return NT_STATUS_NO_MEMORY;
229 remote_ldb = ldb_wrap_connect(tmp_ctx, ctx->event_ctx, ctx->lp_ctx,
230 remote_ldb_url,
231 NULL, ctx->cred, 0);
232 if (!remote_ldb) {
233 r->out.error_string = NULL;
234 talloc_free(tmp_ctx);
235 return NT_STATUS_UNSUCCESSFUL;
238 account_dn = ldb_dn_new(tmp_ctx, remote_ldb, account_dn_str);
239 if (! ldb_dn_validate(account_dn)) {
240 r->out.error_string = talloc_asprintf(r, "Invalid account dn: %s",
241 account_dn_str);
242 talloc_free(tmp_ctx);
243 return NT_STATUS_UNSUCCESSFUL;
246 /* search for the user's record */
247 ret = ldb_search(remote_ldb, tmp_ctx, &res,
248 account_dn, LDB_SCOPE_BASE, attrs, NULL);
249 if (ret != LDB_SUCCESS) {
250 r->out.error_string = talloc_asprintf(r, "ldb_search for %s failed - %s",
251 account_dn_str, ldb_errstring(remote_ldb));
252 talloc_free(tmp_ctx);
253 return NT_STATUS_UNSUCCESSFUL;
256 if (res->count != 1) {
257 r->out.error_string = talloc_asprintf(r, "ldb_search for %s failed - found %d entries",
258 account_dn_str, res->count);
259 talloc_free(tmp_ctx);
260 return NT_STATUS_UNSUCCESSFUL;
263 /* Prepare a new message, for the modify */
264 msg = ldb_msg_new(tmp_ctx);
265 if (!msg) {
266 r->out.error_string = NULL;
267 talloc_free(tmp_ctx);
268 return NT_STATUS_NO_MEMORY;
270 msg->dn = res->msgs[0]->dn;
273 unsigned int i;
274 const char *service_principal_name[6];
275 const char *dns_host_name = strlower_talloc(tmp_ctx,
276 talloc_asprintf(tmp_ctx,
277 "%s.%s",
278 r->in.netbios_name,
279 realm));
281 if (!dns_host_name) {
282 r->out.error_string = NULL;
283 talloc_free(tmp_ctx);
284 return NT_STATUS_NO_MEMORY;
287 service_principal_name[0] = talloc_asprintf(tmp_ctx, "host/%s", dns_host_name);
288 service_principal_name[1] = talloc_asprintf(tmp_ctx, "host/%s", strlower_talloc(tmp_ctx, r->in.netbios_name));
289 service_principal_name[2] = talloc_asprintf(tmp_ctx, "host/%s/%s", dns_host_name, realm);
290 service_principal_name[3] = talloc_asprintf(tmp_ctx, "host/%s/%s", strlower_talloc(tmp_ctx, r->in.netbios_name), realm);
291 service_principal_name[4] = talloc_asprintf(tmp_ctx, "host/%s/%s", dns_host_name, r->out.domain_name);
292 service_principal_name[5] = talloc_asprintf(tmp_ctx, "host/%s/%s", strlower_talloc(tmp_ctx, r->in.netbios_name), r->out.domain_name);
294 for (i=0; i < ARRAY_SIZE(service_principal_name); i++) {
295 if (!service_principal_name[i]) {
296 r->out.error_string = NULL;
297 talloc_free(tmp_ctx);
298 return NT_STATUS_NO_MEMORY;
300 rtn = samdb_msg_add_string(remote_ldb, tmp_ctx, msg, "servicePrincipalName", service_principal_name[i]);
301 if (rtn == -1) {
302 r->out.error_string = NULL;
303 talloc_free(tmp_ctx);
304 return NT_STATUS_NO_MEMORY;
308 rtn = samdb_msg_add_string(remote_ldb, tmp_ctx, msg, "dNSHostName", dns_host_name);
309 if (rtn == -1) {
310 r->out.error_string = NULL;
311 talloc_free(tmp_ctx);
312 return NT_STATUS_NO_MEMORY;
315 rtn = dsdb_replace(remote_ldb, msg, 0);
316 if (rtn != 0) {
317 r->out.error_string
318 = talloc_asprintf(r,
319 "Failed to replace entries on %s",
320 ldb_dn_get_linearized(msg->dn));
321 talloc_free(tmp_ctx);
322 return NT_STATUS_INTERNAL_DB_CORRUPTION;
326 /* DsCrackNames to find out the DN of the domain. */
327 r_crack_names.in.req->req1.format_offered = DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT;
328 r_crack_names.in.req->req1.format_desired = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
329 names[0].str = talloc_asprintf(tmp_ctx, "%s\\", r->out.domain_name);
330 if (!names[0].str) {
331 r->out.error_string = NULL;
332 talloc_free(tmp_ctx);
333 return NT_STATUS_NO_MEMORY;
336 status = dcerpc_drsuapi_DsCrackNames_r(drsuapi_pipe->binding_handle, tmp_ctx, &r_crack_names);
337 if (!NT_STATUS_IS_OK(status)) {
338 r->out.error_string
339 = talloc_asprintf(r,
340 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s",
341 r->in.domain_name,
342 nt_errstr(status));
343 talloc_free(tmp_ctx);
344 return status;
345 } else if (!W_ERROR_IS_OK(r_crack_names.out.result)) {
346 r->out.error_string
347 = talloc_asprintf(r,
348 "DsCrackNames failed - %s", win_errstr(r_crack_names.out.result));
349 talloc_free(tmp_ctx);
350 return NT_STATUS_UNSUCCESSFUL;
351 } else if (*r_crack_names.out.level_out != 1
352 || !r_crack_names.out.ctr->ctr1
353 || r_crack_names.out.ctr->ctr1->count != 1
354 || !r_crack_names.out.ctr->ctr1->array[0].result_name
355 || r_crack_names.out.ctr->ctr1->array[0].status != DRSUAPI_DS_NAME_STATUS_OK) {
356 r->out.error_string = talloc_asprintf(r, "DsCrackNames failed");
357 talloc_free(tmp_ctx);
358 return NT_STATUS_UNSUCCESSFUL;
361 /* Store the account DN. */
362 r->out.account_dn_str = account_dn_str;
363 talloc_steal(r, account_dn_str);
365 /* Store the domain DN. */
366 r->out.domain_dn_str = r_crack_names.out.ctr->ctr1->array[0].result_name;
367 talloc_steal(r, r_crack_names.out.ctr->ctr1->array[0].result_name);
369 /* Store the KVNO of the account, critical for some kerberos
370 * operations */
371 r->out.kvno = ldb_msg_find_attr_as_uint(res->msgs[0], "msDS-KeyVersionNumber", 0);
373 /* Store the account GUID. */
374 r->out.account_guid = samdb_result_guid(res->msgs[0], "objectGUID");
376 if (r->in.acct_type == ACB_SVRTRUST) {
377 status = libnet_JoinSite(ctx, remote_ldb, r);
379 talloc_free(tmp_ctx);
381 return status;
385 * do a domain join using DCERPC/SAMR calls
386 * - connect to the LSA pipe, to try and find out information about the domain
387 * - create a secondary connection to SAMR pipe
388 * - do a samr_Connect to get a policy handle
389 * - do a samr_LookupDomain to get the domain sid
390 * - do a samr_OpenDomain to get a domain handle
391 * - do a samr_CreateAccount to try and get a new account
393 * If that fails, do:
394 * - do a samr_LookupNames to get the users rid
395 * - do a samr_OpenUser to get a user handle
396 * - potentially delete and recreate the user
397 * - assert the account is of the right type with samrQueryUserInfo
399 * - call libnet_SetPassword_samr_handle to set the password,
400 * and pass a samr_UserInfo21 struct to set full_name and the account flags
402 * - do some ADS specific things when we join as Domain Controller,
403 * look at libnet_joinADSDomain() for the details
405 NTSTATUS libnet_JoinDomain(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_JoinDomain *r)
407 TALLOC_CTX *tmp_ctx;
409 NTSTATUS status, cu_status;
411 struct libnet_RpcConnect *connect_with_info;
412 struct dcerpc_pipe *samr_pipe;
414 struct samr_Connect sc;
415 struct policy_handle p_handle;
416 struct samr_OpenDomain od;
417 struct policy_handle d_handle;
418 struct samr_LookupNames ln;
419 struct samr_Ids rids, types;
420 struct samr_OpenUser ou;
421 struct samr_CreateUser2 cu;
422 struct policy_handle *u_handle = NULL;
423 struct samr_QueryUserInfo qui;
424 union samr_UserInfo *uinfo;
425 struct samr_UserInfo21 u_info21;
426 union libnet_SetPassword r2;
427 struct samr_GetUserPwInfo pwp;
428 struct samr_PwInfo info;
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_zero(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 SAMR 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 /* This level makes a connection to the LSA pipe on the way,
471 * to get some useful bits of information about the domain */
472 connect_with_info->level = LIBNET_RPC_CONNECT_DC_INFO;
473 connect_with_info->in.dcerpc_iface = &ndr_table_samr;
476 establish the SAMR connection
478 status = libnet_RpcConnect(ctx, tmp_ctx, connect_with_info);
479 if (!NT_STATUS_IS_OK(status)) {
480 if (r->in.binding) {
481 r->out.error_string = talloc_asprintf(mem_ctx,
482 "Connection to SAMR pipe of DC %s failed: %s",
483 r->in.binding, connect_with_info->out.error_string);
484 } else {
485 r->out.error_string = talloc_asprintf(mem_ctx,
486 "Connection to SAMR pipe of PDC for %s failed: %s",
487 r->in.domain_name, connect_with_info->out.error_string);
489 talloc_free(tmp_ctx);
490 return status;
493 samr_pipe = connect_with_info->out.dcerpc_pipe;
495 status = dcerpc_pipe_auth(tmp_ctx, &samr_pipe,
496 connect_with_info->out.dcerpc_pipe->binding,
497 &ndr_table_samr, ctx->cred, ctx->lp_ctx);
498 if (!NT_STATUS_IS_OK(status)) {
499 r->out.error_string = talloc_asprintf(mem_ctx,
500 "SAMR bind failed: %s",
501 nt_errstr(status));
502 talloc_free(tmp_ctx);
503 return status;
506 /* prepare samr_Connect */
507 ZERO_STRUCT(p_handle);
508 sc.in.system_name = NULL;
509 sc.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
510 sc.out.connect_handle = &p_handle;
512 /* 2. do a samr_Connect to get a policy handle */
513 status = dcerpc_samr_Connect_r(samr_pipe->binding_handle, tmp_ctx, &sc);
514 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(sc.out.result)) {
515 status = sc.out.result;
517 if (!NT_STATUS_IS_OK(status)) {
518 r->out.error_string = talloc_asprintf(mem_ctx,
519 "samr_Connect failed: %s",
520 nt_errstr(status));
521 talloc_free(tmp_ctx);
522 return status;
525 /* If this is a connection on ncacn_ip_tcp to Win2k3 SP1, we don't get back this useful info */
526 if (!connect_with_info->out.domain_name) {
527 if (r->in.level == LIBNET_JOINDOMAIN_AUTOMATIC) {
528 connect_with_info->out.domain_name = talloc_strdup(tmp_ctx, r->in.domain_name);
529 } else {
530 /* Bugger, we just lost our way to automatically find the domain name */
531 connect_with_info->out.domain_name = talloc_strdup(tmp_ctx, lp_workgroup(ctx->lp_ctx));
532 connect_with_info->out.realm = talloc_strdup(tmp_ctx, lp_realm(ctx->lp_ctx));
536 /* Perhaps we didn't get a SID above, because we are against ncacn_ip_tcp */
537 if (!connect_with_info->out.domain_sid) {
538 struct lsa_String name;
539 struct samr_LookupDomain l;
540 struct dom_sid2 *sid = NULL;
541 name.string = connect_with_info->out.domain_name;
542 l.in.connect_handle = &p_handle;
543 l.in.domain_name = &name;
544 l.out.sid = &sid;
546 status = dcerpc_samr_LookupDomain_r(samr_pipe->binding_handle, tmp_ctx, &l);
547 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(l.out.result)) {
548 status = l.out.result;
550 if (!NT_STATUS_IS_OK(status)) {
551 r->out.error_string = talloc_asprintf(mem_ctx,
552 "SAMR LookupDomain failed: %s",
553 nt_errstr(status));
554 talloc_free(tmp_ctx);
555 return status;
557 connect_with_info->out.domain_sid = *l.out.sid;
560 /* prepare samr_OpenDomain */
561 ZERO_STRUCT(d_handle);
562 od.in.connect_handle = &p_handle;
563 od.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
564 od.in.sid = connect_with_info->out.domain_sid;
565 od.out.domain_handle = &d_handle;
567 /* do a samr_OpenDomain to get a domain handle */
568 status = dcerpc_samr_OpenDomain_r(samr_pipe->binding_handle, tmp_ctx, &od);
569 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(od.out.result)) {
570 status = od.out.result;
572 if (!NT_STATUS_IS_OK(status)) {
573 r->out.error_string = talloc_asprintf(mem_ctx,
574 "samr_OpenDomain for [%s] failed: %s",
575 dom_sid_string(tmp_ctx, connect_with_info->out.domain_sid),
576 nt_errstr(status));
577 talloc_free(tmp_ctx);
578 return status;
581 /* prepare samr_CreateUser2 */
582 ZERO_STRUCTP(u_handle);
583 cu.in.domain_handle = &d_handle;
584 cu.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
585 samr_account_name.string = r->in.account_name;
586 cu.in.account_name = &samr_account_name;
587 cu.in.acct_flags = r->in.acct_type;
588 cu.out.user_handle = u_handle;
589 cu.out.rid = &rid;
590 cu.out.access_granted = &access_granted;
592 /* do a samr_CreateUser2 to get an account handle, or an error */
593 cu_status = dcerpc_samr_CreateUser2_r(samr_pipe->binding_handle, tmp_ctx, &cu);
594 if (NT_STATUS_IS_OK(cu_status) && !NT_STATUS_IS_OK(cu.out.result)) {
595 cu_status = cu.out.result;
597 status = cu_status;
598 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
599 /* prepare samr_LookupNames */
600 ln.in.domain_handle = &d_handle;
601 ln.in.num_names = 1;
602 ln.in.names = talloc_array(tmp_ctx, struct lsa_String, 1);
603 ln.out.rids = &rids;
604 ln.out.types = &types;
605 if (!ln.in.names) {
606 r->out.error_string = NULL;
607 talloc_free(tmp_ctx);
608 return NT_STATUS_NO_MEMORY;
610 ln.in.names[0].string = r->in.account_name;
612 /* 5. do a samr_LookupNames to get the users rid */
613 status = dcerpc_samr_LookupNames_r(samr_pipe->binding_handle, tmp_ctx, &ln);
614 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(ln.out.result)) {
615 status = ln.out.result;
617 if (!NT_STATUS_IS_OK(status)) {
618 r->out.error_string = talloc_asprintf(mem_ctx,
619 "samr_LookupNames for [%s] failed: %s",
620 r->in.account_name,
621 nt_errstr(status));
622 talloc_free(tmp_ctx);
623 return status;
626 /* check if we got one RID for the user */
627 if (ln.out.rids->count != 1) {
628 r->out.error_string = talloc_asprintf(mem_ctx,
629 "samr_LookupNames for [%s] returns %d RIDs",
630 r->in.account_name, ln.out.rids->count);
631 talloc_free(tmp_ctx);
632 return NT_STATUS_INVALID_PARAMETER;
635 /* prepare samr_OpenUser */
636 ZERO_STRUCTP(u_handle);
637 ou.in.domain_handle = &d_handle;
638 ou.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
639 ou.in.rid = ln.out.rids->ids[0];
640 rid = ou.in.rid;
641 ou.out.user_handle = u_handle;
643 /* 6. do a samr_OpenUser to get a user handle */
644 status = dcerpc_samr_OpenUser_r(samr_pipe->binding_handle, tmp_ctx, &ou);
645 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(ou.out.result)) {
646 status = ou.out.result;
648 if (!NT_STATUS_IS_OK(status)) {
649 r->out.error_string = talloc_asprintf(mem_ctx,
650 "samr_OpenUser for [%s] failed: %s",
651 r->in.account_name,
652 nt_errstr(status));
653 talloc_free(tmp_ctx);
654 return status;
657 if (r->in.recreate_account) {
658 struct samr_DeleteUser d;
659 d.in.user_handle = u_handle;
660 d.out.user_handle = u_handle;
661 status = dcerpc_samr_DeleteUser_r(samr_pipe->binding_handle, mem_ctx, &d);
662 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(d.out.result)) {
663 status = d.out.result;
665 if (!NT_STATUS_IS_OK(status)) {
666 r->out.error_string = talloc_asprintf(mem_ctx,
667 "samr_DeleteUser (for recreate) of [%s] failed: %s",
668 r->in.account_name,
669 nt_errstr(status));
670 talloc_free(tmp_ctx);
671 return status;
674 /* We want to recreate, so delete and another samr_CreateUser2 */
676 /* &cu filled in above */
677 status = dcerpc_samr_CreateUser2_r(samr_pipe->binding_handle, tmp_ctx, &cu);
678 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(cu.out.result)) {
679 status = cu.out.result;
681 if (!NT_STATUS_IS_OK(status)) {
682 r->out.error_string = talloc_asprintf(mem_ctx,
683 "samr_CreateUser2 (recreate) for [%s] failed: %s",
684 r->in.account_name, nt_errstr(status));
685 talloc_free(tmp_ctx);
686 return status;
689 } else if (!NT_STATUS_IS_OK(status)) {
690 r->out.error_string = talloc_asprintf(mem_ctx,
691 "samr_CreateUser2 for [%s] failed: %s",
692 r->in.account_name, nt_errstr(status));
693 talloc_free(tmp_ctx);
694 return status;
697 /* prepare samr_QueryUserInfo (get flags) */
698 qui.in.user_handle = u_handle;
699 qui.in.level = 16;
700 qui.out.info = &uinfo;
702 status = dcerpc_samr_QueryUserInfo_r(samr_pipe->binding_handle, tmp_ctx, &qui);
703 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(qui.out.result)) {
704 status = qui.out.result;
706 if (!NT_STATUS_IS_OK(status)) {
707 r->out.error_string = talloc_asprintf(mem_ctx,
708 "samr_QueryUserInfo for [%s] failed: %s",
709 r->in.account_name,
710 nt_errstr(status));
711 talloc_free(tmp_ctx);
712 return status;
715 if (!uinfo) {
716 status = NT_STATUS_INVALID_PARAMETER;
717 r->out.error_string
718 = talloc_asprintf(mem_ctx,
719 "samr_QueryUserInfo failed to return qui.out.info for [%s]: %s",
720 r->in.account_name, nt_errstr(status));
721 talloc_free(tmp_ctx);
722 return status;
725 old_acct_flags = (uinfo->info16.acct_flags & (ACB_WSTRUST | ACB_SVRTRUST | ACB_DOMTRUST));
726 /* Possibly bail if the account is of the wrong type */
727 if (old_acct_flags
728 != r->in.acct_type) {
729 const char *old_account_type, *new_account_type;
730 switch (old_acct_flags) {
731 case ACB_WSTRUST:
732 old_account_type = "domain member (member)";
733 break;
734 case ACB_SVRTRUST:
735 old_account_type = "domain controller (bdc)";
736 break;
737 case ACB_DOMTRUST:
738 old_account_type = "trusted domain";
739 break;
740 default:
741 return NT_STATUS_INVALID_PARAMETER;
743 switch (r->in.acct_type) {
744 case ACB_WSTRUST:
745 new_account_type = "domain member (member)";
746 break;
747 case ACB_SVRTRUST:
748 new_account_type = "domain controller (bdc)";
749 break;
750 case ACB_DOMTRUST:
751 new_account_type = "trusted domain";
752 break;
753 default:
754 return NT_STATUS_INVALID_PARAMETER;
757 if (!NT_STATUS_EQUAL(cu_status, NT_STATUS_USER_EXISTS)) {
758 /* We created a new user, but they didn't come out the right type?!? */
759 r->out.error_string
760 = talloc_asprintf(mem_ctx,
761 "We asked to create a new machine account (%s) of type %s, "
762 "but we got an account of type %s. This is unexpected. "
763 "Perhaps delete the account and try again.",
764 r->in.account_name, new_account_type, old_account_type);
765 talloc_free(tmp_ctx);
766 return NT_STATUS_INVALID_PARAMETER;
767 } else {
768 /* The account is of the wrong type, so bail */
770 /* TODO: We should allow a --force option to override, and redo this from the top setting r.in.recreate_account */
771 r->out.error_string
772 = talloc_asprintf(mem_ctx,
773 "The machine account (%s) already exists in the domain %s, "
774 "but is a %s. You asked to join as a %s. Please delete "
775 "the account and try again.",
776 r->in.account_name, connect_with_info->out.domain_name, old_account_type, new_account_type);
777 talloc_free(tmp_ctx);
778 return NT_STATUS_USER_EXISTS;
780 } else {
781 acct_flags = uinfo->info16.acct_flags;
784 acct_flags = (acct_flags & ~(ACB_DISABLED|ACB_PWNOTREQ));
786 /* Find out what password policy this user has */
787 pwp.in.user_handle = u_handle;
788 pwp.out.info = &info;
790 status = dcerpc_samr_GetUserPwInfo_r(samr_pipe->binding_handle, tmp_ctx, &pwp);
791 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(pwp.out.result)) {
792 status = pwp.out.result;
794 if (NT_STATUS_IS_OK(status)) {
795 policy_min_pw_len = pwp.out.info->min_password_length;
798 /* Grab a password of that minimum length */
800 password_str = generate_random_password(tmp_ctx, MAX(8, policy_min_pw_len), 255);
802 /* set full_name and reset flags */
803 ZERO_STRUCT(u_info21);
804 u_info21.full_name.string = r->in.account_name;
805 u_info21.acct_flags = acct_flags;
806 u_info21.fields_present = SAMR_FIELD_FULL_NAME | SAMR_FIELD_ACCT_FLAGS;
808 r2.samr_handle.level = LIBNET_SET_PASSWORD_SAMR_HANDLE;
809 r2.samr_handle.in.account_name = r->in.account_name;
810 r2.samr_handle.in.newpassword = password_str;
811 r2.samr_handle.in.user_handle = u_handle;
812 r2.samr_handle.in.dcerpc_pipe = samr_pipe;
813 r2.samr_handle.in.info21 = &u_info21;
815 status = libnet_SetPassword(ctx, tmp_ctx, &r2);
816 if (!NT_STATUS_IS_OK(status)) {
817 r->out.error_string = talloc_steal(mem_ctx, r2.samr_handle.out.error_string);
818 talloc_free(tmp_ctx);
819 return status;
822 account_sid = dom_sid_add_rid(mem_ctx, connect_with_info->out.domain_sid, rid);
823 if (!account_sid) {
824 r->out.error_string = NULL;
825 talloc_free(tmp_ctx);
826 return NT_STATUS_NO_MEMORY;
829 /* Finish out by pushing various bits of status data out for the caller to use */
830 r->out.join_password = password_str;
831 talloc_steal(mem_ctx, r->out.join_password);
833 r->out.domain_sid = connect_with_info->out.domain_sid;
834 talloc_steal(mem_ctx, r->out.domain_sid);
836 r->out.account_sid = account_sid;
837 talloc_steal(mem_ctx, r->out.account_sid);
839 r->out.domain_name = connect_with_info->out.domain_name;
840 talloc_steal(mem_ctx, r->out.domain_name);
841 r->out.realm = connect_with_info->out.realm;
842 talloc_steal(mem_ctx, r->out.realm);
843 r->out.samr_pipe = samr_pipe;
844 talloc_reparent(tmp_ctx, mem_ctx, samr_pipe);
845 r->out.samr_binding = samr_pipe->binding;
846 talloc_steal(mem_ctx, r->out.samr_binding);
847 r->out.user_handle = u_handle;
848 talloc_steal(mem_ctx, u_handle);
849 r->out.error_string = r2.samr_handle.out.error_string;
850 talloc_steal(mem_ctx, r2.samr_handle.out.error_string);
851 r->out.kvno = 0;
852 r->out.server_dn_str = NULL;
853 talloc_free(tmp_ctx);
855 /* Now, if it was AD, then we want to start looking changing a
856 * few more things. Otherwise, we are done. */
857 if (r->out.realm) {
858 status = libnet_JoinADSDomain(ctx, r);
859 return status;
862 return status;
865 static NTSTATUS libnet_Join_primary_domain(struct libnet_context *ctx,
866 TALLOC_CTX *mem_ctx,
867 struct libnet_Join *r)
869 NTSTATUS status;
870 TALLOC_CTX *tmp_mem;
871 struct libnet_JoinDomain *r2;
872 struct provision_store_self_join_settings *set_secrets;
873 uint32_t acct_type = 0;
874 const char *account_name;
875 const char *netbios_name;
876 const char *error_string;
878 r->out.error_string = NULL;
880 tmp_mem = talloc_new(mem_ctx);
881 if (!tmp_mem) {
882 return NT_STATUS_NO_MEMORY;
885 r2 = talloc(tmp_mem, struct libnet_JoinDomain);
886 if (!r2) {
887 r->out.error_string = NULL;
888 talloc_free(tmp_mem);
889 return NT_STATUS_NO_MEMORY;
892 if (r->in.join_type == SEC_CHAN_BDC) {
893 acct_type = ACB_SVRTRUST;
894 } else if (r->in.join_type == SEC_CHAN_WKSTA) {
895 acct_type = ACB_WSTRUST;
896 } else {
897 r->out.error_string = NULL;
898 talloc_free(tmp_mem);
899 return NT_STATUS_INVALID_PARAMETER;
902 if (r->in.netbios_name != NULL) {
903 netbios_name = r->in.netbios_name;
904 } else {
905 netbios_name = talloc_strdup(tmp_mem, lp_netbios_name(ctx->lp_ctx));
906 if (!netbios_name) {
907 r->out.error_string = NULL;
908 talloc_free(tmp_mem);
909 return NT_STATUS_NO_MEMORY;
913 account_name = talloc_asprintf(tmp_mem, "%s$", netbios_name);
914 if (!account_name) {
915 r->out.error_string = NULL;
916 talloc_free(tmp_mem);
917 return NT_STATUS_NO_MEMORY;
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;
937 set_secrets = talloc(tmp_mem, struct provision_store_self_join_settings);
938 if (!set_secrets) {
939 r->out.error_string = NULL;
940 talloc_free(tmp_mem);
941 return NT_STATUS_NO_MEMORY;
944 ZERO_STRUCTP(set_secrets);
945 set_secrets->domain_name = r2->out.domain_name;
946 set_secrets->realm = r2->out.realm;
947 set_secrets->netbios_name = netbios_name;
948 set_secrets->secure_channel_type = r->in.join_type;
949 set_secrets->machine_password = r2->out.join_password;
950 set_secrets->key_version_number = r2->out.kvno;
951 set_secrets->domain_sid = r2->out.domain_sid;
953 status = provision_store_self_join(ctx, ctx->lp_ctx, ctx->event_ctx, set_secrets, &error_string);
954 if (!NT_STATUS_IS_OK(status)) {
955 r->out.error_string = talloc_steal(mem_ctx, error_string);
956 talloc_free(tmp_mem);
957 return status;
960 /* move all out parameter to the callers TALLOC_CTX */
961 r->out.error_string = NULL;
962 r->out.join_password = r2->out.join_password;
963 talloc_reparent(r2, mem_ctx, r2->out.join_password);
964 r->out.domain_sid = r2->out.domain_sid;
965 talloc_reparent(r2, mem_ctx, r2->out.domain_sid);
966 r->out.domain_name = r2->out.domain_name;
967 talloc_reparent(r2, mem_ctx, r2->out.domain_name);
968 talloc_free(tmp_mem);
969 return NT_STATUS_OK;
972 NTSTATUS libnet_Join(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_Join *r)
974 switch (r->in.join_type) {
975 case SEC_CHAN_WKSTA:
976 return libnet_Join_primary_domain(ctx, mem_ctx, r);
977 case SEC_CHAN_BDC:
978 return libnet_Join_primary_domain(ctx, mem_ctx, r);
979 case SEC_CHAN_DOMAIN:
980 case SEC_CHAN_DNS_DOMAIN:
981 case SEC_CHAN_NULL:
982 break;
985 r->out.error_string = talloc_asprintf(mem_ctx,
986 "Invalid join type specified (%08X) attempting to join domain %s",
987 r->in.join_type, r->in.domain_name);
988 return NT_STATUS_INVALID_PARAMETER;