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