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