00db133f |
1 | #include <stdio.h> |
2 | #include <stdlib.h> |
3 | #include <ctype.h> |
4 | |
5 | #include <time.h> |
6 | #include <assert.h> |
7 | |
8 | #include "putty.h" |
9 | |
10 | /* log session to file stuff ... */ |
a8327734 |
11 | struct LogContext { |
12 | FILE *lgfp; |
9a30e26b |
13 | Filename currlogfilename; |
a8327734 |
14 | void *frontend; |
c229ef97 |
15 | Config cfg; |
a8327734 |
16 | }; |
00db133f |
17 | |
9a30e26b |
18 | static void xlatlognam(Filename *d, Filename s, char *hostname, struct tm *tm); |
00db133f |
19 | |
20 | /* |
21 | * Log session traffic. |
22 | */ |
a8327734 |
23 | void logtraffic(void *handle, unsigned char c, int logmode) |
00db133f |
24 | { |
a8327734 |
25 | struct LogContext *ctx = (struct LogContext *)handle; |
c229ef97 |
26 | if (ctx->cfg.logtype > 0) { |
27 | if (ctx->cfg.logtype == logmode) { |
00db133f |
28 | /* deferred open file from pgm start? */ |
a8327734 |
29 | if (!ctx->lgfp) |
30 | logfopen(ctx); |
31 | if (ctx->lgfp) |
32 | fputc(c, ctx->lgfp); |
00db133f |
33 | } |
34 | } |
35 | } |
36 | |
37 | /* |
11cc5e30 |
38 | * Flush any open log file. |
39 | */ |
40 | void logflush(void *handle) { |
41 | struct LogContext *ctx = (struct LogContext *)handle; |
42 | if (ctx->cfg.logtype > 0) |
43 | if (ctx->lgfp) |
44 | fflush(ctx->lgfp); |
45 | } |
46 | |
47 | /* |
382908ad |
48 | * Log an Event Log entry. Used in SSH packet logging mode; this is |
49 | * also as convenient a place as any to put the output of Event Log |
50 | * entries to stderr when a command-line tool is in verbose mode. |
51 | * (In particular, this is a better place to put it than in the |
52 | * front ends, because it only has to be done once for all |
53 | * platforms. Platforms which don't have a meaningful stderr can |
54 | * just avoid defining FLAG_STDERR. |
fb89f7ff |
55 | */ |
cbe2d68f |
56 | void log_eventlog(void *handle, const char *event) |
fb89f7ff |
57 | { |
a8327734 |
58 | struct LogContext *ctx = (struct LogContext *)handle; |
382908ad |
59 | if ((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)) { |
60 | fprintf(stderr, "%s\n", event); |
61 | fflush(stderr); |
62 | } |
c229ef97 |
63 | if (ctx->cfg.logtype != LGTYP_PACKETS) |
fb89f7ff |
64 | return; |
a8327734 |
65 | if (!ctx->lgfp) |
66 | logfopen(ctx); |
67 | if (ctx->lgfp) |
fa76f4a3 |
68 | fprintf(ctx->lgfp, "Event Log: %s\r\n", event); |
fb89f7ff |
69 | } |
70 | |
71 | /* |
00db133f |
72 | * Log an SSH packet. |
73 | */ |
a8327734 |
74 | void log_packet(void *handle, int direction, int type, |
75 | char *texttype, void *data, int len) |
00db133f |
76 | { |
a8327734 |
77 | struct LogContext *ctx = (struct LogContext *)handle; |
fb89f7ff |
78 | int i, j; |
00db133f |
79 | char dumpdata[80], smalldata[5]; |
80 | |
c229ef97 |
81 | if (ctx->cfg.logtype != LGTYP_PACKETS) |
00db133f |
82 | return; |
a8327734 |
83 | if (!ctx->lgfp) |
84 | logfopen(ctx); |
85 | if (ctx->lgfp) { |
fa76f4a3 |
86 | fprintf(ctx->lgfp, "%s packet type %d / 0x%02x (%s)\r\n", |
00db133f |
87 | direction == PKT_INCOMING ? "Incoming" : "Outgoing", |
88 | type, type, texttype); |
89 | for (i = 0; i < len; i += 16) { |
fa76f4a3 |
90 | sprintf(dumpdata, " %08x%*s\r\n", i, 1+3*16+2+16, ""); |
00db133f |
91 | for (j = 0; j < 16 && i+j < len; j++) { |
92 | int c = ((unsigned char *)data)[i+j]; |
93 | sprintf(smalldata, "%02x", c); |
94 | dumpdata[10+2+3*j] = smalldata[0]; |
95 | dumpdata[10+2+3*j+1] = smalldata[1]; |
96 | dumpdata[10+1+3*16+2+j] = (isprint(c) ? c : '.'); |
97 | } |
fa76f4a3 |
98 | strcpy(dumpdata + 10+1+3*16+2+j, "\r\n"); |
a8327734 |
99 | fputs(dumpdata, ctx->lgfp); |
00db133f |
100 | } |
a8327734 |
101 | fflush(ctx->lgfp); |
00db133f |
102 | } |
103 | } |
104 | |
105 | /* open log file append/overwrite mode */ |
a8327734 |
106 | void logfopen(void *handle) |
00db133f |
107 | { |
a8327734 |
108 | struct LogContext *ctx = (struct LogContext *)handle; |
00db133f |
109 | char buf[256]; |
110 | time_t t; |
111 | struct tm tm; |
112 | char writemod[4]; |
113 | |
fb89f7ff |
114 | /* Prevent repeat calls */ |
a8327734 |
115 | if (ctx->lgfp) |
fb89f7ff |
116 | return; |
117 | |
c229ef97 |
118 | if (!ctx->cfg.logtype) |
00db133f |
119 | return; |
fa76f4a3 |
120 | sprintf(writemod, "wb"); /* default to rewrite */ |
00db133f |
121 | |
122 | time(&t); |
123 | tm = *localtime(&t); |
124 | |
125 | /* substitute special codes in file name */ |
9a30e26b |
126 | xlatlognam(&ctx->currlogfilename, ctx->cfg.logfilename,ctx->cfg.host, &tm); |
00db133f |
127 | |
9a30e26b |
128 | ctx->lgfp = f_open(ctx->currlogfilename, "r"); /* file already present? */ |
a8327734 |
129 | if (ctx->lgfp) { |
00db133f |
130 | int i; |
a8327734 |
131 | fclose(ctx->lgfp); |
c229ef97 |
132 | if (ctx->cfg.logxfovr != LGXF_ASK) { |
133 | i = ((ctx->cfg.logxfovr == LGXF_OVR) ? 2 : 1); |
bf61b566 |
134 | } else |
135 | i = askappend(ctx->frontend, ctx->currlogfilename); |
00db133f |
136 | if (i == 1) |
137 | writemod[0] = 'a'; /* set append mode */ |
138 | else if (i == 0) { /* cancelled */ |
a8327734 |
139 | ctx->lgfp = NULL; |
c229ef97 |
140 | ctx->cfg.logtype = 0; /* disable logging */ |
00db133f |
141 | return; |
142 | } |
143 | } |
144 | |
9a30e26b |
145 | ctx->lgfp = f_open(ctx->currlogfilename, writemod); |
a8327734 |
146 | if (ctx->lgfp) { /* enter into event log */ |
fb89f7ff |
147 | /* --- write header line into log file */ |
a8327734 |
148 | fputs("=~=~=~=~=~=~=~=~=~=~=~= PuTTY log ", ctx->lgfp); |
fb89f7ff |
149 | strftime(buf, 24, "%Y.%m.%d %H:%M:%S", &tm); |
a8327734 |
150 | fputs(buf, ctx->lgfp); |
fa76f4a3 |
151 | fputs(" =~=~=~=~=~=~=~=~=~=~=~=\r\n", ctx->lgfp); |
fb89f7ff |
152 | |
153 | sprintf(buf, "%s session log (%s mode) to file: ", |
00db133f |
154 | (writemod[0] == 'a') ? "Appending" : "Writing new", |
c229ef97 |
155 | (ctx->cfg.logtype == LGTYP_ASCII ? "ASCII" : |
156 | ctx->cfg.logtype == LGTYP_DEBUG ? "raw" : |
157 | ctx->cfg.logtype == LGTYP_PACKETS ? "SSH packets" : "<ukwn>")); |
00db133f |
158 | /* Make sure we do not exceed the output buffer size */ |
9fab77dc |
159 | strncat(buf, filename_to_str(&ctx->currlogfilename), 128); |
00db133f |
160 | buf[strlen(buf)] = '\0'; |
a8327734 |
161 | logevent(ctx->frontend, buf); |
00db133f |
162 | } |
163 | } |
164 | |
a8327734 |
165 | void logfclose(void *handle) |
00db133f |
166 | { |
a8327734 |
167 | struct LogContext *ctx = (struct LogContext *)handle; |
168 | if (ctx->lgfp) { |
169 | fclose(ctx->lgfp); |
170 | ctx->lgfp = NULL; |
00db133f |
171 | } |
172 | } |
173 | |
c229ef97 |
174 | void *log_init(void *frontend, Config *cfg) |
a8327734 |
175 | { |
3d88e64d |
176 | struct LogContext *ctx = snew(struct LogContext); |
a8327734 |
177 | ctx->lgfp = NULL; |
178 | ctx->frontend = frontend; |
c229ef97 |
179 | ctx->cfg = *cfg; /* STRUCTURE COPY */ |
a8327734 |
180 | return ctx; |
181 | } |
182 | |
fabd1805 |
183 | void log_free(void *handle) |
184 | { |
185 | struct LogContext *ctx = (struct LogContext *)handle; |
186 | |
187 | logfclose(ctx); |
188 | sfree(ctx); |
189 | } |
190 | |
c229ef97 |
191 | void log_reconfig(void *handle, Config *cfg) |
192 | { |
193 | struct LogContext *ctx = (struct LogContext *)handle; |
194 | int reset_logging; |
195 | |
9a30e26b |
196 | if (!filename_equal(ctx->cfg.logfilename, cfg->logfilename) || |
c229ef97 |
197 | ctx->cfg.logtype != cfg->logtype) |
198 | reset_logging = TRUE; |
199 | else |
200 | reset_logging = FALSE; |
201 | |
202 | if (reset_logging) |
4512d432 |
203 | logfclose(ctx); |
c229ef97 |
204 | |
205 | ctx->cfg = *cfg; /* STRUCTURE COPY */ |
206 | |
207 | if (reset_logging) |
4512d432 |
208 | logfopen(ctx); |
c229ef97 |
209 | } |
210 | |
00db133f |
211 | /* |
212 | * translate format codes into time/date strings |
213 | * and insert them into log file name |
214 | * |
215 | * "&Y":YYYY "&m":MM "&d":DD "&T":hhmm "&h":<hostname> "&&":& |
216 | */ |
9a30e26b |
217 | static void xlatlognam(Filename *dest, Filename src, |
218 | char *hostname, struct tm *tm) { |
00db133f |
219 | char buf[10], *bufp; |
220 | int size; |
9a30e26b |
221 | char buffer[FILENAME_MAX]; |
222 | int len = sizeof(buffer)-1; |
9fab77dc |
223 | char *d; |
224 | const char *s; |
9a30e26b |
225 | |
226 | d = buffer; |
9fab77dc |
227 | s = filename_to_str(&src); |
00db133f |
228 | |
229 | while (*s) { |
230 | /* Let (bufp, len) be the string to append. */ |
231 | bufp = buf; /* don't usually override this */ |
232 | if (*s == '&') { |
233 | char c; |
234 | s++; |
1709795f |
235 | size = 0; |
00db133f |
236 | if (*s) switch (c = *s++, tolower(c)) { |
237 | case 'y': |
238 | size = strftime(buf, sizeof(buf), "%Y", tm); |
239 | break; |
240 | case 'm': |
241 | size = strftime(buf, sizeof(buf), "%m", tm); |
242 | break; |
243 | case 'd': |
244 | size = strftime(buf, sizeof(buf), "%d", tm); |
245 | break; |
246 | case 't': |
247 | size = strftime(buf, sizeof(buf), "%H%M%S", tm); |
248 | break; |
249 | case 'h': |
250 | bufp = hostname; |
251 | size = strlen(bufp); |
252 | break; |
253 | default: |
254 | buf[0] = '&'; |
255 | size = 1; |
256 | if (c != '&') |
257 | buf[size++] = c; |
258 | } |
259 | } else { |
260 | buf[0] = *s++; |
261 | size = 1; |
262 | } |
263 | if (size > len) |
264 | size = len; |
265 | memcpy(d, bufp, size); |
266 | d += size; |
267 | len -= size; |
268 | } |
269 | *d = '\0'; |
9a30e26b |
270 | |
0ac2edb3 |
271 | *dest = filename_from_str(buffer); |
00db133f |
272 | } |