libcli/cldap: make use of samba_tevent_context_init()
[Samba/gebeck_regimport.git] / source3 / lib / eventlog / eventlog.c
blob0cc0240bcc29b9d855c6af37659496a81751b19b
1 /*
2 * Unix SMB/CIFS implementation.
3 * Eventlog utility routines
4 * Copyright (C) Marcin Krzysztof Porwit 2005,
5 * Copyright (C) Brian Moran 2005.
6 * Copyright (C) Gerald (Jerry) Carter 2005.
7 * Copyright (C) Guenther Deschner 2009.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "system/filesys.h"
25 #include "lib/eventlog/eventlog.h"
26 #include "../libcli/security/security.h"
27 #include "util_tdb.h"
29 /* maintain a list of open eventlog tdbs with reference counts */
31 static ELOG_TDB *open_elog_list;
33 /********************************************************************
34 Init an Eventlog TDB, and return it. If null, something bad
35 happened.
36 ********************************************************************/
38 TDB_CONTEXT *elog_init_tdb( char *tdbfilename )
40 TDB_CONTEXT *tdb;
42 DEBUG(10,("elog_init_tdb: Initializing eventlog tdb (%s)\n",
43 tdbfilename));
45 tdb = tdb_open_log( tdbfilename, 0, TDB_DEFAULT,
46 O_RDWR|O_CREAT|O_TRUNC, 0660 );
48 if ( !tdb ) {
49 DEBUG( 0, ( "Can't open tdb for [%s]\n", tdbfilename ) );
50 return NULL;
53 /* initialize with defaults, copy real values in here from registry */
55 tdb_store_int32( tdb, EVT_OLDEST_ENTRY, 1 );
56 tdb_store_int32( tdb, EVT_NEXT_RECORD, 1 );
57 tdb_store_int32( tdb, EVT_MAXSIZE, 0x80000 );
58 tdb_store_int32( tdb, EVT_RETENTION, 0x93A80 );
60 tdb_store_int32( tdb, EVT_VERSION, EVENTLOG_DATABASE_VERSION_V1 );
62 return tdb;
65 /********************************************************************
66 make the tdb file name for an event log, given destination buffer
67 and size. Caller must free memory.
68 ********************************************************************/
70 char *elog_tdbname(TALLOC_CTX *ctx, const char *name )
72 char *path;
73 char *file;
74 char *tdbname;
76 path = talloc_strdup(ctx, state_path("eventlog"));
77 if (!path) {
78 return NULL;
81 file = talloc_asprintf_strlower_m(path, "%s.tdb", name);
82 if (!file) {
83 talloc_free(path);
84 return NULL;
87 tdbname = talloc_asprintf(path, "%s/%s", state_path("eventlog"), file);
88 if (!tdbname) {
89 talloc_free(path);
90 return NULL;
93 return tdbname;
97 /********************************************************************
98 this function is used to count up the number of bytes in a
99 particular TDB
100 ********************************************************************/
102 struct trav_size_struct {
103 int size;
104 int rec_count;
107 static int eventlog_tdb_size_fn( TDB_CONTEXT * tdb, TDB_DATA key, TDB_DATA data,
108 void *state )
110 struct trav_size_struct *tsize = (struct trav_size_struct *)state;
112 tsize->size += data.dsize;
113 tsize->rec_count++;
115 return 0;
118 /********************************************************************
119 returns the size of the eventlog, and if MaxSize is a non-null
120 ptr, puts the MaxSize there. This is purely a way not to have yet
121 another function that solely reads the maxsize of the eventlog.
122 Yeah, that's it.
123 ********************************************************************/
125 int elog_tdb_size( TDB_CONTEXT * tdb, int *MaxSize, int *Retention )
127 struct trav_size_struct tsize;
129 if ( !tdb )
130 return 0;
132 ZERO_STRUCT( tsize );
134 tdb_traverse( tdb, eventlog_tdb_size_fn, &tsize );
136 if ( MaxSize != NULL ) {
137 *MaxSize = tdb_fetch_int32( tdb, EVT_MAXSIZE );
140 if ( Retention != NULL ) {
141 *Retention = tdb_fetch_int32( tdb, EVT_RETENTION );
144 DEBUG( 1,
145 ( "eventlog size: [%d] for [%d] records\n", tsize.size,
146 tsize.rec_count ) );
147 return tsize.size;
150 /********************************************************************
151 Discard early event logs until we have enough for 'needed' bytes...
152 NO checking done beforehand to see that we actually need to do
153 this, and it's going to pluck records one-by-one. So, it's best
154 to determine that this needs to be done before doing it.
156 Setting whack_by_date to True indicates that eventlogs falling
157 outside of the retention range need to go...
159 return True if we made enough room to accommodate needed bytes
160 ********************************************************************/
162 static bool make_way_for_eventlogs( TDB_CONTEXT * the_tdb, int32_t needed,
163 bool whack_by_date )
165 int32_t start_record, i, new_start;
166 int32_t end_record;
167 int32_t reclen, tresv1, trecnum, timegen, timewr;
168 int nbytes, len, Retention, MaxSize;
169 TDB_DATA key, ret;
170 time_t current_time, exp_time;
172 /* discard some eventlogs */
174 /* read eventlogs from oldest_entry -- there can't be any discontinuity in recnos,
175 although records not necessarily guaranteed to have successive times */
176 /* */
178 /* lock */
179 tdb_lock_bystring_with_timeout( the_tdb, EVT_NEXT_RECORD, 1 );
180 /* read */
181 end_record = tdb_fetch_int32( the_tdb, EVT_NEXT_RECORD );
182 start_record = tdb_fetch_int32( the_tdb, EVT_OLDEST_ENTRY );
183 Retention = tdb_fetch_int32( the_tdb, EVT_RETENTION );
184 MaxSize = tdb_fetch_int32( the_tdb, EVT_MAXSIZE );
186 time( &current_time );
188 /* calculate ... */
189 exp_time = current_time - Retention; /* discard older than exp_time */
191 /* todo - check for sanity in next_record */
192 nbytes = 0;
194 DEBUG( 3,
195 ( "MaxSize [%d] Retention [%d] Current Time [%u] exp_time [%u]\n",
196 MaxSize, Retention, (unsigned int)current_time, (unsigned int)exp_time ) );
197 DEBUG( 3,
198 ( "Start Record [%u] End Record [%u]\n",
199 (unsigned int)start_record,
200 (unsigned int)end_record ));
202 for ( i = start_record; i < end_record; i++ ) {
203 /* read a record, add the amt to nbytes */
204 key.dsize = sizeof(int32_t);
205 key.dptr = (unsigned char *)&i;
206 ret = tdb_fetch_compat( the_tdb, key );
207 if ( ret.dsize == 0 ) {
208 DEBUG( 8,
209 ( "Can't find a record for the key, record [%d]\n",
210 i ) );
211 tdb_unlock_bystring( the_tdb, EVT_NEXT_RECORD );
212 return False;
214 nbytes += ret.dsize; /* note this includes overhead */
216 len = tdb_unpack( ret.dptr, ret.dsize, "ddddd", &reclen,
217 &tresv1, &trecnum, &timegen, &timewr );
218 if (len == -1) {
219 DEBUG( 10,("make_way_for_eventlogs: tdb_unpack failed.\n"));
220 tdb_unlock_bystring( the_tdb, EVT_NEXT_RECORD );
221 SAFE_FREE( ret.dptr );
222 return False;
225 DEBUG( 8,
226 ( "read record %u, record size is [%d], total so far [%d]\n",
227 (unsigned int)i, reclen, nbytes ) );
229 SAFE_FREE( ret.dptr );
231 /* note that other servers may just stop writing records when the size limit
232 is reached, and there are no records older than 'retention'. This doesn't
233 like a very useful thing to do, so instead we whack (as in sleeps with the
234 fishes) just enough records to fit the what we need. This behavior could
235 be changed to 'match', if the need arises. */
237 if ( !whack_by_date && ( nbytes >= needed ) )
238 break; /* done */
239 if ( whack_by_date && ( timegen >= exp_time ) )
240 break; /* done */
243 DEBUG( 3,
244 ( "nbytes [%d] needed [%d] start_record is [%u], should be set to [%u]\n",
245 nbytes, needed, (unsigned int)start_record, (unsigned int)i ) );
246 /* todo - remove eventlog entries here and set starting record to start_record... */
247 new_start = i;
248 if ( start_record != new_start ) {
249 for ( i = start_record; i < new_start; i++ ) {
250 key.dsize = sizeof(int32_t);
251 key.dptr = (unsigned char *)&i;
252 tdb_delete( the_tdb, key );
255 tdb_store_int32( the_tdb, EVT_OLDEST_ENTRY, new_start );
257 tdb_unlock_bystring( the_tdb, EVT_NEXT_RECORD );
258 return True;
261 /********************************************************************
262 some hygiene for an eventlog - see how big it is, and then
263 calculate how many bytes we need to remove
264 ********************************************************************/
266 bool prune_eventlog( TDB_CONTEXT * tdb )
268 int MaxSize, Retention, CalcdSize;
270 if ( !tdb ) {
271 DEBUG( 4, ( "No eventlog tdb handle\n" ) );
272 return False;
275 CalcdSize = elog_tdb_size( tdb, &MaxSize, &Retention );
276 DEBUG( 3,
277 ( "Calculated size [%d] MaxSize [%d]\n", CalcdSize,
278 MaxSize ) );
280 if ( CalcdSize > MaxSize ) {
281 return make_way_for_eventlogs( tdb, CalcdSize - MaxSize,
282 False );
285 return make_way_for_eventlogs( tdb, 0, True );
288 /********************************************************************
289 ********************************************************************/
291 static bool can_write_to_eventlog( TDB_CONTEXT * tdb, int32_t needed )
293 int calcd_size;
294 int MaxSize, Retention;
296 /* see if we can write to the eventlog -- do a policy enforcement */
297 if ( !tdb )
298 return False; /* tdb is null, so we can't write to it */
301 if ( needed < 0 )
302 return False;
303 MaxSize = 0;
304 Retention = 0;
306 calcd_size = elog_tdb_size( tdb, &MaxSize, &Retention );
308 if ( calcd_size <= MaxSize )
309 return True; /* you betcha */
310 if ( calcd_size + needed < MaxSize )
311 return True;
313 if ( Retention == 0xffffffff ) {
314 return False; /* see msdn - we can't write no room, discard */
317 note don't have to test, but always good to show intent, in case changes needed
318 later
321 if ( Retention == 0x00000000 ) {
322 /* discard record(s) */
323 /* todo - decide when to remove a bunch vs. just what we need... */
324 return make_way_for_eventlogs( tdb, calcd_size - MaxSize,
325 True );
328 return make_way_for_eventlogs( tdb, calcd_size - MaxSize, False );
331 /*******************************************************************
332 *******************************************************************/
334 ELOG_TDB *elog_open_tdb( const char *logname, bool force_clear, bool read_only )
336 TDB_CONTEXT *tdb = NULL;
337 uint32_t vers_id;
338 ELOG_TDB *ptr;
339 char *tdbpath = NULL;
340 ELOG_TDB *tdb_node = NULL;
341 char *eventlogdir;
342 TALLOC_CTX *ctx = talloc_tos();
343 bool ok;
345 /* check for invalid options */
347 if (force_clear && read_only) {
348 DEBUG(1,("elog_open_tdb: Invalid flags\n"));
349 return NULL;
352 /* first see if we have an open context */
354 for ( ptr=open_elog_list; ptr; ptr=ptr->next ) {
355 if ( strequal( ptr->name, logname ) ) {
356 ptr->ref_count++;
358 /* trick to alow clearing of the eventlog tdb.
359 The force_clear flag should imply that someone
360 has done a force close. So make sure the tdb
361 is NULL. If this is a normal open, then just
362 return the existing reference */
364 if ( force_clear ) {
365 SMB_ASSERT( ptr->tdb == NULL );
366 break;
368 else
369 return ptr;
373 /* make sure that the eventlog dir exists */
375 eventlogdir = state_path( "eventlog" );
376 ok = directory_create_or_exist(eventlogdir, geteuid(), 0755);
377 if (!ok) {
378 return NULL;
381 /* get the path on disk */
383 tdbpath = elog_tdbname(ctx, logname);
384 if (!tdbpath) {
385 return NULL;
388 DEBUG(7,("elog_open_tdb: Opening %s...(force_clear == %s)\n",
389 tdbpath, force_clear?"True":"False" ));
391 /* the tdb wasn't already open or this is a forced clear open */
393 if ( !force_clear ) {
395 tdb = tdb_open_log( tdbpath, 0, TDB_DEFAULT, read_only ? O_RDONLY : O_RDWR , 0 );
396 if ( tdb ) {
397 vers_id = tdb_fetch_int32( tdb, EVT_VERSION );
399 if ( vers_id != EVENTLOG_DATABASE_VERSION_V1 ) {
400 DEBUG(1,("elog_open_tdb: Invalid version [%d] on file [%s].\n",
401 vers_id, tdbpath));
402 tdb_close( tdb );
403 tdb = elog_init_tdb( tdbpath );
408 if ( !tdb )
409 tdb = elog_init_tdb( tdbpath );
411 /* if we got a valid context, then add it to the list */
413 if ( tdb ) {
414 /* on a forced clear, just reset the tdb context if we already
415 have an open entry in the list */
417 if ( ptr ) {
418 ptr->tdb = tdb;
419 return ptr;
422 if ( !(tdb_node = talloc_zero( NULL, ELOG_TDB)) ) {
423 DEBUG(0,("elog_open_tdb: talloc() failure!\n"));
424 tdb_close( tdb );
425 return NULL;
428 tdb_node->name = talloc_strdup( tdb_node, logname );
429 tdb_node->tdb = tdb;
430 tdb_node->ref_count = 1;
432 DLIST_ADD( open_elog_list, tdb_node );
435 return tdb_node;
438 /*******************************************************************
439 Wrapper to handle reference counts to the tdb
440 *******************************************************************/
442 int elog_close_tdb( ELOG_TDB *etdb, bool force_close )
444 TDB_CONTEXT *tdb;
446 if ( !etdb )
447 return 0;
449 etdb->ref_count--;
451 SMB_ASSERT( etdb->ref_count >= 0 );
453 if ( etdb->ref_count == 0 ) {
454 tdb = etdb->tdb;
455 DLIST_REMOVE( open_elog_list, etdb );
456 TALLOC_FREE( etdb );
457 return tdb_close( tdb );
460 if ( force_close ) {
461 tdb = etdb->tdb;
462 etdb->tdb = NULL;
463 return tdb_close( tdb );
466 return 0;
469 /********************************************************************
470 Note that it's a pretty good idea to initialize the Eventlog_entry
471 structure to zero's before calling parse_logentry on an batch of
472 lines that may resolve to a record. ALSO, it's a good idea to
473 remove any linefeeds (that's EOL to you and me) on the lines
474 going in.
475 ********************************************************************/
477 bool parse_logentry( TALLOC_CTX *mem_ctx, char *line, struct eventlog_Record_tdb *entry, bool * eor )
479 char *start = NULL, *stop = NULL;
481 start = line;
483 /* empty line signyfiying record delimeter, or we're at the end of the buffer */
484 if ( start == NULL || strlen( start ) == 0 ) {
485 DEBUG( 6,
486 ( "parse_logentry: found end-of-record indicator.\n" ) );
487 *eor = True;
488 return True;
490 if ( !( stop = strchr( line, ':' ) ) ) {
491 return False;
494 DEBUG( 6, ( "parse_logentry: trying to parse [%s].\n", line ) );
496 if ( 0 == strncmp( start, "LEN", stop - start ) ) {
497 /* This will get recomputed later anyway -- probably not necessary */
498 entry->size = atoi( stop + 1 );
499 } else if ( 0 == strncmp( start, "RS1", stop - start ) ) {
500 /* For now all these reserved entries seem to have the same value,
501 which can be hardcoded to int(1699505740) for now */
502 entry->reserved = talloc_strdup(mem_ctx, "eLfL");
503 } else if ( 0 == strncmp( start, "RCN", stop - start ) ) {
504 entry->record_number = atoi( stop + 1 );
505 } else if ( 0 == strncmp( start, "TMG", stop - start ) ) {
506 entry->time_generated = atoi( stop + 1 );
507 } else if ( 0 == strncmp( start, "TMW", stop - start ) ) {
508 entry->time_written = atoi( stop + 1 );
509 } else if ( 0 == strncmp( start, "EID", stop - start ) ) {
510 entry->event_id = atoi( stop + 1 );
511 } else if ( 0 == strncmp( start, "ETP", stop - start ) ) {
512 if ( strstr( start, "ERROR" ) ) {
513 entry->event_type = EVENTLOG_ERROR_TYPE;
514 } else if ( strstr( start, "WARNING" ) ) {
515 entry->event_type = EVENTLOG_WARNING_TYPE;
516 } else if ( strstr( start, "INFO" ) ) {
517 entry->event_type = EVENTLOG_INFORMATION_TYPE;
518 } else if ( strstr( start, "AUDIT_SUCCESS" ) ) {
519 entry->event_type = EVENTLOG_AUDIT_SUCCESS;
520 } else if ( strstr( start, "AUDIT_FAILURE" ) ) {
521 entry->event_type = EVENTLOG_AUDIT_FAILURE;
522 } else if ( strstr( start, "SUCCESS" ) ) {
523 entry->event_type = EVENTLOG_SUCCESS;
524 } else {
525 /* some other eventlog type -- currently not defined in MSDN docs, so error out */
526 return False;
531 else if(0 == strncmp(start, "NST", stop - start))
533 entry->num_of_strings = atoi(stop + 1);
536 else if ( 0 == strncmp( start, "ECT", stop - start ) ) {
537 entry->event_category = atoi( stop + 1 );
538 } else if ( 0 == strncmp( start, "RS2", stop - start ) ) {
539 entry->reserved_flags = atoi( stop + 1 );
540 } else if ( 0 == strncmp( start, "CRN", stop - start ) ) {
541 entry->closing_record_number = atoi( stop + 1 );
542 } else if ( 0 == strncmp( start, "USL", stop - start ) ) {
543 entry->sid_length = atoi( stop + 1 );
544 } else if ( 0 == strncmp( start, "SRC", stop - start ) ) {
545 stop++;
546 while ( isspace( stop[0] ) ) {
547 stop++;
549 entry->source_name_len = strlen_m_term(stop);
550 entry->source_name = talloc_strdup(mem_ctx, stop);
551 if (entry->source_name_len == (uint32_t)-1 ||
552 entry->source_name == NULL) {
553 return false;
555 } else if ( 0 == strncmp( start, "SRN", stop - start ) ) {
556 stop++;
557 while ( isspace( stop[0] ) ) {
558 stop++;
560 entry->computer_name_len = strlen_m_term(stop);
561 entry->computer_name = talloc_strdup(mem_ctx, stop);
562 if (entry->computer_name_len == (uint32_t)-1 ||
563 entry->computer_name == NULL) {
564 return false;
566 } else if ( 0 == strncmp( start, "SID", stop - start ) ) {
567 smb_ucs2_t *dummy = NULL;
568 stop++;
569 while ( isspace( stop[0] ) ) {
570 stop++;
572 entry->sid_length = rpcstr_push_talloc(mem_ctx,
573 &dummy,
574 stop);
575 if (entry->sid_length == (uint32_t)-1) {
576 return false;
578 entry->sid = data_blob_talloc(mem_ctx, dummy, entry->sid_length);
579 if (entry->sid.data == NULL) {
580 return false;
582 } else if ( 0 == strncmp( start, "STR", stop - start ) ) {
583 size_t tmp_len;
584 int num_of_strings;
585 /* skip past initial ":" */
586 stop++;
587 /* now skip any other leading whitespace */
588 while ( isspace(stop[0])) {
589 stop++;
591 tmp_len = strlen_m_term(stop);
592 if (tmp_len == (size_t)-1) {
593 return false;
595 num_of_strings = entry->num_of_strings;
596 if (!add_string_to_array(mem_ctx, stop, &entry->strings,
597 &num_of_strings)) {
598 return false;
600 if (num_of_strings > 0xffff) {
601 return false;
603 entry->num_of_strings = num_of_strings;
604 entry->strings_len += tmp_len;
605 } else if ( 0 == strncmp( start, "DAT", stop - start ) ) {
606 /* skip past initial ":" */
607 stop++;
608 /* now skip any other leading whitespace */
609 while ( isspace( stop[0] ) ) {
610 stop++;
612 entry->data_length = strlen_m(stop);
613 entry->data = data_blob_talloc(mem_ctx, stop, entry->data_length);
614 if (!entry->data.data) {
615 return false;
617 } else {
618 /* some other eventlog entry -- not implemented, so dropping on the floor */
619 DEBUG( 10, ( "Unknown entry [%s]. Ignoring.\n", line ) );
620 /* For now return true so that we can keep on parsing this mess. Eventually
621 we will return False here. */
622 return true;
624 return true;
627 /*******************************************************************
628 calculate the correct fields etc for an eventlog entry
629 *******************************************************************/
631 size_t fixup_eventlog_record_tdb(struct eventlog_Record_tdb *r)
633 size_t size = 56; /* static size of integers before buffers start */
635 r->source_name_len = strlen_m_term(r->source_name) * 2;
636 r->computer_name_len = strlen_m_term(r->computer_name) * 2;
637 r->strings_len = ndr_size_string_array(r->strings,
638 r->num_of_strings, LIBNDR_FLAG_STR_NULLTERM) * 2;
640 /* fix up the eventlog entry structure as necessary */
641 r->sid_padding = ( ( 4 - ( ( r->source_name_len + r->computer_name_len ) % 4 ) ) % 4 );
642 r->padding = ( 4 - ( ( r->strings_len + r->data_length ) % 4 ) ) % 4;
644 if (r->sid_length == 0) {
645 /* Should not pad to a DWORD boundary for writing out the sid if there is
646 no SID, so just propagate the padding to pad the data */
647 r->padding += r->sid_padding;
648 r->sid_padding = 0;
651 size += r->source_name_len;
652 size += r->computer_name_len;
653 size += r->sid_padding;
654 size += r->sid_length;
655 size += r->strings_len;
656 size += r->data_length;
657 size += r->padding;
658 /* need another copy of length at the end of the data */
659 size += sizeof(r->size);
661 r->size = size;
663 return size;
667 /********************************************************************
668 ********************************************************************/
670 struct eventlog_Record_tdb *evlog_pull_record_tdb(TALLOC_CTX *mem_ctx,
671 TDB_CONTEXT *tdb,
672 uint32_t record_number)
674 struct eventlog_Record_tdb *r;
675 TDB_DATA data, key;
677 int32_t srecno;
678 enum ndr_err_code ndr_err;
679 DATA_BLOB blob;
681 srecno = record_number;
682 key.dptr = (unsigned char *)&srecno;
683 key.dsize = sizeof(int32_t);
685 data = tdb_fetch_compat(tdb, key);
686 if (data.dsize == 0) {
687 DEBUG(8,("evlog_pull_record_tdb: "
688 "Can't find a record for the key, record %d\n",
689 record_number));
690 return NULL;
693 r = talloc_zero(mem_ctx, struct eventlog_Record_tdb);
694 if (!r) {
695 goto done;
698 blob = data_blob_const(data.dptr, data.dsize);
700 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, r,
701 (ndr_pull_flags_fn_t)ndr_pull_eventlog_Record_tdb);
703 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
704 DEBUG(10,("evlog_pull_record_tdb: failed to decode record %d\n",
705 record_number));
706 TALLOC_FREE(r);
707 goto done;
710 if (DEBUGLEVEL >= 10) {
711 NDR_PRINT_DEBUG(eventlog_Record_tdb, r);
714 DEBUG(10,("evlog_pull_record_tdb: retrieved entry for record %d\n",
715 record_number));
716 done:
717 SAFE_FREE(data.dptr);
719 return r;
722 /********************************************************************
723 ********************************************************************/
725 struct EVENTLOGRECORD *evlog_pull_record(TALLOC_CTX *mem_ctx,
726 TDB_CONTEXT *tdb,
727 uint32_t record_number)
729 struct eventlog_Record_tdb *t;
730 struct EVENTLOGRECORD *r;
731 NTSTATUS status;
733 r = talloc_zero(mem_ctx, struct EVENTLOGRECORD);
734 if (!r) {
735 return NULL;
738 t = evlog_pull_record_tdb(r, tdb, record_number);
739 if (!t) {
740 talloc_free(r);
741 return NULL;
744 status = evlog_tdb_entry_to_evt_entry(r, t, r);
745 if (!NT_STATUS_IS_OK(status)) {
746 talloc_free(r);
747 return NULL;
750 r->Length = r->Length2 = ndr_size_EVENTLOGRECORD(r, 0);
752 return r;
755 /********************************************************************
756 write an eventlog entry. Note that we have to lock, read next
757 eventlog, increment, write, write the record, unlock
759 coming into this, ee has the eventlog record, and the auxilliary date
760 (computer name, etc.) filled into the other structure. Before packing
761 into a record, this routine will calc the appropriate padding, etc.,
762 and then blast out the record in a form that can be read back in
763 ********************************************************************/
765 NTSTATUS evlog_push_record_tdb(TALLOC_CTX *mem_ctx,
766 TDB_CONTEXT *tdb,
767 struct eventlog_Record_tdb *r,
768 uint32_t *record_number)
770 TDB_DATA kbuf, ebuf;
771 DATA_BLOB blob;
772 enum ndr_err_code ndr_err;
773 int ret;
775 if (!r) {
776 return NT_STATUS_INVALID_PARAMETER;
779 if (!can_write_to_eventlog(tdb, r->size)) {
780 return NT_STATUS_EVENTLOG_CANT_START;
783 /* need to read the record number and insert it into the entry here */
785 /* lock */
786 ret = tdb_lock_bystring_with_timeout(tdb, EVT_NEXT_RECORD, 1);
787 if (ret != 0) {
788 return NT_STATUS_LOCK_NOT_GRANTED;
791 /* read */
792 r->record_number = tdb_fetch_int32(tdb, EVT_NEXT_RECORD);
794 ndr_err = ndr_push_struct_blob(&blob, mem_ctx, r,
795 (ndr_push_flags_fn_t)ndr_push_eventlog_Record_tdb);
796 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
797 tdb_unlock_bystring(tdb, EVT_NEXT_RECORD);
798 return ndr_map_error2ntstatus(ndr_err);
801 /* increment the record count */
803 kbuf.dsize = sizeof(int32_t);
804 kbuf.dptr = (uint8_t *)&r->record_number;
806 ebuf.dsize = blob.length;
807 ebuf.dptr = blob.data;
809 ret = tdb_store(tdb, kbuf, ebuf, 0);
810 if (ret != 0) {
811 tdb_unlock_bystring(tdb, EVT_NEXT_RECORD);
812 return NT_STATUS_EVENTLOG_FILE_CORRUPT;
815 ret = tdb_store_int32(tdb, EVT_NEXT_RECORD, r->record_number + 1);
816 if (ret != 0) {
817 tdb_unlock_bystring(tdb, EVT_NEXT_RECORD);
818 return NT_STATUS_EVENTLOG_FILE_CORRUPT;
820 tdb_unlock_bystring(tdb, EVT_NEXT_RECORD);
822 if (record_number) {
823 *record_number = r->record_number;
826 return NT_STATUS_OK;
829 /********************************************************************
830 ********************************************************************/
832 NTSTATUS evlog_push_record(TALLOC_CTX *mem_ctx,
833 TDB_CONTEXT *tdb,
834 struct EVENTLOGRECORD *r,
835 uint32_t *record_number)
837 struct eventlog_Record_tdb *t;
838 NTSTATUS status;
840 t = talloc_zero(mem_ctx, struct eventlog_Record_tdb);
841 if (!t) {
842 return NT_STATUS_NO_MEMORY;
845 status = evlog_evt_entry_to_tdb_entry(t, r, t);
846 if (!NT_STATUS_IS_OK(status)) {
847 talloc_free(t);
848 return status;
851 status = evlog_push_record_tdb(mem_ctx, tdb, t, record_number);
852 talloc_free(t);
854 return status;
857 /********************************************************************
858 ********************************************************************/
860 NTSTATUS evlog_evt_entry_to_tdb_entry(TALLOC_CTX *mem_ctx,
861 const struct EVENTLOGRECORD *e,
862 struct eventlog_Record_tdb *t)
864 uint32_t i;
866 ZERO_STRUCTP(t);
868 t->size = e->Length;
869 t->reserved = e->Reserved;
870 t->record_number = e->RecordNumber;
871 t->time_generated = e->TimeGenerated;
872 t->time_written = e->TimeWritten;
873 t->event_id = e->EventID;
874 t->event_type = e->EventType;
875 t->num_of_strings = e->NumStrings;
876 t->event_category = e->EventCategory;
877 t->reserved_flags = e->ReservedFlags;
878 t->closing_record_number = e->ClosingRecordNumber;
880 t->stringoffset = e->StringOffset;
881 t->sid_length = e->UserSidLength;
882 t->sid_offset = e->UserSidOffset;
883 t->data_length = e->DataLength;
884 t->data_offset = e->DataOffset;
886 t->source_name_len = 2 * strlen_m_term(e->SourceName);
887 t->source_name = talloc_strdup(mem_ctx, e->SourceName);
888 NT_STATUS_HAVE_NO_MEMORY(t->source_name);
890 t->computer_name_len = 2 * strlen_m_term(e->Computername);
891 t->computer_name = talloc_strdup(mem_ctx, e->Computername);
892 NT_STATUS_HAVE_NO_MEMORY(t->computer_name);
894 /* t->sid_padding; */
895 if (e->UserSidLength > 0) {
896 const char *sid_str = NULL;
897 smb_ucs2_t *dummy = NULL;
898 sid_str = sid_string_talloc(mem_ctx, &e->UserSid);
899 t->sid_length = rpcstr_push_talloc(mem_ctx, &dummy, sid_str);
900 if (t->sid_length == -1) {
901 return NT_STATUS_NO_MEMORY;
903 t->sid = data_blob_talloc(mem_ctx, (uint8_t *)dummy, t->sid_length);
904 NT_STATUS_HAVE_NO_MEMORY(t->sid.data);
907 t->strings = talloc_array(mem_ctx, const char *, e->NumStrings);
908 for (i=0; i < e->NumStrings; i++) {
909 t->strings[i] = talloc_strdup(t->strings, e->Strings[i]);
910 NT_STATUS_HAVE_NO_MEMORY(t->strings[i]);
913 t->strings_len = 2 * ndr_size_string_array(t->strings, t->num_of_strings, LIBNDR_FLAG_STR_NULLTERM);
914 t->data = data_blob_talloc(mem_ctx, e->Data, e->DataLength);
915 /* t->padding = r->Pad; */
917 return NT_STATUS_OK;
920 /********************************************************************
921 ********************************************************************/
923 NTSTATUS evlog_tdb_entry_to_evt_entry(TALLOC_CTX *mem_ctx,
924 const struct eventlog_Record_tdb *t,
925 struct EVENTLOGRECORD *e)
927 uint32_t i;
929 ZERO_STRUCTP(e);
931 e->Length = t->size;
932 e->Reserved = t->reserved;
933 e->RecordNumber = t->record_number;
934 e->TimeGenerated = t->time_generated;
935 e->TimeWritten = t->time_written;
936 e->EventID = t->event_id;
937 e->EventType = t->event_type;
938 e->NumStrings = t->num_of_strings;
939 e->EventCategory = t->event_category;
940 e->ReservedFlags = t->reserved_flags;
941 e->ClosingRecordNumber = t->closing_record_number;
943 e->StringOffset = t->stringoffset;
944 e->UserSidLength = t->sid_length;
945 e->UserSidOffset = t->sid_offset;
946 e->DataLength = t->data_length;
947 e->DataOffset = t->data_offset;
949 e->SourceName = talloc_strdup(mem_ctx, t->source_name);
950 NT_STATUS_HAVE_NO_MEMORY(e->SourceName);
952 e->Computername = talloc_strdup(mem_ctx, t->computer_name);
953 NT_STATUS_HAVE_NO_MEMORY(e->Computername);
955 if (t->sid_length > 0) {
956 const char *sid_str = NULL;
957 size_t len;
958 if (!convert_string_talloc(mem_ctx, CH_UTF16, CH_UNIX,
959 t->sid.data, t->sid.length,
960 (void *)&sid_str, &len)) {
961 return NT_STATUS_INVALID_SID;
963 if (len > 0) {
964 string_to_sid(&e->UserSid, sid_str);
968 e->Strings = talloc_array(mem_ctx, const char *, t->num_of_strings);
969 for (i=0; i < t->num_of_strings; i++) {
970 e->Strings[i] = talloc_strdup(e->Strings, t->strings[i]);
971 NT_STATUS_HAVE_NO_MEMORY(e->Strings[i]);
974 e->Data = (uint8_t *)talloc_memdup(mem_ctx, t->data.data, t->data_length);
975 e->Pad = talloc_strdup(mem_ctx, "");
976 NT_STATUS_HAVE_NO_MEMORY(e->Pad);
978 e->Length2 = t->size;
980 return NT_STATUS_OK;
983 /********************************************************************
984 ********************************************************************/
986 NTSTATUS evlog_convert_tdb_to_evt(TALLOC_CTX *mem_ctx,
987 ELOG_TDB *etdb,
988 DATA_BLOB *blob_p,
989 uint32_t *num_records_p)
991 NTSTATUS status = NT_STATUS_OK;
992 enum ndr_err_code ndr_err;
993 DATA_BLOB blob;
994 uint32_t num_records = 0;
995 struct EVENTLOG_EVT_FILE evt;
996 uint32_t count = 1;
997 size_t endoffset = 0;
999 ZERO_STRUCT(evt);
1001 while (1) {
1003 struct eventlog_Record_tdb *r;
1004 struct EVENTLOGRECORD e;
1006 r = evlog_pull_record_tdb(mem_ctx, etdb->tdb, count);
1007 if (!r) {
1008 break;
1011 status = evlog_tdb_entry_to_evt_entry(mem_ctx, r, &e);
1012 if (!NT_STATUS_IS_OK(status)) {
1013 goto done;
1016 endoffset += ndr_size_EVENTLOGRECORD(&e, 0);
1018 ADD_TO_ARRAY(mem_ctx, struct EVENTLOGRECORD, e, &evt.records, &num_records);
1019 count++;
1022 evt.hdr.StartOffset = 0x30;
1023 evt.hdr.EndOffset = evt.hdr.StartOffset + endoffset;
1024 evt.hdr.CurrentRecordNumber = count;
1025 evt.hdr.OldestRecordNumber = 1;
1026 evt.hdr.MaxSize = tdb_fetch_int32(etdb->tdb, EVT_MAXSIZE);
1027 evt.hdr.Flags = 0;
1028 evt.hdr.Retention = tdb_fetch_int32(etdb->tdb, EVT_RETENTION);
1030 if (DEBUGLEVEL >= 10) {
1031 NDR_PRINT_DEBUG(EVENTLOGHEADER, &evt.hdr);
1034 evt.eof.BeginRecord = 0x30;
1035 evt.eof.EndRecord = evt.hdr.StartOffset + endoffset;
1036 evt.eof.CurrentRecordNumber = evt.hdr.CurrentRecordNumber;
1037 evt.eof.OldestRecordNumber = evt.hdr.OldestRecordNumber;
1039 if (DEBUGLEVEL >= 10) {
1040 NDR_PRINT_DEBUG(EVENTLOGEOF, &evt.eof);
1043 ndr_err = ndr_push_struct_blob(&blob, mem_ctx, &evt,
1044 (ndr_push_flags_fn_t)ndr_push_EVENTLOG_EVT_FILE);
1045 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1046 status = ndr_map_error2ntstatus(ndr_err);
1047 goto done;
1050 *blob_p = blob;
1051 *num_records_p = num_records;
1053 done:
1054 return status;