s3-rpc_client: move protos to cli_lsarpc.h
[Samba/ekacnet.git] / source3 / registry / reg_eventlog.c
blobc2b4a75fa9a0d89aa4956751016cf130f5383e6f
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"
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_REGISTRY
29 /**********************************************************************
30 for an eventlog, add in the default values
31 *********************************************************************/
33 bool eventlog_init_keys(void)
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;
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;
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;
60 TALLOC_FREE(subkeys);
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 ) );
67 evtlogpath = talloc_asprintf(ctx, "%s\\%s",
68 KEY_EVENTLOG, *elogs);
69 if (!evtlogpath) {
70 return false;
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;
81 regdb_fetch_keys( evtlogpath, subkeys );
82 regsubkey_ctr_addkey( subkeys, *elogs );
84 if ( !regdb_store_keys( evtlogpath, subkeys ) ) {
85 TALLOC_FREE(subkeys);
86 return False;
88 TALLOC_FREE( subkeys );
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;
95 DEBUG( 5,
96 ( "Storing values to eventlog path of [%s]\n",
97 evtlogpath ) );
98 regdb_fetch_values( evtlogpath, values );
101 if (!regval_ctr_key_exists(values, "MaxSize")) {
103 /* assume we have none, add them all */
105 /* hard code some initial values */
107 /* uiDisplayNameId = 0x00000100; */
108 uiMaxSize = 0x00080000;
109 uiRetention = 0x93A80;
111 regval_ctr_addvalue(values, "MaxSize", REG_DWORD,
112 (char *)&uiMaxSize,
113 sizeof(uint32));
115 regval_ctr_addvalue(values, "Retention", REG_DWORD,
116 (char *)&uiRetention,
117 sizeof(uint32));
119 regval_ctr_addvalue_sz(values, "PrimaryModule", *elogs);
120 push_reg_sz(talloc_tos(), &data, *elogs);
122 regval_ctr_addvalue(values, "Sources", REG_MULTI_SZ,
123 (char *)data.data,
124 data.length);
126 evtfilepath = talloc_asprintf(ctx,
127 "%%SystemRoot%%\\system32\\config\\%s.tdb",
128 *elogs);
129 if (!evtfilepath) {
130 TALLOC_FREE(values);
132 push_reg_sz(talloc_tos(), &data, evtfilepath);
133 regval_ctr_addvalue(values, "File", REG_EXPAND_SZ, (char *)data.data,
134 data.length);
135 regdb_store_values(evtlogpath, values);
139 TALLOC_FREE(values);
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;
148 if (!(values = TALLOC_ZERO_P(ctx, struct regval_ctr))) {
149 DEBUG( 0, ( "talloc() failure!\n" ) );
150 return False;
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")) {
158 /* hard code some initial values */
160 uiCategoryCount = 0x00000007;
161 regval_ctr_addvalue( values, "CategoryCount",
162 REG_DWORD,
163 ( char * ) &uiCategoryCount,
164 sizeof( uint32 ) );
165 push_reg_sz(talloc_tos(), &data,
166 "%SystemRoot%\\system32\\eventlog.dll");
168 regval_ctr_addvalue( values, "CategoryMessageFile",
169 REG_EXPAND_SZ,
170 ( char * ) data.data,
171 data.length);
172 regdb_store_values( evtlogpath, values );
174 TALLOC_FREE(values);
175 elogs++;
178 return true;
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 *********************************************************************/
187 bool eventlog_add_source( const char *eventlog, const char *sourcename,
188 const char *messagefile )
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> */
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;
208 if (!elogs) {
209 return False;
212 for ( i = 0; elogs[i]; i++ ) {
213 if ( strequal( elogs[i], eventlog ) )
214 break;
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 */
224 /* have to assume that the evenlog key itself exists at this point */
225 /* add in a key of [sourcename] under the eventlog key */
227 /* todo add to Sources */
229 if (!( values = TALLOC_ZERO_P(ctx, struct regval_ctr))) {
230 DEBUG( 0, ( "talloc() failure!\n" ));
231 return false;
234 evtlogpath = talloc_asprintf(ctx, "%s\\%s", KEY_EVENTLOG, eventlog);
235 if (!evtlogpath) {
236 TALLOC_FREE(values);
237 return false;
240 regdb_fetch_values( evtlogpath, values );
243 if ( !( rval = regval_ctr_getvalue( values, "Sources" ) ) ) {
244 DEBUG( 0, ( "No Sources value for [%s]!\n", eventlog ) );
245 return False;
247 /* perhaps this adding a new string to a multi_sz should be a fn? */
248 /* check to see if it's there already */
250 if ( rval->type != REG_MULTI_SZ ) {
251 DEBUG( 0,
252 ( "Wrong type for Sources, should be REG_MULTI_SZ\n" ) );
253 return False;
255 /* convert to a 'regulah' chars to do some comparisons */
257 already_in = False;
258 wrklist = NULL;
259 dump_data( 1, rval->data_p, rval->size );
261 blob = data_blob_const(rval->data_p, rval->size);
262 if (!pull_reg_multi_sz(talloc_tos(), &blob, &wrklist)) {
263 return false;
266 for (ii=0; wrklist[ii]; ii++) {
267 numsources++;
270 if (numsources > 0) {
271 /* see if it's in there already */
272 wp = wrklist;
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;
282 wp++;
284 } else {
285 DEBUG( 3,
286 ( "Nothing in the sources list, this might be a problem\n" ) );
289 wp = wrklist;
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 );
295 if ( !wp ) {
296 DEBUG( 0, ( "talloc() failed \n" ) );
297 return False;
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;
305 dump_data( 1, blob.data, blob.length);
306 regval_ctr_addvalue( values, "Sources", REG_MULTI_SZ,
307 ( char * ) 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 ) );
315 TALLOC_FREE(values);
316 TALLOC_FREE(wrklist); /* */
318 werr = regsubkey_ctr_init(ctx, &subkeys);
319 if (!W_ERROR_IS_OK(werr)) {
320 DEBUG( 0, ( "talloc() failure!\n" ) );
321 return False;
323 TALLOC_FREE(evtlogpath);
324 evtlogpath = talloc_asprintf(ctx, "%s\\%s", KEY_EVENTLOG, eventlog );
325 if (!evtlogpath) {
326 TALLOC_FREE(subkeys);
327 return false;
330 regdb_fetch_keys( evtlogpath, subkeys );
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;
340 TALLOC_FREE(subkeys);
342 /* at this point KEY_EVENTLOG/<eventlog>/<sourcename> key is in there. Now need to add EventMessageFile */
344 /* now allocate room for the source's subkeys */
346 werr = regsubkey_ctr_init(ctx, &subkeys);
347 if (!W_ERROR_IS_OK(werr)) {
348 DEBUG( 0, ( "talloc() failure!\n" ) );
349 return False;
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;
359 regdb_fetch_keys( evtlogpath, subkeys );
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;
366 DEBUG( 5,
367 ( "Storing EventMessageFile [%s] to eventlog path of [%s]\n",
368 messagefile, evtlogpath ) );
370 regdb_fetch_values( evtlogpath, values );
372 regval_ctr_addvalue_sz(values, "EventMessageFile", messagefile);
373 regdb_store_values( evtlogpath, values );
375 TALLOC_FREE(values);
377 return True;