r9222: Rename smb_tree_connect() to smb_raw_tcon() to match other raw function
[garming/samba-autobuild/.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                 r.in.number_of_bytes = 0;
70                 r.out.data = NULL;
71
72                 status = dcerpc_eventlog_ReadEventLogW(p, mem_ctx, &r);
73
74                 if (NT_STATUS_EQUAL(r.out.result, NT_STATUS_END_OF_FILE)) {
75                         break;
76                 }
77
78                 if (!NT_STATUS_EQUAL(r.out.result, NT_STATUS_BUFFER_TOO_SMALL)) {
79                         printf("ReadEventLog failed - %s\n", nt_errstr(r.out.result));
80                         return False;
81                 }
82                 
83                 r.in.number_of_bytes = r.out.real_size;
84                 r.out.data = talloc_size(mem_ctx, r.in.number_of_bytes);
85
86                 status = dcerpc_eventlog_ReadEventLogW(p, mem_ctx, &r);
87
88                 if (!NT_STATUS_IS_OK(status)) {
89                         printf("ReadEventLog failed - %s\n", nt_errstr(status));
90                         return False;
91                 }
92                 
93                 r.in.offset++;
94         }
95
96         return True;
97 }
98
99 static BOOL test_CloseEventLog(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
100                                struct policy_handle *handle)
101 {
102         NTSTATUS status;
103         struct eventlog_CloseEventLog r;
104
105         r.in.handle = r.out.handle = handle;
106
107         printf("Testing CloseEventLog\n");
108
109         status = dcerpc_eventlog_CloseEventLog(p, mem_ctx, &r);
110
111         if (!NT_STATUS_IS_OK(status)) {
112                 printf("CloseEventLog failed - %s\n", nt_errstr(status));
113                 return False;
114         }
115
116         return True;
117 }
118
119 static BOOL test_FlushEventLog(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
120                                struct policy_handle *handle)
121 {
122         NTSTATUS status;
123         struct eventlog_FlushEventLog r;
124
125         r.in.handle = handle;
126
127         printf("Testing FlushEventLog\n");
128
129         status = dcerpc_eventlog_FlushEventLog(p, mem_ctx, &r);
130
131         /* Huh?  Does this RPC always return access denied? */
132
133         if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
134                 printf("FlushEventLog failed - %s\n", nt_errstr(status));
135                 return False;
136         }
137
138         return True;
139 }
140
141 static BOOL test_ClearEventLog(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
142                                struct policy_handle *handle)
143 {
144         NTSTATUS status;
145         struct eventlog_ClearEventLogW r;
146
147         r.in.handle = handle;
148         r.in.unknown = NULL;
149
150         printf("Testing ClearEventLog\n");
151
152         status = dcerpc_eventlog_ClearEventLogW(p, mem_ctx, &r);
153
154         if (!NT_STATUS_IS_OK(status)) {
155                 printf("ClearEventLog failed - %s\n", nt_errstr(status));
156                 return False;
157         }
158
159         return True;
160 }
161
162 static BOOL test_OpenEventLog(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
163                               struct policy_handle *handle)
164 {
165         NTSTATUS status;
166         struct eventlog_OpenEventLogW r;
167         struct eventlog_OpenUnknown0 unknown0;
168
169         printf("\ntesting OpenEventLog\n");
170
171         unknown0.unknown0 = 0x005c;
172         unknown0.unknown1 = 0x0001;
173
174         r.in.unknown0 = &unknown0;
175         init_lsa_String(&r.in.logname, "dns server");
176         init_lsa_String(&r.in.servername, NULL);
177         r.in.unknown2 = 0x00000001;
178         r.in.unknown3 = 0x00000001;
179         r.out.handle = handle;
180
181         status = dcerpc_eventlog_OpenEventLogW(p, mem_ctx, &r);
182
183         if (!NT_STATUS_IS_OK(status)) {
184                 printf("OpenEventLog failed - %s\n", nt_errstr(status));
185                 return False;
186         }
187
188         if (!NT_STATUS_IS_OK(r.out.result)) {
189                 printf("OpenEventLog failed - %s\n", nt_errstr(r.out.result));
190                 return False;
191         }
192
193         return True;
194 }
195
196 BOOL torture_rpc_eventlog(void)
197 {
198         NTSTATUS status;
199         struct dcerpc_pipe *p;
200         struct policy_handle handle;
201         TALLOC_CTX *mem_ctx;
202         BOOL ret = True;
203
204         mem_ctx = talloc_init("torture_rpc_atsvc");
205
206         status = torture_rpc_connection(mem_ctx, 
207                                         &p, 
208                                         DCERPC_EVENTLOG_NAME, 
209                                         DCERPC_EVENTLOG_UUID, 
210                                         DCERPC_EVENTLOG_VERSION);
211
212         if (!NT_STATUS_IS_OK(status)) {
213                 talloc_free(mem_ctx);
214                 return False;
215         }
216
217         if (!test_OpenEventLog(p, mem_ctx, &handle)) {
218                 talloc_free(mem_ctx);
219                 return False;
220         }
221
222 #if 0
223         ret &= test_ClearEventLog(p, mem_ctx, &handle); /* Destructive test */
224 #endif
225         
226         ret &= test_GetNumRecords(p, mem_ctx, &handle);
227
228         ret &= test_ReadEventLog(p, mem_ctx, &handle);
229
230         ret &= test_FlushEventLog(p, mem_ctx, &handle);
231
232         ret &= test_CloseEventLog(p, mem_ctx, &handle);
233
234         talloc_free(mem_ctx);
235
236         return ret;
237 }