| 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 ... */ |
| 11 | struct LogContext { |
| 12 | FILE *lgfp; |
| 13 | Filename currlogfilename; |
| 14 | void *frontend; |
| 15 | Config cfg; |
| 16 | }; |
| 17 | |
| 18 | static void xlatlognam(Filename *d, Filename s, char *hostname, struct tm *tm); |
| 19 | |
| 20 | /* |
| 21 | * Log session traffic. |
| 22 | */ |
| 23 | void logtraffic(void *handle, unsigned char c, int logmode) |
| 24 | { |
| 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? */ |
| 29 | if (!ctx->lgfp) |
| 30 | logfopen(ctx); |
| 31 | if (ctx->lgfp) |
| 32 | fputc(c, ctx->lgfp); |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | /* |
| 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 | /* |
| 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. |
| 55 | */ |
| 56 | void log_eventlog(void *handle, const char *event) |
| 57 | { |
| 58 | struct LogContext *ctx = (struct LogContext *)handle; |
| 59 | if ((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)) { |
| 60 | fprintf(stderr, "%s\n", event); |
| 61 | fflush(stderr); |
| 62 | } |
| 63 | if (ctx->cfg.logtype != LGTYP_PACKETS) |
| 64 | return; |
| 65 | if (!ctx->lgfp) |
| 66 | logfopen(ctx); |
| 67 | if (ctx->lgfp) |
| 68 | fprintf(ctx->lgfp, "Event Log: %s\r\n", event); |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * Log an SSH packet. |
| 73 | * If n_blanks != 0, blank or omit some parts. |
| 74 | * Set of blanking areas must be in increasing order. |
| 75 | */ |
| 76 | void log_packet(void *handle, int direction, int type, |
| 77 | char *texttype, void *data, int len, |
| 78 | int n_blanks, const struct logblank_t *blanks) |
| 79 | { |
| 80 | struct LogContext *ctx = (struct LogContext *)handle; |
| 81 | char dumpdata[80], smalldata[5]; |
| 82 | |
| 83 | if (ctx->cfg.logtype != LGTYP_PACKETS) |
| 84 | return; |
| 85 | if (!ctx->lgfp) |
| 86 | logfopen(ctx); |
| 87 | if (ctx->lgfp) { |
| 88 | int p = 0, b = 0, omitted = 0; |
| 89 | int output_pos = 0; /* NZ if pending output in dumpdata */ |
| 90 | |
| 91 | /* Packet header. */ |
| 92 | fprintf(ctx->lgfp, "%s packet type %d / 0x%02x (%s)\r\n", |
| 93 | direction == PKT_INCOMING ? "Incoming" : "Outgoing", |
| 94 | type, type, texttype); |
| 95 | |
| 96 | /* |
| 97 | * Output a hex/ASCII dump of the packet body, blanking/omitting |
| 98 | * parts as specified. |
| 99 | */ |
| 100 | while (p < len) { |
| 101 | int blktype; |
| 102 | |
| 103 | /* Move to a current entry in the blanking array. */ |
| 104 | while ((b < n_blanks) && |
| 105 | (p >= blanks[b].offset + blanks[b].len)) |
| 106 | b++; |
| 107 | /* Work out what type of blanking to apply to |
| 108 | * this byte. */ |
| 109 | blktype = PKTLOG_EMIT; /* default */ |
| 110 | if ((b < n_blanks) && |
| 111 | (p >= blanks[b].offset) && |
| 112 | (p < blanks[b].offset + blanks[b].len)) |
| 113 | blktype = blanks[b].type; |
| 114 | |
| 115 | /* If we're about to stop omitting, it's time to say how |
| 116 | * much we omitted. */ |
| 117 | if ((blktype != PKTLOG_OMIT) && omitted) { |
| 118 | fprintf(ctx->lgfp, " (%d byte%s omitted)\r\n", |
| 119 | omitted, (omitted==1?"":"s")); |
| 120 | omitted = 0; |
| 121 | } |
| 122 | |
| 123 | /* (Re-)initialise dumpdata as necessary |
| 124 | * (start of row, or if we've just stopped omitting) */ |
| 125 | if (!output_pos && !omitted) |
| 126 | sprintf(dumpdata, " %08x%*s\r\n", p-(p%16), 1+3*16+2+16, ""); |
| 127 | |
| 128 | /* Deal with the current byte. */ |
| 129 | if (blktype == PKTLOG_OMIT) { |
| 130 | omitted++; |
| 131 | } else { |
| 132 | int c; |
| 133 | if (blktype == PKTLOG_BLANK) { |
| 134 | c = 'X'; |
| 135 | sprintf(smalldata, "XX"); |
| 136 | } else { /* PKTLOG_EMIT */ |
| 137 | c = ((unsigned char *)data)[p]; |
| 138 | sprintf(smalldata, "%02x", c); |
| 139 | } |
| 140 | dumpdata[10+2+3*(p%16)] = smalldata[0]; |
| 141 | dumpdata[10+2+3*(p%16)+1] = smalldata[1]; |
| 142 | dumpdata[10+1+3*16+2+(p%16)] = (isprint(c) ? c : '.'); |
| 143 | output_pos = (p%16) + 1; |
| 144 | } |
| 145 | |
| 146 | p++; |
| 147 | |
| 148 | /* Flush row if necessary */ |
| 149 | if (((p % 16) == 0) || (p == len) || omitted) { |
| 150 | if (output_pos) { |
| 151 | strcpy(dumpdata + 10+1+3*16+2+output_pos, "\r\n"); |
| 152 | fputs(dumpdata, ctx->lgfp); |
| 153 | output_pos = 0; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | } |
| 158 | |
| 159 | /* Tidy up */ |
| 160 | if (omitted) |
| 161 | fprintf(ctx->lgfp, " (%d byte%s omitted)\r\n", |
| 162 | omitted, (omitted==1?"":"s")); |
| 163 | fflush(ctx->lgfp); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /* open log file append/overwrite mode */ |
| 168 | void logfopen(void *handle) |
| 169 | { |
| 170 | struct LogContext *ctx = (struct LogContext *)handle; |
| 171 | char buf[256]; |
| 172 | struct tm tm; |
| 173 | char writemod[4]; |
| 174 | |
| 175 | /* Prevent repeat calls */ |
| 176 | if (ctx->lgfp) |
| 177 | return; |
| 178 | |
| 179 | if (!ctx->cfg.logtype) |
| 180 | return; |
| 181 | sprintf(writemod, "wb"); /* default to rewrite */ |
| 182 | |
| 183 | tm = ltime(); |
| 184 | |
| 185 | /* substitute special codes in file name */ |
| 186 | xlatlognam(&ctx->currlogfilename, ctx->cfg.logfilename,ctx->cfg.host, &tm); |
| 187 | |
| 188 | ctx->lgfp = f_open(ctx->currlogfilename, "r"); /* file already present? */ |
| 189 | if (ctx->lgfp) { |
| 190 | int i; |
| 191 | fclose(ctx->lgfp); |
| 192 | if (ctx->cfg.logxfovr != LGXF_ASK) { |
| 193 | i = ((ctx->cfg.logxfovr == LGXF_OVR) ? 2 : 1); |
| 194 | } else |
| 195 | i = askappend(ctx->frontend, ctx->currlogfilename); |
| 196 | if (i == 1) |
| 197 | writemod[0] = 'a'; /* set append mode */ |
| 198 | else if (i == 0) { /* cancelled */ |
| 199 | ctx->lgfp = NULL; |
| 200 | ctx->cfg.logtype = 0; /* disable logging */ |
| 201 | return; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | ctx->lgfp = f_open(ctx->currlogfilename, writemod); |
| 206 | if (ctx->lgfp) { /* enter into event log */ |
| 207 | /* --- write header line into log file */ |
| 208 | fputs("=~=~=~=~=~=~=~=~=~=~=~= PuTTY log ", ctx->lgfp); |
| 209 | strftime(buf, 24, "%Y.%m.%d %H:%M:%S", &tm); |
| 210 | fputs(buf, ctx->lgfp); |
| 211 | fputs(" =~=~=~=~=~=~=~=~=~=~=~=\r\n", ctx->lgfp); |
| 212 | |
| 213 | sprintf(buf, "%s session log (%s mode) to file: ", |
| 214 | (writemod[0] == 'a') ? "Appending" : "Writing new", |
| 215 | (ctx->cfg.logtype == LGTYP_ASCII ? "ASCII" : |
| 216 | ctx->cfg.logtype == LGTYP_DEBUG ? "raw" : |
| 217 | ctx->cfg.logtype == LGTYP_PACKETS ? "SSH packets" : "<ukwn>")); |
| 218 | /* Make sure we do not exceed the output buffer size */ |
| 219 | strncat(buf, filename_to_str(&ctx->currlogfilename), 128); |
| 220 | buf[strlen(buf)] = '\0'; |
| 221 | logevent(ctx->frontend, buf); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | void logfclose(void *handle) |
| 226 | { |
| 227 | struct LogContext *ctx = (struct LogContext *)handle; |
| 228 | if (ctx->lgfp) { |
| 229 | fclose(ctx->lgfp); |
| 230 | ctx->lgfp = NULL; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | void *log_init(void *frontend, Config *cfg) |
| 235 | { |
| 236 | struct LogContext *ctx = snew(struct LogContext); |
| 237 | ctx->lgfp = NULL; |
| 238 | ctx->frontend = frontend; |
| 239 | ctx->cfg = *cfg; /* STRUCTURE COPY */ |
| 240 | return ctx; |
| 241 | } |
| 242 | |
| 243 | void log_free(void *handle) |
| 244 | { |
| 245 | struct LogContext *ctx = (struct LogContext *)handle; |
| 246 | |
| 247 | logfclose(ctx); |
| 248 | sfree(ctx); |
| 249 | } |
| 250 | |
| 251 | void log_reconfig(void *handle, Config *cfg) |
| 252 | { |
| 253 | struct LogContext *ctx = (struct LogContext *)handle; |
| 254 | int reset_logging; |
| 255 | |
| 256 | if (!filename_equal(ctx->cfg.logfilename, cfg->logfilename) || |
| 257 | ctx->cfg.logtype != cfg->logtype) |
| 258 | reset_logging = TRUE; |
| 259 | else |
| 260 | reset_logging = FALSE; |
| 261 | |
| 262 | if (reset_logging) |
| 263 | logfclose(ctx); |
| 264 | |
| 265 | ctx->cfg = *cfg; /* STRUCTURE COPY */ |
| 266 | |
| 267 | if (reset_logging) |
| 268 | logfopen(ctx); |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * translate format codes into time/date strings |
| 273 | * and insert them into log file name |
| 274 | * |
| 275 | * "&Y":YYYY "&m":MM "&d":DD "&T":hhmm "&h":<hostname> "&&":& |
| 276 | */ |
| 277 | static void xlatlognam(Filename *dest, Filename src, |
| 278 | char *hostname, struct tm *tm) { |
| 279 | char buf[10], *bufp; |
| 280 | int size; |
| 281 | char buffer[FILENAME_MAX]; |
| 282 | int len = sizeof(buffer)-1; |
| 283 | char *d; |
| 284 | const char *s; |
| 285 | |
| 286 | d = buffer; |
| 287 | s = filename_to_str(&src); |
| 288 | |
| 289 | while (*s) { |
| 290 | /* Let (bufp, len) be the string to append. */ |
| 291 | bufp = buf; /* don't usually override this */ |
| 292 | if (*s == '&') { |
| 293 | char c; |
| 294 | s++; |
| 295 | size = 0; |
| 296 | if (*s) switch (c = *s++, tolower(c)) { |
| 297 | case 'y': |
| 298 | size = strftime(buf, sizeof(buf), "%Y", tm); |
| 299 | break; |
| 300 | case 'm': |
| 301 | size = strftime(buf, sizeof(buf), "%m", tm); |
| 302 | break; |
| 303 | case 'd': |
| 304 | size = strftime(buf, sizeof(buf), "%d", tm); |
| 305 | break; |
| 306 | case 't': |
| 307 | size = strftime(buf, sizeof(buf), "%H%M%S", tm); |
| 308 | break; |
| 309 | case 'h': |
| 310 | bufp = hostname; |
| 311 | size = strlen(bufp); |
| 312 | break; |
| 313 | default: |
| 314 | buf[0] = '&'; |
| 315 | size = 1; |
| 316 | if (c != '&') |
| 317 | buf[size++] = c; |
| 318 | } |
| 319 | } else { |
| 320 | buf[0] = *s++; |
| 321 | size = 1; |
| 322 | } |
| 323 | if (size > len) |
| 324 | size = len; |
| 325 | memcpy(d, bufp, size); |
| 326 | d += size; |
| 327 | len -= size; |
| 328 | } |
| 329 | *d = '\0'; |
| 330 | |
| 331 | *dest = filename_from_str(buffer); |
| 332 | } |