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