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