14 /* log session to file stuff ... */
17 enum { L_CLOSED
, L_OPENING
, L_OPEN
, L_ERROR
} state
;
19 Filename currlogfilename
;
24 static void xlatlognam(Filename
*d
, Filename s
, char *hostname
, struct tm
*tm
);
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.
32 static void logwrite(struct LogContext
*ctx
, void *data
, int len
)
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.
39 if (ctx
->state
== L_CLOSED
)
42 if (ctx
->state
== L_OPENING
) {
43 bufchain_add(&ctx
->queue
, data
, len
);
44 } else if (ctx
->state
== L_OPEN
) {
46 fwrite(data
, 1, len
, ctx
->lgfp
);
47 } /* else L_ERROR, so ignore the write */
51 * Convenience wrapper on logwrite() which printf-formats the
54 static void logprintf(struct LogContext
*ctx
, const char *fmt
, ...)
60 data
= dupvprintf(fmt
, ap
);
63 logwrite(ctx
, data
, strlen(data
));
68 * Flush any open log file.
70 void logflush(void *handle
) {
71 struct LogContext
*ctx
= (struct LogContext
*)handle
;
72 if (ctx
->cfg
.logtype
> 0)
73 if (ctx
->state
== L_OPEN
)
77 static void logfopen_callback(void *handle
, int mode
)
79 struct LogContext
*ctx
= (struct LogContext
*)handle
;
80 char buf
[256], *event
;
85 ctx
->state
= L_ERROR
; /* disable logging */
87 fmode
= (mode
== 1 ?
"ab" : "wb");
88 ctx
->lgfp
= f_open(ctx
->currlogfilename
, fmode
, TRUE
);
95 if (ctx
->state
== L_OPEN
) {
96 /* Write header line into log file. */
98 strftime(buf
, 24, "%Y.%m.%d %H:%M:%S", &tm
);
99 logprintf(ctx
, "=~=~=~=~=~=~=~=~=~=~=~= PuTTY log %s"
100 " =~=~=~=~=~=~=~=~=~=~=~=\r\n", buf
);
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" :
109 ctx
->cfg
.logtype
== LGTYP_SSHRAW ?
"SSH raw data" :
111 filename_to_str(&ctx
->currlogfilename
));
112 logevent(ctx
->frontend
, event
);
116 * Having either succeeded or failed in opening the log file,
117 * we should write any queued data out.
119 assert(ctx
->state
!= L_OPENING
); /* make _sure_ it won't be requeued */
120 while (bufchain_size(&ctx
->queue
)) {
123 bufchain_prefix(&ctx
->queue
, &data
, &len
);
124 logwrite(ctx
, data
, len
);
125 bufchain_consume(&ctx
->queue
, len
);
130 * Open the log file. Takes care of detecting an already-existing
131 * file and asking the user whether they want to append, overwrite
134 void logfopen(void *handle
)
136 struct LogContext
*ctx
= (struct LogContext
*)handle
;
140 /* Prevent repeat calls */
141 if (ctx
->state
!= L_CLOSED
)
144 if (!ctx
->cfg
.logtype
)
149 /* substitute special codes in file name */
150 xlatlognam(&ctx
->currlogfilename
, ctx
->cfg
.logfilename
,ctx
->cfg
.host
, &tm
);
152 ctx
->lgfp
= f_open(ctx
->currlogfilename
, "r", FALSE
); /* file already present? */
155 if (ctx
->cfg
.logxfovr
!= LGXF_ASK
) {
156 mode
= ((ctx
->cfg
.logxfovr
== LGXF_OVR
) ?
2 : 1);
158 mode
= askappend(ctx
->frontend
, ctx
->currlogfilename
,
159 logfopen_callback
, ctx
);
161 mode
= 2; /* create == overwrite */
164 ctx
->state
= L_OPENING
;
166 logfopen_callback(ctx
, mode
); /* open the file */
169 void logfclose(void *handle
)
171 struct LogContext
*ctx
= (struct LogContext
*)handle
;
176 ctx
->state
= L_CLOSED
;
180 * Log session traffic.
182 void logtraffic(void *handle
, unsigned char c
, int logmode
)
184 struct LogContext
*ctx
= (struct LogContext
*)handle
;
185 if (ctx
->cfg
.logtype
> 0) {
186 if (ctx
->cfg
.logtype
== logmode
)
187 logwrite(ctx
, &c
, 1);
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.
200 void log_eventlog(void *handle
, const char *event
)
202 struct LogContext
*ctx
= (struct LogContext
*)handle
;
203 if ((flags
& FLAG_STDERR
) && (flags
& FLAG_VERBOSE
)) {
204 fprintf(stderr
, "%s\n", event
);
207 /* If we don't have a context yet (eg winnet.c init) then skip entirely */
210 if (ctx
->cfg
.logtype
!= LGTYP_PACKETS
&&
211 ctx
->cfg
.logtype
!= LGTYP_SSHRAW
)
213 logprintf(ctx
, "Event Log: %s\r\n", event
);
219 * If n_blanks != 0, blank or omit some parts.
220 * Set of blanking areas must be in increasing order.
222 void log_packet(void *handle
, int direction
, int type
,
223 char *texttype
, const void *data
, int len
,
224 int n_blanks
, const struct logblank_t
*blanks
,
225 const unsigned long *seq
)
227 struct LogContext
*ctx
= (struct LogContext
*)handle
;
228 char dumpdata
[80], smalldata
[5];
229 int p
= 0, b
= 0, omitted
= 0;
230 int output_pos
= 0; /* NZ if pending output in dumpdata */
232 if (!(ctx
->cfg
.logtype
== LGTYP_SSHRAW
||
233 (ctx
->cfg
.logtype
== LGTYP_PACKETS
&& texttype
)))
239 logprintf(ctx
, "%s packet #0x%lx, type %d / 0x%02x (%s)\r\n",
240 direction
== PKT_INCOMING ?
"Incoming" : "Outgoing",
241 *seq
, type
, type
, texttype
);
243 logprintf(ctx
, "%s packet type %d / 0x%02x (%s)\r\n",
244 direction
== PKT_INCOMING ?
"Incoming" : "Outgoing",
245 type
, type
, texttype
);
248 logprintf(ctx
, "%s raw data\r\n",
249 direction
== PKT_INCOMING ?
"Incoming" : "Outgoing");
253 * Output a hex/ASCII dump of the packet body, blanking/omitting
254 * parts as specified.
259 /* Move to a current entry in the blanking array. */
260 while ((b
< n_blanks
) &&
261 (p
>= blanks
[b
].offset
+ blanks
[b
].len
))
263 /* Work out what type of blanking to apply to
265 blktype
= PKTLOG_EMIT
; /* default */
266 if ((b
< n_blanks
) &&
267 (p
>= blanks
[b
].offset
) &&
268 (p
< blanks
[b
].offset
+ blanks
[b
].len
))
269 blktype
= blanks
[b
].type
;
271 /* If we're about to stop omitting, it's time to say how
272 * much we omitted. */
273 if ((blktype
!= PKTLOG_OMIT
) && omitted
) {
274 logprintf(ctx
, " (%d byte%s omitted)\r\n",
275 omitted
, (omitted
==1?
"":"s"));
279 /* (Re-)initialise dumpdata as necessary
280 * (start of row, or if we've just stopped omitting) */
281 if (!output_pos
&& !omitted
)
282 sprintf(dumpdata
, " %08x%*s\r\n", p
-(p
%16), 1+3*16+2+16, "");
284 /* Deal with the current byte. */
285 if (blktype
== PKTLOG_OMIT
) {
289 if (blktype
== PKTLOG_BLANK
) {
291 sprintf(smalldata
, "XX");
292 } else { /* PKTLOG_EMIT */
293 c
= ((unsigned char *)data
)[p
];
294 sprintf(smalldata
, "%02x", c
);
296 dumpdata
[10+2+3*(p
%16)] = smalldata
[0];
297 dumpdata
[10+2+3*(p
%16)+1] = smalldata
[1];
298 dumpdata
[10+1+3*16+2+(p
%16)] = (isprint(c
) ? c
: '.');
299 output_pos
= (p
%16) + 1;
304 /* Flush row if necessary */
305 if (((p
% 16) == 0) || (p
== len
) || omitted
) {
307 strcpy(dumpdata
+ 10+1+3*16+2+output_pos
, "\r\n");
308 logwrite(ctx
, dumpdata
, strlen(dumpdata
));
317 logprintf(ctx
, " (%d byte%s omitted)\r\n",
318 omitted
, (omitted
==1?
"":"s"));
322 void *log_init(void *frontend
, Config
*cfg
)
324 struct LogContext
*ctx
= snew(struct LogContext
);
326 ctx
->state
= L_CLOSED
;
327 ctx
->frontend
= frontend
;
328 ctx
->cfg
= *cfg
; /* STRUCTURE COPY */
329 bufchain_init(&ctx
->queue
);
333 void log_free(void *handle
)
335 struct LogContext
*ctx
= (struct LogContext
*)handle
;
338 bufchain_clear(&ctx
->queue
);
342 void log_reconfig(void *handle
, Config
*cfg
)
344 struct LogContext
*ctx
= (struct LogContext
*)handle
;
347 if (!filename_equal(ctx
->cfg
.logfilename
, cfg
->logfilename
) ||
348 ctx
->cfg
.logtype
!= cfg
->logtype
)
349 reset_logging
= TRUE
;
351 reset_logging
= FALSE
;
356 ctx
->cfg
= *cfg
; /* STRUCTURE COPY */
363 * translate format codes into time/date strings
364 * and insert them into log file name
366 * "&Y":YYYY "&m":MM "&d":DD "&T":hhmmss "&h":<hostname> "&&":&
368 static void xlatlognam(Filename
*dest
, Filename src
,
369 char *hostname
, struct tm
*tm
) {
372 char buffer
[FILENAME_MAX
];
373 int len
= sizeof(buffer
)-1;
378 s
= filename_to_str(&src
);
381 /* Let (bufp, len) be the string to append. */
382 bufp
= buf
; /* don't usually override this */
387 if (*s
) switch (c
= *s
++, tolower(c
)) {
389 size
= strftime(buf
, sizeof(buf
), "%Y", tm
);
392 size
= strftime(buf
, sizeof(buf
), "%m", tm
);
395 size
= strftime(buf
, sizeof(buf
), "%d", tm
);
398 size
= strftime(buf
, sizeof(buf
), "%H%M%S", tm
);
416 memcpy(d
, bufp
, size
);
422 *dest
= filename_from_str(buffer
);