s4-smbtorture: fix segfault in RPC-EVENTLOG test.
[kai/samba.git] / source4 / torture / rpc / eventlog.c
1 /* 
2    Unix SMB/CIFS implementation.
3    test suite for eventlog rpc operations
4
5    Copyright (C) Tim Potter 2003,2005
6    Copyright (C) Jelmer Vernooij 2004
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 "torture/torture.h"
24 #include "librpc/gen_ndr/ndr_eventlog.h"
25 #include "librpc/gen_ndr/ndr_eventlog_c.h"
26 #include "librpc/gen_ndr/ndr_lsa.h"
27 #include "torture/rpc/rpc.h"
28 #include "param/param.h"
29
30 static void init_lsa_String(struct lsa_String *name, const char *s)
31 {
32         name->string = s;
33         name->length = 2*strlen_m(s);
34         name->size = name->length;
35 }
36
37 static bool get_policy_handle(struct torture_context *tctx, 
38                                                           struct dcerpc_pipe *p,
39                                                           struct policy_handle *handle)
40 {
41         struct eventlog_OpenEventLogW r;
42         struct eventlog_OpenUnknown0 unknown0;
43         struct lsa_String logname, servername;
44
45         unknown0.unknown0 = 0x005c;
46         unknown0.unknown1 = 0x0001;
47
48         r.in.unknown0 = &unknown0;
49         init_lsa_String(&logname, "dns server");
50         init_lsa_String(&servername, NULL);
51         r.in.logname = &logname;
52         r.in.servername = &servername;
53         r.in.unknown2 = 0x00000001;
54         r.in.unknown3 = 0x00000001;
55         r.out.handle = handle;
56
57         torture_assert_ntstatus_ok(tctx, 
58                         dcerpc_eventlog_OpenEventLogW(p, tctx, &r), 
59                         "OpenEventLog failed");
60
61         torture_assert_ntstatus_ok(tctx, r.out.result, "OpenEventLog failed");
62
63         return true;
64 }
65
66
67
68 static bool test_GetNumRecords(struct torture_context *tctx, struct dcerpc_pipe *p)
69 {
70         struct eventlog_GetNumRecords r;
71         struct eventlog_CloseEventLog cr;
72         struct policy_handle handle;
73         uint32_t number = 0;
74
75         if (!get_policy_handle(tctx, p, &handle))
76                 return false;
77
78         ZERO_STRUCT(r);
79         r.in.handle = &handle;
80         r.out.number = &number;
81
82         torture_assert_ntstatus_ok(tctx, 
83                         dcerpc_eventlog_GetNumRecords(p, tctx, &r), 
84                         "GetNumRecords failed");
85
86         torture_comment(tctx, "%d records\n", *r.out.number);
87
88         cr.in.handle = cr.out.handle = &handle;
89
90         torture_assert_ntstatus_ok(tctx, 
91                                         dcerpc_eventlog_CloseEventLog(p, tctx, &cr), 
92                                         "CloseEventLog failed");
93         return true;
94 }
95
96 static bool test_ReadEventLog(struct torture_context *tctx, 
97                                                           struct dcerpc_pipe *p)
98 {
99         NTSTATUS status;
100         struct eventlog_ReadEventLogW r;
101         struct eventlog_CloseEventLog cr;
102         struct policy_handle handle;
103
104         if (!get_policy_handle(tctx, p, &handle))
105                 return false;
106
107         ZERO_STRUCT(r);
108         r.in.offset = 0;
109         r.in.handle = &handle;
110         r.in.flags = EVENTLOG_BACKWARDS_READ|EVENTLOG_SEQUENTIAL_READ;
111
112         while (1) {
113                 DATA_BLOB blob;
114                 struct eventlog_Record rec;
115                 struct ndr_pull *ndr;
116                 enum ndr_err_code ndr_err;
117                 uint32_t sent_size = 0;
118                 uint32_t real_size = 0;
119
120                 /* Read first for number of bytes in record */
121
122                 r.in.number_of_bytes = 0;
123                 r.out.data = NULL;
124                 r.out.sent_size = &sent_size;
125                 r.out.real_size = &real_size;
126
127                 status = dcerpc_eventlog_ReadEventLogW(p, tctx, &r);
128
129                 if (NT_STATUS_EQUAL(r.out.result, NT_STATUS_END_OF_FILE)) {
130                         break;
131                 }
132
133                 torture_assert_ntstatus_equal(tctx, r.out.result, NT_STATUS_BUFFER_TOO_SMALL,
134                         "ReadEventLog failed");
135                 
136                 /* Now read the actual record */
137
138                 r.in.number_of_bytes = *r.out.real_size;
139                 r.out.data = talloc_array(tctx, uint8_t, r.in.number_of_bytes);
140
141                 status = dcerpc_eventlog_ReadEventLogW(p, tctx, &r);
142
143                 torture_assert_ntstatus_ok(tctx, status, "ReadEventLog failed");
144                 
145                 /* Decode a user-marshalled record */
146
147                 blob.length = *r.out.sent_size;
148                 blob.data = talloc_steal(tctx, r.out.data);
149
150                 ndr = ndr_pull_init_blob(&blob, tctx, lp_iconv_convenience(tctx->lp_ctx));
151
152                 ndr_err = ndr_pull_eventlog_Record(
153                         ndr, NDR_SCALARS|NDR_BUFFERS, &rec);
154                 status = ndr_map_error2ntstatus(ndr_err);
155
156                 NDR_PRINT_DEBUG(eventlog_Record, &rec);
157
158                 torture_assert_ntstatus_ok(tctx, status, 
159                                 "ReadEventLog failed parsing event log record");
160
161                 r.in.offset++;
162         }
163
164         cr.in.handle = cr.out.handle = &handle;
165
166         torture_assert_ntstatus_ok(tctx, 
167                                         dcerpc_eventlog_CloseEventLog(p, tctx, &cr), 
168                                         "CloseEventLog failed");
169
170         return true;
171 }
172
173 static bool test_FlushEventLog(struct torture_context *tctx, 
174                                                            struct dcerpc_pipe *p)
175 {
176         struct eventlog_FlushEventLog r;
177         struct eventlog_CloseEventLog cr;
178         struct policy_handle handle;
179
180         if (!get_policy_handle(tctx, p, &handle))
181                 return false;
182
183         r.in.handle = &handle;
184
185         /* Huh?  Does this RPC always return access denied? */
186         torture_assert_ntstatus_equal(tctx, 
187                         dcerpc_eventlog_FlushEventLog(p, tctx, &r),
188                         NT_STATUS_ACCESS_DENIED, 
189                         "FlushEventLog failed");
190
191         cr.in.handle = cr.out.handle = &handle;
192
193         torture_assert_ntstatus_ok(tctx, 
194                                         dcerpc_eventlog_CloseEventLog(p, tctx, &cr), 
195                                         "CloseEventLog failed");
196
197         return true;
198 }
199
200 static bool test_ClearEventLog(struct torture_context *tctx, 
201                                struct dcerpc_pipe *p)
202 {
203         struct eventlog_ClearEventLogW r;
204         struct eventlog_CloseEventLog cr;
205         struct policy_handle handle;
206
207         if (!get_policy_handle(tctx, p, &handle))
208                 return false;
209
210         r.in.handle = &handle;
211         r.in.backupfile = NULL;
212
213         torture_assert_ntstatus_ok(tctx, 
214                         dcerpc_eventlog_ClearEventLogW(p, tctx, &r), 
215                         "ClearEventLog failed");
216
217         cr.in.handle = cr.out.handle = &handle;
218
219         torture_assert_ntstatus_ok(tctx, 
220                                         dcerpc_eventlog_CloseEventLog(p, tctx, &cr), 
221                                         "CloseEventLog failed");
222
223         return true;
224 }
225
226 static bool test_OpenEventLog(struct torture_context *tctx, 
227                                                           struct dcerpc_pipe *p)
228 {
229         struct policy_handle handle;
230         struct eventlog_CloseEventLog cr;
231
232         if (!get_policy_handle(tctx, p, &handle))
233                 return false;
234
235         cr.in.handle = cr.out.handle = &handle;
236
237         torture_assert_ntstatus_ok(tctx, 
238                                         dcerpc_eventlog_CloseEventLog(p, tctx, &cr), 
239                                         "CloseEventLog failed");
240
241         return true;
242 }
243
244 struct torture_suite *torture_rpc_eventlog(TALLOC_CTX *mem_ctx)
245 {
246         struct torture_suite *suite;
247         struct torture_rpc_tcase *tcase;
248         struct torture_test *test;
249
250         suite = torture_suite_create(mem_ctx, "EVENTLOG");
251         tcase = torture_suite_add_rpc_iface_tcase(suite, "eventlog", 
252                                                   &ndr_table_eventlog);
253
254         torture_rpc_tcase_add_test(tcase, "OpenEventLog", test_OpenEventLog);
255         test = torture_rpc_tcase_add_test(tcase, "ClearEventLog", 
256                                           test_ClearEventLog);
257         test->dangerous = true;
258         torture_rpc_tcase_add_test(tcase, "GetNumRecords", test_GetNumRecords);
259         torture_rpc_tcase_add_test(tcase, "ReadEventLog", test_ReadEventLog);
260         torture_rpc_tcase_add_test(tcase, "FlushEventLog", test_FlushEventLog);
261
262         return suite;
263 }