For some reason Roman Pompejus' original logging patch opened the log in
[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 };
16
17 static void xlatlognam(char *d, char *s, char *hostname, struct tm *tm);
18
19 /*
20 * Log session traffic.
21 */
22 void logtraffic(void *handle, unsigned char c, int logmode)
23 {
24 struct LogContext *ctx = (struct LogContext *)handle;
25 if (cfg.logtype > 0) {
26 if (cfg.logtype == logmode) {
27 /* deferred open file from pgm start? */
28 if (!ctx->lgfp)
29 logfopen(ctx);
30 if (ctx->lgfp)
31 fputc(c, ctx->lgfp);
32 }
33 }
34 }
35
36 /*
37 * Log an Event Log entry (used in SSH packet logging mode).
38 */
39 void log_eventlog(void *handle, char *event)
40 {
41 struct LogContext *ctx = (struct LogContext *)handle;
42 if (cfg.logtype != LGTYP_PACKETS)
43 return;
44 if (!ctx->lgfp)
45 logfopen(ctx);
46 if (ctx->lgfp)
47 fprintf(ctx->lgfp, "Event Log: %s\n", event);
48 }
49
50 /*
51 * Log an SSH packet.
52 */
53 void log_packet(void *handle, int direction, int type,
54 char *texttype, void *data, int len)
55 {
56 struct LogContext *ctx = (struct LogContext *)handle;
57 int i, j;
58 char dumpdata[80], smalldata[5];
59
60 if (cfg.logtype != LGTYP_PACKETS)
61 return;
62 if (!ctx->lgfp)
63 logfopen(ctx);
64 if (ctx->lgfp) {
65 fprintf(ctx->lgfp, "%s packet type %d / 0x%02x (%s)\n",
66 direction == PKT_INCOMING ? "Incoming" : "Outgoing",
67 type, type, texttype);
68 for (i = 0; i < len; i += 16) {
69 sprintf(dumpdata, " %08x%*s\n", i, 1+3*16+2+16, "");
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 }
77 strcpy(dumpdata + 10+1+3*16+2+j, "\n");
78 fputs(dumpdata, ctx->lgfp);
79 }
80 fflush(ctx->lgfp);
81 }
82 }
83
84 /* open log file append/overwrite mode */
85 void logfopen(void *handle)
86 {
87 struct LogContext *ctx = (struct LogContext *)handle;
88 char buf[256];
89 time_t t;
90 struct tm tm;
91 char writemod[4];
92
93 /* Prevent repeat calls */
94 if (ctx->lgfp)
95 return;
96
97 if (!cfg.logtype)
98 return;
99 sprintf(writemod, "w"); /* default to rewrite */
100
101 time(&t);
102 tm = *localtime(&t);
103
104 /* substitute special codes in file name */
105 xlatlognam(ctx->currlogfilename, cfg.logfilename,cfg.host, &tm);
106
107 ctx->lgfp = fopen(ctx->currlogfilename, "r"); /* file already present? */
108 if (ctx->lgfp) {
109 int i;
110 fclose(ctx->lgfp);
111 i = askappend(ctx->frontend, ctx->currlogfilename);
112 if (i == 1)
113 writemod[0] = 'a'; /* set append mode */
114 else if (i == 0) { /* cancelled */
115 ctx->lgfp = NULL;
116 cfg.logtype = 0; /* disable logging */
117 return;
118 }
119 }
120
121 ctx->lgfp = fopen(ctx->currlogfilename, writemod);
122 if (ctx->lgfp) { /* enter into event log */
123 /* --- write header line into log file */
124 fputs("=~=~=~=~=~=~=~=~=~=~=~= PuTTY log ", ctx->lgfp);
125 strftime(buf, 24, "%Y.%m.%d %H:%M:%S", &tm);
126 fputs(buf, ctx->lgfp);
127 fputs(" =~=~=~=~=~=~=~=~=~=~=~=\n", ctx->lgfp);
128
129 sprintf(buf, "%s session log (%s mode) to file: ",
130 (writemod[0] == 'a') ? "Appending" : "Writing new",
131 (cfg.logtype == LGTYP_ASCII ? "ASCII" :
132 cfg.logtype == LGTYP_DEBUG ? "raw" :
133 cfg.logtype == LGTYP_PACKETS ? "SSH packets" : "<ukwn>"));
134 /* Make sure we do not exceed the output buffer size */
135 strncat(buf, ctx->currlogfilename, 128);
136 buf[strlen(buf)] = '\0';
137 logevent(ctx->frontend, buf);
138 }
139 }
140
141 void logfclose(void *handle)
142 {
143 struct LogContext *ctx = (struct LogContext *)handle;
144 if (ctx->lgfp) {
145 fclose(ctx->lgfp);
146 ctx->lgfp = NULL;
147 }
148 }
149
150 void *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
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 */
164 static void xlatlognam(char *d, char *s, char *hostname, struct tm *tm) {
165 char buf[10], *bufp;
166 int size;
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++;
175 size = 0;
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 }