libspf v1.0: spfquery.c Source File

Main Page | Data Structures | File List | Data Fields | Globals | Search for

# spfquery.c

Go to the documentation of this file.

    00001 /* libspf - Sender Policy Framework library
    00002 *
    00003 *  ANSI C implementation of spf-draft-200405.txt
    00004 *
    00005 *  Author: James Couzens <jcouzens@codeshare.ca>
    00006 *
    00007 *  File:   spfquery.c
    00008 *  Desc:   SPF Query Tool (an example implementation of libSPF)
    00009 *
    00010 *  License:
    00011 *
    00012 *  The libspf Software License, Version 1.0
    00013 *
    00014 *  Copyright (c) 2004 James Couzens & Sean Comeau  All rights
    00015 *  reserved.
    00016 *
    00017 *  Redistribution and use in source and binary forms, with or without
    00018 *  modification, are permitted provided that the following conditions
    00019 *  are met:
    00020 *
    00021 *  1. Redistributions of source code must retain the above copyright
    00022 *     notice, this list of conditions and the following disclaimer.
    00023 *
    00024 *  2. Redistributions in binary form must reproduce the above copyright
    00025 *     notice, this list of conditions and the following disclaimer in
    00026 *     the documentation and/or other materials provided with the
    00027 *     distribution.
    00028 *
    00029 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
    00030 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    00031 *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    00032 *  DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS MAKING USE OF THIS LICESEN
    00033 *  OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    00034 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    00035 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
    00036 *  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    00037 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    00038 *  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
    00039 *  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    00040 *  SUCH DAMAGE.
    00041 *
    00042 */
    00043
    00044 #include "spfquery.h"
    00045
    00046
    00047 /* SPF_usage
    00048 *
    00049 *  Author: James Couzens <jcouzens@6o4.ca>
    00050 *
    00051 *  Date:   07/04/04
    00052 *
    00053 *  Desc:
    00054 *         Main function, allocates memory and makes calls to the libSPF
    00055 *  library which parses the "fake" query.
    00056 *
    00057 */
    00058 int main(int argc, char *argv[])
    00059 {
    00060   u_int8_t i         = 0;       /* utility */
    00061   u_int8_t res       = 0;       /* libSPF result code */
    00062
    00063   char     *margv    = NULL;    /* pointer to current argv element */
    00064   char     *ip       = NULL;    /* ip address to test connecting from */
    00065   char     *address  = NULL;    /* email address to test sending from */
    00066   char     *helo     = NULL;    /* helo hostname to test sending from */
    00067
    00068   char     *tmp      = NULL;    /* utility pointer */
    00069
    00070   peer_info_t *pinfo = NULL;    /* libSPF peer_info structure */
    00071
    00072   if (argc <= 1)
    00073   {
    00074     SPF_usage();
    00075     return(FALSE);
    00076   }
    00077
    00078   for (i = 1; i < argc; i++)
    00079   {
    00080     tmp = argv[i];
    00081
    00082     if (*tmp == '-')
    00083     {
    00084       margv = (tmp + 3);
    00085
    00086       switch (*(tmp + 1))
    00087       {
    00088         case 'd' :
    00089           confg.level = atoi(margv);
    00090           break;
    00091         case 'i' :
    00092           ip  = strdup(margv);
    00093           break;
    00094         case 's' :
    00095           address = strdup(margv);
    00096           break;
    00097         case 'h' :
    00098           helo  = strdup(margv);
    00099           break;
    00100       }
    00101     }
    00102   }
    00103
    00104   if (ip == NULL)
    00105   {
    00106     printf("You need to specify an IP Address to test against\n\n");
    00107     SPF_usage();
    00108     return(FALSE);
    00109   }
    00110   else if (address == NULL)
    00111   {
    00112     printf("You need to specify a from email address\n\n");
    00113     SPF_usage();
    00114     return(FALSE);
    00115   }
    00116   else if (helo == NULL)
    00117   {
    00118     helo = strdup(HELO_HOST);
    00119     printf("You didn't give me a helo host, using (%s)\n", helo);
    00120     return(FALSE);
    00121   }
    00122
    00123   if (confg.level >= 1)
    00124   {
    00125     printf("SPF Query v%s - James Couzens <jcouzens@codeshare.ca>\n\n",
    00126       SPFQUERY_VERSION);
    00127
    00128     printf("DEBUG: %u\n", confg.level);
    00129     printf("IP address: %s\n", ip);
    00130     printf("MAIL FROM: %s\n", address);
    00131     printf("HELO: %s\n", helo);
    00132   }
    00133
    00134   if ((pinfo = SPF_init(helo, ip, NULL, NULL, NULL, FALSE, FALSE)) != NULL)
    00135   {
    00136     /* perform fake HELO */
    00137     SPF_smtp_helo(pinfo, helo);
    00138
    00139     /* perform fake MAIL FROM */
    00140     SPF_smtp_from(pinfo, address);
    00141
    00142     /* perform SPF parse */
    00143     pinfo->RES = SPF_policy_main(pinfo);
    00144
    00145     /* assign result of SPF parse */
    00146     res = pinfo->RES;
    00147
    00148     /* print the results */
    00149     printf( "%s\n%s\n%s\n",
    00150       pinfo->rs ? pinfo->rs : "NULL",
    00151       pinfo->error ? pinfo->error : "NULL",
    00152       pinfo->explain ? pinfo->explain : "NULL");
    00153
    00154     /* close SPF session (free memory associated with parse) */
    00155     SPF_close(pinfo);
    00156
    00157   }
    00158
    00159   free(ip);
    00160   free(address);
    00161   free(helo);
    00162
    00163   return(FALSE);
    00164 }
    00165
    00166
    00167 /* SPF_usage
    00168 *
    00169 *  Author: James Couzens <jcouzens@6o4.ca>
    00170 *
    00171 *  Date:   12/25/03
    00172 *
    00173 *  Desc:
    00174 *         Displays usage help information when the binary is called with
    00175 *  no arguments.
    00176 *
    00177 */
    00178 void SPF_usage()
    00179 {
    00180   printf("Usage:\n");
    00181   printf("\n");
    00182   printf("spfquery [dish]\n");
    00183   printf("\n");
    00184   printf("-d [x]     - DEBUG where x is a number between 1 and 255\n");
    00185   printf("-i [addr]  - IP Address where the fake connection will come from\n");
    00186   printf("-s [email] - What email address to test with\n");
    00187   printf("-h [host]  - HELO hostname to test with\n");
    00188   printf("\n");
    00189   printf("spfquery -i 10.0.0.2 -s jcouzens@6o4.ca -h spftools.net\n");
    00190
    00191   return;
    00192 }
    00193
    00194 /* end spfquery.c */