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