1b528cefcSMark Murray /* 2adb0ddaeSAssar Westerlund * Copyright (c) 1997 - 2001 Kungliga Tekniska H�gskolan 3b528cefcSMark Murray * (Royal Institute of Technology, Stockholm, Sweden). 4b528cefcSMark Murray * All rights reserved. 5b528cefcSMark Murray * 6b528cefcSMark Murray * Redistribution and use in source and binary forms, with or without 7b528cefcSMark Murray * modification, are permitted provided that the following conditions 8b528cefcSMark Murray * are met: 9b528cefcSMark Murray * 10b528cefcSMark Murray * 1. Redistributions of source code must retain the above copyright 11b528cefcSMark Murray * notice, this list of conditions and the following disclaimer. 12b528cefcSMark Murray * 13b528cefcSMark Murray * 2. Redistributions in binary form must reproduce the above copyright 14b528cefcSMark Murray * notice, this list of conditions and the following disclaimer in the 15b528cefcSMark Murray * documentation and/or other materials provided with the distribution. 16b528cefcSMark Murray * 17b528cefcSMark Murray * 3. Neither the name of the Institute nor the names of its contributors 18b528cefcSMark Murray * may be used to endorse or promote products derived from this software 19b528cefcSMark Murray * without specific prior written permission. 20b528cefcSMark Murray * 21b528cefcSMark Murray * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22b528cefcSMark Murray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23b528cefcSMark Murray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24b528cefcSMark Murray * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25b528cefcSMark Murray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26b528cefcSMark Murray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27b528cefcSMark Murray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28b528cefcSMark Murray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29b528cefcSMark Murray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30b528cefcSMark Murray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31b528cefcSMark Murray * SUCH DAMAGE. 32b528cefcSMark Murray */ 33b528cefcSMark Murray 34b528cefcSMark Murray #include "ktutil_locl.h" 35b528cefcSMark Murray 364137ff4cSJacques Vidrine RCSID("$Id: add.c,v 1.3 2001/07/23 09:46:40 joda Exp $"); 37b528cefcSMark Murray 38b528cefcSMark Murray int 39b528cefcSMark Murray kt_add(int argc, char **argv) 40b528cefcSMark Murray { 41b528cefcSMark Murray krb5_error_code ret; 42adb0ddaeSAssar Westerlund krb5_keytab keytab; 43b528cefcSMark Murray krb5_keytab_entry entry; 44b528cefcSMark Murray char buf[128]; 45b528cefcSMark Murray char *principal_string = NULL; 46b528cefcSMark Murray int kvno = -1; 47b528cefcSMark Murray char *enctype_string = NULL; 48b528cefcSMark Murray krb5_enctype enctype; 49b528cefcSMark Murray char *password_string = NULL; 50b528cefcSMark Murray int salt_flag = 1; 51b528cefcSMark Murray int random_flag = 0; 52b528cefcSMark Murray int help_flag = 0; 53b528cefcSMark Murray struct getargs args[] = { 54b528cefcSMark Murray { "principal", 'p', arg_string, NULL, "principal of key", "principal"}, 55b528cefcSMark Murray { "kvno", 'V', arg_integer, NULL, "key version of key" }, 56b528cefcSMark Murray { "enctype", 'e', arg_string, NULL, "encryption type of key" }, 57b528cefcSMark Murray { "password", 'w', arg_string, NULL, "password for key"}, 58b528cefcSMark Murray { "salt", 's', arg_negative_flag, NULL, "no salt" }, 59b528cefcSMark Murray { "random", 'r', arg_flag, NULL, "generate random key" }, 60b528cefcSMark Murray { "help", 'h', arg_flag, NULL } 61b528cefcSMark Murray }; 62b528cefcSMark Murray int num_args = sizeof(args) / sizeof(args[0]); 63b528cefcSMark Murray int optind = 0; 64b528cefcSMark Murray int i = 0; 65b528cefcSMark Murray args[i++].value = &principal_string; 66b528cefcSMark Murray args[i++].value = &kvno; 67b528cefcSMark Murray args[i++].value = &enctype_string; 68b528cefcSMark Murray args[i++].value = &password_string; 69b528cefcSMark Murray args[i++].value = &salt_flag; 70b528cefcSMark Murray args[i++].value = &random_flag; 71b528cefcSMark Murray args[i++].value = &help_flag; 72b528cefcSMark Murray 73b528cefcSMark Murray if(getarg(args, num_args, argc, argv, &optind)) { 74b528cefcSMark Murray arg_printusage(args, num_args, "ktutil add", ""); 75adb0ddaeSAssar Westerlund return 1; 76b528cefcSMark Murray } 77b528cefcSMark Murray if(help_flag) { 78b528cefcSMark Murray arg_printusage(args, num_args, "ktutil add", ""); 79adb0ddaeSAssar Westerlund return 1; 80b528cefcSMark Murray } 814137ff4cSJacques Vidrine if((keytab = ktutil_open_keytab()) == NULL) 82adb0ddaeSAssar Westerlund return 1; 83adb0ddaeSAssar Westerlund 84adb0ddaeSAssar Westerlund memset(&entry, 0, sizeof(entry)); 85b528cefcSMark Murray if(principal_string == NULL) { 86b528cefcSMark Murray printf("Principal: "); 87b528cefcSMark Murray if (fgets(buf, sizeof(buf), stdin) == NULL) 88adb0ddaeSAssar Westerlund return 1; 89b528cefcSMark Murray buf[strcspn(buf, "\r\n")] = '\0'; 90b528cefcSMark Murray principal_string = buf; 91b528cefcSMark Murray } 92b528cefcSMark Murray ret = krb5_parse_name(context, principal_string, &entry.principal); 93b528cefcSMark Murray if(ret) { 94b528cefcSMark Murray krb5_warn(context, ret, "%s", principal_string); 95adb0ddaeSAssar Westerlund goto out; 96b528cefcSMark Murray } 97b528cefcSMark Murray if(enctype_string == NULL) { 98b528cefcSMark Murray printf("Encryption type: "); 99adb0ddaeSAssar Westerlund if (fgets(buf, sizeof(buf), stdin) == NULL) 100adb0ddaeSAssar Westerlund goto out; 101b528cefcSMark Murray buf[strcspn(buf, "\r\n")] = '\0'; 102b528cefcSMark Murray enctype_string = buf; 103b528cefcSMark Murray } 104b528cefcSMark Murray ret = krb5_string_to_enctype(context, enctype_string, &enctype); 105b528cefcSMark Murray if(ret) { 106b528cefcSMark Murray int t; 107b528cefcSMark Murray if(sscanf(enctype_string, "%d", &t) == 1) 108b528cefcSMark Murray enctype = t; 109b528cefcSMark Murray else { 110b528cefcSMark Murray krb5_warn(context, ret, "%s", enctype_string); 111adb0ddaeSAssar Westerlund goto out; 112b528cefcSMark Murray } 113b528cefcSMark Murray } 114b528cefcSMark Murray if(kvno == -1) { 115b528cefcSMark Murray printf("Key version: "); 116adb0ddaeSAssar Westerlund if (fgets(buf, sizeof(buf), stdin) == NULL) 117adb0ddaeSAssar Westerlund goto out; 118b528cefcSMark Murray buf[strcspn(buf, "\r\n")] = '\0'; 119b528cefcSMark Murray kvno = atoi(buf); 120b528cefcSMark Murray } 121b528cefcSMark Murray if(password_string == NULL && random_flag == 0) { 122adb0ddaeSAssar Westerlund if(des_read_pw_string(buf, sizeof(buf), "Password: ", 1)) 123adb0ddaeSAssar Westerlund goto out; 124b528cefcSMark Murray password_string = buf; 125b528cefcSMark Murray } 126b528cefcSMark Murray if(password_string) { 127b528cefcSMark Murray if (!salt_flag) { 128b528cefcSMark Murray krb5_salt salt; 129b528cefcSMark Murray krb5_data pw; 130b528cefcSMark Murray 131b528cefcSMark Murray salt.salttype = KRB5_PW_SALT; 132b528cefcSMark Murray salt.saltvalue.data = NULL; 133b528cefcSMark Murray salt.saltvalue.length = 0; 134b528cefcSMark Murray pw.data = (void*)password_string; 135b528cefcSMark Murray pw.length = strlen(password_string); 136b528cefcSMark Murray krb5_string_to_key_data_salt(context, enctype, pw, salt, 137b528cefcSMark Murray &entry.keyblock); 138b528cefcSMark Murray } else { 139b528cefcSMark Murray krb5_string_to_key(context, enctype, password_string, 140b528cefcSMark Murray entry.principal, &entry.keyblock); 141b528cefcSMark Murray } 142b528cefcSMark Murray memset (password_string, 0, strlen(password_string)); 143b528cefcSMark Murray } else { 144b528cefcSMark Murray krb5_generate_random_keyblock(context, enctype, &entry.keyblock); 145b528cefcSMark Murray } 146b528cefcSMark Murray entry.vno = kvno; 147b528cefcSMark Murray entry.timestamp = time (NULL); 148b528cefcSMark Murray ret = krb5_kt_add_entry(context, keytab, &entry); 149b528cefcSMark Murray if(ret) 150b528cefcSMark Murray krb5_warn(context, ret, "add"); 151adb0ddaeSAssar Westerlund out: 152b528cefcSMark Murray krb5_kt_free_entry(context, &entry); 153adb0ddaeSAssar Westerlund krb5_kt_close(context, keytab); 154b528cefcSMark Murray return 0; 155b528cefcSMark Murray } 156