Each platform's implementation of askappend() is no longer required
[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; 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.
44 */
45 void log_eventlog(void *handle, char *event)
46 {
47 struct LogContext *ctx = (struct LogContext *)handle;
48 if ((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)) {
49 fprintf(stderr, "%s\n", event);
50 fflush(stderr);
51 }
52 if (cfg.logtype != LGTYP_PACKETS)
53 return;
54 if (!ctx->lgfp)
55 logfopen(ctx);
56 if (ctx->lgfp)
57 fprintf(ctx->lgfp, "Event Log: %s\r\n", event);
58 }
59
60 /*
61 * Log an SSH packet.
62 */
63 void log_packet(void *handle, int direction, int type,
64 char *texttype, void *data, int len)
65 {
66 struct LogContext *ctx = (struct LogContext *)handle;
67 int i, j;
68 char dumpdata[80], smalldata[5];
69
70 if (cfg.logtype != LGTYP_PACKETS)
71 return;
72 if (!ctx->lgfp)
73 logfopen(ctx);
74 if (ctx->lgfp) {
75 fprintf(ctx->lgfp, "%s packet type %d / 0x%02x (%s)\r\n",
76 direction == PKT_INCOMING ? "Incoming" : "Outgoing",
77 type, type, texttype);
78 for (i = 0; i < len; i += 16) {
79 sprintf(dumpdata, " %08x%*s\r\n", i, 1+3*16+2+16, "");
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 }
87 strcpy(dumpdata + 10+1+3*16+2+j, "\r\n");
88 fputs(dumpdata, ctx->lgfp);
89 }
90 fflush(ctx->lgfp);
91 }
92 }
93
94 /* open log file append/overwrite mode */
95 void logfopen(void *handle)
96 {
97 struct LogContext *ctx = (struct LogContext *)handle;
98 char buf[256];
99 time_t t;
100 struct tm tm;
101 char writemod[4];
102
103 /* Prevent repeat calls */
104 if (ctx->lgfp)
105 return;
106
107 if (!cfg.logtype)
108 return;
109 sprintf(writemod, "wb"); /* default to rewrite */
110
111 time(&t);
112 tm = *localtime(&t);
113
114 /* substitute special codes in file name */
115 xlatlognam(ctx->currlogfilename, cfg.logfilename,cfg.host, &tm);
116
117 ctx->lgfp = fopen(ctx->currlogfilename, "r"); /* file already present? */
118 if (ctx->lgfp) {
119 int i;
120 fclose(ctx->lgfp);
121 if (cfg.logxfovr != LGXF_ASK) {
122 i = ((cfg.logxfovr == LGXF_OVR) ? 2 : 1);
123 } else
124 i = askappend(ctx->frontend, ctx->currlogfilename);
125 if (i == 1)
126 writemod[0] = 'a'; /* set append mode */
127 else if (i == 0) { /* cancelled */
128 ctx->lgfp = NULL;
129 cfg.logtype = 0; /* disable logging */
130 return;
131 }
132 }
133
134 ctx->lgfp = fopen(ctx->currlogfilename, writemod);
135 if (ctx->lgfp) { /* enter into event log */
136 /* --- write header line into log file */
137 fputs("=~=~=~=~=~=~=~=~=~=~=~= PuTTY log ", ctx->lgfp);
138 strftime(buf, 24, "%Y.%m.%d %H:%M:%S", &tm);
139 fputs(buf, ctx->lgfp);
140 fputs(" =~=~=~=~=~=~=~=~=~=~=~=\r\n", ctx->lgfp);
141
142 sprintf(buf, "%s session log (%s mode) to file: ",
143 (writemod[0] == 'a') ? "Appending" : "Writing new",
144 (cfg.logtype == LGTYP_ASCII ? "ASCII" :
145 cfg.logtype == LGTYP_DEBUG ? "raw" :
146 cfg.logtype == LGTYP_PACKETS ? "SSH packets" : "<ukwn>"));
147 /* Make sure we do not exceed the output buffer size */
148 strncat(buf, ctx->currlogfilename, 128);
149 buf[strlen(buf)] = '\0';
150 logevent(ctx->frontend, buf);
151 }
152 }
153
154 void logfclose(void *handle)
155 {
156 struct LogContext *ctx = (struct LogContext *)handle;
157 if (ctx->lgfp) {
158 fclose(ctx->lgfp);
159 ctx->lgfp = NULL;
160 }
161 }
162
163 void *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
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 */
177 static void xlatlognam(char *d, char *s, char *hostname, struct tm *tm) {
178 char buf[10], *bufp;
179 int size;
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++;
188 size = 0;
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 }