xref: /illumos-gate/usr/src/cmd/krb5/kadmin/dbutil/dump.c (revision 2d9a5a52c758e1dbaee1569f0d91634a0f5cbe39)
17c478bd9Sstevel@tonic-gate /*
22dd2efa5Swillf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  *	Openvision retains the copyright to derivative works of
107c478bd9Sstevel@tonic-gate  *	this source code.  Do *NOT* create a derivative of this
117c478bd9Sstevel@tonic-gate  *	source code before consulting with your legal department.
127c478bd9Sstevel@tonic-gate  *	Do *NOT* integrate *ANY* of this source code into another
137c478bd9Sstevel@tonic-gate  *	product before consulting with your legal department.
147c478bd9Sstevel@tonic-gate  *
157c478bd9Sstevel@tonic-gate  *	For further information, read the top-level Openvision
167c478bd9Sstevel@tonic-gate  *	copyright which is contained in the top-level MIT Kerberos
177c478bd9Sstevel@tonic-gate  *	copyright.
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
207c478bd9Sstevel@tonic-gate  *
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate /*
257c478bd9Sstevel@tonic-gate  * admin/edit/dump.c
267c478bd9Sstevel@tonic-gate  *
277c478bd9Sstevel@tonic-gate  * Copyright 1990,1991 by the Massachusetts Institute of Technology.
287c478bd9Sstevel@tonic-gate  * All Rights Reserved.
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may
317c478bd9Sstevel@tonic-gate  *   require a specific license from the United States Government.
327c478bd9Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
337c478bd9Sstevel@tonic-gate  *   export to obtain such a license before exporting.
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
367c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
377c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
387c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
397c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
407c478bd9Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
417c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
427c478bd9Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
437c478bd9Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
447c478bd9Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
457c478bd9Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
467c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
477c478bd9Sstevel@tonic-gate  * or implied warranty.
487c478bd9Sstevel@tonic-gate  *
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  * Dump a KDC database
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #include <stdio.h>
5454925bf6Swillf #include <k5-int.h>
557c478bd9Sstevel@tonic-gate #include <kadm5/admin.h>
5654925bf6Swillf #include <kadm5/server_internal.h>
5754925bf6Swillf #include <kdb.h>
5854925bf6Swillf #include <com_err.h>
597c478bd9Sstevel@tonic-gate #include <libintl.h>
607c478bd9Sstevel@tonic-gate #include "kdb5_util.h"
6156a424ccSmp153739 #if defined(HAVE_REGEX_H) && defined(HAVE_REGCOMP)
627c478bd9Sstevel@tonic-gate #include <regex.h>
637c478bd9Sstevel@tonic-gate #endif	/* HAVE_REGEX_H */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * Needed for master key conversion.
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate extern krb5_keyblock master_key;
697c478bd9Sstevel@tonic-gate extern krb5_principal master_princ;
707c478bd9Sstevel@tonic-gate static int			mkey_convert;
717c478bd9Sstevel@tonic-gate static krb5_keyblock		new_master_key;
727c478bd9Sstevel@tonic-gate 
7356a424ccSmp153739 static int	backwards;
7456a424ccSmp153739 static int	recursive;
7556a424ccSmp153739 
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate  * Use compile(3) if no regcomp present.
787c478bd9Sstevel@tonic-gate  */
797c478bd9Sstevel@tonic-gate #if	!defined(HAVE_REGCOMP) && defined(HAVE_REGEXP_H)
807c478bd9Sstevel@tonic-gate #define	INIT		char *sp = instring;
817c478bd9Sstevel@tonic-gate #define	GETC()		(*sp++)
827c478bd9Sstevel@tonic-gate #define	PEEKC()		(*sp)
837c478bd9Sstevel@tonic-gate #define	UNGETC(c)	(--sp)
847c478bd9Sstevel@tonic-gate #define	RETURN(c)	return(c)
857c478bd9Sstevel@tonic-gate #define	ERROR(c)
867c478bd9Sstevel@tonic-gate #define	RE_BUF_SIZE	1024
877c478bd9Sstevel@tonic-gate #include <regexp.h>
887c478bd9Sstevel@tonic-gate #endif	/* !HAVE_REGCOMP && HAVE_REGEXP_H */
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate struct dump_args {
917c478bd9Sstevel@tonic-gate     char		*programname;
927c478bd9Sstevel@tonic-gate     FILE		*ofile;
937c478bd9Sstevel@tonic-gate     krb5_context	kcontext;
947c478bd9Sstevel@tonic-gate     char		**names;
957c478bd9Sstevel@tonic-gate     int			nnames;
967c478bd9Sstevel@tonic-gate     int			verbose;
977c478bd9Sstevel@tonic-gate };
987c478bd9Sstevel@tonic-gate 
9956a424ccSmp153739 static krb5_error_code dump_k5beta_iterator (krb5_pointer,
100505d05c7Sgtb 					     krb5_db_entry *);
10156a424ccSmp153739 static krb5_error_code dump_k5beta6_iterator (krb5_pointer,
102505d05c7Sgtb 					      krb5_db_entry *);
10356a424ccSmp153739 static krb5_error_code dump_k5beta6_iterator_ext (krb5_pointer,
10456a424ccSmp153739 						  krb5_db_entry *,
10556a424ccSmp153739 						  int);
10656a424ccSmp153739 static krb5_error_code dump_iprop_iterator (krb5_pointer,
107505d05c7Sgtb 						  krb5_db_entry *);
10856a424ccSmp153739 static krb5_error_code dump_k5beta7_princ (krb5_pointer,
109505d05c7Sgtb 					   krb5_db_entry *);
11056a424ccSmp153739 static krb5_error_code dump_k5beta7_princ_ext (krb5_pointer,
11156a424ccSmp153739 					       krb5_db_entry *,
11256a424ccSmp153739 					       int);
11356a424ccSmp153739 static krb5_error_code dump_k5beta7_princ_withpolicy
11456a424ccSmp153739 			(krb5_pointer, krb5_db_entry *);
11556a424ccSmp153739 static krb5_error_code dump_iprop_princ (krb5_pointer,
116505d05c7Sgtb 					       krb5_db_entry *);
11756a424ccSmp153739 static krb5_error_code dump_ov_princ (krb5_pointer,
118505d05c7Sgtb 				      krb5_db_entry *);
119505d05c7Sgtb static void dump_k5beta7_policy (void *, osa_policy_ent_t);
1207c478bd9Sstevel@tonic-gate 
12156a424ccSmp153739 typedef krb5_error_code (*dump_func)(krb5_pointer,
122505d05c7Sgtb 				     krb5_db_entry *);
1237c478bd9Sstevel@tonic-gate 
12456a424ccSmp153739 static int process_k5beta_record (char *, krb5_context,
12554925bf6Swillf 				  FILE *, int, int *);
12656a424ccSmp153739 static int process_k5beta6_record (char *, krb5_context,
12754925bf6Swillf 				   FILE *, int, int *);
12856a424ccSmp153739 static int process_k5beta7_record (char *, krb5_context,
12954925bf6Swillf 				   FILE *, int, int *);
13056a424ccSmp153739 static int process_ov_record (char *, krb5_context,
13154925bf6Swillf 			      FILE *, int, int *);
13256a424ccSmp153739 typedef krb5_error_code (*load_func)(char *, krb5_context,
13354925bf6Swillf 				     FILE *, int, int *);
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate typedef struct _dump_version {
1367c478bd9Sstevel@tonic-gate      char *name;
1377c478bd9Sstevel@tonic-gate      char *header;
1387c478bd9Sstevel@tonic-gate      int updateonly;
1397c478bd9Sstevel@tonic-gate      int create_kadm5;
1407c478bd9Sstevel@tonic-gate      dump_func dump_princ;
1417c478bd9Sstevel@tonic-gate      osa_adb_iter_policy_func dump_policy;
1427c478bd9Sstevel@tonic-gate      load_func load_record;
1437c478bd9Sstevel@tonic-gate } dump_version;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate dump_version old_version = {
1467c478bd9Sstevel@tonic-gate      "Kerberos version 5 old format",
1477c478bd9Sstevel@tonic-gate      "kdb5_edit load_dump version 2.0\n",
1487c478bd9Sstevel@tonic-gate      0,
1497c478bd9Sstevel@tonic-gate      1,
1507c478bd9Sstevel@tonic-gate      dump_k5beta_iterator,
1517c478bd9Sstevel@tonic-gate      NULL,
15254925bf6Swillf      process_k5beta_record
1537c478bd9Sstevel@tonic-gate };
1547c478bd9Sstevel@tonic-gate dump_version beta6_version = {
1557c478bd9Sstevel@tonic-gate      "Kerberos version 5 beta 6 format",
1567c478bd9Sstevel@tonic-gate      "kdb5_edit load_dump version 3.0\n",
1577c478bd9Sstevel@tonic-gate      0,
1587c478bd9Sstevel@tonic-gate      1,
1597c478bd9Sstevel@tonic-gate      dump_k5beta6_iterator,
1607c478bd9Sstevel@tonic-gate      NULL,
16154925bf6Swillf      process_k5beta6_record
1627c478bd9Sstevel@tonic-gate };
1637c478bd9Sstevel@tonic-gate dump_version beta7_version = {
1647c478bd9Sstevel@tonic-gate      "Kerberos version 5",
1657c478bd9Sstevel@tonic-gate      "kdb5_util load_dump version 4\n",
1667c478bd9Sstevel@tonic-gate      0,
1677c478bd9Sstevel@tonic-gate      0,
1687c478bd9Sstevel@tonic-gate      dump_k5beta7_princ,
1697c478bd9Sstevel@tonic-gate      dump_k5beta7_policy,
17054925bf6Swillf      process_k5beta7_record
1717c478bd9Sstevel@tonic-gate };
1727c478bd9Sstevel@tonic-gate dump_version iprop_version = {
1737c478bd9Sstevel@tonic-gate      "Kerberos iprop version",
1747c478bd9Sstevel@tonic-gate      "iprop",
1757c478bd9Sstevel@tonic-gate      0,
1767c478bd9Sstevel@tonic-gate      0,
1777c478bd9Sstevel@tonic-gate      dump_iprop_princ,
1787c478bd9Sstevel@tonic-gate      dump_k5beta7_policy,
17954925bf6Swillf      process_k5beta7_record
1807c478bd9Sstevel@tonic-gate };
1817c478bd9Sstevel@tonic-gate dump_version ov_version = {
1827c478bd9Sstevel@tonic-gate      "OpenV*Secure V1.0",
1837c478bd9Sstevel@tonic-gate      "OpenV*Secure V1.0\t",
1847c478bd9Sstevel@tonic-gate      1,
1857c478bd9Sstevel@tonic-gate      1,
1867c478bd9Sstevel@tonic-gate      dump_ov_princ,
1877c478bd9Sstevel@tonic-gate      dump_k5beta7_policy,
18854925bf6Swillf      process_ov_record
1897c478bd9Sstevel@tonic-gate };
1907c478bd9Sstevel@tonic-gate 
19156a424ccSmp153739 dump_version r1_3_version = {
19256a424ccSmp153739      "Kerberos version 5 release 1.3",
19356a424ccSmp153739      "kdb5_util load_dump version 5\n",
19456a424ccSmp153739      0,
19556a424ccSmp153739      0,
19656a424ccSmp153739      dump_k5beta7_princ_withpolicy,
19756a424ccSmp153739      dump_k5beta7_policy,
19856a424ccSmp153739      process_k5beta7_record,
19956a424ccSmp153739 };
20056a424ccSmp153739 
2017c478bd9Sstevel@tonic-gate /* External data */
2027c478bd9Sstevel@tonic-gate extern char		*current_dbname;
2037c478bd9Sstevel@tonic-gate extern krb5_boolean	dbactive;
2047c478bd9Sstevel@tonic-gate extern int		exit_status;
2057c478bd9Sstevel@tonic-gate extern krb5_context	util_context;
2067c478bd9Sstevel@tonic-gate extern kadm5_config_params global_params;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate /* Strings */
2097c478bd9Sstevel@tonic-gate 
21056a424ccSmp153739 #define k5beta_dump_header	"kdb5_edit load_dump version 2.0\n"
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate static const char null_mprinc_name[] = "kdb5_dump@MISSING";
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate /*
2157c478bd9Sstevel@tonic-gate  * We define gettext(s) to be s here, so that xgettext will extract the
2167c478bd9Sstevel@tonic-gate  * strings to the .po file. At the end of the message section we will
2177c478bd9Sstevel@tonic-gate  * undef gettext so that we can use it as a funtion.
2187c478bd9Sstevel@tonic-gate  */
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate #define	gettext(s) s
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate /* Message strings */
2237c478bd9Sstevel@tonic-gate static const char regex_err[] =
2247c478bd9Sstevel@tonic-gate 	gettext("%s: regular expression error - %s\n");
2257c478bd9Sstevel@tonic-gate static const char regex_merr[] =
2267c478bd9Sstevel@tonic-gate 	gettext("%s: regular expression match error - %s\n");
2277c478bd9Sstevel@tonic-gate static const char pname_unp_err[] =
2287c478bd9Sstevel@tonic-gate 	gettext("%s: cannot unparse principal name (%s)\n");
2297c478bd9Sstevel@tonic-gate static const char mname_unp_err[] =
2307c478bd9Sstevel@tonic-gate 	gettext("%s: cannot unparse modifier name (%s)\n");
2317c478bd9Sstevel@tonic-gate static const char nokeys_err[] =
2327c478bd9Sstevel@tonic-gate 	gettext("%s: cannot find any standard key for %s\n");
2337c478bd9Sstevel@tonic-gate static const char sdump_tl_inc_err[] =
2347c478bd9Sstevel@tonic-gate 	gettext("%s: tagged data list inconsistency for %s "
2357c478bd9Sstevel@tonic-gate 		"(counted %d, stored %d)\n");
2367c478bd9Sstevel@tonic-gate static const char stand_fmt_name[] =
2377c478bd9Sstevel@tonic-gate 	gettext("Kerberos version 5");
2387c478bd9Sstevel@tonic-gate static const char old_fmt_name[] =
2397c478bd9Sstevel@tonic-gate 	gettext("Kerberos version 5 old format");
2407c478bd9Sstevel@tonic-gate static const char b6_fmt_name[] =
2417c478bd9Sstevel@tonic-gate 	gettext("Kerberos version 5 beta 6 format");
2427c478bd9Sstevel@tonic-gate static const char ofopen_error[] =
2437c478bd9Sstevel@tonic-gate 	gettext("%s: cannot open %s for writing (%s)\n");
2447c478bd9Sstevel@tonic-gate static const char oflock_error[] =
2457c478bd9Sstevel@tonic-gate 	gettext("%s: cannot lock %s (%s)\n");
2467c478bd9Sstevel@tonic-gate static const char dumprec_err[] =
2477c478bd9Sstevel@tonic-gate 	gettext("%s: error performing %s dump (%s)\n");
2487c478bd9Sstevel@tonic-gate static const char dumphdr_err[] =
2497c478bd9Sstevel@tonic-gate 	gettext("%s: error dumping %s header (%s)\n");
2507c478bd9Sstevel@tonic-gate static const char trash_end_fmt[] =
2517c478bd9Sstevel@tonic-gate 	gettext("%s(%d): ignoring trash at end of line: ");
2527c478bd9Sstevel@tonic-gate static const char read_name_string[] =
2537c478bd9Sstevel@tonic-gate 	gettext("name string");
2547c478bd9Sstevel@tonic-gate static const char read_key_type[] =
2557c478bd9Sstevel@tonic-gate 	gettext("key type");
2567c478bd9Sstevel@tonic-gate static const char read_key_data[] =
2577c478bd9Sstevel@tonic-gate 	gettext("key data");
2587c478bd9Sstevel@tonic-gate static const char read_pr_data1[] =
2597c478bd9Sstevel@tonic-gate 	gettext("first set of principal attributes");
2607c478bd9Sstevel@tonic-gate static const char read_mod_name[] =
2617c478bd9Sstevel@tonic-gate 	gettext("modifier name");
2627c478bd9Sstevel@tonic-gate static const char read_pr_data2[] =
2637c478bd9Sstevel@tonic-gate 	gettext("second set of principal attributes");
2647c478bd9Sstevel@tonic-gate static const char read_salt_data[] =
2657c478bd9Sstevel@tonic-gate 	gettext("salt data");
2667c478bd9Sstevel@tonic-gate static const char read_akey_type[] =
2677c478bd9Sstevel@tonic-gate 	gettext("alternate key type");
2687c478bd9Sstevel@tonic-gate static const char read_akey_data[] =
2697c478bd9Sstevel@tonic-gate 	gettext("alternate key data");
2707c478bd9Sstevel@tonic-gate static const char read_asalt_type[] =
2717c478bd9Sstevel@tonic-gate 	gettext("alternate salt type");
2727c478bd9Sstevel@tonic-gate static const char read_asalt_data[] =
2737c478bd9Sstevel@tonic-gate 	gettext("alternate salt data");
2747c478bd9Sstevel@tonic-gate static const char read_exp_data[] =
2757c478bd9Sstevel@tonic-gate 	gettext("expansion data");
2767c478bd9Sstevel@tonic-gate static const char store_err_fmt[] =
2777c478bd9Sstevel@tonic-gate 	gettext("%s(%d): cannot store %s(%s)\n");
2787c478bd9Sstevel@tonic-gate static const char add_princ_fmt[] =
2797c478bd9Sstevel@tonic-gate 	gettext("%s\n");
2807c478bd9Sstevel@tonic-gate static const char parse_err_fmt[] =
2817c478bd9Sstevel@tonic-gate 	gettext("%s(%d): cannot parse %s (%s)\n");
2827c478bd9Sstevel@tonic-gate static const char read_err_fmt[] =
2837c478bd9Sstevel@tonic-gate 	gettext("%s(%d): cannot read %s\n");
2847c478bd9Sstevel@tonic-gate static const char no_mem_fmt[] =
2857c478bd9Sstevel@tonic-gate 	gettext("%s(%d): no memory for buffers\n");
2867c478bd9Sstevel@tonic-gate static const char rhead_err_fmt[] =
2877c478bd9Sstevel@tonic-gate 	gettext("%s(%d): cannot match size tokens\n");
2887c478bd9Sstevel@tonic-gate static const char err_line_fmt[] =
2897c478bd9Sstevel@tonic-gate 	gettext("%s: error processing line %d of %s\n");
2907c478bd9Sstevel@tonic-gate static const char head_bad_fmt[] =
2917c478bd9Sstevel@tonic-gate 	gettext("%s: dump header bad in %s\n");
2927c478bd9Sstevel@tonic-gate static const char read_bytecnt[] =
2937c478bd9Sstevel@tonic-gate 	gettext("record byte count");
2947c478bd9Sstevel@tonic-gate static const char read_encdata[] =
2957c478bd9Sstevel@tonic-gate 	gettext("encoded data");
2967c478bd9Sstevel@tonic-gate static const char n_name_unp_fmt[] =
2977c478bd9Sstevel@tonic-gate 	gettext("%s(%s): cannot unparse name\n");
2987c478bd9Sstevel@tonic-gate static const char n_dec_cont_fmt[] =
2997c478bd9Sstevel@tonic-gate 	gettext("%s(%s): cannot decode contents\n");
3007c478bd9Sstevel@tonic-gate static const char read_nint_data[] =
3017c478bd9Sstevel@tonic-gate 	gettext("principal static attributes");
3027c478bd9Sstevel@tonic-gate static const char read_tcontents[] =
3037c478bd9Sstevel@tonic-gate 	gettext("tagged data contents");
3047c478bd9Sstevel@tonic-gate static const char read_ttypelen[] =
3057c478bd9Sstevel@tonic-gate 	gettext("tagged data type and length");
3067c478bd9Sstevel@tonic-gate static const char read_kcontents[] =
3077c478bd9Sstevel@tonic-gate 	gettext("key data contents");
3087c478bd9Sstevel@tonic-gate static const char read_ktypelen[] =
3097c478bd9Sstevel@tonic-gate 	gettext("key data type and length");
3107c478bd9Sstevel@tonic-gate static const char read_econtents[] =
3117c478bd9Sstevel@tonic-gate 	gettext("extra data contents");
3127c478bd9Sstevel@tonic-gate static const char k5beta_fmt_name[] =
3137c478bd9Sstevel@tonic-gate 	gettext("Kerberos version 5 old format");
3147c478bd9Sstevel@tonic-gate static const char standard_fmt_name[] =
3157c478bd9Sstevel@tonic-gate 	gettext("Kerberos version 5 format");
3167c478bd9Sstevel@tonic-gate static const char no_name_mem_fmt[] =
3177c478bd9Sstevel@tonic-gate 	gettext("%s: cannot get memory for temporary name\n");
3187c478bd9Sstevel@tonic-gate static const char ctx_err_fmt[] =
3197c478bd9Sstevel@tonic-gate 	gettext("%s: cannot initialize Kerberos context\n");
3207c478bd9Sstevel@tonic-gate static const char stdin_name[] =
3217c478bd9Sstevel@tonic-gate 	gettext("standard input");
3227c478bd9Sstevel@tonic-gate static const char remaster_err_fmt[] =
3237c478bd9Sstevel@tonic-gate 	gettext("while re-encoding keys for principal %s with new master key");
3247c478bd9Sstevel@tonic-gate static const char restfail_fmt[] =
3257c478bd9Sstevel@tonic-gate 	gettext("%s: %s restore failed\n");
3267c478bd9Sstevel@tonic-gate static const char close_err_fmt[] =
3277c478bd9Sstevel@tonic-gate 	gettext("%s: cannot close database (%s)\n");
3287c478bd9Sstevel@tonic-gate static const char dbinit_err_fmt[] =
3297c478bd9Sstevel@tonic-gate 	gettext("%s: cannot initialize database (%s)\n");
3307c478bd9Sstevel@tonic-gate static const char dblock_err_fmt[] =
3317c478bd9Sstevel@tonic-gate 	gettext("%s: cannot initialize database lock (%s)\n");
3327c478bd9Sstevel@tonic-gate static const char dbname_err_fmt[] =
3337c478bd9Sstevel@tonic-gate 	gettext("%s: cannot set database name to %s (%s)\n");
3347c478bd9Sstevel@tonic-gate static const char dbdelerr_fmt[] =
3357c478bd9Sstevel@tonic-gate 	gettext("%s: cannot delete bad database %s (%s)\n");
3367c478bd9Sstevel@tonic-gate static const char dbunlockerr_fmt[] =
3377c478bd9Sstevel@tonic-gate 	gettext("%s: cannot unlock database %s (%s)\n");
3387c478bd9Sstevel@tonic-gate static const char dbrenerr_fmt[] =
3397c478bd9Sstevel@tonic-gate 	gettext("%s: cannot rename database %s to %s (%s)\n");
3407c478bd9Sstevel@tonic-gate static const char dbcreaterr_fmt[] =
3417c478bd9Sstevel@tonic-gate 	gettext("%s: cannot create database %s (%s)\n");
3427c478bd9Sstevel@tonic-gate static const char dfile_err_fmt[] =
3437c478bd9Sstevel@tonic-gate 	gettext("%s: cannot open %s (%s)\n");
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate /*
3467c478bd9Sstevel@tonic-gate  * We now return you to your regularly scheduled program.
3477c478bd9Sstevel@tonic-gate  */
3487c478bd9Sstevel@tonic-gate #undef gettext
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate static const char oldoption[] = "-old";
3517c478bd9Sstevel@tonic-gate static const char b6option[] = "-b6";
35256a424ccSmp153739 static const char b7option[] = "-b7";
3537c478bd9Sstevel@tonic-gate static const char ipropoption[] = "-i";
3547c478bd9Sstevel@tonic-gate static const char verboseoption[] = "-verbose";
3557c478bd9Sstevel@tonic-gate static const char updateoption[] = "-update";
3567c478bd9Sstevel@tonic-gate static const char hashoption[] = "-hash";
3577c478bd9Sstevel@tonic-gate static const char ovoption[] = "-ov";
3587c478bd9Sstevel@tonic-gate static const char dump_tmptrail[] = "~";
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate /*
3617c478bd9Sstevel@tonic-gate  * Re-encrypt the key_data with the new master key...
3627c478bd9Sstevel@tonic-gate  */
master_key_convert(context,db_entry)36356a424ccSmp153739 static krb5_error_code master_key_convert(context, db_entry)
3647c478bd9Sstevel@tonic-gate     krb5_context	  context;
3657c478bd9Sstevel@tonic-gate     krb5_db_entry	* db_entry;
3667c478bd9Sstevel@tonic-gate {
3677c478bd9Sstevel@tonic-gate     krb5_error_code	retval;
3687c478bd9Sstevel@tonic-gate     krb5_keyblock	v5plainkey, *key_ptr;
3697c478bd9Sstevel@tonic-gate     krb5_keysalt	keysalt;
37056a424ccSmp153739     int	      i, j;
3717c478bd9Sstevel@tonic-gate     krb5_key_data	new_key_data, *key_data;
3727c478bd9Sstevel@tonic-gate     krb5_boolean	is_mkey;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate     is_mkey = krb5_principal_compare(context, master_princ, db_entry->princ);
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate     if (is_mkey && db_entry->n_key_data != 1)
3777c478bd9Sstevel@tonic-gate 	    fprintf(stderr,
3787c478bd9Sstevel@tonic-gate 		    gettext(
3797c478bd9Sstevel@tonic-gate 		      "Master key db entry has %d keys, expecting only 1!\n"),
3807c478bd9Sstevel@tonic-gate 		    db_entry->n_key_data);
3817c478bd9Sstevel@tonic-gate     for (i=0; i < db_entry->n_key_data; i++) {
3827c478bd9Sstevel@tonic-gate 	key_data = &db_entry->key_data[i];
3837c478bd9Sstevel@tonic-gate 	retval = krb5_dbekd_decrypt_key_data(context, &master_key,
3847c478bd9Sstevel@tonic-gate 					     key_data, &v5plainkey,
3857c478bd9Sstevel@tonic-gate 					     &keysalt);
3867c478bd9Sstevel@tonic-gate 	if (retval)
3877c478bd9Sstevel@tonic-gate 		return retval;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	memset(&new_key_data, 0, sizeof(new_key_data));
3907c478bd9Sstevel@tonic-gate 	key_ptr = is_mkey ? &new_master_key : &v5plainkey;
3917c478bd9Sstevel@tonic-gate 	retval = krb5_dbekd_encrypt_key_data(context, &new_master_key,
3927c478bd9Sstevel@tonic-gate 					     key_ptr, &keysalt,
3937c478bd9Sstevel@tonic-gate 					     key_data->key_data_kvno,
3947c478bd9Sstevel@tonic-gate 					     &new_key_data);
3957c478bd9Sstevel@tonic-gate 	if (retval)
3967c478bd9Sstevel@tonic-gate 		return retval;
3977c478bd9Sstevel@tonic-gate 	krb5_free_keyblock_contents(context, &v5plainkey);
39856a424ccSmp153739 	for (j = 0; j < key_data->key_data_ver; j++) {
39956a424ccSmp153739 	    if (key_data->key_data_length[j]) {
40056a424ccSmp153739 		free(key_data->key_data_contents[j]);
40156a424ccSmp153739 	    }
40256a424ccSmp153739 	}
4037c478bd9Sstevel@tonic-gate 	*key_data = new_key_data;
4047c478bd9Sstevel@tonic-gate     }
4057c478bd9Sstevel@tonic-gate     return 0;
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate /*
4097c478bd9Sstevel@tonic-gate  * Update the "ok" file.
4107c478bd9Sstevel@tonic-gate  */
update_ok_file(file_name)41156a424ccSmp153739 void update_ok_file (file_name)
4127c478bd9Sstevel@tonic-gate      char *file_name;
4137c478bd9Sstevel@tonic-gate {
4147c478bd9Sstevel@tonic-gate 	/* handle slave locking/failure stuff */
4157c478bd9Sstevel@tonic-gate 	char *file_ok;
4167c478bd9Sstevel@tonic-gate 	int fd;
4177c478bd9Sstevel@tonic-gate 	static char ok[]=".dump_ok";
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	if ((file_ok = (char *)malloc(strlen(file_name) + strlen(ok) + 1))
4207c478bd9Sstevel@tonic-gate 	    == NULL) {
4217c478bd9Sstevel@tonic-gate 		com_err(progname, ENOMEM,
4227c478bd9Sstevel@tonic-gate 		    gettext("while allocating filename "
4237c478bd9Sstevel@tonic-gate 			"for update_ok_file"));
4247c478bd9Sstevel@tonic-gate 		exit_status++;
4257c478bd9Sstevel@tonic-gate 		return;
4267c478bd9Sstevel@tonic-gate 	}
4277c478bd9Sstevel@tonic-gate 	strcpy(file_ok, file_name);
4287c478bd9Sstevel@tonic-gate 	strcat(file_ok, ok);
4297c478bd9Sstevel@tonic-gate 	if ((fd = open(file_ok, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0) {
4307c478bd9Sstevel@tonic-gate 		com_err(progname, errno,
4317c478bd9Sstevel@tonic-gate 		    gettext("while creating 'ok' file, '%s'"),
4327c478bd9Sstevel@tonic-gate 			file_ok);
4337c478bd9Sstevel@tonic-gate 		exit_status++;
4347c478bd9Sstevel@tonic-gate 		free(file_ok);
4357c478bd9Sstevel@tonic-gate 		return;
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate 	if (write(fd, "", 1) != 1) {
4387c478bd9Sstevel@tonic-gate 		com_err(progname, errno,
4397c478bd9Sstevel@tonic-gate 		    gettext("while writing to 'ok' file, '%s'"),
4407c478bd9Sstevel@tonic-gate 		    file_ok);
4417c478bd9Sstevel@tonic-gate 	     exit_status++;
4427c478bd9Sstevel@tonic-gate 	     free(file_ok);
4437c478bd9Sstevel@tonic-gate 	     return;
4447c478bd9Sstevel@tonic-gate 	}
44556a424ccSmp153739 
4467c478bd9Sstevel@tonic-gate 	free(file_ok);
4477c478bd9Sstevel@tonic-gate 	close(fd);
44856a424ccSmp153739 	return;
4497c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate /*
4527c478bd9Sstevel@tonic-gate  * name_matches()	- See if a principal name matches a regular expression
4537c478bd9Sstevel@tonic-gate  *			  or string.
4547c478bd9Sstevel@tonic-gate  */
4557c478bd9Sstevel@tonic-gate static int
name_matches(name,arglist)4567c478bd9Sstevel@tonic-gate name_matches(name, arglist)
4577c478bd9Sstevel@tonic-gate     char		*name;
4587c478bd9Sstevel@tonic-gate     struct dump_args	*arglist;
4597c478bd9Sstevel@tonic-gate {
4607c478bd9Sstevel@tonic-gate #if	HAVE_REGCOMP
4617c478bd9Sstevel@tonic-gate     regex_t	match_exp;
4627c478bd9Sstevel@tonic-gate     regmatch_t	match_match;
4637c478bd9Sstevel@tonic-gate     int		match_error;
4647c478bd9Sstevel@tonic-gate     char	match_errmsg[BUFSIZ];
4657c478bd9Sstevel@tonic-gate     size_t	errmsg_size;
4667c478bd9Sstevel@tonic-gate #elif	HAVE_REGEXP_H
4677c478bd9Sstevel@tonic-gate     char	regexp_buffer[RE_BUF_SIZE];
4687c478bd9Sstevel@tonic-gate #elif	HAVE_RE_COMP
4697c478bd9Sstevel@tonic-gate     extern char	*re_comp();
4707c478bd9Sstevel@tonic-gate     char	*re_result;
4717c478bd9Sstevel@tonic-gate #endif	/* HAVE_RE_COMP */
4727c478bd9Sstevel@tonic-gate     int		i, match;
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate     /*
47556a424ccSmp153739      * Plow, brute force, through the list of names/regular expressions.
4767c478bd9Sstevel@tonic-gate      */
4777c478bd9Sstevel@tonic-gate     match = (arglist->nnames) ? 0 : 1;
4787c478bd9Sstevel@tonic-gate     for (i=0; i<arglist->nnames; i++) {
4797c478bd9Sstevel@tonic-gate #if	HAVE_REGCOMP
4807c478bd9Sstevel@tonic-gate 	/*
4817c478bd9Sstevel@tonic-gate 	 * Compile the regular expression.
4827c478bd9Sstevel@tonic-gate 	 */
48356a424ccSmp153739 	match_error = regcomp(&match_exp, arglist->names[i], REG_EXTENDED);
48456a424ccSmp153739 	if (match_error) {
4857c478bd9Sstevel@tonic-gate 	    errmsg_size = regerror(match_error,
4867c478bd9Sstevel@tonic-gate 				   &match_exp,
4877c478bd9Sstevel@tonic-gate 				   match_errmsg,
4887c478bd9Sstevel@tonic-gate 				   sizeof(match_errmsg));
4897c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(regex_err),
4907c478bd9Sstevel@tonic-gate 			    arglist->programname, match_errmsg);
4917c478bd9Sstevel@tonic-gate 	    break;
4927c478bd9Sstevel@tonic-gate 	}
4937c478bd9Sstevel@tonic-gate 	/*
4947c478bd9Sstevel@tonic-gate 	 * See if we have a match.
4957c478bd9Sstevel@tonic-gate 	 */
49656a424ccSmp153739 	match_error = regexec(&match_exp, name, 1, &match_match, 0);
49756a424ccSmp153739 	if (match_error) {
4987c478bd9Sstevel@tonic-gate 	    if (match_error != REG_NOMATCH) {
4997c478bd9Sstevel@tonic-gate 		errmsg_size = regerror(match_error,
5007c478bd9Sstevel@tonic-gate 				       &match_exp,
5017c478bd9Sstevel@tonic-gate 				       match_errmsg,
5027c478bd9Sstevel@tonic-gate 				       sizeof(match_errmsg));
5037c478bd9Sstevel@tonic-gate 				fprintf(stderr, gettext(regex_merr),
5047c478bd9Sstevel@tonic-gate 			arglist->programname, match_errmsg);
5057c478bd9Sstevel@tonic-gate 		break;
5067c478bd9Sstevel@tonic-gate 	    }
50756a424ccSmp153739 	}
50856a424ccSmp153739 	else {
5097c478bd9Sstevel@tonic-gate 	    /*
5107c478bd9Sstevel@tonic-gate 	     * We have a match.  See if it matches the whole
5117c478bd9Sstevel@tonic-gate 	     * name.
5127c478bd9Sstevel@tonic-gate 	     */
5137c478bd9Sstevel@tonic-gate 	    if ((match_match.rm_so == 0) &&
5147c478bd9Sstevel@tonic-gate 		(match_match.rm_eo == strlen(name)))
5157c478bd9Sstevel@tonic-gate 		match = 1;
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 	regfree(&match_exp);
5187c478bd9Sstevel@tonic-gate #elif	HAVE_REGEXP_H
5197c478bd9Sstevel@tonic-gate 	/*
5207c478bd9Sstevel@tonic-gate 	 * Compile the regular expression.
5217c478bd9Sstevel@tonic-gate 	 */
5227c478bd9Sstevel@tonic-gate 	compile(arglist->names[i],
5237c478bd9Sstevel@tonic-gate 		regexp_buffer,
5247c478bd9Sstevel@tonic-gate 		&regexp_buffer[RE_BUF_SIZE],
5257c478bd9Sstevel@tonic-gate 		'\0');
5267c478bd9Sstevel@tonic-gate 	if (step(name, regexp_buffer)) {
5277c478bd9Sstevel@tonic-gate 	    if ((loc1 == name) &&
5287c478bd9Sstevel@tonic-gate 		(loc2 == &name[strlen(name)]))
5297c478bd9Sstevel@tonic-gate 		match = 1;
5307c478bd9Sstevel@tonic-gate 	}
5317c478bd9Sstevel@tonic-gate #elif	HAVE_RE_COMP
5327c478bd9Sstevel@tonic-gate 	/*
5337c478bd9Sstevel@tonic-gate 	 * Compile the regular expression.
5347c478bd9Sstevel@tonic-gate 	 */
5357c478bd9Sstevel@tonic-gate 	if (re_result = re_comp(arglist->names[i])) {
53656a424ccSmp153739 	    fprintf(stderr, gettext(regex_err), arglist->programname, re_result);
5377c478bd9Sstevel@tonic-gate 	    break;
5387c478bd9Sstevel@tonic-gate 	}
5397c478bd9Sstevel@tonic-gate 	if (re_exec(name))
5407c478bd9Sstevel@tonic-gate 	    match = 1;
5417c478bd9Sstevel@tonic-gate #else	/* HAVE_RE_COMP */
5427c478bd9Sstevel@tonic-gate 	/*
54356a424ccSmp153739 	 * If no regular expression support, then just compare the strings.
5447c478bd9Sstevel@tonic-gate 	 */
54556a424ccSmp153739 	if (!strcmp(arglist->names[i], name))
5467c478bd9Sstevel@tonic-gate 	    match = 1;
5477c478bd9Sstevel@tonic-gate #endif	/* HAVE_REGCOMP */
5487c478bd9Sstevel@tonic-gate 	if (match)
5497c478bd9Sstevel@tonic-gate 	    break;
5507c478bd9Sstevel@tonic-gate     }
5517c478bd9Sstevel@tonic-gate     return(match);
5527c478bd9Sstevel@tonic-gate }
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate static krb5_error_code
find_enctype(dbentp,enctype,salttype,kentp)5557c478bd9Sstevel@tonic-gate find_enctype(dbentp, enctype, salttype, kentp)
5567c478bd9Sstevel@tonic-gate     krb5_db_entry	*dbentp;
5577c478bd9Sstevel@tonic-gate     krb5_enctype	enctype;
5587c478bd9Sstevel@tonic-gate     krb5_int32		salttype;
5597c478bd9Sstevel@tonic-gate     krb5_key_data	**kentp;
5607c478bd9Sstevel@tonic-gate {
5617c478bd9Sstevel@tonic-gate     int			i;
5627c478bd9Sstevel@tonic-gate     int			maxkvno;
5637c478bd9Sstevel@tonic-gate     krb5_key_data	*datap;
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate     maxkvno = -1;
5667c478bd9Sstevel@tonic-gate     datap = (krb5_key_data *) NULL;
5677c478bd9Sstevel@tonic-gate     for (i=0; i<dbentp->n_key_data; i++) {
5687c478bd9Sstevel@tonic-gate 	if (( (krb5_enctype)dbentp->key_data[i].key_data_type[0] == enctype) &&
5697c478bd9Sstevel@tonic-gate 	    ((dbentp->key_data[i].key_data_type[1] == salttype) ||
5707c478bd9Sstevel@tonic-gate 	     (salttype < 0))) {
5717c478bd9Sstevel@tonic-gate 	    maxkvno = dbentp->key_data[i].key_data_kvno;
5727c478bd9Sstevel@tonic-gate 	    datap = &dbentp->key_data[i];
5737c478bd9Sstevel@tonic-gate 	}
5747c478bd9Sstevel@tonic-gate     }
5757c478bd9Sstevel@tonic-gate     if (maxkvno >= 0) {
5767c478bd9Sstevel@tonic-gate 	*kentp = datap;
5777c478bd9Sstevel@tonic-gate 	return(0);
5787c478bd9Sstevel@tonic-gate     }
5797c478bd9Sstevel@tonic-gate     return(ENOENT);
5807c478bd9Sstevel@tonic-gate }
5817c478bd9Sstevel@tonic-gate 
58256a424ccSmp153739 #if 0
5837c478bd9Sstevel@tonic-gate /*
5847c478bd9Sstevel@tonic-gate  * dump_k5beta_header()	- Make a dump header that is recognizable by Kerberos
5857c478bd9Sstevel@tonic-gate  *			  Version 5 Beta 5 and previous releases.
5867c478bd9Sstevel@tonic-gate  */
5877c478bd9Sstevel@tonic-gate static krb5_error_code
5887c478bd9Sstevel@tonic-gate dump_k5beta_header(arglist)
5897c478bd9Sstevel@tonic-gate     struct dump_args *arglist;
5907c478bd9Sstevel@tonic-gate {
5917c478bd9Sstevel@tonic-gate     /* The old header consists of the leading string */
5927c478bd9Sstevel@tonic-gate     fprintf(arglist->ofile, k5beta_dump_header);
5937c478bd9Sstevel@tonic-gate     return(0);
5947c478bd9Sstevel@tonic-gate }
59556a424ccSmp153739 #endif
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate /*
5987c478bd9Sstevel@tonic-gate  * dump_k5beta_iterator()	- Dump an entry in a format that is usable
5997c478bd9Sstevel@tonic-gate  *				  by Kerberos Version 5 Beta 5 and previous
6007c478bd9Sstevel@tonic-gate  *				  releases.
6017c478bd9Sstevel@tonic-gate  */
6027c478bd9Sstevel@tonic-gate static krb5_error_code
dump_k5beta_iterator(ptr,entry)6037c478bd9Sstevel@tonic-gate dump_k5beta_iterator(ptr, entry)
6047c478bd9Sstevel@tonic-gate     krb5_pointer	ptr;
6057c478bd9Sstevel@tonic-gate     krb5_db_entry	*entry;
6067c478bd9Sstevel@tonic-gate {
6077c478bd9Sstevel@tonic-gate     krb5_error_code	retval;
6087c478bd9Sstevel@tonic-gate     struct dump_args	*arg;
6097c478bd9Sstevel@tonic-gate     char		*name, *mod_name;
6107c478bd9Sstevel@tonic-gate     krb5_principal	mod_princ;
6117c478bd9Sstevel@tonic-gate     krb5_key_data	*pkey, *akey, nullkey;
6127c478bd9Sstevel@tonic-gate     krb5_timestamp	mod_date, last_pwd_change;
6137c478bd9Sstevel@tonic-gate     int			i;
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate     /* Initialize */
6167c478bd9Sstevel@tonic-gate     arg = (struct dump_args *) ptr;
6177c478bd9Sstevel@tonic-gate     name = (char *) NULL;
6187c478bd9Sstevel@tonic-gate     mod_name = (char *) NULL;
6197c478bd9Sstevel@tonic-gate     memset(&nullkey, 0, sizeof(nullkey));
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate     /*
6227c478bd9Sstevel@tonic-gate      * Flatten the principal name.
6237c478bd9Sstevel@tonic-gate      */
6247c478bd9Sstevel@tonic-gate     if ((retval = krb5_unparse_name(arg->kcontext,
6257c478bd9Sstevel@tonic-gate 				    entry->princ,
6267c478bd9Sstevel@tonic-gate 				    &name))) {
6277c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(pname_unp_err),
6287c478bd9Sstevel@tonic-gate 		arg->programname, error_message(retval));
6297c478bd9Sstevel@tonic-gate 	return(retval);
6307c478bd9Sstevel@tonic-gate     }
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate     /*
6337c478bd9Sstevel@tonic-gate      * Re-encode the keys in the new master key, if necessary.
6347c478bd9Sstevel@tonic-gate      */
6357c478bd9Sstevel@tonic-gate     if (mkey_convert) {
6367c478bd9Sstevel@tonic-gate 	retval = master_key_convert(arg->kcontext, entry);
6377c478bd9Sstevel@tonic-gate 	if (retval) {
6387c478bd9Sstevel@tonic-gate 	    com_err(arg->programname, retval, remaster_err_fmt, name);
6397c478bd9Sstevel@tonic-gate 	    return retval;
6407c478bd9Sstevel@tonic-gate 	}
6417c478bd9Sstevel@tonic-gate     }
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate     /*
6447c478bd9Sstevel@tonic-gate      * If we don't have any match strings, or if our name matches, then
6457c478bd9Sstevel@tonic-gate      * proceed with the dump, otherwise, just forget about it.
6467c478bd9Sstevel@tonic-gate      */
6477c478bd9Sstevel@tonic-gate     if (!arg->nnames || name_matches(name, arg)) {
6487c478bd9Sstevel@tonic-gate 	/*
6497c478bd9Sstevel@tonic-gate 	 * Deserialize the modifier record.
6507c478bd9Sstevel@tonic-gate 	 */
6517c478bd9Sstevel@tonic-gate 	mod_name = (char *) NULL;
6527c478bd9Sstevel@tonic-gate 	mod_princ = NULL;
6537c478bd9Sstevel@tonic-gate 	last_pwd_change = mod_date = 0;
6547c478bd9Sstevel@tonic-gate 	pkey = akey = (krb5_key_data *) NULL;
6557c478bd9Sstevel@tonic-gate 	if (!(retval = krb5_dbe_lookup_mod_princ_data(arg->kcontext,
6567c478bd9Sstevel@tonic-gate 						      entry,
6577c478bd9Sstevel@tonic-gate 						      &mod_date,
6587c478bd9Sstevel@tonic-gate 						      &mod_princ))) {
6597c478bd9Sstevel@tonic-gate 	    if (mod_princ) {
6607c478bd9Sstevel@tonic-gate 		/*
6617c478bd9Sstevel@tonic-gate 		 * Flatten the modifier name.
6627c478bd9Sstevel@tonic-gate 		 */
6637c478bd9Sstevel@tonic-gate 		if ((retval = krb5_unparse_name(arg->kcontext,
6647c478bd9Sstevel@tonic-gate 						mod_princ,
6657c478bd9Sstevel@tonic-gate 						&mod_name)))
6667c478bd9Sstevel@tonic-gate 					fprintf(stderr, gettext(mname_unp_err),
6677c478bd9Sstevel@tonic-gate 					    arg->programname,
6687c478bd9Sstevel@tonic-gate 			    error_message(retval));
6697c478bd9Sstevel@tonic-gate 		krb5_free_principal(arg->kcontext, mod_princ);
6707c478bd9Sstevel@tonic-gate 	    }
6717c478bd9Sstevel@tonic-gate 	}
6727c478bd9Sstevel@tonic-gate 	if (!mod_name)
6737c478bd9Sstevel@tonic-gate 	    mod_name = strdup(null_mprinc_name);
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	/*
67656a424ccSmp153739 	 * Find the last password change record and set it straight.
6777c478bd9Sstevel@tonic-gate 	 */
6787c478bd9Sstevel@tonic-gate 	if ((retval =
6797c478bd9Sstevel@tonic-gate 	     krb5_dbe_lookup_last_pwd_change(arg->kcontext, entry,
6807c478bd9Sstevel@tonic-gate 					     &last_pwd_change))) {
6817c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(nokeys_err),
6827c478bd9Sstevel@tonic-gate 			    arg->programname, name);
6837c478bd9Sstevel@tonic-gate 	    krb5_xfree(mod_name);
6847c478bd9Sstevel@tonic-gate 	    krb5_xfree(name);
6857c478bd9Sstevel@tonic-gate 	    return(retval);
6867c478bd9Sstevel@tonic-gate 	}
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	/*
6897c478bd9Sstevel@tonic-gate 	 * Find the 'primary' key and the 'alternate' key.
6907c478bd9Sstevel@tonic-gate 	 */
6917c478bd9Sstevel@tonic-gate 	if ((retval = find_enctype(entry,
6927c478bd9Sstevel@tonic-gate 				   ENCTYPE_DES_CBC_CRC,
6937c478bd9Sstevel@tonic-gate 				   KRB5_KDB_SALTTYPE_NORMAL,
6947c478bd9Sstevel@tonic-gate 				   &pkey)) &&
6957c478bd9Sstevel@tonic-gate 	    (retval = find_enctype(entry,
6967c478bd9Sstevel@tonic-gate 				   ENCTYPE_DES_CBC_CRC,
6977c478bd9Sstevel@tonic-gate 				   KRB5_KDB_SALTTYPE_V4,
6987c478bd9Sstevel@tonic-gate 				   &akey))) {
6997c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(nokeys_err),
7007c478bd9Sstevel@tonic-gate 			    arg->programname, name);
7017c478bd9Sstevel@tonic-gate 	    krb5_xfree(mod_name);
7027c478bd9Sstevel@tonic-gate 	    krb5_xfree(name);
7037c478bd9Sstevel@tonic-gate 	    return(retval);
7047c478bd9Sstevel@tonic-gate 	}
70556a424ccSmp153739 
70656a424ccSmp153739 	/* If we only have one type, then ship it out as the primary. */
7077c478bd9Sstevel@tonic-gate 	if (!pkey && akey) {
7087c478bd9Sstevel@tonic-gate 	    pkey = akey;
7097c478bd9Sstevel@tonic-gate 	    akey = &nullkey;
71056a424ccSmp153739 	}
71156a424ccSmp153739 	else {
7127c478bd9Sstevel@tonic-gate 	    if (!akey)
7137c478bd9Sstevel@tonic-gate 		akey = &nullkey;
7147c478bd9Sstevel@tonic-gate 	}
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	/*
71756a424ccSmp153739 	 * First put out strings representing the length of the variable
71856a424ccSmp153739 	 * length data in this record, then the name and the primary key type.
7197c478bd9Sstevel@tonic-gate 	 */
72056a424ccSmp153739 	fprintf(arg->ofile, "%d\t%d\t%d\t%d\t%d\t%d\t%s\t%d\t", strlen(name),
7217c478bd9Sstevel@tonic-gate 		strlen(mod_name),
7227c478bd9Sstevel@tonic-gate 		(krb5_int32) pkey->key_data_length[0],
7237c478bd9Sstevel@tonic-gate 		(krb5_int32) akey->key_data_length[0],
7247c478bd9Sstevel@tonic-gate 		(krb5_int32) pkey->key_data_length[1],
7257c478bd9Sstevel@tonic-gate 		(krb5_int32) akey->key_data_length[1],
7267c478bd9Sstevel@tonic-gate 		name,
7277c478bd9Sstevel@tonic-gate 		(krb5_int32) pkey->key_data_type[0]);
7287c478bd9Sstevel@tonic-gate 	for (i=0; i<pkey->key_data_length[0]; i++) {
72956a424ccSmp153739 	    fprintf(arg->ofile, "%02x", pkey->key_data_contents[0][i]);
7307c478bd9Sstevel@tonic-gate 	}
7317c478bd9Sstevel@tonic-gate 	/*
73256a424ccSmp153739 	 * Second, print out strings representing the standard integer
73356a424ccSmp153739 	 * data in this record.
7347c478bd9Sstevel@tonic-gate 	 */
7357c478bd9Sstevel@tonic-gate 	fprintf(arg->ofile,
73656a424ccSmp153739 		"\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%s\t%u\t%u\t%u\t",
7377c478bd9Sstevel@tonic-gate 		(krb5_int32) pkey->key_data_kvno,
7387c478bd9Sstevel@tonic-gate 		entry->max_life, entry->max_renewable_life,
73956a424ccSmp153739 		1 /* Fake mkvno */, entry->expiration, entry->pw_expiration,
74056a424ccSmp153739 		last_pwd_change, entry->last_success, entry->last_failed,
7417c478bd9Sstevel@tonic-gate 		entry->fail_auth_count, mod_name, mod_date,
7427c478bd9Sstevel@tonic-gate 		entry->attributes, pkey->key_data_type[1]);
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	/* Pound out the salt data, if present. */
7457c478bd9Sstevel@tonic-gate 	for (i=0; i<pkey->key_data_length[1]; i++) {
74656a424ccSmp153739 	    fprintf(arg->ofile, "%02x", pkey->key_data_contents[1][i]);
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 	/* Pound out the alternate key type and contents */
7497c478bd9Sstevel@tonic-gate 	fprintf(arg->ofile, "\t%u\t", akey->key_data_type[0]);
7507c478bd9Sstevel@tonic-gate 	for (i=0; i<akey->key_data_length[0]; i++) {
75156a424ccSmp153739 	    fprintf(arg->ofile, "%02x", akey->key_data_contents[0][i]);
7527c478bd9Sstevel@tonic-gate 	}
7537c478bd9Sstevel@tonic-gate 	/* Pound out the alternate salt type and contents */
7547c478bd9Sstevel@tonic-gate 	fprintf(arg->ofile, "\t%u\t", akey->key_data_type[1]);
7557c478bd9Sstevel@tonic-gate 	for (i=0; i<akey->key_data_length[1]; i++) {
75656a424ccSmp153739 	    fprintf(arg->ofile, "%02x", akey->key_data_contents[1][i]);
7577c478bd9Sstevel@tonic-gate 	}
7587c478bd9Sstevel@tonic-gate 	/* Pound out the expansion data. (is null) */
7597c478bd9Sstevel@tonic-gate 	for (i=0; i < 8; i++) {
7607c478bd9Sstevel@tonic-gate 	    fprintf(arg->ofile, "\t%u", 0);
7617c478bd9Sstevel@tonic-gate 	}
7627c478bd9Sstevel@tonic-gate 	fprintf(arg->ofile, ";\n");
7637c478bd9Sstevel@tonic-gate 	/* If we're blabbing, do it */
7647c478bd9Sstevel@tonic-gate 	if (arg->verbose)
7657c478bd9Sstevel@tonic-gate 	    fprintf(stderr, "%s\n", name);
7667c478bd9Sstevel@tonic-gate 	krb5_xfree(mod_name);
7677c478bd9Sstevel@tonic-gate     }
7687c478bd9Sstevel@tonic-gate     krb5_xfree(name);
7697c478bd9Sstevel@tonic-gate     return(0);
7707c478bd9Sstevel@tonic-gate }
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate /*
7737c478bd9Sstevel@tonic-gate  * dump_k5beta6_iterator()	- Output a dump record in krb5b6 format.
7747c478bd9Sstevel@tonic-gate  */
7757c478bd9Sstevel@tonic-gate static krb5_error_code
dump_k5beta6_iterator(ptr,entry)7767c478bd9Sstevel@tonic-gate dump_k5beta6_iterator(ptr, entry)
7777c478bd9Sstevel@tonic-gate     krb5_pointer	ptr;
7787c478bd9Sstevel@tonic-gate     krb5_db_entry	*entry;
7797c478bd9Sstevel@tonic-gate {
78056a424ccSmp153739     return dump_k5beta6_iterator_ext(ptr, entry, 0);
78156a424ccSmp153739 }
78256a424ccSmp153739 
78356a424ccSmp153739 static krb5_error_code
dump_k5beta6_iterator_ext(ptr,entry,kadm)78456a424ccSmp153739 dump_k5beta6_iterator_ext(ptr, entry, kadm)
78556a424ccSmp153739     krb5_pointer	ptr;
78656a424ccSmp153739     krb5_db_entry	*entry;
78756a424ccSmp153739     int			kadm;
78856a424ccSmp153739 {
7897c478bd9Sstevel@tonic-gate     krb5_error_code	retval;
7907c478bd9Sstevel@tonic-gate     struct dump_args	*arg;
7917c478bd9Sstevel@tonic-gate     char		*name;
7927c478bd9Sstevel@tonic-gate     krb5_tl_data	*tlp;
7937c478bd9Sstevel@tonic-gate     krb5_key_data	*kdata;
7947c478bd9Sstevel@tonic-gate     int			counter, skip, i, j;
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate     /* Initialize */
7977c478bd9Sstevel@tonic-gate     arg = (struct dump_args *) ptr;
7987c478bd9Sstevel@tonic-gate     name = (char *) NULL;
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate     /*
8017c478bd9Sstevel@tonic-gate      * Flatten the principal name.
8027c478bd9Sstevel@tonic-gate      */
8037c478bd9Sstevel@tonic-gate     if ((retval = krb5_unparse_name(arg->kcontext,
8047c478bd9Sstevel@tonic-gate 				    entry->princ,
8057c478bd9Sstevel@tonic-gate 				    &name))) {
8067c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(pname_unp_err),
8077c478bd9Sstevel@tonic-gate 		arg->programname, error_message(retval));
8087c478bd9Sstevel@tonic-gate 	return(retval);
8097c478bd9Sstevel@tonic-gate     }
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate     /*
8127c478bd9Sstevel@tonic-gate      * Re-encode the keys in the new master key, if necessary.
8137c478bd9Sstevel@tonic-gate      */
8147c478bd9Sstevel@tonic-gate     if (mkey_convert) {
8157c478bd9Sstevel@tonic-gate 	retval = master_key_convert(arg->kcontext, entry);
8167c478bd9Sstevel@tonic-gate 	if (retval) {
8177c478bd9Sstevel@tonic-gate 	    com_err(arg->programname, retval, remaster_err_fmt, name);
8187c478bd9Sstevel@tonic-gate 	    return retval;
8197c478bd9Sstevel@tonic-gate 	}
8207c478bd9Sstevel@tonic-gate     }
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate     /*
8237c478bd9Sstevel@tonic-gate      * If we don't have any match strings, or if our name matches, then
8247c478bd9Sstevel@tonic-gate      * proceed with the dump, otherwise, just forget about it.
8257c478bd9Sstevel@tonic-gate      */
8267c478bd9Sstevel@tonic-gate     if (!arg->nnames || name_matches(name, arg)) {
8277c478bd9Sstevel@tonic-gate 	/*
82856a424ccSmp153739 	 * We'd like to just blast out the contents as they would appear in
82956a424ccSmp153739 	 * the database so that we can just suck it back in, but it doesn't
83056a424ccSmp153739 	 * lend itself to easy editing.
8317c478bd9Sstevel@tonic-gate 	 */
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 	/*
83456a424ccSmp153739 	 * The dump format is as follows:
83556a424ccSmp153739 	 *	len strlen(name) n_tl_data n_key_data e_length
83656a424ccSmp153739 	 *	name
83756a424ccSmp153739 	 *	attributes max_life max_renewable_life expiration
83856a424ccSmp153739 	 *	pw_expiration last_success last_failed fail_auth_count
83956a424ccSmp153739 	 *	n_tl_data*[type length <contents>]
84056a424ccSmp153739 	 *	n_key_data*[ver kvno ver*(type length <contents>)]
84156a424ccSmp153739 	 *	<e_data>
84256a424ccSmp153739 	 * Fields which are not encapsulated by angle-brackets are to appear
84356a424ccSmp153739 	 * verbatim.  A bracketed field's absence is indicated by a -1 in its
84456a424ccSmp153739 	 * place
8457c478bd9Sstevel@tonic-gate 	 */
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	/*
8487c478bd9Sstevel@tonic-gate 	 * Make sure that the tagged list is reasonably correct.
8497c478bd9Sstevel@tonic-gate 	 */
8507c478bd9Sstevel@tonic-gate 	counter = skip = 0;
8517c478bd9Sstevel@tonic-gate 	for (tlp = entry->tl_data; tlp; tlp = tlp->tl_data_next) {
8527c478bd9Sstevel@tonic-gate 	     /*
85356a424ccSmp153739 	      * don't dump tl data types we know aren't understood by
85456a424ccSmp153739 	      * earlier revisions [krb5-admin/89]
8557c478bd9Sstevel@tonic-gate 	      */
8567c478bd9Sstevel@tonic-gate 	     switch (tlp->tl_data_type) {
8577c478bd9Sstevel@tonic-gate 	     case KRB5_TL_KADM_DATA:
85856a424ccSmp153739 		  if (kadm)
85956a424ccSmp153739 		      counter++;
86056a424ccSmp153739 		  else
8617c478bd9Sstevel@tonic-gate 		      skip++;
8627c478bd9Sstevel@tonic-gate 		  break;
8637c478bd9Sstevel@tonic-gate 	     default:
8647c478bd9Sstevel@tonic-gate 		  counter++;
8657c478bd9Sstevel@tonic-gate 		  break;
8667c478bd9Sstevel@tonic-gate 	     }
8677c478bd9Sstevel@tonic-gate 	}
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 	if (counter + skip == entry->n_tl_data) {
8707c478bd9Sstevel@tonic-gate 	    /* Pound out header */
8717c478bd9Sstevel@tonic-gate 	    fprintf(arg->ofile, "%d\t%d\t%d\t%d\t%d\t%s\t",
8727c478bd9Sstevel@tonic-gate 		    (int) entry->len,
8737c478bd9Sstevel@tonic-gate 		    strlen(name),
8747c478bd9Sstevel@tonic-gate 		    counter,
8757c478bd9Sstevel@tonic-gate 		    (int) entry->n_key_data,
8767c478bd9Sstevel@tonic-gate 		    (int) entry->e_length,
8777c478bd9Sstevel@tonic-gate 		    name);
8787c478bd9Sstevel@tonic-gate 	    fprintf(arg->ofile, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t",
8797c478bd9Sstevel@tonic-gate 		    entry->attributes,
8807c478bd9Sstevel@tonic-gate 		    entry->max_life,
8817c478bd9Sstevel@tonic-gate 		    entry->max_renewable_life,
8827c478bd9Sstevel@tonic-gate 		    entry->expiration,
8837c478bd9Sstevel@tonic-gate 		    entry->pw_expiration,
8847c478bd9Sstevel@tonic-gate 		    entry->last_success,
8857c478bd9Sstevel@tonic-gate 		    entry->last_failed,
8867c478bd9Sstevel@tonic-gate 		    entry->fail_auth_count);
8877c478bd9Sstevel@tonic-gate 	    /* Pound out tagged data. */
88856a424ccSmp153739 	    for (tlp = entry->tl_data; tlp; tlp = tlp->tl_data_next) {
88956a424ccSmp153739 		if (tlp->tl_data_type == KRB5_TL_KADM_DATA && !kadm)
89056a424ccSmp153739 		     continue; /* see above, [krb5-admin/89] */
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 		fprintf(arg->ofile, "%d\t%d\t",
8937c478bd9Sstevel@tonic-gate 			(int) tlp->tl_data_type,
8947c478bd9Sstevel@tonic-gate 			(int) tlp->tl_data_length);
8957c478bd9Sstevel@tonic-gate 		if (tlp->tl_data_length)
89656a424ccSmp153739 		    for (i=0; i<tlp->tl_data_length; i++)
89756a424ccSmp153739 			fprintf(arg->ofile, "%02x", tlp->tl_data_contents[i]);
8987c478bd9Sstevel@tonic-gate 		else
8997c478bd9Sstevel@tonic-gate 		    fprintf(arg->ofile, "%d", -1);
9007c478bd9Sstevel@tonic-gate 		fprintf(arg->ofile, "\t");
9017c478bd9Sstevel@tonic-gate 	    }
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	    /* Pound out key data */
90456a424ccSmp153739 	    for (counter=0; counter<entry->n_key_data; counter++) {
9057c478bd9Sstevel@tonic-gate 		kdata = &entry->key_data[counter];
9067c478bd9Sstevel@tonic-gate 		fprintf(arg->ofile, "%d\t%d\t",
9077c478bd9Sstevel@tonic-gate 			(int) kdata->key_data_ver,
9087c478bd9Sstevel@tonic-gate 			(int) kdata->key_data_kvno);
9097c478bd9Sstevel@tonic-gate 		for (i=0; i<kdata->key_data_ver; i++) {
9107c478bd9Sstevel@tonic-gate 		    fprintf(arg->ofile, "%d\t%d\t",
9117c478bd9Sstevel@tonic-gate 			    kdata->key_data_type[i],
9127c478bd9Sstevel@tonic-gate 			    kdata->key_data_length[i]);
9137c478bd9Sstevel@tonic-gate 		    if (kdata->key_data_length[i])
91456a424ccSmp153739 			for (j=0; j<kdata->key_data_length[i]; j++)
91556a424ccSmp153739 			    fprintf(arg->ofile, "%02x",
91656a424ccSmp153739 				    kdata->key_data_contents[i][j]);
9177c478bd9Sstevel@tonic-gate 		    else
9187c478bd9Sstevel@tonic-gate 			fprintf(arg->ofile, "%d", -1);
9197c478bd9Sstevel@tonic-gate 		    fprintf(arg->ofile, "\t");
9207c478bd9Sstevel@tonic-gate 		}
9217c478bd9Sstevel@tonic-gate 	    }
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	    /* Pound out extra data */
9247c478bd9Sstevel@tonic-gate 	    if (entry->e_length)
9257c478bd9Sstevel@tonic-gate 		for (i=0; i<entry->e_length; i++)
92656a424ccSmp153739 		    fprintf(arg->ofile, "%02x", entry->e_data[i]);
9277c478bd9Sstevel@tonic-gate 	    else
9287c478bd9Sstevel@tonic-gate 		fprintf(arg->ofile, "%d", -1);
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	    /* Print trailer */
9317c478bd9Sstevel@tonic-gate 	    fprintf(arg->ofile, ";\n");
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 	    if (arg->verbose)
9347c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s\n", name);
93556a424ccSmp153739 	}
93656a424ccSmp153739 	else {
9377c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(sdump_tl_inc_err),
9387c478bd9Sstevel@tonic-gate 		    arg->programname, name, counter+skip,
9397c478bd9Sstevel@tonic-gate 		    (int) entry->n_tl_data);
9407c478bd9Sstevel@tonic-gate 	    retval = EINVAL;
9417c478bd9Sstevel@tonic-gate 	}
9427c478bd9Sstevel@tonic-gate     }
9437c478bd9Sstevel@tonic-gate     krb5_xfree(name);
9447c478bd9Sstevel@tonic-gate     return(retval);
9457c478bd9Sstevel@tonic-gate }
94656a424ccSmp153739 
9477c478bd9Sstevel@tonic-gate /*
9487c478bd9Sstevel@tonic-gate  * dump_iprop_iterator()	- Output a dump record in iprop format.
9497c478bd9Sstevel@tonic-gate  */
9507c478bd9Sstevel@tonic-gate static krb5_error_code
dump_iprop_iterator(ptr,entry)9517c478bd9Sstevel@tonic-gate dump_iprop_iterator(ptr, entry)
9527c478bd9Sstevel@tonic-gate     krb5_pointer	ptr;
9537c478bd9Sstevel@tonic-gate     krb5_db_entry	*entry;
9547c478bd9Sstevel@tonic-gate {
9557c478bd9Sstevel@tonic-gate     krb5_error_code	retval;
9567c478bd9Sstevel@tonic-gate     struct dump_args	*arg;
9577c478bd9Sstevel@tonic-gate     char		*name;
9587c478bd9Sstevel@tonic-gate     krb5_tl_data	*tlp;
9597c478bd9Sstevel@tonic-gate     krb5_key_data	*kdata;
9607c478bd9Sstevel@tonic-gate     int			counter, i, j;
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate     /* Initialize */
9637c478bd9Sstevel@tonic-gate     arg = (struct dump_args *) ptr;
9647c478bd9Sstevel@tonic-gate     name = (char *) NULL;
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate     /*
9677c478bd9Sstevel@tonic-gate      * Flatten the principal name.
9687c478bd9Sstevel@tonic-gate      */
9697c478bd9Sstevel@tonic-gate     if ((retval = krb5_unparse_name(arg->kcontext,
9707c478bd9Sstevel@tonic-gate 				    entry->princ,
9717c478bd9Sstevel@tonic-gate 				    &name))) {
9727c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(pname_unp_err),
9737c478bd9Sstevel@tonic-gate 		arg->programname, error_message(retval));
9747c478bd9Sstevel@tonic-gate 	return(retval);
9757c478bd9Sstevel@tonic-gate     }
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate     /*
9787c478bd9Sstevel@tonic-gate      * Re-encode the keys in the new master key, if necessary.
9797c478bd9Sstevel@tonic-gate      */
9807c478bd9Sstevel@tonic-gate     if (mkey_convert) {
9817c478bd9Sstevel@tonic-gate 	retval = master_key_convert(arg->kcontext, entry);
9827c478bd9Sstevel@tonic-gate 	if (retval) {
9837c478bd9Sstevel@tonic-gate 	    com_err(arg->programname, retval, remaster_err_fmt, name);
9847c478bd9Sstevel@tonic-gate 	    return retval;
9857c478bd9Sstevel@tonic-gate 	}
9867c478bd9Sstevel@tonic-gate     }
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate     /*
9897c478bd9Sstevel@tonic-gate      * If we don't have any match strings, or if our name matches, then
9907c478bd9Sstevel@tonic-gate      * proceed with the dump, otherwise, just forget about it.
9917c478bd9Sstevel@tonic-gate      */
9927c478bd9Sstevel@tonic-gate     if (!arg->nnames || name_matches(name, arg)) {
9937c478bd9Sstevel@tonic-gate 	/*
9947c478bd9Sstevel@tonic-gate 	 * We'd like to just blast out the contents as they would
9957c478bd9Sstevel@tonic-gate 	 * appear in the database so that we can just suck it back
9967c478bd9Sstevel@tonic-gate 	 * in, but it doesn't lend itself to easy editing.
9977c478bd9Sstevel@tonic-gate 	 */
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 	/*
10007c478bd9Sstevel@tonic-gate 	 * The dump format is as follows: len strlen(name)
10017c478bd9Sstevel@tonic-gate 	 * n_tl_data n_key_data e_length name attributes max_life
10027c478bd9Sstevel@tonic-gate 	 * max_renewable_life expiration pw_expiration last_success
10037c478bd9Sstevel@tonic-gate 	 * last_failed fail_auth_count n_tl_data*[type length
10047c478bd9Sstevel@tonic-gate 	 * <contents>] n_key_data*[ver kvno ver*(type length
10057c478bd9Sstevel@tonic-gate 	 * <contents>)] <e_data> Fields which are not encapsulated
10067c478bd9Sstevel@tonic-gate 	 * by angle-brackets are to appear verbatim.  Bracketed
10077c478bd9Sstevel@tonic-gate 	 * fields absence is indicated by a -1 in its place
10087c478bd9Sstevel@tonic-gate 	 */
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	/*
10117c478bd9Sstevel@tonic-gate 	 * Make sure that the tagged list is reasonably correct.
10127c478bd9Sstevel@tonic-gate 	 */
10137c478bd9Sstevel@tonic-gate 	counter = 0;
10147c478bd9Sstevel@tonic-gate 	for (tlp = entry->tl_data; tlp; tlp = tlp->tl_data_next)
10157c478bd9Sstevel@tonic-gate 		  counter++;
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 	if (counter == entry->n_tl_data) {
10187c478bd9Sstevel@tonic-gate 	    /* Pound out header */
10197c478bd9Sstevel@tonic-gate 	    fprintf(arg->ofile, "%d\t%d\t%d\t%d\t%d\t%s\t",
10207c478bd9Sstevel@tonic-gate 		    (int) entry->len,
10217c478bd9Sstevel@tonic-gate 		    strlen(name),
10227c478bd9Sstevel@tonic-gate 		    (int) entry->n_tl_data,
10237c478bd9Sstevel@tonic-gate 		    (int) entry->n_key_data,
10247c478bd9Sstevel@tonic-gate 		    (int) entry->e_length,
10257c478bd9Sstevel@tonic-gate 		    name);
10267c478bd9Sstevel@tonic-gate 	    fprintf(arg->ofile, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t",
10277c478bd9Sstevel@tonic-gate 		    entry->attributes,
10287c478bd9Sstevel@tonic-gate 		    entry->max_life,
10297c478bd9Sstevel@tonic-gate 		    entry->max_renewable_life,
10307c478bd9Sstevel@tonic-gate 		    entry->expiration,
10317c478bd9Sstevel@tonic-gate 		    entry->pw_expiration,
10327c478bd9Sstevel@tonic-gate 		    entry->last_success,
10337c478bd9Sstevel@tonic-gate 		    entry->last_failed,
10347c478bd9Sstevel@tonic-gate 		    entry->fail_auth_count);
10357c478bd9Sstevel@tonic-gate 	    /* Pound out tagged data. */
10367c478bd9Sstevel@tonic-gate 			for (tlp = entry->tl_data; tlp;
10377c478bd9Sstevel@tonic-gate 			    tlp = tlp->tl_data_next) {
10387c478bd9Sstevel@tonic-gate 		fprintf(arg->ofile, "%d\t%d\t",
10397c478bd9Sstevel@tonic-gate 			(int) tlp->tl_data_type,
10407c478bd9Sstevel@tonic-gate 			(int) tlp->tl_data_length);
10417c478bd9Sstevel@tonic-gate 		if (tlp->tl_data_length)
10427c478bd9Sstevel@tonic-gate 					for (i = 0;
10437c478bd9Sstevel@tonic-gate 					    i < tlp->tl_data_length;
10447c478bd9Sstevel@tonic-gate 					    i++)
10457c478bd9Sstevel@tonic-gate 						fprintf(arg->ofile, "%02x",
10467c478bd9Sstevel@tonic-gate 							tlp->
10477c478bd9Sstevel@tonic-gate 							tl_data_contents[i]);
10487c478bd9Sstevel@tonic-gate 		else
10497c478bd9Sstevel@tonic-gate 		    fprintf(arg->ofile, "%d", -1);
10507c478bd9Sstevel@tonic-gate 		fprintf(arg->ofile, "\t");
10517c478bd9Sstevel@tonic-gate 	    }
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	    /* Pound out key data */
10547c478bd9Sstevel@tonic-gate 			for (counter = 0;
10557c478bd9Sstevel@tonic-gate 			    counter < entry->n_key_data; counter++) {
10567c478bd9Sstevel@tonic-gate 		kdata = &entry->key_data[counter];
10577c478bd9Sstevel@tonic-gate 		fprintf(arg->ofile, "%d\t%d\t",
10587c478bd9Sstevel@tonic-gate 			(int) kdata->key_data_ver,
10597c478bd9Sstevel@tonic-gate 			(int) kdata->key_data_kvno);
10607c478bd9Sstevel@tonic-gate 		for (i=0; i<kdata->key_data_ver; i++) {
10617c478bd9Sstevel@tonic-gate 		    fprintf(arg->ofile, "%d\t%d\t",
10627c478bd9Sstevel@tonic-gate 			    kdata->key_data_type[i],
10637c478bd9Sstevel@tonic-gate 			    kdata->key_data_length[i]);
10647c478bd9Sstevel@tonic-gate 		    if (kdata->key_data_length[i])
10657c478bd9Sstevel@tonic-gate 						for (j = 0;
10667c478bd9Sstevel@tonic-gate 						    j < kdata->
10677c478bd9Sstevel@tonic-gate 							key_data_length[i];
10687c478bd9Sstevel@tonic-gate 						    j++)
10697c478bd9Sstevel@tonic-gate 							fprintf(arg->ofile,
10707c478bd9Sstevel@tonic-gate 							    "%02x",
10717c478bd9Sstevel@tonic-gate 							    kdata->
10727c478bd9Sstevel@tonic-gate 							    key_data_contents
10737c478bd9Sstevel@tonic-gate 								[i][j]);
10747c478bd9Sstevel@tonic-gate 		    else
10757c478bd9Sstevel@tonic-gate 			fprintf(arg->ofile, "%d", -1);
10767c478bd9Sstevel@tonic-gate 		    fprintf(arg->ofile, "\t");
10777c478bd9Sstevel@tonic-gate 		}
10787c478bd9Sstevel@tonic-gate 	    }
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	    /* Pound out extra data */
10817c478bd9Sstevel@tonic-gate 	    if (entry->e_length)
10827c478bd9Sstevel@tonic-gate 		for (i=0; i<entry->e_length; i++)
10837c478bd9Sstevel@tonic-gate 					fprintf(arg->ofile, "%02x",
10847c478bd9Sstevel@tonic-gate 						entry->e_data[i]);
10857c478bd9Sstevel@tonic-gate 	    else
10867c478bd9Sstevel@tonic-gate 		fprintf(arg->ofile, "%d", -1);
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 	    /* Print trailer */
10897c478bd9Sstevel@tonic-gate 	    fprintf(arg->ofile, ";\n");
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 	    if (arg->verbose)
10927c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s\n", name);
10937c478bd9Sstevel@tonic-gate 		} else {
10947c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(sdump_tl_inc_err),
10957c478bd9Sstevel@tonic-gate 		    arg->programname, name, counter,
10967c478bd9Sstevel@tonic-gate 		    (int) entry->n_tl_data);
10977c478bd9Sstevel@tonic-gate 	    retval = EINVAL;
10987c478bd9Sstevel@tonic-gate 	}
10997c478bd9Sstevel@tonic-gate     }
11007c478bd9Sstevel@tonic-gate     krb5_xfree(name);
11017c478bd9Sstevel@tonic-gate     return(retval);
11027c478bd9Sstevel@tonic-gate }
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate /*
11057c478bd9Sstevel@tonic-gate  * dump_k5beta7_iterator()	- Output a dump record in krb5b7 format.
11067c478bd9Sstevel@tonic-gate  */
11077c478bd9Sstevel@tonic-gate static krb5_error_code
dump_k5beta7_princ(ptr,entry)11087c478bd9Sstevel@tonic-gate dump_k5beta7_princ(ptr, entry)
11097c478bd9Sstevel@tonic-gate     krb5_pointer	ptr;
11107c478bd9Sstevel@tonic-gate     krb5_db_entry	*entry;
11117c478bd9Sstevel@tonic-gate {
111256a424ccSmp153739     return dump_k5beta7_princ_ext(ptr, entry, 0);
111356a424ccSmp153739 }
111456a424ccSmp153739 
111556a424ccSmp153739 static krb5_error_code
dump_k5beta7_princ_ext(ptr,entry,kadm)111656a424ccSmp153739 dump_k5beta7_princ_ext(ptr, entry, kadm)
111756a424ccSmp153739     krb5_pointer	ptr;
111856a424ccSmp153739     krb5_db_entry	*entry;
111956a424ccSmp153739     int			kadm;
112056a424ccSmp153739 {
11217c478bd9Sstevel@tonic-gate      krb5_error_code retval;
11227c478bd9Sstevel@tonic-gate      struct dump_args *arg;
11237c478bd9Sstevel@tonic-gate      char *name;
11247c478bd9Sstevel@tonic-gate      int tmp_nnames;
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate      /* Initialize */
11277c478bd9Sstevel@tonic-gate      arg = (struct dump_args *) ptr;
11287c478bd9Sstevel@tonic-gate      name = (char *) NULL;
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate      /*
11317c478bd9Sstevel@tonic-gate       * Flatten the principal name.
11327c478bd9Sstevel@tonic-gate       */
11337c478bd9Sstevel@tonic-gate      if ((retval = krb5_unparse_name(arg->kcontext,
11347c478bd9Sstevel@tonic-gate 				     entry->princ,
11357c478bd9Sstevel@tonic-gate 				     &name))) {
11367c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(pname_unp_err),
11377c478bd9Sstevel@tonic-gate 		  arg->programname, error_message(retval));
11387c478bd9Sstevel@tonic-gate 	  return(retval);
11397c478bd9Sstevel@tonic-gate      }
11407c478bd9Sstevel@tonic-gate      /*
11417c478bd9Sstevel@tonic-gate       * If we don't have any match strings, or if our name matches, then
11427c478bd9Sstevel@tonic-gate       * proceed with the dump, otherwise, just forget about it.
11437c478bd9Sstevel@tonic-gate       */
11447c478bd9Sstevel@tonic-gate      if (!arg->nnames || name_matches(name, arg)) {
11457c478bd9Sstevel@tonic-gate 	  fprintf(arg->ofile, "princ\t");
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 	  /* save the callee from matching the name again */
11487c478bd9Sstevel@tonic-gate 	  tmp_nnames = arg->nnames;
11497c478bd9Sstevel@tonic-gate 	  arg->nnames = 0;
115056a424ccSmp153739 	  retval = dump_k5beta6_iterator_ext(ptr, entry, kadm);
11517c478bd9Sstevel@tonic-gate 	  arg->nnames = tmp_nnames;
11527c478bd9Sstevel@tonic-gate      }
115356a424ccSmp153739 
11547c478bd9Sstevel@tonic-gate      free(name);
115556a424ccSmp153739      return retval;
11567c478bd9Sstevel@tonic-gate }
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate /*
11597c478bd9Sstevel@tonic-gate  * dump_iprop_princ()	- Output a dump record in iprop format.
11607c478bd9Sstevel@tonic-gate  * This was created in order to dump more data, such as kadm5 tl
11617c478bd9Sstevel@tonic-gate  */
11627c478bd9Sstevel@tonic-gate static krb5_error_code
dump_iprop_princ(ptr,entry)11637c478bd9Sstevel@tonic-gate dump_iprop_princ(ptr, entry)
11647c478bd9Sstevel@tonic-gate     krb5_pointer	ptr;
11657c478bd9Sstevel@tonic-gate     krb5_db_entry	*entry;
11667c478bd9Sstevel@tonic-gate {
11677c478bd9Sstevel@tonic-gate      krb5_error_code retval;
11687c478bd9Sstevel@tonic-gate      struct dump_args *arg;
11697c478bd9Sstevel@tonic-gate      char *name;
11707c478bd9Sstevel@tonic-gate      int tmp_nnames;
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate      /* Initialize */
11737c478bd9Sstevel@tonic-gate      arg = (struct dump_args *) ptr;
11747c478bd9Sstevel@tonic-gate      name = (char *) NULL;
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate      /*
11777c478bd9Sstevel@tonic-gate       * Flatten the principal name.
11787c478bd9Sstevel@tonic-gate       */
11797c478bd9Sstevel@tonic-gate      if ((retval = krb5_unparse_name(arg->kcontext,
11807c478bd9Sstevel@tonic-gate 				     entry->princ,
11817c478bd9Sstevel@tonic-gate 				     &name))) {
11827c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(pname_unp_err),
11837c478bd9Sstevel@tonic-gate 		  arg->programname, error_message(retval));
11847c478bd9Sstevel@tonic-gate 	  return(retval);
11857c478bd9Sstevel@tonic-gate      }
11867c478bd9Sstevel@tonic-gate      /*
11877c478bd9Sstevel@tonic-gate       * If we don't have any match strings, or if our name matches, then
11887c478bd9Sstevel@tonic-gate       * proceed with the dump, otherwise, just forget about it.
11897c478bd9Sstevel@tonic-gate       */
11907c478bd9Sstevel@tonic-gate      if (!arg->nnames || name_matches(name, arg)) {
11917c478bd9Sstevel@tonic-gate 	  fprintf(arg->ofile, "princ\t");
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 	  /* save the callee from matching the name again */
11947c478bd9Sstevel@tonic-gate 	  tmp_nnames = arg->nnames;
11957c478bd9Sstevel@tonic-gate 	  arg->nnames = 0;
11967c478bd9Sstevel@tonic-gate 	  retval = dump_iprop_iterator(ptr, entry);
11977c478bd9Sstevel@tonic-gate 	  arg->nnames = tmp_nnames;
11987c478bd9Sstevel@tonic-gate      }
11997c478bd9Sstevel@tonic-gate      free(name);
12007c478bd9Sstevel@tonic-gate 	return (retval);
12017c478bd9Sstevel@tonic-gate }
120256a424ccSmp153739 
120356a424ccSmp153739 static krb5_error_code
dump_k5beta7_princ_withpolicy(ptr,entry)120456a424ccSmp153739 dump_k5beta7_princ_withpolicy(ptr, entry)
120556a424ccSmp153739     krb5_pointer	ptr;
120656a424ccSmp153739     krb5_db_entry	*entry;
120756a424ccSmp153739 {
120856a424ccSmp153739     return dump_k5beta7_princ_ext(ptr, entry, 1);
120956a424ccSmp153739 }
121056a424ccSmp153739 
dump_k5beta7_policy(void * data,osa_policy_ent_t entry)121156a424ccSmp153739 void dump_k5beta7_policy(void *data, osa_policy_ent_t entry)
12127c478bd9Sstevel@tonic-gate {
12137c478bd9Sstevel@tonic-gate      struct dump_args *arg;
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate      arg = (struct dump_args *) data;
12167c478bd9Sstevel@tonic-gate      fprintf(arg->ofile, "policy\t%s\t%d\t%d\t%d\t%d\t%d\t%d\n", entry->name,
12177c478bd9Sstevel@tonic-gate 	     entry->pw_min_life, entry->pw_max_life, entry->pw_min_length,
12187c478bd9Sstevel@tonic-gate 	     entry->pw_min_classes, entry->pw_history_num,
12197c478bd9Sstevel@tonic-gate 	     entry->policy_refcnt);
12207c478bd9Sstevel@tonic-gate }
12217c478bd9Sstevel@tonic-gate 
print_key_data(FILE * f,krb5_key_data * key_data)122256a424ccSmp153739 static void print_key_data(FILE *f, krb5_key_data *key_data)
12237c478bd9Sstevel@tonic-gate {
12247c478bd9Sstevel@tonic-gate      int c;
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate      fprintf(f, "%d\t%d\t", key_data->key_data_type[0],
12277c478bd9Sstevel@tonic-gate 	     key_data->key_data_length[0]);
12287c478bd9Sstevel@tonic-gate      for(c = 0; c < key_data->key_data_length[0]; c++)
12297c478bd9Sstevel@tonic-gate 	  fprintf(f, "%02x ",
12307c478bd9Sstevel@tonic-gate 		  key_data->key_data_contents[0][c]);
12317c478bd9Sstevel@tonic-gate }
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate /*
12347c478bd9Sstevel@tonic-gate  * Function: print_princ
12357c478bd9Sstevel@tonic-gate  *
12367c478bd9Sstevel@tonic-gate  * Purpose: output osa_adb_princ_ent data in a human
12377c478bd9Sstevel@tonic-gate  *	    readable format (which is a format suitable for
12387c478bd9Sstevel@tonic-gate  *	    ovsec_adm_import consumption)
12397c478bd9Sstevel@tonic-gate  *
12407c478bd9Sstevel@tonic-gate  * Arguments:
12417c478bd9Sstevel@tonic-gate  *	data		(input) pointer to a structure containing a FILE *
12427c478bd9Sstevel@tonic-gate  *			        and a record counter.
12437c478bd9Sstevel@tonic-gate  *	entry		(input) entry to get dumped.
12447c478bd9Sstevel@tonic-gate  *	<return value>	void
12457c478bd9Sstevel@tonic-gate  *
12467c478bd9Sstevel@tonic-gate  * Requires:
12477c478bd9Sstevel@tonic-gate  *	nuttin
12487c478bd9Sstevel@tonic-gate  *
12497c478bd9Sstevel@tonic-gate  * Effects:
12507c478bd9Sstevel@tonic-gate  *	writes data to the specified file pointerp.
12517c478bd9Sstevel@tonic-gate  *
12527c478bd9Sstevel@tonic-gate  * Modifies:
12537c478bd9Sstevel@tonic-gate  *	nuttin
12547c478bd9Sstevel@tonic-gate  *
12557c478bd9Sstevel@tonic-gate  */
dump_ov_princ(krb5_pointer ptr,krb5_db_entry * kdb)125656a424ccSmp153739 static krb5_error_code dump_ov_princ(krb5_pointer ptr, krb5_db_entry *kdb)
12577c478bd9Sstevel@tonic-gate {
12587c478bd9Sstevel@tonic-gate     char *princstr;
125956a424ccSmp153739     int	x, y, foundcrc;
12607c478bd9Sstevel@tonic-gate     struct dump_args *arg;
12617c478bd9Sstevel@tonic-gate     krb5_tl_data tl_data;
12627c478bd9Sstevel@tonic-gate     osa_princ_ent_rec adb;
12637c478bd9Sstevel@tonic-gate     XDR xdrs;
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate     arg = (struct dump_args *) ptr;
12667c478bd9Sstevel@tonic-gate     /*
12677c478bd9Sstevel@tonic-gate      * XXX Currently, lookup_tl_data always returns zero; it sets
126856a424ccSmp153739      * tl_data->tl_data_length to zero if the type isn't found.
126956a424ccSmp153739      * This should be fixed...
12707c478bd9Sstevel@tonic-gate      */
12717c478bd9Sstevel@tonic-gate     /*
12727c478bd9Sstevel@tonic-gate      * XXX Should this function do nothing for a principal with no
127356a424ccSmp153739      * admin data, or print a record of "default" values?   See
127456a424ccSmp153739      * comment in server_kdb.c to help decide.
12757c478bd9Sstevel@tonic-gate      */
12767c478bd9Sstevel@tonic-gate     tl_data.tl_data_type = KRB5_TL_KADM_DATA;
127756a424ccSmp153739     if (krb5_dbe_lookup_tl_data(arg->kcontext, kdb, &tl_data)
127856a424ccSmp153739 	|| (tl_data.tl_data_length == 0))
127956a424ccSmp153739 	 return 0;
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate     memset(&adb, 0, sizeof(adb));
12827c478bd9Sstevel@tonic-gate     xdrmem_create(&xdrs, (const caddr_t) tl_data.tl_data_contents,
12837c478bd9Sstevel@tonic-gate 		  tl_data.tl_data_length, XDR_DECODE);
12847c478bd9Sstevel@tonic-gate     if (! xdr_osa_princ_ent_rec(&xdrs, &adb)) {
12857c478bd9Sstevel@tonic-gate 	 xdr_destroy(&xdrs);
128654925bf6Swillf 	 return(KADM5_XDR_FAILURE);
12877c478bd9Sstevel@tonic-gate     }
12887c478bd9Sstevel@tonic-gate     xdr_destroy(&xdrs);
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate     krb5_unparse_name(arg->kcontext, kdb->princ, &princstr);
12917c478bd9Sstevel@tonic-gate     fprintf(arg->ofile, "princ\t%s\t", princstr);
12927c478bd9Sstevel@tonic-gate     if(adb.policy == NULL)
12937c478bd9Sstevel@tonic-gate 	fputc('\t', arg->ofile);
12947c478bd9Sstevel@tonic-gate     else
12957c478bd9Sstevel@tonic-gate 	fprintf(arg->ofile, "%s\t", adb.policy);
129656a424ccSmp153739     fprintf(arg->ofile, "%lx\t%d\t%d\t%d", adb.aux_attributes,
12977c478bd9Sstevel@tonic-gate 	    adb.old_key_len,adb.old_key_next, adb.admin_history_kvno);
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate     for (x = 0; x < adb.old_key_len; x++) {
13007c478bd9Sstevel@tonic-gate 	 foundcrc = 0;
13017c478bd9Sstevel@tonic-gate 	 for (y = 0; y < adb.old_keys[x].n_key_data; y++) {
13027c478bd9Sstevel@tonic-gate 	      krb5_key_data *key_data = &adb.old_keys[x].key_data[y];
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 	      if (key_data->key_data_type[0] != ENCTYPE_DES_CBC_CRC)
13057c478bd9Sstevel@tonic-gate 		   continue;
13067c478bd9Sstevel@tonic-gate 	      if (foundcrc) {
13077c478bd9Sstevel@tonic-gate 				fprintf(stderr,
13087c478bd9Sstevel@tonic-gate 				    gettext("Warning!  Multiple DES-CBC-CRC "
13097c478bd9Sstevel@tonic-gate 					"keys for principal %s; skipping "
13107c478bd9Sstevel@tonic-gate 					"duplicates.\n"),
13117c478bd9Sstevel@tonic-gate 			   princstr);
13127c478bd9Sstevel@tonic-gate 		   continue;
13137c478bd9Sstevel@tonic-gate 	      }
13147c478bd9Sstevel@tonic-gate 	      foundcrc++;
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 	      fputc('\t', arg->ofile);
13177c478bd9Sstevel@tonic-gate 	      print_key_data(arg->ofile, key_data);
13187c478bd9Sstevel@tonic-gate 	 }
13197c478bd9Sstevel@tonic-gate 	 if (!foundcrc)
13207c478bd9Sstevel@tonic-gate 			fprintf(stderr,
13217c478bd9Sstevel@tonic-gate 			    gettext("Warning!  No DES-CBC-CRC key "
13227c478bd9Sstevel@tonic-gate 				"for principal %s, cannot generate "
13237c478bd9Sstevel@tonic-gate 				"OV-compatible record; skipping\n"),
13247c478bd9Sstevel@tonic-gate 		      princstr);
13257c478bd9Sstevel@tonic-gate     }
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate     fputc('\n', arg->ofile);
13287c478bd9Sstevel@tonic-gate     free(princstr);
132956a424ccSmp153739     return 0;
13307c478bd9Sstevel@tonic-gate }
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate /*
13337c478bd9Sstevel@tonic-gate  * usage is:
133456a424ccSmp153739  *	dump_db [-i] [-old] [-b6] [-b7] [-ov] [-verbose] [-mkey_convert]
133556a424ccSmp153739  *		[-new_mkey_file mkey_file] [-rev] [-recurse]
133656a424ccSmp153739  *		[filename [principals...]]
13377c478bd9Sstevel@tonic-gate  */
13387c478bd9Sstevel@tonic-gate void
dump_db(argc,argv)13397c478bd9Sstevel@tonic-gate dump_db(argc, argv)
13407c478bd9Sstevel@tonic-gate     int		argc;
13417c478bd9Sstevel@tonic-gate     char	**argv;
13427c478bd9Sstevel@tonic-gate {
13437c478bd9Sstevel@tonic-gate     FILE		*f;
13447c478bd9Sstevel@tonic-gate     struct dump_args	arglist;
1345dd9ccd46S /* Solaris Kerberos */
1346dd9ccd46S #if 0
13477c478bd9Sstevel@tonic-gate     char		*programname;
1348dd9ccd46S #endif
13497c478bd9Sstevel@tonic-gate     char		*ofile;
13507c478bd9Sstevel@tonic-gate     krb5_error_code	kret, retval;
13517c478bd9Sstevel@tonic-gate     dump_version	*dump;
13527c478bd9Sstevel@tonic-gate     int			aindex;
13537c478bd9Sstevel@tonic-gate     krb5_boolean	locked;
13547c478bd9Sstevel@tonic-gate     char		*new_mkey_file = 0;
13557c478bd9Sstevel@tonic-gate     bool_t		dump_sno = FALSE;
13567c478bd9Sstevel@tonic-gate     kdb_log_context	*log_ctx;
13572dd2efa5Swillf     /* Solaris Kerberos: adding support for -rev/recurse flags */
13582dd2efa5Swillf     int			db_arg_index = 0;
13592dd2efa5Swillf     char		*db_args[3] = {NULL, NULL, NULL};
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate     /*
13627c478bd9Sstevel@tonic-gate      * Parse the arguments.
13637c478bd9Sstevel@tonic-gate      */
1364dd9ccd46S /* Solaris Kerberos */
1365dd9ccd46S #if 0
13667c478bd9Sstevel@tonic-gate     programname = argv[0];
13677c478bd9Sstevel@tonic-gate     if (strrchr(programname, (int) '/'))
13687c478bd9Sstevel@tonic-gate 	programname = strrchr(argv[0], (int) '/') + 1;
1369dd9ccd46S #endif
13707c478bd9Sstevel@tonic-gate     ofile = (char *) NULL;
137156a424ccSmp153739     dump = &r1_3_version;
13727c478bd9Sstevel@tonic-gate     arglist.verbose = 0;
13737c478bd9Sstevel@tonic-gate     new_mkey_file = 0;
13747c478bd9Sstevel@tonic-gate     mkey_convert = 0;
137556a424ccSmp153739     backwards = 0;
137656a424ccSmp153739     recursive = 0;
13777c478bd9Sstevel@tonic-gate     log_ctx = util_context->kdblog_context;
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate     /*
13807c478bd9Sstevel@tonic-gate      * Parse the qualifiers.
13817c478bd9Sstevel@tonic-gate      */
13827c478bd9Sstevel@tonic-gate     for (aindex = 1; aindex < argc; aindex++) {
138356a424ccSmp153739 	if (!strcmp(argv[aindex], oldoption))
13847c478bd9Sstevel@tonic-gate 	     dump = &old_version;
138556a424ccSmp153739 	else if (!strcmp(argv[aindex], b6option))
13867c478bd9Sstevel@tonic-gate 	     dump = &beta6_version;
138756a424ccSmp153739 	else if (!strcmp(argv[aindex], b7option))
138856a424ccSmp153739 	     dump = &beta7_version;
138956a424ccSmp153739 	else if (!strcmp(argv[aindex], ovoption))
13907c478bd9Sstevel@tonic-gate 	     dump = &ov_version;
13917c478bd9Sstevel@tonic-gate 	else if (!strcmp(argv[aindex], ipropoption)) {
13927c478bd9Sstevel@tonic-gate 			if (log_ctx && log_ctx->iproprole) {
13937c478bd9Sstevel@tonic-gate 				dump = &iprop_version;
13947c478bd9Sstevel@tonic-gate 				/*
13957c478bd9Sstevel@tonic-gate 				 * dump_sno is used to indicate if the serial
13967c478bd9Sstevel@tonic-gate 				 * # should be populated in the output
13977c478bd9Sstevel@tonic-gate 				 * file to be used later by iprop for updating
13987c478bd9Sstevel@tonic-gate 				 * the slave's update log when loading
13997c478bd9Sstevel@tonic-gate 				 */
14007c478bd9Sstevel@tonic-gate 				dump_sno = TRUE;
14017c478bd9Sstevel@tonic-gate 			} else {
14027c478bd9Sstevel@tonic-gate 				fprintf(stderr, gettext("Iprop not enabled\n"));
14037c478bd9Sstevel@tonic-gate 				exit_status++;
14047c478bd9Sstevel@tonic-gate 				return;
14057c478bd9Sstevel@tonic-gate 			}
14067c478bd9Sstevel@tonic-gate 		}
140756a424ccSmp153739 	else if (!strcmp(argv[aindex], verboseoption))
14087c478bd9Sstevel@tonic-gate 	    arglist.verbose++;
14097c478bd9Sstevel@tonic-gate 	else if (!strcmp(argv[aindex], "-mkey_convert"))
14107c478bd9Sstevel@tonic-gate 	    mkey_convert = 1;
14117c478bd9Sstevel@tonic-gate 	else if (!strcmp(argv[aindex], "-new_mkey_file")) {
14127c478bd9Sstevel@tonic-gate 	    new_mkey_file = argv[++aindex];
14137c478bd9Sstevel@tonic-gate 	    mkey_convert = 1;
14142dd2efa5Swillf         } else if (!strcmp(argv[aindex], "-rev")) {
14152dd2efa5Swillf 	    /* Solaris Kerberos: adding support for -rev/recurse flags */
14162dd2efa5Swillf 	    /* hack to pass args to db specific plugin */
14172dd2efa5Swillf 	    db_args[db_arg_index++] = "rev";
14182dd2efa5Swillf 	} else if (!strcmp(argv[aindex], "-recurse")) {
14192dd2efa5Swillf 	    /* hack to pass args to db specific plugin */
14202dd2efa5Swillf 	    db_args[db_arg_index++] = "recurse";
14212dd2efa5Swillf 	} else
14227c478bd9Sstevel@tonic-gate 	    break;
14237c478bd9Sstevel@tonic-gate     }
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate     arglist.names = (char **) NULL;
14267c478bd9Sstevel@tonic-gate     arglist.nnames = 0;
14277c478bd9Sstevel@tonic-gate     if (aindex < argc) {
14287c478bd9Sstevel@tonic-gate 	ofile = argv[aindex];
14297c478bd9Sstevel@tonic-gate 	aindex++;
14307c478bd9Sstevel@tonic-gate 	if (aindex < argc) {
14317c478bd9Sstevel@tonic-gate 	    arglist.names = &argv[aindex];
14327c478bd9Sstevel@tonic-gate 	    arglist.nnames = argc - aindex;
14337c478bd9Sstevel@tonic-gate 	}
14347c478bd9Sstevel@tonic-gate     }
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate     /*
14377c478bd9Sstevel@tonic-gate      * Make sure the database is open.  The policy database only has
14387c478bd9Sstevel@tonic-gate      * to be opened if we try a dump that uses it.
14397c478bd9Sstevel@tonic-gate      */
144054925bf6Swillf     if (!dbactive) {
1441dd9ccd46S 	/* Solaris Kerberos */
1442dd9ccd46S 	com_err(progname, 0, Err_no_database); /* Solaris Kerberos */
14437c478bd9Sstevel@tonic-gate 	exit_status++;
14447c478bd9Sstevel@tonic-gate 	return;
14457c478bd9Sstevel@tonic-gate     }
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate     /*
14487c478bd9Sstevel@tonic-gate      * If we're doing a master key conversion, set up for it.
14497c478bd9Sstevel@tonic-gate      */
14507c478bd9Sstevel@tonic-gate     if (mkey_convert) {
14517c478bd9Sstevel@tonic-gate 	    if (!valid_master_key) {
14527c478bd9Sstevel@tonic-gate 		    /* TRUE here means read the keyboard, but only once */
14537c478bd9Sstevel@tonic-gate 		    retval = krb5_db_fetch_mkey(util_context,
14547c478bd9Sstevel@tonic-gate 						master_princ,
14557c478bd9Sstevel@tonic-gate 						global_params.enctype,
14567c478bd9Sstevel@tonic-gate 						TRUE, FALSE,
14577c478bd9Sstevel@tonic-gate 						(char *) NULL, 0,
14587c478bd9Sstevel@tonic-gate 						&master_key);
14597c478bd9Sstevel@tonic-gate 		    if (retval) {
1460dd9ccd46S 			    /* Solaris Kerberos */
1461dd9ccd46S 			    com_err(progname, retval,
14627c478bd9Sstevel@tonic-gate 				    gettext("while reading master key"));
14637c478bd9Sstevel@tonic-gate 			    exit(1);
14647c478bd9Sstevel@tonic-gate 		    }
14657c478bd9Sstevel@tonic-gate 		    retval = krb5_db_verify_master_key(util_context,
14667c478bd9Sstevel@tonic-gate 						       master_princ,
14677c478bd9Sstevel@tonic-gate 						       &master_key);
14687c478bd9Sstevel@tonic-gate 		    if (retval) {
1469dd9ccd46S 			    /* Solaris Kerberos */
1470dd9ccd46S 			    com_err(progname, retval,
14717c478bd9Sstevel@tonic-gate 				    gettext("while verifying master key"));
14727c478bd9Sstevel@tonic-gate 			    exit(1);
14737c478bd9Sstevel@tonic-gate 		    }
14747c478bd9Sstevel@tonic-gate 	    }
14757c478bd9Sstevel@tonic-gate 	    if (!new_mkey_file)
14767c478bd9Sstevel@tonic-gate 		    printf(gettext("Please enter new master key....\n"));
14777c478bd9Sstevel@tonic-gate 	    if ((retval = krb5_db_fetch_mkey(util_context, master_princ,
14787c478bd9Sstevel@tonic-gate 					     global_params.enctype,
147956a424ccSmp153739 					     (new_mkey_file == 0) ?
148056a424ccSmp153739 					        (krb5_boolean) 1 : 0,
148156a424ccSmp153739 					     TRUE,
14827c478bd9Sstevel@tonic-gate 					     new_mkey_file, 0,
14837c478bd9Sstevel@tonic-gate 					     &new_master_key))) {
1484dd9ccd46S 		    /* Solaris Kerberos */
1485dd9ccd46S 		    com_err(progname, retval,
14867c478bd9Sstevel@tonic-gate 			    gettext("while reading new master key"));
14877c478bd9Sstevel@tonic-gate 		    exit(1);
14887c478bd9Sstevel@tonic-gate 	    }
14897c478bd9Sstevel@tonic-gate     }
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate     kret = 0;
14927c478bd9Sstevel@tonic-gate     locked = 0;
14937c478bd9Sstevel@tonic-gate     if (ofile && strcmp(ofile, "-")) {
14947c478bd9Sstevel@tonic-gate 	/*
149556a424ccSmp153739 	 * Discourage accidental dumping to filenames beginning with '-'.
149656a424ccSmp153739 	 */
149756a424ccSmp153739 	if (ofile[0] == '-')
149856a424ccSmp153739 	    usage();
149956a424ccSmp153739 	/*
15007c478bd9Sstevel@tonic-gate 	 * Make sure that we don't open and truncate on the fopen,
15017c478bd9Sstevel@tonic-gate 	 * since that may hose an on-going kprop process.
15027c478bd9Sstevel@tonic-gate 	 *
150356a424ccSmp153739 	 * We could also control this by opening for read and
150456a424ccSmp153739 	 * write, doing an flock with LOCK_EX, and then
150556a424ccSmp153739 	 * truncating the file once we have gotten the lock,
150656a424ccSmp153739 	 * but that would involve more OS dependencies than I
150756a424ccSmp153739 	 * want to get into.
15087c478bd9Sstevel@tonic-gate 	 */
15097c478bd9Sstevel@tonic-gate 	unlink(ofile);
15107c478bd9Sstevel@tonic-gate 	if (!(f = fopen(ofile, "w"))) {
1511dd9ccd46S 			/* Solaris Kerberos */
15127c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(ofopen_error),
1513dd9ccd46S 		    progname, ofile, error_message(errno));
15147c478bd9Sstevel@tonic-gate 	    exit_status++;
15157c478bd9Sstevel@tonic-gate 	    return;
15167c478bd9Sstevel@tonic-gate        }
15177c478bd9Sstevel@tonic-gate 	if ((kret = krb5_lock_file(util_context,
15187c478bd9Sstevel@tonic-gate 				   fileno(f),
15197c478bd9Sstevel@tonic-gate 				   KRB5_LOCKMODE_EXCLUSIVE))) {
1520dd9ccd46S 			/* Solaris Kerberos */
15217c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(oflock_error),
1522dd9ccd46S 		    progname, ofile, error_message(kret));
15237c478bd9Sstevel@tonic-gate 	    exit_status++;
152456a424ccSmp153739 	}
152556a424ccSmp153739 	else
15267c478bd9Sstevel@tonic-gate 	    locked = 1;
15277c478bd9Sstevel@tonic-gate     } else {
15287c478bd9Sstevel@tonic-gate 	f = stdout;
15297c478bd9Sstevel@tonic-gate     }
15307c478bd9Sstevel@tonic-gate     if (f && !(kret)) {
1531dd9ccd46S 	/* Solaris Kerberos */
1532dd9ccd46S 	arglist.programname = progname;
15337c478bd9Sstevel@tonic-gate 	arglist.ofile = f;
15347c478bd9Sstevel@tonic-gate 	arglist.kcontext = util_context;
15357c478bd9Sstevel@tonic-gate 	fprintf(arglist.ofile, "%s", dump->header);
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate 	if (dump_sno) {
15387c478bd9Sstevel@tonic-gate 		if (ulog_map(util_context, &global_params, FKCOMMAND)) {
1539dd9ccd46S 			/* Solaris Kerberos */
15407c478bd9Sstevel@tonic-gate 			fprintf(stderr,
1541dd9ccd46S 			    gettext("%s: Could not map log\n"), progname);
15427c478bd9Sstevel@tonic-gate 			exit_status++;
15437c478bd9Sstevel@tonic-gate 			goto error;
15447c478bd9Sstevel@tonic-gate 		}
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 		/*
15477c478bd9Sstevel@tonic-gate 		 * We grab the lock twice (once again in the iterator call),
15487c478bd9Sstevel@tonic-gate 		 * but that's ok since the lock func handles incr locks held.
15497c478bd9Sstevel@tonic-gate 		 */
15507c478bd9Sstevel@tonic-gate 		if (krb5_db_lock(util_context, KRB5_LOCKMODE_SHARED)) {
1551dd9ccd46S 			/* Solaris Kerberos */
15527c478bd9Sstevel@tonic-gate 			fprintf(stderr,
1553dd9ccd46S 			    gettext("%s: Couldn't grab lock\n"), progname);
15547c478bd9Sstevel@tonic-gate 			exit_status++;
15557c478bd9Sstevel@tonic-gate 			goto error;
15567c478bd9Sstevel@tonic-gate 		}
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate 		fprintf(f, " %u", log_ctx->ulog->kdb_last_sno);
15597c478bd9Sstevel@tonic-gate 		fprintf(f, " %u", log_ctx->ulog->kdb_last_time.seconds);
15607c478bd9Sstevel@tonic-gate 		fprintf(f, " %u", log_ctx->ulog->kdb_last_time.useconds);
15617c478bd9Sstevel@tonic-gate 	}
15627c478bd9Sstevel@tonic-gate 
15637c478bd9Sstevel@tonic-gate 	if (dump->header[strlen(dump->header)-1] != '\n')
15647c478bd9Sstevel@tonic-gate 	     fputc('\n', arglist.ofile);
15657c478bd9Sstevel@tonic-gate 
15662dd2efa5Swillf 	/* Solaris Kerberos: adding support for -rev/recurse flags */
15672dd2efa5Swillf 	/* don't pass in db_args if there aren't any */
156854925bf6Swillf 	if ((kret = krb5_db_iterate(util_context,
156954925bf6Swillf 				    NULL,
15707c478bd9Sstevel@tonic-gate 				    dump->dump_princ,
15712dd2efa5Swillf 				    (krb5_pointer) &arglist,
15722dd2efa5Swillf 				    db_arg_index > 0 ? (char **)&db_args : NULL))) {
1573dd9ccd46S 	     /* Solaris Kerberos */
157454925bf6Swillf 	     fprintf(stderr, dumprec_err,
1575dd9ccd46S 		     progname, dump->name, error_message(kret));
15767c478bd9Sstevel@tonic-gate 	     exit_status++;
15777c478bd9Sstevel@tonic-gate 		if (dump_sno)
15787c478bd9Sstevel@tonic-gate 			(void) krb5_db_unlock(util_context);
15797c478bd9Sstevel@tonic-gate 	}
15807c478bd9Sstevel@tonic-gate 	if (dump->dump_policy &&
158154925bf6Swillf 	    (kret = krb5_db_iter_policy( util_context, "*", dump->dump_policy,
15827c478bd9Sstevel@tonic-gate 					 &arglist))) {
1583dd9ccd46S 	     /* Solaris Kerberos */
15847c478bd9Sstevel@tonic-gate 	     fprintf(stderr, gettext(dumprec_err),
1585dd9ccd46S 		progname, dump->name,
15867c478bd9Sstevel@tonic-gate 		     error_message(kret));
15877c478bd9Sstevel@tonic-gate 	     exit_status++;
15887c478bd9Sstevel@tonic-gate 	}
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate error:
15917c478bd9Sstevel@tonic-gate 	if (ofile && f != stdout && !exit_status) {
159254925bf6Swillf 	    if (locked) {
159354925bf6Swillf 		(void) krb5_lock_file(util_context, fileno(f), KRB5_LOCKMODE_UNLOCK);
159454925bf6Swillf 		locked = 0;
159554925bf6Swillf 	    }
15967c478bd9Sstevel@tonic-gate 	    fclose(f);
15977c478bd9Sstevel@tonic-gate 	    update_ok_file(ofile);
15987c478bd9Sstevel@tonic-gate 	}
15997c478bd9Sstevel@tonic-gate     }
16007c478bd9Sstevel@tonic-gate     if (locked)
160156a424ccSmp153739 	(void) krb5_lock_file(util_context, fileno(f), KRB5_LOCKMODE_UNLOCK);
16027c478bd9Sstevel@tonic-gate }
16037c478bd9Sstevel@tonic-gate 
16047c478bd9Sstevel@tonic-gate /*
16057c478bd9Sstevel@tonic-gate  * Read a string of bytes while counting the number of lines passed.
16067c478bd9Sstevel@tonic-gate  */
16077c478bd9Sstevel@tonic-gate static int
read_string(f,buf,len,lp)16087c478bd9Sstevel@tonic-gate read_string(f, buf, len, lp)
16097c478bd9Sstevel@tonic-gate     FILE	*f;
16107c478bd9Sstevel@tonic-gate     char	*buf;
16117c478bd9Sstevel@tonic-gate     int		len;
16127c478bd9Sstevel@tonic-gate     int		*lp;
16137c478bd9Sstevel@tonic-gate {
16147c478bd9Sstevel@tonic-gate     int c;
16157c478bd9Sstevel@tonic-gate     int i, retval;
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate     retval = 0;
16187c478bd9Sstevel@tonic-gate     for (i=0; i<len; i++) {
16197c478bd9Sstevel@tonic-gate 	c = fgetc(f);
16207c478bd9Sstevel@tonic-gate 	if (c < 0) {
16217c478bd9Sstevel@tonic-gate 	    retval = 1;
16227c478bd9Sstevel@tonic-gate 	    break;
16237c478bd9Sstevel@tonic-gate 	}
16247c478bd9Sstevel@tonic-gate 	if (c == '\n')
16257c478bd9Sstevel@tonic-gate 	    (*lp)++;
16267c478bd9Sstevel@tonic-gate 	buf[i] = (char) c;
16277c478bd9Sstevel@tonic-gate     }
16287c478bd9Sstevel@tonic-gate     buf[len] = '\0';
16297c478bd9Sstevel@tonic-gate     return(retval);
16307c478bd9Sstevel@tonic-gate }
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate /*
16337c478bd9Sstevel@tonic-gate  * Read a string of two character representations of bytes.
16347c478bd9Sstevel@tonic-gate  */
16357c478bd9Sstevel@tonic-gate static int
read_octet_string(f,buf,len)16367c478bd9Sstevel@tonic-gate read_octet_string(f, buf, len)
16377c478bd9Sstevel@tonic-gate     FILE	*f;
16387c478bd9Sstevel@tonic-gate     krb5_octet	*buf;
16397c478bd9Sstevel@tonic-gate     int		len;
16407c478bd9Sstevel@tonic-gate {
16417c478bd9Sstevel@tonic-gate     int c;
16427c478bd9Sstevel@tonic-gate     int i, retval;
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate     retval = 0;
16457c478bd9Sstevel@tonic-gate     for (i=0; i<len; i++) {
16467c478bd9Sstevel@tonic-gate 	if (fscanf(f, "%02x", &c) != 1) {
16477c478bd9Sstevel@tonic-gate 	    retval = 1;
16487c478bd9Sstevel@tonic-gate 	    break;
16497c478bd9Sstevel@tonic-gate 	}
16507c478bd9Sstevel@tonic-gate 	buf[i] = (krb5_octet) c;
16517c478bd9Sstevel@tonic-gate     }
16527c478bd9Sstevel@tonic-gate     return(retval);
16537c478bd9Sstevel@tonic-gate }
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate /*
16567c478bd9Sstevel@tonic-gate  * Find the end of an old format record.
16577c478bd9Sstevel@tonic-gate  */
16587c478bd9Sstevel@tonic-gate static void
find_record_end(f,fn,lineno)16597c478bd9Sstevel@tonic-gate find_record_end(f, fn, lineno)
16607c478bd9Sstevel@tonic-gate     FILE	*f;
16617c478bd9Sstevel@tonic-gate     char	*fn;
16627c478bd9Sstevel@tonic-gate     int		lineno;
16637c478bd9Sstevel@tonic-gate {
16647c478bd9Sstevel@tonic-gate     int	ch;
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate     if (((ch = fgetc(f)) != ';') || ((ch = fgetc(f)) != '\n')) {
16677c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(trash_end_fmt), fn, lineno);
16687c478bd9Sstevel@tonic-gate 	while (ch != '\n') {
16697c478bd9Sstevel@tonic-gate 	    putc(ch, stderr);
16707c478bd9Sstevel@tonic-gate 	    ch = fgetc(f);
16717c478bd9Sstevel@tonic-gate 	}
16727c478bd9Sstevel@tonic-gate 	putc(ch, stderr);
16737c478bd9Sstevel@tonic-gate     }
16747c478bd9Sstevel@tonic-gate }
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate #if 0
16777c478bd9Sstevel@tonic-gate /*
16787c478bd9Sstevel@tonic-gate  * update_tl_data()	- Generate the tl_data entries.
16797c478bd9Sstevel@tonic-gate  */
16807c478bd9Sstevel@tonic-gate static krb5_error_code
16817c478bd9Sstevel@tonic-gate update_tl_data(kcontext, dbentp, mod_name, mod_date, last_pwd_change)
16827c478bd9Sstevel@tonic-gate     krb5_context	kcontext;
16837c478bd9Sstevel@tonic-gate     krb5_db_entry	*dbentp;
16847c478bd9Sstevel@tonic-gate     krb5_principal	mod_name;
16857c478bd9Sstevel@tonic-gate     krb5_timestamp	mod_date;
16867c478bd9Sstevel@tonic-gate     krb5_timestamp	last_pwd_change;
16877c478bd9Sstevel@tonic-gate {
16887c478bd9Sstevel@tonic-gate     krb5_error_code	kret;
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate     kret = 0 ;
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate     /*
16937c478bd9Sstevel@tonic-gate      * Handle modification principal.
16947c478bd9Sstevel@tonic-gate      */
16957c478bd9Sstevel@tonic-gate     if (mod_name) {
16967c478bd9Sstevel@tonic-gate 	krb5_tl_mod_princ	mprinc;
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 	memset(&mprinc, 0, sizeof(mprinc));
16997c478bd9Sstevel@tonic-gate 	if (!(kret = krb5_copy_principal(kcontext,
17007c478bd9Sstevel@tonic-gate 					 mod_name,
17017c478bd9Sstevel@tonic-gate 					 &mprinc.mod_princ))) {
17027c478bd9Sstevel@tonic-gate 	    mprinc.mod_date = mod_date;
17037c478bd9Sstevel@tonic-gate 	    kret = krb5_dbe_encode_mod_princ_data(kcontext,
17047c478bd9Sstevel@tonic-gate 						  &mprinc,
17057c478bd9Sstevel@tonic-gate 						  dbentp);
17067c478bd9Sstevel@tonic-gate 	}
17077c478bd9Sstevel@tonic-gate 	if (mprinc.mod_princ)
17087c478bd9Sstevel@tonic-gate 	    krb5_free_principal(kcontext, mprinc.mod_princ);
17097c478bd9Sstevel@tonic-gate     }
171056a424ccSmp153739 
17117c478bd9Sstevel@tonic-gate     /*
17127c478bd9Sstevel@tonic-gate      * Handle last password change.
17137c478bd9Sstevel@tonic-gate      */
17147c478bd9Sstevel@tonic-gate     if (!kret) {
17157c478bd9Sstevel@tonic-gate 	krb5_tl_data	*pwchg;
17167c478bd9Sstevel@tonic-gate 	krb5_boolean	linked;
17177c478bd9Sstevel@tonic-gate 
17187c478bd9Sstevel@tonic-gate 	/* Find a previously existing entry */
17197c478bd9Sstevel@tonic-gate 	for (pwchg = dbentp->tl_data;
17207c478bd9Sstevel@tonic-gate 	     (pwchg) && (pwchg->tl_data_type != KRB5_TL_LAST_PWD_CHANGE);
17217c478bd9Sstevel@tonic-gate 	     pwchg = pwchg->tl_data_next);
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 	/* Check to see if we found one. */
17247c478bd9Sstevel@tonic-gate 	linked = 0;
17257c478bd9Sstevel@tonic-gate 	if (!pwchg) {
17267c478bd9Sstevel@tonic-gate 	    /* No, allocate a new one */
172756a424ccSmp153739 	    if ((pwchg = (krb5_tl_data *) malloc(sizeof(krb5_tl_data)))) {
17287c478bd9Sstevel@tonic-gate 		memset(pwchg, 0, sizeof(krb5_tl_data));
17297c478bd9Sstevel@tonic-gate 		if (!(pwchg->tl_data_contents =
17307c478bd9Sstevel@tonic-gate 		      (krb5_octet *) malloc(sizeof(krb5_timestamp)))) {
17317c478bd9Sstevel@tonic-gate 		    free(pwchg);
17327c478bd9Sstevel@tonic-gate 		    pwchg = (krb5_tl_data *) NULL;
173356a424ccSmp153739 		}
173456a424ccSmp153739 		else {
17357c478bd9Sstevel@tonic-gate 		    pwchg->tl_data_type = KRB5_TL_LAST_PWD_CHANGE;
17367c478bd9Sstevel@tonic-gate 		    pwchg->tl_data_length =
17377c478bd9Sstevel@tonic-gate 			(krb5_int16) sizeof(krb5_timestamp);
17387c478bd9Sstevel@tonic-gate 		}
17397c478bd9Sstevel@tonic-gate 	    }
174056a424ccSmp153739 	}
174156a424ccSmp153739 	else
17427c478bd9Sstevel@tonic-gate 	    linked = 1;
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 	/* Do we have an entry? */
17457c478bd9Sstevel@tonic-gate 	if (pwchg && pwchg->tl_data_contents) {
17467c478bd9Sstevel@tonic-gate 	    /* Encode it */
174756a424ccSmp153739 	    krb5_kdb_encode_int32(last_pwd_change, pwchg->tl_data_contents);
17487c478bd9Sstevel@tonic-gate 	    /* Link it in if necessary */
17497c478bd9Sstevel@tonic-gate 	    if (!linked) {
17507c478bd9Sstevel@tonic-gate 		pwchg->tl_data_next = dbentp->tl_data;
17517c478bd9Sstevel@tonic-gate 		dbentp->tl_data = pwchg;
17527c478bd9Sstevel@tonic-gate 		dbentp->n_tl_data++;
17537c478bd9Sstevel@tonic-gate 	    }
175456a424ccSmp153739 	}
175556a424ccSmp153739 	else
17567c478bd9Sstevel@tonic-gate 	    kret = ENOMEM;
17577c478bd9Sstevel@tonic-gate     }
175856a424ccSmp153739 
17597c478bd9Sstevel@tonic-gate     return(kret);
17607c478bd9Sstevel@tonic-gate }
17617c478bd9Sstevel@tonic-gate #endif
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate /*
17647c478bd9Sstevel@tonic-gate  * process_k5beta_record()	- Handle a dump record in old format.
17657c478bd9Sstevel@tonic-gate  *
17667c478bd9Sstevel@tonic-gate  * Returns -1 for end of file, 0 for success and 1 for failure.
17677c478bd9Sstevel@tonic-gate  */
17687c478bd9Sstevel@tonic-gate static int
process_k5beta_record(fname,kcontext,filep,verbose,linenop)176954925bf6Swillf process_k5beta_record(fname, kcontext, filep, verbose, linenop)
17707c478bd9Sstevel@tonic-gate     char		*fname;
17717c478bd9Sstevel@tonic-gate     krb5_context	kcontext;
17727c478bd9Sstevel@tonic-gate     FILE		*filep;
17737c478bd9Sstevel@tonic-gate     int			verbose;
17747c478bd9Sstevel@tonic-gate     int			*linenop;
17757c478bd9Sstevel@tonic-gate {
17767c478bd9Sstevel@tonic-gate     int			nmatched;
17777c478bd9Sstevel@tonic-gate     int			retval;
17787c478bd9Sstevel@tonic-gate     krb5_db_entry	dbent;
17797c478bd9Sstevel@tonic-gate     int			name_len, mod_name_len, key_len;
17807c478bd9Sstevel@tonic-gate     int			alt_key_len, salt_len, alt_salt_len;
17817c478bd9Sstevel@tonic-gate     char		*name;
17827c478bd9Sstevel@tonic-gate     char		*mod_name;
17837c478bd9Sstevel@tonic-gate     int			tmpint1, tmpint2, tmpint3;
17847c478bd9Sstevel@tonic-gate     int			error;
17857c478bd9Sstevel@tonic-gate     const char		*try2read;
17867c478bd9Sstevel@tonic-gate     int			i;
17877c478bd9Sstevel@tonic-gate     krb5_key_data	*pkey, *akey;
17887c478bd9Sstevel@tonic-gate     krb5_timestamp	last_pwd_change, mod_date;
17897c478bd9Sstevel@tonic-gate     krb5_principal	mod_princ;
17907c478bd9Sstevel@tonic-gate     krb5_error_code	kret;
17917c478bd9Sstevel@tonic-gate     krb5_octet		*shortcopy1 = NULL; /* SUNWresync121 memleak fix */
17927c478bd9Sstevel@tonic-gate     krb5_octet		*shortcopy2 = NULL;
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate     try2read = (char *) NULL;
17957c478bd9Sstevel@tonic-gate     (*linenop)++;
17967c478bd9Sstevel@tonic-gate     retval = 1;
17977c478bd9Sstevel@tonic-gate     memset((char *)&dbent, 0, sizeof(dbent));
17987c478bd9Sstevel@tonic-gate 
17997c478bd9Sstevel@tonic-gate     /* Make sure we've got key_data entries */
18007c478bd9Sstevel@tonic-gate     if (krb5_dbe_create_key_data(kcontext, &dbent) ||
18017c478bd9Sstevel@tonic-gate 	krb5_dbe_create_key_data(kcontext, &dbent)) {
18027c478bd9Sstevel@tonic-gate 	krb5_db_free_principal(kcontext, &dbent, 1);
18037c478bd9Sstevel@tonic-gate 	return(1);
18047c478bd9Sstevel@tonic-gate     }
18057c478bd9Sstevel@tonic-gate     pkey = &dbent.key_data[0];
18067c478bd9Sstevel@tonic-gate     akey = &dbent.key_data[1];
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate     /*
18097c478bd9Sstevel@tonic-gate      * Match the sizes.  6 tokens to match.
18107c478bd9Sstevel@tonic-gate      */
18117c478bd9Sstevel@tonic-gate     nmatched = fscanf(filep, "%d\t%d\t%d\t%d\t%d\t%d\t",
18127c478bd9Sstevel@tonic-gate 		      &name_len, &mod_name_len, &key_len,
18137c478bd9Sstevel@tonic-gate 		      &alt_key_len, &salt_len, &alt_salt_len);
18147c478bd9Sstevel@tonic-gate     if (nmatched == 6) {
18157c478bd9Sstevel@tonic-gate         pkey->key_data_length[0] = key_len;
18167c478bd9Sstevel@tonic-gate 	akey->key_data_length[0] = alt_key_len;
18177c478bd9Sstevel@tonic-gate 	pkey->key_data_length[1] = salt_len;
18187c478bd9Sstevel@tonic-gate 	akey->key_data_length[1] = alt_salt_len;
18197c478bd9Sstevel@tonic-gate 	name = (char *) NULL;
18207c478bd9Sstevel@tonic-gate 	mod_name = (char *) NULL;
18217c478bd9Sstevel@tonic-gate 	/*
18227c478bd9Sstevel@tonic-gate 	 * Get the memory for the variable length fields.
18237c478bd9Sstevel@tonic-gate 	 */
18247c478bd9Sstevel@tonic-gate 	if ((name = (char *) malloc((size_t) (name_len + 1))) &&
18257c478bd9Sstevel@tonic-gate 	    (mod_name = (char *) malloc((size_t) (mod_name_len + 1))) &&
18267c478bd9Sstevel@tonic-gate 	    (!key_len ||
18277c478bd9Sstevel@tonic-gate 	     (pkey->key_data_contents[0] =
18287c478bd9Sstevel@tonic-gate 	      (krb5_octet *) malloc((size_t) (key_len + 1)))) &&
18297c478bd9Sstevel@tonic-gate 	    (!alt_key_len ||
18307c478bd9Sstevel@tonic-gate 	     (akey->key_data_contents[0] =
183156a424ccSmp153739 	      (krb5_octet *) malloc((size_t) (alt_key_len + 1)))) &&
18327c478bd9Sstevel@tonic-gate 	    (!salt_len ||
18337c478bd9Sstevel@tonic-gate 	     (pkey->key_data_contents[1] =
18347c478bd9Sstevel@tonic-gate 	      (krb5_octet *) malloc((size_t) (salt_len + 1)))) &&
18357c478bd9Sstevel@tonic-gate 	    (!alt_salt_len ||
18367c478bd9Sstevel@tonic-gate 	     (akey->key_data_contents[1] =
183756a424ccSmp153739 	      (krb5_octet *) malloc((size_t) (alt_salt_len + 1))))
183856a424ccSmp153739 	    ) {
18397c478bd9Sstevel@tonic-gate 	    error = 0;
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate 	    /* Read the principal name */
18427c478bd9Sstevel@tonic-gate 	    if (read_string(filep, name, name_len, linenop)) {
18437c478bd9Sstevel@tonic-gate 		try2read = read_name_string;
18447c478bd9Sstevel@tonic-gate 		error++;
18457c478bd9Sstevel@tonic-gate 	    }
18467c478bd9Sstevel@tonic-gate 	    /* Read the key type */
184756a424ccSmp153739 	    if (!error && (fscanf(filep, "\t%d\t", &tmpint1) != 1)) {
18487c478bd9Sstevel@tonic-gate 		try2read = read_key_type;
18497c478bd9Sstevel@tonic-gate 		error++;
18507c478bd9Sstevel@tonic-gate 	    }
18517c478bd9Sstevel@tonic-gate 	    pkey->key_data_type[0] = tmpint1;
18527c478bd9Sstevel@tonic-gate 	    /* Read the old format key */
18537c478bd9Sstevel@tonic-gate 	    if (!error && read_octet_string(filep,
18547c478bd9Sstevel@tonic-gate 					    pkey->key_data_contents[0],
18557c478bd9Sstevel@tonic-gate 					    pkey->key_data_length[0])) {
18567c478bd9Sstevel@tonic-gate 		try2read = read_key_data;
18577c478bd9Sstevel@tonic-gate 		error++;
18587c478bd9Sstevel@tonic-gate 	    }
18597c478bd9Sstevel@tonic-gate 	    /* convert to a new format key */
186056a424ccSmp153739 	    /* the encrypted version is stored as the unencrypted key length
186156a424ccSmp153739 	       (4 bytes, MSB first) followed by the encrypted key. */
186256a424ccSmp153739 	    if ((pkey->key_data_length[0] > 4)
186356a424ccSmp153739 		&& (pkey->key_data_contents[0][0] == 0)
186456a424ccSmp153739 		&& (pkey->key_data_contents[0][1] == 0)) {
186556a424ccSmp153739 	      /* this really does look like an old key, so drop and swap */
186656a424ccSmp153739 	      /* the *new* length is 2 bytes, LSB first, sigh. */
18677c478bd9Sstevel@tonic-gate 	      size_t shortlen = pkey->key_data_length[0]-4+2;
18687c478bd9Sstevel@tonic-gate 	      krb5_octet *origdata = pkey->key_data_contents[0];
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate 		    shortcopy1 = (krb5_octet *) malloc(shortlen);
18717c478bd9Sstevel@tonic-gate 		    if (shortcopy1) {
18727c478bd9Sstevel@tonic-gate 			shortcopy1[0] = origdata[3];
18737c478bd9Sstevel@tonic-gate 			shortcopy1[1] = origdata[2];
18747c478bd9Sstevel@tonic-gate 			memcpy(shortcopy1 + 2, origdata + 4, shortlen - 2);
18757c478bd9Sstevel@tonic-gate 			free(origdata);
18767c478bd9Sstevel@tonic-gate 			pkey->key_data_length[0] = shortlen;
18777c478bd9Sstevel@tonic-gate 			pkey->key_data_contents[0] = shortcopy1;
18787c478bd9Sstevel@tonic-gate 		    } else {
18797c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(no_mem_fmt), fname, *linenop);
18807c478bd9Sstevel@tonic-gate 			error++;
18817c478bd9Sstevel@tonic-gate 		    }
18827c478bd9Sstevel@tonic-gate 	    }
188356a424ccSmp153739 
18847c478bd9Sstevel@tonic-gate 	    /* Read principal attributes */
188556a424ccSmp153739 	    if (!error && (fscanf(filep,
188656a424ccSmp153739 				  "\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t",
18877c478bd9Sstevel@tonic-gate 				  &tmpint1, &dbent.max_life,
18887c478bd9Sstevel@tonic-gate 				  &dbent.max_renewable_life,
18897c478bd9Sstevel@tonic-gate 				  &tmpint2, &dbent.expiration,
18907c478bd9Sstevel@tonic-gate 				  &dbent.pw_expiration, &last_pwd_change,
18917c478bd9Sstevel@tonic-gate 				  &dbent.last_success, &dbent.last_failed,
18927c478bd9Sstevel@tonic-gate 				  &tmpint3) != 10)) {
18937c478bd9Sstevel@tonic-gate 		try2read = read_pr_data1;
18947c478bd9Sstevel@tonic-gate 		error++;
18957c478bd9Sstevel@tonic-gate 	    }
18967c478bd9Sstevel@tonic-gate 	    pkey->key_data_kvno = tmpint1;
18977c478bd9Sstevel@tonic-gate 	    dbent.fail_auth_count = tmpint3;
18987c478bd9Sstevel@tonic-gate 	    /* Read modifier name */
18997c478bd9Sstevel@tonic-gate 	    if (!error && read_string(filep,
19007c478bd9Sstevel@tonic-gate 				      mod_name,
19017c478bd9Sstevel@tonic-gate 				      mod_name_len,
19027c478bd9Sstevel@tonic-gate 				      linenop)) {
19037c478bd9Sstevel@tonic-gate 		try2read = read_mod_name;
19047c478bd9Sstevel@tonic-gate 		error++;
19057c478bd9Sstevel@tonic-gate 	    }
19067c478bd9Sstevel@tonic-gate 	    /* Read second set of attributes */
19077c478bd9Sstevel@tonic-gate 	    if (!error && (fscanf(filep, "\t%u\t%u\t%u\t",
19087c478bd9Sstevel@tonic-gate 				  &mod_date, &dbent.attributes,
19097c478bd9Sstevel@tonic-gate 				  &tmpint1) != 3)) {
19107c478bd9Sstevel@tonic-gate 		try2read = read_pr_data2;
19117c478bd9Sstevel@tonic-gate 		error++;
19127c478bd9Sstevel@tonic-gate 	    }
19137c478bd9Sstevel@tonic-gate 	    pkey->key_data_type[1] = tmpint1;
19147c478bd9Sstevel@tonic-gate 	    /* Read salt data */
19157c478bd9Sstevel@tonic-gate 	    if (!error && read_octet_string(filep,
19167c478bd9Sstevel@tonic-gate 					    pkey->key_data_contents[1],
19177c478bd9Sstevel@tonic-gate 					    pkey->key_data_length[1])) {
19187c478bd9Sstevel@tonic-gate 		try2read = read_salt_data;
19197c478bd9Sstevel@tonic-gate 		error++;
19207c478bd9Sstevel@tonic-gate 	    }
19217c478bd9Sstevel@tonic-gate 	    /* Read alternate key type */
192256a424ccSmp153739 	    if (!error && (fscanf(filep, "\t%u\t", &tmpint1) != 1)) {
19237c478bd9Sstevel@tonic-gate 		try2read = read_akey_type;
19247c478bd9Sstevel@tonic-gate 		error++;
19257c478bd9Sstevel@tonic-gate 	    }
19267c478bd9Sstevel@tonic-gate 	    akey->key_data_type[0] = tmpint1;
19277c478bd9Sstevel@tonic-gate 	    /* Read alternate key */
19287c478bd9Sstevel@tonic-gate 	    if (!error && read_octet_string(filep,
19297c478bd9Sstevel@tonic-gate 					    akey->key_data_contents[0],
19307c478bd9Sstevel@tonic-gate 					    akey->key_data_length[0])) {
19317c478bd9Sstevel@tonic-gate 		try2read = read_akey_data;
19327c478bd9Sstevel@tonic-gate 		error++;
19337c478bd9Sstevel@tonic-gate 	    }
193456a424ccSmp153739 
19357c478bd9Sstevel@tonic-gate 	    /* convert to a new format key */
193656a424ccSmp153739 	    /* the encrypted version is stored as the unencrypted key length
193756a424ccSmp153739 	       (4 bytes, MSB first) followed by the encrypted key. */
193856a424ccSmp153739 	    if ((akey->key_data_length[0] > 4)
193956a424ccSmp153739 		&& (akey->key_data_contents[0][0] == 0)
194056a424ccSmp153739 		&& (akey->key_data_contents[0][1] == 0)) {
194156a424ccSmp153739 	      /* this really does look like an old key, so drop and swap */
194256a424ccSmp153739 	      /* the *new* length is 2 bytes, LSB first, sigh. */
19437c478bd9Sstevel@tonic-gate 	      size_t shortlen = akey->key_data_length[0]-4+2;
19447c478bd9Sstevel@tonic-gate 
19457c478bd9Sstevel@tonic-gate 		    krb5_octet *origdata = akey->key_data_contents[0];
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate 		    shortcopy2 = (krb5_octet *) malloc(shortlen);
19487c478bd9Sstevel@tonic-gate 		    if (shortcopy2) {
19497c478bd9Sstevel@tonic-gate 			shortcopy2[0] = origdata[3];
19507c478bd9Sstevel@tonic-gate 			shortcopy2[1] = origdata[2];
19517c478bd9Sstevel@tonic-gate 			memcpy(shortcopy2 + 2,
19527c478bd9Sstevel@tonic-gate 			    origdata + 4, shortlen - 2);
19537c478bd9Sstevel@tonic-gate 			free(origdata);
19547c478bd9Sstevel@tonic-gate 			akey->key_data_length[0] = shortlen;
19557c478bd9Sstevel@tonic-gate 			akey->key_data_contents[0] = shortcopy2;
19567c478bd9Sstevel@tonic-gate 		    } else {
19577c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(no_mem_fmt), fname, *linenop);
19587c478bd9Sstevel@tonic-gate 			error++;
19597c478bd9Sstevel@tonic-gate 		    }
19607c478bd9Sstevel@tonic-gate 	    }
196156a424ccSmp153739 
19627c478bd9Sstevel@tonic-gate 	    /* Read alternate salt type */
196356a424ccSmp153739 	    if (!error && (fscanf(filep, "\t%u\t", &tmpint1) != 1)) {
19647c478bd9Sstevel@tonic-gate 		try2read = read_asalt_type;
19657c478bd9Sstevel@tonic-gate 		error++;
19667c478bd9Sstevel@tonic-gate 	    }
19677c478bd9Sstevel@tonic-gate 	    akey->key_data_type[1] = tmpint1;
19687c478bd9Sstevel@tonic-gate 	    /* Read alternate salt data */
19697c478bd9Sstevel@tonic-gate 	    if (!error && read_octet_string(filep,
19707c478bd9Sstevel@tonic-gate 					    akey->key_data_contents[1],
19717c478bd9Sstevel@tonic-gate 					    akey->key_data_length[1])) {
19727c478bd9Sstevel@tonic-gate 		try2read = read_asalt_data;
19737c478bd9Sstevel@tonic-gate 		error++;
19747c478bd9Sstevel@tonic-gate 	    }
19757c478bd9Sstevel@tonic-gate 	    /* Read expansion data - discard it */
19767c478bd9Sstevel@tonic-gate 	    if (!error) {
19777c478bd9Sstevel@tonic-gate 		for (i=0; i<8; i++) {
197856a424ccSmp153739 		    if (fscanf(filep, "\t%u", &tmpint1) != 1) {
19797c478bd9Sstevel@tonic-gate 			try2read = read_exp_data;
19807c478bd9Sstevel@tonic-gate 			error++;
19817c478bd9Sstevel@tonic-gate 			break;
19827c478bd9Sstevel@tonic-gate 		    }
19837c478bd9Sstevel@tonic-gate 		}
19847c478bd9Sstevel@tonic-gate 		if (!error)
19857c478bd9Sstevel@tonic-gate 		    find_record_end(filep, fname, *linenop);
19867c478bd9Sstevel@tonic-gate 	    }
198756a424ccSmp153739 
19887c478bd9Sstevel@tonic-gate 	    /*
198956a424ccSmp153739 	     * If no error, then we're done reading.  Now parse the names
199056a424ccSmp153739 	     * and store the database dbent.
19917c478bd9Sstevel@tonic-gate 	     */
19927c478bd9Sstevel@tonic-gate 	    if (!error) {
199356a424ccSmp153739 		if (!(kret = krb5_parse_name(kcontext,
199456a424ccSmp153739 					     name,
199556a424ccSmp153739 					     &dbent.princ))) {
199656a424ccSmp153739 		    if (!(kret = krb5_parse_name(kcontext,
199756a424ccSmp153739 						 mod_name,
199856a424ccSmp153739 						 &mod_princ))) {
199956a424ccSmp153739 			if (!(kret =
200056a424ccSmp153739 			      krb5_dbe_update_mod_princ_data(kcontext,
200156a424ccSmp153739 							     &dbent,
200256a424ccSmp153739 							     mod_date,
200356a424ccSmp153739 							     mod_princ)) &&
200456a424ccSmp153739 			    !(kret =
200556a424ccSmp153739 			      krb5_dbe_update_last_pwd_change(kcontext,
200656a424ccSmp153739 							      &dbent,
200756a424ccSmp153739 							      last_pwd_change))) {
200856a424ccSmp153739 			    int one = 1;
200956a424ccSmp153739 
201056a424ccSmp153739 			    dbent.len = KRB5_KDB_V1_BASE_LENGTH;
201156a424ccSmp153739 			    pkey->key_data_ver = (pkey->key_data_type[1] || pkey->key_data_length[1]) ?
201256a424ccSmp153739 				2 : 1;
201356a424ccSmp153739 			    akey->key_data_ver = (akey->key_data_type[1] || akey->key_data_length[1]) ?
201456a424ccSmp153739 				2 : 1;
201556a424ccSmp153739 			    if ((pkey->key_data_type[0] ==
201656a424ccSmp153739 				 akey->key_data_type[0]) &&
201756a424ccSmp153739 				(pkey->key_data_type[1] ==
201856a424ccSmp153739 				 akey->key_data_type[1]))
201956a424ccSmp153739 				dbent.n_key_data--;
202056a424ccSmp153739 			    else if ((akey->key_data_type[0] == 0)
202156a424ccSmp153739 				     && (akey->key_data_length[0] == 0)
202256a424ccSmp153739 				     && (akey->key_data_type[1] == 0)
202356a424ccSmp153739 				     && (akey->key_data_length[1] == 0))
202456a424ccSmp153739 			        dbent.n_key_data--;
202554925bf6Swillf 
202654925bf6Swillf 			    dbent.mask = KADM5_LOAD | KADM5_PRINCIPAL | KADM5_ATTRIBUTES |
202754925bf6Swillf 				KADM5_MAX_LIFE | KADM5_MAX_RLIFE | KADM5_KEY_DATA |
202854925bf6Swillf 				KADM5_PRINC_EXPIRE_TIME | KADM5_LAST_SUCCESS |
202954925bf6Swillf 				KADM5_LAST_FAILED | KADM5_FAIL_AUTH_COUNT;
203054925bf6Swillf 
203156a424ccSmp153739 			    if ((kret = krb5_db_put_principal(kcontext,
203256a424ccSmp153739 							      &dbent,
203356a424ccSmp153739 							      &one)) ||
203456a424ccSmp153739 				(one != 1)) {
203556a424ccSmp153739 				fprintf(stderr, gettext(store_err_fmt),
203656a424ccSmp153739 					fname, *linenop, name,
203756a424ccSmp153739 					error_message(kret));
203856a424ccSmp153739 				error++;
20397c478bd9Sstevel@tonic-gate 			    }
204056a424ccSmp153739 			    else {
204156a424ccSmp153739 				if (verbose)
204256a424ccSmp153739 				    fprintf(stderr,
204356a424ccSmp153739 							gettext(add_princ_fmt),
204456a424ccSmp153739 							name);
204556a424ccSmp153739 				retval = 0;
204656a424ccSmp153739 			    }
204756a424ccSmp153739 			    dbent.n_key_data = 2;
204856a424ccSmp153739 			}
204956a424ccSmp153739 			krb5_free_principal(kcontext, mod_princ);
205056a424ccSmp153739 		    }
205156a424ccSmp153739 		    else {
205256a424ccSmp153739 			fprintf(stderr,
205356a424ccSmp153739 				gettext(parse_err_fmt),
205456a424ccSmp153739 				fname, *linenop, mod_name,
205556a424ccSmp153739 				error_message(kret));
205656a424ccSmp153739 			error++;
205756a424ccSmp153739 		    }
205856a424ccSmp153739 		}
205956a424ccSmp153739 		else {
206056a424ccSmp153739 		    fprintf(stderr, gettext(parse_err_fmt),
206156a424ccSmp153739 			    fname, *linenop, name, error_message(kret));
206256a424ccSmp153739 		    error++;
206356a424ccSmp153739 		}
206456a424ccSmp153739 	    }
206556a424ccSmp153739 	    else {
206656a424ccSmp153739 	    fprintf(stderr, gettext(no_mem_fmt), fname, *linenop, try2read);
206756a424ccSmp153739 	    }
206856a424ccSmp153739 	}
206956a424ccSmp153739 	else {
207056a424ccSmp153739 		fprintf(stderr, gettext(read_err_fmt), fname, *linenop);
20717c478bd9Sstevel@tonic-gate 	}
20727c478bd9Sstevel@tonic-gate 
20737c478bd9Sstevel@tonic-gate 	krb5_db_free_principal(kcontext, &dbent, 1);
20747c478bd9Sstevel@tonic-gate 	if (mod_name)
20757c478bd9Sstevel@tonic-gate 	    free(mod_name);
20767c478bd9Sstevel@tonic-gate 	if (name)
20777c478bd9Sstevel@tonic-gate 	    free(name);
207856a424ccSmp153739     }
207956a424ccSmp153739     else {
20807c478bd9Sstevel@tonic-gate 	if (nmatched != EOF)
20817c478bd9Sstevel@tonic-gate 	   fprintf(stderr, gettext(rhead_err_fmt),
20827c478bd9Sstevel@tonic-gate 		fname, *linenop);
20837c478bd9Sstevel@tonic-gate 	else
20847c478bd9Sstevel@tonic-gate 	    retval = -1;
20857c478bd9Sstevel@tonic-gate     }
20867c478bd9Sstevel@tonic-gate 
20877c478bd9Sstevel@tonic-gate     if (shortcopy1)
20887c478bd9Sstevel@tonic-gate 	free(shortcopy1);
20897c478bd9Sstevel@tonic-gate     if (shortcopy2)
20907c478bd9Sstevel@tonic-gate 	free(shortcopy2);
20917c478bd9Sstevel@tonic-gate 
20927c478bd9Sstevel@tonic-gate     return(retval);
20937c478bd9Sstevel@tonic-gate }
20947c478bd9Sstevel@tonic-gate 
20957c478bd9Sstevel@tonic-gate /*
20967c478bd9Sstevel@tonic-gate  * process_k5beta6_record()	- Handle a dump record in krb5b6 format.
20977c478bd9Sstevel@tonic-gate  *
20987c478bd9Sstevel@tonic-gate  * Returns -1 for end of file, 0 for success and 1 for failure.
20997c478bd9Sstevel@tonic-gate  */
21007c478bd9Sstevel@tonic-gate static int
process_k5beta6_record(fname,kcontext,filep,verbose,linenop)210154925bf6Swillf process_k5beta6_record(fname, kcontext, filep, verbose, linenop)
21027c478bd9Sstevel@tonic-gate     char		*fname;
21037c478bd9Sstevel@tonic-gate     krb5_context	kcontext;
21047c478bd9Sstevel@tonic-gate     FILE		*filep;
21057c478bd9Sstevel@tonic-gate     int			verbose;
21067c478bd9Sstevel@tonic-gate     int			*linenop;
21077c478bd9Sstevel@tonic-gate {
21087c478bd9Sstevel@tonic-gate     int			retval;
21097c478bd9Sstevel@tonic-gate     krb5_db_entry	dbentry;
21107c478bd9Sstevel@tonic-gate     krb5_int32		t1, t2, t3, t4, t5, t6, t7, t8, t9;
21117c478bd9Sstevel@tonic-gate     int			nread;
21127c478bd9Sstevel@tonic-gate     int			error;
21137c478bd9Sstevel@tonic-gate     int			i, j, one;
21147c478bd9Sstevel@tonic-gate     char		*name;
21157c478bd9Sstevel@tonic-gate     krb5_key_data	*kp, *kdatap;
21167c478bd9Sstevel@tonic-gate     krb5_tl_data	**tlp, *tl;
21177c478bd9Sstevel@tonic-gate     krb5_octet		*op;
21187c478bd9Sstevel@tonic-gate     krb5_error_code	kret;
21197c478bd9Sstevel@tonic-gate     const char		*try2read;
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate     try2read = (char *) NULL;
21227c478bd9Sstevel@tonic-gate     memset((char *) &dbentry, 0, sizeof(dbentry));
21237c478bd9Sstevel@tonic-gate     (*linenop)++;
21247c478bd9Sstevel@tonic-gate     retval = 1;
21257c478bd9Sstevel@tonic-gate     name = (char *) NULL;
21267c478bd9Sstevel@tonic-gate     kp = (krb5_key_data *) NULL;
21277c478bd9Sstevel@tonic-gate     op = (krb5_octet *) NULL;
21287c478bd9Sstevel@tonic-gate     error = 0;
21297c478bd9Sstevel@tonic-gate     kret = 0;
21307c478bd9Sstevel@tonic-gate     nread = fscanf(filep, "%d\t%d\t%d\t%d\t%d\t", &t1, &t2, &t3, &t4, &t5);
21317c478bd9Sstevel@tonic-gate     if (nread == 5) {
21327c478bd9Sstevel@tonic-gate 	/* Get memory for flattened principal name */
21337c478bd9Sstevel@tonic-gate 	if (!(name = (char *) malloc((size_t) t2 + 1)))
21347c478bd9Sstevel@tonic-gate 	    error++;
21357c478bd9Sstevel@tonic-gate 
21367c478bd9Sstevel@tonic-gate 	/* Get memory for and form tagged data linked list */
21377c478bd9Sstevel@tonic-gate 	tlp = &dbentry.tl_data;
21387c478bd9Sstevel@tonic-gate 	for (i=0; i<t3; i++) {
213956a424ccSmp153739 	    if ((*tlp = (krb5_tl_data *) malloc(sizeof(krb5_tl_data)))) {
21407c478bd9Sstevel@tonic-gate 		memset(*tlp, 0, sizeof(krb5_tl_data));
21417c478bd9Sstevel@tonic-gate 		tlp = &((*tlp)->tl_data_next);
21427c478bd9Sstevel@tonic-gate 		dbentry.n_tl_data++;
214356a424ccSmp153739 	    }
214456a424ccSmp153739 	    else {
21457c478bd9Sstevel@tonic-gate 		error++;
21467c478bd9Sstevel@tonic-gate 		break;
21477c478bd9Sstevel@tonic-gate 	    }
21487c478bd9Sstevel@tonic-gate 	}
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate 	/* Get memory for key list */
21517c478bd9Sstevel@tonic-gate 	if (t4 && !(kp = (krb5_key_data *) malloc((size_t)
21527c478bd9Sstevel@tonic-gate 						  (t4*sizeof(krb5_key_data)))))
21537c478bd9Sstevel@tonic-gate 	    error++;
21547c478bd9Sstevel@tonic-gate 
21557c478bd9Sstevel@tonic-gate 	/* Get memory for extra data */
21567c478bd9Sstevel@tonic-gate 	if (t5 && !(op = (krb5_octet *) malloc((size_t) t5)))
21577c478bd9Sstevel@tonic-gate 	    error++;
21587c478bd9Sstevel@tonic-gate 
21597c478bd9Sstevel@tonic-gate 	if (!error) {
21607c478bd9Sstevel@tonic-gate 	    dbentry.len = t1;
21617c478bd9Sstevel@tonic-gate 	    dbentry.n_key_data = t4;
21627c478bd9Sstevel@tonic-gate 	    dbentry.e_length = t5;
21637c478bd9Sstevel@tonic-gate 	    if (kp) {
216456a424ccSmp153739 		memset(kp, 0, (size_t) (t4*sizeof(krb5_key_data)));
21657c478bd9Sstevel@tonic-gate 		dbentry.key_data = kp;
21667c478bd9Sstevel@tonic-gate 		kp = (krb5_key_data *) NULL;
21677c478bd9Sstevel@tonic-gate 	    }
21687c478bd9Sstevel@tonic-gate 	    if (op) {
21697c478bd9Sstevel@tonic-gate 		memset(op, 0, (size_t) t5);
21707c478bd9Sstevel@tonic-gate 		dbentry.e_data = op;
21717c478bd9Sstevel@tonic-gate 		op = (krb5_octet *) NULL;
21727c478bd9Sstevel@tonic-gate 	    }
217356a424ccSmp153739 
21747c478bd9Sstevel@tonic-gate 	    /* Read in and parse the principal name */
21757c478bd9Sstevel@tonic-gate 	    if (!read_string(filep, name, t2, linenop) &&
217656a424ccSmp153739 		!(kret = krb5_parse_name(kcontext, name, &dbentry.princ))) {
21777c478bd9Sstevel@tonic-gate 
21787c478bd9Sstevel@tonic-gate 		/* Get the fixed principal attributes */
217956a424ccSmp153739 		nread = fscanf(filep, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t",
218056a424ccSmp153739 			       &t2, &t3, &t4, &t5, &t6, &t7, &t8, &t9);
21817c478bd9Sstevel@tonic-gate 		if (nread == 8) {
21827c478bd9Sstevel@tonic-gate 		    dbentry.attributes = (krb5_flags) t2;
21837c478bd9Sstevel@tonic-gate 		    dbentry.max_life = (krb5_deltat) t3;
218456a424ccSmp153739 		    dbentry.max_renewable_life = (krb5_deltat) t4;
218556a424ccSmp153739 		    dbentry.expiration = (krb5_timestamp) t5;
218656a424ccSmp153739 		    dbentry.pw_expiration = (krb5_timestamp) t6;
218756a424ccSmp153739 		    dbentry.last_success = (krb5_timestamp) t7;
218856a424ccSmp153739 		    dbentry.last_failed = (krb5_timestamp) t8;
218956a424ccSmp153739 		    dbentry.fail_auth_count = (krb5_kvno) t9;
219054925bf6Swillf 		    dbentry.mask = KADM5_LOAD | KADM5_PRINCIPAL | KADM5_ATTRIBUTES |
219154925bf6Swillf 			KADM5_MAX_LIFE | KADM5_MAX_RLIFE |
219254925bf6Swillf 			KADM5_PRINC_EXPIRE_TIME | KADM5_LAST_SUCCESS |
219354925bf6Swillf 			KADM5_LAST_FAILED | KADM5_FAIL_AUTH_COUNT;
21947c478bd9Sstevel@tonic-gate 		} else {
21957c478bd9Sstevel@tonic-gate 		    try2read = read_nint_data;
21967c478bd9Sstevel@tonic-gate 		    error++;
21977c478bd9Sstevel@tonic-gate 		}
21987c478bd9Sstevel@tonic-gate 
21997c478bd9Sstevel@tonic-gate 		/*
22007c478bd9Sstevel@tonic-gate 		 * Get the tagged data.
22017c478bd9Sstevel@tonic-gate 		 *
220256a424ccSmp153739 		 * Really, this code ought to discard tl data types
220356a424ccSmp153739 		 * that it knows are special to the current version
220456a424ccSmp153739 		 * and were not supported in the previous version.
220556a424ccSmp153739 		 * But it's a pain to implement that here, and doing
220656a424ccSmp153739 		 * it at dump time has almost as good an effect, so
220756a424ccSmp153739 		 * that's what I did.  [krb5-admin/89]
22087c478bd9Sstevel@tonic-gate 		 */
22097c478bd9Sstevel@tonic-gate 		if (!error && dbentry.n_tl_data) {
221056a424ccSmp153739 		    for (tl = dbentry.tl_data; tl; tl = tl->tl_data_next) {
221156a424ccSmp153739 			nread = fscanf(filep, "%d\t%d\t", &t1, &t2);
221256a424ccSmp153739 			if (nread == 2) {
221356a424ccSmp153739 			    tl->tl_data_type = (krb5_int16) t1;
221456a424ccSmp153739 			    tl->tl_data_length = (krb5_int16) t2;
221556a424ccSmp153739 			    if (tl->tl_data_length) {
221656a424ccSmp153739 				if (!(tl->tl_data_contents =
221756a424ccSmp153739 				      (krb5_octet *) malloc((size_t) t2+1)) ||
221856a424ccSmp153739 				    read_octet_string(filep,
221956a424ccSmp153739 						      tl->tl_data_contents,
222056a424ccSmp153739 						      t2)) {
222156a424ccSmp153739 				    try2read = read_tcontents;
222256a424ccSmp153739 				    error++;
222356a424ccSmp153739 				    break;
22247c478bd9Sstevel@tonic-gate 				}
222554925bf6Swillf 				/* test to set mask fields */
222654925bf6Swillf 				if (t1 == KRB5_TL_KADM_DATA) {
222754925bf6Swillf 				    XDR xdrs;
222854925bf6Swillf 				    osa_princ_ent_rec osa_princ_ent;
222954925bf6Swillf 
223054925bf6Swillf 				    /*
223154925bf6Swillf 				     * Assuming aux_attributes will always be
223254925bf6Swillf 				     * there
223354925bf6Swillf 				     */
223454925bf6Swillf 				    dbentry.mask |= KADM5_AUX_ATTRIBUTES;
223554925bf6Swillf 
223654925bf6Swillf 				    /* test for an actual policy reference */
223754925bf6Swillf 				    memset(&osa_princ_ent, 0, sizeof(osa_princ_ent));
223854925bf6Swillf 				    xdrmem_create(&xdrs, (char *)tl->tl_data_contents,
223954925bf6Swillf 					    tl->tl_data_length, XDR_DECODE);
224054925bf6Swillf 				    if (xdr_osa_princ_ent_rec(&xdrs, &osa_princ_ent) &&
224154925bf6Swillf 					    (osa_princ_ent.aux_attributes & KADM5_POLICY) &&
224254925bf6Swillf 					    osa_princ_ent.policy != NULL) {
224354925bf6Swillf 
224454925bf6Swillf 					dbentry.mask |= KADM5_POLICY;
224554925bf6Swillf 					kdb_free_entry(NULL, NULL, &osa_princ_ent);
224654925bf6Swillf 				    }
224754925bf6Swillf 				    xdr_destroy(&xdrs);
224854925bf6Swillf 				}
224956a424ccSmp153739 			    }
225056a424ccSmp153739 			    else {
225156a424ccSmp153739 				/* Should be a null field */
225256a424ccSmp153739 				nread = fscanf(filep, "%d", &t9);
225356a424ccSmp153739 				if ((nread != 1) || (t9 != -1)) {
225456a424ccSmp153739 				    error++;
225556a424ccSmp153739 				    try2read = read_tcontents;
225656a424ccSmp153739 				    break;
225756a424ccSmp153739 				}
225856a424ccSmp153739 			    }
225956a424ccSmp153739 			}
226056a424ccSmp153739 			else {
226156a424ccSmp153739 			    try2read = read_ttypelen;
226256a424ccSmp153739 			    error++;
226356a424ccSmp153739 			    break;
226456a424ccSmp153739 			}
226556a424ccSmp153739 		    }
226654925bf6Swillf 		    if (!error)
226754925bf6Swillf 			dbentry.mask |= KADM5_TL_DATA;
226856a424ccSmp153739 		}
226956a424ccSmp153739 
22707c478bd9Sstevel@tonic-gate 		/* Get the key data */
22717c478bd9Sstevel@tonic-gate 		if (!error && dbentry.n_key_data) {
227256a424ccSmp153739 		    for (i=0; !error && (i<dbentry.n_key_data); i++) {
227356a424ccSmp153739 			kdatap = &dbentry.key_data[i];
227456a424ccSmp153739 			nread = fscanf(filep, "%d\t%d\t", &t1, &t2);
227556a424ccSmp153739 			if (nread == 2) {
227656a424ccSmp153739 			    kdatap->key_data_ver = (krb5_int16) t1;
227756a424ccSmp153739 			    kdatap->key_data_kvno = (krb5_int16) t2;
227856a424ccSmp153739 
227956a424ccSmp153739 			    for (j=0; j<t1; j++) {
228056a424ccSmp153739 				nread = fscanf(filep, "%d\t%d\t", &t3, &t4);
228156a424ccSmp153739 				if (nread == 2) {
228256a424ccSmp153739 				    kdatap->key_data_type[j] = t3;
228356a424ccSmp153739 				    kdatap->key_data_length[j] = t4;
228456a424ccSmp153739 				    if (t4) {
228556a424ccSmp153739 					if (!(kdatap->key_data_contents[j] =
228656a424ccSmp153739 					      (krb5_octet *)
228756a424ccSmp153739 					      malloc((size_t) t4+1)) ||
228856a424ccSmp153739 					    read_octet_string(filep,
228956a424ccSmp153739 							      kdatap->key_data_contents[j],
229056a424ccSmp153739 							      t4)) {
229156a424ccSmp153739 					    try2read = read_kcontents;
229256a424ccSmp153739 					    error++;
229356a424ccSmp153739 					    break;
22947c478bd9Sstevel@tonic-gate 					}
229556a424ccSmp153739 				    }
229656a424ccSmp153739 				    else {
229756a424ccSmp153739 					/* Should be a null field */
229856a424ccSmp153739 					nread = fscanf(filep, "%d", &t9);
229956a424ccSmp153739 					if ((nread != 1) || (t9 != -1)) {
230056a424ccSmp153739 					    error++;
230156a424ccSmp153739 					    try2read = read_kcontents;
230256a424ccSmp153739 					    break;
230356a424ccSmp153739 					}
230456a424ccSmp153739 				    }
230556a424ccSmp153739 				}
230656a424ccSmp153739 				else {
230756a424ccSmp153739 				    try2read = read_ktypelen;
230856a424ccSmp153739 				    error++;
230956a424ccSmp153739 				    break;
231056a424ccSmp153739 				}
231156a424ccSmp153739 			    }
231256a424ccSmp153739 			}
231356a424ccSmp153739 		    }
231454925bf6Swillf 		    if (!error)
231554925bf6Swillf 			dbentry.mask |= KADM5_KEY_DATA;
231656a424ccSmp153739 		}
231756a424ccSmp153739 
23187c478bd9Sstevel@tonic-gate 		/* Get the extra data */
23197c478bd9Sstevel@tonic-gate 		if (!error && dbentry.e_length) {
23207c478bd9Sstevel@tonic-gate 		    if (read_octet_string(filep,
23217c478bd9Sstevel@tonic-gate 					  dbentry.e_data,
23227c478bd9Sstevel@tonic-gate 					  (int) dbentry.e_length)) {
23237c478bd9Sstevel@tonic-gate 			try2read = read_econtents;
23247c478bd9Sstevel@tonic-gate 			error++;
23257c478bd9Sstevel@tonic-gate 		    }
232656a424ccSmp153739 		}
232756a424ccSmp153739 		else {
23287c478bd9Sstevel@tonic-gate 		    nread = fscanf(filep, "%d", &t9);
23297c478bd9Sstevel@tonic-gate 		    if ((nread != 1) || (t9 != -1)) {
23307c478bd9Sstevel@tonic-gate 			error++;
23317c478bd9Sstevel@tonic-gate 			try2read = read_econtents;
23327c478bd9Sstevel@tonic-gate 		    }
23337c478bd9Sstevel@tonic-gate 		}
23347c478bd9Sstevel@tonic-gate 
23357c478bd9Sstevel@tonic-gate 		/* Finally, find the end of the record. */
23367c478bd9Sstevel@tonic-gate 		if (!error)
23377c478bd9Sstevel@tonic-gate 		    find_record_end(filep, fname, *linenop);
23387c478bd9Sstevel@tonic-gate 
23397c478bd9Sstevel@tonic-gate 		/*
234056a424ccSmp153739 		 * We have either read in all the data or choked.
23417c478bd9Sstevel@tonic-gate 		 */
23427c478bd9Sstevel@tonic-gate 		if (!error) {
23437c478bd9Sstevel@tonic-gate 		    one = 1;
234456a424ccSmp153739 		    if ((kret = krb5_db_put_principal(kcontext,
23457c478bd9Sstevel@tonic-gate 						      &dbentry,
23467c478bd9Sstevel@tonic-gate 						      &one))) {
23477c478bd9Sstevel@tonic-gate 						fprintf(stderr,
23487c478bd9Sstevel@tonic-gate 						    gettext(store_err_fmt),
23497c478bd9Sstevel@tonic-gate 				fname, *linenop,
23507c478bd9Sstevel@tonic-gate 				name, error_message(kret));
235156a424ccSmp153739 		    }
235256a424ccSmp153739 		    else {
23537c478bd9Sstevel@tonic-gate 			if (verbose)
23547c478bd9Sstevel@tonic-gate 							fprintf(stderr,
23557c478bd9Sstevel@tonic-gate 							    gettext(
23567c478bd9Sstevel@tonic-gate 								add_princ_fmt),
23577c478bd9Sstevel@tonic-gate 							    name);
23587c478bd9Sstevel@tonic-gate 			retval = 0;
23597c478bd9Sstevel@tonic-gate 		    }
236056a424ccSmp153739 		}
236156a424ccSmp153739 		else {
23627c478bd9Sstevel@tonic-gate 					fprintf(stderr, gettext(read_err_fmt),
23637c478bd9Sstevel@tonic-gate 					    fname, *linenop, try2read);
23647c478bd9Sstevel@tonic-gate 		}
236556a424ccSmp153739 	    }
236656a424ccSmp153739 	    else {
23677c478bd9Sstevel@tonic-gate 		if (kret)
23687c478bd9Sstevel@tonic-gate 					fprintf(stderr, gettext(parse_err_fmt),
236956a424ccSmp153739 			    fname, *linenop, name, error_message(kret));
23707c478bd9Sstevel@tonic-gate 		else
23717c478bd9Sstevel@tonic-gate 		    fprintf(stderr, gettext(no_mem_fmt),
23727c478bd9Sstevel@tonic-gate 						fname, *linenop);
23737c478bd9Sstevel@tonic-gate 	    }
237456a424ccSmp153739 	}
237556a424ccSmp153739 	else {
23767c478bd9Sstevel@tonic-gate 	    fprintf(stderr,
23777c478bd9Sstevel@tonic-gate 				gettext(rhead_err_fmt), fname, *linenop);
23787c478bd9Sstevel@tonic-gate 	}
23797c478bd9Sstevel@tonic-gate 
23807c478bd9Sstevel@tonic-gate 	if (op)
23817c478bd9Sstevel@tonic-gate 	    free(op);
23827c478bd9Sstevel@tonic-gate 	if (kp)
23837c478bd9Sstevel@tonic-gate 	    free(kp);
23847c478bd9Sstevel@tonic-gate 	if (name)
23857c478bd9Sstevel@tonic-gate 	    free(name);
23867c478bd9Sstevel@tonic-gate 	krb5_db_free_principal(kcontext, &dbentry, 1);
238756a424ccSmp153739     }
238856a424ccSmp153739     else {
23897c478bd9Sstevel@tonic-gate 	if (nread == EOF)
23907c478bd9Sstevel@tonic-gate 	    retval = -1;
23917c478bd9Sstevel@tonic-gate     }
23927c478bd9Sstevel@tonic-gate     return(retval);
23937c478bd9Sstevel@tonic-gate }
23947c478bd9Sstevel@tonic-gate 
239556a424ccSmp153739 static int
process_k5beta7_policy(fname,kcontext,filep,verbose,linenop,pol_db)23967c478bd9Sstevel@tonic-gate process_k5beta7_policy(fname, kcontext, filep, verbose, linenop, pol_db)
23977c478bd9Sstevel@tonic-gate     char		*fname;
23987c478bd9Sstevel@tonic-gate     krb5_context	kcontext;
23997c478bd9Sstevel@tonic-gate     FILE		*filep;
24007c478bd9Sstevel@tonic-gate     int			verbose;
24017c478bd9Sstevel@tonic-gate     int			*linenop;
24027c478bd9Sstevel@tonic-gate     void *pol_db;
24037c478bd9Sstevel@tonic-gate {
24047c478bd9Sstevel@tonic-gate     osa_policy_ent_rec rec;
24057c478bd9Sstevel@tonic-gate     char namebuf[1024];
24067c478bd9Sstevel@tonic-gate     int nread, ret;
24077c478bd9Sstevel@tonic-gate 
24087c478bd9Sstevel@tonic-gate     (*linenop)++;
24097c478bd9Sstevel@tonic-gate     rec.name = namebuf;
24107c478bd9Sstevel@tonic-gate 
24117c478bd9Sstevel@tonic-gate     nread = fscanf(filep, "%1024s\t%d\t%d\t%d\t%d\t%d\t%d", rec.name,
24127c478bd9Sstevel@tonic-gate 		   &rec.pw_min_life, &rec.pw_max_life,
24137c478bd9Sstevel@tonic-gate 		   &rec.pw_min_length, &rec.pw_min_classes,
24147c478bd9Sstevel@tonic-gate 		   &rec.pw_history_num, &rec.policy_refcnt);
24157c478bd9Sstevel@tonic-gate     if (nread == EOF)
241656a424ccSmp153739 	 return -1;
24177c478bd9Sstevel@tonic-gate     else if (nread != 7) {
24187c478bd9Sstevel@tonic-gate 		fprintf(stderr,
24197c478bd9Sstevel@tonic-gate 		    gettext("cannot parse policy on line %d (%d read)\n"),
24207c478bd9Sstevel@tonic-gate 		 *linenop, nread);
242156a424ccSmp153739 	 return 1;
24227c478bd9Sstevel@tonic-gate     }
24237c478bd9Sstevel@tonic-gate 
242454925bf6Swillf     if ((ret = krb5_db_create_policy(kcontext, &rec))) {
242554925bf6Swillf 	 if (ret &&
242654925bf6Swillf 	     ((ret = krb5_db_put_policy(kcontext, &rec)))) {
24277c478bd9Sstevel@tonic-gate 	      fprintf(stderr, gettext("cannot create policy on line %d: %s\n"),
24287c478bd9Sstevel@tonic-gate 		      *linenop, error_message(ret));
242956a424ccSmp153739 	      return 1;
24307c478bd9Sstevel@tonic-gate 	 }
24317c478bd9Sstevel@tonic-gate     }
24327c478bd9Sstevel@tonic-gate     if (verbose)
24337c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("created policy %s\n"), rec.name);
24347c478bd9Sstevel@tonic-gate 
243556a424ccSmp153739     return 0;
24367c478bd9Sstevel@tonic-gate }
24377c478bd9Sstevel@tonic-gate 
24387c478bd9Sstevel@tonic-gate /*
243956a424ccSmp153739  * process_k5beta7_record()	- Handle a dump record in krb5b7 format.
24407c478bd9Sstevel@tonic-gate  *
24417c478bd9Sstevel@tonic-gate  * Returns -1 for end of file, 0 for success and 1 for failure.
24427c478bd9Sstevel@tonic-gate  */
24437c478bd9Sstevel@tonic-gate static int
process_k5beta7_record(fname,kcontext,filep,verbose,linenop)244454925bf6Swillf process_k5beta7_record(fname, kcontext, filep, verbose, linenop)
24457c478bd9Sstevel@tonic-gate     char		*fname;
24467c478bd9Sstevel@tonic-gate     krb5_context	kcontext;
24477c478bd9Sstevel@tonic-gate     FILE		*filep;
24487c478bd9Sstevel@tonic-gate     int			verbose;
24497c478bd9Sstevel@tonic-gate     int			*linenop;
24507c478bd9Sstevel@tonic-gate {
24517c478bd9Sstevel@tonic-gate      int nread;
24527c478bd9Sstevel@tonic-gate      char rectype[100];
24537c478bd9Sstevel@tonic-gate 
24547c478bd9Sstevel@tonic-gate      nread = fscanf(filep, "%100s\t", rectype);
24557c478bd9Sstevel@tonic-gate      if (nread == EOF)
245656a424ccSmp153739 	  return -1;
24577c478bd9Sstevel@tonic-gate      else if (nread != 1)
245856a424ccSmp153739 	  return 1;
24597c478bd9Sstevel@tonic-gate      if (strcmp(rectype, "princ") == 0)
24607c478bd9Sstevel@tonic-gate 	  process_k5beta6_record(fname, kcontext, filep, verbose,
246154925bf6Swillf 				 linenop);
24627c478bd9Sstevel@tonic-gate      else if (strcmp(rectype, "policy") == 0)
24637c478bd9Sstevel@tonic-gate 	  process_k5beta7_policy(fname, kcontext, filep, verbose,
246454925bf6Swillf 				 linenop);
24657c478bd9Sstevel@tonic-gate      else {
24667c478bd9Sstevel@tonic-gate 		fprintf(stderr,
24677c478bd9Sstevel@tonic-gate 		    gettext("unknown record type \"%s\" on line %d\n"),
24687c478bd9Sstevel@tonic-gate 		  rectype, *linenop);
246956a424ccSmp153739 	  return 1;
24707c478bd9Sstevel@tonic-gate      }
24717c478bd9Sstevel@tonic-gate 
247256a424ccSmp153739      return 0;
24737c478bd9Sstevel@tonic-gate }
24747c478bd9Sstevel@tonic-gate 
24757c478bd9Sstevel@tonic-gate /*
24767c478bd9Sstevel@tonic-gate  * process_ov_record()	- Handle a dump record in OpenV*Secure 1.0 format.
24777c478bd9Sstevel@tonic-gate  *
24787c478bd9Sstevel@tonic-gate  * Returns -1 for end of file, 0 for success and 1 for failure.
24797c478bd9Sstevel@tonic-gate  */
24807c478bd9Sstevel@tonic-gate static int
process_ov_record(fname,kcontext,filep,verbose,linenop)248154925bf6Swillf process_ov_record(fname, kcontext, filep, verbose, linenop)
24827c478bd9Sstevel@tonic-gate     char		*fname;
24837c478bd9Sstevel@tonic-gate     krb5_context	kcontext;
24847c478bd9Sstevel@tonic-gate     FILE		*filep;
24857c478bd9Sstevel@tonic-gate     int			verbose;
24867c478bd9Sstevel@tonic-gate     int			*linenop;
24877c478bd9Sstevel@tonic-gate {
24887c478bd9Sstevel@tonic-gate      int nread;
24897c478bd9Sstevel@tonic-gate      char rectype[100];
24907c478bd9Sstevel@tonic-gate 
24917c478bd9Sstevel@tonic-gate      nread = fscanf(filep, "%100s\t", rectype);
24927c478bd9Sstevel@tonic-gate      if (nread == EOF)
249356a424ccSmp153739 	  return -1;
24947c478bd9Sstevel@tonic-gate      else if (nread != 1)
249556a424ccSmp153739 	  return 1;
24967c478bd9Sstevel@tonic-gate      if (strcmp(rectype, "princ") == 0)
24977c478bd9Sstevel@tonic-gate 	  process_ov_principal(fname, kcontext, filep, verbose,
249854925bf6Swillf 			       linenop);
24997c478bd9Sstevel@tonic-gate      else if (strcmp(rectype, "policy") == 0)
25007c478bd9Sstevel@tonic-gate 	  process_k5beta7_policy(fname, kcontext, filep, verbose,
250154925bf6Swillf 				 linenop);
25027c478bd9Sstevel@tonic-gate      else if (strcmp(rectype, "End") == 0)
250356a424ccSmp153739 	  return -1;
25047c478bd9Sstevel@tonic-gate      else {
25057c478bd9Sstevel@tonic-gate 		fprintf(stderr,
25067c478bd9Sstevel@tonic-gate 		    gettext("unknown record type \"%s\" on line %d\n"),
25077c478bd9Sstevel@tonic-gate 		  rectype, *linenop);
250856a424ccSmp153739 	  return 1;
25097c478bd9Sstevel@tonic-gate      }
25107c478bd9Sstevel@tonic-gate 
251156a424ccSmp153739      return 0;
25127c478bd9Sstevel@tonic-gate }
25137c478bd9Sstevel@tonic-gate 
25147c478bd9Sstevel@tonic-gate /*
25157c478bd9Sstevel@tonic-gate  * restore_dump()	- Restore the database from any version dump file.
25167c478bd9Sstevel@tonic-gate  */
25177c478bd9Sstevel@tonic-gate static int
restore_dump(programname,kcontext,dumpfile,f,verbose,dump)251854925bf6Swillf restore_dump(programname, kcontext, dumpfile, f, verbose, dump)
25197c478bd9Sstevel@tonic-gate     char		*programname;
25207c478bd9Sstevel@tonic-gate     krb5_context	kcontext;
25217c478bd9Sstevel@tonic-gate     char		*dumpfile;
25227c478bd9Sstevel@tonic-gate     FILE		*f;
25237c478bd9Sstevel@tonic-gate     int			verbose;
25247c478bd9Sstevel@tonic-gate     dump_version	*dump;
25257c478bd9Sstevel@tonic-gate {
25267c478bd9Sstevel@tonic-gate     int		error;
25277c478bd9Sstevel@tonic-gate     int		lineno;
25287c478bd9Sstevel@tonic-gate 
25297c478bd9Sstevel@tonic-gate     error = 0;
25307c478bd9Sstevel@tonic-gate     lineno = 1;
25317c478bd9Sstevel@tonic-gate 
25327c478bd9Sstevel@tonic-gate     /*
25337c478bd9Sstevel@tonic-gate      * Process the records.
25347c478bd9Sstevel@tonic-gate      */
25357c478bd9Sstevel@tonic-gate     while (!(error = (*dump->load_record)(dumpfile,
25367c478bd9Sstevel@tonic-gate 					  kcontext,
25377c478bd9Sstevel@tonic-gate 					  f,
25387c478bd9Sstevel@tonic-gate 					  verbose,
253954925bf6Swillf 					  &lineno)))
254056a424ccSmp153739 	 ;
25417c478bd9Sstevel@tonic-gate     if (error != -1)
25427c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(err_line_fmt),
25437c478bd9Sstevel@tonic-gate 		    programname, lineno, dumpfile);
25447c478bd9Sstevel@tonic-gate     else
25457c478bd9Sstevel@tonic-gate 	 error = 0;
25467c478bd9Sstevel@tonic-gate 
25477c478bd9Sstevel@tonic-gate     return(error);
25487c478bd9Sstevel@tonic-gate }
25497c478bd9Sstevel@tonic-gate 
25507c478bd9Sstevel@tonic-gate /*
255156a424ccSmp153739  * Usage: load_db [-i] [-old] [-ov] [-b6] [-b7] [-verbose] [-update] [-hash]
255256a424ccSmp153739  *		filename
25537c478bd9Sstevel@tonic-gate  */
25547c478bd9Sstevel@tonic-gate void
load_db(argc,argv)25557c478bd9Sstevel@tonic-gate load_db(argc, argv)
25567c478bd9Sstevel@tonic-gate     int		argc;
25577c478bd9Sstevel@tonic-gate     char	**argv;
25587c478bd9Sstevel@tonic-gate {
25597c478bd9Sstevel@tonic-gate     kadm5_config_params newparams;
25607c478bd9Sstevel@tonic-gate     krb5_error_code	kret;
25617c478bd9Sstevel@tonic-gate     krb5_context	kcontext;
25627c478bd9Sstevel@tonic-gate     FILE		*f;
25637c478bd9Sstevel@tonic-gate     extern char		*optarg;
25647c478bd9Sstevel@tonic-gate     extern int		optind;
2565dd9ccd46S /* Solaris Kerberos */
2566dd9ccd46S #if 0
25677c478bd9Sstevel@tonic-gate     char		*programname;
2568dd9ccd46S #endif
25697c478bd9Sstevel@tonic-gate     char		*dumpfile;
25707c478bd9Sstevel@tonic-gate     char		*dbname;
25717c478bd9Sstevel@tonic-gate     char		*dbname_tmp;
25727c478bd9Sstevel@tonic-gate     char		buf[BUFSIZ];
25737c478bd9Sstevel@tonic-gate     dump_version	*load;
25747c478bd9Sstevel@tonic-gate     int			update, verbose;
25757c478bd9Sstevel@tonic-gate     int			aindex;
25767c478bd9Sstevel@tonic-gate     bool_t		add_update = TRUE;
25777c478bd9Sstevel@tonic-gate     char		iheader[MAX_HEADER];
25787c478bd9Sstevel@tonic-gate     uint32_t		caller, last_sno, last_seconds, last_useconds;
25797c478bd9Sstevel@tonic-gate     kdb_log_context	*log_ctx;
258054925bf6Swillf     int			db_locked = 0;
25817c478bd9Sstevel@tonic-gate 
25827c478bd9Sstevel@tonic-gate     /*
25837c478bd9Sstevel@tonic-gate      * Parse the arguments.
25847c478bd9Sstevel@tonic-gate      */
2585dd9ccd46S /* Solaris Kerberos */
2586dd9ccd46S #if 0
25877c478bd9Sstevel@tonic-gate     programname = argv[0];
25887c478bd9Sstevel@tonic-gate     if (strrchr(programname, (int) '/'))
25897c478bd9Sstevel@tonic-gate 	programname = strrchr(argv[0], (int) '/') + 1;
2590dd9ccd46S #endif
25917c478bd9Sstevel@tonic-gate     dumpfile = (char *) NULL;
25927c478bd9Sstevel@tonic-gate     dbname = global_params.dbname;
25937c478bd9Sstevel@tonic-gate     load = NULL;
25947c478bd9Sstevel@tonic-gate     update = 0;
25957c478bd9Sstevel@tonic-gate     verbose = 0;
25967c478bd9Sstevel@tonic-gate     exit_status = 0;
25977c478bd9Sstevel@tonic-gate     dbname_tmp = (char *) NULL;
25987c478bd9Sstevel@tonic-gate     log_ctx = util_context->kdblog_context;
25997c478bd9Sstevel@tonic-gate 
26007c478bd9Sstevel@tonic-gate     for (aindex = 1; aindex < argc; aindex++) {
260156a424ccSmp153739 	if (!strcmp(argv[aindex], oldoption))
26027c478bd9Sstevel@tonic-gate 	     load = &old_version;
260356a424ccSmp153739 	else if (!strcmp(argv[aindex], b6option))
26047c478bd9Sstevel@tonic-gate 	     load = &beta6_version;
260556a424ccSmp153739 	else if (!strcmp(argv[aindex], b7option))
260656a424ccSmp153739 	     load = &beta7_version;
260756a424ccSmp153739 	else if (!strcmp(argv[aindex], ovoption))
26087c478bd9Sstevel@tonic-gate 	     load = &ov_version;
26097c478bd9Sstevel@tonic-gate 	else if (!strcmp(argv[aindex], ipropoption)) {
26107c478bd9Sstevel@tonic-gate 			if (log_ctx && log_ctx->iproprole) {
26117c478bd9Sstevel@tonic-gate 				load = &iprop_version;
26127c478bd9Sstevel@tonic-gate 				add_update = FALSE;
26137c478bd9Sstevel@tonic-gate 			} else {
26147c478bd9Sstevel@tonic-gate 				fprintf(stderr, gettext("Iprop not enabled\n"));
26157c478bd9Sstevel@tonic-gate 				exit_status++;
26167c478bd9Sstevel@tonic-gate 				return;
26177c478bd9Sstevel@tonic-gate 			}
261856a424ccSmp153739 		}
261956a424ccSmp153739 	else if (!strcmp(argv[aindex], verboseoption))
26207c478bd9Sstevel@tonic-gate 	    verbose = 1;
262156a424ccSmp153739 	else if (!strcmp(argv[aindex], updateoption))
26227c478bd9Sstevel@tonic-gate 	    update = 1;
262354925bf6Swillf 	else if (!strcmp(argv[aindex], hashoption)) {
262454925bf6Swillf 	    if (!add_db_arg("hash=true")) {
262554925bf6Swillf 		com_err(progname, ENOMEM, "while parsing command arguments\n");
262654925bf6Swillf 		exit(1);
262754925bf6Swillf 	    }
262854925bf6Swillf 	} else
26297c478bd9Sstevel@tonic-gate 	    break;
26307c478bd9Sstevel@tonic-gate     }
26317c478bd9Sstevel@tonic-gate     if ((argc - aindex) != 1) {
26327c478bd9Sstevel@tonic-gate 	usage();
26337c478bd9Sstevel@tonic-gate 	return;
26347c478bd9Sstevel@tonic-gate     }
26357c478bd9Sstevel@tonic-gate     dumpfile = argv[aindex];
26367c478bd9Sstevel@tonic-gate 
26377c478bd9Sstevel@tonic-gate     if (!(dbname_tmp = (char *) malloc(strlen(dbname)+
26387c478bd9Sstevel@tonic-gate 				       strlen(dump_tmptrail)+1))) {
2639dd9ccd46S 		/* Solaris Kerberos */
2640dd9ccd46S 		fprintf(stderr, gettext(no_name_mem_fmt), progname);
26417c478bd9Sstevel@tonic-gate 	exit_status++;
26427c478bd9Sstevel@tonic-gate 	return;
26437c478bd9Sstevel@tonic-gate     }
26447c478bd9Sstevel@tonic-gate     strcpy(dbname_tmp, dbname);
26457c478bd9Sstevel@tonic-gate     strcat(dbname_tmp, dump_tmptrail);
26467c478bd9Sstevel@tonic-gate 
26477c478bd9Sstevel@tonic-gate     /*
26487c478bd9Sstevel@tonic-gate      * Initialize the Kerberos context and error tables.
26497c478bd9Sstevel@tonic-gate      */
265054925bf6Swillf     if ((kret = kadm5_init_krb5_context(&kcontext))) {
2651dd9ccd46S 	/* Solaris Kerberos */
2652dd9ccd46S 	fprintf(stderr, gettext(ctx_err_fmt), progname);
26537c478bd9Sstevel@tonic-gate 	free(dbname_tmp);
26547c478bd9Sstevel@tonic-gate 	exit_status++;
26557c478bd9Sstevel@tonic-gate 	return;
26567c478bd9Sstevel@tonic-gate     }
26577c478bd9Sstevel@tonic-gate 
265854925bf6Swillf     if( (kret = krb5_set_default_realm(kcontext, util_context->default_realm)) )
265954925bf6Swillf     {
2660dd9ccd46S 	/* Solaris Kerberos */
2661dd9ccd46S 	fprintf(stderr, gettext("%s: Unable to set the default realm\n"), progname);
266254925bf6Swillf 	free(dbname_tmp);
266354925bf6Swillf 	exit_status++;
266454925bf6Swillf 	return;
266554925bf6Swillf     }
26667c478bd9Sstevel@tonic-gate     if (log_ctx && log_ctx->iproprole)
26677c478bd9Sstevel@tonic-gate 	kcontext->kdblog_context = (void *)log_ctx;
26687c478bd9Sstevel@tonic-gate     /*
26697c478bd9Sstevel@tonic-gate      * Open the dumpfile
26707c478bd9Sstevel@tonic-gate      */
26717c478bd9Sstevel@tonic-gate     if (dumpfile) {
267254925bf6Swillf 	if ((f = fopen(dumpfile, "r")) == NULL) {
2673dd9ccd46S 			/* Solaris Kerberos */
26747c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(dfile_err_fmt),
2675dd9ccd46S 			    progname, dumpfile,
26767c478bd9Sstevel@tonic-gate 		     error_message(errno));
26777c478bd9Sstevel@tonic-gate 	     exit_status++;
26787c478bd9Sstevel@tonic-gate 	     return;
26797c478bd9Sstevel@tonic-gate 	}
26807c478bd9Sstevel@tonic-gate 	if ((kret = krb5_lock_file(kcontext, fileno(f),
26817c478bd9Sstevel@tonic-gate 				   KRB5_LOCKMODE_SHARED))) {
2682dd9ccd46S 	     /* Solaris Kerberos */
2683dd9ccd46S 	     fprintf(stderr, gettext("%s: Cannot lock %s: %s\n"), progname,
26847c478bd9Sstevel@tonic-gate 		     dumpfile, error_message(errno));
26857c478bd9Sstevel@tonic-gate 	     exit_status++;
26867c478bd9Sstevel@tonic-gate 	     return;
26877c478bd9Sstevel@tonic-gate 	}
26887c478bd9Sstevel@tonic-gate     } else
26897c478bd9Sstevel@tonic-gate 	f = stdin;
26907c478bd9Sstevel@tonic-gate 
26917c478bd9Sstevel@tonic-gate     /*
269256a424ccSmp153739      * Auto-detect dump version if we weren't told, verify if we
269356a424ccSmp153739      * were told.
26947c478bd9Sstevel@tonic-gate      */
26957c478bd9Sstevel@tonic-gate     fgets(buf, sizeof(buf), f);
26967c478bd9Sstevel@tonic-gate     if (load) {
269756a424ccSmp153739 	 /* only check what we know; some headers only contain a prefix */
26987c478bd9Sstevel@tonic-gate 	 if (strncmp(buf, load->header, strlen(load->header)) != 0) {
2699dd9ccd46S 			/* Solaris Kerberos */
2700dd9ccd46S 			fprintf(stderr, gettext(head_bad_fmt), progname, dumpfile);
27017c478bd9Sstevel@tonic-gate 	      exit_status++;
270256a424ccSmp153739 	      if (dumpfile) fclose(f);
27037c478bd9Sstevel@tonic-gate 	      return;
27047c478bd9Sstevel@tonic-gate 	 }
27057c478bd9Sstevel@tonic-gate     } else {
27067c478bd9Sstevel@tonic-gate 	 /* perhaps this should be in an array, but so what? */
27077c478bd9Sstevel@tonic-gate 	 if (strcmp(buf, old_version.header) == 0)
27087c478bd9Sstevel@tonic-gate 	      load = &old_version;
27097c478bd9Sstevel@tonic-gate 	 else if (strcmp(buf, beta6_version.header) == 0)
27107c478bd9Sstevel@tonic-gate 	      load = &beta6_version;
27117c478bd9Sstevel@tonic-gate 	 else if (strcmp(buf, beta7_version.header) == 0)
27127c478bd9Sstevel@tonic-gate 	      load = &beta7_version;
271356a424ccSmp153739 	 else if (strcmp(buf, r1_3_version.header) == 0)
271456a424ccSmp153739 	      load = &r1_3_version;
27157c478bd9Sstevel@tonic-gate 	 else if (strncmp(buf, ov_version.header,
27167c478bd9Sstevel@tonic-gate 			  strlen(ov_version.header)) == 0)
27177c478bd9Sstevel@tonic-gate 	      load = &ov_version;
27187c478bd9Sstevel@tonic-gate 	 else {
2719dd9ccd46S 			/* Solaris Kerberos */
27207c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(head_bad_fmt),
2721dd9ccd46S 				progname, dumpfile);
27227c478bd9Sstevel@tonic-gate 	      exit_status++;
272356a424ccSmp153739 	      if (dumpfile) fclose(f);
27247c478bd9Sstevel@tonic-gate 	      return;
27257c478bd9Sstevel@tonic-gate 	 }
27267c478bd9Sstevel@tonic-gate     }
27277c478bd9Sstevel@tonic-gate     if (load->updateonly && !update) {
2728dd9ccd46S 		/* Solaris Kerberos */
27297c478bd9Sstevel@tonic-gate 		fprintf(stderr,
27307c478bd9Sstevel@tonic-gate 		    gettext("%s: dump version %s can only "
27317c478bd9Sstevel@tonic-gate 			"be loaded with the -update flag\n"),
2732dd9ccd46S 		    progname, load->name);
27337c478bd9Sstevel@tonic-gate 	 exit_status++;
27347c478bd9Sstevel@tonic-gate 	 return;
27357c478bd9Sstevel@tonic-gate     }
273656a424ccSmp153739 
27377c478bd9Sstevel@tonic-gate     /*
27387c478bd9Sstevel@tonic-gate      * Cons up params for the new databases.  If we are not in update
273954925bf6Swillf      * mode, we create an alternate database and then promote it to
274054925bf6Swillf      * be the live db.
27417c478bd9Sstevel@tonic-gate      */
27427c478bd9Sstevel@tonic-gate     newparams = global_params;
27437c478bd9Sstevel@tonic-gate     if (! update) {
27447c478bd9Sstevel@tonic-gate 	 newparams.mask |= KADM5_CONFIG_DBNAME;
27457c478bd9Sstevel@tonic-gate 	 newparams.dbname = dbname_tmp;
27467c478bd9Sstevel@tonic-gate 
2747159d09a2SMark Phalan 	 if ((kret = kadm5_get_config_params(kcontext, 1,
27487c478bd9Sstevel@tonic-gate 					     &newparams, &newparams))) {
2749dd9ccd46S 	      /* Solaris Kerberos */
2750dd9ccd46S 	      com_err(progname, kret,
27517c478bd9Sstevel@tonic-gate 			    gettext("while retreiving new "
27527c478bd9Sstevel@tonic-gate 				"configuration parameters"));
27537c478bd9Sstevel@tonic-gate 	      exit_status++;
27547c478bd9Sstevel@tonic-gate 	      return;
27557c478bd9Sstevel@tonic-gate 	 }
275654925bf6Swillf 
275754925bf6Swillf 	 if (!add_db_arg("temporary")) {
275854925bf6Swillf 	     com_err(progname, ENOMEM, "computing parameters for database");
275954925bf6Swillf 	     exit(1);
276054925bf6Swillf 	 }
27617c478bd9Sstevel@tonic-gate     }
276256a424ccSmp153739 
27637c478bd9Sstevel@tonic-gate     /*
276454925bf6Swillf      * If not an update restoration, create the database. otherwise open
27657c478bd9Sstevel@tonic-gate      */
276654925bf6Swillf     if (!update) {
276754925bf6Swillf 	if((kret = krb5_db_create(kcontext, db5util_db_args))) {
276854925bf6Swillf 	    const char *emsg = krb5_get_error_message(kcontext, kret);
276954925bf6Swillf 	    /*
277054925bf6Swillf 	     * See if something (like DAL KDB plugin) has set a specific error
277154925bf6Swillf 	     * message and use that otherwise use default.
277254925bf6Swillf 	     */
277354925bf6Swillf 
277454925bf6Swillf 	    if (emsg != NULL) {
2775dd9ccd46S 		/* Solaris Kerberos */
2776dd9ccd46S 		fprintf(stderr, "%s: %s\n", progname, emsg);
277754925bf6Swillf 		krb5_free_error_message (kcontext, emsg);
277854925bf6Swillf 	    } else {
2779dd9ccd46S 		/* Solaris Kerberos */
278054925bf6Swillf 		fprintf(stderr, dbcreaterr_fmt,
2781dd9ccd46S 			progname, dbname, error_message(kret));
278254925bf6Swillf 	    }
27837c478bd9Sstevel@tonic-gate 	    exit_status++;
27847c478bd9Sstevel@tonic-gate 	    kadm5_free_config_params(kcontext, &newparams);
27857c478bd9Sstevel@tonic-gate 	    if (dumpfile) fclose(f);
27867c478bd9Sstevel@tonic-gate 	    return;
27877c478bd9Sstevel@tonic-gate 	}
278854925bf6Swillf     }
278954925bf6Swillf     else {
279054925bf6Swillf 	    /*
279154925bf6Swillf 	     * Initialize the database.
279254925bf6Swillf 	     */
279354925bf6Swillf 	    if ((kret = krb5_db_open(kcontext, db5util_db_args,
279454925bf6Swillf 				     KRB5_KDB_OPEN_RW | KRB5_KDB_SRV_TYPE_ADMIN))) {
279554925bf6Swillf 		const char *emsg = krb5_get_error_message(kcontext, kret);
279654925bf6Swillf 		/*
279754925bf6Swillf 		 * See if something (like DAL KDB plugin) has set a specific
279854925bf6Swillf 		 * error message and use that otherwise use default.
279954925bf6Swillf 		 */
280054925bf6Swillf 
280154925bf6Swillf 		if (emsg != NULL) {
2802dd9ccd46S 		    /* Solaris Kerberos */
2803dd9ccd46S 		    fprintf(stderr, "%s: %s\n", progname, emsg);
280454925bf6Swillf 		    krb5_free_error_message (kcontext, emsg);
280554925bf6Swillf 		} else {
2806dd9ccd46S 		    /* Solaris Kerberos */
280754925bf6Swillf 		    fprintf(stderr, dbinit_err_fmt,
2808dd9ccd46S 			    progname, error_message(kret));
280954925bf6Swillf 		}
28107c478bd9Sstevel@tonic-gate 		exit_status++;
281154925bf6Swillf 		goto error;
281254925bf6Swillf 	    }
28137c478bd9Sstevel@tonic-gate     }
281456a424ccSmp153739 
281554925bf6Swillf 
28167c478bd9Sstevel@tonic-gate     /*
28177c478bd9Sstevel@tonic-gate      * If an update restoration, make sure the db is left unusable if
28187c478bd9Sstevel@tonic-gate      * the update fails.
28197c478bd9Sstevel@tonic-gate      */
282054925bf6Swillf     if ((kret = krb5_db_lock(kcontext, update?KRB5_DB_LOCKMODE_PERMANENT: KRB5_DB_LOCKMODE_EXCLUSIVE))) {
28217c478bd9Sstevel@tonic-gate 	/*
282254925bf6Swillf 	 * Ignore a not supported error since there is nothing to do about it
282354925bf6Swillf 	 * anyway.
28247c478bd9Sstevel@tonic-gate 	 */
282554925bf6Swillf 	if (kret != KRB5_PLUGIN_OP_NOTSUPP) {
2826dd9ccd46S 	    /* Solaris Kerberos */
282754925bf6Swillf 	    fprintf(stderr, gettext("%s: %s while permanently locking database\n"),
2828dd9ccd46S 		    progname, error_message(kret));
28297c478bd9Sstevel@tonic-gate 	    exit_status++;
28307c478bd9Sstevel@tonic-gate 	    goto error;
28317c478bd9Sstevel@tonic-gate 	}
2832*796b8631SToomas Soome     } else {
283354925bf6Swillf 	db_locked = 1;
2834*796b8631SToomas Soome     }
28357c478bd9Sstevel@tonic-gate 
28367c478bd9Sstevel@tonic-gate 	if (log_ctx && log_ctx->iproprole) {
28377c478bd9Sstevel@tonic-gate 		if (add_update)
28387c478bd9Sstevel@tonic-gate 			caller = FKCOMMAND;
28397c478bd9Sstevel@tonic-gate 		else
28407c478bd9Sstevel@tonic-gate 			caller = FKPROPD;
28417c478bd9Sstevel@tonic-gate 
28427c478bd9Sstevel@tonic-gate 		if (ulog_map(kcontext, &global_params, caller)) {
2843dd9ccd46S 			/* Solaris Kerberos */
28447c478bd9Sstevel@tonic-gate 			fprintf(stderr,
28457c478bd9Sstevel@tonic-gate 				gettext("%s: Could not map log\n"),
2846dd9ccd46S 				progname);
28477c478bd9Sstevel@tonic-gate 			exit_status++;
28487c478bd9Sstevel@tonic-gate 			goto error;
28497c478bd9Sstevel@tonic-gate 		}
28507c478bd9Sstevel@tonic-gate 
28517c478bd9Sstevel@tonic-gate 		/*
28527c478bd9Sstevel@tonic-gate 		 * We don't want to take out the ulog out from underneath
28537c478bd9Sstevel@tonic-gate 		 * kadmind so we reinit the header log.
28547c478bd9Sstevel@tonic-gate 		 *
28557c478bd9Sstevel@tonic-gate 		 * We also don't want to add to the update log since we
28567c478bd9Sstevel@tonic-gate 		 * are doing a whole sale replace of the db, because:
28577c478bd9Sstevel@tonic-gate 		 *	we could easily exceed # of update entries
28587c478bd9Sstevel@tonic-gate 		 *	we could implicity delete db entries during a replace
28597c478bd9Sstevel@tonic-gate 		 *	no advantage in incr updates when entire db is replaced
28607c478bd9Sstevel@tonic-gate 		 */
28617c478bd9Sstevel@tonic-gate 		if (!update) {
28627c478bd9Sstevel@tonic-gate                         memset(log_ctx->ulog, 0, sizeof (kdb_hlog_t));
28637c478bd9Sstevel@tonic-gate 
28647c478bd9Sstevel@tonic-gate                         log_ctx->ulog->kdb_hmagic = KDB_HMAGIC;
28657c478bd9Sstevel@tonic-gate                         log_ctx->ulog->db_version_num = KDB_VERSION;
28667c478bd9Sstevel@tonic-gate                         log_ctx->ulog->kdb_state = KDB_STABLE;
28677c478bd9Sstevel@tonic-gate                         log_ctx->ulog->kdb_block = ULOG_BLOCK;
28687c478bd9Sstevel@tonic-gate 
28697c478bd9Sstevel@tonic-gate 			log_ctx->iproprole = IPROP_NULL;
28707c478bd9Sstevel@tonic-gate 
28717c478bd9Sstevel@tonic-gate 			if (!add_update) {
28727c478bd9Sstevel@tonic-gate 				sscanf(buf, "%s %u %u %u", iheader, &last_sno,
28737c478bd9Sstevel@tonic-gate 					&last_seconds, &last_useconds);
28747c478bd9Sstevel@tonic-gate 
28757c478bd9Sstevel@tonic-gate 				log_ctx->ulog->kdb_last_sno = last_sno;
28767c478bd9Sstevel@tonic-gate 				log_ctx->ulog->kdb_last_time.seconds =
28777c478bd9Sstevel@tonic-gate 				    last_seconds;
28787c478bd9Sstevel@tonic-gate 				log_ctx->ulog->kdb_last_time.useconds =
28797c478bd9Sstevel@tonic-gate 				    last_useconds;
28807c478bd9Sstevel@tonic-gate 			}
28817c478bd9Sstevel@tonic-gate 		}
28827c478bd9Sstevel@tonic-gate 	}
28837c478bd9Sstevel@tonic-gate 
2884dd9ccd46S     /* Solaris Kerberos */
2885dd9ccd46S     if (restore_dump(progname, kcontext, (dumpfile) ? dumpfile : stdin_name,
288654925bf6Swillf 		     f, verbose, load)) {
2887dd9ccd46S 	 /* Solaris Kerberos */
28887c478bd9Sstevel@tonic-gate 	 fprintf(stderr, gettext(restfail_fmt),
2889dd9ccd46S 		 progname, load->name);
28907c478bd9Sstevel@tonic-gate 	 exit_status++;
28917c478bd9Sstevel@tonic-gate     }
289256a424ccSmp153739 
28937c478bd9Sstevel@tonic-gate     if (!update && load->create_kadm5 &&
28947c478bd9Sstevel@tonic-gate 	((kret = kadm5_create_magic_princs(&newparams, kcontext)))) {
28957c478bd9Sstevel@tonic-gate 	 /* error message printed by create_magic_princs */
28967c478bd9Sstevel@tonic-gate 	 exit_status++;
28977c478bd9Sstevel@tonic-gate     }
28987c478bd9Sstevel@tonic-gate 
289954925bf6Swillf     if (db_locked && (kret = krb5_db_unlock(kcontext))) {
290054925bf6Swillf 	 /* change this error? */
2901dd9ccd46S 		/* Solaris Kerberos */
290254925bf6Swillf 		fprintf(stderr, gettext(dbunlockerr_fmt),
2903dd9ccd46S 		 progname, dbname, error_message(kret));
290454925bf6Swillf 	 exit_status++;
290554925bf6Swillf     }
290654925bf6Swillf 
290754925bf6Swillf #if 0
290854925bf6Swillf     if ((kret = krb5_db_fini(kcontext))) {
2909dd9ccd46S 		/* Solaris Kerberos */
291054925bf6Swillf 		fprintf(stderr, gettext(close_err_fmt),
2911dd9ccd46S 		 progname, error_message(kret));
291254925bf6Swillf 	 exit_status++;
291354925bf6Swillf     }
291454925bf6Swillf #endif
291554925bf6Swillf 
29167c478bd9Sstevel@tonic-gate     /* close policy db below */
29177c478bd9Sstevel@tonic-gate 
291854925bf6Swillf     if (exit_status == 0 && !update) {
291954925bf6Swillf 	kret = krb5_db_promote(kcontext, db5util_db_args);
292054925bf6Swillf 	/*
292154925bf6Swillf 	 * Ignore a not supported error since there is nothing to do about it
292254925bf6Swillf 	 * anyway.
292354925bf6Swillf 	 */
292454925bf6Swillf 	if (kret != 0 && kret != KRB5_PLUGIN_OP_NOTSUPP) {
2925dd9ccd46S 	    /* Solaris Kerberos */
292654925bf6Swillf 	    fprintf(stderr, gettext("%s: cannot make newly loaded database live (%s)\n"),
2927dd9ccd46S 		    progname, error_message(kret));
292854925bf6Swillf 	    exit_status++;
292954925bf6Swillf 	}
293054925bf6Swillf     }
293154925bf6Swillf 
29327c478bd9Sstevel@tonic-gate error:
29337c478bd9Sstevel@tonic-gate     /*
293456a424ccSmp153739      * If not an update: if there was an error, destroy the temp database,
293556a424ccSmp153739      * otherwise rename it into place.
29367c478bd9Sstevel@tonic-gate      *
29377c478bd9Sstevel@tonic-gate      * If an update: if there was no error, unlock the database.
29387c478bd9Sstevel@tonic-gate      */
29397c478bd9Sstevel@tonic-gate     if (!update) {
29407c478bd9Sstevel@tonic-gate 	 if (exit_status) {
294154925bf6Swillf 	      kret = krb5_db_destroy(kcontext, db5util_db_args);
294254925bf6Swillf 	      /*
294354925bf6Swillf 	       * Ignore a not supported error since there is nothing to do about
294454925bf6Swillf 	       * it anyway.
294554925bf6Swillf 	       */
294654925bf6Swillf 	      if (kret != 0 && kret != KRB5_PLUGIN_OP_NOTSUPP) {
2947dd9ccd46S 		   /* Solaris Kerberos */
29487c478bd9Sstevel@tonic-gate 		   fprintf(stderr, gettext(dbdelerr_fmt),
2949dd9ccd46S 			   progname, dbname, error_message(kret));
29507c478bd9Sstevel@tonic-gate 		   exit_status++;
29517c478bd9Sstevel@tonic-gate 	      }
29527c478bd9Sstevel@tonic-gate 	 }
29537c478bd9Sstevel@tonic-gate     }
29547c478bd9Sstevel@tonic-gate 
29557c478bd9Sstevel@tonic-gate     if (dumpfile) {
295656a424ccSmp153739 	 (void) krb5_lock_file(kcontext, fileno(f), KRB5_LOCKMODE_UNLOCK);
29577c478bd9Sstevel@tonic-gate 	 fclose(f);
29587c478bd9Sstevel@tonic-gate     }
295956a424ccSmp153739 
29607c478bd9Sstevel@tonic-gate     if (dbname_tmp)
29617c478bd9Sstevel@tonic-gate 	 free(dbname_tmp);
29627c478bd9Sstevel@tonic-gate     krb5_free_context(kcontext);
29637c478bd9Sstevel@tonic-gate }
2964