| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | #include <afsconfig.h> |
| 10 | #include <afs/param.h> |
| 11 | #include <afs/stds.h> |
| 12 | |
| 13 | #include <roken.h> |
| 14 | |
| 15 | #include <krb5.h> |
| 16 | |
| 17 | #ifndef HAVE_KERBEROSV_HEIM_ERR_H |
| 18 | #include <afs/com_err.h> |
| 19 | #endif |
| 20 | #include <afs/cellconfig.h> |
| 21 | #include <afs/keys.h> |
| 22 | #include <afs/dirpath.h> |
| 23 | |
| 24 | #ifdef HAVE_KRB5_CREDS_KEYBLOCK |
| 25 | #define USING_MIT 1 |
| 26 | #endif |
| 27 | #ifdef HAVE_KRB5_CREDS_SESSION1 |
| 28 | #define USING_HEIMDAL1 1 |
| 29 | #endif |
| 30 | |
| 31 | static int |
| 32 | stringToType(const char *string) { |
| 33 | if (strcmp(string, "rxkad") == 0) |
| 34 | return afsconf_rxkad; |
| 35 | |
| 36 | return atoi(string); |
| 37 | } |
| 38 | |
| 39 | static void |
| 40 | printKey(const struct rx_opaque *key) |
| 41 | { |
| 42 | int i; |
| 43 | |
| 44 | for (i = 0; i < key->len; i++) |
| 45 | printf("%02x", ((unsigned char *)key->val)[i]); |
| 46 | printf("\n"); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | static int |
| 51 | char2hex(char c) |
| 52 | { |
| 53 | if (c >= '0' && c <= '9') |
| 54 | return (c - 48); |
| 55 | if ((c >= 'a') && (c <= 'f')) |
| 56 | return (c - 'a' + 10); |
| 57 | |
| 58 | if ((c >= 'A') && (c <= 'F')) |
| 59 | return (c - 'A' + 10); |
| 60 | |
| 61 | return -1; |
| 62 | } |
| 63 | |
| 64 | static struct afsconf_typedKey * |
| 65 | keyFromCommandLine(afsconf_keyType type, int kvno, int subType, |
| 66 | const char *string, size_t length) |
| 67 | { |
| 68 | struct rx_opaque key; |
| 69 | struct afsconf_typedKey *typedKey; |
| 70 | const char *cp; |
| 71 | int i; |
| 72 | |
| 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); |
| 76 | exit(1); |
| 77 | } |
| 78 | |
| 79 | rx_opaque_alloc(&key, length); |
| 80 | cp = string; |
| 81 | for (i = 0; i< length; i++) { |
| 82 | ((char *)key.val)[i] = char2hex(*cp) * 16 + char2hex(*(cp+1)); |
| 83 | cp+=2; |
| 84 | } |
| 85 | |
| 86 | typedKey = afsconf_typedKey_new(type, kvno, subType, &key); |
| 87 | rx_opaque_freeContents(&key); |
| 88 | return typedKey; |
| 89 | } |
| 90 | |
| 91 | #ifdef USING_HEIMDAL1 |
| 92 | #define deref_key_length(key)key->keyvalue.length \ |
| 93 | key->keyvalue.length |
| 94 | |
| 95 | #define deref_key_contents(key)key->keyvalue.data \ |
| 96 | key->keyvalue.data |
| 97 | #else |
| 98 | #define deref_key_length(key)key->keyvalue.length \ |
| 99 | key->length |
| 100 | |
| 101 | #define deref_key_contents(key)key->keyvalue.data \ |
| 102 | key->contents |
| 103 | #endif |
| 104 | |
| 105 | static struct afsconf_typedKey * |
| 106 | keyFromKeytab(int kvno, const char *keytab, const char *princ) |
| 107 | { |
| 108 | int retval; |
| 109 | krb5_principal principal; |
| 110 | krb5_keyblock *key; |
| 111 | krb5_context context; |
| 112 | struct rx_opaque buffer; |
| 113 | struct afsconf_typedKey *typedKey; |
| 114 | |
| 115 | krb5_init_context(&context); |
| 116 | |
| 117 | retval = krb5_parse_name(context, princ, &principal); |
| 118 | if (retval) { |
| 119 | afs_com_err("asetkey", retval, "while parsing AFS principal"); |
| 120 | exit(1); |
| 121 | } |
| 122 | |
| 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, |
| 127 | principal, kvno, |
| 128 | ENCTYPE_DES_CBC_MD5, &key); |
| 129 | if (retval == KRB5_KT_NOTFOUND) |
| 130 | retval = krb5_kt_read_service_key(context, (char *)keytab, |
| 131 | principal, kvno, |
| 132 | ENCTYPE_DES_CBC_MD4, &key); |
| 133 | |
| 134 | if (retval == KRB5_KT_NOTFOUND) { |
| 135 | char * princname = NULL((void *)0); |
| 136 | |
| 137 | krb5_unparse_name(context, principal, &princname); |
| 138 | |
| 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); |
| 143 | exit(1); |
| 144 | } |
| 145 | |
| 146 | if (retval != 0) { |
| 147 | afs_com_err("asetkey", retval, "while extracting AFS service key"); |
| 148 | exit(1); |
| 149 | } |
| 150 | |
| 151 | if (deref_key_length(key)key->keyvalue.length != 8) { |
| 152 | fprintf(stderr__stderrp, "Key length should be 8, but is really %u!\n", |
| 153 | (unsigned int)deref_key_length(key)key->keyvalue.length); |
| 154 | exit(1); |
| 155 | } |
| 156 | |
| 157 | rx_opaque_populate(&buffer, deref_key_contents(key)key->keyvalue.data, deref_key_length(key)key->keyvalue.length); |
| 158 | |
| 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); |
| 163 | return typedKey; |
| 164 | } |
| 165 | |
| 166 | static void |
| 167 | addKey(struct afsconf_dir *dir, int argc, char **argv) { |
| 168 | struct afsconf_typedKey *typedKey; |
| 169 | int type; |
| 170 | int kvno; |
| 171 | int code; |
| 172 | |
| 173 | switch (argc) { |
| 1 | Control jumps to 'case 6:' at line 181 |
|
| 174 | case 4: |
| 175 | typedKey = keyFromCommandLine(afsconf_rxkad, atoi(argv[2]), 0, |
| 176 | argv[3], 8); |
| 177 | break; |
| 178 | case 5: |
| 179 | typedKey = keyFromKeytab(atoi(argv[2]), argv[3], argv[4]); |
| 180 | break; |
| 181 | case 6: |
| 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); |
| 186 | } |
| 187 | break; |
| 3 | Execution continues on line 197 |
|
| 188 | default: |
| 189 | fprintf(stderr__stderrp, "%s add: usage is '%s add <kvno> <keyfile> " |
| 190 | "<princ>\n", argv[0], argv[0]); |
| 191 | fprintf(stderr__stderrp, "\tOR\n\t%s add <kvno> <key>\n", argv[0]); |
| 192 | fprintf(stderr__stderrp, "\tOR\n\t%s add <type> <kvno> <subtype> <key>\n", |
| 193 | argv[0]); |
| 194 | fprintf(stderr__stderrp, "\t\tEx: %s add 0 \"80b6a7cd7a9dadb6\"\n", argv[0]); |
| 195 | exit(1); |
| 196 | } |
| 197 | code = afsconf_AddTypedKey(dir, typedKey, 1); |
| 4 | Function call argument is an uninitialized value |
|
| 198 | afsconf_typedKey_put(&typedKey); |
| 199 | if (code) { |
| 200 | afs_com_err("asetkey", code, "while adding new key"); |
| 201 | exit(1); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | static void |
| 206 | deleteKey(struct afsconf_dir *dir, int argc, char **argv) |
| 207 | { |
| 208 | int kvno; |
| 209 | int code; |
| 210 | |
| 211 | if (argc != 3) { |
| 212 | fprintf(stderr__stderrp, "%s delete: usage is '%s delete <kvno>\n", |
| 213 | argv[0], argv[0]); |
| 214 | exit(1); |
| 215 | } |
| 216 | kvno = atoi(argv[2]); |
| 217 | code = afsconf_DeleteKey(dir, kvno); |
| 218 | if (code) { |
| 219 | afs_com_err(argv[0], code, "while deleting key %d", kvno); |
| 220 | exit(1); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | static void |
| 225 | listKey(struct afsconf_dir *dir, int argc, char **argv) |
| 226 | { |
| 227 | struct afsconf_typedKeyList *keys; |
| 228 | int i; |
| 229 | int code; |
| 230 | |
| 231 | code = afsconf_GetAllKeys(dir, &keys); |
| 232 | if (code) { |
| 233 | afs_com_err("asetkey", code, "while retrieving keys"); |
| 234 | exit(1); |
| 235 | } |
| 236 | for (i = 0; i < keys->nkeys; i++) { |
| 237 | afsconf_keyType type; |
| 238 | int kvno; |
| 239 | int minorType; |
| 240 | struct rx_opaque *keyMaterial; |
| 241 | |
| 242 | afsconf_typedKey_values(keys->keys[i], &type, &kvno, &minorType, |
| 243 | &keyMaterial); |
| 244 | switch(type) { |
| 245 | case afsconf_rxkad: |
| 246 | if (kvno != -1) { |
| 247 | printf("rxkad\tkvno %4d: key is: ", kvno); |
| 248 | printKey(keyMaterial); |
| 249 | } |
| 250 | break; |
| 251 | default: |
| 252 | printf("unknown(%d)\tkvno %4d subtype %d key is: ", type, |
| 253 | kvno, minorType); |
| 254 | printKey(keyMaterial); |
| 255 | break; |
| 256 | } |
| 257 | } |
| 258 | printf("All done.\n"); |
| 259 | } |
| 260 | |
| 261 | int |
| 262 | main(int argc, char *argv[]) |
| 263 | { |
| 264 | struct afsconf_dir *tdir; |
| 265 | const char *confdir; |
| 266 | |
| 267 | if (argc == 1) { |
| 268 | fprintf(stderr__stderrp, "%s: usage is '%s <opcode> options, e.g.\n", |
| 269 | argv[0], argv[0]); |
| 270 | fprintf(stderr__stderrp, "\t%s add <kvno> <keyfile> <princ>\n", argv[0]); |
| 271 | fprintf(stderr__stderrp, "\tOR\n\t%s add <kvno> <key>\n", argv[0]); |
| 272 | fprintf(stderr__stderrp, "\tOR\n\t%s add <type> <kvno> <subtype> <key>\n", |
| 273 | argv[0]); |
| 274 | fprintf(stderr__stderrp, "\t\tEx: %s add 0 \"80b6a7cd7a9dadb6\"\n", argv[0]); |
| 275 | fprintf(stderr__stderrp, "\t%s delete <kvno>\n", argv[0]); |
| 276 | fprintf(stderr__stderrp, "\t%s list\n", argv[0]); |
| 277 | exit(1); |
| 278 | } |
| 279 | |
| 280 | confdir = AFSDIR_SERVER_ETC_DIRPATHgetDirPath(AFSDIR_SERVER_ETC_DIRPATH_ID); |
| 281 | |
| 282 | tdir = afsconf_Open(confdir); |
| 283 | if (!tdir) { |
| 284 | fprintf(stderr__stderrp, "%s: can't initialize conf dir '%s'\n", argv[0], |
| 285 | confdir); |
| 286 | exit(1); |
| 287 | } |
| 288 | if (strcmp(argv[1], "add")==0) { |
| 289 | addKey(tdir, argc, argv); |
| 290 | } |
| 291 | else if (strcmp(argv[1], "delete")==0) { |
| 292 | deleteKey(tdir, argc, argv); |
| 293 | } |
| 294 | else if (strcmp(argv[1], "list") == 0) { |
| 295 | listKey(tdir, argc, argv); |
| 296 | |
| 297 | } |
| 298 | else { |
| 299 | fprintf(stderr__stderrp, "%s: unknown operation '%s', type '%s' for " |
| 300 | "assistance\n", argv[0], argv[1], argv[0]); |
| 301 | exit(1); |
| 302 | } |
| 303 | exit(0); |
| 304 | } |