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