Path: blob/main/crypto/krb5/src/kadmin/ktutil/ktutil.c
34907 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* kadmin/ktutil/ktutil.c - SS user interface for ktutil */2/*3* Copyright 1995, 1996, 2008 by the Massachusetts Institute of Technology.4* All Rights Reserved.5*6* Export of this software from the United States of America may7* require a specific license from the United States Government.8* It is the responsibility of any person or organization contemplating9* export to obtain such a license before exporting.10*11* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and12* distribute this software and its documentation for any purpose and13* without fee is hereby granted, provided that the above copyright14* notice appear in all copies and that both that copyright notice and15* this permission notice appear in supporting documentation, and that16* the name of M.I.T. not be used in advertising or publicity pertaining17* to distribution of the software without specific, written prior18* permission. Furthermore if you modify this software you must label19* your software as modified software and not distribute it in such a20* fashion that it might be confused with the original M.I.T. software.21* M.I.T. makes no representations about the suitability of22* this software for any purpose. It is provided "as is" without express23* or implied warranty.24*/2526#include "k5-int.h"27#include "ktutil.h"28#include <com_err.h>29#include <locale.h>30#include "adm_proto.h"31#include <ss/ss.h>32#include <stdio.h>33#ifdef HAVE_STDLIB_H34#include <stdlib.h>35#endif3637extern ss_request_table ktutil_cmds;38krb5_context kcontext;39krb5_kt_list ktlist = NULL;4041int42main(int argc, char *argv[])43{44krb5_error_code retval;45int sci_idx;4647setlocale(LC_ALL, "");48retval = krb5_init_context(&kcontext);49if (retval) {50com_err(argv[0], retval, _("while initializing krb5"));51exit(1);52}53sci_idx = ss_create_invocation("ktutil", "5.0", (char *)NULL,54&ktutil_cmds, &retval);55if (retval) {56ss_perror(sci_idx, retval, _("creating invocation"));57exit(1);58}59retval = ss_listen(sci_idx);60ktutil_free_kt_list(kcontext, ktlist);61exit(0);62}6364void65ktutil_clear_list(int argc, char *argv[], int sci_idx, void *info_ptr)66{67krb5_error_code retval;6869if (argc != 1) {70fprintf(stderr, _("%s: invalid arguments\n"), argv[0]);71return;72}73retval = ktutil_free_kt_list(kcontext, ktlist);74if (retval)75com_err(argv[0], retval, _("while freeing ktlist"));76ktlist = NULL;77}7879void80ktutil_read_v5(int argc, char *argv[], int sci_idx, void *info_ptr)81{82krb5_error_code retval;8384if (argc != 2) {85fprintf(stderr, _("%s: must specify keytab to read\n"), argv[0]);86return;87}88retval = ktutil_read_keytab(kcontext, argv[1], &ktlist);89if (retval)90com_err(argv[0], retval, _("while reading keytab \"%s\""), argv[1]);91}9293void94ktutil_read_v4(int argc, char *argv[], int sci_idx, void *info_ptr)95{96fprintf(stderr, _("%s: reading srvtabs is no longer supported\n"),97argv[0]);98}99100void101ktutil_write_v5(int argc, char *argv[], int sci_idx, void *info_ptr)102{103krb5_error_code retval;104105if (argc != 2) {106fprintf(stderr, _("%s: must specify keytab to write\n"), argv[0]);107return;108}109retval = ktutil_write_keytab(kcontext, ktlist, argv[1]);110if (retval)111com_err(argv[0], retval, _("while writing keytab \"%s\""), argv[1]);112}113114void115ktutil_write_v4(int argc, char *argv[], int sci_idx, void *info_ptr)116{117fprintf(stderr, _("%s: writing srvtabs is no longer supported\n"),118argv[0]);119}120121void122ktutil_add_entry(int argc, char *argv[], int sci_idx, void *info_ptr)123{124krb5_error_code retval;125char *princ = NULL;126char *enctype = NULL;127krb5_kvno kvno = 0;128int use_pass = 0, use_key = 0, use_kvno = 0, fetch = 0, i;129char *salt = NULL;130131for (i = 1; i < argc; i++) {132if ((strlen(argv[i]) == 2) && !strncmp(argv[i], "-p", 2)) {133princ = argv[++i];134continue;135}136if ((strlen(argv[i]) == 2) && !strncmp(argv[i], "-k", 2)) {137kvno = (krb5_kvno) atoi(argv[++i]);138use_kvno++;139continue;140}141if ((strlen(argv[i]) == 2) && !strncmp(argv[i], "-e", 2)) {142enctype = argv[++i];143continue;144}145if ((strlen(argv[i]) == 9) && !strncmp(argv[i], "-password", 9)) {146use_pass++;147continue;148}149if ((strlen(argv[i]) == 4) && !strncmp(argv[i], "-key", 4)) {150use_key++;151continue;152}153if ((strlen(argv[i]) == 2) && !strncmp(argv[i], "-s", 2)) {154salt = argv[++i];155continue;156}157if ((strlen(argv[i]) == 2) && !strncmp(argv[i], "-f", 2))158fetch++;159}160161if (princ == NULL || use_pass + use_key != 1 || !use_kvno ||162(fetch && salt != NULL)) {163fprintf(stderr, _("usage: %s (-key | -password) -p principal "164"-k kvno [-e enctype] [-f|-s salt]\n"), argv[0]);165return;166}167if (!fetch && enctype == NULL) {168fprintf(stderr, _("enctype must be specified if not using -f\n"));169return;170}171172retval = ktutil_add(kcontext, &ktlist, princ, fetch, kvno, enctype,173use_pass, salt);174if (retval)175com_err(argv[0], retval, _("while adding new entry"));176}177178void179ktutil_delete_entry(int argc, char *argv[], int sci_idx, void *info_ptr)180{181krb5_error_code retval;182183if (argc != 2) {184fprintf(stderr, _("%s: must specify entry to delete\n"), argv[0]);185return;186}187retval = ktutil_delete(kcontext, &ktlist, atoi(argv[1]));188if (retval)189com_err(argv[0], retval, _("while deleting entry %d"), atoi(argv[1]));190}191192void193ktutil_list(int argc, char *argv[], int sci_idx, void *info_ptr)194{195krb5_error_code retval;196krb5_kt_list lp;197int show_time = 0, show_keys = 0, show_enctype = 0;198int i;199unsigned int j;200char *pname;201202for (i = 1; i < argc; i++) {203if ((strlen(argv[i]) == 2) && !strncmp(argv[i], "-t", 2)) {204show_time++;205continue;206}207if ((strlen(argv[i]) == 2) && !strncmp(argv[i], "-k", 2)) {208show_keys++;209continue;210}211if ((strlen(argv[i]) == 2) && !strncmp(argv[i], "-e", 2)) {212show_enctype++;213continue;214}215216fprintf(stderr, _("%s: usage: %s [-t] [-k] [-e]\n"), argv[0], argv[0]);217return;218}219/* XXX Translating would disturb table alignment; skip for now. */220if (show_time) {221printf("slot KVNO Timestamp Principal\n");222printf("---- ---- ----------------- ---------------------------------------------------\n");223} else {224printf("slot KVNO Principal\n");225printf("---- ---- ---------------------------------------------------------------------\n");226}227for (i = 1, lp = ktlist; lp; i++, lp = lp->next) {228retval = krb5_unparse_name(kcontext, lp->entry->principal, &pname);229if (retval) {230com_err(argv[0], retval, "while unparsing principal name");231return;232}233printf("%4d %4d ", i, lp->entry->vno);234if (show_time) {235char fmtbuf[18];236char fill;237time_t tstamp;238239tstamp = lp->entry->timestamp;240lp->entry->timestamp = tstamp;241fill = ' ';242if (!krb5_timestamp_to_sfstring((krb5_timestamp)lp->entry->243timestamp,244fmtbuf,245sizeof(fmtbuf),246&fill))247printf("%s ", fmtbuf);248}249printf("%40s", pname);250if (show_enctype) {251static char buf[256];252if ((retval = krb5_enctype_to_name(lp->entry->key.enctype, FALSE,253buf, sizeof(buf)))) {254com_err(argv[0], retval,255_("While converting enctype to string"));256free(pname);257return;258}259printf(" (%s) ", buf);260}261262if (show_keys) {263printf(" (0x");264for (j = 0; j < lp->entry->key.length; j++)265printf("%02x", lp->entry->key.contents[j]);266printf(")");267}268printf("\n");269free(pname);270}271}272273274