]> git.saurik.com Git - apple/ipsec.git/blob - ipsec-tools/racoon/plog.c
ipsec-93.13.tar.gz
[apple/ipsec.git] / ipsec-tools / racoon / plog.c
1 /* $Id: plog.c,v 1.6.10.1 2005/12/07 10:19:51 vanhu Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include "config.h"
33
34 #include <sys/types.h>
35 #include <sys/param.h>
36
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <errno.h>
41 #ifdef HAVE_STDARG_H
42 #include <stdarg.h>
43 #else
44 #include <varargs.h>
45 #endif
46 #if TIME_WITH_SYS_TIME
47 # include <sys/time.h>
48 # include <time.h>
49 #else
50 # if HAVE_SYS_TIME_H
51 # include <sys/time.h>
52 # else
53 # include <time.h>
54 # endif
55 #endif
56 #include <ctype.h>
57 #include <err.h>
58
59 #include "var.h"
60 #include "misc.h"
61 #include "plog.h"
62 #include "logger.h"
63 #include "debug.h"
64 #include "gcmalloc.h"
65
66 #ifndef VA_COPY
67 # define VA_COPY(dst,src) memcpy(&(dst), (src), sizeof(va_list))
68 #endif
69
70 extern int print_pid;
71
72 char *pname = NULL;
73 u_int32_t loglevel = LLV_BASE;
74 int f_foreground = 0;
75
76 int print_location = 0;
77
78 static struct log *logp = NULL;
79 static char *logfile = NULL;
80
81 static char *plog_common __P((int, const char *, const char *));
82
83 static struct plogtags {
84 char *name;
85 int priority;
86 } ptab[] = {
87 { "(not defined)", 0, },
88 { "INFO", LOG_INFO, },
89 { "NOTIFY", LOG_INFO, },
90 { "WARNING", LOG_INFO, },
91 { "ERROR", LOG_INFO, },
92 { "ERROR", LOG_ERR, },
93 { "DEBUG", LOG_DEBUG, },
94 { "DEBUG2", LOG_DEBUG, },
95 };
96
97 static char *
98 plog_common(pri, fmt, func)
99 int pri;
100 const char *fmt, *func;
101 {
102 static char buf[800]; /* XXX shoule be allocated every time ? */
103 char *p;
104 int reslen, len;
105
106 p = buf;
107 reslen = sizeof(buf);
108
109 if (logfile || f_foreground) {
110 time_t t;
111 struct tm *tm;
112
113 t = time(0);
114 tm = localtime(&t);
115 len = strftime(p, reslen, "%Y-%m-%d %T: ", tm);
116 p += len;
117 reslen -= len;
118 }
119
120 if (pri < ARRAYLEN(ptab)) {
121 if (print_pid)
122 len = snprintf(p, reslen, "[%d] %s: ", getpid(), ptab[pri].name);
123 else
124 len = snprintf(p, reslen, "%s: ", ptab[pri].name);
125 if (len >= 0 && len < reslen) {
126 p += len;
127 reslen -= len;
128 } else
129 *p = '\0';
130 }
131
132 if (print_location)
133 snprintf(p, reslen, "%s: %s", func, fmt);
134 else
135 snprintf(p, reslen, "%s", fmt);
136 #ifdef BROKEN_PRINTF
137 while ((p = strstr(buf,"%z")) != NULL)
138 p[1] = 'l';
139 #endif
140
141 return buf;
142 }
143
144 void
145 plog(int pri, const char *func, struct sockaddr *sa, const char *fmt, ...)
146 {
147 va_list ap;
148
149 va_start(ap, fmt);
150 plogv(pri, func, sa, fmt, &ap);
151 va_end(ap);
152 }
153
154 void
155 plogv(int pri, const char *func, struct sockaddr *sa,
156 const char *fmt, va_list *ap)
157 {
158 char *newfmt;
159 va_list ap_bak;
160
161 if (pri > loglevel)
162 return;
163
164 newfmt = plog_common(pri, fmt, func);
165
166 VA_COPY(ap_bak, ap);
167
168 if (f_foreground)
169 vprintf(newfmt, *ap);
170
171 if (logfile)
172 log_vaprint(logp, newfmt, ap_bak);
173 else {
174 if (pri < ARRAYLEN(ptab))
175 vsyslog(ptab[pri].priority, newfmt, ap_bak);
176 else
177 vsyslog(LOG_ALERT, newfmt, ap_bak);
178 }
179 }
180
181 void
182 plogdump(pri, data, len)
183 int pri;
184 void *data;
185 size_t len;
186 {
187 caddr_t buf;
188 size_t buflen;
189 int i, j;
190
191 if (pri > loglevel)
192 return;
193
194 /*
195 * 2 words a bytes + 1 space 4 bytes + 1 newline 32 bytes
196 * + 2 newline + '\0'
197 */
198 buflen = (len * 2) + (len / 4) + (len / 32) + 3;
199 buf = racoon_malloc(buflen);
200
201 i = 0;
202 j = 0;
203 while (j < len) {
204 if (j % 32 == 0)
205 buf[i++] = '\n';
206 else
207 if (j % 4 == 0)
208 buf[i++] = ' ';
209 snprintf(&buf[i], buflen - i, "%02x",
210 ((unsigned char *)data)[j] & 0xff);
211 i += 2;
212 j++;
213 }
214 if (buflen - i >= 2) {
215 buf[i++] = '\n';
216 buf[i] = '\0';
217 }
218 plog(pri, LOCATION, NULL, "%s", buf);
219
220 racoon_free(buf);
221 }
222
223 void
224 ploginit()
225 {
226 if (logfile) {
227 logp = log_open(250, logfile);
228 if (logp == NULL)
229 errx(1, "ERROR: failed to open log file %s.", logfile);
230 return;
231 }
232
233 openlog(pname, LOG_NDELAY, LOG_DAEMON);
234 }
235
236 void
237 plogset(file)
238 char *file;
239 {
240 if (logfile != NULL)
241 racoon_free(logfile);
242 logfile = racoon_strdup(file);
243 STRDUP_FATAL(logfile);
244 }
245
246 void
247 plogreset(file)
248 char *file;
249 {
250
251 /* if log paths equal - do nothing */
252 if (logfile == NULL && file == NULL)
253 return;
254 if (logfile != NULL && file != NULL)
255 if (!strcmp(logfile, file))
256 return;
257
258 if (logfile == NULL) /* no logfile was specified - daemon was used */
259 closelog(); /* close it */
260 else {
261 log_close(logp);
262 logp = NULL;
263 racoon_free(logfile);
264 logfile = NULL;
265 }
266
267 if (file)
268 plogset(file);
269 ploginit();
270 }
271
272 /*
273 Returns a printable string from (possibly) binary data ;
274 concatenates all unprintable chars to one space.
275 XXX Maybe the printable chars range is too large...
276 */
277 char*
278 binsanitize(binstr, n)
279 char *binstr;
280 size_t n;
281 {
282 int p,q;
283 char* d;
284 for (p = 0, q = 0; p < n; p++) {
285 if (isgraph((int)binstr[p])) {
286 binstr[q++] = binstr[p];
287 } else {
288 if (q && binstr[q - 1] != ' ')
289 binstr[q++] = ' ';
290 }
291 }
292 binstr[q++] = '\0';
293 return binstr;
294 }
295