r23779: Change from v2 or later to v3 or later.
[amitay/samba.git] / source3 / rpc_parse / parse_eventlog.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
4  *  Copyright (C) Marcin Krzysztof Porwit    2005.
5  *  
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *  
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *  
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20  
21 #include "includes.h"
22
23 #undef DBGC_CLASS
24 #define DBGC_CLASS DBGC_RPC_PARSE
25
26 /********************************************************************
27 ********************************************************************/
28
29 BOOL prs_ev_open_unknown0( const char *desc, prs_struct *ps, int depth, EVENTLOG_OPEN_UNKNOWN0 *u )
30 {
31         if ( !u )
32                 return False;
33         
34         if ( !prs_uint16("", ps, depth, &u->unknown1) )
35                 return False;
36         if ( !prs_uint16("", ps, depth, &u->unknown2) )
37                 return False;
38
39         return True;
40 }
41
42 /********************************************************************
43 ********************************************************************/
44
45 BOOL eventlog_io_q_open_eventlog(const char *desc, EVENTLOG_Q_OPEN_EVENTLOG *q_u,
46                                  prs_struct *ps, int depth)
47 {
48         if(q_u == NULL)
49                 return False;
50     
51         prs_debug(ps, depth, desc, "eventlog_io_q_open_eventlog");
52         depth++;
53
54         if(!prs_align(ps))
55                 return False;
56
57         if ( !prs_pointer("", ps, depth, (void*)&q_u->unknown0, sizeof(EVENTLOG_OPEN_UNKNOWN0), (PRS_POINTER_CAST)prs_ev_open_unknown0))
58                 return False;
59
60         if ( !prs_unistr4("logname", ps, depth, &q_u->logname) )
61                 return False;
62         if ( !prs_align(ps) )
63                 return False;
64
65         if ( !prs_unistr4("servername", ps, depth, &q_u->servername) )
66                 return False;
67         if ( !prs_align(ps) )
68                 return False;
69
70         if ( !prs_uint32("unknown1", ps, depth, &q_u->unknown1) )
71                 return False;
72         if ( !prs_uint32("unknown2", ps, depth, &q_u->unknown2) )
73                 return False;
74
75         return True;
76 }
77
78 BOOL eventlog_io_r_open_eventlog(const char *desc, EVENTLOG_R_OPEN_EVENTLOG *r_u,
79                                  prs_struct *ps, int depth)
80 {
81         if(r_u == NULL)
82                 return False;
83
84         prs_debug(ps, depth, desc, "eventlog_io_r_open_eventlog");
85         depth++;
86
87         if(!prs_align(ps))
88                 return False;
89
90         if(!(smb_io_pol_hnd("log handle", &(r_u->handle), ps, depth)))
91                 return False;
92
93         if(!(prs_ntstatus("status code", ps, depth, &r_u->status)))
94                 return False;
95
96         return True;
97 }
98
99 BOOL eventlog_io_q_get_num_records(const char *desc, EVENTLOG_Q_GET_NUM_RECORDS *q_u,
100                                    prs_struct *ps, int depth)
101 {
102         if(q_u == NULL)
103                 return False;
104
105         prs_debug(ps, depth, desc, "eventlog_io_q_get_num_records");
106         depth++;
107
108         if(!(prs_align(ps)))
109                 return False;
110
111         if(!(smb_io_pol_hnd("log handle", &(q_u->handle), ps, depth)))
112                 return False;
113     
114         return True;
115 }
116
117 BOOL eventlog_io_r_get_num_records(const char *desc, EVENTLOG_R_GET_NUM_RECORDS *r_u,
118                                    prs_struct *ps, int depth)
119 {
120         if(r_u == NULL)
121                 return False;
122
123         prs_debug(ps, depth, desc, "eventlog_io_r_get_num_records");
124         depth++;
125
126         if(!(prs_align(ps)))
127                 return False;
128
129         if(!(prs_uint32("num records", ps, depth, &(r_u->num_records))))
130                 return False;
131
132         if(!(prs_ntstatus("status code", ps, depth, &r_u->status)))
133                 return False;
134
135         return True;
136 }
137
138 BOOL eventlog_io_q_get_oldest_entry(const char *desc, EVENTLOG_Q_GET_OLDEST_ENTRY *q_u,
139                                     prs_struct *ps, int depth)
140 {
141         if(q_u == NULL)
142                 return False;
143
144         prs_debug(ps, depth, desc, "eventlog_io_q_get_oldest_entry");
145         depth++;
146     
147         if(!(prs_align(ps)))
148                 return False;
149
150         if(!(smb_io_pol_hnd("log handle", &(q_u->handle), ps, depth)))
151                 return False;
152
153         return True;
154 }
155
156 BOOL eventlog_io_r_get_oldest_entry(const char *desc, EVENTLOG_R_GET_OLDEST_ENTRY *r_u,
157                                     prs_struct *ps, int depth)
158 {
159         if(r_u == NULL)
160                 return False;
161
162         prs_debug(ps, depth, desc, "eventlog_io_r_get_oldest_entry");
163         depth++;
164
165         if(!(prs_align(ps)))
166                 return False;
167
168         if(!(prs_uint32("oldest entry", ps, depth, &(r_u->oldest_entry))))
169                 return False;
170
171         if(!(prs_ntstatus("status code", ps, depth, &r_u->status)))
172                 return False;
173
174         return True;
175 }
176
177 BOOL eventlog_io_q_read_eventlog(const char *desc, EVENTLOG_Q_READ_EVENTLOG *q_u,
178                                  prs_struct *ps, int depth)
179 {
180         if(q_u == NULL)
181                 return False;
182
183         prs_debug(ps, depth, desc, "eventlog_io_q_read_eventlog");
184         depth++;
185
186         if(!(prs_align(ps)))
187                 return False;
188
189         if(!(smb_io_pol_hnd("log handle", &(q_u->handle), ps, depth)))
190                 return False;
191
192         if(!(prs_uint32("read flags", ps, depth, &(q_u->flags))))
193                 return False;
194
195         if(!(prs_uint32("read offset", ps, depth, &(q_u->offset))))
196                 return False;
197
198         if(!(prs_uint32("read buf size", ps, depth, &(q_u->max_read_size))))
199                 return False;
200
201         return True;
202 }
203 /** Structure of response seems to be:
204    DWORD num_bytes_in_resp -- MUST be the same as q_u->max_read_size
205    for i=0..n
206        EVENTLOGRECORD record
207    DWORD sent_size -- sum of EVENTLOGRECORD lengths if records returned, 0 otherwise
208    DWORD real_size -- 0 if records returned, otherwise length of next record to be returned
209    WERROR status */
210 BOOL eventlog_io_r_read_eventlog(const char *desc,
211                                  EVENTLOG_Q_READ_EVENTLOG *q_u,
212                                  EVENTLOG_R_READ_EVENTLOG *r_u,
213                                  prs_struct *ps,
214                                  int depth)
215 {
216         Eventlog_entry *entry;
217         uint32 record_written = 0;
218         uint32 record_total = 0;
219
220         if(r_u == NULL)
221                 return False;
222
223         prs_debug(ps, depth, desc, "eventlog_io_r_read_eventlog");
224         depth++;
225
226         /* First, see if we've read more logs than we can output */
227
228         if(r_u->num_bytes_in_resp > q_u->max_read_size) {
229                 entry = r_u->entry;
230
231                 /* remove the size of the last entry from the list */
232
233                 while(entry->next != NULL)
234                         entry = entry->next;
235
236                 r_u->num_bytes_in_resp -= entry->record.length;
237
238                 /* do not output the last log entry */
239         
240                 r_u->num_records--;
241         }
242     
243         entry = r_u->entry;
244         record_total = r_u->num_records;
245
246         if(r_u->num_bytes_in_resp != 0)
247                 r_u->sent_size = r_u->num_bytes_in_resp;
248         else
249                 r_u->real_size = r_u->bytes_in_next_record;
250
251         if(!(prs_align(ps)))
252                 return False;
253         if(!(prs_uint32("bytes in resp", ps, depth, &(q_u->max_read_size))))
254                 return False;
255
256         while(entry != NULL && record_written < record_total)
257         {
258                 DEBUG(11, ("eventlog_io_r_read_eventlog: writing record [%d] out of [%d].\n", record_written, record_total));
259
260                 /* Encode the actual eventlog record record */
261
262                 if(!(prs_uint32("length", ps, depth, &(entry->record.length))))
263                         return False;
264                 if(!(prs_uint32("reserved", ps, depth, &(entry->record.reserved1))))
265                         return False;
266                 if(!(prs_uint32("record number", ps, depth, &(entry->record.record_number))))
267                         return False;
268                 if(!(prs_uint32("time generated", ps, depth, &(entry->record.time_generated))))
269                         return False;
270                 if(!(prs_uint32("time written", ps, depth, &(entry->record.time_written))))
271                         return False;
272                 if(!(prs_uint32("event id", ps, depth, &(entry->record.event_id))))
273                         return False;
274                 if(!(prs_uint16("event type", ps, depth, &(entry->record.event_type))))
275                         return False;
276                 if(!(prs_uint16("num strings", ps, depth, &(entry->record.num_strings))))
277                         return False;
278                 if(!(prs_uint16("event category", ps, depth, &(entry->record.event_category))))
279                         return False;
280                 if(!(prs_uint16("reserved2", ps, depth, &(entry->record.reserved2))))
281                         return False;
282                 if(!(prs_uint32("closing record", ps, depth, &(entry->record.closing_record_number))))
283                         return False;
284                 if(!(prs_uint32("string offset", ps, depth, &(entry->record.string_offset))))
285                         return False;
286                 if(!(prs_uint32("user sid length", ps, depth, &(entry->record.user_sid_length))))
287                         return False;
288                 if(!(prs_uint32("user sid offset", ps, depth, &(entry->record.user_sid_offset))))
289                         return False;
290                 if(!(prs_uint32("data length", ps, depth, &(entry->record.data_length))))
291                         return False;
292                 if(!(prs_uint32("data offset", ps, depth, &(entry->record.data_offset))))
293                         return False;
294                 if(!(prs_align(ps)))
295                         return False;
296         
297                 /* Now encoding data */
298
299                 if(!(prs_uint8s(False, "buffer", ps, depth, entry->data, 
300                         entry->record.length - sizeof(Eventlog_record) - sizeof(entry->record.length))))
301                 {
302                         return False;
303                 }
304
305                 if(!(prs_align(ps)))
306                         return False;
307                 if(!(prs_uint32("length 2", ps, depth, &(entry->record.length))))
308                         return False;
309
310                 entry = entry->next;
311                 record_written++;
312
313         }       /* end of encoding EVENTLOGRECORD */
314
315         /* Now pad with whitespace until the end of the response buffer */
316
317         if (q_u->max_read_size - r_u->num_bytes_in_resp) {
318                 if (!r_u->end_of_entries_padding) {
319                         return False;
320                 }
321
322                 if(!(prs_uint8s(False, "end of entries padding", ps, 
323                                 depth, r_u->end_of_entries_padding,
324                                 (q_u->max_read_size - r_u->num_bytes_in_resp)))) {
325                         free(r_u->end_of_entries_padding);
326                         return False;
327                 }
328
329                 free(r_u->end_of_entries_padding);
330         }
331
332         /* We had better be DWORD aligned here */
333
334         if(!(prs_uint32("sent size", ps, depth, &(r_u->sent_size))))
335                 return False;
336         if(!(prs_uint32("real size", ps, depth, &(r_u->real_size))))
337                 return False;
338         if(!(prs_ntstatus("status code", ps, depth, &r_u->status)))
339                 return False;
340
341         return True;
342 }
343
344 /** The windows client seems to be doing something funny with the file name
345    A call like
346       ClearEventLog(handle, "backup_file")
347    on the client side will result in the backup file name looking like this on the
348    server side:
349       \??\${CWD of client}\backup_file
350    If an absolute path gets specified, such as
351       ClearEventLog(handle, "C:\\temp\\backup_file")
352    then it is still mangled by the client into this:
353       \??\C:\temp\backup_file
354    when it is on the wire.
355    I'm not sure where the \?? is coming from, or why the ${CWD} of the client process
356    would be added in given that the backup file gets written on the server side. */
357
358 BOOL eventlog_io_q_clear_eventlog(const char *desc, EVENTLOG_Q_CLEAR_EVENTLOG *q_u,
359                                   prs_struct *ps, int depth)
360 {
361         if(q_u == NULL)
362                 return False;
363
364         prs_debug(ps, depth, desc, "eventlog_io_q_clear_eventlog");
365         depth++;
366
367         if(!prs_align(ps))
368                 return False;
369         if(!(smb_io_pol_hnd("log handle", &(q_u->handle), ps, depth)))
370                 return False;
371
372         if ( !prs_unistr4("backupfile", ps, depth, &q_u->backupfile) )
373                 return False;
374
375         return True;
376
377 }
378
379 BOOL eventlog_io_r_clear_eventlog(const char *desc, EVENTLOG_R_CLEAR_EVENTLOG *r_u,
380                                   prs_struct *ps, int depth)
381 {
382         if(r_u == NULL)
383                 return False;
384
385         prs_debug(ps, depth, desc, "eventlog_io_r_clear_eventlog");
386         depth++;
387
388         if(!prs_align(ps))
389                 return False;
390         if(!(prs_ntstatus("status code", ps, depth, &r_u->status)))
391                 return False;
392
393         return True;
394 }