xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/krb5/test_acl.c (revision d3273b5b76f5afaafe308cead5511dbb8df8c5e9)
1*d3273b5bSchristos /*	$NetBSD: test_acl.c,v 1.2 2017/01/28 21:31:49 christos Exp $	*/
2ca1c9b0cSelric 
3ca1c9b0cSelric /*
4ca1c9b0cSelric  * Copyright (c) 2004 Kungliga Tekniska Högskolan
5ca1c9b0cSelric  * (Royal Institute of Technology, Stockholm, Sweden).
6ca1c9b0cSelric  * All rights reserved.
7ca1c9b0cSelric  *
8ca1c9b0cSelric  * Redistribution and use in source and binary forms, with or without
9ca1c9b0cSelric  * modification, are permitted provided that the following conditions
10ca1c9b0cSelric  * are met:
11ca1c9b0cSelric  *
12ca1c9b0cSelric  * 1. Redistributions of source code must retain the above copyright
13ca1c9b0cSelric  *    notice, this list of conditions and the following disclaimer.
14ca1c9b0cSelric  *
15ca1c9b0cSelric  * 2. Redistributions in binary form must reproduce the above copyright
16ca1c9b0cSelric  *    notice, this list of conditions and the following disclaimer in the
17ca1c9b0cSelric  *    documentation and/or other materials provided with the distribution.
18ca1c9b0cSelric  *
19ca1c9b0cSelric  * 3. Neither the name of KTH nor the names of its contributors may be
20ca1c9b0cSelric  *    used to endorse or promote products derived from this software without
21ca1c9b0cSelric  *    specific prior written permission.
22ca1c9b0cSelric  *
23ca1c9b0cSelric  * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
24ca1c9b0cSelric  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ca1c9b0cSelric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26ca1c9b0cSelric  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
27ca1c9b0cSelric  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28ca1c9b0cSelric  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29ca1c9b0cSelric  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30ca1c9b0cSelric  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31ca1c9b0cSelric  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32ca1c9b0cSelric  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33ca1c9b0cSelric  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
34ca1c9b0cSelric 
35ca1c9b0cSelric #include "krb5_locl.h"
36ca1c9b0cSelric #include <err.h>
37ca1c9b0cSelric 
38ca1c9b0cSelric #define RETVAL(c, r, e, s) \
39ca1c9b0cSelric 	do { if (r != e) krb5_errx(c, 1, "%s", s); } while (0)
40ca1c9b0cSelric #define STRINGMATCH(c, s, _s1, _s2) \
41ca1c9b0cSelric 	do {							\
42ca1c9b0cSelric 		if (_s1 == NULL || _s2 == NULL) 		\
43ca1c9b0cSelric 			krb5_errx(c, 1, "s1 or s2 is NULL");	\
44ca1c9b0cSelric 		if (strcmp(_s1,_s2) != 0) 			\
45ca1c9b0cSelric 			krb5_errx(c, 1, "%s", s);		\
46ca1c9b0cSelric 	} while (0)
47ca1c9b0cSelric 
48ca1c9b0cSelric static void
test_match_string(krb5_context context)49ca1c9b0cSelric test_match_string(krb5_context context)
50ca1c9b0cSelric {
51ca1c9b0cSelric     krb5_error_code ret;
52ca1c9b0cSelric     char *s1, *s2;
53ca1c9b0cSelric 
54ca1c9b0cSelric     ret = krb5_acl_match_string(context, "foo", "s", "foo");
55ca1c9b0cSelric     RETVAL(context, ret, 0, "single s");
56ca1c9b0cSelric     ret = krb5_acl_match_string(context, "foo foo", "s", "foo");
57ca1c9b0cSelric     RETVAL(context, ret, EACCES, "too many strings");
58ca1c9b0cSelric     ret = krb5_acl_match_string(context, "foo bar", "ss", "foo", "bar");
59ca1c9b0cSelric     RETVAL(context, ret, 0, "two strings");
60ca1c9b0cSelric     ret = krb5_acl_match_string(context, "foo  bar", "ss", "foo", "bar");
61ca1c9b0cSelric     RETVAL(context, ret, 0, "two strings double space");
62ca1c9b0cSelric     ret = krb5_acl_match_string(context, "foo \tbar", "ss", "foo", "bar");
63ca1c9b0cSelric     RETVAL(context, ret, 0, "two strings space + tab");
64ca1c9b0cSelric     ret = krb5_acl_match_string(context, "foo", "ss", "foo", "bar");
65ca1c9b0cSelric     RETVAL(context, ret, EACCES, "one string, two format strings");
66ca1c9b0cSelric     ret = krb5_acl_match_string(context, "foo", "ss", "foo", "foo");
67ca1c9b0cSelric     RETVAL(context, ret, EACCES, "one string, two format strings (same)");
68ca1c9b0cSelric     ret = krb5_acl_match_string(context, "foo  \t", "s", "foo");
69ca1c9b0cSelric     RETVAL(context, ret, 0, "ending space");
70ca1c9b0cSelric 
71ca1c9b0cSelric     ret = krb5_acl_match_string(context, "foo/bar", "f", "foo/bar");
72ca1c9b0cSelric     RETVAL(context, ret, 0, "liternal fnmatch");
73ca1c9b0cSelric     ret = krb5_acl_match_string(context, "foo/bar", "f", "foo/*");
74ca1c9b0cSelric     RETVAL(context, ret, 0, "foo/*");
75ca1c9b0cSelric     ret = krb5_acl_match_string(context, "foo/bar.example.org", "f",
76ca1c9b0cSelric 				"foo/*.example.org");
77ca1c9b0cSelric     RETVAL(context, ret, 0, "foo/*.example.org");
78ca1c9b0cSelric     ret = krb5_acl_match_string(context, "foo/bar.example.com", "f",
79ca1c9b0cSelric 				"foo/*.example.org");
80ca1c9b0cSelric     RETVAL(context, ret, EACCES, "foo/*.example.com");
81ca1c9b0cSelric 
82ca1c9b0cSelric     ret = krb5_acl_match_string(context, "foo/bar/baz", "f", "foo/*/baz");
83ca1c9b0cSelric     RETVAL(context, ret, 0, "foo/*/baz");
84ca1c9b0cSelric 
85ca1c9b0cSelric     ret = krb5_acl_match_string(context, "foo", "r", &s1);
86ca1c9b0cSelric     RETVAL(context, ret, 0, "ret 1");
87ca1c9b0cSelric     STRINGMATCH(context, "ret 1 match", s1, "foo"); free(s1);
88ca1c9b0cSelric 
89ca1c9b0cSelric     ret = krb5_acl_match_string(context, "foo bar", "rr", &s1, &s2);
90ca1c9b0cSelric     RETVAL(context, ret, 0, "ret 2");
91ca1c9b0cSelric     STRINGMATCH(context, "ret 2 match 1", s1, "foo"); free(s1);
92ca1c9b0cSelric     STRINGMATCH(context, "ret 2 match 2", s2, "bar"); free(s2);
93ca1c9b0cSelric 
94ca1c9b0cSelric     ret = krb5_acl_match_string(context, "foo bar", "sr", "bar", &s1);
95ca1c9b0cSelric     RETVAL(context, ret, EACCES, "ret mismatch");
96ca1c9b0cSelric     if (s1 != NULL) krb5_errx(context, 1, "s1 not NULL");
97ca1c9b0cSelric 
98ca1c9b0cSelric     ret = krb5_acl_match_string(context, "foo", "l", "foo");
99ca1c9b0cSelric     RETVAL(context, ret, EINVAL, "unknown letter");
100ca1c9b0cSelric }
101ca1c9b0cSelric 
102ca1c9b0cSelric 
103ca1c9b0cSelric int
main(int argc,char ** argv)104ca1c9b0cSelric main(int argc, char **argv)
105ca1c9b0cSelric {
106ca1c9b0cSelric     krb5_context context;
107ca1c9b0cSelric     krb5_error_code ret;
108ca1c9b0cSelric 
109ca1c9b0cSelric     setprogname(argv[0]);
110ca1c9b0cSelric 
111ca1c9b0cSelric     ret = krb5_init_context(&context);
112ca1c9b0cSelric     if (ret)
113ca1c9b0cSelric 	errx (1, "krb5_init_context failed: %d", ret);
114ca1c9b0cSelric 
115ca1c9b0cSelric     test_match_string(context);
116ca1c9b0cSelric 
117ca1c9b0cSelric     krb5_free_context(context);
118ca1c9b0cSelric 
119ca1c9b0cSelric     return 0;
120ca1c9b0cSelric }
121