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