Add the ability to close sessions. This adds *_free() functions to most
[u/mdw/putty] / logging.c
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 char currlogfilename[FILENAME_MAX];
14 void *frontend;
15 Config cfg;
16 };
17
18 static void xlatlognam(char *d, char *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 * 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.
45 */
46 void log_eventlog(void *handle, char *event)
47 {
48 struct LogContext *ctx = (struct LogContext *)handle;
49 if ((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)) {
50 fprintf(stderr, "%s\n", event);
51 fflush(stderr);
52 }
53 if (ctx->cfg.logtype != LGTYP_PACKETS)
54 return;
55 if (!ctx->lgfp)
56 logfopen(ctx);
57 if (ctx->lgfp)
58 fprintf(ctx->lgfp, "Event Log: %s\r\n", event);
59 }
60
61 /*
62 * Log an SSH packet.
63 */
64 void log_packet(void *handle, int direction, int type,
65 char *texttype, void *data, int len)
66 {
67 struct LogContext *ctx = (struct LogContext *)handle;
68 int i, j;
69 char dumpdata[80], smalldata[5];
70
71 if (ctx->cfg.logtype != LGTYP_PACKETS)
72 return;
73 if (!ctx->lgfp)
74 logfopen(ctx);
75 if (ctx->lgfp) {
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 : '.');
87 }
88 strcpy(dumpdata + 10+1+3*16+2+j, "\r\n");
89 fputs(dumpdata, ctx->lgfp);
90 }
91 fflush(ctx->lgfp);
92 }
93 }
94
95 /* open log file append/overwrite mode */
96 void logfopen(void *handle)
97 {
98 struct LogContext *ctx = (struct LogContext *)handle;
99 char buf[256];
100 time_t t;
101 struct tm tm;
102 char writemod[4];
103
104 /* Prevent repeat calls */
105 if (ctx->lgfp)
106 return;
107
108 if (!ctx->cfg.logtype)
109 return;
110 sprintf(writemod, "wb"); /* default to rewrite */
111
112 time(&t);
113 tm = *localtime(&t);
114
115 /* substitute special codes in file name */
116 xlatlognam(ctx->currlogfilename, ctx->cfg.logfilename,ctx->cfg.host, &tm);
117
118 ctx->lgfp = fopen(ctx->currlogfilename, "r"); /* file already present? */
119 if (ctx->lgfp) {
120 int i;
121 fclose(ctx->lgfp);
122 if (ctx->cfg.logxfovr != LGXF_ASK) {
123 i = ((ctx->cfg.logxfovr == LGXF_OVR) ? 2 : 1);
124 } else
125 i = askappend(ctx->frontend, ctx->currlogfilename);
126 if (i == 1)
127 writemod[0] = 'a'; /* set append mode */
128 else if (i == 0) { /* cancelled */
129 ctx->lgfp = NULL;
130 ctx->cfg.logtype = 0; /* disable logging */
131 return;
132 }
133 }
134
135 ctx->lgfp = fopen(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);
142
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, ctx->currlogfilename, 128);
150 buf[strlen(buf)] = '\0';
151 logevent(ctx->frontend, buf);
152 }
153 }
154
155 void logfclose(void *handle)
156 {
157 struct LogContext *ctx = (struct LogContext *)handle;
158 if (ctx->lgfp) {
159 fclose(ctx->lgfp);
160 ctx->lgfp = NULL;
161 }
162 }
163
164 void *log_init(void *frontend, Config *cfg)
165 {
166 struct LogContext *ctx = smalloc(sizeof(struct LogContext));
167 ctx->lgfp = NULL;
168 ctx->frontend = frontend;
169 ctx->cfg = *cfg; /* STRUCTURE COPY */
170 return ctx;
171 }
172
173 void log_free(void *handle)
174 {
175 struct LogContext *ctx = (struct LogContext *)handle;
176
177 logfclose(ctx);
178 sfree(ctx);
179 }
180
181 void log_reconfig(void *handle, Config *cfg)
182 {
183 struct LogContext *ctx = (struct LogContext *)handle;
184 int reset_logging;
185
186 if (strcmp(ctx->cfg.logfilename, cfg->logfilename) ||
187 ctx->cfg.logtype != cfg->logtype)
188 reset_logging = TRUE;
189 else
190 reset_logging = FALSE;
191
192 if (reset_logging)
193 logfclose(ctx);
194
195 ctx->cfg = *cfg; /* STRUCTURE COPY */
196
197 if (reset_logging)
198 logfopen(ctx);
199 }
200
201 /*
202 * translate format codes into time/date strings
203 * and insert them into log file name
204 *
205 * "&Y":YYYY "&m":MM "&d":DD "&T":hhmm "&h":<hostname> "&&":&
206 */
207 static void xlatlognam(char *d, char *s, char *hostname, struct tm *tm) {
208 char buf[10], *bufp;
209 int size;
210 int len = FILENAME_MAX-1;
211
212 while (*s) {
213 /* Let (bufp, len) be the string to append. */
214 bufp = buf; /* don't usually override this */
215 if (*s == '&') {
216 char c;
217 s++;
218 size = 0;
219 if (*s) switch (c = *s++, tolower(c)) {
220 case 'y':
221 size = strftime(buf, sizeof(buf), "%Y", tm);
222 break;
223 case 'm':
224 size = strftime(buf, sizeof(buf), "%m", tm);
225 break;
226 case 'd':
227 size = strftime(buf, sizeof(buf), "%d", tm);
228 break;
229 case 't':
230 size = strftime(buf, sizeof(buf), "%H%M%S", tm);
231 break;
232 case 'h':
233 bufp = hostname;
234 size = strlen(bufp);
235 break;
236 default:
237 buf[0] = '&';
238 size = 1;
239 if (c != '&')
240 buf[size++] = c;
241 }
242 } else {
243 buf[0] = *s++;
244 size = 1;
245 }
246 if (size > len)
247 size = len;
248 memcpy(d, bufp, size);
249 d += size;
250 len -= size;
251 }
252 *d = '\0';
253 }