1/*
2 * Copyright (c) 2008 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License").  You may not use this file except in compliance with the
9 * License.  Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23#include <stdlib.h>
24#include <stdio.h>
25#import	 <asl.h>
26#include <sys/types.h>
27#include "ipsecConfigTracer.h"
28#include "ipsecMessageTracer.h"
29
30const char * ipsecConfigTracerFailedString = "Tracer Failed";
31const char * ipsecConfigInvalidEventString = "Invalid Event";
32const char * ipsecConfigString			   = "IPSEC";
33
34const char * const ipsecConfigEventStrings[IPSECCONFIGEVENTCODE_MAX] =	{	CONSTSTR("NONE") /* index place holder */,
35																			CONSTSTR("Configuration Reparse Error"),
36																			CONSTSTR("Configuration Parse Error"),
37                                                                            CONSTSTR("Signal Error"),
38																		};
39
40const char *
41ipsecConfigEventCodeToString (ipsecConfigEventCode_t eventCode)
42{
43	if (eventCode <= IPSECCONFIGEVENTCODE_NONE || eventCode >= IPSECCONFIGEVENTCODE_MAX)
44		return ipsecConfigInvalidEventString;
45	return(ipsecConfigEventStrings[eventCode]);
46}
47
48static
49void
50ipsecConfigLogEvent (const char *event_msg, const char *failure_signature)
51{
52	aslmsg m;
53
54	if (!event_msg) {
55		return;
56	}
57
58	m = asl_new(ASL_TYPE_MSG);
59	asl_set(m, ASL_KEY_FACILITY, PLAINIPSECDOMAIN);
60	asl_set(m, ASL_KEY_MSG, ipsecConfigString);
61#if 0   /* <rdar://problem/6468252> is flooding 300000+ events to MessageTracer servers */
62    if (failure_signature) {
63        asl_set(m, "com.apple.message.domain", PLAINIPSECDOMAIN);
64        asl_set(m, "com.apple.message.result", "failure");	// failure
65        asl_set(m, "com.apple.message.signature", failure_signature);
66    }
67    asl_log(NULL, m, ASL_LEVEL_NOTICE, "%s", event_msg);
68#else
69    if (failure_signature) {
70        asl_log(NULL, m, ASL_LEVEL_NOTICE, "%s (failure: %s)", event_msg, failure_signature);
71    } else {
72        asl_log(NULL, m, ASL_LEVEL_NOTICE, "%s", event_msg);
73    }
74#endif
75	asl_free(m);
76}
77
78void
79ipsecConfigTracerEvent (const char *filename, ipsecConfigEventCode_t eventCode, const char *event, const char *failure_reason)
80{
81	char buf[1024];
82
83	if (filename == NULL) {
84		ipsecConfigLogEvent(CONSTSTR("tracer failed. (Invalid filename)."), ipsecConfigTracerFailedString);
85		return;
86	}
87	if (eventCode <= IPSECCONFIGEVENTCODE_NONE || eventCode >= IPSECCONFIGEVENTCODE_MAX) {
88		ipsecConfigLogEvent(CONSTSTR("tracer failed. (Invalid event code)."), ipsecConfigTracerFailedString);
89		return;
90	}
91	if (event == NULL) {
92		ipsecConfigLogEvent(CONSTSTR("tracer failed. (Invalid event)."), ipsecConfigTracerFailedString);
93		return;
94	}
95
96	buf[0] = (char)0;
97	snprintf(buf, sizeof(buf), "%s. (%s, filename %s).", ipsecConfigEventCodeToString(eventCode), failure_reason, filename);
98	ipsecConfigLogEvent(CONSTSTR(buf), event);
99}
100