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