xref: /minix3/crypto/external/bsd/heimdal/dist/lib/wind/idn-lookup.c (revision ebfedea0ce5bbe81e252ddf32d732e40fb633fae)
1*ebfedea0SLionel Sambuc /*	$NetBSD: idn-lookup.c,v 1.1.1.1 2011/04/13 18:15:58 elric Exp $	*/
2*ebfedea0SLionel Sambuc 
3*ebfedea0SLionel Sambuc /*
4*ebfedea0SLionel Sambuc  * Copyright (c) 2004 Kungliga Tekniska Högskolan
5*ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6*ebfedea0SLionel Sambuc  * All rights reserved.
7*ebfedea0SLionel Sambuc  *
8*ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9*ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10*ebfedea0SLionel Sambuc  * are met:
11*ebfedea0SLionel Sambuc  *
12*ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14*ebfedea0SLionel Sambuc  *
15*ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17*ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18*ebfedea0SLionel Sambuc  *
19*ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
20*ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21*ebfedea0SLionel Sambuc  *    without specific prior written permission.
22*ebfedea0SLionel Sambuc  *
23*ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24*ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27*ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28*ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29*ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30*ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31*ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*ebfedea0SLionel Sambuc  * SUCH DAMAGE.
34*ebfedea0SLionel Sambuc  */
35*ebfedea0SLionel Sambuc 
36*ebfedea0SLionel Sambuc #ifdef HAVE_CONFIG_H
37*ebfedea0SLionel Sambuc #include <config.h>
38*ebfedea0SLionel Sambuc #endif
39*ebfedea0SLionel Sambuc #include <assert.h>
40*ebfedea0SLionel Sambuc #include <err.h>
41*ebfedea0SLionel Sambuc #include <netdb.h>
42*ebfedea0SLionel Sambuc #include <stdio.h>
43*ebfedea0SLionel Sambuc #include <stdlib.h>
44*ebfedea0SLionel Sambuc #include <string.h>
45*ebfedea0SLionel Sambuc #include <sys/socket.h>
46*ebfedea0SLionel Sambuc #include <sys/types.h>
47*ebfedea0SLionel Sambuc 
48*ebfedea0SLionel Sambuc #include <krb5/getarg.h>
49*ebfedea0SLionel Sambuc #include <krb5/roken.h>
50*ebfedea0SLionel Sambuc 
51*ebfedea0SLionel Sambuc #include "windlocl.h"
52*ebfedea0SLionel Sambuc 
53*ebfedea0SLionel Sambuc static int version_flag = 0;
54*ebfedea0SLionel Sambuc static int help_flag	= 0;
55*ebfedea0SLionel Sambuc 
56*ebfedea0SLionel Sambuc 
57*ebfedea0SLionel Sambuc static int
is_separator(uint32_t u)58*ebfedea0SLionel Sambuc is_separator(uint32_t u)
59*ebfedea0SLionel Sambuc {
60*ebfedea0SLionel Sambuc     return u == 0x002E || u == 0x3002;
61*ebfedea0SLionel Sambuc }
62*ebfedea0SLionel Sambuc 
63*ebfedea0SLionel Sambuc static void
lookup(const char * name)64*ebfedea0SLionel Sambuc lookup(const char *name)
65*ebfedea0SLionel Sambuc {
66*ebfedea0SLionel Sambuc     unsigned i;
67*ebfedea0SLionel Sambuc     char encoded[1024];
68*ebfedea0SLionel Sambuc     char *ep;
69*ebfedea0SLionel Sambuc     int ret;
70*ebfedea0SLionel Sambuc     struct addrinfo hints;
71*ebfedea0SLionel Sambuc     struct addrinfo *ai;
72*ebfedea0SLionel Sambuc 
73*ebfedea0SLionel Sambuc     size_t u_len = strlen(name);
74*ebfedea0SLionel Sambuc     uint32_t *u = malloc(u_len * sizeof(uint32_t));
75*ebfedea0SLionel Sambuc     size_t norm_len = u_len * 2;
76*ebfedea0SLionel Sambuc     uint32_t *norm = malloc(norm_len * sizeof(uint32_t));
77*ebfedea0SLionel Sambuc 
78*ebfedea0SLionel Sambuc     if (u == NULL && u_len != 0)
79*ebfedea0SLionel Sambuc 	errx(1, "malloc failed");
80*ebfedea0SLionel Sambuc     if (norm == NULL && norm_len != 0)
81*ebfedea0SLionel Sambuc 	errx(1, "malloc failed");
82*ebfedea0SLionel Sambuc 
83*ebfedea0SLionel Sambuc     ret = wind_utf8ucs4(name, u, &u_len);
84*ebfedea0SLionel Sambuc     if (ret)
85*ebfedea0SLionel Sambuc 	errx(1, "utf8 conversion failed");
86*ebfedea0SLionel Sambuc     ret = wind_stringprep(u, u_len, norm, &norm_len, WIND_PROFILE_NAME);
87*ebfedea0SLionel Sambuc     if (ret)
88*ebfedea0SLionel Sambuc 	errx(1, "stringprep failed");
89*ebfedea0SLionel Sambuc     free(u);
90*ebfedea0SLionel Sambuc 
91*ebfedea0SLionel Sambuc     ep = encoded;
92*ebfedea0SLionel Sambuc     for (i = 0; i < norm_len; ++i) {
93*ebfedea0SLionel Sambuc 	unsigned j;
94*ebfedea0SLionel Sambuc 	size_t len;
95*ebfedea0SLionel Sambuc 
96*ebfedea0SLionel Sambuc 	for (j = i; j < norm_len && !is_separator(norm[j]); ++j)
97*ebfedea0SLionel Sambuc 	    ;
98*ebfedea0SLionel Sambuc 	len = sizeof(encoded) - (ep - encoded);
99*ebfedea0SLionel Sambuc 	ret = wind_punycode_label_toascii(norm + i, j - i, ep, &len);
100*ebfedea0SLionel Sambuc 	if (ret)
101*ebfedea0SLionel Sambuc 	    errx(1, "punycode failed");
102*ebfedea0SLionel Sambuc 
103*ebfedea0SLionel Sambuc 	ep += len;
104*ebfedea0SLionel Sambuc 	*ep++ = '.';
105*ebfedea0SLionel Sambuc 	i = j;
106*ebfedea0SLionel Sambuc     }
107*ebfedea0SLionel Sambuc     *ep = '\0';
108*ebfedea0SLionel Sambuc     free(norm);
109*ebfedea0SLionel Sambuc 
110*ebfedea0SLionel Sambuc     printf("Converted \"%s\" into \"%s\"\n", name, encoded);
111*ebfedea0SLionel Sambuc 
112*ebfedea0SLionel Sambuc     memset(&hints, 0, sizeof(hints));
113*ebfedea0SLionel Sambuc     hints.ai_flags = AI_CANONNAME;
114*ebfedea0SLionel Sambuc     ret = getaddrinfo(encoded, NULL, &hints, &ai);
115*ebfedea0SLionel Sambuc     if(ret)
116*ebfedea0SLionel Sambuc 	errx(1, "getaddrinfo failed: %s", gai_strerror(ret));
117*ebfedea0SLionel Sambuc     printf("canonical-name: %s\n", ai->ai_canonname);
118*ebfedea0SLionel Sambuc     freeaddrinfo(ai);
119*ebfedea0SLionel Sambuc }
120*ebfedea0SLionel Sambuc 
121*ebfedea0SLionel Sambuc static struct getargs args[] = {
122*ebfedea0SLionel Sambuc     {"version",	0,	arg_flag,	&version_flag,
123*ebfedea0SLionel Sambuc      "print version", NULL },
124*ebfedea0SLionel Sambuc     {"help",	0,	arg_flag,	&help_flag,
125*ebfedea0SLionel Sambuc      NULL, NULL }
126*ebfedea0SLionel Sambuc };
127*ebfedea0SLionel Sambuc 
128*ebfedea0SLionel Sambuc static void
usage(int ret)129*ebfedea0SLionel Sambuc usage (int ret)
130*ebfedea0SLionel Sambuc {
131*ebfedea0SLionel Sambuc     arg_printusage(args, sizeof(args)/sizeof(args[0]), NULL,
132*ebfedea0SLionel Sambuc 		   "dns-names ...");
133*ebfedea0SLionel Sambuc     exit (ret);
134*ebfedea0SLionel Sambuc }
135*ebfedea0SLionel Sambuc 
136*ebfedea0SLionel Sambuc int
main(int argc,char ** argv)137*ebfedea0SLionel Sambuc main(int argc, char **argv)
138*ebfedea0SLionel Sambuc {
139*ebfedea0SLionel Sambuc     int optidx = 0;
140*ebfedea0SLionel Sambuc     unsigned i;
141*ebfedea0SLionel Sambuc 
142*ebfedea0SLionel Sambuc     setprogname (argv[0]);
143*ebfedea0SLionel Sambuc 
144*ebfedea0SLionel Sambuc     if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
145*ebfedea0SLionel Sambuc 	usage(1);
146*ebfedea0SLionel Sambuc 
147*ebfedea0SLionel Sambuc     if (help_flag)
148*ebfedea0SLionel Sambuc 	usage (0);
149*ebfedea0SLionel Sambuc 
150*ebfedea0SLionel Sambuc     if(version_flag){
151*ebfedea0SLionel Sambuc 	print_version(NULL);
152*ebfedea0SLionel Sambuc 	exit(0);
153*ebfedea0SLionel Sambuc     }
154*ebfedea0SLionel Sambuc 
155*ebfedea0SLionel Sambuc     argc -= optidx;
156*ebfedea0SLionel Sambuc     argv += optidx;
157*ebfedea0SLionel Sambuc 
158*ebfedea0SLionel Sambuc     if (argc == 0)
159*ebfedea0SLionel Sambuc 	usage(1);
160*ebfedea0SLionel Sambuc 
161*ebfedea0SLionel Sambuc     for (i = 0; i < argc; ++i)
162*ebfedea0SLionel Sambuc 	lookup(argv[i]);
163*ebfedea0SLionel Sambuc     return 0;
164*ebfedea0SLionel Sambuc }
165