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