]>
Commit | Line | Data |
---|---|---|
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 | #include "stdinc.h" | |
36 | #include "ircd_defs.h" | |
37 | #include "logger.h" | |
38 | #include "s_conf.h" | |
39 | #include "send.h" | |
40 | #include "client.h" | |
41 | #include "s_serv.h" | |
42 | ||
43 | static FILE *log_main; | |
44 | static FILE *log_user; | |
45 | static FILE *log_fuser; | |
46 | static FILE *log_oper; | |
47 | static FILE *log_foper; | |
48 | static FILE *log_server; | |
49 | static FILE *log_kill; | |
50 | static FILE *log_kline; | |
51 | static FILE *log_operspy; | |
52 | static FILE *log_ioerror; | |
53 | ||
54 | struct log_struct | |
55 | { | |
56 | char **name; | |
57 | FILE **logfile; | |
58 | }; | |
59 | ||
60 | static 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 }, | |
70 | { &ConfigFileEntry.fname_operspylog, &log_operspy }, | |
71 | { &ConfigFileEntry.fname_ioerrorlog, &log_ioerror } | |
72 | }; | |
73 | ||
74 | static void | |
75 | verify_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); | |
82 | ||
83 | if(access(dirname, F_OK) == -1) | |
84 | { | |
85 | snprintf(buf, sizeof(buf), "WARNING: Unable to access logfile %s - parent directory %s does not exist", filename, dirname); | |
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 | { | |
96 | snprintf(buf, sizeof(buf), "WARNING: Unable to access logfile %s - access to parent directory %s failed: %s", | |
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 | } | |
104 | ||
105 | if(access(filename, W_OK) == -1) | |
106 | { | |
107 | snprintf(buf, sizeof(buf), "WARNING: Access denied for logfile %s: %s", filename, strerror(errno)); | |
108 | if(testing_conf || server_state_foreground) | |
109 | fprintf(stderr, "%s\n", buf); | |
110 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s", buf); | |
111 | return; | |
112 | } | |
113 | return; | |
114 | } | |
115 | ||
116 | void | |
117 | init_main_logfile(void) | |
118 | { | |
119 | verify_logfile_access(logFileName); | |
120 | if(log_main == NULL) | |
121 | { | |
122 | log_main = fopen(logFileName, "a"); | |
123 | } | |
124 | } | |
125 | ||
126 | void | |
127 | open_logfiles(void) | |
128 | { | |
129 | int i; | |
130 | ||
131 | close_logfiles(); | |
132 | ||
133 | log_main = fopen(logFileName, "a"); | |
134 | ||
135 | /* log_main is handled above, so just do the rest */ | |
136 | for(i = 1; i < LAST_LOGFILE; i++) | |
137 | { | |
138 | /* reopen those with paths */ | |
139 | if(!EmptyString(*log_table[i].name)) | |
140 | { | |
141 | verify_logfile_access(*log_table[i].name); | |
142 | *log_table[i].logfile = fopen(*log_table[i].name, "a"); | |
143 | } | |
144 | } | |
145 | } | |
146 | ||
147 | void | |
148 | close_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 | { | |
158 | if(*log_table[i].logfile != NULL) | |
159 | { | |
160 | fclose(*log_table[i].logfile); | |
161 | *log_table[i].logfile = NULL; | |
162 | } | |
163 | } | |
164 | } | |
165 | ||
166 | void | |
167 | ilog(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); | |
178 | vsnprintf(buf, sizeof(buf), format, args); | |
179 | va_end(args); | |
180 | ||
181 | snprintf(buf2, sizeof(buf2), "%s %s\n", | |
182 | smalldate(rb_current_time()), buf); | |
183 | ||
184 | if(fputs(buf2, logfile) < 0) | |
185 | { | |
186 | fclose(logfile); | |
187 | *log_table[dest].logfile = NULL; | |
188 | return; | |
189 | } | |
190 | ||
191 | fflush(logfile); | |
192 | } | |
193 | ||
194 | static void | |
195 | _iprint(const char *domain, const char *buf) | |
196 | { | |
197 | if (domain == NULL || buf == NULL) | |
198 | return; | |
199 | ||
200 | fprintf(stderr, "%8s: %s\n", domain, buf); | |
201 | } | |
202 | ||
203 | void | |
204 | inotice(const char *format, ...) | |
205 | { | |
206 | char buf[BUFSIZE]; | |
207 | va_list args; | |
208 | ||
209 | va_start(args, format); | |
210 | vsnprintf(buf, sizeof(buf), format, args); | |
211 | va_end(args); | |
212 | ||
213 | _iprint("notice", buf); | |
214 | ||
215 | ilog(L_MAIN, "%s", buf); | |
216 | } | |
217 | ||
218 | void | |
219 | iwarn(const char *format, ...) | |
220 | { | |
221 | char buf[BUFSIZE]; | |
222 | va_list args; | |
223 | ||
224 | va_start(args, format); | |
225 | vsnprintf(buf, sizeof(buf), format, args); | |
226 | va_end(args); | |
227 | ||
228 | _iprint("warning", buf); | |
229 | ||
230 | ilog(L_MAIN, "%s", buf); | |
231 | } | |
232 | ||
233 | void | |
234 | ierror(const char *format, ...) | |
235 | { | |
236 | char buf[BUFSIZE]; | |
237 | va_list args; | |
238 | ||
239 | va_start(args, format); | |
240 | vsnprintf(buf, sizeof(buf), format, args); | |
241 | va_end(args); | |
242 | ||
243 | _iprint("error", buf); | |
244 | ||
245 | ilog(L_MAIN, "%s", buf); | |
246 | } | |
247 | ||
248 | void | |
249 | report_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 | ||
267 | const char * | |
268 | smalldate(time_t ltime) | |
269 | { | |
270 | static char buf[MAX_DATE_STRING]; | |
271 | struct tm *lt; | |
272 | ||
273 | lt = localtime(<ime); | |
274 | ||
275 | snprintf(buf, sizeof(buf), "%d/%d/%d %02d.%02d", | |
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 | ||
282 | void | |
283 | ilog_error(const char *error) | |
284 | { | |
285 | int e; | |
286 | const char *errstr; | |
287 | ||
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); | |
294 | } |