2001-11-18 23:19:08 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
* Purpose: File Database Maintenance - Adopt file
|
|
|
|
*
|
|
|
|
*****************************************************************************
|
2004-02-21 17:22:00 +00:00
|
|
|
* Copyright (C) 1997-2004
|
2001-11-18 23:19:08 +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-11-18 23:19:08 +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"
|
2002-01-07 19:16:03 +00:00
|
|
|
#include "../lib/users.h"
|
2004-02-21 17:22:00 +00:00
|
|
|
#include "../lib/mbsedb.h"
|
2001-11-25 12:18:18 +00:00
|
|
|
#include "virscan.h"
|
2001-11-18 23:19:08 +00:00
|
|
|
#include "mbfutil.h"
|
|
|
|
#include "mbflist.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-06-19 20:31:35 +00:00
|
|
|
extern int do_quiet; /* Suppress screen output */
|
|
|
|
extern int do_annon; /* Suppress announce file */
|
|
|
|
extern int do_novir; /* Suppress virus check */
|
2001-11-18 23:19:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
void AdoptFile(int Area, char *File, char *Description)
|
|
|
|
{
|
2001-12-02 20:49:36 +00:00
|
|
|
FILE *fp;
|
2002-11-23 16:48:54 +00:00
|
|
|
char *temp, *temp2, *tmpdir, *unarc, *pwd, *lname;
|
2001-11-19 21:34:21 +00:00
|
|
|
char Desc[256], TDesc[256];
|
|
|
|
int IsArchive = FALSE, MustRearc = FALSE, UnPacked = FALSE;
|
|
|
|
int IsVirus = FALSE, File_Id = FALSE;
|
2002-05-10 18:14:27 +00:00
|
|
|
int i, j, k, lines = 0, File_id_cnt = 0, rc;
|
2004-03-06 21:48:41 +00:00
|
|
|
struct FILE_record f_db;
|
2001-11-18 23:19:08 +00:00
|
|
|
|
2003-11-15 12:31:05 +00:00
|
|
|
Syslog('f', "Adopt(%d, %s, %s)", Area, MBSE_SS(File), MBSE_SS(Description));
|
2001-11-18 23:19:08 +00:00
|
|
|
|
|
|
|
if (!do_quiet)
|
2004-10-27 11:08:09 +00:00
|
|
|
mbse_colour(CYAN, BLACK);
|
2001-11-18 23:19:08 +00:00
|
|
|
|
2001-12-02 20:49:36 +00:00
|
|
|
if (LoadAreaRec(Area) == FALSE)
|
2002-10-20 20:58:55 +00:00
|
|
|
die(MBERR_INIT_ERROR);
|
2001-11-18 23:19:08 +00:00
|
|
|
|
|
|
|
if (area.Available) {
|
|
|
|
temp = calloc(PATH_MAX, sizeof(char));
|
2001-11-25 12:18:18 +00:00
|
|
|
temp2 = calloc(PATH_MAX, sizeof(char));
|
2001-11-18 23:19:08 +00:00
|
|
|
pwd = calloc(PATH_MAX, sizeof(char));
|
2002-06-01 12:27:11 +00:00
|
|
|
tmpdir = calloc(PATH_MAX, sizeof(char));
|
2001-11-18 23:19:08 +00:00
|
|
|
|
2001-11-25 12:18:18 +00:00
|
|
|
if (CheckFDB(Area, area.Path))
|
2002-10-20 20:58:55 +00:00
|
|
|
die(MBERR_INIT_ERROR);
|
2001-11-18 23:19:08 +00:00
|
|
|
getcwd(pwd, PATH_MAX);
|
|
|
|
|
|
|
|
if (!do_quiet) {
|
|
|
|
printf("Adopt file: %s ", File);
|
|
|
|
printf("Unpacking \b\b\b\b\b\b\b\b\b\b");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
2005-12-03 14:52:34 +00:00
|
|
|
snprintf(tmpdir, PATH_MAX, "%s/tmp/arc%d", getenv("MBSE_ROOT"), (int)getpid());
|
|
|
|
if (create_tmpwork()) {
|
|
|
|
WriteError("Can't create %s", tmpdir);
|
|
|
|
if (!do_quiet)
|
|
|
|
printf("\nCan't create dir %s\n", tmpdir);
|
|
|
|
die(MBERR_INIT_ERROR);
|
|
|
|
}
|
|
|
|
|
2005-08-28 14:52:14 +00:00
|
|
|
snprintf(temp, PATH_MAX, "%s/%s", pwd, File);
|
2001-11-25 12:18:18 +00:00
|
|
|
if ((unarc = unpacker(File)) == NULL) {
|
|
|
|
Syslog('+', "No known archive: %s", File);
|
2005-12-03 14:52:34 +00:00
|
|
|
snprintf(temp2, PATH_MAX, "%s/tmp/arc%d/%s", getenv("MBSE_ROOT"), (int)getpid(), File);
|
2002-05-10 18:14:27 +00:00
|
|
|
if ((rc = file_cp(temp, temp2))) {
|
2002-05-18 12:57:00 +00:00
|
|
|
WriteError("Can't copy file to %s, %s", temp2, strerror(rc));
|
2001-11-25 12:18:18 +00:00
|
|
|
if (!do_quiet)
|
2003-11-15 12:31:05 +00:00
|
|
|
printf("\nCan't copy file to %s, %s\n", temp2, strerror(rc));
|
2002-10-20 20:58:55 +00:00
|
|
|
die(MBERR_INIT_ERROR);
|
2001-11-25 12:18:18 +00:00
|
|
|
} else {
|
2002-06-15 14:00:49 +00:00
|
|
|
if (do_novir == FALSE) {
|
|
|
|
if (!do_quiet) {
|
|
|
|
printf("Virscan \b\b\b\b\b\b\b\b\b\b");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
IsVirus = VirScan(tmpdir);
|
|
|
|
} else {
|
|
|
|
IsVirus = FALSE;
|
2001-11-25 12:18:18 +00:00
|
|
|
}
|
|
|
|
if (IsVirus) {
|
2005-12-03 14:52:34 +00:00
|
|
|
clean_tmpwork();
|
2001-11-25 12:18:18 +00:00
|
|
|
chdir(pwd);
|
|
|
|
WriteError("Virus found");
|
2001-11-18 23:19:08 +00:00
|
|
|
if (!do_quiet)
|
2003-11-15 12:31:05 +00:00
|
|
|
printf("\nVirus found\n");
|
2002-10-20 20:58:55 +00:00
|
|
|
die(MBERR_VIRUS_FOUND);
|
2001-11-18 23:19:08 +00:00
|
|
|
}
|
|
|
|
}
|
2001-11-25 12:18:18 +00:00
|
|
|
} else {
|
|
|
|
IsArchive = TRUE;
|
|
|
|
if (strlen(area.Archiver) && (strcmp(unarc, area.Archiver) == 0))
|
|
|
|
MustRearc = TRUE;
|
|
|
|
UnPacked = UnpackFile(temp);
|
|
|
|
if (!UnPacked)
|
2002-10-20 20:58:55 +00:00
|
|
|
die(MBERR_INIT_ERROR);
|
2001-11-18 23:19:08 +00:00
|
|
|
|
2002-06-15 14:00:49 +00:00
|
|
|
if (do_novir == FALSE) {
|
|
|
|
if (!do_quiet) {
|
|
|
|
printf("Virscan \b\b\b\b\b\b\b\b\b\b");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
IsVirus = VirScan(tmpdir);
|
|
|
|
} else {
|
|
|
|
IsVirus = FALSE;
|
|
|
|
}
|
2001-11-18 23:19:08 +00:00
|
|
|
|
2001-11-25 12:18:18 +00:00
|
|
|
if (IsVirus) {
|
2005-12-03 14:52:34 +00:00
|
|
|
clean_tmpwork();
|
2001-11-25 12:18:18 +00:00
|
|
|
chdir(pwd);
|
|
|
|
WriteError("Virus found");
|
|
|
|
if (!do_quiet)
|
2003-11-15 12:31:05 +00:00
|
|
|
printf("\nVirus found\n");
|
2002-10-20 20:58:55 +00:00
|
|
|
die(MBERR_VIRUS_FOUND);
|
2001-11-18 23:19:08 +00:00
|
|
|
}
|
2001-11-25 12:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!do_quiet) {
|
|
|
|
printf("Checking \b\b\b\b\b\b\b\b\b\b");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
2001-11-18 23:19:08 +00:00
|
|
|
|
2004-03-06 21:48:41 +00:00
|
|
|
memset(&f_db, 0, sizeof(f_db));
|
|
|
|
strcpy(f_db.Uploader, CFG.sysop_name);
|
|
|
|
f_db.UploadDate = time(NULL);
|
2001-11-25 16:38:06 +00:00
|
|
|
if (do_annon)
|
2004-03-06 21:48:41 +00:00
|
|
|
f_db.Announced = TRUE;
|
2001-11-25 12:18:18 +00:00
|
|
|
|
|
|
|
if (UnPacked) {
|
|
|
|
/*
|
|
|
|
* Try to get a FILE_ID.DIZ
|
|
|
|
*/
|
2005-12-03 14:52:34 +00:00
|
|
|
snprintf(temp, PATH_MAX, "%s/tmp/arc%d/FILE_ID.DIZ", getenv("MBSE_ROOT"), (int)getpid());
|
2005-08-28 14:52:14 +00:00
|
|
|
snprintf(temp2, PATH_MAX, "%s/tmp/FILE_ID.DIZ", getenv("MBSE_ROOT"));
|
2001-11-18 23:19:08 +00:00
|
|
|
if (file_cp(temp, temp2) == 0) {
|
|
|
|
File_Id = TRUE;
|
|
|
|
} else {
|
2005-12-03 14:52:34 +00:00
|
|
|
snprintf(temp, PATH_MAX, "%s/tmp/arc%d/file_id.diz", getenv("MBSE_ROOT"), (int)getpid());
|
2001-11-18 23:19:08 +00:00
|
|
|
if (file_cp(temp, temp2) == 0)
|
|
|
|
File_Id = TRUE;
|
|
|
|
}
|
2001-11-25 12:18:18 +00:00
|
|
|
|
2001-11-19 21:34:21 +00:00
|
|
|
if (File_Id) {
|
2003-11-15 12:31:05 +00:00
|
|
|
Syslog('f', "FILE_ID.DIZ found");
|
2001-11-19 21:34:21 +00:00
|
|
|
if ((fp = fopen(temp2, "r"))) {
|
|
|
|
/*
|
|
|
|
* Read no more then 25 lines
|
|
|
|
*/
|
|
|
|
while (((fgets(Desc, 255, fp)) != NULL) && (File_id_cnt < 25)) {
|
|
|
|
lines++;
|
|
|
|
/*
|
|
|
|
* Check if the FILE_ID.DIZ is in a normal layout.
|
|
|
|
* This should be max. 10 lines of max. 48 characters.
|
|
|
|
* We check at 51 characters and if the lines are longer,
|
|
|
|
* we discard the FILE_ID.DIZ file.
|
|
|
|
*/
|
|
|
|
if (strlen(Desc) > 51) {
|
|
|
|
File_id_cnt = 0;
|
|
|
|
File_Id = FALSE;
|
|
|
|
Syslog('!', "Discarding illegal formated FILE_ID.DIZ");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen(Desc)) {
|
|
|
|
if (strlen(Desc) > 48)
|
|
|
|
Desc[48] = '\0';
|
|
|
|
j = 0;
|
|
|
|
for (i = 0; i < strlen(Desc); i++) {
|
|
|
|
if ((Desc[i] >= ' ') || (Desc[i] < 0)) {
|
2004-03-06 21:48:41 +00:00
|
|
|
f_db.Desc[File_id_cnt][j] = Desc[i];
|
2001-11-19 21:34:21 +00:00
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
File_id_cnt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
unlink(temp2);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Strip empty lines at end of FILE_ID.DIZ
|
|
|
|
*/
|
2004-03-06 21:48:41 +00:00
|
|
|
while ((strlen(f_db.Desc[File_id_cnt-1]) == 0) && (File_id_cnt))
|
2001-11-19 21:34:21 +00:00
|
|
|
File_id_cnt--;
|
|
|
|
|
|
|
|
Syslog('f', "Got %d FILE_ID.DIZ lines", File_id_cnt);
|
|
|
|
for (i = 0; i < File_id_cnt; i++)
|
2004-03-06 21:48:41 +00:00
|
|
|
Syslog('f', "\"%s\"", f_db.Desc[i]);
|
2001-11-19 21:34:21 +00:00
|
|
|
}
|
|
|
|
}
|
2001-11-25 12:18:18 +00:00
|
|
|
}
|
2001-11-18 23:19:08 +00:00
|
|
|
|
2001-11-25 12:18:18 +00:00
|
|
|
if (!File_id_cnt) {
|
|
|
|
if (Description == NULL) {
|
|
|
|
WriteError("No FILE_ID.DIZ and no description on the commandline");
|
|
|
|
if (!do_quiet)
|
2003-11-15 12:31:05 +00:00
|
|
|
printf("\nNo FILE_ID.DIZ and no description on the commandline\n");
|
2005-12-03 14:52:34 +00:00
|
|
|
clean_tmpwork();
|
2002-10-20 20:58:55 +00:00
|
|
|
die(MBERR_COMMANDLINE);
|
2001-11-19 21:34:21 +00:00
|
|
|
} else {
|
|
|
|
/*
|
2001-11-25 12:18:18 +00:00
|
|
|
* Create description from the commandline.
|
2001-11-19 21:34:21 +00:00
|
|
|
*/
|
2001-11-25 12:18:18 +00:00
|
|
|
if (strlen(Description) < 48) {
|
2001-11-19 21:34:21 +00:00
|
|
|
/*
|
2001-11-25 12:18:18 +00:00
|
|
|
* Less then 48 chars, copy and ready.
|
2001-11-19 21:34:21 +00:00
|
|
|
*/
|
2004-03-06 21:48:41 +00:00
|
|
|
strcpy(f_db.Desc[0], Description);
|
2001-11-25 12:18:18 +00:00
|
|
|
File_id_cnt++;
|
2001-11-19 21:34:21 +00:00
|
|
|
} else {
|
|
|
|
/*
|
2001-11-25 12:18:18 +00:00
|
|
|
* More then 48 characters, break into multiple
|
|
|
|
* lines not longer then 48 characters.
|
2001-11-19 21:34:21 +00:00
|
|
|
*/
|
2001-11-25 12:18:18 +00:00
|
|
|
memset(&TDesc, 0, sizeof(TDesc));
|
|
|
|
strcpy(TDesc, Description);
|
|
|
|
while (strlen(TDesc) > 48) {
|
|
|
|
j = 48;
|
|
|
|
while (TDesc[j] != ' ')
|
|
|
|
j--;
|
2004-03-06 21:48:41 +00:00
|
|
|
strncat(f_db.Desc[File_id_cnt], TDesc, j);
|
2001-11-25 12:18:18 +00:00
|
|
|
File_id_cnt++;
|
|
|
|
k = strlen(TDesc);
|
|
|
|
j++; /* Correct space */
|
|
|
|
for (i = 0; i <= k; i++, j++)
|
|
|
|
TDesc[i] = TDesc[j];
|
|
|
|
}
|
2004-03-06 21:48:41 +00:00
|
|
|
strcpy(f_db.Desc[File_id_cnt], TDesc);
|
2001-11-25 12:18:18 +00:00
|
|
|
File_id_cnt++;
|
2001-11-19 21:34:21 +00:00
|
|
|
}
|
|
|
|
}
|
2001-11-25 12:18:18 +00:00
|
|
|
}
|
2001-11-19 21:34:21 +00:00
|
|
|
|
2001-11-25 12:18:18 +00:00
|
|
|
/*
|
|
|
|
* Import the file.
|
|
|
|
*/
|
|
|
|
chdir(pwd);
|
2005-12-03 14:52:34 +00:00
|
|
|
clean_tmpwork();
|
|
|
|
|
2001-11-27 22:55:00 +00:00
|
|
|
/*
|
2004-12-22 12:12:10 +00:00
|
|
|
* Work out the kind of filename, is it a long filename
|
|
|
|
* or a 8.3 DOS filename. The file on disk must become
|
|
|
|
* 8.3 for import.
|
2001-11-27 22:55:00 +00:00
|
|
|
*/
|
2004-12-22 12:12:10 +00:00
|
|
|
if (is_real_8_3(File)) {
|
|
|
|
Syslog('f', "Adopt, file is 8.3");
|
|
|
|
strcpy(f_db.Name, File);
|
|
|
|
strcpy(f_db.LName, File);
|
|
|
|
for (i = 0; i < strlen(File); i++)
|
|
|
|
if (isupper(f_db.LName[i]))
|
|
|
|
f_db.LName[i] = tolower(f_db.LName[i]);
|
|
|
|
} else {
|
|
|
|
Syslog('f', "Adopt, file is LFN");
|
|
|
|
strcpy(temp2, File);
|
|
|
|
name_mangle(temp2);
|
|
|
|
if (rename(File, temp2)) {
|
|
|
|
Syslog('+', "Can't rename %s to %s", File, temp2);
|
|
|
|
if (!do_quiet)
|
|
|
|
printf("\nCan't rename %s to %s\n", File, temp2);
|
|
|
|
die(MBERR_GENERAL);
|
|
|
|
}
|
|
|
|
strcpy(f_db.Name, temp2);
|
|
|
|
strcpy(f_db.LName, File);
|
|
|
|
}
|
|
|
|
f_db.Size = file_size(f_db.Name);
|
|
|
|
f_db.Crc32 = file_crc(f_db.Name, TRUE);
|
|
|
|
f_db.FileDate = file_time(f_db.Name);
|
2005-08-28 14:52:14 +00:00
|
|
|
snprintf(temp2, PATH_MAX, "%s/%s", area.Path, f_db.Name);
|
2001-11-25 12:18:18 +00:00
|
|
|
|
|
|
|
if (!do_quiet) {
|
|
|
|
printf("Adding \b\b\b\b\b\b\b\b\b\b");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
2004-03-06 21:48:41 +00:00
|
|
|
if (strcmp(f_db.Name, f_db.LName)) {
|
2002-11-23 16:48:54 +00:00
|
|
|
lname = calloc(PATH_MAX, sizeof(char));
|
2005-08-28 14:52:14 +00:00
|
|
|
snprintf(lname, PATH_MAX, "%s/%s", area.Path, f_db.LName);
|
2004-12-22 12:12:10 +00:00
|
|
|
if (AddFile(f_db, Area, temp2, f_db.Name, lname) == FALSE) {
|
2002-11-23 16:48:54 +00:00
|
|
|
die(MBERR_GENERAL);
|
|
|
|
}
|
|
|
|
free(lname);
|
|
|
|
} else {
|
2004-03-06 21:48:41 +00:00
|
|
|
if (AddFile(f_db, Area, temp2, File, NULL) == FALSE) {
|
2002-11-23 16:48:54 +00:00
|
|
|
die(MBERR_GENERAL);
|
|
|
|
}
|
2001-11-25 12:18:18 +00:00
|
|
|
}
|
|
|
|
Syslog('+', "File %s added to area %d", File, Area);
|
|
|
|
|
|
|
|
if (MustRearc) {
|
|
|
|
/* Here we should call the rearc function */
|
2001-11-18 23:19:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free(pwd);
|
2001-11-25 12:18:18 +00:00
|
|
|
free(temp2);
|
2001-11-18 23:19:08 +00:00
|
|
|
free(temp);
|
2002-06-01 12:27:11 +00:00
|
|
|
free(tmpdir);
|
2001-11-18 23:19:08 +00:00
|
|
|
} else {
|
|
|
|
WriteError("Area %d is not available", Area);
|
|
|
|
if (!do_quiet)
|
2003-11-15 12:31:05 +00:00
|
|
|
printf("\nArea %d is not available\n", Area);
|
2001-11-18 23:19:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!do_quiet) {
|
|
|
|
printf("\r \r");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|