r11136: patches from Brian Moran for eventlogadm utility
[kai/samba.git] / source3 / utils / eventlogadm.c
1
2 /*
3  * Samba Unix/Linux SMB client utility 
4  * Write Eventlog records to a tdb, perform other eventlog related functions
5  *
6  *
7  * Copyright (C) Brian Moran                2005.
8  *
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 2 of the License, or
12  * (at your option) any later version.
13  *
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.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24
25 #include "includes.h"
26
27 #undef  DBGC_CLASS
28 #define DBGC_CLASS DBGC_UTIL_EVENTLOG
29
30
31 extern int optind;
32 extern char *optarg;
33
34 int opt_debug = 0;
35
36 static void usage( char *s )
37 {
38         printf( "\nUsage: %s [OPTION]\n\n", s );
39         printf( " -o write <Eventlog Name> \t\t\t\t\tWrites records to eventlog from STDIN\n" );
40         printf( " -o addsource <EventlogName> <sourcename> <msgfileDLLname> \tAdds the specified source & DLL eventlog registry entry\n" );
41         printf( "\nMiscellaneous options:\n" );
42         printf( " -d\t\t\t\t\t\t\t\tturn debug on\n" );
43         printf( " -h\t\t\t\t\t\t\t\tdisplay help\n\n" );
44 }
45
46 static void display_eventlog_names( void )
47 {
48         const char **elogs;
49         int i;
50
51         elogs = lp_eventlog_list(  );
52         printf( "Active eventlog names (from smb.conf):\n" );
53         printf( "--------------------------------------\n" );
54         for ( i = 0; elogs[i]; i++ ) {
55                 printf( "\t%s\n", elogs[i] );
56         }
57 }
58
59 int DoAddSourceCommand( int argc, char **argv, BOOL debugflag, char *exename )
60 {
61
62         if ( argc < 3 ) {
63                 printf( "need more arguments:\n" );
64                 printf( "-o addsource EventlogName SourceName /path/to/eventlogmsg.dll\n" );
65                 return -1;
66         }
67         /* must open the registry before we access it */
68         if ( !regdb_init(  ) ) {
69                 printf( "Can't open the registry.\n" );
70                 return -1;
71         }
72
73         if ( !eventlog_add_source( argv[0], argv[1], argv[2] ) )
74                 return -2;
75         return 0;
76 }
77
78 int DoWriteCommand( int argc, char **argv, BOOL debugflag, char *exename )
79 {
80         FILE *f1;
81         char *argfname;
82         TDB_CONTEXT *elog_tdb;
83
84         /* fixed constants are bad bad bad  */
85         pstring linein;
86         BOOL is_eor;
87         Eventlog_entry ee;
88         int pret, rcnum;
89
90         f1 = stdin;
91         if ( !f1 ) {
92                 printf( "Can't open STDIN\n" );
93                 return -1;
94         }
95
96         if ( debugflag ) {
97                 printf( "Starting write for eventlog [%s]\n", argv[0] );
98                 display_eventlog_names(  );
99         }
100
101         argfname = argv[0];
102
103         if ( !( elog_tdb = elog_open_tdb( argfname ) ) ) {
104                 printf( "can't open the eventlog TDB (%s)\n", argfname );
105                 return -1;
106         }
107
108         ZERO_STRUCT( ee );      /* MUST initialize between records */
109
110         while ( !feof( f1 ) ) {
111                 fgets( linein, sizeof( linein ) - 1, f1 );
112                 linein[strlen( linein ) - 1] = 0;       /* whack the line delimiter */
113
114                 if ( debugflag )
115                         printf( "Read line [%s]\n", linein );
116
117                 is_eor = False;
118
119
120                 pret = parse_logentry( ( char * ) &linein, &ee, &is_eor );
121                 /* should we do something with the return code? */
122
123                 if ( is_eor ) {
124                         fixup_eventlog_entry( &ee );
125
126                         if ( opt_debug )
127                                 printf( "record number [%d], tg [%d] , tw [%d]\n", ee.record.record_number, ee.record.time_generated, ee.record.time_written );
128
129                         if ( ee.record.time_generated != 0 ) {
130
131                                 /* printf("Writing to the event log\n"); */
132
133                                 rcnum = write_eventlog_tdb( elog_tdb, &ee );
134                                 if ( !rcnum ) {
135                                         printf( "Can't write to the event log\n" );
136                                 } else {
137                                         if ( opt_debug )
138                                                 printf( "Wrote record %d\n",
139                                                         rcnum );
140                                 }
141                         } else {
142                                 if ( opt_debug )
143                                         printf( "<null record>\n" );
144                         }
145                         ZERO_STRUCT( ee );      /* MUST initialize between records */
146                 }
147         }
148
149         tdb_close( elog_tdb );
150
151         return 0;
152 }
153
154 /* would be nice to use the popT stuff here, however doing so forces us to drag in a lot of other infrastructure */
155
156 int main( int argc, char *argv[] )
157 {
158         int opt, rc;
159         char *exename;
160         char *srcname, *eventlogname;
161
162
163         fstring opname;
164
165         opt_debug = 0;          /* todo set this from getopts */
166
167         lp_load( dyn_CONFIGFILE, True, False, False );
168
169         exename = argv[0];
170         srcname = NULL;
171
172         /* default */
173
174         fstrcpy( opname, "write" );     /* the default */
175
176 #if 0                           /* TESTING CODE */
177         eventlog_add_source( "System", "TestSourceX", "SomeTestPathX" );
178 #endif
179         while ( ( opt = getopt( argc, argv, "dho:" ) ) != EOF ) {
180                 switch ( opt ) {
181
182                 case 'o':
183                         fstrcpy( opname, optarg );
184                         break;
185
186                 case 'h':
187                         usage( argv[0] );
188                         display_eventlog_names(  );
189                         exit( 0 );
190                         break;
191
192                 case 'd':
193                         opt_debug = 1;
194                         break;
195                 }
196         }
197
198         argc -= optind;
199         argv += optind;
200
201         if ( argc < 1 ) {
202                 printf( "\nNot enough arguments!\n" );
203                 usage( exename );
204                 exit( 1 );
205         }
206
207         /*  note that the separate command types should call usage if they need to... */
208         eventlogname = *argv;
209         while ( 1 ) {
210                 if ( !StrCaseCmp( opname, "addsource" ) ) {
211                         rc = DoAddSourceCommand( argc, argv, opt_debug,
212                                                  exename );
213                         break;
214                 }
215                 if ( !StrCaseCmp( opname, "write" ) ) {
216                         rc = DoWriteCommand( argc, argv, opt_debug, exename );
217                         break;
218                 }
219                 printf( "unknown command [%s]\n", opname );
220                 usage( exename );
221                 exit( 1 );
222                 break;
223         }
224         return rc;
225 }