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