x
[heimdal.git] / admin / util.c
blobc6db682e5a1448dabc4dbae00d00a044afdd9731
1 /*
2 * Copyright (c) 1997 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Kungliga Tekniska
20 * Högskolan and its contributors.
22 * 4. Neither the name of the Institute nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
39 #include "admin_locl.h"
40 #include <parse_units.h>
42 void
43 init_des_key(hdb_entry *ent)
45 Key *k;
46 ent->keys.val = realloc(ent->keys.val,
47 (ent->keys.len + 1) * sizeof(*ent->keys.val));
48 k = ent->keys.val + ent->keys.len;
49 ent->keys.len++;
50 memset(k, 0, sizeof(*k));
51 krb5_generate_random_keyblock(context, KEYTYPE_DES, &k->key);
52 seal_key(k);
53 ent->kvno++;
56 void
57 set_keys(hdb_entry *ent, char *password)
59 krb5_data salt;
60 int i;
62 memset(&salt, 0, sizeof(salt));
63 krb5_get_salt(ent->principal, &salt);
64 for(i = 0; i < ent->keys.len; i++) {
65 krb5_string_to_key(password, &salt, &ent->keys.val[i].key); /* XXX */
66 seal_key(&ent->keys.val[i]);
68 krb5_data_free(&salt);
69 ent->kvno++;
72 char *
73 time2str(time_t t)
75 static char buf[128];
76 strftime(buf, sizeof(buf), "%Y%m%d%H%M%S", gmtime(&t));
77 return buf;
80 void
81 event2string(Event *ev, char **str)
83 char *p;
84 char *pr;
85 if(ev == NULL){
86 *str = strdup("-");
87 return;
89 krb5_unparse_name(context, ev->principal, &pr);
90 asprintf(&p, "%s:%s", time2str(ev->time), pr);
91 free(pr);
92 *str = p;
95 void
96 print_hdbflags (FILE *fp, HDBFlags flags)
98 char buf[1024];
100 unparse_flags (HDBFlags2int(flags), HDBFlags_units, buf, sizeof(buf));
101 fprintf (fp, "%s", buf);
105 parse_hdbflags (const char *s, HDBFlags *flags)
107 int t;
109 t = parse_flags (s, HDBFlags_units, HDBFlags2int(*flags));
110 if (t < 0)
111 return t;
112 else {
113 *flags = int2HDBFlags(t);
114 return 0;
118 void
119 init_entry (HDB *db, hdb_entry *ent)
121 krb5_realm *realm;
122 krb5_principal def_principal;
123 hdb_entry def;
125 realm = krb5_princ_realm(context, ent->principal);
126 krb5_build_principal(context, &def_principal,
127 strlen(*realm),
128 *realm,
129 "default",
130 NULL);
131 def.principal = def_principal;
132 if(db->fetch(context, db, &def)) {
133 krb5_free_principal(context, def_principal);
134 krb5_make_principal(context, &def_principal, NULL, "default", NULL);
135 def.principal = def_principal;
136 if(db->fetch(context, db, &def)){
137 krb5_warnx(context, "No `default' entry found. "
138 "(have you initialised the database?)");
139 memset(&def, 0, sizeof(def));
140 }else
141 krb5_warnx(context, "No `default' principal found for %s, "
142 "using local realms default.", *realm);
144 krb5_free_principal (context, def_principal);
145 ent->flags.client = 1;
146 ent->flags.server = 1;
147 ent->flags.forwardable = 1;
148 ent->flags.proxiable = 1;
149 ent->flags.renewable = 1;
150 ent->flags.postdate = 1;
151 if(def.max_life){
152 ent->max_life = malloc(sizeof(*ent->max_life));
153 *ent->max_life = *def.max_life;
155 if(def.max_renew){
156 ent->max_renew = malloc(sizeof(*ent->max_renew));
157 *ent->max_renew = *def.max_renew;
159 hdb_free_entry(context, &def);
162 static void
163 set_event (hdb_entry *ent, Event *ev)
165 krb5_realm *realm;
167 ev->time = time(NULL);
168 realm = krb5_princ_realm(context, ent->principal);
170 krb5_build_principal(context, &ev->principal,
171 strlen(*realm),
172 *realm,
173 "kadmin",
174 NULL);
177 void
178 set_created_by (hdb_entry *ent)
180 set_event (ent, &ent->created_by);
183 void
184 set_modified_by (hdb_entry *ent)
186 if (ent->modified_by)
187 free_Event(ent->modified_by);
188 else
189 ent->modified_by = malloc(sizeof(*ent->modified_by));
190 set_event (ent, ent->modified_by);
193 static void
194 get_life (const char *name, unsigned **v)
196 char buf[128];
197 time_t t;
199 if (*v) {
200 unparse_time (**v, buf, sizeof(buf));
201 t = getlife (name, buf);
202 } else {
203 t = getlife (name, "unlimited");
205 if (t) {
206 if(*v == NULL)
207 *v = malloc(sizeof(**v));
208 **v = t;
209 } else if(*v) {
210 free(*v);
211 *v = NULL;
215 static void
216 get_time (const char *name, time_t **v)
218 /* XXX */
221 static void
222 get_flags(const char *name, HDBFlags *flags)
224 char buf[1024];
226 fprintf (stderr, "%s [", name);
227 print_hdbflags (stderr, *flags);
228 fprintf (stderr, "]: ");
229 if(fgets(buf, sizeof(buf), stdin) == NULL)
230 return;
231 buf[strlen(buf) - 1] = '\0';
232 if(*buf != '\0')
233 parse_hdbflags(buf, flags);
236 void
237 edit_entry(hdb_entry *ent)
239 get_time ("Valid start", &ent->valid_start);
240 get_time ("Valid end", &ent->valid_end);
241 get_time ("Password end", &ent->pw_end);
242 get_life ("Max ticket life", &ent->max_life);
243 get_life ("Max renewable life", &ent->max_renew);
244 get_flags ("Flags", &ent->flags);
248 set_password(hdb_entry *ent)
250 char buf[128];
251 int i;
253 if(des_read_pw_string(buf, sizeof(buf), "Password:", 1))
254 return -1;
255 for (i = 0; i < ent->keys.len; ++i)
256 free_Key (&ent->keys.val[i]);
257 free (ent->keys.val);
258 if(strcasecmp(buf, "random") == 0) {
259 ent->keys.len = 0;
260 ent->keys.val = NULL;
261 init_des_key(ent);
262 } else{
263 ent->keys.len = 1;
264 ent->keys.val = calloc(1, sizeof(*ent->keys.val));
265 set_keys(ent, buf);
267 return 0;