1 /*        $NetBSD: test_alname.c,v 1.2 2017/01/28 21:31:49 christos Exp $       */
2 
3 /*
4  * Copyright (c) 2003 Kungliga Tekniska Högskolan
5  * (Royal Institute of Technology, Stockholm, Sweden).
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of KTH nor the names of its contributors may be
20  *    used to endorse or promote products derived from this software without
21  *    specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
24  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
34 
35 #include "krb5_locl.h"
36 #include <krb5/getarg.h>
37 #include <err.h>
38 
39 char localname[1024];
40 static size_t lname_size = sizeof (localname);
41 static int lname_size_arg = 0;
42 static int simple_flag = 0;
43 static int verbose_flag = 0;
44 static int version_flag = 0;
45 static int help_flag          = 0;
46 
47 static struct getargs args[] = {
48     {"lname-size",  0,        arg_integer,        &lname_size_arg,
49      "set localname size (0 means use default, must be 0..1023)", "integer" },
50     {"simple",      0,        arg_flag, &simple_flag, /* Used for scripting */
51      "map the given principal and print the resulting localname", NULL },
52     {"verbose",     0,        arg_flag, &verbose_flag,
53      "print the actual principal name as well as the localname", NULL },
54     {"version",     0,        arg_flag, &version_flag,
55      "print version", NULL },
56     {"help",        0,        arg_flag, &help_flag,
57      NULL, NULL }
58 };
59 
60 static void
test_alname(krb5_context context,krb5_const_realm realm,const char * user,const char * inst,const char * localuser,int ok)61 test_alname(krb5_context context, krb5_const_realm realm,
62               const char *user, const char *inst,
63               const char *localuser, int ok)
64 {
65     krb5_principal p;
66     krb5_error_code ret;
67     char *princ;
68 
69     ret = krb5_make_principal(context, &p, realm, user, inst, NULL);
70     if (ret)
71           krb5_err(context, 1, ret, "krb5_build_principal");
72 
73     ret = krb5_unparse_name(context, p, &princ);
74     if (ret)
75           krb5_err(context, 1, ret, "krb5_unparse_name");
76 
77     ret = krb5_aname_to_localname(context, p, lname_size, localname);
78     krb5_free_principal(context, p);
79     if (ret) {
80           if (!ok) {
81               free(princ);
82               return;
83           }
84           krb5_err(context, 1, ret, "krb5_aname_to_localname: %s -> %s",
85                      princ, localuser);
86           free(princ);
87     }
88 
89     if (strcmp(localname, localuser) != 0) {
90           if (ok)
91               errx(1, "compared failed %s != %s (should have succeded)",
92                      localname, localuser);
93     } else {
94           if (!ok)
95               errx(1, "compared failed %s == %s (should have failed)",
96                      localname, localuser);
97     }
98 
99 }
100 
101 static void
usage(int ret)102 usage (int ret)
103 {
104     arg_printusage (args,
105                         sizeof(args)/sizeof(*args),
106                         NULL,
107                         "");
108     exit (ret);
109 }
110 
111 int
main(int argc,char ** argv)112 main(int argc, char **argv)
113 {
114     krb5_context context;
115     krb5_error_code ret;
116     krb5_realm realm;
117     int optidx = 0;
118     char *user;
119 
120     setprogname(argv[0]);
121 
122     if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
123           usage(1);
124 
125     if (help_flag)
126           usage (0);
127 
128     if(version_flag){
129           print_version(NULL);
130           exit(0);
131     }
132 
133     argc -= optidx;
134     argv += optidx;
135 
136     ret = krb5_init_context(&context);
137     if (ret)
138           errx (1, "krb5_init_context failed: %d", ret);
139 
140     if (simple_flag) {
141           krb5_principal princ;
142           char *unparsed;
143           int status = 0;
144 
145           /* Map then print the result and exit */
146           if (argc != 1)
147               errx(1, "One argument is required and it must be a principal name");
148 
149           ret = krb5_parse_name(context, argv[0], &princ);
150           if (ret)
151               krb5_err(context, 1, ret, "krb5_build_principal");
152 
153           ret = krb5_unparse_name(context, princ, &unparsed);
154           if (ret)
155               krb5_err(context, 1, ret, "krb5_unparse_name");
156 
157           if (lname_size_arg > 0 && lname_size_arg < 1024)
158               lname_size = lname_size_arg;
159           else if (lname_size_arg != 0)
160               errx(1, "local name size must be between 0 and 1023 (inclusive)");
161 
162           ret = krb5_aname_to_localname(context, princ, lname_size, localname);
163           if (ret == KRB5_NO_LOCALNAME) {
164               if (verbose_flag)
165                     fprintf(stderr, "No mapping obtained for %s\n", unparsed);
166               exit(1);
167           }
168           switch (ret) {
169           case KRB5_PLUGIN_NO_HANDLE:
170               fprintf(stderr, "Error: KRB5_PLUGIN_NO_HANDLE leaked!\n");
171               status = 2;
172               break;
173           case KRB5_CONFIG_NOTENUFSPACE:
174               fprintf(stderr, "Error: lname-size (%lu) too small\n",
175                         (long unsigned)lname_size);
176               status = 3;
177               break;
178           case 0:
179               if (verbose_flag)
180                     printf("%s ", unparsed);
181               printf("%s\n", localname);
182               break;
183           default:
184               krb5_err(context, 4, ret, "krb5_aname_to_localname");
185               break;
186           }
187           free(unparsed);
188           krb5_free_principal(context, princ);
189           krb5_free_context(context);
190           exit(status);
191     }
192 
193     if (argc != 1)
194           errx(1, "first argument should be a local user that is in root .k5login");
195 
196     user = argv[0];
197 
198     ret = krb5_get_default_realm(context, &realm);
199     if (ret)
200           krb5_err(context, 1, ret, "krb5_get_default_realm");
201 
202     test_alname(context, realm, user, NULL, user, 1);
203     test_alname(context, realm, user, "root", "root", 1);
204 
205     test_alname(context, "FOO.BAR.BAZ.KAKA", user, NULL, user, 0);
206     test_alname(context, "FOO.BAR.BAZ.KAKA", user, "root", "root", 0);
207 
208     test_alname(context, realm, user, NULL,
209                     "not-same-as-user", 0);
210     test_alname(context, realm, user, "root",
211                     "not-same-as-user", 0);
212 
213     test_alname(context, "FOO.BAR.BAZ.KAKA", user, NULL,
214                     "not-same-as-user", 0);
215     test_alname(context, "FOO.BAR.BAZ.KAKA", user, "root",
216                     "not-same-as-user", 0);
217 
218     krb5_free_context(context);
219 
220     return 0;
221 }
222