|
|
1.1 root 1: /* logfile.cpp */
2:
3: /* Synchronet log file routines */
4:
5: /* $Id: logfile.cpp,v 1.38 2006/02/03 03:47:10 rswindell Exp $ */
6:
7: /****************************************************************************
8: * @format.tab-size 4 (Plain Text/Source Code File Header) *
9: * @format.use-tabs true (see http://www.synchro.net/ptsc_hdr.html) *
10: * *
11: * Copyright 2006 Rob Swindell - http://www.synchro.net/copyright.html *
12: * *
13: * This program is free software; you can redistribute it and/or *
14: * modify it under the terms of the GNU General Public License *
15: * as published by the Free Software Foundation; either version 2 *
16: * of the License, or (at your option) any later version. *
17: * See the GNU General Public License for more details: gpl.txt or *
18: * http://www.fsf.org/copyleft/gpl.html *
19: * *
20: * Anonymous FTP access to the most recent released source is available at *
21: * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net *
22: * *
23: * Anonymous CVS access to the development source and modification history *
24: * is available at cvs.synchro.net:/cvsroot/sbbs, example: *
25: * cvs -d :pserver:[email protected]:/cvsroot/sbbs login *
26: * (just hit return, no password is necessary) *
27: * cvs -d :pserver:[email protected]:/cvsroot/sbbs checkout src *
28: * *
29: * For Synchronet coding style and modification guidelines, see *
30: * http://www.synchro.net/source.html *
31: * *
32: * You are encouraged to submit any modifications (preferably in Unix diff *
33: * format) via e-mail to [email protected] *
34: * *
35: * Note: If this box doesn't appear square, then you need to fix your tabs. *
36: ****************************************************************************/
37:
38: #include "sbbs.h"
39:
40: extern "C" BOOL DLLCALL hacklog(scfg_t* cfg, char* prot, char* user, char* text, char* host, SOCKADDR_IN* addr)
41: {
42: char hdr[1024];
43: char tstr[64];
44: char fname[MAX_PATH+1];
45: int file;
46: time_t now=time(NULL);
47:
48: sprintf(fname,"%shack.log",cfg->logs_dir);
49:
50: if((file=sopen(fname,O_CREAT|O_RDWR|O_BINARY|O_APPEND,SH_DENYWR,S_IREAD|S_IWRITE))==-1)
51: return(FALSE);
52:
53: sprintf(hdr,"SUSPECTED %s HACK ATTEMPT from %s on %.24s\r\nUsing port %u at %s [%s]\r\nDetails: "
54: ,prot
55: ,user
56: ,timestr(cfg,&now,tstr)
57: ,addr->sin_port
58: ,host
59: ,inet_ntoa(addr->sin_addr)
60: );
61: write(file,hdr,strlen(hdr));
62: write(file,text,strlen(text));
63: write(file,crlf,2);
64: write(file,crlf,2);
65: close(file);
66:
67: return(TRUE);
68: }
69:
70: extern "C" BOOL DLLCALL spamlog(scfg_t* cfg, char* prot, char* action
71: ,char* reason, char* host, char* ip_addr
72: ,char* to, char* from)
73: {
74: char hdr[1024];
75: char to_user[256];
76: char tstr[64];
77: char fname[MAX_PATH+1];
78: int file;
79: time_t now=time(NULL);
80:
81: sprintf(fname,"%sspam.log",cfg->logs_dir);
82:
83: if((file=sopen(fname,O_CREAT|O_RDWR|O_BINARY|O_APPEND,SH_DENYWR,S_IREAD|S_IWRITE))==-1)
84: return(FALSE);
85:
86: if(to==NULL)
87: to_user[0]=0;
88: else
89: sprintf(to_user,"to: %.128s",to);
90:
91: if(from==NULL)
92: from=host;
93:
94: sprintf(hdr,"SUSPECTED %s SPAM %s on %.24s\r\nHost: %s [%s]\r\nFrom: %.128s %s\r\nReason: "
95: ,prot
96: ,action
97: ,timestr(cfg,&now,tstr)
98: ,host
99: ,ip_addr
100: ,from
101: ,to_user
102: );
103: write(file,hdr,strlen(hdr));
104: if(reason!=NULL)
105: write(file,reason,strlen(reason));
106: write(file,crlf,2);
107: write(file,crlf,2);
108: close(file);
109:
110: return(TRUE);
111: }
112:
113: void sbbs_t::logentry(char *code, char *entry)
114: {
115: char str[512];
116:
117: now=time(NULL);
118: sprintf(str,"Node %2d %s\r\n %s",cfg.node_num,timestr(&now),entry);
119: logline(code,str);
120: }
121:
122: /****************************************************************************/
123: /* Writes 'str' verbatim into node.log */
124: /****************************************************************************/
125: void sbbs_t::log(char *str)
126: {
127: if(logfile_fp==NULL || online==ON_LOCAL) return;
128: if(logcol>=78 || (78-logcol)<strlen(str)) {
129: fprintf(logfile_fp,"\r\n");
130: logcol=1; }
131: if(logcol==1) {
132: fprintf(logfile_fp," ");
133: logcol=4; }
134: fprintf(logfile_fp,str);
135: if(str[strlen(str)-1]==LF)
136: logcol=1;
137: else
138: logcol+=strlen(str);
139: }
140:
141: bool sbbs_t::syslog(char* code, char *entry)
142: {
143: char fname[MAX_PATH+1];
144: char str[128];
145: char tmp[64];
146: int file;
147: struct tm tm;
148:
149: now=time(NULL);
150: if(localtime_r(&now,&tm)==NULL)
151: return(false);
152: sprintf(fname,"%slogs/%2.2d%2.2d%2.2d.log",cfg.logs_dir,tm.tm_mon+1,tm.tm_mday
153: ,TM_YEAR(tm.tm_year));
154: if((file=nopen(fname,O_WRONLY|O_APPEND|O_CREAT))==-1) {
155: lprintf(LOG_ERR,"!ERRROR %d opening/creating %s",errno,fname);
156: return(false);
157: }
158:
159: sprintf(str,"%-2.2s %s %s\r\n\r\n",code, hhmmtostr(&cfg,&tm,tmp), entry);
160: write(file,str,strlen(str));
161: close(file);
162:
163: return(true);
164: }
165:
166: /****************************************************************************/
167: /* Writes 'str' on it's own line in node.log */
168: /****************************************************************************/
169: void sbbs_t::logline(char *code, char *str)
170: {
171: if(strchr(str,'\n')==NULL) { // Keep the console log pretty
172: if(online==ON_LOCAL)
173: eprintf(LOG_INFO,"%s",str);
174: else
175: lprintf(LOG_INFO,"Node %d %s", cfg.node_num, str);
176: }
177: if(logfile_fp==NULL || (online==ON_LOCAL && strcmp(code,"!!"))) return;
178: if(logcol!=1)
179: fprintf(logfile_fp,"\r\n");
180: fprintf(logfile_fp,"%-2.2s %s\r\n",code,str);
181: logcol=1;
182: }
183:
184: /****************************************************************************/
185: /* Writes a comma then 'ch' to log, tracking column. */
186: /****************************************************************************/
187: void sbbs_t::logch(char ch, bool comma)
188: {
189:
190: if(logfile_fp==NULL || (online==ON_LOCAL)) return;
191: if((uchar)ch<' ') /* Don't log control chars */
192: return;
193: if(logcol==1) {
194: logcol=4;
195: fprintf(logfile_fp," ");
196: }
197: else if(logcol>=78) {
198: logcol=4;
199: fprintf(logfile_fp,"\r\n ");
200: }
201: if(comma && logcol!=4) {
202: fprintf(logfile_fp,",");
203: logcol++;
204: }
205: if(ch&0x80) {
206: ch&=0x7f;
207: if(ch<' ')
208: return;
209: fprintf(logfile_fp,"/");
210: }
211: fprintf(logfile_fp,"%c",ch);
212: logcol++;
213: }
214:
215: /****************************************************************************/
216: /* Error handling routine. Prints to local and remote screens the error */
217: /* information, function, action, object and access and then attempts to */
218: /* write the error information into the file ERROR.LOG and NODE.LOG */
219: /****************************************************************************/
220: void sbbs_t::errormsg(int line, const char *source, const char* action, const char *object
221: ,ulong access, const char *extinfo)
222: {
223: const char* src;
224: char str[2048];
225: char tmp[512];
226:
227: /* prevent recursion */
228: if(errormsg_inside)
229: return;
230: errormsg_inside=true;
231:
232: /* Don't log path to source code */
233: src=getfname(source);
234: #if 0
235: switch(action) {
236: case ERR_OPEN:
237: actstr="opening";
238: break;
239: case ERR_CLOSE:
240: actstr="closing";
241: break;
242: case ERR_FDOPEN:
243: actstr="fdopen";
244: break;
245: case ERR_READ:
246: actstr="reading";
247: break;
248: case ERR_WRITE:
249: actstr="writing";
250: break;
251: case ERR_REMOVE:
252: actstr="removing";
253: break;
254: case ERR_ALLOC:
255: actstr="allocating memory";
256: break;
257: case ERR_CHK:
258: actstr="checking";
259: break;
260: case ERR_LEN:
261: actstr="checking length";
262: break;
263: case ERR_EXEC:
264: actstr="executing";
265: break;
266: case ERR_CHDIR:
267: actstr="changing directory";
268: break;
269: case ERR_CREATE:
270: actstr="creating";
271: break;
272: case ERR_LOCK:
273: actstr="locking";
274: break;
275: case ERR_UNLOCK:
276: actstr="unlocking";
277: break;
278: case ERR_TIMEOUT:
279: actstr="time-out waiting for resource";
280: break;
281: case ERR_IOCTL:
282: actstr="sending IOCTL";
283: break;
284: default:
285: actstr="UNKNOWN";
286: break;
287: }
288: #endif
289: sprintf(str,"Node %d !ERROR %d "
290: #ifdef _WIN32
291: "(WinError %d) "
292: #endif
293: "in %s line %d %s \"%s\" access=%ld"
294: ,cfg.node_num, errno
295: #ifdef _WIN32
296: ,GetLastError()
297: #endif
298: ,src, line, action, object, access);
299: if(online==ON_LOCAL)
300: eprintf(LOG_ERR,"%s",str);
301: else {
302: lprintf(LOG_ERR,"%s",str);
303: bprintf("\7\r\nERROR - action: %s",action); /* tell user about error */
304: bprintf("\7\r\n object: %s",object);
305: bprintf("\7\r\n access: %ld",access);
306: if(access>9 && (long)access!=-1 && (short)access!=-1 && (char)access!=-1)
307: bprintf(" (0x%lX)",access);
308: if(cfg.sys_misc&SM_ERRALARM) {
309: sbbs_beep(500,220); sbbs_beep(250,220);
310: sbbs_beep(500,220); sbbs_beep(250,220);
311: sbbs_beep(500,220); sbbs_beep(250,220);
312: nosound(); }
313: bputs("\r\n\r\nThe sysop has been notified. <Hit a key>");
314: getkey(0);
315: CRLF;
316: }
317: sprintf(str,"\r\n source: %s\r\n line: %d\r\n action: %s\r\n"
318: " object: %s\r\n access: %ld"
319: ,src,line,action,object,access);
320: if(access>9 && (long)access!=-1 && (short)access!=-1 && (char)access!=-1) {
321: sprintf(tmp," (0x%lX)",access);
322: strcat(str,tmp); }
323: if(extinfo!=NULL) {
324: sprintf(tmp,"\r\n info: %s",extinfo);
325: strcat(str,tmp);
326: }
327: if(errno) {
328: sprintf(tmp,"\r\n errno: %d (%s)",errno,strerror(errno));
329: strcat(str,tmp);
330: errno=0;
331: }
332: #if defined(_WIN32)
333: if(GetLastError()!=0) {
334: sprintf(tmp,"\r\n WinError: %d (0x%X)",GetLastError(), GetLastError());
335: strcat(str,tmp);
336: }
337: #endif
338: errorlog(str);
339: errormsg_inside=false;
340: }
341:
342: /*****************************************************************************/
343: /* Error logging to NODE.LOG and DATA\ERROR.LOG function */
344: /*****************************************************************************/
345: void sbbs_t::errorlog(char *text)
346: {
347: char hdr[256],str[256],tmp2[256];
348: int file;
349:
350: if(errorlog_inside) /* let's not go recursive on this puppy */
351: return;
352: errorlog_inside=1;
353: if(cfg.node_num>0) {
354: getnodedat(cfg.node_num,&thisnode,1);
355: if(thisnode.errors<UCHAR_MAX)
356: thisnode.errors++;
357: criterrs=thisnode.errors;
358: putnodedat(cfg.node_num,&thisnode);
359: }
360: now=time(NULL);
361: logline("!!",text);
362: sprintf(str,"%serror.log",cfg.logs_dir);
363: if((file=nopen(str,O_WRONLY|O_CREAT|O_APPEND))==-1) {
364: sprintf(tmp2,"!ERROR %d opening/creating %s",errno,str);
365: logline("!!",tmp2);
366: errorlog_inside=0;
367: return; }
368: sprintf(hdr,"%s Node %2d: %s #%d"
369: ,timestr(&now),cfg.node_num,useron.alias,useron.number);
370: write(file,hdr,strlen(hdr));
371: write(file,crlf,2);
372: write(file,text,strlen(text));
373: write(file,"\r\n\r\n",4);
374: close(file);
375: errorlog_inside=0;
376: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.