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