summaryrefslogtreecommitdiff
path: root/tools/spf_example.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/spf_example.c')
-rw-r--r--tools/spf_example.c200
1 files changed, 200 insertions, 0 deletions
diff --git a/tools/spf_example.c b/tools/spf_example.c
new file mode 100644
index 0000000..30a84d6
--- /dev/null
+++ b/tools/spf_example.c
@@ -0,0 +1,200 @@
+/*
+ * 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 <stdio.h>
+# include <stdlib.h> /* malloc / free */
+#endif
+
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h> /* types (u_char .. etc..) */
+#endif
+
+#ifdef HAVE_INTTYPES_H
+#include <inttypes.h>
+#endif
+
+#ifdef HAVE_STRING_H
+# include <string.h> /* strstr / strdup */
+#else
+# ifdef HAVE_STRINGS_H
+# include <strings.h> /* strstr / strdup */
+# endif
+#endif
+
+#ifdef HAVE_SYS_SOCKET_H
+# include <sys/socket.h> /* inet_ functions / structs */
+#endif
+#ifdef HAVE_NETINET_IN_H
+# include <netinet/in.h> /* inet_ functions / structs */
+#endif
+#ifdef HAVE_ARPA_INET_H
+# include <arpa/inet.h> /* in_addr struct */
+#endif
+
+#ifdef HAVE_ARPA_NAMESER_H
+# include <arpa/nameser.h> /* DNS HEADER struct */
+#endif
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#ifdef HAVE_GETOPT_H
+#include <getopt.h>
+#endif
+
+/*
+ * libspf2 public include files that are needed for this example
+ * program
+ */
+
+#include <string>
+#include <list>
+#include <iostream>
+#include <sstream>
+#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 <IP address> The IP address that is sending email\n"
+ " -F <email address> 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 <email address> [optional] The email address used as\n"
+ " the envelope RCPT TO email address, for\n"
+ " secondary-MX checking.\n"
+ " -H <domain name> 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;
+
+}