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