xref: /freebsd/crypto/heimdal/admin/ktutil.c (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
1 /*
2  * Copyright (c) 1997 - 2000 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 the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include "ktutil_locl.h"
35 
36 RCSID("$Id: ktutil.c,v 1.26 2000/02/07 04:29:25 assar Exp $");
37 
38 static int help_flag;
39 static int version_flag;
40 int verbose_flag;
41 char *keytab_string;
42 
43 static int help(int argc, char **argv);
44 
45 static SL_cmd cmds[] = {
46     { "add", 		kt_add,		"add",
47       "adds key to keytab" },
48     { "change",		kt_change,	"change [principal...]",
49       "get new key for principals (all)" },
50     { "copy",		kt_copy,	"copy src dst",
51       "copy one keytab to another" },
52     { "get", 		kt_get,		"get [principal...]",
53       "create key in database and add to keytab" },
54     { "list",		kt_list,	"list",
55       "shows contents of a keytab" },
56     { "purge",		kt_purge,	"purge",
57       "remove old and superceeded entries" },
58     { "remove", 	kt_remove,	"remove",
59       "remove key from keytab" },
60     { "srvconvert",	srvconv,	"srvconvert [flags]",
61       "convert v4 srvtab to keytab" },
62     { "srv2keytab" },
63     { "srvcreate",	srvcreate,	"srvcreate [flags]",
64       "convert keytab to v4 srvtab" },
65     { "key2srvtab" },
66     { "help",		help,		"help",			"" },
67     { NULL, 	NULL,		NULL, 			NULL }
68 };
69 
70 static struct getargs args[] = {
71     {
72 	"version",
73 	0,
74 	arg_flag,
75 	&version_flag,
76 	NULL,
77 	NULL
78     },
79     {
80 	"help",
81 	'h',
82 	arg_flag,
83 	&help_flag,
84 	NULL,
85 	NULL
86     },
87     {
88 	"keytab",
89 	'k',
90 	arg_string,
91 	&keytab_string,
92 	"keytab",
93 	"keytab to operate on"
94     },
95     {
96 	"verbose",
97 	'v',
98 	arg_flag,
99 	&verbose_flag,
100 	"verbose",
101 	"run verbosely"
102     }
103 };
104 
105 static int num_args = sizeof(args) / sizeof(args[0]);
106 
107 krb5_context context;
108 krb5_keytab keytab;
109 
110 static int
111 help(int argc, char **argv)
112 {
113     sl_help(cmds, argc, argv);
114     return 0;
115 }
116 
117 static void
118 usage(int status)
119 {
120     arg_printusage(args, num_args, NULL, "command");
121     exit(status);
122 }
123 
124 int
125 main(int argc, char **argv)
126 {
127     int optind = 0;
128     krb5_error_code ret;
129     set_progname(argv[0]);
130     krb5_init_context(&context);
131     if(getarg(args, num_args, argc, argv, &optind))
132 	usage(1);
133     if(help_flag)
134 	usage(0);
135     if(version_flag) {
136 	print_version(NULL);
137 	exit(0);
138     }
139     argc -= optind;
140     argv += optind;
141     if(argc == 0)
142 	usage(1);
143     if(keytab_string) {
144 	ret = krb5_kt_resolve(context, keytab_string, &keytab);
145     } else {
146 	ret = krb5_kt_default(context, &keytab);
147     }
148     if(ret)
149 	krb5_err(context, 1, ret, "resolving keytab");
150     ret = sl_command(cmds, argc, argv);
151     if(ret == -1)
152 	krb5_warnx (context, "unrecognized command: %s", argv[0]);
153     krb5_kt_close(context, keytab);
154     return ret;
155 }
156