| 1 | #include <windows.h> |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <ctype.h> |
| 6 | |
| 7 | #include <time.h> |
| 8 | #include <assert.h> |
| 9 | |
| 10 | #include "putty.h" |
| 11 | |
| 12 | /* log session to file stuff ... */ |
| 13 | static FILE *lgfp = NULL; |
| 14 | static char timdatbuf[20]; |
| 15 | static char currlogfilename[FILENAME_MAX]; |
| 16 | |
| 17 | static void xlatlognam(char *d, char *s, char *hostname, struct tm *tm); |
| 18 | |
| 19 | /* |
| 20 | * Log session traffic. |
| 21 | */ |
| 22 | void logtraffic(unsigned char c, int logmode) |
| 23 | { |
| 24 | if (cfg.logtype > 0) { |
| 25 | if (cfg.logtype == logmode) { |
| 26 | /* deferred open file from pgm start? */ |
| 27 | if (!lgfp) |
| 28 | logfopen(); |
| 29 | if (lgfp) |
| 30 | fputc(c, lgfp); |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | /* |
| 36 | * Log an SSH packet. |
| 37 | */ |
| 38 | void log_packet(int direction, int type, char *texttype, void *data, int len) |
| 39 | { |
| 40 | int i, j, c; |
| 41 | char dumpdata[80], smalldata[5]; |
| 42 | |
| 43 | if (cfg.logtype != LGTYP_PACKETS) |
| 44 | return; |
| 45 | if (!lgfp) |
| 46 | logfopen(); |
| 47 | if (lgfp) { |
| 48 | fprintf(lgfp, "%s packet type %d / 0x%02x (%s)\n", |
| 49 | direction == PKT_INCOMING ? "Incoming" : "Outgoing", |
| 50 | type, type, texttype); |
| 51 | for (i = 0; i < len; i += 16) { |
| 52 | sprintf(dumpdata, " %08x%*s\n", i, 1+3*16+2+16, ""); |
| 53 | for (j = 0; j < 16 && i+j < len; j++) { |
| 54 | int c = ((unsigned char *)data)[i+j]; |
| 55 | sprintf(smalldata, "%02x", c); |
| 56 | dumpdata[10+2+3*j] = smalldata[0]; |
| 57 | dumpdata[10+2+3*j+1] = smalldata[1]; |
| 58 | dumpdata[10+1+3*16+2+j] = (isprint(c) ? c : '.'); |
| 59 | } |
| 60 | strcpy(dumpdata + 10+1+3*16+2+j, "\n"); |
| 61 | fputs(dumpdata, lgfp); |
| 62 | } |
| 63 | fflush(lgfp); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /* open log file append/overwrite mode */ |
| 68 | void logfopen(void) |
| 69 | { |
| 70 | char buf[256]; |
| 71 | time_t t; |
| 72 | struct tm tm; |
| 73 | char writemod[4]; |
| 74 | |
| 75 | if (!cfg.logtype) |
| 76 | return; |
| 77 | sprintf(writemod, "wb"); /* default to rewrite */ |
| 78 | |
| 79 | time(&t); |
| 80 | tm = *localtime(&t); |
| 81 | |
| 82 | /* substitute special codes in file name */ |
| 83 | xlatlognam(currlogfilename,cfg.logfilename,cfg.host, &tm); |
| 84 | |
| 85 | lgfp = fopen(currlogfilename, "r"); /* file already present? */ |
| 86 | if (lgfp) { |
| 87 | int i; |
| 88 | fclose(lgfp); |
| 89 | i = askappend(currlogfilename); |
| 90 | if (i == 1) |
| 91 | writemod[0] = 'a'; /* set append mode */ |
| 92 | else if (i == 0) { /* cancelled */ |
| 93 | lgfp = NULL; |
| 94 | cfg.logtype = 0; /* disable logging */ |
| 95 | return; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | lgfp = fopen(currlogfilename, writemod); |
| 100 | if (lgfp) { /* enter into event log */ |
| 101 | sprintf(buf, "%s session log (%s mode) to file : ", |
| 102 | (writemod[0] == 'a') ? "Appending" : "Writing new", |
| 103 | (cfg.logtype == LGTYP_ASCII ? "ASCII" : |
| 104 | cfg.logtype == LGTYP_DEBUG ? "raw" : "<ukwn>")); |
| 105 | /* Make sure we do not exceed the output buffer size */ |
| 106 | strncat(buf, currlogfilename, 128); |
| 107 | buf[strlen(buf)] = '\0'; |
| 108 | logevent(buf); |
| 109 | |
| 110 | /* --- write header line into log file */ |
| 111 | fputs("=~=~=~=~=~=~=~=~=~=~=~= PuTTY log ", lgfp); |
| 112 | strftime(buf, 24, "%Y.%m.%d %H:%M:%S", &tm); |
| 113 | fputs(buf, lgfp); |
| 114 | fputs(" =~=~=~=~=~=~=~=~=~=~=~=\r\n", lgfp); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void logfclose(void) |
| 119 | { |
| 120 | if (lgfp) { |
| 121 | fclose(lgfp); |
| 122 | lgfp = NULL; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * translate format codes into time/date strings |
| 128 | * and insert them into log file name |
| 129 | * |
| 130 | * "&Y":YYYY "&m":MM "&d":DD "&T":hhmm "&h":<hostname> "&&":& |
| 131 | */ |
| 132 | static void xlatlognam(char *d, char *s, char *hostname, struct tm *tm) { |
| 133 | char buf[10], *bufp; |
| 134 | int size; |
| 135 | char *ds = d; /* save start pos. */ |
| 136 | int len = FILENAME_MAX-1; |
| 137 | |
| 138 | while (*s) { |
| 139 | /* Let (bufp, len) be the string to append. */ |
| 140 | bufp = buf; /* don't usually override this */ |
| 141 | if (*s == '&') { |
| 142 | char c; |
| 143 | s++; |
| 144 | if (*s) switch (c = *s++, tolower(c)) { |
| 145 | case 'y': |
| 146 | size = strftime(buf, sizeof(buf), "%Y", tm); |
| 147 | break; |
| 148 | case 'm': |
| 149 | size = strftime(buf, sizeof(buf), "%m", tm); |
| 150 | break; |
| 151 | case 'd': |
| 152 | size = strftime(buf, sizeof(buf), "%d", tm); |
| 153 | break; |
| 154 | case 't': |
| 155 | size = strftime(buf, sizeof(buf), "%H%M%S", tm); |
| 156 | break; |
| 157 | case 'h': |
| 158 | bufp = hostname; |
| 159 | size = strlen(bufp); |
| 160 | break; |
| 161 | default: |
| 162 | buf[0] = '&'; |
| 163 | size = 1; |
| 164 | if (c != '&') |
| 165 | buf[size++] = c; |
| 166 | } |
| 167 | } else { |
| 168 | buf[0] = *s++; |
| 169 | size = 1; |
| 170 | } |
| 171 | if (size > len) |
| 172 | size = len; |
| 173 | memcpy(d, bufp, size); |
| 174 | d += size; |
| 175 | len -= size; |
| 176 | } |
| 177 | *d = '\0'; |
| 178 | } |