File: syslog.c

package info (click to toggle)
dpm-postgres 1.7.4.7-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 13,788 kB
  • ctags: 10,782
  • sloc: ansic: 146,136; sh: 13,362; perl: 11,142; python: 5,529; cpp: 5,113; sql: 1,790; makefile: 955; fortran: 113
file content (441 lines) | stat: -rw-r--r-- 11,181 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
 * Copyright (C) 1997-1998 by CERN/IT/PDP/IP
 * All rights reserved
 */
 
#ifndef lint
static char sccsid[] = "@(#)$RCSfile: syslog.c,v $ $Revision: 1.1 $ $Date: 2005/04/13 17:00:30 $ CERN/IT/PDP/DM Christoph von Praun";
#endif /* not lint */
 
/*
 *  05/11/97	(CVP)
 *
 * Mods:
 *  08/02/98	Added conditional main program at the end of this module
 *            Added the file slogmsg.h to the libsyslg package (CVP)
 *            Changed openlog()
 *     
 */

#ifdef _UNICODE
#error syslog does not support the UNICODE character set
#endif

/* 
 * c-headers 
 */
#include <sys/types.h>
#include <string.h>
#include <stdarg.h>   /* ANSI C*/
#include <time.h>
#include <stdio.h>
#include <process.h>
#include <stdlib.h>

/* 
 * windows-headers 
 */
#include <windows.h>
#include <wtypes.h>
#include <winbase.h>
#include <winerror.h>

/* 
 * local-headers 
 */
#include "syslog.h"
#include "slogmsg.h"

/* 
 * defines
 */

#define SLG_APP_NAME "syslog"
#define SLG_APP_FILE_PATH "%SystemRoot%\\system32\\slogmsg.dll"
#define SLG_CAT_FILE_PATH "%SystemRoot%\\system32\\slogmsg.dll"
#define SLG_CAT_COUNT 1
#define SLG_APP_REG_PATH "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\syslog"

#ifdef _DEBUG
#define SLG_PANIC() c = getc(stdin)
#define SLG_DEBUG(COMMAND) COMMAND
#else 
#define SLG_PANIC() ; 
#define SLG_DEBUG(COMMAND) ;
#endif

#define SLG_DEBUG_OUT stderr
#define SLG_BUFSIZE 2048

/* 
 * error messages
 */
#define SLG00 "SLG00 - failed to initialise SID\n"
#define SLG01 "SLG01 - failed to initialise username due to error %d\n"
#define SLG02 "SLG02 - could not report event; maybe log was not opened before?\n"
#define SLG03 "SLG03 - could not create nor open registry key\n"
#define SLG04 "SLG04 - could not set event message file\n"
#define SLG05 "SLG05 - could not set category message file\n"
#define SLG06 "SLG06 - could not set category count file\n"
#define SLG07 "SLG07 - could not set supported types\n"
#define SLG08 "SLG08 - could not register event source\n"
#define SLG09 "SLG09 - DeregisterEventSource failed due to error %d\n"

/* 
 * global variables 
 */
static HANDLE h = NULL;
static int c = 0;
static char username[SLG_BUFSIZE];

/* 
 * procedures 
 */
void syslog(int pri, const char *fmt, ...)
/* 
 * Purpose:
 *		Wrapper function, called by the user, 
 *    The actual work is done by vsyslog()
 *
 * Precondition: 
 *		ftm != NULL, varag list matches the number 
 *    of substitusion strings in fmt
 *    pri is a valid priority
 *
 * Postcondition:
 *		The indicated message is logged into the 
 *    NT - Application log at the given priority
 *
 * Mods:
 *		05/11/97	(CVP)
 */
{
	va_list ap;
	va_start(ap, fmt);
	vsyslog(pri, fmt, ap);
	va_end(ap);
}
	
void vsyslog(int pri, const char *fmt, va_list ap)
/* 
 * Purpose:
 *		Does the actual logging to the NT event log.
 *		This is actually never called directly by the user
 *		but always through syslog()
 *
 * Precondition: 
 *		dto. syslog()
 *
 * Postcondition:
 *		dto. syslog()
 *
 * Mods:
 *		05/11/97	(CVP)
 */
{

	char *aszMsg[1], *p;
	WORD nt_prio = 0;
	
	BYTE sidBuffer[SLG_BUFSIZE];
	PSID psid = (PSID) &sidBuffer;
	PSID PNtSid_ = NULL;
	DWORD sidBufferSize = SLG_BUFSIZE * sizeof(BYTE);
	TCHAR domainBuffer[SLG_BUFSIZE];
	DWORD domainBufferSize = SLG_BUFSIZE * sizeof(TCHAR);
	SID_NAME_USE snu;
	UCHAR SubAuthorityCount = 0;
	BOOL retBOOL = TRUE;
	DWORD SubAuthIndex = 0;

	int saved_errno = 0;
	int nt_pid = _getpid();
	char tbuf[SLG_BUFSIZE];
	char fmt_cpy[SLG_BUFSIZE];
	DWORD bufsize = SLG_BUFSIZE;
	BOOL resBOOL = 0;
	char ch, *t1, *t2;
	DWORD retDWORD = 0;

	saved_errno = errno;
	
	/* 
	 * Username retrieval 
	 */
	if (*username == 0) {
		retBOOL = GetUserName(username, &bufsize); 
		if (retBOOL == 0) {
			SLG_DEBUG(fprintf(SLG_DEBUG_OUT, SLG01, GetLastError()));
			SLG_PANIC();
		}
	}

	ZeroMemory(sidBuffer, SLG_BUFSIZE);
	LookupAccountName(
			NULL, 
			username, 
			sidBuffer,
			&sidBufferSize,
			domainBuffer, 
			&domainBufferSize,
			&snu);

	SubAuthorityCount = *GetSidSubAuthorityCount(psid);

	retBOOL = AllocateAndInitializeSid (
		GetSidIdentifierAuthority(psid),
		SubAuthorityCount,
		0,0,0,0,0,0,0,0,
		&PNtSid_
		);
			
	if (retBOOL != 0) {
		for( ; SubAuthIndex < SubAuthorityCount ; SubAuthIndex++) {
                *GetSidSubAuthority(PNtSid_, SubAuthIndex) =
                *GetSidSubAuthority(psid, SubAuthIndex);
		}
	}
/*
 * in case of error, PNtSid_ will remain NULL, 
 * which is OK anyway
 */

	(void)sprintf(tbuf, "[pid %d / user %s]  ", nt_pid, username);
	for (p = tbuf; *p; ++p);

/*
 * build the message
 * substitute error message for %m 
 */

	for (t1 = fmt_cpy; ch = *fmt; ++fmt) {
		if (ch == '%' && fmt[1] == 'm') {
			++fmt;
			for (t2 = strerror(saved_errno); *t1 = *t2++; ++t1);
		}
		else
			*t1++ = ch;
	}
	*t1 = '\0';

	vsprintf(p, fmt_cpy, ap);
	aszMsg[0] = tbuf;

/*
 * consider the event types:
 *		EVENTLOG_ERROR_TYPE 
 *		EVENTLOG_WARNING_TYPE 
 *		EVENTLOG_INFORMATION_TYPE
 *
 */

	switch(pri) {
	case LOG_EMERG:												/* system is unusable */
	case LOG_ALERT:												/* action must be taken immediately */
	case LOG_CRIT:												/* critical conditions */
	case LOG_ERR:													/* error conditions */
		nt_prio = EVENTLOG_ERROR_TYPE;
		break;
	case LOG_WARNING:											/* warning conditions */
	case LOG_NOTICE:											/* normal but significant condition */
		nt_prio = EVENTLOG_WARNING_TYPE;
		break;
	case LOG_INFO:												/* informational */
	case LOG_DEBUG:												/* debug-level messages */
		nt_prio = EVENTLOG_INFORMATION_TYPE;
		break;
	}

 
	if (!ReportEvent(
				h,										/* event log handle            */ 
        nt_prio,							/* event type                  */ 
        SLG_ONE,							/* category identifier         */ 
        SLG_01,								/* event identifier            */ 
        PNtSid_,							/* user security identifier    */ 
        1,                    /* one substitution string     */ 
        0,                    /* no data                     */ 
        (LPTSTR *) aszMsg,    /* address of string array     */ 
        NULL)                 /* address of data             */ 
	) {
			SLG_DEBUG(fprintf(SLG_DEBUG_OUT, SLG02));
			SLG_PANIC(); 
	}
}



void openlog(const char *ident, int logstat, int logfac)
/* 
 * Purpose:
 *		Initialises the handle to the systemlog that is needed
 *    to put messages in it. Before it checks the registry and
 *    looks if the syslog is already registered as an event 
 *    source
 *
 * Precondition: 
 *    ident, logstat and logfac are ignored, they are there to  
 *    provide the Unix interface
 *
 * Postcondition:
 *		handle released if it was opened before
 *
 * Mods:
 *		05/11/97	(CVP)
 *    12/12/97  (CVP) check if handle == NULL to handle repeated 
 *                    calls to this procedure
 *    09/02/97  (CVP) add check if the key HKEY_LOCAL_MACHINE\SLG_APP_REG_PATH
 *                    does already exist in the registry. If so, all entires to 
 *                    the registry are skipped and a simple RegisterEventSource 
 *                    is sufficient.
 *    10/10/98	(AB)  use of ident parameter, which indicates source of the log
 *                    entries. This parameter was previously ignored. 
 */
{
	HKEY hk; 
	DWORD dwData; 
	CHAR szBuf[SLG_BUFSIZE];
	DWORD catCount;
	
	/* 
	 * repeated calls to openlog should have no effect
	 */ 

	if (h == NULL) {
		ZeroMemory(username, SLG_BUFSIZE * sizeof(char));
		
		/* 
		 * Check if the key is already there 
		 * HKEY_LOCAL_MACHINE\SLG_APP_REG_PATH
		 */
		if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, SLG_APP_REG_PATH, 0,
			KEY_READ, &hk) != ERROR_SUCCESS) {
		
			/* 
			 * Add your source name as a subkey under the Application 
			 * key in the EventLog service portion of the registry. 
			 */ 

			if (RegCreateKey(HKEY_LOCAL_MACHINE, SLG_APP_REG_PATH, &hk)) {
					SLG_DEBUG(fprintf(SLG_DEBUG_OUT, SLG03));
					SLG_PANIC();
			}
 
			/* 
			 * Set the Event ID message-file name. 
			 */ 
 
			strcpy(szBuf, SLG_APP_FILE_PATH); 
 
			/* 
			 * Add the Event ID message-file name to the subkey. 
			 */ 
			if (RegSetValueEx(hk,             /* subkey handle         */ 
							"EventMessageFile",       /* value name            */ 
							0,                        /* must be zero          */ 
							REG_EXPAND_SZ,            /* value type            */ 
							(LPBYTE) szBuf,           /* address of value data */ 
							strlen(szBuf) + 1)				/* length of value data  */ 
					) {      
					SLG_DEBUG(printf(SLG04));
					SLG_PANIC();
			}

			/* 
			 * Add the Category ID category-file name to the subkey. 
			 */ 
			strcpy(szBuf, SLG_CAT_FILE_PATH); 
			if (RegSetValueEx(hk,             /* subkey handle         */ 
							"CategoryMessageFile",    /* value name            */ 
							0,                        /* must be zero          */ 
							REG_EXPAND_SZ,            /* value type            */ 
							(LPBYTE) szBuf,           /* address of value data */ 
							strlen(szBuf) + 1)				/* length of value data  */ 
					) {      
					SLG_DEBUG(fprintf(SLG_DEBUG_OUT, SLG05));
					SLG_PANIC();
			}

			/* 
			 * Set the category Counter 
			 */
			catCount = SLG_CAT_COUNT;
			if (RegSetValueEx(hk,             /* subkey handle         */ 
							"CategoryCount",          /* value name            */ 
							0,                        /* must be zero          */ 
							REG_DWORD,                /* value type            */ 
							(LPBYTE) &catCount,       /* address of value data */ 
							sizeof(DWORD))				    /* length of value data  */ 
					) {      
					SLG_DEBUG(fprintf(SLG_DEBUG_OUT, SLG06));
					SLG_PANIC();
			}

			/* 
			 * Set the supported types flags. 
			 */ 
			dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE; 
 			if (RegSetValueEx(hk,      /* subkey handle                */ 
							"TypesSupported",  /* value name                   */ 
							0,                 /* must be zero                 */ 
							REG_DWORD,         /* value type                   */ 
							(LPBYTE) &dwData,  /* address of value data        */ 
							sizeof(DWORD))	   /* length of value data         */ 
					) {
				SLG_DEBUG(fprintf(SLG_DEBUG_OUT, SLG07));
				SLG_PANIC(); 
			}
		} /* make appropriate regentries for the event source syslog */
		
		RegCloseKey(hk);

		h = RegisterEventSource(
			NULL,						/* uses local computer         */ 
			(ident==NULL) ? SLG_APP_NAME:ident); 	/* source name - syslog by default */

		if (h == NULL) {	
			SLG_DEBUG(fprintf(SLG_DEBUG_OUT, SLG08));
			SLG_PANIC();
		}
	}
}

void closelog() 
/* 
 * Purpose:
 *		Close the handle to the systemlog 
 *
 * Precondition: 
 *    None
 *
 * Postcondition:
 *		handle released if it was opened before
 *
 * Mods:
 *		05/11/97	(CVP)
 *    12/12/97  (CVP) handle will ne set to NULL as soon as it is closed
 */
{
	BOOL retBOOL = 0;

	if (h != NULL) {
		retBOOL = DeregisterEventSource(h); 
		if (retBOOL == 0) {
			SLG_DEBUG(fprintf(SLG_DEBUG_OUT, SLG09, GetLastError()));
			SLG_PANIC();
		}
	}
	h = NULL;
}



#ifdef SLG_TEST
void main() {
	char c;
	openlog(NULL, 0, 0);
	syslog(LOG_INFO, "event %m %d ", 10);
	closelog();
	c = getc(stdin);
}
#endif