5aab9de3167505418b0745439aa2f807b8089a9c
[openafs.git] / src / bozo / bos_util.c
1 /*
2  *  Copyright (C) 1989 by the Massachusetts Institute of Technology
3  *
4  *    Export of software employing encryption from the United States of
5  *    America is assumed to require a specific license from the United
6  *    States Government.  It is the responsibility of any person or
7  *    organization contemplating export to obtain such a license before
8  *    exporting.
9  *
10  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
11  * distribute this software and its documentation for any purpose and
12  * without fee is hereby granted, provided that the above copyright
13  * notice appear in all copies and that both that copyright notice and
14  * this permission notice appear in supporting documentation, and that
15  * the name of M.I.T. not be used in advertising or publicity pertaining
16  * to distribution of the software without specific, written prior
17  * permission.  M.I.T. makes no representations about the suitability of
18  * this software for any purpose.  It is provided "as is" without express
19  * or implied warranty.
20  */
21
22 #include <afsconfig.h>
23 #include <afs/param.h>
24
25 #include <roken.h>
26
27 #include <hcrypto/ui.h>
28 #include <hcrypto/des.h>
29
30 #include <afs/stds.h>
31 #include <afs/afsutil.h>
32 #include <afs/keys.h>
33 #include <afs/cellconfig.h>
34 #include <afs/kautils.h>
35
36 int
37 main(int argc, char **argv)
38 {
39     struct afsconf_dir *tdir;
40     afs_int32 code;
41
42     if (argc == 1) {
43         printf("bos_util: usage is 'bos_util <opcode> options, e.g.\n");
44         printf("    bos_util add <kvno>\n");
45         printf("    bos_util adddes <kvno>\n");
46 #ifdef KERBEROS
47         printf("    bos_util srvtab2keyfile <kvno> <keyfile> <princ>\n");
48 #endif
49         printf("    bos_util delete <kvno>\n");
50         printf("    bos_util list\n");
51         exit(1);
52     }
53
54     tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIR);
55     if (!tdir) {
56         printf("bos_util: can't initialize conf dir '%s'\n",
57                AFSDIR_SERVER_ETC_DIR);
58         exit(1);
59     }
60     if (strcmp(argv[1], "add") == 0) {
61         struct ktc_encryptionKey tkey;
62         int kvno;
63         char buf[BUFSIZ], ver[BUFSIZ];
64         char *tcell = NULL;
65
66         if (argc != 3) {
67             printf("bos_util add: usage is 'bos_util add <kvno>\n");
68             exit(1);
69         }
70         kvno = atoi(argv[2]);
71         memset(&tkey, 0, sizeof(struct ktc_encryptionKey));
72
73         /* prompt for key */
74         code = UI_UTIL_read_pw_string(buf, sizeof(buf), "input key: ", 0);
75         if (code || strlen(buf) == 0) {
76             printf("Bad key: \n");
77             exit(1);
78         }
79         code = UI_UTIL_read_pw_string(ver, sizeof(ver), "Retype input key: ", 0);
80         if (code || strlen(ver) == 0) {
81             printf("Bad key: \n");
82             exit(1);
83         }
84         if (strcmp(ver, buf) != 0) {
85             printf("\nInput key mismatch\n");
86             exit(1);
87         }
88         ka_StringToKey(buf, tcell, &tkey);
89         code = afsconf_AddKey(tdir, kvno, ktc_to_charptr(&tkey), 0);
90         if (code) {
91             printf("bos_util: failed to set key, code %d.\n", code);
92             exit(1);
93         }
94     } else if (strcmp(argv[1], "adddes") == 0) {
95         DES_cblock tkey;
96         int kvno;
97         afs_int32 code;
98         char buf[BUFSIZ], ver[BUFSIZ];
99
100         if (argc != 3) {
101             printf("bos_util adddes: usage is 'bos_util adddes <kvno>\n");
102             exit(1);
103         }
104         kvno = atoi(argv[2]);
105         memset(&tkey, 0, sizeof(struct ktc_encryptionKey));
106
107         /* prompt for key */
108         code = UI_UTIL_read_pw_string(buf, sizeof(buf), "input key: ", 0);
109         if (code || strlen(buf) == 0) {
110             printf("Bad key: \n");
111             exit(1);
112         }
113         code = UI_UTIL_read_pw_string(ver, sizeof(ver), "Retype input key: ", 0);
114         if (code || strlen(ver) == 0) {
115             printf("Bad key: \n");
116             exit(1);
117         }
118         if (strcmp(ver, buf) != 0) {
119             printf("\nInput key mismatch\n");
120             exit(1);
121         }
122         DES_string_to_key(buf, &tkey);
123         code = afsconf_AddKey(tdir, kvno, (char *) &tkey, 0);
124         if (code) {
125             printf("bos_util: failed to set key, code %d.\n", code);
126             exit(1);
127         }
128     }
129 #ifdef KERBEROS
130     else if (strcmp(argv[1], "srvtab2keyfile") == 0) {
131         char tkey[8], name[255], inst[255], realm[255];
132         int kvno;
133         if (argc != 5) {
134             printf
135                 ("bos_util add: usage is 'bos_util srvtab2keyfile <kvno> <keyfile> <princ>\n");
136             exit(1);
137         }
138         kvno = atoi(argv[2]);
139         bzero(tkey, sizeof(tkey));
140         code = kname_parse(name, inst, realm, argv[4]);
141         if (code != 0) {
142             printf("Invalid kerberos name\n");
143             exit(1);
144         }
145         code = read_service_key(name, inst, realm, kvno, argv[3], tkey);
146         if (code != 0) {
147             printf("Can't find key in %s\n", argv[3]);
148             exit(1);
149         }
150         code = afsconf_AddKey(tdir, kvno, tkey, 0);
151         if (code) {
152             printf("bos_util: failed to set key, code %d.\n", code);
153             exit(1);
154         }
155     }
156 #endif
157     else if (strcmp(argv[1], "delete") == 0) {
158         long kvno;
159         if (argc != 3) {
160             printf("bos_util delete: usage is 'bos_util delete <kvno>\n");
161             exit(1);
162         }
163         kvno = atoi(argv[2]);
164         code = afsconf_DeleteKey(tdir, kvno);
165         if (code) {
166             printf("bos_util: failed to delete key %ld, (code %d)\n", kvno,
167                    code);
168             exit(1);
169         }
170     } else if (strcmp(argv[1], "list") == 0) {
171         struct afsconf_keys tkeys;
172         int i;
173         unsigned char tbuffer[9];
174
175         code = afsconf_GetKeys(tdir, &tkeys);
176         if (code) {
177             printf("bos_util: failed to get keys, code %d\n", code);
178             exit(1);
179         }
180         for (i = 0; i < tkeys.nkeys; i++) {
181             if (tkeys.key[i].kvno != -1) {
182                 int count;
183                 unsigned char x[8];
184                 memcpy(tbuffer, tkeys.key[i].key, 8);
185                 tbuffer[8] = 0;
186                 printf("kvno %4d: key is '%s' '", tkeys.key[i].kvno, tbuffer);
187                 strcpy((char *)x, (char *)tbuffer);
188                 for (count = 0; count < 8; count++)
189                     printf("\\%03o", x[count]);
190                 printf("'\n");
191             }
192         }
193         printf("All done.\n");
194     } else {
195         printf
196             ("bos_util: unknown operation '%s', type 'bos_util' for assistance\n",
197              argv[1]);
198         exit(1);
199     }
200     exit(0);
201 }