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