s3:registry: use regsubkey_ctr_init() in reg_eventlog.c
[Samba/bb.git] / source / registry / reg_eventlog.c
blobc02318beacb734306feb73e7b50a0d9d2f8405cd
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"
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_REGISTRY
28 /**********************************************************************
29 for an eventlog, add in the default values
30 *********************************************************************/
32 bool eventlog_init_keys(void)
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 REGVAL_CTR *values;
40 uint32 uiMaxSize;
41 uint32 uiRetention;
42 uint32 uiCategoryCount;
43 UNISTR2 data;
44 TALLOC_CTX *ctx = talloc_tos();
45 WERROR werr;
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;
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;
59 TALLOC_FREE(subkeys);
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 ) );
66 evtlogpath = talloc_asprintf(ctx, "%s\\%s",
67 KEY_EVENTLOG, *elogs);
68 if (!evtlogpath) {
69 return false;
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;
80 regdb_fetch_keys( evtlogpath, subkeys );
81 regsubkey_ctr_addkey( subkeys, *elogs );
83 if ( !regdb_store_keys( evtlogpath, subkeys ) ) {
84 TALLOC_FREE(subkeys);
85 return False;
87 TALLOC_FREE( subkeys );
89 /* now add the values to the KEY_EVENTLOG/Application form key */
90 if (!(values = TALLOC_ZERO_P(ctx, REGVAL_CTR))) {
91 DEBUG( 0, ( "talloc() failure!\n" ) );
92 return False;
94 DEBUG( 5,
95 ( "Storing values to eventlog path of [%s]\n",
96 evtlogpath ) );
97 regdb_fetch_values( evtlogpath, values );
100 if (!regval_ctr_key_exists(values, "MaxSize")) {
102 /* assume we have none, add them all */
104 /* hard code some initial values */
106 /* uiDisplayNameId = 0x00000100; */
107 uiMaxSize = 0x00080000;
108 uiRetention = 0x93A80;
110 regval_ctr_addvalue(values, "MaxSize", REG_DWORD,
111 (char *)&uiMaxSize,
112 sizeof(uint32));
114 regval_ctr_addvalue(values, "Retention", REG_DWORD,
115 (char *)&uiRetention,
116 sizeof(uint32));
117 init_unistr2(&data, *elogs, UNI_STR_TERMINATE);
119 regval_ctr_addvalue(values, "PrimaryModule", REG_SZ,
120 (char *)data.buffer,
121 data.uni_str_len *
122 sizeof(uint16));
123 init_unistr2(&data, *elogs, UNI_STR_TERMINATE);
125 regval_ctr_addvalue(values, "Sources", REG_MULTI_SZ,
126 (char *)data.buffer,
127 data.uni_str_len *
128 sizeof(uint16));
130 evtfilepath = talloc_asprintf(ctx,
131 "%%SystemRoot%%\\system32\\config\\%s.tdb",
132 *elogs);
133 if (!evtfilepath) {
134 TALLOC_FREE(values);
136 init_unistr2(&data, evtfilepath, UNI_STR_TERMINATE);
137 regval_ctr_addvalue(values, "File", REG_EXPAND_SZ, (char *)data.buffer,
138 data.uni_str_len * sizeof(uint16));
139 regdb_store_values(evtlogpath, values);
143 TALLOC_FREE(values);
145 /* now do the values under KEY_EVENTLOG/Application/Application */
146 TALLOC_FREE(evtlogpath);
147 evtlogpath = talloc_asprintf(ctx, "%s\\%s\\%s",
148 KEY_EVENTLOG, *elogs, *elogs);
149 if (!evtlogpath) {
150 return false;
152 if (!(values = TALLOC_ZERO_P(ctx, REGVAL_CTR))) {
153 DEBUG( 0, ( "talloc() failure!\n" ) );
154 return False;
156 DEBUG( 5,
157 ( "Storing values to eventlog path of [%s]\n",
158 evtlogpath));
159 regdb_fetch_values(evtlogpath, values);
160 if (!regval_ctr_key_exists( values, "CategoryCount")) {
162 /* hard code some initial values */
164 uiCategoryCount = 0x00000007;
165 regval_ctr_addvalue( values, "CategoryCount",
166 REG_DWORD,
167 ( char * ) &uiCategoryCount,
168 sizeof( uint32 ) );
169 init_unistr2( &data,
170 "%SystemRoot%\\system32\\eventlog.dll",
171 UNI_STR_TERMINATE );
173 regval_ctr_addvalue( values, "CategoryMessageFile",
174 REG_EXPAND_SZ,
175 ( char * ) data.buffer,
176 data.uni_str_len *
177 sizeof( uint16 ) );
178 regdb_store_values( evtlogpath, values );
180 TALLOC_FREE(values);
181 elogs++;
184 return true;
187 /*********************************************************************
188 for an eventlog, add in a source name. If the eventlog doesn't
189 exist (not in the list) do nothing. If a source for the log
190 already exists, change the information (remove, replace)
191 *********************************************************************/
193 bool eventlog_add_source( const char *eventlog, const char *sourcename,
194 const char *messagefile )
196 /* Find all of the eventlogs, add keys for each of them */
197 /* need to add to the value KEY_EVENTLOG/<eventlog>/Sources string (Creating if necessary)
198 need to add KEY of source to KEY_EVENTLOG/<eventlog>/<source> */
200 const char **elogs = lp_eventlog_list( );
201 char **wrklist, **wp;
202 char *evtlogpath = NULL;
203 struct regsubkey_ctr *subkeys;
204 REGVAL_CTR *values;
205 REGISTRY_VALUE *rval;
206 UNISTR2 data;
207 uint16 *msz_wp;
208 int mbytes, ii;
209 bool already_in;
210 int i;
211 int numsources;
212 TALLOC_CTX *ctx = talloc_tos();
213 WERROR werr;
215 if (!elogs) {
216 return False;
219 for ( i = 0; elogs[i]; i++ ) {
220 if ( strequal( elogs[i], eventlog ) )
221 break;
224 if ( !elogs[i] ) {
225 DEBUG( 0,
226 ( "Eventlog [%s] not found in list of valid event logs\n",
227 eventlog ) );
228 return false; /* invalid named passed in */
231 /* have to assume that the evenlog key itself exists at this point */
232 /* add in a key of [sourcename] under the eventlog key */
234 /* todo add to Sources */
236 if (!( values = TALLOC_ZERO_P(ctx, REGVAL_CTR))) {
237 DEBUG( 0, ( "talloc() failure!\n" ));
238 return false;
241 evtlogpath = talloc_asprintf(ctx, "%s\\%s", KEY_EVENTLOG, eventlog);
242 if (!evtlogpath) {
243 TALLOC_FREE(values);
244 return false;
247 regdb_fetch_values( evtlogpath, values );
250 if ( !( rval = regval_ctr_getvalue( values, "Sources" ) ) ) {
251 DEBUG( 0, ( "No Sources value for [%s]!\n", eventlog ) );
252 return False;
254 /* perhaps this adding a new string to a multi_sz should be a fn? */
255 /* check to see if it's there already */
257 if ( rval->type != REG_MULTI_SZ ) {
258 DEBUG( 0,
259 ( "Wrong type for Sources, should be REG_MULTI_SZ\n" ) );
260 return False;
262 /* convert to a 'regulah' chars to do some comparisons */
264 already_in = False;
265 wrklist = NULL;
266 dump_data( 1, rval->data_p, rval->size );
267 if ( ( numsources =
268 regval_convert_multi_sz( ( uint16 * ) rval->data_p, rval->size,
269 &wrklist ) ) > 0 ) {
271 ii = numsources;
272 /* see if it's in there already */
273 wp = wrklist;
275 while ( ii && 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++;
284 ii--;
286 } else {
287 if ( numsources < 0 ) {
288 DEBUG( 3, ( "problem in getting the sources\n" ) );
289 return False;
291 DEBUG( 3,
292 ( "Nothing in the sources list, this might be a problem\n" ) );
295 wp = wrklist;
297 if ( !already_in ) {
298 /* make a new list with an additional entry; copy values, add another */
299 wp = TALLOC_ARRAY(ctx, char *, numsources + 2 );
301 if ( !wp ) {
302 DEBUG( 0, ( "talloc() failed \n" ) );
303 return False;
305 memcpy( wp, wrklist, sizeof( char * ) * numsources );
306 *( wp + numsources ) = ( char * ) sourcename;
307 *( wp + numsources + 1 ) = NULL;
308 mbytes = regval_build_multi_sz( wp, &msz_wp );
309 dump_data( 1, ( uint8 * ) msz_wp, mbytes );
310 regval_ctr_addvalue( values, "Sources", REG_MULTI_SZ,
311 ( char * ) msz_wp, mbytes );
312 regdb_store_values( evtlogpath, values );
313 TALLOC_FREE(msz_wp);
314 } else {
315 DEBUG( 3,
316 ( "Source name [%s] found in existing list of sources\n",
317 sourcename ) );
319 TALLOC_FREE(values);
320 TALLOC_FREE(wrklist); /* */
322 werr = regsubkey_ctr_init(ctx, &subkeys);
323 if (!W_ERROR_IS_OK(werr)) {
324 DEBUG( 0, ( "talloc() failure!\n" ) );
325 return False;
327 TALLOC_FREE(evtlogpath);
328 evtlogpath = talloc_asprintf(ctx, "%s\\%s", KEY_EVENTLOG, eventlog );
329 if (!evtlogpath) {
330 TALLOC_FREE(subkeys);
331 return false;
334 regdb_fetch_keys( evtlogpath, subkeys );
336 if ( !regsubkey_ctr_key_exists( subkeys, sourcename ) ) {
337 DEBUG( 5,
338 ( " Source name [%s] for eventlog [%s] didn't exist, adding \n",
339 sourcename, eventlog ) );
340 regsubkey_ctr_addkey( subkeys, sourcename );
341 if ( !regdb_store_keys( evtlogpath, subkeys ) )
342 return False;
344 TALLOC_FREE(subkeys);
346 /* at this point KEY_EVENTLOG/<eventlog>/<sourcename> key is in there. Now need to add EventMessageFile */
348 /* now allocate room for the source's subkeys */
350 werr = regsubkey_ctr_init(ctx, &subkeys);
351 if (!W_ERROR_IS_OK(werr)) {
352 DEBUG( 0, ( "talloc() failure!\n" ) );
353 return False;
355 TALLOC_FREE(evtlogpath);
356 evtlogpath = talloc_asprintf(ctx, "%s\\%s\\%s",
357 KEY_EVENTLOG, eventlog, sourcename);
358 if (!evtlogpath) {
359 TALLOC_FREE(subkeys);
360 return false;
363 regdb_fetch_keys( evtlogpath, subkeys );
365 /* now add the values to the KEY_EVENTLOG/Application form key */
366 if ( !( values = TALLOC_ZERO_P(ctx, REGVAL_CTR ) ) ) {
367 DEBUG( 0, ( "talloc() failure!\n" ) );
368 return False;
370 DEBUG( 5,
371 ( "Storing EventMessageFile [%s] to eventlog path of [%s]\n",
372 messagefile, evtlogpath ) );
374 regdb_fetch_values( evtlogpath, values );
376 init_unistr2( &data, messagefile, UNI_STR_TERMINATE );
378 regval_ctr_addvalue( values, "EventMessageFile", REG_SZ,
379 ( char * ) data.buffer,
380 data.uni_str_len * sizeof( uint16 ) );
381 regdb_store_values( evtlogpath, values );
383 TALLOC_FREE(values);
385 return True;