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;
93 ctx->state = L_ERROR; /* disable logging */
95 fmode = (mode == 1 ? "ab" : "wb");
96 ctx->lgfp = f_open(ctx->currlogfilename, fmode, FALSE);
100 ctx->state = L_ERROR;
103 if (ctx->state == L_OPEN) {
104 /* Write header line into log file. */
106 strftime(buf, 24, "%Y.%m.%d %H:%M:%S", &tm);
107 logprintf(ctx, "=~=~=~=~=~=~=~=~=~=~=~= PuTTY log %s"
108 " =~=~=~=~=~=~=~=~=~=~=~=\r\n", buf);
111 event = dupprintf("%s session log (%s mode) to file: %s",
112 ctx->state == L_ERROR ?
113 (mode == 0 ? "Disabled writing" : "Error writing") :
114 (mode == 1 ? "Appending" : "Writing new"),
115 (ctx->logtype == LGTYP_ASCII ? "ASCII" :
116 ctx->logtype == LGTYP_DEBUG ? "raw" :
117 ctx->logtype == LGTYP_PACKETS ? "SSH packets" :
118 ctx->logtype == LGTYP_SSHRAW ? "SSH raw data" :
120 filename_to_str(ctx->currlogfilename));
121 logevent(ctx->frontend, event);
125 * Having either succeeded or failed in opening the log file,
126 * we should write any queued data out.
128 assert(ctx->state != L_OPENING); /* make _sure_ it won't be requeued */
129 while (bufchain_size(&ctx->queue)) {
132 bufchain_prefix(&ctx->queue, &data, &len);
133 logwrite(ctx, data, len);
134 bufchain_consume(&ctx->queue, len);
139 * Open the log file. Takes care of detecting an already-existing
140 * file and asking the user whether they want to append, overwrite
143 void logfopen(void *handle)
145 struct LogContext *ctx = (struct LogContext *)handle;
149 /* Prevent repeat calls */
150 if (ctx->state != L_CLOSED)
158 /* substitute special codes in file name */
159 if (ctx->currlogfilename)
160 filename_free(ctx->currlogfilename);
161 ctx->currlogfilename =
162 xlatlognam(conf_get_filename(ctx->conf, CONF_logfilename),
163 conf_get_str(ctx->conf, CONF_host),
164 conf_get_int(ctx->conf, CONF_port), &tm);
166 ctx->lgfp = f_open(ctx->currlogfilename, "r", FALSE); /* file already present? */
168 int logxfovr = conf_get_int(ctx->conf, CONF_logxfovr);
170 if (logxfovr != LGXF_ASK) {
171 mode = ((logxfovr == LGXF_OVR) ? 2 : 1);
173 mode = askappend(ctx->frontend, ctx->currlogfilename,
174 logfopen_callback, ctx);
176 mode = 2; /* create == overwrite */
179 ctx->state = L_OPENING;
181 logfopen_callback(ctx, mode); /* open the file */
184 void logfclose(void *handle)
186 struct LogContext *ctx = (struct LogContext *)handle;
191 ctx->state = L_CLOSED;
195 * Log session traffic.
197 void logtraffic(void *handle, unsigned char c, int logmode)
199 struct LogContext *ctx = (struct LogContext *)handle;
200 if (ctx->logtype > 0) {
201 if (ctx->logtype == logmode)
202 logwrite(ctx, &c, 1);
207 * Log an Event Log entry. Used in SSH packet logging mode; this is
208 * also as convenient a place as any to put the output of Event Log
209 * entries to stderr when a command-line tool is in verbose mode.
210 * (In particular, this is a better place to put it than in the
211 * front ends, because it only has to be done once for all
212 * platforms. Platforms which don't have a meaningful stderr can
213 * just avoid defining FLAG_STDERR.
215 void log_eventlog(void *handle, const char *event)
217 struct LogContext *ctx = (struct LogContext *)handle;
218 if ((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)) {
219 fprintf(stderr, "%s\n", event);
222 /* If we don't have a context yet (eg winnet.c init) then skip entirely */
225 if (ctx->logtype != LGTYP_PACKETS &&
226 ctx->logtype != LGTYP_SSHRAW)
228 logprintf(ctx, "Event Log: %s\r\n", event);
234 * If n_blanks != 0, blank or omit some parts.
235 * Set of blanking areas must be in increasing order.
237 void log_packet(void *handle, int direction, int type,
238 const char *texttype, const void *data, int len,
239 int n_blanks, const struct logblank_t *blanks,
240 const unsigned long *seq,
241 unsigned downstream_id, const char *additional_log_text)
243 struct LogContext *ctx = (struct LogContext *)handle;
244 char dumpdata[80], smalldata[5];
245 int p = 0, b = 0, omitted = 0;
246 int output_pos = 0; /* NZ if pending output in dumpdata */
248 if (!(ctx->logtype == LGTYP_SSHRAW ||
249 (ctx->logtype == LGTYP_PACKETS && texttype)))
254 logprintf(ctx, "%s packet ",
255 direction == PKT_INCOMING ? "Incoming" : "Outgoing");
258 logprintf(ctx, "#0x%lx, ", *seq);
260 logprintf(ctx, "type %d / 0x%02x (%s)", type, type, texttype);
263 logprintf(ctx, " on behalf of downstream #%u", downstream_id);
264 if (additional_log_text)
265 logprintf(ctx, " (%s)", additional_log_text);
268 logprintf(ctx, "\r\n");
271 * Raw data is logged with a timestamp, so that it's possible
272 * to determine whether a mysterious delay occurred at the
273 * client or server end. (Timestamping the raw data avoids
274 * cluttering the normal case of only logging decrypted SSH
275 * messages, and also adds conceptual rigour in the case where
276 * an SSH message arrives in several pieces.)
281 strftime(buf, 24, "%Y-%m-%d %H:%M:%S", &tm);
282 logprintf(ctx, "%s raw data at %s\r\n",
283 direction == PKT_INCOMING ? "Incoming" : "Outgoing",
288 * Output a hex/ASCII dump of the packet body, blanking/omitting
289 * parts as specified.
294 /* Move to a current entry in the blanking array. */
295 while ((b < n_blanks) &&
296 (p >= blanks[b].offset + blanks[b].len))
298 /* Work out what type of blanking to apply to
300 blktype = PKTLOG_EMIT; /* default */
301 if ((b < n_blanks) &&
302 (p >= blanks[b].offset) &&
303 (p < blanks[b].offset + blanks[b].len))
304 blktype = blanks[b].type;
306 /* If we're about to stop omitting, it's time to say how
307 * much we omitted. */
308 if ((blktype != PKTLOG_OMIT) && omitted) {
309 logprintf(ctx, " (%d byte%s omitted)\r\n",
310 omitted, (omitted==1?"":"s"));
314 /* (Re-)initialise dumpdata as necessary
315 * (start of row, or if we've just stopped omitting) */
316 if (!output_pos && !omitted)
317 sprintf(dumpdata, " %08x%*s\r\n", p-(p%16), 1+3*16+2+16, "");
319 /* Deal with the current byte. */
320 if (blktype == PKTLOG_OMIT) {
324 if (blktype == PKTLOG_BLANK) {
326 sprintf(smalldata, "XX");
327 } else { /* PKTLOG_EMIT */
328 c = ((unsigned char *)data)[p];
329 sprintf(smalldata, "%02x", c);
331 dumpdata[10+2+3*(p%16)] = smalldata[0];
332 dumpdata[10+2+3*(p%16)+1] = smalldata[1];
333 dumpdata[10+1+3*16+2+(p%16)] = (isprint(c) ? c : '.');
334 output_pos = (p%16) + 1;
339 /* Flush row if necessary */
340 if (((p % 16) == 0) || (p == len) || omitted) {
342 strcpy(dumpdata + 10+1+3*16+2+output_pos, "\r\n");
343 logwrite(ctx, dumpdata, strlen(dumpdata));
352 logprintf(ctx, " (%d byte%s omitted)\r\n",
353 omitted, (omitted==1?"":"s"));
357 void *log_init(void *frontend, Conf *conf)
359 struct LogContext *ctx = snew(struct LogContext);
361 ctx->state = L_CLOSED;
362 ctx->frontend = frontend;
363 ctx->conf = conf_copy(conf);
364 ctx->logtype = conf_get_int(ctx->conf, CONF_logtype);
365 ctx->currlogfilename = NULL;
366 bufchain_init(&ctx->queue);
370 void log_free(void *handle)
372 struct LogContext *ctx = (struct LogContext *)handle;
375 bufchain_clear(&ctx->queue);
376 if (ctx->currlogfilename)
377 filename_free(ctx->currlogfilename);
378 conf_free(ctx->conf);
382 void log_reconfig(void *handle, Conf *conf)
384 struct LogContext *ctx = (struct LogContext *)handle;
387 if (!filename_equal(conf_get_filename(ctx->conf, CONF_logfilename),
388 conf_get_filename(conf, CONF_logfilename)) ||
389 conf_get_int(ctx->conf, CONF_logtype) !=
390 conf_get_int(conf, CONF_logtype))
391 reset_logging = TRUE;
393 reset_logging = FALSE;
398 conf_free(ctx->conf);
399 ctx->conf = conf_copy(conf);
401 ctx->logtype = conf_get_int(ctx->conf, CONF_logtype);
408 * translate format codes into time/date strings
409 * and insert them into log file name
411 * "&Y":YYYY "&m":MM "&d":DD "&T":hhmmss "&h":<hostname> "&&":&
413 static Filename *xlatlognam(Filename *src, char *hostname, int port,
423 bufsize = FILENAME_MAX;
424 buffer = snewn(bufsize, char);
426 s = filename_to_str(src);
429 /* Let (bufp, len) be the string to append. */
430 bufp = buf; /* don't usually override this */
435 if (*s) switch (c = *s++, tolower((unsigned char)c)) {
437 size = strftime(buf, sizeof(buf), "%Y", tm);
440 size = strftime(buf, sizeof(buf), "%m", tm);
443 size = strftime(buf, sizeof(buf), "%d", tm);
446 size = strftime(buf, sizeof(buf), "%H%M%S", tm);
453 size = sprintf(buf, "%d", port);
465 if (bufsize <= buflen + size) {
466 bufsize = (buflen + size) * 5 / 4 + 512;
467 buffer = sresize(buffer, bufsize, char);
469 memcpy(buffer + buflen, bufp, size);
472 buffer[buflen] = '\0';
474 ret = filename_from_str(buffer);