s3: Fix 'net rpc join' for users with the SeMachineAccountPrivilege.
[samba.git] / source3 / libnet / libnet_join.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  libnet Join Support
4  *  Copyright (C) Gerald (Jerry) Carter 2006
5  *  Copyright (C) Guenther Deschner 2007-2008
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "includes.h"
22 #include "libnet/libnet.h"
23
24 /****************************************************************
25 ****************************************************************/
26
27 #define LIBNET_JOIN_DUMP_CTX(ctx, r, f) \
28         do { \
29                 char *str = NULL; \
30                 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_JoinCtx, f, r); \
31                 DEBUG(1,("libnet_Join:\n%s", str)); \
32                 TALLOC_FREE(str); \
33         } while (0)
34
35 #define LIBNET_JOIN_IN_DUMP_CTX(ctx, r) \
36         LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
37 #define LIBNET_JOIN_OUT_DUMP_CTX(ctx, r) \
38         LIBNET_JOIN_DUMP_CTX(ctx, r, NDR_OUT)
39
40 #define LIBNET_UNJOIN_DUMP_CTX(ctx, r, f) \
41         do { \
42                 char *str = NULL; \
43                 str = NDR_PRINT_FUNCTION_STRING(ctx, libnet_UnjoinCtx, f, r); \
44                 DEBUG(1,("libnet_Unjoin:\n%s", str)); \
45                 TALLOC_FREE(str); \
46         } while (0)
47
48 #define LIBNET_UNJOIN_IN_DUMP_CTX(ctx, r) \
49         LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_IN | NDR_SET_VALUES)
50 #define LIBNET_UNJOIN_OUT_DUMP_CTX(ctx, r) \
51         LIBNET_UNJOIN_DUMP_CTX(ctx, r, NDR_OUT)
52
53 #define W_ERROR_NOT_OK_GOTO_DONE(x) do { \
54         if (!W_ERROR_IS_OK(x)) {\
55                 goto done;\
56         }\
57 } while (0)
58
59 /****************************************************************
60 ****************************************************************/
61
62 static void libnet_join_set_error_string(TALLOC_CTX *mem_ctx,
63                                          struct libnet_JoinCtx *r,
64                                          const char *format, ...)
65 {
66         va_list args;
67
68         if (r->out.error_string) {
69                 return;
70         }
71
72         va_start(args, format);
73         r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
74         va_end(args);
75 }
76
77 /****************************************************************
78 ****************************************************************/
79
80 static void libnet_unjoin_set_error_string(TALLOC_CTX *mem_ctx,
81                                            struct libnet_UnjoinCtx *r,
82                                            const char *format, ...)
83 {
84         va_list args;
85
86         if (r->out.error_string) {
87                 return;
88         }
89
90         va_start(args, format);
91         r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
92         va_end(args);
93 }
94
95 #ifdef WITH_ADS
96
97 /****************************************************************
98 ****************************************************************/
99
100 static ADS_STATUS libnet_connect_ads(const char *dns_domain_name,
101                                      const char *netbios_domain_name,
102                                      const char *dc_name,
103                                      const char *user_name,
104                                      const char *password,
105                                      ADS_STRUCT **ads)
106 {
107         ADS_STATUS status;
108         ADS_STRUCT *my_ads = NULL;
109
110         my_ads = ads_init(dns_domain_name,
111                           netbios_domain_name,
112                           dc_name);
113         if (!my_ads) {
114                 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
115         }
116
117         if (user_name) {
118                 SAFE_FREE(my_ads->auth.user_name);
119                 my_ads->auth.user_name = SMB_STRDUP(user_name);
120         }
121
122         if (password) {
123                 SAFE_FREE(my_ads->auth.password);
124                 my_ads->auth.password = SMB_STRDUP(password);
125         }
126
127         status = ads_connect_user_creds(my_ads);
128         if (!ADS_ERR_OK(status)) {
129                 ads_destroy(&my_ads);
130                 return status;
131         }
132
133         *ads = my_ads;
134         return ADS_SUCCESS;
135 }
136
137 /****************************************************************
138 ****************************************************************/
139
140 static ADS_STATUS libnet_join_connect_ads(TALLOC_CTX *mem_ctx,
141                                           struct libnet_JoinCtx *r)
142 {
143         ADS_STATUS status;
144
145         status = libnet_connect_ads(r->out.dns_domain_name,
146                                     r->out.netbios_domain_name,
147                                     r->in.dc_name,
148                                     r->in.admin_account,
149                                     r->in.admin_password,
150                                     &r->in.ads);
151         if (!ADS_ERR_OK(status)) {
152                 libnet_join_set_error_string(mem_ctx, r,
153                         "failed to connect to AD: %s",
154                         ads_errstr(status));
155                 return status;
156         }
157
158         if (!r->out.netbios_domain_name) {
159                 r->out.netbios_domain_name = talloc_strdup(mem_ctx,
160                                                            r->in.ads->server.workgroup);
161                 ADS_ERROR_HAVE_NO_MEMORY(r->out.netbios_domain_name);
162         }
163
164         if (!r->out.dns_domain_name) {
165                 r->out.dns_domain_name = talloc_strdup(mem_ctx,
166                                                        r->in.ads->config.realm);
167                 ADS_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
168         }
169
170         r->out.domain_is_ad = true;
171
172         return ADS_SUCCESS;
173 }
174
175 /****************************************************************
176 ****************************************************************/
177
178 static ADS_STATUS libnet_unjoin_connect_ads(TALLOC_CTX *mem_ctx,
179                                             struct libnet_UnjoinCtx *r)
180 {
181         ADS_STATUS status;
182
183         status = libnet_connect_ads(r->in.domain_name,
184                                     r->in.domain_name,
185                                     r->in.dc_name,
186                                     r->in.admin_account,
187                                     r->in.admin_password,
188                                     &r->in.ads);
189         if (!ADS_ERR_OK(status)) {
190                 libnet_unjoin_set_error_string(mem_ctx, r,
191                         "failed to connect to AD: %s",
192                         ads_errstr(status));
193         }
194
195         return status;
196 }
197
198 /****************************************************************
199  join a domain using ADS (LDAP mods)
200 ****************************************************************/
201
202 static ADS_STATUS libnet_join_precreate_machine_acct(TALLOC_CTX *mem_ctx,
203                                                      struct libnet_JoinCtx *r)
204 {
205         ADS_STATUS status;
206         LDAPMessage *res = NULL;
207         const char *attrs[] = { "dn", NULL };
208         bool moved = false;
209
210         status = ads_check_ou_dn(mem_ctx, r->in.ads, &r->in.account_ou);
211         if (!ADS_ERR_OK(status)) {
212                 return status;
213         }
214
215         status = ads_search_dn(r->in.ads, &res, r->in.account_ou, attrs);
216         if (!ADS_ERR_OK(status)) {
217                 return status;
218         }
219
220         if (ads_count_replies(r->in.ads, res) != 1) {
221                 ads_msgfree(r->in.ads, res);
222                 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT);
223         }
224
225         ads_msgfree(r->in.ads, res);
226
227         /* Attempt to create the machine account and bail if this fails.
228            Assume that the admin wants exactly what they requested */
229
230         status = ads_create_machine_acct(r->in.ads,
231                                          r->in.machine_name,
232                                          r->in.account_ou);
233
234         if (ADS_ERR_OK(status)) {
235                 DEBUG(1,("machine account creation created\n"));
236                 return status;
237         } else  if ((status.error_type == ENUM_ADS_ERROR_LDAP) &&
238                     (status.err.rc == LDAP_ALREADY_EXISTS)) {
239                 status = ADS_SUCCESS;
240         }
241
242         if (!ADS_ERR_OK(status)) {
243                 DEBUG(1,("machine account creation failed\n"));
244                 return status;
245         }
246
247         status = ads_move_machine_acct(r->in.ads,
248                                        r->in.machine_name,
249                                        r->in.account_ou,
250                                        &moved);
251         if (!ADS_ERR_OK(status)) {
252                 DEBUG(1,("failure to locate/move pre-existing "
253                         "machine account\n"));
254                 return status;
255         }
256
257         DEBUG(1,("The machine account %s the specified OU.\n",
258                 moved ? "was moved into" : "already exists in"));
259
260         return status;
261 }
262
263 /****************************************************************
264 ****************************************************************/
265
266 static ADS_STATUS libnet_unjoin_remove_machine_acct(TALLOC_CTX *mem_ctx,
267                                                     struct libnet_UnjoinCtx *r)
268 {
269         ADS_STATUS status;
270
271         if (!r->in.ads) {
272                 return libnet_unjoin_connect_ads(mem_ctx, r);
273         }
274
275         status = ads_leave_realm(r->in.ads, r->in.machine_name);
276         if (!ADS_ERR_OK(status)) {
277                 libnet_unjoin_set_error_string(mem_ctx, r,
278                         "failed to leave realm: %s",
279                         ads_errstr(status));
280                 return status;
281         }
282
283         return ADS_SUCCESS;
284 }
285
286 /****************************************************************
287 ****************************************************************/
288
289 static ADS_STATUS libnet_join_find_machine_acct(TALLOC_CTX *mem_ctx,
290                                                 struct libnet_JoinCtx *r)
291 {
292         ADS_STATUS status;
293         LDAPMessage *res = NULL;
294         char *dn = NULL;
295
296         if (!r->in.machine_name) {
297                 return ADS_ERROR(LDAP_NO_MEMORY);
298         }
299
300         status = ads_find_machine_acct(r->in.ads,
301                                        &res,
302                                        r->in.machine_name);
303         if (!ADS_ERR_OK(status)) {
304                 return status;
305         }
306
307         if (ads_count_replies(r->in.ads, res) != 1) {
308                 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
309                 goto done;
310         }
311
312         dn = ads_get_dn(r->in.ads, res);
313         if (!dn) {
314                 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
315                 goto done;
316         }
317
318         r->out.dn = talloc_strdup(mem_ctx, dn);
319         if (!r->out.dn) {
320                 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
321                 goto done;
322         }
323
324  done:
325         ads_msgfree(r->in.ads, res);
326         ads_memfree(r->in.ads, dn);
327
328         return status;
329 }
330
331 /****************************************************************
332  Set a machines dNSHostName and servicePrincipalName attributes
333 ****************************************************************/
334
335 static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
336                                               struct libnet_JoinCtx *r)
337 {
338         ADS_STATUS status;
339         ADS_MODLIST mods;
340         fstring my_fqdn;
341         const char *spn_array[3] = {NULL, NULL, NULL};
342         char *spn = NULL;
343
344         /* Find our DN */
345
346         status = libnet_join_find_machine_acct(mem_ctx, r);
347         if (!ADS_ERR_OK(status)) {
348                 return status;
349         }
350
351         /* Windows only creates HOST/shortname & HOST/fqdn. */
352
353         spn = talloc_asprintf(mem_ctx, "HOST/%s", r->in.machine_name);
354         if (!spn) {
355                 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
356         }
357         strupper_m(spn);
358         spn_array[0] = spn;
359
360         if (!name_to_fqdn(my_fqdn, r->in.machine_name)
361             || (strchr(my_fqdn, '.') == NULL)) {
362                 fstr_sprintf(my_fqdn, "%s.%s", r->in.machine_name,
363                              r->out.dns_domain_name);
364         }
365
366         strlower_m(my_fqdn);
367
368         if (!strequal(my_fqdn, r->in.machine_name)) {
369                 spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
370                 if (!spn) {
371                         return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
372                 }
373                 spn_array[1] = spn;
374         }
375
376         mods = ads_init_mods(mem_ctx);
377         if (!mods) {
378                 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
379         }
380
381         /* fields of primary importance */
382
383         status = ads_mod_str(mem_ctx, &mods, "dNSHostName", my_fqdn);
384         if (!ADS_ERR_OK(status)) {
385                 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
386         }
387
388         status = ads_mod_strlist(mem_ctx, &mods, "servicePrincipalName",
389                                  spn_array);
390         if (!ADS_ERR_OK(status)) {
391                 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
392         }
393
394         return ads_gen_mod(r->in.ads, r->out.dn, mods);
395 }
396
397 /****************************************************************
398 ****************************************************************/
399
400 static ADS_STATUS libnet_join_set_machine_upn(TALLOC_CTX *mem_ctx,
401                                               struct libnet_JoinCtx *r)
402 {
403         ADS_STATUS status;
404         ADS_MODLIST mods;
405
406         if (!r->in.create_upn) {
407                 return ADS_SUCCESS;
408         }
409
410         /* Find our DN */
411
412         status = libnet_join_find_machine_acct(mem_ctx, r);
413         if (!ADS_ERR_OK(status)) {
414                 return status;
415         }
416
417         if (!r->in.upn) {
418                 r->in.upn = talloc_asprintf(mem_ctx,
419                                             "host/%s@%s",
420                                             r->in.machine_name,
421                                             r->out.dns_domain_name);
422                 if (!r->in.upn) {
423                         return ADS_ERROR(LDAP_NO_MEMORY);
424                 }
425         }
426
427         /* now do the mods */
428
429         mods = ads_init_mods(mem_ctx);
430         if (!mods) {
431                 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
432         }
433
434         /* fields of primary importance */
435
436         status = ads_mod_str(mem_ctx, &mods, "userPrincipalName", r->in.upn);
437         if (!ADS_ERR_OK(status)) {
438                 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
439         }
440
441         return ads_gen_mod(r->in.ads, r->out.dn, mods);
442 }
443
444
445 /****************************************************************
446 ****************************************************************/
447
448 static ADS_STATUS libnet_join_set_os_attributes(TALLOC_CTX *mem_ctx,
449                                                 struct libnet_JoinCtx *r)
450 {
451         ADS_STATUS status;
452         ADS_MODLIST mods;
453         char *os_sp = NULL;
454
455         if (!r->in.os_name || !r->in.os_version ) {
456                 return ADS_SUCCESS;
457         }
458
459         /* Find our DN */
460
461         status = libnet_join_find_machine_acct(mem_ctx, r);
462         if (!ADS_ERR_OK(status)) {
463                 return status;
464         }
465
466         /* now do the mods */
467
468         mods = ads_init_mods(mem_ctx);
469         if (!mods) {
470                 return ADS_ERROR(LDAP_NO_MEMORY);
471         }
472
473         os_sp = talloc_asprintf(mem_ctx, "Samba %s", samba_version_string());
474         if (!os_sp) {
475                 return ADS_ERROR(LDAP_NO_MEMORY);
476         }
477
478         /* fields of primary importance */
479
480         status = ads_mod_str(mem_ctx, &mods, "operatingSystem",
481                              r->in.os_name);
482         if (!ADS_ERR_OK(status)) {
483                 return status;
484         }
485
486         status = ads_mod_str(mem_ctx, &mods, "operatingSystemVersion",
487                              r->in.os_version);
488         if (!ADS_ERR_OK(status)) {
489                 return status;
490         }
491
492         status = ads_mod_str(mem_ctx, &mods, "operatingSystemServicePack",
493                              os_sp);
494         if (!ADS_ERR_OK(status)) {
495                 return status;
496         }
497
498         return ads_gen_mod(r->in.ads, r->out.dn, mods);
499 }
500
501 /****************************************************************
502 ****************************************************************/
503
504 static bool libnet_join_create_keytab(TALLOC_CTX *mem_ctx,
505                                       struct libnet_JoinCtx *r)
506 {
507         if (!USE_SYSTEM_KEYTAB) {
508                 return true;
509         }
510
511         if (ads_keytab_create_default(r->in.ads) != 0) {
512                 return false;
513         }
514
515         return true;
516 }
517
518 /****************************************************************
519 ****************************************************************/
520
521 static bool libnet_join_derive_salting_principal(TALLOC_CTX *mem_ctx,
522                                                  struct libnet_JoinCtx *r)
523 {
524         uint32_t domain_func;
525         ADS_STATUS status;
526         const char *salt = NULL;
527         char *std_salt = NULL;
528
529         status = ads_domain_func_level(r->in.ads, &domain_func);
530         if (!ADS_ERR_OK(status)) {
531                 libnet_join_set_error_string(mem_ctx, r,
532                         "failed to determine domain functional level: %s",
533                         ads_errstr(status));
534                 return false;
535         }
536
537         /* go ahead and setup the default salt */
538
539         std_salt = kerberos_standard_des_salt();
540         if (!std_salt) {
541                 libnet_join_set_error_string(mem_ctx, r,
542                         "failed to obtain standard DES salt");
543                 return false;
544         }
545
546         salt = talloc_strdup(mem_ctx, std_salt);
547         if (!salt) {
548                 return false;
549         }
550
551         SAFE_FREE(std_salt);
552
553         /* if it's a Windows functional domain, we have to look for the UPN */
554
555         if (domain_func == DS_DOMAIN_FUNCTION_2000) {
556                 char *upn;
557
558                 upn = ads_get_upn(r->in.ads, mem_ctx,
559                                   r->in.machine_name);
560                 if (upn) {
561                         salt = talloc_strdup(mem_ctx, upn);
562                         if (!salt) {
563                                 return false;
564                         }
565                 }
566         }
567
568         return kerberos_secrets_store_des_salt(salt);
569 }
570
571 /****************************************************************
572 ****************************************************************/
573
574 static ADS_STATUS libnet_join_post_processing_ads(TALLOC_CTX *mem_ctx,
575                                                   struct libnet_JoinCtx *r)
576 {
577         ADS_STATUS status;
578
579         if (!r->in.ads) {
580                 status = libnet_join_connect_ads(mem_ctx, r);
581                 if (!ADS_ERR_OK(status)) {
582                         return status;
583                 }
584         }
585
586         status = libnet_join_set_machine_spn(mem_ctx, r);
587         if (!ADS_ERR_OK(status)) {
588                 libnet_join_set_error_string(mem_ctx, r,
589                         "failed to set machine spn: %s",
590                         ads_errstr(status));
591                 return status;
592         }
593
594         status = libnet_join_set_os_attributes(mem_ctx, r);
595         if (!ADS_ERR_OK(status)) {
596                 libnet_join_set_error_string(mem_ctx, r,
597                         "failed to set machine os attributes: %s",
598                         ads_errstr(status));
599                 return status;
600         }
601
602         status = libnet_join_set_machine_upn(mem_ctx, r);
603         if (!ADS_ERR_OK(status)) {
604                 libnet_join_set_error_string(mem_ctx, r,
605                         "failed to set machine upn: %s",
606                         ads_errstr(status));
607                 return status;
608         }
609
610         if (!libnet_join_derive_salting_principal(mem_ctx, r)) {
611                 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
612         }
613
614         if (!libnet_join_create_keytab(mem_ctx, r)) {
615                 libnet_join_set_error_string(mem_ctx, r,
616                         "failed to create kerberos keytab");
617                 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
618         }
619
620         return ADS_SUCCESS;
621 }
622 #endif /* WITH_ADS */
623
624 /****************************************************************
625  Store the machine password and domain SID
626 ****************************************************************/
627
628 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
629                                                  struct libnet_JoinCtx *r)
630 {
631         if (!secrets_store_domain_sid(r->out.netbios_domain_name,
632                                       r->out.domain_sid))
633         {
634                 DEBUG(1,("Failed to save domain sid\n"));
635                 return false;
636         }
637
638         if (!secrets_store_machine_password(r->in.machine_password,
639                                             r->out.netbios_domain_name,
640                                             r->in.secure_channel_type))
641         {
642                 DEBUG(1,("Failed to save machine password\n"));
643                 return false;
644         }
645
646         return true;
647 }
648
649 /****************************************************************
650  Connect dc's IPC$ share
651 ****************************************************************/
652
653 static NTSTATUS libnet_join_connect_dc_ipc(const char *dc,
654                                            const char *user,
655                                            const char *pass,
656                                            bool use_kerberos,
657                                            struct cli_state **cli)
658 {
659         int flags = 0;
660
661         if (use_kerberos) {
662                 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
663         }
664
665         if (use_kerberos && pass) {
666                 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
667         }
668
669         return cli_full_connection(cli, NULL,
670                                    dc,
671                                    NULL, 0,
672                                    "IPC$", "IPC",
673                                    user,
674                                    NULL,
675                                    pass,
676                                    flags,
677                                    Undefined, NULL);
678 }
679
680 /****************************************************************
681  Lookup domain dc's info
682 ****************************************************************/
683
684 static NTSTATUS libnet_join_lookup_dc_rpc(TALLOC_CTX *mem_ctx,
685                                           struct libnet_JoinCtx *r,
686                                           struct cli_state **cli)
687 {
688         struct rpc_pipe_client *pipe_hnd = NULL;
689         POLICY_HND lsa_pol;
690         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
691         union lsa_PolicyInformation *info = NULL;
692
693         status = libnet_join_connect_dc_ipc(r->in.dc_name,
694                                             r->in.admin_account,
695                                             r->in.admin_password,
696                                             r->in.use_kerberos,
697                                             cli);
698         if (!NT_STATUS_IS_OK(status)) {
699                 goto done;
700         }
701
702         status = cli_rpc_pipe_open_noauth(*cli, &ndr_table_lsarpc.syntax_id,
703                                           &pipe_hnd);
704         if (!NT_STATUS_IS_OK(status)) {
705                 DEBUG(0,("Error connecting to LSA pipe. Error was %s\n",
706                         nt_errstr(status)));
707                 goto done;
708         }
709
710         status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
711                                         SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
712         if (!NT_STATUS_IS_OK(status)) {
713                 goto done;
714         }
715
716         status = rpccli_lsa_QueryInfoPolicy2(pipe_hnd, mem_ctx,
717                                              &lsa_pol,
718                                              LSA_POLICY_INFO_DNS,
719                                              &info);
720         if (NT_STATUS_IS_OK(status)) {
721                 r->out.domain_is_ad = true;
722                 r->out.netbios_domain_name = info->dns.name.string;
723                 r->out.dns_domain_name = info->dns.dns_domain.string;
724                 r->out.forest_name = info->dns.dns_forest.string;
725                 r->out.domain_sid = sid_dup_talloc(mem_ctx, info->dns.sid);
726                 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
727         }
728
729         if (!NT_STATUS_IS_OK(status)) {
730                 status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
731                                                     &lsa_pol,
732                                                     LSA_POLICY_INFO_ACCOUNT_DOMAIN,
733                                                     &info);
734                 if (!NT_STATUS_IS_OK(status)) {
735                         goto done;
736                 }
737
738                 r->out.netbios_domain_name = info->account_domain.name.string;
739                 r->out.domain_sid = sid_dup_talloc(mem_ctx, info->account_domain.sid);
740                 NT_STATUS_HAVE_NO_MEMORY(r->out.domain_sid);
741         }
742
743         rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
744         TALLOC_FREE(pipe_hnd);
745
746  done:
747         return status;
748 }
749
750 /****************************************************************
751  Do the domain join
752 ****************************************************************/
753
754 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
755                                            struct libnet_JoinCtx *r,
756                                            struct cli_state *cli)
757 {
758         struct rpc_pipe_client *pipe_hnd = NULL;
759         POLICY_HND sam_pol, domain_pol, user_pol;
760         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
761         char *acct_name;
762         struct lsa_String lsa_acct_name;
763         uint32_t user_rid;
764         uint32_t acct_flags = ACB_WSTRUST;
765         struct samr_Ids user_rids;
766         struct samr_Ids name_types;
767         union samr_UserInfo user_info;
768
769         struct samr_CryptPassword crypt_pwd;
770         struct samr_CryptPasswordEx crypt_pwd_ex;
771
772         ZERO_STRUCT(sam_pol);
773         ZERO_STRUCT(domain_pol);
774         ZERO_STRUCT(user_pol);
775
776         if (!r->in.machine_password) {
777                 r->in.machine_password = generate_random_str(mem_ctx, DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH);
778                 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
779         }
780
781         /* Open the domain */
782
783         status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
784                                           &pipe_hnd);
785         if (!NT_STATUS_IS_OK(status)) {
786                 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
787                         nt_errstr(status)));
788                 goto done;
789         }
790
791         status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
792                                       pipe_hnd->desthost,
793                                       SAMR_ACCESS_ENUM_DOMAINS
794                                       | SAMR_ACCESS_OPEN_DOMAIN,
795                                       &sam_pol);
796         if (!NT_STATUS_IS_OK(status)) {
797                 goto done;
798         }
799
800         status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
801                                         &sam_pol,
802                                         SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1
803                                         | SAMR_DOMAIN_ACCESS_CREATE_USER
804                                         | SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
805                                         r->out.domain_sid,
806                                         &domain_pol);
807         if (!NT_STATUS_IS_OK(status)) {
808                 goto done;
809         }
810
811         /* Create domain user */
812
813         acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
814         strlower_m(acct_name);
815
816         init_lsa_String(&lsa_acct_name, acct_name);
817
818         if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
819                 uint32_t access_desired =
820                         SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
821                         SEC_STD_WRITE_DAC | SEC_STD_DELETE |
822                         SAMR_USER_ACCESS_SET_PASSWORD |
823                         SAMR_USER_ACCESS_GET_ATTRIBUTES |
824                         SAMR_USER_ACCESS_SET_ATTRIBUTES;
825                 uint32_t access_granted = 0;
826
827                 /* Don't try to set any acct_flags flags other than ACB_WSTRUST */
828
829                 DEBUG(10,("Creating account with desired access mask: %d\n",
830                         access_desired));
831
832                 status = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
833                                                  &domain_pol,
834                                                  &lsa_acct_name,
835                                                  ACB_WSTRUST,
836                                                  access_desired,
837                                                  &user_pol,
838                                                  &access_granted,
839                                                  &user_rid);
840                 if (!NT_STATUS_IS_OK(status) &&
841                     !NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
842
843                         DEBUG(10,("Creation of workstation account failed: %s\n",
844                                 nt_errstr(status)));
845
846                         /* If NT_STATUS_ACCESS_DENIED then we have a valid
847                            username/password combo but the user does not have
848                            administrator access. */
849
850                         if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
851                                 libnet_join_set_error_string(mem_ctx, r,
852                                         "User specified does not have "
853                                         "administrator privileges");
854                         }
855
856                         goto done;
857                 }
858
859                 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
860                         if (!(r->in.join_flags &
861                               WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
862                                 goto done;
863                         }
864                 }
865
866                 /* We *must* do this.... don't ask... */
867
868                 if (NT_STATUS_IS_OK(status)) {
869                         rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
870                 }
871         }
872
873         status = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
874                                          &domain_pol,
875                                          1,
876                                          &lsa_acct_name,
877                                          &user_rids,
878                                          &name_types);
879         if (!NT_STATUS_IS_OK(status)) {
880                 goto done;
881         }
882
883         if (name_types.ids[0] != SID_NAME_USER) {
884                 DEBUG(0,("%s is not a user account (type=%d)\n",
885                         acct_name, name_types.ids[0]));
886                 status = NT_STATUS_INVALID_WORKSTATION;
887                 goto done;
888         }
889
890         user_rid = user_rids.ids[0];
891
892         /* Open handle on user */
893
894         status = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
895                                       &domain_pol,
896                                       SEC_RIGHTS_MAXIMUM_ALLOWED,
897                                       user_rid,
898                                       &user_pol);
899         if (!NT_STATUS_IS_OK(status)) {
900                 goto done;
901         }
902
903         /* Fill in the additional account flags now */
904
905         acct_flags |= ACB_PWNOEXP;
906         if (r->out.domain_is_ad) {
907 #if !defined(ENCTYPE_ARCFOUR_HMAC)
908                 acct_flags |= ACB_USE_DES_KEY_ONLY;
909 #endif
910                 ;;
911         }
912
913         /* Set account flags on machine account */
914         ZERO_STRUCT(user_info.info16);
915         user_info.info16.acct_flags = acct_flags;
916
917         status = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
918                                          &user_pol,
919                                          16,
920                                          &user_info);
921
922         if (!NT_STATUS_IS_OK(status)) {
923
924                 rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
925                                        &user_pol);
926
927                 libnet_join_set_error_string(mem_ctx, r,
928                         "Failed to set account flags for machine account (%s)\n",
929                         nt_errstr(status));
930                 goto done;
931         }
932
933         /* Set password on machine account - first try level 26 */
934
935         init_samr_CryptPasswordEx(r->in.machine_password,
936                                   &cli->user_session_key,
937                                   &crypt_pwd_ex);
938
939         user_info.info26.password = crypt_pwd_ex;
940         user_info.info26.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
941
942         status = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
943                                           &user_pol,
944                                           26,
945                                           &user_info);
946
947         if (NT_STATUS_EQUAL(status, NT_STATUS(DCERPC_FAULT_INVALID_TAG))) {
948
949                 /* retry with level 24 */
950
951                 init_samr_CryptPassword(r->in.machine_password,
952                                         &cli->user_session_key,
953                                         &crypt_pwd);
954
955                 user_info.info24.password = crypt_pwd;
956                 user_info.info24.password_expired = PASS_DONT_CHANGE_AT_NEXT_LOGON;
957
958                 status = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
959                                                   &user_pol,
960                                                   24,
961                                                   &user_info);
962         }
963
964         if (!NT_STATUS_IS_OK(status)) {
965
966                 rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
967                                        &user_pol);
968
969                 libnet_join_set_error_string(mem_ctx, r,
970                         "Failed to set password for machine account (%s)\n",
971                         nt_errstr(status));
972                 goto done;
973         }
974
975         status = NT_STATUS_OK;
976
977  done:
978         if (!pipe_hnd) {
979                 return status;
980         }
981
982         if (is_valid_policy_hnd(&sam_pol)) {
983                 rpccli_samr_Close(pipe_hnd, mem_ctx, &sam_pol);
984         }
985         if (is_valid_policy_hnd(&domain_pol)) {
986                 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
987         }
988         if (is_valid_policy_hnd(&user_pol)) {
989                 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
990         }
991         TALLOC_FREE(pipe_hnd);
992
993         return status;
994 }
995
996 /****************************************************************
997 ****************************************************************/
998
999 NTSTATUS libnet_join_ok(const char *netbios_domain_name,
1000                         const char *machine_name,
1001                         const char *dc_name)
1002 {
1003         uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
1004         struct cli_state *cli = NULL;
1005         struct rpc_pipe_client *pipe_hnd = NULL;
1006         struct rpc_pipe_client *netlogon_pipe = NULL;
1007         NTSTATUS status;
1008         char *machine_password = NULL;
1009         char *machine_account = NULL;
1010
1011         if (!dc_name) {
1012                 return NT_STATUS_INVALID_PARAMETER;
1013         }
1014
1015         if (!secrets_init()) {
1016                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1017         }
1018
1019         machine_password = secrets_fetch_machine_password(netbios_domain_name,
1020                                                           NULL, NULL);
1021         if (!machine_password) {
1022                 return NT_STATUS_NO_TRUST_LSA_SECRET;
1023         }
1024
1025         if (asprintf(&machine_account, "%s$", machine_name) == -1) {
1026                 SAFE_FREE(machine_password);
1027                 return NT_STATUS_NO_MEMORY;
1028         }
1029
1030         status = cli_full_connection(&cli, NULL,
1031                                      dc_name,
1032                                      NULL, 0,
1033                                      "IPC$", "IPC",
1034                                      machine_account,
1035                                      NULL,
1036                                      machine_password,
1037                                      0,
1038                                      Undefined, NULL);
1039         free(machine_account);
1040         free(machine_password);
1041
1042         if (!NT_STATUS_IS_OK(status)) {
1043                 status = cli_full_connection(&cli, NULL,
1044                                              dc_name,
1045                                              NULL, 0,
1046                                              "IPC$", "IPC",
1047                                              "",
1048                                              NULL,
1049                                              "",
1050                                              0,
1051                                              Undefined, NULL);
1052         }
1053
1054         if (!NT_STATUS_IS_OK(status)) {
1055                 return status;
1056         }
1057
1058         status = get_schannel_session_key(cli, netbios_domain_name,
1059                                           &neg_flags, &netlogon_pipe);
1060         if (!NT_STATUS_IS_OK(status)) {
1061                 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_NETWORK_RESPONSE)) {
1062                         cli_shutdown(cli);
1063                         return NT_STATUS_OK;
1064                 }
1065
1066                 DEBUG(0,("libnet_join_ok: failed to get schannel session "
1067                         "key from server %s for domain %s. Error was %s\n",
1068                 cli->desthost, netbios_domain_name, nt_errstr(status)));
1069                 cli_shutdown(cli);
1070                 return status;
1071         }
1072
1073         if (!lp_client_schannel()) {
1074                 cli_shutdown(cli);
1075                 return NT_STATUS_OK;
1076         }
1077
1078         status = cli_rpc_pipe_open_schannel_with_key(
1079                 cli, &ndr_table_netlogon.syntax_id, PIPE_AUTH_LEVEL_PRIVACY,
1080                 netbios_domain_name, netlogon_pipe->dc, &pipe_hnd);
1081
1082         cli_shutdown(cli);
1083
1084         if (!NT_STATUS_IS_OK(status)) {
1085                 DEBUG(0,("libnet_join_ok: failed to open schannel session "
1086                         "on netlogon pipe to server %s for domain %s. "
1087                         "Error was %s\n",
1088                         cli->desthost, netbios_domain_name, nt_errstr(status)));
1089                 return status;
1090         }
1091
1092         return NT_STATUS_OK;
1093 }
1094
1095 /****************************************************************
1096 ****************************************************************/
1097
1098 static WERROR libnet_join_post_verify(TALLOC_CTX *mem_ctx,
1099                                       struct libnet_JoinCtx *r)
1100 {
1101         NTSTATUS status;
1102
1103         status = libnet_join_ok(r->out.netbios_domain_name,
1104                                 r->in.machine_name,
1105                                 r->in.dc_name);
1106         if (!NT_STATUS_IS_OK(status)) {
1107                 libnet_join_set_error_string(mem_ctx, r,
1108                         "failed to verify domain membership after joining: %s",
1109                         get_friendly_nt_error_msg(status));
1110                 return WERR_SETUP_NOT_JOINED;
1111         }
1112
1113         return WERR_OK;
1114 }
1115
1116 /****************************************************************
1117 ****************************************************************/
1118
1119 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
1120                                                     struct libnet_UnjoinCtx *r)
1121 {
1122         if (!secrets_delete_machine_password_ex(lp_workgroup())) {
1123                 return false;
1124         }
1125
1126         if (!secrets_delete_domain_sid(lp_workgroup())) {
1127                 return false;
1128         }
1129
1130         return true;
1131 }
1132
1133 /****************************************************************
1134 ****************************************************************/
1135
1136 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
1137                                              struct libnet_UnjoinCtx *r)
1138 {
1139         struct cli_state *cli = NULL;
1140         struct rpc_pipe_client *pipe_hnd = NULL;
1141         POLICY_HND sam_pol, domain_pol, user_pol;
1142         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1143         char *acct_name;
1144         uint32_t user_rid;
1145         struct lsa_String lsa_acct_name;
1146         struct samr_Ids user_rids;
1147         struct samr_Ids name_types;
1148         union samr_UserInfo *info = NULL;
1149
1150         ZERO_STRUCT(sam_pol);
1151         ZERO_STRUCT(domain_pol);
1152         ZERO_STRUCT(user_pol);
1153
1154         status = libnet_join_connect_dc_ipc(r->in.dc_name,
1155                                             r->in.admin_account,
1156                                             r->in.admin_password,
1157                                             r->in.use_kerberos,
1158                                             &cli);
1159         if (!NT_STATUS_IS_OK(status)) {
1160                 goto done;
1161         }
1162
1163         /* Open the domain */
1164
1165         status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
1166                                           &pipe_hnd);
1167         if (!NT_STATUS_IS_OK(status)) {
1168                 DEBUG(0,("Error connecting to SAM pipe. Error was %s\n",
1169                         nt_errstr(status)));
1170                 goto done;
1171         }
1172
1173         status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1174                                       pipe_hnd->desthost,
1175                                       SEC_RIGHTS_MAXIMUM_ALLOWED,
1176                                       &sam_pol);
1177         if (!NT_STATUS_IS_OK(status)) {
1178                 goto done;
1179         }
1180
1181         status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1182                                         &sam_pol,
1183                                         SEC_RIGHTS_MAXIMUM_ALLOWED,
1184                                         r->in.domain_sid,
1185                                         &domain_pol);
1186         if (!NT_STATUS_IS_OK(status)) {
1187                 goto done;
1188         }
1189
1190         /* Create domain user */
1191
1192         acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
1193         strlower_m(acct_name);
1194
1195         init_lsa_String(&lsa_acct_name, acct_name);
1196
1197         status = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1198                                          &domain_pol,
1199                                          1,
1200                                          &lsa_acct_name,
1201                                          &user_rids,
1202                                          &name_types);
1203
1204         if (!NT_STATUS_IS_OK(status)) {
1205                 goto done;
1206         }
1207
1208         if (name_types.ids[0] != SID_NAME_USER) {
1209                 DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
1210                         name_types.ids[0]));
1211                 status = NT_STATUS_INVALID_WORKSTATION;
1212                 goto done;
1213         }
1214
1215         user_rid = user_rids.ids[0];
1216
1217         /* Open handle on user */
1218
1219         status = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1220                                       &domain_pol,
1221                                       SEC_RIGHTS_MAXIMUM_ALLOWED,
1222                                       user_rid,
1223                                       &user_pol);
1224         if (!NT_STATUS_IS_OK(status)) {
1225                 goto done;
1226         }
1227
1228         /* Get user info */
1229
1230         status = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1231                                            &user_pol,
1232                                            16,
1233                                            &info);
1234         if (!NT_STATUS_IS_OK(status)) {
1235                 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1236                 goto done;
1237         }
1238
1239         /* now disable and setuser info */
1240
1241         info->info16.acct_flags |= ACB_DISABLED;
1242
1243         status = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1244                                          &user_pol,
1245                                          16,
1246                                          info);
1247
1248         rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1249
1250 done:
1251         if (pipe_hnd) {
1252                 if (is_valid_policy_hnd(&domain_pol)) {
1253                         rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1254                 }
1255                 if (is_valid_policy_hnd(&sam_pol)) {
1256                         rpccli_samr_Close(pipe_hnd, mem_ctx, &sam_pol);
1257                 }
1258                 TALLOC_FREE(pipe_hnd);
1259         }
1260
1261         if (cli) {
1262                 cli_shutdown(cli);
1263         }
1264
1265         return status;
1266 }
1267
1268 /****************************************************************
1269 ****************************************************************/
1270
1271 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
1272 {
1273         WERROR werr;
1274         struct smbconf_ctx *ctx;
1275
1276         werr = smbconf_init_reg(r, &ctx, NULL);
1277         if (!W_ERROR_IS_OK(werr)) {
1278                 goto done;
1279         }
1280
1281         if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1282
1283                 werr = smbconf_set_global_parameter(ctx, "security", "user");
1284                 W_ERROR_NOT_OK_GOTO_DONE(werr);
1285
1286                 werr = smbconf_set_global_parameter(ctx, "workgroup",
1287                                                     r->in.domain_name);
1288
1289                 smbconf_delete_global_parameter(ctx, "realm");
1290                 goto done;
1291         }
1292
1293         werr = smbconf_set_global_parameter(ctx, "security", "domain");
1294         W_ERROR_NOT_OK_GOTO_DONE(werr);
1295
1296         werr = smbconf_set_global_parameter(ctx, "workgroup",
1297                                             r->out.netbios_domain_name);
1298         W_ERROR_NOT_OK_GOTO_DONE(werr);
1299
1300         if (r->out.domain_is_ad) {
1301                 werr = smbconf_set_global_parameter(ctx, "security", "ads");
1302                 W_ERROR_NOT_OK_GOTO_DONE(werr);
1303
1304                 werr = smbconf_set_global_parameter(ctx, "realm",
1305                                                     r->out.dns_domain_name);
1306                 W_ERROR_NOT_OK_GOTO_DONE(werr);
1307         }
1308
1309  done:
1310         smbconf_shutdown(ctx);
1311         return werr;
1312 }
1313
1314 /****************************************************************
1315 ****************************************************************/
1316
1317 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
1318 {
1319         WERROR werr = WERR_OK;
1320         struct smbconf_ctx *ctx;
1321
1322         werr = smbconf_init_reg(r, &ctx, NULL);
1323         if (!W_ERROR_IS_OK(werr)) {
1324                 goto done;
1325         }
1326
1327         if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1328
1329                 werr = smbconf_set_global_parameter(ctx, "security", "user");
1330                 W_ERROR_NOT_OK_GOTO_DONE(werr);
1331
1332                 werr = smbconf_delete_global_parameter(ctx, "workgroup");
1333                 W_ERROR_NOT_OK_GOTO_DONE(werr);
1334
1335                 smbconf_delete_global_parameter(ctx, "realm");
1336         }
1337
1338  done:
1339         smbconf_shutdown(ctx);
1340         return werr;
1341 }
1342
1343 /****************************************************************
1344 ****************************************************************/
1345
1346 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
1347 {
1348         WERROR werr;
1349
1350         if (!W_ERROR_IS_OK(r->out.result)) {
1351                 return r->out.result;
1352         }
1353
1354         if (!r->in.modify_config) {
1355                 return WERR_OK;
1356         }
1357
1358         werr = do_join_modify_vals_config(r);
1359         if (!W_ERROR_IS_OK(werr)) {
1360                 return werr;
1361         }
1362
1363         lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1364
1365         r->out.modified_config = true;
1366         r->out.result = werr;
1367
1368         return werr;
1369 }
1370
1371 /****************************************************************
1372 ****************************************************************/
1373
1374 static WERROR libnet_unjoin_config(struct libnet_UnjoinCtx *r)
1375 {
1376         WERROR werr;
1377
1378         if (!W_ERROR_IS_OK(r->out.result)) {
1379                 return r->out.result;
1380         }
1381
1382         if (!r->in.modify_config) {
1383                 return WERR_OK;
1384         }
1385
1386         werr = do_unjoin_modify_vals_config(r);
1387         if (!W_ERROR_IS_OK(werr)) {
1388                 return werr;
1389         }
1390
1391         lp_load(get_dyn_CONFIGFILE(),true,false,false,true);
1392
1393         r->out.modified_config = true;
1394         r->out.result = werr;
1395
1396         return werr;
1397 }
1398
1399 /****************************************************************
1400 ****************************************************************/
1401
1402 static bool libnet_parse_domain_dc(TALLOC_CTX *mem_ctx,
1403                                    const char *domain_str,
1404                                    const char **domain_p,
1405                                    const char **dc_p)
1406 {
1407         char *domain = NULL;
1408         char *dc = NULL;
1409         const char *p = NULL;
1410
1411         if (!domain_str || !domain_p || !dc_p) {
1412                 return false;
1413         }
1414
1415         p = strchr_m(domain_str, '\\');
1416
1417         if (p != NULL) {
1418                 domain = talloc_strndup(mem_ctx, domain_str,
1419                                          PTR_DIFF(p, domain_str));
1420                 dc = talloc_strdup(mem_ctx, p+1);
1421                 if (!dc) {
1422                         return false;
1423                 }
1424         } else {
1425                 domain = talloc_strdup(mem_ctx, domain_str);
1426                 dc = NULL;
1427         }
1428         if (!domain) {
1429                 return false;
1430         }
1431
1432         *domain_p = domain;
1433
1434         if (!*dc_p && dc) {
1435                 *dc_p = dc;
1436         }
1437
1438         return true;
1439 }
1440
1441 /****************************************************************
1442 ****************************************************************/
1443
1444 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
1445                                          struct libnet_JoinCtx *r)
1446 {
1447         if (!r->in.domain_name) {
1448                 libnet_join_set_error_string(mem_ctx, r,
1449                         "No domain name defined");
1450                 return WERR_INVALID_PARAM;
1451         }
1452
1453         if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
1454                                     &r->in.domain_name,
1455                                     &r->in.dc_name)) {
1456                 libnet_join_set_error_string(mem_ctx, r,
1457                         "Failed to parse domain name");
1458                 return WERR_INVALID_PARAM;
1459         }
1460
1461         if (IS_DC) {
1462                 return WERR_SETUP_DOMAIN_CONTROLLER;
1463         }
1464
1465         if (!secrets_init()) {
1466                 libnet_join_set_error_string(mem_ctx, r,
1467                         "Unable to open secrets database");
1468                 return WERR_CAN_NOT_COMPLETE;
1469         }
1470
1471         return WERR_OK;
1472 }
1473
1474 /****************************************************************
1475 ****************************************************************/
1476
1477 static void libnet_join_add_dom_rids_to_builtins(struct dom_sid *domain_sid)
1478 {
1479         NTSTATUS status;
1480
1481         /* Try adding dom admins to builtin\admins. Only log failures. */
1482         status = create_builtin_administrators(domain_sid);
1483         if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1484                 DEBUG(10,("Unable to auto-add domain administrators to "
1485                           "BUILTIN\\Administrators during join because "
1486                           "winbindd must be running."));
1487         } else if (!NT_STATUS_IS_OK(status)) {
1488                 DEBUG(5, ("Failed to auto-add domain administrators to "
1489                           "BUILTIN\\Administrators during join: %s\n",
1490                           nt_errstr(status)));
1491         }
1492
1493         /* Try adding dom users to builtin\users. Only log failures. */
1494         status = create_builtin_users(domain_sid);
1495         if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
1496                 DEBUG(10,("Unable to auto-add domain users to BUILTIN\\users "
1497                           "during join because winbindd must be running."));
1498         } else if (!NT_STATUS_IS_OK(status)) {
1499                 DEBUG(5, ("Failed to auto-add domain administrators to "
1500                           "BUILTIN\\Administrators during join: %s\n",
1501                           nt_errstr(status)));
1502         }
1503 }
1504
1505 /****************************************************************
1506 ****************************************************************/
1507
1508 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
1509                                           struct libnet_JoinCtx *r)
1510 {
1511         WERROR werr;
1512
1513         if (!W_ERROR_IS_OK(r->out.result)) {
1514                 return r->out.result;
1515         }
1516
1517         werr = do_JoinConfig(r);
1518         if (!W_ERROR_IS_OK(werr)) {
1519                 return werr;
1520         }
1521
1522         if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
1523                 return WERR_OK;
1524         }
1525
1526         saf_join_store(r->out.netbios_domain_name, r->in.dc_name);
1527         if (r->out.dns_domain_name) {
1528                 saf_join_store(r->out.dns_domain_name, r->in.dc_name);
1529         }
1530
1531 #ifdef WITH_ADS
1532         if (r->out.domain_is_ad) {
1533                 ADS_STATUS ads_status;
1534
1535                 ads_status  = libnet_join_post_processing_ads(mem_ctx, r);
1536                 if (!ADS_ERR_OK(ads_status)) {
1537                         return WERR_GENERAL_FAILURE;
1538                 }
1539         }
1540 #endif /* WITH_ADS */
1541
1542         libnet_join_add_dom_rids_to_builtins(r->out.domain_sid);
1543
1544         return WERR_OK;
1545 }
1546
1547 /****************************************************************
1548 ****************************************************************/
1549
1550 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
1551 {
1552         const char *krb5_cc_env = NULL;
1553
1554         if (r->in.ads) {
1555                 ads_destroy(&r->in.ads);
1556         }
1557
1558         krb5_cc_env = getenv(KRB5_ENV_CCNAME);
1559         if (krb5_cc_env && StrCaseCmp(krb5_cc_env, "MEMORY:libnetjoin")) {
1560                 unsetenv(KRB5_ENV_CCNAME);
1561         }
1562
1563         return 0;
1564 }
1565
1566 /****************************************************************
1567 ****************************************************************/
1568
1569 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
1570 {
1571         const char *krb5_cc_env = NULL;
1572
1573         if (r->in.ads) {
1574                 ads_destroy(&r->in.ads);
1575         }
1576
1577         krb5_cc_env = getenv(KRB5_ENV_CCNAME);
1578         if (krb5_cc_env && StrCaseCmp(krb5_cc_env, "MEMORY:libnetjoin")) {
1579                 unsetenv(KRB5_ENV_CCNAME);
1580         }
1581
1582         return 0;
1583 }
1584
1585 /****************************************************************
1586 ****************************************************************/
1587
1588 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
1589                            struct libnet_JoinCtx **r)
1590 {
1591         struct libnet_JoinCtx *ctx;
1592         const char *krb5_cc_env = NULL;
1593
1594         ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
1595         if (!ctx) {
1596                 return WERR_NOMEM;
1597         }
1598
1599         talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
1600
1601         ctx->in.machine_name = talloc_strdup(mem_ctx, global_myname());
1602         W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1603
1604         krb5_cc_env = getenv(KRB5_ENV_CCNAME);
1605         if (!krb5_cc_env || (strlen(krb5_cc_env) == 0)) {
1606                 krb5_cc_env = talloc_strdup(mem_ctx, "MEMORY:libnetjoin");
1607                 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env);
1608                 setenv(KRB5_ENV_CCNAME, krb5_cc_env, 1);
1609         }
1610
1611         ctx->in.secure_channel_type = SEC_CHAN_WKSTA;
1612
1613         *r = ctx;
1614
1615         return WERR_OK;
1616 }
1617
1618 /****************************************************************
1619 ****************************************************************/
1620
1621 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
1622                              struct libnet_UnjoinCtx **r)
1623 {
1624         struct libnet_UnjoinCtx *ctx;
1625         const char *krb5_cc_env = NULL;
1626
1627         ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
1628         if (!ctx) {
1629                 return WERR_NOMEM;
1630         }
1631
1632         talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
1633
1634         ctx->in.machine_name = talloc_strdup(mem_ctx, global_myname());
1635         W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1636
1637         krb5_cc_env = getenv(KRB5_ENV_CCNAME);
1638         if (!krb5_cc_env || (strlen(krb5_cc_env) == 0)) {
1639                 krb5_cc_env = talloc_strdup(mem_ctx, "MEMORY:libnetjoin");
1640                 W_ERROR_HAVE_NO_MEMORY(krb5_cc_env);
1641                 setenv(KRB5_ENV_CCNAME, krb5_cc_env, 1);
1642         }
1643
1644         *r = ctx;
1645
1646         return WERR_OK;
1647 }
1648
1649 /****************************************************************
1650 ****************************************************************/
1651
1652 static WERROR libnet_join_check_config(TALLOC_CTX *mem_ctx,
1653                                        struct libnet_JoinCtx *r)
1654 {
1655         bool valid_security = false;
1656         bool valid_workgroup = false;
1657         bool valid_realm = false;
1658
1659         /* check if configuration is already set correctly */
1660
1661         valid_workgroup = strequal(lp_workgroup(), r->out.netbios_domain_name);
1662
1663         switch (r->out.domain_is_ad) {
1664                 case false:
1665                         valid_security = (lp_security() == SEC_DOMAIN);
1666                         if (valid_workgroup && valid_security) {
1667                                 /* nothing to be done */
1668                                 return WERR_OK;
1669                         }
1670                         break;
1671                 case true:
1672                         valid_realm = strequal(lp_realm(), r->out.dns_domain_name);
1673                         switch (lp_security()) {
1674                         case SEC_DOMAIN:
1675                         case SEC_ADS:
1676                                 valid_security = true;
1677                         }
1678
1679                         if (valid_workgroup && valid_realm && valid_security) {
1680                                 /* nothing to be done */
1681                                 return WERR_OK;
1682                         }
1683                         break;
1684         }
1685
1686         /* check if we are supposed to manipulate configuration */
1687
1688         if (!r->in.modify_config) {
1689
1690                 char *wrong_conf = talloc_strdup(mem_ctx, "");
1691
1692                 if (!valid_workgroup) {
1693                         wrong_conf = talloc_asprintf_append(wrong_conf,
1694                                 "\"workgroup\" set to '%s', should be '%s'",
1695                                 lp_workgroup(), r->out.netbios_domain_name);
1696                         W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1697                 }
1698
1699                 if (!valid_realm) {
1700                         wrong_conf = talloc_asprintf_append(wrong_conf,
1701                                 "\"realm\" set to '%s', should be '%s'",
1702                                 lp_realm(), r->out.dns_domain_name);
1703                         W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1704                 }
1705
1706                 if (!valid_security) {
1707                         const char *sec = NULL;
1708                         switch (lp_security()) {
1709                         case SEC_SHARE: sec = "share"; break;
1710                         case SEC_USER:  sec = "user"; break;
1711                         case SEC_DOMAIN: sec = "domain"; break;
1712                         case SEC_ADS: sec = "ads"; break;
1713                         }
1714                         wrong_conf = talloc_asprintf_append(wrong_conf,
1715                                 "\"security\" set to '%s', should be %s",
1716                                 sec, r->out.domain_is_ad ?
1717                                 "either 'domain' or 'ads'" : "'domain'");
1718                         W_ERROR_HAVE_NO_MEMORY(wrong_conf);
1719                 }
1720
1721                 libnet_join_set_error_string(mem_ctx, r,
1722                         "Invalid configuration (%s) and configuration modification "
1723                         "was not requested", wrong_conf);
1724                 return WERR_CAN_NOT_COMPLETE;
1725         }
1726
1727         /* check if we are able to manipulate configuration */
1728
1729         if (!lp_config_backend_is_registry()) {
1730                 libnet_join_set_error_string(mem_ctx, r,
1731                         "Configuration manipulation requested but not "
1732                         "supported by backend");
1733                 return WERR_NOT_SUPPORTED;
1734         }
1735
1736         return WERR_OK;
1737 }
1738
1739 /****************************************************************
1740 ****************************************************************/
1741
1742 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
1743                                 struct libnet_JoinCtx *r)
1744 {
1745         NTSTATUS status;
1746         WERROR werr;
1747         struct cli_state *cli = NULL;
1748 #ifdef WITH_ADS
1749         ADS_STATUS ads_status;
1750 #endif /* WITH_ADS */
1751
1752         if (!r->in.dc_name) {
1753                 struct netr_DsRGetDCNameInfo *info;
1754                 const char *dc;
1755                 status = dsgetdcname(mem_ctx,
1756                                      r->in.msg_ctx,
1757                                      r->in.domain_name,
1758                                      NULL,
1759                                      NULL,
1760                                      DS_FORCE_REDISCOVERY |
1761                                      DS_DIRECTORY_SERVICE_REQUIRED |
1762                                      DS_WRITABLE_REQUIRED |
1763                                      DS_RETURN_DNS_NAME,
1764                                      &info);
1765                 if (!NT_STATUS_IS_OK(status)) {
1766                         libnet_join_set_error_string(mem_ctx, r,
1767                                 "failed to find DC for domain %s",
1768                                 r->in.domain_name,
1769                                 get_friendly_nt_error_msg(status));
1770                         return WERR_DOMAIN_CONTROLLER_NOT_FOUND;
1771                 }
1772
1773                 dc = strip_hostname(info->dc_unc);
1774                 r->in.dc_name = talloc_strdup(mem_ctx, dc);
1775                 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
1776         }
1777
1778         status = libnet_join_lookup_dc_rpc(mem_ctx, r, &cli);
1779         if (!NT_STATUS_IS_OK(status)) {
1780                 libnet_join_set_error_string(mem_ctx, r,
1781                         "failed to lookup DC info for domain '%s' over rpc: %s",
1782                         r->in.domain_name, get_friendly_nt_error_msg(status));
1783                 return ntstatus_to_werror(status);
1784         }
1785
1786         werr = libnet_join_check_config(mem_ctx, r);
1787         if (!W_ERROR_IS_OK(werr)) {
1788                 goto done;
1789         }
1790
1791 #ifdef WITH_ADS
1792         if (r->out.domain_is_ad && r->in.account_ou) {
1793
1794                 ads_status = libnet_join_connect_ads(mem_ctx, r);
1795                 if (!ADS_ERR_OK(ads_status)) {
1796                         return WERR_DEFAULT_JOIN_REQUIRED;
1797                 }
1798
1799                 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
1800                 if (!ADS_ERR_OK(ads_status)) {
1801                         libnet_join_set_error_string(mem_ctx, r,
1802                                 "failed to precreate account in ou %s: %s",
1803                                 r->in.account_ou,
1804                                 ads_errstr(ads_status));
1805                         return WERR_DEFAULT_JOIN_REQUIRED;
1806                 }
1807
1808                 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
1809         }
1810 #endif /* WITH_ADS */
1811
1812         status = libnet_join_joindomain_rpc(mem_ctx, r, cli);
1813         if (!NT_STATUS_IS_OK(status)) {
1814                 libnet_join_set_error_string(mem_ctx, r,
1815                         "failed to join domain '%s' over rpc: %s",
1816                         r->in.domain_name, get_friendly_nt_error_msg(status));
1817                 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1818                         return WERR_SETUP_ALREADY_JOINED;
1819                 }
1820                 werr = ntstatus_to_werror(status);
1821                 goto done;
1822         }
1823
1824         if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
1825                 werr = WERR_SETUP_NOT_JOINED;
1826                 goto done;
1827         }
1828
1829         werr = WERR_OK;
1830
1831  done:
1832         if (cli) {
1833                 cli_shutdown(cli);
1834         }
1835
1836         return werr;
1837 }
1838
1839 /****************************************************************
1840 ****************************************************************/
1841
1842 static WERROR libnet_join_rollback(TALLOC_CTX *mem_ctx,
1843                                    struct libnet_JoinCtx *r)
1844 {
1845         WERROR werr;
1846         struct libnet_UnjoinCtx *u = NULL;
1847
1848         werr = libnet_init_UnjoinCtx(mem_ctx, &u);
1849         if (!W_ERROR_IS_OK(werr)) {
1850                 return werr;
1851         }
1852
1853         u->in.debug             = r->in.debug;
1854         u->in.dc_name           = r->in.dc_name;
1855         u->in.domain_name       = r->in.domain_name;
1856         u->in.admin_account     = r->in.admin_account;
1857         u->in.admin_password    = r->in.admin_password;
1858         u->in.modify_config     = r->in.modify_config;
1859         u->in.unjoin_flags      = WKSSVC_JOIN_FLAGS_JOIN_TYPE |
1860                                   WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE;
1861
1862         werr = libnet_Unjoin(mem_ctx, u);
1863         TALLOC_FREE(u);
1864
1865         return werr;
1866 }
1867
1868 /****************************************************************
1869 ****************************************************************/
1870
1871 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
1872                    struct libnet_JoinCtx *r)
1873 {
1874         WERROR werr;
1875
1876         if (r->in.debug) {
1877                 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx, r);
1878         }
1879
1880         werr = libnet_join_pre_processing(mem_ctx, r);
1881         if (!W_ERROR_IS_OK(werr)) {
1882                 goto done;
1883         }
1884
1885         if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1886                 werr = libnet_DomainJoin(mem_ctx, r);
1887                 if (!W_ERROR_IS_OK(werr)) {
1888                         goto done;
1889                 }
1890         }
1891
1892         werr = libnet_join_post_processing(mem_ctx, r);
1893         if (!W_ERROR_IS_OK(werr)) {
1894                 goto done;
1895         }
1896
1897         if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1898                 werr = libnet_join_post_verify(mem_ctx, r);
1899                 if (!W_ERROR_IS_OK(werr)) {
1900                         libnet_join_rollback(mem_ctx, r);
1901                 }
1902         }
1903
1904  done:
1905         r->out.result = werr;
1906
1907         if (r->in.debug) {
1908                 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx, r);
1909         }
1910         return werr;
1911 }
1912
1913 /****************************************************************
1914 ****************************************************************/
1915
1916 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
1917                                   struct libnet_UnjoinCtx *r)
1918 {
1919         NTSTATUS status;
1920
1921         if (!r->in.domain_sid) {
1922                 struct dom_sid sid;
1923                 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
1924                         libnet_unjoin_set_error_string(mem_ctx, r,
1925                                 "Unable to fetch domain sid: are we joined?");
1926                         return WERR_SETUP_NOT_JOINED;
1927                 }
1928                 r->in.domain_sid = sid_dup_talloc(mem_ctx, &sid);
1929                 W_ERROR_HAVE_NO_MEMORY(r->in.domain_sid);
1930         }
1931
1932         if (!r->in.dc_name) {
1933                 struct netr_DsRGetDCNameInfo *info;
1934                 const char *dc;
1935                 status = dsgetdcname(mem_ctx,
1936                                      r->in.msg_ctx,
1937                                      r->in.domain_name,
1938                                      NULL,
1939                                      NULL,
1940                                      DS_DIRECTORY_SERVICE_REQUIRED |
1941                                      DS_WRITABLE_REQUIRED |
1942                                      DS_RETURN_DNS_NAME,
1943                                      &info);
1944                 if (!NT_STATUS_IS_OK(status)) {
1945                         libnet_unjoin_set_error_string(mem_ctx, r,
1946                                 "failed to find DC for domain %s",
1947                                 r->in.domain_name,
1948                                 get_friendly_nt_error_msg(status));
1949                         return WERR_DOMAIN_CONTROLLER_NOT_FOUND;
1950                 }
1951
1952                 dc = strip_hostname(info->dc_unc);
1953                 r->in.dc_name = talloc_strdup(mem_ctx, dc);
1954                 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
1955         }
1956
1957         status = libnet_join_unjoindomain_rpc(mem_ctx, r);
1958         if (!NT_STATUS_IS_OK(status)) {
1959                 libnet_unjoin_set_error_string(mem_ctx, r,
1960                         "failed to disable machine account via rpc: %s",
1961                         get_friendly_nt_error_msg(status));
1962                 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
1963                         return WERR_SETUP_NOT_JOINED;
1964                 }
1965                 return ntstatus_to_werror(status);
1966         }
1967
1968         r->out.disabled_machine_account = true;
1969
1970 #ifdef WITH_ADS
1971         if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
1972                 ADS_STATUS ads_status;
1973                 libnet_unjoin_connect_ads(mem_ctx, r);
1974                 ads_status = libnet_unjoin_remove_machine_acct(mem_ctx, r);
1975                 if (!ADS_ERR_OK(ads_status)) {
1976                         libnet_unjoin_set_error_string(mem_ctx, r,
1977                                 "failed to remove machine account from AD: %s",
1978                                 ads_errstr(ads_status));
1979                 } else {
1980                         r->out.deleted_machine_account = true;
1981                         /* dirty hack */
1982                         r->out.dns_domain_name = talloc_strdup(mem_ctx,
1983                                                                r->in.ads->server.realm);
1984                         W_ERROR_HAVE_NO_MEMORY(r->out.dns_domain_name);
1985                 }
1986         }
1987 #endif /* WITH_ADS */
1988
1989         libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
1990
1991         return WERR_OK;
1992 }
1993
1994 /****************************************************************
1995 ****************************************************************/
1996
1997 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
1998                                            struct libnet_UnjoinCtx *r)
1999 {
2000         if (!r->in.domain_name) {
2001                 libnet_unjoin_set_error_string(mem_ctx, r,
2002                         "No domain name defined");
2003                 return WERR_INVALID_PARAM;
2004         }
2005
2006         if (!libnet_parse_domain_dc(mem_ctx, r->in.domain_name,
2007                                     &r->in.domain_name,
2008                                     &r->in.dc_name)) {
2009                 libnet_unjoin_set_error_string(mem_ctx, r,
2010                         "Failed to parse domain name");
2011                 return WERR_INVALID_PARAM;
2012         }
2013
2014         if (IS_DC) {
2015                 return WERR_SETUP_DOMAIN_CONTROLLER;
2016         }
2017
2018         if (!secrets_init()) {
2019                 libnet_unjoin_set_error_string(mem_ctx, r,
2020                         "Unable to open secrets database");
2021                 return WERR_CAN_NOT_COMPLETE;
2022         }
2023
2024         return WERR_OK;
2025 }
2026
2027 /****************************************************************
2028 ****************************************************************/
2029
2030 static WERROR libnet_unjoin_post_processing(TALLOC_CTX *mem_ctx,
2031                                             struct libnet_UnjoinCtx *r)
2032 {
2033         saf_delete(r->out.netbios_domain_name);
2034         saf_delete(r->out.dns_domain_name);
2035
2036         return libnet_unjoin_config(r);
2037 }
2038
2039 /****************************************************************
2040 ****************************************************************/
2041
2042 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
2043                      struct libnet_UnjoinCtx *r)
2044 {
2045         WERROR werr;
2046
2047         if (r->in.debug) {
2048                 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx, r);
2049         }
2050
2051         werr = libnet_unjoin_pre_processing(mem_ctx, r);
2052         if (!W_ERROR_IS_OK(werr)) {
2053                 goto done;
2054         }
2055
2056         if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
2057                 werr = libnet_DomainUnjoin(mem_ctx, r);
2058                 if (!W_ERROR_IS_OK(werr)) {
2059                         libnet_unjoin_config(r);
2060                         goto done;
2061                 }
2062         }
2063
2064         werr = libnet_unjoin_post_processing(mem_ctx, r);
2065         if (!W_ERROR_IS_OK(werr)) {
2066                 goto done;
2067         }
2068
2069  done:
2070         r->out.result = werr;
2071
2072         if (r->in.debug) {
2073                 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx, r);
2074         }
2075
2076         return werr;
2077 }