]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - crypto/heimdal/admin/change.c
unfinished sblive driver, playback/mixer only for now - not enabled in
[FreeBSD/FreeBSD.git] / crypto / heimdal / admin / change.c
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: change.c,v 1.1 2000/01/02 04:41:00 assar Exp $");
37
38 static void
39 change_entry (krb5_context context, krb5_keytab_entry *entry,
40               const char *realm, const char *admin_server, int server_port)
41 {
42     krb5_error_code ret;
43     kadm5_config_params conf;
44     void *kadm_handle;
45     char *client_name;
46     krb5_keyblock *keys;
47     int num_keys;
48     int i;
49
50     ret = krb5_unparse_name (context, entry->principal, &client_name);
51     if (ret) {
52         krb5_warn (context, ret, "kadm5_c_init_with_skey_ctx");
53         return;
54     }
55
56     memset (&conf, 0, sizeof(conf));
57
58     if(realm)
59         conf.realm = (char *)realm;
60     else
61         conf.realm = *krb5_princ_realm (context, entry->principal);
62     conf.mask |= KADM5_CONFIG_REALM;
63     
64     if (admin_server) {
65         conf.admin_server = (char *)admin_server;
66         conf.mask |= KADM5_CONFIG_ADMIN_SERVER;
67     }
68
69     if (server_port) {
70         conf.kadmind_port = htons(server_port);
71         conf.mask |= KADM5_CONFIG_KADMIND_PORT;
72     }
73
74     ret = kadm5_init_with_skey_ctx (context,
75                                     client_name,
76                                     keytab_string,
77                                     KADM5_ADMIN_SERVICE,
78                                     &conf, 0, 0,
79                                     &kadm_handle);
80     free (client_name);
81     if (ret) {
82         krb5_warn (context, ret, "kadm5_c_init_with_skey_ctx");
83         return;
84     }
85     ret = kadm5_randkey_principal (kadm_handle, entry->principal,
86                                    &keys, &num_keys);
87     kadm5_destroy (kadm_handle);
88     if (ret) {
89         krb5_warn(context, ret, "kadm5_randkey_principal");
90         return;
91     }
92     for (i = 0; i < num_keys; ++i) {
93         krb5_keytab_entry new_entry;
94
95         new_entry = *entry;
96         new_entry.timestamp = time (NULL);
97         ++new_entry.vno;
98         new_entry.keyblock  = keys[i];
99
100         ret = krb5_kt_add_entry (context, keytab, &new_entry);
101         if (ret)
102             krb5_warn (context, ret, "krb5_kt_add_entry");
103         krb5_free_keyblock_contents (context, &keys[i]);
104     }
105 }
106
107 /*
108  * loop over all the entries in the keytab (or those given) and change
109  * their keys, writing the new keys
110  */
111
112 int
113 kt_change (int argc, char **argv)
114 {
115     krb5_error_code ret;
116     krb5_kt_cursor cursor;
117     krb5_keytab_entry entry;
118     char *realm = NULL;
119     char *admin_server = NULL;
120     int server_port = 0;
121     int help_flag = 0;
122     int optind = 0;
123     int j, max;
124     krb5_principal *princs;
125     
126     struct getargs args[] = {
127         { "realm",      'r',    arg_string,   NULL, 
128           "realm to use", "realm" 
129         },
130         { "admin-server",       'a',    arg_string, NULL,
131           "server to contact", "host" 
132         },
133         { "server-port",        's',    arg_integer, NULL,
134           "port to contact", "port number" 
135         },
136         { "help",               'h',    arg_flag,    NULL }
137     };
138
139     args[0].value = &realm;
140     args[1].value = &admin_server;
141     args[2].value = &server_port;
142     args[3].value = &help_flag;
143
144     if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optind)
145        || help_flag) {
146         arg_printusage(args, sizeof(args) / sizeof(args[0]), 
147                        "ktutil change", "principal...");
148         return 0;
149     }
150     
151     j = 0;
152     max = 10;
153     princs = malloc (max * sizeof(*princs));
154     if (princs == NULL) {
155         krb5_warnx (context, "malloc: out of memory");
156         return 1;
157     }
158
159     ret = krb5_kt_start_seq_get(context, keytab, &cursor);
160     if(ret){
161         krb5_warn(context, ret, "krb5_kt_start_seq_get");
162         return 1;
163     }
164
165     while((ret = krb5_kt_next_entry(context, keytab, &entry, &cursor)) == 0) {
166         int i;
167         int done = 0;
168
169         for (i = 0; i < j; ++i)
170             if (krb5_principal_compare (context, princs[i],
171                                         entry.principal))
172                 break;
173         if (i < j)
174             continue;
175
176         if (optind == argc) {
177             change_entry (context, &entry, realm, admin_server, server_port);
178             done = 1;
179         } else {
180             for (i = optind; i < argc; ++i) {
181                 krb5_principal princ;
182
183                 ret = krb5_parse_name (context, argv[i], &princ);
184                 if (ret) {
185                     krb5_warn (context, ret, "krb5_parse_name %s", argv[i]);
186                     continue;
187                 }
188                 if (krb5_principal_compare (context, princ, entry.principal)) {
189                     change_entry (context, &entry,
190                                   realm, admin_server, server_port);
191                     done = 1;
192                 }
193                 krb5_free_principal (context, princ);
194             }
195         }
196         if (done) {
197             if (j >= max) {
198                 void *tmp;
199
200                 max *= 2;
201                 tmp = realloc (princs, max * sizeof(*princs));
202                 if (tmp == NULL) {
203                     krb5_kt_free_entry (context, &entry);
204                     krb5_warnx (context, "realloc: out of memory");
205                     break;
206                 }
207                 princs = tmp;
208             }
209             ret = krb5_copy_principal (context, entry.principal, &princs[j]);
210             if (ret) {
211                 krb5_warn (context, ret, "krb5_copy_principal");
212                 krb5_kt_free_entry (context, &entry);
213                 break;
214             }
215             ++j;
216         }
217         krb5_kt_free_entry (context, &entry);
218     }
219     while (j-- > 0)
220         krb5_free_principal (context, princs[j]);
221     free (princs);
222     ret = krb5_kt_end_seq_get(context, keytab, &cursor);
223     return 0;
224 }