Remove local crypto
[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
26 #include <sys/types.h>
27 #include <netinet/in.h>
28 #include <netdb.h>
29 #include <string.h>
30 #include <stdio.h>
31
32 #include <afs/stds.h>
33 #include <afs/afsutil.h>
34 #include <afs/keys.h>
35 #include <afs/cellconfig.h>
36 #include <afs/kautils.h>
37 #include <hcrypto/ui.h>
38 #include <hcrypto/des.h>
39
40 int
41 main(int argc, char **argv)
42 {
43     struct afsconf_dir *tdir;
44     afs_int32 code;
45
46     if (argc == 1) {
47         printf("bos_util: usage is 'bos_util <opcode> options, e.g.\n");
48         printf("    bos_util add <kvno>\n");
49         printf("    bos_util adddes <kvno>\n");
50 #ifdef KERBEROS
51         printf("    bos_util srvtab2keyfile <kvno> <keyfile> <princ>\n");
52 #endif
53         printf("    bos_util delete <kvno>\n");
54         printf("    bos_util list\n");
55         exit(1);
56     }
57
58     tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIR);
59     if (!tdir) {
60         printf("bos_util: can't initialize conf dir '%s'\n",
61                AFSDIR_SERVER_ETC_DIR);
62         exit(1);
63     }
64     if (strcmp(argv[1], "add") == 0) {
65         struct ktc_encryptionKey tkey;
66         int kvno;
67         char buf[BUFSIZ], ver[BUFSIZ];
68         char *tcell = NULL;
69
70         if (argc != 3) {
71             printf("bos_util add: usage is 'bos_util add <kvno>\n");
72             exit(1);
73         }
74         kvno = atoi(argv[2]);
75         memset(&tkey, 0, sizeof(struct ktc_encryptionKey));
76
77         /* prompt for key */
78         code = UI_UTIL_read_pw_string(buf, sizeof(buf), "input key: ", 0);
79         if (code || strlen(buf) == 0) {
80             printf("Bad key: \n");
81             exit(1);
82         }
83         code = UI_UTIL_read_pw_string(ver, sizeof(ver), "Retype input key: ", 0);
84         if (code || strlen(ver) == 0) {
85             printf("Bad key: \n");
86             exit(1);
87         }
88         if (strcmp(ver, buf) != 0) {
89             printf("\nInput key mismatch\n");
90             exit(1);
91         }
92         ka_StringToKey(buf, tcell, &tkey);
93         code = afsconf_AddKey(tdir, kvno, ktc_to_charptr(&tkey), 0);
94         if (code) {
95             printf("bos_util: failed to set key, code %d.\n", code);
96             exit(1);
97         }
98     } else if (strcmp(argv[1], "adddes") == 0) {
99         DES_cblock tkey;
100         int kvno;
101         afs_int32 code;
102         char buf[BUFSIZ], ver[BUFSIZ];
103
104         if (argc != 3) {
105             printf("bos_util adddes: usage is 'bos_util adddes <kvno>\n");
106             exit(1);
107         }
108         kvno = atoi(argv[2]);
109         memset(&tkey, 0, sizeof(struct ktc_encryptionKey));
110
111         /* prompt for key */
112         code = UI_UTIL_read_pw_string(buf, sizeof(buf), "input key: ", 0);
113         if (code || strlen(buf) == 0) {
114             printf("Bad key: \n");
115             exit(1);
116         }
117         code = UI_UTIL_read_pw_string(ver, sizeof(ver), "Retype input key: ", 0);
118         if (code || strlen(ver) == 0) {
119             printf("Bad key: \n");
120             exit(1);
121         }
122         if (strcmp(ver, buf) != 0) {
123             printf("\nInput key mismatch\n");
124             exit(1);
125         }
126         DES_string_to_key(buf, &tkey);
127         code = afsconf_AddKey(tdir, kvno, (char *) &tkey, 0);
128         if (code) {
129             printf("bos_util: failed to set key, code %d.\n", code);
130             exit(1);
131         }
132     }
133 #ifdef KERBEROS
134     else if (strcmp(argv[1], "srvtab2keyfile") == 0) {
135         char tkey[8], name[255], inst[255], realm[255];
136         int kvno;
137         if (argc != 5) {
138             printf
139                 ("bos_util add: usage is 'bos_util srvtab2keyfile <kvno> <keyfile> <princ>\n");
140             exit(1);
141         }
142         kvno = atoi(argv[2]);
143         bzero(tkey, sizeof(tkey));
144         code = kname_parse(name, inst, realm, argv[4]);
145         if (code != 0) {
146             printf("Invalid kerberos name\n");
147             exit(1);
148         }
149         code = read_service_key(name, inst, realm, kvno, argv[3], tkey);
150         if (code != 0) {
151             printf("Can't find key in %s\n", argv[3]);
152             exit(1);
153         }
154         code = afsconf_AddKey(tdir, kvno, tkey, 0);
155         if (code) {
156             printf("bos_util: failed to set key, code %d.\n", code);
157             exit(1);
158         }
159     }
160 #endif
161     else if (strcmp(argv[1], "delete") == 0) {
162         long kvno;
163         if (argc != 3) {
164             printf("bos_util delete: usage is 'bos_util delete <kvno>\n");
165             exit(1);
166         }
167         kvno = atoi(argv[2]);
168         code = afsconf_DeleteKey(tdir, kvno);
169         if (code) {
170             printf("bos_util: failed to delete key %ld, (code %d)\n", kvno,
171                    code);
172             exit(1);
173         }
174     } else if (strcmp(argv[1], "list") == 0) {
175         struct afsconf_keys tkeys;
176         int i;
177         unsigned char tbuffer[9];
178
179         code = afsconf_GetKeys(tdir, &tkeys);
180         if (code) {
181             printf("bos_util: failed to get keys, code %d\n", code);
182             exit(1);
183         }
184         for (i = 0; i < tkeys.nkeys; i++) {
185             if (tkeys.key[i].kvno != -1) {
186                 int count;
187                 unsigned char x[8];
188                 memcpy(tbuffer, tkeys.key[i].key, 8);
189                 tbuffer[8] = 0;
190                 printf("kvno %4d: key is '%s' '", tkeys.key[i].kvno, tbuffer);
191                 strcpy((char *)x, (char *)tbuffer);
192                 for (count = 0; count < 8; count++)
193                     printf("\\%03o", x[count]);
194                 printf("'\n");
195             }
196         }
197         printf("All done.\n");
198     } else {
199         printf
200             ("bos_util: unknown operation '%s', type 'bos_util' for assistance\n",
201              argv[1]);
202         exit(1);
203     }
204     exit(0);
205 }