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