1 /*
2  * ProFTPD - FTP server daemon
3  * Copyright (c) 1997, 1998 Public Flood Software
4  * Copyright (c) 1999, 2000 MacGyver aka Habeeb J. Dihu <macgyver@tos.net>
5  * Copyright (c) 2001-2020 The ProFTPD Project team
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
20  *
21  * As a special exemption, Public Flood Software/MacGyver aka Habeeb J. Dihu
22  * and other respective copyright holders give permission to link this program
23  * with OpenSSL, and distribute the resulting executable, without including
24  * the source code for OpenSSL in the source distribution.
25  */
26 
27 /* Logging, either to syslog or stderr, as well as debug logging
28  * and debug levels.
29  */
30 
31 #ifndef PR_LOG_H
32 #define PR_LOG_H
33 
34 #ifndef LOG_AUTHPRIV
35 #define LOG_AUTHPRIV LOG_AUTH
36 #endif
37 
38 #if !defined(WTMP_FILE) && defined(_PATH_WTMP)
39 #define WTMP_FILE _PATH_WTMP
40 #endif
41 
42 /* These are the debug levels, higher numbers print more debugging
43  * info.  DEBUG0 (the default) prints nothing.
44  */
45 
46 #define DEBUG10		10
47 #define DEBUG9		9
48 #define DEBUG8		8
49 #define DEBUG7		7
50 #define DEBUG6		6
51 #define DEBUG5		5
52 #define DEBUG4		4
53 #define	DEBUG3		3
54 #define DEBUG2		2
55 #define DEBUG1		1
56 #define DEBUG0		0
57 
58 /* pr_log_openfile() return values */
59 #define PR_LOG_WRITABLE_DIR	-2
60 #define PR_LOG_SYMLINK		-3
61 
62 /* Log file modes */
63 #define PR_LOG_SYSTEM_MODE	0640
64 
65 #ifdef PR_USE_LASTLOG
66 
67 /* It is tempting to want to have these lastlog-related includes separated
68  * out into their own lastlog.h file.  However, on some systems, such a
69  * proftpd-specific lastlog.h file may collide with the system's lastlog.h
70  * file.  Ultimately it's an issue with the default search order of the
71  * system C preprocessor, not with us -- and not every installation has
72  * this problem.
73  *
74  * In the meantime, the most portable thing is to keep these lastlog-related
75  * includes in this file.  Yay portability.
76  */
77 
78 #ifdef HAVE_LASTLOG_H
79 # include <lastlog.h>
80 #endif
81 
82 #ifdef HAVE_LOGIN_H
83 # include <login.h>
84 #endif
85 
86 #ifdef HAVE_PATHS_H
87 # include <paths.h>
88 #endif
89 
90 #ifndef PR_LASTLOG_PATH
91 # ifdef _PATH_LASTLOG
92 #   define PR_LASTLOG_PATH      _PATH_LASTLOG
93 # else
94 #   ifdef LASTLOG_FILE
95 #     define PR_LASTLOG_PATH    LASTLOG_FILE
96 #   else
97 #     error "Missing path to lastlog file (use --with-lastlog=PATH)"
98 #   endif
99 # endif
100 #endif
101 
102 int log_lastlog(uid_t uid, const char *user_name, const char *tty,
103   const pr_netaddr_t *remote_addr);
104 #endif /* PR_USE_LASTLOG */
105 
106 /* Note: Like lastlog.h, it would be tempting to split out the declaration of
107  * this function, and its necessary system headers, into a proftpd-specific
108  * wtmp.h file.  But that would collide with the system wtmp.h file on
109  * some systems.
110  */
111 int log_wtmp(const char *, const char *, const char *, const pr_netaddr_t *);
112 
113 /* file-based logging functions */
114 int pr_log_openfile(const char *, int *, mode_t);
115 
116 int pr_log_writefile(int, const char *, const char *, ...)
117 #ifdef __GNUC__
118   __attribute__ ((format (printf, 3, 4)));
119 #else
120   ;
121 #endif
122 
123 /* Same as pr_log_writefile(), only this function takes a va_list.
124  * Useful for modules which provide their own varargs wrapper log functions,
125  * but still want to use the core facilities for writing to the log fd.
126  */
127 int pr_log_vwritefile(int, const char *, const char *, va_list ap);
128 
129 /* syslog-based logging functions.  Note that the open/close functions are
130  * not part of the public API; use the pr_log_pri() function to log via
131  * syslog.
132  */
133 void log_closesyslog(void);
134 int log_opensyslog(const char *);
135 int log_getfacility(void);
136 void log_setfacility(int);
137 
138 void pr_log_pri(int, const char *, ...)
139 #ifdef __GNUC__
140        __attribute__ ((format (printf, 2, 3)));
141 #else
142        ;
143 #endif
144 
145 void pr_log_auth(int, const char *, ...)
146 #ifdef __GNUC__
147        __attribute__ ((format (printf, 2, 3)));
148 #else
149        ;
150 #endif
151 
152 void pr_log_debug(int, const char *, ...)
153 #ifdef __GNUC__
154        __attribute__ ((format (printf, 2, 3)));
155 #else
156        ;
157 #endif
158 
159 int pr_log_setdebuglevel(int);
160 int pr_log_setdefaultlevel(int);
161 
162 void log_stderr(int);
163 void log_discard(void);
164 void init_log(void);
165 
166 int pr_log_str2sysloglevel(const char *);
167 
168 /* Define a struct, and some constants, for logging events and any listeners
169  * for those events.
170  */
171 typedef struct log_event {
172 
173   /* Type of log event/message; the values are defined below */
174   unsigned int log_type;
175 
176   /* Log fd associated with this log for this event */
177   int log_fd;
178 
179   /* Log level of this message; the semantics of the log level depend on
180    * on the log type.
181    */
182   int log_level;
183 
184   /* The message being logged. */
185   const char *log_msg;
186   size_t log_msglen;
187 
188 } pr_log_event_t;
189 
190 #define PR_LOG_TYPE_UNSPEC	0
191 #define PR_LOG_NAME_UNSPEC	"core.log.unspec"
192 
193 #define PR_LOG_TYPE_XFERLOG	1
194 #define PR_LOG_NAME_XFERLOG	"core.log.xferlog"
195 
196 #define PR_LOG_TYPE_SYSLOG	2
197 #define PR_LOG_NAME_SYSLOG	"core.log.syslog"
198 
199 #define PR_LOG_TYPE_SYSTEMLOG	3
200 #define PR_LOG_NAME_SYSTEMLOG	"core.log.systemlog"
201 
202 #define PR_LOG_TYPE_EXTLOG	4
203 #define PR_LOG_NAME_EXTLOG	"core.log.extendedlog"
204 
205 #define PR_LOG_TYPE_TRACELOG	5
206 #define PR_LOG_NAME_TRACELOG	"core.log.tracelog"
207 
208 /* Helper function to generate the necessary log event, passing along the
209  * log event context data.
210  */
211 int pr_log_event_generate(unsigned int log_type, int log_fd, int log_level,
212   const char *log_msg, size_t log_msglen);
213 
214 /* Returns TRUE if there are listeners for the specified log type, FALSE
215  * otherwise.
216  */
217 int pr_log_event_listening(unsigned int log_type);
218 
219 /* Log a stacktrace, starting at the location of the calling function.
220  * Note that if fd is less than zero, OR if the given name is null, then the
221  * stacktrace will be logged using pr_log_pri(), otherwise the stacktrace will
222  * be written to the provided file descriptor.
223  */
224 void pr_log_stacktrace(int fd, const char *name);
225 
226 /* Set options that affect the format of the logged messages. */
227 int pr_log_set_options(unsigned long log_opts);
228 #define PR_LOG_OPT_USE_TIMESTAMP			0x0001
229 #define PR_LOG_OPT_USE_HOSTNAME				0x0002
230 #define PR_LOG_OPT_USE_VHOST				0x0004
231 #define PR_LOG_OPT_USE_ROLE_BASED_PROCESS_LABELS	0x0008
232 #define PR_LOG_OPT_DEFAULT		(PR_LOG_OPT_USE_TIMESTAMP|PR_LOG_OPT_USE_HOSTNAME|PR_LOG_OPT_USE_VHOST)
233 
234 #endif /* PR_LOG_H */
235