Back to index

courier  0.68.2
testlookup.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 1998 - 2011 Double Precision, Inc.
00003 ** See COPYING for distribution information.
00004 */
00005 
00006 #include      "config.h"
00007 #include      "rfc1035.h"
00008 #include      "spf.h"
00009 #include      <sys/types.h>
00010 #include      <stdio.h>
00011 #include      <stdlib.h>
00012 #include      <string.h>
00013 #include      <errno.h>
00014 #include      <arpa/inet.h>
00015 
00016 #include      "soxwrap/soxwrap.h"
00017 
00018 
00019 static void setns(const char *p, struct rfc1035_res *res)
00020 {
00021 RFC1035_ADDR ia[4];
00022 int    i=0;
00023 char   *q=malloc(strlen(p)+1), *r;
00024 
00025        strcpy(q, p);
00026        for (r=q; (r=strtok(r, ", ")) != 0; r=0)
00027               if (i < 4)
00028               {
00029                      if (rfc1035_aton(r, &ia[i]) == 0)
00030                      {
00031                             ++i;
00032                      }
00033                      else
00034                      {
00035                             fprintf(stderr, "%s: invalid IP address\n",
00036                                    r);
00037                      }
00038               }
00039        rfc1035_init_ns(res, ia, i);
00040 }
00041 
00042 extern char rfc1035_spf_gettxt(const char *current_domain,
00043                             char *buf);
00044 
00045 static void spflookup(const char *current_domain)
00046 {
00047        char buf[256];
00048 
00049        switch (rfc1035_spf_gettxt(current_domain, buf)) {
00050        case SPF_NONE:
00051               printf("none\n");
00052               return;
00053        case SPF_NEUTRAL:
00054               printf("neutral\n");
00055               return;
00056        case SPF_PASS:
00057               printf("pass: %s\n", buf);
00058               return;
00059        case SPF_FAIL:
00060               printf("fail\n");
00061               return;
00062        case SPF_SOFTFAIL:
00063               printf("softfail\n");
00064               return;
00065        case SPF_ERROR:
00066               printf("error\n");
00067               return;
00068        default:
00069               printf("unknown\n");
00070        }
00071 }
00072 
00073 int main(int argc, char **argv)
00074 {
00075 struct  rfc1035_res res;
00076 struct rfc1035_reply *replyp;
00077 int    argn;
00078 const char *q_name;
00079 int    q_type;
00080 int    q_class;
00081 int    q_xflag=0;
00082 int    q_rflag=0;
00083 char   ptrbuf[RFC1035_MAXNAMESIZE+1];
00084 
00085        rfc1035_init_resolv(&res);
00086 
00087        argn=1;
00088        while (argn < argc)
00089        {
00090               if (argv[argn][0] == '@')
00091               {
00092                      setns(argv[argn]+1, &res);
00093                      ++argn;
00094                      continue;
00095               }
00096 
00097               if (strcmp(argv[argn], "-x") == 0)
00098               {
00099                      q_xflag=1;
00100                      ++argn;
00101                      continue;
00102               }
00103               if (strcmp(argv[argn], "-r") == 0)
00104               {
00105                      q_rflag=1;
00106                      ++argn;
00107                      continue;
00108               }
00109 
00110               if (strcmp(argv[argn], "-dnssec") == 0)
00111               {
00112                      rfc1035_init_dnssec_enable(&res, 1);
00113                      ++argn;
00114                      continue;
00115               }
00116 
00117               if (strcmp(argv[argn], "-udpsize") == 0)
00118               {
00119                      ++argn;
00120 
00121                      if (argn < argc)
00122                      {
00123                             rfc1035_init_edns_payload(&res,
00124                                                    atoi(argv[argn]));
00125                             ++argn;
00126                      }
00127                      continue;
00128               }
00129 
00130               break;
00131        }
00132 
00133        if (argn >= argc)    exit(0);
00134 
00135        q_name=argv[argn++];
00136 
00137        if (q_xflag)
00138        {
00139        struct in_addr ia;
00140 #if    RFC1035_IPV6
00141        struct in6_addr ia6;
00142 
00143               if (inet_pton(AF_INET6, q_name, &ia6) > 0)
00144               {
00145               const char *sin6=(const char *)&ia6;
00146               unsigned i;
00147 
00148                      ptrbuf[0]=0;
00149 
00150                      for (i=sizeof(struct in6_addr); i; )
00151                      {
00152                      char    buf[10];
00153 
00154                             --i;
00155                             sprintf(buf, "%x.%x.",
00156                                    (int)(unsigned char)(sin6[i] & 0x0F),
00157                                    (int)(unsigned char)((sin6[i] >> 4)
00158                                                  & 0x0F));
00159                             strcat(ptrbuf, buf);
00160                      }
00161                      strcat(ptrbuf, "ip6.arpa");
00162                      q_name=ptrbuf;
00163               }
00164               else
00165 #endif
00166               if ( rfc1035_aton_ipv4(q_name, &ia) == 0)
00167               {
00168               char buf[RFC1035_MAXNAMESIZE];
00169               unsigned char a=0,b=0,c=0,d=0;
00170               const char *p=buf;
00171 
00172                      rfc1035_ntoa_ipv4(&ia, buf);
00173 
00174                      while (*p >= '0' && *p <= '9')
00175                             a= (int)a * 10 + (*p++ - '0');
00176                      if (*p)       p++;
00177                      while (*p >= '0' && *p <= '9')
00178                             b= (int)b * 10 + (*p++ - '0');
00179                      if (*p)       p++;
00180                      while (*p >= '0' && *p <= '9')
00181                             c= (int)c * 10 + (*p++ - '0');
00182                      if (*p)       p++;
00183                      while (*p >= '0' && *p <= '9')
00184                             d= (int)d * 10 + (*p++ - '0');
00185 
00186                      sprintf(ptrbuf, "%d.%d.%d.%d.in-addr.arpa",
00187                             (int)d, (int)c, (int)b, (int)a);
00188                      q_name=ptrbuf;
00189               }
00190        }
00191 
00192        if (q_rflag)
00193        {
00194        RFC1035_ADDR a;
00195        int    rc;
00196 
00197               if (rfc1035_aton(q_name, &a) == 0)
00198               {
00199                      rc=rfc1035_ptr(&res, &a,ptrbuf);
00200                      if (rc == 0)
00201                      {
00202                             printf("%s\n", ptrbuf);
00203                             exit(0);
00204                      }
00205               }
00206               else
00207               {
00208               RFC1035_ADDR  *aptr;
00209               unsigned alen;
00210 
00211                      rc=rfc1035_a(&res, q_name, &aptr, &alen);
00212                      if (rc == 0)
00213                      {
00214                      unsigned i;
00215 
00216                             for (i=0; i<alen; i++)
00217                             {
00218                                    rfc1035_ntoa(&aptr[i], ptrbuf);
00219                                    printf("%s\n", ptrbuf);
00220                             }
00221                             exit(0);
00222                      }
00223               }
00224               fprintf(stderr, "%s error.\n", errno == ENOENT ? "Hard":"Soft");
00225               exit(1);
00226        }
00227        
00228        q_type= -1;
00229 
00230        if (argn < argc)
00231        {
00232               if (strcmp(argv[argn], "spf") == 0)
00233                      q_type= -2;
00234               else
00235                      q_type=rfc1035_type_strtoi(argv[argn++]);
00236        }
00237 
00238        if (q_type == -1)
00239               q_type=q_xflag ? RFC1035_TYPE_PTR:RFC1035_TYPE_ANY;
00240 
00241        q_class= -1;
00242        if (argn < argc)
00243               q_class=rfc1035_class_strtoi(argv[argn]);
00244        if (q_class < 0)
00245               q_class=RFC1035_CLASS_IN;
00246 
00247        if (q_type == -2)
00248        {
00249               spflookup(q_name);
00250               exit(0);
00251        }
00252 
00253        replyp=rfc1035_resolve(&res, RFC1035_OPCODE_QUERY,
00254                             q_name, q_type, q_class);
00255 
00256        if (!replyp)
00257        {
00258               perror(argv[0]);
00259               exit(1);
00260        }
00261 
00262        rfc1035_dump(replyp, stdout);
00263        rfc1035_replyfree(replyp);
00264        rfc1035_destroy_resolv(&res);
00265        return (0);
00266 }