00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #define SPF_TEST_VERSION "3.0"
00027
00028
00029
00030 #include "spf_sys_config.h"
00031
00032
00033 #ifdef STDC_HEADERS
00034 # include <stdio.h>
00035 # include <stdlib.h>
00036 #endif
00037
00038 #ifdef HAVE_SYS_TYPES_H
00039 #include <sys/types.h>
00040 #endif
00041
00042 #ifdef HAVE_INTTYPES_H
00043 #include <inttypes.h>
00044
00045 #endif
00046 #ifdef HAVE_STRING_H
00047 # include <string.h>
00048 #else
00049 # ifdef HAVE_STRINGS_H
00050 # include <strings.h>
00051 # endif
00052 #endif
00053
00054 #ifdef HAVE_ARPA_INET_H
00055 # include <arpa/inet.h>
00056 #endif
00057
00058 #ifdef HAVE_NETDB_H
00059 # include <netdb.h>
00060 #endif
00061
00062
00063
00064 #include "spf.h"
00065 #include "spf_dns.h"
00066 #include "spf_dns_test.h"
00067
00068 #include "spf_dns_internal.h"
00069
00070
00071 #define TRUE 1
00072 #define FALSE 0
00073
00074
00075
00076 static void usage()
00077 {
00078 printf( "Usage: spftest [spf \"<spf record>\" | domain <domain name>\n" );
00079 printf( " | ip <ip address> | exp \"<explanation string>\"\n" );
00080 printf( " | version ]\n" );
00081 }
00082
00083
00084 int
00085 main( int argc, char *argv[] )
00086 {
00087 SPF_server_t *spf_server = NULL;
00088 SPF_request_t *spf_request = NULL;
00089 SPF_response_t *spf_response = NULL;
00090 SPF_record_t *spf_record = NULL;
00091 SPF_error_t *spf_error = NULL;
00092
00093 char *spf_rec;
00094 SPF_dns_rr_t *dns_rr = NULL;
00095
00096 SPF_errcode_t err;
00097 int major, minor, patch;
00098 int i;
00099
00100 spf_server = SPF_server_new(SPF_DNS_CACHE, 2);
00101
00102 if ( argc <= 1 ) {
00103 usage();
00104 err = 1;
00105 goto error;
00106 }
00107
00108 if ( strcmp( argv[1], "version" ) == 0 ) {
00109 fprintf( stderr, "spftest version information:\n" );
00110 fprintf( stderr, "SPF test system version: %s\n",
00111 SPF_TEST_VERSION );
00112 fprintf( stderr, "Compiled with SPF library version: %d.%d.%d\n",
00113 SPF_LIB_VERSION_MAJOR, SPF_LIB_VERSION_MINOR,
00114 SPF_LIB_VERSION_PATCH );
00115 SPF_get_lib_version( &major, &minor, &patch );
00116 fprintf( stderr, "Running with SPF library version: %d.%d.%d\n",
00117 major, minor, patch );
00118 fprintf( stderr, "\n" );
00119 err = 0;
00120 goto error;
00121 }
00122
00123 if ( argc <= 2 ) {
00124 usage();
00125 err = 1;
00126 goto error;
00127 }
00128 else if ( strcmp( argv[1], "spf" ) == 0 )
00129 spf_rec = argv[2];
00130 else if ( strcmp( argv[1], "domain" ) == 0 )
00131 {
00132 dns_rr = SPF_dns_lookup( spf_server->resolver, argv[2], ns_t_txt, TRUE );
00133
00134 if ( dns_rr->herrno != NETDB_SUCCESS )
00135 {
00136 printf( "DNS lookup for \"%s\" failed: %d\n",
00137 argv[1], dns_rr->herrno );
00138 err = 1;
00139 goto error;
00140 }
00141 spf_rec = dns_rr->rr[0]->txt;
00142 }
00143 else if ( strcmp( argv[1], "ip" ) == 0 )
00144 {
00145 struct in_addr ipv4;
00146 ipv4.s_addr = 0x04030201;
00147
00148 dns_rr = SPF_dns_rlookup( spf_server->resolver, ipv4, ns_t_ptr, TRUE );
00149
00150 if ( dns_rr->herrno != NETDB_SUCCESS )
00151 {
00152 printf( "DNS lookup for \"%s\" failed: %d\n",
00153 argv[1], dns_rr->herrno );
00154 err = 1;
00155 goto error;
00156 }
00157 spf_rec = dns_rr->rr[0]->txt;
00158
00159
00160 err = 1;
00161 goto error;
00162 }
00163 else if ( strcmp( argv[1], "exp" ) == 0 ) {
00164 int len;
00165 char *p, *s;
00166
00167 len = strlen( argv[2] );
00168 spf_rec = malloc( len * 2 + sizeof( "v=spf1 exp-text=" ) );
00169
00170 strcpy( spf_rec, "v=spf1 exp-text=" );
00171
00172 p = spf_rec + sizeof( "v=spf1 exp-text=" ) - 1;
00173 s = argv[2];
00174
00175 while( *s != '\0' ) {
00176 if ( *s == ' ' ) {
00177 *p++ = '%';
00178 *p++ = '_';
00179 }
00180 else {
00181 *p++ = *s;
00182 }
00183 s++;
00184 }
00185 *p = *s;
00186
00187 }
00188 else {
00189 usage();
00190 err = 1;
00191 goto error;
00192 }
00193
00194 spf_request = SPF_request_new(spf_server);
00195 spf_response = SPF_response_new(spf_request);
00196
00197
00198 printf( "SPF record in: %s\n", spf_rec );
00199 err = SPF_record_compile(spf_server, spf_response,
00200 &spf_record, spf_rec);
00201 #if 0
00202 printf("Code is %d with %d messages, %d errors\n",
00203 err,
00204 SPF_response_messages(spf_response),
00205 SPF_response_errors(spf_response));
00206 #endif
00207 if (SPF_response_messages(spf_response) > 0) {
00208 for (i = 0; i < SPF_response_messages(spf_response); i++) {
00209 spf_error = SPF_response_message(spf_response, i);
00210 printf( "%s: %s%s\n",
00211 SPF_error_errorp(spf_error) ? "Error" : "Warning",
00212
00213
00214 ((SPF_error_errorp(spf_error) && (!err))
00215 ? "[UNRETURNED "
00216 : ""),
00217 SPF_error_message(spf_error) );
00218 }
00219 if (SPF_response_errors(spf_response) > 0) {
00220 if (spf_record) {
00221 SPF_record_free(spf_record);
00222 spf_record = NULL;
00223 }
00224 }
00225 }
00226 else if ( err ) {
00227 printf( "Error: %s (null err_msg)\n", SPF_strerror( err ) );
00228 if (spf_record) {
00229 SPF_record_free(spf_record);
00230 spf_record = NULL;
00231 }
00232 }
00233 else {
00234 printf( "no errors\n" );
00235 }
00236
00237 SPF_record_print( spf_record );
00238
00239 #if 0
00240 if ( strcmp( argv[1], "exp" ) == 0 )
00241 {
00242 char *buf = NULL;
00243 int buf_len = 0;
00244 int err;
00245
00246 SPF_set_rec_dom( spfcid, "midwestcs.com" );
00247
00248 SPF_set_helo_dom( spfcid, "example.com" );
00249 SPF_set_ipv4_str( spfcid, "192.0.2.3" );
00250 SPF_set_env_from( spfcid, "strong-bad@email.example.com" );
00251
00252 err = SPF_find_mod_value( spfcid, c_results.spfid, spfdcid, "exp-text", &buf, &buf_len );
00253 if ( err )
00254 printf( "%s\n", SPF_strerror( err ) );
00255 else
00256 printf( "err=%d buf_len = %d buf=\"%s\"\n", err, buf_len, buf );
00257
00258 free( spf_rec );
00259 if ( buf ) free( buf );
00260 }
00261 #endif
00262
00263 error:
00264 if (spf_response)
00265 SPF_response_free(spf_response);
00266 if (spf_record)
00267 SPF_record_free(spf_record);
00268 if (spf_request)
00269 SPF_request_free(spf_request);
00270 if (dns_rr)
00271 SPF_dns_rr_free(dns_rr);
00272 if (spf_server)
00273 SPF_server_free(spf_server);
00274
00275 return err;
00276 }