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