| 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 3 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, see <http://www.gnu.org/licenses/>.
|
|---|
| 21 | */
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 | #include "includes.h"
|
|---|
| 25 |
|
|---|
| 26 | #undef DBGC_CLASS
|
|---|
| 27 | #define DBGC_CLASS DBGC_UTIL_EVENTLOG
|
|---|
| 28 |
|
|---|
| 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 [OPTION]\n\n", s );
|
|---|
| 38 | printf( " -o write <Eventlog Name> \t\t\t\t\tWrites records to eventlog from STDIN\n" );
|
|---|
| 39 | printf( " -o addsource <EventlogName> <sourcename> <msgfileDLLname> \tAdds the specified source & DLL eventlog registry entry\n" );
|
|---|
| 40 | printf( "\nMiscellaneous options:\n" );
|
|---|
| 41 | printf( " -d\t\t\t\t\t\t\t\tturn debug on\n" );
|
|---|
| 42 | printf( " -h\t\t\t\t\t\t\t\tdisplay help\n\n" );
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | static void display_eventlog_names( void )
|
|---|
| 46 | {
|
|---|
| 47 | const char **elogs;
|
|---|
| 48 | int i;
|
|---|
| 49 |
|
|---|
| 50 | elogs = lp_eventlog_list( );
|
|---|
| 51 | printf( "Active eventlog names (from smb.conf):\n" );
|
|---|
| 52 | printf( "--------------------------------------\n" );
|
|---|
| 53 | if ( elogs ) {
|
|---|
| 54 | for ( i = 0; elogs[i]; i++ ) {
|
|---|
| 55 | printf( "\t%s\n", elogs[i] );
|
|---|
| 56 | }
|
|---|
| 57 | }
|
|---|
| 58 | else
|
|---|
| 59 | printf( "\t<None specified>\n");
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | static int DoAddSourceCommand( int argc, char **argv, bool debugflag, char *exename )
|
|---|
| 63 | {
|
|---|
| 64 |
|
|---|
| 65 | if ( argc < 3 ) {
|
|---|
| 66 | printf( "need more arguments:\n" );
|
|---|
| 67 | printf( "-o addsource EventlogName SourceName /path/to/EventMessageFile.dll\n" );
|
|---|
| 68 | return -1;
|
|---|
| 69 | }
|
|---|
| 70 | /* must open the registry before we access it */
|
|---|
| 71 | if (!W_ERROR_IS_OK(regdb_init())) {
|
|---|
| 72 | printf( "Can't open the registry.\n" );
|
|---|
| 73 | return -1;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | if ( !eventlog_add_source( argv[0], argv[1], argv[2] ) )
|
|---|
| 77 | return -2;
|
|---|
| 78 | return 0;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | static int DoWriteCommand( int argc, char **argv, bool debugflag, char *exename )
|
|---|
| 82 | {
|
|---|
| 83 | FILE *f1;
|
|---|
| 84 | char *argfname;
|
|---|
| 85 | ELOG_TDB *etdb;
|
|---|
| 86 |
|
|---|
| 87 | /* fixed constants are bad bad bad */
|
|---|
| 88 | char linein[1024];
|
|---|
| 89 | bool is_eor;
|
|---|
| 90 | Eventlog_entry ee;
|
|---|
| 91 | int rcnum;
|
|---|
| 92 |
|
|---|
| 93 | f1 = stdin;
|
|---|
| 94 | if ( !f1 ) {
|
|---|
| 95 | printf( "Can't open STDIN\n" );
|
|---|
| 96 | return -1;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | if ( debugflag ) {
|
|---|
| 100 | printf( "Starting write for eventlog [%s]\n", argv[0] );
|
|---|
| 101 | display_eventlog_names( );
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | argfname = argv[0];
|
|---|
| 105 |
|
|---|
| 106 | if ( !( etdb = elog_open_tdb( argfname, False ) ) ) {
|
|---|
| 107 | printf( "can't open the eventlog TDB (%s)\n", argfname );
|
|---|
| 108 | return -1;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | ZERO_STRUCT( ee ); /* MUST initialize between records */
|
|---|
| 112 |
|
|---|
| 113 | while ( !feof( f1 ) ) {
|
|---|
| 114 | fgets( linein, sizeof( linein ) - 1, f1 );
|
|---|
| 115 | linein[strlen( linein ) - 1] = 0; /* whack the line delimiter */
|
|---|
| 116 |
|
|---|
| 117 | if ( debugflag )
|
|---|
| 118 | printf( "Read line [%s]\n", linein );
|
|---|
| 119 |
|
|---|
| 120 | is_eor = False;
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 | parse_logentry( ( char * ) &linein, &ee, &is_eor );
|
|---|
| 124 | /* should we do something with the return code? */
|
|---|
| 125 |
|
|---|
| 126 | if ( is_eor ) {
|
|---|
| 127 | fixup_eventlog_entry( &ee );
|
|---|
| 128 |
|
|---|
| 129 | if ( opt_debug )
|
|---|
| 130 | printf( "record number [%d], tg [%d] , tw [%d]\n", ee.record.record_number, ee.record.time_generated, ee.record.time_written );
|
|---|
| 131 |
|
|---|
| 132 | if ( ee.record.time_generated != 0 ) {
|
|---|
| 133 |
|
|---|
| 134 | /* printf("Writing to the event log\n"); */
|
|---|
| 135 |
|
|---|
| 136 | rcnum = write_eventlog_tdb( ELOG_TDB_CTX(etdb), &ee );
|
|---|
| 137 | if ( !rcnum ) {
|
|---|
| 138 | printf( "Can't write to the event log\n" );
|
|---|
| 139 | } else {
|
|---|
| 140 | if ( opt_debug )
|
|---|
| 141 | printf( "Wrote record %d\n",
|
|---|
| 142 | rcnum );
|
|---|
| 143 | }
|
|---|
| 144 | } else {
|
|---|
| 145 | if ( opt_debug )
|
|---|
| 146 | printf( "<null record>\n" );
|
|---|
| 147 | }
|
|---|
| 148 | ZERO_STRUCT( ee ); /* MUST initialize between records */
|
|---|
| 149 | }
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | elog_close_tdb( etdb , False );
|
|---|
| 153 |
|
|---|
| 154 | return 0;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | /* would be nice to use the popT stuff here, however doing so forces us to drag in a lot of other infrastructure */
|
|---|
| 158 |
|
|---|
| 159 | int main( int argc, char *argv[] )
|
|---|
| 160 | {
|
|---|
| 161 | int opt, rc;
|
|---|
| 162 | char *exename;
|
|---|
| 163 | TALLOC_CTX *frame = talloc_stackframe();
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 | fstring opname;
|
|---|
| 167 |
|
|---|
| 168 | load_case_tables();
|
|---|
| 169 |
|
|---|
| 170 | opt_debug = 0; /* todo set this from getopts */
|
|---|
| 171 |
|
|---|
| 172 | lp_load(get_dyn_CONFIGFILE(), True, False, False, True);
|
|---|
| 173 |
|
|---|
| 174 | exename = argv[0];
|
|---|
| 175 |
|
|---|
| 176 | /* default */
|
|---|
| 177 |
|
|---|
| 178 | fstrcpy( opname, "write" ); /* the default */
|
|---|
| 179 |
|
|---|
| 180 | #if 0 /* TESTING CODE */
|
|---|
| 181 | eventlog_add_source( "System", "TestSourceX", "SomeTestPathX" );
|
|---|
| 182 | #endif
|
|---|
| 183 | while ( ( opt = getopt( argc, argv, "dho:" ) ) != EOF ) {
|
|---|
| 184 | switch ( opt ) {
|
|---|
| 185 |
|
|---|
| 186 | case 'o':
|
|---|
| 187 | fstrcpy( opname, optarg );
|
|---|
| 188 | break;
|
|---|
| 189 |
|
|---|
| 190 | case 'h':
|
|---|
| 191 | usage( exename );
|
|---|
| 192 | display_eventlog_names( );
|
|---|
| 193 | exit( 0 );
|
|---|
| 194 | break;
|
|---|
| 195 |
|
|---|
| 196 | case 'd':
|
|---|
| 197 | opt_debug = 1;
|
|---|
| 198 | break;
|
|---|
| 199 | }
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | argc -= optind;
|
|---|
| 203 | argv += optind;
|
|---|
| 204 |
|
|---|
| 205 | if ( argc < 1 ) {
|
|---|
| 206 | printf( "\nNot enough arguments!\n" );
|
|---|
| 207 | usage( exename );
|
|---|
| 208 | exit( 1 );
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | /* note that the separate command types should call usage if they need to... */
|
|---|
| 212 | while ( 1 ) {
|
|---|
| 213 | if ( !StrCaseCmp( opname, "addsource" ) ) {
|
|---|
| 214 | rc = DoAddSourceCommand( argc, argv, opt_debug,
|
|---|
| 215 | exename );
|
|---|
| 216 | break;
|
|---|
| 217 | }
|
|---|
| 218 | if ( !StrCaseCmp( opname, "write" ) ) {
|
|---|
| 219 | rc = DoWriteCommand( argc, argv, opt_debug, exename );
|
|---|
| 220 | break;
|
|---|
| 221 | }
|
|---|
| 222 | printf( "unknown command [%s]\n", opname );
|
|---|
| 223 | usage( exename );
|
|---|
| 224 | exit( 1 );
|
|---|
| 225 | break;
|
|---|
| 226 | }
|
|---|
| 227 | TALLOC_FREE(frame);
|
|---|
| 228 | return rc;
|
|---|
| 229 | }
|
|---|