4 * asetkey - Manipulates an AFS KeyFile
6 * Updated for Kerberos 5
10 #include <afs/param.h>
17 #ifndef HAVE_KERBEROSV_HEIM_ERR_H
18 #include <afs/com_err.h>
20 #include <afs/cellconfig.h>
22 #include <afs/dirpath.h>
24 #ifdef HAVE_KRB5_CREDS_KEYBLOCK
27 #ifdef HAVE_KRB5_CREDS_SESSION
28 #define USING_HEIMDAL 1
32 stringToType(const char *string) {
33 if (strcmp(string, "rxkad") == 0)
40 printKey(const struct rx_opaque *key)
44 for (i = 0; i < key->len; i++)
45 printf("%02x", ((unsigned char *)key->val)[i]);
53 if (c >= '0' && c <= '9')
55 if ((c >= 'a') && (c <= 'f'))
56 return (c - 'a' + 10);
58 if ((c >= 'A') && (c <= 'F'))
59 return (c - 'A' + 10);
64 static struct afsconf_typedKey *
65 keyFromCommandLine(afsconf_keyType type, int kvno, int subType,
66 const char *string, size_t length)
69 struct afsconf_typedKey *typedKey;
73 if (strlen(string) != 2*length) {
74 printf("key %s is not in right format\n", string);
75 printf(" <key> should be an %d byte hex representation \n", (int) length);
79 rx_opaque_alloc(&key, length);
81 for (i = 0; i< length; i++) {
82 ((char *)key.val)[i] = char2hex(*cp) * 16 + char2hex(*(cp+1));
86 typedKey = afsconf_typedKey_new(type, kvno, subType, &key);
87 rx_opaque_freeContents(&key);
92 #define deref_key_length(key) \
95 #define deref_key_contents(key) \
98 #define deref_key_length(key) \
101 #define deref_key_contents(key) \
105 static struct afsconf_typedKey *
106 keyFromKeytab(int kvno, const char *keytab, const char *princ)
109 krb5_principal principal;
111 krb5_context context;
112 struct rx_opaque buffer;
113 struct afsconf_typedKey *typedKey;
115 krb5_init_context(&context);
117 retval = krb5_parse_name(context, princ, &principal);
119 afs_com_err("asetkey", retval, "while parsing AFS principal");
123 retval = krb5_kt_read_service_key(context, (char *)keytab, principal,
124 kvno, ENCTYPE_DES_CBC_CRC, &key);
125 if (retval == KRB5_KT_NOTFOUND)
126 retval = krb5_kt_read_service_key(context, (char *)keytab,
128 ENCTYPE_DES_CBC_MD5, &key);
129 if (retval == KRB5_KT_NOTFOUND)
130 retval = krb5_kt_read_service_key(context, (char *)keytab,
132 ENCTYPE_DES_CBC_MD4, &key);
134 if (retval == KRB5_KT_NOTFOUND) {
135 char * princname = NULL;
137 krb5_unparse_name(context, principal, &princname);
139 afs_com_err("asetkey", retval,
140 "for keytab entry with Principal %s, kvno %u, "
141 "DES-CBC-CRC/MD5/MD4",
142 princname ? princname : princ, kvno);
147 afs_com_err("asetkey", retval, "while extracting AFS service key");
151 if (deref_key_length(key) != 8) {
152 fprintf(stderr, "Key length should be 8, but is really %u!\n",
153 (unsigned int)deref_key_length(key));
157 rx_opaque_populate(&buffer, deref_key_contents(key), deref_key_length(key));
159 typedKey = afsconf_typedKey_new(afsconf_rxkad, kvno, 0, &buffer);
160 rx_opaque_freeContents(&buffer);
161 krb5_free_principal(context, principal);
162 krb5_free_keyblock(context, key);
167 addKey(struct afsconf_dir *dir, int argc, char **argv) {
168 struct afsconf_typedKey *typedKey;
175 typedKey = keyFromCommandLine(afsconf_rxkad, atoi(argv[2]), 0,
179 typedKey = keyFromKeytab(atoi(argv[2]), argv[3], argv[4]);
182 type = stringToType(argv[2]);
183 kvno = atoi(argv[3]);
184 if (type == afsconf_rxkad) {
185 typedKey = keyFromCommandLine(afsconf_rxkad, kvno, 0, argv[5], 8);
187 fprintf(stderr, "Unknown key type %s\n", argv[2]);
192 fprintf(stderr, "%s add: usage is '%s add <kvno> <keyfile> "
193 "<princ>\n", argv[0], argv[0]);
194 fprintf(stderr, "\tOR\n\t%s add <kvno> <key>\n", argv[0]);
195 fprintf(stderr, "\tOR\n\t%s add <type> <kvno> <subtype> <key>\n",
197 fprintf(stderr, "\t\tEx: %s add 0 \"80b6a7cd7a9dadb6\"\n", argv[0]);
200 code = afsconf_AddTypedKey(dir, typedKey, 1);
201 afsconf_typedKey_put(&typedKey);
203 afs_com_err("asetkey", code, "while adding new key");
209 deleteKey(struct afsconf_dir *dir, int argc, char **argv)
215 fprintf(stderr, "%s delete: usage is '%s delete <kvno>\n",
219 kvno = atoi(argv[2]);
220 code = afsconf_DeleteKey(dir, kvno);
222 afs_com_err(argv[0], code, "while deleting key %d", kvno);
228 listKey(struct afsconf_dir *dir, int argc, char **argv)
230 struct afsconf_typedKeyList *keys;
234 code = afsconf_GetAllKeys(dir, &keys);
236 afs_com_err("asetkey", code, "while retrieving keys");
239 for (i = 0; i < keys->nkeys; i++) {
240 afsconf_keyType type;
243 struct rx_opaque *keyMaterial;
245 afsconf_typedKey_values(keys->keys[i], &type, &kvno, &minorType,
250 printf("rxkad\tkvno %4d: key is: ", kvno);
251 printKey(keyMaterial);
255 printf("unknown(%d)\tkvno %4d subtype %d key is: ", type,
257 printKey(keyMaterial);
261 printf("All done.\n");
265 main(int argc, char *argv[])
267 struct afsconf_dir *tdir;
271 fprintf(stderr, "%s: usage is '%s <opcode> options, e.g.\n",
273 fprintf(stderr, "\t%s add <kvno> <keyfile> <princ>\n", argv[0]);
274 fprintf(stderr, "\tOR\n\t%s add <kvno> <key>\n", argv[0]);
275 fprintf(stderr, "\tOR\n\t%s add <type> <kvno> <subtype> <key>\n",
277 fprintf(stderr, "\t\tEx: %s add 0 \"80b6a7cd7a9dadb6\"\n", argv[0]);
278 fprintf(stderr, "\t%s delete <kvno>\n", argv[0]);
279 fprintf(stderr, "\t%s list\n", argv[0]);
283 confdir = AFSDIR_SERVER_ETC_DIRPATH;
285 tdir = afsconf_Open(confdir);
287 fprintf(stderr, "%s: can't initialize conf dir '%s'\n", argv[0],
291 if (strcmp(argv[1], "add")==0) {
292 addKey(tdir, argc, argv);
294 else if (strcmp(argv[1], "delete")==0) {
295 deleteKey(tdir, argc, argv);
297 else if (strcmp(argv[1], "list") == 0) {
298 listKey(tdir, argc, argv);
302 fprintf(stderr, "%s: unknown operation '%s', type '%s' for "
303 "assistance\n", argv[0], argv[1], argv[0]);