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