Some unsigned/signed warning cleanup
[krb5.git] / src / kadmin / ktutil / ktutil.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  * kadmin/ktutil/ktutil.c
4  *
5  * Copyright 1995, 1996, 2008 by the Massachusetts Institute of Technology.
6  * All Rights Reserved.
7  *
8  * Export of this software from the United States of America may
9  *   require a specific license from the United States Government.
10  *   It is the responsibility of any person or organization contemplating
11  *   export to obtain such a license before exporting.
12  *
13  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
14  * distribute this software and its documentation for any purpose and
15  * without fee is hereby granted, provided that the above copyright
16  * notice appear in all copies and that both that copyright notice and
17  * this permission notice appear in supporting documentation, and that
18  * the name of M.I.T. not be used in advertising or publicity pertaining
19  * to distribution of the software without specific, written prior
20  * permission.  Furthermore if you modify this software you must label
21  * your software as modified software and not distribute it in such a
22  * fashion that it might be confused with the original M.I.T. software.
23  * M.I.T. makes no representations about the suitability of
24  * this software for any purpose.  It is provided "as is" without express
25  * or implied warranty.
26  *
27  * SS user interface for ktutil.
28  */
29
30 #include "k5-int.h"
31 #include "ktutil.h"
32 #include <com_err.h>
33 #include "adm_proto.h"
34 #include <ss/ss.h>
35 #include <stdio.h>
36 #ifdef HAVE_STDLIB_H
37 #include <stdlib.h>
38 #endif
39
40 extern ss_request_table ktutil_cmds;
41 krb5_context kcontext;
42 krb5_kt_list ktlist = NULL;
43
44 int main(argc, argv)
45     int argc;
46     char *argv[];
47 {
48     krb5_error_code retval;
49     int sci_idx;
50
51     retval = krb5_init_context(&kcontext);
52     if (retval) {
53         com_err(argv[0], retval, "while initializing krb5");
54         exit(1);
55     }
56     sci_idx = ss_create_invocation("ktutil", "5.0", (char *)NULL,
57                                    &ktutil_cmds, &retval);
58     if (retval) {
59         ss_perror(sci_idx, retval, "creating invocation");
60         exit(1);
61     }
62     retval = ss_listen(sci_idx);
63     ktutil_free_kt_list(kcontext, ktlist);
64     exit(0);
65 }
66
67 void ktutil_clear_list(argc, argv)
68     int argc;
69     char *argv[];
70 {
71     krb5_error_code retval;
72
73     if (argc != 1) {
74         fprintf(stderr, "%s: invalid arguments\n", argv[0]);
75         return;
76     }
77     retval = ktutil_free_kt_list(kcontext, ktlist);
78     if (retval)
79         com_err(argv[0], retval, "while freeing ktlist");
80     ktlist = NULL;
81 }
82
83 void ktutil_read_v5(argc, argv)
84     int argc;
85     char *argv[];
86 {
87     krb5_error_code retval;
88
89     if (argc != 2) {
90         fprintf(stderr, "%s: must specify keytab to read\n", argv[0]);
91         return;
92     }
93     retval = ktutil_read_keytab(kcontext, argv[1], &ktlist);
94     if (retval)
95         com_err(argv[0], retval, "while reading keytab \"%s\"", argv[1]);
96 }
97
98 void ktutil_read_v4(argc, argv)
99     int argc;
100     char *argv[];
101 {
102     krb5_error_code retval;
103
104     if (argc != 2) {
105         fprintf(stderr, "%s: must specify the srvtab to read\n", argv[0]);
106         return;
107     }
108     retval = ktutil_read_srvtab(kcontext, argv[1], &ktlist);
109     if (retval)
110         com_err(argv[0], retval, "while reading srvtab \"%s\"", argv[1]);
111 }
112
113 void ktutil_write_v5(argc, argv)
114     int argc;
115     char *argv[];
116 {
117     krb5_error_code retval;
118
119     if (argc != 2) {
120         fprintf(stderr, "%s: must specify keytab to write\n", argv[0]);
121         return;
122     }
123     retval = ktutil_write_keytab(kcontext, ktlist, argv[1]);
124     if (retval)
125         com_err(argv[0], retval, "while writing keytab \"%s\"", argv[1]);
126 }
127
128 void ktutil_write_v4(argc, argv)
129     int argc;
130     char *argv[];
131 {
132     fprintf(stderr, "%s: writing srvtabs is no longer supported\n", argv[0]);
133 }
134
135 void ktutil_add_entry(argc, argv)
136     int argc;
137     char *argv[];
138 {
139     krb5_error_code retval;
140     char *princ = NULL;
141     char *enctype = NULL;
142     krb5_kvno kvno = 0;
143     int use_pass = 0, use_key = 0, i;
144
145     for (i = 1; i < argc; i++) {
146         if ((strlen(argv[i]) == 2) && !strncmp(argv[i], "-p", 2)) {
147             princ = argv[++i];
148             continue;
149         }
150         if ((strlen(argv[i]) == 2) && !strncmp(argv[i], "-k", 2)) {
151             kvno = (krb5_kvno) atoi(argv[++i]);
152             continue;
153         }
154         if ((strlen(argv[i]) == 2) && !strncmp(argv[i], "-e", 2)) {
155             enctype = argv[++i];
156             continue;
157         }
158         if ((strlen(argv[i]) == 9) && !strncmp(argv[i], "-password", 9)) {
159             use_pass++;
160             continue;
161         }
162         if ((strlen(argv[i]) == 4) && !strncmp(argv[i], "-key", 4)) {
163             use_key++;
164             continue;
165         }
166     }
167
168     if (argc != 8 || !(princ && kvno && enctype) || (use_pass+use_key != 1)) {
169         fprintf(stderr, "usage: %s (-key | -password) -p principal "
170                 "-k kvno -e enctype\n", argv[0]);
171         return;
172     }
173
174     retval = ktutil_add(kcontext, &ktlist, princ, kvno, enctype, use_pass);
175     if (retval)
176         com_err(argv[0], retval, "while adding new entry");
177 }
178
179 void ktutil_delete_entry(argc, argv)
180     int argc;
181     char *argv[];
182 {
183     krb5_error_code retval;
184
185     if (argc != 2) {
186         fprintf(stderr, "%s: must specify entry to delete\n", argv[0]);
187         return;
188     }
189     retval = ktutil_delete(kcontext, &ktlist, atoi(argv[1]));
190     if (retval)
191         com_err(argv[0], retval, "while deleting entry %d", atoi(argv[1]));
192 }
193
194 void ktutil_list(argc, argv)
195     int argc;
196     char *argv[];
197 {
198     krb5_error_code retval;
199     krb5_kt_list lp;
200     int show_time = 0, show_keys = 0, show_enctype = 0;
201     int i;
202     unsigned int j;
203     char *pname;
204
205     for (i = 1; i < argc; i++) {
206         if ((strlen(argv[i]) == 2) && !strncmp(argv[i], "-t", 2)) {
207             show_time++;
208             continue;
209         }
210         if ((strlen(argv[i]) == 2) && !strncmp(argv[i], "-k", 2)) {
211             show_keys++;
212             continue;
213         }
214         if ((strlen(argv[i]) == 2) && !strncmp(argv[i], "-e", 2)) {
215             show_enctype++;
216             continue;
217         }
218
219         fprintf(stderr, "%s: usage: %s [-t] [-k] [-e]\n", argv[0], argv[0]);
220         return;
221     }
222     if (show_time) {
223         printf("slot KVNO Timestamp         Principal\n");
224         printf("---- ---- ----------------- ---------------------------------------------------\n");
225     } else {
226         printf("slot KVNO Principal\n");
227         printf("---- ---- ---------------------------------------------------------------------\n");
228     }
229     for (i = 1, lp = ktlist; lp; i++, lp = lp->next) {
230         retval = krb5_unparse_name(kcontext, lp->entry->principal, &pname);
231         if (retval) {
232             com_err(argv[0], retval, "while unparsing principal name");
233             return;
234         }
235         printf("%4d %4d ", i, lp->entry->vno);
236         if (show_time) {
237             char fmtbuf[18];
238             char fill;
239             time_t tstamp;
240
241             tstamp = lp->entry->timestamp;
242             (void) localtime(&tstamp);
243             lp->entry->timestamp = tstamp;
244             fill = ' ';
245             if (!krb5_timestamp_to_sfstring((krb5_timestamp)lp->entry->
246                                             timestamp,
247                                             fmtbuf,
248                                             sizeof(fmtbuf),
249                                             &fill))
250                 printf("%s ", fmtbuf);
251         }
252         printf("%40s", pname);
253         if (show_enctype) {
254             static char buf[256];
255             if ((retval = krb5_enctype_to_string(
256                      lp->entry->key.enctype, buf, 256))) {
257                 com_err(argv[0], retval, "While converting enctype to string");
258                 return;
259             }
260             printf(" (%s) ", buf);
261         }
262
263         if (show_keys) {
264             printf(" (0x");
265             for (j = 0; j < lp->entry->key.length; j++)
266                 printf("%02x", lp->entry->key.contents[j]);
267             printf(")");
268         }
269         printf("\n");
270         free(pname);
271     }
272 }