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