2 * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
39 #include "kadmin_locl.h"
40 #include <kadm5/private.h>
61 while(*p
&& !isspace((unsigned char)*p
))
64 while(*p
&& isspace((unsigned char)*p
)) p
++;
69 parse_time_string(time_t *t
, char *s
)
71 int year
, month
, date
, hour
, minute
, second
;
73 if(strcmp(s
, "-") == 0)
76 t
= malloc(sizeof(*t
));
77 sscanf(s
, "%04d%02d%02d%02d%02d%02d",
78 &year
, &month
, &date
, &hour
, &minute
, &second
);
79 tm
.tm_year
= year
- 1900;
80 tm
.tm_mon
= month
- 1;
91 parse_integer(unsigned *u
, char *s
)
93 if(strcmp(s
, "-") == 0)
96 u
= malloc(sizeof(*u
));
102 parse_keys(hdb_entry
*ent
, char *str
)
108 p
= strsep(&str
, ":");
109 sscanf(p
, "%d", &tmp
);
111 p
= strsep(&str
, ":");
114 key
= realloc(ent
->keys
.val
,
115 (ent
->keys
.len
+ 1) * sizeof(*ent
->keys
.val
));
119 key
= ent
->keys
.val
+ ent
->keys
.len
;
121 memset(key
, 0, sizeof(*key
));
122 if(sscanf(p
, "%d", &tmp
) == 1) {
123 key
->mkvno
= malloc(sizeof(*key
->mkvno
));
127 p
= strsep(&str
, ":");
128 sscanf(p
, "%d", &tmp
);
129 key
->key
.keytype
= tmp
;
130 p
= strsep(&str
, ":");
131 krb5_data_alloc(&key
->key
.keyvalue
, (strlen(p
) - 1) / 2 + 1);
132 for(i
= 0; i
< strlen(p
); i
+= 2){
133 sscanf(p
+ i
, "%02x", &tmp
);
134 ((u_char
*)key
->key
.keyvalue
.data
)[i
/ 2] = tmp
;
136 p
= strsep(&str
, ":");
137 if(strcmp(p
, "-") != 0){
140 if(sscanf(p
, "%u/", &type
) != 1){
149 key
->salt
= malloc(sizeof(*key
->salt
));
150 key
->salt
->type
= type
;
154 krb5_data_copy(&key
->salt
->salt
, p
+ 1, p_len
- 2);
156 krb5_data_alloc(&key
->salt
->salt
, (p_len
- 1) / 2 + 1);
157 for(i
= 0; i
< p_len
; i
+= 2){
158 sscanf(p
+ i
, "%02x", &tmp
);
159 ((u_char
*)key
->salt
->salt
.data
)[i
/ 2] = tmp
;
163 krb5_data_zero (&key
->salt
->salt
);
165 p
= strsep(&str
, ":");
170 parse_event(Event
*ev
, char *str
)
173 if(strcmp(str
, "-") == 0)
176 ev
= malloc(sizeof(*ev
));
177 memset(ev
, 0, sizeof(*ev
));
178 p
= strsep(&str
, ":");
179 parse_time_string(&ev
->time
, p
);
180 p
= strsep(&str
, ":");
181 krb5_parse_name(context
, p
, &ev
->principal
);
186 parse_hdbflags2int(char *str
)
189 parse_integer(&i
, str
);
191 return int2HDBFlags(i
);
196 parse_etypes(char *str
, unsigned **val
, unsigned *len
)
202 while(sscanf(str
, "%u", &v
) == 1) {
203 *val
= realloc(*val
, (*len
+1) * sizeof(**val
));
204 (*val
)[(*len
)++] = v
;
205 str
= strchr(str
, ':');
214 doit(char *filename
, int merge
)
224 HDB
*db
= _kadm5_s_get_db(kadm_handle
);
226 f
= fopen(filename
, "r");
228 krb5_warn(context
, errno
, "fopen(%s)", filename
);
232 flags
|= O_CREAT
| O_TRUNC
;
233 ret
= db
->open(context
, db
, flags
, 0600);
235 krb5_warn(context
, ret
, "hdb_open");
240 while(fgets(s
, sizeof(s
), f
)){
246 else if(isspace((unsigned char)*p
)) {
283 memset(&ent
, 0, sizeof(ent
));
284 ret
= krb5_parse_name(context
, e
.principal
, &ent
.principal
);
286 fprintf(stderr
, "%s:%d:%s (%s)\n",
289 krb5_get_err_text(context
, ret
),
294 parse_keys(&ent
, e
.key
);
296 parse_event(&ent
.created_by
, e
.created
);
297 ent
.modified_by
= parse_event(NULL
, e
.modified
);
298 ent
.valid_start
= parse_time_string(NULL
, e
.valid_start
);
299 ent
.valid_end
= parse_time_string(NULL
, e
.valid_end
);
300 ent
.pw_end
= parse_time_string(NULL
, e
.pw_end
);
301 ent
.max_life
= parse_integer(NULL
, e
.max_life
);
302 ent
.max_renew
= parse_integer(NULL
, e
.max_renew
);
303 ent
.flags
= parse_hdbflags2int(e
.flags
);
306 parse_etypes(e
.etypes
, &ent
.etypes
->val
, &ent
.etypes
->len
);
307 if(ent
.etypes
->len
== 0) {
313 db
->store(context
, db
, HDB_F_REPLACE
, &ent
);
314 hdb_free_entry (context
, &ent
);
316 db
->close(context
, db
);
321 load(int argc
, char **argv
)
324 krb5_warnx(context
, "Usage: load filename");
332 merge(int argc
, char **argv
)
335 krb5_warnx(context
, "Usage: merge filename");