Richard points out that it would probably help if I committed
[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 ... */
11static FILE *lgfp = NULL;
00db133f 12static char currlogfilename[FILENAME_MAX];
13
14static void xlatlognam(char *d, char *s, char *hostname, struct tm *tm);
15
16/*
17 * Log session traffic.
18 */
19void logtraffic(unsigned char c, int logmode)
20{
21 if (cfg.logtype > 0) {
22 if (cfg.logtype == logmode) {
23 /* deferred open file from pgm start? */
24 if (!lgfp)
25 logfopen();
26 if (lgfp)
27 fputc(c, lgfp);
28 }
29 }
30}
31
32/*
fb89f7ff 33 * Log an Event Log entry (used in SSH packet logging mode).
34 */
35void log_eventlog(char *event)
36{
37 if (cfg.logtype != LGTYP_PACKETS)
38 return;
39 if (!lgfp)
40 logfopen();
41 if (lgfp)
42 fprintf(lgfp, "Event Log: %s\n", event);
43}
44
45/*
00db133f 46 * Log an SSH packet.
47 */
48void log_packet(int direction, int type, char *texttype, void *data, int len)
49{
fb89f7ff 50 int i, j;
00db133f 51 char dumpdata[80], smalldata[5];
52
53 if (cfg.logtype != LGTYP_PACKETS)
54 return;
55 if (!lgfp)
56 logfopen();
57 if (lgfp) {
58 fprintf(lgfp, "%s packet type %d / 0x%02x (%s)\n",
59 direction == PKT_INCOMING ? "Incoming" : "Outgoing",
60 type, type, texttype);
61 for (i = 0; i < len; i += 16) {
62 sprintf(dumpdata, " %08x%*s\n", i, 1+3*16+2+16, "");
63 for (j = 0; j < 16 && i+j < len; j++) {
64 int c = ((unsigned char *)data)[i+j];
65 sprintf(smalldata, "%02x", c);
66 dumpdata[10+2+3*j] = smalldata[0];
67 dumpdata[10+2+3*j+1] = smalldata[1];
68 dumpdata[10+1+3*16+2+j] = (isprint(c) ? c : '.');
69 }
70 strcpy(dumpdata + 10+1+3*16+2+j, "\n");
71 fputs(dumpdata, lgfp);
72 }
88693305 73 fflush(lgfp);
00db133f 74 }
75}
76
77/* open log file append/overwrite mode */
78void logfopen(void)
79{
80 char buf[256];
81 time_t t;
82 struct tm tm;
83 char writemod[4];
84
fb89f7ff 85 /* Prevent repeat calls */
86 if (lgfp)
87 return;
88
00db133f 89 if (!cfg.logtype)
90 return;
91 sprintf(writemod, "wb"); /* default to rewrite */
92
93 time(&t);
94 tm = *localtime(&t);
95
96 /* substitute special codes in file name */
97 xlatlognam(currlogfilename,cfg.logfilename,cfg.host, &tm);
98
99 lgfp = fopen(currlogfilename, "r"); /* file already present? */
100 if (lgfp) {
101 int i;
102 fclose(lgfp);
103 i = askappend(currlogfilename);
104 if (i == 1)
105 writemod[0] = 'a'; /* set append mode */
106 else if (i == 0) { /* cancelled */
107 lgfp = NULL;
108 cfg.logtype = 0; /* disable logging */
109 return;
110 }
111 }
112
113 lgfp = fopen(currlogfilename, writemod);
114 if (lgfp) { /* enter into event log */
fb89f7ff 115 /* --- write header line into log file */
116 fputs("=~=~=~=~=~=~=~=~=~=~=~= PuTTY log ", lgfp);
117 strftime(buf, 24, "%Y.%m.%d %H:%M:%S", &tm);
118 fputs(buf, lgfp);
119 fputs(" =~=~=~=~=~=~=~=~=~=~=~=\r\n", lgfp);
120
121 sprintf(buf, "%s session log (%s mode) to file: ",
00db133f 122 (writemod[0] == 'a') ? "Appending" : "Writing new",
123 (cfg.logtype == LGTYP_ASCII ? "ASCII" :
fb89f7ff 124 cfg.logtype == LGTYP_DEBUG ? "raw" :
125 cfg.logtype == LGTYP_PACKETS ? "SSH packets" : "<ukwn>"));
00db133f 126 /* Make sure we do not exceed the output buffer size */
127 strncat(buf, currlogfilename, 128);
128 buf[strlen(buf)] = '\0';
129 logevent(buf);
00db133f 130 }
131}
132
133void logfclose(void)
134{
135 if (lgfp) {
136 fclose(lgfp);
137 lgfp = NULL;
138 }
139}
140
141/*
142 * translate format codes into time/date strings
143 * and insert them into log file name
144 *
145 * "&Y":YYYY "&m":MM "&d":DD "&T":hhmm "&h":<hostname> "&&":&
146 */
147static void xlatlognam(char *d, char *s, char *hostname, struct tm *tm) {
148 char buf[10], *bufp;
149 int size;
00db133f 150 int len = FILENAME_MAX-1;
151
152 while (*s) {
153 /* Let (bufp, len) be the string to append. */
154 bufp = buf; /* don't usually override this */
155 if (*s == '&') {
156 char c;
157 s++;
1709795f 158 size = 0;
00db133f 159 if (*s) switch (c = *s++, tolower(c)) {
160 case 'y':
161 size = strftime(buf, sizeof(buf), "%Y", tm);
162 break;
163 case 'm':
164 size = strftime(buf, sizeof(buf), "%m", tm);
165 break;
166 case 'd':
167 size = strftime(buf, sizeof(buf), "%d", tm);
168 break;
169 case 't':
170 size = strftime(buf, sizeof(buf), "%H%M%S", tm);
171 break;
172 case 'h':
173 bufp = hostname;
174 size = strlen(bufp);
175 break;
176 default:
177 buf[0] = '&';
178 size = 1;
179 if (c != '&')
180 buf[size++] = c;
181 }
182 } else {
183 buf[0] = *s++;
184 size = 1;
185 }
186 if (size > len)
187 size = len;
188 memcpy(d, bufp, size);
189 d += size;
190 len -= size;
191 }
192 *d = '\0';
193}