r23792: convert Samba4 to GPLv3
[sfrench/samba-autobuild/.git] / source4 / torture / ldap / uptodatevector.c
1 /* 
2    Unix SMB/CIFS mplementation.
3    LDAP replUpToDateVector tests
4    
5    Copyright (C) Stefan Metzmacher 2007
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
22 #include "includes.h"
23 #include "libcli/ldap/ldap_client.h"
24 #include "lib/cmdline/popt_common.h"
25 #include "db_wrap.h"
26 #include "lib/ldb/include/ldb.h"
27 #include "lib/ldb/include/ldb_errors.h"
28 #include "dsdb/samdb/samdb.h"
29 #include "lib/util/dlinklist.h"
30
31 #include "torture/torture.h"
32 #include "torture/ldap/proto.h"
33
34 #include "librpc/ndr/libndr.h"
35 #include "librpc/gen_ndr/ndr_drsblobs.h"
36
37 static bool test_check_uptodatevector(struct torture_context *torture,
38                                       struct ldb_context *ldb,
39                                       struct ldb_dn *partition_dn)
40 {
41         bool ok = true;
42         uint32_t i;
43         int ret;
44         NTSTATUS status;
45         struct ldb_result *r;
46         const struct ldb_val *utdv_val1;
47         struct replUpToDateVectorBlob utdv1;
48         static const char *attrs[] = {
49                 "uSNChanged",
50                 "replUpToDateVector",
51                 "description",
52                 NULL
53         };
54
55         torture_comment(torture, "Check replUpToDateVector on partition[%s]\n",
56                                  ldb_dn_get_linearized(partition_dn));
57
58         ret = ldb_search(ldb, partition_dn, LDB_SCOPE_BASE, 
59                          "(objectClass=*)", attrs, &r);
60         if (ret != LDB_SUCCESS) {
61                 return false;
62         } else if (r->count != 1) {
63                 talloc_free(r);
64                 return false;
65         }
66         talloc_steal(torture, r);
67
68         ZERO_STRUCT(utdv1);
69         utdv_val1 = ldb_msg_find_ldb_val(r->msgs[0], "replUpToDateVector");
70         if (utdv_val1) {
71                 status = ndr_pull_struct_blob_all(utdv_val1, torture, &utdv1,
72                                                  (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
73                 if (!NT_STATUS_IS_OK(status)) {
74                         return false;
75                 }
76         }
77
78         for (i=0; i < 2; i++) {
79                 const struct ldb_val *utdv_val;
80                 struct replUpToDateVectorBlob utdv;
81                 struct ldb_message *msg;
82                 char *description;
83                 uint32_t j;
84                 bool no_match = false;
85
86                 /* make a 'modify' msg, and only for serverReference */
87                 msg = ldb_msg_new(torture);
88                 if (!msg) return false;
89                 msg->dn = partition_dn;
90
91                 description = talloc_asprintf(msg, "torture replUpToDateVector[%u]", i);
92                 if (!description) return false;
93
94                 ret = ldb_msg_add_string(msg, "description", description);
95                 if (ret != 0) return false;
96
97                 for (j=0;j<msg->num_elements;j++) {
98                         msg->elements[j].flags = LDB_FLAG_MOD_REPLACE;
99                 }
100
101                 ret = ldb_modify(ldb, msg);
102                 if (ret != LDB_SUCCESS) return false;
103
104                 ret = ldb_search(ldb, partition_dn, LDB_SCOPE_BASE, 
105                                  "(objectClass=*)", attrs, &r);
106                 if (ret != LDB_SUCCESS) {
107                         return false;
108                 } else if (r->count != 1) {
109                         talloc_free(r);
110                         return false;
111                 }
112                 talloc_steal(msg, r);
113
114                 ZERO_STRUCT(utdv);
115                 utdv_val = ldb_msg_find_ldb_val(r->msgs[0], "replUpToDateVector");
116                 if (utdv_val) {
117                         status = ndr_pull_struct_blob_all(utdv_val, torture, &utdv,
118                                                          (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob);
119                         if (!NT_STATUS_IS_OK(status)) {
120                                 return false;
121                         }
122                 }
123
124                 if (!utdv_val1 && utdv_val) {
125                         no_match = true;
126                 } else if (utdv_val1 && !utdv_val) {
127                         no_match = true;
128                 } else if (utdv_val1->length != utdv_val->length) {
129                         no_match = true;
130                 } else if (utdv_val1->length && memcmp(utdv_val1->data, utdv_val->data, utdv_val->length) != 0) {
131                         no_match = true;
132                 }
133
134                 torture_comment(torture, "[%u]: uSNChanged[%llu] description[%s] replUpToDateVector[%s]\n", i,
135                                         samdb_result_uint64(r->msgs[0], "uSNChanged", 0),
136                                         samdb_result_string(r->msgs[0], "description", NULL),
137                                         (no_match ? "changed!: not ok" : "not changed: ok"));
138
139                 if (no_match) {
140                         NDR_PRINT_DEBUG(replUpToDateVectorBlob, &utdv1);
141                         NDR_PRINT_DEBUG(replUpToDateVectorBlob, &utdv);
142                         ok = false;
143                 }
144
145                 talloc_free(msg);
146         }
147
148         return ok;
149 }
150
151 BOOL torture_ldap_uptodatevector(struct torture_context *torture)
152 {
153         struct ldb_context *ldb;
154         BOOL ret = True;
155         const char *host = torture_setting_string(torture, "host", NULL);
156         char *url;
157
158         url = talloc_asprintf(torture, "ldap://%s/", host);
159         if (!url) goto failed;
160
161         ldb = ldb_wrap_connect(torture, url,
162                                NULL,
163                                cmdline_credentials,
164                                0, NULL);
165         if (!ldb) goto failed;
166
167         ret &= test_check_uptodatevector(torture, ldb, samdb_base_dn(ldb));
168         ret &= test_check_uptodatevector(torture, ldb, samdb_config_dn(ldb));
169         ret &= test_check_uptodatevector(torture, ldb, samdb_schema_dn(ldb));
170
171         return ret;
172 failed:
173         return False;
174 }