/***************************************************************************
 *                                                                         *
 *   Zabit: Content and attachment filter for qmail.                       *
 *   Version 0.7.1                                                         *
 *   http://www.enderunix.org/zabit                                        *
 *                                                                         *
 *   Copyright (C) 2004 by N.Ersen SISECI                                  *
 *   siseci@enderunix.org                                                  *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   This program 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 this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <stdio.h>
#include <time.h>
#include <string.h>
#include <unistd.h>

#include "zabit.h"
#include "attach.h"
#include "log.h"

char attachlist[ATTACHLISTSIZE][MAXFILENAMELEN];
char disattachlist[ATTACHLISTSIZE][MAXFILENAMELEN];
int attachlistcount = 0;
int disattachlistcount = 0;

void readattachfile(char *attachfile)
{
	FILE *block;
	int i;
	char inpbuf[WORDSIZE];

	if (strlen(attachfile) > (MAXFILENAMELEN - 1)) {
		printf("Error: %s\n", attachfile);
		exit(ERRORTMP);
	}

	if ((block = fopen(attachfile, "r")) == NULL) {
		printf("Can't open attach file: %s\n", attachfile);
		exit(ERRORTMP);
	}

	i = 0;

	memset(inpbuf, 0, WORDSIZE);
	while (!feof(block)) {
		fgets(inpbuf, WORDSIZE, block);
		if (feof(block))
			break;
		if (strlen(inpbuf) > 1)
			inpbuf[strlen(inpbuf) - 1] = '\0';
		if ((inpbuf[0] != '#') && (strlen(inpbuf) > 1)) {
			snprintf(attachlist[i], WORDSIZE - 1, "%s", inpbuf);
			i++;
		}
		if (i > ATTACHLISTSIZE)
			break;
	}
	fclose(block);
	attachlistcount = i;

	return;
}

int checkfilename(char *filename)
{
	int i;
	int fnamelen;
	int anamelen;
	char buf[MAXFILENAMELEN * 3];

	struct tm *ptr;
	time_t tm;
	char timestr[32];

#ifdef DEBUG
	printf("Checking Filename: %s\n", filename);
#endif

	for (i = 0; i < attachlistcount; i++) {
		fnamelen = strlen(filename);
		anamelen = strlen(attachlist[i]);
		if (fnamelen >= anamelen) {
			if (strncasecmp((filename + fnamelen - anamelen),
				attachlist[i], anamelen) == 0) {
				tm = time(NULL);
				ptr = localtime(&tm);
				sprintf(timestr, "%s", asctime(ptr));
				timestr[strlen(timestr) - 1] = 0;

				snprintf(buf, MAXFILENAMELEN * 3 - 1,
					 "%s Attach %s found. Extention: '%s'. Mail rejected.\n",
					 timestr, filename, attachlist[i]);
				printf("%s", buf);
				if (donotlog == 0)
					write_log(buf);
				if (scanonly == 0)
					return SPAMMAIL;
				else
					return 0;

			}
		}
	}
	return 0;
}


void readdisableattachfile(char *disattachfile)
{
        FILE *block;
        int i;
        char inpbuf[WORDSIZE];

        if (strlen(disattachfile) > (MAXFILENAMELEN - 1)) {
                printf("Error: %s\n", disattachfile);
                exit(ERRORTMP);
        }

        if ((block = fopen(disattachfile, "r")) == NULL) {
                printf("Can't open attach file: %s\n", disattachfile);
                exit(ERRORTMP);
        }

        i = 0;

        memset(inpbuf, 0, WORDSIZE);
        while (!feof(block)) {
                fgets(inpbuf, WORDSIZE, block);
                if (feof(block))
                        break;
                if (strlen(inpbuf) > 1)
                        inpbuf[strlen(inpbuf) - 1] = '\0';
                if ((inpbuf[0] != '#') && (strlen(inpbuf) > 1)) {
                        snprintf(disattachlist[i], WORDSIZE - 1, "%s", inpbuf);
                        i++;
                }
                if (i > ATTACHLISTSIZE)
                        break;
        }
        fclose(block);
        disattachlistcount = i;

        return;
}

int checkdisablefilename(char *filename)
{
	int i;
	int fnamelen;
	int danamelen;
	char buf[MAXFILENAMELEN * 3];

#ifdef DEBUG
	printf("Checking Disable Attach Filename: %s\n", filename);
#endif
	for (i = 0; i < disattachlistcount; i++) {
		fnamelen = strlen(filename);
		danamelen = strlen(disattachlist[i]);
		if (fnamelen >= danamelen) 
			if (strncasecmp((filename + fnamelen - danamelen),
				disattachlist[i], danamelen) == 0) 
					return 0;
	}
	return 1;
}


syntax highlighted by Code2HTML, v. 0.9.1