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