"Fossies" - the Fresh Open Source Software Archive

Member "libspf2-1.2.10/src/spftest/spftest.c" (28 Jan 2012, 5984 Bytes) of package /linux/privat/libspf2-1.2.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "spftest.c" see the Fossies "Dox" file reference documentation.

    1 /* 
    2  * This program is free software; you can redistribute it and/or modify
    3  * it under the terms of either:
    4  * 
    5  *   a) The GNU Lesser General Public License as published by the Free
    6  *    Software Foundation; either version 2.1, or (at your option) any
    7  *    later version,
    8  * 
    9  *   OR
   10  * 
   11  *   b) The two-clause BSD license.
   12  *
   13  * These licenses can be found with the distribution in the file LICENSES
   14  */
   15 
   16 
   17 
   18 
   19 /*
   20  * NOTE:
   21  *
   22  * This is just a test bed that can be used while developing the
   23  * library.  It is not intended to make sense or to be useful.
   24  */
   25 
   26 #define SPF_TEST_VERSION  "3.0"
   27 
   28 
   29 /* we include spf_internal.h so us internal config.h */
   30 #include "spf_sys_config.h"
   31 
   32 
   33 #ifdef STDC_HEADERS
   34 # include <stdio.h>
   35 # include <stdlib.h>       /* malloc / free */
   36 #endif
   37 
   38 #ifdef HAVE_SYS_TYPES_H
   39 #include <sys/types.h>  /* types (u_char .. etc..) */
   40 #endif
   41 
   42 #ifdef HAVE_INTTYPES_H
   43 #include <inttypes.h>
   44 
   45 #endif
   46 #ifdef HAVE_STRING_H
   47 # include <string.h>       /* strstr / strdup */
   48 #else
   49 # ifdef HAVE_STRINGS_H
   50 #  include <strings.h>     /* strstr / strdup */
   51 # endif
   52 #endif
   53 
   54 #ifdef HAVE_ARPA_INET_H
   55 # include <arpa/inet.h> /* in_addr struct */
   56 #endif
   57 
   58 #ifdef HAVE_NETDB_H
   59 # include <netdb.h> /* in_addr struct */
   60 #endif
   61 
   62 
   63 
   64 #include "spf.h"
   65 #include "spf_dns.h"
   66 #include "spf_dns_test.h"
   67 
   68 #include "spf_dns_internal.h"       /* we test the lookup functions     */
   69 
   70 
   71 #define TRUE 1
   72 #define FALSE 0
   73 
   74 
   75 
   76 static void usage()
   77 {
   78     printf( "Usage: spftest [spf \"<spf record>\" | domain <domain name>\n" );
   79     printf( "                | ip <ip address> | exp \"<explanation string>\"\n" );
   80     printf( "                | version ]\n" );
   81 }
   82 
   83 
   84 int
   85 main( int argc, char *argv[] )
   86 {
   87     SPF_server_t        *spf_server = NULL;
   88     SPF_request_t       *spf_request = NULL;
   89     SPF_response_t      *spf_response = NULL;
   90     SPF_record_t        *spf_record = NULL;
   91     SPF_error_t         *spf_error = NULL;
   92 
   93     char                *spf_rec;
   94     SPF_dns_rr_t        *dns_rr = NULL;
   95     
   96     SPF_errcode_t        err;
   97     int                  major, minor, patch;
   98     int                  i;
   99 
  100     spf_server = SPF_server_new(SPF_DNS_CACHE, 2);
  101 
  102     if ( argc <= 1 ) {
  103         usage();
  104         err = 1;
  105         goto error;
  106     }
  107 
  108     if ( strcmp( argv[1], "version" ) == 0 ) {
  109         fprintf( stderr, "spftest version information:\n" );
  110         fprintf( stderr, "SPF test system version: %s\n",
  111                  SPF_TEST_VERSION );
  112         fprintf( stderr, "Compiled with SPF library version: %d.%d.%d\n",
  113                  SPF_LIB_VERSION_MAJOR, SPF_LIB_VERSION_MINOR,
  114                  SPF_LIB_VERSION_PATCH );
  115         SPF_get_lib_version( &major, &minor, &patch );
  116         fprintf( stderr, "Running with SPF library version: %d.%d.%d\n",
  117                  major, minor, patch );
  118         fprintf( stderr, "\n" );
  119         err = 0;
  120         goto error;
  121     }
  122     
  123     if ( argc <= 2 ) {
  124         usage();
  125         err = 1;
  126         goto error;
  127     }
  128     else if ( strcmp( argv[1], "spf" ) == 0 )
  129         spf_rec = argv[2];
  130     else if ( strcmp( argv[1], "domain" ) == 0 )
  131     {
  132         dns_rr = SPF_dns_lookup( spf_server->resolver, argv[2], ns_t_txt, TRUE );
  133     
  134         if ( dns_rr->herrno != NETDB_SUCCESS )
  135         {
  136             printf( "DNS lookup for \"%s\" failed:  %d\n",
  137                     argv[1], dns_rr->herrno );
  138             err = 1;
  139             goto error;
  140         }
  141         spf_rec = dns_rr->rr[0]->txt;
  142     }
  143     else if ( strcmp( argv[1], "ip" ) == 0 )
  144     {
  145         struct in_addr ipv4;
  146         ipv4.s_addr = 0x04030201;
  147         
  148         dns_rr = SPF_dns_rlookup( spf_server->resolver, ipv4, ns_t_ptr, TRUE );
  149     
  150         if ( dns_rr->herrno != NETDB_SUCCESS )
  151         {
  152             printf( "DNS lookup for \"%s\" failed:  %d\n",
  153                     argv[1], dns_rr->herrno );
  154             err = 1;
  155             goto error;
  156         }
  157         spf_rec = dns_rr->rr[0]->txt;
  158 
  159         /* FIXME: do something with the rlookup */
  160         err = 1;
  161         goto error;
  162     }
  163     else if ( strcmp( argv[1], "exp" ) == 0 ) {
  164         int     len;
  165         char        *p, *s;
  166         
  167         len = strlen( argv[2] );
  168         spf_rec = malloc( len * 2 + sizeof( "v=spf1 exp-text=" ) );
  169         
  170         strcpy( spf_rec, "v=spf1 exp-text=" );
  171         
  172         p = spf_rec + sizeof( "v=spf1 exp-text=" ) - 1;
  173         s = argv[2];
  174 
  175         while( *s != '\0' ) {
  176             if ( *s == ' ' ) {
  177                 *p++ = '%';
  178                 *p++ = '_';
  179             }
  180             else {
  181                 *p++ = *s;
  182             }
  183             s++;
  184         }
  185         *p = *s;
  186         
  187     }
  188     else {
  189         usage();
  190         err = 1;
  191         goto error;
  192     }
  193     
  194     spf_request = SPF_request_new(spf_server);
  195     spf_response = SPF_response_new(spf_request);
  196 
  197 
  198     printf( "SPF record in:  %s\n", spf_rec );
  199     err = SPF_record_compile(spf_server, spf_response,
  200                     &spf_record, spf_rec);
  201 #if 0
  202     printf("Code is %d with %d messages, %d errors\n",
  203                     err,
  204                     SPF_response_messages(spf_response),
  205                     SPF_response_errors(spf_response));
  206 #endif
  207     if (SPF_response_messages(spf_response) > 0) {
  208         for (i = 0; i < SPF_response_messages(spf_response); i++) {
  209             spf_error = SPF_response_message(spf_response, i);
  210             printf( "%s: %s%s\n",
  211                     SPF_error_errorp(spf_error) ? "Error" : "Warning",
  212                     // SPF_error_code(spf_error),
  213                     // SPF_strerror(SPF_error_code(spf_error)),
  214                     ((SPF_error_errorp(spf_error) && (!err))
  215                             ? "[UNRETURNED "
  216                             : ""),
  217                     SPF_error_message(spf_error) );
  218         }
  219         if (SPF_response_errors(spf_response) > 0) {
  220             if (spf_record) {
  221                 SPF_record_free(spf_record);
  222                 spf_record = NULL;
  223             }
  224         }
  225     }
  226     else if ( err ) {
  227         printf( "Error: %s (null err_msg)\n", SPF_strerror( err ) );
  228         if (spf_record) {
  229             SPF_record_free(spf_record);
  230             spf_record = NULL;
  231         }
  232     }
  233     else {
  234         printf( "no errors\n" );
  235     }
  236 
  237     SPF_record_print( spf_record );
  238 
  239 #if 0
  240     if ( strcmp( argv[1], "exp" ) == 0 )
  241     {
  242         char        *buf = NULL;
  243         int     buf_len = 0;
  244         int     err;
  245         
  246         SPF_set_rec_dom( spfcid, "midwestcs.com" );
  247 
  248         SPF_set_helo_dom( spfcid, "example.com" );
  249         SPF_set_ipv4_str( spfcid, "192.0.2.3" );
  250         SPF_set_env_from( spfcid, "strong-bad@email.example.com" );
  251 
  252         err = SPF_find_mod_value( spfcid, c_results.spfid, spfdcid, "exp-text", &buf, &buf_len );
  253         if ( err )
  254             printf( "%s\n", SPF_strerror( err ) );
  255         else
  256             printf( "err=%d  buf_len = %d  buf=\"%s\"\n", err, buf_len, buf );
  257 
  258         free( spf_rec );
  259         if ( buf ) free( buf );
  260     }
  261 #endif
  262 
  263   error:
  264     if (spf_response)
  265         SPF_response_free(spf_response);
  266     if (spf_record)
  267         SPF_record_free(spf_record);
  268     if (spf_request)
  269         SPF_request_free(spf_request);
  270     if (dns_rr)
  271         SPF_dns_rr_free(dns_rr);
  272     if (spf_server)
  273         SPF_server_free(spf_server);
  274 
  275     return err;
  276 }