xref: /freebsd/crypto/heimdal/admin/ktutil.c (revision 5e9cd1ae3e10592ed70e7575551cba1bbab04d84)
1 /*
2  * Copyright (c) 1997 - 2001 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 #include <err.h>
36 
37 RCSID("$Id: ktutil.c,v 1.30 2001/01/25 12:44:37 assar Exp $");
38 
39 static int help_flag;
40 static int version_flag;
41 int verbose_flag;
42 char *keytab_string;
43 
44 static char keytab_buf[256];
45 
46 static int help(int argc, char **argv);
47 
48 static SL_cmd cmds[] = {
49     { "add", 		kt_add,		"add",
50       "adds key to keytab" },
51     { "change",		kt_change,	"change [principal...]",
52       "get new key for principals (all)" },
53     { "copy",		kt_copy,	"copy src dst",
54       "copy one keytab to another" },
55     { "get", 		kt_get,		"get [principal...]",
56       "create key in database and add to keytab" },
57     { "list",		kt_list,	"list",
58       "shows contents of a keytab" },
59     { "purge",		kt_purge,	"purge",
60       "remove old and superceeded entries" },
61     { "remove", 	kt_remove,	"remove",
62       "remove key from keytab" },
63     { "srvconvert",	srvconv,	"srvconvert [flags]",
64       "convert v4 srvtab to keytab" },
65     { "srv2keytab" },
66     { "srvcreate",	srvcreate,	"srvcreate [flags]",
67       "convert keytab to v4 srvtab" },
68     { "key2srvtab" },
69     { "help",		help,		"help",			"" },
70     { NULL, 	NULL,		NULL, 			NULL }
71 };
72 
73 static struct getargs args[] = {
74     {
75 	"version",
76 	0,
77 	arg_flag,
78 	&version_flag,
79 	NULL,
80 	NULL
81     },
82     {
83 	"help",
84 	'h',
85 	arg_flag,
86 	&help_flag,
87 	NULL,
88 	NULL
89     },
90     {
91 	"keytab",
92 	'k',
93 	arg_string,
94 	&keytab_string,
95 	"keytab",
96 	"keytab to operate on"
97     },
98     {
99 	"verbose",
100 	'v',
101 	arg_flag,
102 	&verbose_flag,
103 	"verbose",
104 	"run verbosely"
105     }
106 };
107 
108 static int num_args = sizeof(args) / sizeof(args[0]);
109 
110 krb5_context context;
111 krb5_keytab keytab;
112 
113 static int
114 help(int argc, char **argv)
115 {
116     sl_help(cmds, argc, argv);
117     return 0;
118 }
119 
120 static void
121 usage(int status)
122 {
123     arg_printusage(args, num_args, NULL, "command");
124     exit(status);
125 }
126 
127 int
128 main(int argc, char **argv)
129 {
130     int optind = 0;
131     krb5_error_code ret;
132     set_progname(argv[0]);
133     ret = krb5_init_context(&context);
134     if (ret)
135 	errx (1, "krb5_init_context failed: %d", ret);
136     if(getarg(args, num_args, argc, argv, &optind))
137 	usage(1);
138     if(help_flag)
139 	usage(0);
140     if(version_flag) {
141 	print_version(NULL);
142 	exit(0);
143     }
144     argc -= optind;
145     argv += optind;
146     if(argc == 0)
147 	usage(1);
148     if(keytab_string) {
149 	ret = krb5_kt_resolve(context, keytab_string, &keytab);
150     } else {
151 	if(krb5_kt_default_name (context, keytab_buf, sizeof(keytab_buf)))
152 	    strlcpy (keytab_buf, "unknown", sizeof(keytab_buf));
153 	keytab_string = keytab_buf;
154 
155 	ret = krb5_kt_default(context, &keytab);
156     }
157     if(ret)
158 	krb5_err(context, 1, ret, "resolving keytab");
159     ret = sl_command(cmds, argc, argv);
160     if(ret == -1)
161 	krb5_warnx (context, "unrecognized command: %s", argv[0]);
162     krb5_kt_close(context, keytab);
163     return ret;
164 }
165