Remove outdated comment.
[u/mdw/putty] / logging.c
CommitLineData
eaf1e20a 1/*
2 * Session logging.
3 */
4
00db133f 5#include <stdio.h>
6#include <stdlib.h>
7#include <ctype.h>
8
9#include <time.h>
10#include <assert.h>
11
12#include "putty.h"
13
14/* log session to file stuff ... */
a8327734 15struct LogContext {
16 FILE *lgfp;
8b029099 17 enum { L_CLOSED, L_OPENING, L_OPEN, L_ERROR } state;
919baedb 18 bufchain queue;
9a30e26b 19 Filename currlogfilename;
a8327734 20 void *frontend;
c229ef97 21 Config cfg;
a8327734 22};
00db133f 23
9a30e26b 24static void xlatlognam(Filename *d, Filename s, char *hostname, struct tm *tm);
00db133f 25
26/*
919baedb 27 * Internal wrapper function which must be called for _all_ output
28 * to the log file. It takes care of opening the log file if it
29 * isn't open, buffering data if it's in the process of being
30 * opened asynchronously, etc.
00db133f 31 */
919baedb 32static void logwrite(struct LogContext *ctx, void *data, int len)
00db133f 33{
919baedb 34 /*
8b029099 35 * In state L_CLOSED, we call logfopen, which will set the state
36 * to one of L_OPENING, L_OPEN or L_ERROR. Hence we process all of
37 * those three _after_ processing L_CLOSED.
919baedb 38 */
8b029099 39 if (ctx->state == L_CLOSED)
919baedb 40 logfopen(ctx);
41
8b029099 42 if (ctx->state == L_OPENING) {
919baedb 43 bufchain_add(&ctx->queue, data, len);
8b029099 44 } else if (ctx->state == L_OPEN) {
919baedb 45 assert(ctx->lgfp);
ecb25722 46 if (fwrite(data, 1, len, ctx->lgfp) < len) {
47 logfclose(ctx);
48 ctx->state = L_ERROR;
49 }
8b029099 50 } /* else L_ERROR, so ignore the write */
919baedb 51}
52
53/*
54 * Convenience wrapper on logwrite() which printf-formats the
55 * string.
56 */
57static void logprintf(struct LogContext *ctx, const char *fmt, ...)
58{
59 va_list ap;
60 char *data;
61
62 va_start(ap, fmt);
63 data = dupvprintf(fmt, ap);
64 va_end(ap);
65
66 logwrite(ctx, data, strlen(data));
67 sfree(data);
00db133f 68}
69
70/*
11cc5e30 71 * Flush any open log file.
72 */
73void logflush(void *handle) {
74 struct LogContext *ctx = (struct LogContext *)handle;
75 if (ctx->cfg.logtype > 0)
8b029099 76 if (ctx->state == L_OPEN)
11cc5e30 77 fflush(ctx->lgfp);
78}
79
919baedb 80static void logfopen_callback(void *handle, int mode)
81{
82 struct LogContext *ctx = (struct LogContext *)handle;
83 char buf[256], *event;
84 struct tm tm;
85 const char *fmode;
86
87 if (mode == 0) {
8b029099 88 ctx->state = L_ERROR; /* disable logging */
919baedb 89 } else {
9cd439a3 90 fmode = (mode == 1 ? "ab" : "wb");
b078de41 91 ctx->lgfp = f_open(ctx->currlogfilename, fmode, FALSE);
919baedb 92 if (ctx->lgfp)
8b029099 93 ctx->state = L_OPEN;
919baedb 94 else
8b029099 95 ctx->state = L_ERROR;
919baedb 96 }
97
8b029099 98 if (ctx->state == L_OPEN) {
919baedb 99 /* Write header line into log file. */
100 tm = ltime();
101 strftime(buf, 24, "%Y.%m.%d %H:%M:%S", &tm);
102 logprintf(ctx, "=~=~=~=~=~=~=~=~=~=~=~= PuTTY log %s"
103 " =~=~=~=~=~=~=~=~=~=~=~=\r\n", buf);
104 }
105
106 event = dupprintf("%s session log (%s mode) to file: %s",
107 (mode == 0 ? "Disabled writing" :
108 mode == 1 ? "Appending" : "Writing new"),
109 (ctx->cfg.logtype == LGTYP_ASCII ? "ASCII" :
110 ctx->cfg.logtype == LGTYP_DEBUG ? "raw" :
111 ctx->cfg.logtype == LGTYP_PACKETS ? "SSH packets" :
bf8a49a1 112 ctx->cfg.logtype == LGTYP_SSHRAW ? "SSH raw data" :
919baedb 113 "unknown"),
114 filename_to_str(&ctx->currlogfilename));
115 logevent(ctx->frontend, event);
116 sfree(event);
117
118 /*
119 * Having either succeeded or failed in opening the log file,
120 * we should write any queued data out.
121 */
8b029099 122 assert(ctx->state != L_OPENING); /* make _sure_ it won't be requeued */
919baedb 123 while (bufchain_size(&ctx->queue)) {
124 void *data;
125 int len;
126 bufchain_prefix(&ctx->queue, &data, &len);
127 logwrite(ctx, data, len);
128 bufchain_consume(&ctx->queue, len);
129 }
130}
131
132/*
133 * Open the log file. Takes care of detecting an already-existing
134 * file and asking the user whether they want to append, overwrite
135 * or cancel logging.
136 */
137void logfopen(void *handle)
138{
139 struct LogContext *ctx = (struct LogContext *)handle;
140 struct tm tm;
141 int mode;
142
143 /* Prevent repeat calls */
8b029099 144 if (ctx->state != L_CLOSED)
919baedb 145 return;
146
147 if (!ctx->cfg.logtype)
148 return;
149
150 tm = ltime();
151
152 /* substitute special codes in file name */
153 xlatlognam(&ctx->currlogfilename, ctx->cfg.logfilename,ctx->cfg.host, &tm);
154
c6940f12 155 ctx->lgfp = f_open(ctx->currlogfilename, "r", FALSE); /* file already present? */
919baedb 156 if (ctx->lgfp) {
157 fclose(ctx->lgfp);
158 if (ctx->cfg.logxfovr != LGXF_ASK) {
159 mode = ((ctx->cfg.logxfovr == LGXF_OVR) ? 2 : 1);
160 } else
161 mode = askappend(ctx->frontend, ctx->currlogfilename,
162 logfopen_callback, ctx);
163 } else
164 mode = 2; /* create == overwrite */
165
166 if (mode < 0)
8b029099 167 ctx->state = L_OPENING;
919baedb 168 else
169 logfopen_callback(ctx, mode); /* open the file */
170}
171
172void logfclose(void *handle)
173{
174 struct LogContext *ctx = (struct LogContext *)handle;
175 if (ctx->lgfp) {
176 fclose(ctx->lgfp);
177 ctx->lgfp = NULL;
178 }
8b029099 179 ctx->state = L_CLOSED;
919baedb 180}
181
182/*
183 * Log session traffic.
184 */
185void logtraffic(void *handle, unsigned char c, int logmode)
186{
187 struct LogContext *ctx = (struct LogContext *)handle;
188 if (ctx->cfg.logtype > 0) {
189 if (ctx->cfg.logtype == logmode)
190 logwrite(ctx, &c, 1);
191 }
192}
193
11cc5e30 194/*
382908ad 195 * Log an Event Log entry. Used in SSH packet logging mode; this is
196 * also as convenient a place as any to put the output of Event Log
197 * entries to stderr when a command-line tool is in verbose mode.
198 * (In particular, this is a better place to put it than in the
199 * front ends, because it only has to be done once for all
200 * platforms. Platforms which don't have a meaningful stderr can
201 * just avoid defining FLAG_STDERR.
fb89f7ff 202 */
cbe2d68f 203void log_eventlog(void *handle, const char *event)
fb89f7ff 204{
a8327734 205 struct LogContext *ctx = (struct LogContext *)handle;
382908ad 206 if ((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)) {
207 fprintf(stderr, "%s\n", event);
208 fflush(stderr);
209 }
c9ef99df 210 /* If we don't have a context yet (eg winnet.c init) then skip entirely */
211 if (!ctx)
212 return;
bf8a49a1 213 if (ctx->cfg.logtype != LGTYP_PACKETS &&
214 ctx->cfg.logtype != LGTYP_SSHRAW)
fb89f7ff 215 return;
919baedb 216 logprintf(ctx, "Event Log: %s\r\n", event);
bf8a49a1 217 logflush(ctx);
fb89f7ff 218}
219
220/*
00db133f 221 * Log an SSH packet.
9a10ecf4 222 * If n_blanks != 0, blank or omit some parts.
223 * Set of blanking areas must be in increasing order.
00db133f 224 */
a8327734 225void log_packet(void *handle, int direction, int type,
0d13bfcd 226 char *texttype, const void *data, int len,
227 int n_blanks, const struct logblank_t *blanks,
228 const unsigned long *seq)
00db133f 229{
a8327734 230 struct LogContext *ctx = (struct LogContext *)handle;
00db133f 231 char dumpdata[80], smalldata[5];
919baedb 232 int p = 0, b = 0, omitted = 0;
233 int output_pos = 0; /* NZ if pending output in dumpdata */
00db133f 234
bf8a49a1 235 if (!(ctx->cfg.logtype == LGTYP_SSHRAW ||
236 (ctx->cfg.logtype == LGTYP_PACKETS && texttype)))
00db133f 237 return;
9a10ecf4 238
919baedb 239 /* Packet header. */
0d13bfcd 240 if (texttype) {
241 if (seq) {
242 logprintf(ctx, "%s packet #0x%lx, type %d / 0x%02x (%s)\r\n",
243 direction == PKT_INCOMING ? "Incoming" : "Outgoing",
244 *seq, type, type, texttype);
245 } else {
246 logprintf(ctx, "%s packet type %d / 0x%02x (%s)\r\n",
247 direction == PKT_INCOMING ? "Incoming" : "Outgoing",
248 type, type, texttype);
249 }
250 } else {
bf8a49a1 251 logprintf(ctx, "%s raw data\r\n",
252 direction == PKT_INCOMING ? "Incoming" : "Outgoing");
0d13bfcd 253 }
919baedb 254
255 /*
256 * Output a hex/ASCII dump of the packet body, blanking/omitting
257 * parts as specified.
258 */
259 while (p < len) {
260 int blktype;
261
262 /* Move to a current entry in the blanking array. */
263 while ((b < n_blanks) &&
264 (p >= blanks[b].offset + blanks[b].len))
265 b++;
266 /* Work out what type of blanking to apply to
267 * this byte. */
268 blktype = PKTLOG_EMIT; /* default */
269 if ((b < n_blanks) &&
270 (p >= blanks[b].offset) &&
271 (p < blanks[b].offset + blanks[b].len))
272 blktype = blanks[b].type;
273
274 /* If we're about to stop omitting, it's time to say how
275 * much we omitted. */
276 if ((blktype != PKTLOG_OMIT) && omitted) {
277 logprintf(ctx, " (%d byte%s omitted)\r\n",
278 omitted, (omitted==1?"":"s"));
279 omitted = 0;
280 }
9a10ecf4 281
919baedb 282 /* (Re-)initialise dumpdata as necessary
283 * (start of row, or if we've just stopped omitting) */
284 if (!output_pos && !omitted)
285 sprintf(dumpdata, " %08x%*s\r\n", p-(p%16), 1+3*16+2+16, "");
286
287 /* Deal with the current byte. */
288 if (blktype == PKTLOG_OMIT) {
289 omitted++;
290 } else {
291 int c;
292 if (blktype == PKTLOG_BLANK) {
293 c = 'X';
294 sprintf(smalldata, "XX");
295 } else { /* PKTLOG_EMIT */
296 c = ((unsigned char *)data)[p];
297 sprintf(smalldata, "%02x", c);
00db133f 298 }
919baedb 299 dumpdata[10+2+3*(p%16)] = smalldata[0];
300 dumpdata[10+2+3*(p%16)+1] = smalldata[1];
301 dumpdata[10+1+3*16+2+(p%16)] = (isprint(c) ? c : '.');
302 output_pos = (p%16) + 1;
00db133f 303 }
9a10ecf4 304
919baedb 305 p++;
00db133f 306
919baedb 307 /* Flush row if necessary */
308 if (((p % 16) == 0) || (p == len) || omitted) {
309 if (output_pos) {
310 strcpy(dumpdata + 10+1+3*16+2+output_pos, "\r\n");
311 logwrite(ctx, dumpdata, strlen(dumpdata));
312 output_pos = 0;
313 }
00db133f 314 }
00db133f 315
00db133f 316 }
00db133f 317
919baedb 318 /* Tidy up */
319 if (omitted)
320 logprintf(ctx, " (%d byte%s omitted)\r\n",
321 omitted, (omitted==1?"":"s"));
322 logflush(ctx);
00db133f 323}
324
c229ef97 325void *log_init(void *frontend, Config *cfg)
a8327734 326{
3d88e64d 327 struct LogContext *ctx = snew(struct LogContext);
a8327734 328 ctx->lgfp = NULL;
8b029099 329 ctx->state = L_CLOSED;
a8327734 330 ctx->frontend = frontend;
c229ef97 331 ctx->cfg = *cfg; /* STRUCTURE COPY */
919baedb 332 bufchain_init(&ctx->queue);
a8327734 333 return ctx;
334}
335
fabd1805 336void log_free(void *handle)
337{
338 struct LogContext *ctx = (struct LogContext *)handle;
339
340 logfclose(ctx);
919baedb 341 bufchain_clear(&ctx->queue);
fabd1805 342 sfree(ctx);
343}
344
c229ef97 345void log_reconfig(void *handle, Config *cfg)
346{
347 struct LogContext *ctx = (struct LogContext *)handle;
348 int reset_logging;
349
9a30e26b 350 if (!filename_equal(ctx->cfg.logfilename, cfg->logfilename) ||
c229ef97 351 ctx->cfg.logtype != cfg->logtype)
352 reset_logging = TRUE;
353 else
354 reset_logging = FALSE;
355
356 if (reset_logging)
4512d432 357 logfclose(ctx);
c229ef97 358
359 ctx->cfg = *cfg; /* STRUCTURE COPY */
360
361 if (reset_logging)
4512d432 362 logfopen(ctx);
c229ef97 363}
364
00db133f 365/*
366 * translate format codes into time/date strings
367 * and insert them into log file name
368 *
6e8f9cab 369 * "&Y":YYYY "&m":MM "&d":DD "&T":hhmmss "&h":<hostname> "&&":&
00db133f 370 */
9a30e26b 371static void xlatlognam(Filename *dest, Filename src,
372 char *hostname, struct tm *tm) {
00db133f 373 char buf[10], *bufp;
374 int size;
9a30e26b 375 char buffer[FILENAME_MAX];
376 int len = sizeof(buffer)-1;
9fab77dc 377 char *d;
378 const char *s;
9a30e26b 379
380 d = buffer;
9fab77dc 381 s = filename_to_str(&src);
00db133f 382
383 while (*s) {
384 /* Let (bufp, len) be the string to append. */
385 bufp = buf; /* don't usually override this */
386 if (*s == '&') {
387 char c;
388 s++;
1709795f 389 size = 0;
d9e0300f 390 if (*s) switch (c = *s++, tolower((unsigned char)c)) {
00db133f 391 case 'y':
392 size = strftime(buf, sizeof(buf), "%Y", tm);
393 break;
394 case 'm':
395 size = strftime(buf, sizeof(buf), "%m", tm);
396 break;
397 case 'd':
398 size = strftime(buf, sizeof(buf), "%d", tm);
399 break;
400 case 't':
401 size = strftime(buf, sizeof(buf), "%H%M%S", tm);
402 break;
403 case 'h':
404 bufp = hostname;
405 size = strlen(bufp);
406 break;
407 default:
408 buf[0] = '&';
409 size = 1;
410 if (c != '&')
411 buf[size++] = c;
412 }
413 } else {
414 buf[0] = *s++;
415 size = 1;
416 }
417 if (size > len)
418 size = len;
419 memcpy(d, bufp, size);
420 d += size;
421 len -= size;
422 }
423 *d = '\0';
9a30e26b 424
0ac2edb3 425 *dest = filename_from_str(buffer);
00db133f 426}