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 "ipsecPolicyTracer.h"
28#include "ipsecMessageTracer.h"
29
30const char *ipsecConfigTracerFailedString = "Tracer Failed";
31const char *ipsecPolicyInvalidEventString = "Invalid Event";
32const char *ipsecPolicyString			  = "IPSEC";
33
34const char * const ipsecPolicyEventStrings[IPSECPOLICYEVENTCODE_MAX] =	{	CONSTSTR("NONE") /* index place holder */,
35																			CONSTSTR("setkey Error"),
36																		};
37
38const char *
39ipsecPolicyEventCodeToString (ipsecPolicyEventCode_t eventCode)
40{
41	if (eventCode <= IPSECPOLICYEVENTCODE_NONE || eventCode >= IPSECPOLICYEVENTCODE_MAX)
42		return ipsecPolicyInvalidEventString;
43	return(ipsecPolicyEventStrings[eventCode]);
44}
45
46static
47void
48ipsecPolicyLogEvent (const char *event_msg, const char *failure_signature)
49{
50	aslmsg m;
51
52	if (!event_msg) {
53		return;
54	}
55
56	m = asl_new(ASL_TYPE_MSG);
57	asl_set(m, ASL_KEY_FACILITY, PLAINIPSECDOMAIN);
58	asl_set(m, ASL_KEY_MSG, ipsecPolicyString);
59#if 0 /* we don't want to send filenames to MessageTracer server */
60    if (failure_signature) {
61        asl_set(m, "com.apple.message.domain", PLAINIPSECDOMAIN);
62        asl_set(m, "com.apple.message.result", "failure");	// failure
63        asl_set(m, "com.apple.message.signature", failure_signature);
64    }
65    asl_log(NULL, m, ASL_LEVEL_NOTICE, "%s", event_msg);
66#else
67    if (failure_signature) {
68        asl_log(NULL, m, ASL_LEVEL_NOTICE, "%s (failure: %s)", event_msg, failure_signature);
69    } else {
70        asl_log(NULL, m, ASL_LEVEL_NOTICE, "%s", event_msg);
71    }
72#endif
73	asl_free(m);
74}
75
76void
77ipsecPolicyTracerEvent (const char *filename, ipsecPolicyEventCode_t eventCode, const char *event, const char *failure_reason)
78{
79	char buf[1024];
80
81	if (filename == NULL) {
82		ipsecPolicyLogEvent(CONSTSTR("tracer failed. (Invalid filename)."), ipsecConfigTracerFailedString);
83		return;
84	}
85	if (eventCode <= IPSECPOLICYEVENTCODE_NONE || eventCode >= IPSECPOLICYEVENTCODE_MAX) {
86		ipsecPolicyLogEvent(CONSTSTR("tracer failed. (Invalid event code)."), ipsecConfigTracerFailedString);
87		return;
88	}
89	if (event == NULL) {
90		ipsecPolicyLogEvent(CONSTSTR("tracer failed. (Invalid event)."), ipsecConfigTracerFailedString);
91		return;
92	}
93
94	buf[0] = (char)0;
95	snprintf(buf, sizeof(buf), "%s. (%s, filename %s).", ipsecPolicyEventCodeToString(eventCode), failure_reason, filename);
96	ipsecPolicyLogEvent(CONSTSTR(buf), event);
97}
98