c29b79790f5e6ff25fffeac9c9a66b0b3db4f932
[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 RCSID
26     ("$Header$");
27
28 #include <sys/types.h>
29 #include <netinet/in.h>
30 #include <netdb.h>
31 #include <string.h>
32
33 #include <afs/stds.h>
34 #include <afs/afsutil.h>
35 #include <rx/rxkad.h>
36 #include <afs/keys.h>
37 #include <afs/cellconfig.h>
38
39 int
40 main(int argc, char **argv)
41 {
42     struct afsconf_dir *tdir;
43     register afs_int32 code;
44
45     if (argc == 1) {
46         printf("bos_util: usage is 'bos_util <opcode> options, e.g.\n");
47         printf("    bos_util add <kvno>\n");
48         printf("    bos_util adddes <kvno>\n");
49 #ifdef KERBEROS
50         printf("    bos_util srvtab2keyfile <kvno> <keyfile> <princ>\n");
51 #endif
52         printf("    bos_util delete <kvno>\n");
53         printf("    bos_util list\n");
54         exit(1);
55     }
56
57     tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIR);
58     if (!tdir) {
59         printf("bos_util: can't initialize conf dir '%s'\n",
60                AFSDIR_SERVER_ETC_DIR);
61         exit(1);
62     }
63     if (strcmp(argv[1], "add") == 0) {
64         struct ktc_encryptionKey tkey;
65         int kvno;
66         char buf[BUFSIZ], ver[BUFSIZ];
67         char *tcell = NULL;
68
69         if (argc != 3) {
70             printf("bos_util add: usage is 'bos_util add <kvno>\n");
71             exit(1);
72         }
73         kvno = atoi(argv[2]);
74         memset(&tkey, 0, sizeof(struct ktc_encryptionKey));
75
76         /* prompt for key */
77         code = des_read_pw_string(buf, sizeof(buf), "input key: ", 0);
78         if (code || strlen(buf) == 0) {
79             printf("Bad key: \n");
80             exit(1);
81         }
82         code = des_read_pw_string(ver, sizeof(ver), "Retype input key: ", 0);
83         if (code || strlen(ver) == 0) {
84             printf("Bad key: \n");
85             exit(1);
86         }
87         if (strcmp(ver, buf) != 0) {
88             printf("\nInput key mismatch\n");
89             exit(1);
90         }
91         ka_StringToKey(buf, tcell, &tkey);
92         code = afsconf_AddKey(tdir, kvno, &tkey, 0);
93         if (code) {
94             printf("bos_util: failed to set key, code %d.\n", code);
95             exit(1);
96         }
97     } else if (strcmp(argv[1], "adddes") == 0) {
98         struct ktc_encryptionKey tkey;
99         int kvno;
100         register afs_int32 code;
101         char buf[BUFSIZ], ver[BUFSIZ];
102         char *tcell = NULL;
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 = des_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 = des_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, &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 %d, (code %d)\n", kvno,
171                    code);
172             exit(1);
173         }
174     } else if (strcmp(argv[1], "list") == 0) {
175         struct afsconf_keys tkeys;
176         register 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(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 }