s4:tortore/rpc/spoolss: some compilers don't like .foo.bar = 5
[Samba/gebeck_regimport.git] / source4 / libnet / libnet_join.c
blobf5cbda083e4c8dc71f79ff28300306ca08366247
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 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
139 r->out.error_string
140 = talloc_asprintf(r,
141 "dcerpc_drsuapi_DsBind failed - %s",
142 dcerpc_errstr(tmp_ctx, drsuapi_pipe->last_fault_code));
143 talloc_free(tmp_ctx);
144 return status;
145 } else {
146 r->out.error_string
147 = talloc_asprintf(r,
148 "dcerpc_drsuapi_DsBind failed - %s",
149 nt_errstr(status));
150 talloc_free(tmp_ctx);
151 return status;
153 } else if (!W_ERROR_IS_OK(r_drsuapi_bind.out.result)) {
154 r->out.error_string
155 = talloc_asprintf(r,
156 "DsBind failed - %s",
157 win_errstr(r_drsuapi_bind.out.result));
158 talloc_free(tmp_ctx);
159 return NT_STATUS_UNSUCCESSFUL;
162 /* Actually 'crack' the names */
163 ZERO_STRUCT(r_crack_names);
164 r_crack_names.in.bind_handle = &drsuapi_bind_handle;
165 r_crack_names.in.level = 1;
166 r_crack_names.in.req = talloc(r, union drsuapi_DsNameRequest);
167 if (!r_crack_names.in.req) {
168 r->out.error_string = NULL;
169 talloc_free(tmp_ctx);
170 return NT_STATUS_NO_MEMORY;
172 r_crack_names.in.req->req1.codepage = 1252; /* western european */
173 r_crack_names.in.req->req1.language = 0x00000407; /* german */
174 r_crack_names.in.req->req1.count = 1;
175 r_crack_names.in.req->req1.names = names;
176 r_crack_names.in.req->req1.format_flags = DRSUAPI_DS_NAME_FLAG_NO_FLAGS;
177 r_crack_names.in.req->req1.format_offered = DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY;
178 r_crack_names.in.req->req1.format_desired = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
179 names[0].str = dom_sid_string(tmp_ctx, r->out.account_sid);
180 if (!names[0].str) {
181 r->out.error_string = NULL;
182 talloc_free(tmp_ctx);
183 return NT_STATUS_NO_MEMORY;
186 r_crack_names.out.ctr = talloc(r, union drsuapi_DsNameCtr);
187 r_crack_names.out.level_out = talloc(r, uint32_t);
188 if (!r_crack_names.out.ctr || !r_crack_names.out.level_out) {
189 r->out.error_string = NULL;
190 talloc_free(tmp_ctx);
191 return NT_STATUS_NO_MEMORY;
194 status = dcerpc_drsuapi_DsCrackNames_r(drsuapi_pipe->binding_handle, tmp_ctx, &r_crack_names);
195 if (!NT_STATUS_IS_OK(status)) {
196 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
197 r->out.error_string
198 = talloc_asprintf(r,
199 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s",
200 names[0].str,
201 dcerpc_errstr(tmp_ctx, drsuapi_pipe->last_fault_code));
202 talloc_free(tmp_ctx);
203 return status;
204 } else {
205 r->out.error_string
206 = talloc_asprintf(r,
207 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s",
208 names[0].str,
209 nt_errstr(status));
210 talloc_free(tmp_ctx);
211 return status;
213 } else if (!W_ERROR_IS_OK(r_crack_names.out.result)) {
214 r->out.error_string
215 = talloc_asprintf(r,
216 "DsCrackNames failed - %s", win_errstr(r_crack_names.out.result));
217 talloc_free(tmp_ctx);
218 return NT_STATUS_UNSUCCESSFUL;
219 } else if (*r_crack_names.out.level_out != 1
220 || !r_crack_names.out.ctr->ctr1
221 || r_crack_names.out.ctr->ctr1->count != 1) {
222 r->out.error_string = talloc_asprintf(r, "DsCrackNames failed");
223 talloc_free(tmp_ctx);
224 return NT_STATUS_INVALID_PARAMETER;
225 } else if (r_crack_names.out.ctr->ctr1->array[0].status != DRSUAPI_DS_NAME_STATUS_OK) {
226 r->out.error_string = talloc_asprintf(r, "DsCrackNames failed: %d", r_crack_names.out.ctr->ctr1->array[0].status);
227 talloc_free(tmp_ctx);
228 return NT_STATUS_UNSUCCESSFUL;
229 } else if (r_crack_names.out.ctr->ctr1->array[0].result_name == NULL) {
230 r->out.error_string = talloc_asprintf(r, "DsCrackNames failed: no result name");
231 talloc_free(tmp_ctx);
232 return NT_STATUS_INVALID_PARAMETER;
235 /* Store the DN of our machine account. */
236 account_dn_str = r_crack_names.out.ctr->ctr1->array[0].result_name;
238 /* Now we know the user's DN, open with LDAP, read and modify a few things */
240 remote_ldb_url = talloc_asprintf(tmp_ctx, "ldap://%s",
241 drsuapi_binding->target_hostname);
242 if (!remote_ldb_url) {
243 r->out.error_string = NULL;
244 talloc_free(tmp_ctx);
245 return NT_STATUS_NO_MEMORY;
248 remote_ldb = ldb_wrap_connect(tmp_ctx, ctx->event_ctx, ctx->lp_ctx,
249 remote_ldb_url,
250 NULL, ctx->cred, 0);
251 if (!remote_ldb) {
252 r->out.error_string = NULL;
253 talloc_free(tmp_ctx);
254 return NT_STATUS_UNSUCCESSFUL;
257 account_dn = ldb_dn_new(tmp_ctx, remote_ldb, account_dn_str);
258 if (! ldb_dn_validate(account_dn)) {
259 r->out.error_string = talloc_asprintf(r, "Invalid account dn: %s",
260 account_dn_str);
261 talloc_free(tmp_ctx);
262 return NT_STATUS_UNSUCCESSFUL;
265 /* search for the user's record */
266 ret = ldb_search(remote_ldb, tmp_ctx, &res,
267 account_dn, LDB_SCOPE_BASE, attrs, NULL);
268 if (ret != LDB_SUCCESS) {
269 r->out.error_string = talloc_asprintf(r, "ldb_search for %s failed - %s",
270 account_dn_str, ldb_errstring(remote_ldb));
271 talloc_free(tmp_ctx);
272 return NT_STATUS_UNSUCCESSFUL;
275 if (res->count != 1) {
276 r->out.error_string = talloc_asprintf(r, "ldb_search for %s failed - found %d entries",
277 account_dn_str, res->count);
278 talloc_free(tmp_ctx);
279 return NT_STATUS_UNSUCCESSFUL;
282 /* Prepare a new message, for the modify */
283 msg = ldb_msg_new(tmp_ctx);
284 if (!msg) {
285 r->out.error_string = NULL;
286 talloc_free(tmp_ctx);
287 return NT_STATUS_NO_MEMORY;
289 msg->dn = res->msgs[0]->dn;
292 unsigned int i;
293 const char *service_principal_name[6];
294 const char *dns_host_name = strlower_talloc(tmp_ctx,
295 talloc_asprintf(tmp_ctx,
296 "%s.%s",
297 r->in.netbios_name,
298 realm));
300 if (!dns_host_name) {
301 r->out.error_string = NULL;
302 talloc_free(tmp_ctx);
303 return NT_STATUS_NO_MEMORY;
306 service_principal_name[0] = talloc_asprintf(tmp_ctx, "host/%s", dns_host_name);
307 service_principal_name[1] = talloc_asprintf(tmp_ctx, "host/%s", strlower_talloc(tmp_ctx, r->in.netbios_name));
308 service_principal_name[2] = talloc_asprintf(tmp_ctx, "host/%s/%s", dns_host_name, realm);
309 service_principal_name[3] = talloc_asprintf(tmp_ctx, "host/%s/%s", strlower_talloc(tmp_ctx, r->in.netbios_name), realm);
310 service_principal_name[4] = talloc_asprintf(tmp_ctx, "host/%s/%s", dns_host_name, r->out.domain_name);
311 service_principal_name[5] = talloc_asprintf(tmp_ctx, "host/%s/%s", strlower_talloc(tmp_ctx, r->in.netbios_name), r->out.domain_name);
313 for (i=0; i < ARRAY_SIZE(service_principal_name); i++) {
314 if (!service_principal_name[i]) {
315 r->out.error_string = NULL;
316 talloc_free(tmp_ctx);
317 return NT_STATUS_NO_MEMORY;
319 rtn = samdb_msg_add_string(remote_ldb, tmp_ctx, msg, "servicePrincipalName", service_principal_name[i]);
320 if (rtn == -1) {
321 r->out.error_string = NULL;
322 talloc_free(tmp_ctx);
323 return NT_STATUS_NO_MEMORY;
327 rtn = samdb_msg_add_string(remote_ldb, tmp_ctx, msg, "dNSHostName", dns_host_name);
328 if (rtn == -1) {
329 r->out.error_string = NULL;
330 talloc_free(tmp_ctx);
331 return NT_STATUS_NO_MEMORY;
334 rtn = dsdb_replace(remote_ldb, msg, 0);
335 if (rtn != 0) {
336 r->out.error_string
337 = talloc_asprintf(r,
338 "Failed to replace entries on %s",
339 ldb_dn_get_linearized(msg->dn));
340 talloc_free(tmp_ctx);
341 return NT_STATUS_INTERNAL_DB_CORRUPTION;
345 /* DsCrackNames to find out the DN of the domain. */
346 r_crack_names.in.req->req1.format_offered = DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT;
347 r_crack_names.in.req->req1.format_desired = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
348 names[0].str = talloc_asprintf(tmp_ctx, "%s\\", r->out.domain_name);
349 if (!names[0].str) {
350 r->out.error_string = NULL;
351 talloc_free(tmp_ctx);
352 return NT_STATUS_NO_MEMORY;
355 status = dcerpc_drsuapi_DsCrackNames_r(drsuapi_pipe->binding_handle, tmp_ctx, &r_crack_names);
356 if (!NT_STATUS_IS_OK(status)) {
357 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
358 r->out.error_string
359 = talloc_asprintf(r,
360 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s",
361 r->in.domain_name,
362 dcerpc_errstr(tmp_ctx, drsuapi_pipe->last_fault_code));
363 talloc_free(tmp_ctx);
364 return status;
365 } else {
366 r->out.error_string
367 = talloc_asprintf(r,
368 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s",
369 r->in.domain_name,
370 nt_errstr(status));
371 talloc_free(tmp_ctx);
372 return status;
374 } else if (!W_ERROR_IS_OK(r_crack_names.out.result)) {
375 r->out.error_string
376 = talloc_asprintf(r,
377 "DsCrackNames failed - %s", win_errstr(r_crack_names.out.result));
378 talloc_free(tmp_ctx);
379 return NT_STATUS_UNSUCCESSFUL;
380 } else if (*r_crack_names.out.level_out != 1
381 || !r_crack_names.out.ctr->ctr1
382 || r_crack_names.out.ctr->ctr1->count != 1
383 || !r_crack_names.out.ctr->ctr1->array[0].result_name
384 || r_crack_names.out.ctr->ctr1->array[0].status != DRSUAPI_DS_NAME_STATUS_OK) {
385 r->out.error_string = talloc_asprintf(r, "DsCrackNames failed");
386 talloc_free(tmp_ctx);
387 return NT_STATUS_UNSUCCESSFUL;
390 /* Store the account DN. */
391 r->out.account_dn_str = account_dn_str;
392 talloc_steal(r, account_dn_str);
394 /* Store the domain DN. */
395 r->out.domain_dn_str = r_crack_names.out.ctr->ctr1->array[0].result_name;
396 talloc_steal(r, r_crack_names.out.ctr->ctr1->array[0].result_name);
398 /* Store the KVNO of the account, critical for some kerberos
399 * operations */
400 r->out.kvno = ldb_msg_find_attr_as_uint(res->msgs[0], "msDS-KeyVersionNumber", 0);
402 /* Store the account GUID. */
403 r->out.account_guid = samdb_result_guid(res->msgs[0], "objectGUID");
405 if (r->in.acct_type == ACB_SVRTRUST) {
406 status = libnet_JoinSite(ctx, remote_ldb, r);
408 talloc_free(tmp_ctx);
410 return status;
414 * do a domain join using DCERPC/SAMR calls
415 * - connect to the LSA pipe, to try and find out information about the domain
416 * - create a secondary connection to SAMR pipe
417 * - do a samr_Connect to get a policy handle
418 * - do a samr_LookupDomain to get the domain sid
419 * - do a samr_OpenDomain to get a domain handle
420 * - do a samr_CreateAccount to try and get a new account
422 * If that fails, do:
423 * - do a samr_LookupNames to get the users rid
424 * - do a samr_OpenUser to get a user handle
425 * - potentially delete and recreate the user
426 * - assert the account is of the right type with samrQueryUserInfo
428 * - call libnet_SetPassword_samr_handle to set the password,
429 * and pass a samr_UserInfo21 struct to set full_name and the account flags
431 * - do some ADS specific things when we join as Domain Controller,
432 * look at libnet_joinADSDomain() for the details
434 NTSTATUS libnet_JoinDomain(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_JoinDomain *r)
436 TALLOC_CTX *tmp_ctx;
438 NTSTATUS status, cu_status;
440 struct libnet_RpcConnect *connect_with_info;
441 struct dcerpc_pipe *samr_pipe;
443 struct samr_Connect sc;
444 struct policy_handle p_handle;
445 struct samr_OpenDomain od;
446 struct policy_handle d_handle;
447 struct samr_LookupNames ln;
448 struct samr_Ids rids, types;
449 struct samr_OpenUser ou;
450 struct samr_CreateUser2 cu;
451 struct policy_handle *u_handle = NULL;
452 struct samr_QueryUserInfo qui;
453 union samr_UserInfo *uinfo;
454 struct samr_UserInfo21 u_info21;
455 union libnet_SetPassword r2;
456 struct samr_GetUserPwInfo pwp;
457 struct samr_PwInfo info;
458 struct lsa_String samr_account_name;
460 uint32_t acct_flags, old_acct_flags;
461 uint32_t rid, access_granted;
462 int policy_min_pw_len = 0;
464 struct dom_sid *account_sid = NULL;
465 const char *password_str = NULL;
467 r->out.error_string = NULL;
468 r2.samr_handle.out.error_string = NULL;
470 tmp_ctx = talloc_named(mem_ctx, 0, "libnet_Join temp context");
471 if (!tmp_ctx) {
472 r->out.error_string = NULL;
473 return NT_STATUS_NO_MEMORY;
476 u_handle = talloc(tmp_ctx, struct policy_handle);
477 if (!u_handle) {
478 r->out.error_string = NULL;
479 talloc_free(tmp_ctx);
480 return NT_STATUS_NO_MEMORY;
483 connect_with_info = talloc_zero(tmp_ctx, struct libnet_RpcConnect);
484 if (!connect_with_info) {
485 r->out.error_string = NULL;
486 talloc_free(tmp_ctx);
487 return NT_STATUS_NO_MEMORY;
490 /* prepare connect to the SAMR pipe of PDC */
491 if (r->in.level == LIBNET_JOINDOMAIN_AUTOMATIC) {
492 connect_with_info->in.binding = NULL;
493 connect_with_info->in.name = r->in.domain_name;
494 } else {
495 connect_with_info->in.binding = r->in.binding;
496 connect_with_info->in.name = NULL;
499 /* This level makes a connection to the LSA pipe on the way,
500 * to get some useful bits of information about the domain */
501 connect_with_info->level = LIBNET_RPC_CONNECT_DC_INFO;
502 connect_with_info->in.dcerpc_iface = &ndr_table_samr;
505 establish the SAMR connection
507 status = libnet_RpcConnect(ctx, tmp_ctx, connect_with_info);
508 if (!NT_STATUS_IS_OK(status)) {
509 if (r->in.binding) {
510 r->out.error_string = talloc_asprintf(mem_ctx,
511 "Connection to SAMR pipe of DC %s failed: %s",
512 r->in.binding, connect_with_info->out.error_string);
513 } else {
514 r->out.error_string = talloc_asprintf(mem_ctx,
515 "Connection to SAMR pipe of PDC for %s failed: %s",
516 r->in.domain_name, connect_with_info->out.error_string);
518 talloc_free(tmp_ctx);
519 return status;
522 samr_pipe = connect_with_info->out.dcerpc_pipe;
524 status = dcerpc_pipe_auth(tmp_ctx, &samr_pipe,
525 connect_with_info->out.dcerpc_pipe->binding,
526 &ndr_table_samr, ctx->cred, ctx->lp_ctx);
527 if (!NT_STATUS_IS_OK(status)) {
528 r->out.error_string = talloc_asprintf(mem_ctx,
529 "SAMR bind failed: %s",
530 nt_errstr(status));
531 talloc_free(tmp_ctx);
532 return status;
535 /* prepare samr_Connect */
536 ZERO_STRUCT(p_handle);
537 sc.in.system_name = NULL;
538 sc.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
539 sc.out.connect_handle = &p_handle;
541 /* 2. do a samr_Connect to get a policy handle */
542 status = dcerpc_samr_Connect_r(samr_pipe->binding_handle, tmp_ctx, &sc);
543 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(sc.out.result)) {
544 status = sc.out.result;
546 if (!NT_STATUS_IS_OK(status)) {
547 r->out.error_string = talloc_asprintf(mem_ctx,
548 "samr_Connect failed: %s",
549 nt_errstr(status));
550 talloc_free(tmp_ctx);
551 return status;
554 /* If this is a connection on ncacn_ip_tcp to Win2k3 SP1, we don't get back this useful info */
555 if (!connect_with_info->out.domain_name) {
556 if (r->in.level == LIBNET_JOINDOMAIN_AUTOMATIC) {
557 connect_with_info->out.domain_name = talloc_strdup(tmp_ctx, r->in.domain_name);
558 } else {
559 /* Bugger, we just lost our way to automatically find the domain name */
560 connect_with_info->out.domain_name = talloc_strdup(tmp_ctx, lp_workgroup(ctx->lp_ctx));
561 connect_with_info->out.realm = talloc_strdup(tmp_ctx, lp_realm(ctx->lp_ctx));
565 /* Perhaps we didn't get a SID above, because we are against ncacn_ip_tcp */
566 if (!connect_with_info->out.domain_sid) {
567 struct lsa_String name;
568 struct samr_LookupDomain l;
569 struct dom_sid2 *sid = NULL;
570 name.string = connect_with_info->out.domain_name;
571 l.in.connect_handle = &p_handle;
572 l.in.domain_name = &name;
573 l.out.sid = &sid;
575 status = dcerpc_samr_LookupDomain_r(samr_pipe->binding_handle, tmp_ctx, &l);
576 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(l.out.result)) {
577 status = l.out.result;
579 if (!NT_STATUS_IS_OK(status)) {
580 r->out.error_string = talloc_asprintf(mem_ctx,
581 "SAMR LookupDomain failed: %s",
582 nt_errstr(status));
583 talloc_free(tmp_ctx);
584 return status;
586 connect_with_info->out.domain_sid = *l.out.sid;
589 /* prepare samr_OpenDomain */
590 ZERO_STRUCT(d_handle);
591 od.in.connect_handle = &p_handle;
592 od.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
593 od.in.sid = connect_with_info->out.domain_sid;
594 od.out.domain_handle = &d_handle;
596 /* do a samr_OpenDomain to get a domain handle */
597 status = dcerpc_samr_OpenDomain_r(samr_pipe->binding_handle, tmp_ctx, &od);
598 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(od.out.result)) {
599 status = od.out.result;
601 if (!NT_STATUS_IS_OK(status)) {
602 r->out.error_string = talloc_asprintf(mem_ctx,
603 "samr_OpenDomain for [%s] failed: %s",
604 dom_sid_string(tmp_ctx, connect_with_info->out.domain_sid),
605 nt_errstr(status));
606 talloc_free(tmp_ctx);
607 return status;
610 /* prepare samr_CreateUser2 */
611 ZERO_STRUCTP(u_handle);
612 cu.in.domain_handle = &d_handle;
613 cu.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
614 samr_account_name.string = r->in.account_name;
615 cu.in.account_name = &samr_account_name;
616 cu.in.acct_flags = r->in.acct_type;
617 cu.out.user_handle = u_handle;
618 cu.out.rid = &rid;
619 cu.out.access_granted = &access_granted;
621 /* do a samr_CreateUser2 to get an account handle, or an error */
622 cu_status = dcerpc_samr_CreateUser2_r(samr_pipe->binding_handle, tmp_ctx, &cu);
623 if (NT_STATUS_IS_OK(cu_status) && !NT_STATUS_IS_OK(cu.out.result)) {
624 cu_status = cu.out.result;
626 status = cu_status;
627 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
628 /* prepare samr_LookupNames */
629 ln.in.domain_handle = &d_handle;
630 ln.in.num_names = 1;
631 ln.in.names = talloc_array(tmp_ctx, struct lsa_String, 1);
632 ln.out.rids = &rids;
633 ln.out.types = &types;
634 if (!ln.in.names) {
635 r->out.error_string = NULL;
636 talloc_free(tmp_ctx);
637 return NT_STATUS_NO_MEMORY;
639 ln.in.names[0].string = r->in.account_name;
641 /* 5. do a samr_LookupNames to get the users rid */
642 status = dcerpc_samr_LookupNames_r(samr_pipe->binding_handle, tmp_ctx, &ln);
643 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(ln.out.result)) {
644 status = ln.out.result;
646 if (!NT_STATUS_IS_OK(status)) {
647 r->out.error_string = talloc_asprintf(mem_ctx,
648 "samr_LookupNames for [%s] failed: %s",
649 r->in.account_name,
650 nt_errstr(status));
651 talloc_free(tmp_ctx);
652 return status;
655 /* check if we got one RID for the user */
656 if (ln.out.rids->count != 1) {
657 r->out.error_string = talloc_asprintf(mem_ctx,
658 "samr_LookupNames for [%s] returns %d RIDs",
659 r->in.account_name, ln.out.rids->count);
660 talloc_free(tmp_ctx);
661 return NT_STATUS_INVALID_PARAMETER;
664 /* prepare samr_OpenUser */
665 ZERO_STRUCTP(u_handle);
666 ou.in.domain_handle = &d_handle;
667 ou.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
668 ou.in.rid = ln.out.rids->ids[0];
669 rid = ou.in.rid;
670 ou.out.user_handle = u_handle;
672 /* 6. do a samr_OpenUser to get a user handle */
673 status = dcerpc_samr_OpenUser_r(samr_pipe->binding_handle, tmp_ctx, &ou);
674 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(ou.out.result)) {
675 status = ou.out.result;
677 if (!NT_STATUS_IS_OK(status)) {
678 r->out.error_string = talloc_asprintf(mem_ctx,
679 "samr_OpenUser for [%s] failed: %s",
680 r->in.account_name,
681 nt_errstr(status));
682 talloc_free(tmp_ctx);
683 return status;
686 if (r->in.recreate_account) {
687 struct samr_DeleteUser d;
688 d.in.user_handle = u_handle;
689 d.out.user_handle = u_handle;
690 status = dcerpc_samr_DeleteUser_r(samr_pipe->binding_handle, mem_ctx, &d);
691 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(d.out.result)) {
692 status = d.out.result;
694 if (!NT_STATUS_IS_OK(status)) {
695 r->out.error_string = talloc_asprintf(mem_ctx,
696 "samr_DeleteUser (for recreate) of [%s] failed: %s",
697 r->in.account_name,
698 nt_errstr(status));
699 talloc_free(tmp_ctx);
700 return status;
703 /* We want to recreate, so delete and another samr_CreateUser2 */
705 /* &cu filled in above */
706 status = dcerpc_samr_CreateUser2_r(samr_pipe->binding_handle, tmp_ctx, &cu);
707 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(cu.out.result)) {
708 status = cu.out.result;
710 if (!NT_STATUS_IS_OK(status)) {
711 r->out.error_string = talloc_asprintf(mem_ctx,
712 "samr_CreateUser2 (recreate) for [%s] failed: %s",
713 r->in.account_name, nt_errstr(status));
714 talloc_free(tmp_ctx);
715 return status;
718 } else if (!NT_STATUS_IS_OK(status)) {
719 r->out.error_string = talloc_asprintf(mem_ctx,
720 "samr_CreateUser2 for [%s] failed: %s",
721 r->in.account_name, nt_errstr(status));
722 talloc_free(tmp_ctx);
723 return status;
726 /* prepare samr_QueryUserInfo (get flags) */
727 qui.in.user_handle = u_handle;
728 qui.in.level = 16;
729 qui.out.info = &uinfo;
731 status = dcerpc_samr_QueryUserInfo_r(samr_pipe->binding_handle, tmp_ctx, &qui);
732 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(qui.out.result)) {
733 status = qui.out.result;
735 if (!NT_STATUS_IS_OK(status)) {
736 r->out.error_string = talloc_asprintf(mem_ctx,
737 "samr_QueryUserInfo for [%s] failed: %s",
738 r->in.account_name,
739 nt_errstr(status));
740 talloc_free(tmp_ctx);
741 return status;
744 if (!uinfo) {
745 status = NT_STATUS_INVALID_PARAMETER;
746 r->out.error_string
747 = talloc_asprintf(mem_ctx,
748 "samr_QueryUserInfo failed to return qui.out.info for [%s]: %s",
749 r->in.account_name, nt_errstr(status));
750 talloc_free(tmp_ctx);
751 return status;
754 old_acct_flags = (uinfo->info16.acct_flags & (ACB_WSTRUST | ACB_SVRTRUST | ACB_DOMTRUST));
755 /* Possibly bail if the account is of the wrong type */
756 if (old_acct_flags
757 != r->in.acct_type) {
758 const char *old_account_type, *new_account_type;
759 switch (old_acct_flags) {
760 case ACB_WSTRUST:
761 old_account_type = "domain member (member)";
762 break;
763 case ACB_SVRTRUST:
764 old_account_type = "domain controller (bdc)";
765 break;
766 case ACB_DOMTRUST:
767 old_account_type = "trusted domain";
768 break;
769 default:
770 return NT_STATUS_INVALID_PARAMETER;
772 switch (r->in.acct_type) {
773 case ACB_WSTRUST:
774 new_account_type = "domain member (member)";
775 break;
776 case ACB_SVRTRUST:
777 new_account_type = "domain controller (bdc)";
778 break;
779 case ACB_DOMTRUST:
780 new_account_type = "trusted domain";
781 break;
782 default:
783 return NT_STATUS_INVALID_PARAMETER;
786 if (!NT_STATUS_EQUAL(cu_status, NT_STATUS_USER_EXISTS)) {
787 /* We created a new user, but they didn't come out the right type?!? */
788 r->out.error_string
789 = talloc_asprintf(mem_ctx,
790 "We asked to create a new machine account (%s) of type %s, "
791 "but we got an account of type %s. This is unexpected. "
792 "Perhaps delete the account and try again.",
793 r->in.account_name, new_account_type, old_account_type);
794 talloc_free(tmp_ctx);
795 return NT_STATUS_INVALID_PARAMETER;
796 } else {
797 /* The account is of the wrong type, so bail */
799 /* TODO: We should allow a --force option to override, and redo this from the top setting r.in.recreate_account */
800 r->out.error_string
801 = talloc_asprintf(mem_ctx,
802 "The machine account (%s) already exists in the domain %s, "
803 "but is a %s. You asked to join as a %s. Please delete "
804 "the account and try again.",
805 r->in.account_name, connect_with_info->out.domain_name, old_account_type, new_account_type);
806 talloc_free(tmp_ctx);
807 return NT_STATUS_USER_EXISTS;
809 } else {
810 acct_flags = uinfo->info16.acct_flags;
813 acct_flags = (acct_flags & ~(ACB_DISABLED|ACB_PWNOTREQ));
815 /* Find out what password policy this user has */
816 pwp.in.user_handle = u_handle;
817 pwp.out.info = &info;
819 status = dcerpc_samr_GetUserPwInfo_r(samr_pipe->binding_handle, tmp_ctx, &pwp);
820 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(pwp.out.result)) {
821 status = pwp.out.result;
823 if (NT_STATUS_IS_OK(status)) {
824 policy_min_pw_len = pwp.out.info->min_password_length;
827 /* Grab a password of that minimum length */
829 password_str = generate_random_password(tmp_ctx, MAX(8, policy_min_pw_len), 255);
831 /* set full_name and reset flags */
832 ZERO_STRUCT(u_info21);
833 u_info21.full_name.string = r->in.account_name;
834 u_info21.acct_flags = acct_flags;
835 u_info21.fields_present = SAMR_FIELD_FULL_NAME | SAMR_FIELD_ACCT_FLAGS;
837 r2.samr_handle.level = LIBNET_SET_PASSWORD_SAMR_HANDLE;
838 r2.samr_handle.in.account_name = r->in.account_name;
839 r2.samr_handle.in.newpassword = password_str;
840 r2.samr_handle.in.user_handle = u_handle;
841 r2.samr_handle.in.dcerpc_pipe = samr_pipe;
842 r2.samr_handle.in.info21 = &u_info21;
844 status = libnet_SetPassword(ctx, tmp_ctx, &r2);
845 if (!NT_STATUS_IS_OK(status)) {
846 r->out.error_string = talloc_steal(mem_ctx, r2.samr_handle.out.error_string);
847 talloc_free(tmp_ctx);
848 return status;
851 account_sid = dom_sid_add_rid(mem_ctx, connect_with_info->out.domain_sid, rid);
852 if (!account_sid) {
853 r->out.error_string = NULL;
854 talloc_free(tmp_ctx);
855 return NT_STATUS_NO_MEMORY;
858 /* Finish out by pushing various bits of status data out for the caller to use */
859 r->out.join_password = password_str;
860 talloc_steal(mem_ctx, r->out.join_password);
862 r->out.domain_sid = connect_with_info->out.domain_sid;
863 talloc_steal(mem_ctx, r->out.domain_sid);
865 r->out.account_sid = account_sid;
866 talloc_steal(mem_ctx, r->out.account_sid);
868 r->out.domain_name = connect_with_info->out.domain_name;
869 talloc_steal(mem_ctx, r->out.domain_name);
870 r->out.realm = connect_with_info->out.realm;
871 talloc_steal(mem_ctx, r->out.realm);
872 r->out.samr_pipe = samr_pipe;
873 talloc_reparent(tmp_ctx, mem_ctx, samr_pipe);
874 r->out.samr_binding = samr_pipe->binding;
875 talloc_steal(mem_ctx, r->out.samr_binding);
876 r->out.user_handle = u_handle;
877 talloc_steal(mem_ctx, u_handle);
878 r->out.error_string = r2.samr_handle.out.error_string;
879 talloc_steal(mem_ctx, r2.samr_handle.out.error_string);
880 r->out.kvno = 0;
881 r->out.server_dn_str = NULL;
882 talloc_free(tmp_ctx);
884 /* Now, if it was AD, then we want to start looking changing a
885 * few more things. Otherwise, we are done. */
886 if (r->out.realm) {
887 status = libnet_JoinADSDomain(ctx, r);
888 return status;
891 return status;
894 static NTSTATUS libnet_Join_primary_domain(struct libnet_context *ctx,
895 TALLOC_CTX *mem_ctx,
896 struct libnet_Join *r)
898 NTSTATUS status;
899 TALLOC_CTX *tmp_mem;
900 struct libnet_JoinDomain *r2;
901 struct provision_store_self_join_settings *set_secrets;
902 uint32_t acct_type = 0;
903 const char *account_name;
904 const char *netbios_name;
905 const char *error_string;
907 r->out.error_string = NULL;
909 tmp_mem = talloc_new(mem_ctx);
910 if (!tmp_mem) {
911 return NT_STATUS_NO_MEMORY;
914 r2 = talloc(tmp_mem, struct libnet_JoinDomain);
915 if (!r2) {
916 r->out.error_string = NULL;
917 talloc_free(tmp_mem);
918 return NT_STATUS_NO_MEMORY;
921 if (r->in.join_type == SEC_CHAN_BDC) {
922 acct_type = ACB_SVRTRUST;
923 } else if (r->in.join_type == SEC_CHAN_WKSTA) {
924 acct_type = ACB_WSTRUST;
925 } else {
926 r->out.error_string = NULL;
927 talloc_free(tmp_mem);
928 return NT_STATUS_INVALID_PARAMETER;
931 if (r->in.netbios_name != NULL) {
932 netbios_name = r->in.netbios_name;
933 } else {
934 netbios_name = talloc_strdup(tmp_mem, lp_netbios_name(ctx->lp_ctx));
935 if (!netbios_name) {
936 r->out.error_string = NULL;
937 talloc_free(tmp_mem);
938 return NT_STATUS_NO_MEMORY;
942 account_name = talloc_asprintf(tmp_mem, "%s$", netbios_name);
943 if (!account_name) {
944 r->out.error_string = NULL;
945 talloc_free(tmp_mem);
946 return NT_STATUS_NO_MEMORY;
950 * join the domain
952 ZERO_STRUCTP(r2);
953 r2->in.domain_name = r->in.domain_name;
954 r2->in.account_name = account_name;
955 r2->in.netbios_name = netbios_name;
956 r2->in.level = LIBNET_JOINDOMAIN_AUTOMATIC;
957 r2->in.acct_type = acct_type;
958 r2->in.recreate_account = false;
959 status = libnet_JoinDomain(ctx, r2, r2);
960 if (!NT_STATUS_IS_OK(status)) {
961 r->out.error_string = talloc_steal(mem_ctx, r2->out.error_string);
962 talloc_free(tmp_mem);
963 return status;
966 set_secrets = talloc(tmp_mem, struct provision_store_self_join_settings);
967 if (!set_secrets) {
968 r->out.error_string = NULL;
969 talloc_free(tmp_mem);
970 return NT_STATUS_NO_MEMORY;
973 ZERO_STRUCTP(set_secrets);
974 set_secrets->domain_name = r2->out.domain_name;
975 set_secrets->realm = r2->out.realm;
976 set_secrets->account_name = account_name;
977 set_secrets->netbios_name = netbios_name;
978 set_secrets->secure_channel_type = r->in.join_type;
979 set_secrets->machine_password = r2->out.join_password;
980 set_secrets->key_version_number = r2->out.kvno;
981 set_secrets->domain_sid = r2->out.domain_sid;
983 status = provision_store_self_join(ctx, ctx->lp_ctx, ctx->event_ctx, set_secrets, &error_string);
984 if (!NT_STATUS_IS_OK(status)) {
985 r->out.error_string = talloc_steal(mem_ctx, error_string);
986 talloc_free(tmp_mem);
987 return status;
990 /* move all out parameter to the callers TALLOC_CTX */
991 r->out.error_string = NULL;
992 r->out.join_password = r2->out.join_password;
993 talloc_reparent(r2, mem_ctx, r2->out.join_password);
994 r->out.domain_sid = r2->out.domain_sid;
995 talloc_reparent(r2, mem_ctx, r2->out.domain_sid);
996 r->out.domain_name = r2->out.domain_name;
997 talloc_reparent(r2, mem_ctx, r2->out.domain_name);
998 talloc_free(tmp_mem);
999 return NT_STATUS_OK;
1002 NTSTATUS libnet_Join(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_Join *r)
1004 switch (r->in.join_type) {
1005 case SEC_CHAN_WKSTA:
1006 return libnet_Join_primary_domain(ctx, mem_ctx, r);
1007 case SEC_CHAN_BDC:
1008 return libnet_Join_primary_domain(ctx, mem_ctx, r);
1009 case SEC_CHAN_DOMAIN:
1010 case SEC_CHAN_DNS_DOMAIN:
1011 case SEC_CHAN_NULL:
1012 break;
1015 r->out.error_string = talloc_asprintf(mem_ctx,
1016 "Invalid join type specified (%08X) attempting to join domain %s",
1017 r->in.join_type, r->in.domain_name);
1018 return NT_STATUS_INVALID_PARAMETER;