Coverage Report

Created: 2025-07-23 07:04

/src/samba/source3/lib/netapi/joindomain.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *  Unix SMB/CIFS implementation.
3
 *  NetApi Join Support
4
 *  Copyright (C) Guenther Deschner 2007-2008
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 3 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
#include "includes.h"
21
#include "ads.h"
22
#include "librpc/gen_ndr/libnetapi.h"
23
#include "libcli/auth/libcli_auth.h"
24
#include "lib/netapi/netapi.h"
25
#include "lib/netapi/netapi_private.h"
26
#include "lib/netapi/libnetapi.h"
27
#include "librpc/gen_ndr/libnet_join.h"
28
#include "libnet/libnet_join.h"
29
#include "../librpc/gen_ndr/ndr_wkssvc_c.h"
30
#include "rpc_client/cli_pipe.h"
31
#include "secrets.h"
32
#include "libsmb/dsgetdcname.h"
33
#include "../librpc/gen_ndr/ndr_ODJ.h"
34
#include "lib/util/base64.h"
35
#include "libnet/libnet_join_offline.h"
36
#include "libcli/security/dom_sid.h"
37
38
/****************************************************************
39
****************************************************************/
40
41
WERROR NetJoinDomain_l(struct libnetapi_ctx *mem_ctx,
42
           struct NetJoinDomain *r)
43
0
{
44
0
  struct libnet_JoinCtx *j = NULL;
45
0
  struct libnetapi_private_ctx *priv;
46
0
  WERROR werr;
47
48
0
  priv = talloc_get_type_abort(mem_ctx->private_data,
49
0
    struct libnetapi_private_ctx);
50
51
0
  if (!r->in.domain) {
52
0
    return WERR_INVALID_PARAMETER;
53
0
  }
54
55
0
  werr = libnet_init_JoinCtx(mem_ctx, &j);
56
0
  W_ERROR_NOT_OK_RETURN(werr);
57
58
0
  j->in.domain_name = talloc_strdup(mem_ctx, r->in.domain);
59
0
  W_ERROR_HAVE_NO_MEMORY(j->in.domain_name);
60
61
0
  if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
62
0
    NTSTATUS status;
63
0
    struct netr_DsRGetDCNameInfo *info = NULL;
64
0
    const char *dc = NULL;
65
0
    uint32_t flags = DS_DIRECTORY_SERVICE_REQUIRED |
66
0
         DS_WRITABLE_REQUIRED |
67
0
         DS_RETURN_DNS_NAME;
68
0
    status = dsgetdcname(mem_ctx, priv->msg_ctx, r->in.domain,
69
0
             NULL, NULL, flags, &info);
70
0
    if (!NT_STATUS_IS_OK(status)) {
71
0
      libnetapi_set_error_string(mem_ctx,
72
0
        "%s", get_friendly_nt_error_msg(status));
73
0
      return ntstatus_to_werror(status);
74
0
    }
75
76
0
    dc = strip_hostname(info->dc_unc);
77
0
    j->in.dc_name = talloc_strdup(mem_ctx, dc);
78
0
    W_ERROR_HAVE_NO_MEMORY(j->in.dc_name);
79
0
  }
80
81
0
  if (r->in.account_ou) {
82
0
    j->in.account_ou = talloc_strdup(mem_ctx, r->in.account_ou);
83
0
    W_ERROR_HAVE_NO_MEMORY(j->in.account_ou);
84
0
  }
85
86
0
  if (r->in.account != NULL) {
87
0
    NTSTATUS status;
88
89
0
    status = ads_simple_creds(j,
90
0
            r->in.domain,
91
0
            r->in.account,
92
0
            r->in.password,
93
0
            &j->in.admin_credentials);
94
0
    if (!NT_STATUS_IS_OK(status)) {
95
0
      TALLOC_FREE(j);
96
0
      return WERR_NERR_BADUSERNAME;
97
0
    }
98
0
  } else {
99
0
    libnetapi_get_creds(mem_ctx, &j->in.admin_credentials);
100
0
    if (j->in.admin_credentials == NULL) {
101
0
      TALLOC_FREE(j);
102
0
      return WERR_NERR_BADUSERNAME;
103
0
    }
104
0
  }
105
106
0
  j->in.join_flags = r->in.join_flags;
107
0
  j->in.modify_config = true;
108
0
  j->in.debug = true;
109
110
0
  werr = libnet_Join(mem_ctx, j);
111
0
  if (!W_ERROR_IS_OK(werr) && j->out.error_string) {
112
0
    libnetapi_set_error_string(mem_ctx, "%s", j->out.error_string);
113
0
  }
114
0
  TALLOC_FREE(j);
115
116
0
  return werr;
117
0
}
118
119
/****************************************************************
120
****************************************************************/
121
122
WERROR NetJoinDomain_r(struct libnetapi_ctx *ctx,
123
           struct NetJoinDomain *r)
124
0
{
125
0
  struct rpc_pipe_client *pipe_cli = NULL;
126
0
  struct wkssvc_PasswordBuffer *encrypted_password = NULL;
127
0
  NTSTATUS status;
128
0
  WERROR werr;
129
0
  unsigned int old_timeout = 0;
130
0
  struct dcerpc_binding_handle *b;
131
0
  DATA_BLOB session_key;
132
133
0
  if (IS_DC) {
134
0
    return WERR_NERR_SETUPDOMAINCONTROLLER;
135
0
  }
136
137
0
  werr = libnetapi_open_pipe(ctx, r->in.server,
138
0
           &ndr_table_wkssvc,
139
0
           &pipe_cli);
140
0
  if (!W_ERROR_IS_OK(werr)) {
141
0
    goto done;
142
0
  }
143
144
0
  b = pipe_cli->binding_handle;
145
146
0
  if (r->in.password) {
147
148
0
    status = dcerpc_binding_handle_transport_session_key(
149
0
        b, talloc_tos(), &session_key);
150
0
    if (!NT_STATUS_IS_OK(status)) {
151
0
      werr = ntstatus_to_werror(status);
152
0
      goto done;
153
0
    }
154
155
0
    werr = encode_wkssvc_join_password_buffer(ctx,
156
0
                r->in.password,
157
0
                &session_key,
158
0
                &encrypted_password);
159
0
    if (!W_ERROR_IS_OK(werr)) {
160
0
      goto done;
161
0
    }
162
0
  }
163
164
0
  old_timeout = rpccli_set_timeout(pipe_cli, 600000);
165
166
0
  status = dcerpc_wkssvc_NetrJoinDomain2(b, talloc_tos(),
167
0
                 r->in.server,
168
0
                 r->in.domain,
169
0
                 r->in.account_ou,
170
0
                 r->in.account,
171
0
                 encrypted_password,
172
0
                 r->in.join_flags,
173
0
                 &werr);
174
0
  if (!NT_STATUS_IS_OK(status)) {
175
0
    werr = ntstatus_to_werror(status);
176
0
    goto done;
177
0
  }
178
179
0
 done:
180
0
  if (pipe_cli && old_timeout) {
181
0
    rpccli_set_timeout(pipe_cli, old_timeout);
182
0
  }
183
184
0
  return werr;
185
0
}
186
/****************************************************************
187
****************************************************************/
188
189
WERROR NetUnjoinDomain_l(struct libnetapi_ctx *mem_ctx,
190
       struct NetUnjoinDomain *r)
191
0
{
192
0
  struct libnet_UnjoinCtx *u = NULL;
193
0
  struct dom_sid domain_sid;
194
0
  const char *domain = NULL;
195
0
  WERROR werr;
196
0
  struct libnetapi_private_ctx *priv;
197
0
  const char *realm = lp_realm();
198
199
0
  priv = talloc_get_type_abort(mem_ctx->private_data,
200
0
    struct libnetapi_private_ctx);
201
202
0
  if (!secrets_fetch_domain_sid(lp_workgroup(), &domain_sid)) {
203
0
    return WERR_NERR_SETUPNOTJOINED;
204
0
  }
205
206
0
  werr = libnet_init_UnjoinCtx(mem_ctx, &u);
207
0
  W_ERROR_NOT_OK_RETURN(werr);
208
209
0
  if (realm[0] != '\0') {
210
0
    domain = realm;
211
0
  } else {
212
0
    domain = lp_workgroup();
213
0
  }
214
215
0
  if (r->in.server_name) {
216
0
    u->in.dc_name = talloc_strdup(mem_ctx, r->in.server_name);
217
0
    W_ERROR_HAVE_NO_MEMORY(u->in.dc_name);
218
0
  } else {
219
0
    NTSTATUS status;
220
0
    struct netr_DsRGetDCNameInfo *info = NULL;
221
0
    const char *dc = NULL;
222
0
    uint32_t flags = DS_DIRECTORY_SERVICE_REQUIRED |
223
0
         DS_WRITABLE_REQUIRED |
224
0
         DS_RETURN_DNS_NAME;
225
0
    status = dsgetdcname(mem_ctx, priv->msg_ctx, domain,
226
0
             NULL, NULL, flags, &info);
227
0
    if (!NT_STATUS_IS_OK(status)) {
228
0
      libnetapi_set_error_string(mem_ctx,
229
0
        "failed to find DC for domain %s: %s",
230
0
        domain,
231
0
        get_friendly_nt_error_msg(status));
232
0
      return ntstatus_to_werror(status);
233
0
    }
234
235
0
    dc = strip_hostname(info->dc_unc);
236
0
    u->in.dc_name = talloc_strdup(mem_ctx, dc);
237
0
    W_ERROR_HAVE_NO_MEMORY(u->in.dc_name);
238
239
0
    u->in.domain_name = domain;
240
0
  }
241
242
0
  if (r->in.account != NULL) {
243
0
    NTSTATUS status;
244
245
0
    status = ads_simple_creds(u,
246
0
            domain,
247
0
            r->in.account,
248
0
            r->in.password,
249
0
            &u->in.admin_credentials);
250
0
    if (!NT_STATUS_IS_OK(status)) {
251
0
      TALLOC_FREE(u);
252
0
      return WERR_NERR_BADUSERNAME;
253
0
    }
254
0
  } else {
255
0
    libnetapi_get_creds(mem_ctx, &u->in.admin_credentials);
256
0
    if (u->in.admin_credentials == NULL) {
257
0
      TALLOC_FREE(u);
258
0
      return WERR_NERR_BADUSERNAME;
259
0
    }
260
0
  }
261
262
0
  u->in.domain_name = domain;
263
0
  u->in.unjoin_flags = r->in.unjoin_flags;
264
0
  u->in.delete_machine_account = false;
265
0
  u->in.modify_config = true;
266
0
  u->in.debug = true;
267
268
0
  u->in.domain_sid = &domain_sid;
269
270
0
  werr = libnet_Unjoin(mem_ctx, u);
271
0
  if (!W_ERROR_IS_OK(werr) && u->out.error_string) {
272
0
    libnetapi_set_error_string(mem_ctx, "%s", u->out.error_string);
273
0
  }
274
0
  TALLOC_FREE(u);
275
276
0
  return werr;
277
0
}
278
279
/****************************************************************
280
****************************************************************/
281
282
WERROR NetUnjoinDomain_r(struct libnetapi_ctx *ctx,
283
       struct NetUnjoinDomain *r)
284
0
{
285
0
  struct rpc_pipe_client *pipe_cli = NULL;
286
0
  struct wkssvc_PasswordBuffer *encrypted_password = NULL;
287
0
  NTSTATUS status;
288
0
  WERROR werr;
289
0
  unsigned int old_timeout = 0;
290
0
  struct dcerpc_binding_handle *b;
291
0
  DATA_BLOB session_key;
292
293
0
  werr = libnetapi_open_pipe(ctx, r->in.server_name,
294
0
           &ndr_table_wkssvc,
295
0
           &pipe_cli);
296
0
  if (!W_ERROR_IS_OK(werr)) {
297
0
    goto done;
298
0
  }
299
300
0
  b = pipe_cli->binding_handle;
301
302
0
  if (r->in.password) {
303
304
0
    status = dcerpc_binding_handle_transport_session_key(
305
0
        b, talloc_tos(), &session_key);
306
0
    if (!NT_STATUS_IS_OK(status)) {
307
0
      werr = ntstatus_to_werror(status);
308
0
      goto done;
309
0
    }
310
311
0
    werr = encode_wkssvc_join_password_buffer(ctx,
312
0
                r->in.password,
313
0
                &session_key,
314
0
                &encrypted_password);
315
0
    if (!W_ERROR_IS_OK(werr)) {
316
0
      goto done;
317
0
    }
318
0
  }
319
320
0
  old_timeout = rpccli_set_timeout(pipe_cli, 60000);
321
322
0
  status = dcerpc_wkssvc_NetrUnjoinDomain2(b, talloc_tos(),
323
0
             r->in.server_name,
324
0
             r->in.account,
325
0
             encrypted_password,
326
0
             r->in.unjoin_flags,
327
0
             &werr);
328
0
  if (!NT_STATUS_IS_OK(status)) {
329
0
    werr = ntstatus_to_werror(status);
330
0
    goto done;
331
0
  }
332
333
0
 done:
334
0
  if (pipe_cli && old_timeout) {
335
0
    rpccli_set_timeout(pipe_cli, old_timeout);
336
0
  }
337
338
0
  return werr;
339
0
}
340
341
/****************************************************************
342
****************************************************************/
343
344
WERROR NetGetJoinInformation_r(struct libnetapi_ctx *ctx,
345
             struct NetGetJoinInformation *r)
346
0
{
347
0
  struct rpc_pipe_client *pipe_cli = NULL;
348
0
  NTSTATUS status;
349
0
  WERROR werr;
350
0
  const char *buffer = NULL;
351
0
  struct dcerpc_binding_handle *b;
352
353
0
  werr = libnetapi_open_pipe(ctx, r->in.server_name,
354
0
           &ndr_table_wkssvc,
355
0
           &pipe_cli);
356
0
  if (!W_ERROR_IS_OK(werr)) {
357
0
    goto done;
358
0
  }
359
360
0
  b = pipe_cli->binding_handle;
361
362
0
  status = dcerpc_wkssvc_NetrGetJoinInformation(b, talloc_tos(),
363
0
                  r->in.server_name,
364
0
                  &buffer,
365
0
                  (enum wkssvc_NetJoinStatus *)r->out.name_type,
366
0
                  &werr);
367
0
  if (!NT_STATUS_IS_OK(status)) {
368
0
    werr = ntstatus_to_werror(status);
369
0
    goto done;
370
0
  }
371
372
0
  if (!W_ERROR_IS_OK(werr)) {
373
0
    goto done;
374
0
  }
375
376
0
  *r->out.name_buffer = talloc_strdup(ctx, buffer);
377
0
  W_ERROR_HAVE_NO_MEMORY(*r->out.name_buffer);
378
379
0
 done:
380
0
  return werr;
381
0
}
382
383
/****************************************************************
384
****************************************************************/
385
386
WERROR NetGetJoinInformation_l(struct libnetapi_ctx *ctx,
387
             struct NetGetJoinInformation *r)
388
0
{
389
0
  const char *realm = lp_realm();
390
391
0
  if ((lp_security() == SEC_ADS) && realm[0] != '\0') {
392
0
    *r->out.name_buffer = talloc_strdup(ctx, realm);
393
0
  } else {
394
0
    *r->out.name_buffer = talloc_strdup(ctx, lp_workgroup());
395
0
  }
396
0
  if (!*r->out.name_buffer) {
397
0
    return WERR_NOT_ENOUGH_MEMORY;
398
0
  }
399
400
0
  switch (lp_server_role()) {
401
0
    case ROLE_DOMAIN_MEMBER:
402
0
    case ROLE_DOMAIN_PDC:
403
0
    case ROLE_DOMAIN_BDC:
404
0
    case ROLE_IPA_DC:
405
0
      *r->out.name_type = NetSetupDomainName;
406
0
      break;
407
0
    case ROLE_STANDALONE:
408
0
    default:
409
0
      *r->out.name_type = NetSetupWorkgroupName;
410
0
      break;
411
0
  }
412
413
0
  return WERR_OK;
414
0
}
415
416
/****************************************************************
417
****************************************************************/
418
419
WERROR NetGetJoinableOUs_l(struct libnetapi_ctx *ctx,
420
         struct NetGetJoinableOUs *r)
421
0
{
422
0
#ifdef HAVE_ADS
423
0
  TALLOC_CTX *tmp_ctx = talloc_stackframe();
424
0
  WERROR ret;
425
0
  NTSTATUS status;
426
0
  ADS_STATUS ads_status;
427
0
  ADS_STRUCT *ads = NULL;
428
0
  struct cli_credentials *creds = NULL;
429
0
  struct netr_DsRGetDCNameInfo *info = NULL;
430
0
  const char *dc = NULL;
431
0
  uint32_t flags = DS_DIRECTORY_SERVICE_REQUIRED |
432
0
       DS_RETURN_DNS_NAME;
433
0
  struct libnetapi_private_ctx *priv;
434
0
  char **p;
435
0
  size_t s;
436
437
0
  priv = talloc_get_type_abort(ctx->private_data,
438
0
    struct libnetapi_private_ctx);
439
440
0
  status = dsgetdcname(tmp_ctx, priv->msg_ctx, r->in.domain,
441
0
           NULL, NULL, flags, &info);
442
0
  if (!NT_STATUS_IS_OK(status)) {
443
0
    libnetapi_set_error_string(ctx, "%s",
444
0
      get_friendly_nt_error_msg(status));
445
0
    ret = ntstatus_to_werror(status);
446
0
    goto out;
447
0
  }
448
449
0
  dc = strip_hostname(info->dc_unc);
450
451
0
  ads = ads_init(tmp_ctx,
452
0
           info->domain_name,
453
0
           info->domain_name,
454
0
           dc,
455
0
           ADS_SASL_PLAIN);
456
0
  if (!ads) {
457
0
    ret = WERR_GEN_FAILURE;
458
0
    goto out;
459
0
  }
460
461
0
  if (r->in.account != NULL) {
462
0
    status = ads_simple_creds(ads,
463
0
            r->in.domain,
464
0
            r->in.account,
465
0
            r->in.password,
466
0
            &creds);
467
0
    if (!NT_STATUS_IS_OK(status)) {
468
0
      ret = WERR_NERR_DEFAULTJOINREQUIRED;
469
0
      goto out;
470
0
    }
471
0
  } else {
472
0
    libnetapi_get_creds(ctx, &creds);
473
0
  }
474
475
0
  ads_status = ads_connect_creds(ads, creds);
476
0
  if (!ADS_ERR_OK(ads_status)) {
477
0
    ret = WERR_NERR_DEFAULTJOINREQUIRED;
478
0
    goto out;
479
0
  }
480
481
0
  ads_status = ads_get_joinable_ous(ads, ctx, &p, &s);
482
0
  if (!ADS_ERR_OK(ads_status)) {
483
0
    ret = WERR_NERR_DEFAULTJOINREQUIRED;
484
0
    goto out;
485
0
  }
486
0
  *r->out.ous = discard_const_p(const char *, p);
487
0
  *r->out.ou_count = s;
488
489
0
  ret = WERR_OK;
490
0
out:
491
0
  TALLOC_FREE(tmp_ctx);
492
493
0
  return ret;
494
#else
495
  return WERR_NOT_SUPPORTED;
496
#endif
497
0
}
498
499
/****************************************************************
500
****************************************************************/
501
502
WERROR NetGetJoinableOUs_r(struct libnetapi_ctx *ctx,
503
         struct NetGetJoinableOUs *r)
504
0
{
505
0
  struct rpc_pipe_client *pipe_cli = NULL;
506
0
  struct wkssvc_PasswordBuffer *encrypted_password = NULL;
507
0
  NTSTATUS status;
508
0
  WERROR werr;
509
0
  struct dcerpc_binding_handle *b;
510
0
  DATA_BLOB session_key;
511
512
0
  werr = libnetapi_open_pipe(ctx, r->in.server_name,
513
0
           &ndr_table_wkssvc,
514
0
           &pipe_cli);
515
0
  if (!W_ERROR_IS_OK(werr)) {
516
0
    goto done;
517
0
  }
518
519
0
  b = pipe_cli->binding_handle;
520
521
0
  if (r->in.password) {
522
523
0
    status = dcerpc_binding_handle_transport_session_key(
524
0
        b, talloc_tos(), &session_key);
525
0
    if (!NT_STATUS_IS_OK(status)) {
526
0
      werr = ntstatus_to_werror(status);
527
0
      goto done;
528
0
    }
529
530
0
    werr = encode_wkssvc_join_password_buffer(ctx,
531
0
                r->in.password,
532
0
                &session_key,
533
0
                &encrypted_password);
534
0
    if (!W_ERROR_IS_OK(werr)) {
535
0
      goto done;
536
0
    }
537
0
  }
538
539
0
  status = dcerpc_wkssvc_NetrGetJoinableOus2(b, talloc_tos(),
540
0
               r->in.server_name,
541
0
               r->in.domain,
542
0
               r->in.account,
543
0
               encrypted_password,
544
0
               r->out.ou_count,
545
0
               r->out.ous,
546
0
               &werr);
547
0
  if (!NT_STATUS_IS_OK(status)) {
548
0
    werr = ntstatus_to_werror(status);
549
0
    goto done;
550
0
  }
551
552
0
 done:
553
0
  return werr;
554
0
}
555
556
/****************************************************************
557
****************************************************************/
558
559
WERROR NetRenameMachineInDomain_r(struct libnetapi_ctx *ctx,
560
          struct NetRenameMachineInDomain *r)
561
0
{
562
0
  struct rpc_pipe_client *pipe_cli = NULL;
563
0
  struct wkssvc_PasswordBuffer *encrypted_password = NULL;
564
0
  NTSTATUS status;
565
0
  WERROR werr;
566
0
  struct dcerpc_binding_handle *b;
567
0
  DATA_BLOB session_key;
568
569
0
  werr = libnetapi_open_pipe(ctx, r->in.server_name,
570
0
           &ndr_table_wkssvc,
571
0
           &pipe_cli);
572
0
  if (!W_ERROR_IS_OK(werr)) {
573
0
    goto done;
574
0
  }
575
576
0
  b = pipe_cli->binding_handle;
577
578
0
  if (r->in.password) {
579
580
0
    status = dcerpc_binding_handle_transport_session_key(
581
0
        b, talloc_tos(), &session_key);
582
0
    if (!NT_STATUS_IS_OK(status)) {
583
0
      werr = ntstatus_to_werror(status);
584
0
      goto done;
585
0
    }
586
587
0
    werr = encode_wkssvc_join_password_buffer(ctx,
588
0
                r->in.password,
589
0
                &session_key,
590
0
                &encrypted_password);
591
0
    if (!W_ERROR_IS_OK(werr)) {
592
0
      goto done;
593
0
    }
594
0
  }
595
596
0
  status = dcerpc_wkssvc_NetrRenameMachineInDomain2(b, talloc_tos(),
597
0
                r->in.server_name,
598
0
                r->in.new_machine_name,
599
0
                r->in.account,
600
0
                encrypted_password,
601
0
                r->in.rename_options,
602
0
                &werr);
603
0
  if (!NT_STATUS_IS_OK(status)) {
604
0
    werr = ntstatus_to_werror(status);
605
0
    goto done;
606
0
  }
607
608
0
 done:
609
0
  return werr;
610
0
}
611
612
/****************************************************************
613
****************************************************************/
614
615
WERROR NetRenameMachineInDomain_l(struct libnetapi_ctx *ctx,
616
          struct NetRenameMachineInDomain *r)
617
0
{
618
0
  LIBNETAPI_REDIRECT_TO_LOCALHOST(ctx, r, NetRenameMachineInDomain);
619
0
}
620
621
/****************************************************************
622
****************************************************************/
623
624
WERROR NetProvisionComputerAccount_r(struct libnetapi_ctx *ctx,
625
             struct NetProvisionComputerAccount *r)
626
0
{
627
0
  return NetProvisionComputerAccount_l(ctx, r);
628
0
}
629
630
/****************************************************************
631
****************************************************************/
632
633
static WERROR NetProvisionComputerAccount_backend(struct libnetapi_ctx *ctx,
634
              struct NetProvisionComputerAccount *r,
635
              TALLOC_CTX *mem_ctx,
636
              struct ODJ_PROVISION_DATA **p)
637
0
{
638
0
  WERROR werr;
639
0
  struct libnet_JoinCtx *j = NULL;
640
641
0
  werr = libnet_init_JoinCtx(mem_ctx, &j);
642
0
  if (!W_ERROR_IS_OK(werr)) {
643
0
    return werr;
644
0
  }
645
646
0
  j->in.domain_name = talloc_strdup(j, r->in.domain);
647
0
  if (j->in.domain_name == NULL) {
648
0
    talloc_free(j);
649
0
    return WERR_NOT_ENOUGH_MEMORY;
650
0
  }
651
652
0
  talloc_free(discard_const_p(char *, j->in.machine_name));
653
0
  j->in.machine_name = talloc_strdup(j, r->in.machine_name);
654
0
  if (j->in.machine_name == NULL) {
655
0
    talloc_free(j);
656
0
    return WERR_NOT_ENOUGH_MEMORY;
657
0
  }
658
659
0
  if (r->in.dcname) {
660
0
    j->in.dc_name = talloc_strdup(j, r->in.dcname);
661
0
    if (j->in.dc_name == NULL) {
662
0
      talloc_free(j);
663
0
      return WERR_NOT_ENOUGH_MEMORY;
664
0
    }
665
0
  }
666
667
0
  if (r->in.machine_account_ou) {
668
0
    j->in.account_ou = talloc_strdup(j, r->in.machine_account_ou);
669
0
    if (j->in.account_ou == NULL) {
670
0
      talloc_free(j);
671
0
      return WERR_NOT_ENOUGH_MEMORY;
672
0
    }
673
0
  }
674
675
0
  libnetapi_get_creds(ctx, &j->in.admin_credentials);
676
0
  if (j->in.admin_credentials == NULL) {
677
0
    talloc_free(j);
678
0
    return WERR_NERR_BADUSERNAME;
679
0
  }
680
681
0
  j->in.debug = true;
682
0
  j->in.join_flags  = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
683
0
          WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
684
685
0
  if (r->in.options & NETSETUP_PROVISION_REUSE_ACCOUNT) {
686
0
    j->in.join_flags |= WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED;
687
0
  }
688
689
0
  if (r->in.options & NETSETUP_PROVISION_USE_DEFAULT_PASSWORD) {
690
0
    j->in.join_flags |= WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED;
691
0
    j->in.machine_password = talloc_strdup(j, r->in.machine_name);
692
0
    if (j->in.machine_password == NULL) {
693
0
      talloc_free(j);
694
0
      return WERR_NOT_ENOUGH_MEMORY;
695
0
    }
696
0
  }
697
698
0
  j->in.provision_computer_account_only = true;
699
700
0
  werr = libnet_Join(mem_ctx, j);
701
0
  if (!W_ERROR_IS_OK(werr) && j->out.error_string) {
702
0
    libnetapi_set_error_string(ctx, "%s", j->out.error_string);
703
0
    talloc_free(j);
704
0
    return werr;
705
0
  }
706
707
0
  werr = libnet_odj_compose_ODJ_PROVISION_DATA(mem_ctx, j, p);
708
0
  if (!W_ERROR_IS_OK(werr)) {
709
0
    talloc_free(j);
710
0
    return werr;
711
0
  }
712
713
0
  TALLOC_FREE(j);
714
715
0
  return WERR_OK;
716
0
}
717
718
WERROR NetProvisionComputerAccount_l(struct libnetapi_ctx *ctx,
719
             struct NetProvisionComputerAccount *r)
720
0
{
721
0
  WERROR werr;
722
0
  enum ndr_err_code ndr_err;
723
0
  const char *b64_bin_data_str;
724
0
  DATA_BLOB blob;
725
0
  struct ODJ_PROVISION_DATA_serialized_ptr odj_provision_data;
726
0
  struct ODJ_PROVISION_DATA *p;
727
0
  TALLOC_CTX *mem_ctx = talloc_new(ctx);
728
729
0
  if (r->in.provision_bin_data == NULL &&
730
0
      r->in.provision_text_data == NULL) {
731
0
    return WERR_INVALID_PARAMETER;
732
0
  }
733
0
  if (r->in.provision_bin_data != NULL &&
734
0
      r->in.provision_text_data != NULL) {
735
0
    return WERR_INVALID_PARAMETER;
736
0
  }
737
0
  if (r->in.provision_bin_data == NULL &&
738
0
      r->in.provision_bin_data_size != NULL) {
739
0
    return WERR_INVALID_PARAMETER;
740
0
  }
741
0
  if (r->in.provision_bin_data != NULL &&
742
0
     r->in.provision_bin_data_size == NULL) {
743
0
    return WERR_INVALID_PARAMETER;
744
0
  }
745
746
0
  if (r->in.domain == NULL) {
747
0
    return WERR_INVALID_PARAMETER;
748
0
  }
749
750
0
  if (r->in.machine_name == NULL) {
751
0
    return WERR_INVALID_PARAMETER;
752
0
  }
753
754
0
  werr = NetProvisionComputerAccount_backend(ctx, r, mem_ctx, &p);
755
0
  if (!W_ERROR_IS_OK(werr)) {
756
0
    talloc_free(mem_ctx);
757
0
    return werr;
758
0
  }
759
760
0
  ZERO_STRUCT(odj_provision_data);
761
762
0
  odj_provision_data.s.p = p;
763
764
0
  ndr_err = ndr_push_struct_blob(&blob, ctx, &odj_provision_data,
765
0
    (ndr_push_flags_fn_t)ndr_push_ODJ_PROVISION_DATA_serialized_ptr);
766
0
  if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
767
0
    talloc_free(mem_ctx);
768
0
    return W_ERROR(NERR_BadOfflineJoinInfo);
769
0
  }
770
771
0
  talloc_free(mem_ctx);
772
773
0
  if (r->out.provision_text_data != NULL) {
774
0
    b64_bin_data_str = base64_encode_data_blob(ctx, blob);
775
0
    if (b64_bin_data_str == NULL) {
776
0
      return WERR_NOT_ENOUGH_MEMORY;
777
0
    }
778
0
    *r->out.provision_text_data = b64_bin_data_str;
779
0
  }
780
781
0
  if (r->out.provision_bin_data != NULL &&
782
0
      r->out.provision_bin_data_size != NULL) {
783
0
    *r->out.provision_bin_data = blob.data;
784
0
    *r->out.provision_bin_data_size = blob.length;
785
0
  }
786
787
0
  return werr;
788
0
}
789
790
/****************************************************************
791
****************************************************************/
792
793
WERROR NetRequestOfflineDomainJoin_r(struct libnetapi_ctx *ctx,
794
             struct NetRequestOfflineDomainJoin *r)
795
0
{
796
0
  return WERR_NOT_SUPPORTED;
797
0
}
798
799
/****************************************************************
800
****************************************************************/
801
802
static WERROR NetRequestOfflineDomainJoin_backend(struct libnetapi_ctx *ctx,
803
              const struct ODJ_WIN7BLOB *win7blob,
804
              const struct ODJ_PROVISION_DATA *odj_provision_data)
805
0
{
806
0
  struct libnet_JoinCtx *j = NULL;
807
0
  WERROR werr;
808
809
0
  werr = libnet_init_JoinCtx(ctx, &j);
810
0
  if (!W_ERROR_IS_OK(werr)) {
811
0
    return werr;
812
0
  }
813
814
0
  j->in.domain_name = talloc_strdup(j, win7blob->lpDomain);
815
0
  if (j->in.domain_name == NULL) {
816
0
    talloc_free(j);
817
0
    return WERR_NOT_ENOUGH_MEMORY;
818
0
  }
819
820
0
  talloc_free(discard_const_p(char *, j->in.machine_name));
821
0
  j->in.machine_name = talloc_strdup(j, win7blob->lpMachineName);
822
0
  if (j->in.machine_name == NULL) {
823
0
    talloc_free(j);
824
0
    return WERR_NOT_ENOUGH_MEMORY;
825
0
  }
826
827
0
  j->in.machine_password = talloc_strdup(j, win7blob->lpMachinePassword);
828
0
  if (j->in.machine_password == NULL) {
829
0
    talloc_free(j);
830
0
    return WERR_NOT_ENOUGH_MEMORY;
831
0
  }
832
833
0
  j->in.request_offline_join = true;
834
0
  j->in.odj_provision_data = discard_const(odj_provision_data);
835
0
  j->in.debug = true;
836
0
  j->in.join_flags  = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
837
0
          WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED;
838
839
0
  werr = libnet_Join(j, j);
840
0
  if (!W_ERROR_IS_OK(werr)) {
841
0
    if (j->out.error_string != NULL) {
842
0
      libnetapi_set_error_string(ctx, "%s", j->out.error_string);
843
0
    }
844
0
    talloc_free(j);
845
0
    return werr;
846
0
  }
847
848
0
  TALLOC_FREE(j);
849
850
0
  return WERR_OK;
851
0
}
852
853
WERROR NetRequestOfflineDomainJoin_l(struct libnetapi_ctx *ctx,
854
             struct NetRequestOfflineDomainJoin *r)
855
0
{
856
0
  DATA_BLOB blob, blob_base64;
857
0
  enum ndr_err_code ndr_err;
858
0
  struct ODJ_PROVISION_DATA_serialized_ptr odj_provision_data;
859
0
  bool ok;
860
0
  struct ODJ_WIN7BLOB win7blob = { 0 };
861
0
  WERROR werr;
862
863
0
  if (r->in.provision_bin_data == NULL ||
864
0
      r->in.provision_bin_data_size == 0) {
865
0
    return W_ERROR(NERR_NoOfflineJoinInfo);
866
0
  }
867
868
0
  if (r->in.provision_bin_data_size < 2) {
869
0
    return W_ERROR(NERR_BadOfflineJoinInfo);
870
0
  }
871
872
  /*
873
   * Windows produces and consumes UTF16/UCS2 encoded blobs. Check for the
874
   * unicode BOM mark and convert back to UNIX charset if necessary.
875
   */
876
0
  if (r->in.provision_bin_data[0] == 0xff &&
877
0
      r->in.provision_bin_data[1] == 0xfe) {
878
0
    ok = convert_string_talloc(ctx, CH_UTF16LE, CH_UNIX,
879
0
             r->in.provision_bin_data+2,
880
0
             r->in.provision_bin_data_size-2,
881
0
             &blob_base64.data,
882
0
             &blob_base64.length);
883
0
    if (!ok) {
884
0
      return W_ERROR(NERR_BadOfflineJoinInfo);
885
0
    }
886
0
  } else {
887
0
    blob_base64 = data_blob(r->in.provision_bin_data,
888
0
          r->in.provision_bin_data_size);
889
0
  }
890
891
0
  blob = base64_decode_data_blob_talloc(ctx, (const char *)blob_base64.data);
892
893
0
  ndr_err = ndr_pull_struct_blob(&blob, ctx, &odj_provision_data,
894
0
    (ndr_pull_flags_fn_t)ndr_pull_ODJ_PROVISION_DATA_serialized_ptr);
895
0
  if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
896
0
    return W_ERROR(NERR_BadOfflineJoinInfo);
897
0
  }
898
899
0
  if (DEBUGLEVEL >= 10) {
900
0
    NDR_PRINT_DEBUG(ODJ_PROVISION_DATA_serialized_ptr, &odj_provision_data);
901
0
  }
902
903
0
  if (odj_provision_data.s.p->ulVersion != 1) {
904
0
    return W_ERROR(NERR_ProvisioningBlobUnsupported);
905
0
  }
906
907
0
  werr = libnet_odj_find_win7blob(odj_provision_data.s.p, &win7blob);
908
0
  if (!W_ERROR_IS_OK(werr)) {
909
0
    return werr;
910
0
  }
911
912
0
  if (!(r->in.options & NETSETUP_PROVISION_ONLINE_CALLER)) {
913
0
    return WERR_NERR_SETUPNOTJOINED;
914
0
  }
915
916
0
  werr = NetRequestOfflineDomainJoin_backend(ctx,
917
0
               &win7blob,
918
0
               odj_provision_data.s.p);
919
0
  if (!W_ERROR_IS_OK(werr)) {
920
0
    return werr;
921
0
  }
922
923
0
  return W_ERROR(NERR_JoinPerformedMustRestart);
924
0
}
925
926
/****************************************************************
927
****************************************************************/
928
929
WERROR NetComposeOfflineDomainJoin_r(struct libnetapi_ctx *ctx,
930
             struct NetComposeOfflineDomainJoin *r)
931
0
{
932
0
  return WERR_NOT_SUPPORTED;
933
0
}
934
935
/****************************************************************
936
****************************************************************/
937
938
static WERROR NetComposeOfflineDomainJoin_backend(struct libnetapi_ctx *ctx,
939
              struct NetComposeOfflineDomainJoin *r,
940
              TALLOC_CTX *mem_ctx,
941
              struct ODJ_PROVISION_DATA **p)
942
0
{
943
0
  struct libnet_JoinCtx *j = NULL;
944
0
  WERROR werr;
945
946
0
  werr = libnet_init_JoinCtx(ctx, &j);
947
0
  if (!W_ERROR_IS_OK(werr)) {
948
0
    return werr;
949
0
  }
950
951
0
  j->in.domain_name = talloc_strdup(j, r->in.dns_domain_name);
952
0
  if (j->in.domain_name == NULL) {
953
0
    return WERR_NOT_ENOUGH_MEMORY;
954
0
  }
955
956
0
  j->in.dc_name = talloc_strdup(j, r->in.dc_name);
957
0
  W_ERROR_HAVE_NO_MEMORY(j->in.dc_name);
958
959
0
  j->in.machine_password = talloc_strdup(j, r->in.machine_account_password);
960
0
  W_ERROR_HAVE_NO_MEMORY(j->in.machine_password);
961
962
0
  j->out.account_name = talloc_strdup(j, r->in.machine_account_name);
963
0
  W_ERROR_HAVE_NO_MEMORY(j->out.account_name);
964
965
0
  j->out.dns_domain_name = talloc_strdup(j, r->in.dns_domain_name);
966
0
  W_ERROR_HAVE_NO_MEMORY(j->out.dns_domain_name);
967
968
0
  j->out.netbios_domain_name = talloc_strdup(j, r->in.netbios_domain_name);
969
0
  W_ERROR_HAVE_NO_MEMORY(j->out.netbios_domain_name);
970
971
0
  j->out.domain_sid = dom_sid_dup(j, (struct dom_sid *)r->in.domain_sid);
972
0
  W_ERROR_HAVE_NO_MEMORY(j->out.domain_sid);
973
974
0
  j->out.domain_guid = *r->in.domain_guid;
975
976
0
  j->out.forest_name = talloc_strdup(j, r->in.forest_name);
977
0
  W_ERROR_HAVE_NO_MEMORY(j->out.forest_name);
978
979
0
  j->out.domain_is_ad = r->in.domain_is_ad;
980
981
0
  j->out.dcinfo = talloc_zero(j, struct netr_DsRGetDCNameInfo);
982
0
  W_ERROR_HAVE_NO_MEMORY(j->out.dcinfo);
983
984
0
  j->out.dcinfo->dc_unc = talloc_asprintf(j->out.dcinfo, "\\\\%s", r->in.dc_name);
985
0
  W_ERROR_HAVE_NO_MEMORY(j->out.dcinfo->dc_unc);
986
987
0
  j->out.dcinfo->dc_address = talloc_asprintf(j->out.dcinfo, "\\\\%s", r->in.dc_address);
988
0
  W_ERROR_HAVE_NO_MEMORY(j->out.dcinfo->dc_address);
989
990
0
  j->out.dcinfo->dc_address_type = DS_ADDRESS_TYPE_INET;
991
992
0
  j->out.dcinfo->domain_guid = *r->in.domain_guid;
993
994
0
  j->out.dcinfo->domain_name = talloc_strdup(j->out.dcinfo, r->in.dns_domain_name);
995
0
  W_ERROR_HAVE_NO_MEMORY(j->out.dcinfo->domain_name);
996
997
0
  j->out.dcinfo->forest_name = talloc_strdup(j->out.dcinfo, r->in.forest_name);
998
0
  W_ERROR_HAVE_NO_MEMORY(j->out.dcinfo->forest_name);
999
1000
0
  werr = libnet_odj_compose_ODJ_PROVISION_DATA(mem_ctx, j, p);
1001
0
  if (!W_ERROR_IS_OK(werr)) {
1002
0
    return werr;
1003
0
  }
1004
1005
0
  return WERR_OK;
1006
0
}
1007
1008
WERROR NetComposeOfflineDomainJoin_l(struct libnetapi_ctx *ctx,
1009
             struct NetComposeOfflineDomainJoin *r)
1010
0
{
1011
0
  WERROR werr;
1012
0
  enum ndr_err_code ndr_err;
1013
0
  const char *b64_bin_data_str;
1014
0
  DATA_BLOB blob;
1015
0
  struct ODJ_PROVISION_DATA_serialized_ptr odj_compose_data;
1016
0
  struct ODJ_PROVISION_DATA *p;
1017
0
  TALLOC_CTX *tmp_ctx = talloc_stackframe();
1018
1019
0
  if (r->in.compose_bin_data == NULL &&
1020
0
      r->in.compose_text_data == NULL) {
1021
0
    werr = WERR_INVALID_PARAMETER;
1022
0
    goto out;
1023
0
  }
1024
0
  if (r->in.compose_bin_data != NULL &&
1025
0
      r->in.compose_text_data != NULL) {
1026
0
    werr = WERR_INVALID_PARAMETER;
1027
0
    goto out;
1028
0
  }
1029
0
  if (r->in.compose_bin_data == NULL &&
1030
0
      r->in.compose_bin_data_size != NULL) {
1031
0
    werr = WERR_INVALID_PARAMETER;
1032
0
    goto out;
1033
0
  }
1034
0
  if (r->in.compose_bin_data != NULL &&
1035
0
      r->in.compose_bin_data_size == NULL) {
1036
0
    werr = WERR_INVALID_PARAMETER;
1037
0
    goto out;
1038
0
  }
1039
1040
0
  if (r->in.dns_domain_name == NULL) {
1041
0
    werr = WERR_INVALID_PARAMETER;
1042
0
    goto out;
1043
0
  }
1044
1045
0
  if (r->in.netbios_domain_name == NULL) {
1046
0
    werr = WERR_INVALID_PARAMETER;
1047
0
    goto out;
1048
0
  }
1049
1050
0
  if (r->in.domain_sid == NULL) {
1051
0
    werr = WERR_INVALID_PARAMETER;
1052
0
    goto out;
1053
0
  }
1054
1055
0
  if (r->in.domain_guid == NULL) {
1056
0
    werr = WERR_INVALID_PARAMETER;
1057
0
    goto out;
1058
0
  }
1059
1060
0
  if (r->in.forest_name == NULL) {
1061
0
    werr = WERR_INVALID_PARAMETER;
1062
0
    goto out;
1063
0
  }
1064
1065
0
  if (r->in.machine_account_name == NULL) {
1066
0
    werr = WERR_INVALID_PARAMETER;
1067
0
    goto out;
1068
0
  }
1069
1070
0
  if (r->in.machine_account_password == NULL) {
1071
0
    werr = WERR_INVALID_PARAMETER;
1072
0
    goto out;
1073
0
  }
1074
1075
0
  if (r->in.dc_name == NULL) {
1076
0
    werr = WERR_INVALID_PARAMETER;
1077
0
    goto out;
1078
0
  }
1079
1080
0
  if (r->in.dc_address == NULL) {
1081
0
    werr = WERR_INVALID_PARAMETER;
1082
0
    goto out;
1083
0
  }
1084
1085
0
  werr = NetComposeOfflineDomainJoin_backend(ctx, r, tmp_ctx, &p);
1086
0
  if (!W_ERROR_IS_OK(werr)) {
1087
0
    goto out;
1088
0
  }
1089
1090
0
  ZERO_STRUCT(odj_compose_data);
1091
1092
0
  odj_compose_data.s.p = p;
1093
1094
0
  ndr_err = ndr_push_struct_blob(&blob, ctx, &odj_compose_data,
1095
0
    (ndr_push_flags_fn_t)ndr_push_ODJ_PROVISION_DATA_serialized_ptr);
1096
0
  if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1097
0
    werr = W_ERROR(NERR_BadOfflineJoinInfo);
1098
0
    goto out;
1099
0
  }
1100
1101
0
  if (r->out.compose_text_data != NULL) {
1102
0
    b64_bin_data_str = base64_encode_data_blob(ctx, blob);
1103
0
    if (b64_bin_data_str == NULL) {
1104
0
      werr = WERR_NOT_ENOUGH_MEMORY;
1105
0
    }
1106
0
    *r->out.compose_text_data = b64_bin_data_str;
1107
0
  }
1108
1109
0
  if (r->out.compose_bin_data != NULL &&
1110
0
      r->out.compose_bin_data_size != NULL) {
1111
0
    *r->out.compose_bin_data = blob.data;
1112
0
    *r->out.compose_bin_data_size = blob.length;
1113
0
  }
1114
1115
0
  werr = WERR_OK;
1116
0
out:
1117
0
  talloc_free(tmp_ctx);
1118
0
  return werr;
1119
0
}