| 1 |
/* libspf - Sender Policy Framework library |
| 2 |
* |
| 3 |
* ANSI C implementation of spf-draft-200405.txt |
| 4 |
* |
| 5 |
* Author: James Couzens <jcouzens@codeshare.ca> |
| 6 |
* |
| 7 |
* File: spfqtool.c |
| 8 |
* Desc: SPF Query Tool (an example implementation of libSPF) |
| 9 |
* |
| 10 |
* License: |
| 11 |
* |
| 12 |
* The libspf Software License, Version 1.0 |
| 13 |
* |
| 14 |
* Copyright (c) 2004 James Couzens & Sean Comeau All rights |
| 15 |
* reserved. |
| 16 |
* |
| 17 |
* Redistribution and use in source and binary forms, with or without |
| 18 |
* modification, are permitted provided that the following conditions |
| 19 |
* are met: |
| 20 |
* |
| 21 |
* 1. Redistributions of source code must retain the above copyright |
| 22 |
* notice, this list of conditions and the following disclaimer. |
| 23 |
* |
| 24 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 25 |
* notice, this list of conditions and the following disclaimer in |
| 26 |
* the documentation and/or other materials provided with the |
| 27 |
* distribution. |
| 28 |
* |
| 29 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
| 30 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 31 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 32 |
* DISCLAIMED. IN NO EVENT SHALL THE AUTHORS MAKING USE OF THIS LICESEN |
| 33 |
* OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 34 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 35 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| 36 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 37 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 38 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 39 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 40 |
* SUCH DAMAGE. |
| 41 |
* |
| 42 |
*/ |
| 43 |
|
| 44 |
#include "spfqtool.h" |
| 45 |
|
| 46 |
|
| 47 |
/* SPF_usage |
| 48 |
* |
| 49 |
* Author: James Couzens <jcouzens@6o4.ca> |
| 50 |
* |
| 51 |
* Date: 07/04/04 |
| 52 |
* Date: 07/28/04 - Renamed to spfqtool |
| 53 |
* Date: 09/08/04 - Added SPF_build_header call |
| 54 |
* |
| 55 |
* Desc: |
| 56 |
* Main function, allocates memory and makes calls to the libSPF |
| 57 |
* library which parses the "fake" query. |
| 58 |
* |
| 59 |
*/ |
| 60 |
int main(int argc, char *argv[]) |
| 61 |
{ |
| 62 |
u_int8_t i = 0; /* utility */ |
| 63 |
|
| 64 |
SPF_RESULT res = SPF_UNKNOWN; /* libSPF result code */ |
| 65 |
|
| 66 |
SPF_BOOL use_explain = SPF_FALSE; /* T / F provide SPF Explanation */ |
| 67 |
SPF_BOOL use_trusted = SPF_FALSE; /* T / F attempt Trusted Forwarder */ |
| 68 |
SPF_BOOL use_guess = SPF_FALSE; /* T / F attempt Best Guess */ |
| 69 |
SPF_BOOL test_mode = SPF_FALSE; /* T / F enable test mode */ |
| 70 |
|
| 71 |
char *margv = NULL; /* pointer to current argv element */ |
| 72 |
char *ip = NULL; /* ip address to test connecting from */ |
| 73 |
char *address = NULL; /* email address to test sending from */ |
| 74 |
char *helo = NULL; /* helo hostname to test sending from */ |
| 75 |
char *tmp = NULL; /* utility pointer */ |
| 76 |
char *buf = NULL; /* buffer to store generated SPF header */ |
| 77 |
|
| 78 |
peer_info_t *pinfo = NULL; /* libSPF peer_info structure */ |
| 79 |
|
| 80 |
if (argc <= 1) |
| 81 |
{ |
| 82 |
SPF_usage(); |
| 83 |
|
| 84 |
return(SPF_FALSE); |
| 85 |
} |
| 86 |
|
| 87 |
for (i = 1; i < argc; i++) |
| 88 |
{ |
| 89 |
tmp = argv[i]; |
| 90 |
|
| 91 |
if (*tmp == '-') |
| 92 |
{ |
| 93 |
margv = (tmp + 3); |
| 94 |
|
| 95 |
switch (*(tmp + 1)) |
| 96 |
{ |
| 97 |
/* best guess */ |
| 98 |
case 'b' : |
| 99 |
{ |
| 100 |
use_guess = atoi(margv); |
| 101 |
|
| 102 |
break; |
| 103 |
} /* 'b' */ |
| 104 |
|
| 105 |
/* debug */ |
| 106 |
case 'd' : |
| 107 |
{ |
| 108 |
confg.level = atoi(margv); |
| 109 |
|
| 110 |
break; |
| 111 |
} /* 'd' */ |
| 112 |
|
| 113 |
/* explanation */ |
| 114 |
case 'e' : |
| 115 |
{ |
| 116 |
use_explain = atoi(margv); |
| 117 |
|
| 118 |
break; |
| 119 |
} /* 'e' */ |
| 120 |
|
| 121 |
/* RFC2821 HELO */ |
| 122 |
case 'h' : |
| 123 |
{ |
| 124 |
helo = strdup(margv); |
| 125 |
|
| 126 |
break; |
| 127 |
} /* 'h' */ |
| 128 |
|
| 129 |
/* ip address */ |
| 130 |
case 'i' : |
| 131 |
{ |
| 132 |
ip = strdup(margv); |
| 133 |
|
| 134 |
break; |
| 135 |
} /* 'i' */ |
| 136 |
|
| 137 |
/* source email address */ |
| 138 |
case 's' : |
| 139 |
{ |
| 140 |
address = strdup(margv); |
| 141 |
|
| 142 |
break; |
| 143 |
} /* 's' */ |
| 144 |
|
| 145 |
/* trusted forwarder */ |
| 146 |
case 't' : |
| 147 |
{ |
| 148 |
use_trusted = atoi(margv); |
| 149 |
|
| 150 |
break; |
| 151 |
} /* 't' */ |
| 152 |
|
| 153 |
/* version */ |
| 154 |
case 'v' : |
| 155 |
{ |
| 156 |
printf("SPF Query Tool v%s - James Couzens <jcouzens@codeshare.ca>\n\n", |
| 157 |
SPFQTOOL_VERSION); |
| 158 |
|
| 159 |
return(0); |
| 160 |
} /* 'v' */ |
| 161 |
|
| 162 |
/* test mode */ |
| 163 |
case 'z' : |
| 164 |
{ |
| 165 |
test_mode = SPF_TRUE; |
| 166 |
|
| 167 |
break; |
| 168 |
} /* 'z' */ |
| 169 |
|
| 170 |
default: |
| 171 |
{ |
| 172 |
break; |
| 173 |
} /* default */ |
| 174 |
} |
| 175 |
} |
| 176 |
} /* for */ |
| 177 |
|
| 178 |
if (ip == NULL) |
| 179 |
{ |
| 180 |
printf("You need to specify an IP Address to test against\n\n"); |
| 181 |
|
| 182 |
SPF_usage(); |
| 183 |
|
| 184 |
free(address); |
| 185 |
|
| 186 |
return(SPF_FALSE); |
| 187 |
} |
| 188 |
else if (address == NULL) |
| 189 |
{ |
| 190 |
printf("You need to specify a from email address\n\n"); |
| 191 |
|
| 192 |
SPF_usage(); |
| 193 |
|
| 194 |
free(ip); |
| 195 |
|
| 196 |
return(SPF_FALSE); |
| 197 |
} |
| 198 |
else if (helo == NULL) |
| 199 |
{ |
| 200 |
helo = strdup(HELO_HOST); |
| 201 |
|
| 202 |
printf("You didn't give me a helo host, using (%s)\n", helo); |
| 203 |
} |
| 204 |
|
| 205 |
if (confg.level >= 1) |
| 206 |
{ |
| 207 |
printf("SPF Query Tool v%s - James Couzens <jcouzens@codeshare.ca>\n", |
| 208 |
SPFQTOOL_VERSION); |
| 209 |
|
| 210 |
printf("[DEBUG]: Debugging level: %u\n", confg.level); |
| 211 |
printf("[DEBUG]: RFC2821 Mail From: %s\n", address); |
| 212 |
printf("[DEBUG]: RFC2821 HELO: %s\n", helo); |
| 213 |
printf("[DEBUG]: Purported address: %s\n", ip); |
| 214 |
|
| 215 |
printf("[DEBUG]: SPF Explanation: %s\n", |
| 216 |
use_explain ? "Enabled" : "Disabled"); |
| 217 |
|
| 218 |
printf("[DEBUG]: Trusted Forwarder: %s\n", |
| 219 |
use_trusted ? "Enabled" : "Disabled"); |
| 220 |
|
| 221 |
printf("[DEBUG]: Best Guess: %s\n", |
| 222 |
use_guess ? "Enabled" : "Disabled"); |
| 223 |
|
| 224 |
printf("\n"); |
| 225 |
} |
| 226 |
|
| 227 |
if ((pinfo = SPF_init(helo, ip, NULL, NULL, NULL, |
| 228 |
use_trusted, use_guess)) != NULL) |
| 229 |
{ |
| 230 |
/* perform fake HELO */ |
| 231 |
SPF_smtp_helo(pinfo, helo); |
| 232 |
|
| 233 |
/* perform fake MAIL FROM */ |
| 234 |
SPF_smtp_from(pinfo, address); |
| 235 |
|
| 236 |
/* assign and perform SPF parse */ |
| 237 |
pinfo->RES = SPF_policy_main(pinfo); |
| 238 |
res = pinfo->RES; |
| 239 |
|
| 240 |
free(address); |
| 241 |
free(helo); |
| 242 |
free(ip); |
| 243 |
|
| 244 |
/* print the results of the query. The NULL check on the output is |
| 245 |
* to the benefit of SOLARIS users where printf is unable to handle |
| 246 |
* a NULL variable. Linux users can omit this check. |
| 247 |
*/ |
| 248 |
|
| 249 |
if (test_mode != SPF_TRUE) |
| 250 |
{ |
| 251 |
printf("SPF short result: %s\n", |
| 252 |
pinfo->rs ? pinfo->rs : "NULL"); |
| 253 |
|
| 254 |
printf("SPF verbose result: %s\n", |
| 255 |
pinfo->error ? pinfo->error : "NULL"); |
| 256 |
} |
| 257 |
else |
| 258 |
{ |
| 259 |
printf("%s\n%s\n%s\n", |
| 260 |
pinfo->rs ? pinfo->rs : "NULL", |
| 261 |
pinfo->error ? pinfo->error : "NULL", |
| 262 |
pinfo->explain ? pinfo->explain : "NULL"); |
| 263 |
} |
| 264 |
|
| 265 |
if (use_explain == SPF_TRUE) |
| 266 |
{ |
| 267 |
buf = SPF_get_explain(pinfo); |
| 268 |
|
| 269 |
printf("SPF explanation: %s\n", |
| 270 |
pinfo->explain ? pinfo->explain : "NULL"); |
| 271 |
|
| 272 |
free(buf); |
| 273 |
} |
| 274 |
|
| 275 |
/* for the tests we need to be silent :-) */ |
| 276 |
if (test_mode != SPF_TRUE) |
| 277 |
{ |
| 278 |
if (use_trusted == SPF_TRUE) |
| 279 |
{ |
| 280 |
printf("Trusted Forwarder: Attempted.\n"); |
| 281 |
} |
| 282 |
|
| 283 |
if (use_guess == SPF_TRUE) |
| 284 |
{ |
| 285 |
printf("Best Guess: Attempted.\n"); |
| 286 |
} |
| 287 |
|
| 288 |
if ((buf = SPF_build_header(pinfo)) != NULL) |
| 289 |
{ |
| 290 |
printf("RFC2822 header: %s\n", buf); |
| 291 |
free(buf); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
/* close SPF session (free memory associated with parse) */ |
| 296 |
SPF_close(pinfo); |
| 297 |
} |
| 298 |
|
| 299 |
/* |
| 300 |
free(buf); |
| 301 |
free(pinfo); |
| 302 |
*/ |
| 303 |
return(SPF_FALSE); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
/* SPF_usage |
| 308 |
* |
| 309 |
* Author: James Couzens <jcouzens@6o4.ca> |
| 310 |
* |
| 311 |
* Date: 12/25/03 |
| 312 |
* Date: 07/28/04 - Renamed to spfqtool |
| 313 |
* |
| 314 |
* Desc: |
| 315 |
* Displays usage help information when the binary is called with |
| 316 |
* no arguments. |
| 317 |
* |
| 318 |
*/ |
| 319 |
void SPF_usage() |
| 320 |
{ |
| 321 |
printf("spfqtool usage:\n"); |
| 322 |
printf("\n"); |
| 323 |
printf("spfqtool [b|d|e|i|s|t|h|v]\n"); |
| 324 |
printf("\n"); |
| 325 |
printf("-b [0,1] - Enable Best Guess support (True (0) or False (1))\n"); |
| 326 |
printf("-d [x] - DEBUG where x is a number between 1 and 255\n"); |
| 327 |
printf("-e [0,1] - Enable SPF explanation (True (0) or False (1))\n"); |
| 328 |
printf("-h [host] - HELO hostname to test with\n"); |
| 329 |
printf("-i [addr] - IP Address where the fake connection will come from\n"); |
| 330 |
printf("-s [email] - What email address to test with\n"); |
| 331 |
printf("-t [0,1] - Enable Trusted Forwarder support (True (0) or False (1))\n"); |
| 332 |
printf("-v - Display version string\n"); |
| 333 |
printf("\n"); |
| 334 |
printf("Example: ./spfqtool -i 10.0.0.2 -s jcouzens@6o4.ca -h spftools.net\n"); |
| 335 |
printf("\n"); |
| 336 |
printf("Minimum required arguments are 'i', and 's'\n"); |
| 337 |
printf("\n"); |
| 338 |
|
| 339 |
return; |
| 340 |
} |
| 341 |
|
| 342 |
/* end spfqtool.c */ |