r11063: merging missing error code for event logs
[ira/wip.git] / source / utils / eventlogadm.c
1 /*
2  * Samba Unix/Linux SMB client utility 
3  * Write Eventlog records to a tdb
4  *
5  * Copyright (C) Brian Moran                2005.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22
23 #include "includes.h"
24
25 #undef  DBGC_CLASS
26 #define DBGC_CLASS DBGC_UTIL_EVENTLOG
27
28 Eventlog_entry ee;
29
30 extern int optind;
31 extern char *optarg;
32
33 int opt_debug = 0;
34
35 static void usage( char *s )
36 {
37         printf( "\nUsage: %s [-d] [-h] <Eventlog Name>\n", s );
38         printf( "\t-d\tturn debug on\n" );
39         printf( "\t-h\tdisplay help\n\n" );
40 }
41
42 static void display_eventlog_names( void )
43 {
44         const char **elogs;
45         int i;
46
47         elogs = lp_eventlog_list(  );
48         printf( "Active eventlog names (from smb.conf):\n" );
49         printf( "--------------------------------------\n" );
50         for ( i = 0; elogs[i]; i++ ) {
51                 printf( "\t%s\n", elogs[i] );
52         }
53 }
54
55 int main( int argc, char *argv[] )
56 {
57         FILE *f1;
58
59         /* fixed constants are bad bad bad  */
60         pstring linein;
61         BOOL is_eor;
62         int pret, opt;
63         int rcnum;
64         char *argfname, *exename;
65         char *tdbname;
66
67
68         TDB_CONTEXT *elog_tdb;
69
70         opt_debug = 0;          /* todo set this from getopts */
71
72
73         lp_load( dyn_CONFIGFILE, True, False, False );
74
75         exename = argv[0];
76
77         while ( ( opt = getopt( argc, argv, "dh" ) ) != -1 ) {
78                 switch ( opt ) {
79                 case 'h':
80                         usage( argv[0] );
81                         display_eventlog_names(  );
82                         exit( 0 );
83                         break;
84
85                 case 'd':
86                         opt_debug = 1;
87                         break;
88                 }
89         }
90
91         argc -= optind;
92         argv += optind;
93
94         if ( argc < 1 ) {
95                 usage( exename );
96                 exit( 1 );
97         }
98
99
100
101         f1 = stdin;
102
103         if ( !f1 ) {
104                 printf( "Can't open STDIN\n" );
105                 return -1;
106         }
107
108
109         if ( opt_debug ) {
110                 printf( "Starting %s for eventlog [%s]\n", exename, argv[0] );
111                 display_eventlog_names(  );
112         }
113
114         argfname = argv[0];
115
116         if ( !(elog_tdb = elog_open_tdb( argfname ) ) ) {
117                 printf( "can't open the eventlog TDB (%s)\n", tdbname );
118                 return -1;
119         }
120
121         ZERO_STRUCT( ee );      /* MUST initialize between records */
122
123         while ( !feof( f1 ) ) {
124                 fgets( linein, sizeof( linein ) - 1, f1 );
125                 linein[strlen( linein ) - 1] = 0;       /* whack the line delimiter */
126
127                 if ( opt_debug )
128                         printf( "Read line [%s]\n", linein );
129
130                 is_eor = False;
131
132                 pret = parse_logentry( ( char * ) &linein, &ee, &is_eor );
133
134                 if ( is_eor ) {
135                         fixup_eventlog_entry( &ee );
136
137                         if ( opt_debug )
138                                 printf( "record number [%d], tg [%d] , tw [%d]\n", 
139                                         ee.record.record_number, 
140                                         ee.record.time_generated, 
141                                         ee.record.time_written );
142
143                         if ( ee.record.time_generated != 0 ) {
144
145                                 /* printf("Writing to the event log\n"); */
146
147                                 rcnum = write_eventlog_tdb( elog_tdb, &ee ); 
148                                 if ( !rcnum ) {
149                                         printf( "Can't write to the event log\n" );
150                                 } else {
151                                         if ( opt_debug )
152                                                 printf( "Wrote record %d\n",
153                                                         rcnum );
154                                 }
155                         } else {
156                                 if ( opt_debug )
157                                         printf( "<null record>\n" );
158                         }
159                         ZERO_STRUCT( ee );      /* MUST initialize between records */
160                 }
161         }
162
163         tdb_close( elog_tdb );
164
165         return 0;
166 }