DEVEL15-openafs-string-header-cleanup-20071030
[openafs.git] / src / aklog / asetkey.c
1 /*
2  * $Id$
3  *
4  * asetkey - Manipulates an AFS KeyFile
5  *
6  * Updated for Kerberos 5
7  */
8
9 #include <afsconfig.h>
10 #include <stdio.h>
11 #include <sys/types.h>
12 #include <netinet/in.h>
13 #include <netdb.h>
14 #ifdef HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #include <stdlib.h>
18 #ifdef HAVE_MEMORY_H
19 #include <memory.h>
20 #endif /* HAVE_MEMORY_H */
21 #include <string.h>
22
23 #include <afs/stds.h>
24 #include <krb5.h>
25
26 #include <afs/com_err.h>
27 #include <afs/cellconfig.h>
28 #include <afs/keys.h>
29 #include <afs/dirpath.h>
30
31 #ifdef HAVE_KRB5_CREDS_KEYBLOCK
32 #define USING_MIT 1
33 #endif
34 #ifdef HAVE_KRB5_CREDS_SESSION
35 #define USING_HEIMDAL 1
36 #endif
37
38 int
39 main(int argc, char *argv[])
40 {
41     struct afsconf_dir *tdir;
42     register long code;
43     const char *confdir;
44
45     if (argc == 1) {
46         fprintf(stderr, "%s: usage is '%s <opcode> options, e.g.\n",
47                 argv[0], argv[0]);
48         fprintf(stderr, "\t%s add <kvno> <keyfile> <princ>\n", argv[0]);
49         fprintf(stderr, "\t%s delete <kvno>\n", argv[0]);
50         fprintf(stderr, "\t%s list\n", argv[0]);
51         exit(1);
52     }
53
54     confdir = AFSDIR_SERVER_ETC_DIRPATH;
55
56     tdir = afsconf_Open(confdir);
57     if (!tdir) {
58         fprintf(stderr, "%s: can't initialize conf dir '%s'\n", argv[0],
59                 confdir);
60         exit(1);
61     }
62     if (strcmp(argv[1], "add")==0) {
63         krb5_context context;
64         krb5_principal principal;
65         krb5_keyblock *key;
66         krb5_error_code retval;
67         int kvno;
68
69         if (argc != 5) {
70             fprintf(stderr, "%s add: usage is '%s add <kvno> <keyfile> "
71                     "<princ>\n", argv[0], argv[0]);
72             exit(1);
73         }
74
75         krb5_init_context(&context);
76
77         kvno = atoi(argv[2]);
78         retval = krb5_parse_name(context, argv[4], &principal);
79         if (retval != 0) {
80                 afs_com_err(argv[0], retval, "while parsing AFS principal");
81                 exit(1);
82         }
83         retval = krb5_kt_read_service_key(context, argv[3], principal, kvno,
84                                           ENCTYPE_DES_CBC_CRC, &key);
85         if (retval != 0) {
86                 afs_com_err(argv[0], retval, "while extracting AFS service key");
87                 exit(1);
88         }
89
90 #ifdef USING_HEIMDAL
91 #define deref_key_length(key) \
92           key->keyvalue.length
93
94 #define deref_key_contents(key) \
95         key->keyvalue.data
96 #else
97 #define deref_key_length(key) \
98           key->length
99
100 #define deref_key_contents(key) \
101         key->contents
102 #endif
103         if (deref_key_length(key) != 8) {
104                 fprintf(stderr, "Key length should be 8, but is really %d!\n",
105                         deref_key_length(key));
106                 exit(1);
107         }
108
109         code = afsconf_AddKey(tdir, kvno, (char *) deref_key_contents(key), 1);
110         if (code) {
111             fprintf(stderr, "%s: failed to set key, code %d.\n", argv[0], code);
112             exit(1);
113         }
114         krb5_free_principal(context, principal);
115         krb5_free_keyblock(context, key);
116     }
117     else if (strcmp(argv[1], "delete")==0) {
118         long kvno;
119         if (argc != 3) {
120             fprintf(stderr, "%s delete: usage is '%s delete <kvno>\n",
121                     argv[0], argv[0]);
122             exit(1);
123         }
124         kvno = atoi(argv[2]);
125         code = afsconf_DeleteKey(tdir, kvno);
126         if (code) {
127             fprintf(stderr, "%s: failed to delete key %d, (code %d)\n",
128                     argv[0], kvno, code);
129             exit(1);
130         }
131     }
132     else if (strcmp(argv[1], "list") == 0) {
133         struct afsconf_keys tkeys;
134         register int i, j;
135         
136         code = afsconf_GetKeys(tdir, &tkeys);
137         if (code) {
138             fprintf(stderr, "%s: failed to get keys, code %d\n", argv[0], code);
139             exit(1);
140         }
141         for(i=0;i<tkeys.nkeys;i++) {
142             if (tkeys.key[i].kvno != -1) {
143                 printf("kvno %4d: key is: ", tkeys.key[i].kvno);
144                 for (j = 0; j < 8; j++)
145                         printf("%02x", (unsigned char) tkeys.key[i].key[j]);
146                 printf("\n");
147             }
148         }
149         printf("All done.\n");
150     }
151     else {
152         fprintf(stderr, "%s: unknown operation '%s', type '%s' for "
153                 "assistance\n", argv[0], argv[1], argv[0]);
154         exit(1);
155     }
156     exit(0);
157 }