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 fwrite(data, 1, len, ctx->lgfp);
\r
47 } /* else L_ERROR, so ignore the write */
\r
51 * Convenience wrapper on logwrite() which printf-formats the
\r
54 static void logprintf(struct LogContext *ctx, const char *fmt, ...)
\r
60 data = dupvprintf(fmt, ap);
\r
63 logwrite(ctx, data, strlen(data));
\r
68 * Flush any open log file.
\r
70 void logflush(void *handle) {
\r
71 struct LogContext *ctx = (struct LogContext *)handle;
\r
72 if (ctx->cfg.logtype > 0)
\r
73 if (ctx->state == L_OPEN)
\r
77 static void logfopen_callback(void *handle, int mode)
\r
79 struct LogContext *ctx = (struct LogContext *)handle;
\r
80 char buf[256], *event;
\r
85 ctx->state = L_ERROR; /* disable logging */
\r
87 fmode = (mode == 1 ? "ab" : "wb");
\r
88 ctx->lgfp = f_open(ctx->currlogfilename, fmode, TRUE);
\r
90 ctx->state = L_OPEN;
\r
92 ctx->state = L_ERROR;
\r
95 if (ctx->state == L_OPEN) {
\r
96 /* Write header line into log file. */
\r
98 strftime(buf, 24, "%Y.%m.%d %H:%M:%S", &tm);
\r
99 logprintf(ctx, "=~=~=~=~=~=~=~=~=~=~=~= PuTTY log %s"
\r
100 " =~=~=~=~=~=~=~=~=~=~=~=\r\n", buf);
\r
103 event = dupprintf("%s session log (%s mode) to file: %s",
\r
104 (mode == 0 ? "Disabled writing" :
\r
105 mode == 1 ? "Appending" : "Writing new"),
\r
106 (ctx->cfg.logtype == LGTYP_ASCII ? "ASCII" :
\r
107 ctx->cfg.logtype == LGTYP_DEBUG ? "raw" :
\r
108 ctx->cfg.logtype == LGTYP_PACKETS ? "SSH packets" :
\r
109 ctx->cfg.logtype == LGTYP_SSHRAW ? "SSH raw data" :
\r
111 filename_to_str(&ctx->currlogfilename));
\r
112 logevent(ctx->frontend, event);
\r
116 * Having either succeeded or failed in opening the log file,
\r
117 * we should write any queued data out.
\r
119 assert(ctx->state != L_OPENING); /* make _sure_ it won't be requeued */
\r
120 while (bufchain_size(&ctx->queue)) {
\r
123 bufchain_prefix(&ctx->queue, &data, &len);
\r
124 logwrite(ctx, data, len);
\r
125 bufchain_consume(&ctx->queue, len);
\r
130 * Open the log file. Takes care of detecting an already-existing
\r
131 * file and asking the user whether they want to append, overwrite
\r
132 * or cancel logging.
\r
134 void logfopen(void *handle)
\r
136 struct LogContext *ctx = (struct LogContext *)handle;
\r
140 /* Prevent repeat calls */
\r
141 if (ctx->state != L_CLOSED)
\r
144 if (!ctx->cfg.logtype)
\r
149 /* substitute special codes in file name */
\r
150 xlatlognam(&ctx->currlogfilename, ctx->cfg.logfilename,ctx->cfg.host, &tm);
\r
152 ctx->lgfp = f_open(ctx->currlogfilename, "r", FALSE); /* file already present? */
\r
155 if (ctx->cfg.logxfovr != LGXF_ASK) {
\r
156 mode = ((ctx->cfg.logxfovr == LGXF_OVR) ? 2 : 1);
\r
158 mode = askappend(ctx->frontend, ctx->currlogfilename,
\r
159 logfopen_callback, ctx);
\r
161 mode = 2; /* create == overwrite */
\r
164 ctx->state = L_OPENING;
\r
166 logfopen_callback(ctx, mode); /* open the file */
\r
169 void logfclose(void *handle)
\r
171 struct LogContext *ctx = (struct LogContext *)handle;
\r
176 ctx->state = L_CLOSED;
\r
180 * Log session traffic.
\r
182 void logtraffic(void *handle, unsigned char c, int logmode)
\r
184 struct LogContext *ctx = (struct LogContext *)handle;
\r
185 if (ctx->cfg.logtype > 0) {
\r
186 if (ctx->cfg.logtype == logmode)
\r
187 logwrite(ctx, &c, 1);
\r
192 * Log an Event Log entry. Used in SSH packet logging mode; this is
\r
193 * also as convenient a place as any to put the output of Event Log
\r
194 * entries to stderr when a command-line tool is in verbose mode.
\r
195 * (In particular, this is a better place to put it than in the
\r
196 * front ends, because it only has to be done once for all
\r
197 * platforms. Platforms which don't have a meaningful stderr can
\r
198 * just avoid defining FLAG_STDERR.
\r
200 void log_eventlog(void *handle, const char *event)
\r
202 struct LogContext *ctx = (struct LogContext *)handle;
\r
203 if ((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)) {
\r
204 fprintf(stderr, "%s\n", event);
\r
207 /* If we don't have a context yet (eg winnet.c init) then skip entirely */
\r
210 if (ctx->cfg.logtype != LGTYP_PACKETS &&
\r
211 ctx->cfg.logtype != LGTYP_SSHRAW)
\r
213 logprintf(ctx, "Event Log: %s\r\n", event);
\r
218 * Log an SSH packet.
\r
219 * If n_blanks != 0, blank or omit some parts.
\r
220 * Set of blanking areas must be in increasing order.
\r
222 void log_packet(void *handle, int direction, int type,
\r
223 char *texttype, void *data, int len,
\r
224 int n_blanks, const struct logblank_t *blanks)
\r
226 struct LogContext *ctx = (struct LogContext *)handle;
\r
227 char dumpdata[80], smalldata[5];
\r
228 int p = 0, b = 0, omitted = 0;
\r
229 int output_pos = 0; /* NZ if pending output in dumpdata */
\r
231 if (!(ctx->cfg.logtype == LGTYP_SSHRAW ||
\r
232 (ctx->cfg.logtype == LGTYP_PACKETS && texttype)))
\r
235 /* Packet header. */
\r
237 logprintf(ctx, "%s packet type %d / 0x%02x (%s)\r\n",
\r
238 direction == PKT_INCOMING ? "Incoming" : "Outgoing",
\r
239 type, type, texttype);
\r
241 logprintf(ctx, "%s raw data\r\n",
\r
242 direction == PKT_INCOMING ? "Incoming" : "Outgoing");
\r
245 * Output a hex/ASCII dump of the packet body, blanking/omitting
\r
246 * parts as specified.
\r
251 /* Move to a current entry in the blanking array. */
\r
252 while ((b < n_blanks) &&
\r
253 (p >= blanks[b].offset + blanks[b].len))
\r
255 /* Work out what type of blanking to apply to
\r
257 blktype = PKTLOG_EMIT; /* default */
\r
258 if ((b < n_blanks) &&
\r
259 (p >= blanks[b].offset) &&
\r
260 (p < blanks[b].offset + blanks[b].len))
\r
261 blktype = blanks[b].type;
\r
263 /* If we're about to stop omitting, it's time to say how
\r
264 * much we omitted. */
\r
265 if ((blktype != PKTLOG_OMIT) && omitted) {
\r
266 logprintf(ctx, " (%d byte%s omitted)\r\n",
\r
267 omitted, (omitted==1?"":"s"));
\r
271 /* (Re-)initialise dumpdata as necessary
\r
272 * (start of row, or if we've just stopped omitting) */
\r
273 if (!output_pos && !omitted)
\r
274 sprintf(dumpdata, " %08x%*s\r\n", p-(p%16), 1+3*16+2+16, "");
\r
276 /* Deal with the current byte. */
\r
277 if (blktype == PKTLOG_OMIT) {
\r
281 if (blktype == PKTLOG_BLANK) {
\r
283 sprintf(smalldata, "XX");
\r
284 } else { /* PKTLOG_EMIT */
\r
285 c = ((unsigned char *)data)[p];
\r
286 sprintf(smalldata, "%02x", c);
\r
288 dumpdata[10+2+3*(p%16)] = smalldata[0];
\r
289 dumpdata[10+2+3*(p%16)+1] = smalldata[1];
\r
290 dumpdata[10+1+3*16+2+(p%16)] = (isprint(c) ? c : '.');
\r
291 output_pos = (p%16) + 1;
\r
296 /* Flush row if necessary */
\r
297 if (((p % 16) == 0) || (p == len) || omitted) {
\r
299 strcpy(dumpdata + 10+1+3*16+2+output_pos, "\r\n");
\r
300 logwrite(ctx, dumpdata, strlen(dumpdata));
\r
309 logprintf(ctx, " (%d byte%s omitted)\r\n",
\r
310 omitted, (omitted==1?"":"s"));
\r
314 void *log_init(void *frontend, Config *cfg)
\r
316 struct LogContext *ctx = snew(struct LogContext);
\r
318 ctx->state = L_CLOSED;
\r
319 ctx->frontend = frontend;
\r
320 ctx->cfg = *cfg; /* STRUCTURE COPY */
\r
321 bufchain_init(&ctx->queue);
\r
325 void log_free(void *handle)
\r
327 struct LogContext *ctx = (struct LogContext *)handle;
\r
330 bufchain_clear(&ctx->queue);
\r
334 void log_reconfig(void *handle, Config *cfg)
\r
336 struct LogContext *ctx = (struct LogContext *)handle;
\r
339 if (!filename_equal(ctx->cfg.logfilename, cfg->logfilename) ||
\r
340 ctx->cfg.logtype != cfg->logtype)
\r
341 reset_logging = TRUE;
\r
343 reset_logging = FALSE;
\r
348 ctx->cfg = *cfg; /* STRUCTURE COPY */
\r
355 * translate format codes into time/date strings
\r
356 * and insert them into log file name
\r
358 * "&Y":YYYY "&m":MM "&d":DD "&T":hhmmss "&h":<hostname> "&&":&
\r
360 static void xlatlognam(Filename *dest, Filename src,
\r
361 char *hostname, struct tm *tm) {
\r
362 char buf[10], *bufp;
\r
364 char buffer[FILENAME_MAX];
\r
365 int len = sizeof(buffer)-1;
\r
370 s = filename_to_str(&src);
\r
373 /* Let (bufp, len) be the string to append. */
\r
374 bufp = buf; /* don't usually override this */
\r
379 if (*s) switch (c = *s++, tolower(c)) {
\r
381 size = strftime(buf, sizeof(buf), "%Y", tm);
\r
384 size = strftime(buf, sizeof(buf), "%m", tm);
\r
387 size = strftime(buf, sizeof(buf), "%d", tm);
\r
390 size = strftime(buf, sizeof(buf), "%H%M%S", tm);
\r
394 size = strlen(bufp);
\r
408 memcpy(d, bufp, size);
\r
414 *dest = filename_from_str(buffer);
\r