r22001: change prototype of dump_data(), so that it takes unsigned char * now,
[metze/samba/wip.git] / source3 / registry / reg_eventlog.c
1
2 /* 
3  *  Unix SMB/CIFS implementation.
4  *  Virtual Windows Registry Layer
5  *  Copyright (C) Marcin Krzysztof Porwit    2005,
6  *  Copyright (C) Brian Moran                2005.
7  *  Copyright (C) Gerald (Jerry) Carter      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 #include "includes.h"
25
26
27 /**********************************************************************
28  for an eventlog, add in the default values
29 *********************************************************************/
30
31 BOOL eventlog_init_keys( void )
32 {
33         /* Find all of the eventlogs, add keys for each of them */
34         const char **elogs = lp_eventlog_list(  );
35         pstring evtlogpath;
36         pstring evtfilepath;
37         REGSUBKEY_CTR *subkeys;
38         REGVAL_CTR *values;
39         uint32 uiMaxSize;
40         uint32 uiRetention;
41         uint32 uiCategoryCount;
42         UNISTR2 data;
43
44         while ( elogs && *elogs ) {
45                 if ( !( subkeys = TALLOC_ZERO_P( NULL, REGSUBKEY_CTR ) ) ) {
46                         DEBUG( 0, ( "talloc() failure!\n" ) );
47                         return False;
48                 }
49                 regdb_fetch_keys( KEY_EVENTLOG, subkeys );
50                 regsubkey_ctr_addkey( subkeys, *elogs );
51                 if ( !regdb_store_keys( KEY_EVENTLOG, subkeys ) ) {
52                         TALLOC_FREE(subkeys);
53                         return False;
54                 }
55                 TALLOC_FREE( subkeys );
56
57                 /* add in the key of form KEY_EVENTLOG/Application */
58                 DEBUG( 5,
59                        ( "Adding key of [%s] to path of [%s]\n", *elogs,
60                          KEY_EVENTLOG ) );
61
62                 slprintf( evtlogpath, sizeof( evtlogpath ) - 1, "%s\\%s",
63                           KEY_EVENTLOG, *elogs );
64                 /* add in the key of form KEY_EVENTLOG/Application/Application */
65                 DEBUG( 5,
66                        ( "Adding key of [%s] to path of [%s]\n", *elogs,
67                          evtlogpath ) );
68                 if ( !( subkeys = TALLOC_ZERO_P( NULL, REGSUBKEY_CTR ) ) ) {
69                         DEBUG( 0, ( "talloc() failure!\n" ) );
70                         return False;
71                 }
72                 regdb_fetch_keys( evtlogpath, subkeys );
73                 regsubkey_ctr_addkey( subkeys, *elogs );
74
75                 if ( !regdb_store_keys( evtlogpath, subkeys ) ) {
76                         TALLOC_FREE(subkeys);
77                         return False;
78                 }
79                 TALLOC_FREE( subkeys );
80
81                 /* now add the values to the KEY_EVENTLOG/Application form key */
82                 if ( !( values = TALLOC_ZERO_P( NULL, REGVAL_CTR ) ) ) {
83                         DEBUG( 0, ( "talloc() failure!\n" ) );
84                         return False;
85                 }
86                 DEBUG( 5,
87                        ( "Storing values to eventlog path of [%s]\n",
88                          evtlogpath ) );
89                 regdb_fetch_values( evtlogpath, values );
90
91
92                 if ( !regval_ctr_key_exists( values, "MaxSize" ) ) {
93
94                         /* assume we have none, add them all */
95
96                         /* hard code some initial values */
97
98                         /* uiDisplayNameId = 0x00000100; */
99                         uiMaxSize = 0x00080000;
100                         uiRetention = 0x93A80;
101
102                         regval_ctr_addvalue( values, "MaxSize", REG_DWORD,
103                                              ( char * ) &uiMaxSize,
104                                              sizeof( uint32 ) );
105
106                         regval_ctr_addvalue( values, "Retention", REG_DWORD,
107                                              ( char * ) &uiRetention,
108                                              sizeof( uint32 ) );
109                         init_unistr2( &data, *elogs, UNI_STR_TERMINATE );
110
111                         regval_ctr_addvalue( values, "PrimaryModule", REG_SZ,
112                                              ( char * ) data.buffer,
113                                              data.uni_str_len *
114                                              sizeof( uint16 ) );
115                         init_unistr2( &data, *elogs, UNI_STR_TERMINATE );
116
117                         regval_ctr_addvalue( values, "Sources", REG_MULTI_SZ,
118                                              ( char * ) data.buffer,
119                                              data.uni_str_len *
120                                              sizeof( uint16 ) );
121
122                         pstr_sprintf( evtfilepath, "%%SystemRoot%%\\system32\\config\\%s.tdb", *elogs );
123                         init_unistr2( &data, evtfilepath, UNI_STR_TERMINATE );
124                         regval_ctr_addvalue( values, "File", REG_EXPAND_SZ, ( char * ) data.buffer,
125                                              data.uni_str_len * sizeof( uint16 ) );
126                         regdb_store_values( evtlogpath, values );
127
128                 }
129
130                 TALLOC_FREE( values );
131
132                 /* now do the values under KEY_EVENTLOG/Application/Application */
133                 slprintf( evtlogpath, sizeof( evtlogpath ) - 1, "%s\\%s\\%s",
134                           KEY_EVENTLOG, *elogs, *elogs );
135                 if ( !( values = TALLOC_ZERO_P( NULL, REGVAL_CTR ) ) ) {
136                         DEBUG( 0, ( "talloc() failure!\n" ) );
137                         return False;
138                 }
139                 DEBUG( 5,
140                        ( "Storing values to eventlog path of [%s]\n",
141                          evtlogpath ) );
142                 regdb_fetch_values( evtlogpath, values );
143                 if ( !regval_ctr_key_exists( values, "CategoryCount" ) ) {
144
145                         /* hard code some initial values */
146
147                         uiCategoryCount = 0x00000007;
148                         regval_ctr_addvalue( values, "CategoryCount",
149                                              REG_DWORD,
150                                              ( char * ) &uiCategoryCount,
151                                              sizeof( uint32 ) );
152                         init_unistr2( &data,
153                                       "%SystemRoot%\\system32\\eventlog.dll",
154                                       UNI_STR_TERMINATE );
155
156                         regval_ctr_addvalue( values, "CategoryMessageFile",
157                                              REG_EXPAND_SZ,
158                                              ( char * ) data.buffer,
159                                              data.uni_str_len *
160                                              sizeof( uint16 ) );
161                         regdb_store_values( evtlogpath, values );
162                 }
163                 TALLOC_FREE( values );
164                 elogs++;
165         }
166
167         return True;
168
169 }
170
171 /*********************************************************************
172  for an eventlog, add in a source name. If the eventlog doesn't 
173  exist (not in the list) do nothing.   If a source for the log 
174  already exists, change the information (remove, replace)
175 *********************************************************************/
176
177 BOOL eventlog_add_source( const char *eventlog, const char *sourcename,
178                           const char *messagefile )
179 {
180         /* Find all of the eventlogs, add keys for each of them */
181         /* need to add to the value KEY_EVENTLOG/<eventlog>/Sources string (Creating if necessary)
182            need to add KEY of source to KEY_EVENTLOG/<eventlog>/<source> */
183
184         const char **elogs = lp_eventlog_list(  );
185         char **wrklist, **wp;
186         pstring evtlogpath;
187         REGSUBKEY_CTR *subkeys;
188         REGVAL_CTR *values;
189         REGISTRY_VALUE *rval;
190         UNISTR2 data;
191         uint16 *msz_wp;
192         int mbytes, ii;
193         BOOL already_in;
194         int i;
195         int numsources;
196
197         for ( i = 0; elogs[i]; i++ ) {
198                 if ( strequal( elogs[i], eventlog ) )
199                         break;
200         }
201
202         if ( !elogs[i] ) {
203                 DEBUG( 0,
204                        ( "Eventlog [%s] not found in list of valid event logs\n",
205                          eventlog ) );
206                 return False;   /* invalid named passed in */
207         }
208
209         /* have to assume that the evenlog key itself exists at this point */
210         /* add in a key of [sourcename] under the eventlog key */
211
212         /* todo add to Sources */
213
214         if ( !( values = TALLOC_ZERO_P( NULL, REGVAL_CTR ) ) ) {
215                 DEBUG( 0, ( "talloc() failure!\n" ) );
216                 return False;
217         }
218
219         pstr_sprintf( evtlogpath, "%s\\%s", KEY_EVENTLOG, eventlog );
220
221         regdb_fetch_values( evtlogpath, values );
222
223
224         if ( !( rval = regval_ctr_getvalue( values, "Sources" ) ) ) {
225                 DEBUG( 0, ( "No Sources value for [%s]!\n", eventlog ) );
226                 return False;
227         }
228         /* perhaps this adding a new string to a multi_sz should be a fn? */
229         /* check to see if it's there already */
230
231         if ( rval->type != REG_MULTI_SZ ) {
232                 DEBUG( 0,
233                        ( "Wrong type for Sources, should be REG_MULTI_SZ\n" ) );
234                 return False;
235         }
236         /* convert to a 'regulah' chars to do some comparisons */
237
238         already_in = False;
239         wrklist = NULL;
240         dump_data( 1, rval->data_p, rval->size );
241         if ( ( numsources =
242                regval_convert_multi_sz( ( uint16 * ) rval->data_p, rval->size,
243                                         &wrklist ) ) > 0 ) {
244
245                 ii = numsources;
246                 /* see if it's in there already */
247                 wp = wrklist;
248
249                 while ( ii && wp && *wp ) {
250                         if ( strequal( *wp, sourcename ) ) {
251                                 DEBUG( 5,
252                                        ( "Source name [%s] already in list for [%s] \n",
253                                          sourcename, eventlog ) );
254                                 already_in = True;
255                                 break;
256                         }
257                         wp++;
258                         ii--;
259                 }
260         } else {
261                 if ( numsources < 0 ) {
262                         DEBUG( 3, ( "problem in getting the sources\n" ) );
263                         return False;
264                 }
265                 DEBUG( 3,
266                        ( "Nothing in the sources list, this might be a problem\n" ) );
267         }
268
269         wp = wrklist;
270
271         if ( !already_in ) {
272                 /* make a new list with an additional entry; copy values, add another */
273                 wp = TALLOC_ARRAY( NULL, char *, numsources + 2 );
274
275                 if ( !wp ) {
276                         DEBUG( 0, ( "talloc() failed \n" ) );
277                         return False;
278                 }
279                 memcpy( wp, wrklist, sizeof( char * ) * numsources );
280                 *( wp + numsources ) = ( char * ) sourcename;
281                 *( wp + numsources + 1 ) = NULL;
282                 mbytes = regval_build_multi_sz( wp, &msz_wp );
283                 dump_data( 1, ( uint8 * ) msz_wp, mbytes );
284                 regval_ctr_addvalue( values, "Sources", REG_MULTI_SZ,
285                                      ( char * ) msz_wp, mbytes );
286                 regdb_store_values( evtlogpath, values );
287                 TALLOC_FREE( msz_wp );
288         } else {
289                 DEBUG( 3,
290                        ( "Source name [%s] found in existing list of sources\n",
291                          sourcename ) );
292         }
293         TALLOC_FREE( values );
294         TALLOC_FREE( wrklist ); /*  */
295
296         if ( !( subkeys = TALLOC_ZERO_P( NULL, REGSUBKEY_CTR ) ) ) {
297                 DEBUG( 0, ( "talloc() failure!\n" ) );
298                 return False;
299         }
300         pstr_sprintf( evtlogpath, "%s\\%s", KEY_EVENTLOG, eventlog );
301
302         regdb_fetch_keys( evtlogpath, subkeys );
303
304         if ( !regsubkey_ctr_key_exists( subkeys, sourcename ) ) {
305                 DEBUG( 5,
306                        ( " Source name [%s] for eventlog [%s] didn't exist, adding \n",
307                          sourcename, eventlog ) );
308                 regsubkey_ctr_addkey( subkeys, sourcename );
309                 if ( !regdb_store_keys( evtlogpath, subkeys ) )
310                         return False;
311         }
312         TALLOC_FREE( subkeys );
313
314         /* at this point KEY_EVENTLOG/<eventlog>/<sourcename> key is in there. Now need to add EventMessageFile */
315
316         /* now allocate room for the source's subkeys */
317
318         if ( !( subkeys = TALLOC_ZERO_P( NULL, REGSUBKEY_CTR ) ) ) {
319                 DEBUG( 0, ( "talloc() failure!\n" ) );
320                 return False;
321         }
322         slprintf( evtlogpath, sizeof( evtlogpath ) - 1, "%s\\%s\\%s",
323                   KEY_EVENTLOG, eventlog, sourcename );
324
325         regdb_fetch_keys( evtlogpath, subkeys );
326
327         /* now add the values to the KEY_EVENTLOG/Application form key */
328         if ( !( values = TALLOC_ZERO_P( NULL, REGVAL_CTR ) ) ) {
329                 DEBUG( 0, ( "talloc() failure!\n" ) );
330                 return False;
331         }
332         DEBUG( 5,
333                ( "Storing EventMessageFile [%s] to eventlog path of [%s]\n",
334                  messagefile, evtlogpath ) );
335
336         regdb_fetch_values( evtlogpath, values );
337
338         init_unistr2( &data, messagefile, UNI_STR_TERMINATE );
339
340         regval_ctr_addvalue( values, "EventMessageFile", REG_SZ,
341                              ( char * ) data.buffer,
342                              data.uni_str_len * sizeof( uint16 ) );
343         regdb_store_values( evtlogpath, values );
344
345         TALLOC_FREE( values );
346
347         return True;
348 }