10 /* log session to file stuff ... */
13 Filename currlogfilename
;
18 static void xlatlognam(Filename
*d
, Filename s
, char *hostname
, struct tm
*tm
);
21 * Log session traffic.
23 void logtraffic(void *handle
, unsigned char c
, int logmode
)
25 struct LogContext
*ctx
= (struct LogContext
*)handle
;
26 if (ctx
->cfg
.logtype
> 0) {
27 if (ctx
->cfg
.logtype
== logmode
) {
28 /* deferred open file from pgm start? */
38 * Log an Event Log entry. Used in SSH packet logging mode; this is
39 * also as convenient a place as any to put the output of Event Log
40 * entries to stderr when a command-line tool is in verbose mode.
41 * (In particular, this is a better place to put it than in the
42 * front ends, because it only has to be done once for all
43 * platforms. Platforms which don't have a meaningful stderr can
44 * just avoid defining FLAG_STDERR.
46 void log_eventlog(void *handle
, char *event
)
48 struct LogContext
*ctx
= (struct LogContext
*)handle
;
49 if ((flags
& FLAG_STDERR
) && (flags
& FLAG_VERBOSE
)) {
50 fprintf(stderr
, "%s\n", event
);
53 if (ctx
->cfg
.logtype
!= LGTYP_PACKETS
)
58 fprintf(ctx
->lgfp
, "Event Log: %s\r\n", event
);
64 void log_packet(void *handle
, int direction
, int type
,
65 char *texttype
, void *data
, int len
)
67 struct LogContext
*ctx
= (struct LogContext
*)handle
;
69 char dumpdata
[80], smalldata
[5];
71 if (ctx
->cfg
.logtype
!= LGTYP_PACKETS
)
76 fprintf(ctx
->lgfp
, "%s packet type %d / 0x%02x (%s)\r\n",
77 direction
== PKT_INCOMING ?
"Incoming" : "Outgoing",
78 type
, type
, texttype
);
79 for (i
= 0; i
< len
; i
+= 16) {
80 sprintf(dumpdata
, " %08x%*s\r\n", i
, 1+3*16+2+16, "");
81 for (j
= 0; j
< 16 && i
+j
< len
; j
++) {
82 int c
= ((unsigned char *)data
)[i
+j
];
83 sprintf(smalldata
, "%02x", c
);
84 dumpdata
[10+2+3*j
] = smalldata
[0];
85 dumpdata
[10+2+3*j
+1] = smalldata
[1];
86 dumpdata
[10+1+3*16+2+j
] = (isprint(c
) ? c
: '.');
88 strcpy(dumpdata
+ 10+1+3*16+2+j
, "\r\n");
89 fputs(dumpdata
, ctx
->lgfp
);
95 /* open log file append/overwrite mode */
96 void logfopen(void *handle
)
98 struct LogContext
*ctx
= (struct LogContext
*)handle
;
104 /* Prevent repeat calls */
108 if (!ctx
->cfg
.logtype
)
110 sprintf(writemod
, "wb"); /* default to rewrite */
115 /* substitute special codes in file name */
116 xlatlognam(&ctx
->currlogfilename
, ctx
->cfg
.logfilename
,ctx
->cfg
.host
, &tm
);
118 ctx
->lgfp
= f_open(ctx
->currlogfilename
, "r"); /* file already present? */
122 if (ctx
->cfg
.logxfovr
!= LGXF_ASK
) {
123 i
= ((ctx
->cfg
.logxfovr
== LGXF_OVR
) ?
2 : 1);
125 i
= askappend(ctx
->frontend
, ctx
->currlogfilename
);
127 writemod
[0] = 'a'; /* set append mode */
128 else if (i
== 0) { /* cancelled */
130 ctx
->cfg
.logtype
= 0; /* disable logging */
135 ctx
->lgfp
= f_open(ctx
->currlogfilename
, writemod
);
136 if (ctx
->lgfp
) { /* enter into event log */
137 /* --- write header line into log file */
138 fputs("=~=~=~=~=~=~=~=~=~=~=~= PuTTY log ", ctx
->lgfp
);
139 strftime(buf
, 24, "%Y.%m.%d %H:%M:%S", &tm
);
140 fputs(buf
, ctx
->lgfp
);
141 fputs(" =~=~=~=~=~=~=~=~=~=~=~=\r\n", ctx
->lgfp
);
143 sprintf(buf
, "%s session log (%s mode) to file: ",
144 (writemod
[0] == 'a') ?
"Appending" : "Writing new",
145 (ctx
->cfg
.logtype
== LGTYP_ASCII ?
"ASCII" :
146 ctx
->cfg
.logtype
== LGTYP_DEBUG ?
"raw" :
147 ctx
->cfg
.logtype
== LGTYP_PACKETS ?
"SSH packets" : "<ukwn>"));
148 /* Make sure we do not exceed the output buffer size */
149 strncat(buf
, filename_to_str(&ctx
->currlogfilename
), 128);
150 buf
[strlen(buf
)] = '\0';
151 logevent(ctx
->frontend
, buf
);
155 void logfclose(void *handle
)
157 struct LogContext
*ctx
= (struct LogContext
*)handle
;
164 void *log_init(void *frontend
, Config
*cfg
)
166 struct LogContext
*ctx
= snew(struct LogContext
);
168 ctx
->frontend
= frontend
;
169 ctx
->cfg
= *cfg
; /* STRUCTURE COPY */
173 void log_free(void *handle
)
175 struct LogContext
*ctx
= (struct LogContext
*)handle
;
181 void log_reconfig(void *handle
, Config
*cfg
)
183 struct LogContext
*ctx
= (struct LogContext
*)handle
;
186 if (!filename_equal(ctx
->cfg
.logfilename
, cfg
->logfilename
) ||
187 ctx
->cfg
.logtype
!= cfg
->logtype
)
188 reset_logging
= TRUE
;
190 reset_logging
= FALSE
;
195 ctx
->cfg
= *cfg
; /* STRUCTURE COPY */
202 * translate format codes into time/date strings
203 * and insert them into log file name
205 * "&Y":YYYY "&m":MM "&d":DD "&T":hhmm "&h":<hostname> "&&":&
207 static void xlatlognam(Filename
*dest
, Filename src
,
208 char *hostname
, struct tm
*tm
) {
211 char buffer
[FILENAME_MAX
];
212 int len
= sizeof(buffer
)-1;
217 s
= filename_to_str(&src
);
220 /* Let (bufp, len) be the string to append. */
221 bufp
= buf
; /* don't usually override this */
226 if (*s
) switch (c
= *s
++, tolower(c
)) {
228 size
= strftime(buf
, sizeof(buf
), "%Y", tm
);
231 size
= strftime(buf
, sizeof(buf
), "%m", tm
);
234 size
= strftime(buf
, sizeof(buf
), "%d", tm
);
237 size
= strftime(buf
, sizeof(buf
), "%H%M%S", tm
);
255 memcpy(d
, bufp
, size
);
261 *dest
= filename_from_str(buffer
);