1#include <config.h>
2#include <stdio.h>
3#include <stdarg.h>
4#include <unistd.h>
5#ifdef HAVE_SYS_TIME_H
6#include <sys/time.h>
7#endif
8#ifdef HAVE_TIME_H
9#include <time.h>
10#endif
11#include <main.h>
12#ifdef HAVE_SYSLOG_H
13#include <syslog.h>
14#endif
15#include <string.h>
16#include <stdlib.h>
17#include <errno.h>
18
19#include "logging.h"
20#include "options.h"
21#include "mystring.h"
22
23FILE *logfile = NULL;
24FILE *statuslog = NULL;
25FILE *statuslogforreading = NULL;
26
27char log_syslog = 0;
28
29#ifdef LEAN
30void log_init()
31{
32}
33#else
34void log_init()
35{
36//    char *foo = config_getoption("LOGFILE");
37    char *foo = "/var/log/bftpd.log";
38#ifdef HAVE_SYSLOG_H
39	if (!strcasecmp(foo, "syslog")) {
40        log_syslog = 1;
41		openlog(global_argv[0], LOG_PID, LOG_DAEMON);
42	} else
43#endif
44    if (foo[0])
45        if (!(logfile = fopen(foo, "a"))) {
46    		control_printf(SL_FAILURE, "421-Could not open log file.\r\n"
47    		         "421 Server disabled for security reasons.");
48    		exit(1);
49    	}
50    statuslog = fopen(PATH_STATUSLOG, "a");
51    /* This one is for the admin code. */
52    statuslogforreading = fopen(PATH_STATUSLOG, "r");
53}
54#endif
55
56/* Status file format:
57 * <ID> <Type> <Type2> <String>
58 * ID: PID of server process
59 * Type: SL_INFO for information, SL_COMMAND for command, SL_REPLY for reply.
60 * Type2: If Type = SL_INFO or SL_COMMAND, Type2 is SL_UNDEF. Else,
61 * it's SL_SUCCESS for success and SL_FAILURE for error.
62 */
63#ifdef LEAN
64void bftpd_statuslog(char type, char type2, char *format, ...)
65{
66}
67#else
68void bftpd_statuslog(char type, char type2, char *format, ...)
69{
70    if (statuslog) {
71        va_list val;
72        char buffer[1024];
73        va_start(val, format);
74        vsnprintf(buffer, sizeof(buffer), format, val);
75        va_end(val);
76        fseek(statuslog, 0, SEEK_END);
77        fprintf(statuslog, "%i %i %i %s\n", (int) getpid(), type, type2,
78                buffer);
79        fflush(statuslog);
80    }
81}
82#endif
83
84#ifdef LEAN
85void bftpd_log(char *format, ...)
86{
87}
88#else
89void bftpd_log(char *format, ...)
90{
91	va_list val;
92	char buffer[1024], timestr[40];
93	time_t t;
94	va_start(val, format);
95	vsnprintf(buffer, sizeof(buffer), format, val);
96	va_end(val);
97	if (logfile) {
98		fseek(logfile, 0, SEEK_END);
99		time(&t);
100		strcpy(timestr, (char *) ctime(&t));
101		timestr[strlen(timestr) - 1] = '\0';
102		fprintf(logfile, "%s %s[%i]: %s", timestr, global_argv[0],
103				(int) getpid(), buffer);
104		fflush(logfile);
105	}
106#ifdef HAVE_SYSLOG_H
107    else if (log_syslog)
108        syslog(LOG_DAEMON | LOG_INFO, "%s", buffer);
109#endif
110}
111#endif
112
113#ifdef LEAN
114void log_end()
115{
116}
117#else
118void log_end()
119{
120	if (logfile) {
121		fclose(logfile);
122		logfile = NULL;
123	}
124#ifdef HAVE_SYSLOG_H
125    else if (log_syslog)
126		closelog();
127#endif
128    if (statuslog) {
129        fclose(statuslog);
130        statuslog = NULL;
131    }
132}
133#endif
134