14 /* log session to file stuff ... */
\r
17 enum { L_CLOSED, L_OPENING, L_OPEN, L_ERROR } state;
\r
19 Filename currlogfilename;
\r
24 static void xlatlognam(Filename *d, Filename s, char *hostname, struct tm *tm);
\r
27 * Internal wrapper function which must be called for _all_ output
\r
28 * to the log file. It takes care of opening the log file if it
\r
29 * isn't open, buffering data if it's in the process of being
\r
30 * opened asynchronously, etc.
\r
32 static void logwrite(struct LogContext *ctx, void *data, int len)
\r
35 * In state L_CLOSED, we call logfopen, which will set the state
\r
36 * to one of L_OPENING, L_OPEN or L_ERROR. Hence we process all of
\r
37 * those three _after_ processing L_CLOSED.
\r
39 if (ctx->state == L_CLOSED)
\r
42 if (ctx->state == L_OPENING) {
\r
43 bufchain_add(&ctx->queue, data, len);
\r
44 } else if (ctx->state == L_OPEN) {
\r
46 if (fwrite(data, 1, len, ctx->lgfp) < (size_t)len) {
\r
48 ctx->state = L_ERROR;
\r
49 /* Log state is L_ERROR so this won't cause a loop */
\r
50 logevent(ctx->frontend,
\r
51 "Disabled writing session log due to error while writing");
\r
53 } /* else L_ERROR, so ignore the write */
\r
57 * Convenience wrapper on logwrite() which printf-formats the
\r
60 static void logprintf(struct LogContext *ctx, const char *fmt, ...)
\r
66 data = dupvprintf(fmt, ap);
\r
69 logwrite(ctx, data, strlen(data));
\r
74 * Flush any open log file.
\r
76 void logflush(void *handle) {
\r
77 struct LogContext *ctx = (struct LogContext *)handle;
\r
78 if (ctx->cfg.logtype > 0)
\r
79 if (ctx->state == L_OPEN)
\r
83 static void logfopen_callback(void *handle, int mode)
\r
85 struct LogContext *ctx = (struct LogContext *)handle;
\r
86 char buf[256], *event;
\r
91 ctx->state = L_ERROR; /* disable logging */
\r
93 fmode = (mode == 1 ? "ab" : "wb");
\r
94 ctx->lgfp = f_open(ctx->currlogfilename, fmode, FALSE);
\r
96 ctx->state = L_OPEN;
\r
98 ctx->state = L_ERROR;
\r
101 if (ctx->state == L_OPEN) {
\r
102 /* Write header line into log file. */
\r
104 strftime(buf, 24, "%Y.%m.%d %H:%M:%S", &tm);
\r
105 logprintf(ctx, "=~=~=~=~=~=~=~=~=~=~=~= PuTTY log %s"
\r
106 " =~=~=~=~=~=~=~=~=~=~=~=\r\n", buf);
\r
109 event = dupprintf("%s session log (%s mode) to file: %s",
\r
110 ctx->state == L_ERROR ?
\r
111 (mode == 0 ? "Disabled writing" : "Error writing") :
\r
112 (mode == 1 ? "Appending" : "Writing new"),
\r
113 (ctx->cfg.logtype == LGTYP_ASCII ? "ASCII" :
\r
114 ctx->cfg.logtype == LGTYP_DEBUG ? "raw" :
\r
115 ctx->cfg.logtype == LGTYP_PACKETS ? "SSH packets" :
\r
116 ctx->cfg.logtype == LGTYP_SSHRAW ? "SSH raw data" :
\r
118 filename_to_str(&ctx->currlogfilename));
\r
119 logevent(ctx->frontend, event);
\r
123 * Having either succeeded or failed in opening the log file,
\r
124 * we should write any queued data out.
\r
126 assert(ctx->state != L_OPENING); /* make _sure_ it won't be requeued */
\r
127 while (bufchain_size(&ctx->queue)) {
\r
130 bufchain_prefix(&ctx->queue, &data, &len);
\r
131 logwrite(ctx, data, len);
\r
132 bufchain_consume(&ctx->queue, len);
\r
137 * Open the log file. Takes care of detecting an already-existing
\r
138 * file and asking the user whether they want to append, overwrite
\r
139 * or cancel logging.
\r
141 void logfopen(void *handle)
\r
143 struct LogContext *ctx = (struct LogContext *)handle;
\r
147 /* Prevent repeat calls */
\r
148 if (ctx->state != L_CLOSED)
\r
151 if (!ctx->cfg.logtype)
\r
156 /* substitute special codes in file name */
\r
157 xlatlognam(&ctx->currlogfilename, ctx->cfg.logfilename,ctx->cfg.host, &tm);
\r
159 ctx->lgfp = f_open(ctx->currlogfilename, "r", FALSE); /* file already present? */
\r
162 if (ctx->cfg.logxfovr != LGXF_ASK) {
\r
163 mode = ((ctx->cfg.logxfovr == LGXF_OVR) ? 2 : 1);
\r
165 mode = askappend(ctx->frontend, ctx->currlogfilename,
\r
166 logfopen_callback, ctx);
\r
168 mode = 2; /* create == overwrite */
\r
171 ctx->state = L_OPENING;
\r
173 logfopen_callback(ctx, mode); /* open the file */
\r
176 void logfclose(void *handle)
\r
178 struct LogContext *ctx = (struct LogContext *)handle;
\r
183 ctx->state = L_CLOSED;
\r
187 * Log session traffic.
\r
189 void logtraffic(void *handle, unsigned char c, int logmode)
\r
191 struct LogContext *ctx = (struct LogContext *)handle;
\r
192 if (ctx->cfg.logtype > 0) {
\r
193 if (ctx->cfg.logtype == logmode)
\r
194 logwrite(ctx, &c, 1);
\r
199 * Log an Event Log entry. Used in SSH packet logging mode; this is
\r
200 * also as convenient a place as any to put the output of Event Log
\r
201 * entries to stderr when a command-line tool is in verbose mode.
\r
202 * (In particular, this is a better place to put it than in the
\r
203 * front ends, because it only has to be done once for all
\r
204 * platforms. Platforms which don't have a meaningful stderr can
\r
205 * just avoid defining FLAG_STDERR.
\r
207 void log_eventlog(void *handle, const char *event)
\r
209 struct LogContext *ctx = (struct LogContext *)handle;
\r
210 if ((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)) {
\r
211 fprintf(stderr, "%s\n", event);
\r
214 /* If we don't have a context yet (eg winnet.c init) then skip entirely */
\r
217 if (ctx->cfg.logtype != LGTYP_PACKETS &&
\r
218 ctx->cfg.logtype != LGTYP_SSHRAW)
\r
220 logprintf(ctx, "Event Log: %s\r\n", event);
\r
225 * Log an SSH packet.
\r
226 * If n_blanks != 0, blank or omit some parts.
\r
227 * Set of blanking areas must be in increasing order.
\r
229 void log_packet(void *handle, int direction, int type,
\r
230 char *texttype, const void *data, int len,
\r
231 int n_blanks, const struct logblank_t *blanks,
\r
232 const unsigned long *seq)
\r
234 struct LogContext *ctx = (struct LogContext *)handle;
\r
235 char dumpdata[80], smalldata[5];
\r
236 int p = 0, b = 0, omitted = 0;
\r
237 int output_pos = 0; /* NZ if pending output in dumpdata */
\r
239 if (!(ctx->cfg.logtype == LGTYP_SSHRAW ||
\r
240 (ctx->cfg.logtype == LGTYP_PACKETS && texttype)))
\r
243 /* Packet header. */
\r
246 logprintf(ctx, "%s packet #0x%lx, type %d / 0x%02x (%s)\r\n",
\r
247 direction == PKT_INCOMING ? "Incoming" : "Outgoing",
\r
248 *seq, type, type, texttype);
\r
250 logprintf(ctx, "%s packet type %d / 0x%02x (%s)\r\n",
\r
251 direction == PKT_INCOMING ? "Incoming" : "Outgoing",
\r
252 type, type, texttype);
\r
255 logprintf(ctx, "%s raw data\r\n",
\r
256 direction == PKT_INCOMING ? "Incoming" : "Outgoing");
\r
260 * Output a hex/ASCII dump of the packet body, blanking/omitting
\r
261 * parts as specified.
\r
266 /* Move to a current entry in the blanking array. */
\r
267 while ((b < n_blanks) &&
\r
268 (p >= blanks[b].offset + blanks[b].len))
\r
270 /* Work out what type of blanking to apply to
\r
272 blktype = PKTLOG_EMIT; /* default */
\r
273 if ((b < n_blanks) &&
\r
274 (p >= blanks[b].offset) &&
\r
275 (p < blanks[b].offset + blanks[b].len))
\r
276 blktype = blanks[b].type;
\r
278 /* If we're about to stop omitting, it's time to say how
\r
279 * much we omitted. */
\r
280 if ((blktype != PKTLOG_OMIT) && omitted) {
\r
281 logprintf(ctx, " (%d byte%s omitted)\r\n",
\r
282 omitted, (omitted==1?"":"s"));
\r
286 /* (Re-)initialise dumpdata as necessary
\r
287 * (start of row, or if we've just stopped omitting) */
\r
288 if (!output_pos && !omitted)
\r
289 sprintf(dumpdata, " %08x%*s\r\n", p-(p%16), 1+3*16+2+16, "");
\r
291 /* Deal with the current byte. */
\r
292 if (blktype == PKTLOG_OMIT) {
\r
296 if (blktype == PKTLOG_BLANK) {
\r
298 sprintf(smalldata, "XX");
\r
299 } else { /* PKTLOG_EMIT */
\r
300 c = ((unsigned char *)data)[p];
\r
301 sprintf(smalldata, "%02x", c);
\r
303 dumpdata[10+2+3*(p%16)] = smalldata[0];
\r
304 dumpdata[10+2+3*(p%16)+1] = smalldata[1];
\r
305 dumpdata[10+1+3*16+2+(p%16)] = (isprint(c) ? c : '.');
\r
306 output_pos = (p%16) + 1;
\r
311 /* Flush row if necessary */
\r
312 if (((p % 16) == 0) || (p == len) || omitted) {
\r
314 strcpy(dumpdata + 10+1+3*16+2+output_pos, "\r\n");
\r
315 logwrite(ctx, dumpdata, strlen(dumpdata));
\r
324 logprintf(ctx, " (%d byte%s omitted)\r\n",
\r
325 omitted, (omitted==1?"":"s"));
\r
329 void *log_init(void *frontend, Config *cfg)
\r
331 struct LogContext *ctx = snew(struct LogContext);
\r
333 ctx->state = L_CLOSED;
\r
334 ctx->frontend = frontend;
\r
335 ctx->cfg = *cfg; /* STRUCTURE COPY */
\r
336 bufchain_init(&ctx->queue);
\r
340 void log_free(void *handle)
\r
342 struct LogContext *ctx = (struct LogContext *)handle;
\r
345 bufchain_clear(&ctx->queue);
\r
349 void log_reconfig(void *handle, Config *cfg)
\r
351 struct LogContext *ctx = (struct LogContext *)handle;
\r
354 if (!filename_equal(ctx->cfg.logfilename, cfg->logfilename) ||
\r
355 ctx->cfg.logtype != cfg->logtype)
\r
356 reset_logging = TRUE;
\r
358 reset_logging = FALSE;
\r
363 ctx->cfg = *cfg; /* STRUCTURE COPY */
\r
370 * translate format codes into time/date strings
\r
371 * and insert them into log file name
\r
373 * "&Y":YYYY "&m":MM "&d":DD "&T":hhmmss "&h":<hostname> "&&":&
\r
375 static void xlatlognam(Filename *dest, Filename src,
\r
376 char *hostname, struct tm *tm) {
\r
377 char buf[10], *bufp;
\r
379 char buffer[FILENAME_MAX];
\r
380 int len = sizeof(buffer)-1;
\r
385 s = filename_to_str(&src);
\r
388 /* Let (bufp, len) be the string to append. */
\r
389 bufp = buf; /* don't usually override this */
\r
394 if (*s) switch (c = *s++, tolower((unsigned char)c)) {
\r
396 size = strftime(buf, sizeof(buf), "%Y", tm);
\r
399 size = strftime(buf, sizeof(buf), "%m", tm);
\r
402 size = strftime(buf, sizeof(buf), "%d", tm);
\r
405 size = strftime(buf, sizeof(buf), "%H%M%S", tm);
\r
409 size = strlen(bufp);
\r
423 memcpy(d, bufp, size);
\r
429 *dest = filename_from_str(buffer);
\r