1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
3 * Copyright 1994, 2008 by the Massachusetts Institute of Technology.
6 * Export of this software from the United States of America may
7 * require a specific license from the United States Government.
8 * It is the responsibility of any person or organization contemplating
9 * export to obtain such a license before exporting.
11 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
12 * distribute this software and its documentation for any purpose and
13 * without fee is hereby granted, provided that the above copyright
14 * notice appear in all copies and that both that copyright notice and
15 * this permission notice appear in supporting documentation, and that
16 * the name of M.I.T. not be used in advertising or publicity pertaining
17 * to distribution of the software without specific, written prior
18 * permission. Furthermore if you modify this software you must label
19 * your software as modified software and not distribute it in such a
20 * fashion that it might be confused with the original M.I.T. software.
21 * M.I.T. makes no representations about the suitability of
22 * this software for any purpose. It is provided "as is" without express
23 * or implied warranty.
26 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
30 /* Base functions for a kadmin command line interface using the OVSecure
35 #include <kadm5/admin.h>
36 #include <adm_proto.h>
41 #include <sys/types.h>
45 /* #include <sys/timeb.h> */
49 static krb5_boolean script_mode = FALSE;
51 char *def_realm = NULL;
56 char *ccache_name = NULL;
61 info(const char *fmt, ...)
62 #if !defined(__cplusplus) && (__GNUC__ > 2)
63 __attribute__((__format__(__printf__, 1, 2)))
68 error(const char *fmt, ...)
69 #if !defined(__cplusplus) && (__GNUC__ > 2)
70 __attribute__((__format__(__printf__, 1, 2)))
74 /* Like printf, but suppressed if script_mode is set. */
76 info(const char *fmt, ...)
87 /* Like fprintf to stderr; also set exit_status if script_mode is set. */
89 error(const char *fmt, ...)
96 vfprintf(stderr, fmt, ap);
103 error(_("Usage: %s [-r realm] [-p principal] [-q query] "
104 "[clnt|local args]\n"
105 " [command args...]\n"
106 "\tclnt args: [-s admin_server[:port]] "
107 "[[-c ccache]|[-k [-t keytab]]]|[-n] [-O | -N]\n"
108 "\tlocal args: [-x db_args]* [-d dbname] "
109 "[-e \"enc:salt ...\"] [-m] [-w password] "
110 "where,\n\t[-x db_args]* - any number of database specific "
112 "\t\t\tLook at each database documentation for supported "
113 "arguments\n"), whoami);
118 strdur(time_t duration)
121 int neg, days, hours, minutes, seconds;
128 days = duration / (24 * 3600);
129 duration %= 24 * 3600;
130 hours = duration / 3600;
132 minutes = duration / 60;
135 snprintf(out, sizeof(out), "%s%d %s %02d:%02d:%02d", neg ? "-" : "",
136 days, days == 1 ? "day" : "days",
137 hours, minutes, seconds);
142 strdate(krb5_timestamp when)
146 time_t lcltim = ts2tt(when);
148 tm = localtime(&lcltim);
150 strftime(out, sizeof(out), "%a %b %d %H:%M:%S %Z %Y", tm) == 0)
151 strlcpy(out, "(error)", sizeof(out));
155 /* Parse a date string using getdate.y. On failure, output an error message
156 * and return (time_t)-1. */
158 parse_date(char *str, time_t now)
162 date = get_date_rel(str, now);
163 if (date == (time_t)-1)
164 error(_("Invalid date specification \"%s\".\n"), str);
169 * Parse a time interval. Use krb5_string_to_deltat() if it works; otherwise
170 * use getdate.y and subtract now, with sanity checks. On failure, output an
171 * error message and return (time_t)-1.
174 parse_interval(char *str, time_t now)
179 if (krb5_string_to_deltat(str, &delta) == 0)
182 date = parse_date(str, now);
183 if (date == (time_t)-1)
186 /* Interpret an absolute time of 0 (e.g. "never") as an interval of 0. */
190 /* Don't return a negative interval if the date is in the past. */
192 error(_("Interval specification \"%s\" is in the past.\n"), str);
199 /* this is a wrapper to go around krb5_parse_principal so we can set
200 the default realm up properly */
201 static krb5_error_code
202 kadmin_parse_name(char *name, krb5_principal *principal)
205 krb5_error_code retval;
208 /* assumes def_realm is initialized! */
209 cp = strchr(name, '@');
211 if (cp - name && *(cp - 1) != '\\')
214 cp = strchr(cp + 1, '@');
217 result = asprintf(&fullname, "%s@%s", name, def_realm);
219 result = asprintf(&fullname, "%s", name);
222 retval = krb5_parse_name(context, fullname, principal);
228 extended_com_err_fn(const char *myprog, errcode_t code,
229 const char *fmt, va_list args)
234 emsg = krb5_get_error_message(context, code);
235 error("%s: %s ", myprog, emsg);
236 krb5_free_error_message(context, emsg);
238 error("%s: ", myprog);
240 vfprintf(stderr, fmt, args);
244 /* Create a principal using the oldest appropriate kadm5 API. */
245 static krb5_error_code
246 create_princ(kadm5_principal_ent_rec *princ, long mask, int n_ks,
247 krb5_key_salt_tuple *ks, char *pass)
250 return kadm5_create_principal_3(handle, princ, mask, n_ks, ks, pass);
252 return kadm5_create_principal(handle, princ, mask, pass);
255 /* Randomize a principal's password using the appropriate kadm5 API. */
257 randkey_princ(void *lhandle, krb5_principal princ, krb5_boolean keepold,
258 int n_ks, krb5_key_salt_tuple *ks, krb5_keyblock **key,
263 /* Try the newer API first, because the Solaris kadmind only creates DES
264 * keys when the old API is used. */
265 ret = kadm5_randkey_principal_3(lhandle, princ, keepold, n_ks, ks, key,
268 /* Fall back to the old version if we get an error and aren't using any new
270 if (ret == KADM5_RPC_ERROR && !keepold && ks == NULL)
271 ret = kadm5_randkey_principal(lhandle, princ, key, n_keys);
277 policy_exists(const char *name)
279 kadm5_policy_ent_rec pol;
281 if (kadm5_get_policy(handle, (char *)name, &pol) != 0)
283 kadm5_free_policy_ent(handle, &pol);
288 kadmin_startup(int argc, char *argv[], char **request_out, char ***args_out)
291 char *princstr = NULL, *keytab_name = NULL, *query = NULL;
292 char *password = NULL;
293 char *luser, *canon, *cp;
294 int optchar, freeprinc = 0, use_keytab = 0, use_anonymous = 0;
298 krb5_principal princ;
299 kadm5_config_params params;
300 char **db_args = NULL;
301 int db_args_size = 0;
302 char *db_name = NULL;
303 char *svcname, *realm;
305 memset(¶ms, 0, sizeof(params));
307 set_com_err_hook(extended_com_err_fn);
309 retval = kadm5_init_krb5_context(&context);
311 com_err(whoami, retval, _("while initializing krb5 library"));
315 while ((optchar = getopt(argc, argv,
316 "+x:r:p:knq:w:d:s:mc:t:e:ON")) != EOF) {
320 db_args = realloc(db_args, sizeof(char*) * (db_args_size + 1));
321 if (db_args == NULL) {
322 error(_("%s: Cannot initialize. Not enough memory\n"), whoami);
325 db_args[db_args_size - 1] = optarg;
326 db_args[db_args_size] = NULL;
336 ccache_name = optarg;
345 keytab_name = optarg;
354 /* db_name has to be passed as part of the db_args. */
356 asprintf(&db_name, "dbname=%s", optarg);
359 db_args = realloc(db_args, sizeof(char*) * (db_args_size + 1));
360 if (db_args == NULL) {
361 error(_("%s: Cannot initialize. Not enough memory\n"), whoami);
364 db_args[db_args_size - 1] = db_name;
365 db_args[db_args_size] = NULL;
368 params.admin_server = optarg;
369 params.mask |= KADM5_CONFIG_ADMIN_SERVER;
372 params.mkey_from_kbd = 1;
373 params.mask |= KADM5_CONFIG_MKEY_FROM_KBD;
376 retval = krb5_string_to_keysalts(optarg, NULL, NULL, 0,
378 ¶ms.num_keysalts);
380 com_err(whoami, retval, _("while parsing keysalts %s"),
384 params.mask |= KADM5_CONFIG_ENCTYPES;
387 params.mask |= KADM5_CONFIG_OLD_AUTH_GSSAPI;
390 params.mask |= KADM5_CONFIG_AUTH_NOFALLBACK;
396 if ((ccache_name && use_keytab) ||
397 (keytab_name && !use_keytab) ||
398 (ccache_name && use_anonymous) ||
399 (use_anonymous && use_keytab))
402 if (query != NULL && argv[optind] != NULL) {
403 error(_("%s: -q is exclusive with command-line query"), whoami);
407 if (argv[optind] != NULL)
410 if (def_realm == NULL && krb5_get_default_realm(context, &def_realm)) {
411 error(_("%s: unable to get default realm\n"), whoami);
415 params.mask |= KADM5_CONFIG_REALM;
416 params.realm = def_realm;
418 if (params.mask & KADM5_CONFIG_OLD_AUTH_GSSAPI)
419 svcname = KADM5_ADMIN_SERVICE;
424 * Set cc to an open credentials cache, either specified by the -c
425 * argument or the default.
427 if (ccache_name == NULL) {
428 retval = krb5_cc_default(context, &cc);
430 com_err(whoami, retval,
431 _("while opening default credentials cache"));
435 retval = krb5_cc_resolve(context, ccache_name, &cc);
437 com_err(whoami, retval, _("while opening credentials cache %s"),
444 * If no principal name is specified: If authenticating anonymously, use
445 * the anonymous principal for the local realm, else if a ccache was
446 * specified and its primary principal name can be read, it is used, else
447 * if a keytab was specified, the principal name is host/hostname,
448 * otherwise append "/admin" to the primary name of the default ccache,
451 * Gee, 100+ lines to figure out the client principal name. This
452 * should be compressed...
455 if (princstr == NULL) {
457 if (asprintf(&princstr, "%s/%s@%s", KRB5_WELLKNOWN_NAMESTR,
458 KRB5_ANONYMOUS_PRINCSTR, def_realm) < 0) {
459 error(_("%s: out of memory\n"), whoami);
463 } else if (ccache_name != NULL &&
464 !krb5_cc_get_principal(context, cc, &princ)) {
465 retval = krb5_unparse_name(context, princ, &princstr);
467 com_err(whoami, retval,
468 _("while canonicalizing principal name"));
471 krb5_free_principal(context, princ);
473 } else if (use_keytab != 0) {
474 retval = krb5_sname_to_principal(context, NULL, "host",
475 KRB5_NT_SRV_HST, &princ);
477 com_err(whoami, retval, _("creating host service principal"));
480 retval = krb5_unparse_name(context, princ, &princstr);
482 com_err(whoami, retval,
483 _("while canonicalizing principal name"));
486 krb5_free_principal(context, princ);
488 } else if (!krb5_cc_get_principal(context, cc, &princ)) {
489 if (krb5_unparse_name(context, princ, &canon)) {
490 error(_("%s: unable to canonicalize principal\n"), whoami);
493 /* Strip out realm of principal if it's there. */
494 realm = strchr(canon, '@');
496 if (realm > canon && *(realm - 1) != '\\')
498 realm = strchr(realm + 1, '@');
502 cp = strchr(canon, '/');
504 if (cp > canon && *(cp - 1) != '\\')
506 cp = strchr(cp + 1, '/');
510 if (asprintf(&princstr, "%s/admin%s%s", canon,
512 (realm) ? realm : "") < 0) {
513 error(_("%s: out of memory\n"), whoami);
517 krb5_free_principal(context, princ);
519 } else if ((luser = getenv("USER"))) {
520 if (asprintf(&princstr, "%s/admin@%s", luser, def_realm) < 0) {
521 error(_("%s: out of memory\n"), whoami);
525 } else if ((pw = getpwuid(getuid()))) {
526 if (asprintf(&princstr, "%s/admin@%s", pw->pw_name,
528 error(_("%s: out of memory\n"), whoami);
533 error(_("%s: unable to figure out a principal name\n"), whoami);
538 retval = krb5_klog_init(context, "admin_server", whoami, 0);
540 com_err(whoami, retval, _("while setting up logging"));
545 * Initialize the kadm5 connection. If we were given a ccache,
546 * use it. Otherwise, use/prompt for the password.
549 info(_("Authenticating as principal %s with existing "
550 "credentials.\n"), princstr);
551 retval = kadm5_init_with_creds(context, princstr, cc, svcname, ¶ms,
552 KADM5_STRUCT_VERSION,
553 KADM5_API_VERSION_4, db_args, &handle);
554 } else if (use_anonymous) {
555 info(_("Authenticating as principal %s with password; "
556 "anonymous requested.\n"), princstr);
557 retval = kadm5_init_anonymous(context, princstr, svcname, ¶ms,
558 KADM5_STRUCT_VERSION,
559 KADM5_API_VERSION_4, db_args, &handle);
560 } else if (use_keytab) {
561 if (keytab_name != NULL) {
562 info(_("Authenticating as principal %s with keytab %s.\n"),
563 princstr, keytab_name);
565 info(_("Authenticating as principal %s with default keytab.\n"),
568 retval = kadm5_init_with_skey(context, princstr, keytab_name, svcname,
569 ¶ms, KADM5_STRUCT_VERSION,
570 KADM5_API_VERSION_4, db_args, &handle);
572 info(_("Authenticating as principal %s with password.\n"),
574 retval = kadm5_init_with_password(context, princstr, password, svcname,
575 ¶ms, KADM5_STRUCT_VERSION,
576 KADM5_API_VERSION_4, db_args,
580 com_err(whoami, retval, _("while initializing %s interface"), whoami);
581 if (retval == KADM5_BAD_CLIENT_PARAMS ||
582 retval == KADM5_BAD_SERVER_PARAMS)
589 free(params.keysalts);
593 retval = krb5_cc_close(context, cc);
595 com_err(whoami, retval, _("while closing ccache %s"), ccache_name);
599 retval = kadm5_init_iprop(handle, 0);
601 com_err(whoami, retval, _("while mapping update log"));
605 *request_out = query;
606 *args_out = argv + optind;
615 retval = kadm5_unlock(handle);
617 com_err("quit", retval, _("while unlocking locked database"));
623 kadm5_destroy(handle);
624 if (ccache_name != NULL && !script_mode) {
625 fprintf(stderr, "\n\a\a\a%s",
626 _("Administration credentials NOT DESTROYED.\n"));
629 /* insert more random cleanup here */
630 krb5_klog_close(context);
631 krb5_free_context(context);
636 kadmin_lock(int argc, char *argv[])
642 retval = kadm5_lock(handle);
644 com_err("lock", retval, "");
651 kadmin_unlock(int argc, char *argv[])
657 retval = kadm5_unlock(handle);
659 com_err("unlock", retval, "");
666 kadmin_delprinc(int argc, char *argv[])
669 krb5_principal princ = NULL;
674 (argc == 3 && !strcmp("-force", argv[1])))) {
675 error(_("usage: delete_principal [-force] principal\n"));
678 retval = kadmin_parse_name(argv[argc - 1], &princ);
680 com_err("delete_principal", retval, _("while parsing principal name"));
683 retval = krb5_unparse_name(context, princ, &canon);
685 com_err("delete_principal", retval,
686 _("while canonicalizing principal"));
689 if (argc == 2 && !script_mode) {
690 printf(_("Are you sure you want to delete the principal \"%s\"? "
691 "(yes/no): "), canon);
692 fgets(reply, sizeof (reply), stdin);
693 if (strcmp("yes\n", reply)) {
694 fprintf(stderr, _("Principal \"%s\" not deleted\n"), canon);
698 retval = kadm5_delete_principal(handle, princ);
700 com_err("delete_principal", retval,
701 _("while deleting principal \"%s\""), canon);
704 info(_("Principal \"%s\" deleted.\n"), canon);
705 info(_("Make sure that you have removed this principal from all ACLs "
706 "before reusing.\n"));
709 krb5_free_principal(context, princ);
714 kadmin_renameprinc(int argc, char *argv[])
717 krb5_principal oprinc = NULL, nprinc = NULL;
718 char *ocanon = NULL, *ncanon = NULL;
721 if (!(argc == 3 || (argc == 4 && !strcmp("-force", argv[1])))) {
722 error(_("usage: rename_principal [-force] old_principal "
726 retval = kadmin_parse_name(argv[argc - 2], &oprinc);
728 com_err("rename_principal", retval,
729 _("while parsing old principal name"));
732 retval = kadmin_parse_name(argv[argc - 1], &nprinc);
734 com_err("rename_principal", retval,
735 _("while parsing new principal name"));
738 retval = krb5_unparse_name(context, oprinc, &ocanon);
740 com_err("rename_principal", retval,
741 _("while canonicalizing old principal"));
744 retval = krb5_unparse_name(context, nprinc, &ncanon);
746 com_err("rename_principal", retval,
747 _("while canonicalizing new principal"));
750 if (argc == 3 && !script_mode) {
751 printf(_("Are you sure you want to rename the principal \"%s\" "
752 "to \"%s\"? (yes/no): "), ocanon, ncanon);
753 fgets(reply, sizeof(reply), stdin);
754 if (strcmp("yes\n", reply)) {
755 fprintf(stderr, _("Principal \"%s\" not renamed\n"), ocanon);
759 retval = kadm5_rename_principal(handle, oprinc, nprinc);
761 com_err("rename_principal", retval,
762 _("while renaming principal \"%s\" to \"%s\""),
766 info(_("Principal \"%s\" renamed to \"%s\".\n"), ocanon, ncanon);
767 info(_("Make sure that you have removed the old principal from all ACLs "
768 "before reusing.\n"));
771 krb5_free_principal(context, nprinc);
772 krb5_free_principal(context, oprinc);
778 cpw_usage(const char *str)
782 error(_("usage: change_password [-randkey] [-keepold] "
783 "[-e keysaltlist] [-pw password] principal\n"));
787 kadmin_cpw(int argc, char *argv[])
790 static char newpw[1024];
791 static char prompt1[1024], prompt2[1024];
792 char *canon = NULL, *pwarg = NULL;
793 int n_ks_tuple = 0, randkey = 0;
794 krb5_boolean keepold = FALSE;
795 krb5_key_salt_tuple *ks_tuple = NULL;
796 krb5_principal princ = NULL;
797 char **db_args = NULL;
798 int db_args_size = 0;
804 for (argv++, argc--; argc > 0 && **argv == '-'; argc--, argv++) {
805 if (!strcmp("-x", *argv)) {
808 cpw_usage(_("change_password: missing db argument"));
812 db_args = realloc(db_args, sizeof(char*) * (db_args_size + 1));
813 if (db_args == NULL) {
814 error(_("change_password: Not enough memory\n"));
817 db_args[db_args_size - 1] = *++argv;
818 db_args[db_args_size] = NULL;
819 } else if (!strcmp("-pw", *argv)) {
822 cpw_usage(_("change_password: missing password arg"));
826 } else if (!strcmp("-randkey", *argv)) {
828 } else if (!strcmp("-keepold", *argv)) {
830 } else if (!strcmp("-e", *argv)) {
833 cpw_usage(_("change_password: missing keysaltlist arg"));
836 retval = krb5_string_to_keysalts(*++argv, NULL, NULL, 0,
837 &ks_tuple, &n_ks_tuple);
839 com_err("change_password", retval,
840 _("while parsing keysalts %s"), *argv);
844 com_err("change_password", 0, _("unrecognized option %s"), *argv);
851 com_err("change_password", 0, _("missing principal name"));
853 com_err("change_password", 0, _("too many arguments"));
857 retval = kadmin_parse_name(*argv, &princ);
859 com_err("change_password", retval, _("while parsing principal name"));
862 retval = krb5_unparse_name(context, princ, &canon);
864 com_err("change_password", retval,
865 _("while canonicalizing principal"));
869 if (keepold || ks_tuple != NULL) {
870 retval = kadm5_chpass_principal_3(handle, princ, keepold,
871 n_ks_tuple, ks_tuple, pwarg);
873 retval = kadm5_chpass_principal(handle, princ, pwarg);
876 com_err("change_password", retval,
877 _("while changing password for \"%s\"."), canon);
880 info(_("Password for \"%s\" changed.\n"), canon);
881 } else if (randkey) {
882 retval = randkey_princ(handle, princ, keepold, n_ks_tuple, ks_tuple,
885 com_err("change_password", retval,
886 _("while randomizing key for \"%s\"."), canon);
889 info(_("Key for \"%s\" randomized.\n"), canon);
891 unsigned int i = sizeof (newpw) - 1;
893 snprintf(prompt1, sizeof(prompt1),
894 _("Enter password for principal \"%s\""), canon);
895 snprintf(prompt2, sizeof(prompt2),
896 _("Re-enter password for principal \"%s\""), canon);
897 retval = krb5_read_password(context, prompt1, prompt2,
900 com_err("change_password", retval,
901 _("while reading password for \"%s\"."), canon);
904 if (keepold || ks_tuple != NULL) {
905 retval = kadm5_chpass_principal_3(handle, princ, keepold,
906 n_ks_tuple, ks_tuple,
909 retval = kadm5_chpass_principal(handle, princ, newpw);
911 memset(newpw, 0, sizeof (newpw));
913 com_err("change_password", retval,
914 _("while changing password for \"%s\"."), canon);
917 info(_("Password for \"%s\" changed.\n"), canon);
922 krb5_free_principal(context, princ);
927 kadmin_free_tl_data(krb5_int16 *n_tl_datap, krb5_tl_data **tl_datap)
929 krb5_tl_data *tl_data = *tl_datap, *next;
930 int n_tl_data = *n_tl_datap;
936 for (i = 0; tl_data && (i < n_tl_data); i++) {
937 next = tl_data->tl_data_next;
938 free(tl_data->tl_data_contents);
944 /* Construct a tl_data element and add it to the tail of *tl_datap. */
946 add_tl_data(krb5_int16 *n_tl_datap, krb5_tl_data **tl_datap,
947 krb5_int16 tl_type, krb5_ui_2 len, krb5_octet *contents)
949 krb5_tl_data *tl_data;
953 tl_data = calloc(1, sizeof(*tl_data));
954 if (copy == NULL || tl_data == NULL) {
955 error(_("Not enough memory\n"));
958 memcpy(copy, contents, len);
960 tl_data->tl_data_type = tl_type;
961 tl_data->tl_data_length = len;
962 tl_data->tl_data_contents = copy;
963 tl_data->tl_data_next = NULL;
965 for (; *tl_datap != NULL; tl_datap = &(*tl_datap)->tl_data_next);
971 unlock_princ(kadm5_principal_ent_t princ, long *mask, const char *caller)
973 krb5_error_code retval;
975 krb5_octet timebuf[4];
977 /* Zero out the failed auth count. */
978 princ->fail_auth_count = 0;
979 *mask |= KADM5_FAIL_AUTH_COUNT;
981 /* Record the timestamp of this unlock operation so that replica KDCs will
982 * see it, since fail_auth_count is unreplicated. */
983 retval = krb5_timeofday(context, &now);
985 com_err(caller, retval, _("while getting time"));
988 store_32_le((krb5_int32)now, timebuf);
989 add_tl_data(&princ->n_tl_data, &princ->tl_data,
990 KRB5_TL_LAST_ADMIN_UNLOCK, 4, timebuf);
991 *mask |= KADM5_TL_DATA;
995 * Parse addprinc or modprinc arguments. Some output fields may be
996 * filled in on error.
999 kadmin_parse_princ_args(int argc, char *argv[], kadm5_principal_ent_t oprinc,
1000 long *mask, char **pass, krb5_boolean *randkey,
1001 krb5_boolean *nokey, krb5_key_salt_tuple **ks_tuple,
1002 int *n_ks_tuple, char *caller)
1005 time_t now, date, interval;
1006 krb5_error_code retval;
1015 for (i = 1; i < argc - 1; i++) {
1016 if (!strcmp("-x",argv[i])) {
1020 add_tl_data(&oprinc->n_tl_data, &oprinc->tl_data,
1021 KRB5_TL_DB_ARGS, strlen(argv[i]) + 1,
1022 (krb5_octet *)argv[i]);
1023 *mask |= KADM5_TL_DATA;
1026 if (!strcmp("-expire", argv[i])) {
1029 date = parse_date(argv[i], now);
1030 if (date == (time_t)-1)
1032 oprinc->princ_expire_time = date;
1033 *mask |= KADM5_PRINC_EXPIRE_TIME;
1036 if (!strcmp("-pwexpire", argv[i])) {
1039 date = parse_date(argv[i], now);
1040 if (date == (time_t)-1)
1042 oprinc->pw_expiration = date;
1043 *mask |= KADM5_PW_EXPIRATION;
1046 if (!strcmp("-maxlife", argv[i])) {
1049 interval = parse_interval(argv[i], now);
1050 if (interval == (time_t)-1)
1052 oprinc->max_life = interval;
1053 *mask |= KADM5_MAX_LIFE;
1056 if (!strcmp("-maxrenewlife", argv[i])) {
1059 interval = parse_interval(argv[i], now);
1060 if (interval == (time_t)-1)
1062 oprinc->max_renewable_life = interval;
1063 *mask |= KADM5_MAX_RLIFE;
1066 if (!strcmp("-kvno", argv[i])) {
1069 oprinc->kvno = atoi(argv[i]);
1070 *mask |= KADM5_KVNO;
1073 if (!strcmp("-policy", argv[i])) {
1076 oprinc->policy = argv[i];
1077 *mask |= KADM5_POLICY;
1080 if (!strcmp("-clearpolicy", argv[i])) {
1081 oprinc->policy = NULL;
1082 *mask |= KADM5_POLICY_CLR;
1085 if (!strcmp("-pw", argv[i])) {
1091 if (!strcmp("-randkey", argv[i])) {
1095 if (!strcmp("-nokey", argv[i])) {
1099 if (!strcmp("-unlock", argv[i])) {
1100 unlock_princ(oprinc, mask, caller);
1103 if (!strcmp("-e", argv[i])) {
1106 retval = krb5_string_to_keysalts(argv[i], NULL, NULL, 0,
1107 ks_tuple, n_ks_tuple);
1109 com_err(caller, retval, _("while parsing keysalts %s"),
1115 retval = krb5_flagspec_to_mask(argv[i], &oprinc->attributes,
1116 &oprinc->attributes);
1120 *mask |= KADM5_ATTRIBUTES;
1124 retval = kadmin_parse_name(argv[i], &oprinc->principal);
1126 com_err(caller, retval, _("while parsing principal"));
1133 kadmin_addprinc_usage()
1135 error(_("usage: add_principal [options] principal\n"));
1136 error(_("\toptions are:\n"));
1137 error(_("\t\t[-randkey|-nokey] [-x db_princ_args]* [-expire expdate] "
1138 "[-pwexpire pwexpdate] [-maxlife maxtixlife]\n"
1139 "\t\t[-kvno kvno] [-policy policy] [-clearpolicy]\n"
1140 "\t\t[-pw password] [-maxrenewlife maxrenewlife]\n"
1141 "\t\t[-e keysaltlist]\n\t\t[{+|-}attribute]\n"));
1142 error(_("\tattributes are:\n"));
1143 error(_("\t\tallow_postdated allow_forwardable allow_tgs_req "
1145 "\t\tallow_proxiable allow_dup_skey allow_tix requires_preauth\n"
1146 "\t\trequires_hwauth needchange allow_svr "
1147 "password_changing_service\n"
1148 "\t\tok_as_delegate ok_to_auth_as_delegate no_auth_data_required\n"
1149 "\t\tlockdown_keys\n"
1150 "\nwhere,\n\t[-x db_princ_args]* - any number of database "
1151 "specific arguments.\n"
1152 "\t\t\tLook at each database documentation for supported "
1157 kadmin_modprinc_usage()
1159 error(_("usage: modify_principal [options] principal\n"));
1160 error(_("\toptions are:\n"));
1161 error(_("\t\t[-x db_princ_args]* [-expire expdate] "
1162 "[-pwexpire pwexpdate] [-maxlife maxtixlife]\n"
1163 "\t\t[-kvno kvno] [-policy policy] [-clearpolicy]\n"
1164 "\t\t[-maxrenewlife maxrenewlife] [-unlock] [{+|-}attribute]\n"));
1165 error(_("\tattributes are:\n"));
1166 error(_("\t\tallow_postdated allow_forwardable allow_tgs_req "
1168 "\t\tallow_proxiable allow_dup_skey allow_tix requires_preauth\n"
1169 "\t\trequires_hwauth needchange allow_svr "
1170 "password_changing_service\n"
1171 "\t\tok_as_delegate ok_to_auth_as_delegate no_auth_data_required\n"
1172 "\t\tlockdown_keys\n"
1173 "\nwhere,\n\t[-x db_princ_args]* - any number of database "
1174 "specific arguments.\n"
1175 "\t\t\tLook at each database documentation for supported "
1179 /* Create a dummy password for old-style (pre-1.8) randkey creation. */
1181 prepare_dummy_password(char *buf, size_t sz)
1185 /* Must try to pass any password policy in place, and be valid UTF-8. */
1186 strlcpy(buf, "6F a[", sz);
1187 for (i = strlen(buf); i < sz - 1; i++)
1188 buf[i] = 'a' + (i % 26);
1193 kadmin_addprinc(int argc, char *argv[])
1195 kadm5_principal_ent_rec princ;
1197 krb5_boolean randkey = FALSE, nokey = FALSE, old_style_randkey = FALSE;
1199 krb5_key_salt_tuple *ks_tuple = NULL;
1200 char *pass, *canon = NULL;
1201 krb5_error_code retval;
1202 char newpw[1024], dummybuf[256];
1203 static char prompt1[1024], prompt2[1024];
1205 /* Zero all fields in request structure */
1206 memset(&princ, 0, sizeof(princ));
1208 princ.attributes = 0;
1209 if (kadmin_parse_princ_args(argc, argv, &princ, &mask, &pass, &randkey,
1210 &nokey, &ks_tuple, &n_ks_tuple,
1212 kadmin_addprinc_usage();
1216 retval = krb5_unparse_name(context, princ.principal, &canon);
1218 com_err("add_principal", retval, _("while canonicalizing principal"));
1222 if (mask & KADM5_POLICY) {
1223 /* Warn if the specified policy does not exist. */
1224 if (!script_mode && !policy_exists(princ.policy)) {
1225 fprintf(stderr, _("WARNING: policy \"%s\" does not exist\n"),
1228 } else if (!(mask & KADM5_POLICY_CLR)) {
1229 /* If the policy "default" exists, assign it. */
1230 if (policy_exists("default")) {
1232 fprintf(stderr, _("No policy specified for %s; "
1233 "assigning \"default\"\n"), canon);
1235 princ.policy = "default";
1236 mask |= KADM5_POLICY;
1237 } else if (!script_mode) {
1238 fprintf(stderr, _("No policy specified for %s; "
1239 "defaulting to no policy\n"), canon);
1242 /* Don't send KADM5_POLICY_CLR to the server. */
1243 mask &= ~KADM5_POLICY_CLR;
1247 mask |= KADM5_KEY_DATA;
1248 } else if (randkey) {
1250 } else if (pass == NULL) {
1251 unsigned int sz = sizeof(newpw) - 1;
1253 snprintf(prompt1, sizeof(prompt1),
1254 _("Enter password for principal \"%s\""), canon);
1255 snprintf(prompt2, sizeof(prompt2),
1256 _("Re-enter password for principal \"%s\""), canon);
1257 retval = krb5_read_password(context, prompt1, prompt2, newpw, &sz);
1259 com_err("add_principal", retval,
1260 _("while reading password for \"%s\"."), canon);
1265 mask |= KADM5_PRINCIPAL;
1266 retval = create_princ(&princ, mask, n_ks_tuple, ks_tuple, pass);
1267 if (retval == EINVAL && randkey) {
1269 * The server doesn't support randkey creation. Create the principal
1270 * with a dummy password and disallow tickets.
1272 prepare_dummy_password(dummybuf, sizeof(dummybuf));
1273 princ.attributes |= KRB5_KDB_DISALLOW_ALL_TIX;
1274 mask |= KADM5_ATTRIBUTES;
1276 retval = create_princ(&princ, mask, n_ks_tuple, ks_tuple, pass);
1277 old_style_randkey = 1;
1279 if (retval == KADM5_BAD_MASK && nokey) {
1280 error(_("Admin server does not support -nokey while creating "
1281 "\"%s\"\n"), canon);
1285 com_err("add_principal", retval, "while creating \"%s\".", canon);
1288 if (old_style_randkey) {
1289 /* Randomize the password and re-enable tickets. */
1290 retval = randkey_princ(handle, princ.principal, FALSE, n_ks_tuple,
1291 ks_tuple, NULL, NULL);
1293 com_err("add_principal", retval,
1294 _("while randomizing key for \"%s\"."), canon);
1297 princ.attributes &= ~KRB5_KDB_DISALLOW_ALL_TIX; /* clear notix */
1298 mask = KADM5_ATTRIBUTES;
1299 retval = kadm5_modify_principal(handle, &princ, mask);
1301 com_err("add_principal", retval,
1302 _("while clearing DISALLOW_ALL_TIX for \"%s\"."), canon);
1306 info("Principal \"%s\" created.\n", canon);
1309 krb5_free_principal(context, princ.principal);
1312 kadmin_free_tl_data(&princ.n_tl_data, &princ.tl_data);
1316 kadmin_modprinc(int argc, char *argv[])
1318 kadm5_principal_ent_rec princ, oldprinc;
1319 krb5_principal kprinc = NULL;
1321 krb5_error_code retval;
1322 char *pass, *canon = NULL;
1323 krb5_boolean randkey = FALSE, nokey = FALSE;
1325 krb5_key_salt_tuple *ks_tuple = NULL;
1328 kadmin_modprinc_usage();
1332 memset(&oldprinc, 0, sizeof(oldprinc));
1333 memset(&princ, 0, sizeof(princ));
1335 retval = kadmin_parse_name(argv[argc - 1], &kprinc);
1337 com_err("modify_principal", retval, _("while parsing principal"));
1340 retval = krb5_unparse_name(context, kprinc, &canon);
1342 com_err("modify_principal", retval,
1343 _("while canonicalizing principal"));
1346 retval = kadm5_get_principal(handle, kprinc, &oldprinc,
1347 KADM5_PRINCIPAL_NORMAL_MASK);
1349 com_err("modify_principal", retval, _("while getting \"%s\"."), canon);
1352 princ.attributes = oldprinc.attributes;
1353 kadm5_free_principal_ent(handle, &oldprinc);
1354 retval = kadmin_parse_princ_args(argc, argv,
1356 &pass, &randkey, &nokey,
1357 &ks_tuple, &n_ks_tuple,
1358 "modify_principal");
1359 if (retval || ks_tuple != NULL || randkey || nokey || pass) {
1360 kadmin_modprinc_usage();
1363 if (mask & KADM5_POLICY) {
1364 /* Warn if the specified policy does not exist. */
1365 if (!script_mode && !policy_exists(princ.policy)) {
1366 fprintf(stderr, _("WARNING: policy \"%s\" does not exist\n"),
1371 /* Skip this if all we're doing is setting certhash. */
1372 retval = kadm5_modify_principal(handle, &princ, mask);
1375 com_err("modify_principal", retval, _("while modifying \"%s\"."),
1379 info(_("Principal \"%s\" modified.\n"), canon);
1381 krb5_free_principal(context, kprinc);
1382 krb5_free_principal(context, princ.principal);
1383 kadmin_free_tl_data(&princ.n_tl_data, &princ.tl_data);
1389 kadmin_getprinc(int argc, char *argv[])
1391 kadm5_principal_ent_rec dprinc;
1392 krb5_principal princ = NULL;
1393 krb5_error_code retval;
1394 const char *polname, *noexist;
1395 char *canon = NULL, *princstr = NULL, *modprincstr = NULL;
1396 char **sp = NULL, **attrstrs = NULL;
1399 if (!(argc == 2 || (argc == 3 && !strcmp("-terse", argv[1])))) {
1400 error(_("usage: get_principal [-terse] principal\n"));
1404 memset(&dprinc, 0, sizeof(dprinc));
1406 retval = kadmin_parse_name(argv[argc - 1], &princ);
1408 com_err("get_principal", retval, _("while parsing principal"));
1411 retval = krb5_unparse_name(context, princ, &canon);
1413 com_err("get_principal", retval, _("while canonicalizing principal"));
1416 retval = kadm5_get_principal(handle, princ, &dprinc,
1417 KADM5_PRINCIPAL_NORMAL_MASK | KADM5_KEY_DATA);
1419 com_err("get_principal", retval, _("while retrieving \"%s\"."), canon);
1422 retval = krb5_unparse_name(context, dprinc.principal, &princstr);
1424 com_err("get_principal", retval, _("while unparsing principal"));
1427 retval = krb5_unparse_name(context, dprinc.mod_name, &modprincstr);
1429 com_err("get_principal", retval, _("while unparsing principal"));
1433 printf(_("Principal: %s\n"), princstr);
1434 printf(_("Expiration date: %s\n"), dprinc.princ_expire_time ?
1435 strdate(dprinc.princ_expire_time) : _("[never]"));
1436 printf(_("Last password change: %s\n"), dprinc.last_pwd_change ?
1437 strdate(dprinc.last_pwd_change) : _("[never]"));
1438 printf(_("Password expiration date: %s\n"),
1439 dprinc.pw_expiration ?
1440 strdate(dprinc.pw_expiration) : _("[never]"));
1441 printf(_("Maximum ticket life: %s\n"), strdur(dprinc.max_life));
1442 printf(_("Maximum renewable life: %s\n"),
1443 strdur(dprinc.max_renewable_life));
1444 printf(_("Last modified: %s (%s)\n"), strdate(dprinc.mod_date),
1446 printf(_("Last successful authentication: %s\n"),
1447 dprinc.last_success ? strdate(dprinc.last_success) :
1449 printf("Last failed authentication: %s\n",
1450 dprinc.last_failed ? strdate(dprinc.last_failed) :
1452 printf(_("Failed password attempts: %d\n"),
1453 dprinc.fail_auth_count);
1454 printf(_("Number of keys: %d\n"), dprinc.n_key_data);
1455 for (i = 0; i < dprinc.n_key_data; i++) {
1456 krb5_key_data *key_data = &dprinc.key_data[i];
1457 char enctype[BUFSIZ], salttype[BUFSIZ];
1458 char *deprecated = "";
1460 if (krb5_enctype_to_name(key_data->key_data_type[0], FALSE,
1461 enctype, sizeof(enctype)))
1462 snprintf(enctype, sizeof(enctype), _("<Encryption type 0x%x>"),
1463 key_data->key_data_type[0]);
1464 if (!krb5_c_valid_enctype(key_data->key_data_type[0]))
1465 deprecated = "UNSUPPORTED:";
1466 else if (krb5int_c_deprecated_enctype(key_data->key_data_type[0]))
1467 deprecated = "DEPRECATED:";
1468 printf("Key: vno %d, %s%s", key_data->key_data_kvno, deprecated,
1470 if (key_data->key_data_ver > 1 &&
1471 key_data->key_data_type[1] != KRB5_KDB_SALTTYPE_NORMAL) {
1472 if (krb5_salttype_to_string(key_data->key_data_type[1],
1473 salttype, sizeof(salttype)))
1474 snprintf(salttype, sizeof(salttype), _("<Salt type 0x%x>"),
1475 key_data->key_data_type[1]);
1476 printf(":%s", salttype);
1480 printf(_("MKey: vno %d\n"), dprinc.mkvno);
1482 printf(_("Attributes:"));
1483 retval = krb5_flags_to_strings(dprinc.attributes, &attrstrs);
1485 com_err("get_principal", retval, _("while printing flags"));
1488 for (sp = attrstrs; sp != NULL && *sp != NULL; sp++) {
1494 polname = (dprinc.policy != NULL) ? dprinc.policy : _("[none]");
1495 noexist = (dprinc.policy != NULL && !policy_exists(dprinc.policy)) ?
1496 _(" [does not exist]") : "";
1497 printf(_("Policy: %s%s\n"), polname, noexist);
1499 printf("\"%s\"\t%d\t%d\t%d\t%d\t\"%s\"\t%d\t%d\t%d\t%d\t\"%s\""
1500 "\t%d\t%d\t%d\t%d\t%d",
1501 princstr, dprinc.princ_expire_time, dprinc.last_pwd_change,
1502 dprinc.pw_expiration, dprinc.max_life, modprincstr,
1503 dprinc.mod_date, dprinc.attributes, dprinc.kvno,
1504 dprinc.mkvno, dprinc.policy ? dprinc.policy : "[none]",
1505 dprinc.max_renewable_life, dprinc.last_success,
1506 dprinc.last_failed, dprinc.fail_auth_count,
1508 for (i = 0; i < dprinc.n_key_data; i++)
1509 printf("\t%d\t%d\t%d\t%d",
1510 dprinc.key_data[i].key_data_ver,
1511 dprinc.key_data[i].key_data_kvno,
1512 dprinc.key_data[i].key_data_type[0],
1513 dprinc.key_data[i].key_data_type[1]);
1517 krb5_free_principal(context, princ);
1518 kadm5_free_principal_ent(handle, &dprinc);
1525 kadmin_getprincs(int argc, char *argv[])
1527 krb5_error_code retval;
1528 char *expr, **names;
1532 if (!(argc == 1 || (argc == 2 && (expr = argv[1])))) {
1533 error(_("usage: get_principals [expression]\n"));
1536 retval = kadm5_get_principals(handle, expr, &names, &count);
1538 com_err("get_principals", retval, _("while retrieving list."));
1541 for (i = 0; i < count; i++)
1542 printf("%s\n", names[i]);
1543 kadm5_free_name_list(handle, names, count);
1547 kadmin_parse_policy_args(int argc, char *argv[], kadm5_policy_ent_t policy,
1548 long *mask, char *caller)
1550 krb5_error_code retval;
1552 time_t now, interval;
1556 for (i = 1; i < argc - 1; i++) {
1557 if (!strcmp(argv[i], "-maxlife")) {
1560 interval = parse_interval(argv[i], now);
1561 if (interval == (time_t)-1)
1563 policy->pw_max_life = interval;
1564 *mask |= KADM5_PW_MAX_LIFE;
1566 } else if (!strcmp(argv[i], "-minlife")) {
1569 interval = parse_interval(argv[i], now);
1570 if (interval == (time_t)-1)
1572 policy->pw_min_life = interval;
1573 *mask |= KADM5_PW_MIN_LIFE;
1575 } else if (!strcmp(argv[i], "-minlength")) {
1578 policy->pw_min_length = atoi(argv[i]);
1579 *mask |= KADM5_PW_MIN_LENGTH;
1581 } else if (!strcmp(argv[i], "-minclasses")) {
1584 policy->pw_min_classes = atoi(argv[i]);
1585 *mask |= KADM5_PW_MIN_CLASSES;
1587 } else if (!strcmp(argv[i], "-history")) {
1590 policy->pw_history_num = atoi(argv[i]);
1591 *mask |= KADM5_PW_HISTORY_NUM;
1593 } else if (strlen(argv[i]) == 11 &&
1594 !strcmp(argv[i], "-maxfailure")) {
1597 policy->pw_max_fail = atoi(argv[i]);
1598 *mask |= KADM5_PW_MAX_FAILURE;
1600 } else if (strlen(argv[i]) == 21 &&
1601 !strcmp(argv[i], "-failurecountinterval")) {
1604 interval = parse_interval(argv[i], now);
1605 if (interval == (time_t)-1)
1607 policy->pw_failcnt_interval = interval;
1608 *mask |= KADM5_PW_FAILURE_COUNT_INTERVAL;
1610 } else if (strlen(argv[i]) == 16 &&
1611 !strcmp(argv[i], "-lockoutduration")) {
1614 interval = parse_interval(argv[i], now);
1615 if (interval == (time_t)-1)
1617 policy->pw_lockout_duration = interval;
1618 *mask |= KADM5_PW_LOCKOUT_DURATION;
1620 } else if (!strcmp(argv[i], "-allowedkeysalts")) {
1621 krb5_key_salt_tuple *ks_tuple = NULL;
1626 if (strcmp(argv[i], "-")) {
1627 retval = krb5_string_to_keysalts(argv[i], ",", NULL, 0,
1628 &ks_tuple, &n_ks_tuple);
1630 com_err(caller, retval, _("while parsing keysalts %s"),
1635 policy->allowed_keysalts = argv[i];
1637 *mask |= KADM5_POLICY_ALLOWED_KEYSALTS;
1643 error(_("%s: parser lost count!\n"), caller);
1650 kadmin_addmodpol_usage(char *func)
1652 error(_("usage; %s [options] policy\n"), func);
1653 error(_("\toptions are:\n"));
1654 error(_("\t\t[-maxlife time] [-minlife time] [-minlength length]\n"
1655 "\t\t[-minclasses number] [-history number]\n"
1656 "\t\t[-maxfailure number] [-failurecountinterval time]\n"
1657 "\t\t[-allowedkeysalts keysalts]\n"));
1658 error(_("\t\t[-lockoutduration time]\n"));
1662 kadmin_addpol(int argc, char *argv[])
1664 krb5_error_code retval;
1666 kadm5_policy_ent_rec policy;
1668 memset(&policy, 0, sizeof(policy));
1669 if (kadmin_parse_policy_args(argc, argv, &policy, &mask, "add_policy")) {
1670 kadmin_addmodpol_usage("add_policy");
1673 policy.policy = argv[argc - 1];
1674 mask |= KADM5_POLICY;
1675 retval = kadm5_create_policy(handle, &policy, mask);
1677 com_err("add_policy", retval, _("while creating policy \"%s\"."),
1683 kadmin_modpol(int argc, char *argv[])
1685 krb5_error_code retval;
1687 kadm5_policy_ent_rec policy;
1689 memset(&policy, 0, sizeof(policy));
1690 if (kadmin_parse_policy_args(argc, argv, &policy, &mask,
1692 kadmin_addmodpol_usage("modify_policy");
1695 policy.policy = argv[argc - 1];
1696 retval = kadm5_modify_policy(handle, &policy, mask);
1698 com_err("modify_policy", retval, _("while modifying policy \"%s\"."),
1704 kadmin_delpol(int argc, char *argv[])
1706 krb5_error_code retval;
1709 if (!(argc == 2 || (argc == 3 && !strcmp("-force", argv[1])))) {
1710 error(_("usage: delete_policy [-force] policy\n"));
1713 if (argc == 2 && !script_mode) {
1714 printf(_("Are you sure you want to delete the policy \"%s\"? "
1715 "(yes/no): "), argv[1]);
1716 fgets(reply, sizeof(reply), stdin);
1717 if (strcmp("yes\n", reply)) {
1718 fprintf(stderr, _("Policy \"%s\" not deleted.\n"), argv[1]);
1722 retval = kadm5_delete_policy(handle, argv[argc - 1]);
1724 com_err("delete_policy:", retval, _("while deleting policy \"%s\""),
1730 kadmin_getpol(int argc, char *argv[])
1732 krb5_error_code retval;
1733 kadm5_policy_ent_rec policy;
1735 if (!(argc == 2 || (argc == 3 && !strcmp("-terse", argv[1])))) {
1736 error(_("usage: get_policy [-terse] policy\n"));
1739 retval = kadm5_get_policy(handle, argv[argc - 1], &policy);
1741 com_err("get_policy", retval, _("while retrieving policy \"%s\"."),
1746 printf(_("Policy: %s\n"), policy.policy);
1747 printf(_("Maximum password life: %s\n"), strdur(policy.pw_max_life));
1748 printf(_("Minimum password life: %s\n"), strdur(policy.pw_min_life));
1749 printf(_("Minimum password length: %ld\n"), policy.pw_min_length);
1750 printf(_("Minimum number of password character classes: %ld\n"),
1751 policy.pw_min_classes);
1752 printf(_("Number of old keys kept: %ld\n"), policy.pw_history_num);
1753 printf(_("Maximum password failures before lockout: %lu\n"),
1754 (unsigned long)policy.pw_max_fail);
1755 printf(_("Password failure count reset interval: %s\n"),
1756 strdur(policy.pw_failcnt_interval));
1757 printf(_("Password lockout duration: %s\n"),
1758 strdur(policy.pw_lockout_duration));
1759 if (policy.allowed_keysalts != NULL)
1760 printf(_("Allowed key/salt types: %s\n"), policy.allowed_keysalts);
1762 /* Output 0 where we used to output policy_refcnt. */
1763 printf("\"%s\"\t%ld\t%ld\t%ld\t%ld\t%ld\t0\t%lu\t%ld\t%ld\t%s\n",
1764 policy.policy, policy.pw_max_life, policy.pw_min_life,
1765 policy.pw_min_length, policy.pw_min_classes,
1766 policy.pw_history_num, (unsigned long)policy.pw_max_fail,
1767 (long)policy.pw_failcnt_interval,
1768 (long)policy.pw_lockout_duration,
1769 (policy.allowed_keysalts == NULL) ? "-" :
1770 policy.allowed_keysalts);
1772 kadm5_free_policy_ent(handle, &policy);
1776 kadmin_getpols(int argc, char *argv[])
1778 krb5_error_code retval;
1779 char *expr, **names;
1783 if (!(argc == 1 || (argc == 2 && (expr = argv[1])))) {
1784 error(_("usage: get_policies [expression]\n"));
1787 retval = kadm5_get_policies(handle, expr, &names, &count);
1789 com_err("get_policies", retval, _("while retrieving list."));
1792 for (i = 0; i < count; i++)
1793 printf("%s\n", names[i]);
1794 kadm5_free_name_list(handle, names, count);
1798 kadmin_getprivs(int argc, char *argv[])
1800 static char *privs[] = {"INQUIRE", "ADD", "MODIFY", "DELETE"};
1801 krb5_error_code retval;
1806 error(_("usage: get_privs\n"));
1809 retval = kadm5_get_privs(handle, &plist);
1811 com_err("get_privs", retval, _("while retrieving privileges"));
1814 printf(_("current privileges:"));
1815 for (i = 0; i < sizeof (privs) / sizeof (char *); i++) {
1817 printf(" %s", privs[i]);
1823 kadmin_purgekeys(int argc, char *argv[])
1827 char *pname = NULL, *canon = NULL;
1828 krb5_principal princ;
1830 if (argc == 4 && strcmp(argv[1], "-keepkvno") == 0) {
1831 keepkvno = atoi(argv[2]);
1833 } else if (argc == 3 && strcmp(argv[1], "-all") == 0) {
1834 keepkvno = KRB5_INT32_MAX;
1836 } else if (argc == 2) {
1839 if (pname == NULL) {
1840 error(_("usage: purgekeys [-all|-keepkvno oldest_kvno_to_keep] "
1845 retval = kadmin_parse_name(pname, &princ);
1847 com_err("purgekeys", retval, _("while parsing principal"));
1851 retval = krb5_unparse_name(context, princ, &canon);
1853 com_err("purgekeys", retval, _("while canonicalizing principal"));
1857 retval = kadm5_purgekeys(handle, princ, keepkvno);
1859 com_err("purgekeys", retval,
1860 _("while purging keys for principal \"%s\""), canon);
1864 if (keepkvno == KRB5_INT32_MAX)
1865 info(_("All keys for principal \"%s\" removed.\n"), canon);
1867 info(_("Old keys for principal \"%s\" purged.\n"), canon);
1869 krb5_free_principal(context, princ);
1875 kadmin_getstrings(int argc, char *argv[])
1878 char *pname, *canon = NULL;
1879 krb5_principal princ = NULL;
1880 krb5_string_attr *strings = NULL;
1884 error(_("usage: get_strings principal\n"));
1889 retval = kadmin_parse_name(pname, &princ);
1891 com_err("get_strings", retval, _("while parsing principal"));
1895 retval = krb5_unparse_name(context, princ, &canon);
1897 com_err("get_strings", retval, _("while canonicalizing principal"));
1901 retval = kadm5_get_strings(handle, princ, &strings, &count);
1903 com_err("get_strings", retval,
1904 _("while getting attributes for principal \"%s\""), canon);
1909 printf(_("(No string attributes.)\n"));
1910 for (i = 0; i < count; i++)
1911 printf("%s: %s\n", strings[i].key, strings[i].value);
1912 kadm5_free_strings(handle, strings, count);
1915 krb5_free_principal(context, princ);
1921 kadmin_setstring(int argc, char *argv[])
1924 char *pname, *canon = NULL, *key, *value;
1925 krb5_principal princ = NULL;
1928 error(_("usage: set_string principal key value\n"));
1935 retval = kadmin_parse_name(pname, &princ);
1937 com_err("set_string", retval, _("while parsing principal"));
1941 retval = krb5_unparse_name(context, princ, &canon);
1943 com_err("set_string", retval, _("while canonicalizing principal"));
1947 retval = kadm5_set_string(handle, princ, key, value);
1949 com_err("set_string", retval,
1950 _("while setting attribute on principal \"%s\""), canon);
1954 info(_("Attribute set for principal \"%s\".\n"), canon);
1956 krb5_free_principal(context, princ);
1962 kadmin_delstring(int argc, char *argv[])
1965 char *pname, *canon = NULL, *key;
1966 krb5_principal princ = NULL;
1969 error(_("usage: del_string principal key\n"));
1975 retval = kadmin_parse_name(pname, &princ);
1977 com_err("delstring", retval, _("while parsing principal"));
1981 retval = krb5_unparse_name(context, princ, &canon);
1983 com_err("del_string", retval, _("while canonicalizing principal"));
1987 retval = kadm5_set_string(handle, princ, key, NULL);
1989 com_err("del_string", retval,
1990 _("while deleting attribute from principal \"%s\""), canon);
1994 info(_("Attribute removed from principal \"%s\".\n"), canon);
1996 krb5_free_principal(context, princ);