| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 
 |  
#include <stdio.h>
#include <sys/wait.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h> 
#include <sys/stat.h> 
#include <unistd.h> 
/* Exim includes */
#include "local_scan.h"
/* JunkProbe include */
#include "libJunkProbe.h"
 
/* Macros */
#define TIMEOUT			20
#ifndef FILTER_LOCATION
#  define FILTER_LOCATION 	"/usr/bin/bmf"
#endif
 
#define US   (unsigned char *)
 
/* Importation des fonctions d'Exim */
extern uschar  *spool_directory;
extern uschar 	*primary_hostname;
extern int 	strcmpic(uschar *s, uschar *t);
 
// ou 'uschar' est un 'unsigned char' dans l'API 'local_scan.h' //
int junkprobe(uschar **yield, int argc, uschar *argv[]) {
 
    volatile FILE 	*scan_fout = NULL;
    uschar 		buffer[1024];
    int 		execn = 0;
    uschar 		**exec = malloc(sizeof (char*) * 15);
    int 		mod_reg = 0;
    uschar 		*flag = NULL;
    uschar 		*learn = US"yes";
    float 		value = -1, require = -1;
    struct stat 	st;
    int 		i = 0;
    char 		*cmd = NULL;
    int 		defer_ok = 0;
 
/* benchmark: */
    starttime = benchmark();
 
/* command */
    exec[execn++] = US FILTER_LOCATION;
 
    if ( argc < 1 ) /* argv[0] , n'est pas utiliser pour indiquer le chemin */
                          /* de la database de BMF . Attribution de la base par defaut. */
	argv[0] = string_sprintf("%s/junkprobe/bmf", spool_directory);
 
/* argv[0] contient le type et le chemin de la base de cette */
/* manière: {db:/var/spool/exim/bmf} */
    switch( argv[0][0] ) {
	case 't': /* type text */
	    argv[0] += 5;
	    exec[execn++] = US"text";
	    break;
	case 'd': /* type db */
	    argv[0] += 3;
	    exec[execn++] = US"db";
	    break;
	case 'm': /* type mysql */
	    argv[0] += 6;
	    exec[execn++] = US"mysql";
	    break;
	default:
	    execn = 1; /* le type n'est pas reconnu, on laisse alors BMF */
                              /* le detecter lui-même. On enlève l'argument '-f' */
                              /* inseré plus haut */
	    break; 
    };
 
/* vérification de la database */
    if ( ! exec[execn-1][0] == 'm'   /* pas de teste si type MySQL */
	|| Ustat(argv[0], &st) == -1  /* la database doit exister*/
	|| ! S_ISDIR(st.st_mode) ) {  /* la database est un répertoire */
	log_write(0, LOG_MAIN
	    , "${dlfunc bmf warning: failed calling database %s", argv[0]);
 
	if ( ( exec[1][0] == '-' )
	    || ( exec[1][1] == 'f' ) )
	    execn = 1; /* le type n'est pas reconnu, on laisse alors BMF */
                              /* le detecter lui-même. On enlève l'argument '-f' */
                              /* inseré plus haut */
 
	/* Alors on met la DB par defaut */
	argv[0] = string_sprintf("%s/junkprobe/bmf", spool_directory);
 
       /* on re-teste l'exsistance de la DB pour valider */
	if ( stat(argv[0], &st) == -1 ) {
	    log_write(0, LOG_MAIN | LOG_PANIC
		, "${dlfunc bmf ERROR: failed calling database %s"
		, argv[0]);
	    goto bail;
	};
    };
 
/* tout va bien, on ajoute l'agument '-d' pour BMF */
    exec[execn++] = US"-d";
/* chemin de la DB */
    exec[execn++] = argv[0];
 
/* "le getopt() du pauvre" pour récuper les arguments pour BMF */
/* et les valider */
    while ( (cmd = strtok( (i == 0) ? (char*)(argv[1]) : NULL, " "))) {
	i++;
 
	while (cmd[0] == '-') cmd++; /* on enlève le ou les "-" */
 
	switch ( cmd[0] ) {
	    case 't': /* mode teste. */
		exec[execn++] = US"-t";
		learn = US"no";
		mod_reg = 0;
		break;
	    case 'n': /* enregistrement non-spam. */
		exec[execn++] = US"-n";
		learn = US"ham";
		mod_reg = 1;
		break;
	    case 'N': /* Enregistrement non-spam et nétoyage spam. */
		exec[execn++] = US"-N";
		learn = US"reg_ham";
		mod_reg = 1;
		break;
	    case 's': /* enregistrement spam. */
		exec[execn++] = US"-s";
		learn = US"spam";
		mod_reg = 2;
		break;
	    case 'S': /* Enregistrement spam et nétoyage non-spam. */
		exec[execn++] = US"-S";
		learn = US"reg_spam";
		mod_reg = 2;
		break;
	    case 'k': /* pour le Bayes calculation. Defaut 15. */
		cmd = strtok(NULL, " ");
		if ( ! isdigit(cmd[0]) ) /* l'argument suivant doit être de type 'digit' */
		    break;
		exec[execn++] = US"-k";
		exec[execn++] = US cmd;
		break;
	    case 'd':
		if ( strcmpic( US cmd, US"defer_ok" ) == 0 )
		    defer_ok = 1; /* en cas de probleme */
		break;
            default:
		break;
	};
	if ( i >= 3 ) /* pas plus de  >= 3 arguments */
	    break;
    };
 
    exec[execn++] = US"-p";
    exec[execn++] = NULL;
 
/* Ouverture du pipe avec avec les arguments réciltés plus haut */
    scan_fout = (FILE *)open_pipe(exec, NULL, TIMEOUT);
    if ( scan_fout == (FILE *)NULL )
	goto bail;
 
   free(exec); | 
Partager