Annotation of sbbs/src/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.53 2011/09/21 03:10:53 rswindell 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 2011 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: 
                     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: 
1.1.1.2 ! root       50:        if((file=sopen(fname,O_CREAT|O_RDWR|O_BINARY|O_APPEND,SH_DENYWR,DEFFILEMODE))==-1)
1.1       root       51:                return(FALSE);
                     52: 
1.1.1.2 ! root       53:        sprintf(hdr,"SUSPECTED %s HACK ATTEMPT for user '%s' on %.24s\r\nUsing port %u at %s [%s]\r\nDetails: "
1.1       root       54:                ,prot
                     55:                ,user
1.1.1.2 ! root       56:                ,timestr(cfg,now,tstr)
1.1       root       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: 
1.1.1.2 ! root       70: BOOL sbbs_t::hacklog(char* prot, char* text)
        !            71: {
        !            72:        return ::hacklog(&cfg, prot, useron.alias, text, client_name, &client_addr);
        !            73: }
        !            74: 
1.1       root       75: extern "C" BOOL DLLCALL spamlog(scfg_t* cfg, char* prot, char* action
                     76:                                                                ,char* reason, char* host, char* ip_addr
                     77:                                                                ,char* to, char* from)
                     78: {
                     79:        char    hdr[1024];
                     80:        char    to_user[256];
                     81:        char    tstr[64];
                     82:        char    fname[MAX_PATH+1];
                     83:        int             file;
                     84:        time_t  now=time(NULL);
                     85: 
                     86:        sprintf(fname,"%sspam.log",cfg->logs_dir);
                     87: 
1.1.1.2 ! root       88:        if((file=sopen(fname,O_CREAT|O_RDWR|O_BINARY|O_APPEND,SH_DENYWR,DEFFILEMODE))==-1)
1.1       root       89:                return(FALSE);
                     90: 
                     91:        if(to==NULL)
                     92:                to_user[0]=0;
                     93:        else
                     94:                sprintf(to_user,"to: %.128s",to);
                     95: 
                     96:        if(from==NULL)
                     97:                from=host;
                     98:                
                     99:        sprintf(hdr,"SUSPECTED %s SPAM %s on %.24s\r\nHost: %s [%s]\r\nFrom: %.128s %s\r\nReason: "
                    100:                ,prot
                    101:                ,action
1.1.1.2 ! root      102:                ,timestr(cfg,now,tstr)
1.1       root      103:                ,host
                    104:                ,ip_addr
                    105:                ,from
                    106:                ,to_user
                    107:                );
                    108:        write(file,hdr,strlen(hdr));
                    109:        if(reason!=NULL)
                    110:                write(file,reason,strlen(reason));
                    111:        write(file,crlf,2);
                    112:        write(file,crlf,2);
                    113:        close(file);
                    114: 
                    115:        return(TRUE);
                    116: }
                    117: 
1.1.1.2 ! root      118: extern "C" int DLLCALL errorlog(scfg_t* cfg, const char* host, const char* text)
        !           119: {
        !           120:        FILE*   fp;
        !           121:        char    buf[128];
        !           122:        char    path[MAX_PATH+1];
        !           123: 
        !           124:        sprintf(path,"%serror.log",cfg->logs_dir);
        !           125:        if((fp=fnopen(NULL,path,O_WRONLY|O_CREAT|O_APPEND))==NULL)
        !           126:                return -1; 
        !           127:        fprintf(fp,"%s %s\r\n%s\r\n\r\n", timestr(cfg,time(NULL),buf), host==NULL ? "":host, text);
        !           128:        fclose(fp);
        !           129:        return 0;
        !           130: }
        !           131: 
        !           132: void sbbs_t::logentry(const char *code, const char *entry)
1.1       root      133: {
                    134:        char str[512];
                    135: 
                    136:        now=time(NULL);
1.1.1.2 ! root      137:        sprintf(str,"Node %2d  %s\r\n   %s",cfg.node_num,timestr(now),entry);
1.1       root      138:        logline(code,str);
                    139: }
                    140: 
                    141: /****************************************************************************/
                    142: /* Writes 'str' verbatim into node.log                                                                                 */
                    143: /****************************************************************************/
                    144: void sbbs_t::log(char *str)
                    145: {
                    146:        if(logfile_fp==NULL || online==ON_LOCAL) return;
                    147:        if(logcol>=78 || (78-logcol)<strlen(str)) {
                    148:                fprintf(logfile_fp,"\r\n");
1.1.1.2 ! root      149:                logcol=1; 
        !           150:        }
1.1       root      151:        if(logcol==1) {
                    152:                fprintf(logfile_fp,"   ");
1.1.1.2 ! root      153:                logcol=4; 
        !           154:        }
1.1       root      155:        fprintf(logfile_fp,str);
1.1.1.2 ! root      156:        if(*lastchar(str)==LF) {
1.1       root      157:                logcol=1;
1.1.1.2 ! root      158:                fflush(logfile_fp);
        !           159:        }
1.1       root      160:        else
                    161:                logcol+=strlen(str);
                    162: }
                    163: 
1.1.1.2 ! root      164: bool sbbs_t::syslog(const char* code, const char *entry)
1.1       root      165: {              
                    166:        char    fname[MAX_PATH+1];
                    167:        char    str[128];
                    168:        char    tmp[64];
                    169:        int             file;
                    170:        struct tm tm;
                    171: 
                    172:        now=time(NULL);
                    173:        if(localtime_r(&now,&tm)==NULL)
                    174:                return(false);
                    175:        sprintf(fname,"%slogs/%2.2d%2.2d%2.2d.log",cfg.logs_dir,tm.tm_mon+1,tm.tm_mday
                    176:                ,TM_YEAR(tm.tm_year));
                    177:        if((file=nopen(fname,O_WRONLY|O_APPEND|O_CREAT))==-1) {
                    178:                lprintf(LOG_ERR,"!ERRROR %d opening/creating %s",errno,fname); 
                    179:                return(false);
                    180:        }
                    181: 
                    182:        sprintf(str,"%-2.2s %s  %s\r\n\r\n",code, hhmmtostr(&cfg,&tm,tmp), entry);
                    183:        write(file,str,strlen(str));
                    184:        close(file);
                    185: 
                    186:        return(true);
                    187: }
                    188: 
                    189: /****************************************************************************/
1.1.1.2 ! root      190: /* Writes 'str' on it's own line in node.log (using LOG_INFO level)                    */
        !           191: /****************************************************************************/
        !           192: void sbbs_t::logline(const char *code, const char *str)
        !           193: {
        !           194:        logline(LOG_INFO, code, str);
        !           195: }
        !           196: 
        !           197: /****************************************************************************/
1.1       root      198: /* Writes 'str' on it's own line in node.log                                                           */
                    199: /****************************************************************************/
1.1.1.2 ! root      200: void sbbs_t::logline(int level, const char *code, const char *str)
1.1       root      201: {
                    202:        if(strchr(str,'\n')==NULL) {    // Keep the console log pretty
                    203:                if(online==ON_LOCAL)
1.1.1.2 ! root      204:                        eprintf(level,"%s",str);
1.1       root      205:                else
1.1.1.2 ! root      206:                        lprintf(level,"Node %d %s", cfg.node_num, str);
1.1       root      207:        }
                    208:        if(logfile_fp==NULL || (online==ON_LOCAL && strcmp(code,"!!"))) return;
                    209:        if(logcol!=1)
                    210:                fprintf(logfile_fp,"\r\n");
                    211:        fprintf(logfile_fp,"%-2.2s %s\r\n",code,str);
                    212:        logcol=1;
1.1.1.2 ! root      213:        fflush(logfile_fp);
1.1       root      214: }
                    215: 
                    216: /****************************************************************************/
                    217: /* Writes a comma then 'ch' to log, tracking column.                                           */
                    218: /****************************************************************************/
                    219: void sbbs_t::logch(char ch, bool comma)
                    220: {
                    221: 
                    222:        if(logfile_fp==NULL || (online==ON_LOCAL)) return;
                    223:        if((uchar)ch<' ')       /* Don't log control chars */
                    224:                return;
                    225:        if(logcol==1) {
                    226:                logcol=4;
                    227:                fprintf(logfile_fp,"   "); 
                    228:        }
                    229:        else if(logcol>=78) {
                    230:                logcol=4;
                    231:                fprintf(logfile_fp,"\r\n   "); 
                    232:        }
                    233:        if(comma && logcol!=4) {
                    234:                fprintf(logfile_fp,",");
                    235:                logcol++; 
                    236:        }
                    237:        if(ch&0x80) {
                    238:                ch&=0x7f;
                    239:                if(ch<' ')
                    240:                        return;
                    241:                fprintf(logfile_fp,"/"); 
                    242:        }
                    243:        fprintf(logfile_fp,"%c",ch);
                    244:        logcol++;
                    245: }
                    246: 
                    247: /****************************************************************************/
                    248: /* Error handling routine. Prints to local and remote screens the error     */
                    249: /* information, function, action, object and access and then attempts to    */
                    250: /* write the error information into the file ERROR.LOG and NODE.LOG         */
                    251: /****************************************************************************/
                    252: void sbbs_t::errormsg(int line, const char *source, const char* action, const char *object
                    253:                                          ,ulong access, const char *extinfo)
                    254: {
                    255:        const char*     src;
                    256:     char       str[2048];
                    257: 
                    258:        /* prevent recursion */
                    259:        if(errormsg_inside)
                    260:                return;
                    261:        errormsg_inside=true;
                    262: 
                    263:        /* Don't log path to source code */
                    264:        src=getfname(source);
1.1.1.2 ! root      265:        safe_snprintf(str,sizeof(str),"ERROR %d (%s) "
1.1       root      266: #ifdef _WIN32
1.1.1.2 ! root      267:                "(WinError %u) "
1.1       root      268: #endif
1.1.1.2 ! root      269:                "in %s line %u %s \"%s\" access=%ld %s%s"
        !           270:                ,errno,STRERROR(errno)
1.1       root      271: #ifdef _WIN32
                    272:                ,GetLastError()
                    273: #endif
1.1.1.2 ! root      274:                ,src, line, action, object, access
        !           275:                ,extinfo==NULL ? "":"info="
        !           276:                ,extinfo==NULL ? "":extinfo);
1.1       root      277:        if(online==ON_LOCAL)
                    278:                eprintf(LOG_ERR,"%s",str);
                    279:        else {
1.1.1.2 ! root      280:                int savatr=curatr;
        !           281:                lprintf(LOG_ERR,"Node %d !%s",cfg.node_num, str);
        !           282:                attr(cfg.color[clr_err]);
        !           283:                bprintf("\7\r\n!ERROR %s %s\r\n", action, object);   /* tell user about error */
        !           284:                bputs("\r\nThe sysop has been notified.\r\n");
        !           285:                pause();
        !           286:                attr(savatr);
1.1       root      287:                CRLF;
                    288:        }
1.1.1.2 ! root      289:        safe_snprintf(str,sizeof(str),"ERROR %s %s", action, object);
1.1       root      290:        if(cfg.node_num>0) {
                    291:                getnodedat(cfg.node_num,&thisnode,1);
                    292:                if(thisnode.errors<UCHAR_MAX)
                    293:                        thisnode.errors++;
                    294:                criterrs=thisnode.errors;
                    295:                putnodedat(cfg.node_num,&thisnode);
                    296:        }
                    297:        now=time(NULL);
1.1.1.2 ! root      298: 
        !           299:        if(logfile_fp!=NULL) {
        !           300:                if(logcol!=1)
        !           301:                        fprintf(logfile_fp,"\r\n");
        !           302:                fprintf(logfile_fp,"!! %s\r\n",str);
        !           303:                logcol=1;
        !           304:                fflush(logfile_fp);
        !           305:        }
        !           306: 
        !           307:        errormsg_inside=false;
1.1       root      308: }

unix.superglobalmegacorp.com

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