1*0a6a1f1dSLionel Sambuc /* $NetBSD: test_x500.c,v 1.1.1.1 2014/04/24 12:45:51 pettai Exp $ */
2*0a6a1f1dSLionel Sambuc
3*0a6a1f1dSLionel Sambuc /*
4*0a6a1f1dSLionel Sambuc * Copyright (c) 2011 Kungliga Tekniska Högskolan
5*0a6a1f1dSLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6*0a6a1f1dSLionel Sambuc * All rights reserved.
7*0a6a1f1dSLionel Sambuc *
8*0a6a1f1dSLionel Sambuc * Redistribution and use in source and binary forms, with or without
9*0a6a1f1dSLionel Sambuc * modification, are permitted provided that the following conditions
10*0a6a1f1dSLionel Sambuc * are met:
11*0a6a1f1dSLionel Sambuc *
12*0a6a1f1dSLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer.
14*0a6a1f1dSLionel Sambuc *
15*0a6a1f1dSLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17*0a6a1f1dSLionel Sambuc * documentation and/or other materials provided with the distribution.
18*0a6a1f1dSLionel Sambuc *
19*0a6a1f1dSLionel Sambuc * 3. Neither the name of KTH nor the names of its contributors may be
20*0a6a1f1dSLionel Sambuc * used to endorse or promote products derived from this software without
21*0a6a1f1dSLionel Sambuc * specific prior written permission.
22*0a6a1f1dSLionel Sambuc *
23*0a6a1f1dSLionel Sambuc * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
24*0a6a1f1dSLionel Sambuc * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*0a6a1f1dSLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26*0a6a1f1dSLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
27*0a6a1f1dSLionel Sambuc * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28*0a6a1f1dSLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29*0a6a1f1dSLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30*0a6a1f1dSLionel Sambuc * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31*0a6a1f1dSLionel Sambuc * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32*0a6a1f1dSLionel Sambuc * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33*0a6a1f1dSLionel Sambuc * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
34*0a6a1f1dSLionel Sambuc
35*0a6a1f1dSLionel Sambuc #include "krb5_locl.h"
36*0a6a1f1dSLionel Sambuc #include <err.h>
37*0a6a1f1dSLionel Sambuc
38*0a6a1f1dSLionel Sambuc /*
39*0a6a1f1dSLionel Sambuc *
40*0a6a1f1dSLionel Sambuc */
41*0a6a1f1dSLionel Sambuc
42*0a6a1f1dSLionel Sambuc static void
check_linear(krb5_context context,const char * client_realm,const char * server_realm,const char * realm,...)43*0a6a1f1dSLionel Sambuc check_linear(krb5_context context,
44*0a6a1f1dSLionel Sambuc const char *client_realm,
45*0a6a1f1dSLionel Sambuc const char *server_realm,
46*0a6a1f1dSLionel Sambuc const char *realm,
47*0a6a1f1dSLionel Sambuc ...)
48*0a6a1f1dSLionel Sambuc {
49*0a6a1f1dSLionel Sambuc unsigned int num_inrealms = 0, num_realms = 0, n;
50*0a6a1f1dSLionel Sambuc char **inrealms = NULL;
51*0a6a1f1dSLionel Sambuc char **realms = NULL;
52*0a6a1f1dSLionel Sambuc krb5_error_code ret;
53*0a6a1f1dSLionel Sambuc krb5_data tr;
54*0a6a1f1dSLionel Sambuc va_list va;
55*0a6a1f1dSLionel Sambuc
56*0a6a1f1dSLionel Sambuc krb5_data_zero(&tr);
57*0a6a1f1dSLionel Sambuc
58*0a6a1f1dSLionel Sambuc va_start(va, realm);
59*0a6a1f1dSLionel Sambuc
60*0a6a1f1dSLionel Sambuc while (realm) {
61*0a6a1f1dSLionel Sambuc inrealms = erealloc(inrealms, (num_inrealms + 2) * sizeof(inrealms[0]));
62*0a6a1f1dSLionel Sambuc inrealms[num_inrealms] = rk_UNCONST(realm);
63*0a6a1f1dSLionel Sambuc num_inrealms++;
64*0a6a1f1dSLionel Sambuc realm = va_arg(va, const char *);
65*0a6a1f1dSLionel Sambuc }
66*0a6a1f1dSLionel Sambuc if (inrealms)
67*0a6a1f1dSLionel Sambuc inrealms[num_inrealms] = NULL;
68*0a6a1f1dSLionel Sambuc
69*0a6a1f1dSLionel Sambuc ret = krb5_domain_x500_encode(inrealms, num_inrealms, &tr);
70*0a6a1f1dSLionel Sambuc if (ret)
71*0a6a1f1dSLionel Sambuc krb5_err(context, 1, ret, "krb5_domain_x500_encode");
72*0a6a1f1dSLionel Sambuc
73*0a6a1f1dSLionel Sambuc ret = krb5_domain_x500_decode(context, tr,
74*0a6a1f1dSLionel Sambuc &realms, &num_realms,
75*0a6a1f1dSLionel Sambuc client_realm, server_realm);
76*0a6a1f1dSLionel Sambuc if (ret)
77*0a6a1f1dSLionel Sambuc krb5_err(context, 1, ret, "krb5_domain_x500_decode");
78*0a6a1f1dSLionel Sambuc
79*0a6a1f1dSLionel Sambuc krb5_data_free(&tr);
80*0a6a1f1dSLionel Sambuc
81*0a6a1f1dSLionel Sambuc if (num_inrealms != num_realms)
82*0a6a1f1dSLionel Sambuc errx(1, "num_inrealms != num_realms");
83*0a6a1f1dSLionel Sambuc
84*0a6a1f1dSLionel Sambuc for(n = 0; n < num_realms; n++)
85*0a6a1f1dSLionel Sambuc free(realms[n]);
86*0a6a1f1dSLionel Sambuc free(realms);
87*0a6a1f1dSLionel Sambuc
88*0a6a1f1dSLionel Sambuc free(inrealms);
89*0a6a1f1dSLionel Sambuc }
90*0a6a1f1dSLionel Sambuc
91*0a6a1f1dSLionel Sambuc
92*0a6a1f1dSLionel Sambuc int
main(int argc,char ** argv)93*0a6a1f1dSLionel Sambuc main(int argc, char **argv)
94*0a6a1f1dSLionel Sambuc {
95*0a6a1f1dSLionel Sambuc krb5_context context;
96*0a6a1f1dSLionel Sambuc krb5_error_code ret;
97*0a6a1f1dSLionel Sambuc
98*0a6a1f1dSLionel Sambuc setprogname(argv[0]);
99*0a6a1f1dSLionel Sambuc
100*0a6a1f1dSLionel Sambuc ret = krb5_init_context(&context);
101*0a6a1f1dSLionel Sambuc if (ret)
102*0a6a1f1dSLionel Sambuc errx(1, "krb5_init_context");
103*0a6a1f1dSLionel Sambuc
104*0a6a1f1dSLionel Sambuc
105*0a6a1f1dSLionel Sambuc check_linear(context, "KTH1.SE", "KTH1.SE", NULL);
106*0a6a1f1dSLionel Sambuc check_linear(context, "KTH1.SE", "KTH2.SE", NULL);
107*0a6a1f1dSLionel Sambuc check_linear(context, "KTH1.SE", "KTH3.SE", "KTH2.SE", NULL);
108*0a6a1f1dSLionel Sambuc check_linear(context, "KTH1.SE", "KTH4.SE", "KTH3.SE", "KTH2.SE", NULL);
109*0a6a1f1dSLionel Sambuc check_linear(context, "KTH1.SE", "KTH5.SE", "KTH4.SE", "KTH3.SE", "KTH2.SE", NULL);
110*0a6a1f1dSLionel Sambuc
111*0a6a1f1dSLionel Sambuc return 0;
112*0a6a1f1dSLionel Sambuc }
113