]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - logging.c
Retired the #ifdef DUMP_PACKETS stuff in ssh.c because I'm utterly
[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 SSH packet.
37  */
38 void log_packet(int direction, int type, char *texttype, void *data, int len)
39 {
40     int i, j, c;
41     char dumpdata[80], smalldata[5];
42
43     if (cfg.logtype != LGTYP_PACKETS)
44         return;
45     if (!lgfp)
46         logfopen();
47     if (lgfp) {
48         fprintf(lgfp, "%s packet type %d / 0x%02x (%s)\n",
49                 direction == PKT_INCOMING ? "Incoming" : "Outgoing",
50                 type, type, texttype);
51         for (i = 0; i < len; i += 16) {
52             sprintf(dumpdata, "  %08x%*s\n", i, 1+3*16+2+16, "");
53             for (j = 0; j < 16 && i+j < len; j++) {
54                 int c = ((unsigned char *)data)[i+j];
55                 sprintf(smalldata, "%02x", c);
56                 dumpdata[10+2+3*j] = smalldata[0];
57                 dumpdata[10+2+3*j+1] = smalldata[1];
58                 dumpdata[10+1+3*16+2+j] = (isprint(c) ? c : '.');
59             }
60             strcpy(dumpdata + 10+1+3*16+2+j, "\n");
61             fputs(dumpdata, lgfp);
62         }
63     }
64 }
65
66 /* open log file append/overwrite mode */
67 void logfopen(void)
68 {
69     char buf[256];
70     time_t t;
71     struct tm tm;
72     char writemod[4];
73
74     if (!cfg.logtype)
75         return;
76     sprintf(writemod, "wb");           /* default to rewrite */
77
78     time(&t);
79     tm = *localtime(&t);
80
81     /* substitute special codes in file name */
82     xlatlognam(currlogfilename,cfg.logfilename,cfg.host, &tm);
83
84     lgfp = fopen(currlogfilename, "r"); /* file already present? */
85     if (lgfp) {
86         int i;
87         fclose(lgfp);
88         i = askappend(currlogfilename);
89         if (i == 1)
90             writemod[0] = 'a';         /* set append mode */
91         else if (i == 0) {             /* cancelled */
92             lgfp = NULL;
93             cfg.logtype = 0;           /* disable logging */
94             return;
95         }
96     }
97
98     lgfp = fopen(currlogfilename, writemod);
99     if (lgfp) {                        /* enter into event log */
100         sprintf(buf, "%s session log (%s mode) to file : ",
101                 (writemod[0] == 'a') ? "Appending" : "Writing new",
102                 (cfg.logtype == LGTYP_ASCII ? "ASCII" :
103                  cfg.logtype == LGTYP_DEBUG ? "raw" : "<ukwn>"));
104         /* Make sure we do not exceed the output buffer size */
105         strncat(buf, currlogfilename, 128);
106         buf[strlen(buf)] = '\0';
107         logevent(buf);
108
109         /* --- write header line into log file */
110         fputs("=~=~=~=~=~=~=~=~=~=~=~= PuTTY log ", lgfp);
111         strftime(buf, 24, "%Y.%m.%d %H:%M:%S", &tm);
112         fputs(buf, lgfp);
113         fputs(" =~=~=~=~=~=~=~=~=~=~=~=\r\n", lgfp);
114     }
115 }
116
117 void logfclose(void)
118 {
119     if (lgfp) {
120         fclose(lgfp);
121         lgfp = NULL;
122     }
123 }
124
125 /*
126  * translate format codes into time/date strings
127  * and insert them into log file name
128  *
129  * "&Y":YYYY   "&m":MM   "&d":DD   "&T":hhmm   "&h":<hostname>   "&&":&
130  */
131 static void xlatlognam(char *d, char *s, char *hostname, struct tm *tm) {
132     char buf[10], *bufp;
133     int size;
134     char *ds = d; /* save start pos. */
135     int len = FILENAME_MAX-1;
136
137     while (*s) {
138         /* Let (bufp, len) be the string to append. */
139         bufp = buf;                    /* don't usually override this */
140         if (*s == '&') {
141             char c;
142             s++;
143             if (*s) switch (c = *s++, tolower(c)) {
144               case 'y':
145                 size = strftime(buf, sizeof(buf), "%Y", tm);
146                 break;
147               case 'm':
148                 size = strftime(buf, sizeof(buf), "%m", tm);
149                 break;
150               case 'd':
151                 size = strftime(buf, sizeof(buf), "%d", tm);
152                 break;
153               case 't':
154                 size = strftime(buf, sizeof(buf), "%H%M%S", tm);
155                 break;
156               case 'h':
157                 bufp = hostname;
158                 size = strlen(bufp);
159                 break;
160               default:
161                 buf[0] = '&';
162                 size = 1;
163                 if (c != '&')
164                     buf[size++] = c;
165             }
166         } else {
167             buf[0] = *s++;
168             size = 1;
169         }
170         if (size > len)
171             size = len;
172         memcpy(d, bufp, size);
173         d += size;
174         len -= size;
175     }
176     *d = '\0';
177 }