/* * spf_example - An example program for how to use libspf2 * */ /* * The libspf2 library uses the GNU autoconf system to help make * the library more portable. The config.h file should have the * HAVE_xxx defines that are appropriate for your system. Either use * autconf to create it, or create it by hand. */ #include "utils.h" # include "spf_config.h" /* jsd: copied from somewhere */ /* not quite created by hand, */ /* but not now automatic, either */ #ifdef STDC_HEADERS # include # include /* malloc / free */ #endif #ifdef HAVE_SYS_TYPES_H #include /* types (u_char .. etc..) */ #endif #ifdef HAVE_INTTYPES_H #include #endif #ifdef HAVE_STRING_H # include /* strstr / strdup */ #else # ifdef HAVE_STRINGS_H # include /* strstr / strdup */ # endif #endif #ifdef HAVE_SYS_SOCKET_H # include /* inet_ functions / structs */ #endif #ifdef HAVE_NETINET_IN_H # include /* inet_ functions / structs */ #endif #ifdef HAVE_ARPA_INET_H # include /* in_addr struct */ #endif #ifdef HAVE_ARPA_NAMESER_H # include /* DNS HEADER struct */ #endif #ifdef HAVE_UNISTD_H #include #endif #ifdef HAVE_GETOPT_H #include #endif /* * libspf2 public include files that are needed for this example * program */ #include #include #include #include #include "sepofra.h" using namespace std; /* * usage() just prints out the command line options for this program */ static void usage() { fprintf( stderr, "Usage:\n" " spf_example [options]\n" "\n" "Valid data options are:\n" " -I The IP address that is sending email\n" " -F The email address used as the\n" " envelope MAIL FROM. If no username (local\n" " part) is given, 'postmaster' will be\n" " assumed.\n" " -T [optional] The email address used as\n" " the envelope RCPT TO email address, for\n" " secondary-MX checking.\n" " -H The domain name given on the SMTP HELO\n" " command. This is only needed if the\n" " -F option is not given.\n" " -d increase debug level.\n" " -d5 set debug level to 5.\n" ); } /* * All the code is in the main routine, but most usages of libspf2 * would have the code spread around into various subrotines. */ int main( int argc, char *argv[] ) { sepofra my_spf; int c; int res = 0; string opt_ip; string opt_mailfrom; string opt_helo; string opt_rcpt_to; int opt_debug = 0; /* * check the arguments */ while (1) { c = getopt(argc, argv, "hI:F:H:T:d::" ); if (c == -1) break; switch (c) { case 'h': usage(); exit(0); case 'I': opt_ip = trim(optarg); break; case 'F': opt_mailfrom = trim(optarg, " \t\r\n<>"); break; case 'H': opt_helo = trim(optarg, " \t\r\n<>"); break; case 'T': opt_rcpt_to = optarg; break; case 0: case '?': goto helpout; break; case 'd': if (optarg == NULL) opt_debug++; else opt_debug = atoi( optarg ); break; default: fprintf( stderr, "Error: getopt returned character code 0%o ??\n", c); } } if (optind != argc) { fprintf(stderr, "extraneous verbiage: '%s'\n", argv[optind]); goto helpout; } if (!opt_ip.length()){ fprintf(stderr, "Option -I 'IP.address' is required\n"); goto helpout; } if (!opt_helo.length()){ fprintf(stderr, "option '-H host.name' is required\n"); goto helpout; } if (!opt_mailfrom.length()){ fprintf(stderr, "option '-F mail@from' is required\n"); goto helpout; } try { my_spf.check(opt_ip, opt_helo, opt_mailfrom, opt_rcpt_to, opt_debug); cout << "*** " << my_spf.explain() << endl; } catch (bad_thing foo) { cout << "Caught bad thing: " << foo.what() << endl; } return 0; helpout: fprintf(stderr, "for help try -h\n"); res = 255; return res; }