Home | History | Annotate | Line # | Download | only in kuser
      1 /*	$NetBSD: heimtools.c,v 1.2 2017/01/28 21:31:45 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2010 Kungliga Tekniska Hgskolan
      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 the Institute nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include "kuser_locl.h"
     37 #include <krb5/sl.h>
     38 #include "heimtools-commands.h"
     39 
     40 krb5_context heimtools_context;
     41 static int version_flag;
     42 static int help_flag;
     43 
     44 static struct getargs args[] = {
     45     { "version", 	0,   arg_flag, &version_flag, NULL, NULL },
     46     { "help",		0,   arg_flag, &help_flag, NULL, NULL }
     47 };
     48 
     49 static void
     50 usage(int ret)
     51 {
     52     arg_printusage_i18n(args,
     53 			sizeof(args)/sizeof(*args),
     54 			N_("Usage: ", ""),
     55 			NULL,
     56 			"command ..",
     57 			getarg_i18n);
     58     exit (ret);
     59 }
     60 
     61 int
     62 help(void *opt, int argc, char **argv)
     63 {
     64     sl_slc_help(commands, argc, argv);
     65     return 0;
     66 }
     67 
     68 int
     69 kgetcred(struct kgetcred_options *opt, int argc, char **argv)
     70 {
     71     return 0;
     72 }
     73 
     74 /*
     75  * Wrapper for command line compatiblity
     76  */
     77 
     78 int
     79 kvno(struct kvno_options *opt, int argc, char **argv)
     80 {
     81     struct kgetcred_options k;
     82     memset(&k, 0, sizeof(k));
     83 
     84     k.cache_string = opt->cache_string;
     85     k.enctype_string = opt->enctype_string;
     86 
     87     return kgetcred(&k, argc, argv);
     88 }
     89 
     90 static int
     91 command_alias(const char *name)
     92 {
     93     const char *aliases[] = {
     94 	"kinit", "klist", "kswitch", "kgetcred", "kvno", "kdeltkt",
     95 	"kdestroy", "kcpytkt", NULL
     96     }, **p = aliases;
     97 
     98     while (*p && strcmp(name, *p) != 0)
     99 	p++;
    100     return *p != NULL;
    101 }
    102 
    103 
    104 int
    105 main(int argc, char **argv)
    106 {
    107     krb5_error_code ret;
    108     int optidx = 0;
    109     int exit_status = 0;
    110 
    111     setprogname (argv[0]);
    112 
    113     setlocale (LC_ALL, "");
    114     bindtextdomain ("heimdal_kuser", HEIMDAL_LOCALEDIR);
    115     textdomain("heimdal_kuser");
    116 
    117     ret = krb5_init_context(&heimtools_context);
    118     if (ret == KRB5_CONFIG_BADFORMAT)
    119 	errx (1, "krb5_init_context failed to parse configuration file");
    120     else if (ret)
    121 	errx(1, "krb5_init_context failed: %d", ret);
    122 
    123     /*
    124      * Support linking of heimtools to commands
    125      */
    126 
    127     if (!command_alias(getprogname())) {
    128 
    129 	if (argc == 1) {
    130 	    sl_slc_help(commands, 0, NULL);
    131 	    return 1;
    132 	}
    133 
    134 	if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
    135 	    usage(1);
    136 
    137 	if (help_flag)
    138 	    usage (0);
    139 
    140 	if(version_flag) {
    141 	    print_version(NULL);
    142 	    exit(0);
    143 	}
    144 
    145     } else {
    146 	argv[0] = rk_UNCONST(getprogname());
    147     }
    148 
    149     argc -= optidx;
    150     argv += optidx;
    151 
    152     if (argc != 0) {
    153 	ret = sl_command(commands, argc, argv);
    154 	if(ret == -1)
    155 	    sl_did_you_mean(commands, argv[0]);
    156 	else if (ret == -2)
    157 	    ret = 0;
    158 	if(ret != 0)
    159 	    exit_status = 1;
    160     } else {
    161 	sl_slc_help(commands, argc, argv);
    162 	exit_status = 1;
    163     }
    164 
    165     krb5_free_context(heimtools_context);
    166     return exit_status;
    167 }
    168