/
dump.c
3029 lines (2783 loc) · 97.4 KB
/
dump.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* kadmin/dbutil/dump.c - Dump a KDC database */
/*
* Copyright 1990,1991,2001,2006,2008,2009 by the Massachusetts Institute of
* Technology. All Rights Reserved.
*
* Export of this software from the United States of America may
* require a specific license from the United States Government.
* It is the responsibility of any person or organization contemplating
* export to obtain such a license before exporting.
*
* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
* distribute this software and its documentation for any purpose and
* without fee is hereby granted, provided that the above copyright
* notice appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation, and that
* the name of M.I.T. not be used in advertising or publicity pertaining
* to distribution of the software without specific, written prior
* permission. Furthermore if you modify this software you must label
* your software as modified software and not distribute it in such a
* fashion that it might be confused with the original M.I.T. software.
* M.I.T. makes no representations about the suitability of
* this software for any purpose. It is provided "as is" without express
* or implied warranty.
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <stdio.h>
#include <k5-int.h>
#include <kadm5/admin.h>
#include <kadm5/server_internal.h>
#include <kdb.h>
#include <com_err.h>
#include "kdb5_util.h"
#if defined(HAVE_REGEX_H) && defined(HAVE_REGCOMP)
#include <regex.h>
#endif /* HAVE_REGEX_H */
/*
* Needed for master key conversion.
*/
static int mkey_convert;
krb5_keyblock new_master_keyblock;
krb5_kvno new_mkvno;
static int backwards;
static int recursive;
#define K5Q1(x) #x
#define K5Q(x) K5Q1(x)
#define K5CONST_WIDTH_SCANF_STR(x) "%" K5Q(x) "s"
/*
* Use compile(3) if no regcomp present.
*/
#if !defined(HAVE_REGCOMP) && defined(HAVE_REGEXP_H)
#define INIT char *sp = instring;
#define GETC() (*sp++)
#define PEEKC() (*sp)
#define UNGETC(c) (--sp)
#define RETURN(c) return(c)
#define ERROR(c)
#define RE_BUF_SIZE 1024
#include <regexp.h>
#endif /* !HAVE_REGCOMP && HAVE_REGEXP_H */
#define FLAG_VERBOSE 0x1 /* be verbose */
#define FLAG_UPDATE 0x2 /* processing an update */
#define FLAG_OMIT_NRA 0x4 /* avoid dumping non-replicated attrs */
struct dump_args {
char *programname;
FILE *ofile;
krb5_context kcontext;
char **names;
int nnames;
int flags;
};
static krb5_error_code dump_k5beta_iterator (krb5_pointer,
krb5_db_entry *);
static krb5_error_code dump_k5beta6_iterator (krb5_pointer,
krb5_db_entry *);
static krb5_error_code dump_k5beta6_iterator_ext (krb5_pointer,
krb5_db_entry *,
int);
static krb5_error_code dump_k5beta7_princ (krb5_pointer,
krb5_db_entry *);
static krb5_error_code dump_k5beta7_princ_ext (krb5_pointer,
krb5_db_entry *,
int);
static krb5_error_code dump_k5beta7_princ_withpolicy
(krb5_pointer, krb5_db_entry *);
static krb5_error_code dump_ov_princ (krb5_pointer,
krb5_db_entry *);
static void dump_k5beta7_policy (void *, osa_policy_ent_t);
static void dump_r1_8_policy (void *, osa_policy_ent_t);
static void dump_r1_11_policy (void *, osa_policy_ent_t);
typedef krb5_error_code (*dump_func)(krb5_pointer,
krb5_db_entry *);
static int process_k5beta_record (char *, krb5_context,
FILE *, int, int *);
static int process_k5beta6_record (char *, krb5_context,
FILE *, int, int *);
static int process_k5beta7_record (char *, krb5_context,
FILE *, int, int *);
static int process_r1_8_record (char *, krb5_context,
FILE *, int, int *);
static int process_r1_11_record (char *, krb5_context,
FILE *, int, int *);
static int process_ov_record (char *, krb5_context,
FILE *, int, int *);
typedef krb5_error_code (*load_func)(char *, krb5_context,
FILE *, int, int *);
typedef struct _dump_version {
char *name;
char *header;
int updateonly;
int create_kadm5;
int iprop;
int ipropx;
dump_func dump_princ;
osa_adb_iter_policy_func dump_policy;
load_func load_record;
} dump_version;
dump_version old_version = {
"Kerberos version 5 old format",
"kdb5_edit load_dump version 2.0\n",
0,
1,
0,
0,
dump_k5beta_iterator,
NULL,
process_k5beta_record,
};
dump_version beta6_version = {
"Kerberos version 5 beta 6 format",
"kdb5_edit load_dump version 3.0\n",
0,
1,
0,
0,
dump_k5beta6_iterator,
NULL,
process_k5beta6_record,
};
dump_version beta7_version = {
"Kerberos version 5",
"kdb5_util load_dump version 4\n",
0,
0,
0,
0,
dump_k5beta7_princ,
dump_k5beta7_policy,
process_k5beta7_record,
};
dump_version iprop_version = {
"Kerberos iprop version",
"iprop",
0,
0,
1,
0,
dump_k5beta7_princ_withpolicy,
dump_k5beta7_policy,
process_k5beta7_record,
};
dump_version ov_version = {
"OpenV*Secure V1.0",
"OpenV*Secure V1.0\t",
1,
1,
0,
0,
dump_ov_princ,
dump_k5beta7_policy,
process_ov_record
};
dump_version r1_3_version = {
"Kerberos version 5 release 1.3",
"kdb5_util load_dump version 5\n",
0,
0,
0,
0,
dump_k5beta7_princ_withpolicy,
dump_k5beta7_policy,
process_k5beta7_record,
};
dump_version r1_8_version = {
"Kerberos version 5 release 1.8",
"kdb5_util load_dump version 6\n",
0,
0,
0,
0,
dump_k5beta7_princ_withpolicy,
dump_r1_8_policy,
process_r1_8_record,
};
dump_version r1_11_version = {
"Kerberos version 5 release 1.11",
"kdb5_util load_dump version 7\n",
0,
0,
0,
0,
dump_k5beta7_princ_withpolicy,
dump_r1_11_policy,
process_r1_11_record,
};
dump_version ipropx_1_version = {
"Kerberos iprop extensible version",
"ipropx",
0,
0,
1,
1,
dump_k5beta7_princ_withpolicy,
dump_r1_11_policy,
process_r1_11_record,
};
/* External data */
extern char *current_dbname;
extern krb5_boolean dbactive;
extern int exit_status;
extern krb5_context util_context;
extern kadm5_config_params global_params;
extern krb5_db_entry *master_entry;
/* Strings */
#define k5beta_dump_header "kdb5_edit load_dump version 2.0\n"
static const char null_mprinc_name[] = "kdb5_dump@MISSING";
/* Message strings */
#define regex_err _("%s: regular expression error - %s\n")
#define regex_merr _("%s: regular expression match error - %s\n")
#define pname_unp_err _("%s: cannot unparse principal name (%s)\n")
#define mname_unp_err _("%s: cannot unparse modifier name (%s)\n")
#define nokeys_err _("%s: cannot find any standard key for %s\n")
#define sdump_tl_inc_err _("%s: tagged data list inconsistency for %s (counted %d, stored %d)\n")
#define ofopen_error _("%s: cannot open %s for writing (%s)\n")
#define oflock_error _("%s: cannot lock %s (%s)\n")
#define dumprec_err _("%s: error performing %s dump (%s)\n")
#define trash_end_fmt _("%s(%d): ignoring trash at end of line: ")
#define read_nomem _("entry (out of memory)")
#define read_header _("dump entry header")
#define read_negint _("dump entry (unexpected negative numeric field)")
#define read_name_string _("name string")
#define read_key_type _("key type")
#define read_key_data _("key data")
#define read_pr_data1 _("first set of principal attributes")
#define read_mod_name _("modifier name")
#define read_pr_data2 _("second set of principal attributes")
#define read_salt_data _("salt data")
#define read_akey_type _("alternate key type")
#define read_akey_data _("alternate key data")
#define read_asalt_type _("alternate salt type")
#define read_asalt_data _("alternate salt data")
#define read_exp_data _("expansion data")
#define store_err_fmt _("%s(%d): cannot store %s(%s)\n")
#define add_princ_fmt _("%s\n")
#define parse_err_fmt _("%s(%d): cannot parse %s (%s)\n")
#define read_err_fmt _("%s(%d): cannot read %s\n")
#define no_mem_fmt _("%s(%d): no memory for buffers\n")
#define rhead_err_fmt _("%s(%d): cannot match size tokens\n")
#define err_line_fmt _("%s: error processing line %d of %s\n")
#define head_bad_fmt _("%s: dump header bad in %s\n")
#define read_nint_data _("principal static attributes")
#define read_tcontents _("tagged data contents")
#define read_ttypelen _("tagged data type and length")
#define read_kcontents _("key data contents")
#define read_ktypelen _("key data type and length")
#define read_econtents _("extra data contents")
#define no_name_mem_fmt _("%s: cannot get memory for temporary name\n")
#define ctx_err_fmt _("%s: cannot initialize Kerberos context\n")
#define stdin_name _("standard input")
#define remaster_err_fmt _("while re-encoding keys for principal %s with new master key")
#define restfail_fmt _("%s: %s restore failed\n")
#define close_err_fmt _("%s: cannot close database (%s)\n")
#define dbinit_err_fmt _("%s: cannot initialize database (%s)\n")
#define dbdelerr_fmt _("%s: cannot delete bad database %s (%s)\n")
#define dbunlockerr_fmt _("%s: cannot unlock database %s (%s)\n")
#define dbcreaterr_fmt _("%s: cannot create database %s (%s)\n")
#define dfile_err_fmt _("%s: cannot open %s (%s)\n")
static const char oldoption[] = "-old";
static const char b6option[] = "-b6";
static const char b7option[] = "-b7";
static const char ipropoption[] = "-i";
static const char conditionaloption[] = "-c";
static const char verboseoption[] = "-verbose";
static const char updateoption[] = "-update";
static const char hashoption[] = "-hash";
static const char ovoption[] = "-ov";
static const char r13option[] = "-r13";
static const char r18option[] = "-r18";
static const char dump_tmptrail[] = "~";
/*
* Re-encrypt the key_data with the new master key...
*/
krb5_error_code master_key_convert(context, db_entry)
krb5_context context;
krb5_db_entry * db_entry;
{
krb5_error_code retval;
krb5_keyblock v5plainkey, *key_ptr;
krb5_keysalt keysalt;
int i, j;
krb5_key_data new_key_data, *key_data;
krb5_boolean is_mkey;
krb5_kvno kvno;
is_mkey = krb5_principal_compare(context, master_princ, db_entry->princ);
if (is_mkey) {
retval = add_new_mkey(context, db_entry, &new_master_keyblock, new_mkvno);
if (retval)
return retval;
} else {
for (i=0; i < db_entry->n_key_data; i++) {
krb5_keyblock *tmp_mkey;
key_data = &db_entry->key_data[i];
retval = krb5_dbe_find_mkey(context, db_entry, &tmp_mkey);
if (retval)
return retval;
retval = krb5_dbe_decrypt_key_data(context, tmp_mkey, key_data,
&v5plainkey, &keysalt);
if (retval)
return retval;
memset(&new_key_data, 0, sizeof(new_key_data));
key_ptr = &v5plainkey;
kvno = (krb5_kvno) key_data->key_data_kvno;
retval = krb5_dbe_encrypt_key_data(context, &new_master_keyblock,
key_ptr, &keysalt, (int) kvno,
&new_key_data);
if (retval)
return retval;
krb5_free_keyblock_contents(context, &v5plainkey);
for (j = 0; j < key_data->key_data_ver; j++) {
if (key_data->key_data_length[j]) {
free(key_data->key_data_contents[j]);
}
}
*key_data = new_key_data;
}
assert(new_mkvno > 0);
retval = krb5_dbe_update_mkvno(context, db_entry, new_mkvno);
if (retval)
return retval;
}
return 0;
}
/* Create temp file for new dump to be named ofile. */
static FILE *
create_ofile(char *ofile, char **tmpname)
{
int fd = -1;
FILE *f;
*tmpname = NULL;
if (asprintf(tmpname, "%s-XXXXXX", ofile) < 0)
goto error;
fd = mkstemp(*tmpname);
if (fd == -1)
goto error;
f = fdopen(fd, "w+");
if (f != NULL)
return f;
error:
com_err(progname, errno,
_("while allocating temporary filename dump"));
if (fd >= 0)
unlink(*tmpname);
exit(1);
}
/* Rename new dump file into place */
static void
finish_ofile(char *ofile, char **tmpname)
{
if (rename(*tmpname, ofile) == -1) {
com_err(progname, errno, _("while renaming dump file into place"));
exit(1);
}
free(*tmpname);
*tmpname = NULL;
}
/*
* Read the dump header. Returns 1 on success, 0 if the file is not a
* recognized iprop dump format.
*/
static int
parse_iprop_header(char *buf, dump_version **dv, uint32_t *last_sno,
uint32_t *last_seconds, uint32_t *last_useconds)
{
char head[128];
int nread;
uint32_t u[4];
uint32_t *up = &u[0];
nread = sscanf(buf, "%127s %u %u %u %u", head, &u[0], &u[1], &u[2], &u[3]);
if (nread < 1)
return 0;
if (!strcmp(head, ipropx_1_version.header)) {
if (nread != 5)
return 0;
if (u[0] == IPROPX_VERSION_0)
*dv = &iprop_version;
else if (u[0] == IPROPX_VERSION_1)
*dv = &ipropx_1_version;
else {
fprintf(stderr, _("%s: Unknown iprop dump version %d\n"),
progname, u[0]);
return 0;
}
up = &u[1];
} else if (!strcmp(head, iprop_version.header)) {
if (nread != 4)
return 0;
*dv = &iprop_version;
} else {
fprintf(stderr, "Invalid iprop header\n");
return 0;
}
*last_sno = *(up++);
*last_seconds = *(up++);
*last_useconds = *(up++);
return 1;
}
/*
* Return 1 if the {sno, timestamp} in an existing dump file is in the
* ulog, else return 0.
*/
static int
current_dump_sno_in_ulog(char *ifile, kdb_log_context *log_ctx)
{
dump_version *junk;
uint32_t last_sno, last_seconds, last_useconds;
char buf[BUFSIZ];
FILE *f;
uint32_t i, indx;
uint32_t ulogentries = log_ctx->ulogentries;
kdb_hlog_t *ulog = log_ctx->ulog;
kdb_ent_header_t * indx_log;
if (ulog->kdb_last_sno == 0)
return 0; /* nothing in ulog */
f = fopen(ifile, "r");
if (f == NULL)
return 0; /* aliasing other errors to ENOENT here is OK */
if (fgets(buf, sizeof(buf), f) == NULL)
return errno ? -1 : 0;
fclose(f);
if (!parse_iprop_header(buf, &junk, &last_sno, &last_seconds,
&last_useconds))
return 0;
/*
* Perform a quick test, but it can't be relied upon as the only test.
*/
if (ulog->kdb_first_sno > last_sno ||
ulog->kdb_first_time.seconds > last_seconds ||
(ulog->kdb_first_time.seconds == last_seconds &&
ulog->kdb_first_time.useconds > last_useconds))
return 0;
/*
* If the dump is the most recent, don't iterate through the ulog.
* It enables hierarchical propagation even when the slave hasn't
* collected any updates in its ulog yet.
*/
if (ulog->kdb_last_time.seconds == last_seconds &&
ulog->kdb_last_time.useconds == last_useconds &&
ulog->kdb_last_sno == last_sno)
return 1;
/*
* Iterate through the ulog looking for an exact match.
*/
for (i = ulog->kdb_last_sno - ulog->kdb_num; i < ulog->kdb_last_sno; i++) {
indx = i % ulogentries;
indx_log = (kdb_ent_header_t *)INDEX(ulog, indx);
if (indx_log->kdb_umagic != KDB_ULOG_MAGIC) {
(void) fprintf(stderr, _("Corrupt update entry\n"));
return 0;
}
if (indx_log->kdb_time.seconds == last_seconds &&
indx_log->kdb_time.useconds == last_useconds &&
indx_log->kdb_entry_sno == last_sno)
return 1;
}
return 0;
}
/* Create the .dump_ok file. */
static int
prep_ok_file(krb5_context context, char *file_name, int *fd)
{
static char ok[]=".dump_ok";
krb5_error_code retval;
char *file_ok;
if (asprintf(&file_ok, "%s%s", file_name, ok) < 0) {
com_err(progname, ENOMEM, _("while allocating dump_ok filename"));
exit_status++;
return 0;
}
*fd = open(file_ok, O_WRONLY | O_CREAT | O_TRUNC, 0600);
if (*fd == -1) {
com_err(progname, errno, _("while creating 'ok' file, '%s'"), file_ok);
exit_status++;
free(file_ok);
return 0;
}
retval = krb5_lock_file(context, *fd, KRB5_LOCKMODE_EXCLUSIVE);
if (retval) {
com_err(progname, retval, _("while locking 'ok' file, '%s'"), file_ok);
return 0;
}
return 1;
}
/*
* Update the "ok" file.
*/
static void
update_ok_file(krb5_context context, int fd)
{
write(fd, "", 1);
krb5_lock_file(context, fd, KRB5_LOCKMODE_UNLOCK);
close(fd);
}
/*
* name_matches() - See if a principal name matches a regular expression
* or string.
*/
static int
name_matches(name, arglist)
char *name;
struct dump_args *arglist;
{
#if HAVE_REGCOMP
regex_t match_exp;
regmatch_t match_match;
int match_error;
char match_errmsg[BUFSIZ];
size_t errmsg_size;
#elif HAVE_REGEXP_H
char regexp_buffer[RE_BUF_SIZE];
#elif HAVE_RE_COMP
extern char *re_comp();
char *re_result;
#endif /* HAVE_RE_COMP */
int i, match;
/*
* Plow, brute force, through the list of names/regular expressions.
*/
match = (arglist->nnames) ? 0 : 1;
for (i=0; i<arglist->nnames; i++) {
#if HAVE_REGCOMP
/*
* Compile the regular expression.
*/
match_error = regcomp(&match_exp, arglist->names[i], REG_EXTENDED);
if (match_error) {
errmsg_size = regerror(match_error,
&match_exp,
match_errmsg,
sizeof(match_errmsg));
fprintf(stderr, regex_err, arglist->programname, match_errmsg);
break;
}
/*
* See if we have a match.
*/
match_error = regexec(&match_exp, name, 1, &match_match, 0);
if (match_error) {
if (match_error != REG_NOMATCH) {
errmsg_size = regerror(match_error,
&match_exp,
match_errmsg,
sizeof(match_errmsg));
fprintf(stderr, regex_merr,
arglist->programname, match_errmsg);
break;
}
}
else {
/*
* We have a match. See if it matches the whole
* name.
*/
if ((match_match.rm_so == 0) &&
((size_t)match_match.rm_eo == strlen(name)))
match = 1;
}
regfree(&match_exp);
#elif HAVE_REGEXP_H
/*
* Compile the regular expression.
*/
compile(arglist->names[i],
regexp_buffer,
®exp_buffer[RE_BUF_SIZE],
'\0');
if (step(name, regexp_buffer)) {
if ((loc1 == name) &&
(loc2 == &name[strlen(name)]))
match = 1;
}
#elif HAVE_RE_COMP
/*
* Compile the regular expression.
*/
if (re_result = re_comp(arglist->names[i])) {
fprintf(stderr, regex_err, arglist->programname, re_result);
break;
}
if (re_exec(name))
match = 1;
#else /* HAVE_RE_COMP */
/*
* If no regular expression support, then just compare the strings.
*/
if (!strcmp(arglist->names[i], name))
match = 1;
#endif /* HAVE_REGCOMP */
if (match)
break;
}
return(match);
}
static krb5_error_code
find_enctype(dbentp, enctype, salttype, kentp)
krb5_db_entry *dbentp;
krb5_enctype enctype;
krb5_int32 salttype;
krb5_key_data **kentp;
{
int i;
int maxkvno;
krb5_key_data *datap;
maxkvno = -1;
datap = (krb5_key_data *) NULL;
for (i=0; i<dbentp->n_key_data; i++) {
if (( (krb5_enctype)dbentp->key_data[i].key_data_type[0] == enctype) &&
((dbentp->key_data[i].key_data_type[1] == salttype) ||
(salttype < 0))) {
maxkvno = dbentp->key_data[i].key_data_kvno;
datap = &dbentp->key_data[i];
}
}
if (maxkvno >= 0) {
*kentp = datap;
return(0);
}
return(ENOENT);
}
#if 0
/*
* dump_k5beta_header() - Make a dump header that is recognizable by Kerberos
* Version 5 Beta 5 and previous releases.
*/
static krb5_error_code
dump_k5beta_header(arglist)
struct dump_args *arglist;
{
/* The old header consists of the leading string */
fprintf(arglist->ofile, k5beta_dump_header);
return(0);
}
#endif
/*
* dump_k5beta_iterator() - Dump an entry in a format that is usable
* by Kerberos Version 5 Beta 5 and previous
* releases.
*/
static krb5_error_code
dump_k5beta_iterator(ptr, entry)
krb5_pointer ptr;
krb5_db_entry *entry;
{
krb5_error_code retval;
struct dump_args *arg;
char *name, *mod_name;
krb5_principal mod_princ;
krb5_key_data *pkey, *akey, nullkey;
krb5_timestamp mod_date, last_pwd_change;
int i;
/* Initialize */
arg = (struct dump_args *) ptr;
name = (char *) NULL;
mod_name = (char *) NULL;
memset(&nullkey, 0, sizeof(nullkey));
/*
* Flatten the principal name.
*/
if ((retval = krb5_unparse_name(arg->kcontext,
entry->princ,
&name))) {
fprintf(stderr, pname_unp_err,
arg->programname, error_message(retval));
return(retval);
}
/*
* Re-encode the keys in the new master key, if necessary.
*/
if (mkey_convert) {
retval = master_key_convert(arg->kcontext, entry);
if (retval) {
com_err(arg->programname, retval, remaster_err_fmt, name);
return retval;
}
}
/*
* If we don't have any match strings, or if our name matches, then
* proceed with the dump, otherwise, just forget about it.
*/
if (!arg->nnames || name_matches(name, arg)) {
/*
* Deserialize the modifier record.
*/
mod_name = (char *) NULL;
mod_princ = NULL;
last_pwd_change = mod_date = 0;
pkey = akey = (krb5_key_data *) NULL;
if (!(retval = krb5_dbe_lookup_mod_princ_data(arg->kcontext,
entry,
&mod_date,
&mod_princ))) {
if (mod_princ) {
/*
* Flatten the modifier name.
*/
if ((retval = krb5_unparse_name(arg->kcontext,
mod_princ,
&mod_name)))
fprintf(stderr, mname_unp_err, arg->programname,
error_message(retval));
krb5_free_principal(arg->kcontext, mod_princ);
}
}
if (!mod_name)
mod_name = strdup(null_mprinc_name);
/*
* Find the last password change record and set it straight.
*/
if ((retval =
krb5_dbe_lookup_last_pwd_change(arg->kcontext, entry,
&last_pwd_change))) {
fprintf(stderr, nokeys_err, arg->programname, name);
free(mod_name);
free(name);
return(retval);
}
/*
* Find the 'primary' key and the 'alternate' key.
*/
if ((retval = find_enctype(entry,
ENCTYPE_DES_CBC_CRC,
KRB5_KDB_SALTTYPE_NORMAL,
&pkey)) &&
(retval = find_enctype(entry,
ENCTYPE_DES_CBC_CRC,
KRB5_KDB_SALTTYPE_V4,
&akey))) {
fprintf(stderr, nokeys_err, arg->programname, name);
free(mod_name);
free(name);
return(retval);
}
/* If we only have one type, then ship it out as the primary. */
if (!pkey && akey) {
pkey = akey;
akey = &nullkey;
}
else {
if (!akey)
akey = &nullkey;
}
/*
* First put out strings representing the length of the variable
* length data in this record, then the name and the primary key type.
*/
fprintf(arg->ofile, "%lu\t%lu\t%d\t%d\t%d\t%d\t%s\t%d\t",
(unsigned long) strlen(name),
(unsigned long) strlen(mod_name),
(krb5_int32) pkey->key_data_length[0],
(krb5_int32) akey->key_data_length[0],
(krb5_int32) pkey->key_data_length[1],
(krb5_int32) akey->key_data_length[1],
name,
(krb5_int32) pkey->key_data_type[0]);
for (i=0; i<pkey->key_data_length[0]; i++) {
fprintf(arg->ofile, "%02x", pkey->key_data_contents[0][i]);
}
/*
* Second, print out strings representing the standard integer
* data in this record.
*/
fprintf(arg->ofile,
"\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",
(krb5_int32) pkey->key_data_kvno,
entry->max_life, entry->max_renewable_life,
1 /* Fake mkvno */, entry->expiration, entry->pw_expiration,
last_pwd_change,
(arg->flags & FLAG_OMIT_NRA) ? 0 : entry->last_success,
(arg->flags & FLAG_OMIT_NRA) ? 0 : entry->last_failed,
(arg->flags & FLAG_OMIT_NRA) ? 0 : entry->fail_auth_count,
mod_name, mod_date,
entry->attributes, pkey->key_data_type[1]);
/* Pound out the salt data, if present. */
for (i=0; i<pkey->key_data_length[1]; i++) {
fprintf(arg->ofile, "%02x", pkey->key_data_contents[1][i]);
}
/* Pound out the alternate key type and contents */
fprintf(arg->ofile, "\t%u\t", akey->key_data_type[0]);
for (i=0; i<akey->key_data_length[0]; i++) {
fprintf(arg->ofile, "%02x", akey->key_data_contents[0][i]);
}
/* Pound out the alternate salt type and contents */
fprintf(arg->ofile, "\t%u\t", akey->key_data_type[1]);
for (i=0; i<akey->key_data_length[1]; i++) {
fprintf(arg->ofile, "%02x", akey->key_data_contents[1][i]);
}
/* Pound out the expansion data. (is null) */
for (i=0; i < 8; i++) {
fprintf(arg->ofile, "\t%u", 0);
}
fprintf(arg->ofile, ";\n");
/* If we're blabbing, do it */
if (arg->flags & FLAG_VERBOSE)
fprintf(stderr, "%s\n", name);
free(mod_name);
}
free(name);
return(0);
}
/*
* dump_k5beta6_iterator() - Output a dump record in krb5b6 format.
*/
static krb5_error_code
dump_k5beta6_iterator(ptr, entry)
krb5_pointer ptr;
krb5_db_entry *entry;
{
return dump_k5beta6_iterator_ext(ptr, entry, 0);
}
/*
* Dumps TL data; common to principals and policies.
*
* If filter_kadm then the KRB5_TL_KADM_DATA (where a principal's policy
* name is stored) is filtered out. This is for dump formats that don't
* support policies.
*/
static void
dump_tl_data(FILE *ofile, krb5_tl_data *tlp, krb5_boolean filter_kadm)
{
int i;
for (; tlp; tlp = tlp->tl_data_next) {
if (tlp->tl_data_type == KRB5_TL_KADM_DATA && filter_kadm)
continue;
fprintf(ofile, "\t%d\t%d\t",
(int) tlp->tl_data_type,
(int) tlp->tl_data_length);
if (tlp->tl_data_length) {
for (i = 0; i < tlp->tl_data_length; i++)
fprintf(ofile, "%02x", tlp->tl_data_contents[i]);
} else {
fprintf(ofile, "%d", -1);
}
}
}
static krb5_error_code
dump_k5beta6_iterator_ext(ptr, entry, kadm)
krb5_pointer ptr;
krb5_db_entry *entry;
int kadm;
{
krb5_error_code retval;
struct dump_args *arg;
char *name;
krb5_tl_data *tlp;
krb5_key_data *kdata;
int counter, skip, i, j;
/* Initialize */
arg = (struct dump_args *) ptr;
name = (char *) NULL;
/*
* Flatten the principal name.
*/
if ((retval = krb5_unparse_name(arg->kcontext,
entry->princ,
&name))) {
fprintf(stderr, pname_unp_err,
arg->programname, error_message(retval));
return(retval);
}
/*
* Re-encode the keys in the new master key, if necessary.
*/
if (mkey_convert) {
retval = master_key_convert(arg->kcontext, entry);
if (retval) {
com_err(arg->programname, retval, remaster_err_fmt, name);
return retval;
}
}
/*
* If we don't have any match strings, or if our name matches, then
* proceed with the dump, otherwise, just forget about it.
*/
if (!arg->nnames || name_matches(name, arg)) {
/*
* We'd like to just blast out the contents as they would appear in
* the database so that we can just suck it back in, but it doesn't
* lend itself to easy editing.
*/
/*
* The dump format is as follows:
* len strlen(name) n_tl_data n_key_data e_length
* name
* attributes max_life max_renewable_life expiration
* pw_expiration last_success last_failed fail_auth_count
* n_tl_data*[type length <contents>]
* n_key_data*[ver kvno ver*(type length <contents>)]
* <e_data>
* Fields which are not encapsulated by angle-brackets are to appear
* verbatim. A bracketed field's absence is indicated by a -1 in its
* place
*/
/*
* Make sure that the tagged list is reasonably correct.
*/
counter = skip = 0;
for (tlp = entry->tl_data; tlp; tlp = tlp->tl_data_next) {
/*
* don't dump tl data types we know aren't understood by
* earlier revisions [krb5-admin/89]
*/
switch (tlp->tl_data_type) {
case KRB5_TL_KADM_DATA: