remove unused variable
[heimdal.git] / kdc / hprop.c
blob24dd5f6c3b4e552c6034c9be0771031677ce458f
1 /*
2 * Copyright (c) 1997 - 2005 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. 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.
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.
34 #define KRB5_DEPRECATED /* uses v4 functions that will die */
36 #include "hprop.h"
38 RCSID("$Id$");
40 static int version_flag;
41 static int help_flag;
42 static const char *ktname = HPROP_KEYTAB;
43 static const char *database;
44 static char *mkeyfile;
45 static int to_stdout;
46 static int verbose_flag;
47 static int encrypt_flag;
48 static int decrypt_flag;
49 static hdb_master_key mkey5;
51 static char *source_type;
53 static char *afs_cell;
54 static char *v4_realm;
56 static int kaspecials_flag;
57 static int ka_use_null_salt;
59 static char *local_realm=NULL;
61 static int
62 open_socket(krb5_context context, const char *hostname, const char *port)
64 struct addrinfo *ai, *a;
65 struct addrinfo hints;
66 int error;
68 memset (&hints, 0, sizeof(hints));
69 hints.ai_socktype = SOCK_STREAM;
70 hints.ai_protocol = IPPROTO_TCP;
72 error = getaddrinfo (hostname, port, &hints, &ai);
73 if (error) {
74 warnx ("%s: %s", hostname, gai_strerror(error));
75 return -1;
78 for (a = ai; a != NULL; a = a->ai_next) {
79 int s;
81 s = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
82 if (s < 0)
83 continue;
84 if (connect (s, a->ai_addr, a->ai_addrlen) < 0) {
85 warn ("connect(%s)", hostname);
86 close (s);
87 continue;
89 freeaddrinfo (ai);
90 return s;
92 warnx ("failed to contact %s", hostname);
93 freeaddrinfo (ai);
94 return -1;
97 krb5_error_code
98 v5_prop(krb5_context context, HDB *db, hdb_entry_ex *entry, void *appdata)
100 krb5_error_code ret;
101 struct prop_data *pd = appdata;
102 krb5_data data;
104 if(encrypt_flag) {
105 ret = hdb_seal_keys_mkey(context, &entry->entry, mkey5);
106 if (ret) {
107 krb5_warn(context, ret, "hdb_seal_keys_mkey");
108 return ret;
111 if(decrypt_flag) {
112 ret = hdb_unseal_keys_mkey(context, &entry->entry, mkey5);
113 if (ret) {
114 krb5_warn(context, ret, "hdb_unseal_keys_mkey");
115 return ret;
119 ret = hdb_entry2value(context, &entry->entry, &data);
120 if(ret) {
121 krb5_warn(context, ret, "hdb_entry2value");
122 return ret;
125 if(to_stdout)
126 ret = krb5_write_message(context, &pd->sock, &data);
127 else
128 ret = krb5_write_priv_message(context, pd->auth_context,
129 &pd->sock, &data);
130 krb5_data_free(&data);
131 return ret;
135 v4_prop(void *arg, struct v4_principal *p)
137 struct prop_data *pd = arg;
138 hdb_entry_ex ent;
139 krb5_error_code ret;
141 memset(&ent, 0, sizeof(ent));
143 ret = krb5_425_conv_principal(pd->context, p->name, p->instance, v4_realm,
144 &ent.entry.principal);
145 if(ret) {
146 krb5_warn(pd->context, ret,
147 "krb5_425_conv_principal %s.%s@%s",
148 p->name, p->instance, v4_realm);
149 return 0;
152 if(verbose_flag) {
153 char *s;
154 krb5_unparse_name_short(pd->context, ent.entry.principal, &s);
155 krb5_warnx(pd->context, "%s.%s -> %s", p->name, p->instance, s);
156 free(s);
159 ent.entry.kvno = p->kvno;
160 ent.entry.keys.len = 3;
161 ent.entry.keys.val = malloc(ent.entry.keys.len * sizeof(*ent.entry.keys.val));
162 if (ent.entry.keys.val == NULL)
163 krb5_errx(pd->context, ENOMEM, "malloc");
164 if(p->mkvno != -1) {
165 ent.entry.keys.val[0].mkvno = malloc (sizeof(*ent.entry.keys.val[0].mkvno));
166 if (ent.entry.keys.val[0].mkvno == NULL)
167 krb5_errx(pd->context, ENOMEM, "malloc");
168 *(ent.entry.keys.val[0].mkvno) = p->mkvno;
169 } else
170 ent.entry.keys.val[0].mkvno = NULL;
171 ent.entry.keys.val[0].salt = calloc(1, sizeof(*ent.entry.keys.val[0].salt));
172 if (ent.entry.keys.val[0].salt == NULL)
173 krb5_errx(pd->context, ENOMEM, "calloc");
174 ent.entry.keys.val[0].salt->type = KRB5_PADATA_PW_SALT;
175 ent.entry.keys.val[0].key.keytype = ETYPE_DES_CBC_MD5;
176 krb5_data_alloc(&ent.entry.keys.val[0].key.keyvalue, DES_KEY_SZ);
177 memcpy(ent.entry.keys.val[0].key.keyvalue.data, p->key, 8);
179 copy_Key(&ent.entry.keys.val[0], &ent.entry.keys.val[1]);
180 ent.entry.keys.val[1].key.keytype = ETYPE_DES_CBC_MD4;
181 copy_Key(&ent.entry.keys.val[0], &ent.entry.keys.val[2]);
182 ent.entry.keys.val[2].key.keytype = ETYPE_DES_CBC_CRC;
185 int life = _krb5_krb_life_to_time(0, p->max_life);
186 if(life == NEVERDATE){
187 ent.entry.max_life = NULL;
188 } else {
189 /* clean up lifetime a bit */
190 if(life > 86400)
191 life = (life + 86399) / 86400 * 86400;
192 else if(life > 3600)
193 life = (life + 3599) / 3600 * 3600;
194 ALLOC(ent.entry.max_life);
195 *ent.entry.max_life = life;
199 ALLOC(ent.entry.valid_end);
200 *ent.entry.valid_end = p->exp_date;
202 ret = krb5_make_principal(pd->context, &ent.entry.created_by.principal,
203 v4_realm,
204 "kadmin",
205 "hprop",
206 NULL);
207 if(ret){
208 krb5_warn(pd->context, ret, "krb5_make_principal");
209 ret = 0;
210 goto out;
212 ent.entry.created_by.time = time(NULL);
213 ALLOC(ent.entry.modified_by);
214 ret = krb5_425_conv_principal(pd->context, p->mod_name, p->mod_instance,
215 v4_realm, &ent.entry.modified_by->principal);
216 if(ret){
217 krb5_warn(pd->context, ret, "%s.%s@%s", p->name, p->instance, v4_realm);
218 ent.entry.modified_by->principal = NULL;
219 ret = 0;
220 goto out;
222 ent.entry.modified_by->time = p->mod_date;
224 ent.entry.flags.forwardable = 1;
225 ent.entry.flags.renewable = 1;
226 ent.entry.flags.proxiable = 1;
227 ent.entry.flags.postdate = 1;
228 ent.entry.flags.client = 1;
229 ent.entry.flags.server = 1;
231 /* special case password changing service */
232 if(strcmp(p->name, "changepw") == 0 &&
233 strcmp(p->instance, "kerberos") == 0) {
234 ent.entry.flags.forwardable = 0;
235 ent.entry.flags.renewable = 0;
236 ent.entry.flags.proxiable = 0;
237 ent.entry.flags.postdate = 0;
238 ent.entry.flags.initial = 1;
239 ent.entry.flags.change_pw = 1;
242 ret = v5_prop(pd->context, NULL, &ent, pd);
244 if (strcmp (p->name, "krbtgt") == 0
245 && strcmp (v4_realm, p->instance) != 0) {
246 krb5_free_principal (pd->context, ent.entry.principal);
247 ret = krb5_425_conv_principal (pd->context, p->name,
248 v4_realm, p->instance,
249 &ent.entry.principal);
250 if (ret == 0)
251 ret = v5_prop (pd->context, NULL, &ent, pd);
254 out:
255 hdb_free_entry(pd->context, &ent);
256 return ret;
259 #include "kadb.h"
261 /* read a `ka_entry' from `fd' at offset `pos' */
262 static void
263 read_block(krb5_context context, int fd, int32_t pos, void *buf, size_t len)
265 krb5_error_code ret;
266 #ifdef HAVE_PREAD
267 if((ret = pread(fd, buf, len, 64 + pos)) < 0)
268 krb5_err(context, 1, errno, "pread(%u)", 64 + pos);
269 #else
270 if(lseek(fd, 64 + pos, SEEK_SET) == (off_t)-1)
271 krb5_err(context, 1, errno, "lseek(%u)", 64 + pos);
272 ret = read(fd, buf, len);
273 if(ret < 0)
274 krb5_err(context, 1, errno, "read(%lu)", (unsigned long)len);
275 #endif
276 if(ret != len)
277 krb5_errx(context, 1, "read(%lu) = %u", (unsigned long)len, ret);
280 static int
281 ka_convert(struct prop_data *pd, int fd, struct ka_entry *ent)
283 int32_t flags = ntohl(ent->flags);
284 krb5_error_code ret;
285 hdb_entry_ex hdb;
287 if(!kaspecials_flag
288 && (flags & KAFNORMAL) == 0) /* remove special entries */
289 return 0;
290 memset(&hdb, 0, sizeof(hdb));
291 ret = krb5_425_conv_principal(pd->context, ent->name, ent->instance,
292 v4_realm, &hdb.entry.principal);
293 if(ret) {
294 krb5_warn(pd->context, ret,
295 "krb5_425_conv_principal (%s.%s@%s)",
296 ent->name, ent->instance, v4_realm);
297 return 0;
299 hdb.entry.kvno = ntohl(ent->kvno);
300 hdb.entry.keys.len = 3;
301 hdb.entry.keys.val =
302 malloc(hdb.entry.keys.len * sizeof(*hdb.entry.keys.val));
303 if (hdb.entry.keys.val == NULL)
304 krb5_errx(pd->context, ENOMEM, "malloc");
305 hdb.entry.keys.val[0].mkvno = NULL;
306 hdb.entry.keys.val[0].salt = calloc(1, sizeof(*hdb.entry.keys.val[0].salt));
307 if (hdb.entry.keys.val[0].salt == NULL)
308 krb5_errx(pd->context, ENOMEM, "calloc");
309 if (ka_use_null_salt) {
310 hdb.entry.keys.val[0].salt->type = hdb_pw_salt;
311 hdb.entry.keys.val[0].salt->salt.data = NULL;
312 hdb.entry.keys.val[0].salt->salt.length = 0;
313 } else {
314 hdb.entry.keys.val[0].salt->type = hdb_afs3_salt;
315 hdb.entry.keys.val[0].salt->salt.data = strdup(afs_cell);
316 if (hdb.entry.keys.val[0].salt->salt.data == NULL)
317 krb5_errx(pd->context, ENOMEM, "strdup");
318 hdb.entry.keys.val[0].salt->salt.length = strlen(afs_cell);
321 hdb.entry.keys.val[0].key.keytype = ETYPE_DES_CBC_MD5;
322 krb5_data_copy(&hdb.entry.keys.val[0].key.keyvalue,
323 ent->key,
324 sizeof(ent->key));
325 copy_Key(&hdb.entry.keys.val[0], &hdb.entry.keys.val[1]);
326 hdb.entry.keys.val[1].key.keytype = ETYPE_DES_CBC_MD4;
327 copy_Key(&hdb.entry.keys.val[0], &hdb.entry.keys.val[2]);
328 hdb.entry.keys.val[2].key.keytype = ETYPE_DES_CBC_CRC;
330 ALLOC(hdb.entry.max_life);
331 *hdb.entry.max_life = ntohl(ent->max_life);
333 if(ntohl(ent->valid_end) != NEVERDATE && ntohl(ent->valid_end) != 0xffffffff) {
334 ALLOC(hdb.entry.valid_end);
335 *hdb.entry.valid_end = ntohl(ent->valid_end);
338 if (ntohl(ent->pw_change) != NEVERDATE &&
339 ent->pw_expire != 255 &&
340 ent->pw_expire != 0) {
341 ALLOC(hdb.entry.pw_end);
342 *hdb.entry.pw_end = ntohl(ent->pw_change)
343 + 24 * 60 * 60 * ent->pw_expire;
346 ret = krb5_make_principal(pd->context, &hdb.entry.created_by.principal,
347 v4_realm,
348 "kadmin",
349 "hprop",
350 NULL);
351 hdb.entry.created_by.time = time(NULL);
353 if(ent->mod_ptr){
354 struct ka_entry mod;
355 ALLOC(hdb.entry.modified_by);
356 read_block(pd->context, fd, ntohl(ent->mod_ptr), &mod, sizeof(mod));
358 krb5_425_conv_principal(pd->context, mod.name, mod.instance, v4_realm,
359 &hdb.entry.modified_by->principal);
360 hdb.entry.modified_by->time = ntohl(ent->mod_time);
361 memset(&mod, 0, sizeof(mod));
364 hdb.entry.flags.forwardable = 1;
365 hdb.entry.flags.renewable = 1;
366 hdb.entry.flags.proxiable = 1;
367 hdb.entry.flags.postdate = 1;
368 /* XXX - AFS 3.4a creates krbtgt.REALMOFCELL as NOTGS+NOSEAL */
369 if (strcmp(ent->name, "krbtgt") == 0 &&
370 (flags & (KAFNOTGS|KAFNOSEAL)) == (KAFNOTGS|KAFNOSEAL))
371 flags &= ~(KAFNOTGS|KAFNOSEAL);
373 hdb.entry.flags.client = (flags & KAFNOTGS) == 0;
374 hdb.entry.flags.server = (flags & KAFNOSEAL) == 0;
376 ret = v5_prop(pd->context, NULL, &hdb, pd);
377 hdb_free_entry(pd->context, &hdb);
378 return ret;
381 static int
382 ka_dump(struct prop_data *pd, const char *file)
384 struct ka_header header;
385 int i;
386 int fd = open(file, O_RDONLY);
388 if(fd < 0)
389 krb5_err(pd->context, 1, errno, "open(%s)", file);
390 read_block(pd->context, fd, 0, &header, sizeof(header));
391 if(header.version1 != header.version2)
392 krb5_errx(pd->context, 1, "Version mismatch in header: %ld/%ld",
393 (long)ntohl(header.version1), (long)ntohl(header.version2));
394 if(ntohl(header.version1) != 5)
395 krb5_errx(pd->context, 1, "Unknown database version %ld (expected 5)",
396 (long)ntohl(header.version1));
397 for(i = 0; i < ntohl(header.hashsize); i++){
398 int32_t pos = ntohl(header.hash[i]);
399 while(pos){
400 struct ka_entry ent;
401 read_block(pd->context, fd, pos, &ent, sizeof(ent));
402 ka_convert(pd, fd, &ent);
403 pos = ntohl(ent.next);
406 return 0;
411 struct getargs args[] = {
412 { "master-key", 'm', arg_string, &mkeyfile, "v5 master key file", "file" },
413 { "database", 'd', arg_string, &database, "database", "file" },
414 { "source", 0, arg_string, &source_type, "type of database to read",
415 "heimdal"
416 "|mit-dump"
417 "|krb4-dump"
418 "|kaserver"
421 { "v4-realm", 'r', arg_string, &v4_realm, "v4 realm to use" },
422 { "cell", 'c', arg_string, &afs_cell, "name of AFS cell" },
423 { "kaspecials", 'S', arg_flag, &kaspecials_flag, "dump KASPECIAL keys"},
424 { "keytab", 'k', arg_string, &ktname, "keytab to use for authentication", "keytab" },
425 { "v5-realm", 'R', arg_string, &local_realm, "v5 realm to use" },
426 { "decrypt", 'D', arg_flag, &decrypt_flag, "decrypt keys" },
427 { "encrypt", 'E', arg_flag, &encrypt_flag, "encrypt keys" },
428 { "stdout", 'n', arg_flag, &to_stdout, "dump to stdout" },
429 { "verbose", 'v', arg_flag, &verbose_flag },
430 { "version", 0, arg_flag, &version_flag },
431 { "help", 'h', arg_flag, &help_flag }
434 static int num_args = sizeof(args) / sizeof(args[0]);
436 static void
437 usage(int ret)
439 arg_printusage (args, num_args, NULL, "[host[:port]] ...");
440 exit (ret);
443 static void
444 get_creds(krb5_context context, krb5_ccache *cache)
446 krb5_keytab keytab;
447 krb5_principal client;
448 krb5_error_code ret;
449 krb5_get_init_creds_opt *init_opts;
450 krb5_preauthtype preauth = KRB5_PADATA_ENC_TIMESTAMP;
451 krb5_creds creds;
453 ret = krb5_kt_register(context, &hdb_kt_ops);
454 if(ret) krb5_err(context, 1, ret, "krb5_kt_register");
456 ret = krb5_kt_resolve(context, ktname, &keytab);
457 if(ret) krb5_err(context, 1, ret, "krb5_kt_resolve");
459 ret = krb5_make_principal(context, &client, NULL,
460 "kadmin", HPROP_NAME, NULL);
461 if(ret) krb5_err(context, 1, ret, "krb5_make_principal");
463 ret = krb5_get_init_creds_opt_alloc(context, &init_opts);
464 if(ret) krb5_err(context, 1, ret, "krb5_get_init_creds_opt_alloc");
465 krb5_get_init_creds_opt_set_preauth_list(init_opts, &preauth, 1);
467 ret = krb5_get_init_creds_keytab(context, &creds, client, keytab, 0, NULL, init_opts);
468 if(ret) krb5_err(context, 1, ret, "krb5_get_init_creds");
470 krb5_get_init_creds_opt_free(context, init_opts);
472 ret = krb5_kt_close(context, keytab);
473 if(ret) krb5_err(context, 1, ret, "krb5_kt_close");
475 ret = krb5_cc_new_unique(context, krb5_cc_type_memory, NULL, cache);
476 if(ret) krb5_err(context, 1, ret, "krb5_cc_new_unique");
478 ret = krb5_cc_initialize(context, *cache, client);
479 if(ret) krb5_err(context, 1, ret, "krb5_cc_initialize");
481 krb5_free_principal(context, client);
483 ret = krb5_cc_store_cred(context, *cache, &creds);
484 if(ret) krb5_err(context, 1, ret, "krb5_cc_store_cred");
486 krb5_free_cred_contents(context, &creds);
489 enum hprop_source {
490 HPROP_HEIMDAL = 1,
491 HPROP_KRB4_DUMP,
492 HPROP_KASERVER,
493 HPROP_MIT_DUMP
496 #define IS_TYPE_V4(X) ((X) == HPROP_KRB4_DUMP || (X) == HPROP_KASERVER)
498 struct {
499 int type;
500 const char *name;
501 } types[] = {
502 { HPROP_HEIMDAL, "heimdal" },
503 { HPROP_KRB4_DUMP, "krb4-dump" },
504 { HPROP_KASERVER, "kaserver" },
505 { HPROP_MIT_DUMP, "mit-dump" }
508 static int
509 parse_source_type(const char *s)
511 int i;
512 for(i = 0; i < sizeof(types) / sizeof(types[0]); i++) {
513 if(strstr(types[i].name, s) == types[i].name)
514 return types[i].type;
516 return 0;
519 static int
520 iterate (krb5_context context,
521 const char *database_name,
522 HDB *db,
523 int type,
524 struct prop_data *pd)
526 int ret;
528 switch(type) {
529 case HPROP_KRB4_DUMP:
530 ret = v4_prop_dump(pd, database_name);
531 if(ret)
532 krb5_warnx(context, "v4_prop_dump: %s",
533 krb5_get_err_text(context, ret));
534 break;
535 case HPROP_KASERVER:
536 ret = ka_dump(pd, database_name);
537 if(ret)
538 krb5_warn(context, ret, "ka_dump");
539 break;
540 case HPROP_MIT_DUMP:
541 ret = mit_prop_dump(pd, database_name);
542 if (ret)
543 krb5_warnx(context, "mit_prop_dump: %s",
544 krb5_get_err_text(context, ret));
545 break;
546 case HPROP_HEIMDAL:
547 ret = hdb_foreach(context, db, HDB_F_DECRYPT, v5_prop, pd);
548 if(ret)
549 krb5_warn(context, ret, "hdb_foreach");
550 break;
551 default:
552 krb5_errx(context, 1, "unknown prop type: %d", type);
554 return ret;
557 static int
558 dump_database (krb5_context context, int type,
559 const char *database_name, HDB *db)
561 krb5_error_code ret;
562 struct prop_data pd;
563 krb5_data data;
565 pd.context = context;
566 pd.auth_context = NULL;
567 pd.sock = STDOUT_FILENO;
569 ret = iterate (context, database_name, db, type, &pd);
570 if (ret)
571 krb5_errx(context, 1, "iterate failure");
572 krb5_data_zero (&data);
573 ret = krb5_write_message (context, &pd.sock, &data);
574 if (ret)
575 krb5_err(context, 1, ret, "krb5_write_message");
577 return 0;
580 static int
581 propagate_database (krb5_context context, int type,
582 const char *database_name,
583 HDB *db, krb5_ccache ccache,
584 int optidx, int argc, char **argv)
586 krb5_principal server;
587 krb5_error_code ret;
588 int i, failed = 0;
590 for(i = optidx; i < argc; i++){
591 krb5_auth_context auth_context;
592 int fd;
593 struct prop_data pd;
594 krb5_data data;
596 char *port, portstr[NI_MAXSERV];
597 char *host = argv[i];
599 port = strchr(host, ':');
600 if(port == NULL) {
601 snprintf(portstr, sizeof(portstr), "%u",
602 ntohs(krb5_getportbyname (context, "hprop", "tcp",
603 HPROP_PORT)));
604 port = portstr;
605 } else
606 *port++ = '\0';
608 fd = open_socket(context, host, port);
609 if(fd < 0) {
610 failed++;
611 krb5_warn (context, errno, "connect %s", host);
612 continue;
615 ret = krb5_sname_to_principal(context, argv[i],
616 HPROP_NAME, KRB5_NT_SRV_HST, &server);
617 if(ret) {
618 failed++;
619 krb5_warn(context, ret, "krb5_sname_to_principal(%s)", host);
620 close(fd);
621 continue;
624 if (local_realm) {
625 krb5_realm my_realm;
626 krb5_get_default_realm(context,&my_realm);
627 krb5_principal_set_realm(context,server,my_realm);
628 krb5_xfree(my_realm);
631 auth_context = NULL;
632 ret = krb5_sendauth(context,
633 &auth_context,
634 &fd,
635 HPROP_VERSION,
636 NULL,
637 server,
638 AP_OPTS_MUTUAL_REQUIRED | AP_OPTS_USE_SUBKEY,
639 NULL, /* in_data */
640 NULL, /* in_creds */
641 ccache,
642 NULL,
643 NULL,
644 NULL);
646 krb5_free_principal(context, server);
648 if(ret) {
649 failed++;
650 krb5_warn(context, ret, "krb5_sendauth (%s)", host);
651 close(fd);
652 goto next_host;
655 pd.context = context;
656 pd.auth_context = auth_context;
657 pd.sock = fd;
659 ret = iterate (context, database_name, db, type, &pd);
660 if (ret) {
661 krb5_warnx(context, "iterate to host %s failed", host);
662 failed++;
663 goto next_host;
666 krb5_data_zero (&data);
667 ret = krb5_write_priv_message(context, auth_context, &fd, &data);
668 if(ret) {
669 krb5_warn(context, ret, "krb5_write_priv_message");
670 failed++;
671 goto next_host;
674 ret = krb5_read_priv_message(context, auth_context, &fd, &data);
675 if(ret) {
676 krb5_warn(context, ret, "krb5_read_priv_message: %s", host);
677 failed++;
678 goto next_host;
679 } else
680 krb5_data_free (&data);
682 next_host:
683 krb5_auth_con_free(context, auth_context);
684 close(fd);
686 if (failed)
687 return 1;
688 return 0;
692 main(int argc, char **argv)
694 krb5_error_code ret;
695 krb5_context context;
696 krb5_ccache ccache = NULL;
697 HDB *db = NULL;
698 int optidx = 0;
700 int type, exit_code;
702 setprogname(argv[0]);
704 if(getarg(args, num_args, argc, argv, &optidx))
705 usage(1);
707 if(help_flag)
708 usage(0);
710 if(version_flag){
711 print_version(NULL);
712 exit(0);
715 ret = krb5_init_context(&context);
716 if(ret)
717 exit(1);
719 if(local_realm)
720 krb5_set_default_realm(context, local_realm);
722 if(v4_realm == NULL) {
723 ret = krb5_get_default_realm(context, &v4_realm);
724 if(ret)
725 krb5_err(context, 1, ret, "krb5_get_default_realm");
728 if(afs_cell == NULL) {
729 afs_cell = strdup(v4_realm);
730 if(afs_cell == NULL)
731 krb5_errx(context, 1, "out of memory");
732 strlwr(afs_cell);
736 if(encrypt_flag && decrypt_flag)
737 krb5_errx(context, 1,
738 "only one of `--encrypt' and `--decrypt' is meaningful");
740 if(source_type != NULL) {
741 type = parse_source_type(source_type);
742 if(type == 0)
743 krb5_errx(context, 1, "unknown source type `%s'", source_type);
744 } else
745 type = HPROP_HEIMDAL;
747 if(!to_stdout)
748 get_creds(context, &ccache);
750 if(decrypt_flag || encrypt_flag) {
751 ret = hdb_read_master_key(context, mkeyfile, &mkey5);
752 if(ret && ret != ENOENT)
753 krb5_err(context, 1, ret, "hdb_read_master_key");
754 if(ret)
755 krb5_errx(context, 1, "No master key file found");
758 if (IS_TYPE_V4(type) && v4_realm == NULL)
759 krb5_errx(context, 1, "Its a Kerberos 4 database "
760 "but no realm configured");
762 switch(type) {
763 case HPROP_KASERVER:
764 if (database == NULL)
765 database = DEFAULT_DATABASE;
766 ka_use_null_salt = krb5_config_get_bool_default(context, NULL, FALSE,
767 "hprop",
768 "afs_uses_null_salt",
769 NULL);
771 break;
772 case HPROP_KRB4_DUMP:
773 if (database == NULL)
774 krb5_errx(context, 1, "no dump file specified");
776 break;
777 case HPROP_MIT_DUMP:
778 if (database == NULL)
779 krb5_errx(context, 1, "no dump file specified");
780 break;
781 case HPROP_HEIMDAL:
782 ret = hdb_create (context, &db, database);
783 if(ret)
784 krb5_err(context, 1, ret, "hdb_create: %s", database);
785 ret = db->hdb_open(context, db, O_RDONLY, 0);
786 if(ret)
787 krb5_err(context, 1, ret, "db->hdb_open");
788 break;
789 default:
790 krb5_errx(context, 1, "unknown dump type `%d'", type);
791 break;
794 if (to_stdout)
795 exit_code = dump_database (context, type, database, db);
796 else
797 exit_code = propagate_database (context, type, database,
798 db, ccache, optidx, argc, argv);
800 if(ccache != NULL)
801 krb5_cc_destroy(context, ccache);
803 if(db != NULL)
804 (*db->hdb_destroy)(context, db);
806 krb5_free_context(context);
807 return exit_code;