s4-libcli Remove resolve_name() as it conflicts with Samba3.
[samba.git] / source4 / torture / libnet / libnet_BecomeDC.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    libnet_BecomeDC() tests
5
6    Copyright (C) Stefan Metzmacher <metze@samba.org> 2006
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "lib/cmdline/popt_common.h"
24 #include "torture/rpc/torture_rpc.h"
25 #include "libnet/libnet.h"
26 #include "dsdb/samdb/samdb.h"
27 #include "../lib/util/dlinklist.h"
28 #include "librpc/gen_ndr/ndr_drsuapi.h"
29 #include "librpc/gen_ndr/ndr_drsblobs.h"
30 #include "system/time.h"
31 #include "ldb_wrap.h"
32 #include "auth/auth.h"
33 #include "param/param.h"
34 #include "param/provision.h"
35 #include "libcli/resolve/resolve.h"
36
37 bool torture_net_become_dc(struct torture_context *torture)
38 {
39         bool ret = true;
40         NTSTATUS status;
41         struct libnet_BecomeDC b;
42         struct libnet_UnbecomeDC u;
43         struct libnet_vampire_cb_state *s;
44         struct ldb_message *msg;
45         int ldb_ret;
46         uint32_t i;
47         char *sam_ldb_path;
48         const char *address;
49         struct nbt_name name;
50         const char *netbios_name;
51         struct cli_credentials *machine_account;
52         struct test_join *tj;
53         struct loadparm_context *lp_ctx;
54         struct ldb_context *ldb;
55         struct libnet_context *ctx;
56         struct dsdb_schema *schema;
57
58         char *location = NULL;
59         torture_assert_ntstatus_ok(torture, torture_temp_dir(torture, "libnet_BecomeDC", &location), 
60                                    "torture_temp_dir should return NT_STATUS_OK" );
61
62         netbios_name = lpcfg_parm_string(torture->lp_ctx, NULL, "become dc", "smbtorture dc");
63         if (!netbios_name || !netbios_name[0]) {
64                 netbios_name = "smbtorturedc";
65         }
66
67         make_nbt_name_server(&name, torture_setting_string(torture, "host", NULL));
68
69         /* do an initial name resolution to find its IP */
70         status = resolve_name_ex(lpcfg_resolve_context(torture->lp_ctx),
71                                  0, 0,
72                                  &name, torture, &address, torture->ev);
73         torture_assert_ntstatus_ok(torture, status, talloc_asprintf(torture,
74                                    "Failed to resolve %s - %s\n",
75                                    name.name, nt_errstr(status)));
76
77
78         /* Join domain as a member server. */
79         tj = torture_join_domain(torture, netbios_name,
80                                  ACB_WSTRUST,
81                                  &machine_account);
82         torture_assert(torture, tj, talloc_asprintf(torture,
83                                                     "%s failed to join domain as workstation\n",
84                                                     netbios_name));
85
86         s = libnet_vampire_cb_state_init(torture, torture->lp_ctx, torture->ev,
87                                netbios_name,
88                                torture_join_dom_netbios_name(tj),
89                                torture_join_dom_dns_name(tj),
90                                location);
91         torture_assert(torture, s, "libnet_vampire_cb_state_init");
92
93         ctx = libnet_context_init(torture->ev, torture->lp_ctx);
94         ctx->cred = cmdline_credentials;
95
96         ZERO_STRUCT(b);
97         b.in.domain_dns_name            = torture_join_dom_dns_name(tj);
98         b.in.domain_netbios_name        = torture_join_dom_netbios_name(tj);
99         b.in.domain_sid                 = torture_join_sid(tj);
100         b.in.source_dsa_address         = address;
101         b.in.dest_dsa_netbios_name      = netbios_name;
102
103         b.in.callbacks.private_data     = s;
104         b.in.callbacks.check_options    = libnet_vampire_cb_check_options;
105         b.in.callbacks.prepare_db       = libnet_vampire_cb_prepare_db;
106         b.in.callbacks.schema_chunk     = libnet_vampire_cb_schema_chunk;
107         b.in.callbacks.config_chunk     = libnet_vampire_cb_store_chunk;
108         b.in.callbacks.domain_chunk     = libnet_vampire_cb_store_chunk;
109
110         status = libnet_BecomeDC(ctx, s, &b);
111         torture_assert_ntstatus_ok_goto(torture, status, ret, cleanup, talloc_asprintf(torture,
112                                    "libnet_BecomeDC() failed - %s %s\n",
113                                    nt_errstr(status), b.out.error_string));
114         ldb = libnet_vampire_cb_ldb(s);
115
116         msg = ldb_msg_new(s);
117         torture_assert_int_equal_goto(torture, (msg?1:0), 1, ret, cleanup,
118                                       "ldb_msg_new() failed\n");
119         msg->dn = ldb_dn_new(msg, ldb, "@ROOTDSE");
120         torture_assert_int_equal_goto(torture, (msg->dn?1:0), 1, ret, cleanup,
121                                       "ldb_msg_new(@ROOTDSE) failed\n");
122
123         ldb_ret = ldb_msg_add_string(msg, "isSynchronized", "TRUE");
124         torture_assert_int_equal_goto(torture, ldb_ret, LDB_SUCCESS, ret, cleanup,
125                                       "ldb_msg_add_string(msg, isSynchronized, TRUE) failed\n");
126
127         for (i=0; i < msg->num_elements; i++) {
128                 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
129         }
130
131         torture_comment(torture, "mark ROOTDSE with isSynchronized=TRUE\n");
132         ldb_ret = ldb_modify(libnet_vampire_cb_ldb(s), msg);
133         torture_assert_int_equal_goto(torture, ldb_ret, LDB_SUCCESS, ret, cleanup,
134                                       "ldb_modify() failed\n");
135         
136         /* commit the transaction now we know the secrets were written
137          * out properly
138         */
139         ldb_ret = ldb_transaction_commit(ldb);
140         torture_assert_int_equal_goto(torture, ldb_ret, LDB_SUCCESS, ret, cleanup,
141                                       "ldb_transaction_commit() failed\n");
142
143         /* reopen the ldb */
144         talloc_unlink(s, ldb);
145
146         lp_ctx = libnet_vampire_cb_lp_ctx(s);
147         sam_ldb_path = talloc_asprintf(s, "%s/%s", location, "private/sam.ldb");
148         lpcfg_set_cmdline(lp_ctx, "sam database", sam_ldb_path);
149         torture_comment(torture, "Reopen the SAM LDB with system credentials and all replicated data: %s\n", sam_ldb_path);
150         ldb = samdb_connect(s, torture->ev, lp_ctx, system_session(lp_ctx), 0);
151         torture_assert_goto(torture, ldb != NULL, ret, cleanup,
152                                       talloc_asprintf(torture,
153                                       "Failed to open '%s'\n", sam_ldb_path));
154
155         torture_assert_goto(torture, dsdb_uses_global_schema(ldb), ret, cleanup,
156                                                 "Uses global schema");
157
158         schema = dsdb_get_schema(ldb, s);
159         torture_assert_goto(torture, schema != NULL, ret, cleanup,
160                                       "Failed to get loaded dsdb_schema\n");
161
162         /* Make sure we get this from the command line */
163         if (lpcfg_parm_bool(torture->lp_ctx, NULL, "become dc", "do not unjoin", false)) {
164                 talloc_free(s);
165                 return ret;
166         }
167
168 cleanup:
169         ZERO_STRUCT(u);
170         u.in.domain_dns_name            = torture_join_dom_dns_name(tj);
171         u.in.domain_netbios_name        = torture_join_dom_netbios_name(tj);
172         u.in.source_dsa_address         = address;
173         u.in.dest_dsa_netbios_name      = netbios_name;
174
175         status = libnet_UnbecomeDC(ctx, s, &u);
176         torture_assert_ntstatus_ok(torture, status, talloc_asprintf(torture,
177                                    "libnet_UnbecomeDC() failed - %s %s\n",
178                                    nt_errstr(status), u.out.error_string));
179
180         /* Leave domain. */
181         torture_leave_domain(torture, tj);
182
183         talloc_free(s);
184         return ret;
185 }