windows-talocale-20060829
[openafs.git] / src / WINNT / aklog / asetkey.c
1 /*
2  * $Id$
3  *
4  * asetkey - Manipulates an AFS KeyFile
5  *
6  * Updated for Kerberos 5
7  */
8
9 #include <winsock.h>
10
11 #include <sys/types.h>
12 #include <krb5.h>
13
14 #include <afs/stds.h>
15 #include <afs/cellconfig.h>
16 #include <afs/keys.h>
17 #ifndef PRE_AFS35
18 #include <afs/dirpath.h>
19 #endif /* !PRE_AFS35 */
20
21 int
22 main(int argc, char **argv)
23 {
24     struct afsconf_dir *tdir;
25     register long code;
26     const char *confdir;
27
28     if (argc == 1) {
29         printf("asetkey: usage is 'setkey <opcode> options, e.g.\n");
30         printf("    asetkey add <kvno> <keyfile> <princ>\n");
31         printf("    asetkey delete <kvno>\n");
32         printf("    asetkey list\n");
33         exit(1);
34     }
35
36 #ifdef PRE_AFS35
37     confdir = AFSCONF_SERVERNAME;
38 #else /* PRE_AFS35 */
39     confdir = AFSDIR_SERVER_ETC_DIRPATH;
40 #endif /* PRE_AFS35 */
41
42     tdir = afsconf_Open(confdir);
43     if (!tdir) {
44         printf("asetkey: can't initialize conf dir '%s'\n", confdir);
45         exit(1);
46     }
47     if (strcmp(argv[1], "add")==0) {
48         krb5_context context;
49         krb5_principal principal;
50         krb5_keyblock *key;
51         krb5_error_code retval;
52         int kvno;
53
54         if (argc != 5) {
55             printf("asetkey add: usage is 'asetkey add <kvno> <keyfile> <princ>\n");
56             exit(1);
57         }
58
59         krb5_init_context(&context);
60
61         kvno = atoi(argv[2]);
62         retval = krb5_parse_name(context, argv[4], &principal);
63         if (retval != 0) {
64                 com_err(argv[0], retval, "while parsing AFS principal");
65                 exit(1);
66         }
67         retval = krb5_kt_read_service_key(context, argv[3], principal, kvno,
68                                           ENCTYPE_DES_CBC_CRC, &key);
69         if (retval != 0) {
70                 com_err(argv[0], retval, "while extracting AFS service key");
71                 exit(1);
72         }
73
74         if (key->length != 8) {
75                 printf("Key length should be 8, but is really %d!\n",
76                        key->length);
77                 exit(1);
78         }
79
80         code = afsconf_AddKey(tdir, kvno, key->contents, 1);
81         if (code) {
82             printf("asetkey: failed to set key, code %d.\n", code);
83             exit(1);
84         }
85         krb5_free_principal(context, principal);
86         krb5_free_keyblock(context, key);
87     }
88     else if (strcmp(argv[1], "delete")==0) {
89         long kvno;
90         if (argc != 3) {
91             printf("asetkey delete: usage is 'asetkey delete <kvno>\n");
92             exit(1);
93         }
94         kvno = atoi(argv[2]);
95         code = afsconf_DeleteKey(tdir, kvno);
96         if (code) {
97             printf("asetkey: failed to delete key %d, (code %d)\n", kvno, code);
98             exit(1);
99         }
100     }
101     else if (strcmp(argv[1], "list") == 0) {
102         struct afsconf_keys tkeys;
103         register int i, j;
104         
105         code = afsconf_GetKeys(tdir, &tkeys);
106         if (code) {
107             printf("asetkey: failed to get keys, code %d\n", code);
108             exit(1);
109         }
110         for(i=0;i<tkeys.nkeys;i++) {
111             if (tkeys.key[i].kvno != -1) {
112                 printf("kvno %4d: key is: ", tkeys.key[i].kvno);
113                 for (j = 0; j < 8; j++)
114                         printf("%02x", (unsigned char) tkeys.key[i].key[j]);
115                 printf("\n");
116             }
117         }
118         printf("All done.\n");
119     }
120     else {
121         printf("asetkey: unknown operation '%s', type 'asetkey' for assistance\n", argv[1]);
122         exit(1);
123     }
124     exit(0);
125 }