2 * Copyright (c) 1997 - 2001 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 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.
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
34 #include "ktutil_locl.h"
36 RCSID("$Id: change.c,v 1.4 2001/07/23 09:46:40 joda Exp $");
39 change_entry (krb5_context context, krb5_keytab keytab,
40 krb5_keytab_entry *entry,
41 const char *realm, const char *admin_server, int server_port)
44 kadm5_config_params conf;
51 ret = krb5_unparse_name (context, entry->principal, &client_name);
53 krb5_warn (context, ret, "krb5_unparse_name");
57 memset (&conf, 0, sizeof(conf));
60 conf.realm = (char *)realm;
62 conf.realm = *krb5_princ_realm (context, entry->principal);
63 conf.mask |= KADM5_CONFIG_REALM;
66 conf.admin_server = (char *)admin_server;
67 conf.mask |= KADM5_CONFIG_ADMIN_SERVER;
71 conf.kadmind_port = htons(server_port);
72 conf.mask |= KADM5_CONFIG_KADMIND_PORT;
75 ret = kadm5_init_with_skey_ctx (context,
83 krb5_warn (context, ret, "kadm5_c_init_with_skey_ctx");
86 ret = kadm5_randkey_principal (kadm_handle, entry->principal,
88 kadm5_destroy (kadm_handle);
90 krb5_warn(context, ret, "kadm5_randkey_principal");
93 for (i = 0; i < num_keys; ++i) {
94 krb5_keytab_entry new_entry;
97 new_entry.timestamp = time (NULL);
99 new_entry.keyblock = keys[i];
101 ret = krb5_kt_add_entry (context, keytab, &new_entry);
103 krb5_warn (context, ret, "krb5_kt_add_entry");
104 krb5_free_keyblock_contents (context, &keys[i]);
109 * loop over all the entries in the keytab (or those given) and change
110 * their keys, writing the new keys
114 kt_change (int argc, char **argv)
118 krb5_kt_cursor cursor;
119 krb5_keytab_entry entry;
121 char *admin_server = NULL;
126 krb5_principal *princs;
128 struct getargs args[] = {
129 { "realm", 'r', arg_string, NULL,
130 "realm to use", "realm"
132 { "admin-server", 'a', arg_string, NULL,
133 "server to contact", "host"
135 { "server-port", 's', arg_integer, NULL,
136 "port to contact", "port number"
138 { "help", 'h', arg_flag, NULL }
141 args[0].value = &realm;
142 args[1].value = &admin_server;
143 args[2].value = &server_port;
144 args[3].value = &help_flag;
146 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optind)
148 arg_printusage(args, sizeof(args) / sizeof(args[0]),
149 "ktutil change", "principal...");
153 if((keytab = ktutil_open_keytab()) == NULL)
158 princs = malloc (max * sizeof(*princs));
159 if (princs == NULL) {
160 krb5_warnx (context, "malloc: out of memory");
164 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
166 krb5_warn(context, ret, "krb5_kt_start_seq_get %s", keytab_string);
170 while((ret = krb5_kt_next_entry(context, keytab, &entry, &cursor)) == 0) {
174 for (i = 0; i < j; ++i)
175 if (krb5_principal_compare (context, princs[i],
181 if (optind == argc) {
182 change_entry (context, keytab, &entry, realm, admin_server,
186 for (i = optind; i < argc; ++i) {
187 krb5_principal princ;
189 ret = krb5_parse_name (context, argv[i], &princ);
191 krb5_warn (context, ret, "krb5_parse_name %s", argv[i]);
194 if (krb5_principal_compare (context, princ, entry.principal)) {
195 change_entry (context, keytab, &entry,
196 realm, admin_server, server_port);
199 krb5_free_principal (context, princ);
207 tmp = realloc (princs, max * sizeof(*princs));
209 krb5_kt_free_entry (context, &entry);
210 krb5_warnx (context, "realloc: out of memory");
215 ret = krb5_copy_principal (context, entry.principal, &princs[j]);
217 krb5_warn (context, ret, "krb5_copy_principal");
218 krb5_kt_free_entry (context, &entry);
223 krb5_kt_free_entry (context, &entry);
226 krb5_free_principal (context, princs[j]);
228 ret = krb5_kt_end_seq_get(context, keytab, &cursor);
230 krb5_kt_close(context, keytab);