2 * Copyright (c) 2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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. */
33 #include "krb5_locl.h"
38 static size_t lname_size
= sizeof (localname
);
39 static int lname_size_arg
= 0;
40 static int simple_flag
= 0;
41 static int verbose_flag
= 0;
42 static int version_flag
= 0;
43 static int help_flag
= 0;
45 static struct getargs args
[] = {
46 {"lname-size", 0, arg_integer
, &lname_size_arg
,
47 "set localname size (0 means use default, must be 0..1023)", "integer" },
48 {"simple", 0, arg_flag
, &simple_flag
, /* Used for scripting */
49 "map the given principal and print the resulting localname", NULL
},
50 {"verbose", 0, arg_flag
, &verbose_flag
,
51 "print the actual principal name as well as the localname", NULL
},
52 {"version", 0, arg_flag
, &version_flag
,
53 "print version", NULL
},
54 {"help", 0, arg_flag
, &help_flag
,
59 test_alname(krb5_context context
, krb5_const_realm realm
,
60 const char *user
, const char *inst
,
61 const char *localuser
, int ok
)
67 ret
= krb5_make_principal(context
, &p
, realm
, user
, inst
, NULL
);
69 krb5_err(context
, 1, ret
, "krb5_build_principal");
71 ret
= krb5_unparse_name(context
, p
, &princ
);
73 krb5_err(context
, 1, ret
, "krb5_unparse_name");
75 ret
= krb5_aname_to_localname(context
, p
, lname_size
, localname
);
76 krb5_free_principal(context
, p
);
82 krb5_err(context
, 1, ret
, "krb5_aname_to_localname: %s -> %s",
87 if (strcmp(localname
, localuser
) != 0) {
89 errx(1, "compared failed %s != %s (should have succeded)",
90 localname
, localuser
);
93 errx(1, "compared failed %s == %s (should have failed)",
94 localname
, localuser
);
102 arg_printusage (args
,
103 sizeof(args
)/sizeof(*args
),
110 main(int argc
, char **argv
)
112 krb5_context context
;
118 setprogname(argv
[0]);
120 if(getarg(args
, sizeof(args
) / sizeof(args
[0]), argc
, argv
, &optidx
))
134 ret
= krb5_init_context(&context
);
136 errx (1, "krb5_init_context failed: %d", ret
);
139 krb5_principal princ
;
143 /* Map then print the result and exit */
145 errx(1, "One argument is required and it must be a principal name");
147 ret
= krb5_parse_name(context
, argv
[0], &princ
);
149 krb5_err(context
, 1, ret
, "krb5_build_principal");
151 ret
= krb5_unparse_name(context
, princ
, &unparsed
);
153 krb5_err(context
, 1, ret
, "krb5_unparse_name");
155 if (lname_size_arg
> 0 && lname_size_arg
< 1024)
156 lname_size
= lname_size_arg
;
157 else if (lname_size_arg
!= 0)
158 errx(1, "local name size must be between 0 and 1023 (inclusive)");
160 ret
= krb5_aname_to_localname(context
, princ
, lname_size
, localname
);
161 if (ret
== KRB5_NO_LOCALNAME
) {
163 fprintf(stderr
, "No mapping obtained for %s\n", unparsed
);
167 case KRB5_PLUGIN_NO_HANDLE
:
168 fprintf(stderr
, "Error: KRB5_PLUGIN_NO_HANDLE leaked!\n");
171 case KRB5_CONFIG_NOTENUFSPACE
:
172 fprintf(stderr
, "Error: lname-size (%lu) too small\n",
173 (long unsigned)lname_size
);
178 printf("%s ", unparsed
);
179 printf("%s\n", localname
);
182 krb5_err(context
, 4, ret
, "krb5_aname_to_localname");
186 krb5_free_principal(context
, princ
);
187 krb5_free_context(context
);
192 errx(1, "first argument should be a local user that is in root .k5login");
196 ret
= krb5_get_default_realm(context
, &realm
);
198 krb5_err(context
, 1, ret
, "krb5_get_default_realm");
200 test_alname(context
, realm
, user
, NULL
, user
, 1);
201 test_alname(context
, realm
, user
, "root", "root", 1);
203 test_alname(context
, "FOO.BAR.BAZ.KAKA", user
, NULL
, user
, 0);
204 test_alname(context
, "FOO.BAR.BAZ.KAKA", user
, "root", "root", 0);
206 test_alname(context
, realm
, user
, NULL
,
207 "not-same-as-user", 0);
208 test_alname(context
, realm
, user
, "root",
209 "not-same-as-user", 0);
211 test_alname(context
, "FOO.BAR.BAZ.KAKA", user
, NULL
,
212 "not-same-as-user", 0);
213 test_alname(context
, "FOO.BAR.BAZ.KAKA", user
, "root",
214 "not-same-as-user", 0);
216 krb5_free_context(context
);