spfquery.c

Go to the documentation of this file.
00001 /* spfquery - Sender Permitted From command line utility 00002 * 00003 * Author: Wayne Schlitt <wayne@midwestcs.com> 00004 * 00005 * File: spfquery.c 00006 * Desc: SPF command line utility 00007 * 00008 * 00009 * This program is free software; you can redistribute it and/or modify 00010 * it under the terms of either: 00011 * 00012 * a) the GNU General Public License as published by the Free Software 00013 * Foundation; either version 1, or (at your option) any later 00014 * version, or 00015 * 00016 * OR 00017 * 00018 * b) The following license: 00019 * 00020 * License: 00021 * 00022 * The libspf Software License, Version 1.0 00023 * 00024 * Copyright (c) 2004 Wayne Schlitt All rights reserved. 00025 * 00026 * Redistribution and use in source and binary forms, with or without 00027 * modification, are permitted provided that the following conditions 00028 * are met: 00029 * 00030 * 1. Redistributions of source code must retain the above copyright 00031 * notice, this list of conditions and the following disclaimer. 00032 * 00033 * 2. Redistributions in binary form must reproduce the above copyright 00034 * notice, this list of conditions and the following disclaimer in 00035 * the documentation and/or other materials provided with the 00036 * distribution. 00037 * 00038 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 00039 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00040 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00041 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS MAKING USE OF THIS LICESEN 00042 * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00043 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00044 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 00045 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00046 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00047 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 00048 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00049 * SUCH DAMAGE. 00050 * 00051 */ 00052 00053 #include "spfquery.h" 00054 00055 int main(int argc, char *argv[]) 00056 { 00057 int c; 00058 int res; 00059 00060 char *opt_file = NULL; 00061 char *opt_ipv4 = NULL; /* Ipv4 from addr */ 00062 char *opt_sender = NULL; /* MAIL FROM: */ 00063 char *opt_helo = NULL; /* HELO */ 00064 char *opt_rcpt_to = NULL; /* RCPT TO: */ 00065 char *opt_local = NULL; /* local mechainism/modifier prefix */ 00066 char *opt_trusted = NULL; /* trusted-forwarder spf query */ 00067 char *opt_guess = NULL; /* best guest spf query */ 00068 char *opt_exp = NULL; /* explanation string */ 00069 char *opt_max_lookup = NULL; 00070 char *opt_sanitize = NULL; 00071 char *opt_debuglevel = NULL; /* debug level */ 00072 char *opt_myhostname = "spfquery"; 00073 00074 char in_line[4096]; 00075 char *p; 00076 00077 int dbg_level = 0; 00078 00079 peer_info_t *peer_info = NULL; 00080 00081 FILE *fin; 00082 00083 while (TRUE) 00084 { 00085 int option_index = 0; 00086 00087 static struct option long_options[] = 00088 { 00089 { "file", 1, 0, 'f' }, 00090 { "ipv4", 1, 0, 'i' }, 00091 { "sender", 1, 0, 's' }, 00092 { "helo", 1, 0, 'h' }, 00093 { "rcpt-to", 1, 0, 'r' }, 00094 { "local", 1, 0, 'l' }, 00095 { "trusted", 1, 0, 't' }, 00096 { "guess", 1, 0, 'g' }, 00097 { "default_explanation", 1, 0, 'e' }, 00098 { "max-lookup", 1, 0, 'm' }, 00099 { "sanitize", 1, 0, 'c' }, 00100 { "myhostname", 1, 0, 'n' }, 00101 { "verbose", 1, 0, 'v' }, 00102 { "help", 0, 0, '?' }, 00103 { 0, 0, 0, 0 } 00104 }; 00105 00106 c = getopt_long (argc, argv, "f:i:s:h:r:v:ltgemcn", 00107 long_options, &option_index); 00108 00109 if (c == -1) 00110 { 00111 break; 00112 } 00113 00114 /* build arguments from switches passed */ 00115 switch (c) 00116 { 00117 case '?': 00118 SPF_usage(); 00119 return 1; 00120 break; 00121 case 'f': 00122 opt_file = optarg; 00123 break; 00124 case 'i': 00125 opt_ipv4 = optarg; 00126 break; 00127 case 's': 00128 opt_sender = optarg; 00129 break; 00130 case 'h': 00131 opt_helo = optarg; 00132 break; 00133 case 'r': 00134 opt_rcpt_to = optarg; 00135 break; 00136 case 'l': 00137 opt_local = optarg; 00138 break; 00139 case 't': 00140 opt_trusted = optarg; 00141 break; 00142 case 'g': 00143 opt_guess = optarg; 00144 break; 00145 case 'e': 00146 opt_exp = optarg; 00147 break; 00148 case 'm': 00149 opt_max_lookup = optarg; 00150 break; 00151 case 'c': /* "clean" */ 00152 opt_sanitize = optarg; 00153 break; 00154 case 'n': /* "name" */ 00155 opt_myhostname = optarg; 00156 break; 00157 case 'v': 00158 opt_debuglevel = optarg; 00159 dbg_level = atoi(opt_debuglevel); 00160 confg.level = dbg_level; 00161 fprintf(stderr, "DEBUGGING LEVEL IS: %i\n", dbg_level); 00162 break; 00163 default: 00164 fprintf(stderr, "Error: getopt returned character code 0%o ??\n", c); 00165 } 00166 } 00167 00168 if (optind != argc) 00169 { 00170 SPF_usage(); 00171 return 1; 00172 } 00173 00174 /* process the SPF request */ 00175 if (opt_ipv4 == NULL || opt_sender == NULL || opt_helo == NULL) 00176 { 00177 if (opt_file == NULL || opt_ipv4 != NULL || opt_sender != NULL || 00178 opt_helo != NULL) 00179 { 00180 SPF_usage(); 00181 return TRUE; 00182 } 00183 00184 /* from STDIN */ 00185 if (strcmp(opt_file, "-" ) == 0) 00186 { 00187 fin = stdin; 00188 } 00189 else 00190 { 00191 fin = fopen(opt_file, "r"); 00192 } 00193 00194 if (!fin) 00195 { 00196 fprintf(stderr, "Could not open: %s\n", opt_file); 00197 return 1; 00198 } 00199 00200 while ( TRUE ) 00201 { 00202 if (fgets(in_line, sizeof(in_line), stdin) == NULL) 00203 { 00204 break; 00205 } 00206 00207 p = strchr(in_line, '\n'); 00208 00209 if (p != NULL) 00210 { 00211 *p = '\0'; 00212 } 00213 00214 p = in_line; 00215 00216 while ((opt_ipv4 = strsep(&p, " \t\n")) != NULL && *opt_ipv4 == '\0') 00217 { 00218 ; 00219 } 00220 00221 if (opt_ipv4 == NULL) 00222 { 00223 break; 00224 } 00225 00226 while ((opt_sender = strsep(&p, "\t\n")) != NULL && *opt_ipv4 == '\0') 00227 { 00228 ; 00229 } 00230 00231 while ((opt_helo = strsep(&p, "\t\n")) != NULL && *opt_ipv4 == '\0') 00232 { 00233 ; 00234 } 00235 00236 if (dbg_level >= 1) 00237 { 00238 printf ("ipv4: %s\n", opt_ipv4); 00239 printf ("sender: %s\n", opt_sender); 00240 printf ("helo: %s\n", opt_helo); 00241 } 00242 00243 if (peer_info == NULL) 00244 { 00245 peer_info = SPF_init(opt_myhostname, 00246 opt_ipv4, NULL, 00247 opt_trusted ? opt_trusted : NULL, 00248 NULL, 00249 opt_trusted ? TRUE : FALSE, 00250 FALSE); 00251 } 00252 00253 if (opt_helo != NULL) 00254 { 00255 SPF_smtp_helo(peer_info, opt_helo); 00256 } 00257 00258 if (opt_sender != NULL) 00259 { 00260 SPF_smtp_from(peer_info, opt_sender); 00261 } 00262 00263 peer_info->RES = SPF_policy_main(peer_info); 00264 res = peer_info->RES; 00265 00266 printf( "%s\n%s\n%s\n", peer_info->rs, peer_info->error, 00267 peer_info->explain); 00268 00269 SPF_close(peer_info); 00270 } 00271 } 00272 else 00273 { 00274 if (opt_file != NULL) 00275 { 00276 SPF_usage(); 00277 return 1; 00278 } 00279 00280 /* from the command line */ 00281 if ( dbg_level ) 00282 { 00283 printf ("ipv4: %s\n", opt_ipv4); 00284 printf ("sender: %s\n", opt_sender); 00285 printf ("helo: %s\n", opt_helo); 00286 } 00287 00288 if (peer_info == NULL) 00289 { 00290 peer_info = SPF_init(opt_myhostname, 00291 opt_ipv4, NULL, 00292 opt_trusted ? opt_trusted : NULL, 00293 NULL, 00294 opt_trusted ? TRUE : FALSE, 00295 FALSE); 00296 } 00297 00298 if (opt_helo != NULL) 00299 { 00300 SPF_smtp_helo(peer_info, opt_helo); 00301 } 00302 00303 if (opt_sender != NULL) 00304 { 00305 SPF_smtp_from(peer_info, opt_sender); 00306 } 00307 00308 peer_info->RES = SPF_policy_main(peer_info); 00309 00310 printf( "%s\n%s\n%s\n", peer_info->rs, peer_info->error, 00311 peer_info->explain); 00312 00313 res = peer_info->RES; 00314 SPF_close(peer_info); 00315 00316 return res; 00317 } 00318 00319 return FALSE; 00320 } 00321 00322 00323 /* SPF_usage 00324 * 00325 * Author: Wayne Schlitt <wayne@midwestcs.com> 00326 * Author: James Couzens <jcouzens@6o4.ca> 00327 * 00328 * Date: 12/25/03 00329 * 00330 * Desc: 00331 * Displays usage help information when the binary is called with 00332 * no arguments. 00333 * 00334 */ 00335 void SPF_usage() 00336 { 00337 fprintf(stderr, "Usage:\n"); 00338 fprintf(stderr, "\n"); 00339 fprintf(stderr, "spfquery [-v] [-f <file>|spf data options]\n"); 00340 fprintf(stderr, "\n"); 00341 fprintf(stderr, "spfquery -i <IP Address> -s <email address> -h domain\n"); 00342 fprintf(stderr, "\n"); 00343 fprintf(stderr, "spfquery -i 10.0.0.2 -s jcouzens@6o4.ca -h spftools.net\n"); 00344 00345 return; 00346 } 00347 00348 /* end spfquery.c */

Generated on Thu Jul 1 14:05:44 2004 for libspf v1.0 by doxygen 1.3.7