xref: /freebsd/crypto/heimdal/admin/add.c (revision adb0ddaeac0a71a08d6af3a711387b59efcc94b6)
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 
36adb0ddaeSAssar Westerlund RCSID("$Id: add.c,v 1.2 2001/05/10 15:39:15 assar 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     }
81adb0ddaeSAssar Westerlund     if (keytab_string == NULL) {
82adb0ddaeSAssar Westerlund 	ret = krb5_kt_default_modify_name (context, keytab_buf,
83adb0ddaeSAssar Westerlund 					   sizeof(keytab_buf));
84adb0ddaeSAssar Westerlund 	if (ret) {
85adb0ddaeSAssar Westerlund 	    krb5_warn(context, ret, "krb5_kt_default_modify_name");
86adb0ddaeSAssar Westerlund 	    return 1;
87adb0ddaeSAssar Westerlund 	}
88adb0ddaeSAssar Westerlund 	keytab_string = keytab_buf;
89adb0ddaeSAssar Westerlund     }
90adb0ddaeSAssar Westerlund     ret = krb5_kt_resolve(context, keytab_string, &keytab);
91adb0ddaeSAssar Westerlund     if (ret) {
92adb0ddaeSAssar Westerlund 	krb5_warn(context, ret, "resolving keytab %s", keytab_string);
93adb0ddaeSAssar Westerlund 	return 1;
94adb0ddaeSAssar Westerlund     }
95adb0ddaeSAssar Westerlund 
96adb0ddaeSAssar Westerlund     if (verbose_flag)
97adb0ddaeSAssar Westerlund 	fprintf (stderr, "Using keytab %s\n", keytab_string);
98adb0ddaeSAssar Westerlund 
99adb0ddaeSAssar Westerlund     memset(&entry, 0, sizeof(entry));
100b528cefcSMark Murray     if(principal_string == NULL) {
101b528cefcSMark Murray 	printf("Principal: ");
102b528cefcSMark Murray 	if (fgets(buf, sizeof(buf), stdin) == NULL)
103adb0ddaeSAssar Westerlund 	    return 1;
104b528cefcSMark Murray 	buf[strcspn(buf, "\r\n")] = '\0';
105b528cefcSMark Murray 	principal_string = buf;
106b528cefcSMark Murray     }
107b528cefcSMark Murray     ret = krb5_parse_name(context, principal_string, &entry.principal);
108b528cefcSMark Murray     if(ret) {
109b528cefcSMark Murray 	krb5_warn(context, ret, "%s", principal_string);
110adb0ddaeSAssar Westerlund 	goto out;
111b528cefcSMark Murray     }
112b528cefcSMark Murray     if(enctype_string == NULL) {
113b528cefcSMark Murray 	printf("Encryption type: ");
114adb0ddaeSAssar Westerlund 	if (fgets(buf, sizeof(buf), stdin) == NULL)
115adb0ddaeSAssar Westerlund 	    goto out;
116b528cefcSMark Murray 	buf[strcspn(buf, "\r\n")] = '\0';
117b528cefcSMark Murray 	enctype_string = buf;
118b528cefcSMark Murray     }
119b528cefcSMark Murray     ret = krb5_string_to_enctype(context, enctype_string, &enctype);
120b528cefcSMark Murray     if(ret) {
121b528cefcSMark Murray 	int t;
122b528cefcSMark Murray 	if(sscanf(enctype_string, "%d", &t) == 1)
123b528cefcSMark Murray 	    enctype = t;
124b528cefcSMark Murray 	else {
125b528cefcSMark Murray 	    krb5_warn(context, ret, "%s", enctype_string);
126adb0ddaeSAssar Westerlund 	    goto out;
127b528cefcSMark Murray 	}
128b528cefcSMark Murray     }
129b528cefcSMark Murray     if(kvno == -1) {
130b528cefcSMark Murray 	printf("Key version: ");
131adb0ddaeSAssar Westerlund 	if (fgets(buf, sizeof(buf), stdin) == NULL)
132adb0ddaeSAssar Westerlund 	    goto out;
133b528cefcSMark Murray 	buf[strcspn(buf, "\r\n")] = '\0';
134b528cefcSMark Murray 	kvno = atoi(buf);
135b528cefcSMark Murray     }
136b528cefcSMark Murray     if(password_string == NULL && random_flag == 0) {
137adb0ddaeSAssar Westerlund 	if(des_read_pw_string(buf, sizeof(buf), "Password: ", 1))
138adb0ddaeSAssar Westerlund 	    goto out;
139b528cefcSMark Murray 	password_string = buf;
140b528cefcSMark Murray     }
141b528cefcSMark Murray     if(password_string) {
142b528cefcSMark Murray 	if (!salt_flag) {
143b528cefcSMark Murray 	    krb5_salt salt;
144b528cefcSMark Murray 	    krb5_data pw;
145b528cefcSMark Murray 
146b528cefcSMark Murray 	    salt.salttype         = KRB5_PW_SALT;
147b528cefcSMark Murray 	    salt.saltvalue.data   = NULL;
148b528cefcSMark Murray 	    salt.saltvalue.length = 0;
149b528cefcSMark Murray 	    pw.data = (void*)password_string;
150b528cefcSMark Murray 	    pw.length = strlen(password_string);
151b528cefcSMark Murray 	    krb5_string_to_key_data_salt(context, enctype, pw, salt,
152b528cefcSMark Murray 					 &entry.keyblock);
153b528cefcSMark Murray         } else {
154b528cefcSMark Murray 	    krb5_string_to_key(context, enctype, password_string,
155b528cefcSMark Murray 			       entry.principal, &entry.keyblock);
156b528cefcSMark Murray 	}
157b528cefcSMark Murray 	memset (password_string, 0, strlen(password_string));
158b528cefcSMark Murray     } else {
159b528cefcSMark Murray 	krb5_generate_random_keyblock(context, enctype, &entry.keyblock);
160b528cefcSMark Murray     }
161b528cefcSMark Murray     entry.vno = kvno;
162b528cefcSMark Murray     entry.timestamp = time (NULL);
163b528cefcSMark Murray     ret = krb5_kt_add_entry(context, keytab, &entry);
164b528cefcSMark Murray     if(ret)
165b528cefcSMark Murray 	krb5_warn(context, ret, "add");
166adb0ddaeSAssar Westerlund  out:
167b528cefcSMark Murray     krb5_kt_free_entry(context, &entry);
168adb0ddaeSAssar Westerlund     krb5_kt_close(context, keytab);
169b528cefcSMark Murray     return 0;
170b528cefcSMark Murray }
171