r14379: Build torture/rpc/ as a seperate smbtorture module. Move helper
[ira/wip.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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "torture/torture.h"
25 #include "librpc/gen_ndr/ndr_eventlog.h"
26 #include "librpc/gen_ndr/ndr_lsa.h"
27 #include "torture/rpc/rpc.h"
28
29 static void init_lsa_String(struct lsa_String *name, const char *s)
30 {
31         name->string = s;
32         name->length = 2*strlen_m(s);
33         name->size = name->length;
34 }
35
36 static BOOL test_GetNumRecords(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
37                                struct policy_handle *handle)
38 {
39         NTSTATUS status;
40         struct eventlog_GetNumRecords r;
41
42         printf("\ntesting GetNumRecords\n");
43
44         r.in.handle = handle;
45
46         status = dcerpc_eventlog_GetNumRecords(p, mem_ctx, &r);
47
48         if (!NT_STATUS_IS_OK(status)) {
49                 printf("GetNumRecords failed - %s\n", nt_errstr(status));
50                 return False;
51         }
52
53         printf("%d records\n", r.out.number);
54
55         return True;
56 }
57
58 static BOOL test_ReadEventLog(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
59                               struct policy_handle *handle)
60 {
61         NTSTATUS status;
62         struct eventlog_ReadEventLogW r;
63
64         printf("\ntesting ReadEventLog\n");
65
66         r.in.offset = 0;
67         r.in.handle = handle;
68         r.in.flags = EVENTLOG_BACKWARDS_READ|EVENTLOG_SEQUENTIAL_READ;
69
70         while (1) {
71                 DATA_BLOB blob;
72                 struct eventlog_Record rec;
73                 struct ndr_pull *ndr;
74
75                 /* Read first for number of bytes in record */
76
77                 r.in.number_of_bytes = 0;
78                 r.out.data = NULL;
79
80                 status = dcerpc_eventlog_ReadEventLogW(p, mem_ctx, &r);
81
82                 if (NT_STATUS_EQUAL(r.out.result, NT_STATUS_END_OF_FILE)) {
83                         break;
84                 }
85
86                 if (!NT_STATUS_EQUAL(r.out.result, NT_STATUS_BUFFER_TOO_SMALL)) {
87                         printf("ReadEventLog failed - %s\n", nt_errstr(r.out.result));
88                         return False;
89                 }
90                 
91                 /* Now read the actual record */
92
93                 r.in.number_of_bytes = r.out.real_size;
94                 r.out.data = talloc_size(mem_ctx, r.in.number_of_bytes);
95
96                 status = dcerpc_eventlog_ReadEventLogW(p, mem_ctx, &r);
97
98                 if (!NT_STATUS_IS_OK(status)) {
99                         printf("ReadEventLog failed - %s\n", nt_errstr(status));
100                         return False;
101                 }
102                 
103                 /* Decode a user-marshalled record */
104
105                 blob.length = r.out.sent_size;
106                 blob.data = talloc_steal(mem_ctx, r.out.data);
107
108                 ndr = ndr_pull_init_blob(&blob, mem_ctx);
109
110                 status = ndr_pull_eventlog_Record(
111                         ndr, NDR_SCALARS|NDR_BUFFERS, &rec);
112
113                 NDR_PRINT_DEBUG(eventlog_Record, &rec);
114
115                 if (!NT_STATUS_IS_OK(status)) {
116                         printf("ReadEventLog failed parsing event log record "
117                                "- %s\n", nt_errstr(status));
118                         return False;
119                 }
120
121                 r.in.offset++;
122         }
123
124         return True;
125 }
126
127 static BOOL test_CloseEventLog(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
128                                struct policy_handle *handle)
129 {
130         NTSTATUS status;
131         struct eventlog_CloseEventLog r;
132
133         r.in.handle = r.out.handle = handle;
134
135         printf("Testing CloseEventLog\n");
136
137         status = dcerpc_eventlog_CloseEventLog(p, mem_ctx, &r);
138
139         if (!NT_STATUS_IS_OK(status)) {
140                 printf("CloseEventLog failed - %s\n", nt_errstr(status));
141                 return False;
142         }
143
144         return True;
145 }
146
147 static BOOL test_FlushEventLog(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
148                                struct policy_handle *handle)
149 {
150         NTSTATUS status;
151         struct eventlog_FlushEventLog r;
152
153         r.in.handle = handle;
154
155         printf("Testing FlushEventLog\n");
156
157         status = dcerpc_eventlog_FlushEventLog(p, mem_ctx, &r);
158
159         /* Huh?  Does this RPC always return access denied? */
160
161         if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
162                 printf("FlushEventLog failed - %s\n", nt_errstr(status));
163                 return False;
164         }
165
166         return True;
167 }
168
169 static BOOL test_ClearEventLog(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
170                                struct policy_handle *handle)
171 {
172         NTSTATUS status;
173         struct eventlog_ClearEventLogW r;
174
175         r.in.handle = handle;
176         r.in.unknown = NULL;
177
178         printf("Testing ClearEventLog\n");
179
180         status = dcerpc_eventlog_ClearEventLogW(p, mem_ctx, &r);
181
182         if (!NT_STATUS_IS_OK(status)) {
183                 printf("ClearEventLog failed - %s\n", nt_errstr(status));
184                 return False;
185         }
186
187         return True;
188 }
189
190 static BOOL test_OpenEventLog(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
191                               struct policy_handle *handle)
192 {
193         NTSTATUS status;
194         struct eventlog_OpenEventLogW r;
195         struct eventlog_OpenUnknown0 unknown0;
196
197         printf("\ntesting OpenEventLog\n");
198
199         unknown0.unknown0 = 0x005c;
200         unknown0.unknown1 = 0x0001;
201
202         r.in.unknown0 = &unknown0;
203         init_lsa_String(&r.in.logname, "dns server");
204         init_lsa_String(&r.in.servername, NULL);
205         r.in.unknown2 = 0x00000001;
206         r.in.unknown3 = 0x00000001;
207         r.out.handle = handle;
208
209         status = dcerpc_eventlog_OpenEventLogW(p, mem_ctx, &r);
210
211         if (!NT_STATUS_IS_OK(status)) {
212                 printf("OpenEventLog failed - %s\n", nt_errstr(status));
213                 return False;
214         }
215
216         if (!NT_STATUS_IS_OK(r.out.result)) {
217                 printf("OpenEventLog failed - %s\n", nt_errstr(r.out.result));
218                 return False;
219         }
220
221         return True;
222 }
223
224 BOOL torture_rpc_eventlog(void)
225 {
226         NTSTATUS status;
227         struct dcerpc_pipe *p;
228         struct policy_handle handle;
229         TALLOC_CTX *mem_ctx;
230         BOOL ret = True;
231
232         mem_ctx = talloc_init("torture_rpc_atsvc");
233
234         status = torture_rpc_connection(mem_ctx, &p, &dcerpc_table_eventlog);
235
236         if (!NT_STATUS_IS_OK(status)) {
237                 talloc_free(mem_ctx);
238                 return False;
239         }
240
241         if (!test_OpenEventLog(p, mem_ctx, &handle)) {
242                 talloc_free(mem_ctx);
243                 return False;
244         }
245
246 #if 0
247         ret &= test_ClearEventLog(p, mem_ctx, &handle); /* Destructive test */
248 #endif
249         
250         ret &= test_GetNumRecords(p, mem_ctx, &handle);
251
252         ret &= test_ReadEventLog(p, mem_ctx, &handle);
253
254         ret &= test_FlushEventLog(p, mem_ctx, &handle);
255
256         ret &= test_CloseEventLog(p, mem_ctx, &handle);
257
258         talloc_free(mem_ctx);
259
260         return ret;
261 }