Annotation of sbbs/sbbs3/logfile.cpp, revision 1.1.1.2

1.1       root        1: /* logfile.cpp */
                      2: 
                      3: /* Synchronet log file routines */
                      4: 
1.1.1.2 ! root        5: /* $Id: logfile.cpp,v 1.36 2004/05/30 06:47:52 deuce Exp $ */
1.1       root        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:  *                                                                                                                                                     *
1.1.1.2 ! root       11:  * Copyright 2003 Rob Swindell - http://www.synchro.net/copyright.html         *
1.1       root       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: 
1.1.1.2 ! root       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: 
1.1       root      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: 
1.1.1.2 ! root      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: 
1.1       root      166: /****************************************************************************/
                    167: /* Writes 'str' on it's own line in node.log                                                           */
                    168: /****************************************************************************/
                    169: void sbbs_t::logline(char *code, char *str)
                    170: {
1.1.1.2 ! root      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:        }
1.1       root      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: /****************************************************************************/
1.1.1.2 ! root      187: void sbbs_t::logch(char ch, bool comma)
1.1       root      188: {
                    189: 
                    190:        if(logfile_fp==NULL || (online==ON_LOCAL)) return;
1.1.1.2 ! root      191:        if((uchar)ch<' ')       /* Don't log control chars */
1.1       root      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;
1.1.1.2 ! root      207:                if(ch<' ')
1.1       root      208:                        return;
                    209:                fprintf(logfile_fp,"/"); 
                    210:        }
                    211:        fprintf(logfile_fp,"%c",ch);
                    212:        logcol++;
                    213: }
                    214: 
1.1.1.2 ! root      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, 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:     char*      actstr;
        !           227: 
        !           228:        /* prevent recursion */
        !           229:        if(errormsg_inside)
        !           230:                return;
        !           231:        errormsg_inside=true;
        !           232: 
        !           233:        /* Don't log path to source code */
        !           234:        src=getfname(source);
        !           235: 
        !           236:        switch(action) {
        !           237:                case ERR_OPEN:
        !           238:                        actstr="opening";
        !           239:                        break;
        !           240:                case ERR_CLOSE:
        !           241:                        actstr="closing";
        !           242:                        break;
        !           243:                case ERR_FDOPEN:
        !           244:                        actstr="fdopen";
        !           245:                        break;
        !           246:                case ERR_READ:
        !           247:                        actstr="reading";
        !           248:                        break;
        !           249:                case ERR_WRITE:
        !           250:                        actstr="writing";
        !           251:                        break;
        !           252:                case ERR_REMOVE:
        !           253:                        actstr="removing";
        !           254:                        break;
        !           255:                case ERR_ALLOC:
        !           256:                        actstr="allocating memory";
        !           257:                        break;
        !           258:                case ERR_CHK:
        !           259:                        actstr="checking";
        !           260:                        break;
        !           261:                case ERR_LEN:
        !           262:                        actstr="checking length";
        !           263:                        break;
        !           264:                case ERR_EXEC:
        !           265:                        actstr="executing";
        !           266:                        break;
        !           267:                case ERR_CHDIR:
        !           268:                        actstr="changing directory";
        !           269:                        break;
        !           270:                case ERR_CREATE:
        !           271:                        actstr="creating";
        !           272:                        break;
        !           273:                case ERR_LOCK:
        !           274:                        actstr="locking";
        !           275:                        break;
        !           276:                case ERR_UNLOCK:
        !           277:                        actstr="unlocking";
        !           278:                        break;
        !           279:                case ERR_TIMEOUT:
        !           280:                actstr="time-out waiting for resource";
        !           281:                        break;
        !           282:                case ERR_IOCTL:
        !           283:                actstr="sending IOCTL";
        !           284:                        break;
        !           285:                default:
        !           286:                        actstr="UNKNOWN"; 
        !           287:                        break;
        !           288:        }
        !           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, actstr, 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",actstr);   /* 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,actstr,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(__MSDOS__)
        !           333:        if(_doserrno && _doserrno!=(ulong)errno) {
        !           334:                sprintf(tmp,"\r\n  doserrno: %d",_doserrno);
        !           335:                strcat(str,tmp); 
        !           336:        }
        !           337:        _doserrno=0;
        !           338: #endif
        !           339: #if defined(_WIN32)
        !           340:        if(GetLastError()!=0) {
        !           341:                sprintf(tmp,"\r\n  WinError: %d (0x%X)",GetLastError(), GetLastError());
        !           342:                strcat(str,tmp);
        !           343:        }
        !           344: #endif
        !           345:        errorlog(str);
        !           346:        errormsg_inside=false;
        !           347: }
        !           348: 
        !           349: /*****************************************************************************/
        !           350: /* Error logging to NODE.LOG and DATA\ERROR.LOG function                     */
        !           351: /*****************************************************************************/
        !           352: void sbbs_t::errorlog(char *text)
        !           353: {
        !           354:     char hdr[256],str[256],tmp2[256];
        !           355:     int file;
        !           356: 
        !           357:        if(errorlog_inside)             /* let's not go recursive on this puppy */
        !           358:                return;
        !           359:        errorlog_inside=1;
        !           360:        if(cfg.node_num>0) {
        !           361:                getnodedat(cfg.node_num,&thisnode,1);
        !           362:                if(thisnode.errors<UCHAR_MAX)
        !           363:                        thisnode.errors++;
        !           364:                criterrs=thisnode.errors;
        !           365:                putnodedat(cfg.node_num,&thisnode);
        !           366:        }
        !           367:        now=time(NULL);
        !           368:        logline("!!",text);
        !           369:        sprintf(str,"%serror.log",cfg.logs_dir);
        !           370:        if((file=nopen(str,O_WRONLY|O_CREAT|O_APPEND))==-1) {
        !           371:                sprintf(tmp2,"!ERROR %d opening/creating %s",errno,str);
        !           372:                logline("!!",tmp2);
        !           373:                errorlog_inside=0;
        !           374:                return; }
        !           375:        sprintf(hdr,"%s Node %2d: %s #%d"
        !           376:                ,timestr(&now),cfg.node_num,useron.alias,useron.number);
        !           377:        write(file,hdr,strlen(hdr));
        !           378:        write(file,crlf,2);
        !           379:        write(file,text,strlen(text));
        !           380:        write(file,"\r\n\r\n",4);
        !           381:        close(file);
        !           382:        errorlog_inside=0;
        !           383: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.