r15099: An attempt to fix BSD make portability issues. With these changes Samba 4...
[Samba.git] / source4 / libnet / libnet_join.c
blobe50755229dcb6cc1e30ce2a856441e80cc0c248a
1 /*
2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2004
5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
6 Copyright (C) Brad Henry 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "includes.h"
24 #include "libnet/libnet.h"
25 #include "librpc/gen_ndr/ndr_drsuapi_c.h"
26 #include "lib/ldb/include/ldb.h"
27 #include "lib/ldb/include/ldb_errors.h"
28 #include "passdb/secrets.h"
29 #include "dsdb/samdb/samdb.h"
30 #include "db_wrap.h"
31 #include "libcli/security/security.h"
32 #include "auth/credentials/credentials.h"
33 #include "librpc/gen_ndr/ndr_samr_c.h"
34 #include "librpc/gen_ndr/ndr_security.h"
37 * complete a domain join, when joining to a AD domain:
38 * 1.) connect and bind to the DRSUAPI pipe
39 * 2.) do a DsCrackNames() to find the machine account dn
40 * 3.) connect to LDAP
41 * 4.) do an ldap search to find the "msDS-KeyVersionNumber" of the machine account
42 * 5.) set the servicePrincipalName's of the machine account via LDAP, (maybe we should use DsWriteAccountSpn()...)
43 * 6.) do a DsCrackNames() to find the domain dn
44 * 7.) find out Site specific stuff, look at libnet_JoinSite() for details
46 static NTSTATUS libnet_JoinADSDomain(struct libnet_context *ctx, struct libnet_JoinDomain *r)
48 NTSTATUS status;
50 TALLOC_CTX *tmp_ctx;
52 const char *realm = r->out.realm;
54 struct dcerpc_binding *samr_binding = r->out.samr_binding;
56 struct dcerpc_pipe *drsuapi_pipe;
57 struct dcerpc_binding *drsuapi_binding;
58 struct drsuapi_DsBind r_drsuapi_bind;
59 struct drsuapi_DsCrackNames r_crack_names;
60 struct drsuapi_DsNameString names[1];
61 struct policy_handle drsuapi_bind_handle;
62 struct GUID drsuapi_bind_guid;
64 struct ldb_context *remote_ldb;
65 const struct ldb_dn *account_dn;
66 const char *account_dn_str;
67 const char *remote_ldb_url;
68 struct ldb_result *res;
69 struct ldb_message *msg;
71 int ret, rtn;
73 unsigned int kvno;
75 const char * const attrs[] = {
76 "msDS-KeyVersionNumber",
77 "servicePrincipalName",
78 "dNSHostName",
79 NULL,
82 r->out.error_string = NULL;
84 /* We need to convert between a samAccountName and domain to a
85 * DN in the directory. The correct way to do this is with
86 * DRSUAPI CrackNames */
88 /* Fiddle with the bindings, so get to DRSUAPI on
89 * NCACN_IP_TCP, sealed */
90 tmp_ctx = talloc_named(r, 0, "libnet_JoinADSDomain temp context");
91 if (!tmp_ctx) {
92 r->out.error_string = NULL;
93 return NT_STATUS_NO_MEMORY;
96 drsuapi_binding = talloc(tmp_ctx, struct dcerpc_binding);
97 if (!drsuapi_binding) {
98 r->out.error_string = NULL;
99 talloc_free(tmp_ctx);
100 return NT_STATUS_NO_MEMORY;
103 *drsuapi_binding = *samr_binding;
105 /* DRSUAPI is only available on IP_TCP, and locally on NCALRPC */
106 if (drsuapi_binding->transport != NCALRPC) {
107 drsuapi_binding->transport = NCACN_IP_TCP;
109 drsuapi_binding->endpoint = NULL;
110 drsuapi_binding->flags |= DCERPC_SEAL;
112 status = dcerpc_pipe_connect_b(tmp_ctx,
113 &drsuapi_pipe,
114 drsuapi_binding,
115 &dcerpc_table_drsuapi,
116 ctx->cred,
117 ctx->event_ctx);
118 if (!NT_STATUS_IS_OK(status)) {
119 r->out.error_string = talloc_asprintf(r,
120 "Connection to DRSUAPI pipe of PDC of domain '%s' failed: %s",
121 r->in.domain_name,
122 nt_errstr(status));
123 talloc_free(tmp_ctx);
124 return status;
127 /* get a DRSUAPI pipe handle */
128 GUID_from_string(DRSUAPI_DS_BIND_GUID, &drsuapi_bind_guid);
130 r_drsuapi_bind.in.bind_guid = &drsuapi_bind_guid;
131 r_drsuapi_bind.in.bind_info = NULL;
132 r_drsuapi_bind.out.bind_handle = &drsuapi_bind_handle;
134 status = dcerpc_drsuapi_DsBind(drsuapi_pipe, tmp_ctx, &r_drsuapi_bind);
135 if (!NT_STATUS_IS_OK(status)) {
136 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
137 r->out.error_string
138 = talloc_asprintf(r,
139 "dcerpc_drsuapi_DsBind failed - %s\n",
140 dcerpc_errstr(tmp_ctx, drsuapi_pipe->last_fault_code));
141 talloc_free(tmp_ctx);
142 return status;
143 } else {
144 r->out.error_string
145 = talloc_asprintf(r,
146 "dcerpc_drsuapi_DsBind failed - %s\n",
147 nt_errstr(status));
148 talloc_free(tmp_ctx);
149 return status;
151 } else if (!W_ERROR_IS_OK(r_drsuapi_bind.out.result)) {
152 r->out.error_string
153 = talloc_asprintf(r,
154 "DsBind failed - %s\n",
155 win_errstr(r_drsuapi_bind.out.result));
156 talloc_free(tmp_ctx);
157 return NT_STATUS_UNSUCCESSFUL;
160 /* Actually 'crack' the names */
161 ZERO_STRUCT(r_crack_names);
162 r_crack_names.in.bind_handle = &drsuapi_bind_handle;
163 r_crack_names.in.level = 1;
164 r_crack_names.in.req.req1.unknown1 = 0x000004e4;
165 r_crack_names.in.req.req1.unknown2 = 0x00000407;
166 r_crack_names.in.req.req1.count = 1;
167 r_crack_names.in.req.req1.names = names;
168 r_crack_names.in.req.req1.format_flags = DRSUAPI_DS_NAME_FLAG_NO_FLAGS;
169 r_crack_names.in.req.req1.format_offered= DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY;
170 r_crack_names.in.req.req1.format_desired= DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
171 names[0].str = dom_sid_string(tmp_ctx, r->out.account_sid);
172 if (!names[0].str) {
173 r->out.error_string = NULL;
174 talloc_free(tmp_ctx);
175 return NT_STATUS_NO_MEMORY;
178 status = dcerpc_drsuapi_DsCrackNames(drsuapi_pipe, tmp_ctx, &r_crack_names);
179 if (!NT_STATUS_IS_OK(status)) {
180 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
181 r->out.error_string
182 = talloc_asprintf(r,
183 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s\n",
184 names[0].str,
185 dcerpc_errstr(tmp_ctx, drsuapi_pipe->last_fault_code));
186 talloc_free(tmp_ctx);
187 return status;
188 } else {
189 r->out.error_string
190 = talloc_asprintf(r,
191 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s\n",
192 names[0].str,
193 nt_errstr(status));
194 talloc_free(tmp_ctx);
195 return status;
197 } else if (!W_ERROR_IS_OK(r_crack_names.out.result)) {
198 r->out.error_string
199 = talloc_asprintf(r,
200 "DsCrackNames failed - %s\n", win_errstr(r_crack_names.out.result));
201 talloc_free(tmp_ctx);
202 return NT_STATUS_UNSUCCESSFUL;
203 } else if (r_crack_names.out.level != 1
204 || !r_crack_names.out.ctr.ctr1
205 || r_crack_names.out.ctr.ctr1->count != 1
206 || !r_crack_names.out.ctr.ctr1->array[0].result_name
207 || r_crack_names.out.ctr.ctr1->array[0].status != DRSUAPI_DS_NAME_STATUS_OK) {
208 r->out.error_string = talloc_asprintf(r, "DsCrackNames failed\n");
209 talloc_free(tmp_ctx);
210 return NT_STATUS_UNSUCCESSFUL;
213 /* Store the DN of our machine account. */
214 account_dn_str = r_crack_names.out.ctr.ctr1->array[0].result_name;
216 account_dn = ldb_dn_explode(tmp_ctx, account_dn_str);
217 if (!account_dn) {
218 r->out.error_string = talloc_asprintf(r, "Invalid account dn: %s",
219 account_dn_str);
220 talloc_free(tmp_ctx);
221 return NT_STATUS_UNSUCCESSFUL;
224 /* Now we know the user's DN, open with LDAP, read and modify a few things */
226 remote_ldb_url = talloc_asprintf(tmp_ctx, "ldap://%s",
227 drsuapi_binding->host);
228 if (!remote_ldb_url) {
229 r->out.error_string = NULL;
230 talloc_free(tmp_ctx);
231 return NT_STATUS_NO_MEMORY;
234 remote_ldb = ldb_wrap_connect(tmp_ctx, remote_ldb_url,
235 NULL, ctx->cred, 0, NULL);
236 if (!remote_ldb) {
237 r->out.error_string = NULL;
238 talloc_free(tmp_ctx);
239 return NT_STATUS_UNSUCCESSFUL;
242 /* search for the user's record */
243 ret = ldb_search(remote_ldb, account_dn, LDB_SCOPE_BASE,
244 NULL, attrs, &res);
245 if (ret != LDB_SUCCESS || res->count != 1) {
246 r->out.error_string = talloc_asprintf(r, "ldb_search for %s failed - %s\n",
247 account_dn_str, ldb_errstring(remote_ldb));
248 talloc_free(tmp_ctx);
249 return NT_STATUS_UNSUCCESSFUL;
252 /* If we have a kvno recorded in AD, we need it locally as well */
253 kvno = ldb_msg_find_uint(res->msgs[0], "msDS-KeyVersionNumber", 0);
255 /* Prepare a new message, for the modify */
256 msg = ldb_msg_new(tmp_ctx);
257 if (!msg) {
258 r->out.error_string = NULL;
259 talloc_free(tmp_ctx);
260 return NT_STATUS_NO_MEMORY;
262 msg->dn = res->msgs[0]->dn;
265 int i;
266 const char *service_principal_name[6];
267 const char *dns_host_name = strlower_talloc(tmp_ctx,
268 talloc_asprintf(tmp_ctx,
269 "%s.%s",
270 r->in.netbios_name,
271 realm));
273 if (!dns_host_name) {
274 r->out.error_string = NULL;
275 talloc_free(tmp_ctx);
276 return NT_STATUS_NO_MEMORY;
279 service_principal_name[0] = talloc_asprintf(tmp_ctx, "host/%s", dns_host_name);
280 service_principal_name[1] = talloc_asprintf(tmp_ctx, "host/%s", strlower_talloc(tmp_ctx, r->in.netbios_name));
281 service_principal_name[2] = talloc_asprintf(tmp_ctx, "host/%s/%s", dns_host_name, realm);
282 service_principal_name[3] = talloc_asprintf(tmp_ctx, "host/%s/%s", strlower_talloc(tmp_ctx, r->in.netbios_name), realm);
283 service_principal_name[4] = talloc_asprintf(tmp_ctx, "host/%s/%s", dns_host_name, r->out.domain_name);
284 service_principal_name[5] = talloc_asprintf(tmp_ctx, "host/%s/%s", strlower_talloc(tmp_ctx, r->in.netbios_name), r->out.domain_name);
286 for (i=0; i < ARRAY_SIZE(service_principal_name); i++) {
287 if (!service_principal_name[i]) {
288 r->out.error_string = NULL;
289 talloc_free(tmp_ctx);
290 return NT_STATUS_NO_MEMORY;
292 rtn = samdb_msg_add_string(remote_ldb, tmp_ctx, msg, "servicePrincipalName", service_principal_name[i]);
293 if (rtn == -1) {
294 r->out.error_string = NULL;
295 talloc_free(tmp_ctx);
296 return NT_STATUS_NO_MEMORY;
300 rtn = samdb_msg_add_string(remote_ldb, tmp_ctx, msg, "dNSHostName", dns_host_name);
301 if (rtn == -1) {
302 r->out.error_string = NULL;
303 talloc_free(tmp_ctx);
304 return NT_STATUS_NO_MEMORY;
307 rtn = samdb_replace(remote_ldb, tmp_ctx, msg);
308 if (rtn != 0) {
309 r->out.error_string
310 = talloc_asprintf(r,
311 "Failed to replace entries on %s\n",
312 ldb_dn_linearize(tmp_ctx, msg->dn));
313 talloc_free(tmp_ctx);
314 return NT_STATUS_INTERNAL_DB_CORRUPTION;
318 /* DsCrackNames to find out the DN of the domain. */
319 r_crack_names.in.req.req1.format_offered = DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT;
320 r_crack_names.in.req.req1.format_desired = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
321 names[0].str = talloc_asprintf(tmp_ctx, "%s\\", r->out.domain_name);
322 if (!names[0].str) {
323 r->out.error_string = NULL;
324 talloc_free(tmp_ctx);
325 return NT_STATUS_NO_MEMORY;
328 status = dcerpc_drsuapi_DsCrackNames(drsuapi_pipe, tmp_ctx, &r_crack_names);
329 if (!NT_STATUS_IS_OK(status)) {
330 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
331 r->out.error_string
332 = talloc_asprintf(r,
333 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s\n",
334 r->in.domain_name,
335 dcerpc_errstr(tmp_ctx, drsuapi_pipe->last_fault_code));
336 talloc_free(tmp_ctx);
337 return status;
338 } else {
339 r->out.error_string
340 = talloc_asprintf(r,
341 "dcerpc_drsuapi_DsCrackNames for [%s] failed - %s\n",
342 r->in.domain_name,
343 nt_errstr(status));
344 talloc_free(tmp_ctx);
345 return status;
347 } else if (!W_ERROR_IS_OK(r_crack_names.out.result)) {
348 r->out.error_string
349 = talloc_asprintf(r,
350 "DsCrackNames failed - %s\n", win_errstr(r_crack_names.out.result));
351 talloc_free(tmp_ctx);
352 return NT_STATUS_UNSUCCESSFUL;
353 } else if (r_crack_names.out.level != 1
354 || !r_crack_names.out.ctr.ctr1
355 || r_crack_names.out.ctr.ctr1->count != 1
356 || !r_crack_names.out.ctr.ctr1->array[0].result_name
357 || r_crack_names.out.ctr.ctr1->array[0].status != DRSUAPI_DS_NAME_STATUS_OK) {
358 r->out.error_string = talloc_asprintf(r, "DsCrackNames failed\n");
359 talloc_free(tmp_ctx);
360 return NT_STATUS_UNSUCCESSFUL;
363 /* Store the account DN. */
364 r->out.account_dn_str = account_dn_str;
365 talloc_steal(r, account_dn_str);
367 /* Store the domain DN. */
368 r->out.domain_dn_str = r_crack_names.out.ctr.ctr1->array[0].result_name;
369 talloc_steal(r, r_crack_names.out.ctr.ctr1->array[0].result_name);
371 r->out.kvno = kvno;
373 if (r->in.acct_type == ACB_SVRTRUST) {
374 status = libnet_JoinSite(remote_ldb, r);
376 talloc_free(tmp_ctx);
378 return status;
382 * do a domain join using DCERPC/SAMR calls
383 * - connect to the LSA pipe, to try and find out information about the domain
384 * - create a secondary connection to SAMR pipe
385 * - do a samr_Connect to get a policy handle
386 * - do a samr_LookupDomain to get the domain sid
387 * - do a samr_OpenDomain to get a domain handle
388 * - do a samr_CreateAccount to try and get a new account
390 * If that fails, do:
391 * - do a samr_LookupNames to get the users rid
392 * - do a samr_OpenUser to get a user handle
393 * - potentially delete and recreate the user
394 * - assert the account is of the right type with samrQueryUserInfo
396 * - call libnet_SetPassword_samr_handle to set the password
398 * - do a samrSetUserInfo to set the account flags
399 * - do some ADS specific things when we join as Domain Controller,
400 * look at libnet_joinADSDomain() for the details
402 NTSTATUS libnet_JoinDomain(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_JoinDomain *r)
404 TALLOC_CTX *tmp_ctx;
406 NTSTATUS status, cu_status;
408 struct libnet_RpcConnectDCInfo *connect_with_info;
409 struct dcerpc_pipe *samr_pipe;
411 struct samr_Connect sc;
412 struct policy_handle p_handle;
413 struct samr_OpenDomain od;
414 struct policy_handle d_handle;
415 struct samr_LookupNames ln;
416 struct samr_OpenUser ou;
417 struct samr_CreateUser2 cu;
418 struct policy_handle *u_handle = NULL;
419 struct samr_QueryUserInfo qui;
420 struct samr_SetUserInfo sui;
421 union samr_UserInfo u_info;
422 union libnet_SetPassword r2;
423 struct samr_GetUserPwInfo pwp;
424 struct lsa_String samr_account_name;
426 uint32_t acct_flags, old_acct_flags;
427 uint32_t rid, access_granted;
428 int policy_min_pw_len = 0;
430 struct dom_sid *account_sid = NULL;
431 const char *password_str = NULL;
433 r->out.error_string = NULL;
434 r2.samr_handle.out.error_string = NULL;
436 tmp_ctx = talloc_named(mem_ctx, 0, "libnet_Join temp context");
437 if (!tmp_ctx) {
438 r->out.error_string = NULL;
439 return NT_STATUS_NO_MEMORY;
442 u_handle = talloc(tmp_ctx, struct policy_handle);
443 if (!u_handle) {
444 r->out.error_string = NULL;
445 talloc_free(tmp_ctx);
446 return NT_STATUS_NO_MEMORY;
449 connect_with_info = talloc(tmp_ctx, struct libnet_RpcConnectDCInfo);
450 if (!connect_with_info) {
451 r->out.error_string = NULL;
452 talloc_free(tmp_ctx);
453 return NT_STATUS_NO_MEMORY;
456 /* prepare connect to the LSA pipe of PDC */
457 if (r->in.level == LIBNET_JOINDOMAIN_AUTOMATIC) {
458 connect_with_info->level = LIBNET_RPC_CONNECT_PDC;
459 connect_with_info->in.name = r->in.domain_name;
460 } else {
461 connect_with_info->level = LIBNET_RPC_CONNECT_BINDING;
462 connect_with_info->in.binding = r->in.binding;
465 connect_with_info->in.dcerpc_iface = &dcerpc_table_samr;
467 establish a SAMR connection, on the same CIFS transport
470 status = libnet_RpcConnectDCInfo(ctx, connect_with_info);
471 if (!NT_STATUS_IS_OK(status)) {
472 if (r->in.binding) {
473 r->out.error_string = talloc_asprintf(mem_ctx,
474 "Connection to SAMR pipe of DC %s failed: %s",
475 r->in.binding, connect_with_info->out.error_string);
476 } else {
477 r->out.error_string = talloc_asprintf(mem_ctx,
478 "Connection to SAMR pipe of PDC for %s failed: %s",
479 r->in.domain_name, connect_with_info->out.error_string);
481 talloc_free(tmp_ctx);
482 return status;
485 samr_pipe = connect_with_info->out.dcerpc_pipe,
487 status = dcerpc_pipe_auth(samr_pipe,
488 connect_with_info->out.dcerpc_pipe->binding,
489 &dcerpc_table_samr, ctx->cred);
490 if (!NT_STATUS_IS_OK(status)) {
491 r->out.error_string = talloc_asprintf(mem_ctx,
492 "SAMR bind failed: %s",
493 nt_errstr(status));
494 talloc_free(tmp_ctx);
495 return status;
498 /* prepare samr_Connect */
499 ZERO_STRUCT(p_handle);
500 sc.in.system_name = NULL;
501 sc.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
502 sc.out.connect_handle = &p_handle;
504 /* 2. do a samr_Connect to get a policy handle */
505 status = dcerpc_samr_Connect(samr_pipe, tmp_ctx, &sc);
506 if (!NT_STATUS_IS_OK(status)) {
507 r->out.error_string = talloc_asprintf(mem_ctx,
508 "samr_Connect failed: %s",
509 nt_errstr(status));
510 talloc_free(tmp_ctx);
511 return status;
514 /* If this is a connection on ncacn_ip_tcp to Win2k3 SP1, we don't get back this useful info */
515 if (!connect_with_info->out.domain_name) {
516 if (r->in.level == LIBNET_JOINDOMAIN_AUTOMATIC) {
517 connect_with_info->out.domain_name = talloc_strdup(tmp_ctx, r->in.domain_name);
518 } else {
519 /* Bugger, we just lost our way to automaticly find the domain name */
520 connect_with_info->out.domain_name = talloc_strdup(tmp_ctx, lp_workgroup());
524 /* Perhaps we didn't get a SID above, because we are against ncacn_ip_tcp */
525 if (!connect_with_info->out.domain_sid) {
526 struct lsa_String name;
527 struct samr_LookupDomain l;
528 name.string = connect_with_info->out.domain_name;
529 l.in.connect_handle = &p_handle;
530 l.in.domain_name = &name;
532 status = dcerpc_samr_LookupDomain(samr_pipe, tmp_ctx, &l);
533 if (!NT_STATUS_IS_OK(status)) {
534 r->out.error_string = talloc_asprintf(mem_ctx,
535 "SAMR LookupDomain failed: %s",
536 nt_errstr(status));
537 talloc_free(tmp_ctx);
538 return status;
540 connect_with_info->out.domain_sid = l.out.sid;
543 /* prepare samr_OpenDomain */
544 ZERO_STRUCT(d_handle);
545 od.in.connect_handle = &p_handle;
546 od.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
547 od.in.sid = connect_with_info->out.domain_sid;
548 od.out.domain_handle = &d_handle;
550 /* do a samr_OpenDomain to get a domain handle */
551 status = dcerpc_samr_OpenDomain(samr_pipe, tmp_ctx, &od);
552 if (!NT_STATUS_IS_OK(status)) {
553 r->out.error_string = talloc_asprintf(mem_ctx,
554 "samr_OpenDomain for [%s] failed: %s",
555 dom_sid_string(tmp_ctx, connect_with_info->out.domain_sid),
556 nt_errstr(status));
557 talloc_free(tmp_ctx);
558 return status;
561 /* prepare samr_CreateUser2 */
562 ZERO_STRUCTP(u_handle);
563 cu.in.domain_handle = &d_handle;
564 cu.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
565 samr_account_name.string = r->in.account_name;
566 cu.in.account_name = &samr_account_name;
567 cu.in.acct_flags = r->in.acct_type;
568 cu.out.user_handle = u_handle;
569 cu.out.rid = &rid;
570 cu.out.access_granted = &access_granted;
572 /* do a samr_CreateUser2 to get an account handle, or an error */
573 cu_status = dcerpc_samr_CreateUser2(samr_pipe, tmp_ctx, &cu);
574 status = cu_status;
575 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
576 /* prepare samr_LookupNames */
577 ln.in.domain_handle = &d_handle;
578 ln.in.num_names = 1;
579 ln.in.names = talloc_array(tmp_ctx, struct lsa_String, 1);
580 if (!ln.in.names) {
581 r->out.error_string = NULL;
582 talloc_free(tmp_ctx);
583 return NT_STATUS_NO_MEMORY;
585 ln.in.names[0].string = r->in.account_name;
587 /* 5. do a samr_LookupNames to get the users rid */
588 status = dcerpc_samr_LookupNames(samr_pipe, tmp_ctx, &ln);
589 if (!NT_STATUS_IS_OK(status)) {
590 r->out.error_string = talloc_asprintf(mem_ctx,
591 "samr_LookupNames for [%s] failed: %s",
592 r->in.account_name,
593 nt_errstr(status));
594 talloc_free(tmp_ctx);
595 return status;
598 /* check if we got one RID for the user */
599 if (ln.out.rids.count != 1) {
600 r->out.error_string = talloc_asprintf(mem_ctx,
601 "samr_LookupNames for [%s] returns %d RIDs\n",
602 r->in.account_name, ln.out.rids.count);
603 talloc_free(tmp_ctx);
604 return NT_STATUS_INVALID_PARAMETER;
607 /* prepare samr_OpenUser */
608 ZERO_STRUCTP(u_handle);
609 ou.in.domain_handle = &d_handle;
610 ou.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
611 ou.in.rid = ln.out.rids.ids[0];
612 rid = ou.in.rid;
613 ou.out.user_handle = u_handle;
615 /* 6. do a samr_OpenUser to get a user handle */
616 status = dcerpc_samr_OpenUser(samr_pipe, tmp_ctx, &ou);
617 if (!NT_STATUS_IS_OK(status)) {
618 r->out.error_string = talloc_asprintf(mem_ctx,
619 "samr_OpenUser for [%s] failed: %s",
620 r->in.account_name,
621 nt_errstr(status));
622 talloc_free(tmp_ctx);
623 return status;
626 if (r->in.recreate_account) {
627 struct samr_DeleteUser d;
628 d.in.user_handle = u_handle;
629 d.out.user_handle = u_handle;
630 status = dcerpc_samr_DeleteUser(samr_pipe, mem_ctx, &d);
631 if (!NT_STATUS_IS_OK(status)) {
632 r->out.error_string = talloc_asprintf(mem_ctx,
633 "samr_DeleteUser (for recreate) of [%s] failed: %s",
634 r->in.account_name,
635 nt_errstr(status));
636 talloc_free(tmp_ctx);
637 return status;
640 /* We want to recreate, so delete and another samr_CreateUser2 */
642 /* &cu filled in above */
643 status = dcerpc_samr_CreateUser2(samr_pipe, tmp_ctx, &cu);
644 if (!NT_STATUS_IS_OK(status)) {
645 r->out.error_string = talloc_asprintf(mem_ctx,
646 "samr_CreateUser2 (recreate) for [%s] failed: %s\n",
647 r->in.domain_name, nt_errstr(status));
648 talloc_free(tmp_ctx);
649 return status;
652 } else if (!NT_STATUS_IS_OK(status)) {
653 r->out.error_string = talloc_asprintf(mem_ctx,
654 "samr_CreateUser2 for [%s] failed: %s\n",
655 r->in.domain_name, nt_errstr(status));
656 talloc_free(tmp_ctx);
657 return status;
660 /* prepare samr_QueryUserInfo (get flags) */
661 qui.in.user_handle = u_handle;
662 qui.in.level = 16;
664 status = dcerpc_samr_QueryUserInfo(samr_pipe, tmp_ctx, &qui);
665 if (!NT_STATUS_IS_OK(status)) {
666 r->out.error_string = talloc_asprintf(mem_ctx,
667 "samr_QueryUserInfo for [%s] failed: %s",
668 r->in.account_name,
669 nt_errstr(status));
670 talloc_free(tmp_ctx);
671 return status;
674 if (!qui.out.info) {
675 status = NT_STATUS_INVALID_PARAMETER;
676 r->out.error_string
677 = talloc_asprintf(mem_ctx,
678 "samr_QueryUserInfo failed to return qui.out.info for [%s]: %s\n",
679 r->in.account_name, nt_errstr(status));
680 talloc_free(tmp_ctx);
681 return status;
684 old_acct_flags = (qui.out.info->info16.acct_flags & (ACB_WSTRUST | ACB_SVRTRUST | ACB_DOMTRUST));
685 /* Possibly bail if the account is of the wrong type */
686 if (old_acct_flags
687 != r->in.acct_type) {
688 const char *old_account_type, *new_account_type;
689 switch (old_acct_flags) {
690 case ACB_WSTRUST:
691 old_account_type = "domain member (member)";
692 break;
693 case ACB_SVRTRUST:
694 old_account_type = "domain controller (bdc)";
695 break;
696 case ACB_DOMTRUST:
697 old_account_type = "trusted domain";
698 break;
699 default:
700 return NT_STATUS_INVALID_PARAMETER;
702 switch (r->in.acct_type) {
703 case ACB_WSTRUST:
704 new_account_type = "domain member (member)";
705 break;
706 case ACB_SVRTRUST:
707 new_account_type = "domain controller (bdc)";
708 break;
709 case ACB_DOMTRUST:
710 new_account_type = "trusted domain";
711 break;
712 default:
713 return NT_STATUS_INVALID_PARAMETER;
716 if (!NT_STATUS_EQUAL(cu_status, NT_STATUS_USER_EXISTS)) {
717 /* We created a new user, but they didn't come out the right type?!? */
718 r->out.error_string
719 = talloc_asprintf(mem_ctx,
720 "We asked to create a new machine account (%s) of type %s, "
721 "but we got an account of type %s. This is unexpected. "
722 "Perhaps delete the account and try again.\n",
723 r->in.account_name, new_account_type, old_account_type);
724 talloc_free(tmp_ctx);
725 return NT_STATUS_INVALID_PARAMETER;
726 } else {
727 /* The account is of the wrong type, so bail */
729 /* TODO: We should allow a --force option to override, and redo this from the top setting r.in.recreate_account */
730 r->out.error_string
731 = talloc_asprintf(mem_ctx,
732 "The machine account (%s) already exists in the domain %s, "
733 "but is a %s. You asked to join as a %s. Please delete "
734 "the account and try again.\n",
735 r->in.account_name, connect_with_info->out.domain_name, old_account_type, new_account_type);
736 talloc_free(tmp_ctx);
737 return NT_STATUS_USER_EXISTS;
739 } else {
740 acct_flags = qui.out.info->info16.acct_flags;
743 acct_flags = (acct_flags & ~ACB_DISABLED);
745 /* Find out what password policy this user has */
746 pwp.in.user_handle = u_handle;
748 status = dcerpc_samr_GetUserPwInfo(samr_pipe, tmp_ctx, &pwp);
749 if (NT_STATUS_IS_OK(status)) {
750 policy_min_pw_len = pwp.out.info.min_password_length;
753 /* Grab a password of that minimum length */
755 password_str = generate_random_str(tmp_ctx, MAX(8, policy_min_pw_len));
757 r2.samr_handle.level = LIBNET_SET_PASSWORD_SAMR_HANDLE;
758 r2.samr_handle.in.account_name = r->in.account_name;
759 r2.samr_handle.in.newpassword = password_str;
760 r2.samr_handle.in.user_handle = u_handle;
761 r2.samr_handle.in.dcerpc_pipe = samr_pipe;
763 status = libnet_SetPassword(ctx, tmp_ctx, &r2);
764 if (!NT_STATUS_IS_OK(status)) {
765 r->out.error_string = talloc_steal(mem_ctx, r2.samr_handle.out.error_string);
766 talloc_free(tmp_ctx);
767 return status;
770 /* reset flags (if required) */
771 if (acct_flags != qui.out.info->info16.acct_flags) {
772 ZERO_STRUCT(u_info);
773 u_info.info16.acct_flags = acct_flags;
775 sui.in.user_handle = u_handle;
776 sui.in.info = &u_info;
777 sui.in.level = 16;
779 dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
780 if (!NT_STATUS_IS_OK(status)) {
781 r->out.error_string = talloc_asprintf(mem_ctx,
782 "samr_SetUserInfo for [%s] failed to remove ACB_DISABLED flag: %s",
783 r->in.account_name,
784 nt_errstr(status));
785 talloc_free(tmp_ctx);
786 return status;
790 account_sid = dom_sid_add_rid(mem_ctx, connect_with_info->out.domain_sid, rid);
791 if (!account_sid) {
792 r->out.error_string = NULL;
793 talloc_free(tmp_ctx);
794 return NT_STATUS_NO_MEMORY;
797 /* Finish out by pushing various bits of status data out for the caller to use */
798 r->out.join_password = password_str;
799 talloc_steal(mem_ctx, r->out.join_password);
801 r->out.domain_sid = connect_with_info->out.domain_sid;
802 talloc_steal(mem_ctx, r->out.domain_sid);
804 r->out.account_sid = account_sid;
805 talloc_steal(mem_ctx, r->out.account_sid);
807 r->out.domain_name = connect_with_info->out.domain_name;
808 talloc_steal(mem_ctx, r->out.domain_name);
809 r->out.realm = connect_with_info->out.realm;
810 talloc_steal(mem_ctx, r->out.realm);
811 r->out.samr_pipe = samr_pipe;
812 talloc_steal(mem_ctx, samr_pipe);
813 r->out.samr_binding = samr_pipe->binding;
814 talloc_steal(mem_ctx, r->out.samr_binding);
815 r->out.user_handle = u_handle;
816 talloc_steal(mem_ctx, u_handle);
817 r->out.error_string = r2.samr_handle.out.error_string;
818 talloc_steal(mem_ctx, r2.samr_handle.out.error_string);
819 r->out.kvno = 0;
820 r->out.server_dn_str = NULL;
821 talloc_free(tmp_ctx);
823 /* Now, if it was AD, then we want to start looking changing a
824 * few more things. Otherwise, we are done. */
825 if (r->out.realm) {
826 status = libnet_JoinADSDomain(ctx, r);
827 return status;
830 return status;
833 static NTSTATUS libnet_Join_primary_domain(struct libnet_context *ctx,
834 TALLOC_CTX *mem_ctx,
835 struct libnet_Join *r)
837 NTSTATUS status;
838 TALLOC_CTX *tmp_mem;
839 struct libnet_JoinDomain *r2;
840 int ret, rtn;
841 struct ldb_context *ldb;
842 const struct ldb_dn *base_dn;
843 struct ldb_message **msgs, *msg;
844 const char *sct;
845 const char * const attrs[] = {
846 "whenChanged",
847 "secret",
848 "priorSecret",
849 "priorChanged",
850 "krb5Keytab",
851 "privateKeytab",
852 NULL
854 uint32_t acct_type = 0;
855 const char *account_name;
856 const char *netbios_name;
857 char *filter;
859 r->out.error_string = NULL;
861 tmp_mem = talloc_new(mem_ctx);
862 if (!tmp_mem) {
863 return NT_STATUS_NO_MEMORY;
866 r2 = talloc(tmp_mem, struct libnet_JoinDomain);
867 if (!r2) {
868 r->out.error_string = NULL;
869 talloc_free(tmp_mem);
870 return NT_STATUS_NO_MEMORY;
873 if (r->in.join_type == SEC_CHAN_BDC) {
874 acct_type = ACB_SVRTRUST;
875 } else if (r->in.join_type == SEC_CHAN_WKSTA) {
876 acct_type = ACB_WSTRUST;
877 } else {
878 r->out.error_string = NULL;
879 talloc_free(tmp_mem);
880 return NT_STATUS_INVALID_PARAMETER;
883 if (r->in.netbios_name != NULL) {
884 netbios_name = r->in.netbios_name;
885 } else {
886 netbios_name = talloc_reference(tmp_mem, lp_netbios_name());
887 if (!netbios_name) {
888 r->out.error_string = NULL;
889 talloc_free(tmp_mem);
890 return NT_STATUS_NO_MEMORY;
894 account_name = talloc_asprintf(tmp_mem, "%s$", netbios_name);
895 if (!account_name) {
896 r->out.error_string = NULL;
897 talloc_free(tmp_mem);
898 return NT_STATUS_NO_MEMORY;
902 * Local secrets are stored in secrets.ldb
903 * open it to make sure we can write the info into it after the join
905 ldb = secrets_db_connect(tmp_mem);
906 if (!ldb) {
907 r->out.error_string
908 = talloc_asprintf(mem_ctx,
909 "Could not open secrets database\n");
910 talloc_free(tmp_mem);
911 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
915 * join the domain
917 ZERO_STRUCTP(r2);
918 r2->in.domain_name = r->in.domain_name;
919 r2->in.account_name = account_name;
920 r2->in.netbios_name = netbios_name;
921 r2->in.level = LIBNET_JOINDOMAIN_AUTOMATIC;
922 r2->in.acct_type = acct_type;
923 r2->in.recreate_account = False;
924 status = libnet_JoinDomain(ctx, r2, r2);
925 if (!NT_STATUS_IS_OK(status)) {
926 r->out.error_string = talloc_steal(mem_ctx, r2->out.error_string);
927 talloc_free(tmp_mem);
928 return status;
932 * now prepare the record for secrets.ldb
934 sct = talloc_asprintf(tmp_mem, "%d", r->in.join_type);
935 if (!sct) {
936 r->out.error_string = NULL;
937 talloc_free(tmp_mem);
938 return NT_STATUS_NO_MEMORY;
941 msg = ldb_msg_new(tmp_mem);
942 if (!msg) {
943 r->out.error_string = NULL;
944 talloc_free(tmp_mem);
945 return NT_STATUS_NO_MEMORY;
948 base_dn = ldb_dn_explode(tmp_mem, "cn=Primary Domains");
949 if (!base_dn) {
950 r->out.error_string = NULL;
951 talloc_free(tmp_mem);
952 return NT_STATUS_NO_MEMORY;
955 msg->dn = ldb_dn_build_child(tmp_mem, "flatname", r2->out.domain_name, base_dn);
956 if (!msg->dn) {
957 r->out.error_string = NULL;
958 talloc_free(tmp_mem);
959 return NT_STATUS_NO_MEMORY;
962 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "flatname", r2->out.domain_name);
963 if (rtn == -1) {
964 r->out.error_string = NULL;
965 talloc_free(tmp_mem);
966 return NT_STATUS_NO_MEMORY;
969 if (r2->out.realm) {
970 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "realm", r2->out.realm);
971 if (rtn == -1) {
972 r->out.error_string = NULL;
973 talloc_free(tmp_mem);
974 return NT_STATUS_NO_MEMORY;
977 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "objectClass", "primaryDomain");
978 if (rtn == -1) {
979 r->out.error_string = NULL;
980 talloc_free(tmp_mem);
981 return NT_STATUS_NO_MEMORY;
985 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "objectClass", "primaryDomain");
986 if (rtn == -1) {
987 r->out.error_string = NULL;
988 talloc_free(tmp_mem);
989 return NT_STATUS_NO_MEMORY;
992 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "secret", r2->out.join_password);
993 if (rtn == -1) {
994 r->out.error_string = NULL;
995 talloc_free(tmp_mem);
996 return NT_STATUS_NO_MEMORY;
999 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "samAccountName", r2->in.account_name);
1000 if (rtn == -1) {
1001 r->out.error_string = NULL;
1002 talloc_free(tmp_mem);
1003 return NT_STATUS_NO_MEMORY;
1006 rtn = samdb_msg_add_string(ldb, tmp_mem, msg, "secureChannelType", sct);
1007 if (rtn == -1) {
1008 r->out.error_string = NULL;
1009 talloc_free(tmp_mem);
1010 return NT_STATUS_NO_MEMORY;
1013 if (r2->out.kvno) {
1014 rtn = samdb_msg_add_uint(ldb, tmp_mem, msg, "msDS-KeyVersionNumber",
1015 r2->out.kvno);
1016 if (rtn == -1) {
1017 r->out.error_string = NULL;
1018 talloc_free(tmp_mem);
1019 return NT_STATUS_NO_MEMORY;
1023 if (r2->out.domain_sid) {
1024 rtn = samdb_msg_add_dom_sid(ldb, tmp_mem, msg, "objectSid",
1025 r2->out.domain_sid);
1026 if (rtn == -1) {
1027 r->out.error_string = NULL;
1028 talloc_free(tmp_mem);
1029 return NT_STATUS_NO_MEMORY;
1034 * search for the secret record
1035 * - remove the records we find
1036 * - and fetch the old secret and store it under priorSecret
1038 ret = gendb_search(ldb,
1039 tmp_mem, base_dn,
1040 &msgs, attrs,
1041 "(|" SECRETS_PRIMARY_DOMAIN_FILTER "(realm=%s))",
1042 r2->out.domain_name, r2->out.realm);
1043 if (ret == 0) {
1044 rtn = samdb_msg_set_string(ldb, tmp_mem, msg, "secretsKeytab", "secrets.keytab");
1045 if (rtn == -1) {
1046 r->out.error_string = NULL;
1047 talloc_free(tmp_mem);
1048 return NT_STATUS_NO_MEMORY;
1050 } else if (ret == -1) {
1051 r->out.error_string
1052 = talloc_asprintf(mem_ctx,
1053 "Search for domain: %s and realm: %s failed: %s",
1054 r2->out.domain_name, r2->out.realm, ldb_errstring(ldb));
1055 talloc_free(tmp_mem);
1056 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1057 } else {
1058 const struct ldb_val *private_keytab;
1059 const struct ldb_val *krb5_keytab;
1060 const struct ldb_val *prior_secret;
1061 const struct ldb_val *prior_modified_time;
1062 int i;
1064 for (i = 0; i < ret; i++) {
1065 ldb_delete(ldb, msgs[i]->dn);
1068 prior_secret = ldb_msg_find_ldb_val(msgs[0], "secret");
1069 if (prior_secret) {
1070 rtn = samdb_msg_set_value(ldb, tmp_mem, msg, "priorSecret", prior_secret);
1071 if (rtn == -1) {
1072 r->out.error_string = NULL;
1073 talloc_free(tmp_mem);
1074 return NT_STATUS_NO_MEMORY;
1077 rtn = samdb_msg_set_string(ldb, tmp_mem, msg, "secret", r2->out.join_password);
1078 if (rtn == -1) {
1079 r->out.error_string = NULL;
1080 talloc_free(tmp_mem);
1081 return NT_STATUS_NO_MEMORY;
1084 prior_modified_time = ldb_msg_find_ldb_val(msgs[0],
1085 "whenChanged");
1086 if (prior_modified_time) {
1087 rtn = samdb_msg_set_value(ldb, tmp_mem, msg, "priorWhenChanged",
1088 prior_modified_time);
1089 if (rtn == -1) {
1090 r->out.error_string = NULL;
1091 talloc_free(tmp_mem);
1092 return NT_STATUS_NO_MEMORY;
1096 rtn = samdb_msg_set_string(ldb, tmp_mem, msg, "samAccountName", r2->in.account_name);
1097 if (rtn == -1) {
1098 r->out.error_string = NULL;
1099 talloc_free(tmp_mem);
1100 return NT_STATUS_NO_MEMORY;
1103 rtn = samdb_msg_set_string(ldb, tmp_mem, msg, "secureChannelType", sct);
1104 if (rtn == -1) {
1105 r->out.error_string = NULL;
1106 talloc_free(tmp_mem);
1107 return NT_STATUS_NO_MEMORY;
1110 /* We will want to keep the keytab names */
1111 private_keytab = ldb_msg_find_ldb_val(msgs[0], "privateKeytab");
1112 if (private_keytab) {
1113 rtn = samdb_msg_set_value(ldb, tmp_mem, msg, "privateKeytab", private_keytab);
1114 if (rtn == -1) {
1115 r->out.error_string = NULL;
1116 talloc_free(tmp_mem);
1117 return NT_STATUS_NO_MEMORY;
1120 krb5_keytab = ldb_msg_find_ldb_val(msgs[0], "krb5Keytab");
1121 if (krb5_keytab) {
1122 rtn = samdb_msg_set_value(ldb, tmp_mem, msg, "krb5Keytab", krb5_keytab);
1123 if (rtn == -1) {
1124 r->out.error_string = NULL;
1125 talloc_free(tmp_mem);
1126 return NT_STATUS_NO_MEMORY;
1131 /* create the secret */
1132 ret = samdb_add(ldb, tmp_mem, msg);
1133 if (ret != 0) {
1134 r->out.error_string = talloc_asprintf(mem_ctx, "Failed to create secret record %s\n",
1135 ldb_dn_linearize(ldb, msg->dn));
1136 talloc_free(tmp_mem);
1137 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1140 if (r2->out.realm) {
1141 struct cli_credentials *creds;
1142 /* Make a credentials structure from it */
1143 creds = cli_credentials_init(mem_ctx);
1144 if (!creds) {
1145 r->out.error_string = NULL;
1146 talloc_free(tmp_mem);
1147 return NT_STATUS_NO_MEMORY;
1149 cli_credentials_set_conf(creds);
1150 filter = talloc_asprintf(mem_ctx, "dn=%s", ldb_dn_linearize(mem_ctx, msg->dn));
1151 status = cli_credentials_set_secrets(creds, NULL, filter);
1152 if (!NT_STATUS_IS_OK(status)) {
1153 r->out.error_string = talloc_asprintf(mem_ctx, "Failed to read secrets for keytab update for %s\n",
1154 filter);
1155 talloc_free(tmp_mem);
1156 return status;
1158 ret = cli_credentials_update_keytab(creds);
1159 if (ret != 0) {
1160 r->out.error_string = talloc_asprintf(mem_ctx, "Failed to update keytab for %s\n",
1161 filter);
1162 talloc_free(tmp_mem);
1163 return NT_STATUS_UNSUCCESSFUL;
1167 /* move all out parameter to the callers TALLOC_CTX */
1168 r->out.error_string = NULL;
1169 r->out.join_password = r2->out.join_password;
1170 talloc_steal(mem_ctx, r2->out.join_password);
1171 r->out.domain_sid = r2->out.domain_sid;
1172 talloc_steal(mem_ctx, r2->out.domain_sid);
1173 r->out.domain_name = r2->out.domain_name;
1174 talloc_steal(mem_ctx, r2->out.domain_name);
1175 talloc_free(tmp_mem);
1176 return NT_STATUS_OK;
1179 NTSTATUS libnet_Join(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_Join *r)
1181 switch (r->in.join_type) {
1182 case SEC_CHAN_WKSTA:
1183 return libnet_Join_primary_domain(ctx, mem_ctx, r);
1184 case SEC_CHAN_BDC:
1185 return libnet_Join_primary_domain(ctx, mem_ctx, r);
1186 case SEC_CHAN_DOMAIN:
1187 break;
1190 r->out.error_string = talloc_asprintf(mem_ctx,
1191 "Invalid join type specified (%08X) attempting to join domain %s",
1192 r->in.join_type, r->in.domain_name);
1193 return NT_STATUS_INVALID_PARAMETER;