s3:registry: extract the reg_backend_db prototypes into their own header.
[Samba/ekacnet.git] / source3 / registry / reg_eventlog.c
blob43b6bb8968d01f103d904f2c6a37fbc97ed99273
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.
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.
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/>.
23 #include "includes.h"
24 #include "registry.h"
25 #include "reg_backend_db.h"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_REGISTRY
30 /**********************************************************************
31 for an eventlog, add in the default values
32 *********************************************************************/
34 bool eventlog_init_keys(void)
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;
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;
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;
61 TALLOC_FREE(subkeys);
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 ) );
68 evtlogpath = talloc_asprintf(ctx, "%s\\%s",
69 KEY_EVENTLOG, *elogs);
70 if (!evtlogpath) {
71 return false;
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;
82 regdb_fetch_keys( evtlogpath, subkeys );
83 regsubkey_ctr_addkey( subkeys, *elogs );
85 if ( !regdb_store_keys( evtlogpath, subkeys ) ) {
86 TALLOC_FREE(subkeys);
87 return False;
89 TALLOC_FREE( subkeys );
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;
96 DEBUG( 5,
97 ( "Storing values to eventlog path of [%s]\n",
98 evtlogpath ) );
99 regdb_fetch_values( evtlogpath, values );
102 if (!regval_ctr_key_exists(values, "MaxSize")) {
104 /* assume we have none, add them all */
106 /* hard code some initial values */
108 /* uiDisplayNameId = 0x00000100; */
109 uiMaxSize = 0x00080000;
110 uiRetention = 0x93A80;
112 regval_ctr_addvalue(values, "MaxSize", REG_DWORD,
113 (uint8 *)&uiMaxSize,
114 sizeof(uint32));
116 regval_ctr_addvalue(values, "Retention", REG_DWORD,
117 (uint8 *)&uiRetention,
118 sizeof(uint32));
120 regval_ctr_addvalue_sz(values, "PrimaryModule", *elogs);
121 push_reg_sz(talloc_tos(), &data, *elogs);
123 regval_ctr_addvalue(values, "Sources", REG_MULTI_SZ,
124 data.data,
125 data.length);
127 evtfilepath = talloc_asprintf(ctx,
128 "%%SystemRoot%%\\system32\\config\\%s.tdb",
129 *elogs);
130 if (!evtfilepath) {
131 TALLOC_FREE(values);
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);
140 TALLOC_FREE(values);
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;
149 if (!(values = TALLOC_ZERO_P(ctx, struct regval_ctr))) {
150 DEBUG( 0, ( "talloc() failure!\n" ) );
151 return False;
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")) {
159 /* hard code some initial values */
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");
169 regval_ctr_addvalue( values, "CategoryMessageFile",
170 REG_EXPAND_SZ,
171 data.data,
172 data.length);
173 regdb_store_values( evtlogpath, values );
175 TALLOC_FREE(values);
176 elogs++;
179 return true;
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 *********************************************************************/
188 bool eventlog_add_source( const char *eventlog, const char *sourcename,
189 const char *messagefile )
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> */
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;
209 if (!elogs) {
210 return False;
213 for ( i = 0; elogs[i]; i++ ) {
214 if ( strequal( elogs[i], eventlog ) )
215 break;
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 */
225 /* have to assume that the evenlog key itself exists at this point */
226 /* add in a key of [sourcename] under the eventlog key */
228 /* todo add to Sources */
230 if (!( values = TALLOC_ZERO_P(ctx, struct regval_ctr))) {
231 DEBUG( 0, ( "talloc() failure!\n" ));
232 return false;
235 evtlogpath = talloc_asprintf(ctx, "%s\\%s", KEY_EVENTLOG, eventlog);
236 if (!evtlogpath) {
237 TALLOC_FREE(values);
238 return false;
241 regdb_fetch_values( evtlogpath, values );
244 if ( !( rval = regval_ctr_getvalue( values, "Sources" ) ) ) {
245 DEBUG( 0, ( "No Sources value for [%s]!\n", eventlog ) );
246 return False;
248 /* perhaps this adding a new string to a multi_sz should be a fn? */
249 /* check to see if it's there already */
251 if ( rval->type != REG_MULTI_SZ ) {
252 DEBUG( 0,
253 ( "Wrong type for Sources, should be REG_MULTI_SZ\n" ) );
254 return False;
256 /* convert to a 'regulah' chars to do some comparisons */
258 already_in = False;
259 wrklist = NULL;
260 dump_data( 1, rval->data_p, rval->size );
262 blob = data_blob_const(rval->data_p, rval->size);
263 if (!pull_reg_multi_sz(talloc_tos(), &blob, &wrklist)) {
264 return false;
267 for (ii=0; wrklist[ii]; ii++) {
268 numsources++;
271 if (numsources > 0) {
272 /* see if it's in there already */
273 wp = wrklist;
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;
283 wp++;
285 } else {
286 DEBUG( 3,
287 ( "Nothing in the sources list, this might be a problem\n" ) );
290 wp = wrklist;
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 );
296 if ( !wp ) {
297 DEBUG( 0, ( "talloc() failed \n" ) );
298 return False;
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;
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 ) );
316 TALLOC_FREE(values);
317 TALLOC_FREE(wrklist); /* */
319 werr = regsubkey_ctr_init(ctx, &subkeys);
320 if (!W_ERROR_IS_OK(werr)) {
321 DEBUG( 0, ( "talloc() failure!\n" ) );
322 return False;
324 TALLOC_FREE(evtlogpath);
325 evtlogpath = talloc_asprintf(ctx, "%s\\%s", KEY_EVENTLOG, eventlog );
326 if (!evtlogpath) {
327 TALLOC_FREE(subkeys);
328 return false;
331 regdb_fetch_keys( evtlogpath, subkeys );
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;
341 TALLOC_FREE(subkeys);
343 /* at this point KEY_EVENTLOG/<eventlog>/<sourcename> key is in there. Now need to add EventMessageFile */
345 /* now allocate room for the source's subkeys */
347 werr = regsubkey_ctr_init(ctx, &subkeys);
348 if (!W_ERROR_IS_OK(werr)) {
349 DEBUG( 0, ( "talloc() failure!\n" ) );
350 return False;
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;
360 regdb_fetch_keys( evtlogpath, subkeys );
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;
367 DEBUG( 5,
368 ( "Storing EventMessageFile [%s] to eventlog path of [%s]\n",
369 messagefile, evtlogpath ) );
371 regdb_fetch_values( evtlogpath, values );
373 regval_ctr_addvalue_sz(values, "EventMessageFile", messagefile);
374 regdb_store_values( evtlogpath, values );
376 TALLOC_FREE(values);
378 return True;