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