1 /*	$NetBSD: resolve-test.c,v 1.1.1.2 2014/04/24 12:45:52 pettai 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 loop_integer = 1;
50 static int version_flag = 0;
51 static int help_flag	= 0;
52 
53 static struct getargs args[] = {
54     {"loop",	0,	arg_integer,	&loop_integer,
55      "loop resolving", NULL },
56     {"version",	0,	arg_flag,	&version_flag,
57      "print version", NULL },
58     {"help",	0,	arg_flag,	&help_flag,
59      NULL, NULL }
60 };
61 
62 static void
usage(int ret)63 usage (int ret)
64 {
65     arg_printusage (args,
66 		    sizeof(args)/sizeof(*args),
67 		    NULL,
68 		    "dns-record resource-record-type");
69     exit (ret);
70 }
71 
72 int
main(int argc,char ** argv)73 main(int argc, char **argv)
74 {
75     struct rk_dns_reply *r;
76     struct rk_resource_record *rr;
77     int optidx = 0, i, exit_code = 0;
78 
79     setprogname (argv[0]);
80 
81     if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
82 	usage(1);
83 
84     if (help_flag)
85 	usage (0);
86 
87     if(version_flag){
88 	printf("some version\n");
89 	exit(0);
90     }
91 
92     argc -= optidx;
93     argv += optidx;
94 
95     if (argc != 2)
96 	usage(1);
97 
98     for (i = 0; i < loop_integer; i++) {
99 
100 	r = rk_dns_lookup(argv[0], argv[1]);
101 	if(r == NULL){
102 	    printf("No reply.\n");
103 	    exit_code = 1;
104 	    break;
105 	}
106 	if(r->q.type == rk_ns_t_srv)
107 	    rk_dns_srv_order(r);
108 
109 	for(rr = r->head; rr;rr=rr->next){
110 	    printf("%-30s %-5s %-6d ", rr->domain, rk_dns_type_to_string(rr->type), rr->ttl);
111 	    switch(rr->type){
112 	    case rk_ns_t_ns:
113 	    case rk_ns_t_cname:
114 	    case rk_ns_t_ptr:
115 		printf("%s\n", (char*)rr->u.data);
116 		break;
117 	    case rk_ns_t_a:
118 		printf("%s\n", inet_ntoa(*rr->u.a));
119 		break;
120 	    case rk_ns_t_mx:
121 	    case rk_ns_t_afsdb:{
122 		printf("%d %s\n", rr->u.mx->preference, rr->u.mx->domain);
123 		break;
124 	    }
125 	    case rk_ns_t_srv:{
126 		struct rk_srv_record *srv = rr->u.srv;
127 		printf("%d %d %d %s\n", srv->priority, srv->weight,
128 		       srv->port, srv->target);
129 		break;
130 	    }
131 	    case rk_ns_t_txt: {
132 		printf("%s\n", rr->u.txt);
133 		break;
134 	    }
135 	    case rk_ns_t_sig : {
136 		struct rk_sig_record *sig = rr->u.sig;
137 		const char *type_string = rk_dns_type_to_string (sig->type);
138 
139 		printf ("type %u (%s), algorithm %u, labels %u, orig_ttl %u, sig_expiration %u, sig_inception %u, key_tag %u, signer %s\n",
140 			sig->type, type_string ? type_string : "",
141 			sig->algorithm, sig->labels, sig->orig_ttl,
142 			sig->sig_expiration, sig->sig_inception, sig->key_tag,
143 			sig->signer);
144 		break;
145 	    }
146 	    case rk_ns_t_key : {
147 		struct rk_key_record *key = rr->u.key;
148 
149 		printf ("flags %u, protocol %u, algorithm %u\n",
150 			key->flags, key->protocol, key->algorithm);
151 		break;
152 	    }
153 	    case rk_ns_t_sshfp : {
154 		struct rk_sshfp_record *sshfp = rr->u.sshfp;
155 		size_t i;
156 
157 		printf ("alg %u type %u length %lu data ", sshfp->algorithm,
158 			sshfp->type,  (unsigned long)sshfp->sshfp_len);
159 		for (i = 0; i < sshfp->sshfp_len; i++)
160 		    printf("%02X", sshfp->sshfp_data[i]);
161 		printf("\n");
162 
163 		break;
164 	    }
165 	    case rk_ns_t_ds : {
166 		struct rk_ds_record *ds = rr->u.ds;
167 		size_t i;
168 
169 		printf ("key tag %u alg %u type %u length %lu data ",
170 			ds->key_tag, ds->algorithm, ds->digest_type,
171 			(unsigned long)ds->digest_len);
172 		for (i = 0; i < ds->digest_len; i++)
173 		    printf("%02X", ds->digest_data[i]);
174 		printf("\n");
175 
176 		break;
177 	    }
178 	    default:
179 		printf("\n");
180 		break;
181 	    }
182 	}
183 	rk_dns_free_data(r);
184     }
185 
186     return exit_code;
187 }
188