This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
deb-mbse/lib/clcomm.c

544 lines
12 KiB
C
Raw Normal View History

2001-08-17 05:46:24 +00:00
/*****************************************************************************
*
* $Id$
2001-08-17 05:46:24 +00:00
* Purpose ...............: Client/Server communications
*
*****************************************************************************
2004-02-21 14:24:03 +00:00
* Copyright (C) 1997-2004
2001-08-17 05:46:24 +00:00
*
* Michiel Broek FIDO: 2:280/2802
* Beekmansbos 10
* 1971 BV IJmuiden
* the Netherlands
*
* This file is part of MBSE BBS.
*
* This BBS 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, or (at your option) any
* later version.
*
* MBSE BBS is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MBSE BBS; see the file COPYING. If not, write to the Free
2003-08-15 20:05:34 +00:00
* Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
2001-08-17 05:46:24 +00:00
*****************************************************************************/
2002-06-30 12:48:44 +00:00
#include "../config.h"
2004-02-21 14:24:03 +00:00
#include "mbselib.h"
2001-08-17 05:46:24 +00:00
int do_quiet = FALSE; /* Quiet flag */
int show_log = FALSE; /* Show loglines on screen */
int most_debug = FALSE; /* Toggle normal/most debugging */
2003-11-24 22:19:50 +00:00
int tcp_regged = FALSE; /* Safety TCP registration flag */
2001-08-17 05:46:24 +00:00
char progname[21]; /* Program name */
2003-09-09 19:39:51 +00:00
char logdebug[PATH_MAX]; /* Debug logfile */
2001-08-17 05:46:24 +00:00
char logfile[PATH_MAX]; /* Normal logfile */
char errfile[PATH_MAX]; /* Error logfile */
char mgrfile[PATH_MAX]; /* Area/File- mgr logfile */
2001-08-17 05:46:24 +00:00
long loggrade; /* Logging grade */
pid_t mypid; /* Original parent pid if child */
unsigned long lcrc = 0, tcrc = 1; /* CRC value of logstring */
int lcnt = 0; /* Same message counter */
static char *pbuff = NULL;
extern char cpath[108];
extern char spath[108];
2001-08-17 05:46:24 +00:00
char *xmalloc(size_t size)
{
char *tmp;
tmp = malloc(size);
if (!tmp)
abort();
return tmp;
}
char *xstrcpy(char *src)
{
char *tmp;
if (src == NULL)
return(NULL);
tmp = xmalloc(strlen(src)+1);
strcpy(tmp, src);
return tmp;
}
char *xstrcat(char *src, char *add)
{
char *tmp;
size_t size = 0;
if ((add == NULL) || (strlen(add) == 0))
return src;
if (src)
size = strlen(src);
size += strlen(add);
tmp = xmalloc(size + 1);
*tmp = '\0';
if (src) {
strcpy(tmp, src);
free(src);
}
strcat(tmp, add);
return tmp;
}
void InitClient(char *user, char *myname, char *where, char *logfname, long loggr, char *err, char *mgr, char *debug)
2001-08-17 05:46:24 +00:00
{
if ((getenv("MBSE_ROOT")) == NULL) {
printf("Could not get the MBSE_ROOT environment variable\n");
printf("Please set the environment variable ie:\n");
printf("\"MBSE_ROOT=/opt/mbse; export MBSE_ROOT\"\n\n");
2002-10-20 20:58:55 +00:00
exit(MBERR_INIT_ERROR);
2001-08-17 05:46:24 +00:00
}
sprintf(progname, "%s", myname);
sprintf(logfile, "%s", logfname);
2001-08-17 05:46:24 +00:00
sprintf(errfile, "%s", err);
sprintf(mgrfile, "%s", mgr);
2003-09-09 19:39:51 +00:00
sprintf(logdebug, "%s", debug);
2001-08-17 05:46:24 +00:00
loggrade = loggr;
sprintf(cpath, "%s/tmp/%s%d", getenv("MBSE_ROOT"), progname, getpid());
sprintf(spath, "%s/tmp/mbtask", getenv("MBSE_ROOT"));
/*
* Store my pid in case a child process is forked and wants to do
* some communications with the mbsed server.
*/
mypid = getpid();
if (socket_connect(user, myname, where) == -1) {
printf("PANIC: cannot access socket\n");
2002-10-20 20:58:55 +00:00
exit(MBERR_INIT_ERROR);
2001-08-17 05:46:24 +00:00
}
}
void ExitClient(int errcode)
{
if (socket_shutdown(mypid) == -1)
printf("PANIC: unable to shutdown socket\n");
unlink(cpath);
fflush(stdout);
fflush(stdin);
if (pbuff)
free(pbuff);
exit(errcode);
}
void SockS(const char *format, ...)
{
char *out;
va_list va_ptr;
out = calloc(SS_BUFSIZE, sizeof(char));
va_start(va_ptr, format);
vsprintf(out, format, va_ptr);
va_end(va_ptr);
if (socket_send(out) == 0)
socket_receive();
free(out);
}
char *SockR(const char *format, ...)
{
static char buf[SS_BUFSIZE];
char *out;
va_list va_ptr;
memset(&buf, 0, SS_BUFSIZE);
out = calloc(SS_BUFSIZE, sizeof(char));
va_start(va_ptr, format);
vsprintf(out, format, va_ptr);
va_end(va_ptr);
if (socket_send(out) == 0)
sprintf(buf, "%s", socket_receive());
free(out);
return buf;
}
void WriteError(const char *format, ...)
{
char *outputstr;
va_list va_ptr;
int i;
outputstr = calloc(10240, sizeof(char));
va_start(va_ptr, format);
vsprintf(outputstr, format, va_ptr);
va_end(va_ptr);
for (i = 0; i < strlen(outputstr); i++)
if (outputstr[i] == '\r' || outputstr[i] == '\n')
outputstr[i] = ' ';
if (*outputstr == '$')
sprintf(outputstr+strlen(outputstr), ": %s", strerror(errno));
if (strlen(outputstr) > (SS_BUFSIZE - 64)) {
outputstr[SS_BUFSIZE - 65] = ';';
outputstr[SS_BUFSIZE - 64] = '\0';
}
tcrc = StringCRC32(outputstr);
if (tcrc == lcrc) {
lcnt++;
free(outputstr);
return;
} else {
lcrc = tcrc;
if (lcnt) {
lcnt++;
2003-09-09 19:39:51 +00:00
SockS("ALOG:5,%s,%s,%d,?,Last message repeated %d times;", logdebug, progname, mypid, lcnt);
2001-08-17 05:46:24 +00:00
SockS("ALOG:5,%s,%s,%d,?,Last message repeated %d times;", logfile, progname, mypid, lcnt);
SockS("ALOG:5,%s,%s,%d,?,Last message repeated %d times;", errfile, progname, mypid, lcnt);
}
lcnt = 0;
}
2003-09-09 19:39:51 +00:00
SockS("ALOG:5,%s,%s,%d,?,%s;", logdebug, progname, mypid, *outputstr == '$' ? outputstr+1 : outputstr);
2001-08-17 05:46:24 +00:00
SockS("ALOG:5,%s,%s,%d,?,%s;", logfile, progname, mypid, *outputstr == '$' ? outputstr+1 : outputstr);
SockS("ALOG:5,%s,%s,%d,?,%s;", errfile, progname, mypid, *outputstr == '$' ? outputstr+1 : outputstr);
free(outputstr);
}
/*
* Standard system logging
*/
2001-08-17 05:46:24 +00:00
void Syslog(int level, const char *format, ...)
{
char *outstr;
va_list va_ptr;
outstr = calloc(10240, sizeof(char));
va_start(va_ptr, format);
vsprintf(outstr, format, va_ptr);
va_end(va_ptr);
Syslogp(level, outstr);
free(outstr);
}
/*
* System logging without string formatting.
*/
2001-08-17 05:46:24 +00:00
void Syslogp(int level, char *outstr)
{
long mask = 0;
2003-09-09 19:39:51 +00:00
int i, upper, debug;
2003-09-09 19:39:51 +00:00
debug = isalpha(level);
upper = isupper(level);
2003-09-09 19:39:51 +00:00
switch(tolower(level)) {
case ' ' : mask = DLOG_ALLWAYS; break;
case '?' : mask = DLOG_ERROR; break;
case '!' : mask = DLOG_ATTENT; break;
case '+' : mask = DLOG_NORMAL; break;
case '-' : mask = DLOG_VERBOSE; break;
case 'a' : mask = DLOG_TCP; break;
case 'b' : mask = DLOG_BBS; break;
case 'c' : mask = DLOG_CHAT; break;
case 'd' : mask = DLOG_DEVIO; break;
case 'e' : mask = DLOG_EXEC; break;
case 'f' : mask = DLOG_FILEFWD; break;
case 'h' : mask = DLOG_HYDRA; break;
case 'i' : mask = DLOG_IEMSI; break;
case 'l' : mask = DLOG_LOCK; break;
case 'm' : mask = DLOG_MAIL; break;
case 'n' : mask = DLOG_NODELIST; break;
case 'o' : mask = DLOG_OUTSCAN; break;
case 'p' : mask = DLOG_PACK; break;
case 'r' : mask = DLOG_ROUTE; break;
case 's' : mask = DLOG_SESSION; break;
case 't' : mask = DLOG_TTY; break;
case 'x' : mask = DLOG_XMODEM; break;
case 'z' : mask = DLOG_ZMODEM; break;
}
if (((loggrade | DLOG_ALLWAYS | DLOG_ERROR) & mask) == 0)
return;
/*
* Don't log uppercase debug levels when most_debug is FALSE
*/
if (upper && !most_debug)
return;
2001-08-17 05:46:24 +00:00
for (i = 0; i < strlen(outstr); i++)
if (outstr[i] == '\r' || outstr[i] == '\n')
outstr[i] = ' ';
if (strlen(outstr) > (SS_BUFSIZE - 64))
outstr[SS_BUFSIZE - 64] = '\0';
2001-08-17 05:46:24 +00:00
tcrc = StringCRC32(outstr);
if (tcrc == lcrc) {
lcnt++;
return;
} else {
lcrc = tcrc;
if (lcnt) {
lcnt++;
2003-09-09 19:39:51 +00:00
SockS("ALOG:5,%s,%s,%d,%c,Last message repeated %d times;", logdebug, progname, mypid, level, lcnt);
if (!debug)
SockS("ALOG:5,%s,%s,%d,%c,Last message repeated %d times;", logfile, progname, mypid, level, lcnt);
2001-08-17 05:46:24 +00:00
}
lcnt = 0;
}
2001-08-17 05:46:24 +00:00
if (show_log)
printf("%c %s\n", level, outstr);
2001-08-17 05:46:24 +00:00
2003-09-09 19:39:51 +00:00
if (*outstr == '$') {
SockS("ALOG:5,%s,%s,%d,%c,%s: %s;", logdebug, progname, mypid, level, outstr+1, strerror(errno));
if (!debug)
SockS("ALOG:5,%s,%s,%d,%c,%s: %s;", logfile, progname, mypid, level, outstr+1, strerror(errno));
} else {
SockS("ALOG:5,%s,%s,%d,%c,%s;", logdebug, progname, mypid, level, outstr);
if (!debug)
SockS("ALOG:5,%s,%s,%d,%c,%s;", logfile, progname, mypid, level, outstr);
}
2001-08-17 05:46:24 +00:00
}
void Mgrlog(const char *format, ...)
{
char *outstr;
va_list va_ptr;
int i;
outstr = calloc(10240, sizeof(char));
va_start(va_ptr, format);
vsprintf(outstr, format, va_ptr);
va_end(va_ptr);
for (i = 0; i < strlen(outstr); i++)
if (outstr[i] == '\r' || outstr[i] == '\n')
outstr[i] = ' ';
if (strlen(outstr) > (SS_BUFSIZE - 64))
outstr[SS_BUFSIZE - 64] = '\0';
SockS("ALOG:5,%s,%s,%d,+,%s;", mgrfile, progname, mypid, outstr);
Syslogp('+', outstr);
free(outstr);
}
2001-08-17 05:46:24 +00:00
void IsDoing(const char *format, ...)
{
char *outputstr;
va_list va_ptr;
outputstr = calloc(SS_BUFSIZE, sizeof(char));
va_start(va_ptr, format);
vsprintf(outputstr, format, va_ptr);
va_end(va_ptr);
SockS("ADOI:2,%d,%s;", mypid, outputstr);
free(outputstr);
}
2002-03-23 13:12:29 +00:00
void RegTCP(void)
{
2003-11-24 22:19:50 +00:00
if (!tcp_regged)
SockS("ATCP:1,%d;", mypid);
tcp_regged = TRUE;
2002-03-23 13:12:29 +00:00
}
2001-08-17 05:46:24 +00:00
void SetTTY(char *tty)
{
SockS("ATTY:2,%d,%s;", mypid, tty);
}
void UserCity(pid_t pid, char *user, char *city)
{
SockS("AUSR:3,%d,%s,%s;", pid, user, city);
}
void DoNop()
{
SockS("GNOP:1,%d;", mypid);
}
static time_t nop = 0;
/*
* This function can be called very often but will only send once a minute
* a NOP to the server. This is a simple solution to keep server trafic low.
*/
void Nopper(void)
{
time_t now;
now = time(NULL);
if (((time_t)now - (time_t)nop) > 60) {
nop = now;
SockS("GNOP:1,%d;", mypid);
}
}
/*
* Set new alarmtime for Client/Server connection,
* if zero set the default time.
*/
void Altime(int altime)
{
if (altime)
SockS("ATIM:2,%d,%d;", mypid, altime);
else
SockS("ADEF:1,%d;", mypid);
}
unsigned long sequencer()
{
char *buf, *res;
unsigned long seq = 0;
buf = calloc(SS_BUFSIZE, sizeof(char));
sprintf(buf, "SSEQ:0;");
if (socket_send(buf) == 0) {
free(buf);
buf = socket_receive();
res = strtok(buf, ",");
res = strtok(NULL, ";");
seq = atol(res);
}
return seq;
}
2004-03-20 13:26:34 +00:00
/*
* Check enough diskspace.
* return 0=No, 1=Yes, 2=Unknown, 3=Error
*/
2004-03-20 19:25:57 +00:00
int enoughspace(unsigned long needed)
2004-03-20 13:26:34 +00:00
{
char *buf, *res;
int rc = 3;
buf = calloc(SS_BUFSIZE, sizeof(char));
2004-03-20 19:25:57 +00:00
sprintf(buf, "DSPC:1,%ld;", needed);
2004-03-20 13:26:34 +00:00
if (socket_send(buf) == 0) {
free(buf);
buf = socket_receive();
res = strtok(buf, ",");
res = strtok(NULL, ";");
rc = atoi(res);
}
Syslog('s', "enoughspace() rc=%d", rc);
return rc;
}
2001-08-17 05:46:24 +00:00
char *printable(char *s, int l)
{
2003-07-06 20:37:58 +00:00
int len;
char *p;
if (pbuff)
free(pbuff);
pbuff=NULL;
if (s == NULL)
return (char *)"(null)";
if (l > 0)
len=l;
else if (l == 0)
len=strlen(s);
else {
len=strlen(s);
if (len > -l)
len=-l;
}
2001-08-17 05:46:24 +00:00
pbuff=(char*)xmalloc(len*3+1);
2003-07-06 20:37:58 +00:00
p=pbuff;
while (len--) {
if (isprint(*(unsigned char*)s))
*p++=*s;
else
switch (*s) {
case '\\': *p++='\\'; *p++='\\'; break;
case '\r': *p++='\\'; *p++='r'; break;
case '\n': *p++='\\'; *p++='n'; break;
case '\t': *p++='\\'; *p++='t'; break;
case '\b': *p++='\\'; *p++='b'; break;
2003-11-23 22:47:26 +00:00
default: sprintf(p,"\\%02x", (*s & 0xff)); p+=3; break;
2003-07-06 20:37:58 +00:00
}
s++;
}
*p='\0';
return pbuff;
2001-08-17 05:46:24 +00:00
}
char *printablec(char c)
{
return printable(&c,1);
}