1 /*	$NetBSD: resolve-test.c,v 1.1.1.1 2011/04/13 18:15:42 elric Exp $	*/
2 
3 /*
4  * Copyright (c) 1995 - 2004 Kungliga Tekniska Högskolan
5  * (Royal Institute of Technology, Stockholm, Sweden).
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the Institute nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 
37 #include <config.h>
38 
39 #include <krb5/roken.h>
40 #include <krb5/getarg.h>
41 #ifdef HAVE_ARPA_NAMESER_H
42 #include <arpa/nameser.h>
43 #endif
44 #ifdef HAVE_RESOLV_H
45 #include <resolv.h>
46 #endif
47 #include <krb5/resolve.h>
48 
49 static int version_flag = 0;
50 static int help_flag	= 0;
51 
52 static struct getargs args[] = {
53     {"version",	0,	arg_flag,	&version_flag,
54      "print version", NULL },
55     {"help",	0,	arg_flag,	&help_flag,
56      NULL, NULL }
57 };
58 
59 static void
60 usage (int ret)
61 {
62     arg_printusage (args,
63 		    sizeof(args)/sizeof(*args),
64 		    NULL,
65 		    "dns-record resource-record-type");
66     exit (ret);
67 }
68 
69 int
70 main(int argc, char **argv)
71 {
72     struct rk_dns_reply *r;
73     struct rk_resource_record *rr;
74     int optidx = 0;
75 
76     setprogname (argv[0]);
77 
78     if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
79 	usage(1);
80 
81     if (help_flag)
82 	usage (0);
83 
84     if(version_flag){
85 	printf("some version\n");
86 	exit(0);
87     }
88 
89     argc -= optidx;
90     argv += optidx;
91 
92     if (argc != 2)
93 	usage(1);
94 
95     r = rk_dns_lookup(argv[0], argv[1]);
96     if(r == NULL){
97 	printf("No reply.\n");
98 	return 1;
99     }
100     if(r->q.type == rk_ns_t_srv)
101 	rk_dns_srv_order(r);
102 
103     for(rr = r->head; rr;rr=rr->next){
104 	printf("%-30s %-5s %-6d ", rr->domain, rk_dns_type_to_string(rr->type), rr->ttl);
105 	switch(rr->type){
106 	case rk_ns_t_ns:
107 	case rk_ns_t_cname:
108 	case rk_ns_t_ptr:
109 	    printf("%s\n", (char*)rr->u.data);
110 	    break;
111 	case rk_ns_t_a:
112 	    printf("%s\n", inet_ntoa(*rr->u.a));
113 	    break;
114 	case rk_ns_t_mx:
115 	case rk_ns_t_afsdb:{
116 	    printf("%d %s\n", rr->u.mx->preference, rr->u.mx->domain);
117 	    break;
118 	}
119 	case rk_ns_t_srv:{
120 	    struct rk_srv_record *srv = rr->u.srv;
121 	    printf("%d %d %d %s\n", srv->priority, srv->weight,
122 		   srv->port, srv->target);
123 	    break;
124 	}
125 	case rk_ns_t_txt: {
126 	    printf("%s\n", rr->u.txt);
127 	    break;
128 	}
129 	case rk_ns_t_sig : {
130 	    struct rk_sig_record *sig = rr->u.sig;
131 	    const char *type_string = rk_dns_type_to_string (sig->type);
132 
133 	    printf ("type %u (%s), algorithm %u, labels %u, orig_ttl %u, sig_expiration %u, sig_inception %u, key_tag %u, signer %s\n",
134 		    sig->type, type_string ? type_string : "",
135 		    sig->algorithm, sig->labels, sig->orig_ttl,
136 		    sig->sig_expiration, sig->sig_inception, sig->key_tag,
137 		    sig->signer);
138 	    break;
139 	}
140 	case rk_ns_t_key : {
141 	    struct rk_key_record *key = rr->u.key;
142 
143 	    printf ("flags %u, protocol %u, algorithm %u\n",
144 		    key->flags, key->protocol, key->algorithm);
145 	    break;
146 	}
147 	case rk_ns_t_sshfp : {
148 	    struct rk_sshfp_record *sshfp = rr->u.sshfp;
149 	    size_t i;
150 
151 	    printf ("alg %u type %u length %lu data ", sshfp->algorithm,
152 		    sshfp->type,  (unsigned long)sshfp->sshfp_len);
153 	    for (i = 0; i < sshfp->sshfp_len; i++)
154 		printf("%02X", sshfp->sshfp_data[i]);
155 	    printf("\n");
156 
157 	    break;
158 	}
159 	case rk_ns_t_ds : {
160 	    struct rk_ds_record *ds = rr->u.ds;
161 	    size_t i;
162 
163 	    printf ("key tag %u alg %u type %u length %lu data ",
164 		    ds->key_tag, ds->algorithm, ds->digest_type,
165 		    (unsigned long)ds->digest_len);
166 	    for (i = 0; i < ds->digest_len; i++)
167 		printf("%02X", ds->digest_data[i]);
168 	    printf("\n");
169 
170 	    break;
171 	}
172 	default:
173 	    printf("\n");
174 	    break;
175 	}
176     }
177 
178     return 0;
179 }
180