The Unicode module no longer depends on `cfg', since it gets the
[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/*
382908ad 37 * Log an Event Log entry. Used in SSH packet logging mode; this is
38 * also as convenient a place as any to put the output of Event Log
39 * entries to stderr when a command-line tool is in verbose mode.
40 * (In particular, this is a better place to put it than in the
41 * front ends, because it only has to be done once for all
42 * platforms. Platforms which don't have a meaningful stderr can
43 * just avoid defining FLAG_STDERR.
fb89f7ff 44 */
a8327734 45void log_eventlog(void *handle, char *event)
fb89f7ff 46{
a8327734 47 struct LogContext *ctx = (struct LogContext *)handle;
382908ad 48 if ((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)) {
49 fprintf(stderr, "%s\n", event);
50 fflush(stderr);
51 }
fb89f7ff 52 if (cfg.logtype != LGTYP_PACKETS)
53 return;
a8327734 54 if (!ctx->lgfp)
55 logfopen(ctx);
56 if (ctx->lgfp)
fa76f4a3 57 fprintf(ctx->lgfp, "Event Log: %s\r\n", event);
fb89f7ff 58}
59
60/*
00db133f 61 * Log an SSH packet.
62 */
a8327734 63void log_packet(void *handle, int direction, int type,
64 char *texttype, void *data, int len)
00db133f 65{
a8327734 66 struct LogContext *ctx = (struct LogContext *)handle;
fb89f7ff 67 int i, j;
00db133f 68 char dumpdata[80], smalldata[5];
69
70 if (cfg.logtype != LGTYP_PACKETS)
71 return;
a8327734 72 if (!ctx->lgfp)
73 logfopen(ctx);
74 if (ctx->lgfp) {
fa76f4a3 75 fprintf(ctx->lgfp, "%s packet type %d / 0x%02x (%s)\r\n",
00db133f 76 direction == PKT_INCOMING ? "Incoming" : "Outgoing",
77 type, type, texttype);
78 for (i = 0; i < len; i += 16) {
fa76f4a3 79 sprintf(dumpdata, " %08x%*s\r\n", i, 1+3*16+2+16, "");
00db133f 80 for (j = 0; j < 16 && i+j < len; j++) {
81 int c = ((unsigned char *)data)[i+j];
82 sprintf(smalldata, "%02x", c);
83 dumpdata[10+2+3*j] = smalldata[0];
84 dumpdata[10+2+3*j+1] = smalldata[1];
85 dumpdata[10+1+3*16+2+j] = (isprint(c) ? c : '.');
86 }
fa76f4a3 87 strcpy(dumpdata + 10+1+3*16+2+j, "\r\n");
a8327734 88 fputs(dumpdata, ctx->lgfp);
00db133f 89 }
a8327734 90 fflush(ctx->lgfp);
00db133f 91 }
92}
93
94/* open log file append/overwrite mode */
a8327734 95void logfopen(void *handle)
00db133f 96{
a8327734 97 struct LogContext *ctx = (struct LogContext *)handle;
00db133f 98 char buf[256];
99 time_t t;
100 struct tm tm;
101 char writemod[4];
102
fb89f7ff 103 /* Prevent repeat calls */
a8327734 104 if (ctx->lgfp)
fb89f7ff 105 return;
106
00db133f 107 if (!cfg.logtype)
108 return;
fa76f4a3 109 sprintf(writemod, "wb"); /* default to rewrite */
00db133f 110
111 time(&t);
112 tm = *localtime(&t);
113
114 /* substitute special codes in file name */
a8327734 115 xlatlognam(ctx->currlogfilename, cfg.logfilename,cfg.host, &tm);
00db133f 116
a8327734 117 ctx->lgfp = fopen(ctx->currlogfilename, "r"); /* file already present? */
118 if (ctx->lgfp) {
00db133f 119 int i;
a8327734 120 fclose(ctx->lgfp);
bf61b566 121 if (cfg.logxfovr != LGXF_ASK) {
122 i = ((cfg.logxfovr == LGXF_OVR) ? 2 : 1);
123 } else
124 i = askappend(ctx->frontend, ctx->currlogfilename);
00db133f 125 if (i == 1)
126 writemod[0] = 'a'; /* set append mode */
127 else if (i == 0) { /* cancelled */
a8327734 128 ctx->lgfp = NULL;
00db133f 129 cfg.logtype = 0; /* disable logging */
130 return;
131 }
132 }
133
a8327734 134 ctx->lgfp = fopen(ctx->currlogfilename, writemod);
135 if (ctx->lgfp) { /* enter into event log */
fb89f7ff 136 /* --- write header line into log file */
a8327734 137 fputs("=~=~=~=~=~=~=~=~=~=~=~= PuTTY log ", ctx->lgfp);
fb89f7ff 138 strftime(buf, 24, "%Y.%m.%d %H:%M:%S", &tm);
a8327734 139 fputs(buf, ctx->lgfp);
fa76f4a3 140 fputs(" =~=~=~=~=~=~=~=~=~=~=~=\r\n", ctx->lgfp);
fb89f7ff 141
142 sprintf(buf, "%s session log (%s mode) to file: ",
00db133f 143 (writemod[0] == 'a') ? "Appending" : "Writing new",
144 (cfg.logtype == LGTYP_ASCII ? "ASCII" :
fb89f7ff 145 cfg.logtype == LGTYP_DEBUG ? "raw" :
146 cfg.logtype == LGTYP_PACKETS ? "SSH packets" : "<ukwn>"));
00db133f 147 /* Make sure we do not exceed the output buffer size */
a8327734 148 strncat(buf, ctx->currlogfilename, 128);
00db133f 149 buf[strlen(buf)] = '\0';
a8327734 150 logevent(ctx->frontend, buf);
00db133f 151 }
152}
153
a8327734 154void logfclose(void *handle)
00db133f 155{
a8327734 156 struct LogContext *ctx = (struct LogContext *)handle;
157 if (ctx->lgfp) {
158 fclose(ctx->lgfp);
159 ctx->lgfp = NULL;
00db133f 160 }
161}
162
a8327734 163void *log_init(void *frontend)
164{
165 struct LogContext *ctx = smalloc(sizeof(struct LogContext));
166 ctx->lgfp = NULL;
167 ctx->frontend = frontend;
168 return ctx;
169}
170
00db133f 171/*
172 * translate format codes into time/date strings
173 * and insert them into log file name
174 *
175 * "&Y":YYYY "&m":MM "&d":DD "&T":hhmm "&h":<hostname> "&&":&
176 */
177static void xlatlognam(char *d, char *s, char *hostname, struct tm *tm) {
178 char buf[10], *bufp;
179 int size;
00db133f 180 int len = FILENAME_MAX-1;
181
182 while (*s) {
183 /* Let (bufp, len) be the string to append. */
184 bufp = buf; /* don't usually override this */
185 if (*s == '&') {
186 char c;
187 s++;
1709795f 188 size = 0;
00db133f 189 if (*s) switch (c = *s++, tolower(c)) {
190 case 'y':
191 size = strftime(buf, sizeof(buf), "%Y", tm);
192 break;
193 case 'm':
194 size = strftime(buf, sizeof(buf), "%m", tm);
195 break;
196 case 'd':
197 size = strftime(buf, sizeof(buf), "%d", tm);
198 break;
199 case 't':
200 size = strftime(buf, sizeof(buf), "%H%M%S", tm);
201 break;
202 case 'h':
203 bufp = hostname;
204 size = strlen(bufp);
205 break;
206 default:
207 buf[0] = '&';
208 size = 1;
209 if (c != '&')
210 buf[size++] = c;
211 }
212 } else {
213 buf[0] = *s++;
214 size = 1;
215 }
216 if (size > len)
217 size = len;
218 memcpy(d, bufp, size);
219 d += size;
220 len -= size;
221 }
222 *d = '\0';
223}