Пример #1
0
int main (int argc, char const *argv[]) {
    SMFSettings_T *settings = smf_settings_new();
    int pipefd[2];
    char *fname;

    printf("Start smf_pipe tests...\n");

    fflush(stdout); 

    if (pipe(pipefd)) {
        return -1;
    }

    printf("* preparing pipe engine...\t\t\t");
    smf_settings_set_debug(settings, 1);
    smf_settings_set_queue_dir(settings, BINARY_DIR);
    smf_settings_set_engine(settings, "pipe");
    smf_settings_set_nexthop(settings, "/dev/null");
    
    /* add test modules */
    smf_settings_add_module(settings, BINARY_DIR "/libtestmod1.so");
    smf_settings_add_module(settings, BINARY_DIR "/libtestmod2.so");

    asprintf(&fname, "%s/m0001.txt",SAMPLES_DIR);

    switch (fork()) {
        case -1:
            printf("failed\n");
            return -1;
        case 0:
            close(pipefd[0]); 
            dup2(pipefd[1], 1); 
            close(pipefd[1]);  
            execl("/bin/cat", "cat", fname, (char *)NULL);
            
            break;
        default:
            close(pipefd[1]);  
            dup2(pipefd[0], 0);
            close(pipefd[0]); 
            printf("passed\n");
            printf("* sending test message...\t\t\t\t");
            if (load(settings) != 0) {
                printf("failed\n");
                return -1;
            }

            printf("passed\n");
            break;
    }
    free(fname);
    smf_settings_free(settings);
    return 0;
}
Пример #2
0
static void setup() {
    fail_unless((settings = smf_settings_new()) != NULL);
    smf_settings_set_queue_dir(settings, BINARY_DIR);  
    fail_unless((session = smf_session_new()) != NULL);
    
    queue = smf_modules_pqueue_init(error_cb, processing_error_cb, nexthop_error_cb);
    fail_unless(queue != NULL);
    
    create_sample_message(session, SAMPLES_DIR "/m0001.txt");
    create_spoolfile(session, SAMPLES_DIR "/m0001.txt");
        
    init_cb_data(&mod1_data);
    init_cb_data(&mod2_data);
    init_cb_data(&mod3_data);
    init_cb_data(&error_data);
    init_cb_data(&processing_error_data);
    init_cb_data(&nexthop_error_data);
}
Пример #3
0
int main(int argc, char *argv[]) {
    int ret = 0;
    int option = 0;
    int debug = 0;
    int opt_index = 0;
    char *config_file = NULL;
    SMFSettings_T *settings = NULL;
    struct stat sb;
    
    struct option long_options[] = {
        { "debug", 0, NULL, 'd' },
        { "file", 1, NULL, 'f' },
        { "help", 0, NULL, 'h' },
        { NULL, 0, NULL, 0 }
    };

    while(1) {
        option = getopt_long(argc, argv, "df:h",long_options,&opt_index);
        if (option == EOF)
            break;

        switch(option) {
            case 'h':
                usage();
                break;
            case 'd':
                debug = 1;
                break;
            case 'f':
                config_file = strdup(optarg);
                break;
            default:
                usage();
                break;
        }
    }

    settings = smf_settings_new();
    /* parse config file and fill settings struct */
    if (smf_settings_parse_config(&settings,config_file) != 0) {
        if (config_file != NULL) free(config_file);
        smf_settings_free(settings);
        return -1;
    }

    if (config_file != NULL) free(config_file);

    if (debug == 1)
        smf_settings_set_debug(settings,debug);

    openlog("spmfilter", LOG_PID, smf_settings_get_syslog_facility(settings));

    /* connect to database/ldap server, if necessary */
    if((settings->backend != NULL) && (settings->lookup_persistent == 1)) {
#ifdef HAVE_LDAP
        if (strcmp(settings->backend,"ldap") == 0) {
            if (smf_lookup_ldap_connect(settings) != 0) {
                fprintf(stderr,"spmfilter: unable to establish lookup connection!");
                return -1;
            }
        }
#endif

#ifdef HAVE_ZDB
        if (strcmp(settings->backend,"sql") == 0) {
            if (smf_lookup_sql_connect(settings) != 0) {
                fprintf(stderr,"spmfilter: unable to establish lookup connection!");
                return -1;
            }
        }
#endif  
    }

    /* check queue dir */
    if (stat(settings->queue_dir,&sb) != 0) {
        perror("spmfilter: failed to open queue directory");
        smf_settings_free(settings);
        return -1; 
    }

    if(!S_ISDIR(sb.st_mode)) {
        fprintf(stderr, "spmfilter: [%s] is not a directory",settings->queue_dir);
        smf_settings_free(settings);
        return -1; 
    }
        
    if (access(settings->queue_dir,W_OK) != 0) {
        perror("spmfilter: queue directory is not writeable");
        smf_settings_free(settings);
        return -1; 
    }

    ret = smf_modules_engine_load(settings);

    if((settings->backend != NULL) && (settings->lookup_persistent == 1)) {
#ifdef HAVE_LDAP
        if (strcmp(settings->backend,"ldap") == 0)
            smf_lookup_ldap_disconnect(settings);
#endif

#ifdef HAVE_ZDB
        if (strcmp(settings->backend,"sql") == 0) 
            smf_lookup_sql_disconnect(settings);
#endif  
    }

    /* free all stuff */
    smf_settings_free(settings);

    return ret;
}
Пример #4
0
int main (int argc, char const *argv[]) {
    char *msg_file = NULL;
    SMFSmtpStatus_T *status = NULL;
    SMFSettings_T *settings = smf_settings_new();
    SMFEnvelope_T *env = smf_envelope_new();
    pid_t pid;

    smf_settings_set_pid_file(settings, "/tmp/smf_test_smtpd.pid");
    smf_settings_set_bind_ip(settings, "127.0.0.1");
    smf_settings_set_foreground(settings, 1);
    smf_settings_set_spare_childs(settings, 0);
    smf_settings_set_max_childs(settings,1);
    smf_settings_set_debug(settings,1);
    smf_settings_set_queue_dir(settings, BINARY_DIR);
    smf_settings_set_engine(settings, "smtpd");

    /* add test modules */
    smf_settings_add_module(settings, BINARY_DIR "/libtestmod1.so");
    smf_settings_add_module(settings, BINARY_DIR "/libtestmod2.so");
    
    printf("Start smf_smtpd tests...\n");

    printf("* preparing smtpd engine...\t\t\t");
    
    printf("passed\n");
    switch(pid = fork()) {
        case -1:
            printf("failed\n");
            break;
        case 0:
            if (load(settings) != 0) {
                return -1;
            }
            break;

        default:
            printf("* sending test message ...\t\t\t");
            asprintf(&msg_file, "%s/m0001.txt",SAMPLES_DIR);

            smf_envelope_set_nexthop(env, "127.0.0.1:33332");
            smf_envelope_set_sender(env, test_email);
            smf_envelope_add_rcpt(env, test_email);
            status = smf_smtp_deliver(env, SMF_TLS_DISABLED, msg_file, NULL);
            
            if (status->code == -1) {
                kill(pid,SIGTERM);
                printf("failed\n");
                return -1;
            }
            
            smf_smtp_status_free(status);
            printf("passed\n");
            kill(pid,SIGTERM);
            waitpid(pid, NULL, 0);

            break;
    }

    if(msg_file != NULL)
        free(msg_file);
        
    
    smf_settings_free(settings);
    smf_envelope_free(env);

    return 0;
}