xref: /onnv-gate/usr/src/cmd/krb5/kadmin/dbutil/dump.c (revision 8092:19771b16f0a8)
10Sstevel@tonic-gate /*
25916Swillf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate /*
70Sstevel@tonic-gate  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  *	Openvision retains the copyright to derivative works of
100Sstevel@tonic-gate  *	this source code.  Do *NOT* create a derivative of this
110Sstevel@tonic-gate  *	source code before consulting with your legal department.
120Sstevel@tonic-gate  *	Do *NOT* integrate *ANY* of this source code into another
130Sstevel@tonic-gate  *	product before consulting with your legal department.
140Sstevel@tonic-gate  *
150Sstevel@tonic-gate  *	For further information, read the top-level Openvision
160Sstevel@tonic-gate  *	copyright which is contained in the top-level MIT Kerberos
170Sstevel@tonic-gate  *	copyright.
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
200Sstevel@tonic-gate  *
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate 
230Sstevel@tonic-gate 
240Sstevel@tonic-gate /*
250Sstevel@tonic-gate  * admin/edit/dump.c
260Sstevel@tonic-gate  *
270Sstevel@tonic-gate  * Copyright 1990,1991 by the Massachusetts Institute of Technology.
280Sstevel@tonic-gate  * All Rights Reserved.
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  * Export of this software from the United States of America may
310Sstevel@tonic-gate  *   require a specific license from the United States Government.
320Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
330Sstevel@tonic-gate  *   export to obtain such a license before exporting.
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
360Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
370Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
380Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
390Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
400Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
410Sstevel@tonic-gate  * to distribution of the software without specific, written prior
420Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
430Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
440Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
450Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
460Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
470Sstevel@tonic-gate  * or implied warranty.
480Sstevel@tonic-gate  *
490Sstevel@tonic-gate  *
500Sstevel@tonic-gate  * Dump a KDC database
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate 
534960Swillf #include <stdio.h>
540Sstevel@tonic-gate #include <k5-int.h>
554960Swillf #include <kadm5/admin.h>
564960Swillf #include <kadm5/server_internal.h>
574960Swillf #include <kdb.h>
580Sstevel@tonic-gate #include <com_err.h>
590Sstevel@tonic-gate #include <libintl.h>
600Sstevel@tonic-gate #include "kdb5_util.h"
612881Smp153739 #if defined(HAVE_REGEX_H) && defined(HAVE_REGCOMP)
620Sstevel@tonic-gate #include <regex.h>
630Sstevel@tonic-gate #endif	/* HAVE_REGEX_H */
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate  * Needed for master key conversion.
670Sstevel@tonic-gate  */
680Sstevel@tonic-gate extern krb5_keyblock master_key;
690Sstevel@tonic-gate extern krb5_principal master_princ;
700Sstevel@tonic-gate static int			mkey_convert;
710Sstevel@tonic-gate static krb5_keyblock		new_master_key;
720Sstevel@tonic-gate 
732881Smp153739 static int	backwards;
742881Smp153739 static int	recursive;
752881Smp153739 
760Sstevel@tonic-gate /*
770Sstevel@tonic-gate  * Use compile(3) if no regcomp present.
780Sstevel@tonic-gate  */
790Sstevel@tonic-gate #if	!defined(HAVE_REGCOMP) && defined(HAVE_REGEXP_H)
800Sstevel@tonic-gate #define	INIT		char *sp = instring;
810Sstevel@tonic-gate #define	GETC()		(*sp++)
820Sstevel@tonic-gate #define	PEEKC()		(*sp)
830Sstevel@tonic-gate #define	UNGETC(c)	(--sp)
840Sstevel@tonic-gate #define	RETURN(c)	return(c)
850Sstevel@tonic-gate #define	ERROR(c)
860Sstevel@tonic-gate #define	RE_BUF_SIZE	1024
870Sstevel@tonic-gate #include <regexp.h>
880Sstevel@tonic-gate #endif	/* !HAVE_REGCOMP && HAVE_REGEXP_H */
890Sstevel@tonic-gate 
900Sstevel@tonic-gate struct dump_args {
910Sstevel@tonic-gate     char		*programname;
920Sstevel@tonic-gate     FILE		*ofile;
930Sstevel@tonic-gate     krb5_context	kcontext;
940Sstevel@tonic-gate     char		**names;
950Sstevel@tonic-gate     int			nnames;
960Sstevel@tonic-gate     int			verbose;
970Sstevel@tonic-gate };
980Sstevel@tonic-gate 
992881Smp153739 static krb5_error_code dump_k5beta_iterator (krb5_pointer,
1002881Smp153739 					     krb5_db_entry *);
1012881Smp153739 static krb5_error_code dump_k5beta6_iterator (krb5_pointer,
1022881Smp153739 					      krb5_db_entry *);
1032881Smp153739 static krb5_error_code dump_k5beta6_iterator_ext (krb5_pointer,
1042881Smp153739 						  krb5_db_entry *,
1052881Smp153739 						  int);
1062881Smp153739 static krb5_error_code dump_iprop_iterator (krb5_pointer,
1072881Smp153739 						  krb5_db_entry *);
1082881Smp153739 static krb5_error_code dump_k5beta7_princ (krb5_pointer,
1092881Smp153739 					   krb5_db_entry *);
1102881Smp153739 static krb5_error_code dump_k5beta7_princ_ext (krb5_pointer,
1112881Smp153739 					       krb5_db_entry *,
1122881Smp153739 					       int);
1132881Smp153739 static krb5_error_code dump_k5beta7_princ_withpolicy
1142881Smp153739 			(krb5_pointer, krb5_db_entry *);
1152881Smp153739 static krb5_error_code dump_iprop_princ (krb5_pointer,
1162881Smp153739 					       krb5_db_entry *);
1172881Smp153739 static krb5_error_code dump_ov_princ (krb5_pointer,
1182881Smp153739 				      krb5_db_entry *);
119781Sgtb static void dump_k5beta7_policy (void *, osa_policy_ent_t);
1200Sstevel@tonic-gate 
1212881Smp153739 typedef krb5_error_code (*dump_func)(krb5_pointer,
1222881Smp153739 				     krb5_db_entry *);
1230Sstevel@tonic-gate 
1242881Smp153739 static int process_k5beta_record (char *, krb5_context,
1254960Swillf 				  FILE *, int, int *);
1262881Smp153739 static int process_k5beta6_record (char *, krb5_context,
1274960Swillf 				   FILE *, int, int *);
1282881Smp153739 static int process_k5beta7_record (char *, krb5_context,
1294960Swillf 				   FILE *, int, int *);
1302881Smp153739 static int process_ov_record (char *, krb5_context,
1314960Swillf 			      FILE *, int, int *);
1322881Smp153739 typedef krb5_error_code (*load_func)(char *, krb5_context,
1334960Swillf 				     FILE *, int, int *);
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate typedef struct _dump_version {
1360Sstevel@tonic-gate      char *name;
1370Sstevel@tonic-gate      char *header;
1380Sstevel@tonic-gate      int updateonly;
1390Sstevel@tonic-gate      int create_kadm5;
1400Sstevel@tonic-gate      dump_func dump_princ;
1410Sstevel@tonic-gate      osa_adb_iter_policy_func dump_policy;
1420Sstevel@tonic-gate      load_func load_record;
1430Sstevel@tonic-gate } dump_version;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate dump_version old_version = {
1460Sstevel@tonic-gate      "Kerberos version 5 old format",
1470Sstevel@tonic-gate      "kdb5_edit load_dump version 2.0\n",
1480Sstevel@tonic-gate      0,
1490Sstevel@tonic-gate      1,
1500Sstevel@tonic-gate      dump_k5beta_iterator,
1510Sstevel@tonic-gate      NULL,
1524960Swillf      process_k5beta_record
1530Sstevel@tonic-gate };
1540Sstevel@tonic-gate dump_version beta6_version = {
1550Sstevel@tonic-gate      "Kerberos version 5 beta 6 format",
1560Sstevel@tonic-gate      "kdb5_edit load_dump version 3.0\n",
1570Sstevel@tonic-gate      0,
1580Sstevel@tonic-gate      1,
1590Sstevel@tonic-gate      dump_k5beta6_iterator,
1600Sstevel@tonic-gate      NULL,
1614960Swillf      process_k5beta6_record
1620Sstevel@tonic-gate };
1630Sstevel@tonic-gate dump_version beta7_version = {
1640Sstevel@tonic-gate      "Kerberos version 5",
1650Sstevel@tonic-gate      "kdb5_util load_dump version 4\n",
1660Sstevel@tonic-gate      0,
1670Sstevel@tonic-gate      0,
1680Sstevel@tonic-gate      dump_k5beta7_princ,
1690Sstevel@tonic-gate      dump_k5beta7_policy,
1704960Swillf      process_k5beta7_record
1710Sstevel@tonic-gate };
1720Sstevel@tonic-gate dump_version iprop_version = {
1730Sstevel@tonic-gate      "Kerberos iprop version",
1740Sstevel@tonic-gate      "iprop",
1750Sstevel@tonic-gate      0,
1760Sstevel@tonic-gate      0,
1770Sstevel@tonic-gate      dump_iprop_princ,
1780Sstevel@tonic-gate      dump_k5beta7_policy,
1794960Swillf      process_k5beta7_record
1800Sstevel@tonic-gate };
1810Sstevel@tonic-gate dump_version ov_version = {
1820Sstevel@tonic-gate      "OpenV*Secure V1.0",
1830Sstevel@tonic-gate      "OpenV*Secure V1.0\t",
1840Sstevel@tonic-gate      1,
1850Sstevel@tonic-gate      1,
1860Sstevel@tonic-gate      dump_ov_princ,
1870Sstevel@tonic-gate      dump_k5beta7_policy,
1884960Swillf      process_ov_record
1890Sstevel@tonic-gate };
1900Sstevel@tonic-gate 
1912881Smp153739 dump_version r1_3_version = {
1922881Smp153739      "Kerberos version 5 release 1.3",
1932881Smp153739      "kdb5_util load_dump version 5\n",
1942881Smp153739      0,
1952881Smp153739      0,
1962881Smp153739      dump_k5beta7_princ_withpolicy,
1972881Smp153739      dump_k5beta7_policy,
1982881Smp153739      process_k5beta7_record,
1992881Smp153739 };
2002881Smp153739 
2010Sstevel@tonic-gate /* External data */
2020Sstevel@tonic-gate extern char		*current_dbname;
2030Sstevel@tonic-gate extern krb5_boolean	dbactive;
2040Sstevel@tonic-gate extern int		exit_status;
2050Sstevel@tonic-gate extern krb5_context	util_context;
2060Sstevel@tonic-gate extern kadm5_config_params global_params;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate /* Strings */
2090Sstevel@tonic-gate 
2102881Smp153739 #define k5beta_dump_header	"kdb5_edit load_dump version 2.0\n"
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate static const char null_mprinc_name[] = "kdb5_dump@MISSING";
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate /*
2150Sstevel@tonic-gate  * We define gettext(s) to be s here, so that xgettext will extract the
2160Sstevel@tonic-gate  * strings to the .po file. At the end of the message section we will
2170Sstevel@tonic-gate  * undef gettext so that we can use it as a funtion.
2180Sstevel@tonic-gate  */
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate #define	gettext(s) s
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate /* Message strings */
2230Sstevel@tonic-gate static const char regex_err[] =
2240Sstevel@tonic-gate 	gettext("%s: regular expression error - %s\n");
2250Sstevel@tonic-gate static const char regex_merr[] =
2260Sstevel@tonic-gate 	gettext("%s: regular expression match error - %s\n");
2270Sstevel@tonic-gate static const char pname_unp_err[] =
2280Sstevel@tonic-gate 	gettext("%s: cannot unparse principal name (%s)\n");
2290Sstevel@tonic-gate static const char mname_unp_err[] =
2300Sstevel@tonic-gate 	gettext("%s: cannot unparse modifier name (%s)\n");
2310Sstevel@tonic-gate static const char nokeys_err[] =
2320Sstevel@tonic-gate 	gettext("%s: cannot find any standard key for %s\n");
2330Sstevel@tonic-gate static const char sdump_tl_inc_err[] =
2340Sstevel@tonic-gate 	gettext("%s: tagged data list inconsistency for %s "
2350Sstevel@tonic-gate 		"(counted %d, stored %d)\n");
2360Sstevel@tonic-gate static const char stand_fmt_name[] =
2370Sstevel@tonic-gate 	gettext("Kerberos version 5");
2380Sstevel@tonic-gate static const char old_fmt_name[] =
2390Sstevel@tonic-gate 	gettext("Kerberos version 5 old format");
2400Sstevel@tonic-gate static const char b6_fmt_name[] =
2410Sstevel@tonic-gate 	gettext("Kerberos version 5 beta 6 format");
2420Sstevel@tonic-gate static const char ofopen_error[] =
2430Sstevel@tonic-gate 	gettext("%s: cannot open %s for writing (%s)\n");
2440Sstevel@tonic-gate static const char oflock_error[] =
2450Sstevel@tonic-gate 	gettext("%s: cannot lock %s (%s)\n");
2460Sstevel@tonic-gate static const char dumprec_err[] =
2470Sstevel@tonic-gate 	gettext("%s: error performing %s dump (%s)\n");
2480Sstevel@tonic-gate static const char dumphdr_err[] =
2490Sstevel@tonic-gate 	gettext("%s: error dumping %s header (%s)\n");
2500Sstevel@tonic-gate static const char trash_end_fmt[] =
2510Sstevel@tonic-gate 	gettext("%s(%d): ignoring trash at end of line: ");
2520Sstevel@tonic-gate static const char read_name_string[] =
2530Sstevel@tonic-gate 	gettext("name string");
2540Sstevel@tonic-gate static const char read_key_type[] =
2550Sstevel@tonic-gate 	gettext("key type");
2560Sstevel@tonic-gate static const char read_key_data[] =
2570Sstevel@tonic-gate 	gettext("key data");
2580Sstevel@tonic-gate static const char read_pr_data1[] =
2590Sstevel@tonic-gate 	gettext("first set of principal attributes");
2600Sstevel@tonic-gate static const char read_mod_name[] =
2610Sstevel@tonic-gate 	gettext("modifier name");
2620Sstevel@tonic-gate static const char read_pr_data2[] =
2630Sstevel@tonic-gate 	gettext("second set of principal attributes");
2640Sstevel@tonic-gate static const char read_salt_data[] =
2650Sstevel@tonic-gate 	gettext("salt data");
2660Sstevel@tonic-gate static const char read_akey_type[] =
2670Sstevel@tonic-gate 	gettext("alternate key type");
2680Sstevel@tonic-gate static const char read_akey_data[] =
2690Sstevel@tonic-gate 	gettext("alternate key data");
2700Sstevel@tonic-gate static const char read_asalt_type[] =
2710Sstevel@tonic-gate 	gettext("alternate salt type");
2720Sstevel@tonic-gate static const char read_asalt_data[] =
2730Sstevel@tonic-gate 	gettext("alternate salt data");
2740Sstevel@tonic-gate static const char read_exp_data[] =
2750Sstevel@tonic-gate 	gettext("expansion data");
2760Sstevel@tonic-gate static const char store_err_fmt[] =
2770Sstevel@tonic-gate 	gettext("%s(%d): cannot store %s(%s)\n");
2780Sstevel@tonic-gate static const char add_princ_fmt[] =
2790Sstevel@tonic-gate 	gettext("%s\n");
2800Sstevel@tonic-gate static const char parse_err_fmt[] =
2810Sstevel@tonic-gate 	gettext("%s(%d): cannot parse %s (%s)\n");
2820Sstevel@tonic-gate static const char read_err_fmt[] =
2830Sstevel@tonic-gate 	gettext("%s(%d): cannot read %s\n");
2840Sstevel@tonic-gate static const char no_mem_fmt[] =
2850Sstevel@tonic-gate 	gettext("%s(%d): no memory for buffers\n");
2860Sstevel@tonic-gate static const char rhead_err_fmt[] =
2870Sstevel@tonic-gate 	gettext("%s(%d): cannot match size tokens\n");
2880Sstevel@tonic-gate static const char err_line_fmt[] =
2890Sstevel@tonic-gate 	gettext("%s: error processing line %d of %s\n");
2900Sstevel@tonic-gate static const char head_bad_fmt[] =
2910Sstevel@tonic-gate 	gettext("%s: dump header bad in %s\n");
2920Sstevel@tonic-gate static const char read_bytecnt[] =
2930Sstevel@tonic-gate 	gettext("record byte count");
2940Sstevel@tonic-gate static const char read_encdata[] =
2950Sstevel@tonic-gate 	gettext("encoded data");
2960Sstevel@tonic-gate static const char n_name_unp_fmt[] =
2970Sstevel@tonic-gate 	gettext("%s(%s): cannot unparse name\n");
2980Sstevel@tonic-gate static const char n_dec_cont_fmt[] =
2990Sstevel@tonic-gate 	gettext("%s(%s): cannot decode contents\n");
3000Sstevel@tonic-gate static const char read_nint_data[] =
3010Sstevel@tonic-gate 	gettext("principal static attributes");
3020Sstevel@tonic-gate static const char read_tcontents[] =
3030Sstevel@tonic-gate 	gettext("tagged data contents");
3040Sstevel@tonic-gate static const char read_ttypelen[] =
3050Sstevel@tonic-gate 	gettext("tagged data type and length");
3060Sstevel@tonic-gate static const char read_kcontents[] =
3070Sstevel@tonic-gate 	gettext("key data contents");
3080Sstevel@tonic-gate static const char read_ktypelen[] =
3090Sstevel@tonic-gate 	gettext("key data type and length");
3100Sstevel@tonic-gate static const char read_econtents[] =
3110Sstevel@tonic-gate 	gettext("extra data contents");
3120Sstevel@tonic-gate static const char k5beta_fmt_name[] =
3130Sstevel@tonic-gate 	gettext("Kerberos version 5 old format");
3140Sstevel@tonic-gate static const char standard_fmt_name[] =
3150Sstevel@tonic-gate 	gettext("Kerberos version 5 format");
3160Sstevel@tonic-gate static const char no_name_mem_fmt[] =
3170Sstevel@tonic-gate 	gettext("%s: cannot get memory for temporary name\n");
3180Sstevel@tonic-gate static const char ctx_err_fmt[] =
3190Sstevel@tonic-gate 	gettext("%s: cannot initialize Kerberos context\n");
3200Sstevel@tonic-gate static const char stdin_name[] =
3210Sstevel@tonic-gate 	gettext("standard input");
3220Sstevel@tonic-gate static const char remaster_err_fmt[] =
3230Sstevel@tonic-gate 	gettext("while re-encoding keys for principal %s with new master key");
3240Sstevel@tonic-gate static const char restfail_fmt[] =
3250Sstevel@tonic-gate 	gettext("%s: %s restore failed\n");
3260Sstevel@tonic-gate static const char close_err_fmt[] =
3270Sstevel@tonic-gate 	gettext("%s: cannot close database (%s)\n");
3280Sstevel@tonic-gate static const char dbinit_err_fmt[] =
3290Sstevel@tonic-gate 	gettext("%s: cannot initialize database (%s)\n");
3300Sstevel@tonic-gate static const char dblock_err_fmt[] =
3310Sstevel@tonic-gate 	gettext("%s: cannot initialize database lock (%s)\n");
3320Sstevel@tonic-gate static const char dbname_err_fmt[] =
3330Sstevel@tonic-gate 	gettext("%s: cannot set database name to %s (%s)\n");
3340Sstevel@tonic-gate static const char dbdelerr_fmt[] =
3350Sstevel@tonic-gate 	gettext("%s: cannot delete bad database %s (%s)\n");
3360Sstevel@tonic-gate static const char dbunlockerr_fmt[] =
3370Sstevel@tonic-gate 	gettext("%s: cannot unlock database %s (%s)\n");
3380Sstevel@tonic-gate static const char dbrenerr_fmt[] =
3390Sstevel@tonic-gate 	gettext("%s: cannot rename database %s to %s (%s)\n");
3400Sstevel@tonic-gate static const char dbcreaterr_fmt[] =
3410Sstevel@tonic-gate 	gettext("%s: cannot create database %s (%s)\n");
3420Sstevel@tonic-gate static const char dfile_err_fmt[] =
3430Sstevel@tonic-gate 	gettext("%s: cannot open %s (%s)\n");
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate /*
3460Sstevel@tonic-gate  * We now return you to your regularly scheduled program.
3470Sstevel@tonic-gate  */
3480Sstevel@tonic-gate #undef gettext
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate static const char oldoption[] = "-old";
3510Sstevel@tonic-gate static const char b6option[] = "-b6";
3522881Smp153739 static const char b7option[] = "-b7";
3530Sstevel@tonic-gate static const char ipropoption[] = "-i";
3540Sstevel@tonic-gate static const char verboseoption[] = "-verbose";
3550Sstevel@tonic-gate static const char updateoption[] = "-update";
3560Sstevel@tonic-gate static const char hashoption[] = "-hash";
3570Sstevel@tonic-gate static const char ovoption[] = "-ov";
3580Sstevel@tonic-gate static const char dump_tmptrail[] = "~";
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate /*
3610Sstevel@tonic-gate  * Re-encrypt the key_data with the new master key...
3620Sstevel@tonic-gate  */
master_key_convert(context,db_entry)3632881Smp153739 static krb5_error_code master_key_convert(context, db_entry)
3640Sstevel@tonic-gate     krb5_context	  context;
3650Sstevel@tonic-gate     krb5_db_entry	* db_entry;
3660Sstevel@tonic-gate {
3670Sstevel@tonic-gate     krb5_error_code	retval;
3680Sstevel@tonic-gate     krb5_keyblock 	v5plainkey, *key_ptr;
3690Sstevel@tonic-gate     krb5_keysalt 	keysalt;
3702881Smp153739     int	      i, j;
3710Sstevel@tonic-gate     krb5_key_data	new_key_data, *key_data;
3720Sstevel@tonic-gate     krb5_boolean	is_mkey;
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate     is_mkey = krb5_principal_compare(context, master_princ, db_entry->princ);
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate     if (is_mkey && db_entry->n_key_data != 1)
3770Sstevel@tonic-gate 	    fprintf(stderr,
3780Sstevel@tonic-gate 		    gettext(
3790Sstevel@tonic-gate 		      "Master key db entry has %d keys, expecting only 1!\n"),
3800Sstevel@tonic-gate 		    db_entry->n_key_data);
3810Sstevel@tonic-gate     for (i=0; i < db_entry->n_key_data; i++) {
3820Sstevel@tonic-gate 	key_data = &db_entry->key_data[i];
3830Sstevel@tonic-gate 	if (key_data->key_data_length == 0)
3840Sstevel@tonic-gate 	    continue;
3850Sstevel@tonic-gate 	retval = krb5_dbekd_decrypt_key_data(context, &master_key,
3860Sstevel@tonic-gate 					     key_data, &v5plainkey,
3870Sstevel@tonic-gate 					     &keysalt);
3880Sstevel@tonic-gate 	if (retval)
3890Sstevel@tonic-gate 		return retval;
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	memset(&new_key_data, 0, sizeof(new_key_data));
3920Sstevel@tonic-gate 	key_ptr = is_mkey ? &new_master_key : &v5plainkey;
3930Sstevel@tonic-gate 	retval = krb5_dbekd_encrypt_key_data(context, &new_master_key,
3940Sstevel@tonic-gate 					     key_ptr, &keysalt,
3950Sstevel@tonic-gate 					     key_data->key_data_kvno,
3960Sstevel@tonic-gate 					     &new_key_data);
3970Sstevel@tonic-gate 	if (retval)
3980Sstevel@tonic-gate 		return retval;
3990Sstevel@tonic-gate 	krb5_free_keyblock_contents(context, &v5plainkey);
4002881Smp153739 	for (j = 0; j < key_data->key_data_ver; j++) {
4012881Smp153739 	    if (key_data->key_data_length[j]) {
4022881Smp153739 		free(key_data->key_data_contents[j]);
4032881Smp153739 	    }
4042881Smp153739 	}
4050Sstevel@tonic-gate 	*key_data = new_key_data;
4060Sstevel@tonic-gate     }
4070Sstevel@tonic-gate     return 0;
4080Sstevel@tonic-gate }
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate /*
4110Sstevel@tonic-gate  * Update the "ok" file.
4120Sstevel@tonic-gate  */
update_ok_file(file_name)4132881Smp153739 void update_ok_file (file_name)
4140Sstevel@tonic-gate      char *file_name;
4150Sstevel@tonic-gate {
4160Sstevel@tonic-gate 	/* handle slave locking/failure stuff */
4170Sstevel@tonic-gate 	char *file_ok;
4180Sstevel@tonic-gate 	int fd;
4190Sstevel@tonic-gate 	static char ok[]=".dump_ok";
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	if ((file_ok = (char *)malloc(strlen(file_name) + strlen(ok) + 1))
4220Sstevel@tonic-gate 	    == NULL) {
4230Sstevel@tonic-gate 		com_err(progname, ENOMEM,
4240Sstevel@tonic-gate 		    gettext("while allocating filename "
4250Sstevel@tonic-gate 			"for update_ok_file"));
4260Sstevel@tonic-gate 		exit_status++;
4270Sstevel@tonic-gate 		return;
4280Sstevel@tonic-gate 	}
4290Sstevel@tonic-gate 	strcpy(file_ok, file_name);
4300Sstevel@tonic-gate 	strcat(file_ok, ok);
4310Sstevel@tonic-gate 	if ((fd = open(file_ok, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0) {
4320Sstevel@tonic-gate 		com_err(progname, errno,
4330Sstevel@tonic-gate 		    gettext("while creating 'ok' file, '%s'"),
4340Sstevel@tonic-gate 			file_ok);
4350Sstevel@tonic-gate 		exit_status++;
4360Sstevel@tonic-gate 		free(file_ok);
4370Sstevel@tonic-gate 		return;
4380Sstevel@tonic-gate 	}
4390Sstevel@tonic-gate 	if (write(fd, "", 1) != 1) {
4400Sstevel@tonic-gate 		com_err(progname, errno,
4410Sstevel@tonic-gate 		    gettext("while writing to 'ok' file, '%s'"),
4420Sstevel@tonic-gate 		    file_ok);
4430Sstevel@tonic-gate 	     exit_status++;
4440Sstevel@tonic-gate 	     free(file_ok);
4450Sstevel@tonic-gate 	     return;
4460Sstevel@tonic-gate 	}
4472881Smp153739 
4480Sstevel@tonic-gate 	free(file_ok);
4490Sstevel@tonic-gate 	close(fd);
4502881Smp153739 	return;
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate /*
4540Sstevel@tonic-gate  * name_matches()	- See if a principal name matches a regular expression
4550Sstevel@tonic-gate  *			  or string.
4560Sstevel@tonic-gate  */
4570Sstevel@tonic-gate static int
name_matches(name,arglist)4580Sstevel@tonic-gate name_matches(name, arglist)
4590Sstevel@tonic-gate     char		*name;
4600Sstevel@tonic-gate     struct dump_args	*arglist;
4610Sstevel@tonic-gate {
4620Sstevel@tonic-gate #if	HAVE_REGCOMP
4630Sstevel@tonic-gate     regex_t	match_exp;
4640Sstevel@tonic-gate     regmatch_t	match_match;
4650Sstevel@tonic-gate     int		match_error;
4660Sstevel@tonic-gate     char	match_errmsg[BUFSIZ];
4670Sstevel@tonic-gate     size_t	errmsg_size;
4680Sstevel@tonic-gate #elif	HAVE_REGEXP_H
4690Sstevel@tonic-gate     char	regexp_buffer[RE_BUF_SIZE];
4700Sstevel@tonic-gate #elif	HAVE_RE_COMP
4710Sstevel@tonic-gate     extern char	*re_comp();
4720Sstevel@tonic-gate     char	*re_result;
4730Sstevel@tonic-gate #endif	/* HAVE_RE_COMP */
4740Sstevel@tonic-gate     int		i, match;
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate     /*
4772881Smp153739      * Plow, brute force, through the list of names/regular expressions.
4780Sstevel@tonic-gate      */
4790Sstevel@tonic-gate     match = (arglist->nnames) ? 0 : 1;
4800Sstevel@tonic-gate     for (i=0; i<arglist->nnames; i++) {
4810Sstevel@tonic-gate #if	HAVE_REGCOMP
4820Sstevel@tonic-gate 	/*
4830Sstevel@tonic-gate 	 * Compile the regular expression.
4840Sstevel@tonic-gate 	 */
4852881Smp153739 	match_error = regcomp(&match_exp, arglist->names[i], REG_EXTENDED);
4862881Smp153739 	if (match_error) {
4870Sstevel@tonic-gate 	    errmsg_size = regerror(match_error,
4880Sstevel@tonic-gate 				   &match_exp,
4890Sstevel@tonic-gate 				   match_errmsg,
4900Sstevel@tonic-gate 				   sizeof(match_errmsg));
4910Sstevel@tonic-gate 			fprintf(stderr, gettext(regex_err),
4920Sstevel@tonic-gate 			    arglist->programname, match_errmsg);
4930Sstevel@tonic-gate 	    break;
4940Sstevel@tonic-gate 	}
4950Sstevel@tonic-gate 	/*
4960Sstevel@tonic-gate 	 * See if we have a match.
4970Sstevel@tonic-gate 	 */
4982881Smp153739 	match_error = regexec(&match_exp, name, 1, &match_match, 0);
4992881Smp153739 	if (match_error) {
5000Sstevel@tonic-gate 	    if (match_error != REG_NOMATCH) {
5010Sstevel@tonic-gate 		errmsg_size = regerror(match_error,
5020Sstevel@tonic-gate 				       &match_exp,
5030Sstevel@tonic-gate 				       match_errmsg,
5040Sstevel@tonic-gate 				       sizeof(match_errmsg));
5050Sstevel@tonic-gate 				fprintf(stderr, gettext(regex_merr),
5060Sstevel@tonic-gate 			arglist->programname, match_errmsg);
5070Sstevel@tonic-gate 		break;
5080Sstevel@tonic-gate 	    }
5092881Smp153739 	}
5102881Smp153739 	else {
5110Sstevel@tonic-gate 	    /*
5120Sstevel@tonic-gate 	     * We have a match.  See if it matches the whole
5130Sstevel@tonic-gate 	     * name.
5140Sstevel@tonic-gate 	     */
5150Sstevel@tonic-gate 	    if ((match_match.rm_so == 0) &&
5160Sstevel@tonic-gate 		(match_match.rm_eo == strlen(name)))
5170Sstevel@tonic-gate 		match = 1;
5180Sstevel@tonic-gate 	}
5190Sstevel@tonic-gate 	regfree(&match_exp);
5200Sstevel@tonic-gate #elif	HAVE_REGEXP_H
5210Sstevel@tonic-gate 	/*
5220Sstevel@tonic-gate 	 * Compile the regular expression.
5230Sstevel@tonic-gate 	 */
5240Sstevel@tonic-gate 	compile(arglist->names[i],
5250Sstevel@tonic-gate 		regexp_buffer,
5260Sstevel@tonic-gate 		&regexp_buffer[RE_BUF_SIZE],
5270Sstevel@tonic-gate 		'\0');
5280Sstevel@tonic-gate 	if (step(name, regexp_buffer)) {
5290Sstevel@tonic-gate 	    if ((loc1 == name) &&
5300Sstevel@tonic-gate 		(loc2 == &name[strlen(name)]))
5310Sstevel@tonic-gate 		match = 1;
5320Sstevel@tonic-gate 	}
5330Sstevel@tonic-gate #elif	HAVE_RE_COMP
5340Sstevel@tonic-gate 	/*
5350Sstevel@tonic-gate 	 * Compile the regular expression.
5360Sstevel@tonic-gate 	 */
5370Sstevel@tonic-gate 	if (re_result = re_comp(arglist->names[i])) {
5382881Smp153739 	    fprintf(stderr, gettext(regex_err), arglist->programname, re_result);
5390Sstevel@tonic-gate 	    break;
5400Sstevel@tonic-gate 	}
5410Sstevel@tonic-gate 	if (re_exec(name))
5420Sstevel@tonic-gate 	    match = 1;
5430Sstevel@tonic-gate #else	/* HAVE_RE_COMP */
5440Sstevel@tonic-gate 	/*
5452881Smp153739 	 * If no regular expression support, then just compare the strings.
5460Sstevel@tonic-gate 	 */
5472881Smp153739 	if (!strcmp(arglist->names[i], name))
5480Sstevel@tonic-gate 	    match = 1;
5490Sstevel@tonic-gate #endif	/* HAVE_REGCOMP */
5500Sstevel@tonic-gate 	if (match)
5510Sstevel@tonic-gate 	    break;
5520Sstevel@tonic-gate     }
5530Sstevel@tonic-gate     return(match);
5540Sstevel@tonic-gate }
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate static krb5_error_code
find_enctype(dbentp,enctype,salttype,kentp)5570Sstevel@tonic-gate find_enctype(dbentp, enctype, salttype, kentp)
5580Sstevel@tonic-gate     krb5_db_entry	*dbentp;
5590Sstevel@tonic-gate     krb5_enctype	enctype;
5600Sstevel@tonic-gate     krb5_int32		salttype;
5610Sstevel@tonic-gate     krb5_key_data	**kentp;
5620Sstevel@tonic-gate {
5630Sstevel@tonic-gate     int			i;
5640Sstevel@tonic-gate     int			maxkvno;
5650Sstevel@tonic-gate     krb5_key_data	*datap;
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate     maxkvno = -1;
5680Sstevel@tonic-gate     datap = (krb5_key_data *) NULL;
5690Sstevel@tonic-gate     for (i=0; i<dbentp->n_key_data; i++) {
5700Sstevel@tonic-gate 	if (( (krb5_enctype)dbentp->key_data[i].key_data_type[0] == enctype) &&
5710Sstevel@tonic-gate 	    ((dbentp->key_data[i].key_data_type[1] == salttype) ||
5720Sstevel@tonic-gate 	     (salttype < 0))) {
5730Sstevel@tonic-gate 	    maxkvno = dbentp->key_data[i].key_data_kvno;
5740Sstevel@tonic-gate 	    datap = &dbentp->key_data[i];
5750Sstevel@tonic-gate 	}
5760Sstevel@tonic-gate     }
5770Sstevel@tonic-gate     if (maxkvno >= 0) {
5780Sstevel@tonic-gate 	*kentp = datap;
5790Sstevel@tonic-gate 	return(0);
5800Sstevel@tonic-gate     }
5810Sstevel@tonic-gate     return(ENOENT);
5820Sstevel@tonic-gate }
5830Sstevel@tonic-gate 
5842881Smp153739 #if 0
5850Sstevel@tonic-gate /*
5860Sstevel@tonic-gate  * dump_k5beta_header()	- Make a dump header that is recognizable by Kerberos
5870Sstevel@tonic-gate  *			  Version 5 Beta 5 and previous releases.
5880Sstevel@tonic-gate  */
5890Sstevel@tonic-gate static krb5_error_code
5900Sstevel@tonic-gate dump_k5beta_header(arglist)
5910Sstevel@tonic-gate     struct dump_args *arglist;
5920Sstevel@tonic-gate {
5930Sstevel@tonic-gate     /* The old header consists of the leading string */
5940Sstevel@tonic-gate     fprintf(arglist->ofile, k5beta_dump_header);
5950Sstevel@tonic-gate     return(0);
5960Sstevel@tonic-gate }
5972881Smp153739 #endif
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate /*
6000Sstevel@tonic-gate  * dump_k5beta_iterator()	- Dump an entry in a format that is usable
6010Sstevel@tonic-gate  *				  by Kerberos Version 5 Beta 5 and previous
6020Sstevel@tonic-gate  *				  releases.
6030Sstevel@tonic-gate  */
6040Sstevel@tonic-gate static krb5_error_code
dump_k5beta_iterator(ptr,entry)6050Sstevel@tonic-gate dump_k5beta_iterator(ptr, entry)
6060Sstevel@tonic-gate     krb5_pointer	ptr;
6070Sstevel@tonic-gate     krb5_db_entry	*entry;
6080Sstevel@tonic-gate {
6090Sstevel@tonic-gate     krb5_error_code	retval;
6100Sstevel@tonic-gate     struct dump_args	*arg;
6110Sstevel@tonic-gate     char		*name, *mod_name;
6120Sstevel@tonic-gate     krb5_principal	mod_princ;
6130Sstevel@tonic-gate     krb5_key_data	*pkey, *akey, nullkey;
6140Sstevel@tonic-gate     krb5_timestamp	mod_date, last_pwd_change;
6150Sstevel@tonic-gate     int			i;
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate     /* Initialize */
6180Sstevel@tonic-gate     arg = (struct dump_args *) ptr;
6190Sstevel@tonic-gate     name = (char *) NULL;
6200Sstevel@tonic-gate     mod_name = (char *) NULL;
6210Sstevel@tonic-gate     memset(&nullkey, 0, sizeof(nullkey));
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate     /*
6240Sstevel@tonic-gate      * Flatten the principal name.
6250Sstevel@tonic-gate      */
6260Sstevel@tonic-gate     if ((retval = krb5_unparse_name(arg->kcontext,
6270Sstevel@tonic-gate 				    entry->princ,
6280Sstevel@tonic-gate 				    &name))) {
6290Sstevel@tonic-gate 		fprintf(stderr, gettext(pname_unp_err),
6300Sstevel@tonic-gate 		arg->programname, error_message(retval));
6310Sstevel@tonic-gate 	return(retval);
6320Sstevel@tonic-gate     }
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate     /*
6350Sstevel@tonic-gate      * Re-encode the keys in the new master key, if necessary.
6360Sstevel@tonic-gate      */
6370Sstevel@tonic-gate     if (mkey_convert) {
6380Sstevel@tonic-gate 	retval = master_key_convert(arg->kcontext, entry);
6390Sstevel@tonic-gate 	if (retval) {
6400Sstevel@tonic-gate 	    com_err(arg->programname, retval, remaster_err_fmt, name);
6410Sstevel@tonic-gate 	    return retval;
6420Sstevel@tonic-gate 	}
6430Sstevel@tonic-gate     }
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate     /*
6460Sstevel@tonic-gate      * If we don't have any match strings, or if our name matches, then
6470Sstevel@tonic-gate      * proceed with the dump, otherwise, just forget about it.
6480Sstevel@tonic-gate      */
6490Sstevel@tonic-gate     if (!arg->nnames || name_matches(name, arg)) {
6500Sstevel@tonic-gate 	/*
6510Sstevel@tonic-gate 	 * Deserialize the modifier record.
6520Sstevel@tonic-gate 	 */
6530Sstevel@tonic-gate 	mod_name = (char *) NULL;
6540Sstevel@tonic-gate 	mod_princ = NULL;
6550Sstevel@tonic-gate 	last_pwd_change = mod_date = 0;
6560Sstevel@tonic-gate 	pkey = akey = (krb5_key_data *) NULL;
6570Sstevel@tonic-gate 	if (!(retval = krb5_dbe_lookup_mod_princ_data(arg->kcontext,
6580Sstevel@tonic-gate 						      entry,
6590Sstevel@tonic-gate 						      &mod_date,
6600Sstevel@tonic-gate 						      &mod_princ))) {
6610Sstevel@tonic-gate 	    if (mod_princ) {
6620Sstevel@tonic-gate 		/*
6630Sstevel@tonic-gate 		 * Flatten the modifier name.
6640Sstevel@tonic-gate 		 */
6650Sstevel@tonic-gate 		if ((retval = krb5_unparse_name(arg->kcontext,
6660Sstevel@tonic-gate 						mod_princ,
6670Sstevel@tonic-gate 						&mod_name)))
6680Sstevel@tonic-gate 					fprintf(stderr, gettext(mname_unp_err),
6690Sstevel@tonic-gate 					    arg->programname,
6700Sstevel@tonic-gate 			    error_message(retval));
6710Sstevel@tonic-gate 		krb5_free_principal(arg->kcontext, mod_princ);
6720Sstevel@tonic-gate 	    }
6730Sstevel@tonic-gate 	}
6740Sstevel@tonic-gate 	if (!mod_name)
6750Sstevel@tonic-gate 	    mod_name = strdup(null_mprinc_name);
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	/*
6782881Smp153739 	 * Find the last password change record and set it straight.
6790Sstevel@tonic-gate 	 */
6800Sstevel@tonic-gate 	if ((retval =
6810Sstevel@tonic-gate 	     krb5_dbe_lookup_last_pwd_change(arg->kcontext, entry,
6822881Smp153739 					     &last_pwd_change))) {
6830Sstevel@tonic-gate 			fprintf(stderr, gettext(nokeys_err),
6840Sstevel@tonic-gate 			    arg->programname, name);
6850Sstevel@tonic-gate 	    krb5_xfree(mod_name);
6860Sstevel@tonic-gate 	    krb5_xfree(name);
6870Sstevel@tonic-gate 	    return(retval);
6880Sstevel@tonic-gate 	}
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	/*
6910Sstevel@tonic-gate 	 * Find the 'primary' key and the 'alternate' key.
6920Sstevel@tonic-gate 	 */
6930Sstevel@tonic-gate 	if ((retval = find_enctype(entry,
6940Sstevel@tonic-gate 				   ENCTYPE_DES_CBC_CRC,
6950Sstevel@tonic-gate 				   KRB5_KDB_SALTTYPE_NORMAL,
6960Sstevel@tonic-gate 				   &pkey)) &&
6970Sstevel@tonic-gate 	    (retval = find_enctype(entry,
6980Sstevel@tonic-gate 				   ENCTYPE_DES_CBC_CRC,
6990Sstevel@tonic-gate 				   KRB5_KDB_SALTTYPE_V4,
7000Sstevel@tonic-gate 				   &akey))) {
7010Sstevel@tonic-gate 			fprintf(stderr, gettext(nokeys_err),
7020Sstevel@tonic-gate 			    arg->programname, name);
7030Sstevel@tonic-gate 	    krb5_xfree(mod_name);
7040Sstevel@tonic-gate 	    krb5_xfree(name);
7050Sstevel@tonic-gate 	    return(retval);
7060Sstevel@tonic-gate 	}
7072881Smp153739 
7082881Smp153739 	/* If we only have one type, then ship it out as the primary. */
7090Sstevel@tonic-gate 	if (!pkey && akey) {
7100Sstevel@tonic-gate 	    pkey = akey;
7110Sstevel@tonic-gate 	    akey = &nullkey;
7122881Smp153739 	}
7132881Smp153739 	else {
7140Sstevel@tonic-gate 	    if (!akey)
7150Sstevel@tonic-gate 		akey = &nullkey;
7160Sstevel@tonic-gate 	}
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	/*
7192881Smp153739 	 * First put out strings representing the length of the variable
7202881Smp153739 	 * length data in this record, then the name and the primary key type.
7210Sstevel@tonic-gate 	 */
7222881Smp153739 	fprintf(arg->ofile, "%d\t%d\t%d\t%d\t%d\t%d\t%s\t%d\t", strlen(name),
7230Sstevel@tonic-gate 		strlen(mod_name),
7240Sstevel@tonic-gate 		(krb5_int32) pkey->key_data_length[0],
7250Sstevel@tonic-gate 		(krb5_int32) akey->key_data_length[0],
7260Sstevel@tonic-gate 		(krb5_int32) pkey->key_data_length[1],
7270Sstevel@tonic-gate 		(krb5_int32) akey->key_data_length[1],
7280Sstevel@tonic-gate 		name,
7290Sstevel@tonic-gate 		(krb5_int32) pkey->key_data_type[0]);
7300Sstevel@tonic-gate 	for (i=0; i<pkey->key_data_length[0]; i++) {
7312881Smp153739 	    fprintf(arg->ofile, "%02x", pkey->key_data_contents[0][i]);
7320Sstevel@tonic-gate 	}
7330Sstevel@tonic-gate 	/*
7342881Smp153739 	 * Second, print out strings representing the standard integer
7352881Smp153739 	 * data in this record.
7360Sstevel@tonic-gate 	 */
7370Sstevel@tonic-gate 	fprintf(arg->ofile,
7382881Smp153739 		"\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",
7390Sstevel@tonic-gate 		(krb5_int32) pkey->key_data_kvno,
7400Sstevel@tonic-gate 		entry->max_life, entry->max_renewable_life,
7412881Smp153739 		1 /* Fake mkvno */, entry->expiration, entry->pw_expiration,
7422881Smp153739 		last_pwd_change, entry->last_success, entry->last_failed,
7430Sstevel@tonic-gate 		entry->fail_auth_count, mod_name, mod_date,
7440Sstevel@tonic-gate 		entry->attributes, pkey->key_data_type[1]);
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 	/* Pound out the salt data, if present. */
7470Sstevel@tonic-gate 	for (i=0; i<pkey->key_data_length[1]; i++) {
7482881Smp153739 	    fprintf(arg->ofile, "%02x", pkey->key_data_contents[1][i]);
7490Sstevel@tonic-gate 	}
7500Sstevel@tonic-gate 	/* Pound out the alternate key type and contents */
7510Sstevel@tonic-gate 	fprintf(arg->ofile, "\t%u\t", akey->key_data_type[0]);
7520Sstevel@tonic-gate 	for (i=0; i<akey->key_data_length[0]; i++) {
7532881Smp153739 	    fprintf(arg->ofile, "%02x", akey->key_data_contents[0][i]);
7540Sstevel@tonic-gate 	}
7550Sstevel@tonic-gate 	/* Pound out the alternate salt type and contents */
7560Sstevel@tonic-gate 	fprintf(arg->ofile, "\t%u\t", akey->key_data_type[1]);
7570Sstevel@tonic-gate 	for (i=0; i<akey->key_data_length[1]; i++) {
7582881Smp153739 	    fprintf(arg->ofile, "%02x", akey->key_data_contents[1][i]);
7590Sstevel@tonic-gate 	}
7600Sstevel@tonic-gate 	/* Pound out the expansion data. (is null) */
7610Sstevel@tonic-gate 	for (i=0; i < 8; i++) {
7620Sstevel@tonic-gate 	    fprintf(arg->ofile, "\t%u", 0);
7630Sstevel@tonic-gate 	}
7640Sstevel@tonic-gate 	fprintf(arg->ofile, ";\n");
7650Sstevel@tonic-gate 	/* If we're blabbing, do it */
7660Sstevel@tonic-gate 	if (arg->verbose)
7670Sstevel@tonic-gate 	    fprintf(stderr, "%s\n", name);
7680Sstevel@tonic-gate 	krb5_xfree(mod_name);
7690Sstevel@tonic-gate     }
7700Sstevel@tonic-gate     krb5_xfree(name);
7710Sstevel@tonic-gate     return(0);
7720Sstevel@tonic-gate }
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate /*
7750Sstevel@tonic-gate  * dump_k5beta6_iterator()	- Output a dump record in krb5b6 format.
7760Sstevel@tonic-gate  */
7770Sstevel@tonic-gate static krb5_error_code
dump_k5beta6_iterator(ptr,entry)7780Sstevel@tonic-gate dump_k5beta6_iterator(ptr, entry)
7790Sstevel@tonic-gate     krb5_pointer	ptr;
7800Sstevel@tonic-gate     krb5_db_entry	*entry;
7810Sstevel@tonic-gate {
7822881Smp153739     return dump_k5beta6_iterator_ext(ptr, entry, 0);
7832881Smp153739 }
7842881Smp153739 
7852881Smp153739 static krb5_error_code
dump_k5beta6_iterator_ext(ptr,entry,kadm)7862881Smp153739 dump_k5beta6_iterator_ext(ptr, entry, kadm)
7872881Smp153739     krb5_pointer	ptr;
7882881Smp153739     krb5_db_entry	*entry;
7892881Smp153739     int			kadm;
7902881Smp153739 {
7910Sstevel@tonic-gate     krb5_error_code	retval;
7920Sstevel@tonic-gate     struct dump_args	*arg;
7930Sstevel@tonic-gate     char		*name;
7940Sstevel@tonic-gate     krb5_tl_data	*tlp;
7950Sstevel@tonic-gate     krb5_key_data	*kdata;
7960Sstevel@tonic-gate     int			counter, skip, i, j;
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate     /* Initialize */
7990Sstevel@tonic-gate     arg = (struct dump_args *) ptr;
8000Sstevel@tonic-gate     name = (char *) NULL;
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate     /*
8030Sstevel@tonic-gate      * Flatten the principal name.
8040Sstevel@tonic-gate      */
8050Sstevel@tonic-gate     if ((retval = krb5_unparse_name(arg->kcontext,
8060Sstevel@tonic-gate 				    entry->princ,
8070Sstevel@tonic-gate 				    &name))) {
8080Sstevel@tonic-gate 		fprintf(stderr, gettext(pname_unp_err),
8090Sstevel@tonic-gate 		arg->programname, error_message(retval));
8100Sstevel@tonic-gate 	return(retval);
8110Sstevel@tonic-gate     }
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate     /*
8140Sstevel@tonic-gate      * Re-encode the keys in the new master key, if necessary.
8150Sstevel@tonic-gate      */
8160Sstevel@tonic-gate     if (mkey_convert) {
8170Sstevel@tonic-gate 	retval = master_key_convert(arg->kcontext, entry);
8180Sstevel@tonic-gate 	if (retval) {
8190Sstevel@tonic-gate 	    com_err(arg->programname, retval, remaster_err_fmt, name);
8200Sstevel@tonic-gate 	    return retval;
8210Sstevel@tonic-gate 	}
8220Sstevel@tonic-gate     }
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate     /*
8250Sstevel@tonic-gate      * If we don't have any match strings, or if our name matches, then
8260Sstevel@tonic-gate      * proceed with the dump, otherwise, just forget about it.
8270Sstevel@tonic-gate      */
8280Sstevel@tonic-gate     if (!arg->nnames || name_matches(name, arg)) {
8290Sstevel@tonic-gate 	/*
8302881Smp153739 	 * We'd like to just blast out the contents as they would appear in
8312881Smp153739 	 * the database so that we can just suck it back in, but it doesn't
8322881Smp153739 	 * lend itself to easy editing.
8330Sstevel@tonic-gate 	 */
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	/*
8362881Smp153739 	 * The dump format is as follows:
8372881Smp153739 	 *	len strlen(name) n_tl_data n_key_data e_length
8382881Smp153739 	 *	name
8392881Smp153739 	 *	attributes max_life max_renewable_life expiration
8402881Smp153739 	 *	pw_expiration last_success last_failed fail_auth_count
8412881Smp153739 	 *	n_tl_data*[type length <contents>]
8422881Smp153739 	 *	n_key_data*[ver kvno ver*(type length <contents>)]
8432881Smp153739 	 *	<e_data>
8442881Smp153739 	 * Fields which are not encapsulated by angle-brackets are to appear
8452881Smp153739 	 * verbatim.  A bracketed field's absence is indicated by a -1 in its
8462881Smp153739 	 * place
8470Sstevel@tonic-gate 	 */
8480Sstevel@tonic-gate 
8492881Smp153739 	/*
8500Sstevel@tonic-gate 	 * Make sure that the tagged list is reasonably correct.
8510Sstevel@tonic-gate 	 */
8520Sstevel@tonic-gate 	counter = skip = 0;
8530Sstevel@tonic-gate 	for (tlp = entry->tl_data; tlp; tlp = tlp->tl_data_next) {
8542881Smp153739 	     /*
8552881Smp153739 	      * don't dump tl data types we know aren't understood by
8562881Smp153739 	      * earlier revisions [krb5-admin/89]
8572881Smp153739 	      */
8582881Smp153739 	     switch (tlp->tl_data_type) {
8592881Smp153739 	     case KRB5_TL_KADM_DATA:
8602881Smp153739 		  if (kadm)
8612881Smp153739 		      counter++;
8622881Smp153739 		  else
8632881Smp153739 		      skip++;
8642881Smp153739 		  break;
8652881Smp153739 	     default:
8662881Smp153739 		  counter++;
8672881Smp153739 		  break;
8682881Smp153739 	     }
8690Sstevel@tonic-gate 	}
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 	if (counter + skip == entry->n_tl_data) {
8720Sstevel@tonic-gate 	    /* Pound out header */
8730Sstevel@tonic-gate 	    fprintf(arg->ofile, "%d\t%d\t%d\t%d\t%d\t%s\t",
8740Sstevel@tonic-gate 		    (int) entry->len,
8750Sstevel@tonic-gate 		    strlen(name),
8760Sstevel@tonic-gate 		    counter,
8770Sstevel@tonic-gate 		    (int) entry->n_key_data,
8780Sstevel@tonic-gate 		    (int) entry->e_length,
8790Sstevel@tonic-gate 		    name);
8800Sstevel@tonic-gate 	    fprintf(arg->ofile, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t",
8810Sstevel@tonic-gate 		    entry->attributes,
8820Sstevel@tonic-gate 		    entry->max_life,
8830Sstevel@tonic-gate 		    entry->max_renewable_life,
8840Sstevel@tonic-gate 		    entry->expiration,
8850Sstevel@tonic-gate 		    entry->pw_expiration,
8860Sstevel@tonic-gate 		    entry->last_success,
8870Sstevel@tonic-gate 		    entry->last_failed,
8880Sstevel@tonic-gate 		    entry->fail_auth_count);
8890Sstevel@tonic-gate 	    /* Pound out tagged data. */
8902881Smp153739 	    for (tlp = entry->tl_data; tlp; tlp = tlp->tl_data_next) {
8912881Smp153739 		if (tlp->tl_data_type == KRB5_TL_KADM_DATA && !kadm)
8922881Smp153739 		     continue; /* see above, [krb5-admin/89] */
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 		fprintf(arg->ofile, "%d\t%d\t",
8950Sstevel@tonic-gate 			(int) tlp->tl_data_type,
8960Sstevel@tonic-gate 			(int) tlp->tl_data_length);
8970Sstevel@tonic-gate 		if (tlp->tl_data_length)
8982881Smp153739 		    for (i=0; i<tlp->tl_data_length; i++)
8992881Smp153739 			fprintf(arg->ofile, "%02x", tlp->tl_data_contents[i]);
9000Sstevel@tonic-gate 		else
9010Sstevel@tonic-gate 		    fprintf(arg->ofile, "%d", -1);
9020Sstevel@tonic-gate 		fprintf(arg->ofile, "\t");
9030Sstevel@tonic-gate 	    }
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 	    /* Pound out key data */
9062881Smp153739 	    for (counter=0; counter<entry->n_key_data; counter++) {
9070Sstevel@tonic-gate 		kdata = &entry->key_data[counter];
9080Sstevel@tonic-gate 		fprintf(arg->ofile, "%d\t%d\t",
9090Sstevel@tonic-gate 			(int) kdata->key_data_ver,
9100Sstevel@tonic-gate 			(int) kdata->key_data_kvno);
9110Sstevel@tonic-gate 		for (i=0; i<kdata->key_data_ver; i++) {
9120Sstevel@tonic-gate 		    fprintf(arg->ofile, "%d\t%d\t",
9130Sstevel@tonic-gate 			    kdata->key_data_type[i],
9140Sstevel@tonic-gate 			    kdata->key_data_length[i]);
9150Sstevel@tonic-gate 		    if (kdata->key_data_length[i])
9162881Smp153739 			for (j=0; j<kdata->key_data_length[i]; j++)
9172881Smp153739 			    fprintf(arg->ofile, "%02x",
9182881Smp153739 				    kdata->key_data_contents[i][j]);
9190Sstevel@tonic-gate 		    else
9200Sstevel@tonic-gate 			fprintf(arg->ofile, "%d", -1);
9210Sstevel@tonic-gate 		    fprintf(arg->ofile, "\t");
9220Sstevel@tonic-gate 		}
9230Sstevel@tonic-gate 	    }
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 	    /* Pound out extra data */
9260Sstevel@tonic-gate 	    if (entry->e_length)
9270Sstevel@tonic-gate 		for (i=0; i<entry->e_length; i++)
9282881Smp153739 		    fprintf(arg->ofile, "%02x", entry->e_data[i]);
9290Sstevel@tonic-gate 	    else
9300Sstevel@tonic-gate 		fprintf(arg->ofile, "%d", -1);
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 	    /* Print trailer */
9330Sstevel@tonic-gate 	    fprintf(arg->ofile, ";\n");
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	    if (arg->verbose)
9360Sstevel@tonic-gate 		fprintf(stderr, "%s\n", name);
9372881Smp153739 	}
9382881Smp153739 	else {
9390Sstevel@tonic-gate 			fprintf(stderr, gettext(sdump_tl_inc_err),
9402881Smp153739 		    arg->programname, name, counter+skip,
9410Sstevel@tonic-gate 		    (int) entry->n_tl_data);
9420Sstevel@tonic-gate 	    retval = EINVAL;
9430Sstevel@tonic-gate 	}
9440Sstevel@tonic-gate     }
9450Sstevel@tonic-gate     krb5_xfree(name);
9460Sstevel@tonic-gate     return(retval);
9470Sstevel@tonic-gate }
9482881Smp153739 
9490Sstevel@tonic-gate /*
9500Sstevel@tonic-gate  * dump_iprop_iterator()	- Output a dump record in iprop format.
9510Sstevel@tonic-gate  */
9520Sstevel@tonic-gate static krb5_error_code
dump_iprop_iterator(ptr,entry)9530Sstevel@tonic-gate dump_iprop_iterator(ptr, entry)
9540Sstevel@tonic-gate     krb5_pointer	ptr;
9550Sstevel@tonic-gate     krb5_db_entry	*entry;
9560Sstevel@tonic-gate {
9570Sstevel@tonic-gate     krb5_error_code	retval;
9580Sstevel@tonic-gate     struct dump_args	*arg;
9590Sstevel@tonic-gate     char		*name;
9600Sstevel@tonic-gate     krb5_tl_data	*tlp;
9610Sstevel@tonic-gate     krb5_key_data	*kdata;
9620Sstevel@tonic-gate     int			counter, i, j;
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate     /* Initialize */
9650Sstevel@tonic-gate     arg = (struct dump_args *) ptr;
9660Sstevel@tonic-gate     name = (char *) NULL;
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate     /*
9690Sstevel@tonic-gate      * Flatten the principal name.
9700Sstevel@tonic-gate      */
9710Sstevel@tonic-gate     if ((retval = krb5_unparse_name(arg->kcontext,
9720Sstevel@tonic-gate 				    entry->princ,
9730Sstevel@tonic-gate 				    &name))) {
9740Sstevel@tonic-gate 		fprintf(stderr, gettext(pname_unp_err),
9750Sstevel@tonic-gate 		arg->programname, error_message(retval));
9760Sstevel@tonic-gate 	return(retval);
9770Sstevel@tonic-gate     }
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate     /*
9800Sstevel@tonic-gate      * Re-encode the keys in the new master key, if necessary.
9810Sstevel@tonic-gate      */
9820Sstevel@tonic-gate     if (mkey_convert) {
9830Sstevel@tonic-gate 	retval = master_key_convert(arg->kcontext, entry);
9840Sstevel@tonic-gate 	if (retval) {
9850Sstevel@tonic-gate 	    com_err(arg->programname, retval, remaster_err_fmt, name);
9860Sstevel@tonic-gate 	    return retval;
9870Sstevel@tonic-gate 	}
9880Sstevel@tonic-gate     }
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate     /*
9910Sstevel@tonic-gate      * If we don't have any match strings, or if our name matches, then
9920Sstevel@tonic-gate      * proceed with the dump, otherwise, just forget about it.
9930Sstevel@tonic-gate      */
9940Sstevel@tonic-gate     if (!arg->nnames || name_matches(name, arg)) {
9950Sstevel@tonic-gate 	/*
9960Sstevel@tonic-gate 	 * We'd like to just blast out the contents as they would
9970Sstevel@tonic-gate 	 * appear in the database so that we can just suck it back
9980Sstevel@tonic-gate 	 * in, but it doesn't lend itself to easy editing.
9990Sstevel@tonic-gate 	 */
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 	/*
10020Sstevel@tonic-gate 	 * The dump format is as follows: len strlen(name)
10030Sstevel@tonic-gate 	 * n_tl_data n_key_data e_length name attributes max_life
10040Sstevel@tonic-gate 	 * max_renewable_life expiration pw_expiration last_success
10050Sstevel@tonic-gate 	 * last_failed fail_auth_count n_tl_data*[type length
10060Sstevel@tonic-gate 	 * <contents>] n_key_data*[ver kvno ver*(type length
10070Sstevel@tonic-gate 	 * <contents>)] <e_data> Fields which are not encapsulated
10080Sstevel@tonic-gate 	 * by angle-brackets are to appear verbatim.  Bracketed
10090Sstevel@tonic-gate 	 * fields absence is indicated by a -1 in its place
10100Sstevel@tonic-gate 	 */
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate 	/*
10130Sstevel@tonic-gate 	 * Make sure that the tagged list is reasonably correct.
10140Sstevel@tonic-gate 	 */
10150Sstevel@tonic-gate 	counter = 0;
10160Sstevel@tonic-gate 	for (tlp = entry->tl_data; tlp; tlp = tlp->tl_data_next)
10170Sstevel@tonic-gate 		  counter++;
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 	if (counter == entry->n_tl_data) {
10200Sstevel@tonic-gate 	    /* Pound out header */
10210Sstevel@tonic-gate 	    fprintf(arg->ofile, "%d\t%d\t%d\t%d\t%d\t%s\t",
10220Sstevel@tonic-gate 		    (int) entry->len,
10230Sstevel@tonic-gate 		    strlen(name),
10240Sstevel@tonic-gate 		    (int) entry->n_tl_data,
10250Sstevel@tonic-gate 		    (int) entry->n_key_data,
10260Sstevel@tonic-gate 		    (int) entry->e_length,
10270Sstevel@tonic-gate 		    name);
10280Sstevel@tonic-gate 	    fprintf(arg->ofile, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t",
10290Sstevel@tonic-gate 		    entry->attributes,
10300Sstevel@tonic-gate 		    entry->max_life,
10310Sstevel@tonic-gate 		    entry->max_renewable_life,
10320Sstevel@tonic-gate 		    entry->expiration,
10330Sstevel@tonic-gate 		    entry->pw_expiration,
10340Sstevel@tonic-gate 		    entry->last_success,
10350Sstevel@tonic-gate 		    entry->last_failed,
10360Sstevel@tonic-gate 		    entry->fail_auth_count);
10370Sstevel@tonic-gate 	    /* Pound out tagged data. */
10380Sstevel@tonic-gate 			for (tlp = entry->tl_data; tlp;
10390Sstevel@tonic-gate 			    tlp = tlp->tl_data_next) {
10400Sstevel@tonic-gate 		fprintf(arg->ofile, "%d\t%d\t",
10410Sstevel@tonic-gate 			(int) tlp->tl_data_type,
10420Sstevel@tonic-gate 			(int) tlp->tl_data_length);
10430Sstevel@tonic-gate 		if (tlp->tl_data_length)
10440Sstevel@tonic-gate 					for (i = 0;
10450Sstevel@tonic-gate 					    i < tlp->tl_data_length;
10460Sstevel@tonic-gate 					    i++)
10470Sstevel@tonic-gate 						fprintf(arg->ofile, "%02x",
10480Sstevel@tonic-gate 							tlp->
10490Sstevel@tonic-gate 							tl_data_contents[i]);
10500Sstevel@tonic-gate 		else
10510Sstevel@tonic-gate 		    fprintf(arg->ofile, "%d", -1);
10520Sstevel@tonic-gate 		fprintf(arg->ofile, "\t");
10530Sstevel@tonic-gate 	    }
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 	    /* Pound out key data */
10560Sstevel@tonic-gate 			for (counter = 0;
10570Sstevel@tonic-gate 			    counter < entry->n_key_data; counter++) {
10580Sstevel@tonic-gate 		kdata = &entry->key_data[counter];
10590Sstevel@tonic-gate 		fprintf(arg->ofile, "%d\t%d\t",
10600Sstevel@tonic-gate 			(int) kdata->key_data_ver,
10610Sstevel@tonic-gate 			(int) kdata->key_data_kvno);
10620Sstevel@tonic-gate 		for (i=0; i<kdata->key_data_ver; i++) {
10630Sstevel@tonic-gate 		    fprintf(arg->ofile, "%d\t%d\t",
10640Sstevel@tonic-gate 			    kdata->key_data_type[i],
10650Sstevel@tonic-gate 			    kdata->key_data_length[i]);
10660Sstevel@tonic-gate 		    if (kdata->key_data_length[i])
10670Sstevel@tonic-gate 						for (j = 0;
10680Sstevel@tonic-gate 						    j < kdata->
10690Sstevel@tonic-gate 							key_data_length[i];
10700Sstevel@tonic-gate 						    j++)
10710Sstevel@tonic-gate 							fprintf(arg->ofile,
10720Sstevel@tonic-gate 							    "%02x",
10730Sstevel@tonic-gate 							    kdata->
10740Sstevel@tonic-gate 							    key_data_contents
10750Sstevel@tonic-gate 								[i][j]);
10760Sstevel@tonic-gate 		    else
10770Sstevel@tonic-gate 			fprintf(arg->ofile, "%d", -1);
10780Sstevel@tonic-gate 		    fprintf(arg->ofile, "\t");
10790Sstevel@tonic-gate 		}
10800Sstevel@tonic-gate 	    }
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 	    /* Pound out extra data */
10830Sstevel@tonic-gate 	    if (entry->e_length)
10840Sstevel@tonic-gate 		for (i=0; i<entry->e_length; i++)
10850Sstevel@tonic-gate 					fprintf(arg->ofile, "%02x",
10860Sstevel@tonic-gate 						entry->e_data[i]);
10870Sstevel@tonic-gate 	    else
10880Sstevel@tonic-gate 		fprintf(arg->ofile, "%d", -1);
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 	    /* Print trailer */
10910Sstevel@tonic-gate 	    fprintf(arg->ofile, ";\n");
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 	    if (arg->verbose)
10940Sstevel@tonic-gate 		fprintf(stderr, "%s\n", name);
10950Sstevel@tonic-gate 		} else {
10960Sstevel@tonic-gate 			fprintf(stderr, gettext(sdump_tl_inc_err),
10970Sstevel@tonic-gate 		    arg->programname, name, counter,
10980Sstevel@tonic-gate 		    (int) entry->n_tl_data);
10990Sstevel@tonic-gate 	    retval = EINVAL;
11000Sstevel@tonic-gate 	}
11010Sstevel@tonic-gate     }
11020Sstevel@tonic-gate     krb5_xfree(name);
11030Sstevel@tonic-gate     return(retval);
11040Sstevel@tonic-gate }
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate /*
11070Sstevel@tonic-gate  * dump_k5beta7_iterator()	- Output a dump record in krb5b7 format.
11080Sstevel@tonic-gate  */
11090Sstevel@tonic-gate static krb5_error_code
dump_k5beta7_princ(ptr,entry)11100Sstevel@tonic-gate dump_k5beta7_princ(ptr, entry)
11110Sstevel@tonic-gate     krb5_pointer	ptr;
11120Sstevel@tonic-gate     krb5_db_entry	*entry;
11130Sstevel@tonic-gate {
11142881Smp153739     return dump_k5beta7_princ_ext(ptr, entry, 0);
11152881Smp153739 }
11162881Smp153739 
11172881Smp153739 static krb5_error_code
dump_k5beta7_princ_ext(ptr,entry,kadm)11182881Smp153739 dump_k5beta7_princ_ext(ptr, entry, kadm)
11192881Smp153739     krb5_pointer	ptr;
11202881Smp153739     krb5_db_entry	*entry;
11212881Smp153739     int			kadm;
11222881Smp153739 {
11230Sstevel@tonic-gate      krb5_error_code retval;
11240Sstevel@tonic-gate      struct dump_args *arg;
11250Sstevel@tonic-gate      char *name;
11260Sstevel@tonic-gate      int tmp_nnames;
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate      /* Initialize */
11290Sstevel@tonic-gate      arg = (struct dump_args *) ptr;
11300Sstevel@tonic-gate      name = (char *) NULL;
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate      /*
11330Sstevel@tonic-gate       * Flatten the principal name.
11340Sstevel@tonic-gate       */
11350Sstevel@tonic-gate      if ((retval = krb5_unparse_name(arg->kcontext,
11360Sstevel@tonic-gate 				     entry->princ,
11370Sstevel@tonic-gate 				     &name))) {
11380Sstevel@tonic-gate 		fprintf(stderr, gettext(pname_unp_err),
11390Sstevel@tonic-gate 		  arg->programname, error_message(retval));
11400Sstevel@tonic-gate 	  return(retval);
11410Sstevel@tonic-gate      }
11420Sstevel@tonic-gate      /*
11430Sstevel@tonic-gate       * If we don't have any match strings, or if our name matches, then
11440Sstevel@tonic-gate       * proceed with the dump, otherwise, just forget about it.
11450Sstevel@tonic-gate       */
11460Sstevel@tonic-gate      if (!arg->nnames || name_matches(name, arg)) {
11470Sstevel@tonic-gate 	  fprintf(arg->ofile, "princ\t");
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 	  /* save the callee from matching the name again */
11500Sstevel@tonic-gate 	  tmp_nnames = arg->nnames;
11510Sstevel@tonic-gate 	  arg->nnames = 0;
11522881Smp153739 	  retval = dump_k5beta6_iterator_ext(ptr, entry, kadm);
11530Sstevel@tonic-gate 	  arg->nnames = tmp_nnames;
11540Sstevel@tonic-gate      }
11552881Smp153739 
11560Sstevel@tonic-gate      free(name);
11572881Smp153739      return retval;
11580Sstevel@tonic-gate }
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate /*
11610Sstevel@tonic-gate  * dump_iprop_princ()	- Output a dump record in iprop format.
11620Sstevel@tonic-gate  * This was created in order to dump more data, such as kadm5 tl
11630Sstevel@tonic-gate  */
11640Sstevel@tonic-gate static krb5_error_code
dump_iprop_princ(ptr,entry)11650Sstevel@tonic-gate dump_iprop_princ(ptr, entry)
11660Sstevel@tonic-gate     krb5_pointer	ptr;
11670Sstevel@tonic-gate     krb5_db_entry	*entry;
11680Sstevel@tonic-gate {
11690Sstevel@tonic-gate      krb5_error_code retval;
11700Sstevel@tonic-gate      struct dump_args *arg;
11710Sstevel@tonic-gate      char *name;
11720Sstevel@tonic-gate      int tmp_nnames;
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate      /* Initialize */
11750Sstevel@tonic-gate      arg = (struct dump_args *) ptr;
11760Sstevel@tonic-gate      name = (char *) NULL;
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate      /*
11790Sstevel@tonic-gate       * Flatten the principal name.
11800Sstevel@tonic-gate       */
11810Sstevel@tonic-gate      if ((retval = krb5_unparse_name(arg->kcontext,
11820Sstevel@tonic-gate 				     entry->princ,
11830Sstevel@tonic-gate 				     &name))) {
11840Sstevel@tonic-gate 		fprintf(stderr, gettext(pname_unp_err),
11850Sstevel@tonic-gate 		  arg->programname, error_message(retval));
11860Sstevel@tonic-gate 	  return(retval);
11870Sstevel@tonic-gate      }
11880Sstevel@tonic-gate      /*
11890Sstevel@tonic-gate       * If we don't have any match strings, or if our name matches, then
11900Sstevel@tonic-gate       * proceed with the dump, otherwise, just forget about it.
11910Sstevel@tonic-gate       */
11920Sstevel@tonic-gate      if (!arg->nnames || name_matches(name, arg)) {
11930Sstevel@tonic-gate 	  fprintf(arg->ofile, "princ\t");
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 	  /* save the callee from matching the name again */
11960Sstevel@tonic-gate 	  tmp_nnames = arg->nnames;
11970Sstevel@tonic-gate 	  arg->nnames = 0;
11980Sstevel@tonic-gate 	  retval = dump_iprop_iterator(ptr, entry);
11990Sstevel@tonic-gate 	  arg->nnames = tmp_nnames;
12000Sstevel@tonic-gate      }
12010Sstevel@tonic-gate      free(name);
12020Sstevel@tonic-gate 	return (retval);
12030Sstevel@tonic-gate }
12042881Smp153739 
12052881Smp153739 static krb5_error_code
dump_k5beta7_princ_withpolicy(ptr,entry)12062881Smp153739 dump_k5beta7_princ_withpolicy(ptr, entry)
12072881Smp153739     krb5_pointer	ptr;
12082881Smp153739     krb5_db_entry	*entry;
12092881Smp153739 {
12102881Smp153739     return dump_k5beta7_princ_ext(ptr, entry, 1);
12112881Smp153739 }
12122881Smp153739 
dump_k5beta7_policy(void * data,osa_policy_ent_t entry)12132881Smp153739 void dump_k5beta7_policy(void *data, osa_policy_ent_t entry)
12140Sstevel@tonic-gate {
12150Sstevel@tonic-gate      struct dump_args *arg;
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate      arg = (struct dump_args *) data;
12180Sstevel@tonic-gate      fprintf(arg->ofile, "policy\t%s\t%d\t%d\t%d\t%d\t%d\t%d\n", entry->name,
12190Sstevel@tonic-gate 	     entry->pw_min_life, entry->pw_max_life, entry->pw_min_length,
12200Sstevel@tonic-gate 	     entry->pw_min_classes, entry->pw_history_num,
12210Sstevel@tonic-gate 	     entry->policy_refcnt);
12220Sstevel@tonic-gate }
12230Sstevel@tonic-gate 
print_key_data(FILE * f,krb5_key_data * key_data)12242881Smp153739 static void print_key_data(FILE *f, krb5_key_data *key_data)
12250Sstevel@tonic-gate {
12260Sstevel@tonic-gate      int c;
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate      fprintf(f, "%d\t%d\t", key_data->key_data_type[0],
12290Sstevel@tonic-gate 	     key_data->key_data_length[0]);
12300Sstevel@tonic-gate      for(c = 0; c < key_data->key_data_length[0]; c++)
12310Sstevel@tonic-gate 	  fprintf(f, "%02x ",
12320Sstevel@tonic-gate 		  key_data->key_data_contents[0][c]);
12330Sstevel@tonic-gate }
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate /*
12360Sstevel@tonic-gate  * Function: print_princ
12370Sstevel@tonic-gate  *
12380Sstevel@tonic-gate  * Purpose: output osa_adb_princ_ent data in a human
12390Sstevel@tonic-gate  *	    readable format (which is a format suitable for
12400Sstevel@tonic-gate  *	    ovsec_adm_import consumption)
12410Sstevel@tonic-gate  *
12420Sstevel@tonic-gate  * Arguments:
12430Sstevel@tonic-gate  *	data		(input) pointer to a structure containing a FILE *
12440Sstevel@tonic-gate  *			        and a record counter.
12450Sstevel@tonic-gate  *	entry		(input) entry to get dumped.
12460Sstevel@tonic-gate  * 	<return value>	void
12470Sstevel@tonic-gate  *
12480Sstevel@tonic-gate  * Requires:
12490Sstevel@tonic-gate  *	nuttin
12500Sstevel@tonic-gate  *
12510Sstevel@tonic-gate  * Effects:
12520Sstevel@tonic-gate  *	writes data to the specified file pointerp.
12530Sstevel@tonic-gate  *
12540Sstevel@tonic-gate  * Modifies:
12550Sstevel@tonic-gate  *	nuttin
12560Sstevel@tonic-gate  *
12570Sstevel@tonic-gate  */
dump_ov_princ(krb5_pointer ptr,krb5_db_entry * kdb)12582881Smp153739 static krb5_error_code dump_ov_princ(krb5_pointer ptr, krb5_db_entry *kdb)
12590Sstevel@tonic-gate {
12600Sstevel@tonic-gate     char *princstr;
12612881Smp153739     int	x, y, foundcrc;
12620Sstevel@tonic-gate     struct dump_args *arg;
12630Sstevel@tonic-gate     krb5_tl_data tl_data;
12640Sstevel@tonic-gate     osa_princ_ent_rec adb;
12650Sstevel@tonic-gate     XDR xdrs;
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate     arg = (struct dump_args *) ptr;
12680Sstevel@tonic-gate     /*
12690Sstevel@tonic-gate      * XXX Currently, lookup_tl_data always returns zero; it sets
12702881Smp153739      * tl_data->tl_data_length to zero if the type isn't found.
12712881Smp153739      * This should be fixed...
12720Sstevel@tonic-gate      */
12730Sstevel@tonic-gate     /*
12740Sstevel@tonic-gate      * XXX Should this function do nothing for a principal with no
12752881Smp153739      * admin data, or print a record of "default" values?   See
12762881Smp153739      * comment in server_kdb.c to help decide.
12770Sstevel@tonic-gate      */
12780Sstevel@tonic-gate     tl_data.tl_data_type = KRB5_TL_KADM_DATA;
12792881Smp153739     if (krb5_dbe_lookup_tl_data(arg->kcontext, kdb, &tl_data)
12802881Smp153739 	|| (tl_data.tl_data_length == 0))
12812881Smp153739 	 return 0;
12820Sstevel@tonic-gate 
12830Sstevel@tonic-gate     memset(&adb, 0, sizeof(adb));
12842881Smp153739     xdrmem_create(&xdrs, (const caddr_t) tl_data.tl_data_contents,
12850Sstevel@tonic-gate 		  tl_data.tl_data_length, XDR_DECODE);
12860Sstevel@tonic-gate     if (! xdr_osa_princ_ent_rec(&xdrs, &adb)) {
12870Sstevel@tonic-gate 	 xdr_destroy(&xdrs);
12884960Swillf 	 return(KADM5_XDR_FAILURE);
12890Sstevel@tonic-gate     }
12900Sstevel@tonic-gate     xdr_destroy(&xdrs);
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate     krb5_unparse_name(arg->kcontext, kdb->princ, &princstr);
12930Sstevel@tonic-gate     fprintf(arg->ofile, "princ\t%s\t", princstr);
12940Sstevel@tonic-gate     if(adb.policy == NULL)
12950Sstevel@tonic-gate 	fputc('\t', arg->ofile);
12960Sstevel@tonic-gate     else
12970Sstevel@tonic-gate 	fprintf(arg->ofile, "%s\t", adb.policy);
12982881Smp153739     fprintf(arg->ofile, "%lx\t%d\t%d\t%d", adb.aux_attributes,
12990Sstevel@tonic-gate 	    adb.old_key_len,adb.old_key_next, adb.admin_history_kvno);
13000Sstevel@tonic-gate 
13010Sstevel@tonic-gate     for (x = 0; x < adb.old_key_len; x++) {
13020Sstevel@tonic-gate 	 foundcrc = 0;
13030Sstevel@tonic-gate 	 for (y = 0; y < adb.old_keys[x].n_key_data; y++) {
13040Sstevel@tonic-gate 	      krb5_key_data *key_data = &adb.old_keys[x].key_data[y];
13050Sstevel@tonic-gate 
13060Sstevel@tonic-gate 	      if (key_data->key_data_type[0] != ENCTYPE_DES_CBC_CRC)
13070Sstevel@tonic-gate 		   continue;
13080Sstevel@tonic-gate 	      if (foundcrc) {
13090Sstevel@tonic-gate 				fprintf(stderr,
13100Sstevel@tonic-gate 				    gettext("Warning!  Multiple DES-CBC-CRC "
13110Sstevel@tonic-gate 					"keys for principal %s; skipping "
13120Sstevel@tonic-gate 					"duplicates.\n"),
13130Sstevel@tonic-gate 			   princstr);
13140Sstevel@tonic-gate 		   continue;
13150Sstevel@tonic-gate 	      }
13160Sstevel@tonic-gate 	      foundcrc++;
13170Sstevel@tonic-gate 
13180Sstevel@tonic-gate 	      fputc('\t', arg->ofile);
13190Sstevel@tonic-gate 	      print_key_data(arg->ofile, key_data);
13200Sstevel@tonic-gate 	 }
13210Sstevel@tonic-gate 	 if (!foundcrc)
13220Sstevel@tonic-gate 			fprintf(stderr,
13230Sstevel@tonic-gate 			    gettext("Warning!  No DES-CBC-CRC key "
13240Sstevel@tonic-gate 				"for principal %s, cannot generate "
13250Sstevel@tonic-gate 				"OV-compatible record; skipping\n"),
13260Sstevel@tonic-gate 		      princstr);
13270Sstevel@tonic-gate     }
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate     fputc('\n', arg->ofile);
13300Sstevel@tonic-gate     free(princstr);
13312881Smp153739     return 0;
13320Sstevel@tonic-gate }
13330Sstevel@tonic-gate 
13340Sstevel@tonic-gate /*
13350Sstevel@tonic-gate  * usage is:
13362881Smp153739  *	dump_db [-i] [-old] [-b6] [-b7] [-ov] [-verbose] [-mkey_convert]
13372881Smp153739  *		[-new_mkey_file mkey_file] [-rev] [-recurse]
13382881Smp153739  *		[filename [principals...]]
13390Sstevel@tonic-gate  */
13400Sstevel@tonic-gate void
dump_db(argc,argv)13410Sstevel@tonic-gate dump_db(argc, argv)
13420Sstevel@tonic-gate     int		argc;
13430Sstevel@tonic-gate     char	**argv;
13440Sstevel@tonic-gate {
13450Sstevel@tonic-gate     FILE		*f;
13460Sstevel@tonic-gate     struct dump_args	arglist;
1347*8092SMark.Phalan@Sun.COM /* Solaris Kerberos */
1348*8092SMark.Phalan@Sun.COM #if 0
13490Sstevel@tonic-gate     char		*programname;
1350*8092SMark.Phalan@Sun.COM #endif
13510Sstevel@tonic-gate     char		*ofile;
13520Sstevel@tonic-gate     krb5_error_code	kret, retval;
13530Sstevel@tonic-gate     dump_version	*dump;
13540Sstevel@tonic-gate     int			aindex;
13550Sstevel@tonic-gate     krb5_boolean	locked;
13560Sstevel@tonic-gate     char		*new_mkey_file = 0;
13570Sstevel@tonic-gate     bool_t		dump_sno = FALSE;
13580Sstevel@tonic-gate     kdb_log_context	*log_ctx;
13595916Swillf     /* Solaris Kerberos: adding support for -rev/recurse flags */
13605916Swillf     int			db_arg_index = 0;
13615916Swillf     char		*db_args[3] = {NULL, NULL, NULL};
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate     /*
13640Sstevel@tonic-gate      * Parse the arguments.
13650Sstevel@tonic-gate      */
1366*8092SMark.Phalan@Sun.COM /* Solaris Kerberos */
1367*8092SMark.Phalan@Sun.COM #if 0
13680Sstevel@tonic-gate     programname = argv[0];
13690Sstevel@tonic-gate     if (strrchr(programname, (int) '/'))
13700Sstevel@tonic-gate 	programname = strrchr(argv[0], (int) '/') + 1;
1371*8092SMark.Phalan@Sun.COM #endif
13720Sstevel@tonic-gate     ofile = (char *) NULL;
13732881Smp153739     dump = &r1_3_version;
13740Sstevel@tonic-gate     arglist.verbose = 0;
13750Sstevel@tonic-gate     new_mkey_file = 0;
13760Sstevel@tonic-gate     mkey_convert = 0;
13772881Smp153739     backwards = 0;
13782881Smp153739     recursive = 0;
13790Sstevel@tonic-gate     log_ctx = util_context->kdblog_context;
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate     /*
13820Sstevel@tonic-gate      * Parse the qualifiers.
13830Sstevel@tonic-gate      */
13840Sstevel@tonic-gate     for (aindex = 1; aindex < argc; aindex++) {
13852881Smp153739 	if (!strcmp(argv[aindex], oldoption))
13860Sstevel@tonic-gate 	     dump = &old_version;
13872881Smp153739 	else if (!strcmp(argv[aindex], b6option))
13880Sstevel@tonic-gate 	     dump = &beta6_version;
13892881Smp153739 	else if (!strcmp(argv[aindex], b7option))
13902881Smp153739 	     dump = &beta7_version;
13912881Smp153739 	else if (!strcmp(argv[aindex], ovoption))
13920Sstevel@tonic-gate 	     dump = &ov_version;
13932881Smp153739 	else if (!strcmp(argv[aindex], ipropoption)) {
13940Sstevel@tonic-gate 			if (log_ctx && log_ctx->iproprole) {
13950Sstevel@tonic-gate 				dump = &iprop_version;
13960Sstevel@tonic-gate 				/*
13970Sstevel@tonic-gate 				 * dump_sno is used to indicate if the serial
13980Sstevel@tonic-gate 				 * # should be populated in the output
13990Sstevel@tonic-gate 				 * file to be used later by iprop for updating
14000Sstevel@tonic-gate 				 * the slave's update log when loading
14010Sstevel@tonic-gate 				 */
14020Sstevel@tonic-gate 				dump_sno = TRUE;
14030Sstevel@tonic-gate 			} else {
14040Sstevel@tonic-gate 				fprintf(stderr, gettext("Iprop not enabled\n"));
14050Sstevel@tonic-gate 				exit_status++;
14060Sstevel@tonic-gate 				return;
14070Sstevel@tonic-gate 			}
14080Sstevel@tonic-gate 		}
14092881Smp153739 	else if (!strcmp(argv[aindex], verboseoption))
14100Sstevel@tonic-gate 	    arglist.verbose++;
14110Sstevel@tonic-gate 	else if (!strcmp(argv[aindex], "-mkey_convert"))
14120Sstevel@tonic-gate 	    mkey_convert = 1;
14130Sstevel@tonic-gate 	else if (!strcmp(argv[aindex], "-new_mkey_file")) {
14140Sstevel@tonic-gate 	    new_mkey_file = argv[++aindex];
14150Sstevel@tonic-gate 	    mkey_convert = 1;
14165916Swillf         } else if (!strcmp(argv[aindex], "-rev")) {
14175916Swillf 	    /* Solaris Kerberos: adding support for -rev/recurse flags */
14185916Swillf 	    /* hack to pass args to db specific plugin */
14195916Swillf 	    db_args[db_arg_index++] = "rev";
14205916Swillf 	} else if (!strcmp(argv[aindex], "-recurse")) {
14215916Swillf 	    /* hack to pass args to db specific plugin */
14225916Swillf 	    db_args[db_arg_index++] = "recurse";
14235916Swillf 	} else
14240Sstevel@tonic-gate 	    break;
14250Sstevel@tonic-gate     }
14260Sstevel@tonic-gate 
14270Sstevel@tonic-gate     arglist.names = (char **) NULL;
14280Sstevel@tonic-gate     arglist.nnames = 0;
14290Sstevel@tonic-gate     if (aindex < argc) {
14300Sstevel@tonic-gate 	ofile = argv[aindex];
14310Sstevel@tonic-gate 	aindex++;
14320Sstevel@tonic-gate 	if (aindex < argc) {
14330Sstevel@tonic-gate 	    arglist.names = &argv[aindex];
14340Sstevel@tonic-gate 	    arglist.nnames = argc - aindex;
14350Sstevel@tonic-gate 	}
14360Sstevel@tonic-gate     }
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate     /*
14390Sstevel@tonic-gate      * Make sure the database is open.  The policy database only has
14400Sstevel@tonic-gate      * to be opened if we try a dump that uses it.
14410Sstevel@tonic-gate      */
14424960Swillf     if (!dbactive) {
1443*8092SMark.Phalan@Sun.COM 	/* Solaris Kerberos */
1444*8092SMark.Phalan@Sun.COM 	com_err(progname, 0, Err_no_database); /* Solaris Kerberos */
14450Sstevel@tonic-gate 	exit_status++;
14460Sstevel@tonic-gate 	return;
14470Sstevel@tonic-gate     }
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate     /*
14500Sstevel@tonic-gate      * If we're doing a master key conversion, set up for it.
14510Sstevel@tonic-gate      */
14520Sstevel@tonic-gate     if (mkey_convert) {
14530Sstevel@tonic-gate 	    if (!valid_master_key) {
14540Sstevel@tonic-gate 		    /* TRUE here means read the keyboard, but only once */
14550Sstevel@tonic-gate 		    retval = krb5_db_fetch_mkey(util_context,
14560Sstevel@tonic-gate 						master_princ,
14570Sstevel@tonic-gate 						global_params.enctype,
14580Sstevel@tonic-gate 						TRUE, FALSE,
14590Sstevel@tonic-gate 						(char *) NULL, 0,
14600Sstevel@tonic-gate 						&master_key);
14610Sstevel@tonic-gate 		    if (retval) {
1462*8092SMark.Phalan@Sun.COM 			    /* Solaris Kerberos */
1463*8092SMark.Phalan@Sun.COM 			    com_err(progname, retval,
14640Sstevel@tonic-gate 				    gettext("while reading master key"));
14650Sstevel@tonic-gate 			    exit(1);
14660Sstevel@tonic-gate 		    }
14670Sstevel@tonic-gate 		    retval = krb5_db_verify_master_key(util_context,
14680Sstevel@tonic-gate 						       master_princ,
14690Sstevel@tonic-gate 						       &master_key);
14700Sstevel@tonic-gate 		    if (retval) {
1471*8092SMark.Phalan@Sun.COM 			    /* Solaris Kerberos */
1472*8092SMark.Phalan@Sun.COM 			    com_err(progname, retval,
14730Sstevel@tonic-gate 				    gettext("while verifying master key"));
14740Sstevel@tonic-gate 			    exit(1);
14750Sstevel@tonic-gate 		    }
14760Sstevel@tonic-gate 	    }
14770Sstevel@tonic-gate 	    if (!new_mkey_file)
14780Sstevel@tonic-gate 		    printf(gettext("Please enter new master key....\n"));
14790Sstevel@tonic-gate 	    if ((retval = krb5_db_fetch_mkey(util_context, master_princ,
14800Sstevel@tonic-gate 					     global_params.enctype,
14812881Smp153739 					     (new_mkey_file == 0) ?
14822881Smp153739 					        (krb5_boolean) 1 : 0,
14832881Smp153739 					     TRUE,
14840Sstevel@tonic-gate 					     new_mkey_file, 0,
14850Sstevel@tonic-gate 					     &new_master_key))) {
1486*8092SMark.Phalan@Sun.COM 		    /* Solaris Kerberos */
1487*8092SMark.Phalan@Sun.COM 		    com_err(progname, retval,
14880Sstevel@tonic-gate 			    gettext("while reading new master key"));
14890Sstevel@tonic-gate 		    exit(1);
14900Sstevel@tonic-gate 	    }
14910Sstevel@tonic-gate     }
14920Sstevel@tonic-gate 
14930Sstevel@tonic-gate     kret = 0;
14940Sstevel@tonic-gate     locked = 0;
14950Sstevel@tonic-gate     if (ofile && strcmp(ofile, "-")) {
14960Sstevel@tonic-gate 	/*
14972881Smp153739 	 * Discourage accidental dumping to filenames beginning with '-'.
14982881Smp153739 	 */
14992881Smp153739 	if (ofile[0] == '-')
15002881Smp153739 	    usage();
15012881Smp153739 	/*
15020Sstevel@tonic-gate 	 * Make sure that we don't open and truncate on the fopen,
15030Sstevel@tonic-gate 	 * since that may hose an on-going kprop process.
15040Sstevel@tonic-gate 	 *
15052881Smp153739 	 * We could also control this by opening for read and
15062881Smp153739 	 * write, doing an flock with LOCK_EX, and then
15072881Smp153739 	 * truncating the file once we have gotten the lock,
15082881Smp153739 	 * but that would involve more OS dependencies than I
15092881Smp153739 	 * want to get into.
15100Sstevel@tonic-gate 	 */
15110Sstevel@tonic-gate 	unlink(ofile);
15120Sstevel@tonic-gate 	if (!(f = fopen(ofile, "w"))) {
1513*8092SMark.Phalan@Sun.COM 			/* Solaris Kerberos */
15140Sstevel@tonic-gate 			fprintf(stderr, gettext(ofopen_error),
1515*8092SMark.Phalan@Sun.COM 		    progname, ofile, error_message(errno));
15160Sstevel@tonic-gate 	    exit_status++;
15170Sstevel@tonic-gate 	    return;
15180Sstevel@tonic-gate        }
15190Sstevel@tonic-gate 	if ((kret = krb5_lock_file(util_context,
15200Sstevel@tonic-gate 				   fileno(f),
15210Sstevel@tonic-gate 				   KRB5_LOCKMODE_EXCLUSIVE))) {
1522*8092SMark.Phalan@Sun.COM 			/* Solaris Kerberos */
15230Sstevel@tonic-gate 			fprintf(stderr, gettext(oflock_error),
1524*8092SMark.Phalan@Sun.COM 		    progname, ofile, error_message(kret));
15250Sstevel@tonic-gate 	    exit_status++;
15262881Smp153739 	}
15272881Smp153739 	else
15280Sstevel@tonic-gate 	    locked = 1;
15290Sstevel@tonic-gate     } else {
15300Sstevel@tonic-gate 	f = stdout;
15310Sstevel@tonic-gate     }
15320Sstevel@tonic-gate     if (f && !(kret)) {
1533*8092SMark.Phalan@Sun.COM 	/* Solaris Kerberos */
1534*8092SMark.Phalan@Sun.COM 	arglist.programname = progname;
15350Sstevel@tonic-gate 	arglist.ofile = f;
15360Sstevel@tonic-gate 	arglist.kcontext = util_context;
15370Sstevel@tonic-gate 	fprintf(arglist.ofile, "%s", dump->header);
15380Sstevel@tonic-gate 
15390Sstevel@tonic-gate 	if (dump_sno) {
15400Sstevel@tonic-gate 		if (ulog_map(util_context, &global_params, FKCOMMAND)) {
1541*8092SMark.Phalan@Sun.COM 			/* Solaris Kerberos */
15420Sstevel@tonic-gate 			fprintf(stderr,
1543*8092SMark.Phalan@Sun.COM 			    gettext("%s: Could not map log\n"), progname);
15440Sstevel@tonic-gate 			exit_status++;
15450Sstevel@tonic-gate 			goto error;
15460Sstevel@tonic-gate 		}
15470Sstevel@tonic-gate 
15480Sstevel@tonic-gate 		/*
15490Sstevel@tonic-gate 		 * We grab the lock twice (once again in the iterator call),
15500Sstevel@tonic-gate 		 * but that's ok since the lock func handles incr locks held.
15510Sstevel@tonic-gate 		 */
15520Sstevel@tonic-gate 		if (krb5_db_lock(util_context, KRB5_LOCKMODE_SHARED)) {
1553*8092SMark.Phalan@Sun.COM 			/* Solaris Kerberos */
15540Sstevel@tonic-gate 			fprintf(stderr,
1555*8092SMark.Phalan@Sun.COM 			    gettext("%s: Couldn't grab lock\n"), progname);
15560Sstevel@tonic-gate 			exit_status++;
15570Sstevel@tonic-gate 			goto error;
15580Sstevel@tonic-gate 		}
15590Sstevel@tonic-gate 
15600Sstevel@tonic-gate 		fprintf(f, " %u", log_ctx->ulog->kdb_last_sno);
15610Sstevel@tonic-gate 		fprintf(f, " %u", log_ctx->ulog->kdb_last_time.seconds);
15620Sstevel@tonic-gate 		fprintf(f, " %u", log_ctx->ulog->kdb_last_time.useconds);
15630Sstevel@tonic-gate 	}
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate 	if (dump->header[strlen(dump->header)-1] != '\n')
15660Sstevel@tonic-gate 	     fputc('\n', arglist.ofile);
15670Sstevel@tonic-gate 
15685916Swillf 	/* Solaris Kerberos: adding support for -rev/recurse flags */
15695916Swillf 	/* don't pass in db_args if there aren't any */
15704960Swillf 	if ((kret = krb5_db_iterate(util_context,
15714960Swillf 				    NULL,
15724960Swillf 				    dump->dump_princ,
15735916Swillf 				    (krb5_pointer) &arglist,
15745916Swillf 				    db_arg_index > 0 ? (char **)&db_args : NULL))) {
1575*8092SMark.Phalan@Sun.COM 	     /* Solaris Kerberos */
15764960Swillf 	     fprintf(stderr, dumprec_err,
1577*8092SMark.Phalan@Sun.COM 		     progname, dump->name, error_message(kret));
15780Sstevel@tonic-gate 	     exit_status++;
15790Sstevel@tonic-gate 		if (dump_sno)
15800Sstevel@tonic-gate 			(void) krb5_db_unlock(util_context);
15810Sstevel@tonic-gate 	}
15820Sstevel@tonic-gate 	if (dump->dump_policy &&
15834960Swillf 	    (kret = krb5_db_iter_policy( util_context, "*", dump->dump_policy,
15844960Swillf 					 &arglist))) {
1585*8092SMark.Phalan@Sun.COM 	     /* Solaris Kerberos */
15864960Swillf 	     fprintf(stderr, gettext(dumprec_err),
1587*8092SMark.Phalan@Sun.COM 		progname, dump->name,
15880Sstevel@tonic-gate 		     error_message(kret));
15890Sstevel@tonic-gate 	     exit_status++;
15900Sstevel@tonic-gate 	}
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate error:
15930Sstevel@tonic-gate 	if (ofile && f != stdout && !exit_status) {
15944960Swillf 	    if (locked) {
15954960Swillf 		(void) krb5_lock_file(util_context, fileno(f), KRB5_LOCKMODE_UNLOCK);
15964960Swillf 		locked = 0;
15974960Swillf 	    }
15984960Swillf 	    fclose(f);
15994960Swillf 	    update_ok_file(ofile);
16000Sstevel@tonic-gate 	}
16010Sstevel@tonic-gate     }
16020Sstevel@tonic-gate     if (locked)
16032881Smp153739 	(void) krb5_lock_file(util_context, fileno(f), KRB5_LOCKMODE_UNLOCK);
16040Sstevel@tonic-gate }
16050Sstevel@tonic-gate 
16060Sstevel@tonic-gate /*
16070Sstevel@tonic-gate  * Read a string of bytes while counting the number of lines passed.
16080Sstevel@tonic-gate  */
16090Sstevel@tonic-gate static int
read_string(f,buf,len,lp)16100Sstevel@tonic-gate read_string(f, buf, len, lp)
16110Sstevel@tonic-gate     FILE	*f;
16120Sstevel@tonic-gate     char	*buf;
16130Sstevel@tonic-gate     int		len;
16140Sstevel@tonic-gate     int		*lp;
16150Sstevel@tonic-gate {
16160Sstevel@tonic-gate     int c;
16170Sstevel@tonic-gate     int i, retval;
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate     retval = 0;
16200Sstevel@tonic-gate     for (i=0; i<len; i++) {
16210Sstevel@tonic-gate 	c = fgetc(f);
16220Sstevel@tonic-gate 	if (c < 0) {
16230Sstevel@tonic-gate 	    retval = 1;
16240Sstevel@tonic-gate 	    break;
16250Sstevel@tonic-gate 	}
16260Sstevel@tonic-gate 	if (c == '\n')
16270Sstevel@tonic-gate 	    (*lp)++;
16280Sstevel@tonic-gate 	buf[i] = (char) c;
16290Sstevel@tonic-gate     }
16300Sstevel@tonic-gate     buf[len] = '\0';
16310Sstevel@tonic-gate     return(retval);
16320Sstevel@tonic-gate }
16330Sstevel@tonic-gate 
16340Sstevel@tonic-gate /*
16350Sstevel@tonic-gate  * Read a string of two character representations of bytes.
16360Sstevel@tonic-gate  */
16370Sstevel@tonic-gate static int
read_octet_string(f,buf,len)16380Sstevel@tonic-gate read_octet_string(f, buf, len)
16390Sstevel@tonic-gate     FILE	*f;
16400Sstevel@tonic-gate     krb5_octet	*buf;
16410Sstevel@tonic-gate     int		len;
16420Sstevel@tonic-gate {
16430Sstevel@tonic-gate     int c;
16440Sstevel@tonic-gate     int i, retval;
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate     retval = 0;
16470Sstevel@tonic-gate     for (i=0; i<len; i++) {
16480Sstevel@tonic-gate 	if (fscanf(f, "%02x", &c) != 1) {
16490Sstevel@tonic-gate 	    retval = 1;
16500Sstevel@tonic-gate 	    break;
16510Sstevel@tonic-gate 	}
16520Sstevel@tonic-gate 	buf[i] = (krb5_octet) c;
16530Sstevel@tonic-gate     }
16540Sstevel@tonic-gate     return(retval);
16550Sstevel@tonic-gate }
16560Sstevel@tonic-gate 
16570Sstevel@tonic-gate /*
16580Sstevel@tonic-gate  * Find the end of an old format record.
16590Sstevel@tonic-gate  */
16600Sstevel@tonic-gate static void
find_record_end(f,fn,lineno)16610Sstevel@tonic-gate find_record_end(f, fn, lineno)
16620Sstevel@tonic-gate     FILE	*f;
16630Sstevel@tonic-gate     char	*fn;
16640Sstevel@tonic-gate     int		lineno;
16650Sstevel@tonic-gate {
16660Sstevel@tonic-gate     int	ch;
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate     if (((ch = fgetc(f)) != ';') || ((ch = fgetc(f)) != '\n')) {
16690Sstevel@tonic-gate 		fprintf(stderr, gettext(trash_end_fmt), fn, lineno);
16700Sstevel@tonic-gate 	while (ch != '\n') {
16710Sstevel@tonic-gate 	    putc(ch, stderr);
16720Sstevel@tonic-gate 	    ch = fgetc(f);
16730Sstevel@tonic-gate 	}
16740Sstevel@tonic-gate 	putc(ch, stderr);
16750Sstevel@tonic-gate     }
16760Sstevel@tonic-gate }
16770Sstevel@tonic-gate 
16780Sstevel@tonic-gate #if 0
16790Sstevel@tonic-gate /*
16800Sstevel@tonic-gate  * update_tl_data()	- Generate the tl_data entries.
16810Sstevel@tonic-gate  */
16820Sstevel@tonic-gate static krb5_error_code
16830Sstevel@tonic-gate update_tl_data(kcontext, dbentp, mod_name, mod_date, last_pwd_change)
16840Sstevel@tonic-gate     krb5_context	kcontext;
16850Sstevel@tonic-gate     krb5_db_entry	*dbentp;
16860Sstevel@tonic-gate     krb5_principal	mod_name;
16870Sstevel@tonic-gate     krb5_timestamp	mod_date;
16880Sstevel@tonic-gate     krb5_timestamp	last_pwd_change;
16890Sstevel@tonic-gate {
16900Sstevel@tonic-gate     krb5_error_code	kret;
16910Sstevel@tonic-gate 
16920Sstevel@tonic-gate     kret = 0 ;
16930Sstevel@tonic-gate 
16940Sstevel@tonic-gate     /*
16950Sstevel@tonic-gate      * Handle modification principal.
16960Sstevel@tonic-gate      */
16970Sstevel@tonic-gate     if (mod_name) {
16980Sstevel@tonic-gate 	krb5_tl_mod_princ	mprinc;
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 	memset(&mprinc, 0, sizeof(mprinc));
17010Sstevel@tonic-gate 	if (!(kret = krb5_copy_principal(kcontext,
17020Sstevel@tonic-gate 					 mod_name,
17030Sstevel@tonic-gate 					 &mprinc.mod_princ))) {
17040Sstevel@tonic-gate 	    mprinc.mod_date = mod_date;
17050Sstevel@tonic-gate 	    kret = krb5_dbe_encode_mod_princ_data(kcontext,
17060Sstevel@tonic-gate 						  &mprinc,
17070Sstevel@tonic-gate 						  dbentp);
17080Sstevel@tonic-gate 	}
17090Sstevel@tonic-gate 	if (mprinc.mod_princ)
17100Sstevel@tonic-gate 	    krb5_free_principal(kcontext, mprinc.mod_princ);
17110Sstevel@tonic-gate     }
17122881Smp153739 
17130Sstevel@tonic-gate     /*
17140Sstevel@tonic-gate      * Handle last password change.
17150Sstevel@tonic-gate      */
17160Sstevel@tonic-gate     if (!kret) {
17170Sstevel@tonic-gate 	krb5_tl_data	*pwchg;
17180Sstevel@tonic-gate 	krb5_boolean	linked;
17190Sstevel@tonic-gate 
17200Sstevel@tonic-gate 	/* Find a previously existing entry */
17210Sstevel@tonic-gate 	for (pwchg = dbentp->tl_data;
17220Sstevel@tonic-gate 	     (pwchg) && (pwchg->tl_data_type != KRB5_TL_LAST_PWD_CHANGE);
17230Sstevel@tonic-gate 	     pwchg = pwchg->tl_data_next);
17240Sstevel@tonic-gate 
17250Sstevel@tonic-gate 	/* Check to see if we found one. */
17260Sstevel@tonic-gate 	linked = 0;
17270Sstevel@tonic-gate 	if (!pwchg) {
17280Sstevel@tonic-gate 	    /* No, allocate a new one */
17292881Smp153739 	    if ((pwchg = (krb5_tl_data *) malloc(sizeof(krb5_tl_data)))) {
17302881Smp153739 		memset(pwchg, 0, sizeof(krb5_tl_data));
17312881Smp153739 		if (!(pwchg->tl_data_contents =
17322881Smp153739 		      (krb5_octet *) malloc(sizeof(krb5_timestamp)))) {
17332881Smp153739 		    free(pwchg);
17342881Smp153739 		    pwchg = (krb5_tl_data *) NULL;
17352881Smp153739 		}
17362881Smp153739 		else {
17372881Smp153739 		    pwchg->tl_data_type = KRB5_TL_LAST_PWD_CHANGE;
17382881Smp153739 		    pwchg->tl_data_length =
17392881Smp153739 			(krb5_int16) sizeof(krb5_timestamp);
17402881Smp153739 		}
17410Sstevel@tonic-gate 	    }
17422881Smp153739 	}
17432881Smp153739 	else
17442881Smp153739 	    linked = 1;
17450Sstevel@tonic-gate 
17460Sstevel@tonic-gate 	/* Do we have an entry? */
17470Sstevel@tonic-gate 	if (pwchg && pwchg->tl_data_contents) {
17480Sstevel@tonic-gate 	    /* Encode it */
17492881Smp153739 	    krb5_kdb_encode_int32(last_pwd_change, pwchg->tl_data_contents);
17500Sstevel@tonic-gate 	    /* Link it in if necessary */
17510Sstevel@tonic-gate 	    if (!linked) {
17520Sstevel@tonic-gate 		pwchg->tl_data_next = dbentp->tl_data;
17530Sstevel@tonic-gate 		dbentp->tl_data = pwchg;
17540Sstevel@tonic-gate 		dbentp->n_tl_data++;
17550Sstevel@tonic-gate 	    }
17562881Smp153739 	}
17572881Smp153739 	else
17580Sstevel@tonic-gate 	    kret = ENOMEM;
17590Sstevel@tonic-gate     }
17602881Smp153739 
17610Sstevel@tonic-gate     return(kret);
17620Sstevel@tonic-gate }
17630Sstevel@tonic-gate #endif
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate /*
17660Sstevel@tonic-gate  * process_k5beta_record()	- Handle a dump record in old format.
17670Sstevel@tonic-gate  *
17680Sstevel@tonic-gate  * Returns -1 for end of file, 0 for success and 1 for failure.
17690Sstevel@tonic-gate  */
17700Sstevel@tonic-gate static int
process_k5beta_record(fname,kcontext,filep,verbose,linenop)17714960Swillf process_k5beta_record(fname, kcontext, filep, verbose, linenop)
17720Sstevel@tonic-gate     char		*fname;
17730Sstevel@tonic-gate     krb5_context	kcontext;
17740Sstevel@tonic-gate     FILE		*filep;
17750Sstevel@tonic-gate     int			verbose;
17760Sstevel@tonic-gate     int			*linenop;
17770Sstevel@tonic-gate {
17780Sstevel@tonic-gate     int			nmatched;
17790Sstevel@tonic-gate     int			retval;
17800Sstevel@tonic-gate     krb5_db_entry	dbent;
17810Sstevel@tonic-gate     int			name_len, mod_name_len, key_len;
17820Sstevel@tonic-gate     int			alt_key_len, salt_len, alt_salt_len;
17830Sstevel@tonic-gate     char		*name;
17840Sstevel@tonic-gate     char		*mod_name;
17850Sstevel@tonic-gate     int			tmpint1, tmpint2, tmpint3;
17860Sstevel@tonic-gate     int			error;
17870Sstevel@tonic-gate     const char		*try2read;
17880Sstevel@tonic-gate     int			i;
17890Sstevel@tonic-gate     krb5_key_data	*pkey, *akey;
17900Sstevel@tonic-gate     krb5_timestamp	last_pwd_change, mod_date;
17910Sstevel@tonic-gate     krb5_principal	mod_princ;
17920Sstevel@tonic-gate     krb5_error_code	kret;
17930Sstevel@tonic-gate     krb5_octet 		*shortcopy1 = NULL; /* SUNWresync121 memleak fix */
17940Sstevel@tonic-gate     krb5_octet 		*shortcopy2 = NULL;
17950Sstevel@tonic-gate 
17960Sstevel@tonic-gate     try2read = (char *) NULL;
17970Sstevel@tonic-gate     (*linenop)++;
17980Sstevel@tonic-gate     retval = 1;
17990Sstevel@tonic-gate     memset((char *)&dbent, 0, sizeof(dbent));
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate     /* Make sure we've got key_data entries */
18020Sstevel@tonic-gate     if (krb5_dbe_create_key_data(kcontext, &dbent) ||
18030Sstevel@tonic-gate 	krb5_dbe_create_key_data(kcontext, &dbent)) {
18040Sstevel@tonic-gate 	krb5_db_free_principal(kcontext, &dbent, 1);
18050Sstevel@tonic-gate 	return(1);
18060Sstevel@tonic-gate     }
18070Sstevel@tonic-gate     pkey = &dbent.key_data[0];
18080Sstevel@tonic-gate     akey = &dbent.key_data[1];
18090Sstevel@tonic-gate 
18100Sstevel@tonic-gate     /*
18110Sstevel@tonic-gate      * Match the sizes.  6 tokens to match.
18120Sstevel@tonic-gate      */
18130Sstevel@tonic-gate     nmatched = fscanf(filep, "%d\t%d\t%d\t%d\t%d\t%d\t",
18140Sstevel@tonic-gate 		      &name_len, &mod_name_len, &key_len,
18150Sstevel@tonic-gate 		      &alt_key_len, &salt_len, &alt_salt_len);
18160Sstevel@tonic-gate     if (nmatched == 6) {
18170Sstevel@tonic-gate         pkey->key_data_length[0] = key_len;
18180Sstevel@tonic-gate 	akey->key_data_length[0] = alt_key_len;
18190Sstevel@tonic-gate 	pkey->key_data_length[1] = salt_len;
18200Sstevel@tonic-gate 	akey->key_data_length[1] = alt_salt_len;
18210Sstevel@tonic-gate 	name = (char *) NULL;
18220Sstevel@tonic-gate 	mod_name = (char *) NULL;
18230Sstevel@tonic-gate 	/*
18240Sstevel@tonic-gate 	 * Get the memory for the variable length fields.
18250Sstevel@tonic-gate 	 */
18260Sstevel@tonic-gate 	if ((name = (char *) malloc((size_t) (name_len + 1))) &&
18270Sstevel@tonic-gate 	    (mod_name = (char *) malloc((size_t) (mod_name_len + 1))) &&
18280Sstevel@tonic-gate 	    (!key_len ||
18290Sstevel@tonic-gate 	     (pkey->key_data_contents[0] =
18300Sstevel@tonic-gate 	      (krb5_octet *) malloc((size_t) (key_len + 1)))) &&
18310Sstevel@tonic-gate 	    (!alt_key_len ||
18320Sstevel@tonic-gate 	     (akey->key_data_contents[0] =
18332881Smp153739 	      (krb5_octet *) malloc((size_t) (alt_key_len + 1)))) &&
18340Sstevel@tonic-gate 	    (!salt_len ||
18350Sstevel@tonic-gate 	     (pkey->key_data_contents[1] =
18360Sstevel@tonic-gate 	      (krb5_octet *) malloc((size_t) (salt_len + 1)))) &&
18370Sstevel@tonic-gate 	    (!alt_salt_len ||
18380Sstevel@tonic-gate 	     (akey->key_data_contents[1] =
18392881Smp153739 	      (krb5_octet *) malloc((size_t) (alt_salt_len + 1))))
18402881Smp153739 	    ) {
18410Sstevel@tonic-gate 	    error = 0;
18420Sstevel@tonic-gate 
18430Sstevel@tonic-gate 	    /* Read the principal name */
18440Sstevel@tonic-gate 	    if (read_string(filep, name, name_len, linenop)) {
18450Sstevel@tonic-gate 		try2read = read_name_string;
18460Sstevel@tonic-gate 		error++;
18470Sstevel@tonic-gate 	    }
18480Sstevel@tonic-gate 	    /* Read the key type */
18492881Smp153739 	    if (!error && (fscanf(filep, "\t%d\t", &tmpint1) != 1)) {
18502881Smp153739 		try2read = read_key_type;
18512881Smp153739 		error++;
18520Sstevel@tonic-gate 	    }
18530Sstevel@tonic-gate 	    pkey->key_data_type[0] = tmpint1;
18540Sstevel@tonic-gate 	    /* Read the old format key */
18550Sstevel@tonic-gate 	    if (!error && read_octet_string(filep,
18560Sstevel@tonic-gate 					    pkey->key_data_contents[0],
18570Sstevel@tonic-gate 					    pkey->key_data_length[0])) {
18580Sstevel@tonic-gate 		try2read = read_key_data;
18590Sstevel@tonic-gate 		error++;
18600Sstevel@tonic-gate 	    }
18610Sstevel@tonic-gate 	    /* convert to a new format key */
18622881Smp153739 	    /* the encrypted version is stored as the unencrypted key length
18632881Smp153739 	       (4 bytes, MSB first) followed by the encrypted key. */
18642881Smp153739 	    if ((pkey->key_data_length[0] > 4)
18652881Smp153739 		&& (pkey->key_data_contents[0][0] == 0)
18662881Smp153739 		&& (pkey->key_data_contents[0][1] == 0)) {
18672881Smp153739 	      /* this really does look like an old key, so drop and swap */
18682881Smp153739 	      /* the *new* length is 2 bytes, LSB first, sigh. */
18692881Smp153739 	      size_t shortlen = pkey->key_data_length[0]-4+2;
18702881Smp153739 	      krb5_octet *origdata = pkey->key_data_contents[0];
18710Sstevel@tonic-gate 
18720Sstevel@tonic-gate 		    shortcopy1 = (krb5_octet *) malloc(shortlen);
18730Sstevel@tonic-gate 		    if (shortcopy1) {
18740Sstevel@tonic-gate 			shortcopy1[0] = origdata[3];
18750Sstevel@tonic-gate 			shortcopy1[1] = origdata[2];
18760Sstevel@tonic-gate 			memcpy(shortcopy1 + 2, origdata + 4, shortlen - 2);
18770Sstevel@tonic-gate 			free(origdata);
18780Sstevel@tonic-gate 			pkey->key_data_length[0] = shortlen;
18790Sstevel@tonic-gate 			pkey->key_data_contents[0] = shortcopy1;
18800Sstevel@tonic-gate 		    } else {
18810Sstevel@tonic-gate 			fprintf(stderr, gettext(no_mem_fmt), fname, *linenop);
18820Sstevel@tonic-gate 			error++;
18830Sstevel@tonic-gate 		    }
18840Sstevel@tonic-gate 	    }
18852881Smp153739 
18860Sstevel@tonic-gate 	    /* Read principal attributes */
18872881Smp153739 	    if (!error && (fscanf(filep,
18882881Smp153739 				  "\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t",
18892881Smp153739 				  &tmpint1, &dbent.max_life,
18902881Smp153739 				  &dbent.max_renewable_life,
18912881Smp153739 				  &tmpint2, &dbent.expiration,
18922881Smp153739 				  &dbent.pw_expiration, &last_pwd_change,
18932881Smp153739 				  &dbent.last_success, &dbent.last_failed,
18942881Smp153739 				  &tmpint3) != 10)) {
18952881Smp153739 		try2read = read_pr_data1;
18962881Smp153739 		error++;
18970Sstevel@tonic-gate 	    }
18980Sstevel@tonic-gate 	    pkey->key_data_kvno = tmpint1;
18990Sstevel@tonic-gate 	    dbent.fail_auth_count = tmpint3;
19000Sstevel@tonic-gate 	    /* Read modifier name */
19010Sstevel@tonic-gate 	    if (!error && read_string(filep,
19020Sstevel@tonic-gate 				      mod_name,
19030Sstevel@tonic-gate 				      mod_name_len,
19040Sstevel@tonic-gate 				      linenop)) {
19050Sstevel@tonic-gate 		try2read = read_mod_name;
19060Sstevel@tonic-gate 		error++;
19070Sstevel@tonic-gate 	    }
19080Sstevel@tonic-gate 	    /* Read second set of attributes */
19090Sstevel@tonic-gate 	    if (!error && (fscanf(filep, "\t%u\t%u\t%u\t",
19100Sstevel@tonic-gate 				  &mod_date, &dbent.attributes,
19110Sstevel@tonic-gate 				  &tmpint1) != 3)) {
19120Sstevel@tonic-gate 		try2read = read_pr_data2;
19130Sstevel@tonic-gate 		error++;
19140Sstevel@tonic-gate 	    }
19150Sstevel@tonic-gate 	    pkey->key_data_type[1] = tmpint1;
19160Sstevel@tonic-gate 	    /* Read salt data */
19170Sstevel@tonic-gate 	    if (!error && read_octet_string(filep,
19180Sstevel@tonic-gate 					    pkey->key_data_contents[1],
19190Sstevel@tonic-gate 					    pkey->key_data_length[1])) {
19200Sstevel@tonic-gate 		try2read = read_salt_data;
19210Sstevel@tonic-gate 		error++;
19220Sstevel@tonic-gate 	    }
19230Sstevel@tonic-gate 	    /* Read alternate key type */
19242881Smp153739 	    if (!error && (fscanf(filep, "\t%u\t", &tmpint1) != 1)) {
19252881Smp153739 		try2read = read_akey_type;
19262881Smp153739 		error++;
19270Sstevel@tonic-gate 	    }
19280Sstevel@tonic-gate 	    akey->key_data_type[0] = tmpint1;
19290Sstevel@tonic-gate 	    /* Read alternate key */
19300Sstevel@tonic-gate 	    if (!error && read_octet_string(filep,
19310Sstevel@tonic-gate 					    akey->key_data_contents[0],
19320Sstevel@tonic-gate 					    akey->key_data_length[0])) {
19332881Smp153739 		try2read = read_akey_data;
19342881Smp153739 		error++;
19350Sstevel@tonic-gate 	    }
19362881Smp153739 
19370Sstevel@tonic-gate 	    /* convert to a new format key */
19382881Smp153739 	    /* the encrypted version is stored as the unencrypted key length
19392881Smp153739 	       (4 bytes, MSB first) followed by the encrypted key. */
19402881Smp153739 	    if ((akey->key_data_length[0] > 4)
19412881Smp153739 		&& (akey->key_data_contents[0][0] == 0)
19422881Smp153739 		&& (akey->key_data_contents[0][1] == 0)) {
19432881Smp153739 	      /* this really does look like an old key, so drop and swap */
19442881Smp153739 	      /* the *new* length is 2 bytes, LSB first, sigh. */
19452881Smp153739 	      size_t shortlen = akey->key_data_length[0]-4+2;
19460Sstevel@tonic-gate 
19470Sstevel@tonic-gate 		    krb5_octet *origdata = akey->key_data_contents[0];
19480Sstevel@tonic-gate 
19490Sstevel@tonic-gate 		    shortcopy2 = (krb5_octet *) malloc(shortlen);
19500Sstevel@tonic-gate 		    if (shortcopy2) {
19510Sstevel@tonic-gate 			shortcopy2[0] = origdata[3];
19520Sstevel@tonic-gate 			shortcopy2[1] = origdata[2];
19530Sstevel@tonic-gate 			memcpy(shortcopy2 + 2,
19540Sstevel@tonic-gate 			    origdata + 4, shortlen - 2);
19550Sstevel@tonic-gate 			free(origdata);
19560Sstevel@tonic-gate 			akey->key_data_length[0] = shortlen;
19570Sstevel@tonic-gate 			akey->key_data_contents[0] = shortcopy2;
19580Sstevel@tonic-gate 		    } else {
19590Sstevel@tonic-gate 			fprintf(stderr, gettext(no_mem_fmt), fname, *linenop);
19600Sstevel@tonic-gate 			error++;
19610Sstevel@tonic-gate 		    }
19620Sstevel@tonic-gate 	    }
19632881Smp153739 
19640Sstevel@tonic-gate 	    /* Read alternate salt type */
19652881Smp153739 	    if (!error && (fscanf(filep, "\t%u\t", &tmpint1) != 1)) {
19662881Smp153739 		try2read = read_asalt_type;
19672881Smp153739 		error++;
19680Sstevel@tonic-gate 	    }
19690Sstevel@tonic-gate 	    akey->key_data_type[1] = tmpint1;
19700Sstevel@tonic-gate 	    /* Read alternate salt data */
19710Sstevel@tonic-gate 	    if (!error && read_octet_string(filep,
19720Sstevel@tonic-gate 					    akey->key_data_contents[1],
19730Sstevel@tonic-gate 					    akey->key_data_length[1])) {
19740Sstevel@tonic-gate 		try2read = read_asalt_data;
19750Sstevel@tonic-gate 		error++;
19760Sstevel@tonic-gate 	    }
19770Sstevel@tonic-gate 	    /* Read expansion data - discard it */
19780Sstevel@tonic-gate 	    if (!error) {
19790Sstevel@tonic-gate 		for (i=0; i<8; i++) {
19802881Smp153739 		    if (fscanf(filep, "\t%u", &tmpint1) != 1) {
19810Sstevel@tonic-gate 			try2read = read_exp_data;
19820Sstevel@tonic-gate 			error++;
19830Sstevel@tonic-gate 			break;
19842881Smp153739 		    }
19850Sstevel@tonic-gate 		}
19860Sstevel@tonic-gate 		if (!error)
19870Sstevel@tonic-gate 		    find_record_end(filep, fname, *linenop);
19880Sstevel@tonic-gate 	    }
19892881Smp153739 
19900Sstevel@tonic-gate 	    /*
19912881Smp153739 	     * If no error, then we're done reading.  Now parse the names
19922881Smp153739 	     * and store the database dbent.
19930Sstevel@tonic-gate 	     */
19940Sstevel@tonic-gate 	    if (!error) {
19952881Smp153739 		if (!(kret = krb5_parse_name(kcontext,
19962881Smp153739 					     name,
19972881Smp153739 					     &dbent.princ))) {
19982881Smp153739 		    if (!(kret = krb5_parse_name(kcontext,
19992881Smp153739 						 mod_name,
20002881Smp153739 						 &mod_princ))) {
20012881Smp153739 			if (!(kret =
20022881Smp153739 			      krb5_dbe_update_mod_princ_data(kcontext,
20032881Smp153739 							     &dbent,
20042881Smp153739 							     mod_date,
20052881Smp153739 							     mod_princ)) &&
20062881Smp153739 			    !(kret =
20072881Smp153739 			      krb5_dbe_update_last_pwd_change(kcontext,
20082881Smp153739 							      &dbent,
20092881Smp153739 							      last_pwd_change))) {
20102881Smp153739 			    int one = 1;
20112881Smp153739 
20122881Smp153739 			    dbent.len = KRB5_KDB_V1_BASE_LENGTH;
20132881Smp153739 			    pkey->key_data_ver = (pkey->key_data_type[1] || pkey->key_data_length[1]) ?
20142881Smp153739 				2 : 1;
20152881Smp153739 			    akey->key_data_ver = (akey->key_data_type[1] || akey->key_data_length[1]) ?
20162881Smp153739 				2 : 1;
20172881Smp153739 			    if ((pkey->key_data_type[0] ==
20182881Smp153739 				 akey->key_data_type[0]) &&
20192881Smp153739 				(pkey->key_data_type[1] ==
20202881Smp153739 				 akey->key_data_type[1]))
20212881Smp153739 				dbent.n_key_data--;
20222881Smp153739 			    else if ((akey->key_data_type[0] == 0)
20232881Smp153739 				     && (akey->key_data_length[0] == 0)
20242881Smp153739 				     && (akey->key_data_type[1] == 0)
20252881Smp153739 				     && (akey->key_data_length[1] == 0))
20262881Smp153739 			        dbent.n_key_data--;
20274960Swillf 
20284960Swillf 			    dbent.mask = KADM5_LOAD | KADM5_PRINCIPAL | KADM5_ATTRIBUTES |
20294960Swillf 				KADM5_MAX_LIFE | KADM5_MAX_RLIFE | KADM5_KEY_DATA |
20304960Swillf 				KADM5_PRINC_EXPIRE_TIME | KADM5_LAST_SUCCESS |
20314960Swillf 				KADM5_LAST_FAILED | KADM5_FAIL_AUTH_COUNT;
20324960Swillf 
20332881Smp153739 			    if ((kret = krb5_db_put_principal(kcontext,
20342881Smp153739 							      &dbent,
20352881Smp153739 							      &one)) ||
20362881Smp153739 				(one != 1)) {
20372881Smp153739 				fprintf(stderr, gettext(store_err_fmt),
20382881Smp153739 					fname, *linenop, name,
20392881Smp153739 					error_message(kret));
20402881Smp153739 				error++;
20412881Smp153739 			    }
20422881Smp153739 			    else {
20432881Smp153739 				if (verbose)
20442881Smp153739 				    fprintf(stderr,
20452881Smp153739 							gettext(add_princ_fmt),
20462881Smp153739 							name);
20472881Smp153739 				retval = 0;
20482881Smp153739 			    }
20492881Smp153739 			    dbent.n_key_data = 2;
20502881Smp153739 			}
20512881Smp153739 			krb5_free_principal(kcontext, mod_princ);
20522881Smp153739 		    }
20532881Smp153739 		    else {
20542881Smp153739 			fprintf(stderr,
20552881Smp153739 				gettext(parse_err_fmt),
20562881Smp153739 				fname, *linenop, mod_name,
20572881Smp153739 				error_message(kret));
20582881Smp153739 			error++;
20592881Smp153739 		    }
20602881Smp153739 		}
20612881Smp153739 		else {
20622881Smp153739 		    fprintf(stderr, gettext(parse_err_fmt),
20632881Smp153739 			    fname, *linenop, name, error_message(kret));
20642881Smp153739 		    error++;
20652881Smp153739 		}
20660Sstevel@tonic-gate 	    }
20672881Smp153739 	    else {
20682881Smp153739 	    fprintf(stderr, gettext(no_mem_fmt), fname, *linenop, try2read);
20692881Smp153739 	    }
20702881Smp153739 	}
20712881Smp153739 	else {
20722881Smp153739 		fprintf(stderr, gettext(read_err_fmt), fname, *linenop);
20730Sstevel@tonic-gate 	}
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate 	krb5_db_free_principal(kcontext, &dbent, 1);
20760Sstevel@tonic-gate 	if (mod_name)
20770Sstevel@tonic-gate 	    free(mod_name);
20780Sstevel@tonic-gate 	if (name)
20790Sstevel@tonic-gate 	    free(name);
20802881Smp153739     }
20812881Smp153739     else {
20820Sstevel@tonic-gate 	if (nmatched != EOF)
20830Sstevel@tonic-gate 	   fprintf(stderr, gettext(rhead_err_fmt),
20840Sstevel@tonic-gate 		fname, *linenop);
20850Sstevel@tonic-gate 	else
20862881Smp153739 	    retval = -1;
20870Sstevel@tonic-gate     }
20880Sstevel@tonic-gate 
20890Sstevel@tonic-gate     if (shortcopy1)
20900Sstevel@tonic-gate 	free(shortcopy1);
20910Sstevel@tonic-gate     if (shortcopy2)
20920Sstevel@tonic-gate 	free(shortcopy2);
20930Sstevel@tonic-gate 
20942881Smp153739     return(retval);
20950Sstevel@tonic-gate }
20960Sstevel@tonic-gate 
20970Sstevel@tonic-gate /*
20980Sstevel@tonic-gate  * process_k5beta6_record()	- Handle a dump record in krb5b6 format.
20990Sstevel@tonic-gate  *
21000Sstevel@tonic-gate  * Returns -1 for end of file, 0 for success and 1 for failure.
21010Sstevel@tonic-gate  */
21020Sstevel@tonic-gate static int
process_k5beta6_record(fname,kcontext,filep,verbose,linenop)21034960Swillf process_k5beta6_record(fname, kcontext, filep, verbose, linenop)
21040Sstevel@tonic-gate     char		*fname;
21050Sstevel@tonic-gate     krb5_context	kcontext;
21060Sstevel@tonic-gate     FILE		*filep;
21070Sstevel@tonic-gate     int			verbose;
21080Sstevel@tonic-gate     int			*linenop;
21090Sstevel@tonic-gate {
21100Sstevel@tonic-gate     int			retval;
21110Sstevel@tonic-gate     krb5_db_entry	dbentry;
21120Sstevel@tonic-gate     krb5_int32		t1, t2, t3, t4, t5, t6, t7, t8, t9;
21130Sstevel@tonic-gate     int			nread;
21140Sstevel@tonic-gate     int			error;
21150Sstevel@tonic-gate     int			i, j, one;
21160Sstevel@tonic-gate     char		*name;
21170Sstevel@tonic-gate     krb5_key_data	*kp, *kdatap;
21180Sstevel@tonic-gate     krb5_tl_data	**tlp, *tl;
21190Sstevel@tonic-gate     krb5_octet 		*op;
21200Sstevel@tonic-gate     krb5_error_code	kret;
21210Sstevel@tonic-gate     const char		*try2read;
21220Sstevel@tonic-gate 
21230Sstevel@tonic-gate     try2read = (char *) NULL;
21240Sstevel@tonic-gate     memset((char *) &dbentry, 0, sizeof(dbentry));
21250Sstevel@tonic-gate     (*linenop)++;
21260Sstevel@tonic-gate     retval = 1;
21270Sstevel@tonic-gate     name = (char *) NULL;
21280Sstevel@tonic-gate     kp = (krb5_key_data *) NULL;
21290Sstevel@tonic-gate     op = (krb5_octet *) NULL;
21300Sstevel@tonic-gate     error = 0;
21310Sstevel@tonic-gate     kret = 0;
21320Sstevel@tonic-gate     nread = fscanf(filep, "%d\t%d\t%d\t%d\t%d\t", &t1, &t2, &t3, &t4, &t5);
21330Sstevel@tonic-gate     if (nread == 5) {
21340Sstevel@tonic-gate 	/* Get memory for flattened principal name */
21350Sstevel@tonic-gate 	if (!(name = (char *) malloc((size_t) t2 + 1)))
21360Sstevel@tonic-gate 	    error++;
21370Sstevel@tonic-gate 
21380Sstevel@tonic-gate 	/* Get memory for and form tagged data linked list */
21390Sstevel@tonic-gate 	tlp = &dbentry.tl_data;
21400Sstevel@tonic-gate 	for (i=0; i<t3; i++) {
21412881Smp153739 	    if ((*tlp = (krb5_tl_data *) malloc(sizeof(krb5_tl_data)))) {
21420Sstevel@tonic-gate 		memset(*tlp, 0, sizeof(krb5_tl_data));
21430Sstevel@tonic-gate 		tlp = &((*tlp)->tl_data_next);
21440Sstevel@tonic-gate 		dbentry.n_tl_data++;
21452881Smp153739 	    }
21462881Smp153739 	    else {
21470Sstevel@tonic-gate 		error++;
21480Sstevel@tonic-gate 		break;
21490Sstevel@tonic-gate 	    }
21500Sstevel@tonic-gate 	}
21510Sstevel@tonic-gate 
21520Sstevel@tonic-gate 	/* Get memory for key list */
21530Sstevel@tonic-gate 	if (t4 && !(kp = (krb5_key_data *) malloc((size_t)
21540Sstevel@tonic-gate 						  (t4*sizeof(krb5_key_data)))))
21550Sstevel@tonic-gate 	    error++;
21560Sstevel@tonic-gate 
21570Sstevel@tonic-gate 	/* Get memory for extra data */
21580Sstevel@tonic-gate 	if (t5 && !(op = (krb5_octet *) malloc((size_t) t5)))
21590Sstevel@tonic-gate 	    error++;
21600Sstevel@tonic-gate 
21610Sstevel@tonic-gate 	if (!error) {
21620Sstevel@tonic-gate 	    dbentry.len = t1;
21630Sstevel@tonic-gate 	    dbentry.n_key_data = t4;
21640Sstevel@tonic-gate 	    dbentry.e_length = t5;
21650Sstevel@tonic-gate 	    if (kp) {
21662881Smp153739 		memset(kp, 0, (size_t) (t4*sizeof(krb5_key_data)));
21670Sstevel@tonic-gate 		dbentry.key_data = kp;
21680Sstevel@tonic-gate 		kp = (krb5_key_data *) NULL;
21690Sstevel@tonic-gate 	    }
21700Sstevel@tonic-gate 	    if (op) {
21710Sstevel@tonic-gate 		memset(op, 0, (size_t) t5);
21720Sstevel@tonic-gate 		dbentry.e_data = op;
21730Sstevel@tonic-gate 		op = (krb5_octet *) NULL;
21740Sstevel@tonic-gate 	    }
21752881Smp153739 
21760Sstevel@tonic-gate 	    /* Read in and parse the principal name */
21770Sstevel@tonic-gate 	    if (!read_string(filep, name, t2, linenop) &&
21782881Smp153739 		!(kret = krb5_parse_name(kcontext, name, &dbentry.princ))) {
21790Sstevel@tonic-gate 
21800Sstevel@tonic-gate 		/* Get the fixed principal attributes */
21812881Smp153739 		nread = fscanf(filep, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t",
21822881Smp153739 			       &t2, &t3, &t4, &t5, &t6, &t7, &t8, &t9);
21830Sstevel@tonic-gate 		if (nread == 8) {
21840Sstevel@tonic-gate 		    dbentry.attributes = (krb5_flags) t2;
21850Sstevel@tonic-gate 		    dbentry.max_life = (krb5_deltat) t3;
21862881Smp153739 		    dbentry.max_renewable_life = (krb5_deltat) t4;
21872881Smp153739 		    dbentry.expiration = (krb5_timestamp) t5;
21882881Smp153739 		    dbentry.pw_expiration = (krb5_timestamp) t6;
21892881Smp153739 		    dbentry.last_success = (krb5_timestamp) t7;
21902881Smp153739 		    dbentry.last_failed = (krb5_timestamp) t8;
21912881Smp153739 		    dbentry.fail_auth_count = (krb5_kvno) t9;
21924960Swillf 		    dbentry.mask = KADM5_LOAD | KADM5_PRINCIPAL | KADM5_ATTRIBUTES |
21934960Swillf 			KADM5_MAX_LIFE | KADM5_MAX_RLIFE |
21944960Swillf 			KADM5_PRINC_EXPIRE_TIME | KADM5_LAST_SUCCESS |
21954960Swillf 			KADM5_LAST_FAILED | KADM5_FAIL_AUTH_COUNT;
21960Sstevel@tonic-gate 		} else {
21970Sstevel@tonic-gate 		    try2read = read_nint_data;
21980Sstevel@tonic-gate 		    error++;
21990Sstevel@tonic-gate 		}
22000Sstevel@tonic-gate 
22010Sstevel@tonic-gate 		/*
22020Sstevel@tonic-gate 		 * Get the tagged data.
22030Sstevel@tonic-gate 		 *
22042881Smp153739 		 * Really, this code ought to discard tl data types
22052881Smp153739 		 * that it knows are special to the current version
22062881Smp153739 		 * and were not supported in the previous version.
22072881Smp153739 		 * But it's a pain to implement that here, and doing
22082881Smp153739 		 * it at dump time has almost as good an effect, so
22092881Smp153739 		 * that's what I did.  [krb5-admin/89]
22100Sstevel@tonic-gate 		 */
22110Sstevel@tonic-gate 		if (!error && dbentry.n_tl_data) {
22122881Smp153739 		    for (tl = dbentry.tl_data; tl; tl = tl->tl_data_next) {
22132881Smp153739 			nread = fscanf(filep, "%d\t%d\t", &t1, &t2);
22142881Smp153739 			if (nread == 2) {
22152881Smp153739 			    tl->tl_data_type = (krb5_int16) t1;
22162881Smp153739 			    tl->tl_data_length = (krb5_int16) t2;
22172881Smp153739 			    if (tl->tl_data_length) {
22182881Smp153739 				if (!(tl->tl_data_contents =
22192881Smp153739 				      (krb5_octet *) malloc((size_t) t2+1)) ||
22202881Smp153739 				    read_octet_string(filep,
22212881Smp153739 						      tl->tl_data_contents,
22222881Smp153739 						      t2)) {
22232881Smp153739 				    try2read = read_tcontents;
22242881Smp153739 				    error++;
22252881Smp153739 				    break;
22260Sstevel@tonic-gate 				}
22274960Swillf 				/* test to set mask fields */
22284960Swillf 				if (t1 == KRB5_TL_KADM_DATA) {
22294960Swillf 				    XDR xdrs;
22304960Swillf 				    osa_princ_ent_rec osa_princ_ent;
22314960Swillf 
22324960Swillf 				    /*
22334960Swillf 				     * Assuming aux_attributes will always be
22344960Swillf 				     * there
22354960Swillf 				     */
22364960Swillf 				    dbentry.mask |= KADM5_AUX_ATTRIBUTES;
22374960Swillf 
22384960Swillf 				    /* test for an actual policy reference */
22394960Swillf 				    memset(&osa_princ_ent, 0, sizeof(osa_princ_ent));
22404960Swillf 				    xdrmem_create(&xdrs, (char *)tl->tl_data_contents,
22414960Swillf 					    tl->tl_data_length, XDR_DECODE);
22424960Swillf 				    if (xdr_osa_princ_ent_rec(&xdrs, &osa_princ_ent) &&
22434960Swillf 					    (osa_princ_ent.aux_attributes & KADM5_POLICY) &&
22444960Swillf 					    osa_princ_ent.policy != NULL) {
22454960Swillf 
22464960Swillf 					dbentry.mask |= KADM5_POLICY;
22474960Swillf 					kdb_free_entry(NULL, NULL, &osa_princ_ent);
22484960Swillf 				    }
22494960Swillf 				    xdr_destroy(&xdrs);
22504960Swillf 				}
22512881Smp153739 			    }
22522881Smp153739 			    else {
22532881Smp153739 				/* Should be a null field */
22542881Smp153739 				nread = fscanf(filep, "%d", &t9);
22552881Smp153739 				if ((nread != 1) || (t9 != -1)) {
22562881Smp153739 				    error++;
22572881Smp153739 				    try2read = read_tcontents;
22582881Smp153739 				    break;
22592881Smp153739 				}
22602881Smp153739 			    }
22612881Smp153739 			}
22622881Smp153739 			else {
22632881Smp153739 			    try2read = read_ttypelen;
22642881Smp153739 			    error++;
22652881Smp153739 			    break;
22662881Smp153739 			}
22672881Smp153739 		    }
22684960Swillf 		    if (!error)
22694960Swillf 			dbentry.mask |= KADM5_TL_DATA;
22702881Smp153739 		}
22712881Smp153739 
22720Sstevel@tonic-gate 		/* Get the key data */
22730Sstevel@tonic-gate 		if (!error && dbentry.n_key_data) {
22742881Smp153739 		    for (i=0; !error && (i<dbentry.n_key_data); i++) {
22752881Smp153739 			kdatap = &dbentry.key_data[i];
22762881Smp153739 			nread = fscanf(filep, "%d\t%d\t", &t1, &t2);
22772881Smp153739 			if (nread == 2) {
22782881Smp153739 			    kdatap->key_data_ver = (krb5_int16) t1;
22792881Smp153739 			    kdatap->key_data_kvno = (krb5_int16) t2;
22802881Smp153739 
22812881Smp153739 			    for (j=0; j<t1; j++) {
22822881Smp153739 				nread = fscanf(filep, "%d\t%d\t", &t3, &t4);
22832881Smp153739 				if (nread == 2) {
22842881Smp153739 				    kdatap->key_data_type[j] = t3;
22852881Smp153739 				    kdatap->key_data_length[j] = t4;
22862881Smp153739 				    if (t4) {
22872881Smp153739 					if (!(kdatap->key_data_contents[j] =
22882881Smp153739 					      (krb5_octet *)
22892881Smp153739 					      malloc((size_t) t4+1)) ||
22902881Smp153739 					    read_octet_string(filep,
22912881Smp153739 							      kdatap->key_data_contents[j],
22922881Smp153739 							      t4)) {
22932881Smp153739 					    try2read = read_kcontents;
22942881Smp153739 					    error++;
22952881Smp153739 					    break;
22960Sstevel@tonic-gate 					}
22972881Smp153739 				    }
22982881Smp153739 				    else {
22992881Smp153739 					/* Should be a null field */
23002881Smp153739 					nread = fscanf(filep, "%d", &t9);
23012881Smp153739 					if ((nread != 1) || (t9 != -1)) {
23022881Smp153739 					    error++;
23032881Smp153739 					    try2read = read_kcontents;
23042881Smp153739 					    break;
23052881Smp153739 					}
23062881Smp153739 				    }
23072881Smp153739 				}
23082881Smp153739 				else {
23092881Smp153739 				    try2read = read_ktypelen;
23102881Smp153739 				    error++;
23112881Smp153739 				    break;
23122881Smp153739 				}
23132881Smp153739 			    }
23142881Smp153739 			}
23152881Smp153739 		    }
23164960Swillf 		    if (!error)
23174960Swillf 			dbentry.mask |= KADM5_KEY_DATA;
23182881Smp153739 		}
23192881Smp153739 
23200Sstevel@tonic-gate 		/* Get the extra data */
23210Sstevel@tonic-gate 		if (!error && dbentry.e_length) {
23220Sstevel@tonic-gate 		    if (read_octet_string(filep,
23230Sstevel@tonic-gate 					  dbentry.e_data,
23240Sstevel@tonic-gate 					  (int) dbentry.e_length)) {
23250Sstevel@tonic-gate 			try2read = read_econtents;
23260Sstevel@tonic-gate 			error++;
23270Sstevel@tonic-gate 		    }
23282881Smp153739 		}
23292881Smp153739 		else {
23300Sstevel@tonic-gate 		    nread = fscanf(filep, "%d", &t9);
23310Sstevel@tonic-gate 		    if ((nread != 1) || (t9 != -1)) {
23320Sstevel@tonic-gate 			error++;
23330Sstevel@tonic-gate 			try2read = read_econtents;
23340Sstevel@tonic-gate 		    }
23350Sstevel@tonic-gate 		}
23360Sstevel@tonic-gate 
23370Sstevel@tonic-gate 		/* Finally, find the end of the record. */
23380Sstevel@tonic-gate 		if (!error)
23390Sstevel@tonic-gate 		    find_record_end(filep, fname, *linenop);
23400Sstevel@tonic-gate 
23410Sstevel@tonic-gate 		/*
23422881Smp153739 		 * We have either read in all the data or choked.
23430Sstevel@tonic-gate 		 */
23440Sstevel@tonic-gate 		if (!error) {
23450Sstevel@tonic-gate 		    one = 1;
23462881Smp153739 		    if ((kret = krb5_db_put_principal(kcontext,
23470Sstevel@tonic-gate 						      &dbentry,
23480Sstevel@tonic-gate 						      &one))) {
23490Sstevel@tonic-gate 						fprintf(stderr,
23500Sstevel@tonic-gate 						    gettext(store_err_fmt),
23510Sstevel@tonic-gate 				fname, *linenop,
23520Sstevel@tonic-gate 				name, error_message(kret));
23532881Smp153739 		    }
23542881Smp153739 		    else {
23550Sstevel@tonic-gate 			if (verbose)
23560Sstevel@tonic-gate 							fprintf(stderr,
23570Sstevel@tonic-gate 							    gettext(
23580Sstevel@tonic-gate 								add_princ_fmt),
23590Sstevel@tonic-gate 							    name);
23600Sstevel@tonic-gate 			retval = 0;
23610Sstevel@tonic-gate 		    }
23622881Smp153739 		}
23632881Smp153739 		else {
23640Sstevel@tonic-gate 					fprintf(stderr, gettext(read_err_fmt),
23650Sstevel@tonic-gate 					    fname, *linenop, try2read);
23660Sstevel@tonic-gate 		}
23672881Smp153739 	    }
23682881Smp153739 	    else {
23690Sstevel@tonic-gate 		if (kret)
23700Sstevel@tonic-gate 					fprintf(stderr, gettext(parse_err_fmt),
23712881Smp153739 			    fname, *linenop, name, error_message(kret));
23720Sstevel@tonic-gate 		else
23732881Smp153739 		    fprintf(stderr, gettext(no_mem_fmt),
23740Sstevel@tonic-gate 						fname, *linenop);
23750Sstevel@tonic-gate 	    }
23762881Smp153739 	}
23772881Smp153739 	else {
23782881Smp153739 	    fprintf(stderr,
23790Sstevel@tonic-gate 				gettext(rhead_err_fmt), fname, *linenop);
23800Sstevel@tonic-gate 	}
23810Sstevel@tonic-gate 
23820Sstevel@tonic-gate 	if (op)
23830Sstevel@tonic-gate 	    free(op);
23840Sstevel@tonic-gate 	if (kp)
23850Sstevel@tonic-gate 	    free(kp);
23860Sstevel@tonic-gate 	if (name)
23870Sstevel@tonic-gate 	    free(name);
23880Sstevel@tonic-gate 	krb5_db_free_principal(kcontext, &dbentry, 1);
23892881Smp153739     }
23902881Smp153739     else {
23910Sstevel@tonic-gate 	if (nread == EOF)
23920Sstevel@tonic-gate 	    retval = -1;
23930Sstevel@tonic-gate     }
23940Sstevel@tonic-gate     return(retval);
23950Sstevel@tonic-gate }
23960Sstevel@tonic-gate 
23972881Smp153739 static int
process_k5beta7_policy(fname,kcontext,filep,verbose,linenop,pol_db)23980Sstevel@tonic-gate process_k5beta7_policy(fname, kcontext, filep, verbose, linenop, pol_db)
23990Sstevel@tonic-gate     char		*fname;
24000Sstevel@tonic-gate     krb5_context	kcontext;
24010Sstevel@tonic-gate     FILE		*filep;
24020Sstevel@tonic-gate     int			verbose;
24030Sstevel@tonic-gate     int			*linenop;
24040Sstevel@tonic-gate     void *pol_db;
24050Sstevel@tonic-gate {
24060Sstevel@tonic-gate     osa_policy_ent_rec rec;
24070Sstevel@tonic-gate     char namebuf[1024];
24080Sstevel@tonic-gate     int nread, ret;
24090Sstevel@tonic-gate 
24100Sstevel@tonic-gate     (*linenop)++;
24110Sstevel@tonic-gate     rec.name = namebuf;
24120Sstevel@tonic-gate 
24130Sstevel@tonic-gate     nread = fscanf(filep, "%1024s\t%d\t%d\t%d\t%d\t%d\t%d", rec.name,
24140Sstevel@tonic-gate 		   &rec.pw_min_life, &rec.pw_max_life,
24150Sstevel@tonic-gate 		   &rec.pw_min_length, &rec.pw_min_classes,
24160Sstevel@tonic-gate 		   &rec.pw_history_num, &rec.policy_refcnt);
24170Sstevel@tonic-gate     if (nread == EOF)
24182881Smp153739 	 return -1;
24190Sstevel@tonic-gate     else if (nread != 7) {
24200Sstevel@tonic-gate 		fprintf(stderr,
24210Sstevel@tonic-gate 		    gettext("cannot parse policy on line %d (%d read)\n"),
24220Sstevel@tonic-gate 		 *linenop, nread);
24232881Smp153739 	 return 1;
24240Sstevel@tonic-gate     }
24250Sstevel@tonic-gate 
24264960Swillf     if ((ret = krb5_db_create_policy(kcontext, &rec))) {
24274960Swillf 	 if (ret &&
24284960Swillf 	     ((ret = krb5_db_put_policy(kcontext, &rec)))) {
24290Sstevel@tonic-gate 	      fprintf(stderr, gettext("cannot create policy on line %d: %s\n"),
24300Sstevel@tonic-gate 		      *linenop, error_message(ret));
24312881Smp153739 	      return 1;
24320Sstevel@tonic-gate 	 }
24330Sstevel@tonic-gate     }
24340Sstevel@tonic-gate     if (verbose)
24350Sstevel@tonic-gate 		fprintf(stderr, gettext("created policy %s\n"), rec.name);
24360Sstevel@tonic-gate 
24372881Smp153739     return 0;
24380Sstevel@tonic-gate }
24390Sstevel@tonic-gate 
24400Sstevel@tonic-gate /*
24412881Smp153739  * process_k5beta7_record()	- Handle a dump record in krb5b7 format.
24420Sstevel@tonic-gate  *
24430Sstevel@tonic-gate  * Returns -1 for end of file, 0 for success and 1 for failure.
24440Sstevel@tonic-gate  */
24450Sstevel@tonic-gate static int
process_k5beta7_record(fname,kcontext,filep,verbose,linenop)24464960Swillf process_k5beta7_record(fname, kcontext, filep, verbose, linenop)
24470Sstevel@tonic-gate     char		*fname;
24480Sstevel@tonic-gate     krb5_context	kcontext;
24490Sstevel@tonic-gate     FILE		*filep;
24500Sstevel@tonic-gate     int			verbose;
24510Sstevel@tonic-gate     int			*linenop;
24520Sstevel@tonic-gate {
24530Sstevel@tonic-gate      int nread;
24540Sstevel@tonic-gate      char rectype[100];
24550Sstevel@tonic-gate 
24560Sstevel@tonic-gate      nread = fscanf(filep, "%100s\t", rectype);
24570Sstevel@tonic-gate      if (nread == EOF)
24582881Smp153739 	  return -1;
24590Sstevel@tonic-gate      else if (nread != 1)
24602881Smp153739 	  return 1;
24610Sstevel@tonic-gate      if (strcmp(rectype, "princ") == 0)
24620Sstevel@tonic-gate 	  process_k5beta6_record(fname, kcontext, filep, verbose,
24634960Swillf 				 linenop);
24640Sstevel@tonic-gate      else if (strcmp(rectype, "policy") == 0)
24650Sstevel@tonic-gate 	  process_k5beta7_policy(fname, kcontext, filep, verbose,
24664960Swillf 				 linenop);
24670Sstevel@tonic-gate      else {
24680Sstevel@tonic-gate 		fprintf(stderr,
24690Sstevel@tonic-gate 		    gettext("unknown record type \"%s\" on line %d\n"),
24700Sstevel@tonic-gate 		  rectype, *linenop);
24712881Smp153739 	  return 1;
24720Sstevel@tonic-gate      }
24730Sstevel@tonic-gate 
24742881Smp153739      return 0;
24750Sstevel@tonic-gate }
24760Sstevel@tonic-gate 
24770Sstevel@tonic-gate /*
24780Sstevel@tonic-gate  * process_ov_record()	- Handle a dump record in OpenV*Secure 1.0 format.
24790Sstevel@tonic-gate  *
24800Sstevel@tonic-gate  * Returns -1 for end of file, 0 for success and 1 for failure.
24810Sstevel@tonic-gate  */
24820Sstevel@tonic-gate static int
process_ov_record(fname,kcontext,filep,verbose,linenop)24834960Swillf process_ov_record(fname, kcontext, filep, verbose, linenop)
24840Sstevel@tonic-gate     char		*fname;
24850Sstevel@tonic-gate     krb5_context	kcontext;
24860Sstevel@tonic-gate     FILE		*filep;
24870Sstevel@tonic-gate     int			verbose;
24880Sstevel@tonic-gate     int			*linenop;
24890Sstevel@tonic-gate {
24900Sstevel@tonic-gate      int nread;
24910Sstevel@tonic-gate      char rectype[100];
24920Sstevel@tonic-gate 
24930Sstevel@tonic-gate      nread = fscanf(filep, "%100s\t", rectype);
24940Sstevel@tonic-gate      if (nread == EOF)
24952881Smp153739 	  return -1;
24960Sstevel@tonic-gate      else if (nread != 1)
24972881Smp153739 	  return 1;
24980Sstevel@tonic-gate      if (strcmp(rectype, "princ") == 0)
24990Sstevel@tonic-gate 	  process_ov_principal(fname, kcontext, filep, verbose,
25004960Swillf 			       linenop);
25010Sstevel@tonic-gate      else if (strcmp(rectype, "policy") == 0)
25020Sstevel@tonic-gate 	  process_k5beta7_policy(fname, kcontext, filep, verbose,
25034960Swillf 				 linenop);
25040Sstevel@tonic-gate      else if (strcmp(rectype, "End") == 0)
25052881Smp153739 	  return -1;
25060Sstevel@tonic-gate      else {
25070Sstevel@tonic-gate 		fprintf(stderr,
25080Sstevel@tonic-gate 		    gettext("unknown record type \"%s\" on line %d\n"),
25090Sstevel@tonic-gate 		  rectype, *linenop);
25102881Smp153739 	  return 1;
25110Sstevel@tonic-gate      }
25120Sstevel@tonic-gate 
25132881Smp153739      return 0;
25140Sstevel@tonic-gate }
25150Sstevel@tonic-gate 
25160Sstevel@tonic-gate /*
25170Sstevel@tonic-gate  * restore_dump()	- Restore the database from any version dump file.
25180Sstevel@tonic-gate  */
25190Sstevel@tonic-gate static int
restore_dump(programname,kcontext,dumpfile,f,verbose,dump)25204960Swillf restore_dump(programname, kcontext, dumpfile, f, verbose, dump)
25210Sstevel@tonic-gate     char		*programname;
25220Sstevel@tonic-gate     krb5_context	kcontext;
25230Sstevel@tonic-gate     char		*dumpfile;
25240Sstevel@tonic-gate     FILE		*f;
25250Sstevel@tonic-gate     int			verbose;
25260Sstevel@tonic-gate     dump_version	*dump;
25270Sstevel@tonic-gate {
25280Sstevel@tonic-gate     int		error;
25290Sstevel@tonic-gate     int		lineno;
25300Sstevel@tonic-gate 
25310Sstevel@tonic-gate     error = 0;
25320Sstevel@tonic-gate     lineno = 1;
25330Sstevel@tonic-gate 
25340Sstevel@tonic-gate     /*
25350Sstevel@tonic-gate      * Process the records.
25360Sstevel@tonic-gate      */
25370Sstevel@tonic-gate     while (!(error = (*dump->load_record)(dumpfile,
25380Sstevel@tonic-gate 					  kcontext,
25390Sstevel@tonic-gate 					  f,
25400Sstevel@tonic-gate 					  verbose,
25414960Swillf 					  &lineno)))
25422881Smp153739 	 ;
25430Sstevel@tonic-gate     if (error != -1)
25440Sstevel@tonic-gate 		fprintf(stderr, gettext(err_line_fmt),
25450Sstevel@tonic-gate 		    programname, lineno, dumpfile);
25460Sstevel@tonic-gate     else
25470Sstevel@tonic-gate 	 error = 0;
25480Sstevel@tonic-gate 
25490Sstevel@tonic-gate     return(error);
25500Sstevel@tonic-gate }
25510Sstevel@tonic-gate 
25520Sstevel@tonic-gate /*
25532881Smp153739  * Usage: load_db [-i] [-old] [-ov] [-b6] [-b7] [-verbose] [-update] [-hash]
25542881Smp153739  *		filename
25550Sstevel@tonic-gate  */
25560Sstevel@tonic-gate void
load_db(argc,argv)25570Sstevel@tonic-gate load_db(argc, argv)
25580Sstevel@tonic-gate     int		argc;
25590Sstevel@tonic-gate     char	**argv;
25600Sstevel@tonic-gate {
25610Sstevel@tonic-gate     kadm5_config_params newparams;
25620Sstevel@tonic-gate     krb5_error_code	kret;
25630Sstevel@tonic-gate     krb5_context	kcontext;
25640Sstevel@tonic-gate     FILE		*f;
25650Sstevel@tonic-gate     extern char		*optarg;
25660Sstevel@tonic-gate     extern int		optind;
2567*8092SMark.Phalan@Sun.COM /* Solaris Kerberos */
2568*8092SMark.Phalan@Sun.COM #if 0
25690Sstevel@tonic-gate     char		*programname;
2570*8092SMark.Phalan@Sun.COM #endif
25710Sstevel@tonic-gate     char		*dumpfile;
25720Sstevel@tonic-gate     char		*dbname;
25730Sstevel@tonic-gate     char		*dbname_tmp;
25740Sstevel@tonic-gate     char		buf[BUFSIZ];
25750Sstevel@tonic-gate     dump_version	*load;
25760Sstevel@tonic-gate     int			update, verbose;
25770Sstevel@tonic-gate     krb5_int32		crflags;
25780Sstevel@tonic-gate     int			aindex;
25790Sstevel@tonic-gate     bool_t		add_update = TRUE;
25800Sstevel@tonic-gate     char		iheader[MAX_HEADER];
25810Sstevel@tonic-gate     uint32_t		caller, last_sno, last_seconds, last_useconds;
25820Sstevel@tonic-gate     kdb_log_context	*log_ctx;
25834960Swillf     int			db_locked = 0;
25840Sstevel@tonic-gate 
25850Sstevel@tonic-gate     /*
25860Sstevel@tonic-gate      * Parse the arguments.
25870Sstevel@tonic-gate      */
2588*8092SMark.Phalan@Sun.COM /* Solaris Kerberos */
2589*8092SMark.Phalan@Sun.COM #if 0
25900Sstevel@tonic-gate     programname = argv[0];
25910Sstevel@tonic-gate     if (strrchr(programname, (int) '/'))
25920Sstevel@tonic-gate 	programname = strrchr(argv[0], (int) '/') + 1;
2593*8092SMark.Phalan@Sun.COM #endif
25940Sstevel@tonic-gate     dumpfile = (char *) NULL;
25950Sstevel@tonic-gate     dbname = global_params.dbname;
25960Sstevel@tonic-gate     load = NULL;
25970Sstevel@tonic-gate     update = 0;
25980Sstevel@tonic-gate     verbose = 0;
25990Sstevel@tonic-gate     crflags = KRB5_KDB_CREATE_BTREE;
26000Sstevel@tonic-gate     exit_status = 0;
26010Sstevel@tonic-gate     dbname_tmp = (char *) NULL;
26020Sstevel@tonic-gate     log_ctx = util_context->kdblog_context;
26030Sstevel@tonic-gate 
26040Sstevel@tonic-gate     for (aindex = 1; aindex < argc; aindex++) {
26052881Smp153739 	if (!strcmp(argv[aindex], oldoption))
26060Sstevel@tonic-gate 	     load = &old_version;
26072881Smp153739 	else if (!strcmp(argv[aindex], b6option))
26080Sstevel@tonic-gate 	     load = &beta6_version;
26092881Smp153739 	else if (!strcmp(argv[aindex], b7option))
26102881Smp153739 	     load = &beta7_version;
26112881Smp153739 	else if (!strcmp(argv[aindex], ovoption))
26120Sstevel@tonic-gate 	     load = &ov_version;
26132881Smp153739 	else if (!strcmp(argv[aindex], ipropoption)) {
26140Sstevel@tonic-gate 			if (log_ctx && log_ctx->iproprole) {
26150Sstevel@tonic-gate 				load = &iprop_version;
26160Sstevel@tonic-gate 				add_update = FALSE;
26170Sstevel@tonic-gate 			} else {
26180Sstevel@tonic-gate 				fprintf(stderr, gettext("Iprop not enabled\n"));
26190Sstevel@tonic-gate 				exit_status++;
26200Sstevel@tonic-gate 				return;
26210Sstevel@tonic-gate 			}
26222881Smp153739 		}
26232881Smp153739 	else if (!strcmp(argv[aindex], verboseoption))
26240Sstevel@tonic-gate 	    verbose = 1;
26252881Smp153739 	else if (!strcmp(argv[aindex], updateoption))
26260Sstevel@tonic-gate 	    update = 1;
26274960Swillf 	else if (!strcmp(argv[aindex], hashoption)) {
26284960Swillf 	    if (!add_db_arg("hash=true")) {
26294960Swillf 		com_err(progname, ENOMEM, "while parsing command arguments\n");
26304960Swillf 		exit(1);
26314960Swillf 	    }
26324960Swillf 	} else
26330Sstevel@tonic-gate 	    break;
26340Sstevel@tonic-gate     }
26350Sstevel@tonic-gate     if ((argc - aindex) != 1) {
26360Sstevel@tonic-gate 	usage();
26370Sstevel@tonic-gate 	return;
26380Sstevel@tonic-gate     }
26390Sstevel@tonic-gate     dumpfile = argv[aindex];
26400Sstevel@tonic-gate 
26410Sstevel@tonic-gate     if (!(dbname_tmp = (char *) malloc(strlen(dbname)+
26420Sstevel@tonic-gate 				       strlen(dump_tmptrail)+1))) {
2643*8092SMark.Phalan@Sun.COM 		/* Solaris Kerberos */
2644*8092SMark.Phalan@Sun.COM 		fprintf(stderr, gettext(no_name_mem_fmt), progname);
26450Sstevel@tonic-gate 	exit_status++;
26460Sstevel@tonic-gate 	return;
26470Sstevel@tonic-gate     }
26480Sstevel@tonic-gate     strcpy(dbname_tmp, dbname);
26490Sstevel@tonic-gate     strcat(dbname_tmp, dump_tmptrail);
26500Sstevel@tonic-gate 
26510Sstevel@tonic-gate     /*
26520Sstevel@tonic-gate      * Initialize the Kerberos context and error tables.
26530Sstevel@tonic-gate      */
26544960Swillf     if ((kret = kadm5_init_krb5_context(&kcontext))) {
2655*8092SMark.Phalan@Sun.COM 	/* Solaris Kerberos */
2656*8092SMark.Phalan@Sun.COM 	fprintf(stderr, gettext(ctx_err_fmt), progname);
26570Sstevel@tonic-gate 	free(dbname_tmp);
26580Sstevel@tonic-gate 	exit_status++;
26590Sstevel@tonic-gate 	return;
26600Sstevel@tonic-gate     }
26610Sstevel@tonic-gate 
26624960Swillf     if( (kret = krb5_set_default_realm(kcontext, util_context->default_realm)) )
26634960Swillf     {
2664*8092SMark.Phalan@Sun.COM 	/* Solaris Kerberos */
2665*8092SMark.Phalan@Sun.COM 	fprintf(stderr, gettext("%s: Unable to set the default realm\n"), progname);
26664960Swillf 	free(dbname_tmp);
26674960Swillf 	exit_status++;
26684960Swillf 	return;
26694960Swillf     }
26700Sstevel@tonic-gate     if (log_ctx && log_ctx->iproprole)
26710Sstevel@tonic-gate 	kcontext->kdblog_context = (void *)log_ctx;
26720Sstevel@tonic-gate     /*
26730Sstevel@tonic-gate      * Open the dumpfile
26740Sstevel@tonic-gate      */
26750Sstevel@tonic-gate     if (dumpfile) {
26764960Swillf 	if ((f = fopen(dumpfile, "r")) == NULL) {
2677*8092SMark.Phalan@Sun.COM 			/* Solaris Kerberos */
26780Sstevel@tonic-gate 			fprintf(stderr, gettext(dfile_err_fmt),
2679*8092SMark.Phalan@Sun.COM 			    progname, dumpfile,
26800Sstevel@tonic-gate 		     error_message(errno));
26810Sstevel@tonic-gate 	     exit_status++;
26820Sstevel@tonic-gate 	     return;
26830Sstevel@tonic-gate 	}
26840Sstevel@tonic-gate 	if ((kret = krb5_lock_file(kcontext, fileno(f),
26850Sstevel@tonic-gate 				   KRB5_LOCKMODE_SHARED))) {
2686*8092SMark.Phalan@Sun.COM 	     /* Solaris Kerberos */
2687*8092SMark.Phalan@Sun.COM 	     fprintf(stderr, gettext("%s: Cannot lock %s: %s\n"), progname,
26880Sstevel@tonic-gate 		     dumpfile, error_message(errno));
26890Sstevel@tonic-gate 	     exit_status++;
26900Sstevel@tonic-gate 	     return;
26910Sstevel@tonic-gate 	}
26920Sstevel@tonic-gate     } else
26930Sstevel@tonic-gate 	f = stdin;
26940Sstevel@tonic-gate 
26950Sstevel@tonic-gate     /*
26962881Smp153739      * Auto-detect dump version if we weren't told, verify if we
26972881Smp153739      * were told.
26980Sstevel@tonic-gate      */
26990Sstevel@tonic-gate     fgets(buf, sizeof(buf), f);
27000Sstevel@tonic-gate     if (load) {
27012881Smp153739 	 /* only check what we know; some headers only contain a prefix */
27020Sstevel@tonic-gate 	 if (strncmp(buf, load->header, strlen(load->header)) != 0) {
2703*8092SMark.Phalan@Sun.COM 			/* Solaris Kerberos */
2704*8092SMark.Phalan@Sun.COM 			fprintf(stderr, gettext(head_bad_fmt), progname, dumpfile);
27050Sstevel@tonic-gate 	      exit_status++;
27062881Smp153739 	      if (dumpfile) fclose(f);
27070Sstevel@tonic-gate 	      return;
27080Sstevel@tonic-gate 	 }
27090Sstevel@tonic-gate     } else {
27100Sstevel@tonic-gate 	 /* perhaps this should be in an array, but so what? */
27110Sstevel@tonic-gate 	 if (strcmp(buf, old_version.header) == 0)
27120Sstevel@tonic-gate 	      load = &old_version;
27130Sstevel@tonic-gate 	 else if (strcmp(buf, beta6_version.header) == 0)
27140Sstevel@tonic-gate 	      load = &beta6_version;
27150Sstevel@tonic-gate 	 else if (strcmp(buf, beta7_version.header) == 0)
27160Sstevel@tonic-gate 	      load = &beta7_version;
27172881Smp153739 	 else if (strcmp(buf, r1_3_version.header) == 0)
27182881Smp153739 	      load = &r1_3_version;
27190Sstevel@tonic-gate 	 else if (strncmp(buf, ov_version.header,
27200Sstevel@tonic-gate 			  strlen(ov_version.header)) == 0)
27210Sstevel@tonic-gate 	      load = &ov_version;
27222881Smp153739 	 else {
2723*8092SMark.Phalan@Sun.COM 			/* Solaris Kerberos */
27240Sstevel@tonic-gate 			fprintf(stderr, gettext(head_bad_fmt),
2725*8092SMark.Phalan@Sun.COM 				progname, dumpfile);
27260Sstevel@tonic-gate 	      exit_status++;
27272881Smp153739 	      if (dumpfile) fclose(f);
27280Sstevel@tonic-gate 	      return;
27290Sstevel@tonic-gate 	 }
27300Sstevel@tonic-gate     }
27310Sstevel@tonic-gate     if (load->updateonly && !update) {
2732*8092SMark.Phalan@Sun.COM 		/* Solaris Kerberos */
27330Sstevel@tonic-gate 		fprintf(stderr,
27340Sstevel@tonic-gate 		    gettext("%s: dump version %s can only "
27350Sstevel@tonic-gate 			"be loaded with the -update flag\n"),
2736*8092SMark.Phalan@Sun.COM 		    progname, load->name);
27370Sstevel@tonic-gate 	 exit_status++;
27380Sstevel@tonic-gate 	 return;
27390Sstevel@tonic-gate     }
27402881Smp153739 
27410Sstevel@tonic-gate     /*
27420Sstevel@tonic-gate      * Cons up params for the new databases.  If we are not in update
27434960Swillf      * mode, we create an alternate database and then promote it to
27444960Swillf      * be the live db.
27450Sstevel@tonic-gate      */
27460Sstevel@tonic-gate     newparams = global_params;
27470Sstevel@tonic-gate     if (! update) {
27480Sstevel@tonic-gate 	 newparams.mask |= KADM5_CONFIG_DBNAME;
27490Sstevel@tonic-gate 	 newparams.dbname = dbname_tmp;
27500Sstevel@tonic-gate 
27517934SMark.Phalan@Sun.COM 	 if ((kret = kadm5_get_config_params(kcontext, 1,
27520Sstevel@tonic-gate 					     &newparams, &newparams))) {
2753*8092SMark.Phalan@Sun.COM 	      /* Solaris Kerberos */
2754*8092SMark.Phalan@Sun.COM 	      com_err(progname, kret,
27550Sstevel@tonic-gate 			    gettext("while retreiving new "
27560Sstevel@tonic-gate 				"configuration parameters"));
27570Sstevel@tonic-gate 	      exit_status++;
27580Sstevel@tonic-gate 	      return;
27590Sstevel@tonic-gate 	 }
27604960Swillf 
27614960Swillf 	 if (!add_db_arg("temporary")) {
27624960Swillf 	     com_err(progname, ENOMEM, "computing parameters for database");
27634960Swillf 	     exit(1);
27644960Swillf 	 }
27650Sstevel@tonic-gate     }
27662881Smp153739 
27670Sstevel@tonic-gate     /*
27684960Swillf      * If not an update restoration, create the database. otherwise open
27690Sstevel@tonic-gate      */
27704960Swillf     if (!update) {
27714960Swillf 	if((kret = krb5_db_create(kcontext, db5util_db_args))) {
27724960Swillf 	    const char *emsg = krb5_get_error_message(kcontext, kret);
27734960Swillf 	    /*
27744960Swillf 	     * See if something (like DAL KDB plugin) has set a specific error
27754960Swillf 	     * message and use that otherwise use default.
27764960Swillf 	     */
27774960Swillf 
27784960Swillf 	    if (emsg != NULL) {
2779*8092SMark.Phalan@Sun.COM 		/* Solaris Kerberos */
2780*8092SMark.Phalan@Sun.COM 		fprintf(stderr, "%s: %s\n", progname, emsg);
27814960Swillf 		krb5_free_error_message (kcontext, emsg);
27824960Swillf 	    } else {
2783*8092SMark.Phalan@Sun.COM 		/* Solaris Kerberos */
27844960Swillf 		fprintf(stderr, dbcreaterr_fmt,
2785*8092SMark.Phalan@Sun.COM 			progname, dbname, error_message(kret));
27864960Swillf 	    }
27874960Swillf 	    exit_status++;
27884960Swillf 	    kadm5_free_config_params(kcontext, &newparams);
27894960Swillf 	    if (dumpfile) fclose(f);
27904960Swillf 	    return;
27914960Swillf 	}
27920Sstevel@tonic-gate     }
27934960Swillf     else {
27944960Swillf 	    /*
27954960Swillf 	     * Initialize the database.
27964960Swillf 	     */
27974960Swillf 	    if ((kret = krb5_db_open(kcontext, db5util_db_args,
27984960Swillf 				     KRB5_KDB_OPEN_RW | KRB5_KDB_SRV_TYPE_ADMIN))) {
27994960Swillf 		const char *emsg = krb5_get_error_message(kcontext, kret);
28004960Swillf 		/*
28014960Swillf 		 * See if something (like DAL KDB plugin) has set a specific
28024960Swillf 		 * error message and use that otherwise use default.
28034960Swillf 		 */
28044960Swillf 
28054960Swillf 		if (emsg != NULL) {
2806*8092SMark.Phalan@Sun.COM 		    /* Solaris Kerberos */
2807*8092SMark.Phalan@Sun.COM 		    fprintf(stderr, "%s: %s\n", progname, emsg);
28084960Swillf 		    krb5_free_error_message (kcontext, emsg);
28094960Swillf 		} else {
2810*8092SMark.Phalan@Sun.COM 		    /* Solaris Kerberos */
28114960Swillf 		    fprintf(stderr, dbinit_err_fmt,
2812*8092SMark.Phalan@Sun.COM 			    progname, error_message(kret));
28134960Swillf 		}
28144960Swillf 		exit_status++;
28154960Swillf 		goto error;
28164960Swillf 	    }
28170Sstevel@tonic-gate     }
28182881Smp153739 
28194960Swillf 
28200Sstevel@tonic-gate     /*
28210Sstevel@tonic-gate      * If an update restoration, make sure the db is left unusable if
28220Sstevel@tonic-gate      * the update fails.
28230Sstevel@tonic-gate      */
28244960Swillf     if ((kret = krb5_db_lock(kcontext, update?KRB5_DB_LOCKMODE_PERMANENT: KRB5_DB_LOCKMODE_EXCLUSIVE))) {
28254960Swillf 	/*
28264960Swillf 	 * Ignore a not supported error since there is nothing to do about it
28274960Swillf 	 * anyway.
28284960Swillf 	 */
28294960Swillf 	if (kret != KRB5_PLUGIN_OP_NOTSUPP) {
2830*8092SMark.Phalan@Sun.COM 	    /* Solaris Kerberos */
28314960Swillf 	    fprintf(stderr, gettext("%s: %s while permanently locking database\n"),
2832*8092SMark.Phalan@Sun.COM 		    progname, error_message(kret));
28334960Swillf 	    exit_status++;
28344960Swillf 	    goto error;
28354960Swillf 	}
28360Sstevel@tonic-gate     }
28374960Swillf     else
28384960Swillf 	db_locked = 1;
28392881Smp153739 
28400Sstevel@tonic-gate 	if (log_ctx && log_ctx->iproprole) {
28410Sstevel@tonic-gate 		if (add_update)
28420Sstevel@tonic-gate 			caller = FKCOMMAND;
28430Sstevel@tonic-gate 		else
28440Sstevel@tonic-gate 			caller = FKPROPD;
28450Sstevel@tonic-gate 
28460Sstevel@tonic-gate 		if (ulog_map(kcontext, &global_params, caller)) {
2847*8092SMark.Phalan@Sun.COM 			/* Solaris Kerberos */
28480Sstevel@tonic-gate 			fprintf(stderr,
28490Sstevel@tonic-gate 				gettext("%s: Could not map log\n"),
2850*8092SMark.Phalan@Sun.COM 				progname);
28510Sstevel@tonic-gate 			exit_status++;
28520Sstevel@tonic-gate 			goto error;
28530Sstevel@tonic-gate 		}
28540Sstevel@tonic-gate 
28550Sstevel@tonic-gate 		/*
28560Sstevel@tonic-gate 		 * We don't want to take out the ulog out from underneath
28570Sstevel@tonic-gate 		 * kadmind so we reinit the header log.
28580Sstevel@tonic-gate 		 *
28590Sstevel@tonic-gate 		 * We also don't want to add to the update log since we
28600Sstevel@tonic-gate 		 * are doing a whole sale replace of the db, because:
28610Sstevel@tonic-gate 		 * 	we could easily exceed # of update entries
28620Sstevel@tonic-gate 		 * 	we could implicity delete db entries during a replace
28630Sstevel@tonic-gate 		 *	no advantage in incr updates when entire db is replaced
28640Sstevel@tonic-gate 		 */
28650Sstevel@tonic-gate 		if (!update) {
28660Sstevel@tonic-gate                         memset(log_ctx->ulog, 0, sizeof (kdb_hlog_t));
28670Sstevel@tonic-gate 
28680Sstevel@tonic-gate                         log_ctx->ulog->kdb_hmagic = KDB_HMAGIC;
28690Sstevel@tonic-gate                         log_ctx->ulog->db_version_num = KDB_VERSION;
28700Sstevel@tonic-gate                         log_ctx->ulog->kdb_state = KDB_STABLE;
28710Sstevel@tonic-gate                         log_ctx->ulog->kdb_block = ULOG_BLOCK;
28720Sstevel@tonic-gate 
28730Sstevel@tonic-gate 			log_ctx->iproprole = IPROP_NULL;
28740Sstevel@tonic-gate 
28750Sstevel@tonic-gate 			if (!add_update) {
28760Sstevel@tonic-gate 				sscanf(buf, "%s %u %u %u", iheader, &last_sno,
28770Sstevel@tonic-gate 					&last_seconds, &last_useconds);
28780Sstevel@tonic-gate 
28790Sstevel@tonic-gate 				log_ctx->ulog->kdb_last_sno = last_sno;
28800Sstevel@tonic-gate 				log_ctx->ulog->kdb_last_time.seconds =
28810Sstevel@tonic-gate 				    last_seconds;
28820Sstevel@tonic-gate 				log_ctx->ulog->kdb_last_time.useconds =
28830Sstevel@tonic-gate 				    last_useconds;
28840Sstevel@tonic-gate 			}
28850Sstevel@tonic-gate 		}
28860Sstevel@tonic-gate 	}
28870Sstevel@tonic-gate 
2888*8092SMark.Phalan@Sun.COM     /* Solaris Kerberos */
2889*8092SMark.Phalan@Sun.COM     if (restore_dump(progname, kcontext, (dumpfile) ? dumpfile : stdin_name,
28904960Swillf 		     f, verbose, load)) {
2891*8092SMark.Phalan@Sun.COM 	 /* Solaris Kerberos */
28924960Swillf 	 fprintf(stderr, gettext(restfail_fmt),
2893*8092SMark.Phalan@Sun.COM 		 progname, load->name);
28940Sstevel@tonic-gate 	 exit_status++;
28950Sstevel@tonic-gate     }
28962881Smp153739 
28970Sstevel@tonic-gate     if (!update && load->create_kadm5 &&
28982881Smp153739 	((kret = kadm5_create_magic_princs(&newparams, kcontext)))) {
28990Sstevel@tonic-gate 	 /* error message printed by create_magic_princs */
29000Sstevel@tonic-gate 	 exit_status++;
29010Sstevel@tonic-gate     }
29020Sstevel@tonic-gate 
29034960Swillf     if (db_locked && (kret = krb5_db_unlock(kcontext))) {
29044960Swillf 	 /* change this error? */
2905*8092SMark.Phalan@Sun.COM 		/* Solaris Kerberos */
29064960Swillf 		fprintf(stderr, gettext(dbunlockerr_fmt),
2907*8092SMark.Phalan@Sun.COM 		 progname, dbname, error_message(kret));
29084960Swillf 	 exit_status++;
29094960Swillf     }
29104960Swillf 
29114960Swillf #if 0
29124960Swillf     if ((kret = krb5_db_fini(kcontext))) {
2913*8092SMark.Phalan@Sun.COM 		/* Solaris Kerberos */
29144960Swillf 		fprintf(stderr, gettext(close_err_fmt),
2915*8092SMark.Phalan@Sun.COM 		 progname, error_message(kret));
29164960Swillf 	 exit_status++;
29174960Swillf     }
29184960Swillf #endif
29194960Swillf 
29200Sstevel@tonic-gate     /* close policy db below */
29210Sstevel@tonic-gate 
29224960Swillf     if (exit_status == 0 && !update) {
29234960Swillf 	kret = krb5_db_promote(kcontext, db5util_db_args);
29244960Swillf 	/*
29254960Swillf 	 * Ignore a not supported error since there is nothing to do about it
29264960Swillf 	 * anyway.
29274960Swillf 	 */
29284960Swillf 	if (kret != 0 && kret != KRB5_PLUGIN_OP_NOTSUPP) {
2929*8092SMark.Phalan@Sun.COM 	    /* Solaris Kerberos */
29304960Swillf 	    fprintf(stderr, gettext("%s: cannot make newly loaded database live (%s)\n"),
2931*8092SMark.Phalan@Sun.COM 		    progname, error_message(kret));
29324960Swillf 	    exit_status++;
29334960Swillf 	}
29344960Swillf     }
29354960Swillf 
29360Sstevel@tonic-gate error:
29370Sstevel@tonic-gate     /*
29382881Smp153739      * If not an update: if there was an error, destroy the temp database,
29392881Smp153739      * otherwise rename it into place.
29400Sstevel@tonic-gate      *
29410Sstevel@tonic-gate      * If an update: if there was no error, unlock the database.
29420Sstevel@tonic-gate      */
29430Sstevel@tonic-gate     if (!update) {
29440Sstevel@tonic-gate 	 if (exit_status) {
29454960Swillf 	      kret = krb5_db_destroy(kcontext, db5util_db_args);
29464960Swillf 	      /*
29474960Swillf 	       * Ignore a not supported error since there is nothing to do about
29484960Swillf 	       * it anyway.
29494960Swillf 	       */
29504960Swillf 	      if (kret != 0 && kret != KRB5_PLUGIN_OP_NOTSUPP) {
2951*8092SMark.Phalan@Sun.COM 		   /* Solaris Kerberos */
29524960Swillf 		   fprintf(stderr, gettext(dbdelerr_fmt),
2953*8092SMark.Phalan@Sun.COM 			   progname, dbname, error_message(kret));
29540Sstevel@tonic-gate 		   exit_status++;
29550Sstevel@tonic-gate 	      }
29562881Smp153739 	 }
29570Sstevel@tonic-gate     }
29580Sstevel@tonic-gate 
29590Sstevel@tonic-gate     if (dumpfile) {
29602881Smp153739 	 (void) krb5_lock_file(kcontext, fileno(f), KRB5_LOCKMODE_UNLOCK);
29610Sstevel@tonic-gate 	 fclose(f);
29620Sstevel@tonic-gate     }
29632881Smp153739 
29640Sstevel@tonic-gate     if (dbname_tmp)
29650Sstevel@tonic-gate 	 free(dbname_tmp);
29660Sstevel@tonic-gate     krb5_free_context(kcontext);
29670Sstevel@tonic-gate }
2968