File: ktutil.c

package info (click to toggle)
krb5 1.12.1%2Bdfsg-19%2Bdeb8u4
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 67,680 kB
  • ctags: 34,155
  • sloc: ansic: 309,213; cpp: 15,753; exp: 13,257; makefile: 7,652; python: 7,228; sh: 6,457; perl: 3,514; asm: 1,544; yacc: 1,187; awk: 396; xml: 368; csh: 147; lisp: 104; sed: 43
file content (274 lines) | stat: -rw-r--r-- 8,275 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* kadmin/ktutil/ktutil.c - SS user interface for ktutil */
/*
 * Copyright 1995, 1996, 2008 by the Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * Export of this software from the United States of America may
 *   require a specific license from the United States Government.
 *   It is the responsibility of any person or organization contemplating
 *   export to obtain such a license before exporting.
 *
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  Furthermore if you modify this software you must label
 * your software as modified software and not distribute it in such a
 * fashion that it might be confused with the original M.I.T. software.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 */

#include "k5-int.h"
#include "ktutil.h"
#include <com_err.h>
#include <locale.h>
#include "adm_proto.h"
#include <ss/ss.h>
#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

extern ss_request_table ktutil_cmds;
krb5_context kcontext;
krb5_kt_list ktlist = NULL;

int main(argc, argv)
    int argc;
    char *argv[];
{
    krb5_error_code retval;
    int sci_idx;

    setlocale(LC_ALL, "");
    retval = krb5_init_context(&kcontext);
    if (retval) {
        com_err(argv[0], retval, _("while initializing krb5"));
        exit(1);
    }
    sci_idx = ss_create_invocation("ktutil", "5.0", (char *)NULL,
                                   &ktutil_cmds, &retval);
    if (retval) {
        ss_perror(sci_idx, retval, _("creating invocation"));
        exit(1);
    }
    retval = ss_listen(sci_idx);
    ktutil_free_kt_list(kcontext, ktlist);
    exit(0);
}

void ktutil_clear_list(argc, argv)
    int argc;
    char *argv[];
{
    krb5_error_code retval;

    if (argc != 1) {
        fprintf(stderr, _("%s: invalid arguments\n"), argv[0]);
        return;
    }
    retval = ktutil_free_kt_list(kcontext, ktlist);
    if (retval)
        com_err(argv[0], retval, _("while freeing ktlist"));
    ktlist = NULL;
}

void ktutil_read_v5(argc, argv)
    int argc;
    char *argv[];
{
    krb5_error_code retval;

    if (argc != 2) {
        fprintf(stderr, _("%s: must specify keytab to read\n"), argv[0]);
        return;
    }
    retval = ktutil_read_keytab(kcontext, argv[1], &ktlist);
    if (retval)
        com_err(argv[0], retval, _("while reading keytab \"%s\""), argv[1]);
}

void ktutil_read_v4(argc, argv)
    int argc;
    char *argv[];
{
    krb5_error_code retval;

    if (argc != 2) {
        fprintf(stderr, _("%s: must specify the srvtab to read\n"), argv[0]);
        return;
    }
    retval = ktutil_read_srvtab(kcontext, argv[1], &ktlist);
    if (retval)
        com_err(argv[0], retval, _("while reading srvtab \"%s\""), argv[1]);
}

void ktutil_write_v5(argc, argv)
    int argc;
    char *argv[];
{
    krb5_error_code retval;

    if (argc != 2) {
        fprintf(stderr, _("%s: must specify keytab to write\n"), argv[0]);
        return;
    }
    retval = ktutil_write_keytab(kcontext, ktlist, argv[1]);
    if (retval)
        com_err(argv[0], retval, _("while writing keytab \"%s\""), argv[1]);
}

void ktutil_write_v4(argc, argv)
    int argc;
    char *argv[];
{
    fprintf(stderr, _("%s: writing srvtabs is no longer supported\n"),
            argv[0]);
}

void ktutil_add_entry(argc, argv)
    int argc;
    char *argv[];
{
    krb5_error_code retval;
    char *princ = NULL;
    char *enctype = NULL;
    krb5_kvno kvno = 0;
    int use_pass = 0, use_key = 0, i;

    for (i = 1; i < argc; i++) {
        if ((strlen(argv[i]) == 2) && !strncmp(argv[i], "-p", 2)) {
            princ = argv[++i];
            continue;
        }
        if ((strlen(argv[i]) == 2) && !strncmp(argv[i], "-k", 2)) {
            kvno = (krb5_kvno) atoi(argv[++i]);
            continue;
        }
        if ((strlen(argv[i]) == 2) && !strncmp(argv[i], "-e", 2)) {
            enctype = argv[++i];
            continue;
        }
        if ((strlen(argv[i]) == 9) && !strncmp(argv[i], "-password", 9)) {
            use_pass++;
            continue;
        }
        if ((strlen(argv[i]) == 4) && !strncmp(argv[i], "-key", 4)) {
            use_key++;
            continue;
        }
    }

    if (argc != 8 || !(princ && kvno && enctype) || (use_pass+use_key != 1)) {
        fprintf(stderr, _("usage: %s (-key | -password) -p principal "
                          "-k kvno -e enctype\n"), argv[0]);
        return;
    }

    retval = ktutil_add(kcontext, &ktlist, princ, kvno, enctype, use_pass);
    if (retval)
        com_err(argv[0], retval, _("while adding new entry"));
}

void ktutil_delete_entry(argc, argv)
    int argc;
    char *argv[];
{
    krb5_error_code retval;

    if (argc != 2) {
        fprintf(stderr, _("%s: must specify entry to delete\n"), argv[0]);
        return;
    }
    retval = ktutil_delete(kcontext, &ktlist, atoi(argv[1]));
    if (retval)
        com_err(argv[0], retval, _("while deleting entry %d"), atoi(argv[1]));
}

void ktutil_list(argc, argv)
    int argc;
    char *argv[];
{
    krb5_error_code retval;
    krb5_kt_list lp;
    int show_time = 0, show_keys = 0, show_enctype = 0;
    int i;
    unsigned int j;
    char *pname;

    for (i = 1; i < argc; i++) {
        if ((strlen(argv[i]) == 2) && !strncmp(argv[i], "-t", 2)) {
            show_time++;
            continue;
        }
        if ((strlen(argv[i]) == 2) && !strncmp(argv[i], "-k", 2)) {
            show_keys++;
            continue;
        }
        if ((strlen(argv[i]) == 2) && !strncmp(argv[i], "-e", 2)) {
            show_enctype++;
            continue;
        }

        fprintf(stderr, _("%s: usage: %s [-t] [-k] [-e]\n"), argv[0], argv[0]);
        return;
    }
    /* XXX Translating would disturb table alignment; skip for now. */
    if (show_time) {
        printf("slot KVNO Timestamp         Principal\n");
        printf("---- ---- ----------------- ---------------------------------------------------\n");
    } else {
        printf("slot KVNO Principal\n");
        printf("---- ---- ---------------------------------------------------------------------\n");
    }
    for (i = 1, lp = ktlist; lp; i++, lp = lp->next) {
        retval = krb5_unparse_name(kcontext, lp->entry->principal, &pname);
        if (retval) {
            com_err(argv[0], retval, "while unparsing principal name");
            return;
        }
        printf("%4d %4d ", i, lp->entry->vno);
        if (show_time) {
            char fmtbuf[18];
            char fill;
            time_t tstamp;

            tstamp = lp->entry->timestamp;
            (void) localtime(&tstamp);
            lp->entry->timestamp = tstamp;
            fill = ' ';
            if (!krb5_timestamp_to_sfstring((krb5_timestamp)lp->entry->
                                            timestamp,
                                            fmtbuf,
                                            sizeof(fmtbuf),
                                            &fill))
                printf("%s ", fmtbuf);
        }
        printf("%40s", pname);
        if (show_enctype) {
            static char buf[256];
            if ((retval = krb5_enctype_to_name(lp->entry->key.enctype, FALSE,
                                               buf, sizeof(buf)))) {
                com_err(argv[0], retval,
                        _("While converting enctype to string"));
                return;
            }
            printf(" (%s) ", buf);
        }

        if (show_keys) {
            printf(" (0x");
            for (j = 0; j < lp->entry->key.length; j++)
                printf("%02x", lp->entry->key.contents[j]);
            printf(")");
        }
        printf("\n");
        free(pname);
    }
}