Filter:   InfoImg
download s_log.c
Language: C
License: GPL
Copyright: (C) 2002 by the past and present ircd coders, and others.
LOC: 250
Project Info
NeoStats
Server: SourceForge
Type: cvs
...stats\neostats\NeoIRCd\src\
   .cvsignore
   .depend
   adns.c
   balloc.c
   channel.c
   channel_mode.c
   class.c
   client.c
   cloak.c
   crypt.c
   descrip.mms
   dynlink.c
   event.c
   fdlist.c
   fileio.c
   getopt.c
   hash.c
   hook.c
   hostmask.c
   irc_string.c
   ircd.c
   ircd_parser.y
   ircd_signal.c
   ircdauth.c
   kdparse.c
   linebuf.c
   list.c
   listener.c
   m_error.c
   Makefile.in
   match.c
   md5.c
   memory.c
   messages.tab
   modules.c
   motd.c
   numeric.c
   packet.c
   parse.c
   restart.c
   resv.c
   rsa.c
   s_auth.c
   s_bsd.c
   s_bsd_devpoll.c
   s_bsd_kqueue.c
   s_bsd_poll.c
   s_bsd_select.c
   s_bsd_sigio.c
   s_conf.c
   s_debug.c
   s_gline.c
   s_log.c
   s_misc.c
   s_serv.c
   s_stats.c
   s_user.c
   scache.c
   send.c
   snprintf.c
   sprintf_irc.c
   ssl.c
   tools.c
   version.c.SH
   whowas.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
 *  NeoIRCd: NeoStats Group. Based on Hybird7
 *  s_log.c: Logger functions.
 *
 *  Copyright (C) 2002 by the past and present ircd coders, and others.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 *  USA
 *
 *  $Id: s_log.c,v 1.6 2003/03/06 14:01:51 fishwaldo Exp $
 */

#include "stdinc.h"
#ifdef USE_SYSLOG
/* XXX #ifdef HAVE_SYSLOG_H */
#include <syslog.h>
#endif

#include "client.h"	/* Needed for struct Client */
#include "s_log.h"
#include "fileio.h"
#include "irc_string.h"
#include "ircd.h"
#include "s_misc.h"
#include "event.h"	/* Needed for EVH etc. */
#include "s_conf.h"
#include "memory.h"



/* some older syslogs would overflow at 2024 */
#define LOG_BUFSIZE 2000

#ifdef USE_LOGFILE
static FBFILE* logFile;
#endif
static int logLevel = INIT_LOG_LEVEL;


#ifndef SYSLOG_USERS
static EVH user_log_resync;
static FBFILE *user_log_fb=NULL;
#endif


#ifdef USE_SYSLOG
static int sysLogLevel[] = {
  LOG_CRIT,
  LOG_ERR,
  LOG_WARNING,
  LOG_NOTICE,
  LOG_INFO,
  LOG_INFO,
  LOG_INFO
};
#endif

static const char *logLevelToString[] =
{ "L_CRIT",
  "L_ERROR",
  "L_WARN",
  "L_NOTICE",
  "L_TRACE",
  "L_INFO",
  "L_DEBUG"
};

/*
 * open_log - open ircd logging file
 * returns true (1) if successful, false (0) otherwise
 */
#if defined(USE_LOGFILE) 

static int 
open_log(const char* filename)
{
#ifndef DEBUG
  if (!server_state.foreground)
  {
    close(2); /* let the logfile grab fd 2 to catch stderr */
  }
#endif
  logFile = fbopen(filename, "a");
  if (logFile == NULL)
  {
#ifdef USE_SYSLOG
    syslog(LOG_ERR, "Unable to open log file: %s: %s",
           filename, strerror(errno));
#endif
    return 0;
  }
  return 1;
}
#endif


#if defined(USE_LOGFILE) 
static void 
write_log(const char* message)
{
  char buf[LOG_BUFSIZE];

  if(logFile == NULL)
    return;

  snprintf(buf, LOG_BUFSIZE, "[%s] %s\n", smalldate(CurrentTime), message);
  fbputs(buf, logFile);
}
#endif
   
void 
ilog(int priority, const char* fmt, ...)
{
  char    buf[LOG_BUFSIZE];
  va_list args;
  assert(-1 < priority);
  if(fmt == NULL)
    return;
#ifndef DEBUG
  if (priority > logLevel)
    return;
#endif
  va_start(args, fmt);
  vsprintf(buf, fmt, args);
  va_end(args);

#ifdef USE_SYSLOG  
  if (priority <= L_DEBUG)
    syslog(sysLogLevel[priority], "%s", buf);
#endif
#ifdef DEBUG
    printf("%s\n", buf);
#endif
#if defined(USE_LOGFILE) 
  write_log(buf);
#endif
}
  
void 
init_log(const char* filename)
{
#if defined(USE_LOGFILE) 
  open_log(filename);
#endif
#ifdef USE_SYSLOG
  openlog("ircd", LOG_PID | LOG_NDELAY, LOG_FACILITY);
#endif
#ifndef SYSLOG_USERS
  eventAddIsh("user_log_resync", user_log_resync, NULL, 60);
#endif
}

void
reopen_log(const char* filename)
{
#if defined(USE_LOGFILE)
  fbclose(logFile);
  open_log(filename);
#endif  
  
}
void 
set_log_level(int level)
{
  if (L_ERROR < level && level <= L_DEBUG)
    logLevel = level;
}

int 
get_log_level(void)
{
  return( logLevel );
}

const char *
get_log_level_as_string(int level)
{
  if(level > L_DEBUG)
    level = L_DEBUG;
  else if(level < L_ERROR)
    level = L_ERROR;

  return(logLevelToString[level]);
}


/*
 * log_user_exit
 *
 * inputs	- pointer to connecting client
 * output	- NONE
 * side effects - Current exiting client is logged to
 *		  either SYSLOG or to file.
 */
void 
log_user_exit(struct Client *source_p)
{
  time_t        on_for;

  on_for = CurrentTime - source_p->firsttime;

#ifdef SYSLOG_USERS

  if (IsPerson(source_p))
    {

      ilog(L_INFO, "%s (%3ld:%02ld:%02ld): %s!%s@%s %ld/%ld\n",
	  myctime(source_p->firsttime),
	  (signed long) on_for / 3600,
	  (signed long) (on_for % 3600)/60,
	  (signed long) on_for % 60,
	  source_p->name, source_p->username, source_p->host,
	  source_p->localClient->sendK,
	  source_p->localClient->receiveK);
    }

#else
  {
    char        linebuf[BUFSIZ];

    /*
     * This conditional makes the logfile active only after
     * it's been created - thus logging can be turned off by
     * removing the file.
     * -Taner
     */
    if (IsPerson(source_p))
    {
      if (user_log_fb == NULL)
      {
	if((ConfigFileEntry.fname_userlog[0] != '\0') && 
	   (user_log_fb = fbopen(ConfigFileEntry.fname_userlog, "r")) != NULL )
	{
	  fbclose(user_log_fb);
	  user_log_fb = fbopen(ConfigFileEntry.fname_userlog, "a");
	}
      }
      if (user_log_fb != NULL)
      {
	ircsprintf(linebuf,
		   "%s (%3ld:%02ld:%02ld): %s!%s@%s %d/%d\n",
		   myctime(source_p->firsttime),
		   (signed long) on_for / 3600,
		   (signed long) (on_for % 3600)/60,
		   (signed long) on_for % 60,
		   source_p->name, source_p->username, source_p->host,
		   source_p->localClient->sendK,
		   source_p->localClient->receiveK);
	
	fbputs(linebuf, user_log_fb);
      }
    }
  }
#endif
}

#ifndef SYSLOG_USERS
/*
 * user_log_resync
 *
 * inputs	- NONE
 * output	- NONE
 * side effects	-
 */
static void
user_log_resync(void *notused)
{
  if (user_log_fb != NULL)
    {
      fbclose(user_log_fb);
      user_log_fb = NULL;
    }
}
#endif

/* XXX log_oper and log_failed_oper should be combined in future */
/*
 * log_oper
 *
 * inputs	- pointer to client being opered up
 * output	- none
 * side effects - ConfigFileEntry.fname_operlog is written to, if its present
 */

void 
log_oper(struct Client *source_p, char *name)
{
  FBFILE *oper_fb;
  char linebuf[BUFSIZE];

  if (ConfigFileEntry.fname_operlog[0] == '\0')
    return;
  
  if (IsPerson(source_p))
  {
    if((oper_fb = fbopen(ConfigFileEntry.fname_operlog, "r")) != NULL)
    {
      fbclose(oper_fb);
      oper_fb = fbopen(ConfigFileEntry.fname_operlog, "a");
    }

    if(oper_fb != NULL)
    {
      ircsprintf(linebuf, "%s OPER (%s) by (%s!%s@%s)\n",
		 myctime(CurrentTime), name, 
		 source_p->name, source_p->username,
		 source_p->host);

      fbputs(linebuf,oper_fb);
      fbclose(oper_fb);
    }
  }
}

/*
 * log_failed_oper
 *
 * inputs	- pointer to client that failed top oper up
 * output	- none
 * side effects - ConfigFileEntry.fname_foperlog is written to, if its present
 */

void 
log_failed_oper(struct Client *source_p, char *name)
{
  FBFILE *oper_fb;
  char linebuf[BUFSIZE];

  if (ConfigFileEntry.fname_foperlog[0] == '\0')
    return;
  
  if (IsPerson(source_p))
  {
    if((oper_fb = fbopen(ConfigFileEntry.fname_foperlog, "r")) != NULL)
    {
      fbclose(oper_fb);
      oper_fb = fbopen(ConfigFileEntry.fname_foperlog, "a");
    }

    if(oper_fb != NULL)
    {
      ircsprintf(linebuf, "%s FAILED OPER (%s) by (%s!%s@%s)\n",
		 myctime(CurrentTime), name, 
		 source_p->name, source_p->username, source_p->host);
      
      fbputs(linebuf,oper_fb);
      fbclose(oper_fb);
    }
  }
}