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