2001-12-14 17:57:50 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "putty.h"
|
|
|
|
|
|
|
|
/* log session to file stuff ... */
|
2002-10-26 16:58:13 +04:00
|
|
|
struct LogContext {
|
|
|
|
FILE *lgfp;
|
|
|
|
char currlogfilename[FILENAME_MAX];
|
|
|
|
void *frontend;
|
|
|
|
};
|
2001-12-14 17:57:50 +03:00
|
|
|
|
|
|
|
static void xlatlognam(char *d, char *s, char *hostname, struct tm *tm);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Log session traffic.
|
|
|
|
*/
|
2002-10-26 16:58:13 +04:00
|
|
|
void logtraffic(void *handle, unsigned char c, int logmode)
|
2001-12-14 17:57:50 +03:00
|
|
|
{
|
2002-10-26 16:58:13 +04:00
|
|
|
struct LogContext *ctx = (struct LogContext *)handle;
|
2001-12-14 17:57:50 +03:00
|
|
|
if (cfg.logtype > 0) {
|
|
|
|
if (cfg.logtype == logmode) {
|
|
|
|
/* deferred open file from pgm start? */
|
2002-10-26 16:58:13 +04:00
|
|
|
if (!ctx->lgfp)
|
|
|
|
logfopen(ctx);
|
|
|
|
if (ctx->lgfp)
|
|
|
|
fputc(c, ctx->lgfp);
|
2001-12-14 17:57:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-15 17:21:32 +04:00
|
|
|
/*
|
2002-12-19 17:24:28 +03:00
|
|
|
* Log an Event Log entry. Used in SSH packet logging mode; this is
|
|
|
|
* also as convenient a place as any to put the output of Event Log
|
|
|
|
* entries to stderr when a command-line tool is in verbose mode.
|
|
|
|
* (In particular, this is a better place to put it than in the
|
|
|
|
* front ends, because it only has to be done once for all
|
|
|
|
* platforms. Platforms which don't have a meaningful stderr can
|
|
|
|
* just avoid defining FLAG_STDERR.
|
2002-09-15 17:21:32 +04:00
|
|
|
*/
|
2002-10-26 16:58:13 +04:00
|
|
|
void log_eventlog(void *handle, char *event)
|
2002-09-15 17:21:32 +04:00
|
|
|
{
|
2002-10-26 16:58:13 +04:00
|
|
|
struct LogContext *ctx = (struct LogContext *)handle;
|
2002-12-19 17:24:28 +03:00
|
|
|
if ((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)) {
|
|
|
|
fprintf(stderr, "%s\n", event);
|
|
|
|
fflush(stderr);
|
|
|
|
}
|
2002-09-15 17:21:32 +04:00
|
|
|
if (cfg.logtype != LGTYP_PACKETS)
|
|
|
|
return;
|
2002-10-26 16:58:13 +04:00
|
|
|
if (!ctx->lgfp)
|
|
|
|
logfopen(ctx);
|
|
|
|
if (ctx->lgfp)
|
2002-12-08 15:10:58 +03:00
|
|
|
fprintf(ctx->lgfp, "Event Log: %s\r\n", event);
|
2002-09-15 17:21:32 +04:00
|
|
|
}
|
|
|
|
|
2001-12-14 17:57:50 +03:00
|
|
|
/*
|
|
|
|
* Log an SSH packet.
|
|
|
|
*/
|
2002-10-26 16:58:13 +04:00
|
|
|
void log_packet(void *handle, int direction, int type,
|
|
|
|
char *texttype, void *data, int len)
|
2001-12-14 17:57:50 +03:00
|
|
|
{
|
2002-10-26 16:58:13 +04:00
|
|
|
struct LogContext *ctx = (struct LogContext *)handle;
|
2002-09-15 17:21:32 +04:00
|
|
|
int i, j;
|
2001-12-14 17:57:50 +03:00
|
|
|
char dumpdata[80], smalldata[5];
|
|
|
|
|
|
|
|
if (cfg.logtype != LGTYP_PACKETS)
|
|
|
|
return;
|
2002-10-26 16:58:13 +04:00
|
|
|
if (!ctx->lgfp)
|
|
|
|
logfopen(ctx);
|
|
|
|
if (ctx->lgfp) {
|
2002-12-08 15:10:58 +03:00
|
|
|
fprintf(ctx->lgfp, "%s packet type %d / 0x%02x (%s)\r\n",
|
2001-12-14 17:57:50 +03:00
|
|
|
direction == PKT_INCOMING ? "Incoming" : "Outgoing",
|
|
|
|
type, type, texttype);
|
|
|
|
for (i = 0; i < len; i += 16) {
|
2002-12-08 15:10:58 +03:00
|
|
|
sprintf(dumpdata, " %08x%*s\r\n", i, 1+3*16+2+16, "");
|
2001-12-14 17:57:50 +03:00
|
|
|
for (j = 0; j < 16 && i+j < len; j++) {
|
|
|
|
int c = ((unsigned char *)data)[i+j];
|
|
|
|
sprintf(smalldata, "%02x", c);
|
|
|
|
dumpdata[10+2+3*j] = smalldata[0];
|
|
|
|
dumpdata[10+2+3*j+1] = smalldata[1];
|
|
|
|
dumpdata[10+1+3*16+2+j] = (isprint(c) ? c : '.');
|
|
|
|
}
|
2002-12-08 15:10:58 +03:00
|
|
|
strcpy(dumpdata + 10+1+3*16+2+j, "\r\n");
|
2002-10-26 16:58:13 +04:00
|
|
|
fputs(dumpdata, ctx->lgfp);
|
2001-12-14 17:57:50 +03:00
|
|
|
}
|
2002-10-26 16:58:13 +04:00
|
|
|
fflush(ctx->lgfp);
|
2001-12-14 17:57:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* open log file append/overwrite mode */
|
2002-10-26 16:58:13 +04:00
|
|
|
void logfopen(void *handle)
|
2001-12-14 17:57:50 +03:00
|
|
|
{
|
2002-10-26 16:58:13 +04:00
|
|
|
struct LogContext *ctx = (struct LogContext *)handle;
|
2001-12-14 17:57:50 +03:00
|
|
|
char buf[256];
|
|
|
|
time_t t;
|
|
|
|
struct tm tm;
|
|
|
|
char writemod[4];
|
|
|
|
|
2002-09-15 17:21:32 +04:00
|
|
|
/* Prevent repeat calls */
|
2002-10-26 16:58:13 +04:00
|
|
|
if (ctx->lgfp)
|
2002-09-15 17:21:32 +04:00
|
|
|
return;
|
|
|
|
|
2001-12-14 17:57:50 +03:00
|
|
|
if (!cfg.logtype)
|
|
|
|
return;
|
2002-12-08 15:10:58 +03:00
|
|
|
sprintf(writemod, "wb"); /* default to rewrite */
|
2001-12-14 17:57:50 +03:00
|
|
|
|
|
|
|
time(&t);
|
|
|
|
tm = *localtime(&t);
|
|
|
|
|
|
|
|
/* substitute special codes in file name */
|
2002-10-26 16:58:13 +04:00
|
|
|
xlatlognam(ctx->currlogfilename, cfg.logfilename,cfg.host, &tm);
|
2001-12-14 17:57:50 +03:00
|
|
|
|
2002-10-26 16:58:13 +04:00
|
|
|
ctx->lgfp = fopen(ctx->currlogfilename, "r"); /* file already present? */
|
|
|
|
if (ctx->lgfp) {
|
2001-12-14 17:57:50 +03:00
|
|
|
int i;
|
2002-10-26 16:58:13 +04:00
|
|
|
fclose(ctx->lgfp);
|
|
|
|
i = askappend(ctx->frontend, ctx->currlogfilename);
|
2001-12-14 17:57:50 +03:00
|
|
|
if (i == 1)
|
|
|
|
writemod[0] = 'a'; /* set append mode */
|
|
|
|
else if (i == 0) { /* cancelled */
|
2002-10-26 16:58:13 +04:00
|
|
|
ctx->lgfp = NULL;
|
2001-12-14 17:57:50 +03:00
|
|
|
cfg.logtype = 0; /* disable logging */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-26 16:58:13 +04:00
|
|
|
ctx->lgfp = fopen(ctx->currlogfilename, writemod);
|
|
|
|
if (ctx->lgfp) { /* enter into event log */
|
2002-09-15 17:21:32 +04:00
|
|
|
/* --- write header line into log file */
|
2002-10-26 16:58:13 +04:00
|
|
|
fputs("=~=~=~=~=~=~=~=~=~=~=~= PuTTY log ", ctx->lgfp);
|
2002-09-15 17:21:32 +04:00
|
|
|
strftime(buf, 24, "%Y.%m.%d %H:%M:%S", &tm);
|
2002-10-26 16:58:13 +04:00
|
|
|
fputs(buf, ctx->lgfp);
|
2002-12-08 15:10:58 +03:00
|
|
|
fputs(" =~=~=~=~=~=~=~=~=~=~=~=\r\n", ctx->lgfp);
|
2002-09-15 17:21:32 +04:00
|
|
|
|
|
|
|
sprintf(buf, "%s session log (%s mode) to file: ",
|
2001-12-14 17:57:50 +03:00
|
|
|
(writemod[0] == 'a') ? "Appending" : "Writing new",
|
|
|
|
(cfg.logtype == LGTYP_ASCII ? "ASCII" :
|
2002-09-15 17:21:32 +04:00
|
|
|
cfg.logtype == LGTYP_DEBUG ? "raw" :
|
|
|
|
cfg.logtype == LGTYP_PACKETS ? "SSH packets" : "<ukwn>"));
|
2001-12-14 17:57:50 +03:00
|
|
|
/* Make sure we do not exceed the output buffer size */
|
2002-10-26 16:58:13 +04:00
|
|
|
strncat(buf, ctx->currlogfilename, 128);
|
2001-12-14 17:57:50 +03:00
|
|
|
buf[strlen(buf)] = '\0';
|
2002-10-26 16:58:13 +04:00
|
|
|
logevent(ctx->frontend, buf);
|
2001-12-14 17:57:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-26 16:58:13 +04:00
|
|
|
void logfclose(void *handle)
|
2001-12-14 17:57:50 +03:00
|
|
|
{
|
2002-10-26 16:58:13 +04:00
|
|
|
struct LogContext *ctx = (struct LogContext *)handle;
|
|
|
|
if (ctx->lgfp) {
|
|
|
|
fclose(ctx->lgfp);
|
|
|
|
ctx->lgfp = NULL;
|
2001-12-14 17:57:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-26 16:58:13 +04:00
|
|
|
void *log_init(void *frontend)
|
|
|
|
{
|
|
|
|
struct LogContext *ctx = smalloc(sizeof(struct LogContext));
|
|
|
|
ctx->lgfp = NULL;
|
|
|
|
ctx->frontend = frontend;
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
2001-12-14 17:57:50 +03:00
|
|
|
/*
|
|
|
|
* translate format codes into time/date strings
|
|
|
|
* and insert them into log file name
|
|
|
|
*
|
|
|
|
* "&Y":YYYY "&m":MM "&d":DD "&T":hhmm "&h":<hostname> "&&":&
|
|
|
|
*/
|
|
|
|
static void xlatlognam(char *d, char *s, char *hostname, struct tm *tm) {
|
|
|
|
char buf[10], *bufp;
|
|
|
|
int size;
|
|
|
|
int len = FILENAME_MAX-1;
|
|
|
|
|
|
|
|
while (*s) {
|
|
|
|
/* Let (bufp, len) be the string to append. */
|
|
|
|
bufp = buf; /* don't usually override this */
|
|
|
|
if (*s == '&') {
|
|
|
|
char c;
|
|
|
|
s++;
|
2002-10-09 22:09:42 +04:00
|
|
|
size = 0;
|
2001-12-14 17:57:50 +03:00
|
|
|
if (*s) switch (c = *s++, tolower(c)) {
|
|
|
|
case 'y':
|
|
|
|
size = strftime(buf, sizeof(buf), "%Y", tm);
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
size = strftime(buf, sizeof(buf), "%m", tm);
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
size = strftime(buf, sizeof(buf), "%d", tm);
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
size = strftime(buf, sizeof(buf), "%H%M%S", tm);
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
bufp = hostname;
|
|
|
|
size = strlen(bufp);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
buf[0] = '&';
|
|
|
|
size = 1;
|
|
|
|
if (c != '&')
|
|
|
|
buf[size++] = c;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
buf[0] = *s++;
|
|
|
|
size = 1;
|
|
|
|
}
|
|
|
|
if (size > len)
|
|
|
|
size = len;
|
|
|
|
memcpy(d, bufp, size);
|
|
|
|
d += size;
|
|
|
|
len -= size;
|
|
|
|
}
|
|
|
|
*d = '\0';
|
|
|
|
}
|