Skip to content
Snippets Groups Projects
logfile.cpp 8.07 KiB
Newer Older
/* Synchronet log file routines */

/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
rswindell's avatar
rswindell committed
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
 *																			*
 * 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.					*
 * See the GNU General Public License for more details: gpl.txt or			*
 * http://www.fsf.org/copyleft/gpl.html										*
 *																			*
 * Anonymous FTP access to the most recent released source is available at	*
 * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net	*
 *																			*
 * Anonymous CVS access to the development source and modification history	*
 * is available at cvs.synchro.net:/cvsroot/sbbs, example:					*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login			*
 *     (just hit return, no password is necessary)							*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src		*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * You are encouraged to submit any modifications (preferably in Unix diff	*
 * format) via e-mail to mods@synchro.net									*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

#include "sbbs.h"

rswindell's avatar
rswindell committed
const char* log_line_ending = "\r\n";

extern "C" BOOL DLLCALL hacklog(scfg_t* cfg, const char* prot, const char* user, const char* text, const char* host, union xp_sockaddr* addr)
rswindell's avatar
rswindell committed
	FILE*	fp;
deuce's avatar
deuce committed
	char	ip[INET6_ADDRSTRLEN];
rswindell's avatar
rswindell committed
	SAFEPRINTF(fname, "%shack.log", cfg->logs_dir);
rswindell's avatar
rswindell committed
	if((fp = fnopen(NULL, fname, O_WRONLY|O_CREAT|O_APPEND)) == NULL)
		return false;
deuce's avatar
deuce committed
	inet_addrtop(addr, ip, sizeof(ip));
rswindell's avatar
rswindell committed
	fprintf(fp,"SUSPECTED %s HACK ATTEMPT for user '%s' on %.24s%sUsing port %u at %s [%s]%s"
rswindell's avatar
rswindell committed
		,log_line_ending
deuce's avatar
deuce committed
		,inet_addrport(addr)
deuce's avatar
deuce committed
		,ip
rswindell's avatar
rswindell committed
		,log_line_ending
rswindell's avatar
rswindell committed
	if(text != NULL)
		fprintf(fp, "Details: %s%s", text, log_line_ending);
	fputs(log_line_ending, fp);
	fclose(fp);
rswindell's avatar
rswindell committed
	return true;
BOOL sbbs_t::hacklog(char* prot, char* text)
{
	return ::hacklog(&cfg, prot, useron.alias, text, client_name, &client_addr);
}

extern "C" BOOL DLLCALL spamlog(scfg_t* cfg, char* prot, char* action
								,char* reason, char* host, char* ip_addr
								,char* to, char* from)
rswindell's avatar
rswindell committed
	FILE*	fp;
rswindell's avatar
rswindell committed
	SAFEPRINTF(fname, "%sspam.log", cfg->logs_dir);
rswindell's avatar
rswindell committed
	if((fp = fnopen(NULL, fname, O_WRONLY|O_CREAT|O_APPEND)) == NULL)
		return false;
	if(to==NULL)
		to_user[0]=0;
	else
rswindell's avatar
rswindell committed
		SAFEPRINTF(to_user,"to: %.128s",to);
rswindell's avatar
rswindell committed
	fprintf(fp, "SUSPECTED %s SPAM %s on %.24s%sHost: %s [%s]%sFrom: %.128s %s%s"
rswindell's avatar
rswindell committed
		,log_line_ending
rswindell's avatar
rswindell committed
		,log_line_ending
rswindell's avatar
rswindell committed
		,log_line_ending
rswindell's avatar
rswindell committed
	if(reason != NULL)
		fprintf(fp, "Reason: %s%s", reason, log_line_ending);
	fputs(log_line_ending, fp);
	fclose(fp);

	return true;
extern "C" int DLLCALL errorlog(scfg_t* cfg, const char* host, const char* text)
rswindell's avatar
rswindell committed
	SAFEPRINTF(path, "%serror.log", cfg->logs_dir);
	if((fp = fnopen(NULL,path,O_WRONLY|O_CREAT|O_APPEND))==NULL)
rswindell's avatar
rswindell committed
	fprintf(fp,"%s %s%s%s%s%s"
		,timestr(cfg,time32(NULL),buf)
		,host==NULL ? "":host
		,log_line_ending
		,text
		,log_line_ending
		,log_line_ending
		);
void sbbs_t::logentry(const char *code, const char *entry)
{
	char str[512];

	now=time(NULL);
rswindell's avatar
rswindell committed
	SAFEPRINTF4(str, "Node %2d  %s%s   %s", cfg.node_num, timestr(now), log_line_ending, entry);
	logline(code,str);
}

/****************************************************************************/
/* Writes 'str' verbatim into node.log 										*/
/****************************************************************************/
void sbbs_t::log(char *str)
{
	if(logfile_fp==NULL || online==ON_LOCAL) return;
	if(logcol>=78 || (78-logcol)<strlen(str)) {
rswindell's avatar
rswindell committed
		fputs(log_line_ending, logfile_fp);
	if(logcol==1) {
	else
		logcol+=strlen(str);
}

/****************************************************************************/
/* Writes 'str' on it's own line in node.log (using LOG_INFO level)			*/
/****************************************************************************/
void sbbs_t::logline(const char *code, const char *str)
{
	logline(LOG_INFO, code, str);
}

/****************************************************************************/
/* Writes 'str' on it's own line in node.log								*/
/****************************************************************************/
void sbbs_t::logline(int level, const char *code, const char *str)
	if(strchr(str,'\n')==NULL) 	// Keep the console log pretty
		lputs(level, str);
	if(logfile_fp==NULL || (online==ON_LOCAL && strcmp(code,"!!"))) return;
	if(logcol!=1)
rswindell's avatar
rswindell committed
		fputs(log_line_ending, logfile_fp);
	fprintf(logfile_fp,"%-2.2s %s%s", code, str, log_line_ending);
	fflush(logfile_fp);
}

/****************************************************************************/
/* Writes a comma then 'ch' to log, tracking column.						*/
/****************************************************************************/
void sbbs_t::logch(char ch, bool comma)
{

	if(logfile_fp==NULL || (online==ON_LOCAL)) return;
	if((uchar)ch<' ')	/* Don't log control chars */
		return;
	if(logcol==1) {
		logcol=4;
		fprintf(logfile_fp,"   "); 
	}
	else if(logcol>=78) {
		logcol=4;
rswindell's avatar
rswindell committed
		fprintf(logfile_fp, "%s   ", log_line_ending); 
	}
	if(comma && logcol!=4) {
		fprintf(logfile_fp,",");
		logcol++; 
	}
	if(ch&0x80) {
		ch&=0x7f;
			return;
		fprintf(logfile_fp,"/"); 
	}
	fprintf(logfile_fp,"%c",ch);
	logcol++;
}

/****************************************************************************/
/* Error handling routine. Prints to local and remote screens the error     */
/* information, function, action, object and access and then attempts to    */
/* write the error information into the file ERROR.LOG and NODE.LOG         */
/****************************************************************************/
void sbbs_t::errormsg(int line, const char* function, const char *src, const char* action, const char *object
					  ,long access, const char *extinfo)

	/* prevent recursion */
	if(errormsg_inside)
		return;
	errormsg_inside=true;

	safe_snprintf(str,sizeof(str),"ERROR %d (%s) "
		"(WinError %u) "
		"in %s line %u (%s) %s \"%s\" access=%ld %s%s"
		,errno,STRERROR(errno)
		,src, line, function, action, object, access
		,extinfo==NULL ? "":"info="
		,extinfo==NULL ? "":extinfo);
		if(useron.number)
			eprintf(LOG_ERR, "<%s> %s", useron.alias, str);
		else
			eprintf(LOG_ERR, "%s", str);
		int savatr=curatr;
		lprintf(LOG_ERR, "!%s", str);
		attr(cfg.color[clr_err]);
		bprintf("\7\r\n!ERROR %s %s\r\n", action, object);   /* tell user about error */
		bputs("\r\nThe sysop has been notified.\r\n");
		pause();
		attr(savatr);
	safe_snprintf(str,sizeof(str),"ERROR %s %s", action, object);
	if(cfg.node_num>0) {
		getnodedat(cfg.node_num,&thisnode,1);
		if(thisnode.errors<UCHAR_MAX)
			thisnode.errors++;
		criterrs=thisnode.errors;
		putnodedat(cfg.node_num,&thisnode);
	}
	now=time(NULL);
	if(logfile_fp!=NULL) {
		if(logcol!=1)
rswindell's avatar
rswindell committed
			fputs(log_line_ending, logfile_fp);
		fprintf(logfile_fp,"!! %s%s", str, log_line_ending);
		logcol=1;
		fflush(logfile_fp);