1b528cefcSMark Murray /* 2b528cefcSMark Murray * Copyright (c) 1997 - 2000 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 36b528cefcSMark Murray RCSID("$Id: add.c,v 1.1 2000/01/02 04:41:00 assar Exp $"); 37b528cefcSMark Murray 38b528cefcSMark Murray int 39b528cefcSMark Murray kt_add(int argc, char **argv) 40b528cefcSMark Murray { 41b528cefcSMark Murray krb5_error_code ret; 42b528cefcSMark Murray krb5_keytab_entry entry; 43b528cefcSMark Murray char buf[128]; 44b528cefcSMark Murray char *principal_string = NULL; 45b528cefcSMark Murray int kvno = -1; 46b528cefcSMark Murray char *enctype_string = NULL; 47b528cefcSMark Murray krb5_enctype enctype; 48b528cefcSMark Murray char *password_string = NULL; 49b528cefcSMark Murray int salt_flag = 1; 50b528cefcSMark Murray int random_flag = 0; 51b528cefcSMark Murray int help_flag = 0; 52b528cefcSMark Murray struct getargs args[] = { 53b528cefcSMark Murray { "principal", 'p', arg_string, NULL, "principal of key", "principal"}, 54b528cefcSMark Murray { "kvno", 'V', arg_integer, NULL, "key version of key" }, 55b528cefcSMark Murray { "enctype", 'e', arg_string, NULL, "encryption type of key" }, 56b528cefcSMark Murray { "password", 'w', arg_string, NULL, "password for key"}, 57b528cefcSMark Murray { "salt", 's', arg_negative_flag, NULL, "no salt" }, 58b528cefcSMark Murray { "random", 'r', arg_flag, NULL, "generate random key" }, 59b528cefcSMark Murray { "help", 'h', arg_flag, NULL } 60b528cefcSMark Murray }; 61b528cefcSMark Murray int num_args = sizeof(args) / sizeof(args[0]); 62b528cefcSMark Murray int optind = 0; 63b528cefcSMark Murray int i = 0; 64b528cefcSMark Murray args[i++].value = &principal_string; 65b528cefcSMark Murray args[i++].value = &kvno; 66b528cefcSMark Murray args[i++].value = &enctype_string; 67b528cefcSMark Murray args[i++].value = &password_string; 68b528cefcSMark Murray args[i++].value = &salt_flag; 69b528cefcSMark Murray args[i++].value = &random_flag; 70b528cefcSMark Murray args[i++].value = &help_flag; 71b528cefcSMark Murray 72b528cefcSMark Murray if(getarg(args, num_args, argc, argv, &optind)) { 73b528cefcSMark Murray arg_printusage(args, num_args, "ktutil add", ""); 74b528cefcSMark Murray return 0; 75b528cefcSMark Murray } 76b528cefcSMark Murray if(help_flag) { 77b528cefcSMark Murray arg_printusage(args, num_args, "ktutil add", ""); 78b528cefcSMark Murray return 0; 79b528cefcSMark Murray } 80b528cefcSMark Murray if(principal_string == NULL) { 81b528cefcSMark Murray printf("Principal: "); 82b528cefcSMark Murray if (fgets(buf, sizeof(buf), stdin) == NULL) 83b528cefcSMark Murray return 0; 84b528cefcSMark Murray buf[strcspn(buf, "\r\n")] = '\0'; 85b528cefcSMark Murray principal_string = buf; 86b528cefcSMark Murray } 87b528cefcSMark Murray ret = krb5_parse_name(context, principal_string, &entry.principal); 88b528cefcSMark Murray if(ret) { 89b528cefcSMark Murray krb5_warn(context, ret, "%s", principal_string); 90b528cefcSMark Murray return 0; 91b528cefcSMark Murray } 92b528cefcSMark Murray if(enctype_string == NULL) { 93b528cefcSMark Murray printf("Encryption type: "); 94b528cefcSMark Murray if (fgets(buf, sizeof(buf), stdin) == NULL) { 95b528cefcSMark Murray krb5_free_principal (context, entry.principal); 96b528cefcSMark Murray return 0; 97b528cefcSMark Murray } 98b528cefcSMark Murray buf[strcspn(buf, "\r\n")] = '\0'; 99b528cefcSMark Murray enctype_string = buf; 100b528cefcSMark Murray } 101b528cefcSMark Murray ret = krb5_string_to_enctype(context, enctype_string, &enctype); 102b528cefcSMark Murray if(ret) { 103b528cefcSMark Murray int t; 104b528cefcSMark Murray if(sscanf(enctype_string, "%d", &t) == 1) 105b528cefcSMark Murray enctype = t; 106b528cefcSMark Murray else { 107b528cefcSMark Murray krb5_warn(context, ret, "%s", enctype_string); 108b528cefcSMark Murray krb5_free_principal(context, entry.principal); 109b528cefcSMark Murray return 0; 110b528cefcSMark Murray } 111b528cefcSMark Murray } 112b528cefcSMark Murray if(kvno == -1) { 113b528cefcSMark Murray printf("Key version: "); 114b528cefcSMark Murray if (fgets(buf, sizeof(buf), stdin) == NULL) { 115b528cefcSMark Murray krb5_free_principal (context, entry.principal); 116b528cefcSMark Murray return 0; 117b528cefcSMark Murray } 118b528cefcSMark Murray buf[strcspn(buf, "\r\n")] = '\0'; 119b528cefcSMark Murray kvno = atoi(buf); 120b528cefcSMark Murray } 121b528cefcSMark Murray if(password_string == NULL && random_flag == 0) { 122b528cefcSMark Murray if(des_read_pw_string(buf, sizeof(buf), "Password: ", 1)) { 123b528cefcSMark Murray krb5_free_principal (context, entry.principal); 124b528cefcSMark Murray return 0; 125b528cefcSMark Murray } 126b528cefcSMark Murray password_string = buf; 127b528cefcSMark Murray } 128b528cefcSMark Murray if(password_string) { 129b528cefcSMark Murray if (!salt_flag) { 130b528cefcSMark Murray krb5_salt salt; 131b528cefcSMark Murray krb5_data pw; 132b528cefcSMark Murray 133b528cefcSMark Murray salt.salttype = KRB5_PW_SALT; 134b528cefcSMark Murray salt.saltvalue.data = NULL; 135b528cefcSMark Murray salt.saltvalue.length = 0; 136b528cefcSMark Murray pw.data = (void*)password_string; 137b528cefcSMark Murray pw.length = strlen(password_string); 138b528cefcSMark Murray krb5_string_to_key_data_salt(context, enctype, pw, salt, 139b528cefcSMark Murray &entry.keyblock); 140b528cefcSMark Murray } else { 141b528cefcSMark Murray krb5_string_to_key(context, enctype, password_string, 142b528cefcSMark Murray entry.principal, &entry.keyblock); 143b528cefcSMark Murray } 144b528cefcSMark Murray memset (password_string, 0, strlen(password_string)); 145b528cefcSMark Murray } else { 146b528cefcSMark Murray krb5_generate_random_keyblock(context, enctype, &entry.keyblock); 147b528cefcSMark Murray } 148b528cefcSMark Murray entry.vno = kvno; 149b528cefcSMark Murray entry.timestamp = time (NULL); 150b528cefcSMark Murray ret = krb5_kt_add_entry(context, keytab, &entry); 151b528cefcSMark Murray if(ret) 152b528cefcSMark Murray krb5_warn(context, ret, "add"); 153b528cefcSMark Murray krb5_kt_free_entry(context, &entry); 154b528cefcSMark Murray return 0; 155b528cefcSMark Murray } 156