]> jfr.im git - irc/rqf/shadowircd.git/blob - src/logger.c
Removal of ancient SVN ID's part one
[irc/rqf/shadowircd.git] / src / logger.c
1 /*
2 * charybdis: an advanced Internet Relay Chat Daemon(ircd).
3 *
4 * Copyright (C) 2003 Lee H <lee@leeh.co.uk>
5 * Copyright (C) 2003-2005 ircd-ratbox development team
6 * Copyright (C) 2008 William Pitcock <nenolod@sacredspiral.co.uk>
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.
14 *
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.
18 *
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.
33 *
34 */
35
36 #include "stdinc.h"
37 #include "ircd_defs.h"
38 #include "logger.h"
39 #include "s_conf.h"
40 #include "send.h"
41 #include "client.h"
42 #include "s_serv.h"
43
44 static FILE *log_main;
45 static FILE *log_user;
46 static FILE *log_fuser;
47 static FILE *log_oper;
48 static FILE *log_foper;
49 static FILE *log_server;
50 static FILE *log_kill;
51 static FILE *log_kline;
52 static FILE *log_operspy;
53 static FILE *log_ioerror;
54
55 struct log_struct
56 {
57 char **name;
58 FILE **logfile;
59 };
60
61 static struct log_struct log_table[LAST_LOGFILE] =
62 {
63 { NULL, &log_main },
64 { &ConfigFileEntry.fname_userlog, &log_user },
65 { &ConfigFileEntry.fname_fuserlog, &log_fuser },
66 { &ConfigFileEntry.fname_operlog, &log_oper },
67 { &ConfigFileEntry.fname_foperlog, &log_foper },
68 { &ConfigFileEntry.fname_serverlog, &log_server },
69 { &ConfigFileEntry.fname_killlog, &log_kill },
70 { &ConfigFileEntry.fname_klinelog, &log_kline },
71 { &ConfigFileEntry.fname_operspylog, &log_operspy },
72 { &ConfigFileEntry.fname_ioerrorlog, &log_ioerror }
73 };
74
75 static void
76 verify_logfile_access(const char *filename)
77 {
78 char *dirname, *d;
79 char buf[512];
80 d = rb_dirname(filename);
81 dirname = LOCAL_COPY(d);
82 rb_free(d);
83
84 if(access(dirname, F_OK) == -1)
85 {
86 rb_snprintf(buf, sizeof(buf), "WARNING: Unable to access logfile %s - parent directory %s does not exist", filename, dirname);
87 if(testing_conf || server_state_foreground)
88 fprintf(stderr, "%s\n", buf);
89 sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s", buf);
90 return;
91 }
92
93 if(access(filename, F_OK) == -1)
94 {
95 if(access(dirname, W_OK) == -1)
96 {
97 rb_snprintf(buf, sizeof(buf), "WARNING: Unable to access logfile %s - access to parent directory %s failed: %s",
98 filename, dirname, strerror(errno));
99 if(testing_conf || server_state_foreground)
100 fprintf(stderr, "%s\n", buf);
101 sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s", buf);
102 }
103 return;
104 }
105
106 if(access(filename, W_OK) == -1)
107 {
108 rb_snprintf(buf, sizeof(buf), "WARNING: Access denied for logfile %s: %s", filename, strerror(errno));
109 if(testing_conf || server_state_foreground)
110 fprintf(stderr, "%s\n", buf);
111 sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s", buf);
112 return;
113 }
114 return;
115 }
116
117 void
118 init_main_logfile(void)
119 {
120 verify_logfile_access(logFileName);
121 if(log_main == NULL)
122 {
123 log_main = fopen(logFileName, "a");
124 }
125 }
126
127 void
128 open_logfiles(void)
129 {
130 int i;
131
132 close_logfiles();
133
134 log_main = fopen(logFileName, "a");
135
136 /* log_main is handled above, so just do the rest */
137 for(i = 1; i < LAST_LOGFILE; i++)
138 {
139 /* reopen those with paths */
140 if(!EmptyString(*log_table[i].name))
141 {
142 verify_logfile_access(*log_table[i].name);
143 *log_table[i].logfile = fopen(*log_table[i].name, "a");
144 }
145 }
146 }
147
148 void
149 close_logfiles(void)
150 {
151 int i;
152
153 if(log_main != NULL)
154 fclose(log_main);
155
156 /* log_main is handled above, so just do the rest */
157 for(i = 1; i < LAST_LOGFILE; i++)
158 {
159 if(*log_table[i].logfile != NULL)
160 {
161 fclose(*log_table[i].logfile);
162 *log_table[i].logfile = NULL;
163 }
164 }
165 }
166
167 void
168 ilog(ilogfile dest, const char *format, ...)
169 {
170 FILE *logfile = *log_table[dest].logfile;
171 char buf[BUFSIZE];
172 char buf2[BUFSIZE];
173 va_list args;
174
175 if(logfile == NULL)
176 return;
177
178 va_start(args, format);
179 rb_vsnprintf(buf, sizeof(buf), format, args);
180 va_end(args);
181
182 rb_snprintf(buf2, sizeof(buf2), "%s %s\n",
183 smalldate(rb_current_time()), buf);
184
185 if(fputs(buf2, logfile) < 0)
186 {
187 fclose(logfile);
188 *log_table[dest].logfile = NULL;
189 return;
190 }
191
192 fflush(logfile);
193 }
194
195 static void
196 _iprint(const char *domain, const char *buf)
197 {
198 if (domain == NULL || buf == NULL)
199 return;
200
201 fprintf(stderr, "%8s: %s\n", domain, buf);
202 }
203
204 void
205 inotice(const char *format, ...)
206 {
207 char buf[BUFSIZE];
208 va_list args;
209
210 va_start(args, format);
211 rb_vsnprintf(buf, sizeof(buf), format, args);
212 va_end(args);
213
214 _iprint("notice", buf);
215
216 ilog(L_MAIN, "%s", buf);
217 }
218
219 void
220 iwarn(const char *format, ...)
221 {
222 char buf[BUFSIZE];
223 va_list args;
224
225 va_start(args, format);
226 rb_vsnprintf(buf, sizeof(buf), format, args);
227 va_end(args);
228
229 _iprint("warning", buf);
230
231 ilog(L_MAIN, "%s", buf);
232 }
233
234 void
235 ierror(const char *format, ...)
236 {
237 char buf[BUFSIZE];
238 va_list args;
239
240 va_start(args, format);
241 rb_vsnprintf(buf, sizeof(buf), format, args);
242 va_end(args);
243
244 _iprint("error", buf);
245
246 ilog(L_MAIN, "%s", buf);
247 }
248
249 void
250 report_operspy(struct Client *source_p, const char *token, const char *arg)
251 {
252 /* if its not my client its already propagated */
253 if(MyClient(source_p))
254 sendto_match_servs(source_p, "*", CAP_ENCAP, NOCAPS,
255 "ENCAP * OPERSPY %s %s",
256 token, arg ? arg : "");
257
258 sendto_realops_snomask(SNO_OPERSPY,
259 ConfigFileEntry.operspy_admin_only ? L_ADMIN : L_ALL,
260 "OPERSPY %s %s %s",
261 get_oper_name(source_p), token,
262 arg ? arg : "");
263
264 ilog(L_OPERSPY, "OPERSPY %s %s %s",
265 get_oper_name(source_p), token, arg ? arg : "");
266 }
267
268 const char *
269 smalldate(time_t ltime)
270 {
271 static char buf[MAX_DATE_STRING];
272 struct tm *lt;
273
274 lt = localtime(&ltime);
275
276 rb_snprintf(buf, sizeof(buf), "%d/%d/%d %02d.%02d",
277 lt->tm_year + 1900, lt->tm_mon + 1,
278 lt->tm_mday, lt->tm_hour, lt->tm_min);
279
280 return buf;
281 }
282
283 void
284 ilog_error(const char *error)
285 {
286 ilog(L_IOERROR, "%s: %d (%s)", error, errno, strerror(errno));
287
288 sendto_realops_snomask(SNO_DEBUG, L_ALL, "%s: %d (%s)", error, errno, strerror(errno));
289 }