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