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