4 * asetkey - Manipulates an AFS KeyFile
6 * Updated for Kerberos 5
9 * Copyright (c) 2007 Secure Endpoints Inc.
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
17 * * Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * * Neither the name of the Secure Endpoints Inc. nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <sys/types.h>
43 #include <afs/cellconfig.h>
46 #include <afs/dirpath.h>
47 #endif /* !PRE_AFS35 */
48 #include <afs/com_err.h>
51 validate_krb5_availability(void)
54 #define KRB5LIB "krb5_32.dll"
56 #define KRB5LIB "krb5_64.dll"
58 HINSTANCE h = LoadLibrary(KRB5LIB);
62 fprintf(stderr, "Kerberos for Windows library %s is not available.\n", KRB5LIB);
68 main(int argc, char **argv)
70 struct afsconf_dir *tdir;
74 validate_krb5_availability();
77 printf("asetkey: usage is 'setkey <opcode> options, e.g.\n");
78 printf(" asetkey add <kvno> <keyfile> <princ>\n");
79 printf(" asetkey delete <kvno>\n");
80 printf(" asetkey list\n");
85 confdir = AFSCONF_SERVERNAME;
87 confdir = AFSDIR_SERVER_ETC_DIRPATH;
88 #endif /* PRE_AFS35 */
90 tdir = afsconf_Open(confdir);
92 printf("asetkey: can't initialize conf dir '%s'\n", confdir);
95 if (strcmp(argv[1], "add")==0) {
97 krb5_principal principal;
99 krb5_error_code retval;
103 printf("asetkey add: usage is 'asetkey add <kvno> <keyfile> <princ>\n");
107 krb5_init_context(&context);
109 kvno = atoi(argv[2]);
110 retval = krb5_parse_name(context, argv[4], &principal);
112 afs_com_err(argv[0], retval, "while parsing AFS principal");
115 retval = krb5_kt_read_service_key(context, argv[3], principal, kvno,
116 ENCTYPE_DES_CBC_CRC, &key);
117 if (retval == KRB5_KT_NOTFOUND)
118 retval = krb5_kt_read_service_key(context, argv[3], principal, kvno,
119 ENCTYPE_DES_CBC_MD5, &key);
120 if (retval == KRB5_KT_NOTFOUND)
121 retval = krb5_kt_read_service_key(context, argv[3], principal, kvno,
122 ENCTYPE_DES_CBC_MD4, &key);
123 if (retval == KRB5_KT_NOTFOUND) {
124 char * princname = NULL;
126 krb5_unparse_name(context, principal, &princname);
128 afs_com_err(argv[0], retval,
129 "for keytab entry with Principal %s, kvno %u, DES-CBC-CRC/MD5/MD4",
130 princname ? princname : argv[4],
133 } else if (retval != 0) {
134 afs_com_err(argv[0], retval, "while extracting AFS service key");
138 if (key->length != 8) {
139 printf("Key length should be 8, but is really %d!\n",
144 code = afsconf_AddKey(tdir, kvno, key->contents, 1);
146 printf("asetkey: failed to set key, code %d.\n", code);
149 krb5_free_principal(context, principal);
150 krb5_free_keyblock(context, key);
152 else if (strcmp(argv[1], "delete")==0) {
155 printf("asetkey delete: usage is 'asetkey delete <kvno>\n");
158 kvno = atoi(argv[2]);
159 code = afsconf_DeleteKey(tdir, kvno);
161 printf("asetkey: failed to delete key %d, (code %d)\n", kvno, code);
165 else if (strcmp(argv[1], "list") == 0) {
166 struct afsconf_keys tkeys;
169 code = afsconf_GetKeys(tdir, &tkeys);
171 printf("asetkey: failed to get keys, code %d\n", code);
174 for(i=0;i<tkeys.nkeys;i++) {
175 if (tkeys.key[i].kvno != -1) {
176 printf("kvno %4d: key is: ", tkeys.key[i].kvno);
177 for (j = 0; j < 8; j++)
178 printf("%02x", (unsigned char) tkeys.key[i].key[j]);
182 printf("All done.\n");
185 printf("asetkey: unknown operation '%s', type 'asetkey' for assistance\n", argv[1]);