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