xref: /illumos-gate/usr/src/cmd/krb5/kadmin/dbutil/dump.c (revision 55fea89d)
17c478bd9Sstevel@tonic-gate /*
22dd2efa5Swillf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  *	Openvision retains the copyright to derivative works of
107c478bd9Sstevel@tonic-gate  *	this source code.  Do *NOT* create a derivative of this
117c478bd9Sstevel@tonic-gate  *	source code before consulting with your legal department.
127c478bd9Sstevel@tonic-gate  *	Do *NOT* integrate *ANY* of this source code into another
137c478bd9Sstevel@tonic-gate  *	product before consulting with your legal department.
147c478bd9Sstevel@tonic-gate  *
157c478bd9Sstevel@tonic-gate  *	For further information, read the top-level Openvision
167c478bd9Sstevel@tonic-gate  *	copyright which is contained in the top-level MIT Kerberos
177c478bd9Sstevel@tonic-gate  *	copyright.
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
207c478bd9Sstevel@tonic-gate  *
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate /*
257c478bd9Sstevel@tonic-gate  * admin/edit/dump.c
267c478bd9Sstevel@tonic-gate  *
277c478bd9Sstevel@tonic-gate  * Copyright 1990,1991 by the Massachusetts Institute of Technology.
287c478bd9Sstevel@tonic-gate  * All Rights Reserved.
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may
317c478bd9Sstevel@tonic-gate  *   require a specific license from the United States Government.
327c478bd9Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
337c478bd9Sstevel@tonic-gate  *   export to obtain such a license before exporting.
34*55fea89dSDan Cross  *
357c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
367c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
377c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
387c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
397c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
407c478bd9Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
417c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
427c478bd9Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
437c478bd9Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
447c478bd9Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
457c478bd9Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
467c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
477c478bd9Sstevel@tonic-gate  * or implied warranty.
48*55fea89dSDan Cross  *
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  * Dump a KDC database
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #include <stdio.h>
5454925bf6Swillf #include <k5-int.h>
557c478bd9Sstevel@tonic-gate #include <kadm5/admin.h>
5654925bf6Swillf #include <kadm5/server_internal.h>
5754925bf6Swillf #include <kdb.h>
5854925bf6Swillf #include <com_err.h>
597c478bd9Sstevel@tonic-gate #include <libintl.h>
607c478bd9Sstevel@tonic-gate #include "kdb5_util.h"
6156a424ccSmp #if defined(HAVE_REGEX_H) && defined(HAVE_REGCOMP)
627c478bd9Sstevel@tonic-gate #include <regex.h>
637c478bd9Sstevel@tonic-gate #endif	/* HAVE_REGEX_H */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * Needed for master key conversion.
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate extern krb5_keyblock master_key;
697c478bd9Sstevel@tonic-gate extern krb5_principal master_princ;
707c478bd9Sstevel@tonic-gate static int			mkey_convert;
717c478bd9Sstevel@tonic-gate static krb5_keyblock		new_master_key;
727c478bd9Sstevel@tonic-gate 
7356a424ccSmp static int	backwards;
7456a424ccSmp static int	recursive;
7556a424ccSmp 
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate  * Use compile(3) if no regcomp present.
787c478bd9Sstevel@tonic-gate  */
797c478bd9Sstevel@tonic-gate #if	!defined(HAVE_REGCOMP) && defined(HAVE_REGEXP_H)
807c478bd9Sstevel@tonic-gate #define	INIT		char *sp = instring;
817c478bd9Sstevel@tonic-gate #define	GETC()		(*sp++)
827c478bd9Sstevel@tonic-gate #define	PEEKC()		(*sp)
837c478bd9Sstevel@tonic-gate #define	UNGETC(c)	(--sp)
847c478bd9Sstevel@tonic-gate #define	RETURN(c)	return(c)
85*55fea89dSDan Cross #define	ERROR(c)
867c478bd9Sstevel@tonic-gate #define	RE_BUF_SIZE	1024
877c478bd9Sstevel@tonic-gate #include <regexp.h>
887c478bd9Sstevel@tonic-gate #endif	/* !HAVE_REGCOMP && HAVE_REGEXP_H */
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate struct dump_args {
917c478bd9Sstevel@tonic-gate     char		*programname;
927c478bd9Sstevel@tonic-gate     FILE		*ofile;
937c478bd9Sstevel@tonic-gate     krb5_context	kcontext;
947c478bd9Sstevel@tonic-gate     char		**names;
957c478bd9Sstevel@tonic-gate     int			nnames;
967c478bd9Sstevel@tonic-gate     int			verbose;
977c478bd9Sstevel@tonic-gate };
987c478bd9Sstevel@tonic-gate 
9956a424ccSmp static krb5_error_code dump_k5beta_iterator (krb5_pointer,
10056a424ccSmp 					     krb5_db_entry *);
10156a424ccSmp static krb5_error_code dump_k5beta6_iterator (krb5_pointer,
10256a424ccSmp 					      krb5_db_entry *);
10356a424ccSmp static krb5_error_code dump_k5beta6_iterator_ext (krb5_pointer,
10456a424ccSmp 						  krb5_db_entry *,
10556a424ccSmp 						  int);
10656a424ccSmp static krb5_error_code dump_iprop_iterator (krb5_pointer,
10756a424ccSmp 						  krb5_db_entry *);
10856a424ccSmp static krb5_error_code dump_k5beta7_princ (krb5_pointer,
10956a424ccSmp 					   krb5_db_entry *);
11056a424ccSmp static krb5_error_code dump_k5beta7_princ_ext (krb5_pointer,
11156a424ccSmp 					       krb5_db_entry *,
11256a424ccSmp 					       int);
11356a424ccSmp static krb5_error_code dump_k5beta7_princ_withpolicy
11456a424ccSmp 			(krb5_pointer, krb5_db_entry *);
11556a424ccSmp static krb5_error_code dump_iprop_princ (krb5_pointer,
11656a424ccSmp 					       krb5_db_entry *);
11756a424ccSmp static krb5_error_code dump_ov_princ (krb5_pointer,
11856a424ccSmp 				      krb5_db_entry *);
119505d05c7Sgtb static void dump_k5beta7_policy (void *, osa_policy_ent_t);
1207c478bd9Sstevel@tonic-gate 
12156a424ccSmp typedef krb5_error_code (*dump_func)(krb5_pointer,
12256a424ccSmp 				     krb5_db_entry *);
1237c478bd9Sstevel@tonic-gate 
12456a424ccSmp static int process_k5beta_record (char *, krb5_context,
12554925bf6Swillf 				  FILE *, int, int *);
12656a424ccSmp static int process_k5beta6_record (char *, krb5_context,
12754925bf6Swillf 				   FILE *, int, int *);
12856a424ccSmp static int process_k5beta7_record (char *, krb5_context,
12954925bf6Swillf 				   FILE *, int, int *);
13056a424ccSmp static int process_ov_record (char *, krb5_context,
13154925bf6Swillf 			      FILE *, int, int *);
13256a424ccSmp typedef krb5_error_code (*load_func)(char *, krb5_context,
13354925bf6Swillf 				     FILE *, int, int *);
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate typedef struct _dump_version {
1367c478bd9Sstevel@tonic-gate      char *name;
1377c478bd9Sstevel@tonic-gate      char *header;
1387c478bd9Sstevel@tonic-gate      int updateonly;
1397c478bd9Sstevel@tonic-gate      int create_kadm5;
1407c478bd9Sstevel@tonic-gate      dump_func dump_princ;
1417c478bd9Sstevel@tonic-gate      osa_adb_iter_policy_func dump_policy;
1427c478bd9Sstevel@tonic-gate      load_func load_record;
1437c478bd9Sstevel@tonic-gate } dump_version;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate dump_version old_version = {
1467c478bd9Sstevel@tonic-gate      "Kerberos version 5 old format",
1477c478bd9Sstevel@tonic-gate      "kdb5_edit load_dump version 2.0\n",
1487c478bd9Sstevel@tonic-gate      0,
1497c478bd9Sstevel@tonic-gate      1,
1507c478bd9Sstevel@tonic-gate      dump_k5beta_iterator,
1517c478bd9Sstevel@tonic-gate      NULL,
15254925bf6Swillf      process_k5beta_record
1537c478bd9Sstevel@tonic-gate };
1547c478bd9Sstevel@tonic-gate dump_version beta6_version = {
1557c478bd9Sstevel@tonic-gate      "Kerberos version 5 beta 6 format",
1567c478bd9Sstevel@tonic-gate      "kdb5_edit load_dump version 3.0\n",
1577c478bd9Sstevel@tonic-gate      0,
1587c478bd9Sstevel@tonic-gate      1,
1597c478bd9Sstevel@tonic-gate      dump_k5beta6_iterator,
1607c478bd9Sstevel@tonic-gate      NULL,
16154925bf6Swillf      process_k5beta6_record
1627c478bd9Sstevel@tonic-gate };
1637c478bd9Sstevel@tonic-gate dump_version beta7_version = {
1647c478bd9Sstevel@tonic-gate      "Kerberos version 5",
1657c478bd9Sstevel@tonic-gate      "kdb5_util load_dump version 4\n",
1667c478bd9Sstevel@tonic-gate      0,
1677c478bd9Sstevel@tonic-gate      0,
1687c478bd9Sstevel@tonic-gate      dump_k5beta7_princ,
1697c478bd9Sstevel@tonic-gate      dump_k5beta7_policy,
17054925bf6Swillf      process_k5beta7_record
1717c478bd9Sstevel@tonic-gate };
1727c478bd9Sstevel@tonic-gate dump_version iprop_version = {
1737c478bd9Sstevel@tonic-gate      "Kerberos iprop version",
1747c478bd9Sstevel@tonic-gate      "iprop",
1757c478bd9Sstevel@tonic-gate      0,
1767c478bd9Sstevel@tonic-gate      0,
1777c478bd9Sstevel@tonic-gate      dump_iprop_princ,
1787c478bd9Sstevel@tonic-gate      dump_k5beta7_policy,
17954925bf6Swillf      process_k5beta7_record
1807c478bd9Sstevel@tonic-gate };
1817c478bd9Sstevel@tonic-gate dump_version ov_version = {
1827c478bd9Sstevel@tonic-gate      "OpenV*Secure V1.0",
1837c478bd9Sstevel@tonic-gate      "OpenV*Secure V1.0\t",
1847c478bd9Sstevel@tonic-gate      1,
1857c478bd9Sstevel@tonic-gate      1,
1867c478bd9Sstevel@tonic-gate      dump_ov_princ,
1877c478bd9Sstevel@tonic-gate      dump_k5beta7_policy,
18854925bf6Swillf      process_ov_record
1897c478bd9Sstevel@tonic-gate };
1907c478bd9Sstevel@tonic-gate 
19156a424ccSmp dump_version r1_3_version = {
19256a424ccSmp      "Kerberos version 5 release 1.3",
19356a424ccSmp      "kdb5_util load_dump version 5\n",
19456a424ccSmp      0,
19556a424ccSmp      0,
19656a424ccSmp      dump_k5beta7_princ_withpolicy,
19756a424ccSmp      dump_k5beta7_policy,
19856a424ccSmp      process_k5beta7_record,
19956a424ccSmp };
20056a424ccSmp 
2017c478bd9Sstevel@tonic-gate /* External data */
2027c478bd9Sstevel@tonic-gate extern char		*current_dbname;
2037c478bd9Sstevel@tonic-gate extern krb5_boolean	dbactive;
2047c478bd9Sstevel@tonic-gate extern int		exit_status;
2057c478bd9Sstevel@tonic-gate extern krb5_context	util_context;
2067c478bd9Sstevel@tonic-gate extern kadm5_config_params global_params;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate /* Strings */
2097c478bd9Sstevel@tonic-gate 
21056a424ccSmp #define k5beta_dump_header	"kdb5_edit load_dump version 2.0\n"
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate static const char null_mprinc_name[] = "kdb5_dump@MISSING";
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate /*
2157c478bd9Sstevel@tonic-gate  * We define gettext(s) to be s here, so that xgettext will extract the
2167c478bd9Sstevel@tonic-gate  * strings to the .po file. At the end of the message section we will
2177c478bd9Sstevel@tonic-gate  * undef gettext so that we can use it as a funtion.
2187c478bd9Sstevel@tonic-gate  */
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate #define	gettext(s) s
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate /* Message strings */
2237c478bd9Sstevel@tonic-gate static const char regex_err[] =
2247c478bd9Sstevel@tonic-gate 	gettext("%s: regular expression error - %s\n");
2257c478bd9Sstevel@tonic-gate static const char regex_merr[] =
2267c478bd9Sstevel@tonic-gate 	gettext("%s: regular expression match error - %s\n");
2277c478bd9Sstevel@tonic-gate static const char pname_unp_err[] =
2287c478bd9Sstevel@tonic-gate 	gettext("%s: cannot unparse principal name (%s)\n");
2297c478bd9Sstevel@tonic-gate static const char mname_unp_err[] =
2307c478bd9Sstevel@tonic-gate 	gettext("%s: cannot unparse modifier name (%s)\n");
2317c478bd9Sstevel@tonic-gate static const char nokeys_err[] =
2327c478bd9Sstevel@tonic-gate 	gettext("%s: cannot find any standard key for %s\n");
2337c478bd9Sstevel@tonic-gate static const char sdump_tl_inc_err[] =
2347c478bd9Sstevel@tonic-gate 	gettext("%s: tagged data list inconsistency for %s "
2357c478bd9Sstevel@tonic-gate 		"(counted %d, stored %d)\n");
2367c478bd9Sstevel@tonic-gate static const char stand_fmt_name[] =
2377c478bd9Sstevel@tonic-gate 	gettext("Kerberos version 5");
2387c478bd9Sstevel@tonic-gate static const char old_fmt_name[] =
2397c478bd9Sstevel@tonic-gate 	gettext("Kerberos version 5 old format");
2407c478bd9Sstevel@tonic-gate static const char b6_fmt_name[] =
2417c478bd9Sstevel@tonic-gate 	gettext("Kerberos version 5 beta 6 format");
2427c478bd9Sstevel@tonic-gate static const char ofopen_error[] =
2437c478bd9Sstevel@tonic-gate 	gettext("%s: cannot open %s for writing (%s)\n");
2447c478bd9Sstevel@tonic-gate static const char oflock_error[] =
2457c478bd9Sstevel@tonic-gate 	gettext("%s: cannot lock %s (%s)\n");
2467c478bd9Sstevel@tonic-gate static const char dumprec_err[] =
2477c478bd9Sstevel@tonic-gate 	gettext("%s: error performing %s dump (%s)\n");
2487c478bd9Sstevel@tonic-gate static const char dumphdr_err[] =
2497c478bd9Sstevel@tonic-gate 	gettext("%s: error dumping %s header (%s)\n");
2507c478bd9Sstevel@tonic-gate static const char trash_end_fmt[] =
2517c478bd9Sstevel@tonic-gate 	gettext("%s(%d): ignoring trash at end of line: ");
2527c478bd9Sstevel@tonic-gate static const char read_name_string[] =
2537c478bd9Sstevel@tonic-gate 	gettext("name string");
2547c478bd9Sstevel@tonic-gate static const char read_key_type[] =
2557c478bd9Sstevel@tonic-gate 	gettext("key type");
2567c478bd9Sstevel@tonic-gate static const char read_key_data[] =
2577c478bd9Sstevel@tonic-gate 	gettext("key data");
2587c478bd9Sstevel@tonic-gate static const char read_pr_data1[] =
2597c478bd9Sstevel@tonic-gate 	gettext("first set of principal attributes");
2607c478bd9Sstevel@tonic-gate static const char read_mod_name[] =
2617c478bd9Sstevel@tonic-gate 	gettext("modifier name");
2627c478bd9Sstevel@tonic-gate static const char read_pr_data2[] =
2637c478bd9Sstevel@tonic-gate 	gettext("second set of principal attributes");
2647c478bd9Sstevel@tonic-gate static const char read_salt_data[] =
2657c478bd9Sstevel@tonic-gate 	gettext("salt data");
2667c478bd9Sstevel@tonic-gate static const char read_akey_type[] =
2677c478bd9Sstevel@tonic-gate 	gettext("alternate key type");
2687c478bd9Sstevel@tonic-gate static const char read_akey_data[] =
2697c478bd9Sstevel@tonic-gate 	gettext("alternate key data");
2707c478bd9Sstevel@tonic-gate static const char read_asalt_type[] =
2717c478bd9Sstevel@tonic-gate 	gettext("alternate salt type");
2727c478bd9Sstevel@tonic-gate static const char read_asalt_data[] =
2737c478bd9Sstevel@tonic-gate 	gettext("alternate salt data");
2747c478bd9Sstevel@tonic-gate static const char read_exp_data[] =
2757c478bd9Sstevel@tonic-gate 	gettext("expansion data");
2767c478bd9Sstevel@tonic-gate static const char store_err_fmt[] =
2777c478bd9Sstevel@tonic-gate 	gettext("%s(%d): cannot store %s(%s)\n");
2787c478bd9Sstevel@tonic-gate static const char add_princ_fmt[] =
2797c478bd9Sstevel@tonic-gate 	gettext("%s\n");
2807c478bd9Sstevel@tonic-gate static const char parse_err_fmt[] =
2817c478bd9Sstevel@tonic-gate 	gettext("%s(%d): cannot parse %s (%s)\n");
2827c478bd9Sstevel@tonic-gate static const char read_err_fmt[] =
2837c478bd9Sstevel@tonic-gate 	gettext("%s(%d): cannot read %s\n");
2847c478bd9Sstevel@tonic-gate static const char no_mem_fmt[] =
2857c478bd9Sstevel@tonic-gate 	gettext("%s(%d): no memory for buffers\n");
2867c478bd9Sstevel@tonic-gate static const char rhead_err_fmt[] =
2877c478bd9Sstevel@tonic-gate 	gettext("%s(%d): cannot match size tokens\n");
2887c478bd9Sstevel@tonic-gate static const char err_line_fmt[] =
2897c478bd9Sstevel@tonic-gate 	gettext("%s: error processing line %d of %s\n");
2907c478bd9Sstevel@tonic-gate static const char head_bad_fmt[] =
2917c478bd9Sstevel@tonic-gate 	gettext("%s: dump header bad in %s\n");
2927c478bd9Sstevel@tonic-gate static const char read_bytecnt[] =
2937c478bd9Sstevel@tonic-gate 	gettext("record byte count");
2947c478bd9Sstevel@tonic-gate static const char read_encdata[] =
2957c478bd9Sstevel@tonic-gate 	gettext("encoded data");
2967c478bd9Sstevel@tonic-gate static const char n_name_unp_fmt[] =
2977c478bd9Sstevel@tonic-gate 	gettext("%s(%s): cannot unparse name\n");
2987c478bd9Sstevel@tonic-gate static const char n_dec_cont_fmt[] =
2997c478bd9Sstevel@tonic-gate 	gettext("%s(%s): cannot decode contents\n");
3007c478bd9Sstevel@tonic-gate static const char read_nint_data[] =
3017c478bd9Sstevel@tonic-gate 	gettext("principal static attributes");
3027c478bd9Sstevel@tonic-gate static const char read_tcontents[] =
3037c478bd9Sstevel@tonic-gate 	gettext("tagged data contents");
3047c478bd9Sstevel@tonic-gate static const char read_ttypelen[] =
3057c478bd9Sstevel@tonic-gate 	gettext("tagged data type and length");
3067c478bd9Sstevel@tonic-gate static const char read_kcontents[] =
3077c478bd9Sstevel@tonic-gate 	gettext("key data contents");
3087c478bd9Sstevel@tonic-gate static const char read_ktypelen[] =
3097c478bd9Sstevel@tonic-gate 	gettext("key data type and length");
3107c478bd9Sstevel@tonic-gate static const char read_econtents[] =
3117c478bd9Sstevel@tonic-gate 	gettext("extra data contents");
3127c478bd9Sstevel@tonic-gate static const char k5beta_fmt_name[] =
3137c478bd9Sstevel@tonic-gate 	gettext("Kerberos version 5 old format");
3147c478bd9Sstevel@tonic-gate static const char standard_fmt_name[] =
3157c478bd9Sstevel@tonic-gate 	gettext("Kerberos version 5 format");
3167c478bd9Sstevel@tonic-gate static const char no_name_mem_fmt[] =
3177c478bd9Sstevel@tonic-gate 	gettext("%s: cannot get memory for temporary name\n");
3187c478bd9Sstevel@tonic-gate static const char ctx_err_fmt[] =
3197c478bd9Sstevel@tonic-gate 	gettext("%s: cannot initialize Kerberos context\n");
3207c478bd9Sstevel@tonic-gate static const char stdin_name[] =
3217c478bd9Sstevel@tonic-gate 	gettext("standard input");
322*55fea89dSDan Cross static const char remaster_err_fmt[] =
3237c478bd9Sstevel@tonic-gate 	gettext("while re-encoding keys for principal %s with new master key");
3247c478bd9Sstevel@tonic-gate static const char restfail_fmt[] =
3257c478bd9Sstevel@tonic-gate 	gettext("%s: %s restore failed\n");
3267c478bd9Sstevel@tonic-gate static const char close_err_fmt[] =
3277c478bd9Sstevel@tonic-gate 	gettext("%s: cannot close database (%s)\n");
3287c478bd9Sstevel@tonic-gate static const char dbinit_err_fmt[] =
3297c478bd9Sstevel@tonic-gate 	gettext("%s: cannot initialize database (%s)\n");
3307c478bd9Sstevel@tonic-gate static const char dblock_err_fmt[] =
3317c478bd9Sstevel@tonic-gate 	gettext("%s: cannot initialize database lock (%s)\n");
3327c478bd9Sstevel@tonic-gate static const char dbname_err_fmt[] =
3337c478bd9Sstevel@tonic-gate 	gettext("%s: cannot set database name to %s (%s)\n");
3347c478bd9Sstevel@tonic-gate static const char dbdelerr_fmt[] =
3357c478bd9Sstevel@tonic-gate 	gettext("%s: cannot delete bad database %s (%s)\n");
3367c478bd9Sstevel@tonic-gate static const char dbunlockerr_fmt[] =
3377c478bd9Sstevel@tonic-gate 	gettext("%s: cannot unlock database %s (%s)\n");
3387c478bd9Sstevel@tonic-gate static const char dbrenerr_fmt[] =
3397c478bd9Sstevel@tonic-gate 	gettext("%s: cannot rename database %s to %s (%s)\n");
3407c478bd9Sstevel@tonic-gate static const char dbcreaterr_fmt[] =
3417c478bd9Sstevel@tonic-gate 	gettext("%s: cannot create database %s (%s)\n");
3427c478bd9Sstevel@tonic-gate static const char dfile_err_fmt[] =
3437c478bd9Sstevel@tonic-gate 	gettext("%s: cannot open %s (%s)\n");
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate /*
3467c478bd9Sstevel@tonic-gate  * We now return you to your regularly scheduled program.
3477c478bd9Sstevel@tonic-gate  */
3487c478bd9Sstevel@tonic-gate #undef gettext
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate static const char oldoption[] = "-old";
3517c478bd9Sstevel@tonic-gate static const char b6option[] = "-b6";
35256a424ccSmp static const char b7option[] = "-b7";
3537c478bd9Sstevel@tonic-gate static const char ipropoption[] = "-i";
3547c478bd9Sstevel@tonic-gate static const char verboseoption[] = "-verbose";
3557c478bd9Sstevel@tonic-gate static const char updateoption[] = "-update";
3567c478bd9Sstevel@tonic-gate static const char hashoption[] = "-hash";
3577c478bd9Sstevel@tonic-gate static const char ovoption[] = "-ov";
3587c478bd9Sstevel@tonic-gate static const char dump_tmptrail[] = "~";
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate /*
3617c478bd9Sstevel@tonic-gate  * Re-encrypt the key_data with the new master key...
3627c478bd9Sstevel@tonic-gate  */
master_key_convert(context,db_entry)36356a424ccSmp static krb5_error_code master_key_convert(context, db_entry)
3647c478bd9Sstevel@tonic-gate     krb5_context	  context;
3657c478bd9Sstevel@tonic-gate     krb5_db_entry	* db_entry;
3667c478bd9Sstevel@tonic-gate {
3677c478bd9Sstevel@tonic-gate     krb5_error_code	retval;
3687c478bd9Sstevel@tonic-gate     krb5_keyblock 	v5plainkey, *key_ptr;
3697c478bd9Sstevel@tonic-gate     krb5_keysalt 	keysalt;
37056a424ccSmp     int	      i, j;
3717c478bd9Sstevel@tonic-gate     krb5_key_data	new_key_data, *key_data;
3727c478bd9Sstevel@tonic-gate     krb5_boolean	is_mkey;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate     is_mkey = krb5_principal_compare(context, master_princ, db_entry->princ);
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate     if (is_mkey && db_entry->n_key_data != 1)
3777c478bd9Sstevel@tonic-gate 	    fprintf(stderr,
3787c478bd9Sstevel@tonic-gate 		    gettext(
3797c478bd9Sstevel@tonic-gate 		      "Master key db entry has %d keys, expecting only 1!\n"),
3807c478bd9Sstevel@tonic-gate 		    db_entry->n_key_data);
3817c478bd9Sstevel@tonic-gate     for (i=0; i < db_entry->n_key_data; i++) {
3827c478bd9Sstevel@tonic-gate 	key_data = &db_entry->key_data[i];
3837c478bd9Sstevel@tonic-gate 	if (key_data->key_data_length == 0)
3847c478bd9Sstevel@tonic-gate 	    continue;
3857c478bd9Sstevel@tonic-gate 	retval = krb5_dbekd_decrypt_key_data(context, &master_key,
3867c478bd9Sstevel@tonic-gate 					     key_data, &v5plainkey,
3877c478bd9Sstevel@tonic-gate 					     &keysalt);
3887c478bd9Sstevel@tonic-gate 	if (retval)
3897c478bd9Sstevel@tonic-gate 		return retval;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	memset(&new_key_data, 0, sizeof(new_key_data));
3927c478bd9Sstevel@tonic-gate 	key_ptr = is_mkey ? &new_master_key : &v5plainkey;
3937c478bd9Sstevel@tonic-gate 	retval = krb5_dbekd_encrypt_key_data(context, &new_master_key,
3947c478bd9Sstevel@tonic-gate 					     key_ptr, &keysalt,
3957c478bd9Sstevel@tonic-gate 					     key_data->key_data_kvno,
3967c478bd9Sstevel@tonic-gate 					     &new_key_data);
3977c478bd9Sstevel@tonic-gate 	if (retval)
3987c478bd9Sstevel@tonic-gate 		return retval;
3997c478bd9Sstevel@tonic-gate 	krb5_free_keyblock_contents(context, &v5plainkey);
40056a424ccSmp 	for (j = 0; j < key_data->key_data_ver; j++) {
40156a424ccSmp 	    if (key_data->key_data_length[j]) {
40256a424ccSmp 		free(key_data->key_data_contents[j]);
40356a424ccSmp 	    }
40456a424ccSmp 	}
4057c478bd9Sstevel@tonic-gate 	*key_data = new_key_data;
4067c478bd9Sstevel@tonic-gate     }
4077c478bd9Sstevel@tonic-gate     return 0;
4087c478bd9Sstevel@tonic-gate }
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate /*
4117c478bd9Sstevel@tonic-gate  * Update the "ok" file.
4127c478bd9Sstevel@tonic-gate  */
update_ok_file(file_name)41356a424ccSmp void update_ok_file (file_name)
4147c478bd9Sstevel@tonic-gate      char *file_name;
4157c478bd9Sstevel@tonic-gate {
4167c478bd9Sstevel@tonic-gate 	/* handle slave locking/failure stuff */
4177c478bd9Sstevel@tonic-gate 	char *file_ok;
4187c478bd9Sstevel@tonic-gate 	int fd;
4197c478bd9Sstevel@tonic-gate 	static char ok[]=".dump_ok";
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	if ((file_ok = (char *)malloc(strlen(file_name) + strlen(ok) + 1))
4227c478bd9Sstevel@tonic-gate 	    == NULL) {
4237c478bd9Sstevel@tonic-gate 		com_err(progname, ENOMEM,
4247c478bd9Sstevel@tonic-gate 		    gettext("while allocating filename "
4257c478bd9Sstevel@tonic-gate 			"for update_ok_file"));
4267c478bd9Sstevel@tonic-gate 		exit_status++;
4277c478bd9Sstevel@tonic-gate 		return;
4287c478bd9Sstevel@tonic-gate 	}
4297c478bd9Sstevel@tonic-gate 	strcpy(file_ok, file_name);
4307c478bd9Sstevel@tonic-gate 	strcat(file_ok, ok);
4317c478bd9Sstevel@tonic-gate 	if ((fd = open(file_ok, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0) {
4327c478bd9Sstevel@tonic-gate 		com_err(progname, errno,
4337c478bd9Sstevel@tonic-gate 		    gettext("while creating 'ok' file, '%s'"),
4347c478bd9Sstevel@tonic-gate 			file_ok);
4357c478bd9Sstevel@tonic-gate 		exit_status++;
4367c478bd9Sstevel@tonic-gate 		free(file_ok);
4377c478bd9Sstevel@tonic-gate 		return;
4387c478bd9Sstevel@tonic-gate 	}
4397c478bd9Sstevel@tonic-gate 	if (write(fd, "", 1) != 1) {
4407c478bd9Sstevel@tonic-gate 		com_err(progname, errno,
4417c478bd9Sstevel@tonic-gate 		    gettext("while writing to 'ok' file, '%s'"),
4427c478bd9Sstevel@tonic-gate 		    file_ok);
4437c478bd9Sstevel@tonic-gate 	     exit_status++;
4447c478bd9Sstevel@tonic-gate 	     free(file_ok);
4457c478bd9Sstevel@tonic-gate 	     return;
4467c478bd9Sstevel@tonic-gate 	}
44756a424ccSmp 
4487c478bd9Sstevel@tonic-gate 	free(file_ok);
4497c478bd9Sstevel@tonic-gate 	close(fd);
45056a424ccSmp 	return;
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate /*
4547c478bd9Sstevel@tonic-gate  * name_matches()	- See if a principal name matches a regular expression
4557c478bd9Sstevel@tonic-gate  *			  or string.
4567c478bd9Sstevel@tonic-gate  */
4577c478bd9Sstevel@tonic-gate static int
name_matches(name,arglist)4587c478bd9Sstevel@tonic-gate name_matches(name, arglist)
4597c478bd9Sstevel@tonic-gate     char		*name;
4607c478bd9Sstevel@tonic-gate     struct dump_args	*arglist;
4617c478bd9Sstevel@tonic-gate {
4627c478bd9Sstevel@tonic-gate #if	HAVE_REGCOMP
4637c478bd9Sstevel@tonic-gate     regex_t	match_exp;
4647c478bd9Sstevel@tonic-gate     regmatch_t	match_match;
4657c478bd9Sstevel@tonic-gate     int		match_error;
4667c478bd9Sstevel@tonic-gate     char	match_errmsg[BUFSIZ];
4677c478bd9Sstevel@tonic-gate     size_t	errmsg_size;
4687c478bd9Sstevel@tonic-gate #elif	HAVE_REGEXP_H
4697c478bd9Sstevel@tonic-gate     char	regexp_buffer[RE_BUF_SIZE];
4707c478bd9Sstevel@tonic-gate #elif	HAVE_RE_COMP
4717c478bd9Sstevel@tonic-gate     extern char	*re_comp();
4727c478bd9Sstevel@tonic-gate     char	*re_result;
4737c478bd9Sstevel@tonic-gate #endif	/* HAVE_RE_COMP */
4747c478bd9Sstevel@tonic-gate     int		i, match;
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate     /*
47756a424ccSmp      * Plow, brute force, through the list of names/regular expressions.
4787c478bd9Sstevel@tonic-gate      */
4797c478bd9Sstevel@tonic-gate     match = (arglist->nnames) ? 0 : 1;
4807c478bd9Sstevel@tonic-gate     for (i=0; i<arglist->nnames; i++) {
4817c478bd9Sstevel@tonic-gate #if	HAVE_REGCOMP
4827c478bd9Sstevel@tonic-gate 	/*
4837c478bd9Sstevel@tonic-gate 	 * Compile the regular expression.
4847c478bd9Sstevel@tonic-gate 	 */
48556a424ccSmp 	match_error = regcomp(&match_exp, arglist->names[i], REG_EXTENDED);
48656a424ccSmp 	if (match_error) {
4877c478bd9Sstevel@tonic-gate 	    errmsg_size = regerror(match_error,
4887c478bd9Sstevel@tonic-gate 				   &match_exp,
4897c478bd9Sstevel@tonic-gate 				   match_errmsg,
4907c478bd9Sstevel@tonic-gate 				   sizeof(match_errmsg));
4917c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(regex_err),
4927c478bd9Sstevel@tonic-gate 			    arglist->programname, match_errmsg);
4937c478bd9Sstevel@tonic-gate 	    break;
4947c478bd9Sstevel@tonic-gate 	}
4957c478bd9Sstevel@tonic-gate 	/*
4967c478bd9Sstevel@tonic-gate 	 * See if we have a match.
4977c478bd9Sstevel@tonic-gate 	 */
49856a424ccSmp 	match_error = regexec(&match_exp, name, 1, &match_match, 0);
49956a424ccSmp 	if (match_error) {
5007c478bd9Sstevel@tonic-gate 	    if (match_error != REG_NOMATCH) {
5017c478bd9Sstevel@tonic-gate 		errmsg_size = regerror(match_error,
5027c478bd9Sstevel@tonic-gate 				       &match_exp,
5037c478bd9Sstevel@tonic-gate 				       match_errmsg,
5047c478bd9Sstevel@tonic-gate 				       sizeof(match_errmsg));
5057c478bd9Sstevel@tonic-gate 				fprintf(stderr, gettext(regex_merr),
5067c478bd9Sstevel@tonic-gate 			arglist->programname, match_errmsg);
5077c478bd9Sstevel@tonic-gate 		break;
5087c478bd9Sstevel@tonic-gate 	    }
50956a424ccSmp 	}
51056a424ccSmp 	else {
5117c478bd9Sstevel@tonic-gate 	    /*
5127c478bd9Sstevel@tonic-gate 	     * We have a match.  See if it matches the whole
5137c478bd9Sstevel@tonic-gate 	     * name.
5147c478bd9Sstevel@tonic-gate 	     */
5157c478bd9Sstevel@tonic-gate 	    if ((match_match.rm_so == 0) &&
5167c478bd9Sstevel@tonic-gate 		(match_match.rm_eo == strlen(name)))
5177c478bd9Sstevel@tonic-gate 		match = 1;
5187c478bd9Sstevel@tonic-gate 	}
5197c478bd9Sstevel@tonic-gate 	regfree(&match_exp);
5207c478bd9Sstevel@tonic-gate #elif	HAVE_REGEXP_H
5217c478bd9Sstevel@tonic-gate 	/*
5227c478bd9Sstevel@tonic-gate 	 * Compile the regular expression.
5237c478bd9Sstevel@tonic-gate 	 */
5247c478bd9Sstevel@tonic-gate 	compile(arglist->names[i],
525*55fea89dSDan Cross 		regexp_buffer,
5267c478bd9Sstevel@tonic-gate 		&regexp_buffer[RE_BUF_SIZE],
5277c478bd9Sstevel@tonic-gate 		'\0');
5287c478bd9Sstevel@tonic-gate 	if (step(name, regexp_buffer)) {
5297c478bd9Sstevel@tonic-gate 	    if ((loc1 == name) &&
5307c478bd9Sstevel@tonic-gate 		(loc2 == &name[strlen(name)]))
5317c478bd9Sstevel@tonic-gate 		match = 1;
5327c478bd9Sstevel@tonic-gate 	}
5337c478bd9Sstevel@tonic-gate #elif	HAVE_RE_COMP
5347c478bd9Sstevel@tonic-gate 	/*
5357c478bd9Sstevel@tonic-gate 	 * Compile the regular expression.
5367c478bd9Sstevel@tonic-gate 	 */
5377c478bd9Sstevel@tonic-gate 	if (re_result = re_comp(arglist->names[i])) {
53856a424ccSmp 	    fprintf(stderr, gettext(regex_err), arglist->programname, re_result);
5397c478bd9Sstevel@tonic-gate 	    break;
5407c478bd9Sstevel@tonic-gate 	}
5417c478bd9Sstevel@tonic-gate 	if (re_exec(name))
5427c478bd9Sstevel@tonic-gate 	    match = 1;
5437c478bd9Sstevel@tonic-gate #else	/* HAVE_RE_COMP */
5447c478bd9Sstevel@tonic-gate 	/*
54556a424ccSmp 	 * If no regular expression support, then just compare the strings.
5467c478bd9Sstevel@tonic-gate 	 */
54756a424ccSmp 	if (!strcmp(arglist->names[i], name))
5487c478bd9Sstevel@tonic-gate 	    match = 1;
5497c478bd9Sstevel@tonic-gate #endif	/* HAVE_REGCOMP */
5507c478bd9Sstevel@tonic-gate 	if (match)
5517c478bd9Sstevel@tonic-gate 	    break;
5527c478bd9Sstevel@tonic-gate     }
5537c478bd9Sstevel@tonic-gate     return(match);
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate static krb5_error_code
find_enctype(dbentp,enctype,salttype,kentp)5577c478bd9Sstevel@tonic-gate find_enctype(dbentp, enctype, salttype, kentp)
5587c478bd9Sstevel@tonic-gate     krb5_db_entry	*dbentp;
5597c478bd9Sstevel@tonic-gate     krb5_enctype	enctype;
5607c478bd9Sstevel@tonic-gate     krb5_int32		salttype;
5617c478bd9Sstevel@tonic-gate     krb5_key_data	**kentp;
5627c478bd9Sstevel@tonic-gate {
5637c478bd9Sstevel@tonic-gate     int			i;
5647c478bd9Sstevel@tonic-gate     int			maxkvno;
5657c478bd9Sstevel@tonic-gate     krb5_key_data	*datap;
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate     maxkvno = -1;
5687c478bd9Sstevel@tonic-gate     datap = (krb5_key_data *) NULL;
5697c478bd9Sstevel@tonic-gate     for (i=0; i<dbentp->n_key_data; i++) {
5707c478bd9Sstevel@tonic-gate 	if (( (krb5_enctype)dbentp->key_data[i].key_data_type[0] == enctype) &&
5717c478bd9Sstevel@tonic-gate 	    ((dbentp->key_data[i].key_data_type[1] == salttype) ||
5727c478bd9Sstevel@tonic-gate 	     (salttype < 0))) {
5737c478bd9Sstevel@tonic-gate 	    maxkvno = dbentp->key_data[i].key_data_kvno;
5747c478bd9Sstevel@tonic-gate 	    datap = &dbentp->key_data[i];
5757c478bd9Sstevel@tonic-gate 	}
5767c478bd9Sstevel@tonic-gate     }
5777c478bd9Sstevel@tonic-gate     if (maxkvno >= 0) {
5787c478bd9Sstevel@tonic-gate 	*kentp = datap;
5797c478bd9Sstevel@tonic-gate 	return(0);
5807c478bd9Sstevel@tonic-gate     }
581*55fea89dSDan Cross     return(ENOENT);
5827c478bd9Sstevel@tonic-gate }
5837c478bd9Sstevel@tonic-gate 
58456a424ccSmp #if 0
5857c478bd9Sstevel@tonic-gate /*
5867c478bd9Sstevel@tonic-gate  * dump_k5beta_header()	- Make a dump header that is recognizable by Kerberos
5877c478bd9Sstevel@tonic-gate  *			  Version 5 Beta 5 and previous releases.
5887c478bd9Sstevel@tonic-gate  */
5897c478bd9Sstevel@tonic-gate static krb5_error_code
5907c478bd9Sstevel@tonic-gate dump_k5beta_header(arglist)
5917c478bd9Sstevel@tonic-gate     struct dump_args *arglist;
5927c478bd9Sstevel@tonic-gate {
5937c478bd9Sstevel@tonic-gate     /* The old header consists of the leading string */
5947c478bd9Sstevel@tonic-gate     fprintf(arglist->ofile, k5beta_dump_header);
5957c478bd9Sstevel@tonic-gate     return(0);
5967c478bd9Sstevel@tonic-gate }
59756a424ccSmp #endif
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate /*
6007c478bd9Sstevel@tonic-gate  * dump_k5beta_iterator()	- Dump an entry in a format that is usable
6017c478bd9Sstevel@tonic-gate  *				  by Kerberos Version 5 Beta 5 and previous
6027c478bd9Sstevel@tonic-gate  *				  releases.
6037c478bd9Sstevel@tonic-gate  */
6047c478bd9Sstevel@tonic-gate static krb5_error_code
dump_k5beta_iterator(ptr,entry)6057c478bd9Sstevel@tonic-gate dump_k5beta_iterator(ptr, entry)
6067c478bd9Sstevel@tonic-gate     krb5_pointer	ptr;
6077c478bd9Sstevel@tonic-gate     krb5_db_entry	*entry;
6087c478bd9Sstevel@tonic-gate {
6097c478bd9Sstevel@tonic-gate     krb5_error_code	retval;
6107c478bd9Sstevel@tonic-gate     struct dump_args	*arg;
6117c478bd9Sstevel@tonic-gate     char		*name, *mod_name;
6127c478bd9Sstevel@tonic-gate     krb5_principal	mod_princ;
6137c478bd9Sstevel@tonic-gate     krb5_key_data	*pkey, *akey, nullkey;
6147c478bd9Sstevel@tonic-gate     krb5_timestamp	mod_date, last_pwd_change;
6157c478bd9Sstevel@tonic-gate     int			i;
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate     /* Initialize */
6187c478bd9Sstevel@tonic-gate     arg = (struct dump_args *) ptr;
6197c478bd9Sstevel@tonic-gate     name = (char *) NULL;
6207c478bd9Sstevel@tonic-gate     mod_name = (char *) NULL;
6217c478bd9Sstevel@tonic-gate     memset(&nullkey, 0, sizeof(nullkey));
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate     /*
6247c478bd9Sstevel@tonic-gate      * Flatten the principal name.
6257c478bd9Sstevel@tonic-gate      */
6267c478bd9Sstevel@tonic-gate     if ((retval = krb5_unparse_name(arg->kcontext,
6277c478bd9Sstevel@tonic-gate 				    entry->princ,
6287c478bd9Sstevel@tonic-gate 				    &name))) {
6297c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(pname_unp_err),
6307c478bd9Sstevel@tonic-gate 		arg->programname, error_message(retval));
6317c478bd9Sstevel@tonic-gate 	return(retval);
6327c478bd9Sstevel@tonic-gate     }
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate     /*
6357c478bd9Sstevel@tonic-gate      * Re-encode the keys in the new master key, if necessary.
6367c478bd9Sstevel@tonic-gate      */
6377c478bd9Sstevel@tonic-gate     if (mkey_convert) {
6387c478bd9Sstevel@tonic-gate 	retval = master_key_convert(arg->kcontext, entry);
6397c478bd9Sstevel@tonic-gate 	if (retval) {
6407c478bd9Sstevel@tonic-gate 	    com_err(arg->programname, retval, remaster_err_fmt, name);
6417c478bd9Sstevel@tonic-gate 	    return retval;
6427c478bd9Sstevel@tonic-gate 	}
6437c478bd9Sstevel@tonic-gate     }
644*55fea89dSDan Cross 
6457c478bd9Sstevel@tonic-gate     /*
6467c478bd9Sstevel@tonic-gate      * If we don't have any match strings, or if our name matches, then
6477c478bd9Sstevel@tonic-gate      * proceed with the dump, otherwise, just forget about it.
6487c478bd9Sstevel@tonic-gate      */
6497c478bd9Sstevel@tonic-gate     if (!arg->nnames || name_matches(name, arg)) {
6507c478bd9Sstevel@tonic-gate 	/*
6517c478bd9Sstevel@tonic-gate 	 * Deserialize the modifier record.
6527c478bd9Sstevel@tonic-gate 	 */
6537c478bd9Sstevel@tonic-gate 	mod_name = (char *) NULL;
6547c478bd9Sstevel@tonic-gate 	mod_princ = NULL;
6557c478bd9Sstevel@tonic-gate 	last_pwd_change = mod_date = 0;
6567c478bd9Sstevel@tonic-gate 	pkey = akey = (krb5_key_data *) NULL;
6577c478bd9Sstevel@tonic-gate 	if (!(retval = krb5_dbe_lookup_mod_princ_data(arg->kcontext,
6587c478bd9Sstevel@tonic-gate 						      entry,
6597c478bd9Sstevel@tonic-gate 						      &mod_date,
6607c478bd9Sstevel@tonic-gate 						      &mod_princ))) {
6617c478bd9Sstevel@tonic-gate 	    if (mod_princ) {
6627c478bd9Sstevel@tonic-gate 		/*
6637c478bd9Sstevel@tonic-gate 		 * Flatten the modifier name.
6647c478bd9Sstevel@tonic-gate 		 */
6657c478bd9Sstevel@tonic-gate 		if ((retval = krb5_unparse_name(arg->kcontext,
6667c478bd9Sstevel@tonic-gate 						mod_princ,
6677c478bd9Sstevel@tonic-gate 						&mod_name)))
6687c478bd9Sstevel@tonic-gate 					fprintf(stderr, gettext(mname_unp_err),
6697c478bd9Sstevel@tonic-gate 					    arg->programname,
6707c478bd9Sstevel@tonic-gate 			    error_message(retval));
6717c478bd9Sstevel@tonic-gate 		krb5_free_principal(arg->kcontext, mod_princ);
6727c478bd9Sstevel@tonic-gate 	    }
6737c478bd9Sstevel@tonic-gate 	}
6747c478bd9Sstevel@tonic-gate 	if (!mod_name)
6757c478bd9Sstevel@tonic-gate 	    mod_name = strdup(null_mprinc_name);
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	/*
67856a424ccSmp 	 * Find the last password change record and set it straight.
6797c478bd9Sstevel@tonic-gate 	 */
6807c478bd9Sstevel@tonic-gate 	if ((retval =
6817c478bd9Sstevel@tonic-gate 	     krb5_dbe_lookup_last_pwd_change(arg->kcontext, entry,
68256a424ccSmp 					     &last_pwd_change))) {
6837c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(nokeys_err),
6847c478bd9Sstevel@tonic-gate 			    arg->programname, name);
6857c478bd9Sstevel@tonic-gate 	    krb5_xfree(mod_name);
6867c478bd9Sstevel@tonic-gate 	    krb5_xfree(name);
6877c478bd9Sstevel@tonic-gate 	    return(retval);
6887c478bd9Sstevel@tonic-gate 	}
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	/*
6917c478bd9Sstevel@tonic-gate 	 * Find the 'primary' key and the 'alternate' key.
6927c478bd9Sstevel@tonic-gate 	 */
6937c478bd9Sstevel@tonic-gate 	if ((retval = find_enctype(entry,
6947c478bd9Sstevel@tonic-gate 				   ENCTYPE_DES_CBC_CRC,
6957c478bd9Sstevel@tonic-gate 				   KRB5_KDB_SALTTYPE_NORMAL,
6967c478bd9Sstevel@tonic-gate 				   &pkey)) &&
6977c478bd9Sstevel@tonic-gate 	    (retval = find_enctype(entry,
6987c478bd9Sstevel@tonic-gate 				   ENCTYPE_DES_CBC_CRC,
6997c478bd9Sstevel@tonic-gate 				   KRB5_KDB_SALTTYPE_V4,
7007c478bd9Sstevel@tonic-gate 				   &akey))) {
7017c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(nokeys_err),
7027c478bd9Sstevel@tonic-gate 			    arg->programname, name);
7037c478bd9Sstevel@tonic-gate 	    krb5_xfree(mod_name);
7047c478bd9Sstevel@tonic-gate 	    krb5_xfree(name);
7057c478bd9Sstevel@tonic-gate 	    return(retval);
7067c478bd9Sstevel@tonic-gate 	}
70756a424ccSmp 
70856a424ccSmp 	/* If we only have one type, then ship it out as the primary. */
7097c478bd9Sstevel@tonic-gate 	if (!pkey && akey) {
7107c478bd9Sstevel@tonic-gate 	    pkey = akey;
7117c478bd9Sstevel@tonic-gate 	    akey = &nullkey;
71256a424ccSmp 	}
71356a424ccSmp 	else {
7147c478bd9Sstevel@tonic-gate 	    if (!akey)
7157c478bd9Sstevel@tonic-gate 		akey = &nullkey;
7167c478bd9Sstevel@tonic-gate 	}
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	/*
71956a424ccSmp 	 * First put out strings representing the length of the variable
72056a424ccSmp 	 * length data in this record, then the name and the primary key type.
7217c478bd9Sstevel@tonic-gate 	 */
72256a424ccSmp 	fprintf(arg->ofile, "%d\t%d\t%d\t%d\t%d\t%d\t%s\t%d\t", strlen(name),
7237c478bd9Sstevel@tonic-gate 		strlen(mod_name),
7247c478bd9Sstevel@tonic-gate 		(krb5_int32) pkey->key_data_length[0],
7257c478bd9Sstevel@tonic-gate 		(krb5_int32) akey->key_data_length[0],
7267c478bd9Sstevel@tonic-gate 		(krb5_int32) pkey->key_data_length[1],
7277c478bd9Sstevel@tonic-gate 		(krb5_int32) akey->key_data_length[1],
7287c478bd9Sstevel@tonic-gate 		name,
7297c478bd9Sstevel@tonic-gate 		(krb5_int32) pkey->key_data_type[0]);
7307c478bd9Sstevel@tonic-gate 	for (i=0; i<pkey->key_data_length[0]; i++) {
73156a424ccSmp 	    fprintf(arg->ofile, "%02x", pkey->key_data_contents[0][i]);
7327c478bd9Sstevel@tonic-gate 	}
7337c478bd9Sstevel@tonic-gate 	/*
73456a424ccSmp 	 * Second, print out strings representing the standard integer
73556a424ccSmp 	 * data in this record.
7367c478bd9Sstevel@tonic-gate 	 */
7377c478bd9Sstevel@tonic-gate 	fprintf(arg->ofile,
73856a424ccSmp 		"\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",
7397c478bd9Sstevel@tonic-gate 		(krb5_int32) pkey->key_data_kvno,
7407c478bd9Sstevel@tonic-gate 		entry->max_life, entry->max_renewable_life,
74156a424ccSmp 		1 /* Fake mkvno */, entry->expiration, entry->pw_expiration,
74256a424ccSmp 		last_pwd_change, entry->last_success, entry->last_failed,
7437c478bd9Sstevel@tonic-gate 		entry->fail_auth_count, mod_name, mod_date,
7447c478bd9Sstevel@tonic-gate 		entry->attributes, pkey->key_data_type[1]);
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	/* Pound out the salt data, if present. */
7477c478bd9Sstevel@tonic-gate 	for (i=0; i<pkey->key_data_length[1]; i++) {
74856a424ccSmp 	    fprintf(arg->ofile, "%02x", pkey->key_data_contents[1][i]);
7497c478bd9Sstevel@tonic-gate 	}
7507c478bd9Sstevel@tonic-gate 	/* Pound out the alternate key type and contents */
7517c478bd9Sstevel@tonic-gate 	fprintf(arg->ofile, "\t%u\t", akey->key_data_type[0]);
7527c478bd9Sstevel@tonic-gate 	for (i=0; i<akey->key_data_length[0]; i++) {
75356a424ccSmp 	    fprintf(arg->ofile, "%02x", akey->key_data_contents[0][i]);
7547c478bd9Sstevel@tonic-gate 	}
7557c478bd9Sstevel@tonic-gate 	/* Pound out the alternate salt type and contents */
7567c478bd9Sstevel@tonic-gate 	fprintf(arg->ofile, "\t%u\t", akey->key_data_type[1]);
7577c478bd9Sstevel@tonic-gate 	for (i=0; i<akey->key_data_length[1]; i++) {
75856a424ccSmp 	    fprintf(arg->ofile, "%02x", akey->key_data_contents[1][i]);
7597c478bd9Sstevel@tonic-gate 	}
7607c478bd9Sstevel@tonic-gate 	/* Pound out the expansion data. (is null) */
7617c478bd9Sstevel@tonic-gate 	for (i=0; i < 8; i++) {
7627c478bd9Sstevel@tonic-gate 	    fprintf(arg->ofile, "\t%u", 0);
7637c478bd9Sstevel@tonic-gate 	}
7647c478bd9Sstevel@tonic-gate 	fprintf(arg->ofile, ";\n");
7657c478bd9Sstevel@tonic-gate 	/* If we're blabbing, do it */
7667c478bd9Sstevel@tonic-gate 	if (arg->verbose)
7677c478bd9Sstevel@tonic-gate 	    fprintf(stderr, "%s\n", name);
7687c478bd9Sstevel@tonic-gate 	krb5_xfree(mod_name);
7697c478bd9Sstevel@tonic-gate     }
7707c478bd9Sstevel@tonic-gate     krb5_xfree(name);
7717c478bd9Sstevel@tonic-gate     return(0);
7727c478bd9Sstevel@tonic-gate }
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate /*
7757c478bd9Sstevel@tonic-gate  * dump_k5beta6_iterator()	- Output a dump record in krb5b6 format.
7767c478bd9Sstevel@tonic-gate  */
7777c478bd9Sstevel@tonic-gate static krb5_error_code
dump_k5beta6_iterator(ptr,entry)7787c478bd9Sstevel@tonic-gate dump_k5beta6_iterator(ptr, entry)
7797c478bd9Sstevel@tonic-gate     krb5_pointer	ptr;
7807c478bd9Sstevel@tonic-gate     krb5_db_entry	*entry;
78156a424ccSmp {
78256a424ccSmp     return dump_k5beta6_iterator_ext(ptr, entry, 0);
78356a424ccSmp }
78456a424ccSmp 
78556a424ccSmp static krb5_error_code
dump_k5beta6_iterator_ext(ptr,entry,kadm)78656a424ccSmp dump_k5beta6_iterator_ext(ptr, entry, kadm)
78756a424ccSmp     krb5_pointer	ptr;
78856a424ccSmp     krb5_db_entry	*entry;
78956a424ccSmp     int			kadm;
7907c478bd9Sstevel@tonic-gate {
7917c478bd9Sstevel@tonic-gate     krb5_error_code	retval;
7927c478bd9Sstevel@tonic-gate     struct dump_args	*arg;
7937c478bd9Sstevel@tonic-gate     char		*name;
7947c478bd9Sstevel@tonic-gate     krb5_tl_data	*tlp;
7957c478bd9Sstevel@tonic-gate     krb5_key_data	*kdata;
7967c478bd9Sstevel@tonic-gate     int			counter, skip, i, j;
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate     /* Initialize */
7997c478bd9Sstevel@tonic-gate     arg = (struct dump_args *) ptr;
8007c478bd9Sstevel@tonic-gate     name = (char *) NULL;
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate     /*
8037c478bd9Sstevel@tonic-gate      * Flatten the principal name.
8047c478bd9Sstevel@tonic-gate      */
8057c478bd9Sstevel@tonic-gate     if ((retval = krb5_unparse_name(arg->kcontext,
8067c478bd9Sstevel@tonic-gate 				    entry->princ,
8077c478bd9Sstevel@tonic-gate 				    &name))) {
8087c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(pname_unp_err),
8097c478bd9Sstevel@tonic-gate 		arg->programname, error_message(retval));
8107c478bd9Sstevel@tonic-gate 	return(retval);
8117c478bd9Sstevel@tonic-gate     }
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate     /*
8147c478bd9Sstevel@tonic-gate      * Re-encode the keys in the new master key, if necessary.
8157c478bd9Sstevel@tonic-gate      */
8167c478bd9Sstevel@tonic-gate     if (mkey_convert) {
8177c478bd9Sstevel@tonic-gate 	retval = master_key_convert(arg->kcontext, entry);
8187c478bd9Sstevel@tonic-gate 	if (retval) {
8197c478bd9Sstevel@tonic-gate 	    com_err(arg->programname, retval, remaster_err_fmt, name);
8207c478bd9Sstevel@tonic-gate 	    return retval;
8217c478bd9Sstevel@tonic-gate 	}
8227c478bd9Sstevel@tonic-gate     }
823*55fea89dSDan Cross 
8247c478bd9Sstevel@tonic-gate     /*
8257c478bd9Sstevel@tonic-gate      * If we don't have any match strings, or if our name matches, then
8267c478bd9Sstevel@tonic-gate      * proceed with the dump, otherwise, just forget about it.
8277c478bd9Sstevel@tonic-gate      */
8287c478bd9Sstevel@tonic-gate     if (!arg->nnames || name_matches(name, arg)) {
8297c478bd9Sstevel@tonic-gate 	/*
83056a424ccSmp 	 * We'd like to just blast out the contents as they would appear in
83156a424ccSmp 	 * the database so that we can just suck it back in, but it doesn't
83256a424ccSmp 	 * lend itself to easy editing.
8337c478bd9Sstevel@tonic-gate 	 */
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	/*
83656a424ccSmp 	 * The dump format is as follows:
83756a424ccSmp 	 *	len strlen(name) n_tl_data n_key_data e_length
83856a424ccSmp 	 *	name
83956a424ccSmp 	 *	attributes max_life max_renewable_life expiration
84056a424ccSmp 	 *	pw_expiration last_success last_failed fail_auth_count
84156a424ccSmp 	 *	n_tl_data*[type length <contents>]
84256a424ccSmp 	 *	n_key_data*[ver kvno ver*(type length <contents>)]
84356a424ccSmp 	 *	<e_data>
84456a424ccSmp 	 * Fields which are not encapsulated by angle-brackets are to appear
84556a424ccSmp 	 * verbatim.  A bracketed field's absence is indicated by a -1 in its
84656a424ccSmp 	 * place
8477c478bd9Sstevel@tonic-gate 	 */
8487c478bd9Sstevel@tonic-gate 
84956a424ccSmp 	/*
8507c478bd9Sstevel@tonic-gate 	 * Make sure that the tagged list is reasonably correct.
8517c478bd9Sstevel@tonic-gate 	 */
8527c478bd9Sstevel@tonic-gate 	counter = skip = 0;
8537c478bd9Sstevel@tonic-gate 	for (tlp = entry->tl_data; tlp; tlp = tlp->tl_data_next) {
85456a424ccSmp 	     /*
85556a424ccSmp 	      * don't dump tl data types we know aren't understood by
85656a424ccSmp 	      * earlier revisions [krb5-admin/89]
85756a424ccSmp 	      */
85856a424ccSmp 	     switch (tlp->tl_data_type) {
85956a424ccSmp 	     case KRB5_TL_KADM_DATA:
86056a424ccSmp 		  if (kadm)
86156a424ccSmp 		      counter++;
86256a424ccSmp 		  else
86356a424ccSmp 		      skip++;
86456a424ccSmp 		  break;
86556a424ccSmp 	     default:
86656a424ccSmp 		  counter++;
86756a424ccSmp 		  break;
86856a424ccSmp 	     }
8697c478bd9Sstevel@tonic-gate 	}
870*55fea89dSDan Cross 
8717c478bd9Sstevel@tonic-gate 	if (counter + skip == entry->n_tl_data) {
8727c478bd9Sstevel@tonic-gate 	    /* Pound out header */
8737c478bd9Sstevel@tonic-gate 	    fprintf(arg->ofile, "%d\t%d\t%d\t%d\t%d\t%s\t",
8747c478bd9Sstevel@tonic-gate 		    (int) entry->len,
8757c478bd9Sstevel@tonic-gate 		    strlen(name),
8767c478bd9Sstevel@tonic-gate 		    counter,
8777c478bd9Sstevel@tonic-gate 		    (int) entry->n_key_data,
8787c478bd9Sstevel@tonic-gate 		    (int) entry->e_length,
8797c478bd9Sstevel@tonic-gate 		    name);
8807c478bd9Sstevel@tonic-gate 	    fprintf(arg->ofile, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t",
8817c478bd9Sstevel@tonic-gate 		    entry->attributes,
8827c478bd9Sstevel@tonic-gate 		    entry->max_life,
8837c478bd9Sstevel@tonic-gate 		    entry->max_renewable_life,
8847c478bd9Sstevel@tonic-gate 		    entry->expiration,
8857c478bd9Sstevel@tonic-gate 		    entry->pw_expiration,
8867c478bd9Sstevel@tonic-gate 		    entry->last_success,
8877c478bd9Sstevel@tonic-gate 		    entry->last_failed,
8887c478bd9Sstevel@tonic-gate 		    entry->fail_auth_count);
8897c478bd9Sstevel@tonic-gate 	    /* Pound out tagged data. */
89056a424ccSmp 	    for (tlp = entry->tl_data; tlp; tlp = tlp->tl_data_next) {
89156a424ccSmp 		if (tlp->tl_data_type == KRB5_TL_KADM_DATA && !kadm)
89256a424ccSmp 		     continue; /* see above, [krb5-admin/89] */
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 		fprintf(arg->ofile, "%d\t%d\t",
8957c478bd9Sstevel@tonic-gate 			(int) tlp->tl_data_type,
8967c478bd9Sstevel@tonic-gate 			(int) tlp->tl_data_length);
8977c478bd9Sstevel@tonic-gate 		if (tlp->tl_data_length)
89856a424ccSmp 		    for (i=0; i<tlp->tl_data_length; i++)
89956a424ccSmp 			fprintf(arg->ofile, "%02x", tlp->tl_data_contents[i]);
9007c478bd9Sstevel@tonic-gate 		else
9017c478bd9Sstevel@tonic-gate 		    fprintf(arg->ofile, "%d", -1);
9027c478bd9Sstevel@tonic-gate 		fprintf(arg->ofile, "\t");
9037c478bd9Sstevel@tonic-gate 	    }
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	    /* Pound out key data */
90656a424ccSmp 	    for (counter=0; counter<entry->n_key_data; counter++) {
9077c478bd9Sstevel@tonic-gate 		kdata = &entry->key_data[counter];
9087c478bd9Sstevel@tonic-gate 		fprintf(arg->ofile, "%d\t%d\t",
9097c478bd9Sstevel@tonic-gate 			(int) kdata->key_data_ver,
9107c478bd9Sstevel@tonic-gate 			(int) kdata->key_data_kvno);
9117c478bd9Sstevel@tonic-gate 		for (i=0; i<kdata->key_data_ver; i++) {
9127c478bd9Sstevel@tonic-gate 		    fprintf(arg->ofile, "%d\t%d\t",
9137c478bd9Sstevel@tonic-gate 			    kdata->key_data_type[i],
9147c478bd9Sstevel@tonic-gate 			    kdata->key_data_length[i]);
9157c478bd9Sstevel@tonic-gate 		    if (kdata->key_data_length[i])
91656a424ccSmp 			for (j=0; j<kdata->key_data_length[i]; j++)
91756a424ccSmp 			    fprintf(arg->ofile, "%02x",
91856a424ccSmp 				    kdata->key_data_contents[i][j]);
9197c478bd9Sstevel@tonic-gate 		    else
9207c478bd9Sstevel@tonic-gate 			fprintf(arg->ofile, "%d", -1);
9217c478bd9Sstevel@tonic-gate 		    fprintf(arg->ofile, "\t");
9227c478bd9Sstevel@tonic-gate 		}
9237c478bd9Sstevel@tonic-gate 	    }
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	    /* Pound out extra data */
9267c478bd9Sstevel@tonic-gate 	    if (entry->e_length)
9277c478bd9Sstevel@tonic-gate 		for (i=0; i<entry->e_length; i++)
92856a424ccSmp 		    fprintf(arg->ofile, "%02x", entry->e_data[i]);
9297c478bd9Sstevel@tonic-gate 	    else
9307c478bd9Sstevel@tonic-gate 		fprintf(arg->ofile, "%d", -1);
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 	    /* Print trailer */
9337c478bd9Sstevel@tonic-gate 	    fprintf(arg->ofile, ";\n");
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	    if (arg->verbose)
9367c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s\n", name);
93756a424ccSmp 	}
93856a424ccSmp 	else {
9397c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(sdump_tl_inc_err),
94056a424ccSmp 		    arg->programname, name, counter+skip,
941*55fea89dSDan Cross 		    (int) entry->n_tl_data);
9427c478bd9Sstevel@tonic-gate 	    retval = EINVAL;
9437c478bd9Sstevel@tonic-gate 	}
9447c478bd9Sstevel@tonic-gate     }
9457c478bd9Sstevel@tonic-gate     krb5_xfree(name);
9467c478bd9Sstevel@tonic-gate     return(retval);
9477c478bd9Sstevel@tonic-gate }
94856a424ccSmp 
9497c478bd9Sstevel@tonic-gate /*
9507c478bd9Sstevel@tonic-gate  * dump_iprop_iterator()	- Output a dump record in iprop format.
9517c478bd9Sstevel@tonic-gate  */
9527c478bd9Sstevel@tonic-gate static krb5_error_code
dump_iprop_iterator(ptr,entry)9537c478bd9Sstevel@tonic-gate dump_iprop_iterator(ptr, entry)
9547c478bd9Sstevel@tonic-gate     krb5_pointer	ptr;
9557c478bd9Sstevel@tonic-gate     krb5_db_entry	*entry;
9567c478bd9Sstevel@tonic-gate {
9577c478bd9Sstevel@tonic-gate     krb5_error_code	retval;
9587c478bd9Sstevel@tonic-gate     struct dump_args	*arg;
9597c478bd9Sstevel@tonic-gate     char		*name;
9607c478bd9Sstevel@tonic-gate     krb5_tl_data	*tlp;
9617c478bd9Sstevel@tonic-gate     krb5_key_data	*kdata;
9627c478bd9Sstevel@tonic-gate     int			counter, i, j;
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate     /* Initialize */
9657c478bd9Sstevel@tonic-gate     arg = (struct dump_args *) ptr;
9667c478bd9Sstevel@tonic-gate     name = (char *) NULL;
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate     /*
9697c478bd9Sstevel@tonic-gate      * Flatten the principal name.
9707c478bd9Sstevel@tonic-gate      */
9717c478bd9Sstevel@tonic-gate     if ((retval = krb5_unparse_name(arg->kcontext,
9727c478bd9Sstevel@tonic-gate 				    entry->princ,
9737c478bd9Sstevel@tonic-gate 				    &name))) {
9747c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(pname_unp_err),
9757c478bd9Sstevel@tonic-gate 		arg->programname, error_message(retval));
9767c478bd9Sstevel@tonic-gate 	return(retval);
9777c478bd9Sstevel@tonic-gate     }
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate     /*
9807c478bd9Sstevel@tonic-gate      * Re-encode the keys in the new master key, if necessary.
9817c478bd9Sstevel@tonic-gate      */
9827c478bd9Sstevel@tonic-gate     if (mkey_convert) {
9837c478bd9Sstevel@tonic-gate 	retval = master_key_convert(arg->kcontext, entry);
9847c478bd9Sstevel@tonic-gate 	if (retval) {
9857c478bd9Sstevel@tonic-gate 	    com_err(arg->programname, retval, remaster_err_fmt, name);
9867c478bd9Sstevel@tonic-gate 	    return retval;
9877c478bd9Sstevel@tonic-gate 	}
9887c478bd9Sstevel@tonic-gate     }
989*55fea89dSDan Cross 
9907c478bd9Sstevel@tonic-gate     /*
9917c478bd9Sstevel@tonic-gate      * If we don't have any match strings, or if our name matches, then
9927c478bd9Sstevel@tonic-gate      * proceed with the dump, otherwise, just forget about it.
9937c478bd9Sstevel@tonic-gate      */
9947c478bd9Sstevel@tonic-gate     if (!arg->nnames || name_matches(name, arg)) {
9957c478bd9Sstevel@tonic-gate 	/*
9967c478bd9Sstevel@tonic-gate 	 * We'd like to just blast out the contents as they would
9977c478bd9Sstevel@tonic-gate 	 * appear in the database so that we can just suck it back
9987c478bd9Sstevel@tonic-gate 	 * in, but it doesn't lend itself to easy editing.
9997c478bd9Sstevel@tonic-gate 	 */
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	/*
10027c478bd9Sstevel@tonic-gate 	 * The dump format is as follows: len strlen(name)
10037c478bd9Sstevel@tonic-gate 	 * n_tl_data n_key_data e_length name attributes max_life
10047c478bd9Sstevel@tonic-gate 	 * max_renewable_life expiration pw_expiration last_success
10057c478bd9Sstevel@tonic-gate 	 * last_failed fail_auth_count n_tl_data*[type length
10067c478bd9Sstevel@tonic-gate 	 * <contents>] n_key_data*[ver kvno ver*(type length
10077c478bd9Sstevel@tonic-gate 	 * <contents>)] <e_data> Fields which are not encapsulated
10087c478bd9Sstevel@tonic-gate 	 * by angle-brackets are to appear verbatim.  Bracketed
10097c478bd9Sstevel@tonic-gate 	 * fields absence is indicated by a -1 in its place
10107c478bd9Sstevel@tonic-gate 	 */
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 	/*
10137c478bd9Sstevel@tonic-gate 	 * Make sure that the tagged list is reasonably correct.
10147c478bd9Sstevel@tonic-gate 	 */
10157c478bd9Sstevel@tonic-gate 	counter = 0;
10167c478bd9Sstevel@tonic-gate 	for (tlp = entry->tl_data; tlp; tlp = tlp->tl_data_next)
10177c478bd9Sstevel@tonic-gate 		  counter++;
1018*55fea89dSDan Cross 
10197c478bd9Sstevel@tonic-gate 	if (counter == entry->n_tl_data) {
10207c478bd9Sstevel@tonic-gate 	    /* Pound out header */
10217c478bd9Sstevel@tonic-gate 	    fprintf(arg->ofile, "%d\t%d\t%d\t%d\t%d\t%s\t",
10227c478bd9Sstevel@tonic-gate 		    (int) entry->len,
10237c478bd9Sstevel@tonic-gate 		    strlen(name),
10247c478bd9Sstevel@tonic-gate 		    (int) entry->n_tl_data,
10257c478bd9Sstevel@tonic-gate 		    (int) entry->n_key_data,
10267c478bd9Sstevel@tonic-gate 		    (int) entry->e_length,
10277c478bd9Sstevel@tonic-gate 		    name);
10287c478bd9Sstevel@tonic-gate 	    fprintf(arg->ofile, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t",
10297c478bd9Sstevel@tonic-gate 		    entry->attributes,
10307c478bd9Sstevel@tonic-gate 		    entry->max_life,
10317c478bd9Sstevel@tonic-gate 		    entry->max_renewable_life,
10327c478bd9Sstevel@tonic-gate 		    entry->expiration,
10337c478bd9Sstevel@tonic-gate 		    entry->pw_expiration,
10347c478bd9Sstevel@tonic-gate 		    entry->last_success,
10357c478bd9Sstevel@tonic-gate 		    entry->last_failed,
10367c478bd9Sstevel@tonic-gate 		    entry->fail_auth_count);
10377c478bd9Sstevel@tonic-gate 	    /* Pound out tagged data. */
10387c478bd9Sstevel@tonic-gate 			for (tlp = entry->tl_data; tlp;
10397c478bd9Sstevel@tonic-gate 			    tlp = tlp->tl_data_next) {
10407c478bd9Sstevel@tonic-gate 		fprintf(arg->ofile, "%d\t%d\t",
10417c478bd9Sstevel@tonic-gate 			(int) tlp->tl_data_type,
10427c478bd9Sstevel@tonic-gate 			(int) tlp->tl_data_length);
10437c478bd9Sstevel@tonic-gate 		if (tlp->tl_data_length)
10447c478bd9Sstevel@tonic-gate 					for (i = 0;
10457c478bd9Sstevel@tonic-gate 					    i < tlp->tl_data_length;
10467c478bd9Sstevel@tonic-gate 					    i++)
10477c478bd9Sstevel@tonic-gate 						fprintf(arg->ofile, "%02x",
10487c478bd9Sstevel@tonic-gate 							tlp->
10497c478bd9Sstevel@tonic-gate 							tl_data_contents[i]);
10507c478bd9Sstevel@tonic-gate 		else
10517c478bd9Sstevel@tonic-gate 		    fprintf(arg->ofile, "%d", -1);
10527c478bd9Sstevel@tonic-gate 		fprintf(arg->ofile, "\t");
10537c478bd9Sstevel@tonic-gate 	    }
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 	    /* Pound out key data */
10567c478bd9Sstevel@tonic-gate 			for (counter = 0;
10577c478bd9Sstevel@tonic-gate 			    counter < entry->n_key_data; counter++) {
10587c478bd9Sstevel@tonic-gate 		kdata = &entry->key_data[counter];
10597c478bd9Sstevel@tonic-gate 		fprintf(arg->ofile, "%d\t%d\t",
10607c478bd9Sstevel@tonic-gate 			(int) kdata->key_data_ver,
10617c478bd9Sstevel@tonic-gate 			(int) kdata->key_data_kvno);
10627c478bd9Sstevel@tonic-gate 		for (i=0; i<kdata->key_data_ver; i++) {
10637c478bd9Sstevel@tonic-gate 		    fprintf(arg->ofile, "%d\t%d\t",
10647c478bd9Sstevel@tonic-gate 			    kdata->key_data_type[i],
10657c478bd9Sstevel@tonic-gate 			    kdata->key_data_length[i]);
10667c478bd9Sstevel@tonic-gate 		    if (kdata->key_data_length[i])
10677c478bd9Sstevel@tonic-gate 						for (j = 0;
10687c478bd9Sstevel@tonic-gate 						    j < kdata->
10697c478bd9Sstevel@tonic-gate 							key_data_length[i];
10707c478bd9Sstevel@tonic-gate 						    j++)
10717c478bd9Sstevel@tonic-gate 							fprintf(arg->ofile,
10727c478bd9Sstevel@tonic-gate 							    "%02x",
10737c478bd9Sstevel@tonic-gate 							    kdata->
10747c478bd9Sstevel@tonic-gate 							    key_data_contents
10757c478bd9Sstevel@tonic-gate 								[i][j]);
10767c478bd9Sstevel@tonic-gate 		    else
10777c478bd9Sstevel@tonic-gate 			fprintf(arg->ofile, "%d", -1);
10787c478bd9Sstevel@tonic-gate 		    fprintf(arg->ofile, "\t");
10797c478bd9Sstevel@tonic-gate 		}
10807c478bd9Sstevel@tonic-gate 	    }
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	    /* Pound out extra data */
10837c478bd9Sstevel@tonic-gate 	    if (entry->e_length)
10847c478bd9Sstevel@tonic-gate 		for (i=0; i<entry->e_length; i++)
10857c478bd9Sstevel@tonic-gate 					fprintf(arg->ofile, "%02x",
10867c478bd9Sstevel@tonic-gate 						entry->e_data[i]);
10877c478bd9Sstevel@tonic-gate 	    else
10887c478bd9Sstevel@tonic-gate 		fprintf(arg->ofile, "%d", -1);
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	    /* Print trailer */
10917c478bd9Sstevel@tonic-gate 	    fprintf(arg->ofile, ";\n");
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 	    if (arg->verbose)
10947c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s\n", name);
10957c478bd9Sstevel@tonic-gate 		} else {
10967c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(sdump_tl_inc_err),
10977c478bd9Sstevel@tonic-gate 		    arg->programname, name, counter,
1098*55fea89dSDan Cross 		    (int) entry->n_tl_data);
10997c478bd9Sstevel@tonic-gate 	    retval = EINVAL;
11007c478bd9Sstevel@tonic-gate 	}
11017c478bd9Sstevel@tonic-gate     }
11027c478bd9Sstevel@tonic-gate     krb5_xfree(name);
11037c478bd9Sstevel@tonic-gate     return(retval);
11047c478bd9Sstevel@tonic-gate }
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate /*
11077c478bd9Sstevel@tonic-gate  * dump_k5beta7_iterator()	- Output a dump record in krb5b7 format.
11087c478bd9Sstevel@tonic-gate  */
11097c478bd9Sstevel@tonic-gate static krb5_error_code
dump_k5beta7_princ(ptr,entry)11107c478bd9Sstevel@tonic-gate dump_k5beta7_princ(ptr, entry)
11117c478bd9Sstevel@tonic-gate     krb5_pointer	ptr;
11127c478bd9Sstevel@tonic-gate     krb5_db_entry	*entry;
111356a424ccSmp {
111456a424ccSmp     return dump_k5beta7_princ_ext(ptr, entry, 0);
111556a424ccSmp }
111656a424ccSmp 
111756a424ccSmp static krb5_error_code
dump_k5beta7_princ_ext(ptr,entry,kadm)111856a424ccSmp dump_k5beta7_princ_ext(ptr, entry, kadm)
111956a424ccSmp     krb5_pointer	ptr;
112056a424ccSmp     krb5_db_entry	*entry;
112156a424ccSmp     int			kadm;
11227c478bd9Sstevel@tonic-gate {
11237c478bd9Sstevel@tonic-gate      krb5_error_code retval;
11247c478bd9Sstevel@tonic-gate      struct dump_args *arg;
11257c478bd9Sstevel@tonic-gate      char *name;
11267c478bd9Sstevel@tonic-gate      int tmp_nnames;
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate      /* Initialize */
11297c478bd9Sstevel@tonic-gate      arg = (struct dump_args *) ptr;
11307c478bd9Sstevel@tonic-gate      name = (char *) NULL;
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate      /*
11337c478bd9Sstevel@tonic-gate       * Flatten the principal name.
11347c478bd9Sstevel@tonic-gate       */
11357c478bd9Sstevel@tonic-gate      if ((retval = krb5_unparse_name(arg->kcontext,
11367c478bd9Sstevel@tonic-gate 				     entry->princ,
11377c478bd9Sstevel@tonic-gate 				     &name))) {
11387c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(pname_unp_err),
11397c478bd9Sstevel@tonic-gate 		  arg->programname, error_message(retval));
11407c478bd9Sstevel@tonic-gate 	  return(retval);
11417c478bd9Sstevel@tonic-gate      }
11427c478bd9Sstevel@tonic-gate      /*
11437c478bd9Sstevel@tonic-gate       * If we don't have any match strings, or if our name matches, then
11447c478bd9Sstevel@tonic-gate       * proceed with the dump, otherwise, just forget about it.
11457c478bd9Sstevel@tonic-gate       */
11467c478bd9Sstevel@tonic-gate      if (!arg->nnames || name_matches(name, arg)) {
11477c478bd9Sstevel@tonic-gate 	  fprintf(arg->ofile, "princ\t");
1148*55fea89dSDan Cross 
11497c478bd9Sstevel@tonic-gate 	  /* save the callee from matching the name again */
11507c478bd9Sstevel@tonic-gate 	  tmp_nnames = arg->nnames;
11517c478bd9Sstevel@tonic-gate 	  arg->nnames = 0;
115256a424ccSmp 	  retval = dump_k5beta6_iterator_ext(ptr, entry, kadm);
11537c478bd9Sstevel@tonic-gate 	  arg->nnames = tmp_nnames;
11547c478bd9Sstevel@tonic-gate      }
115556a424ccSmp 
11567c478bd9Sstevel@tonic-gate      free(name);
115756a424ccSmp      return retval;
11587c478bd9Sstevel@tonic-gate }
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate /*
11617c478bd9Sstevel@tonic-gate  * dump_iprop_princ()	- Output a dump record in iprop format.
11627c478bd9Sstevel@tonic-gate  * This was created in order to dump more data, such as kadm5 tl
11637c478bd9Sstevel@tonic-gate  */
11647c478bd9Sstevel@tonic-gate static krb5_error_code
dump_iprop_princ(ptr,entry)11657c478bd9Sstevel@tonic-gate dump_iprop_princ(ptr, entry)
11667c478bd9Sstevel@tonic-gate     krb5_pointer	ptr;
11677c478bd9Sstevel@tonic-gate     krb5_db_entry	*entry;
11687c478bd9Sstevel@tonic-gate {
11697c478bd9Sstevel@tonic-gate      krb5_error_code retval;
11707c478bd9Sstevel@tonic-gate      struct dump_args *arg;
11717c478bd9Sstevel@tonic-gate      char *name;
11727c478bd9Sstevel@tonic-gate      int tmp_nnames;
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate      /* Initialize */
11757c478bd9Sstevel@tonic-gate      arg = (struct dump_args *) ptr;
11767c478bd9Sstevel@tonic-gate      name = (char *) NULL;
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate      /*
11797c478bd9Sstevel@tonic-gate       * Flatten the principal name.
11807c478bd9Sstevel@tonic-gate       */
11817c478bd9Sstevel@tonic-gate      if ((retval = krb5_unparse_name(arg->kcontext,
11827c478bd9Sstevel@tonic-gate 				     entry->princ,
11837c478bd9Sstevel@tonic-gate 				     &name))) {
11847c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(pname_unp_err),
11857c478bd9Sstevel@tonic-gate 		  arg->programname, error_message(retval));
11867c478bd9Sstevel@tonic-gate 	  return(retval);
11877c478bd9Sstevel@tonic-gate      }
11887c478bd9Sstevel@tonic-gate      /*
11897c478bd9Sstevel@tonic-gate       * If we don't have any match strings, or if our name matches, then
11907c478bd9Sstevel@tonic-gate       * proceed with the dump, otherwise, just forget about it.
11917c478bd9Sstevel@tonic-gate       */
11927c478bd9Sstevel@tonic-gate      if (!arg->nnames || name_matches(name, arg)) {
11937c478bd9Sstevel@tonic-gate 	  fprintf(arg->ofile, "princ\t");
1194*55fea89dSDan Cross 
11957c478bd9Sstevel@tonic-gate 	  /* save the callee from matching the name again */
11967c478bd9Sstevel@tonic-gate 	  tmp_nnames = arg->nnames;
11977c478bd9Sstevel@tonic-gate 	  arg->nnames = 0;
11987c478bd9Sstevel@tonic-gate 	  retval = dump_iprop_iterator(ptr, entry);
11997c478bd9Sstevel@tonic-gate 	  arg->nnames = tmp_nnames;
12007c478bd9Sstevel@tonic-gate      }
12017c478bd9Sstevel@tonic-gate      free(name);
12027c478bd9Sstevel@tonic-gate 	return (retval);
12037c478bd9Sstevel@tonic-gate }
120456a424ccSmp 
120556a424ccSmp static krb5_error_code
dump_k5beta7_princ_withpolicy(ptr,entry)120656a424ccSmp dump_k5beta7_princ_withpolicy(ptr, entry)
120756a424ccSmp     krb5_pointer	ptr;
120856a424ccSmp     krb5_db_entry	*entry;
120956a424ccSmp {
121056a424ccSmp     return dump_k5beta7_princ_ext(ptr, entry, 1);
121156a424ccSmp }
121256a424ccSmp 
dump_k5beta7_policy(void * data,osa_policy_ent_t entry)121356a424ccSmp void dump_k5beta7_policy(void *data, osa_policy_ent_t entry)
12147c478bd9Sstevel@tonic-gate {
12157c478bd9Sstevel@tonic-gate      struct dump_args *arg;
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate      arg = (struct dump_args *) data;
12187c478bd9Sstevel@tonic-gate      fprintf(arg->ofile, "policy\t%s\t%d\t%d\t%d\t%d\t%d\t%d\n", entry->name,
12197c478bd9Sstevel@tonic-gate 	     entry->pw_min_life, entry->pw_max_life, entry->pw_min_length,
12207c478bd9Sstevel@tonic-gate 	     entry->pw_min_classes, entry->pw_history_num,
12217c478bd9Sstevel@tonic-gate 	     entry->policy_refcnt);
12227c478bd9Sstevel@tonic-gate }
12237c478bd9Sstevel@tonic-gate 
print_key_data(FILE * f,krb5_key_data * key_data)122456a424ccSmp static void print_key_data(FILE *f, krb5_key_data *key_data)
12257c478bd9Sstevel@tonic-gate {
12267c478bd9Sstevel@tonic-gate      int c;
1227*55fea89dSDan Cross 
12287c478bd9Sstevel@tonic-gate      fprintf(f, "%d\t%d\t", key_data->key_data_type[0],
12297c478bd9Sstevel@tonic-gate 	     key_data->key_data_length[0]);
1230*55fea89dSDan Cross      for(c = 0; c < key_data->key_data_length[0]; c++)
12317c478bd9Sstevel@tonic-gate 	  fprintf(f, "%02x ",
12327c478bd9Sstevel@tonic-gate 		  key_data->key_data_contents[0][c]);
12337c478bd9Sstevel@tonic-gate }
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate /*
12367c478bd9Sstevel@tonic-gate  * Function: print_princ
1237*55fea89dSDan Cross  *
12387c478bd9Sstevel@tonic-gate  * Purpose: output osa_adb_princ_ent data in a human
12397c478bd9Sstevel@tonic-gate  *	    readable format (which is a format suitable for
12407c478bd9Sstevel@tonic-gate  *	    ovsec_adm_import consumption)
12417c478bd9Sstevel@tonic-gate  *
12427c478bd9Sstevel@tonic-gate  * Arguments:
12437c478bd9Sstevel@tonic-gate  *	data		(input) pointer to a structure containing a FILE *
12447c478bd9Sstevel@tonic-gate  *			        and a record counter.
12457c478bd9Sstevel@tonic-gate  *	entry		(input) entry to get dumped.
12467c478bd9Sstevel@tonic-gate  * 	<return value>	void
12477c478bd9Sstevel@tonic-gate  *
12487c478bd9Sstevel@tonic-gate  * Requires:
12497c478bd9Sstevel@tonic-gate  *	nuttin
1250*55fea89dSDan Cross  *
12517c478bd9Sstevel@tonic-gate  * Effects:
12527c478bd9Sstevel@tonic-gate  *	writes data to the specified file pointerp.
12537c478bd9Sstevel@tonic-gate  *
12547c478bd9Sstevel@tonic-gate  * Modifies:
12557c478bd9Sstevel@tonic-gate  *	nuttin
1256*55fea89dSDan Cross  *
12577c478bd9Sstevel@tonic-gate  */
dump_ov_princ(krb5_pointer ptr,krb5_db_entry * kdb)125856a424ccSmp static krb5_error_code dump_ov_princ(krb5_pointer ptr, krb5_db_entry *kdb)
12597c478bd9Sstevel@tonic-gate {
12607c478bd9Sstevel@tonic-gate     char *princstr;
126156a424ccSmp     int	x, y, foundcrc;
12627c478bd9Sstevel@tonic-gate     struct dump_args *arg;
12637c478bd9Sstevel@tonic-gate     krb5_tl_data tl_data;
12647c478bd9Sstevel@tonic-gate     osa_princ_ent_rec adb;
12657c478bd9Sstevel@tonic-gate     XDR xdrs;
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate     arg = (struct dump_args *) ptr;
12687c478bd9Sstevel@tonic-gate     /*
12697c478bd9Sstevel@tonic-gate      * XXX Currently, lookup_tl_data always returns zero; it sets
127056a424ccSmp      * tl_data->tl_data_length to zero if the type isn't found.
127156a424ccSmp      * This should be fixed...
12727c478bd9Sstevel@tonic-gate      */
12737c478bd9Sstevel@tonic-gate     /*
12747c478bd9Sstevel@tonic-gate      * XXX Should this function do nothing for a principal with no
127556a424ccSmp      * admin data, or print a record of "default" values?   See
127656a424ccSmp      * comment in server_kdb.c to help decide.
12777c478bd9Sstevel@tonic-gate      */
12787c478bd9Sstevel@tonic-gate     tl_data.tl_data_type = KRB5_TL_KADM_DATA;
127956a424ccSmp     if (krb5_dbe_lookup_tl_data(arg->kcontext, kdb, &tl_data)
128056a424ccSmp 	|| (tl_data.tl_data_length == 0))
128156a424ccSmp 	 return 0;
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate     memset(&adb, 0, sizeof(adb));
128456a424ccSmp     xdrmem_create(&xdrs, (const caddr_t) tl_data.tl_data_contents,
12857c478bd9Sstevel@tonic-gate 		  tl_data.tl_data_length, XDR_DECODE);
12867c478bd9Sstevel@tonic-gate     if (! xdr_osa_princ_ent_rec(&xdrs, &adb)) {
12877c478bd9Sstevel@tonic-gate 	 xdr_destroy(&xdrs);
128854925bf6Swillf 	 return(KADM5_XDR_FAILURE);
12897c478bd9Sstevel@tonic-gate     }
12907c478bd9Sstevel@tonic-gate     xdr_destroy(&xdrs);
1291*55fea89dSDan Cross 
12927c478bd9Sstevel@tonic-gate     krb5_unparse_name(arg->kcontext, kdb->princ, &princstr);
12937c478bd9Sstevel@tonic-gate     fprintf(arg->ofile, "princ\t%s\t", princstr);
12947c478bd9Sstevel@tonic-gate     if(adb.policy == NULL)
12957c478bd9Sstevel@tonic-gate 	fputc('\t', arg->ofile);
12967c478bd9Sstevel@tonic-gate     else
12977c478bd9Sstevel@tonic-gate 	fprintf(arg->ofile, "%s\t", adb.policy);
129856a424ccSmp     fprintf(arg->ofile, "%lx\t%d\t%d\t%d", adb.aux_attributes,
12997c478bd9Sstevel@tonic-gate 	    adb.old_key_len,adb.old_key_next, adb.admin_history_kvno);
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate     for (x = 0; x < adb.old_key_len; x++) {
13027c478bd9Sstevel@tonic-gate 	 foundcrc = 0;
13037c478bd9Sstevel@tonic-gate 	 for (y = 0; y < adb.old_keys[x].n_key_data; y++) {
13047c478bd9Sstevel@tonic-gate 	      krb5_key_data *key_data = &adb.old_keys[x].key_data[y];
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 	      if (key_data->key_data_type[0] != ENCTYPE_DES_CBC_CRC)
13077c478bd9Sstevel@tonic-gate 		   continue;
13087c478bd9Sstevel@tonic-gate 	      if (foundcrc) {
13097c478bd9Sstevel@tonic-gate 				fprintf(stderr,
13107c478bd9Sstevel@tonic-gate 				    gettext("Warning!  Multiple DES-CBC-CRC "
13117c478bd9Sstevel@tonic-gate 					"keys for principal %s; skipping "
13127c478bd9Sstevel@tonic-gate 					"duplicates.\n"),
13137c478bd9Sstevel@tonic-gate 			   princstr);
13147c478bd9Sstevel@tonic-gate 		   continue;
13157c478bd9Sstevel@tonic-gate 	      }
13167c478bd9Sstevel@tonic-gate 	      foundcrc++;
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 	      fputc('\t', arg->ofile);
13197c478bd9Sstevel@tonic-gate 	      print_key_data(arg->ofile, key_data);
13207c478bd9Sstevel@tonic-gate 	 }
13217c478bd9Sstevel@tonic-gate 	 if (!foundcrc)
13227c478bd9Sstevel@tonic-gate 			fprintf(stderr,
13237c478bd9Sstevel@tonic-gate 			    gettext("Warning!  No DES-CBC-CRC key "
13247c478bd9Sstevel@tonic-gate 				"for principal %s, cannot generate "
13257c478bd9Sstevel@tonic-gate 				"OV-compatible record; skipping\n"),
13267c478bd9Sstevel@tonic-gate 		      princstr);
13277c478bd9Sstevel@tonic-gate     }
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate     fputc('\n', arg->ofile);
13307c478bd9Sstevel@tonic-gate     free(princstr);
133156a424ccSmp     return 0;
13327c478bd9Sstevel@tonic-gate }
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate /*
13357c478bd9Sstevel@tonic-gate  * usage is:
133656a424ccSmp  *	dump_db [-i] [-old] [-b6] [-b7] [-ov] [-verbose] [-mkey_convert]
133756a424ccSmp  *		[-new_mkey_file mkey_file] [-rev] [-recurse]
133856a424ccSmp  *		[filename [principals...]]
13397c478bd9Sstevel@tonic-gate  */
13407c478bd9Sstevel@tonic-gate void
dump_db(argc,argv)13417c478bd9Sstevel@tonic-gate dump_db(argc, argv)
13427c478bd9Sstevel@tonic-gate     int		argc;
13437c478bd9Sstevel@tonic-gate     char	**argv;
13447c478bd9Sstevel@tonic-gate {
13457c478bd9Sstevel@tonic-gate     FILE		*f;
13467c478bd9Sstevel@tonic-gate     struct dump_args	arglist;
1347dd9ccd46S /* Solaris Kerberos */
1348dd9ccd46S #if 0
13497c478bd9Sstevel@tonic-gate     char		*programname;
1350dd9ccd46S #endif
13517c478bd9Sstevel@tonic-gate     char		*ofile;
13527c478bd9Sstevel@tonic-gate     krb5_error_code	kret, retval;
13537c478bd9Sstevel@tonic-gate     dump_version	*dump;
13547c478bd9Sstevel@tonic-gate     int			aindex;
13557c478bd9Sstevel@tonic-gate     krb5_boolean	locked;
13567c478bd9Sstevel@tonic-gate     char		*new_mkey_file = 0;
13577c478bd9Sstevel@tonic-gate     bool_t		dump_sno = FALSE;
13587c478bd9Sstevel@tonic-gate     kdb_log_context	*log_ctx;
13592dd2efa5Swillf     /* Solaris Kerberos: adding support for -rev/recurse flags */
13602dd2efa5Swillf     int			db_arg_index = 0;
13612dd2efa5Swillf     char		*db_args[3] = {NULL, NULL, NULL};
1362*55fea89dSDan Cross 
13637c478bd9Sstevel@tonic-gate     /*
13647c478bd9Sstevel@tonic-gate      * Parse the arguments.
13657c478bd9Sstevel@tonic-gate      */
1366dd9ccd46S /* Solaris Kerberos */
1367dd9ccd46S #if 0
13687c478bd9Sstevel@tonic-gate     programname = argv[0];
13697c478bd9Sstevel@tonic-gate     if (strrchr(programname, (int) '/'))
13707c478bd9Sstevel@tonic-gate 	programname = strrchr(argv[0], (int) '/') + 1;
1371dd9ccd46S #endif
13727c478bd9Sstevel@tonic-gate     ofile = (char *) NULL;
137356a424ccSmp     dump = &r1_3_version;
13747c478bd9Sstevel@tonic-gate     arglist.verbose = 0;
13757c478bd9Sstevel@tonic-gate     new_mkey_file = 0;
13767c478bd9Sstevel@tonic-gate     mkey_convert = 0;
137756a424ccSmp     backwards = 0;
137856a424ccSmp     recursive = 0;
13797c478bd9Sstevel@tonic-gate     log_ctx = util_context->kdblog_context;
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate     /*
13827c478bd9Sstevel@tonic-gate      * Parse the qualifiers.
13837c478bd9Sstevel@tonic-gate      */
13847c478bd9Sstevel@tonic-gate     for (aindex = 1; aindex < argc; aindex++) {
138556a424ccSmp 	if (!strcmp(argv[aindex], oldoption))
13867c478bd9Sstevel@tonic-gate 	     dump = &old_version;
138756a424ccSmp 	else if (!strcmp(argv[aindex], b6option))
13887c478bd9Sstevel@tonic-gate 	     dump = &beta6_version;
138956a424ccSmp 	else if (!strcmp(argv[aindex], b7option))
139056a424ccSmp 	     dump = &beta7_version;
139156a424ccSmp 	else if (!strcmp(argv[aindex], ovoption))
13927c478bd9Sstevel@tonic-gate 	     dump = &ov_version;
139356a424ccSmp 	else if (!strcmp(argv[aindex], ipropoption)) {
13947c478bd9Sstevel@tonic-gate 			if (log_ctx && log_ctx->iproprole) {
13957c478bd9Sstevel@tonic-gate 				dump = &iprop_version;
13967c478bd9Sstevel@tonic-gate 				/*
13977c478bd9Sstevel@tonic-gate 				 * dump_sno is used to indicate if the serial
13987c478bd9Sstevel@tonic-gate 				 * # should be populated in the output
13997c478bd9Sstevel@tonic-gate 				 * file to be used later by iprop for updating
14007c478bd9Sstevel@tonic-gate 				 * the slave's update log when loading
14017c478bd9Sstevel@tonic-gate 				 */
14027c478bd9Sstevel@tonic-gate 				dump_sno = TRUE;
14037c478bd9Sstevel@tonic-gate 			} else {
14047c478bd9Sstevel@tonic-gate 				fprintf(stderr, gettext("Iprop not enabled\n"));
14057c478bd9Sstevel@tonic-gate 				exit_status++;
14067c478bd9Sstevel@tonic-gate 				return;
14077c478bd9Sstevel@tonic-gate 			}
14087c478bd9Sstevel@tonic-gate 		}
140956a424ccSmp 	else if (!strcmp(argv[aindex], verboseoption))
14107c478bd9Sstevel@tonic-gate 	    arglist.verbose++;
14117c478bd9Sstevel@tonic-gate 	else if (!strcmp(argv[aindex], "-mkey_convert"))
14127c478bd9Sstevel@tonic-gate 	    mkey_convert = 1;
14137c478bd9Sstevel@tonic-gate 	else if (!strcmp(argv[aindex], "-new_mkey_file")) {
14147c478bd9Sstevel@tonic-gate 	    new_mkey_file = argv[++aindex];
14157c478bd9Sstevel@tonic-gate 	    mkey_convert = 1;
14162dd2efa5Swillf         } else if (!strcmp(argv[aindex], "-rev")) {
14172dd2efa5Swillf 	    /* Solaris Kerberos: adding support for -rev/recurse flags */
14182dd2efa5Swillf 	    /* hack to pass args to db specific plugin */
14192dd2efa5Swillf 	    db_args[db_arg_index++] = "rev";
14202dd2efa5Swillf 	} else if (!strcmp(argv[aindex], "-recurse")) {
14212dd2efa5Swillf 	    /* hack to pass args to db specific plugin */
14222dd2efa5Swillf 	    db_args[db_arg_index++] = "recurse";
14232dd2efa5Swillf 	} else
14247c478bd9Sstevel@tonic-gate 	    break;
14257c478bd9Sstevel@tonic-gate     }
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate     arglist.names = (char **) NULL;
14287c478bd9Sstevel@tonic-gate     arglist.nnames = 0;
14297c478bd9Sstevel@tonic-gate     if (aindex < argc) {
14307c478bd9Sstevel@tonic-gate 	ofile = argv[aindex];
14317c478bd9Sstevel@tonic-gate 	aindex++;
14327c478bd9Sstevel@tonic-gate 	if (aindex < argc) {
14337c478bd9Sstevel@tonic-gate 	    arglist.names = &argv[aindex];
14347c478bd9Sstevel@tonic-gate 	    arglist.nnames = argc - aindex;
14357c478bd9Sstevel@tonic-gate 	}
14367c478bd9Sstevel@tonic-gate     }
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate     /*
14397c478bd9Sstevel@tonic-gate      * Make sure the database is open.  The policy database only has
14407c478bd9Sstevel@tonic-gate      * to be opened if we try a dump that uses it.
14417c478bd9Sstevel@tonic-gate      */
144254925bf6Swillf     if (!dbactive) {
1443dd9ccd46S 	/* Solaris Kerberos */
1444dd9ccd46S 	com_err(progname, 0, Err_no_database); /* Solaris Kerberos */
14457c478bd9Sstevel@tonic-gate 	exit_status++;
14467c478bd9Sstevel@tonic-gate 	return;
14477c478bd9Sstevel@tonic-gate     }
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate     /*
14507c478bd9Sstevel@tonic-gate      * If we're doing a master key conversion, set up for it.
14517c478bd9Sstevel@tonic-gate      */
14527c478bd9Sstevel@tonic-gate     if (mkey_convert) {
14537c478bd9Sstevel@tonic-gate 	    if (!valid_master_key) {
14547c478bd9Sstevel@tonic-gate 		    /* TRUE here means read the keyboard, but only once */
14557c478bd9Sstevel@tonic-gate 		    retval = krb5_db_fetch_mkey(util_context,
14567c478bd9Sstevel@tonic-gate 						master_princ,
14577c478bd9Sstevel@tonic-gate 						global_params.enctype,
14587c478bd9Sstevel@tonic-gate 						TRUE, FALSE,
14597c478bd9Sstevel@tonic-gate 						(char *) NULL, 0,
14607c478bd9Sstevel@tonic-gate 						&master_key);
14617c478bd9Sstevel@tonic-gate 		    if (retval) {
1462dd9ccd46S 			    /* Solaris Kerberos */
1463dd9ccd46S 			    com_err(progname, retval,
14647c478bd9Sstevel@tonic-gate 				    gettext("while reading master key"));
14657c478bd9Sstevel@tonic-gate 			    exit(1);
14667c478bd9Sstevel@tonic-gate 		    }
14677c478bd9Sstevel@tonic-gate 		    retval = krb5_db_verify_master_key(util_context,
14687c478bd9Sstevel@tonic-gate 						       master_princ,
14697c478bd9Sstevel@tonic-gate 						       &master_key);
14707c478bd9Sstevel@tonic-gate 		    if (retval) {
1471dd9ccd46S 			    /* Solaris Kerberos */
1472dd9ccd46S 			    com_err(progname, retval,
14737c478bd9Sstevel@tonic-gate 				    gettext("while verifying master key"));
14747c478bd9Sstevel@tonic-gate 			    exit(1);
14757c478bd9Sstevel@tonic-gate 		    }
14767c478bd9Sstevel@tonic-gate 	    }
14777c478bd9Sstevel@tonic-gate 	    if (!new_mkey_file)
14787c478bd9Sstevel@tonic-gate 		    printf(gettext("Please enter new master key....\n"));
1479*55fea89dSDan Cross 	    if ((retval = krb5_db_fetch_mkey(util_context, master_princ,
14807c478bd9Sstevel@tonic-gate 					     global_params.enctype,
1481*55fea89dSDan Cross 					     (new_mkey_file == 0) ?
1482*55fea89dSDan Cross 					        (krb5_boolean) 1 : 0,
1483*55fea89dSDan Cross 					     TRUE,
14847c478bd9Sstevel@tonic-gate 					     new_mkey_file, 0,
1485*55fea89dSDan Cross 					     &new_master_key))) {
1486dd9ccd46S 		    /* Solaris Kerberos */
1487dd9ccd46S 		    com_err(progname, retval,
14887c478bd9Sstevel@tonic-gate 			    gettext("while reading new master key"));
14897c478bd9Sstevel@tonic-gate 		    exit(1);
14907c478bd9Sstevel@tonic-gate 	    }
14917c478bd9Sstevel@tonic-gate     }
14927c478bd9Sstevel@tonic-gate 
14937c478bd9Sstevel@tonic-gate     kret = 0;
14947c478bd9Sstevel@tonic-gate     locked = 0;
14957c478bd9Sstevel@tonic-gate     if (ofile && strcmp(ofile, "-")) {
149656a424ccSmp 	/*
149756a424ccSmp 	 * Discourage accidental dumping to filenames beginning with '-'.
149856a424ccSmp 	 */
149956a424ccSmp 	if (ofile[0] == '-')
150056a424ccSmp 	    usage();
15017c478bd9Sstevel@tonic-gate 	/*
15027c478bd9Sstevel@tonic-gate 	 * Make sure that we don't open and truncate on the fopen,
15037c478bd9Sstevel@tonic-gate 	 * since that may hose an on-going kprop process.
1504*55fea89dSDan Cross 	 *
150556a424ccSmp 	 * We could also control this by opening for read and
150656a424ccSmp 	 * write, doing an flock with LOCK_EX, and then
150756a424ccSmp 	 * truncating the file once we have gotten the lock,
150856a424ccSmp 	 * but that would involve more OS dependencies than I
150956a424ccSmp 	 * want to get into.
15107c478bd9Sstevel@tonic-gate 	 */
15117c478bd9Sstevel@tonic-gate 	unlink(ofile);
15127c478bd9Sstevel@tonic-gate 	if (!(f = fopen(ofile, "w"))) {
1513dd9ccd46S 			/* Solaris Kerberos */
15147c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(ofopen_error),
1515dd9ccd46S 		    progname, ofile, error_message(errno));
15167c478bd9Sstevel@tonic-gate 	    exit_status++;
15177c478bd9Sstevel@tonic-gate 	    return;
15187c478bd9Sstevel@tonic-gate        }
15197c478bd9Sstevel@tonic-gate 	if ((kret = krb5_lock_file(util_context,
15207c478bd9Sstevel@tonic-gate 				   fileno(f),
15217c478bd9Sstevel@tonic-gate 				   KRB5_LOCKMODE_EXCLUSIVE))) {
1522dd9ccd46S 			/* Solaris Kerberos */
15237c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(oflock_error),
1524dd9ccd46S 		    progname, ofile, error_message(kret));
15257c478bd9Sstevel@tonic-gate 	    exit_status++;
152656a424ccSmp 	}
152756a424ccSmp 	else
15287c478bd9Sstevel@tonic-gate 	    locked = 1;
15297c478bd9Sstevel@tonic-gate     } else {
15307c478bd9Sstevel@tonic-gate 	f = stdout;
15317c478bd9Sstevel@tonic-gate     }
15327c478bd9Sstevel@tonic-gate     if (f && !(kret)) {
1533dd9ccd46S 	/* Solaris Kerberos */
1534dd9ccd46S 	arglist.programname = progname;
15357c478bd9Sstevel@tonic-gate 	arglist.ofile = f;
15367c478bd9Sstevel@tonic-gate 	arglist.kcontext = util_context;
15377c478bd9Sstevel@tonic-gate 	fprintf(arglist.ofile, "%s", dump->header);
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate 	if (dump_sno) {
15407c478bd9Sstevel@tonic-gate 		if (ulog_map(util_context, &global_params, FKCOMMAND)) {
1541dd9ccd46S 			/* Solaris Kerberos */
15427c478bd9Sstevel@tonic-gate 			fprintf(stderr,
1543dd9ccd46S 			    gettext("%s: Could not map log\n"), progname);
15447c478bd9Sstevel@tonic-gate 			exit_status++;
15457c478bd9Sstevel@tonic-gate 			goto error;
15467c478bd9Sstevel@tonic-gate 		}
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 		/*
15497c478bd9Sstevel@tonic-gate 		 * We grab the lock twice (once again in the iterator call),
15507c478bd9Sstevel@tonic-gate 		 * but that's ok since the lock func handles incr locks held.
15517c478bd9Sstevel@tonic-gate 		 */
15527c478bd9Sstevel@tonic-gate 		if (krb5_db_lock(util_context, KRB5_LOCKMODE_SHARED)) {
1553dd9ccd46S 			/* Solaris Kerberos */
15547c478bd9Sstevel@tonic-gate 			fprintf(stderr,
1555dd9ccd46S 			    gettext("%s: Couldn't grab lock\n"), progname);
15567c478bd9Sstevel@tonic-gate 			exit_status++;
15577c478bd9Sstevel@tonic-gate 			goto error;
15587c478bd9Sstevel@tonic-gate 		}
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate 		fprintf(f, " %u", log_ctx->ulog->kdb_last_sno);
15617c478bd9Sstevel@tonic-gate 		fprintf(f, " %u", log_ctx->ulog->kdb_last_time.seconds);
15627c478bd9Sstevel@tonic-gate 		fprintf(f, " %u", log_ctx->ulog->kdb_last_time.useconds);
15637c478bd9Sstevel@tonic-gate 	}
15647c478bd9Sstevel@tonic-gate 
15657c478bd9Sstevel@tonic-gate 	if (dump->header[strlen(dump->header)-1] != '\n')
15667c478bd9Sstevel@tonic-gate 	     fputc('\n', arglist.ofile);
1567*55fea89dSDan Cross 
15682dd2efa5Swillf 	/* Solaris Kerberos: adding support for -rev/recurse flags */
15692dd2efa5Swillf 	/* don't pass in db_args if there aren't any */
157054925bf6Swillf 	if ((kret = krb5_db_iterate(util_context,
157154925bf6Swillf 				    NULL,
157254925bf6Swillf 				    dump->dump_princ,
15732dd2efa5Swillf 				    (krb5_pointer) &arglist,
15742dd2efa5Swillf 				    db_arg_index > 0 ? (char **)&db_args : NULL))) {
1575dd9ccd46S 	     /* Solaris Kerberos */
157654925bf6Swillf 	     fprintf(stderr, dumprec_err,
1577dd9ccd46S 		     progname, dump->name, error_message(kret));
15787c478bd9Sstevel@tonic-gate 	     exit_status++;
15797c478bd9Sstevel@tonic-gate 		if (dump_sno)
15807c478bd9Sstevel@tonic-gate 			(void) krb5_db_unlock(util_context);
15817c478bd9Sstevel@tonic-gate 	}
15827c478bd9Sstevel@tonic-gate 	if (dump->dump_policy &&
158354925bf6Swillf 	    (kret = krb5_db_iter_policy( util_context, "*", dump->dump_policy,
1584*55fea89dSDan Cross 					 &arglist))) {
1585dd9ccd46S 	     /* Solaris Kerberos */
158654925bf6Swillf 	     fprintf(stderr, gettext(dumprec_err),
1587dd9ccd46S 		progname, dump->name,
15887c478bd9Sstevel@tonic-gate 		     error_message(kret));
15897c478bd9Sstevel@tonic-gate 	     exit_status++;
15907c478bd9Sstevel@tonic-gate 	}
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate error:
15937c478bd9Sstevel@tonic-gate 	if (ofile && f != stdout && !exit_status) {
159454925bf6Swillf 	    if (locked) {
159554925bf6Swillf 		(void) krb5_lock_file(util_context, fileno(f), KRB5_LOCKMODE_UNLOCK);
159654925bf6Swillf 		locked = 0;
159754925bf6Swillf 	    }
159854925bf6Swillf 	    fclose(f);
159954925bf6Swillf 	    update_ok_file(ofile);
16007c478bd9Sstevel@tonic-gate 	}
16017c478bd9Sstevel@tonic-gate     }
16027c478bd9Sstevel@tonic-gate     if (locked)
160356a424ccSmp 	(void) krb5_lock_file(util_context, fileno(f), KRB5_LOCKMODE_UNLOCK);
16047c478bd9Sstevel@tonic-gate }
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate /*
16077c478bd9Sstevel@tonic-gate  * Read a string of bytes while counting the number of lines passed.
16087c478bd9Sstevel@tonic-gate  */
16097c478bd9Sstevel@tonic-gate static int
read_string(f,buf,len,lp)16107c478bd9Sstevel@tonic-gate read_string(f, buf, len, lp)
16117c478bd9Sstevel@tonic-gate     FILE	*f;
16127c478bd9Sstevel@tonic-gate     char	*buf;
16137c478bd9Sstevel@tonic-gate     int		len;
16147c478bd9Sstevel@tonic-gate     int		*lp;
16157c478bd9Sstevel@tonic-gate {
16167c478bd9Sstevel@tonic-gate     int c;
16177c478bd9Sstevel@tonic-gate     int i, retval;
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate     retval = 0;
16207c478bd9Sstevel@tonic-gate     for (i=0; i<len; i++) {
16217c478bd9Sstevel@tonic-gate 	c = fgetc(f);
16227c478bd9Sstevel@tonic-gate 	if (c < 0) {
16237c478bd9Sstevel@tonic-gate 	    retval = 1;
16247c478bd9Sstevel@tonic-gate 	    break;
16257c478bd9Sstevel@tonic-gate 	}
16267c478bd9Sstevel@tonic-gate 	if (c == '\n')
16277c478bd9Sstevel@tonic-gate 	    (*lp)++;
16287c478bd9Sstevel@tonic-gate 	buf[i] = (char) c;
16297c478bd9Sstevel@tonic-gate     }
16307c478bd9Sstevel@tonic-gate     buf[len] = '\0';
16317c478bd9Sstevel@tonic-gate     return(retval);
16327c478bd9Sstevel@tonic-gate }
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate /*
16357c478bd9Sstevel@tonic-gate  * Read a string of two character representations of bytes.
16367c478bd9Sstevel@tonic-gate  */
16377c478bd9Sstevel@tonic-gate static int
read_octet_string(f,buf,len)16387c478bd9Sstevel@tonic-gate read_octet_string(f, buf, len)
16397c478bd9Sstevel@tonic-gate     FILE	*f;
16407c478bd9Sstevel@tonic-gate     krb5_octet	*buf;
16417c478bd9Sstevel@tonic-gate     int		len;
16427c478bd9Sstevel@tonic-gate {
16437c478bd9Sstevel@tonic-gate     int c;
16447c478bd9Sstevel@tonic-gate     int i, retval;
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate     retval = 0;
16477c478bd9Sstevel@tonic-gate     for (i=0; i<len; i++) {
16487c478bd9Sstevel@tonic-gate 	if (fscanf(f, "%02x", &c) != 1) {
16497c478bd9Sstevel@tonic-gate 	    retval = 1;
16507c478bd9Sstevel@tonic-gate 	    break;
16517c478bd9Sstevel@tonic-gate 	}
16527c478bd9Sstevel@tonic-gate 	buf[i] = (krb5_octet) c;
16537c478bd9Sstevel@tonic-gate     }
16547c478bd9Sstevel@tonic-gate     return(retval);
16557c478bd9Sstevel@tonic-gate }
16567c478bd9Sstevel@tonic-gate 
16577c478bd9Sstevel@tonic-gate /*
16587c478bd9Sstevel@tonic-gate  * Find the end of an old format record.
16597c478bd9Sstevel@tonic-gate  */
16607c478bd9Sstevel@tonic-gate static void
find_record_end(f,fn,lineno)16617c478bd9Sstevel@tonic-gate find_record_end(f, fn, lineno)
16627c478bd9Sstevel@tonic-gate     FILE	*f;
16637c478bd9Sstevel@tonic-gate     char	*fn;
16647c478bd9Sstevel@tonic-gate     int		lineno;
16657c478bd9Sstevel@tonic-gate {
16667c478bd9Sstevel@tonic-gate     int	ch;
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate     if (((ch = fgetc(f)) != ';') || ((ch = fgetc(f)) != '\n')) {
16697c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(trash_end_fmt), fn, lineno);
16707c478bd9Sstevel@tonic-gate 	while (ch != '\n') {
16717c478bd9Sstevel@tonic-gate 	    putc(ch, stderr);
16727c478bd9Sstevel@tonic-gate 	    ch = fgetc(f);
16737c478bd9Sstevel@tonic-gate 	}
16747c478bd9Sstevel@tonic-gate 	putc(ch, stderr);
16757c478bd9Sstevel@tonic-gate     }
16767c478bd9Sstevel@tonic-gate }
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate #if 0
16797c478bd9Sstevel@tonic-gate /*
16807c478bd9Sstevel@tonic-gate  * update_tl_data()	- Generate the tl_data entries.
16817c478bd9Sstevel@tonic-gate  */
16827c478bd9Sstevel@tonic-gate static krb5_error_code
16837c478bd9Sstevel@tonic-gate update_tl_data(kcontext, dbentp, mod_name, mod_date, last_pwd_change)
16847c478bd9Sstevel@tonic-gate     krb5_context	kcontext;
16857c478bd9Sstevel@tonic-gate     krb5_db_entry	*dbentp;
16867c478bd9Sstevel@tonic-gate     krb5_principal	mod_name;
16877c478bd9Sstevel@tonic-gate     krb5_timestamp	mod_date;
16887c478bd9Sstevel@tonic-gate     krb5_timestamp	last_pwd_change;
16897c478bd9Sstevel@tonic-gate {
16907c478bd9Sstevel@tonic-gate     krb5_error_code	kret;
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate     kret = 0 ;
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate     /*
16957c478bd9Sstevel@tonic-gate      * Handle modification principal.
16967c478bd9Sstevel@tonic-gate      */
16977c478bd9Sstevel@tonic-gate     if (mod_name) {
16987c478bd9Sstevel@tonic-gate 	krb5_tl_mod_princ	mprinc;
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate 	memset(&mprinc, 0, sizeof(mprinc));
17017c478bd9Sstevel@tonic-gate 	if (!(kret = krb5_copy_principal(kcontext,
17027c478bd9Sstevel@tonic-gate 					 mod_name,
17037c478bd9Sstevel@tonic-gate 					 &mprinc.mod_princ))) {
17047c478bd9Sstevel@tonic-gate 	    mprinc.mod_date = mod_date;
17057c478bd9Sstevel@tonic-gate 	    kret = krb5_dbe_encode_mod_princ_data(kcontext,
17067c478bd9Sstevel@tonic-gate 						  &mprinc,
17077c478bd9Sstevel@tonic-gate 						  dbentp);
17087c478bd9Sstevel@tonic-gate 	}
17097c478bd9Sstevel@tonic-gate 	if (mprinc.mod_princ)
17107c478bd9Sstevel@tonic-gate 	    krb5_free_principal(kcontext, mprinc.mod_princ);
17117c478bd9Sstevel@tonic-gate     }
171256a424ccSmp 
17137c478bd9Sstevel@tonic-gate     /*
17147c478bd9Sstevel@tonic-gate      * Handle last password change.
17157c478bd9Sstevel@tonic-gate      */
17167c478bd9Sstevel@tonic-gate     if (!kret) {
17177c478bd9Sstevel@tonic-gate 	krb5_tl_data	*pwchg;
17187c478bd9Sstevel@tonic-gate 	krb5_boolean	linked;
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 	/* Find a previously existing entry */
17217c478bd9Sstevel@tonic-gate 	for (pwchg = dbentp->tl_data;
17227c478bd9Sstevel@tonic-gate 	     (pwchg) && (pwchg->tl_data_type != KRB5_TL_LAST_PWD_CHANGE);
17237c478bd9Sstevel@tonic-gate 	     pwchg = pwchg->tl_data_next);
17247c478bd9Sstevel@tonic-gate 
17257c478bd9Sstevel@tonic-gate 	/* Check to see if we found one. */
17267c478bd9Sstevel@tonic-gate 	linked = 0;
17277c478bd9Sstevel@tonic-gate 	if (!pwchg) {
17287c478bd9Sstevel@tonic-gate 	    /* No, allocate a new one */
172956a424ccSmp 	    if ((pwchg = (krb5_tl_data *) malloc(sizeof(krb5_tl_data)))) {
173056a424ccSmp 		memset(pwchg, 0, sizeof(krb5_tl_data));
173156a424ccSmp 		if (!(pwchg->tl_data_contents =
173256a424ccSmp 		      (krb5_octet *) malloc(sizeof(krb5_timestamp)))) {
173356a424ccSmp 		    free(pwchg);
173456a424ccSmp 		    pwchg = (krb5_tl_data *) NULL;
173556a424ccSmp 		}
173656a424ccSmp 		else {
173756a424ccSmp 		    pwchg->tl_data_type = KRB5_TL_LAST_PWD_CHANGE;
173856a424ccSmp 		    pwchg->tl_data_length =
173956a424ccSmp 			(krb5_int16) sizeof(krb5_timestamp);
174056a424ccSmp 		}
17417c478bd9Sstevel@tonic-gate 	    }
174256a424ccSmp 	}
174356a424ccSmp 	else
174456a424ccSmp 	    linked = 1;
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 	/* Do we have an entry? */
17477c478bd9Sstevel@tonic-gate 	if (pwchg && pwchg->tl_data_contents) {
17487c478bd9Sstevel@tonic-gate 	    /* Encode it */
174956a424ccSmp 	    krb5_kdb_encode_int32(last_pwd_change, pwchg->tl_data_contents);
17507c478bd9Sstevel@tonic-gate 	    /* Link it in if necessary */
17517c478bd9Sstevel@tonic-gate 	    if (!linked) {
17527c478bd9Sstevel@tonic-gate 		pwchg->tl_data_next = dbentp->tl_data;
17537c478bd9Sstevel@tonic-gate 		dbentp->tl_data = pwchg;
17547c478bd9Sstevel@tonic-gate 		dbentp->n_tl_data++;
17557c478bd9Sstevel@tonic-gate 	    }
175656a424ccSmp 	}
175756a424ccSmp 	else
17587c478bd9Sstevel@tonic-gate 	    kret = ENOMEM;
17597c478bd9Sstevel@tonic-gate     }
176056a424ccSmp 
17617c478bd9Sstevel@tonic-gate     return(kret);
17627c478bd9Sstevel@tonic-gate }
17637c478bd9Sstevel@tonic-gate #endif
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate /*
17667c478bd9Sstevel@tonic-gate  * process_k5beta_record()	- Handle a dump record in old format.
17677c478bd9Sstevel@tonic-gate  *
17687c478bd9Sstevel@tonic-gate  * Returns -1 for end of file, 0 for success and 1 for failure.
17697c478bd9Sstevel@tonic-gate  */
17707c478bd9Sstevel@tonic-gate static int
process_k5beta_record(fname,kcontext,filep,verbose,linenop)177154925bf6Swillf process_k5beta_record(fname, kcontext, filep, verbose, linenop)
17727c478bd9Sstevel@tonic-gate     char		*fname;
17737c478bd9Sstevel@tonic-gate     krb5_context	kcontext;
17747c478bd9Sstevel@tonic-gate     FILE		*filep;
17757c478bd9Sstevel@tonic-gate     int			verbose;
17767c478bd9Sstevel@tonic-gate     int			*linenop;
17777c478bd9Sstevel@tonic-gate {
17787c478bd9Sstevel@tonic-gate     int			nmatched;
17797c478bd9Sstevel@tonic-gate     int			retval;
17807c478bd9Sstevel@tonic-gate     krb5_db_entry	dbent;
17817c478bd9Sstevel@tonic-gate     int			name_len, mod_name_len, key_len;
17827c478bd9Sstevel@tonic-gate     int			alt_key_len, salt_len, alt_salt_len;
17837c478bd9Sstevel@tonic-gate     char		*name;
17847c478bd9Sstevel@tonic-gate     char		*mod_name;
17857c478bd9Sstevel@tonic-gate     int			tmpint1, tmpint2, tmpint3;
17867c478bd9Sstevel@tonic-gate     int			error;
17877c478bd9Sstevel@tonic-gate     const char		*try2read;
17887c478bd9Sstevel@tonic-gate     int			i;
17897c478bd9Sstevel@tonic-gate     krb5_key_data	*pkey, *akey;
17907c478bd9Sstevel@tonic-gate     krb5_timestamp	last_pwd_change, mod_date;
17917c478bd9Sstevel@tonic-gate     krb5_principal	mod_princ;
17927c478bd9Sstevel@tonic-gate     krb5_error_code	kret;
17937c478bd9Sstevel@tonic-gate     krb5_octet 		*shortcopy1 = NULL; /* SUNWresync121 memleak fix */
17947c478bd9Sstevel@tonic-gate     krb5_octet 		*shortcopy2 = NULL;
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate     try2read = (char *) NULL;
17977c478bd9Sstevel@tonic-gate     (*linenop)++;
17987c478bd9Sstevel@tonic-gate     retval = 1;
17997c478bd9Sstevel@tonic-gate     memset((char *)&dbent, 0, sizeof(dbent));
18007c478bd9Sstevel@tonic-gate 
18017c478bd9Sstevel@tonic-gate     /* Make sure we've got key_data entries */
18027c478bd9Sstevel@tonic-gate     if (krb5_dbe_create_key_data(kcontext, &dbent) ||
18037c478bd9Sstevel@tonic-gate 	krb5_dbe_create_key_data(kcontext, &dbent)) {
18047c478bd9Sstevel@tonic-gate 	krb5_db_free_principal(kcontext, &dbent, 1);
18057c478bd9Sstevel@tonic-gate 	return(1);
18067c478bd9Sstevel@tonic-gate     }
18077c478bd9Sstevel@tonic-gate     pkey = &dbent.key_data[0];
18087c478bd9Sstevel@tonic-gate     akey = &dbent.key_data[1];
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate     /*
18117c478bd9Sstevel@tonic-gate      * Match the sizes.  6 tokens to match.
18127c478bd9Sstevel@tonic-gate      */
18137c478bd9Sstevel@tonic-gate     nmatched = fscanf(filep, "%d\t%d\t%d\t%d\t%d\t%d\t",
18147c478bd9Sstevel@tonic-gate 		      &name_len, &mod_name_len, &key_len,
18157c478bd9Sstevel@tonic-gate 		      &alt_key_len, &salt_len, &alt_salt_len);
18167c478bd9Sstevel@tonic-gate     if (nmatched == 6) {
18177c478bd9Sstevel@tonic-gate         pkey->key_data_length[0] = key_len;
18187c478bd9Sstevel@tonic-gate 	akey->key_data_length[0] = alt_key_len;
18197c478bd9Sstevel@tonic-gate 	pkey->key_data_length[1] = salt_len;
18207c478bd9Sstevel@tonic-gate 	akey->key_data_length[1] = alt_salt_len;
18217c478bd9Sstevel@tonic-gate 	name = (char *) NULL;
18227c478bd9Sstevel@tonic-gate 	mod_name = (char *) NULL;
18237c478bd9Sstevel@tonic-gate 	/*
18247c478bd9Sstevel@tonic-gate 	 * Get the memory for the variable length fields.
18257c478bd9Sstevel@tonic-gate 	 */
18267c478bd9Sstevel@tonic-gate 	if ((name = (char *) malloc((size_t) (name_len + 1))) &&
18277c478bd9Sstevel@tonic-gate 	    (mod_name = (char *) malloc((size_t) (mod_name_len + 1))) &&
18287c478bd9Sstevel@tonic-gate 	    (!key_len ||
1829*55fea89dSDan Cross 	     (pkey->key_data_contents[0] =
18307c478bd9Sstevel@tonic-gate 	      (krb5_octet *) malloc((size_t) (key_len + 1)))) &&
18317c478bd9Sstevel@tonic-gate 	    (!alt_key_len ||
1832*55fea89dSDan Cross 	     (akey->key_data_contents[0] =
183356a424ccSmp 	      (krb5_octet *) malloc((size_t) (alt_key_len + 1)))) &&
18347c478bd9Sstevel@tonic-gate 	    (!salt_len ||
1835*55fea89dSDan Cross 	     (pkey->key_data_contents[1] =
18367c478bd9Sstevel@tonic-gate 	      (krb5_octet *) malloc((size_t) (salt_len + 1)))) &&
18377c478bd9Sstevel@tonic-gate 	    (!alt_salt_len ||
1838*55fea89dSDan Cross 	     (akey->key_data_contents[1] =
183956a424ccSmp 	      (krb5_octet *) malloc((size_t) (alt_salt_len + 1))))
184056a424ccSmp 	    ) {
18417c478bd9Sstevel@tonic-gate 	    error = 0;
18427c478bd9Sstevel@tonic-gate 
18437c478bd9Sstevel@tonic-gate 	    /* Read the principal name */
18447c478bd9Sstevel@tonic-gate 	    if (read_string(filep, name, name_len, linenop)) {
18457c478bd9Sstevel@tonic-gate 		try2read = read_name_string;
18467c478bd9Sstevel@tonic-gate 		error++;
18477c478bd9Sstevel@tonic-gate 	    }
18487c478bd9Sstevel@tonic-gate 	    /* Read the key type */
184956a424ccSmp 	    if (!error && (fscanf(filep, "\t%d\t", &tmpint1) != 1)) {
185056a424ccSmp 		try2read = read_key_type;
185156a424ccSmp 		error++;
18527c478bd9Sstevel@tonic-gate 	    }
18537c478bd9Sstevel@tonic-gate 	    pkey->key_data_type[0] = tmpint1;
18547c478bd9Sstevel@tonic-gate 	    /* Read the old format key */
18557c478bd9Sstevel@tonic-gate 	    if (!error && read_octet_string(filep,
18567c478bd9Sstevel@tonic-gate 					    pkey->key_data_contents[0],
18577c478bd9Sstevel@tonic-gate 					    pkey->key_data_length[0])) {
18587c478bd9Sstevel@tonic-gate 		try2read = read_key_data;
18597c478bd9Sstevel@tonic-gate 		error++;
18607c478bd9Sstevel@tonic-gate 	    }
18617c478bd9Sstevel@tonic-gate 	    /* convert to a new format key */
186256a424ccSmp 	    /* the encrypted version is stored as the unencrypted key length
186356a424ccSmp 	       (4 bytes, MSB first) followed by the encrypted key. */
186456a424ccSmp 	    if ((pkey->key_data_length[0] > 4)
186556a424ccSmp 		&& (pkey->key_data_contents[0][0] == 0)
186656a424ccSmp 		&& (pkey->key_data_contents[0][1] == 0)) {
186756a424ccSmp 	      /* this really does look like an old key, so drop and swap */
186856a424ccSmp 	      /* the *new* length is 2 bytes, LSB first, sigh. */
186956a424ccSmp 	      size_t shortlen = pkey->key_data_length[0]-4+2;
187056a424ccSmp 	      krb5_octet *origdata = pkey->key_data_contents[0];
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate 		    shortcopy1 = (krb5_octet *) malloc(shortlen);
18737c478bd9Sstevel@tonic-gate 		    if (shortcopy1) {
18747c478bd9Sstevel@tonic-gate 			shortcopy1[0] = origdata[3];
18757c478bd9Sstevel@tonic-gate 			shortcopy1[1] = origdata[2];
18767c478bd9Sstevel@tonic-gate 			memcpy(shortcopy1 + 2, origdata + 4, shortlen - 2);
18777c478bd9Sstevel@tonic-gate 			free(origdata);
18787c478bd9Sstevel@tonic-gate 			pkey->key_data_length[0] = shortlen;
18797c478bd9Sstevel@tonic-gate 			pkey->key_data_contents[0] = shortcopy1;
18807c478bd9Sstevel@tonic-gate 		    } else {
18817c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(no_mem_fmt), fname, *linenop);
18827c478bd9Sstevel@tonic-gate 			error++;
18837c478bd9Sstevel@tonic-gate 		    }
18847c478bd9Sstevel@tonic-gate 	    }
1885*55fea89dSDan Cross 
18867c478bd9Sstevel@tonic-gate 	    /* Read principal attributes */
188756a424ccSmp 	    if (!error && (fscanf(filep,
188856a424ccSmp 				  "\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\t",
188956a424ccSmp 				  &tmpint1, &dbent.max_life,
189056a424ccSmp 				  &dbent.max_renewable_life,
189156a424ccSmp 				  &tmpint2, &dbent.expiration,
189256a424ccSmp 				  &dbent.pw_expiration, &last_pwd_change,
189356a424ccSmp 				  &dbent.last_success, &dbent.last_failed,
189456a424ccSmp 				  &tmpint3) != 10)) {
189556a424ccSmp 		try2read = read_pr_data1;
189656a424ccSmp 		error++;
18977c478bd9Sstevel@tonic-gate 	    }
18987c478bd9Sstevel@tonic-gate 	    pkey->key_data_kvno = tmpint1;
18997c478bd9Sstevel@tonic-gate 	    dbent.fail_auth_count = tmpint3;
19007c478bd9Sstevel@tonic-gate 	    /* Read modifier name */
19017c478bd9Sstevel@tonic-gate 	    if (!error && read_string(filep,
19027c478bd9Sstevel@tonic-gate 				      mod_name,
19037c478bd9Sstevel@tonic-gate 				      mod_name_len,
19047c478bd9Sstevel@tonic-gate 				      linenop)) {
19057c478bd9Sstevel@tonic-gate 		try2read = read_mod_name;
19067c478bd9Sstevel@tonic-gate 		error++;
19077c478bd9Sstevel@tonic-gate 	    }
19087c478bd9Sstevel@tonic-gate 	    /* Read second set of attributes */
19097c478bd9Sstevel@tonic-gate 	    if (!error && (fscanf(filep, "\t%u\t%u\t%u\t",
19107c478bd9Sstevel@tonic-gate 				  &mod_date, &dbent.attributes,
19117c478bd9Sstevel@tonic-gate 				  &tmpint1) != 3)) {
19127c478bd9Sstevel@tonic-gate 		try2read = read_pr_data2;
19137c478bd9Sstevel@tonic-gate 		error++;
19147c478bd9Sstevel@tonic-gate 	    }
19157c478bd9Sstevel@tonic-gate 	    pkey->key_data_type[1] = tmpint1;
19167c478bd9Sstevel@tonic-gate 	    /* Read salt data */
19177c478bd9Sstevel@tonic-gate 	    if (!error && read_octet_string(filep,
19187c478bd9Sstevel@tonic-gate 					    pkey->key_data_contents[1],
19197c478bd9Sstevel@tonic-gate 					    pkey->key_data_length[1])) {
19207c478bd9Sstevel@tonic-gate 		try2read = read_salt_data;
19217c478bd9Sstevel@tonic-gate 		error++;
19227c478bd9Sstevel@tonic-gate 	    }
19237c478bd9Sstevel@tonic-gate 	    /* Read alternate key type */
192456a424ccSmp 	    if (!error && (fscanf(filep, "\t%u\t", &tmpint1) != 1)) {
192556a424ccSmp 		try2read = read_akey_type;
192656a424ccSmp 		error++;
19277c478bd9Sstevel@tonic-gate 	    }
19287c478bd9Sstevel@tonic-gate 	    akey->key_data_type[0] = tmpint1;
19297c478bd9Sstevel@tonic-gate 	    /* Read alternate key */
19307c478bd9Sstevel@tonic-gate 	    if (!error && read_octet_string(filep,
19317c478bd9Sstevel@tonic-gate 					    akey->key_data_contents[0],
19327c478bd9Sstevel@tonic-gate 					    akey->key_data_length[0])) {
193356a424ccSmp 		try2read = read_akey_data;
193456a424ccSmp 		error++;
19357c478bd9Sstevel@tonic-gate 	    }
193656a424ccSmp 
19377c478bd9Sstevel@tonic-gate 	    /* convert to a new format key */
193856a424ccSmp 	    /* the encrypted version is stored as the unencrypted key length
193956a424ccSmp 	       (4 bytes, MSB first) followed by the encrypted key. */
194056a424ccSmp 	    if ((akey->key_data_length[0] > 4)
194156a424ccSmp 		&& (akey->key_data_contents[0][0] == 0)
194256a424ccSmp 		&& (akey->key_data_contents[0][1] == 0)) {
194356a424ccSmp 	      /* this really does look like an old key, so drop and swap */
194456a424ccSmp 	      /* the *new* length is 2 bytes, LSB first, sigh. */
194556a424ccSmp 	      size_t shortlen = akey->key_data_length[0]-4+2;
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate 		    krb5_octet *origdata = akey->key_data_contents[0];
19487c478bd9Sstevel@tonic-gate 
19497c478bd9Sstevel@tonic-gate 		    shortcopy2 = (krb5_octet *) malloc(shortlen);
19507c478bd9Sstevel@tonic-gate 		    if (shortcopy2) {
19517c478bd9Sstevel@tonic-gate 			shortcopy2[0] = origdata[3];
19527c478bd9Sstevel@tonic-gate 			shortcopy2[1] = origdata[2];
19537c478bd9Sstevel@tonic-gate 			memcpy(shortcopy2 + 2,
19547c478bd9Sstevel@tonic-gate 			    origdata + 4, shortlen - 2);
19557c478bd9Sstevel@tonic-gate 			free(origdata);
19567c478bd9Sstevel@tonic-gate 			akey->key_data_length[0] = shortlen;
19577c478bd9Sstevel@tonic-gate 			akey->key_data_contents[0] = shortcopy2;
19587c478bd9Sstevel@tonic-gate 		    } else {
19597c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(no_mem_fmt), fname, *linenop);
19607c478bd9Sstevel@tonic-gate 			error++;
19617c478bd9Sstevel@tonic-gate 		    }
19627c478bd9Sstevel@tonic-gate 	    }
1963*55fea89dSDan Cross 
19647c478bd9Sstevel@tonic-gate 	    /* Read alternate salt type */
196556a424ccSmp 	    if (!error && (fscanf(filep, "\t%u\t", &tmpint1) != 1)) {
196656a424ccSmp 		try2read = read_asalt_type;
196756a424ccSmp 		error++;
19687c478bd9Sstevel@tonic-gate 	    }
19697c478bd9Sstevel@tonic-gate 	    akey->key_data_type[1] = tmpint1;
19707c478bd9Sstevel@tonic-gate 	    /* Read alternate salt data */
19717c478bd9Sstevel@tonic-gate 	    if (!error && read_octet_string(filep,
19727c478bd9Sstevel@tonic-gate 					    akey->key_data_contents[1],
19737c478bd9Sstevel@tonic-gate 					    akey->key_data_length[1])) {
19747c478bd9Sstevel@tonic-gate 		try2read = read_asalt_data;
19757c478bd9Sstevel@tonic-gate 		error++;
19767c478bd9Sstevel@tonic-gate 	    }
19777c478bd9Sstevel@tonic-gate 	    /* Read expansion data - discard it */
19787c478bd9Sstevel@tonic-gate 	    if (!error) {
19797c478bd9Sstevel@tonic-gate 		for (i=0; i<8; i++) {
198056a424ccSmp 		    if (fscanf(filep, "\t%u", &tmpint1) != 1) {
19817c478bd9Sstevel@tonic-gate 			try2read = read_exp_data;
19827c478bd9Sstevel@tonic-gate 			error++;
19837c478bd9Sstevel@tonic-gate 			break;
198456a424ccSmp 		    }
19857c478bd9Sstevel@tonic-gate 		}
19867c478bd9Sstevel@tonic-gate 		if (!error)
19877c478bd9Sstevel@tonic-gate 		    find_record_end(filep, fname, *linenop);
19887c478bd9Sstevel@tonic-gate 	    }
1989*55fea89dSDan Cross 
19907c478bd9Sstevel@tonic-gate 	    /*
199156a424ccSmp 	     * If no error, then we're done reading.  Now parse the names
199256a424ccSmp 	     * and store the database dbent.
19937c478bd9Sstevel@tonic-gate 	     */
19947c478bd9Sstevel@tonic-gate 	    if (!error) {
199556a424ccSmp 		if (!(kret = krb5_parse_name(kcontext,
199656a424ccSmp 					     name,
199756a424ccSmp 					     &dbent.princ))) {
199856a424ccSmp 		    if (!(kret = krb5_parse_name(kcontext,
199956a424ccSmp 						 mod_name,
200056a424ccSmp 						 &mod_princ))) {
200156a424ccSmp 			if (!(kret =
200256a424ccSmp 			      krb5_dbe_update_mod_princ_data(kcontext,
200356a424ccSmp 							     &dbent,
200456a424ccSmp 							     mod_date,
200556a424ccSmp 							     mod_princ)) &&
200656a424ccSmp 			    !(kret =
200756a424ccSmp 			      krb5_dbe_update_last_pwd_change(kcontext,
200856a424ccSmp 							      &dbent,
200956a424ccSmp 							      last_pwd_change))) {
201056a424ccSmp 			    int one = 1;
201156a424ccSmp 
201256a424ccSmp 			    dbent.len = KRB5_KDB_V1_BASE_LENGTH;
201356a424ccSmp 			    pkey->key_data_ver = (pkey->key_data_type[1] || pkey->key_data_length[1]) ?
201456a424ccSmp 				2 : 1;
201556a424ccSmp 			    akey->key_data_ver = (akey->key_data_type[1] || akey->key_data_length[1]) ?
201656a424ccSmp 				2 : 1;
201756a424ccSmp 			    if ((pkey->key_data_type[0] ==
201856a424ccSmp 				 akey->key_data_type[0]) &&
201956a424ccSmp 				(pkey->key_data_type[1] ==
202056a424ccSmp 				 akey->key_data_type[1]))
202156a424ccSmp 				dbent.n_key_data--;
202256a424ccSmp 			    else if ((akey->key_data_type[0] == 0)
202356a424ccSmp 				     && (akey->key_data_length[0] == 0)
202456a424ccSmp 				     && (akey->key_data_type[1] == 0)
202556a424ccSmp 				     && (akey->key_data_length[1] == 0))
202656a424ccSmp 			        dbent.n_key_data--;
202754925bf6Swillf 
202854925bf6Swillf 			    dbent.mask = KADM5_LOAD | KADM5_PRINCIPAL | KADM5_ATTRIBUTES |
202954925bf6Swillf 				KADM5_MAX_LIFE | KADM5_MAX_RLIFE | KADM5_KEY_DATA |
203054925bf6Swillf 				KADM5_PRINC_EXPIRE_TIME | KADM5_LAST_SUCCESS |
203154925bf6Swillf 				KADM5_LAST_FAILED | KADM5_FAIL_AUTH_COUNT;
203254925bf6Swillf 
203356a424ccSmp 			    if ((kret = krb5_db_put_principal(kcontext,
203456a424ccSmp 							      &dbent,
203556a424ccSmp 							      &one)) ||
203656a424ccSmp 				(one != 1)) {
203756a424ccSmp 				fprintf(stderr, gettext(store_err_fmt),
203856a424ccSmp 					fname, *linenop, name,
203956a424ccSmp 					error_message(kret));
204056a424ccSmp 				error++;
204156a424ccSmp 			    }
204256a424ccSmp 			    else {
204356a424ccSmp 				if (verbose)
204456a424ccSmp 				    fprintf(stderr,
204556a424ccSmp 							gettext(add_princ_fmt),
204656a424ccSmp 							name);
204756a424ccSmp 				retval = 0;
204856a424ccSmp 			    }
204956a424ccSmp 			    dbent.n_key_data = 2;
205056a424ccSmp 			}
205156a424ccSmp 			krb5_free_principal(kcontext, mod_princ);
205256a424ccSmp 		    }
205356a424ccSmp 		    else {
205456a424ccSmp 			fprintf(stderr,
205556a424ccSmp 				gettext(parse_err_fmt),
205656a424ccSmp 				fname, *linenop, mod_name,
205756a424ccSmp 				error_message(kret));
205856a424ccSmp 			error++;
205956a424ccSmp 		    }
206056a424ccSmp 		}
206156a424ccSmp 		else {
206256a424ccSmp 		    fprintf(stderr, gettext(parse_err_fmt),
206356a424ccSmp 			    fname, *linenop, name, error_message(kret));
206456a424ccSmp 		    error++;
206556a424ccSmp 		}
206656a424ccSmp 	    }
206756a424ccSmp 	    else {
206856a424ccSmp 	    fprintf(stderr, gettext(no_mem_fmt), fname, *linenop, try2read);
20697c478bd9Sstevel@tonic-gate 	    }
207056a424ccSmp 	}
207156a424ccSmp 	else {
207256a424ccSmp 		fprintf(stderr, gettext(read_err_fmt), fname, *linenop);
20737c478bd9Sstevel@tonic-gate 	}
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate 	krb5_db_free_principal(kcontext, &dbent, 1);
20767c478bd9Sstevel@tonic-gate 	if (mod_name)
20777c478bd9Sstevel@tonic-gate 	    free(mod_name);
20787c478bd9Sstevel@tonic-gate 	if (name)
20797c478bd9Sstevel@tonic-gate 	    free(name);
208056a424ccSmp     }
208156a424ccSmp     else {
20827c478bd9Sstevel@tonic-gate 	if (nmatched != EOF)
20837c478bd9Sstevel@tonic-gate 	   fprintf(stderr, gettext(rhead_err_fmt),
20847c478bd9Sstevel@tonic-gate 		fname, *linenop);
20857c478bd9Sstevel@tonic-gate 	else
208656a424ccSmp 	    retval = -1;
20877c478bd9Sstevel@tonic-gate     }
20887c478bd9Sstevel@tonic-gate 
20897c478bd9Sstevel@tonic-gate     if (shortcopy1)
20907c478bd9Sstevel@tonic-gate 	free(shortcopy1);
20917c478bd9Sstevel@tonic-gate     if (shortcopy2)
20927c478bd9Sstevel@tonic-gate 	free(shortcopy2);
20937c478bd9Sstevel@tonic-gate 
209456a424ccSmp     return(retval);
20957c478bd9Sstevel@tonic-gate }
20967c478bd9Sstevel@tonic-gate 
20977c478bd9Sstevel@tonic-gate /*
20987c478bd9Sstevel@tonic-gate  * process_k5beta6_record()	- Handle a dump record in krb5b6 format.
20997c478bd9Sstevel@tonic-gate  *
21007c478bd9Sstevel@tonic-gate  * Returns -1 for end of file, 0 for success and 1 for failure.
21017c478bd9Sstevel@tonic-gate  */
21027c478bd9Sstevel@tonic-gate static int
process_k5beta6_record(fname,kcontext,filep,verbose,linenop)210354925bf6Swillf process_k5beta6_record(fname, kcontext, filep, verbose, linenop)
21047c478bd9Sstevel@tonic-gate     char		*fname;
21057c478bd9Sstevel@tonic-gate     krb5_context	kcontext;
21067c478bd9Sstevel@tonic-gate     FILE		*filep;
21077c478bd9Sstevel@tonic-gate     int			verbose;
21087c478bd9Sstevel@tonic-gate     int			*linenop;
21097c478bd9Sstevel@tonic-gate {
21107c478bd9Sstevel@tonic-gate     int			retval;
21117c478bd9Sstevel@tonic-gate     krb5_db_entry	dbentry;
21127c478bd9Sstevel@tonic-gate     krb5_int32		t1, t2, t3, t4, t5, t6, t7, t8, t9;
21137c478bd9Sstevel@tonic-gate     int			nread;
21147c478bd9Sstevel@tonic-gate     int			error;
21157c478bd9Sstevel@tonic-gate     int			i, j, one;
21167c478bd9Sstevel@tonic-gate     char		*name;
21177c478bd9Sstevel@tonic-gate     krb5_key_data	*kp, *kdatap;
21187c478bd9Sstevel@tonic-gate     krb5_tl_data	**tlp, *tl;
21197c478bd9Sstevel@tonic-gate     krb5_octet 		*op;
21207c478bd9Sstevel@tonic-gate     krb5_error_code	kret;
21217c478bd9Sstevel@tonic-gate     const char		*try2read;
21227c478bd9Sstevel@tonic-gate 
21237c478bd9Sstevel@tonic-gate     try2read = (char *) NULL;
21247c478bd9Sstevel@tonic-gate     memset((char *) &dbentry, 0, sizeof(dbentry));
21257c478bd9Sstevel@tonic-gate     (*linenop)++;
21267c478bd9Sstevel@tonic-gate     retval = 1;
21277c478bd9Sstevel@tonic-gate     name = (char *) NULL;
21287c478bd9Sstevel@tonic-gate     kp = (krb5_key_data *) NULL;
21297c478bd9Sstevel@tonic-gate     op = (krb5_octet *) NULL;
21307c478bd9Sstevel@tonic-gate     error = 0;
21317c478bd9Sstevel@tonic-gate     kret = 0;
21327c478bd9Sstevel@tonic-gate     nread = fscanf(filep, "%d\t%d\t%d\t%d\t%d\t", &t1, &t2, &t3, &t4, &t5);
21337c478bd9Sstevel@tonic-gate     if (nread == 5) {
21347c478bd9Sstevel@tonic-gate 	/* Get memory for flattened principal name */
21357c478bd9Sstevel@tonic-gate 	if (!(name = (char *) malloc((size_t) t2 + 1)))
21367c478bd9Sstevel@tonic-gate 	    error++;
21377c478bd9Sstevel@tonic-gate 
21387c478bd9Sstevel@tonic-gate 	/* Get memory for and form tagged data linked list */
21397c478bd9Sstevel@tonic-gate 	tlp = &dbentry.tl_data;
21407c478bd9Sstevel@tonic-gate 	for (i=0; i<t3; i++) {
214156a424ccSmp 	    if ((*tlp = (krb5_tl_data *) malloc(sizeof(krb5_tl_data)))) {
21427c478bd9Sstevel@tonic-gate 		memset(*tlp, 0, sizeof(krb5_tl_data));
21437c478bd9Sstevel@tonic-gate 		tlp = &((*tlp)->tl_data_next);
21447c478bd9Sstevel@tonic-gate 		dbentry.n_tl_data++;
214556a424ccSmp 	    }
214656a424ccSmp 	    else {
21477c478bd9Sstevel@tonic-gate 		error++;
21487c478bd9Sstevel@tonic-gate 		break;
21497c478bd9Sstevel@tonic-gate 	    }
21507c478bd9Sstevel@tonic-gate 	}
21517c478bd9Sstevel@tonic-gate 
21527c478bd9Sstevel@tonic-gate 	/* Get memory for key list */
21537c478bd9Sstevel@tonic-gate 	if (t4 && !(kp = (krb5_key_data *) malloc((size_t)
21547c478bd9Sstevel@tonic-gate 						  (t4*sizeof(krb5_key_data)))))
21557c478bd9Sstevel@tonic-gate 	    error++;
21567c478bd9Sstevel@tonic-gate 
21577c478bd9Sstevel@tonic-gate 	/* Get memory for extra data */
21587c478bd9Sstevel@tonic-gate 	if (t5 && !(op = (krb5_octet *) malloc((size_t) t5)))
21597c478bd9Sstevel@tonic-gate 	    error++;
21607c478bd9Sstevel@tonic-gate 
21617c478bd9Sstevel@tonic-gate 	if (!error) {
21627c478bd9Sstevel@tonic-gate 	    dbentry.len = t1;
21637c478bd9Sstevel@tonic-gate 	    dbentry.n_key_data = t4;
21647c478bd9Sstevel@tonic-gate 	    dbentry.e_length = t5;
21657c478bd9Sstevel@tonic-gate 	    if (kp) {
216656a424ccSmp 		memset(kp, 0, (size_t) (t4*sizeof(krb5_key_data)));
21677c478bd9Sstevel@tonic-gate 		dbentry.key_data = kp;
21687c478bd9Sstevel@tonic-gate 		kp = (krb5_key_data *) NULL;
21697c478bd9Sstevel@tonic-gate 	    }
21707c478bd9Sstevel@tonic-gate 	    if (op) {
21717c478bd9Sstevel@tonic-gate 		memset(op, 0, (size_t) t5);
21727c478bd9Sstevel@tonic-gate 		dbentry.e_data = op;
21737c478bd9Sstevel@tonic-gate 		op = (krb5_octet *) NULL;
21747c478bd9Sstevel@tonic-gate 	    }
217556a424ccSmp 
21767c478bd9Sstevel@tonic-gate 	    /* Read in and parse the principal name */
21777c478bd9Sstevel@tonic-gate 	    if (!read_string(filep, name, t2, linenop) &&
217856a424ccSmp 		!(kret = krb5_parse_name(kcontext, name, &dbentry.princ))) {
21797c478bd9Sstevel@tonic-gate 
21807c478bd9Sstevel@tonic-gate 		/* Get the fixed principal attributes */
218156a424ccSmp 		nread = fscanf(filep, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t",
218256a424ccSmp 			       &t2, &t3, &t4, &t5, &t6, &t7, &t8, &t9);
21837c478bd9Sstevel@tonic-gate 		if (nread == 8) {
21847c478bd9Sstevel@tonic-gate 		    dbentry.attributes = (krb5_flags) t2;
21857c478bd9Sstevel@tonic-gate 		    dbentry.max_life = (krb5_deltat) t3;
218656a424ccSmp 		    dbentry.max_renewable_life = (krb5_deltat) t4;
218756a424ccSmp 		    dbentry.expiration = (krb5_timestamp) t5;
218856a424ccSmp 		    dbentry.pw_expiration = (krb5_timestamp) t6;
218956a424ccSmp 		    dbentry.last_success = (krb5_timestamp) t7;
219056a424ccSmp 		    dbentry.last_failed = (krb5_timestamp) t8;
219156a424ccSmp 		    dbentry.fail_auth_count = (krb5_kvno) t9;
219254925bf6Swillf 		    dbentry.mask = KADM5_LOAD | KADM5_PRINCIPAL | KADM5_ATTRIBUTES |
219354925bf6Swillf 			KADM5_MAX_LIFE | KADM5_MAX_RLIFE |
219454925bf6Swillf 			KADM5_PRINC_EXPIRE_TIME | KADM5_LAST_SUCCESS |
219554925bf6Swillf 			KADM5_LAST_FAILED | KADM5_FAIL_AUTH_COUNT;
21967c478bd9Sstevel@tonic-gate 		} else {
21977c478bd9Sstevel@tonic-gate 		    try2read = read_nint_data;
21987c478bd9Sstevel@tonic-gate 		    error++;
21997c478bd9Sstevel@tonic-gate 		}
22007c478bd9Sstevel@tonic-gate 
22017c478bd9Sstevel@tonic-gate 		/*
22027c478bd9Sstevel@tonic-gate 		 * Get the tagged data.
22037c478bd9Sstevel@tonic-gate 		 *
220456a424ccSmp 		 * Really, this code ought to discard tl data types
220556a424ccSmp 		 * that it knows are special to the current version
220656a424ccSmp 		 * and were not supported in the previous version.
220756a424ccSmp 		 * But it's a pain to implement that here, and doing
220856a424ccSmp 		 * it at dump time has almost as good an effect, so
220956a424ccSmp 		 * that's what I did.  [krb5-admin/89]
22107c478bd9Sstevel@tonic-gate 		 */
22117c478bd9Sstevel@tonic-gate 		if (!error && dbentry.n_tl_data) {
221256a424ccSmp 		    for (tl = dbentry.tl_data; tl; tl = tl->tl_data_next) {
221356a424ccSmp 			nread = fscanf(filep, "%d\t%d\t", &t1, &t2);
221456a424ccSmp 			if (nread == 2) {
221556a424ccSmp 			    tl->tl_data_type = (krb5_int16) t1;
221656a424ccSmp 			    tl->tl_data_length = (krb5_int16) t2;
221756a424ccSmp 			    if (tl->tl_data_length) {
221856a424ccSmp 				if (!(tl->tl_data_contents =
221956a424ccSmp 				      (krb5_octet *) malloc((size_t) t2+1)) ||
222056a424ccSmp 				    read_octet_string(filep,
222156a424ccSmp 						      tl->tl_data_contents,
222256a424ccSmp 						      t2)) {
222356a424ccSmp 				    try2read = read_tcontents;
222456a424ccSmp 				    error++;
222556a424ccSmp 				    break;
22267c478bd9Sstevel@tonic-gate 				}
222754925bf6Swillf 				/* test to set mask fields */
222854925bf6Swillf 				if (t1 == KRB5_TL_KADM_DATA) {
222954925bf6Swillf 				    XDR xdrs;
223054925bf6Swillf 				    osa_princ_ent_rec osa_princ_ent;
223154925bf6Swillf 
2232*55fea89dSDan Cross 				    /*
223354925bf6Swillf 				     * Assuming aux_attributes will always be
223454925bf6Swillf 				     * there
223554925bf6Swillf 				     */
223654925bf6Swillf 				    dbentry.mask |= KADM5_AUX_ATTRIBUTES;
223754925bf6Swillf 
223854925bf6Swillf 				    /* test for an actual policy reference */
223954925bf6Swillf 				    memset(&osa_princ_ent, 0, sizeof(osa_princ_ent));
224054925bf6Swillf 				    xdrmem_create(&xdrs, (char *)tl->tl_data_contents,
224154925bf6Swillf 					    tl->tl_data_length, XDR_DECODE);
224254925bf6Swillf 				    if (xdr_osa_princ_ent_rec(&xdrs, &osa_princ_ent) &&
224354925bf6Swillf 					    (osa_princ_ent.aux_attributes & KADM5_POLICY) &&
224454925bf6Swillf 					    osa_princ_ent.policy != NULL) {
224554925bf6Swillf 
224654925bf6Swillf 					dbentry.mask |= KADM5_POLICY;
224754925bf6Swillf 					kdb_free_entry(NULL, NULL, &osa_princ_ent);
224854925bf6Swillf 				    }
224954925bf6Swillf 				    xdr_destroy(&xdrs);
225054925bf6Swillf 				}
225156a424ccSmp 			    }
225256a424ccSmp 			    else {
225356a424ccSmp 				/* Should be a null field */
225456a424ccSmp 				nread = fscanf(filep, "%d", &t9);
225556a424ccSmp 				if ((nread != 1) || (t9 != -1)) {
225656a424ccSmp 				    error++;
225756a424ccSmp 				    try2read = read_tcontents;
225856a424ccSmp 				    break;
225956a424ccSmp 				}
226056a424ccSmp 			    }
226156a424ccSmp 			}
226256a424ccSmp 			else {
226356a424ccSmp 			    try2read = read_ttypelen;
226456a424ccSmp 			    error++;
226556a424ccSmp 			    break;
226656a424ccSmp 			}
226756a424ccSmp 		    }
226854925bf6Swillf 		    if (!error)
226954925bf6Swillf 			dbentry.mask |= KADM5_TL_DATA;
227056a424ccSmp 		}
227156a424ccSmp 
22727c478bd9Sstevel@tonic-gate 		/* Get the key data */
22737c478bd9Sstevel@tonic-gate 		if (!error && dbentry.n_key_data) {
227456a424ccSmp 		    for (i=0; !error && (i<dbentry.n_key_data); i++) {
227556a424ccSmp 			kdatap = &dbentry.key_data[i];
227656a424ccSmp 			nread = fscanf(filep, "%d\t%d\t", &t1, &t2);
227756a424ccSmp 			if (nread == 2) {
227856a424ccSmp 			    kdatap->key_data_ver = (krb5_int16) t1;
227956a424ccSmp 			    kdatap->key_data_kvno = (krb5_int16) t2;
228056a424ccSmp 
228156a424ccSmp 			    for (j=0; j<t1; j++) {
228256a424ccSmp 				nread = fscanf(filep, "%d\t%d\t", &t3, &t4);
228356a424ccSmp 				if (nread == 2) {
228456a424ccSmp 				    kdatap->key_data_type[j] = t3;
228556a424ccSmp 				    kdatap->key_data_length[j] = t4;
228656a424ccSmp 				    if (t4) {
228756a424ccSmp 					if (!(kdatap->key_data_contents[j] =
228856a424ccSmp 					      (krb5_octet *)
228956a424ccSmp 					      malloc((size_t) t4+1)) ||
229056a424ccSmp 					    read_octet_string(filep,
229156a424ccSmp 							      kdatap->key_data_contents[j],
229256a424ccSmp 							      t4)) {
229356a424ccSmp 					    try2read = read_kcontents;
229456a424ccSmp 					    error++;
229556a424ccSmp 					    break;
22967c478bd9Sstevel@tonic-gate 					}
229756a424ccSmp 				    }
229856a424ccSmp 				    else {
229956a424ccSmp 					/* Should be a null field */
230056a424ccSmp 					nread = fscanf(filep, "%d", &t9);
230156a424ccSmp 					if ((nread != 1) || (t9 != -1)) {
230256a424ccSmp 					    error++;
230356a424ccSmp 					    try2read = read_kcontents;
230456a424ccSmp 					    break;
230556a424ccSmp 					}
230656a424ccSmp 				    }
230756a424ccSmp 				}
230856a424ccSmp 				else {
230956a424ccSmp 				    try2read = read_ktypelen;
231056a424ccSmp 				    error++;
231156a424ccSmp 				    break;
231256a424ccSmp 				}
231356a424ccSmp 			    }
231456a424ccSmp 			}
231556a424ccSmp 		    }
231654925bf6Swillf 		    if (!error)
231754925bf6Swillf 			dbentry.mask |= KADM5_KEY_DATA;
231856a424ccSmp 		}
231956a424ccSmp 
23207c478bd9Sstevel@tonic-gate 		/* Get the extra data */
23217c478bd9Sstevel@tonic-gate 		if (!error && dbentry.e_length) {
23227c478bd9Sstevel@tonic-gate 		    if (read_octet_string(filep,
23237c478bd9Sstevel@tonic-gate 					  dbentry.e_data,
23247c478bd9Sstevel@tonic-gate 					  (int) dbentry.e_length)) {
23257c478bd9Sstevel@tonic-gate 			try2read = read_econtents;
23267c478bd9Sstevel@tonic-gate 			error++;
23277c478bd9Sstevel@tonic-gate 		    }
232856a424ccSmp 		}
232956a424ccSmp 		else {
23307c478bd9Sstevel@tonic-gate 		    nread = fscanf(filep, "%d", &t9);
23317c478bd9Sstevel@tonic-gate 		    if ((nread != 1) || (t9 != -1)) {
23327c478bd9Sstevel@tonic-gate 			error++;
23337c478bd9Sstevel@tonic-gate 			try2read = read_econtents;
23347c478bd9Sstevel@tonic-gate 		    }
23357c478bd9Sstevel@tonic-gate 		}
23367c478bd9Sstevel@tonic-gate 
23377c478bd9Sstevel@tonic-gate 		/* Finally, find the end of the record. */
23387c478bd9Sstevel@tonic-gate 		if (!error)
23397c478bd9Sstevel@tonic-gate 		    find_record_end(filep, fname, *linenop);
23407c478bd9Sstevel@tonic-gate 
23417c478bd9Sstevel@tonic-gate 		/*
234256a424ccSmp 		 * We have either read in all the data or choked.
23437c478bd9Sstevel@tonic-gate 		 */
23447c478bd9Sstevel@tonic-gate 		if (!error) {
23457c478bd9Sstevel@tonic-gate 		    one = 1;
234656a424ccSmp 		    if ((kret = krb5_db_put_principal(kcontext,
23477c478bd9Sstevel@tonic-gate 						      &dbentry,
23487c478bd9Sstevel@tonic-gate 						      &one))) {
23497c478bd9Sstevel@tonic-gate 						fprintf(stderr,
23507c478bd9Sstevel@tonic-gate 						    gettext(store_err_fmt),
23517c478bd9Sstevel@tonic-gate 				fname, *linenop,
23527c478bd9Sstevel@tonic-gate 				name, error_message(kret));
235356a424ccSmp 		    }
235456a424ccSmp 		    else {
23557c478bd9Sstevel@tonic-gate 			if (verbose)
23567c478bd9Sstevel@tonic-gate 							fprintf(stderr,
23577c478bd9Sstevel@tonic-gate 							    gettext(
23587c478bd9Sstevel@tonic-gate 								add_princ_fmt),
23597c478bd9Sstevel@tonic-gate 							    name);
23607c478bd9Sstevel@tonic-gate 			retval = 0;
23617c478bd9Sstevel@tonic-gate 		    }
236256a424ccSmp 		}
236356a424ccSmp 		else {
23647c478bd9Sstevel@tonic-gate 					fprintf(stderr, gettext(read_err_fmt),
23657c478bd9Sstevel@tonic-gate 					    fname, *linenop, try2read);
23667c478bd9Sstevel@tonic-gate 		}
236756a424ccSmp 	    }
236856a424ccSmp 	    else {
23697c478bd9Sstevel@tonic-gate 		if (kret)
23707c478bd9Sstevel@tonic-gate 					fprintf(stderr, gettext(parse_err_fmt),
237156a424ccSmp 			    fname, *linenop, name, error_message(kret));
23727c478bd9Sstevel@tonic-gate 		else
237356a424ccSmp 		    fprintf(stderr, gettext(no_mem_fmt),
23747c478bd9Sstevel@tonic-gate 						fname, *linenop);
23757c478bd9Sstevel@tonic-gate 	    }
237656a424ccSmp 	}
237756a424ccSmp 	else {
237856a424ccSmp 	    fprintf(stderr,
23797c478bd9Sstevel@tonic-gate 				gettext(rhead_err_fmt), fname, *linenop);
23807c478bd9Sstevel@tonic-gate 	}
23817c478bd9Sstevel@tonic-gate 
23827c478bd9Sstevel@tonic-gate 	if (op)
23837c478bd9Sstevel@tonic-gate 	    free(op);
23847c478bd9Sstevel@tonic-gate 	if (kp)
23857c478bd9Sstevel@tonic-gate 	    free(kp);
23867c478bd9Sstevel@tonic-gate 	if (name)
23877c478bd9Sstevel@tonic-gate 	    free(name);
23887c478bd9Sstevel@tonic-gate 	krb5_db_free_principal(kcontext, &dbentry, 1);
238956a424ccSmp     }
239056a424ccSmp     else {
23917c478bd9Sstevel@tonic-gate 	if (nread == EOF)
23927c478bd9Sstevel@tonic-gate 	    retval = -1;
23937c478bd9Sstevel@tonic-gate     }
23947c478bd9Sstevel@tonic-gate     return(retval);
23957c478bd9Sstevel@tonic-gate }
23967c478bd9Sstevel@tonic-gate 
2397*55fea89dSDan Cross static int
process_k5beta7_policy(fname,kcontext,filep,verbose,linenop,pol_db)23987c478bd9Sstevel@tonic-gate process_k5beta7_policy(fname, kcontext, filep, verbose, linenop, pol_db)
23997c478bd9Sstevel@tonic-gate     char		*fname;
24007c478bd9Sstevel@tonic-gate     krb5_context	kcontext;
24017c478bd9Sstevel@tonic-gate     FILE		*filep;
24027c478bd9Sstevel@tonic-gate     int			verbose;
24037c478bd9Sstevel@tonic-gate     int			*linenop;
24047c478bd9Sstevel@tonic-gate     void *pol_db;
24057c478bd9Sstevel@tonic-gate {
24067c478bd9Sstevel@tonic-gate     osa_policy_ent_rec rec;
24077c478bd9Sstevel@tonic-gate     char namebuf[1024];
24087c478bd9Sstevel@tonic-gate     int nread, ret;
24097c478bd9Sstevel@tonic-gate 
24107c478bd9Sstevel@tonic-gate     (*linenop)++;
24117c478bd9Sstevel@tonic-gate     rec.name = namebuf;
24127c478bd9Sstevel@tonic-gate 
24137c478bd9Sstevel@tonic-gate     nread = fscanf(filep, "%1024s\t%d\t%d\t%d\t%d\t%d\t%d", rec.name,
24147c478bd9Sstevel@tonic-gate 		   &rec.pw_min_life, &rec.pw_max_life,
24157c478bd9Sstevel@tonic-gate 		   &rec.pw_min_length, &rec.pw_min_classes,
24167c478bd9Sstevel@tonic-gate 		   &rec.pw_history_num, &rec.policy_refcnt);
24177c478bd9Sstevel@tonic-gate     if (nread == EOF)
241856a424ccSmp 	 return -1;
24197c478bd9Sstevel@tonic-gate     else if (nread != 7) {
24207c478bd9Sstevel@tonic-gate 		fprintf(stderr,
24217c478bd9Sstevel@tonic-gate 		    gettext("cannot parse policy on line %d (%d read)\n"),
24227c478bd9Sstevel@tonic-gate 		 *linenop, nread);
242356a424ccSmp 	 return 1;
24247c478bd9Sstevel@tonic-gate     }
24257c478bd9Sstevel@tonic-gate 
242654925bf6Swillf     if ((ret = krb5_db_create_policy(kcontext, &rec))) {
242754925bf6Swillf 	 if (ret &&
242854925bf6Swillf 	     ((ret = krb5_db_put_policy(kcontext, &rec)))) {
24297c478bd9Sstevel@tonic-gate 	      fprintf(stderr, gettext("cannot create policy on line %d: %s\n"),
24307c478bd9Sstevel@tonic-gate 		      *linenop, error_message(ret));
243156a424ccSmp 	      return 1;
24327c478bd9Sstevel@tonic-gate 	 }
24337c478bd9Sstevel@tonic-gate     }
24347c478bd9Sstevel@tonic-gate     if (verbose)
24357c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("created policy %s\n"), rec.name);
2436*55fea89dSDan Cross 
243756a424ccSmp     return 0;
24387c478bd9Sstevel@tonic-gate }
24397c478bd9Sstevel@tonic-gate 
24407c478bd9Sstevel@tonic-gate /*
244156a424ccSmp  * process_k5beta7_record()	- Handle a dump record in krb5b7 format.
24427c478bd9Sstevel@tonic-gate  *
24437c478bd9Sstevel@tonic-gate  * Returns -1 for end of file, 0 for success and 1 for failure.
24447c478bd9Sstevel@tonic-gate  */
24457c478bd9Sstevel@tonic-gate static int
process_k5beta7_record(fname,kcontext,filep,verbose,linenop)244654925bf6Swillf process_k5beta7_record(fname, kcontext, filep, verbose, linenop)
24477c478bd9Sstevel@tonic-gate     char		*fname;
24487c478bd9Sstevel@tonic-gate     krb5_context	kcontext;
24497c478bd9Sstevel@tonic-gate     FILE		*filep;
24507c478bd9Sstevel@tonic-gate     int			verbose;
24517c478bd9Sstevel@tonic-gate     int			*linenop;
24527c478bd9Sstevel@tonic-gate {
24537c478bd9Sstevel@tonic-gate      int nread;
24547c478bd9Sstevel@tonic-gate      char rectype[100];
24557c478bd9Sstevel@tonic-gate 
24567c478bd9Sstevel@tonic-gate      nread = fscanf(filep, "%100s\t", rectype);
24577c478bd9Sstevel@tonic-gate      if (nread == EOF)
245856a424ccSmp 	  return -1;
24597c478bd9Sstevel@tonic-gate      else if (nread != 1)
246056a424ccSmp 	  return 1;
24617c478bd9Sstevel@tonic-gate      if (strcmp(rectype, "princ") == 0)
24627c478bd9Sstevel@tonic-gate 	  process_k5beta6_record(fname, kcontext, filep, verbose,
246354925bf6Swillf 				 linenop);
24647c478bd9Sstevel@tonic-gate      else if (strcmp(rectype, "policy") == 0)
24657c478bd9Sstevel@tonic-gate 	  process_k5beta7_policy(fname, kcontext, filep, verbose,
246654925bf6Swillf 				 linenop);
24677c478bd9Sstevel@tonic-gate      else {
24687c478bd9Sstevel@tonic-gate 		fprintf(stderr,
24697c478bd9Sstevel@tonic-gate 		    gettext("unknown record type \"%s\" on line %d\n"),
24707c478bd9Sstevel@tonic-gate 		  rectype, *linenop);
247156a424ccSmp 	  return 1;
24727c478bd9Sstevel@tonic-gate      }
24737c478bd9Sstevel@tonic-gate 
247456a424ccSmp      return 0;
24757c478bd9Sstevel@tonic-gate }
24767c478bd9Sstevel@tonic-gate 
24777c478bd9Sstevel@tonic-gate /*
24787c478bd9Sstevel@tonic-gate  * process_ov_record()	- Handle a dump record in OpenV*Secure 1.0 format.
24797c478bd9Sstevel@tonic-gate  *
24807c478bd9Sstevel@tonic-gate  * Returns -1 for end of file, 0 for success and 1 for failure.
24817c478bd9Sstevel@tonic-gate  */
24827c478bd9Sstevel@tonic-gate static int
process_ov_record(fname,kcontext,filep,verbose,linenop)248354925bf6Swillf process_ov_record(fname, kcontext, filep, verbose, linenop)
24847c478bd9Sstevel@tonic-gate     char		*fname;
24857c478bd9Sstevel@tonic-gate     krb5_context	kcontext;
24867c478bd9Sstevel@tonic-gate     FILE		*filep;
24877c478bd9Sstevel@tonic-gate     int			verbose;
24887c478bd9Sstevel@tonic-gate     int			*linenop;
24897c478bd9Sstevel@tonic-gate {
24907c478bd9Sstevel@tonic-gate      int nread;
24917c478bd9Sstevel@tonic-gate      char rectype[100];
24927c478bd9Sstevel@tonic-gate 
24937c478bd9Sstevel@tonic-gate      nread = fscanf(filep, "%100s\t", rectype);
24947c478bd9Sstevel@tonic-gate      if (nread == EOF)
249556a424ccSmp 	  return -1;
24967c478bd9Sstevel@tonic-gate      else if (nread != 1)
249756a424ccSmp 	  return 1;
24987c478bd9Sstevel@tonic-gate      if (strcmp(rectype, "princ") == 0)
24997c478bd9Sstevel@tonic-gate 	  process_ov_principal(fname, kcontext, filep, verbose,
250054925bf6Swillf 			       linenop);
25017c478bd9Sstevel@tonic-gate      else if (strcmp(rectype, "policy") == 0)
25027c478bd9Sstevel@tonic-gate 	  process_k5beta7_policy(fname, kcontext, filep, verbose,
250354925bf6Swillf 				 linenop);
25047c478bd9Sstevel@tonic-gate      else if (strcmp(rectype, "End") == 0)
250556a424ccSmp 	  return -1;
25067c478bd9Sstevel@tonic-gate      else {
25077c478bd9Sstevel@tonic-gate 		fprintf(stderr,
25087c478bd9Sstevel@tonic-gate 		    gettext("unknown record type \"%s\" on line %d\n"),
25097c478bd9Sstevel@tonic-gate 		  rectype, *linenop);
251056a424ccSmp 	  return 1;
25117c478bd9Sstevel@tonic-gate      }
25127c478bd9Sstevel@tonic-gate 
251356a424ccSmp      return 0;
25147c478bd9Sstevel@tonic-gate }
25157c478bd9Sstevel@tonic-gate 
25167c478bd9Sstevel@tonic-gate /*
25177c478bd9Sstevel@tonic-gate  * restore_dump()	- Restore the database from any version dump file.
25187c478bd9Sstevel@tonic-gate  */
25197c478bd9Sstevel@tonic-gate static int
restore_dump(programname,kcontext,dumpfile,f,verbose,dump)252054925bf6Swillf restore_dump(programname, kcontext, dumpfile, f, verbose, dump)
25217c478bd9Sstevel@tonic-gate     char		*programname;
25227c478bd9Sstevel@tonic-gate     krb5_context	kcontext;
25237c478bd9Sstevel@tonic-gate     char		*dumpfile;
25247c478bd9Sstevel@tonic-gate     FILE		*f;
25257c478bd9Sstevel@tonic-gate     int			verbose;
25267c478bd9Sstevel@tonic-gate     dump_version	*dump;
25277c478bd9Sstevel@tonic-gate {
2528*55fea89dSDan Cross     int		error;
25297c478bd9Sstevel@tonic-gate     int		lineno;
25307c478bd9Sstevel@tonic-gate 
25317c478bd9Sstevel@tonic-gate     error = 0;
25327c478bd9Sstevel@tonic-gate     lineno = 1;
25337c478bd9Sstevel@tonic-gate 
25347c478bd9Sstevel@tonic-gate     /*
25357c478bd9Sstevel@tonic-gate      * Process the records.
25367c478bd9Sstevel@tonic-gate      */
25377c478bd9Sstevel@tonic-gate     while (!(error = (*dump->load_record)(dumpfile,
2538*55fea89dSDan Cross 					  kcontext,
25397c478bd9Sstevel@tonic-gate 					  f,
25407c478bd9Sstevel@tonic-gate 					  verbose,
254154925bf6Swillf 					  &lineno)))
254256a424ccSmp 	 ;
25437c478bd9Sstevel@tonic-gate     if (error != -1)
25447c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(err_line_fmt),
25457c478bd9Sstevel@tonic-gate 		    programname, lineno, dumpfile);
25467c478bd9Sstevel@tonic-gate     else
25477c478bd9Sstevel@tonic-gate 	 error = 0;
25487c478bd9Sstevel@tonic-gate 
25497c478bd9Sstevel@tonic-gate     return(error);
25507c478bd9Sstevel@tonic-gate }
25517c478bd9Sstevel@tonic-gate 
25527c478bd9Sstevel@tonic-gate /*
255356a424ccSmp  * Usage: load_db [-i] [-old] [-ov] [-b6] [-b7] [-verbose] [-update] [-hash]
255456a424ccSmp  *		filename
25557c478bd9Sstevel@tonic-gate  */
25567c478bd9Sstevel@tonic-gate void
load_db(argc,argv)25577c478bd9Sstevel@tonic-gate load_db(argc, argv)
25587c478bd9Sstevel@tonic-gate     int		argc;
25597c478bd9Sstevel@tonic-gate     char	**argv;
25607c478bd9Sstevel@tonic-gate {
25617c478bd9Sstevel@tonic-gate     kadm5_config_params newparams;
25627c478bd9Sstevel@tonic-gate     krb5_error_code	kret;
25637c478bd9Sstevel@tonic-gate     krb5_context	kcontext;
25647c478bd9Sstevel@tonic-gate     FILE		*f;
25657c478bd9Sstevel@tonic-gate     extern char		*optarg;
25667c478bd9Sstevel@tonic-gate     extern int		optind;
2567dd9ccd46S /* Solaris Kerberos */
2568dd9ccd46S #if 0
25697c478bd9Sstevel@tonic-gate     char		*programname;
2570dd9ccd46S #endif
25717c478bd9Sstevel@tonic-gate     char		*dumpfile;
25727c478bd9Sstevel@tonic-gate     char		*dbname;
25737c478bd9Sstevel@tonic-gate     char		*dbname_tmp;
25747c478bd9Sstevel@tonic-gate     char		buf[BUFSIZ];
25757c478bd9Sstevel@tonic-gate     dump_version	*load;
25767c478bd9Sstevel@tonic-gate     int			update, verbose;
25777c478bd9Sstevel@tonic-gate     int			aindex;
25787c478bd9Sstevel@tonic-gate     bool_t		add_update = TRUE;
25797c478bd9Sstevel@tonic-gate     char		iheader[MAX_HEADER];
25807c478bd9Sstevel@tonic-gate     uint32_t		caller, last_sno, last_seconds, last_useconds;
25817c478bd9Sstevel@tonic-gate     kdb_log_context	*log_ctx;
258254925bf6Swillf     int			db_locked = 0;
25837c478bd9Sstevel@tonic-gate 
25847c478bd9Sstevel@tonic-gate     /*
25857c478bd9Sstevel@tonic-gate      * Parse the arguments.
25867c478bd9Sstevel@tonic-gate      */
2587dd9ccd46S /* Solaris Kerberos */
2588dd9ccd46S #if 0
25897c478bd9Sstevel@tonic-gate     programname = argv[0];
25907c478bd9Sstevel@tonic-gate     if (strrchr(programname, (int) '/'))
25917c478bd9Sstevel@tonic-gate 	programname = strrchr(argv[0], (int) '/') + 1;
2592dd9ccd46S #endif
25937c478bd9Sstevel@tonic-gate     dumpfile = (char *) NULL;
25947c478bd9Sstevel@tonic-gate     dbname = global_params.dbname;
25957c478bd9Sstevel@tonic-gate     load = NULL;
25967c478bd9Sstevel@tonic-gate     update = 0;
25977c478bd9Sstevel@tonic-gate     verbose = 0;
25987c478bd9Sstevel@tonic-gate     exit_status = 0;
25997c478bd9Sstevel@tonic-gate     dbname_tmp = (char *) NULL;
26007c478bd9Sstevel@tonic-gate     log_ctx = util_context->kdblog_context;
26017c478bd9Sstevel@tonic-gate 
26027c478bd9Sstevel@tonic-gate     for (aindex = 1; aindex < argc; aindex++) {
260356a424ccSmp 	if (!strcmp(argv[aindex], oldoption))
26047c478bd9Sstevel@tonic-gate 	     load = &old_version;
260556a424ccSmp 	else if (!strcmp(argv[aindex], b6option))
26067c478bd9Sstevel@tonic-gate 	     load = &beta6_version;
260756a424ccSmp 	else if (!strcmp(argv[aindex], b7option))
260856a424ccSmp 	     load = &beta7_version;
260956a424ccSmp 	else if (!strcmp(argv[aindex], ovoption))
26107c478bd9Sstevel@tonic-gate 	     load = &ov_version;
261156a424ccSmp 	else if (!strcmp(argv[aindex], ipropoption)) {
26127c478bd9Sstevel@tonic-gate 			if (log_ctx && log_ctx->iproprole) {
26137c478bd9Sstevel@tonic-gate 				load = &iprop_version;
26147c478bd9Sstevel@tonic-gate 				add_update = FALSE;
26157c478bd9Sstevel@tonic-gate 			} else {
26167c478bd9Sstevel@tonic-gate 				fprintf(stderr, gettext("Iprop not enabled\n"));
26177c478bd9Sstevel@tonic-gate 				exit_status++;
26187c478bd9Sstevel@tonic-gate 				return;
26197c478bd9Sstevel@tonic-gate 			}
262056a424ccSmp 		}
262156a424ccSmp 	else if (!strcmp(argv[aindex], verboseoption))
26227c478bd9Sstevel@tonic-gate 	    verbose = 1;
262356a424ccSmp 	else if (!strcmp(argv[aindex], updateoption))
26247c478bd9Sstevel@tonic-gate 	    update = 1;
262554925bf6Swillf 	else if (!strcmp(argv[aindex], hashoption)) {
262654925bf6Swillf 	    if (!add_db_arg("hash=true")) {
262754925bf6Swillf 		com_err(progname, ENOMEM, "while parsing command arguments\n");
262854925bf6Swillf 		exit(1);
262954925bf6Swillf 	    }
263054925bf6Swillf 	} else
26317c478bd9Sstevel@tonic-gate 	    break;
26327c478bd9Sstevel@tonic-gate     }
26337c478bd9Sstevel@tonic-gate     if ((argc - aindex) != 1) {
26347c478bd9Sstevel@tonic-gate 	usage();
26357c478bd9Sstevel@tonic-gate 	return;
26367c478bd9Sstevel@tonic-gate     }
26377c478bd9Sstevel@tonic-gate     dumpfile = argv[aindex];
26387c478bd9Sstevel@tonic-gate 
26397c478bd9Sstevel@tonic-gate     if (!(dbname_tmp = (char *) malloc(strlen(dbname)+
26407c478bd9Sstevel@tonic-gate 				       strlen(dump_tmptrail)+1))) {
2641dd9ccd46S 		/* Solaris Kerberos */
2642dd9ccd46S 		fprintf(stderr, gettext(no_name_mem_fmt), progname);
26437c478bd9Sstevel@tonic-gate 	exit_status++;
26447c478bd9Sstevel@tonic-gate 	return;
26457c478bd9Sstevel@tonic-gate     }
26467c478bd9Sstevel@tonic-gate     strcpy(dbname_tmp, dbname);
26477c478bd9Sstevel@tonic-gate     strcat(dbname_tmp, dump_tmptrail);
26487c478bd9Sstevel@tonic-gate 
26497c478bd9Sstevel@tonic-gate     /*
26507c478bd9Sstevel@tonic-gate      * Initialize the Kerberos context and error tables.
26517c478bd9Sstevel@tonic-gate      */
265254925bf6Swillf     if ((kret = kadm5_init_krb5_context(&kcontext))) {
2653dd9ccd46S 	/* Solaris Kerberos */
2654dd9ccd46S 	fprintf(stderr, gettext(ctx_err_fmt), progname);
26557c478bd9Sstevel@tonic-gate 	free(dbname_tmp);
26567c478bd9Sstevel@tonic-gate 	exit_status++;
26577c478bd9Sstevel@tonic-gate 	return;
26587c478bd9Sstevel@tonic-gate     }
26597c478bd9Sstevel@tonic-gate 
266054925bf6Swillf     if( (kret = krb5_set_default_realm(kcontext, util_context->default_realm)) )
266154925bf6Swillf     {
2662dd9ccd46S 	/* Solaris Kerberos */
2663dd9ccd46S 	fprintf(stderr, gettext("%s: Unable to set the default realm\n"), progname);
266454925bf6Swillf 	free(dbname_tmp);
266554925bf6Swillf 	exit_status++;
266654925bf6Swillf 	return;
266754925bf6Swillf     }
26687c478bd9Sstevel@tonic-gate     if (log_ctx && log_ctx->iproprole)
26697c478bd9Sstevel@tonic-gate 	kcontext->kdblog_context = (void *)log_ctx;
26707c478bd9Sstevel@tonic-gate     /*
26717c478bd9Sstevel@tonic-gate      * Open the dumpfile
26727c478bd9Sstevel@tonic-gate      */
26737c478bd9Sstevel@tonic-gate     if (dumpfile) {
267454925bf6Swillf 	if ((f = fopen(dumpfile, "r")) == NULL) {
2675dd9ccd46S 			/* Solaris Kerberos */
26767c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(dfile_err_fmt),
2677dd9ccd46S 			    progname, dumpfile,
2678*55fea89dSDan Cross 		     error_message(errno));
26797c478bd9Sstevel@tonic-gate 	     exit_status++;
26807c478bd9Sstevel@tonic-gate 	     return;
26817c478bd9Sstevel@tonic-gate 	}
26827c478bd9Sstevel@tonic-gate 	if ((kret = krb5_lock_file(kcontext, fileno(f),
26837c478bd9Sstevel@tonic-gate 				   KRB5_LOCKMODE_SHARED))) {
2684dd9ccd46S 	     /* Solaris Kerberos */
2685dd9ccd46S 	     fprintf(stderr, gettext("%s: Cannot lock %s: %s\n"), progname,
26867c478bd9Sstevel@tonic-gate 		     dumpfile, error_message(errno));
26877c478bd9Sstevel@tonic-gate 	     exit_status++;
26887c478bd9Sstevel@tonic-gate 	     return;
26897c478bd9Sstevel@tonic-gate 	}
26907c478bd9Sstevel@tonic-gate     } else
26917c478bd9Sstevel@tonic-gate 	f = stdin;
26927c478bd9Sstevel@tonic-gate 
26937c478bd9Sstevel@tonic-gate     /*
269456a424ccSmp      * Auto-detect dump version if we weren't told, verify if we
269556a424ccSmp      * were told.
26967c478bd9Sstevel@tonic-gate      */
26977c478bd9Sstevel@tonic-gate     fgets(buf, sizeof(buf), f);
26987c478bd9Sstevel@tonic-gate     if (load) {
269956a424ccSmp 	 /* only check what we know; some headers only contain a prefix */
27007c478bd9Sstevel@tonic-gate 	 if (strncmp(buf, load->header, strlen(load->header)) != 0) {
2701dd9ccd46S 			/* Solaris Kerberos */
2702dd9ccd46S 			fprintf(stderr, gettext(head_bad_fmt), progname, dumpfile);
27037c478bd9Sstevel@tonic-gate 	      exit_status++;
270456a424ccSmp 	      if (dumpfile) fclose(f);
27057c478bd9Sstevel@tonic-gate 	      return;
27067c478bd9Sstevel@tonic-gate 	 }
27077c478bd9Sstevel@tonic-gate     } else {
27087c478bd9Sstevel@tonic-gate 	 /* perhaps this should be in an array, but so what? */
27097c478bd9Sstevel@tonic-gate 	 if (strcmp(buf, old_version.header) == 0)
27107c478bd9Sstevel@tonic-gate 	      load = &old_version;
27117c478bd9Sstevel@tonic-gate 	 else if (strcmp(buf, beta6_version.header) == 0)
27127c478bd9Sstevel@tonic-gate 	      load = &beta6_version;
27137c478bd9Sstevel@tonic-gate 	 else if (strcmp(buf, beta7_version.header) == 0)
27147c478bd9Sstevel@tonic-gate 	      load = &beta7_version;
271556a424ccSmp 	 else if (strcmp(buf, r1_3_version.header) == 0)
271656a424ccSmp 	      load = &r1_3_version;
27177c478bd9Sstevel@tonic-gate 	 else if (strncmp(buf, ov_version.header,
27187c478bd9Sstevel@tonic-gate 			  strlen(ov_version.header)) == 0)
27197c478bd9Sstevel@tonic-gate 	      load = &ov_version;
272056a424ccSmp 	 else {
2721dd9ccd46S 			/* Solaris Kerberos */
27227c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(head_bad_fmt),
2723dd9ccd46S 				progname, dumpfile);
27247c478bd9Sstevel@tonic-gate 	      exit_status++;
272556a424ccSmp 	      if (dumpfile) fclose(f);
27267c478bd9Sstevel@tonic-gate 	      return;
27277c478bd9Sstevel@tonic-gate 	 }
27287c478bd9Sstevel@tonic-gate     }
27297c478bd9Sstevel@tonic-gate     if (load->updateonly && !update) {
2730dd9ccd46S 		/* Solaris Kerberos */
27317c478bd9Sstevel@tonic-gate 		fprintf(stderr,
27327c478bd9Sstevel@tonic-gate 		    gettext("%s: dump version %s can only "
27337c478bd9Sstevel@tonic-gate 			"be loaded with the -update flag\n"),
2734dd9ccd46S 		    progname, load->name);
27357c478bd9Sstevel@tonic-gate 	 exit_status++;
27367c478bd9Sstevel@tonic-gate 	 return;
27377c478bd9Sstevel@tonic-gate     }
273856a424ccSmp 
27397c478bd9Sstevel@tonic-gate     /*
27407c478bd9Sstevel@tonic-gate      * Cons up params for the new databases.  If we are not in update
274154925bf6Swillf      * mode, we create an alternate database and then promote it to
274254925bf6Swillf      * be the live db.
27437c478bd9Sstevel@tonic-gate      */
27447c478bd9Sstevel@tonic-gate     newparams = global_params;
27457c478bd9Sstevel@tonic-gate     if (! update) {
27467c478bd9Sstevel@tonic-gate 	 newparams.mask |= KADM5_CONFIG_DBNAME;
27477c478bd9Sstevel@tonic-gate 	 newparams.dbname = dbname_tmp;
27487c478bd9Sstevel@tonic-gate 
2749159d09a2SMark Phalan 	 if ((kret = kadm5_get_config_params(kcontext, 1,
27507c478bd9Sstevel@tonic-gate 					     &newparams, &newparams))) {
2751dd9ccd46S 	      /* Solaris Kerberos */
2752dd9ccd46S 	      com_err(progname, kret,
27537c478bd9Sstevel@tonic-gate 			    gettext("while retreiving new "
27547c478bd9Sstevel@tonic-gate 				"configuration parameters"));
27557c478bd9Sstevel@tonic-gate 	      exit_status++;
27567c478bd9Sstevel@tonic-gate 	      return;
27577c478bd9Sstevel@tonic-gate 	 }
275854925bf6Swillf 
275954925bf6Swillf 	 if (!add_db_arg("temporary")) {
276054925bf6Swillf 	     com_err(progname, ENOMEM, "computing parameters for database");
276154925bf6Swillf 	     exit(1);
276254925bf6Swillf 	 }
27637c478bd9Sstevel@tonic-gate     }
2764*55fea89dSDan Cross 
27657c478bd9Sstevel@tonic-gate     /*
276654925bf6Swillf      * If not an update restoration, create the database. otherwise open
27677c478bd9Sstevel@tonic-gate      */
276854925bf6Swillf     if (!update) {
276954925bf6Swillf 	if((kret = krb5_db_create(kcontext, db5util_db_args))) {
277054925bf6Swillf 	    const char *emsg = krb5_get_error_message(kcontext, kret);
2771*55fea89dSDan Cross 	    /*
277254925bf6Swillf 	     * See if something (like DAL KDB plugin) has set a specific error
277354925bf6Swillf 	     * message and use that otherwise use default.
277454925bf6Swillf 	     */
277556a424ccSmp 
277654925bf6Swillf 	    if (emsg != NULL) {
2777dd9ccd46S 		/* Solaris Kerberos */
2778dd9ccd46S 		fprintf(stderr, "%s: %s\n", progname, emsg);
277954925bf6Swillf 		krb5_free_error_message (kcontext, emsg);
278054925bf6Swillf 	    } else {
2781dd9ccd46S 		/* Solaris Kerberos */
278254925bf6Swillf 		fprintf(stderr, dbcreaterr_fmt,
2783dd9ccd46S 			progname, dbname, error_message(kret));
278454925bf6Swillf 	    }
278554925bf6Swillf 	    exit_status++;
278654925bf6Swillf 	    kadm5_free_config_params(kcontext, &newparams);
278754925bf6Swillf 	    if (dumpfile) fclose(f);
278854925bf6Swillf 	    return;
278954925bf6Swillf 	}
27907c478bd9Sstevel@tonic-gate     }
279154925bf6Swillf     else {
279254925bf6Swillf 	    /*
279354925bf6Swillf 	     * Initialize the database.
279454925bf6Swillf 	     */
2795*55fea89dSDan Cross 	    if ((kret = krb5_db_open(kcontext, db5util_db_args,
279654925bf6Swillf 				     KRB5_KDB_OPEN_RW | KRB5_KDB_SRV_TYPE_ADMIN))) {
279754925bf6Swillf 		const char *emsg = krb5_get_error_message(kcontext, kret);
2798*55fea89dSDan Cross 		/*
279954925bf6Swillf 		 * See if something (like DAL KDB plugin) has set a specific
280054925bf6Swillf 		 * error message and use that otherwise use default.
280154925bf6Swillf 		 */
280254925bf6Swillf 
280354925bf6Swillf 		if (emsg != NULL) {
2804dd9ccd46S 		    /* Solaris Kerberos */
2805dd9ccd46S 		    fprintf(stderr, "%s: %s\n", progname, emsg);
280654925bf6Swillf 		    krb5_free_error_message (kcontext, emsg);
280754925bf6Swillf 		} else {
2808dd9ccd46S 		    /* Solaris Kerberos */
280954925bf6Swillf 		    fprintf(stderr, dbinit_err_fmt,
2810dd9ccd46S 			    progname, error_message(kret));
281154925bf6Swillf 		}
281254925bf6Swillf 		exit_status++;
281354925bf6Swillf 		goto error;
281454925bf6Swillf 	    }
28157c478bd9Sstevel@tonic-gate     }
281654925bf6Swillf 
281754925bf6Swillf 
28187c478bd9Sstevel@tonic-gate     /*
28197c478bd9Sstevel@tonic-gate      * If an update restoration, make sure the db is left unusable if
28207c478bd9Sstevel@tonic-gate      * the update fails.
28217c478bd9Sstevel@tonic-gate      */
282254925bf6Swillf     if ((kret = krb5_db_lock(kcontext, update?KRB5_DB_LOCKMODE_PERMANENT: KRB5_DB_LOCKMODE_EXCLUSIVE))) {
2823*55fea89dSDan Cross 	/*
282454925bf6Swillf 	 * Ignore a not supported error since there is nothing to do about it
282554925bf6Swillf 	 * anyway.
282654925bf6Swillf 	 */
282754925bf6Swillf 	if (kret != KRB5_PLUGIN_OP_NOTSUPP) {
2828dd9ccd46S 	    /* Solaris Kerberos */
282954925bf6Swillf 	    fprintf(stderr, gettext("%s: %s while permanently locking database\n"),
2830dd9ccd46S 		    progname, error_message(kret));
283154925bf6Swillf 	    exit_status++;
283254925bf6Swillf 	    goto error;
283354925bf6Swillf 	}
2834796b8631SToomas Soome     } else {
283554925bf6Swillf 	db_locked = 1;
2836796b8631SToomas Soome     }
2837*55fea89dSDan Cross 
28387c478bd9Sstevel@tonic-gate 	if (log_ctx && log_ctx->iproprole) {
28397c478bd9Sstevel@tonic-gate 		if (add_update)
28407c478bd9Sstevel@tonic-gate 			caller = FKCOMMAND;
28417c478bd9Sstevel@tonic-gate 		else
28427c478bd9Sstevel@tonic-gate 			caller = FKPROPD;
28437c478bd9Sstevel@tonic-gate 
28447c478bd9Sstevel@tonic-gate 		if (ulog_map(kcontext, &global_params, caller)) {
2845dd9ccd46S 			/* Solaris Kerberos */
28467c478bd9Sstevel@tonic-gate 			fprintf(stderr,
28477c478bd9Sstevel@tonic-gate 				gettext("%s: Could not map log\n"),
2848dd9ccd46S 				progname);
28497c478bd9Sstevel@tonic-gate 			exit_status++;
28507c478bd9Sstevel@tonic-gate 			goto error;
28517c478bd9Sstevel@tonic-gate 		}
28527c478bd9Sstevel@tonic-gate 
28537c478bd9Sstevel@tonic-gate 		/*
28547c478bd9Sstevel@tonic-gate 		 * We don't want to take out the ulog out from underneath
28557c478bd9Sstevel@tonic-gate 		 * kadmind so we reinit the header log.
28567c478bd9Sstevel@tonic-gate 		 *
28577c478bd9Sstevel@tonic-gate 		 * We also don't want to add to the update log since we
28587c478bd9Sstevel@tonic-gate 		 * are doing a whole sale replace of the db, because:
28597c478bd9Sstevel@tonic-gate 		 * 	we could easily exceed # of update entries
28607c478bd9Sstevel@tonic-gate 		 * 	we could implicity delete db entries during a replace
28617c478bd9Sstevel@tonic-gate 		 *	no advantage in incr updates when entire db is replaced
28627c478bd9Sstevel@tonic-gate 		 */
28637c478bd9Sstevel@tonic-gate 		if (!update) {
28647c478bd9Sstevel@tonic-gate                         memset(log_ctx->ulog, 0, sizeof (kdb_hlog_t));
2865*55fea89dSDan Cross 
28667c478bd9Sstevel@tonic-gate                         log_ctx->ulog->kdb_hmagic = KDB_HMAGIC;
28677c478bd9Sstevel@tonic-gate                         log_ctx->ulog->db_version_num = KDB_VERSION;
28687c478bd9Sstevel@tonic-gate                         log_ctx->ulog->kdb_state = KDB_STABLE;
28697c478bd9Sstevel@tonic-gate                         log_ctx->ulog->kdb_block = ULOG_BLOCK;
28707c478bd9Sstevel@tonic-gate 
28717c478bd9Sstevel@tonic-gate 			log_ctx->iproprole = IPROP_NULL;
28727c478bd9Sstevel@tonic-gate 
28737c478bd9Sstevel@tonic-gate 			if (!add_update) {
28747c478bd9Sstevel@tonic-gate 				sscanf(buf, "%s %u %u %u", iheader, &last_sno,
28757c478bd9Sstevel@tonic-gate 					&last_seconds, &last_useconds);
28767c478bd9Sstevel@tonic-gate 
28777c478bd9Sstevel@tonic-gate 				log_ctx->ulog->kdb_last_sno = last_sno;
28787c478bd9Sstevel@tonic-gate 				log_ctx->ulog->kdb_last_time.seconds =
28797c478bd9Sstevel@tonic-gate 				    last_seconds;
28807c478bd9Sstevel@tonic-gate 				log_ctx->ulog->kdb_last_time.useconds =
28817c478bd9Sstevel@tonic-gate 				    last_useconds;
28827c478bd9Sstevel@tonic-gate 			}
28837c478bd9Sstevel@tonic-gate 		}
28847c478bd9Sstevel@tonic-gate 	}
28857c478bd9Sstevel@tonic-gate 
2886dd9ccd46S     /* Solaris Kerberos */
2887dd9ccd46S     if (restore_dump(progname, kcontext, (dumpfile) ? dumpfile : stdin_name,
288854925bf6Swillf 		     f, verbose, load)) {
2889dd9ccd46S 	 /* Solaris Kerberos */
289054925bf6Swillf 	 fprintf(stderr, gettext(restfail_fmt),
2891dd9ccd46S 		 progname, load->name);
28927c478bd9Sstevel@tonic-gate 	 exit_status++;
28937c478bd9Sstevel@tonic-gate     }
289456a424ccSmp 
289554925bf6Swillf     if (!update && load->create_kadm5 &&
289654925bf6Swillf 	((kret = kadm5_create_magic_princs(&newparams, kcontext)))) {
289754925bf6Swillf 	 /* error message printed by create_magic_princs */
289854925bf6Swillf 	 exit_status++;
289954925bf6Swillf     }
2900*55fea89dSDan Cross 
290154925bf6Swillf     if (db_locked && (kret = krb5_db_unlock(kcontext))) {
29027c478bd9Sstevel@tonic-gate 	 /* change this error? */
2903dd9ccd46S 		/* Solaris Kerberos */
29047c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(dbunlockerr_fmt),
2905dd9ccd46S 		 progname, dbname, error_message(kret));
29067c478bd9Sstevel@tonic-gate 	 exit_status++;
29077c478bd9Sstevel@tonic-gate     }
290854925bf6Swillf 
290954925bf6Swillf #if 0
291056a424ccSmp     if ((kret = krb5_db_fini(kcontext))) {
2911dd9ccd46S 		/* Solaris Kerberos */
29127c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext(close_err_fmt),
2913dd9ccd46S 		 progname, error_message(kret));
29147c478bd9Sstevel@tonic-gate 	 exit_status++;
29157c478bd9Sstevel@tonic-gate     }
291654925bf6Swillf #endif
29177c478bd9Sstevel@tonic-gate 
29187c478bd9Sstevel@tonic-gate     /* close policy db below */
29197c478bd9Sstevel@tonic-gate 
292054925bf6Swillf     if (exit_status == 0 && !update) {
292154925bf6Swillf 	kret = krb5_db_promote(kcontext, db5util_db_args);
2922*55fea89dSDan Cross 	/*
292354925bf6Swillf 	 * Ignore a not supported error since there is nothing to do about it
292454925bf6Swillf 	 * anyway.
292554925bf6Swillf 	 */
292654925bf6Swillf 	if (kret != 0 && kret != KRB5_PLUGIN_OP_NOTSUPP) {
2927dd9ccd46S 	    /* Solaris Kerberos */
292854925bf6Swillf 	    fprintf(stderr, gettext("%s: cannot make newly loaded database live (%s)\n"),
2929dd9ccd46S 		    progname, error_message(kret));
293054925bf6Swillf 	    exit_status++;
293154925bf6Swillf 	}
293254925bf6Swillf     }
293354925bf6Swillf 
29347c478bd9Sstevel@tonic-gate error:
29357c478bd9Sstevel@tonic-gate     /*
293656a424ccSmp      * If not an update: if there was an error, destroy the temp database,
293756a424ccSmp      * otherwise rename it into place.
29387c478bd9Sstevel@tonic-gate      *
29397c478bd9Sstevel@tonic-gate      * If an update: if there was no error, unlock the database.
29407c478bd9Sstevel@tonic-gate      */
29417c478bd9Sstevel@tonic-gate     if (!update) {
29427c478bd9Sstevel@tonic-gate 	 if (exit_status) {
294354925bf6Swillf 	      kret = krb5_db_destroy(kcontext, db5util_db_args);
2944*55fea89dSDan Cross 	      /*
294554925bf6Swillf 	       * Ignore a not supported error since there is nothing to do about
294654925bf6Swillf 	       * it anyway.
294754925bf6Swillf 	       */
294854925bf6Swillf 	      if (kret != 0 && kret != KRB5_PLUGIN_OP_NOTSUPP) {
2949dd9ccd46S 		   /* Solaris Kerberos */
295054925bf6Swillf 		   fprintf(stderr, gettext(dbdelerr_fmt),
2951dd9ccd46S 			   progname, dbname, error_message(kret));
29527c478bd9Sstevel@tonic-gate 		   exit_status++;
29537c478bd9Sstevel@tonic-gate 	      }
29547c478bd9Sstevel@tonic-gate 	 }
29557c478bd9Sstevel@tonic-gate     }
29567c478bd9Sstevel@tonic-gate 
29577c478bd9Sstevel@tonic-gate     if (dumpfile) {
295856a424ccSmp 	 (void) krb5_lock_file(kcontext, fileno(f), KRB5_LOCKMODE_UNLOCK);
29597c478bd9Sstevel@tonic-gate 	 fclose(f);
29607c478bd9Sstevel@tonic-gate     }
296156a424ccSmp 
29627c478bd9Sstevel@tonic-gate     if (dbname_tmp)
29637c478bd9Sstevel@tonic-gate 	 free(dbname_tmp);
29647c478bd9Sstevel@tonic-gate     krb5_free_context(kcontext);
29657c478bd9Sstevel@tonic-gate }
2966