xref: /freebsd/crypto/heimdal/lib/krb5/test_acl.c (revision 6a068746777241722b2b32c5d0bc443a2a64d80b)
1 /*
2  * Copyright (c) 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 KTH nor the names of its contributors may be
18  *    used to endorse or promote products derived from this software without
19  *    specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
22  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
32 
33 #include "krb5_locl.h"
34 #include <err.h>
35 
36 #define RETVAL(c, r, e, s) \
37 	do { if (r != e) krb5_errx(c, 1, "%s", s); } while (0)
38 #define STRINGMATCH(c, s, _s1, _s2) \
39 	do {							\
40 		if (_s1 == NULL || _s2 == NULL) 		\
41 			krb5_errx(c, 1, "s1 or s2 is NULL");	\
42 		if (strcmp(_s1,_s2) != 0) 			\
43 			krb5_errx(c, 1, "%s", s);		\
44 	} while (0)
45 
46 static void
test_match_string(krb5_context context)47 test_match_string(krb5_context context)
48 {
49     krb5_error_code ret;
50     char *s1, *s2;
51 
52     ret = krb5_acl_match_string(context, "foo", "s", "foo");
53     RETVAL(context, ret, 0, "single s");
54     ret = krb5_acl_match_string(context, "foo foo", "s", "foo");
55     RETVAL(context, ret, EACCES, "too many strings");
56     ret = krb5_acl_match_string(context, "foo bar", "ss", "foo", "bar");
57     RETVAL(context, ret, 0, "two strings");
58     ret = krb5_acl_match_string(context, "foo  bar", "ss", "foo", "bar");
59     RETVAL(context, ret, 0, "two strings double space");
60     ret = krb5_acl_match_string(context, "foo \tbar", "ss", "foo", "bar");
61     RETVAL(context, ret, 0, "two strings space + tab");
62     ret = krb5_acl_match_string(context, "foo", "ss", "foo", "bar");
63     RETVAL(context, ret, EACCES, "one string, two format strings");
64     ret = krb5_acl_match_string(context, "foo", "ss", "foo", "foo");
65     RETVAL(context, ret, EACCES, "one string, two format strings (same)");
66     ret = krb5_acl_match_string(context, "foo  \t", "s", "foo");
67     RETVAL(context, ret, 0, "ending space");
68 
69     ret = krb5_acl_match_string(context, "foo/bar", "f", "foo/bar");
70     RETVAL(context, ret, 0, "liternal fnmatch");
71     ret = krb5_acl_match_string(context, "foo/bar", "f", "foo/*");
72     RETVAL(context, ret, 0, "foo/*");
73     ret = krb5_acl_match_string(context, "foo/bar.example.org", "f",
74 				"foo/*.example.org");
75     RETVAL(context, ret, 0, "foo/*.example.org");
76     ret = krb5_acl_match_string(context, "foo/bar.example.com", "f",
77 				"foo/*.example.org");
78     RETVAL(context, ret, EACCES, "foo/*.example.com");
79 
80     ret = krb5_acl_match_string(context, "foo/bar/baz", "f", "foo/*/baz");
81     RETVAL(context, ret, 0, "foo/*/baz");
82 
83     ret = krb5_acl_match_string(context, "foo", "r", &s1);
84     RETVAL(context, ret, 0, "ret 1");
85     STRINGMATCH(context, "ret 1 match", s1, "foo"); free(s1);
86 
87     ret = krb5_acl_match_string(context, "foo bar", "rr", &s1, &s2);
88     RETVAL(context, ret, 0, "ret 2");
89     STRINGMATCH(context, "ret 2 match 1", s1, "foo"); free(s1);
90     STRINGMATCH(context, "ret 2 match 2", s2, "bar"); free(s2);
91 
92     ret = krb5_acl_match_string(context, "foo bar", "sr", "bar", &s1);
93     RETVAL(context, ret, EACCES, "ret mismatch");
94     if (s1 != NULL) krb5_errx(context, 1, "s1 not NULL");
95 
96     ret = krb5_acl_match_string(context, "foo", "l", "foo");
97     RETVAL(context, ret, EINVAL, "unknown letter");
98 }
99 
100 
101 int
main(int argc,char ** argv)102 main(int argc, char **argv)
103 {
104     krb5_context context;
105     krb5_error_code ret;
106 
107     setprogname(argv[0]);
108 
109     ret = krb5_init_context(&context);
110     if (ret)
111 	errx (1, "krb5_init_context failed: %d", ret);
112 
113     test_match_string(context);
114 
115     krb5_free_context(context);
116 
117     return 0;
118 }
119