1 /* $NetBSD: change.c,v 1.1.1.2 2014/04/24 12:45:26 pettai Exp $ */
4 * Copyright (c) 1997-2005 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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.
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.
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
36 #include "ktutil_locl.h"
40 static krb5_error_code
41 change_entry (krb5_keytab keytab
,
42 krb5_principal principal
, krb5_kvno kvno
,
43 const char *realm
, const char *admin_server
, int server_port
)
46 kadm5_config_params conf
;
53 ret
= krb5_unparse_name (context
, principal
, &client_name
);
55 krb5_warn (context
, ret
, "krb5_unparse_name");
59 memset (&conf
, 0, sizeof(conf
));
62 realm
= krb5_principal_get_realm(context
, principal
);
63 conf
.realm
= strdup(realm
);
64 if (conf
.realm
== NULL
) {
66 krb5_set_error_message(context
, ENOMEM
, "malloc failed");
69 conf
.mask
|= KADM5_CONFIG_REALM
;
72 conf
.admin_server
= strdup(admin_server
);
73 if (conf
.admin_server
== NULL
) {
76 krb5_set_error_message(context
, ENOMEM
, "malloc failed");
79 conf
.mask
|= KADM5_CONFIG_ADMIN_SERVER
;
83 conf
.kadmind_port
= htons(server_port
);
84 conf
.mask
|= KADM5_CONFIG_KADMIND_PORT
;
87 ret
= kadm5_init_with_skey_ctx (context
,
93 free(conf
.admin_server
);
96 krb5_warn (context
, ret
,
97 "kadm5_c_init_with_skey_ctx: %s:", client_name
);
101 ret
= kadm5_randkey_principal (kadm_handle
, principal
, &keys
, &num_keys
);
102 kadm5_destroy (kadm_handle
);
104 krb5_warn(context
, ret
, "kadm5_randkey_principal: %s:", client_name
);
109 for (i
= 0; i
< num_keys
; ++i
) {
110 krb5_keytab_entry new_entry
;
112 new_entry
.principal
= principal
;
113 new_entry
.timestamp
= time (NULL
);
114 new_entry
.vno
= kvno
+ 1;
115 new_entry
.keyblock
= keys
[i
];
117 ret
= krb5_kt_add_entry (context
, keytab
, &new_entry
);
119 krb5_warn (context
, ret
, "krb5_kt_add_entry");
120 krb5_free_keyblock_contents (context
, &keys
[i
]);
126 * loop over all the entries in the keytab (or those given) and change
127 * their keys, writing the new keys
131 krb5_principal principal
;
136 kt_change (struct change_options
*opt
, int argc
, char **argv
)
140 krb5_kt_cursor cursor
;
141 krb5_keytab_entry entry
;
143 struct change_set
*changeset
;
146 if((keytab
= ktutil_open_keytab()) == NULL
)
153 ret
= krb5_kt_start_seq_get(context
, keytab
, &cursor
);
155 krb5_warn(context
, ret
, "%s", keytab_string
);
159 while((ret
= krb5_kt_next_entry(context
, keytab
, &entry
, &cursor
)) == 0) {
162 for (i
= 0; i
< j
; ++i
) {
163 if (krb5_principal_compare (context
, changeset
[i
].principal
,
165 if (changeset
[i
].kvno
< entry
.vno
)
166 changeset
[i
].kvno
= entry
.vno
;
171 krb5_kt_free_entry (context
, &entry
);
178 for (i
= 0; i
< argc
; ++i
) {
179 krb5_principal princ
;
181 ret
= krb5_parse_name (context
, argv
[i
], &princ
);
183 krb5_warn (context
, ret
, "%s", argv
[i
]);
186 if (krb5_principal_compare (context
, princ
, entry
.principal
))
189 krb5_free_principal (context
, princ
);
197 max
= max(max
* 2, 1);
198 tmp
= realloc (changeset
, max
* sizeof(*changeset
));
200 krb5_kt_free_entry (context
, &entry
);
201 krb5_warnx (context
, "realloc: out of memory");
207 ret
= krb5_copy_principal (context
, entry
.principal
,
208 &changeset
[j
].principal
);
210 krb5_warn (context
, ret
, "krb5_copy_principal");
211 krb5_kt_free_entry (context
, &entry
);
214 changeset
[j
].kvno
= entry
.vno
;
217 krb5_kt_free_entry (context
, &entry
);
219 krb5_kt_end_seq_get(context
, keytab
, &cursor
);
221 if (ret
== KRB5_KT_END
) {
223 for (i
= 0; i
< j
; i
++) {
227 ret
= krb5_unparse_name (context
, changeset
[i
].principal
,
230 krb5_warn (context
, ret
, "krb5_unparse_name");
232 printf("Changing %s kvno %d\n",
233 client_name
, changeset
[i
].kvno
);
237 ret
= change_entry (keytab
,
238 changeset
[i
].principal
, changeset
[i
].kvno
,
240 opt
->admin_server_string
,
241 opt
->server_port_integer
);
247 for (i
= 0; i
< j
; i
++)
248 krb5_free_principal (context
, changeset
[i
].principal
);
252 krb5_kt_close(context
, keytab
);