xref: /freebsd/crypto/heimdal/admin/get.c (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
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: get.c,v 1.15 2000/01/02 04:41:01 assar Exp $");
37 
38 int
39 kt_get(int argc, char **argv)
40 {
41     krb5_error_code ret;
42     kadm5_config_params conf;
43     void *kadm_handle;
44     char *principal = NULL;
45     char *realm = NULL;
46     char *admin_server = NULL;
47     int server_port = 0;
48     int help_flag = 0;
49     int optind = 0;
50     int i, j;
51 
52     struct getargs args[] = {
53 	{ "principal",	'p',	arg_string,   NULL,
54 	  "admin principal", "principal"
55 	},
56 	{ "realm",	'r',	arg_string,   NULL,
57 	  "realm to use", "realm"
58 	},
59 	{ "admin-server",	'a',	arg_string, NULL,
60 	  "server to contact", "host"
61 	},
62 	{ "server-port",	's',	arg_integer, NULL,
63 	  "port to contact", "port number"
64 	},
65 	{ "help",		'h',	arg_flag,    NULL }
66     };
67 
68     args[0].value = &principal;
69     args[1].value = &realm;
70     args[2].value = &admin_server;
71     args[3].value = &server_port;
72     args[4].value = &help_flag;
73 
74     memset(&conf, 0, sizeof(conf));
75 
76     if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optind)
77        || help_flag) {
78 	arg_printusage(args, sizeof(args) / sizeof(args[0]),
79 		       "ktutil get", "principal...");
80 	return 0;
81     }
82 
83     if(realm) {
84 	krb5_set_default_realm(context, realm); /* XXX should be fixed
85 						   some other way */
86 	conf.realm = realm;
87 	conf.mask |= KADM5_CONFIG_REALM;
88     }
89 
90     if (admin_server) {
91 	conf.admin_server = admin_server;
92 	conf.mask |= KADM5_CONFIG_ADMIN_SERVER;
93     }
94 
95     if (server_port) {
96 	conf.kadmind_port = htons(server_port);
97 	conf.mask |= KADM5_CONFIG_KADMIND_PORT;
98     }
99 
100     ret = kadm5_init_with_password_ctx(context,
101 				       principal,
102 				       NULL,
103 				       KADM5_ADMIN_SERVICE,
104 				       &conf, 0, 0,
105 				       &kadm_handle);
106     if(ret) {
107 	krb5_warn(context, ret, "kadm5_init_with_password");
108 	return 0;
109     }
110 
111 
112     for(i = optind; i < argc; i++){
113 	krb5_principal princ_ent;
114 	kadm5_principal_ent_rec princ;
115 	int mask = 0;
116 	krb5_keyblock *keys;
117 	int n_keys;
118 	int created = 0;
119 	krb5_keytab_entry entry;
120 
121 	ret = krb5_parse_name(context, argv[i], &princ_ent);
122 	memset(&princ, 0, sizeof(princ));
123 	princ.principal = princ_ent;
124 	mask |= KADM5_PRINCIPAL;
125 	princ.attributes |= KRB5_KDB_DISALLOW_ALL_TIX;
126 	mask |= KADM5_ATTRIBUTES;
127 	princ.princ_expire_time = 0;
128 	mask |= KADM5_PRINC_EXPIRE_TIME;
129 
130 	ret = kadm5_create_principal(kadm_handle, &princ, mask, "x");
131 	if(ret == 0)
132 	    created++;
133 	else if(ret != KADM5_DUP) {
134 	    krb5_free_principal(context, princ_ent);
135 	    continue;
136 	}
137 	ret = kadm5_randkey_principal(kadm_handle, princ_ent, &keys, &n_keys);
138 
139 	ret = kadm5_get_principal(kadm_handle, princ_ent, &princ,
140 			      KADM5_PRINCIPAL | KADM5_KVNO | KADM5_ATTRIBUTES);
141 	princ.attributes &= (~KRB5_KDB_DISALLOW_ALL_TIX);
142 	mask = KADM5_ATTRIBUTES;
143 	if(created) {
144 	    princ.kvno = 1;
145 	    mask |= KADM5_KVNO;
146 	}
147 	ret = kadm5_modify_principal(kadm_handle, &princ, mask);
148 	for(j = 0; j < n_keys; j++) {
149 	    entry.principal = princ_ent;
150 	    entry.vno = princ.kvno;
151 	    entry.keyblock = keys[j];
152 	    entry.timestamp = time (NULL);
153 	    ret = krb5_kt_add_entry(context, keytab, &entry);
154 	    krb5_free_keyblock_contents(context, &keys[j]);
155 	}
156 
157 	kadm5_free_principal_ent(kadm_handle, &princ);
158 	krb5_free_principal(context, princ_ent);
159     }
160     kadm5_destroy(kadm_handle);
161     return 0;
162 }
163