]> jfr.im git - solanum.git/blame - ircd/logger.c
authd: identd fixes
[solanum.git] / ircd / logger.c
CommitLineData
212380e3 1/*
ba1a1399 2 * charybdis: an advanced Internet Relay Chat Daemon(ircd).
212380e3
AC
3 *
4 * Copyright (C) 2003 Lee H <lee@leeh.co.uk>
5 * Copyright (C) 2003-2005 ircd-ratbox development team
ba1a1399 6 * Copyright (C) 2008 William Pitcock <nenolod@sacredspiral.co.uk>
212380e3
AC
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * 1.Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
ba1a1399 14 *
212380e3
AC
15 * 2.Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
ba1a1399 18 *
212380e3
AC
19 * 3.The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
212380e3
AC
33 */
34
35#include "stdinc.h"
36#include "ircd_defs.h"
4016731b 37#include "logger.h"
212380e3 38#include "s_conf.h"
212380e3
AC
39#include "send.h"
40#include "client.h"
41#include "s_serv.h"
42
43static FILE *log_main;
44static FILE *log_user;
45static FILE *log_fuser;
46static FILE *log_oper;
47static FILE *log_foper;
48static FILE *log_server;
49static FILE *log_kill;
212380e3
AC
50static FILE *log_kline;
51static FILE *log_operspy;
52static FILE *log_ioerror;
53
54struct log_struct
55{
56 char **name;
57 FILE **logfile;
58};
59
60static struct log_struct log_table[LAST_LOGFILE] =
61{
62 { NULL, &log_main },
63 { &ConfigFileEntry.fname_userlog, &log_user },
64 { &ConfigFileEntry.fname_fuserlog, &log_fuser },
65 { &ConfigFileEntry.fname_operlog, &log_oper },
66 { &ConfigFileEntry.fname_foperlog, &log_foper },
67 { &ConfigFileEntry.fname_serverlog, &log_server },
68 { &ConfigFileEntry.fname_killlog, &log_kill },
69 { &ConfigFileEntry.fname_klinelog, &log_kline },
212380e3
AC
70 { &ConfigFileEntry.fname_operspylog, &log_operspy },
71 { &ConfigFileEntry.fname_ioerrorlog, &log_ioerror }
72};
73
cdf7c361
AS
74static void
75verify_logfile_access(const char *filename)
76{
77 char *dirname, *d;
78 char buf[512];
79 d = rb_dirname(filename);
80 dirname = LOCAL_COPY(d);
81 rb_free(d);
55abcbb2 82
cdf7c361
AS
83 if(access(dirname, F_OK) == -1)
84 {
5203cba5 85 snprintf(buf, sizeof(buf), "WARNING: Unable to access logfile %s - parent directory %s does not exist", filename, dirname);
cdf7c361
AS
86 if(testing_conf || server_state_foreground)
87 fprintf(stderr, "%s\n", buf);
88 sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s", buf);
89 return;
90 }
91
92 if(access(filename, F_OK) == -1)
93 {
94 if(access(dirname, W_OK) == -1)
95 {
5203cba5 96 snprintf(buf, sizeof(buf), "WARNING: Unable to access logfile %s - access to parent directory %s failed: %s",
cdf7c361
AS
97 filename, dirname, strerror(errno));
98 if(testing_conf || server_state_foreground)
99 fprintf(stderr, "%s\n", buf);
100 sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s", buf);
101 }
102 return;
103 }
55abcbb2 104
cdf7c361
AS
105 if(access(filename, W_OK) == -1)
106 {
5203cba5 107 snprintf(buf, sizeof(buf), "WARNING: Access denied for logfile %s: %s", filename, strerror(errno));
cdf7c361 108 if(testing_conf || server_state_foreground)
55abcbb2 109 fprintf(stderr, "%s\n", buf);
cdf7c361
AS
110 sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s", buf);
111 return;
112 }
113 return;
114}
115
212380e3
AC
116void
117init_main_logfile(void)
118{
cdf7c361 119 verify_logfile_access(logFileName);
212380e3 120 if(log_main == NULL)
cdf7c361 121 {
9b6ff0c8 122 log_main = fopen(logFileName, "a");
cdf7c361 123 }
212380e3
AC
124}
125
126void
127open_logfiles(void)
128{
129 int i;
130
ea82a3ca 131 close_logfiles();
212380e3 132
9b6ff0c8 133 log_main = fopen(logFileName, "a");
212380e3
AC
134
135 /* log_main is handled above, so just do the rest */
136 for(i = 1; i < LAST_LOGFILE; i++)
137 {
ea82a3ca
VY
138 /* reopen those with paths */
139 if(!EmptyString(*log_table[i].name))
cdf7c361
AS
140 {
141 verify_logfile_access(*log_table[i].name);
ea82a3ca 142 *log_table[i].logfile = fopen(*log_table[i].name, "a");
cdf7c361 143 }
ea82a3ca 144 }
55abcbb2 145}
ea82a3ca
VY
146
147void
148close_logfiles(void)
149{
150 int i;
151
152 if(log_main != NULL)
153 fclose(log_main);
154
155 /* log_main is handled above, so just do the rest */
156 for(i = 1; i < LAST_LOGFILE; i++)
157 {
212380e3
AC
158 if(*log_table[i].logfile != NULL)
159 {
160 fclose(*log_table[i].logfile);
161 *log_table[i].logfile = NULL;
162 }
212380e3 163 }
ea82a3ca 164}
212380e3
AC
165
166void
167ilog(ilogfile dest, const char *format, ...)
168{
169 FILE *logfile = *log_table[dest].logfile;
170 char buf[BUFSIZE];
171 char buf2[BUFSIZE];
172 va_list args;
173
174 if(logfile == NULL)
175 return;
176
177 va_start(args, format);
5203cba5 178 vsnprintf(buf, sizeof(buf), format, args);
212380e3
AC
179 va_end(args);
180
5203cba5 181 snprintf(buf2, sizeof(buf2), "%s %s\n",
b52c2949 182 smalldate(rb_current_time()), buf);
212380e3
AC
183
184 if(fputs(buf2, logfile) < 0)
185 {
186 fclose(logfile);
187 *log_table[dest].logfile = NULL;
b7bb9657 188 return;
212380e3
AC
189 }
190
191 fflush(logfile);
192}
193
194static void
ba1a1399 195_iprint(const char *domain, const char *buf)
212380e3
AC
196{
197 if (domain == NULL || buf == NULL)
198 return;
199
200 fprintf(stderr, "%8s: %s\n", domain, buf);
201}
202
203void
204inotice(const char *format, ...)
205{
206 char buf[BUFSIZE];
207 va_list args;
208
209 va_start(args, format);
5203cba5 210 vsnprintf(buf, sizeof(buf), format, args);
212380e3
AC
211 va_end(args);
212
213 _iprint("notice", buf);
214
215 ilog(L_MAIN, "%s", buf);
216}
217
218void
219iwarn(const char *format, ...)
220{
221 char buf[BUFSIZE];
222 va_list args;
223
224 va_start(args, format);
5203cba5 225 vsnprintf(buf, sizeof(buf), format, args);
212380e3
AC
226 va_end(args);
227
228 _iprint("warning", buf);
229
230 ilog(L_MAIN, "%s", buf);
231}
232
233void
234ierror(const char *format, ...)
235{
236 char buf[BUFSIZE];
237 va_list args;
238
239 va_start(args, format);
5203cba5 240 vsnprintf(buf, sizeof(buf), format, args);
212380e3
AC
241 va_end(args);
242
243 _iprint("error", buf);
244
245 ilog(L_MAIN, "%s", buf);
246}
247
248void
249report_operspy(struct Client *source_p, const char *token, const char *arg)
250{
251 /* if its not my client its already propagated */
252 if(MyClient(source_p))
253 sendto_match_servs(source_p, "*", CAP_ENCAP, NOCAPS,
254 "ENCAP * OPERSPY %s %s",
255 token, arg ? arg : "");
256
257 sendto_realops_snomask(SNO_OPERSPY,
258 ConfigFileEntry.operspy_admin_only ? L_ADMIN : L_ALL,
259 "OPERSPY %s %s %s",
260 get_oper_name(source_p), token,
261 arg ? arg : "");
262
263 ilog(L_OPERSPY, "OPERSPY %s %s %s",
264 get_oper_name(source_p), token, arg ? arg : "");
265}
266
267const char *
b52c2949 268smalldate(time_t ltime)
212380e3
AC
269{
270 static char buf[MAX_DATE_STRING];
271 struct tm *lt;
212380e3
AC
272
273 lt = localtime(&ltime);
274
5203cba5 275 snprintf(buf, sizeof(buf), "%d/%d/%d %02d.%02d",
212380e3
AC
276 lt->tm_year + 1900, lt->tm_mon + 1,
277 lt->tm_mday, lt->tm_hour, lt->tm_min);
278
279 return buf;
280}
281
212380e3 282void
ba1a1399 283ilog_error(const char *error)
212380e3 284{
db6b1735
JT
285 int e;
286 const char *errstr;
212380e3 287
db6b1735
JT
288 e = errno;
289 errstr = strerror(e);
290
291 ilog(L_IOERROR, "%s: %d (%s)", error, e, errstr);
292 sendto_realops_snomask(SNO_DEBUG, L_ALL, "%s: %d (%s)",
293 error, e, errstr);
212380e3 294}