VERSION: Disable git snapshots for the 4.0.15 release.
[Samba.git] / source4 / libnet / libnet_join.c
blob8c1b57ddde180f43fd1b923b3423e1707eb0ed24
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 <ldb.h>
26 #include <ldb_errors.h>
27 #include "dsdb/samdb/samdb.h"
28 #include "ldb_wrap.h"
29 #include "libcli/security/security.h"
30 #include "auth/credentials/credentials.h"
31 #include "auth/credentials/credentials_krb5.h"
32 #include "librpc/gen_ndr/ndr_samr_c.h"
33 #include "param/param.h"
34 #include "param/provision.h"
35 #include "system/kerberos.h"
36 #include "auth/kerberos/kerberos.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_zero(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 (account_dn == NULL) {
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[2];
275 const char *dns_host_name = strlower_talloc(msg,
276 talloc_asprintf(msg,
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(msg, "HOST/%s",
288 dns_host_name);
289 service_principal_name[1] = talloc_asprintf(msg, "HOST/%s",
290 r->in.netbios_name);
292 for (i=0; i < ARRAY_SIZE(service_principal_name); i++) {
293 if (!service_principal_name[i]) {
294 r->out.error_string = NULL;
295 talloc_free(tmp_ctx);
296 return NT_STATUS_NO_MEMORY;
298 rtn = ldb_msg_add_string(msg, "servicePrincipalName",
299 service_principal_name[i]);
300 if (rtn != LDB_SUCCESS) {
301 r->out.error_string = NULL;
302 talloc_free(tmp_ctx);
303 return NT_STATUS_NO_MEMORY;
307 rtn = ldb_msg_add_string(msg, "dNSHostName", dns_host_name);
308 if (rtn != LDB_SUCCESS) {
309 r->out.error_string = NULL;
310 talloc_free(tmp_ctx);
311 return NT_STATUS_NO_MEMORY;
314 rtn = dsdb_replace(remote_ldb, msg, 0);
315 if (rtn != LDB_SUCCESS) {
316 r->out.error_string
317 = talloc_asprintf(r,
318 "Failed to replace entries on %s",
319 ldb_dn_get_linearized(msg->dn));
320 talloc_free(tmp_ctx);
321 return NT_STATUS_INTERNAL_DB_CORRUPTION;
325 msg = ldb_msg_new(tmp_ctx);
326 if (!msg) {
327 r->out.error_string = NULL;
328 talloc_free(tmp_ctx);
329 return NT_STATUS_NO_MEMORY;
331 msg->dn = res->msgs[0]->dn;
333 rtn = samdb_msg_add_uint(remote_ldb, msg, msg,
334 "msDS-SupportedEncryptionTypes", ENC_ALL_TYPES);
335 if (rtn != LDB_SUCCESS) {
336 r->out.error_string = NULL;
337 talloc_free(tmp_ctx);
338 return NT_STATUS_NO_MEMORY;
341 rtn = dsdb_replace(remote_ldb, msg, 0);
342 /* The remote server may not support this attribute, if it
343 * isn't a modern schema */
344 if (rtn != LDB_SUCCESS && rtn != LDB_ERR_NO_SUCH_ATTRIBUTE) {
345 r->out.error_string
346 = talloc_asprintf(r,
347 "Failed to replace msDS-SupportedEncryptionTypes on %s",
348 ldb_dn_get_linearized(msg->dn));
349 talloc_free(tmp_ctx);
350 return NT_STATUS_INTERNAL_DB_CORRUPTION;
353 /* DsCrackNames to find out the DN of the domain. */
354 r_crack_names.in.req->req1.format_offered = DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT;
355 r_crack_names.in.req->req1.format_desired = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
356 names[0].str = talloc_asprintf(tmp_ctx, "%s\\", r->out.domain_name);
357 if (!names[0].str) {
358 r->out.error_string = NULL;
359 talloc_free(tmp_ctx);
360 return NT_STATUS_NO_MEMORY;
363 status = dcerpc_drsuapi_DsCrackNames_r(drsuapi_pipe->binding_handle, tmp_ctx, &r_crack_names);
364 if (!NT_STATUS_IS_OK(status)) {
365 r->out.error_string
366 = talloc_asprintf(r,
367 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s",
368 r->in.domain_name,
369 nt_errstr(status));
370 talloc_free(tmp_ctx);
371 return status;
372 } else if (!W_ERROR_IS_OK(r_crack_names.out.result)) {
373 r->out.error_string
374 = talloc_asprintf(r,
375 "DsCrackNames failed - %s", win_errstr(r_crack_names.out.result));
376 talloc_free(tmp_ctx);
377 return NT_STATUS_UNSUCCESSFUL;
378 } else if (*r_crack_names.out.level_out != 1
379 || !r_crack_names.out.ctr->ctr1
380 || r_crack_names.out.ctr->ctr1->count != 1
381 || !r_crack_names.out.ctr->ctr1->array[0].result_name
382 || r_crack_names.out.ctr->ctr1->array[0].status != DRSUAPI_DS_NAME_STATUS_OK) {
383 r->out.error_string = talloc_asprintf(r, "DsCrackNames failed");
384 talloc_free(tmp_ctx);
385 return NT_STATUS_UNSUCCESSFUL;
388 /* Store the account DN. */
389 r->out.account_dn_str = account_dn_str;
390 talloc_steal(r, account_dn_str);
392 /* Store the domain DN. */
393 r->out.domain_dn_str = r_crack_names.out.ctr->ctr1->array[0].result_name;
394 talloc_steal(r, r_crack_names.out.ctr->ctr1->array[0].result_name);
396 /* Store the KVNO of the account, critical for some kerberos
397 * operations */
398 r->out.kvno = ldb_msg_find_attr_as_uint(res->msgs[0], "msDS-KeyVersionNumber", 0);
400 /* Store the account GUID. */
401 r->out.account_guid = samdb_result_guid(res->msgs[0], "objectGUID");
403 if (r->in.acct_type == ACB_SVRTRUST) {
404 status = libnet_JoinSite(ctx, remote_ldb, r);
406 talloc_free(tmp_ctx);
408 return status;
412 * do a domain join using DCERPC/SAMR calls
413 * - connect to the LSA pipe, to try and find out information about the domain
414 * - create a secondary connection to SAMR pipe
415 * - do a samr_Connect to get a policy handle
416 * - do a samr_LookupDomain to get the domain sid
417 * - do a samr_OpenDomain to get a domain handle
418 * - do a samr_CreateAccount to try and get a new account
420 * If that fails, do:
421 * - do a samr_LookupNames to get the users rid
422 * - do a samr_OpenUser to get a user handle
423 * - potentially delete and recreate the user
424 * - assert the account is of the right type with samrQueryUserInfo
426 * - call libnet_SetPassword_samr_handle to set the password,
427 * and pass a samr_UserInfo21 struct to set full_name and the account flags
429 * - do some ADS specific things when we join as Domain Controller,
430 * look at libnet_joinADSDomain() for the details
432 NTSTATUS libnet_JoinDomain(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_JoinDomain *r)
434 TALLOC_CTX *tmp_ctx;
436 NTSTATUS status, cu_status;
438 struct libnet_RpcConnect *connect_with_info;
439 struct dcerpc_pipe *samr_pipe;
441 struct samr_Connect sc;
442 struct policy_handle p_handle;
443 struct samr_OpenDomain od;
444 struct policy_handle d_handle;
445 struct samr_LookupNames ln;
446 struct samr_Ids rids, types;
447 struct samr_OpenUser ou;
448 struct samr_CreateUser2 cu;
449 struct policy_handle *u_handle = NULL;
450 struct samr_QueryUserInfo qui;
451 union samr_UserInfo *uinfo;
452 struct samr_UserInfo21 u_info21;
453 union libnet_SetPassword r2;
454 struct samr_GetUserPwInfo pwp;
455 struct samr_PwInfo info;
456 struct lsa_String samr_account_name;
458 uint32_t acct_flags, old_acct_flags;
459 uint32_t rid, access_granted;
460 int policy_min_pw_len = 0;
462 struct dom_sid *account_sid = NULL;
463 const char *password_str = NULL;
465 r->out.error_string = NULL;
466 r2.samr_handle.out.error_string = NULL;
468 tmp_ctx = talloc_named(mem_ctx, 0, "libnet_Join temp context");
469 if (!tmp_ctx) {
470 r->out.error_string = NULL;
471 return NT_STATUS_NO_MEMORY;
474 u_handle = talloc(tmp_ctx, struct policy_handle);
475 if (!u_handle) {
476 r->out.error_string = NULL;
477 talloc_free(tmp_ctx);
478 return NT_STATUS_NO_MEMORY;
481 connect_with_info = talloc_zero(tmp_ctx, struct libnet_RpcConnect);
482 if (!connect_with_info) {
483 r->out.error_string = NULL;
484 talloc_free(tmp_ctx);
485 return NT_STATUS_NO_MEMORY;
488 /* prepare connect to the SAMR pipe of PDC */
489 if (r->in.level == LIBNET_JOINDOMAIN_AUTOMATIC) {
490 connect_with_info->in.binding = NULL;
491 connect_with_info->in.name = r->in.domain_name;
492 } else {
493 connect_with_info->in.binding = r->in.binding;
494 connect_with_info->in.name = NULL;
497 /* This level makes a connection to the LSA pipe on the way,
498 * to get some useful bits of information about the domain */
499 connect_with_info->level = LIBNET_RPC_CONNECT_DC_INFO;
500 connect_with_info->in.dcerpc_iface = &ndr_table_samr;
503 establish the SAMR connection
505 status = libnet_RpcConnect(ctx, tmp_ctx, connect_with_info);
506 if (!NT_STATUS_IS_OK(status)) {
507 if (r->in.binding) {
508 r->out.error_string = talloc_asprintf(mem_ctx,
509 "Connection to SAMR pipe of DC %s failed: %s",
510 r->in.binding, connect_with_info->out.error_string);
511 } else {
512 r->out.error_string = talloc_asprintf(mem_ctx,
513 "Connection to SAMR pipe of PDC for %s failed: %s",
514 r->in.domain_name, connect_with_info->out.error_string);
516 talloc_free(tmp_ctx);
517 return status;
520 samr_pipe = connect_with_info->out.dcerpc_pipe;
522 status = dcerpc_pipe_auth(tmp_ctx, &samr_pipe,
523 connect_with_info->out.dcerpc_pipe->binding,
524 &ndr_table_samr, ctx->cred, ctx->lp_ctx);
525 if (!NT_STATUS_IS_OK(status)) {
526 r->out.error_string = talloc_asprintf(mem_ctx,
527 "SAMR bind failed: %s",
528 nt_errstr(status));
529 talloc_free(tmp_ctx);
530 return status;
533 /* prepare samr_Connect */
534 ZERO_STRUCT(p_handle);
535 sc.in.system_name = NULL;
536 sc.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
537 sc.out.connect_handle = &p_handle;
539 /* 2. do a samr_Connect to get a policy handle */
540 status = dcerpc_samr_Connect_r(samr_pipe->binding_handle, tmp_ctx, &sc);
541 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(sc.out.result)) {
542 status = sc.out.result;
544 if (!NT_STATUS_IS_OK(status)) {
545 r->out.error_string = talloc_asprintf(mem_ctx,
546 "samr_Connect failed: %s",
547 nt_errstr(status));
548 talloc_free(tmp_ctx);
549 return status;
552 /* If this is a connection on ncacn_ip_tcp to Win2k3 SP1, we don't get back this useful info */
553 if (!connect_with_info->out.domain_name) {
554 if (r->in.level == LIBNET_JOINDOMAIN_AUTOMATIC) {
555 connect_with_info->out.domain_name = talloc_strdup(tmp_ctx, r->in.domain_name);
556 } else {
557 /* Bugger, we just lost our way to automatically find the domain name */
558 connect_with_info->out.domain_name = talloc_strdup(tmp_ctx, lpcfg_workgroup(ctx->lp_ctx));
559 connect_with_info->out.realm = talloc_strdup(tmp_ctx, lpcfg_realm(ctx->lp_ctx));
563 /* Perhaps we didn't get a SID above, because we are against ncacn_ip_tcp */
564 if (!connect_with_info->out.domain_sid) {
565 struct lsa_String name;
566 struct samr_LookupDomain l;
567 struct dom_sid2 *sid = NULL;
568 name.string = connect_with_info->out.domain_name;
569 l.in.connect_handle = &p_handle;
570 l.in.domain_name = &name;
571 l.out.sid = &sid;
573 status = dcerpc_samr_LookupDomain_r(samr_pipe->binding_handle, tmp_ctx, &l);
574 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(l.out.result)) {
575 status = l.out.result;
577 if (!NT_STATUS_IS_OK(status)) {
578 r->out.error_string = talloc_asprintf(mem_ctx,
579 "SAMR LookupDomain failed: %s",
580 nt_errstr(status));
581 talloc_free(tmp_ctx);
582 return status;
584 connect_with_info->out.domain_sid = *l.out.sid;
587 /* prepare samr_OpenDomain */
588 ZERO_STRUCT(d_handle);
589 od.in.connect_handle = &p_handle;
590 od.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
591 od.in.sid = connect_with_info->out.domain_sid;
592 od.out.domain_handle = &d_handle;
594 /* do a samr_OpenDomain to get a domain handle */
595 status = dcerpc_samr_OpenDomain_r(samr_pipe->binding_handle, tmp_ctx, &od);
596 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(od.out.result)) {
597 status = od.out.result;
599 if (!NT_STATUS_IS_OK(status)) {
600 r->out.error_string = talloc_asprintf(mem_ctx,
601 "samr_OpenDomain for [%s] failed: %s",
602 dom_sid_string(tmp_ctx, connect_with_info->out.domain_sid),
603 nt_errstr(status));
604 talloc_free(tmp_ctx);
605 return status;
608 /* prepare samr_CreateUser2 */
609 ZERO_STRUCTP(u_handle);
610 cu.in.domain_handle = &d_handle;
611 cu.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
612 samr_account_name.string = r->in.account_name;
613 cu.in.account_name = &samr_account_name;
614 cu.in.acct_flags = r->in.acct_type;
615 cu.out.user_handle = u_handle;
616 cu.out.rid = &rid;
617 cu.out.access_granted = &access_granted;
619 /* do a samr_CreateUser2 to get an account handle, or an error */
620 cu_status = dcerpc_samr_CreateUser2_r(samr_pipe->binding_handle, tmp_ctx, &cu);
621 if (NT_STATUS_IS_OK(cu_status) && !NT_STATUS_IS_OK(cu.out.result)) {
622 cu_status = cu.out.result;
624 status = cu_status;
625 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
626 /* prepare samr_LookupNames */
627 ln.in.domain_handle = &d_handle;
628 ln.in.num_names = 1;
629 ln.in.names = talloc_array(tmp_ctx, struct lsa_String, 1);
630 ln.out.rids = &rids;
631 ln.out.types = &types;
632 if (!ln.in.names) {
633 r->out.error_string = NULL;
634 talloc_free(tmp_ctx);
635 return NT_STATUS_NO_MEMORY;
637 ln.in.names[0].string = r->in.account_name;
639 /* 5. do a samr_LookupNames to get the users rid */
640 status = dcerpc_samr_LookupNames_r(samr_pipe->binding_handle, tmp_ctx, &ln);
641 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(ln.out.result)) {
642 status = ln.out.result;
644 if (!NT_STATUS_IS_OK(status)) {
645 r->out.error_string = talloc_asprintf(mem_ctx,
646 "samr_LookupNames for [%s] failed: %s",
647 r->in.account_name,
648 nt_errstr(status));
649 talloc_free(tmp_ctx);
650 return status;
653 /* check if we got one RID for the user */
654 if (ln.out.rids->count != 1) {
655 r->out.error_string = talloc_asprintf(mem_ctx,
656 "samr_LookupNames for [%s] returns %d RIDs",
657 r->in.account_name, ln.out.rids->count);
658 talloc_free(tmp_ctx);
659 return NT_STATUS_INVALID_NETWORK_RESPONSE;
662 if (ln.out.types->count != 1) {
663 r->out.error_string = talloc_asprintf(mem_ctx,
664 "samr_LookupNames for [%s] returns %d RID TYPEs",
665 r->in.account_name, ln.out.types->count);
666 talloc_free(tmp_ctx);
667 return NT_STATUS_INVALID_NETWORK_RESPONSE;
670 /* prepare samr_OpenUser */
671 ZERO_STRUCTP(u_handle);
672 ou.in.domain_handle = &d_handle;
673 ou.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
674 ou.in.rid = ln.out.rids->ids[0];
675 rid = ou.in.rid;
676 ou.out.user_handle = u_handle;
678 /* 6. do a samr_OpenUser to get a user handle */
679 status = dcerpc_samr_OpenUser_r(samr_pipe->binding_handle, tmp_ctx, &ou);
680 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(ou.out.result)) {
681 status = ou.out.result;
683 if (!NT_STATUS_IS_OK(status)) {
684 r->out.error_string = talloc_asprintf(mem_ctx,
685 "samr_OpenUser for [%s] failed: %s",
686 r->in.account_name,
687 nt_errstr(status));
688 talloc_free(tmp_ctx);
689 return status;
692 if (r->in.recreate_account) {
693 struct samr_DeleteUser d;
694 d.in.user_handle = u_handle;
695 d.out.user_handle = u_handle;
696 status = dcerpc_samr_DeleteUser_r(samr_pipe->binding_handle, mem_ctx, &d);
697 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(d.out.result)) {
698 status = d.out.result;
700 if (!NT_STATUS_IS_OK(status)) {
701 r->out.error_string = talloc_asprintf(mem_ctx,
702 "samr_DeleteUser (for recreate) of [%s] failed: %s",
703 r->in.account_name,
704 nt_errstr(status));
705 talloc_free(tmp_ctx);
706 return status;
709 /* We want to recreate, so delete and another samr_CreateUser2 */
711 /* &cu filled in above */
712 status = dcerpc_samr_CreateUser2_r(samr_pipe->binding_handle, tmp_ctx, &cu);
713 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(cu.out.result)) {
714 status = cu.out.result;
716 if (!NT_STATUS_IS_OK(status)) {
717 r->out.error_string = talloc_asprintf(mem_ctx,
718 "samr_CreateUser2 (recreate) for [%s] failed: %s",
719 r->in.account_name, nt_errstr(status));
720 talloc_free(tmp_ctx);
721 return status;
724 } else if (!NT_STATUS_IS_OK(status)) {
725 r->out.error_string = talloc_asprintf(mem_ctx,
726 "samr_CreateUser2 for [%s] failed: %s",
727 r->in.account_name, nt_errstr(status));
728 talloc_free(tmp_ctx);
729 return status;
732 /* prepare samr_QueryUserInfo (get flags) */
733 qui.in.user_handle = u_handle;
734 qui.in.level = 16;
735 qui.out.info = &uinfo;
737 status = dcerpc_samr_QueryUserInfo_r(samr_pipe->binding_handle, tmp_ctx, &qui);
738 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(qui.out.result)) {
739 status = qui.out.result;
741 if (!NT_STATUS_IS_OK(status)) {
742 r->out.error_string = talloc_asprintf(mem_ctx,
743 "samr_QueryUserInfo for [%s] failed: %s",
744 r->in.account_name,
745 nt_errstr(status));
746 talloc_free(tmp_ctx);
747 return status;
750 if (!uinfo) {
751 status = NT_STATUS_INVALID_PARAMETER;
752 r->out.error_string
753 = talloc_asprintf(mem_ctx,
754 "samr_QueryUserInfo failed to return qui.out.info for [%s]: %s",
755 r->in.account_name, nt_errstr(status));
756 talloc_free(tmp_ctx);
757 return status;
760 old_acct_flags = (uinfo->info16.acct_flags & (ACB_WSTRUST | ACB_SVRTRUST | ACB_DOMTRUST));
761 /* Possibly bail if the account is of the wrong type */
762 if (old_acct_flags
763 != r->in.acct_type) {
764 const char *old_account_type, *new_account_type;
765 switch (old_acct_flags) {
766 case ACB_WSTRUST:
767 old_account_type = "domain member (member)";
768 break;
769 case ACB_SVRTRUST:
770 old_account_type = "domain controller (bdc)";
771 break;
772 case ACB_DOMTRUST:
773 old_account_type = "trusted domain";
774 break;
775 default:
776 return NT_STATUS_INVALID_PARAMETER;
778 switch (r->in.acct_type) {
779 case ACB_WSTRUST:
780 new_account_type = "domain member (member)";
781 break;
782 case ACB_SVRTRUST:
783 new_account_type = "domain controller (bdc)";
784 break;
785 case ACB_DOMTRUST:
786 new_account_type = "trusted domain";
787 break;
788 default:
789 return NT_STATUS_INVALID_PARAMETER;
792 if (!NT_STATUS_EQUAL(cu_status, NT_STATUS_USER_EXISTS)) {
793 /* We created a new user, but they didn't come out the right type?!? */
794 r->out.error_string
795 = talloc_asprintf(mem_ctx,
796 "We asked to create a new machine account (%s) of type %s, "
797 "but we got an account of type %s. This is unexpected. "
798 "Perhaps delete the account and try again.",
799 r->in.account_name, new_account_type, old_account_type);
800 talloc_free(tmp_ctx);
801 return NT_STATUS_INVALID_PARAMETER;
802 } else {
803 /* The account is of the wrong type, so bail */
805 /* TODO: We should allow a --force option to override, and redo this from the top setting r.in.recreate_account */
806 r->out.error_string
807 = talloc_asprintf(mem_ctx,
808 "The machine account (%s) already exists in the domain %s, "
809 "but is a %s. You asked to join as a %s. Please delete "
810 "the account and try again.",
811 r->in.account_name, connect_with_info->out.domain_name, old_account_type, new_account_type);
812 talloc_free(tmp_ctx);
813 return NT_STATUS_USER_EXISTS;
815 } else {
816 acct_flags = uinfo->info16.acct_flags;
819 acct_flags = (acct_flags & ~(ACB_DISABLED|ACB_PWNOTREQ));
821 /* Find out what password policy this user has */
822 pwp.in.user_handle = u_handle;
823 pwp.out.info = &info;
825 status = dcerpc_samr_GetUserPwInfo_r(samr_pipe->binding_handle, tmp_ctx, &pwp);
826 if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(pwp.out.result)) {
827 status = pwp.out.result;
829 if (NT_STATUS_IS_OK(status)) {
830 policy_min_pw_len = pwp.out.info->min_password_length;
833 if (r->in.account_pass != NULL) {
834 password_str = talloc_strdup(tmp_ctx, r->in.account_pass);
835 } else {
836 /* Grab a password of that minimum length */
837 password_str = generate_random_password(tmp_ctx,
838 MAX(8, policy_min_pw_len), 255);
840 if (!password_str) {
841 r->out.error_string = NULL;
842 talloc_free(tmp_ctx);
843 return NT_STATUS_NO_MEMORY;
846 /* set full_name and reset flags */
847 ZERO_STRUCT(u_info21);
848 u_info21.full_name.string = r->in.account_name;
849 u_info21.acct_flags = acct_flags;
850 u_info21.fields_present = SAMR_FIELD_FULL_NAME | SAMR_FIELD_ACCT_FLAGS;
852 r2.samr_handle.level = LIBNET_SET_PASSWORD_SAMR_HANDLE;
853 r2.samr_handle.in.account_name = r->in.account_name;
854 r2.samr_handle.in.newpassword = password_str;
855 r2.samr_handle.in.user_handle = u_handle;
856 r2.samr_handle.in.dcerpc_pipe = samr_pipe;
857 r2.samr_handle.in.info21 = &u_info21;
859 status = libnet_SetPassword(ctx, tmp_ctx, &r2);
860 if (!NT_STATUS_IS_OK(status)) {
861 r->out.error_string = talloc_steal(mem_ctx, r2.samr_handle.out.error_string);
862 talloc_free(tmp_ctx);
863 return status;
866 account_sid = dom_sid_add_rid(mem_ctx, connect_with_info->out.domain_sid, rid);
867 if (!account_sid) {
868 r->out.error_string = NULL;
869 talloc_free(tmp_ctx);
870 return NT_STATUS_NO_MEMORY;
873 /* Finish out by pushing various bits of status data out for the caller to use */
874 r->out.join_password = password_str;
875 talloc_steal(mem_ctx, r->out.join_password);
877 r->out.domain_sid = connect_with_info->out.domain_sid;
878 talloc_steal(mem_ctx, r->out.domain_sid);
880 r->out.account_sid = account_sid;
881 talloc_steal(mem_ctx, r->out.account_sid);
883 r->out.domain_name = connect_with_info->out.domain_name;
884 talloc_steal(mem_ctx, r->out.domain_name);
885 r->out.realm = connect_with_info->out.realm;
886 talloc_steal(mem_ctx, r->out.realm);
887 r->out.samr_pipe = samr_pipe;
888 talloc_reparent(tmp_ctx, mem_ctx, samr_pipe);
889 r->out.samr_binding = samr_pipe->binding;
890 talloc_steal(mem_ctx, r->out.samr_binding);
891 r->out.user_handle = u_handle;
892 talloc_steal(mem_ctx, u_handle);
893 r->out.error_string = r2.samr_handle.out.error_string;
894 talloc_steal(mem_ctx, r2.samr_handle.out.error_string);
895 r->out.kvno = 0;
896 r->out.server_dn_str = NULL;
897 talloc_free(tmp_ctx);
899 /* Now, if it was AD, then we want to start looking changing a
900 * few more things. Otherwise, we are done. */
901 if (r->out.realm) {
902 status = libnet_JoinADSDomain(ctx, r);
903 return status;
906 return status;
909 NTSTATUS libnet_Join_member(struct libnet_context *ctx,
910 TALLOC_CTX *mem_ctx,
911 struct libnet_Join_member *r)
913 NTSTATUS status;
914 TALLOC_CTX *tmp_mem;
915 struct libnet_JoinDomain *r2;
916 struct provision_store_self_join_settings *set_secrets;
917 uint32_t acct_type = 0;
918 const char *account_name;
919 const char *netbios_name;
920 const char *error_string;
922 r->out.error_string = NULL;
924 tmp_mem = talloc_new(mem_ctx);
925 if (!tmp_mem) {
926 return NT_STATUS_NO_MEMORY;
929 r2 = talloc_zero(tmp_mem, struct libnet_JoinDomain);
930 if (!r2) {
931 r->out.error_string = NULL;
932 talloc_free(tmp_mem);
933 return NT_STATUS_NO_MEMORY;
936 acct_type = ACB_WSTRUST;
938 if (r->in.netbios_name != NULL) {
939 netbios_name = r->in.netbios_name;
940 } else {
941 netbios_name = talloc_strdup(tmp_mem, lpcfg_netbios_name(ctx->lp_ctx));
942 if (!netbios_name) {
943 r->out.error_string = NULL;
944 talloc_free(tmp_mem);
945 return NT_STATUS_NO_MEMORY;
949 account_name = talloc_asprintf(tmp_mem, "%s$", netbios_name);
950 if (!account_name) {
951 r->out.error_string = NULL;
952 talloc_free(tmp_mem);
953 return NT_STATUS_NO_MEMORY;
957 * join the domain
959 r2->in.domain_name = r->in.domain_name;
960 r2->in.account_name = account_name;
961 r2->in.netbios_name = netbios_name;
962 r2->in.level = LIBNET_JOINDOMAIN_AUTOMATIC;
963 r2->in.acct_type = acct_type;
964 r2->in.recreate_account = false;
965 r2->in.account_pass = r->in.account_pass;
966 status = libnet_JoinDomain(ctx, r2, r2);
967 if (!NT_STATUS_IS_OK(status)) {
968 r->out.error_string = talloc_steal(mem_ctx, r2->out.error_string);
969 talloc_free(tmp_mem);
970 return status;
973 set_secrets = talloc(tmp_mem, struct provision_store_self_join_settings);
974 if (!set_secrets) {
975 r->out.error_string = NULL;
976 talloc_free(tmp_mem);
977 return NT_STATUS_NO_MEMORY;
980 ZERO_STRUCTP(set_secrets);
981 set_secrets->domain_name = r2->out.domain_name;
982 set_secrets->realm = r2->out.realm;
983 set_secrets->netbios_name = netbios_name;
984 set_secrets->secure_channel_type = SEC_CHAN_WKSTA;
985 set_secrets->machine_password = r2->out.join_password;
986 set_secrets->key_version_number = r2->out.kvno;
987 set_secrets->domain_sid = r2->out.domain_sid;
989 status = provision_store_self_join(ctx, ctx->lp_ctx, ctx->event_ctx, set_secrets, &error_string);
990 if (!NT_STATUS_IS_OK(status)) {
991 r->out.error_string = talloc_steal(mem_ctx, error_string);
992 talloc_free(tmp_mem);
993 return status;
996 /* move all out parameter to the callers TALLOC_CTX */
997 r->out.error_string = NULL;
998 r->out.join_password = r2->out.join_password;
999 talloc_reparent(r2, mem_ctx, r2->out.join_password);
1000 r->out.domain_sid = r2->out.domain_sid;
1001 talloc_reparent(r2, mem_ctx, r2->out.domain_sid);
1002 r->out.domain_name = r2->out.domain_name;
1003 talloc_reparent(r2, mem_ctx, r2->out.domain_name);
1004 talloc_free(tmp_mem);
1005 return NT_STATUS_OK;