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/mbsetup/m_virus.c

495 lines
13 KiB
C
Raw Normal View History

2001-08-17 05:46:24 +00:00
/*****************************************************************************
*
2001-11-14 21:37:58 +00:00
* $Id$
2001-08-17 05:46:24 +00:00
* Purpose ...............: Setup Virus structure.
*
*****************************************************************************
2005-08-29 14:37:51 +00:00
* Copyright (C) 1997-2005
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.
*
* MB 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 MB 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 17:22:00 +00:00
#include "../lib/mbselib.h"
#include "../paths.h"
2001-08-17 05:46:24 +00:00
#include "screen.h"
#include "mutil.h"
#include "ledit.h"
#include "stlist.h"
#include "m_global.h"
#include "m_virus.h"
int VirUpdated = 0;
/*
* Count nr of virscan records in the database.
* Creates the database if it doesn't exist.
*/
int CountVirus(void)
{
FILE *fil;
char ffile[PATH_MAX];
2001-08-17 05:46:24 +00:00
int count;
2005-08-29 14:37:51 +00:00
snprintf(ffile, PATH_MAX, "%s/etc/virscan.data", getenv("MBSE_ROOT"));
2001-08-17 05:46:24 +00:00
if ((fil = fopen(ffile, "r")) == NULL) {
if ((fil = fopen(ffile, "a+")) != NULL) {
Syslog('+', "Created new %s", ffile);
2001-08-17 05:46:24 +00:00
virscanhdr.hdrsize = sizeof(virscanhdr);
virscanhdr.recsize = sizeof(virscan);
fwrite(&virscanhdr, sizeof(virscanhdr), 1, fil);
/*
* Create some default records but don't enable them.
2001-08-17 05:46:24 +00:00
*/
memset(&virscan, 0, sizeof(virscan));
2005-08-29 14:37:51 +00:00
snprintf(virscan.comment, 41, "AntiVir/Linux Scanner");
if (strlen(_PATH_ANTIVIR)) {
2005-08-29 14:37:51 +00:00
snprintf(virscan.scanner, 65, "%s", _PATH_ANTIVIR);
virscan.available = TRUE;
} else {
2005-08-29 14:37:51 +00:00
snprintf(virscan.scanner, 65, "/usr/bin/antivir");
virscan.available = FALSE;
}
2005-08-29 14:37:51 +00:00
snprintf(virscan.options, 65, "-allfiles -s -q");
fwrite(&virscan, sizeof(virscan), 1, fil);
memset(&virscan, 0, sizeof(virscan));
2005-08-29 14:37:51 +00:00
snprintf(virscan.comment, 41, "F-Prot scanner");
if (strlen(_PATH_FPROT)) {
2005-08-29 14:37:51 +00:00
snprintf(virscan.scanner, 65, "%s .", _PATH_FPROT);
virscan.available = TRUE;
} else {
snprintf(virscan.scanner, 65, "/usr/local/bin/fpscan .");
virscan.available = FALSE;
}
2005-08-29 14:37:51 +00:00
snprintf(virscan.options, 65, "-archive -silent");
2001-08-17 05:46:24 +00:00
fwrite(&virscan, sizeof(virscan), 1, fil);
memset(&virscan, 0, sizeof(virscan));
2005-08-29 14:37:51 +00:00
snprintf(virscan.comment, 41, "McAfee VirusScan for Linux");
if (strlen(_PATH_UVSCAN)) {
2005-08-29 14:37:51 +00:00
snprintf(virscan.scanner, 65, "%s", _PATH_UVSCAN);
virscan.available = TRUE;
} else {
2005-08-29 14:37:51 +00:00
snprintf(virscan.scanner, 65, "/usr/local/bin/uvscan");
virscan.available = FALSE;
}
2005-08-29 14:37:51 +00:00
snprintf(virscan.options, 65, "--noboot --noexpire -r --secure -");
2001-08-17 05:46:24 +00:00
fwrite(&virscan, sizeof(virscan), 1, fil);
2004-04-11 12:36:02 +00:00
memset(&virscan, 0, sizeof(virscan));
2005-08-29 14:37:51 +00:00
snprintf(virscan.comment, 41, "Clam AntiVirus");
2004-04-11 12:36:02 +00:00
if (strlen(_PATH_CLAMAV)) {
2005-08-29 14:37:51 +00:00
snprintf(virscan.scanner, 65, "%s", _PATH_CLAMAV);
2004-04-11 12:36:02 +00:00
virscan.available = TRUE;
} else {
2005-08-29 14:37:51 +00:00
snprintf(virscan.scanner, 65, "/usr/local/bin/clamscan");
2004-04-11 12:36:02 +00:00
virscan.available = FALSE;
}
2005-08-29 14:37:51 +00:00
snprintf(virscan.options, 65, "--quiet --recursive");
2004-04-11 12:36:02 +00:00
fwrite(&virscan, sizeof(virscan), 1, fil);
2001-08-17 05:46:24 +00:00
fclose(fil);
2001-11-14 21:37:58 +00:00
chmod(ffile, 0640);
2004-04-11 12:36:02 +00:00
return 4;
2001-08-17 05:46:24 +00:00
} else
return -1;
}
fread(&virscanhdr, sizeof(virscanhdr), 1, fil);
fseek(fil, 0, SEEK_END);
count = (ftell(fil) - virscanhdr.hdrsize) / virscanhdr.recsize;
fclose(fil);
return count;
}
/*
* Open database for editing. The datafile is copied, if the format
* is changed it will be converted on the fly. All editing must be
* done on the copied file.
*/
int OpenVirus(void);
2001-08-17 05:46:24 +00:00
int OpenVirus(void)
{
FILE *fin, *fout;
char fnin[PATH_MAX], fnout[PATH_MAX];
2005-10-11 20:49:41 +00:00
int oldsize;
2001-08-17 05:46:24 +00:00
2005-08-29 14:37:51 +00:00
snprintf(fnin, PATH_MAX, "%s/etc/virscan.data", getenv("MBSE_ROOT"));
snprintf(fnout, PATH_MAX, "%s/etc/virscan.temp", getenv("MBSE_ROOT"));
2001-08-17 05:46:24 +00:00
if ((fin = fopen(fnin, "r")) != NULL) {
if ((fout = fopen(fnout, "w")) != NULL) {
fread(&virscanhdr, sizeof(virscanhdr), 1, fin);
/*
* In case we are automatic upgrading the data format
* we save the old format. If it is changed, the
* database must always be updated.
*/
oldsize = virscanhdr.recsize;
if (oldsize != sizeof(virscan)) {
2001-08-17 05:46:24 +00:00
VirUpdated = 1;
Syslog('+', "Upgraded %s, format changed", fnin);
} else
2001-08-17 05:46:24 +00:00
VirUpdated = 0;
virscanhdr.hdrsize = sizeof(virscanhdr);
virscanhdr.recsize = sizeof(virscan);
fwrite(&virscanhdr, sizeof(virscanhdr), 1, fout);
/*
* The datarecord is filled with zero's before each
* read, so if the format changed, the new fields
* will be empty.
*/
memset(&virscan, 0, sizeof(virscan));
while (fread(&virscan, oldsize, 1, fin) == 1) {
fwrite(&virscan, sizeof(virscan), 1, fout);
memset(&virscan, 0, sizeof(virscan));
}
fclose(fin);
fclose(fout);
return 0;
} else
return -1;
}
return -1;
}
void CloseVirus(int);
void CloseVirus(int force)
2001-08-17 05:46:24 +00:00
{
char fin[PATH_MAX], fout[PATH_MAX];
2001-08-17 05:46:24 +00:00
FILE *fi, *fo;
st_list *vir = NULL, *tmp;
2005-08-29 14:37:51 +00:00
snprintf(fin, PATH_MAX, "%s/etc/virscan.data", getenv("MBSE_ROOT"));
snprintf(fout, PATH_MAX, "%s/etc/virscan.temp", getenv("MBSE_ROOT"));
2001-08-17 05:46:24 +00:00
if (VirUpdated == 1) {
if (force || (yes_no((char *)"Database is changed, save changes") == 1)) {
2001-08-17 05:46:24 +00:00
working(1, 0, 0);
fi = fopen(fout, "r");
fo = fopen(fin, "w");
fread(&virscanhdr, virscanhdr.hdrsize, 1, fi);
fwrite(&virscanhdr, virscanhdr.hdrsize, 1, fo);
while (fread(&virscan, virscanhdr.recsize, 1, fi) == 1)
if (!virscan.deleted)
fill_stlist(&vir, virscan.comment, ftell(fi) - virscanhdr.recsize);
sort_stlist(&vir);
for (tmp = vir; tmp; tmp = tmp->next) {
fseek(fi, tmp->pos, SEEK_SET);
fread(&virscan, virscanhdr.recsize, 1, fi);
fwrite(&virscan, virscanhdr.recsize, 1, fo);
}
tidy_stlist(&vir);
fclose(fi);
fclose(fo);
unlink(fout);
2001-11-14 21:37:58 +00:00
chmod(fin, 0640);
2001-08-17 05:46:24 +00:00
Syslog('+', "Updated \"virscan.data\"");
2004-02-05 21:05:31 +00:00
if (!force)
working(6, 0, 0);
2001-08-17 05:46:24 +00:00
return;
}
}
2002-01-25 20:21:50 +00:00
chmod(fin, 0640);
2001-08-17 05:46:24 +00:00
unlink(fout);
}
int AppendVirus(void)
{
FILE *fil;
char ffile[PATH_MAX];
2001-08-17 05:46:24 +00:00
2005-08-29 14:37:51 +00:00
snprintf(ffile, PATH_MAX, "%s/etc/virscan.temp", getenv("MBSE_ROOT"));
2001-08-17 05:46:24 +00:00
if ((fil = fopen(ffile, "a")) != NULL) {
memset(&virscan, 0, sizeof(virscan));
fwrite(&virscan, sizeof(virscan), 1, fil);
fclose(fil);
VirUpdated = 1;
return 0;
} else
return -1;
}
/*
* Edit one record, return -1 if there are errors, 0 if ok.
*/
int EditVirRec(int Area)
{
FILE *fil;
char mfile[PATH_MAX];
2005-10-11 20:49:41 +00:00
int offset;
2001-08-17 05:46:24 +00:00
int j;
2005-10-11 20:49:41 +00:00
unsigned int crc, crc1;
2001-08-17 05:46:24 +00:00
clr_index();
working(1, 0, 0);
IsDoing("Edit VirScan");
2005-08-29 14:37:51 +00:00
snprintf(mfile, PATH_MAX, "%s/etc/virscan.temp", getenv("MBSE_ROOT"));
2001-08-17 05:46:24 +00:00
if ((fil = fopen(mfile, "r")) == NULL) {
working(2, 0, 0);
return -1;
}
offset = sizeof(virscanhdr) + ((Area -1) * sizeof(virscan));
if (fseek(fil, offset, 0) != 0) {
working(2, 0, 0);
return -1;
}
fread(&virscan, sizeof(virscan), 1, fil);
fclose(fil);
crc = 0xffffffff;
crc = upd_crc32((char *)&virscan, crc, sizeof(virscan));
set_color(WHITE, BLACK);
mbse_mvprintw( 5, 2, "4. EDIT VIRUS SCANNER");
2001-08-17 05:46:24 +00:00
set_color(CYAN, BLACK);
mbse_mvprintw( 7, 2, "1. Comment");
mbse_mvprintw( 8, 2, "2. Command");
mbse_mvprintw( 9, 2, "3. Options");
mbse_mvprintw(10, 2, "4. Available");
mbse_mvprintw(11, 2, "5. Deleted");
mbse_mvprintw(12, 2, "6. Error lvl");
2001-08-17 05:46:24 +00:00
for (;;) {
set_color(WHITE, BLACK);
show_str( 7,16,40, virscan.comment);
show_str( 8,16,64, virscan.scanner);
show_str( 9,16,64, virscan.options);
show_bool(10,16, virscan.available);
show_bool(11,16, virscan.deleted);
show_int( 12,16, virscan.error);
j = select_menu(6);
switch(j) {
case 0: crc1 = 0xffffffff;
crc1 = upd_crc32((char *)&virscan, crc1, sizeof(virscan));
if (crc != crc1) {
if (yes_no((char *)"Record is changed, save") == 1) {
working(1, 0, 0);
if ((fil = fopen(mfile, "r+")) == NULL) {
working(2, 0, 0);
return -1;
}
fseek(fil, offset, 0);
fwrite(&virscan, sizeof(virscan), 1, fil);
fclose(fil);
VirUpdated = 1;
2004-02-05 21:05:31 +00:00
working(6, 0, 0);
2001-08-17 05:46:24 +00:00
}
}
IsDoing("Browsing Menu");
return 0;
case 1: E_STR( 7,16,40,virscan.comment, "The ^Comment^ for this record")
case 2: E_STR( 8,16,64,virscan.scanner, "The full ^name and path^ to the binary of this scanner")
case 3: E_STR( 9,16,64,virscan.options, "The ^commandline options^ for this scanner")
case 4: E_BOOL(10,16, virscan.available,"Switch if this virus scanner is ^Available^ for use.")
case 5: E_BOOL(11,16, virscan.deleted, "Is this scanner ^deleted^")
case 6: E_INT( 12,16, virscan.error, "The ^Error Level^ the scanner returns when no virus is found")
}
}
return 0;
}
void EditVirus(void)
{
int records, i, x, y;
char pick[12];
FILE *fil;
char temp[PATH_MAX];
2005-10-11 20:49:41 +00:00
int offset;
2001-08-17 05:46:24 +00:00
clr_index();
working(1, 0, 0);
IsDoing("Browsing Menu");
if (config_read() == -1) {
working(2, 0, 0);
return;
}
records = CountVirus();
if (records == -1) {
working(2, 0, 0);
return;
}
if (OpenVirus() == -1) {
working(2, 0, 0);
return;
}
for (;;) {
clr_index();
set_color(WHITE, BLACK);
mbse_mvprintw( 5, 4, "4. VIRUS SCANNERS SETUP");
2001-08-17 05:46:24 +00:00
set_color(CYAN, BLACK);
if (records != 0) {
2005-08-29 14:37:51 +00:00
snprintf(temp, PATH_MAX, "%s/etc/virscan.temp", getenv("MBSE_ROOT"));
2001-08-17 05:46:24 +00:00
if ((fil = fopen(temp, "r")) != NULL) {
fread(&virscanhdr, sizeof(virscanhdr), 1, fil);
x = 2;
y = 7;
set_color(CYAN, BLACK);
for (i = 1; i <= records; i++) {
offset = sizeof(virscanhdr) + ((i - 1) * virscanhdr.recsize);
fseek(fil, offset, 0);
fread(&virscan, virscanhdr.recsize, 1, fil);
if (i == 11) {
x = 42;
y = 7;
}
if (virscan.available)
set_color(CYAN, BLACK);
else
set_color(LIGHTBLUE, BLACK);
2005-08-29 14:37:51 +00:00
snprintf(temp, 81, "%3d. %-32s", i, virscan.comment);
2001-08-17 05:46:24 +00:00
temp[37] = 0;
mbse_mvprintw(y, x, temp);
2001-08-17 05:46:24 +00:00
y++;
}
fclose(fil);
}
}
strcpy(pick, select_record(records, 20));
if (strncmp(pick, "-", 1) == 0) {
CloseVirus(FALSE);
2001-08-17 05:46:24 +00:00
return;
}
if (strncmp(pick, "A", 1) == 0) {
working(1, 0, 0);
if (AppendVirus() == 0) {
records++;
working(3, 0, 0);
} else
working(2, 0, 0);
}
if ((atoi(pick) >= 1) && (atoi(pick) <= records))
EditVirRec(atoi(pick));
}
}
void InitVirus(void)
{
CountVirus();
OpenVirus();
CloseVirus(TRUE);
}
2001-08-17 05:46:24 +00:00
int virus_doc(FILE *fp, FILE *toc, int page)
{
2004-04-03 18:46:54 +00:00
char temp[PATH_MAX];
FILE *wp, *ip, *vir;
int nr = 0, j;
2001-08-17 05:46:24 +00:00
2005-08-29 14:37:51 +00:00
snprintf(temp, PATH_MAX, "%s/etc/virscan.data", getenv("MBSE_ROOT"));
2004-04-03 18:46:54 +00:00
if ((vir = fopen(temp, "r")) == NULL)
return page;
2001-08-17 05:46:24 +00:00
2004-04-03 18:46:54 +00:00
page = newpage(fp, page);
addtoc(fp, toc, 4, 0, page, (char *)"Virus scanners");
j = 0;
fprintf(fp, "\n\n");
fread(&virscanhdr, sizeof(virscanhdr), 1, vir);
ip = open_webdoc((char *)"virscan.html", (char *)"Virus Scanners", NULL);
fprintf(ip, "<A HREF=\"index.html\">Main</A>\n");
2005-05-08 21:34:51 +00:00
fprintf(ip, "<P>\n");
fprintf(ip, "<TABLE border='1' cellspacing='0' cellpadding='2'>\n");
2005-05-08 21:34:51 +00:00
fprintf(ip, "<TBODY>\n");
fprintf(ip, "<TR><TH align='left'>Nr</TH><TH align='left'>Comment</TH><TH align='left'>Available</TH></TR>\n");
2004-04-03 18:46:54 +00:00
while ((fread(&virscan, virscanhdr.recsize, 1, vir)) == 1) {
if (j == 5) {
page = newpage(fp, page);
fprintf(fp, "\n");
j = 0;
}
2001-08-17 05:46:24 +00:00
2004-04-03 18:46:54 +00:00
nr++;
2005-08-29 14:37:51 +00:00
snprintf(temp, 81, "virscan_%d.html", nr);
2005-05-08 21:34:51 +00:00
fprintf(ip, "<TR><TD><A HREF=\"%s\">%d</A></TD><TD>%s</TD><TD>%s</TD></TR>\n",
temp, nr, virscan.comment, getboolean(virscan.available));
2004-04-03 18:46:54 +00:00
if ((wp = open_webdoc(temp, (char *)"Virus Scanner", virscan.comment))) {
fprintf(wp, "<A HREF=\"index.html\">Main</A>&nbsp;<A HREF=\"virscan.html\">Back</A>\n");
fprintf(wp, "<P>\n");
fprintf(wp, "<TABLE width='600' border='0' cellspacing='0' cellpadding='2'>\n");
fprintf(wp, "<COL width='30%%'><COL width='70%%'>\n");
fprintf(wp, "<TBODY>\n");
add_webtable(wp, (char *)"Scanner name", virscan.comment);
add_webtable(wp, (char *)"Command line", virscan.scanner);
add_webtable(wp, (char *)"Options", virscan.options);
add_webtable(wp, (char *)"Available", getboolean(virscan.available));
add_webdigit(wp, (char *)"Errorlevel OK", virscan.error);
fprintf(wp, "</TBODY>\n");
fprintf(wp, "</TABLE>\n");
close_webdoc(wp);
2001-08-17 05:46:24 +00:00
}
2004-04-03 18:46:54 +00:00
fprintf(fp, " Scanner name %s\n", virscan.comment);
fprintf(fp, " Command line %s\n", virscan.scanner);
fprintf(fp, " Options %s\n", virscan.options);
fprintf(fp, " Available %s\n", getboolean(virscan.available));
fprintf(fp, " Errorlevel OK %d\n", virscan.error);
fprintf(fp, "\n\n\n");
j++;
}
2005-05-08 21:34:51 +00:00
fprintf(ip, "</TBODY>\n");
fprintf(ip, "</TABLE>\n");
2004-04-03 18:46:54 +00:00
close_webdoc(ip);
fclose(vir);
return page;
2001-08-17 05:46:24 +00:00
}