4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (C) 2015, Trustees of Indiana University
25 * Author: Jeremy Filizetti <jfilizet@iu.edu>
37 #include <sys/types.h>
40 #include <lnet/nidstr.h>
41 #include <lustre/lustre_idl.h>
50 /* One week default expiration */
51 #define SK_DEFAULT_EXPIRE 604800
52 #define SK_DEFAULT_SK_KEYLEN 256
53 #define SK_DEFAULT_PRIME_BITS 2048
54 #define SK_DEFAULT_NODEMAP "default"
56 /* Names match up with openssl enc and dgst commands */
57 char *sk_crypt2name[] = {
58 [SK_CRYPT_EMPTY] = "NONE",
59 [SK_CRYPT_AES256_CTR] = "AES-256-CTR",
62 char *sk_hmac2name[] = {
63 [SK_HMAC_EMPTY] = "NONE",
64 [SK_HMAC_SHA256] = "SHA256",
65 [SK_HMAC_SHA512] = "SHA512",
68 static int sk_name2crypt(char *name)
72 for (i = 0; i < SK_CRYPT_MAX; i++) {
73 if (strcasecmp(name, sk_crypt2name[i]) == 0)
77 return SK_CRYPT_INVALID;
80 static int sk_name2hmac(char *name)
84 for (i = 0; i < SK_HMAC_MAX; i++) {
85 if (strcasecmp(name, sk_hmac2name[i]) == 0)
89 return SK_HMAC_INVALID;
92 static void usage(FILE *fp, char *program)
96 fprintf(fp, "Usage %s [OPTIONS] {-l|-m|-r|-w} <keyfile>\n", program);
97 fprintf(fp, "-l|--load <keyfile> Load key from file into user's "
99 fprintf(fp, "-m|--modify <keyfile> Modify keyfile's attributes\n");
100 fprintf(fp, "-r|--read <keyfile> Show keyfile's attributes\n");
101 fprintf(fp, "-w|--write <keyfile> Generate keyfile\n\n");
102 fprintf(fp, "Modify/Write Options:\n");
103 fprintf(fp, "-c|--crypt <num> Cipher for encryption "
104 "(Default: AES Counter mode)\n");
105 for (i = 1; i < SK_CRYPT_MAX; i++)
106 fprintf(fp, " %s\n", sk_crypt2name[i]);
108 fprintf(fp, "-i|--hmac <num> Hash algorithm for integrity "
109 "(Default: SHA256)\n");
110 for (i = 1; i < SK_HMAC_MAX; i++)
111 fprintf(fp, " %s\n", sk_hmac2name[i]);
113 fprintf(fp, "-e|--expire <num> Seconds before contexts from "
114 "key expire (Default: %d seconds (%.3g days))\n",
115 SK_DEFAULT_EXPIRE, (double)SK_DEFAULT_EXPIRE / 3600 / 24);
116 fprintf(fp, "-f|--fsname <name> File system name for key\n");
117 fprintf(fp, "-g|--mgsnids <nids> Comma seperated list of MGS "
118 "NIDs. Only required when mgssec is used (Default: \"\")\n");
119 fprintf(fp, "-n|--nodemap <name> Nodemap name for key "
120 "(Default: \"%s\")\n", SK_DEFAULT_NODEMAP);
121 fprintf(fp, "-p|--prime-bits <len> Prime length (p) for DHKE in "
122 "bits (Default: %d)\n", SK_DEFAULT_PRIME_BITS);
123 fprintf(fp, "-t|--type <type> Key type (mgs, server, "
125 fprintf(fp, "-k|--key-bits <len> Shared key length in bits "
126 "(Default: %d)\n", SK_DEFAULT_SK_KEYLEN);
127 fprintf(fp, "-d|--data <file> Key random data source "
128 "(Default: /dev/random)\n\n");
129 fprintf(fp, "Other Options:\n");
130 fprintf(fp, "-v|--verbose Increase verbosity for errors\n");
134 static ssize_t get_key_data(char *src, void *buffer, size_t bits)
141 /* convert bits to minimum number of bytes */
142 remain = (bits + 7) / 8;
144 printf("Reading random data for shared key from '%s'\n", src);
145 fd = open(src, O_RDONLY);
147 fprintf(stderr, "error: opening '%s': %s\n", src,
153 rc = read(fd, ptr, remain);
157 fprintf(stderr, "error: reading from '%s': %s\n", src,
162 } else if (rc == 0) {
164 "error: key source too short for %zd-bit key\n",
179 static int write_config_file(char *output_file,
180 struct sk_keyfile_config *config, bool overwrite)
184 int flags = O_WRONLY | O_CREAT;
189 sk_config_cpu_to_disk(config);
191 fd = open(output_file, flags, 0400);
193 fprintf(stderr, "error: opening '%s': %s\n", output_file,
198 rc = write(fd, config, sizeof(*config));
200 fprintf(stderr, "error: writing to '%s': %s\n", output_file,
203 } else if (rc != sizeof(*config)) {
204 fprintf(stderr, "error: short write to '%s'\n", output_file);
215 static int print_config(char *filename)
217 struct sk_keyfile_config *config;
220 config = sk_read_file(filename);
224 if (sk_validate_config(config)) {
225 fprintf(stderr, "error: key configuration failed validation\n");
230 printf("Version: %u\n", config->skc_version);
232 if (config->skc_type & SK_TYPE_MGS)
234 if (config->skc_type & SK_TYPE_SERVER)
236 if (config->skc_type & SK_TYPE_CLIENT)
239 printf("HMAC alg: %s\n", sk_hmac2name[config->skc_hmac_alg]);
240 printf("Crypto alg: %s\n", sk_crypt2name[config->skc_crypt_alg]);
241 printf("Ctx Expiration: %u seconds\n", config->skc_expire);
242 printf("Shared keylen: %u bits\n", config->skc_shared_keylen);
243 printf("Prime length: %u bits\n", config->skc_prime_bits);
244 printf("File system: %s\n", config->skc_fsname);
245 printf("MGS NIDs: ");
246 for (i = 0; i < MAX_MGSNIDS; i++) {
247 if (config->skc_mgsnids[i] == LNET_NID_ANY)
249 printf(" %s", libcfs_nid2str(config->skc_mgsnids[i]));
252 printf("Nodemap name: %s\n", config->skc_nodemap);
253 printf("Shared key:\n");
254 print_hex(0, config->skc_shared_key, config->skc_shared_keylen / 8);
256 /* Don't print empty keys */
257 for (i = 0; i < SK_MAX_P_BYTES; i++)
258 if (config->skc_p[i] != 0)
261 if (i != SK_MAX_P_BYTES) {
262 printf("Prime (p):\n");
263 print_hex(0, config->skc_p, config->skc_prime_bits / 8);
270 static int parse_mgsnids(char *mgsnids, struct sk_keyfile_config *config)
279 /* replace all old values */
280 for (i = 0; i < MAX_MGSNIDS; i++)
281 config->skc_mgsnids[i] = LNET_NID_ANY;
284 end = mgsnids + strlen(mgsnids);
286 while (ptr < end && i < MAX_MGSNIDS) {
287 sep = strstr(ptr, ",");
291 nid = libcfs_str2nid(ptr);
292 if (nid == LNET_NID_ANY) {
293 fprintf(stderr, "error: invalid MGS NID: %s\n", ptr);
298 config->skc_mgsnids[i++] = nid;
299 ptr += strlen(ptr) + 1;
302 if (i == MAX_MGSNIDS) {
303 fprintf(stderr, "error: more than %u MGS NIDs provided\n", i);
310 int main(int argc, char **argv)
312 struct sk_keyfile_config *config;
313 char *datafile = NULL;
318 char *mgsnids = NULL;
319 char *nodemap = NULL;
323 int crypt = SK_CRYPT_EMPTY;
324 int hmac = SK_HMAC_EMPTY;
326 int shared_keylen = -1;
331 enum sk_key_type type = SK_TYPE_INVALID;
332 bool generate_prime = false;
335 static struct option long_opt[] = {
336 {"crypt", 1, 0, 'c'},
338 {"expire", 1, 0, 'e'},
339 {"fsname", 1, 0, 'f'},
340 {"mgsnids", 1, 0, 'g'},
343 {"integrity", 1, 0, 'i'},
344 {"key-bits", 1, 0, 'k'},
345 {"shared", 1, 0, 'k'},
347 {"modify", 1, 0, 'm'},
348 {"nodemap", 1, 0, 'n'},
349 {"prime-bits", 1, 0, 'p'},
352 {"verbose", 0, 0, 'v'},
353 {"write", 1, 0, 'w'},
357 while ((opt = getopt_long(argc, argv,
358 "c:d:e:f:g:hi:l:m:n:p:r:s:k:t:w:v", long_opt,
362 crypt = sk_name2crypt(optarg);
368 expire = atoi(optarg);
370 fprintf(stderr, "warning: using a %us key "
371 "expiration may cause issues during "
372 "key renegotiation\n", expire);
376 if (strlen(fsname) > MTI_NAME_MAXLEN) {
378 "error: file system name longer than "
379 "%u characters\n", MTI_NAME_MAXLEN);
387 usage(stdout, argv[0]);
390 hmac = sk_name2hmac(optarg);
393 shared_keylen = atoi(optarg);
403 if (strlen(nodemap) > LUSTRE_NODEMAP_NAME_LENGTH) {
405 "error: nodemap name longer than "
407 LUSTRE_NODEMAP_NAME_LENGTH);
412 prime_bits = atoi(optarg);
413 if (prime_bits <= 0) {
415 "error: invalid prime length: '%s'\n",
424 tmp2 = strdup(optarg);
427 "error: failed to allocate type\n");
430 tmp = strsep(&tmp2, ",");
431 while (tmp != NULL) {
432 if (strcasecmp(tmp, "server") == 0) {
433 type |= SK_TYPE_SERVER;
434 } else if (strcasecmp(tmp, "mgs") == 0) {
436 } else if (strcasecmp(tmp, "client") == 0) {
437 type |= SK_TYPE_CLIENT;
440 "error: invalid type '%s', "
441 "must be mgs, server, or client"
445 tmp = strsep(&tmp2, ",");
456 fprintf(stderr, "error: unknown option: '%c'\n", opt);
462 if (optind != argc) {
464 "error: extraneous arguments provided, check usage\n");
468 if (!input && !output && !load && !modify) {
469 usage(stderr, argv[0]);
473 /* init gss logger for foreground (no syslog) which prints to stderr */
474 initerr(NULL, verbose, 1);
477 return print_config(input);
480 if (sk_load_keyfile(load))
485 if (crypt == SK_CRYPT_INVALID) {
486 fprintf(stderr, "error: invalid crypt algorithm specified\n");
489 if (hmac == SK_HMAC_INVALID) {
490 fprintf(stderr, "error: invalid HMAC algorithm specified\n");
495 config = sk_read_file(modify);
499 if (type != SK_TYPE_INVALID) {
500 /* generate key when adding client type */
501 if (!(config->skc_type & SK_TYPE_CLIENT) &&
502 type & SK_TYPE_CLIENT)
503 generate_prime = true;
504 else if (!(type & SK_TYPE_CLIENT))
505 memset(config->skc_p, 0, SK_MAX_P_BYTES);
507 config->skc_type = type;
509 if (prime_bits != -1) {
510 memset(config->skc_p, 0, SK_MAX_P_BYTES);
511 if (config->skc_prime_bits != prime_bits &&
512 config->skc_type & SK_TYPE_CLIENT)
513 generate_prime = true;
516 /* write mode for a new key */
517 if (!fsname && !mgsnids) {
519 "error: missing --fsname or --mgsnids\n");
523 config = calloc(1, sizeof(*config));
527 /* Set the defaults for new key */
528 config->skc_version = SK_CONF_VERSION;
529 config->skc_expire = SK_DEFAULT_EXPIRE;
530 config->skc_shared_keylen = SK_DEFAULT_SK_KEYLEN;
531 config->skc_prime_bits = SK_DEFAULT_PRIME_BITS;
532 config->skc_crypt_alg = SK_CRYPT_AES256_CTR;
533 config->skc_hmac_alg = SK_HMAC_SHA256;
534 for (i = 0; i < MAX_MGSNIDS; i++)
535 config->skc_mgsnids[i] = LNET_NID_ANY;
537 if (type == SK_TYPE_INVALID) {
538 fprintf(stderr, "error: no type specified for key\n");
541 config->skc_type = type;
542 generate_prime = type & SK_TYPE_CLIENT;
544 strncpy(config->skc_nodemap, SK_DEFAULT_NODEMAP,
545 strlen(SK_DEFAULT_NODEMAP));
548 datafile = "/dev/random";
551 if (crypt != SK_CRYPT_EMPTY)
552 config->skc_crypt_alg = crypt;
553 if (hmac != SK_HMAC_EMPTY)
554 config->skc_hmac_alg = hmac;
556 config->skc_expire = expire;
557 if (shared_keylen != -1)
558 config->skc_shared_keylen = shared_keylen;
559 if (prime_bits != -1)
560 config->skc_prime_bits = prime_bits;
562 strncpy(config->skc_fsname, fsname, strlen(fsname));
564 strncpy(config->skc_nodemap, nodemap, strlen(nodemap));
565 if (mgsnids && parse_mgsnids(mgsnids, config))
567 if (sk_validate_config(config)) {
568 fprintf(stderr, "error: key configuration failed validation\n");
572 if (datafile && get_key_data(datafile, config->skc_shared_key,
573 config->skc_shared_keylen)) {
574 fprintf(stderr, "error: failure getting key data from '%s'\n",
579 if (generate_prime) {
580 printf("Generating DH parameters, this can take a while...\n");
581 dh = DH_generate_parameters(config->skc_prime_bits,
582 SK_GENERATOR, NULL, NULL);
583 if (BN_num_bytes(dh->p) > SK_MAX_P_BYTES) {
584 fprintf(stderr, "error: cannot generate DH parameters: "
585 "requested length %d exceeds maximum %d\n",
586 config->skc_prime_bits, SK_MAX_P_BYTES * 8);
589 if (BN_bn2bin(dh->p, config->skc_p) != BN_num_bytes(dh->p)) {
591 "error: convert BIGNUM p to binary failed\n");
596 if (write_config_file(modify ?: output, config, modify))