14 /* log session to file stuff ... */
17 enum { L_CLOSED, L_OPENING, L_OPEN, L_ERROR } state;
19 Filename *currlogfilename;
22 int logtype; /* cached out of conf */
25 static Filename *xlatlognam(Filename *s, char *hostname, int port,
29 * Internal wrapper function which must be called for _all_ output
30 * to the log file. It takes care of opening the log file if it
31 * isn't open, buffering data if it's in the process of being
32 * opened asynchronously, etc.
34 static void logwrite(struct LogContext *ctx, void *data, int len)
37 * In state L_CLOSED, we call logfopen, which will set the state
38 * to one of L_OPENING, L_OPEN or L_ERROR. Hence we process all of
39 * those three _after_ processing L_CLOSED.
41 if (ctx->state == L_CLOSED)
44 if (ctx->state == L_OPENING) {
45 bufchain_add(&ctx->queue, data, len);
46 } else if (ctx->state == L_OPEN) {
48 if (fwrite(data, 1, len, ctx->lgfp) < (size_t)len) {
51 /* Log state is L_ERROR so this won't cause a loop */
52 logevent(ctx->frontend,
53 "Disabled writing session log due to error while writing");
55 } /* else L_ERROR, so ignore the write */
59 * Convenience wrapper on logwrite() which printf-formats the
62 static void logprintf(struct LogContext *ctx, const char *fmt, ...)
68 data = dupvprintf(fmt, ap);
71 logwrite(ctx, data, strlen(data));
76 * Flush any open log file.
78 void logflush(void *handle) {
79 struct LogContext *ctx = (struct LogContext *)handle;
81 if (ctx->state == L_OPEN)
85 static void logfopen_callback(void *handle, int mode)
87 struct LogContext *ctx = (struct LogContext *)handle;
88 char buf[256], *event;
94 ctx->state = L_ERROR; /* disable logging */
96 fmode = (mode == 1 ? "ab" : "wb");
97 ctx->lgfp = f_open(ctx->currlogfilename, fmode, FALSE);
101 ctx->state = L_ERROR;
106 if (ctx->state == L_OPEN) {
107 /* Write header line into log file. */
109 strftime(buf, 24, "%Y.%m.%d %H:%M:%S", &tm);
110 logprintf(ctx, "=~=~=~=~=~=~=~=~=~=~=~= PuTTY log %s"
111 " =~=~=~=~=~=~=~=~=~=~=~=\r\n", buf);
114 event = dupprintf("%s session log (%s mode) to file: %s",
115 ctx->state == L_ERROR ?
116 (mode == 0 ? "Disabled writing" : "Error writing") :
117 (mode == 1 ? "Appending" : "Writing new"),
118 (ctx->logtype == LGTYP_ASCII ? "ASCII" :
119 ctx->logtype == LGTYP_DEBUG ? "raw" :
120 ctx->logtype == LGTYP_PACKETS ? "SSH packets" :
121 ctx->logtype == LGTYP_SSHRAW ? "SSH raw data" :
123 filename_to_str(ctx->currlogfilename));
124 logevent(ctx->frontend, event);
127 * If we failed to open the log file due to filesystem error
128 * (as opposed to user action such as clicking Cancel in the
129 * askappend box), we should log it more prominently. We do
130 * this by sending it to the same place that stderr output
131 * from the main session goes (so, either a console tool's
132 * actual stderr, or a terminal window).
134 * Of course this is one case in which that policy won't cause
135 * it to turn up embarrassingly in a log file of real server
136 * output, because the whole point is that we haven't managed
137 * to open any such log file :-)
139 from_backend(ctx->frontend, 1, event, strlen(event));
140 from_backend(ctx->frontend, 1, "\r\n", 2);
145 * Having either succeeded or failed in opening the log file,
146 * we should write any queued data out.
148 assert(ctx->state != L_OPENING); /* make _sure_ it won't be requeued */
149 while (bufchain_size(&ctx->queue)) {
152 bufchain_prefix(&ctx->queue, &data, &len);
153 logwrite(ctx, data, len);
154 bufchain_consume(&ctx->queue, len);
159 * Open the log file. Takes care of detecting an already-existing
160 * file and asking the user whether they want to append, overwrite
163 void logfopen(void *handle)
165 struct LogContext *ctx = (struct LogContext *)handle;
170 /* Prevent repeat calls */
171 if (ctx->state != L_CLOSED)
179 /* substitute special codes in file name */
180 if (ctx->currlogfilename)
181 filename_free(ctx->currlogfilename);
182 ctx->currlogfilename =
183 xlatlognam(conf_get_filename(ctx->conf, CONF_logfilename),
184 conf_get_str(ctx->conf, CONF_host),
185 conf_get_int(ctx->conf, CONF_port), &tm);
187 fp = f_open(ctx->currlogfilename, "r", FALSE); /* file already present? */
189 int logxfovr = conf_get_int(ctx->conf, CONF_logxfovr);
191 if (logxfovr != LGXF_ASK) {
192 mode = ((logxfovr == LGXF_OVR) ? 2 : 1);
194 mode = askappend(ctx->frontend, ctx->currlogfilename,
195 logfopen_callback, ctx);
197 mode = 2; /* create == overwrite */
200 ctx->state = L_OPENING;
202 logfopen_callback(ctx, mode); /* open the file */
205 void logfclose(void *handle)
207 struct LogContext *ctx = (struct LogContext *)handle;
212 ctx->state = L_CLOSED;
216 * Log session traffic.
218 void logtraffic(void *handle, unsigned char c, int logmode)
220 struct LogContext *ctx = (struct LogContext *)handle;
221 if (ctx->logtype > 0) {
222 if (ctx->logtype == logmode)
223 logwrite(ctx, &c, 1);
228 * Log an Event Log entry. Used in SSH packet logging mode; this is
229 * also as convenient a place as any to put the output of Event Log
230 * entries to stderr when a command-line tool is in verbose mode.
231 * (In particular, this is a better place to put it than in the
232 * front ends, because it only has to be done once for all
233 * platforms. Platforms which don't have a meaningful stderr can
234 * just avoid defining FLAG_STDERR.
236 void log_eventlog(void *handle, const char *event)
238 struct LogContext *ctx = (struct LogContext *)handle;
239 if ((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)) {
240 fprintf(stderr, "%s\n", event);
243 /* If we don't have a context yet (eg winnet.c init) then skip entirely */
246 if (ctx->logtype != LGTYP_PACKETS &&
247 ctx->logtype != LGTYP_SSHRAW)
249 logprintf(ctx, "Event Log: %s\r\n", event);
255 * If n_blanks != 0, blank or omit some parts.
256 * Set of blanking areas must be in increasing order.
258 void log_packet(void *handle, int direction, int type,
259 char *texttype, const void *data, int len,
260 int n_blanks, const struct logblank_t *blanks,
261 const unsigned long *seq,
262 unsigned downstream_id, const char *additional_log_text)
264 struct LogContext *ctx = (struct LogContext *)handle;
265 char dumpdata[80], smalldata[5];
266 int p = 0, b = 0, omitted = 0;
267 int output_pos = 0; /* NZ if pending output in dumpdata */
269 if (!(ctx->logtype == LGTYP_SSHRAW ||
270 (ctx->logtype == LGTYP_PACKETS && texttype)))
275 logprintf(ctx, "%s packet ",
276 direction == PKT_INCOMING ? "Incoming" : "Outgoing");
279 logprintf(ctx, "#0x%lx, ", *seq);
281 logprintf(ctx, "type %d / 0x%02x (%s)", type, type, texttype);
284 logprintf(ctx, " on behalf of downstream #%u", downstream_id);
285 if (additional_log_text)
286 logprintf(ctx, " (%s)", additional_log_text);
289 logprintf(ctx, "\r\n");
292 * Raw data is logged with a timestamp, so that it's possible
293 * to determine whether a mysterious delay occurred at the
294 * client or server end. (Timestamping the raw data avoids
295 * cluttering the normal case of only logging decrypted SSH
296 * messages, and also adds conceptual rigour in the case where
297 * an SSH message arrives in several pieces.)
302 strftime(buf, 24, "%Y-%m-%d %H:%M:%S", &tm);
303 logprintf(ctx, "%s raw data at %s\r\n",
304 direction == PKT_INCOMING ? "Incoming" : "Outgoing",
309 * Output a hex/ASCII dump of the packet body, blanking/omitting
310 * parts as specified.
315 /* Move to a current entry in the blanking array. */
316 while ((b < n_blanks) &&
317 (p >= blanks[b].offset + blanks[b].len))
319 /* Work out what type of blanking to apply to
321 blktype = PKTLOG_EMIT; /* default */
322 if ((b < n_blanks) &&
323 (p >= blanks[b].offset) &&
324 (p < blanks[b].offset + blanks[b].len))
325 blktype = blanks[b].type;
327 /* If we're about to stop omitting, it's time to say how
328 * much we omitted. */
329 if ((blktype != PKTLOG_OMIT) && omitted) {
330 logprintf(ctx, " (%d byte%s omitted)\r\n",
331 omitted, (omitted==1?"":"s"));
335 /* (Re-)initialise dumpdata as necessary
336 * (start of row, or if we've just stopped omitting) */
337 if (!output_pos && !omitted)
338 sprintf(dumpdata, " %08x%*s\r\n", p-(p%16), 1+3*16+2+16, "");
340 /* Deal with the current byte. */
341 if (blktype == PKTLOG_OMIT) {
345 if (blktype == PKTLOG_BLANK) {
347 sprintf(smalldata, "XX");
348 } else { /* PKTLOG_EMIT */
349 c = ((unsigned char *)data)[p];
350 sprintf(smalldata, "%02x", c);
352 dumpdata[10+2+3*(p%16)] = smalldata[0];
353 dumpdata[10+2+3*(p%16)+1] = smalldata[1];
354 dumpdata[10+1+3*16+2+(p%16)] = (isprint(c) ? c : '.');
355 output_pos = (p%16) + 1;
360 /* Flush row if necessary */
361 if (((p % 16) == 0) || (p == len) || omitted) {
363 strcpy(dumpdata + 10+1+3*16+2+output_pos, "\r\n");
364 logwrite(ctx, dumpdata, strlen(dumpdata));
373 logprintf(ctx, " (%d byte%s omitted)\r\n",
374 omitted, (omitted==1?"":"s"));
378 void *log_init(void *frontend, Conf *conf)
380 struct LogContext *ctx = snew(struct LogContext);
382 ctx->state = L_CLOSED;
383 ctx->frontend = frontend;
384 ctx->conf = conf_copy(conf);
385 ctx->logtype = conf_get_int(ctx->conf, CONF_logtype);
386 ctx->currlogfilename = NULL;
387 bufchain_init(&ctx->queue);
391 void log_free(void *handle)
393 struct LogContext *ctx = (struct LogContext *)handle;
396 bufchain_clear(&ctx->queue);
397 if (ctx->currlogfilename)
398 filename_free(ctx->currlogfilename);
399 conf_free(ctx->conf);
403 void log_reconfig(void *handle, Conf *conf)
405 struct LogContext *ctx = (struct LogContext *)handle;
408 if (!filename_equal(conf_get_filename(ctx->conf, CONF_logfilename),
409 conf_get_filename(conf, CONF_logfilename)) ||
410 conf_get_int(ctx->conf, CONF_logtype) !=
411 conf_get_int(conf, CONF_logtype))
412 reset_logging = TRUE;
414 reset_logging = FALSE;
419 conf_free(ctx->conf);
420 ctx->conf = conf_copy(conf);
422 ctx->logtype = conf_get_int(ctx->conf, CONF_logtype);
429 * translate format codes into time/date strings
430 * and insert them into log file name
432 * "&Y":YYYY "&m":MM "&d":DD "&T":hhmmss "&h":<hostname> "&&":&
434 static Filename *xlatlognam(Filename *src, char *hostname, int port,
444 bufsize = FILENAME_MAX;
445 buffer = snewn(bufsize, char);
447 s = filename_to_str(src);
450 int sanitise = FALSE;
451 /* Let (bufp, len) be the string to append. */
452 bufp = buf; /* don't usually override this */
457 if (*s) switch (c = *s++, tolower((unsigned char)c)) {
459 size = strftime(buf, sizeof(buf), "%Y", tm);
462 size = strftime(buf, sizeof(buf), "%m", tm);
465 size = strftime(buf, sizeof(buf), "%d", tm);
468 size = strftime(buf, sizeof(buf), "%H%M%S", tm);
475 size = sprintf(buf, "%d", port);
483 /* Never allow path separators - or any other illegal
484 * filename character - to come out of any of these
485 * auto-format directives. E.g. 'hostname' can contain
486 * colons, if it's an IPv6 address, and colons aren't
487 * legal in filenames on Windows. */
493 if (bufsize <= buflen + size) {
494 bufsize = (buflen + size) * 5 / 4 + 512;
495 buffer = sresize(buffer, bufsize, char);
500 c = filename_char_sanitise(c);
501 buffer[buflen++] = c;
504 buffer[buflen] = '\0';
506 ret = filename_from_str(buffer);