Collect all init_lsa_string varients in one place.
[jra/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 /****************************************************************
54 ****************************************************************/
55
56 static void libnet_join_set_error_string(TALLOC_CTX *mem_ctx,
57                                          struct libnet_JoinCtx *r,
58                                          const char *format, ...)
59 {
60         va_list args;
61
62         if (r->out.error_string) {
63                 return;
64         }
65
66         va_start(args, format);
67         r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
68         va_end(args);
69 }
70
71 /****************************************************************
72 ****************************************************************/
73
74 static void libnet_unjoin_set_error_string(TALLOC_CTX *mem_ctx,
75                                            struct libnet_UnjoinCtx *r,
76                                            const char *format, ...)
77 {
78         va_list args;
79
80         if (r->out.error_string) {
81                 return;
82         }
83
84         va_start(args, format);
85         r->out.error_string = talloc_vasprintf(mem_ctx, format, args);
86         va_end(args);
87 }
88
89 #ifdef WITH_ADS
90
91 /****************************************************************
92 ****************************************************************/
93
94 static ADS_STATUS libnet_connect_ads(const char *dns_domain_name,
95                                      const char *netbios_domain_name,
96                                      const char *dc_name,
97                                      const char *user_name,
98                                      const char *password,
99                                      ADS_STRUCT **ads)
100 {
101         ADS_STATUS status;
102         ADS_STRUCT *my_ads = NULL;
103
104         my_ads = ads_init(dns_domain_name,
105                           netbios_domain_name,
106                           dc_name);
107         if (!my_ads) {
108                 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
109         }
110
111         if (user_name) {
112                 SAFE_FREE(my_ads->auth.user_name);
113                 my_ads->auth.user_name = SMB_STRDUP(user_name);
114         }
115
116         if (password) {
117                 SAFE_FREE(my_ads->auth.password);
118                 my_ads->auth.password = SMB_STRDUP(password);
119         }
120
121         status = ads_connect(my_ads);
122         if (!ADS_ERR_OK(status)) {
123                 ads_destroy(&my_ads);
124                 return status;
125         }
126
127         *ads = my_ads;
128         return ADS_SUCCESS;
129 }
130
131 /****************************************************************
132 ****************************************************************/
133
134 static ADS_STATUS libnet_join_connect_ads(TALLOC_CTX *mem_ctx,
135                                           struct libnet_JoinCtx *r)
136 {
137         ADS_STATUS status;
138
139         status = libnet_connect_ads(r->in.domain_name,
140                                     r->in.domain_name,
141                                     r->in.dc_name,
142                                     r->in.admin_account,
143                                     r->in.admin_password,
144                                     &r->in.ads);
145         if (!ADS_ERR_OK(status)) {
146                 libnet_join_set_error_string(mem_ctx, r,
147                         "failed to connect to AD: %s",
148                         ads_errstr(status));
149         }
150
151         return status;
152 }
153
154 /****************************************************************
155 ****************************************************************/
156
157 static ADS_STATUS libnet_unjoin_connect_ads(TALLOC_CTX *mem_ctx,
158                                             struct libnet_UnjoinCtx *r)
159 {
160         ADS_STATUS status;
161
162         status = libnet_connect_ads(r->in.domain_name,
163                                     r->in.domain_name,
164                                     r->in.dc_name,
165                                     r->in.admin_account,
166                                     r->in.admin_password,
167                                     &r->in.ads);
168         if (!ADS_ERR_OK(status)) {
169                 libnet_unjoin_set_error_string(mem_ctx, r,
170                         "failed to connect to AD: %s",
171                         ads_errstr(status));
172         }
173
174         return status;
175 }
176
177 /****************************************************************
178 ****************************************************************/
179
180 static ADS_STATUS libnet_join_precreate_machine_acct(TALLOC_CTX *mem_ctx,
181                                                      struct libnet_JoinCtx *r)
182 {
183         ADS_STATUS status;
184         LDAPMessage *res = NULL;
185         const char *attrs[] = { "dn", NULL };
186
187         status = ads_search_dn(r->in.ads, &res, r->in.account_ou, attrs);
188         if (!ADS_ERR_OK(status)) {
189                 return status;
190         }
191
192         if (ads_count_replies(r->in.ads, res) != 1) {
193                 ads_msgfree(r->in.ads, res);
194                 return ADS_ERROR_LDAP(LDAP_NO_SUCH_OBJECT);
195         }
196
197         status = ads_create_machine_acct(r->in.ads,
198                                          r->in.machine_name,
199                                          r->in.account_ou);
200         ads_msgfree(r->in.ads, res);
201
202         if ((status.error_type == ENUM_ADS_ERROR_LDAP) &&
203             (status.err.rc == LDAP_ALREADY_EXISTS)) {
204                 status = ADS_SUCCESS;
205         }
206
207         return status;
208 }
209
210 /****************************************************************
211 ****************************************************************/
212
213 static ADS_STATUS libnet_unjoin_remove_machine_acct(TALLOC_CTX *mem_ctx,
214                                                     struct libnet_UnjoinCtx *r)
215 {
216         ADS_STATUS status;
217
218         if (!r->in.ads) {
219                 status = libnet_unjoin_connect_ads(mem_ctx, r);
220                 if (!ADS_ERR_OK(status)) {
221                         return status;
222                 }
223         }
224
225         status = ads_leave_realm(r->in.ads, r->in.machine_name);
226         if (!ADS_ERR_OK(status)) {
227                 libnet_unjoin_set_error_string(mem_ctx, r,
228                         "failed to leave realm: %s",
229                         ads_errstr(status));
230                 return status;
231         }
232
233         return ADS_SUCCESS;
234 }
235
236 /****************************************************************
237 ****************************************************************/
238
239 static ADS_STATUS libnet_join_find_machine_acct(TALLOC_CTX *mem_ctx,
240                                                 struct libnet_JoinCtx *r)
241 {
242         ADS_STATUS status;
243         LDAPMessage *res = NULL;
244         char *dn = NULL;
245
246         if (!r->in.machine_name) {
247                 return ADS_ERROR(LDAP_NO_MEMORY);
248         }
249
250         status = ads_find_machine_acct(r->in.ads,
251                                        &res,
252                                        r->in.machine_name);
253         if (!ADS_ERR_OK(status)) {
254                 return status;
255         }
256
257         if (ads_count_replies(r->in.ads, res) != 1) {
258                 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
259                 goto done;
260         }
261
262         dn = ads_get_dn(r->in.ads, res);
263         if (!dn) {
264                 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
265                 goto done;
266         }
267
268         r->out.dn = talloc_strdup(mem_ctx, dn);
269         if (!r->out.dn) {
270                 status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
271                 goto done;
272         }
273
274  done:
275         ads_msgfree(r->in.ads, res);
276         ads_memfree(r->in.ads, dn);
277
278         return status;
279 }
280
281 /****************************************************************
282 ****************************************************************/
283
284 static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
285                                               struct libnet_JoinCtx *r)
286 {
287         ADS_STATUS status;
288         ADS_MODLIST mods;
289         fstring my_fqdn;
290         const char *spn_array[3] = {NULL, NULL, NULL};
291         char *spn = NULL;
292
293         if (!r->in.ads) {
294                 status = libnet_join_connect_ads(mem_ctx, r);
295                 if (!ADS_ERR_OK(status)) {
296                         return status;
297                 }
298         }
299
300         status = libnet_join_find_machine_acct(mem_ctx, r);
301         if (!ADS_ERR_OK(status)) {
302                 return status;
303         }
304
305         spn = talloc_asprintf(mem_ctx, "HOST/%s", r->in.machine_name);
306         if (!spn) {
307                 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
308         }
309         strupper_m(spn);
310         spn_array[0] = spn;
311
312         if (name_to_fqdn(my_fqdn, r->in.machine_name) &&
313             !strequal(my_fqdn, r->in.machine_name)) {
314
315                 strlower_m(my_fqdn);
316                 spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
317                 if (!spn) {
318                         return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
319                 }
320                 spn_array[1] = spn;
321         }
322
323         mods = ads_init_mods(mem_ctx);
324         if (!mods) {
325                 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
326         }
327
328         status = ads_mod_str(mem_ctx, &mods, "dNSHostName", my_fqdn);
329         if (!ADS_ERR_OK(status)) {
330                 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
331         }
332
333         status = ads_mod_strlist(mem_ctx, &mods, "servicePrincipalName",
334                                  spn_array);
335         if (!ADS_ERR_OK(status)) {
336                 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
337         }
338
339         return ads_gen_mod(r->in.ads, r->out.dn, mods);
340 }
341
342 /****************************************************************
343 ****************************************************************/
344
345 static ADS_STATUS libnet_join_set_machine_upn(TALLOC_CTX *mem_ctx,
346                                               struct libnet_JoinCtx *r)
347 {
348         ADS_STATUS status;
349         ADS_MODLIST mods;
350
351         if (!r->in.create_upn) {
352                 return ADS_SUCCESS;
353         }
354
355         if (!r->in.ads) {
356                 status = libnet_join_connect_ads(mem_ctx, r);
357                 if (!ADS_ERR_OK(status)) {
358                         return status;
359                 }
360         }
361
362         status = libnet_join_find_machine_acct(mem_ctx, r);
363         if (!ADS_ERR_OK(status)) {
364                 return status;
365         }
366
367         if (!r->in.upn) {
368                 r->in.upn = talloc_asprintf(mem_ctx,
369                                             "host/%s@%s",
370                                             r->in.machine_name,
371                                             r->out.dns_domain_name);
372                 if (!r->in.upn) {
373                         return ADS_ERROR(LDAP_NO_MEMORY);
374                 }
375         }
376
377         mods = ads_init_mods(mem_ctx);
378         if (!mods) {
379                 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
380         }
381
382         status = ads_mod_str(mem_ctx, &mods, "userPrincipalName", r->in.upn);
383         if (!ADS_ERR_OK(status)) {
384                 return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
385         }
386
387         return ads_gen_mod(r->in.ads, r->out.dn, mods);
388 }
389
390
391 /****************************************************************
392 ****************************************************************/
393
394 static ADS_STATUS libnet_join_set_os_attributes(TALLOC_CTX *mem_ctx,
395                                                 struct libnet_JoinCtx *r)
396 {
397         ADS_STATUS status;
398         ADS_MODLIST mods;
399         char *os_sp = NULL;
400
401         if (!r->in.os_name || !r->in.os_version ) {
402                 return ADS_SUCCESS;
403         }
404
405         if (!r->in.ads) {
406                 status = libnet_join_connect_ads(mem_ctx, r);
407                 if (!ADS_ERR_OK(status)) {
408                         return status;
409                 }
410         }
411
412         status = libnet_join_find_machine_acct(mem_ctx, r);
413         if (!ADS_ERR_OK(status)) {
414                 return status;
415         }
416
417         mods = ads_init_mods(mem_ctx);
418         if (!mods) {
419                 return ADS_ERROR(LDAP_NO_MEMORY);
420         }
421
422         os_sp = talloc_asprintf(mem_ctx, "Samba %s", SAMBA_VERSION_STRING);
423         if (!os_sp) {
424                 return ADS_ERROR(LDAP_NO_MEMORY);
425         }
426
427         status = ads_mod_str(mem_ctx, &mods, "operatingSystem",
428                              r->in.os_name);
429         if (!ADS_ERR_OK(status)) {
430                 return status;
431         }
432
433         status = ads_mod_str(mem_ctx, &mods, "operatingSystemVersion",
434                              r->in.os_version);
435         if (!ADS_ERR_OK(status)) {
436                 return status;
437         }
438
439         status = ads_mod_str(mem_ctx, &mods, "operatingSystemServicePack",
440                              os_sp);
441         if (!ADS_ERR_OK(status)) {
442                 return status;
443         }
444
445         return ads_gen_mod(r->in.ads, r->out.dn, mods);
446 }
447
448 /****************************************************************
449 ****************************************************************/
450
451 static bool libnet_join_create_keytab(TALLOC_CTX *mem_ctx,
452                                       struct libnet_JoinCtx *r)
453 {
454         if (!lp_use_kerberos_keytab()) {
455                 return true;
456         }
457
458         if (!ads_keytab_create_default(r->in.ads)) {
459                 return false;
460         }
461
462         return true;
463 }
464
465 /****************************************************************
466 ****************************************************************/
467
468 static bool libnet_join_derive_salting_principal(TALLOC_CTX *mem_ctx,
469                                                  struct libnet_JoinCtx *r)
470 {
471         uint32_t domain_func;
472         ADS_STATUS status;
473         const char *salt = NULL;
474         char *std_salt = NULL;
475
476         status = ads_domain_func_level(r->in.ads, &domain_func);
477         if (!ADS_ERR_OK(status)) {
478                 libnet_join_set_error_string(mem_ctx, r,
479                         "failed to determine domain functional level: %s",
480                         ads_errstr(status));
481                 return false;
482         }
483
484         std_salt = kerberos_standard_des_salt();
485         if (!std_salt) {
486                 libnet_join_set_error_string(mem_ctx, r,
487                         "failed to obtain standard DES salt");
488                 return false;
489         }
490
491         salt = talloc_strdup(mem_ctx, std_salt);
492         if (!salt) {
493                 return false;
494         }
495
496         SAFE_FREE(std_salt);
497
498         if (domain_func == DS_DOMAIN_FUNCTION_2000) {
499                 char *upn;
500
501                 upn = ads_get_upn(r->in.ads, mem_ctx,
502                                   r->in.machine_name);
503                 if (upn) {
504                         salt = talloc_strdup(mem_ctx, upn);
505                         if (!salt) {
506                                 return false;
507                         }
508                 }
509         }
510
511         return kerberos_secrets_store_des_salt(salt);
512 }
513
514 /****************************************************************
515 ****************************************************************/
516
517 static ADS_STATUS libnet_join_post_processing_ads(TALLOC_CTX *mem_ctx,
518                                                   struct libnet_JoinCtx *r)
519 {
520         ADS_STATUS status;
521
522         status = libnet_join_set_machine_spn(mem_ctx, r);
523         if (!ADS_ERR_OK(status)) {
524                 libnet_join_set_error_string(mem_ctx, r,
525                         "failed to set machine spn: %s",
526                         ads_errstr(status));
527                 return status;
528         }
529
530         status = libnet_join_set_os_attributes(mem_ctx, r);
531         if (!ADS_ERR_OK(status)) {
532                 libnet_join_set_error_string(mem_ctx, r,
533                         "failed to set machine os attributes: %s",
534                         ads_errstr(status));
535                 return status;
536         }
537
538         status = libnet_join_set_machine_upn(mem_ctx, r);
539         if (!ADS_ERR_OK(status)) {
540                 libnet_join_set_error_string(mem_ctx, r,
541                         "failed to set machine upn: %s",
542                         ads_errstr(status));
543                 return status;
544         }
545
546         if (!libnet_join_derive_salting_principal(mem_ctx, r)) {
547                 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
548         }
549
550         if (!libnet_join_create_keytab(mem_ctx, r)) {
551                 libnet_join_set_error_string(mem_ctx, r,
552                         "failed to create kerberos keytab");
553                 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
554         }
555
556         return ADS_SUCCESS;
557 }
558 #endif /* WITH_ADS */
559
560 /****************************************************************
561 ****************************************************************/
562
563 static bool libnet_join_joindomain_store_secrets(TALLOC_CTX *mem_ctx,
564                                                  struct libnet_JoinCtx *r)
565 {
566         if (!secrets_store_domain_sid(r->out.netbios_domain_name,
567                                       r->out.domain_sid))
568         {
569                 return false;
570         }
571
572         if (!secrets_store_machine_password(r->in.machine_password,
573                                             r->out.netbios_domain_name,
574                                             SEC_CHAN_WKSTA))
575         {
576                 return false;
577         }
578
579         return true;
580 }
581
582 /****************************************************************
583 ****************************************************************/
584
585 static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
586                                            struct libnet_JoinCtx *r)
587 {
588         struct cli_state *cli = NULL;
589         struct rpc_pipe_client *pipe_hnd = NULL;
590         POLICY_HND sam_pol, domain_pol, user_pol, lsa_pol;
591         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
592         char *acct_name;
593         struct lsa_String lsa_acct_name;
594         uint32 user_rid;
595         uint32 acb_info = ACB_WSTRUST;
596         uchar pwbuf[532];
597         struct MD5Context md5ctx;
598         uchar md5buffer[16];
599         DATA_BLOB digested_session_key;
600         uchar md4_trust_password[16];
601         union lsa_PolicyInformation *info = NULL;
602         struct samr_Ids user_rids;
603         struct samr_Ids name_types;
604         union samr_UserInfo user_info;
605
606         if (!r->in.machine_password) {
607                 r->in.machine_password = talloc_strdup(mem_ctx, generate_random_str(DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH));
608                 NT_STATUS_HAVE_NO_MEMORY(r->in.machine_password);
609         }
610
611         status = cli_full_connection(&cli, NULL,
612                                      r->in.dc_name,
613                                      NULL, 0,
614                                      "IPC$", "IPC",
615                                      r->in.admin_account,
616                                      NULL,
617                                      r->in.admin_password,
618                                      0,
619                                      Undefined, NULL);
620
621         if (!NT_STATUS_IS_OK(status)) {
622                 goto done;
623         }
624
625         pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &status);
626         if (!pipe_hnd) {
627                 goto done;
628         }
629
630         status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
631                                         SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
632         if (!NT_STATUS_IS_OK(status)) {
633                 goto done;
634         }
635
636         status = rpccli_lsa_QueryInfoPolicy2(pipe_hnd, mem_ctx,
637                                              &lsa_pol,
638                                              LSA_POLICY_INFO_DNS,
639                                              &info);
640         if (NT_STATUS_IS_OK(status)) {
641                 r->out.domain_is_ad = true;
642                 r->out.netbios_domain_name = info->dns.name.string;
643                 r->out.dns_domain_name = info->dns.dns_domain.string;
644                 r->out.domain_sid = info->dns.sid;
645         }
646
647         if (!NT_STATUS_IS_OK(status)) {
648                 status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
649                                                     &lsa_pol,
650                                                     LSA_POLICY_INFO_ACCOUNT_DOMAIN,
651                                                     &info);
652                 if (!NT_STATUS_IS_OK(status)) {
653                         goto done;
654                 }
655
656                 r->out.netbios_domain_name = info->account_domain.name.string;
657                 r->out.domain_sid = info->account_domain.sid;
658         }
659
660         rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
661         cli_rpc_pipe_close(pipe_hnd);
662
663         pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &status);
664         if (!pipe_hnd) {
665                 goto done;
666         }
667
668         status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
669                                       pipe_hnd->cli->desthost,
670                                       SEC_RIGHTS_MAXIMUM_ALLOWED,
671                                       &sam_pol);
672         if (!NT_STATUS_IS_OK(status)) {
673                 goto done;
674         }
675
676         status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
677                                         &sam_pol,
678                                         SEC_RIGHTS_MAXIMUM_ALLOWED,
679                                         r->out.domain_sid,
680                                         &domain_pol);
681         if (!NT_STATUS_IS_OK(status)) {
682                 goto done;
683         }
684
685         acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
686         strlower_m(acct_name);
687
688         init_lsa_String(&lsa_acct_name, acct_name);
689
690         if (r->in.join_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE) {
691                 uint32_t acct_flags =
692                         SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
693                         SEC_STD_WRITE_DAC | SEC_STD_DELETE |
694                         SAMR_USER_ACCESS_SET_PASSWORD |
695                         SAMR_USER_ACCESS_GET_ATTRIBUTES |
696                         SAMR_USER_ACCESS_SET_ATTRIBUTES;
697                 uint32_t access_granted = 0;
698
699                 status = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
700                                                  &domain_pol,
701                                                  &lsa_acct_name,
702                                                  ACB_WSTRUST,
703                                                  acct_flags,
704                                                  &user_pol,
705                                                  &access_granted,
706                                                  &user_rid);
707                 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
708                         if (!(r->in.join_flags &
709                               WKSSVC_JOIN_FLAGS_DOMAIN_JOIN_IF_JOINED)) {
710                                 goto done;
711                         }
712                 }
713
714                 if (NT_STATUS_IS_OK(status)) {
715                         rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
716                 }
717         }
718
719         status = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
720                                          &domain_pol,
721                                          1,
722                                          &lsa_acct_name,
723                                          &user_rids,
724                                          &name_types);
725         if (!NT_STATUS_IS_OK(status)) {
726                 goto done;
727         }
728
729         if (name_types.ids[0] != SID_NAME_USER) {
730                 status = NT_STATUS_INVALID_WORKSTATION;
731                 goto done;
732         }
733
734         user_rid = user_rids.ids[0];
735
736         status = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
737                                       &domain_pol,
738                                       SEC_RIGHTS_MAXIMUM_ALLOWED,
739                                       user_rid,
740                                       &user_pol);
741         if (!NT_STATUS_IS_OK(status)) {
742                 goto done;
743         }
744
745         E_md4hash(r->in.machine_password, md4_trust_password);
746         encode_pw_buffer(pwbuf, r->in.machine_password, STR_UNICODE);
747
748         generate_random_buffer((uint8*)md5buffer, sizeof(md5buffer));
749         digested_session_key = data_blob_talloc(mem_ctx, 0, 16);
750
751         MD5Init(&md5ctx);
752         MD5Update(&md5ctx, md5buffer, sizeof(md5buffer));
753         MD5Update(&md5ctx, cli->user_session_key.data,
754                   cli->user_session_key.length);
755         MD5Final(digested_session_key.data, &md5ctx);
756
757         SamOEMhashBlob(pwbuf, sizeof(pwbuf), &digested_session_key);
758         memcpy(&pwbuf[516], md5buffer, sizeof(md5buffer));
759
760         acb_info |= ACB_PWNOEXP;
761         if (r->out.domain_is_ad) {
762 #if !defined(ENCTYPE_ARCFOUR_HMAC)
763                 acb_info |= ACB_USE_DES_KEY_ONLY;
764 #endif
765                 ;;
766         }
767
768         ZERO_STRUCT(user_info.info25);
769
770         user_info.info25.info.fields_present = ACCT_NT_PWD_SET |
771                                                ACCT_LM_PWD_SET |
772                                                SAMR_FIELD_ACCT_FLAGS;
773         user_info.info25.info.acct_flags = acb_info;
774         memcpy(&user_info.info25.password.data, pwbuf, sizeof(pwbuf));
775
776         status = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
777                                          &user_pol,
778                                          25,
779                                          &user_info);
780         if (!NT_STATUS_IS_OK(status)) {
781                 goto done;
782         }
783
784         rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
785         cli_rpc_pipe_close(pipe_hnd);
786
787         status = NT_STATUS_OK;
788  done:
789         if (cli) {
790                 cli_shutdown(cli);
791         }
792
793         return status;
794 }
795
796 /****************************************************************
797 ****************************************************************/
798
799 static bool libnet_join_unjoindomain_remove_secrets(TALLOC_CTX *mem_ctx,
800                                                     struct libnet_UnjoinCtx *r)
801 {
802         if (!secrets_delete_machine_password_ex(lp_workgroup())) {
803                 return false;
804         }
805
806         if (!secrets_delete_domain_sid(lp_workgroup())) {
807                 return false;
808         }
809
810         return true;
811 }
812
813 /****************************************************************
814 ****************************************************************/
815
816 static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
817                                              struct libnet_UnjoinCtx *r)
818 {
819         struct cli_state *cli = NULL;
820         struct rpc_pipe_client *pipe_hnd = NULL;
821         POLICY_HND sam_pol, domain_pol, user_pol;
822         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
823         char *acct_name;
824         uint32 user_rid;
825         struct lsa_String lsa_acct_name;
826         struct samr_Ids user_rids;
827         struct samr_Ids name_types;
828         union samr_UserInfo *info = NULL;
829
830         status = cli_full_connection(&cli, NULL,
831                                      r->in.dc_name,
832                                      NULL, 0,
833                                      "IPC$", "IPC",
834                                      r->in.admin_account,
835                                      NULL,
836                                      r->in.admin_password,
837                                      0, Undefined, NULL);
838
839         if (!NT_STATUS_IS_OK(status)) {
840                 goto done;
841         }
842
843         pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &status);
844         if (!pipe_hnd) {
845                 goto done;
846         }
847
848         status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
849                                       pipe_hnd->cli->desthost,
850                                       SEC_RIGHTS_MAXIMUM_ALLOWED,
851                                       &sam_pol);
852         if (!NT_STATUS_IS_OK(status)) {
853                 goto done;
854         }
855
856         status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
857                                         &sam_pol,
858                                         SEC_RIGHTS_MAXIMUM_ALLOWED,
859                                         r->in.domain_sid,
860                                         &domain_pol);
861         if (!NT_STATUS_IS_OK(status)) {
862                 goto done;
863         }
864
865         acct_name = talloc_asprintf(mem_ctx, "%s$", r->in.machine_name);
866         strlower_m(acct_name);
867
868         init_lsa_String(&lsa_acct_name, acct_name);
869
870         status = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
871                                          &domain_pol,
872                                          1,
873                                          &lsa_acct_name,
874                                          &user_rids,
875                                          &name_types);
876
877         if (!NT_STATUS_IS_OK(status)) {
878                 goto done;
879         }
880
881         if (name_types.ids[0] != SID_NAME_USER) {
882                 status = NT_STATUS_INVALID_WORKSTATION;
883                 goto done;
884         }
885
886         user_rid = user_rids.ids[0];
887
888         status = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
889                                       &domain_pol,
890                                       SEC_RIGHTS_MAXIMUM_ALLOWED,
891                                       user_rid,
892                                       &user_pol);
893         if (!NT_STATUS_IS_OK(status)) {
894                 goto done;
895         }
896
897         status = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
898                                            &user_pol,
899                                            16,
900                                            &info);
901         if (!NT_STATUS_IS_OK(status)) {
902                 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
903                 goto done;
904         }
905
906         info->info16.acct_flags |= ACB_DISABLED;
907
908         status = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
909                                          &user_pol,
910                                          16,
911                                          info);
912
913         rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
914
915 done:
916         if (pipe_hnd) {
917                 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
918                 rpccli_samr_Close(pipe_hnd, mem_ctx, &sam_pol);
919                 cli_rpc_pipe_close(pipe_hnd);
920         }
921
922         if (cli) {
923                 cli_shutdown(cli);
924         }
925
926         return status;
927 }
928
929 /****************************************************************
930 ****************************************************************/
931
932 static WERROR do_join_modify_vals_config(struct libnet_JoinCtx *r)
933 {
934         WERROR werr;
935         struct libnet_conf_ctx *ctx;
936
937         werr = libnet_conf_open(r, &ctx);
938         if (!W_ERROR_IS_OK(werr)) {
939                 goto done;
940         }
941
942         if (!(r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE)) {
943
944                 werr = libnet_conf_set_global_parameter(ctx, "security", "user");
945                 if (!W_ERROR_IS_OK(werr)) {
946                         goto done;
947                 }
948
949                 werr = libnet_conf_set_global_parameter(ctx, "workgroup",
950                                                         r->in.domain_name);
951                 goto done;
952         }
953
954         werr = libnet_conf_set_global_parameter(ctx, "security", "domain");
955         if (!W_ERROR_IS_OK(werr)) {
956                 goto done;
957         }
958
959         werr = libnet_conf_set_global_parameter(ctx, "workgroup",
960                                                 r->out.netbios_domain_name);
961         if (!W_ERROR_IS_OK(werr)) {
962                 goto done;
963         }
964
965         if (r->out.domain_is_ad) {
966                 werr = libnet_conf_set_global_parameter(ctx, "security", "ads");
967                 if (!W_ERROR_IS_OK(werr)) {
968                         goto done;
969                 }
970
971                 werr = libnet_conf_set_global_parameter(ctx, "realm",
972                                                         r->out.dns_domain_name);
973         }
974
975 done:
976         libnet_conf_close(ctx);
977         return werr;
978 }
979
980 /****************************************************************
981 ****************************************************************/
982
983 static WERROR do_unjoin_modify_vals_config(struct libnet_UnjoinCtx *r)
984 {
985         WERROR werr = WERR_OK;
986         struct libnet_conf_ctx *ctx;
987
988         werr = libnet_conf_open(r, &ctx);
989         if (!W_ERROR_IS_OK(werr)) {
990                 goto done;
991         }
992
993         if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
994
995                 werr = libnet_conf_set_global_parameter(ctx, "security", "user");
996                 if (!W_ERROR_IS_OK(werr)) {
997                         goto done;
998                 }
999         }
1000
1001         libnet_conf_delete_global_parameter(ctx, "realm");
1002
1003 done:
1004         libnet_conf_close(ctx);
1005         return werr;
1006 }
1007
1008 /****************************************************************
1009 ****************************************************************/
1010
1011 static WERROR do_JoinConfig(struct libnet_JoinCtx *r)
1012 {
1013         WERROR werr;
1014
1015         if (!W_ERROR_IS_OK(r->out.result)) {
1016                 return r->out.result;
1017         }
1018
1019         if (!r->in.modify_config) {
1020                 return WERR_OK;
1021         }
1022
1023         werr = do_join_modify_vals_config(r);
1024         if (!W_ERROR_IS_OK(werr)) {
1025                 return werr;
1026         }
1027
1028         r->out.modified_config = true;
1029         r->out.result = werr;
1030
1031         return werr;
1032 }
1033
1034 /****************************************************************
1035 ****************************************************************/
1036
1037 static WERROR do_UnjoinConfig(struct libnet_UnjoinCtx *r)
1038 {
1039         WERROR werr;
1040
1041         if (!W_ERROR_IS_OK(r->out.result)) {
1042                 return r->out.result;
1043         }
1044
1045         if (!r->in.modify_config) {
1046                 return WERR_OK;
1047         }
1048
1049         werr = do_unjoin_modify_vals_config(r);
1050         if (!W_ERROR_IS_OK(werr)) {
1051                 return werr;
1052         }
1053
1054         r->out.modified_config = true;
1055         r->out.result = werr;
1056
1057         return werr;
1058 }
1059
1060 /****************************************************************
1061 ****************************************************************/
1062
1063 static WERROR libnet_join_pre_processing(TALLOC_CTX *mem_ctx,
1064                                          struct libnet_JoinCtx *r)
1065 {
1066
1067         if (!r->in.domain_name) {
1068                 return WERR_INVALID_PARAM;
1069         }
1070
1071         if (r->in.modify_config && !lp_config_backend_is_registry()) {
1072                 return WERR_NOT_SUPPORTED;
1073         }
1074
1075         if (IS_DC) {
1076                 return WERR_SETUP_DOMAIN_CONTROLLER;
1077         }
1078
1079         if (!secrets_init()) {
1080                 libnet_join_set_error_string(mem_ctx, r,
1081                         "Unable to open secrets database");
1082                 return WERR_CAN_NOT_COMPLETE;
1083         }
1084
1085         return WERR_OK;
1086 }
1087
1088 /****************************************************************
1089 ****************************************************************/
1090
1091 static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx,
1092                                           struct libnet_JoinCtx *r)
1093 {
1094         WERROR werr;
1095
1096         if (!W_ERROR_IS_OK(r->out.result)) {
1097                 return r->out.result;
1098         }
1099
1100         werr = do_JoinConfig(r);
1101         if (!W_ERROR_IS_OK(werr)) {
1102                 return werr;
1103         }
1104
1105         if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1106                 saf_store(r->in.domain_name, r->in.dc_name);
1107         }
1108
1109         return WERR_OK;
1110 }
1111
1112 /****************************************************************
1113 ****************************************************************/
1114
1115 static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
1116 {
1117         if (r->in.ads) {
1118                 ads_destroy(&r->in.ads);
1119         }
1120
1121         return 0;
1122 }
1123
1124 /****************************************************************
1125 ****************************************************************/
1126
1127 static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
1128 {
1129         if (r->in.ads) {
1130                 ads_destroy(&r->in.ads);
1131         }
1132
1133         return 0;
1134 }
1135
1136 /****************************************************************
1137 ****************************************************************/
1138
1139 WERROR libnet_init_JoinCtx(TALLOC_CTX *mem_ctx,
1140                            struct libnet_JoinCtx **r)
1141 {
1142         struct libnet_JoinCtx *ctx;
1143
1144         ctx = talloc_zero(mem_ctx, struct libnet_JoinCtx);
1145         if (!ctx) {
1146                 return WERR_NOMEM;
1147         }
1148
1149         talloc_set_destructor(ctx, libnet_destroy_JoinCtx);
1150
1151         ctx->in.machine_name = talloc_strdup(mem_ctx, global_myname());
1152         W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1153
1154         *r = ctx;
1155
1156         return WERR_OK;
1157 }
1158
1159 /****************************************************************
1160 ****************************************************************/
1161
1162 WERROR libnet_init_UnjoinCtx(TALLOC_CTX *mem_ctx,
1163                              struct libnet_UnjoinCtx **r)
1164 {
1165         struct libnet_UnjoinCtx *ctx;
1166
1167         ctx = talloc_zero(mem_ctx, struct libnet_UnjoinCtx);
1168         if (!ctx) {
1169                 return WERR_NOMEM;
1170         }
1171
1172         talloc_set_destructor(ctx, libnet_destroy_UnjoinCtx);
1173
1174         ctx->in.machine_name = talloc_strdup(mem_ctx, global_myname());
1175         W_ERROR_HAVE_NO_MEMORY(ctx->in.machine_name);
1176
1177         *r = ctx;
1178
1179         return WERR_OK;
1180 }
1181
1182 /****************************************************************
1183 ****************************************************************/
1184
1185 static WERROR libnet_DomainJoin(TALLOC_CTX *mem_ctx,
1186                                 struct libnet_JoinCtx *r)
1187 {
1188         NTSTATUS status;
1189 #ifdef WITH_ADS
1190         ADS_STATUS ads_status;
1191 #endif /* WITH_ADS */
1192
1193         if (!r->in.dc_name) {
1194                 struct DS_DOMAIN_CONTROLLER_INFO *info;
1195                 status = dsgetdcname(mem_ctx,
1196                                      r->in.domain_name,
1197                                      NULL,
1198                                      NULL,
1199                                      DS_DIRECTORY_SERVICE_REQUIRED |
1200                                      DS_WRITABLE_REQUIRED |
1201                                      DS_RETURN_DNS_NAME,
1202                                      &info);
1203                 if (!NT_STATUS_IS_OK(status)) {
1204                         libnet_join_set_error_string(mem_ctx, r,
1205                                 "failed to find DC for domain %s",
1206                                 r->in.domain_name,
1207                                 get_friendly_nt_error_msg(status));
1208                         return WERR_DOMAIN_CONTROLLER_NOT_FOUND;
1209                 }
1210
1211                 r->in.dc_name = talloc_strdup(mem_ctx,
1212                                               info->domain_controller_name);
1213                 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
1214         }
1215
1216 #ifdef WITH_ADS
1217         if (r->in.account_ou) {
1218
1219                 ads_status = libnet_join_connect_ads(mem_ctx, r);
1220                 if (!ADS_ERR_OK(ads_status)) {
1221                         return WERR_DEFAULT_JOIN_REQUIRED;
1222                 }
1223
1224                 ads_status = libnet_join_precreate_machine_acct(mem_ctx, r);
1225                 if (!ADS_ERR_OK(ads_status)) {
1226                         libnet_join_set_error_string(mem_ctx, r,
1227                                 "failed to precreate account in ou %s: %s",
1228                                 r->in.account_ou,
1229                                 ads_errstr(ads_status));
1230                         return WERR_DEFAULT_JOIN_REQUIRED;
1231                 }
1232
1233                 r->in.join_flags &= ~WKSSVC_JOIN_FLAGS_ACCOUNT_CREATE;
1234         }
1235 #endif /* WITH_ADS */
1236
1237         status = libnet_join_joindomain_rpc(mem_ctx, r);
1238         if (!NT_STATUS_IS_OK(status)) {
1239                 libnet_join_set_error_string(mem_ctx, r,
1240                         "failed to join domain over rpc: %s",
1241                         get_friendly_nt_error_msg(status));
1242                 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
1243                         return WERR_SETUP_ALREADY_JOINED;
1244                 }
1245                 return ntstatus_to_werror(status);
1246         }
1247
1248         if (!libnet_join_joindomain_store_secrets(mem_ctx, r)) {
1249                 return WERR_SETUP_NOT_JOINED;
1250         }
1251
1252 #ifdef WITH_ADS
1253         if (r->out.domain_is_ad) {
1254                 ads_status  = libnet_join_post_processing_ads(mem_ctx, r);
1255                 if (!ADS_ERR_OK(ads_status)) {
1256                         return WERR_GENERAL_FAILURE;
1257                 }
1258         }
1259 #endif /* WITH_ADS */
1260
1261         return WERR_OK;
1262 }
1263
1264 /****************************************************************
1265 ****************************************************************/
1266
1267 WERROR libnet_Join(TALLOC_CTX *mem_ctx,
1268                    struct libnet_JoinCtx *r)
1269 {
1270         WERROR werr;
1271
1272         if (r->in.debug) {
1273                 LIBNET_JOIN_IN_DUMP_CTX(mem_ctx, r);
1274         }
1275
1276         werr = libnet_join_pre_processing(mem_ctx, r);
1277         if (!W_ERROR_IS_OK(werr)) {
1278                 goto done;
1279         }
1280
1281         if (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1282                 werr = libnet_DomainJoin(mem_ctx, r);
1283                 if (!W_ERROR_IS_OK(werr)) {
1284                         goto done;
1285                 }
1286         }
1287
1288         werr = libnet_join_post_processing(mem_ctx, r);
1289         if (!W_ERROR_IS_OK(werr)) {
1290                 goto done;
1291         }
1292  done:
1293         r->out.result = werr;
1294
1295         if (r->in.debug) {
1296                 LIBNET_JOIN_OUT_DUMP_CTX(mem_ctx, r);
1297         }
1298         return werr;
1299 }
1300
1301 /****************************************************************
1302 ****************************************************************/
1303
1304 static WERROR libnet_DomainUnjoin(TALLOC_CTX *mem_ctx,
1305                                   struct libnet_UnjoinCtx *r)
1306 {
1307         NTSTATUS status;
1308
1309         if (!r->in.domain_sid) {
1310                 struct dom_sid sid;
1311                 if (!secrets_fetch_domain_sid(lp_workgroup(), &sid)) {
1312                         libnet_unjoin_set_error_string(mem_ctx, r,
1313                                 "Unable to fetch domain sid: are we joined?");
1314                         return WERR_SETUP_NOT_JOINED;
1315                 }
1316                 r->in.domain_sid = sid_dup_talloc(mem_ctx, &sid);
1317                 W_ERROR_HAVE_NO_MEMORY(r->in.domain_sid);
1318         }
1319
1320         if (!r->in.dc_name) {
1321                 struct DS_DOMAIN_CONTROLLER_INFO *info;
1322                 status = dsgetdcname(mem_ctx,
1323                                      r->in.domain_name,
1324                                      NULL,
1325                                      NULL,
1326                                      DS_DIRECTORY_SERVICE_REQUIRED |
1327                                      DS_WRITABLE_REQUIRED |
1328                                      DS_RETURN_DNS_NAME,
1329                                      &info);
1330                 if (!NT_STATUS_IS_OK(status)) {
1331                         libnet_unjoin_set_error_string(mem_ctx, r,
1332                                 "failed to find DC for domain %s",
1333                                 r->in.domain_name,
1334                                 get_friendly_nt_error_msg(status));
1335                         return WERR_DOMAIN_CONTROLLER_NOT_FOUND;
1336                 }
1337
1338                 r->in.dc_name = talloc_strdup(mem_ctx,
1339                                               info->domain_controller_name);
1340                 W_ERROR_HAVE_NO_MEMORY(r->in.dc_name);
1341         }
1342
1343         status = libnet_join_unjoindomain_rpc(mem_ctx, r);
1344         if (!NT_STATUS_IS_OK(status)) {
1345                 libnet_unjoin_set_error_string(mem_ctx, r,
1346                         "failed to disable machine account via rpc: %s",
1347                         get_friendly_nt_error_msg(status));
1348                 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
1349                         return WERR_SETUP_NOT_JOINED;
1350                 }
1351                 return ntstatus_to_werror(status);
1352         }
1353
1354 #ifdef WITH_ADS
1355         if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_ACCOUNT_DELETE) {
1356                 ADS_STATUS ads_status;
1357                 libnet_unjoin_connect_ads(mem_ctx, r);
1358                 ads_status = libnet_unjoin_remove_machine_acct(mem_ctx, r);
1359                 if (!ADS_ERR_OK(ads_status)) {
1360                         libnet_unjoin_set_error_string(mem_ctx, r,
1361                                 "failed to remove machine account from AD: %s",
1362                                 ads_errstr(ads_status));
1363                 }
1364         }
1365 #endif /* WITH_ADS */
1366
1367         libnet_join_unjoindomain_remove_secrets(mem_ctx, r);
1368
1369         return WERR_OK;
1370 }
1371
1372 /****************************************************************
1373 ****************************************************************/
1374
1375 static WERROR libnet_unjoin_pre_processing(TALLOC_CTX *mem_ctx,
1376                                            struct libnet_UnjoinCtx *r)
1377 {
1378         if (r->in.modify_config && !lp_config_backend_is_registry()) {
1379                 return WERR_NOT_SUPPORTED;
1380         }
1381
1382         if (!secrets_init()) {
1383                 libnet_unjoin_set_error_string(mem_ctx, r,
1384                         "Unable to open secrets database");
1385                 return WERR_CAN_NOT_COMPLETE;
1386         }
1387
1388         return WERR_OK;
1389 }
1390
1391
1392 /****************************************************************
1393 ****************************************************************/
1394
1395 WERROR libnet_Unjoin(TALLOC_CTX *mem_ctx,
1396                      struct libnet_UnjoinCtx *r)
1397 {
1398         WERROR werr;
1399
1400         if (r->in.debug) {
1401                 LIBNET_UNJOIN_IN_DUMP_CTX(mem_ctx, r);
1402         }
1403
1404         werr = libnet_unjoin_pre_processing(mem_ctx, r);
1405         if (!W_ERROR_IS_OK(werr)) {
1406                 goto done;
1407         }
1408
1409         if (r->in.unjoin_flags & WKSSVC_JOIN_FLAGS_JOIN_TYPE) {
1410                 werr = libnet_DomainUnjoin(mem_ctx, r);
1411                 if (!W_ERROR_IS_OK(werr)) {
1412                         goto done;
1413                 }
1414         }
1415
1416         werr = do_UnjoinConfig(r);
1417         if (!W_ERROR_IS_OK(werr)) {
1418                 goto done;
1419         }
1420
1421  done:
1422         r->out.result = werr;
1423
1424         if (r->in.debug) {
1425                 LIBNET_UNJOIN_OUT_DUMP_CTX(mem_ctx, r);
1426         }
1427
1428         return werr;
1429 }