File: logger.c

package info (click to toggle)
muddleftpd 1.3.13.1-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,340 kB
  • ctags: 2,404
  • sloc: ansic: 16,327; python: 642; makefile: 345; sh: 241
file content (125 lines) | stat: -rw-r--r-- 3,417 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* Copyright (C) 1999 Beau Kuiper

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include "ftpd.h"

int logoutfd = -1;
int logoutmask = 0;

int log_initcontext(char *logname)
{
	return(open(logname, O_APPEND | O_CREAT | O_WRONLY, 0600));
} 

void log_setcontext(int logfd, int logmask)
{
	logoutfd = logfd;
	logoutmask = logmask;
}

void log_addentry(int type, FTPSTATE *peer, char *desc)
{
	if ((logoutfd != -1) && (((logoutmask) & type) == type))
	{
		int currenttime = time(NULL);
		int outlen,i;
		char *timestr = ctime((time_t *)&currenttime);
		char *outstring;

		int writeresult;
		timestr[strlen(timestr) - 1] = '\0';
		
		switch(type)
		{
			case MYLOG_DACCESS: /* access denied */
				outstring = safe_snprintf("-- %s : %s(%s) : Access Denied to ftp server.\n", timestr, peer->hostname, getipstr(peer->remoteip));
				break;
			case MYLOG_FTRANS: /* transfer log */
				outstring = safe_snprintf("++ %s : %s@%s(%s)/%d : %s\n", 
					timestr, peer->username, peer->hostname, getipstr(peer->remoteip),
					peer->threadnum, desc);
				break;
			case MYLOG_COMMAND: /* command log */
				outstring = safe_snprintf("<< %s : %s@%s(%s)/%d : %s\n", timestr, 
					peer->username, peer->hostname, getipstr(peer->remoteip), 
					peer->threadnum, desc);
				break;
			case MYLOG_RESPONSE: /* response log */
				outstring = safe_snprintf(">> %s : %s@%s(%s)/%d : %s\n", timestr, 
					peer->username, peer->hostname, getipstr(peer->remoteip),
					peer->threadnum, desc);
				break;
			case MYLOG_LOGIN: /* login log */
				outstring = safe_snprintf("!! %s : %s@%s(%s)/%d : %s\n", timestr, 
					peer->username, peer->hostname, getipstr(peer->remoteip),
					peer->threadnum, desc);
				break;
			case MYLOG_INFO: /* info log */  
				outstring = safe_snprintf("** %s : Info - %s\n", timestr, desc); 
				break; 
			case MYLOG_DEBUG: /* debug log */
				outstring = safe_snprintf("dd %s : DEBUG - %s\n", timestr, desc);
				break;
			default:
				outstring = safe_snprintf("uu Unknown log type, report as a bug!\n");
		}		
		
		outlen = strlen(outstring);
		
		writeresult = write(logoutfd, outstring, outlen);
		
		if (writeresult != outlen)
			ERRORMSGFATAL(strerror(errno));

		freewrapper(outstring);
	}
} 

/* this one assumes it must free desc after doing the log */

void log_giveentry(int type, FTPSTATE *peer, char *desc)
{
	log_addentry(type, peer, desc);
	freewrapper(desc);
}

void debuglog(char *format, ...)
{
#ifdef DEBUG
	char *logmsg;
	va_list ap;
	int len;

	va_start(ap, format);
	len = vsnprintf(NULL, 0, format, ap);
	va_end(ap);
	
	va_start(ap, format);
	logmsg = safe_vsnprintf(len, format, ap);
	va_end(ap);
	
	log_addentry(MYLOG_DEBUG, NULL, logmsg);
	freewrapper(logmsg);
#endif
}

void log_shutdown(void)
{
	if (logoutfd != -1)
		close(logoutfd);
}