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

/****************************************************************************
 * @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										*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

#include "sbbs.h"
#include "git_branch.h"
#include "git_hash.h"
rswindell's avatar
rswindell committed
const char* log_line_ending = "\r\n";

extern "C" BOOL 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;
Rob Swindell's avatar
Rob Swindell committed
BOOL sbbs_t::hacklog(const char* prot, const char* text)
{
	return ::hacklog(&cfg, prot, useron.alias, text, client_name, &client_addr);
}

extern "C" BOOL 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 errorlog(scfg_t* cfg, int level, const char* host, const char* text)
{
	FILE*	fp;
	char	buf[128];
	char	path[MAX_PATH+1];
rswindell's avatar
rswindell committed
	SAFEPRINTF(path, "%serror.log", cfg->logs_dir);
	if((fp = fnopen(NULL,path,O_WRONLY|O_CREAT|O_APPEND))==NULL)
	fprintf(fp,"%.24s %s/%s %s%s%s%s%s"
rswindell's avatar
rswindell committed
		,host==NULL ? "":host
		,log_line_ending
		,text
		,log_line_ending
		,log_line_ending
		);
	if(cfg->node_erruser && level <= cfg->node_errlevel) {
		char subject[128];
		SAFEPRINTF2(subject, "%s %sERROR occurred", host == NULL ? "" : host, level <= LOG_CRIT ? "CRITICAL " : "");
		notify(cfg, cfg->node_erruser, subject, text);
	}
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(const 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)
	char	errno_str[128];
	char	errno_info[256] = "";

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

	if(strcmp(action, ERR_CHK) != 0)
		safe_snprintf(errno_info, sizeof(errno_info), "%d (%s) "
	#ifdef _WIN32
			"(WinError %u) "
	#endif
			,errno, safe_strerror(errno, errno_str, sizeof(errno_str))
	#ifdef _WIN32
			,GetLastError()
	#endif
		);

	safe_snprintf(str,sizeof(str),"ERROR %s"
		"in %s line %u (%s) %s \"%s\" access=%ld %s%s"
		,src, line, function, action, object, access
		,extinfo==NULL ? "":"info="
		,extinfo==NULL ? "":extinfo);

	lprintf(LOG_ERR, "!%s", str);
	if(online == ON_REMOTE) {
		int savatr=curatr;
		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);