1*1c9681d1Schristos /*	$NetBSD: test_x500.c,v 1.2 2017/01/28 21:31:49 christos Exp $	*/
2603f2576Spettai 
3603f2576Spettai /*
4603f2576Spettai  * Copyright (c) 2011 Kungliga Tekniska Högskolan
5603f2576Spettai  * (Royal Institute of Technology, Stockholm, Sweden).
6603f2576Spettai  * All rights reserved.
7603f2576Spettai  *
8603f2576Spettai  * Redistribution and use in source and binary forms, with or without
9603f2576Spettai  * modification, are permitted provided that the following conditions
10603f2576Spettai  * are met:
11603f2576Spettai  *
12603f2576Spettai  * 1. Redistributions of source code must retain the above copyright
13603f2576Spettai  *    notice, this list of conditions and the following disclaimer.
14603f2576Spettai  *
15603f2576Spettai  * 2. Redistributions in binary form must reproduce the above copyright
16603f2576Spettai  *    notice, this list of conditions and the following disclaimer in the
17603f2576Spettai  *    documentation and/or other materials provided with the distribution.
18603f2576Spettai  *
19603f2576Spettai  * 3. Neither the name of KTH nor the names of its contributors may be
20603f2576Spettai  *    used to endorse or promote products derived from this software without
21603f2576Spettai  *    specific prior written permission.
22603f2576Spettai  *
23603f2576Spettai  * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
24603f2576Spettai  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25603f2576Spettai  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26603f2576Spettai  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
27603f2576Spettai  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28603f2576Spettai  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29603f2576Spettai  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30603f2576Spettai  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31603f2576Spettai  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32603f2576Spettai  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33603f2576Spettai  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
34603f2576Spettai 
35603f2576Spettai #include "krb5_locl.h"
36603f2576Spettai #include <err.h>
37603f2576Spettai 
38603f2576Spettai /*
39603f2576Spettai  *
40603f2576Spettai  */
41603f2576Spettai 
42603f2576Spettai static void
check_linear(krb5_context context,const char * client_realm,const char * server_realm,const char * realm,...)43603f2576Spettai check_linear(krb5_context context,
44603f2576Spettai 	     const char *client_realm,
45603f2576Spettai 	     const char *server_realm,
46603f2576Spettai 	     const char *realm,
47603f2576Spettai 	     ...)
48603f2576Spettai {
49603f2576Spettai     unsigned int num_inrealms = 0, num_realms = 0, n;
50603f2576Spettai     char **inrealms = NULL;
51603f2576Spettai     char **realms = NULL;
52603f2576Spettai     krb5_error_code ret;
53603f2576Spettai     krb5_data tr;
54603f2576Spettai     va_list va;
55603f2576Spettai 
56603f2576Spettai     krb5_data_zero(&tr);
57603f2576Spettai 
58603f2576Spettai     va_start(va, realm);
59603f2576Spettai 
60603f2576Spettai     while (realm) {
61603f2576Spettai 	inrealms = erealloc(inrealms, (num_inrealms + 2) * sizeof(inrealms[0]));
62603f2576Spettai 	inrealms[num_inrealms] = rk_UNCONST(realm);
63603f2576Spettai 	num_inrealms++;
64603f2576Spettai 	realm = va_arg(va, const char *);
65603f2576Spettai     }
66603f2576Spettai     if (inrealms)
67603f2576Spettai 	inrealms[num_inrealms] = NULL;
68603f2576Spettai 
69603f2576Spettai     ret = krb5_domain_x500_encode(inrealms, num_inrealms, &tr);
70603f2576Spettai     if (ret)
71603f2576Spettai 	krb5_err(context, 1, ret, "krb5_domain_x500_encode");
72603f2576Spettai 
73603f2576Spettai     ret = krb5_domain_x500_decode(context, tr,
74603f2576Spettai 				  &realms, &num_realms,
75603f2576Spettai 				  client_realm, server_realm);
76603f2576Spettai     if (ret)
77603f2576Spettai 	krb5_err(context, 1, ret, "krb5_domain_x500_decode");
78603f2576Spettai 
79603f2576Spettai     krb5_data_free(&tr);
80603f2576Spettai 
81603f2576Spettai     if (num_inrealms != num_realms)
82603f2576Spettai 	errx(1, "num_inrealms != num_realms");
83603f2576Spettai 
84603f2576Spettai     for(n = 0; n < num_realms; n++)
85603f2576Spettai 	free(realms[n]);
86603f2576Spettai     free(realms);
87603f2576Spettai 
88603f2576Spettai     free(inrealms);
89603f2576Spettai }
90603f2576Spettai 
91603f2576Spettai 
92603f2576Spettai int
main(int argc,char ** argv)93603f2576Spettai main(int argc, char **argv)
94603f2576Spettai {
95603f2576Spettai     krb5_context context;
96603f2576Spettai     krb5_error_code ret;
97603f2576Spettai 
98603f2576Spettai     setprogname(argv[0]);
99603f2576Spettai 
100603f2576Spettai     ret = krb5_init_context(&context);
101603f2576Spettai     if (ret)
102603f2576Spettai 	errx(1, "krb5_init_context");
103603f2576Spettai 
104603f2576Spettai 
105603f2576Spettai     check_linear(context, "KTH1.SE", "KTH1.SE", NULL);
106603f2576Spettai     check_linear(context, "KTH1.SE", "KTH2.SE", NULL);
107603f2576Spettai     check_linear(context, "KTH1.SE", "KTH3.SE", "KTH2.SE", NULL);
108603f2576Spettai     check_linear(context, "KTH1.SE", "KTH4.SE", "KTH3.SE", "KTH2.SE", NULL);
109603f2576Spettai     check_linear(context, "KTH1.SE", "KTH5.SE", "KTH4.SE", "KTH3.SE", "KTH2.SE", NULL);
110603f2576Spettai 
111603f2576Spettai     return 0;
112603f2576Spettai }
113