2 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
8 * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved.
10 * $Id: keytab.c,v 1.28 2004/05/31 12:39:16 epeisach Exp $
11 * $Source: /cvs/krbdev/krb5/src/kadmin/cli/keytab.c,v $
15 * Copyright (C) 1998 by the FundsXpress, INC.
17 * All rights reserved.
19 * Export of this software from the United States of America may require
20 * a specific license from the United States Government. It is the
21 * responsibility of any person or organization contemplating export to
22 * obtain such a license before exporting.
24 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
25 * distribute this software and its documentation for any purpose and
26 * without fee is hereby granted, provided that the above copyright
27 * notice appear in all copies and that both that copyright notice and
28 * this permission notice appear in supporting documentation, and that
29 * the name of FundsXpress. not be used in advertising or publicity pertaining
30 * to distribution of the software without specific, written prior
31 * permission. FundsXpress makes no representations about the suitability of
32 * this software for any purpose. It is provided "as is" without express
33 * or implied warranty.
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
36 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
37 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
40 static char *rcsid
= "$Header: /cvs/krbdev/krb5/src/kadmin/cli/keytab.c,v 1.28 2004/05/31 12:39:16 epeisach Exp $";
47 #include <kadm5/admin.h>
48 #include <krb5/adm_proto.h>
52 static int add_principal(void *lhandle
, char *keytab_str
, krb5_keytab keytab
,
54 int n_ks_tuple
, krb5_key_salt_tuple
*ks_tuple
,
56 static int remove_principal(char *keytab_str
, krb5_keytab keytab
, char
57 *princ_str
, char *kvno_str
);
58 static char *etype_string(krb5_enctype enctype
);
59 static char *etype_istring(krb5_enctype enctype
);
63 static void add_usage()
65 fprintf(stderr
, "%s: %s\n", gettext("Usage"),
66 "ktadd [-k[eytab] keytab] [-q] [-e keysaltlist] "
67 "[principal | -glob princ-exp] [...]\n");
70 static void rem_usage()
72 fprintf(stderr
, "%s: %s\n",
74 "ktremove [-k[eytab] keytab] [-q] principal "
75 "[kvno|\"all\"|\"old\"]\n");
78 static int process_keytab(krb5_context my_context
, char **keytab_str
,
84 if (*keytab_str
== NULL
) {
85 if (code
= krb5_kt_default(my_context
, keytab
)) {
86 com_err(whoami
, code
, gettext("while opening default keytab"));
89 if (code
= krb5_kt_get_name(my_context
, *keytab
, buf
, BUFSIZ
)) {
90 com_err(whoami
, code
, gettext("while retrieving keytab name"));
93 if (!(*keytab_str
= strdup(buf
))) {
94 com_err(whoami
, ENOMEM
, gettext("while creating keytab name"));
98 if (strchr(*keytab_str
, ':') != NULL
) {
99 *keytab_str
= strdup(*keytab_str
);
100 if (*keytab_str
== NULL
) {
101 com_err(whoami
, ENOMEM
,
102 gettext("while creating keytab name"));
106 char *tmp
= *keytab_str
;
108 *keytab_str
= (char *)
109 malloc(strlen("WRFILE:")+strlen(tmp
)+1);
110 if (*keytab_str
== NULL
) {
111 com_err(whoami
, ENOMEM
,
112 gettext("while creating keytab name"));
115 sprintf(*keytab_str
, "WRFILE:%s", tmp
);
118 code
= krb5_kt_resolve(my_context
, *keytab_str
, keytab
);
120 com_err(whoami
, code
,
121 gettext("while resolving keytab %s"), *keytab_str
);
131 void kadmin_keytab_add(int argc
, char **argv
)
133 krb5_keytab keytab
= 0;
134 char *keytab_str
= NULL
, **princs
;
136 krb5_error_code retval
;
138 krb5_boolean keepold
= FALSE
;
139 krb5_key_salt_tuple
*ks_tuple
= NULL
;
144 if (strncmp(*argv
, "-k", 2) == 0) {
146 if (!argc
|| keytab_str
) {
151 } else if (strcmp(*argv
, "-q") == 0) {
153 } else if (strcmp(*argv
, "-e") == 0) {
159 retval
= krb5_string_to_keysalts(*++argv
, ", \t", ":.-", 0,
160 &ks_tuple
, &n_ks_tuple
);
162 com_err("ktadd", retval
,
163 gettext("while parsing keysalts %s"),
178 if (process_keytab(context
, &keytab_str
, &keytab
))
182 if (strcmp(*argv
, "-glob") == 0) {
183 if (*++argv
== NULL
) {
188 code
= kadm5_get_principals(handle
, *argv
, &princs
, &num
);
190 com_err(whoami
, code
,
191 gettext("while expanding expression "
198 for (i
= 0; i
< num
; i
++)
199 (void) add_principal(handle
, keytab_str
, keytab
,
200 keepold
, n_ks_tuple
, ks_tuple
,
202 kadm5_free_name_list(handle
, princs
, num
);
204 (void) add_principal(handle
, keytab_str
, keytab
,
205 keepold
, n_ks_tuple
, ks_tuple
,
210 code
= krb5_kt_close(context
, keytab
);
212 com_err(whoami
, code
, gettext("while closing keytab"));
217 void kadmin_keytab_remove(int argc
, char **argv
)
219 krb5_keytab keytab
= 0;
220 char *keytab_str
= NULL
;
226 if (strncmp(*argv
, "-k", 2) == 0) {
228 if (!argc
|| keytab_str
) {
233 } else if (strcmp(*argv
, "-q") == 0) {
240 if (argc
!= 1 && argc
!= 2) {
244 if (process_keytab(context
, &keytab_str
, &keytab
))
247 (void) remove_principal(keytab_str
, keytab
, argv
[0], argv
[1]);
249 code
= krb5_kt_close(context
, keytab
);
251 com_err(whoami
, code
, gettext("while closing keytab"));
257 int add_principal(void *lhandle
, char *keytab_str
, krb5_keytab keytab
,
258 krb5_boolean keepold
, int n_ks_tuple
,
259 krb5_key_salt_tuple
*ks_tuple
,
262 kadm5_principal_ent_rec princ_rec
;
263 krb5_principal princ
;
264 krb5_keytab_entry new_entry
;
268 krb5_key_salt_tuple
*permitted_etypes
= NULL
;
270 (void) memset((char *)&princ_rec
, 0, sizeof(princ_rec
));
276 code
= krb5_parse_name(context
, princ_str
, &princ
);
278 com_err(whoami
, code
,
279 gettext("while parsing -add principal name %s"),
284 if (ks_tuple
== NULL
) {
285 krb5_enctype
*ptr
, *ktypes
= NULL
;
287 code
= krb5_get_permitted_enctypes(context
, &ktypes
);
288 if (!code
&& ktypes
&& *ktypes
) {
291 * Count the results. This is stupid, the API above
292 * should have included an output param to indicate
293 * the size of the list that is returned.
295 for (ptr
= ktypes
; *ptr
; ptr
++) nktypes
++;
297 /* Allocate a new key-salt tuple set */
298 permitted_etypes
= (krb5_key_salt_tuple
*)malloc (
299 sizeof (krb5_key_salt_tuple
) * nktypes
);
300 if (permitted_etypes
== NULL
) {
306 * Because the keysalt parameter doesn't matter for
307 * keys stored in the keytab, use the default "normal"
310 (void) krb5_string_to_salttype("normal", &salttype
);
311 for (i
= 0; i
< nktypes
; i
++) {
312 permitted_etypes
[i
].ks_enctype
= ktypes
[i
];
313 permitted_etypes
[i
].ks_salttype
= salttype
;
321 permitted_etypes
= ks_tuple
;
322 nktypes
= n_ks_tuple
;
325 code
= kadm5_randkey_principal_3(lhandle
, princ
,
326 keepold
, nktypes
, permitted_etypes
,
329 #ifndef _KADMIN_LOCAL_
330 /* this block is not needed in the kadmin.local client */
333 * If the above call failed, we may be talking to an older
334 * admin server, so try the older API.
336 if (code
== KADM5_RPC_ERROR
) {
337 code
= kadm5_randkey_principal_old(handle
, princ
, &keys
, &nkeys
);
339 #endif /* !KADMIN_LOCAL */
341 if (code
== KADM5_UNK_PRINC
) {
343 gettext("%s: Principal %s does not exist.\n"),
345 /* Solaris Kerberos: Better error messages */
346 } else if (code
== KRB5_BAD_ENCTYPE
) {
348 fprintf(stderr
, gettext("%s: Error from the remote system: "
349 "%s while changing %s's key\n"), whoami
,
350 error_message(code
), princ_str
);
352 et
= permitted_etypes
[0].ks_enctype
;
353 fprintf(stderr
, gettext("%s: Encryption types "
354 "requested: %s (%d)"), whoami
,
355 etype_istring(et
), et
);
357 for (i
= 1; i
< nktypes
; i
++) {
358 et
= permitted_etypes
[i
].ks_enctype
;
359 fprintf(stderr
, ", %s (%d)",
360 etype_istring(et
), et
);
362 fprintf(stderr
, "\n");
365 com_err(whoami
, code
,
366 gettext("while changing %s's key"),
372 code
= kadm5_get_principal(lhandle
, princ
, &princ_rec
,
373 KADM5_PRINCIPAL_NORMAL_MASK
);
375 com_err(whoami
, code
, gettext("while retrieving principal"));
379 for (i
= 0; i
< nkeys
; i
++) {
380 memset((char *) &new_entry
, 0, sizeof(new_entry
));
381 new_entry
.principal
= princ
;
382 new_entry
.key
= keys
[i
];
383 new_entry
.vno
= princ_rec
.kvno
;
385 code
= krb5_kt_add_entry(context
, keytab
, &new_entry
);
387 com_err(whoami
, code
,
388 gettext("while adding key to keytab"));
389 (void) kadm5_free_principal_ent(lhandle
, &princ_rec
);
394 printf(gettext("Entry for principal %s with kvno %d, "
395 "encryption type %s added to keytab %s.\n"),
396 princ_str
, princ_rec
.kvno
,
397 etype_string(keys
[i
].enctype
), keytab_str
);
400 code
= kadm5_free_principal_ent(lhandle
, &princ_rec
);
402 com_err(whoami
, code
, gettext("while freeing principal entry"));
408 for (i
= 0; i
< nkeys
; i
++)
409 krb5_free_keyblock_contents(context
, &keys
[i
]);
413 krb5_free_principal(context
, princ
);
415 if (permitted_etypes
!= NULL
&& ks_tuple
== NULL
)
416 free(permitted_etypes
);
421 int remove_principal(char *keytab_str
, krb5_keytab keytab
, char
422 *princ_str
, char *kvno_str
)
424 krb5_principal princ
;
425 krb5_keytab_entry entry
;
426 krb5_kt_cursor cursor
;
427 enum { UNDEF
, SPEC
, HIGH
, ALL
, OLD
} mode
;
428 int code
, did_something
;
431 code
= krb5_parse_name(context
, princ_str
, &princ
);
433 com_err(whoami
, code
,
434 gettext("while parsing principal name %s"),
440 if (kvno_str
== NULL
) {
443 } else if (strcmp(kvno_str
, "all") == 0) {
446 } else if (strcmp(kvno_str
, "old") == 0) {
451 kvno
= atoi(kvno_str
);
454 /* kvno is set to specified value for SPEC, 0 otherwise */
455 code
= krb5_kt_get_entry(context
, keytab
, princ
, kvno
, 0, &entry
);
457 if (code
== ENOENT
) {
459 gettext("%s: Keytab %s does not exist.\n"),
461 } else if (code
== KRB5_KT_NOTFOUND
) {
464 gettext("%s: No entry for principal "
465 "%s exists in keytab %s\n"),
466 whoami
, princ_str
, keytab_str
);
469 gettext("%s: No entry for principal "
470 "%s with kvno %d exists in "
472 whoami
, princ_str
, kvno
, keytab_str
);
474 com_err(whoami
, code
,
475 gettext("while retrieving highest "
476 "kvno from keytab"));
481 /* set kvno to spec'ed value for SPEC, highest kvno otherwise */
483 krb5_kt_free_entry(context
, &entry
);
485 code
= krb5_kt_start_seq_get(context
, keytab
, &cursor
);
487 com_err(whoami
, code
, gettext("while starting keytab scan"));
492 while ((code
= krb5_kt_next_entry(context
, keytab
, &entry
, &cursor
)) == 0) {
493 if (krb5_principal_compare(context
, princ
, entry
.principal
) &&
495 (mode
== SPEC
&& entry
.vno
== kvno
) ||
496 (mode
== OLD
&& entry
.vno
!= kvno
) ||
497 (mode
== HIGH
&& entry
.vno
== kvno
))) {
500 * Ack! What a kludge... the scanning functions lock
501 * the keytab so entries cannot be removed while they
504 code
= krb5_kt_end_seq_get(context
, keytab
, &cursor
);
506 com_err(whoami
, code
,
507 gettext("while temporarily "
508 "ending keytab scan"));
511 code
= krb5_kt_remove_entry(context
, keytab
, &entry
);
513 com_err(whoami
, code
,
514 gettext("while deleting entry "
518 code
= krb5_kt_start_seq_get(context
, keytab
, &cursor
);
520 com_err(whoami
, code
,
521 gettext("while restarting keytab scan"));
527 printf(gettext("Entry for principal "
529 "removed from keytab %s.\n"),
530 princ_str
, entry
.vno
, keytab_str
);
532 krb5_kt_free_entry(context
, &entry
);
534 if (code
&& code
!= KRB5_KT_END
) {
535 com_err(whoami
, code
, gettext("while scanning keytab"));
538 if ((code
= krb5_kt_end_seq_get(context
, keytab
, &cursor
))) {
539 com_err(whoami
, code
, gettext("while ending keytab scan"));
544 * If !did_someting then mode must be OLD or we would have
545 * already returned with an error. But check it anyway just to
546 * prevent unexpected error messages...
548 if (!did_something
&& mode
== OLD
) {
550 gettext("%s: There is only one entry for principal "
551 "%s in keytab %s\n"),
552 whoami
, princ_str
, keytab_str
);
560 * etype_string(enctype): return a string representation of the
561 * encryption type. XXX copied from klist.c; this should be a
562 * library function, or perhaps just #defines
564 static char *etype_string(enctype
)
565 krb5_enctype enctype
;
567 static char buf
[100];
570 if ((ret
= krb5_enctype_to_string(enctype
, buf
, sizeof(buf
))))
571 sprintf(buf
, "etype %d", enctype
);
576 /* Solaris Kerberos */
577 static char *etype_istring(krb5_enctype enctype
) {
578 static char buf
[100];
581 if ((ret
= krb5_enctype_to_istring(enctype
, buf
, sizeof(buf
))))
582 sprintf(buf
, "unknown", enctype
);