summaryrefslogtreecommitdiffstats
path: root/src/include/kdb.h
blob: d74e3e3235a12ffeabab641ae6ec9204af901e86 (plain)
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
/*
 * include/krb5/kdb.h
 *
 * Copyright 1990,1991 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.
 * 
 *
 * KDC Database interface definitions.
 */

/*
 * Copyright (C) 1998 by the FundsXpress, INC.
 * 
 * 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 FundsXpress. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  FundsXpress makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/* This API is not considered as stable as the main krb5 API.
 *
 * - We may make arbitrary incompatible changes between feature
 *   releases (e.g. from 1.7 to 1.8).
 * - We will make some effort to avoid making incompatible changes for
 *   bugfix releases, but will make them if necessary.
 */

#ifndef KRB5_KDB5__
#define KRB5_KDB5__

#include <krb5.h>

/* Salt types */
#define KRB5_KDB_SALTTYPE_NORMAL	0
#define KRB5_KDB_SALTTYPE_V4		1
#define KRB5_KDB_SALTTYPE_NOREALM	2
#define KRB5_KDB_SALTTYPE_ONLYREALM	3
#define KRB5_KDB_SALTTYPE_SPECIAL	4
#define KRB5_KDB_SALTTYPE_AFS3		5
#define KRB5_KDB_SALTTYPE_CERTHASH	6

/* Attributes */
#define	KRB5_KDB_DISALLOW_POSTDATED	0x00000001
#define	KRB5_KDB_DISALLOW_FORWARDABLE	0x00000002
#define	KRB5_KDB_DISALLOW_TGT_BASED	0x00000004
#define	KRB5_KDB_DISALLOW_RENEWABLE	0x00000008
#define	KRB5_KDB_DISALLOW_PROXIABLE	0x00000010
#define	KRB5_KDB_DISALLOW_DUP_SKEY	0x00000020
#define	KRB5_KDB_DISALLOW_ALL_TIX	0x00000040
#define	KRB5_KDB_REQUIRES_PRE_AUTH	0x00000080
#define KRB5_KDB_REQUIRES_HW_AUTH	0x00000100
#define	KRB5_KDB_REQUIRES_PWCHANGE	0x00000200
#define KRB5_KDB_DISALLOW_SVR		0x00001000
#define KRB5_KDB_PWCHANGE_SERVICE	0x00002000
#define KRB5_KDB_SUPPORT_DESMD5         0x00004000
#define	KRB5_KDB_NEW_PRINC		0x00008000
#define KRB5_KDB_OK_AS_DELEGATE		0x00100000
#define KRB5_KDB_OK_TO_AUTH_AS_DELEGATE	0x00200000 /* S4U2Self OK */
#define KRB5_KDB_NO_AUTH_DATA_REQUIRED	0x00400000

/* Creation flags */
#define KRB5_KDB_CREATE_BTREE		0x00000001
#define KRB5_KDB_CREATE_HASH		0x00000002

#if !defined(_WIN32)

/*
 * Note --- these structures cannot be modified without changing the
 * database version number in libkdb.a, but should be expandable by
 * adding new tl_data types.
 */
typedef struct _krb5_tl_data {
    struct _krb5_tl_data* tl_data_next;		/* NOT saved */
    krb5_int16 		  tl_data_type;		
    krb5_ui_2		  tl_data_length;	
    krb5_octet 	        * tl_data_contents;	
} krb5_tl_data;

/* 
 * If this ever changes up the version number and make the arrays be as
 * big as necessary.
 *
 * Currently the first type is the enctype and the second is the salt type.
 */
typedef struct _krb5_key_data {
    krb5_int16 		  key_data_ver;		/* Version */
    krb5_int16		  key_data_kvno;	/* Key Version */
    krb5_int16		  key_data_type[2];	/* Array of types */
    krb5_ui_2		  key_data_length[2];	/* Array of lengths */
    krb5_octet 	        * key_data_contents[2];	/* Array of pointers */
} krb5_key_data;

#define KRB5_KDB_V1_KEY_DATA_ARRAY	2	/* # of array elements */

typedef struct _krb5_keysalt {
    krb5_int16		  type;	
    krb5_data		  data;			/* Length, data */
} krb5_keysalt;

typedef struct _krb5_db_entry_new {
    krb5_magic 		  magic;		/* NOT saved */
    krb5_ui_2		  len;			
    krb5_ui_4             mask;                 /* members currently changed/set */	
    krb5_flags 		  attributes;
    krb5_deltat		  max_life;
    krb5_deltat		  max_renewable_life;
    krb5_timestamp 	  expiration;	  	/* When the client expires */
    krb5_timestamp 	  pw_expiration;  	/* When its passwd expires */
    krb5_timestamp 	  last_success;		/* Last successful passwd */
    krb5_timestamp 	  last_failed;		/* Last failed passwd attempt */
    krb5_kvno 	 	  fail_auth_count; 	/* # of failed passwd attempt */
    krb5_int16 		  n_tl_data;
    krb5_int16 		  n_key_data;
    krb5_ui_2		  e_length;		/* Length of extra data */
    krb5_octet		* e_data;		/* Extra data to be saved */

    krb5_principal 	  princ;		/* Length, data */	
    krb5_tl_data	* tl_data;		/* Linked list */
    krb5_key_data       * key_data;		/* Array */
} krb5_db_entry;

typedef struct _osa_policy_ent_t {
    int               version;
    char      *name;
    krb5_ui_4       pw_min_life;
    krb5_ui_4       pw_max_life;
    krb5_ui_4       pw_min_length;
    krb5_ui_4       pw_min_classes;
    krb5_ui_4       pw_history_num;
    krb5_ui_4       policy_refcnt;
    /* Only valid if version > 1 */
    krb5_ui_4       pw_max_fail;                /* pwdMaxFailure */
    krb5_ui_4       pw_failcnt_interval;        /* pwdFailureCountInterval */
    krb5_ui_4       pw_lockout_duration;        /* pwdLockoutDuration */
} osa_policy_ent_rec, *osa_policy_ent_t;

typedef       void    (*osa_adb_iter_policy_func) (void *, osa_policy_ent_t);

typedef struct __krb5_key_salt_tuple {
    krb5_enctype	ks_enctype;
    krb5_int32		ks_salttype;
} krb5_key_salt_tuple;

#define	KRB5_KDB_MAGIC_NUMBER		0xdbdbdbdb
#define KRB5_KDB_V1_BASE_LENGTH		38

#define KRB5_TL_LAST_PWD_CHANGE		0x0001
#define KRB5_TL_MOD_PRINC		0x0002
#define KRB5_TL_KADM_DATA		0x0003
#define KRB5_TL_KADM5_E_DATA		0x0004
#define KRB5_TL_RB1_CHALLENGE		0x0005
#ifdef SECURID
#define KRB5_TL_SECURID_STATE           0x0006
#define KRB5_TL_DB_ARGS                 0x7fff
#endif /* SECURID */
#define KRB5_TL_USER_CERTIFICATE        0x0007
#define KRB5_TL_MKVNO                   0x0008
#define KRB5_TL_ACTKVNO                 0x0009
#define KRB5_TL_MKEY_AUX                0x000a

/* version number for KRB5_TL_ACTKVNO data */
#define KRB5_TL_ACTKVNO_VER     1

/* version number for KRB5_TL_MKEY_AUX data */
#define KRB5_TL_MKEY_AUX_VER    1

typedef struct _krb5_actkvno_node {
    struct _krb5_actkvno_node *next;
    krb5_kvno      act_kvno;
    krb5_timestamp act_time;
} krb5_actkvno_node;

typedef struct _krb5_mkey_aux_node {
    struct _krb5_mkey_aux_node *next;
    krb5_kvno        mkey_kvno; /* kvno of mkey protecting the latest_mkey */
    krb5_key_data    latest_mkey; /* most recent mkey */
} krb5_mkey_aux_node;

typedef struct _krb5_keylist_node {
    krb5_keyblock keyblock;
    krb5_kvno     kvno;
    struct _krb5_keylist_node *next;
} krb5_keylist_node;

/*
 * Determines the number of failed KDC requests before DISALLOW_ALL_TIX is set
 * on the principal.
 */
#define KRB5_MAX_FAIL_COUNT		5

/* XXX depends on knowledge of krb5_parse_name() formats */
#define KRB5_KDB_M_NAME		"K/M"	/* Kerberos/Master */

/* prompts used by default when reading the KDC password from the keyboard. */
#define KRB5_KDC_MKEY_1	"Enter KDC database master key"
#define KRB5_KDC_MKEY_2	"Re-enter KDC database master key to verify"


extern char *krb5_mkey_pwd_prompt1;
extern char *krb5_mkey_pwd_prompt2;

/*
 * These macros specify the encoding of data within the database.
 *
 * Data encoding is little-endian.
 */
#ifdef _KRB5_INT_H
#include "k5-platform.h"
#define	krb5_kdb_decode_int16(cp, i16)	\
	*((krb5_int16 *) &(i16)) = load_16_le(cp)
#define	krb5_kdb_decode_int32(cp, i32)	\
	*((krb5_int32 *) &(i32)) = load_32_le(cp)
#define krb5_kdb_encode_int16(i16, cp)	store_16_le(i16, cp)
#define	krb5_kdb_encode_int32(i32, cp)	store_32_le(i32, cp)
#endif /* _KRB5_INT_H */

#define KRB5_KDB_OPEN_RW                0
#define KRB5_KDB_OPEN_RO                1

#ifndef KRB5_KDB_SRV_TYPE_KDC
#define KRB5_KDB_SRV_TYPE_KDC           0x0100        
#endif

#ifndef KRB5_KDB_SRV_TYPE_ADMIN
#define KRB5_KDB_SRV_TYPE_ADMIN         0x0200  
#endif

#ifndef KRB5_KDB_SRV_TYPE_PASSWD
#define KRB5_KDB_SRV_TYPE_PASSWD        0x0300
#endif

#ifndef KRB5_KDB_SRV_TYPE_OTHER
#define KRB5_KDB_SRV_TYPE_OTHER         0x0400  
#endif

#define KRB5_KDB_OPT_SET_DB_NAME        0
#define KRB5_KDB_OPT_SET_LOCK_MODE      1

#define KRB5_DB_LOCKMODE_SHARED       0x0001
#define KRB5_DB_LOCKMODE_EXCLUSIVE    0x0002
#define KRB5_DB_LOCKMODE_DONTBLOCK    0x0004
#define KRB5_DB_LOCKMODE_PERMANENT    0x0008

/* libkdb.spec */
krb5_error_code krb5_db_setup_lib_handle(krb5_context kcontext);
krb5_error_code krb5_db_open( krb5_context kcontext, char **db_args, int mode );
krb5_error_code krb5_db_init  ( krb5_context kcontext );
krb5_error_code krb5_db_create ( krb5_context kcontext, char **db_args );
krb5_error_code krb5_db_inited  ( krb5_context kcontext );
krb5_error_code kdb5_db_create ( krb5_context kcontext, char **db_args );
krb5_error_code krb5_db_fini ( krb5_context kcontext );
const char * krb5_db_errcode2string ( krb5_context kcontext, long err_code );
krb5_error_code krb5_db_destroy ( krb5_context kcontext, char **db_args );
krb5_error_code krb5_db_promote ( krb5_context kcontext, char **db_args );
krb5_error_code krb5_db_get_age ( krb5_context kcontext, char *db_name, time_t *t );
krb5_error_code krb5_db_set_option ( krb5_context kcontext, int option, void *value );
krb5_error_code krb5_db_lock ( krb5_context kcontext, int lock_mode );
krb5_error_code krb5_db_unlock ( krb5_context kcontext );
krb5_error_code krb5_db_get_principal ( krb5_context kcontext,
					krb5_const_principal search_for,
					krb5_db_entry *entries,
					int *nentries,
					krb5_boolean *more );
krb5_error_code krb5_db_free_principal ( krb5_context kcontext,
					 krb5_db_entry *entry,
					 int count );
krb5_error_code krb5_db_put_principal ( krb5_context kcontext,
					krb5_db_entry *entries,
					int *nentries);
krb5_error_code krb5_db_delete_principal ( krb5_context kcontext,
					   krb5_principal search_for,
					   int *nentries );
krb5_error_code krb5_db_iterate ( krb5_context kcontext,
				  char *match_entry,
				  int (*func) (krb5_pointer, krb5_db_entry *),
				  krb5_pointer func_arg );
krb5_error_code krb5_supported_realms ( krb5_context kcontext,
					char **realms );
krb5_error_code krb5_free_supported_realms ( krb5_context kcontext,
					     char **realms );
krb5_error_code krb5_db_set_master_key_ext ( krb5_context kcontext,
					     char *pwd,
					     krb5_keyblock *key );
krb5_error_code krb5_db_set_mkey ( krb5_context context, 
				   krb5_keyblock *key);
krb5_error_code krb5_db_get_mkey ( krb5_context kcontext,
				   krb5_keyblock **key );

krb5_error_code krb5_db_set_mkey_list( krb5_context context,
                                       krb5_keylist_node * keylist);

krb5_error_code krb5_db_get_mkey_list( krb5_context kcontext,
                                       krb5_keylist_node ** keylist);

krb5_error_code krb5_db_free_master_key ( krb5_context kcontext,
					  krb5_keyblock *key );
krb5_error_code krb5_db_store_master_key  ( krb5_context kcontext, 
					    char *keyfile, 
					    krb5_principal mname,
					    krb5_kvno kvno,
					    krb5_keyblock *key,
					    char *master_pwd);
krb5_error_code krb5_db_store_master_key_list  ( krb5_context kcontext, 
						 char *keyfile, 
						 krb5_principal mname,
						 krb5_keylist_node *keylist,
						 char *master_pwd);
krb5_error_code krb5_db_fetch_mkey  ( krb5_context   context,
				      krb5_principal mname,
				      krb5_enctype   etype,
				      krb5_boolean   fromkeyboard,
				      krb5_boolean   twice,
				      char          *db_args,
                                      krb5_kvno     *kvno,
				      krb5_data     *salt,
				      krb5_keyblock *key);
krb5_error_code krb5_db_verify_master_key ( krb5_context   kcontext,
					    krb5_principal mprinc,
                                            krb5_kvno      kvno,
					    krb5_keyblock  *mkey );
krb5_error_code
krb5_db_fetch_mkey_list( krb5_context    context,
		         krb5_principal  mname,
		         const krb5_keyblock * mkey,
		         krb5_kvno             mkvno,
		         krb5_keylist_node  **mkeys_list );

krb5_error_code
krb5_db_free_mkey_list( krb5_context         context,
		        krb5_keylist_node  *mkey_list );

krb5_error_code
krb5_dbe_find_enctype( krb5_context	kcontext,
		       krb5_db_entry	*dbentp,
		       krb5_int32		ktype,
		       krb5_int32		stype,
		       krb5_int32		kvno,
		       krb5_key_data	**kdatap);


krb5_error_code krb5_dbe_search_enctype ( krb5_context kcontext, 
					  krb5_db_entry *dbentp, 
					  krb5_int32 *start, 
					  krb5_int32 ktype, 
					  krb5_int32 stype, 
					  krb5_int32 kvno, 
					  krb5_key_data **kdatap);

krb5_error_code
krb5_db_setup_mkey_name ( krb5_context context,
			  const char *keyname,
			  const char *realm,
			  char **fullname,
			  krb5_principal *principal);

krb5_error_code
krb5_dbekd_decrypt_key_data( krb5_context 	  context,
			     const krb5_keyblock	* mkey,
			     const krb5_key_data	* key_data,
			     krb5_keyblock 	* dbkey,
			     krb5_keysalt 	* keysalt);

krb5_error_code
krb5_dbekd_encrypt_key_data( krb5_context 		  context,
			     const krb5_keyblock	* mkey,
			     const krb5_keyblock 	* dbkey,
			     const krb5_keysalt		* keysalt,
			     int			  keyver,
			     krb5_key_data	        * key_data);

krb5_error_code
krb5_dbe_fetch_act_key_list(krb5_context          context,
			     krb5_principal       princ,
			     krb5_actkvno_node  **act_key_list);

krb5_error_code
krb5_dbe_find_act_mkey( krb5_context          context,
                        krb5_keylist_node   * mkey_list,
                        krb5_actkvno_node   * act_mkey_list,
                        krb5_kvno           * act_kvno,
                        krb5_keyblock      ** act_mkey);

krb5_error_code
krb5_dbe_find_mkey( krb5_context	 context,
                    krb5_keylist_node * mkey_list,
                    krb5_db_entry      * entry,
                    krb5_keyblock      ** mkey);

krb5_error_code
krb5_dbe_lookup_mkvno( krb5_context    context,
		       krb5_db_entry * entry,
		       krb5_kvno     * mkvno);

krb5_error_code
krb5_dbe_lookup_mod_princ_data( krb5_context          context,
				krb5_db_entry       * entry,
				krb5_timestamp      * mod_time,
				krb5_principal      * mod_princ);
 
krb5_error_code
krb5_dbe_lookup_mkey_aux( krb5_context         context,
		          krb5_db_entry      * entry,
		          krb5_mkey_aux_node ** mkey_aux_data_list);
krb5_error_code
krb5_dbe_update_mkvno( krb5_context    context,
		       krb5_db_entry * entry,
		       krb5_kvno       mkvno);

krb5_error_code
krb5_dbe_lookup_actkvno( krb5_context         context,
		         krb5_db_entry      * entry,
		         krb5_actkvno_node ** actkvno_list);

krb5_error_code
krb5_dbe_update_mkey_aux( krb5_context          context,
		          krb5_db_entry       * entry,
		          krb5_mkey_aux_node  * mkey_aux_data_list);

krb5_error_code
krb5_dbe_update_actkvno(krb5_context    context,
			krb5_db_entry * entry,
			const krb5_actkvno_node *actkvno_list);

krb5_error_code
krb5_dbe_update_last_pwd_change( krb5_context     context,
				 krb5_db_entry  * entry,
				 krb5_timestamp	  stamp);

krb5_error_code
krb5_dbe_lookup_tl_data( krb5_context          context,
			 krb5_db_entry       * entry,
			 krb5_tl_data        * ret_tl_data);

krb5_error_code
krb5_dbe_create_key_data( krb5_context          context,
			  krb5_db_entry       * entry);


krb5_error_code
krb5_dbe_update_mod_princ_data( krb5_context          context,
				krb5_db_entry       * entry,
				krb5_timestamp        mod_date,
				krb5_const_principal  mod_princ);

void *krb5_db_alloc( krb5_context kcontext,
		     void *ptr,
		     size_t size );

void krb5_db_free( krb5_context kcontext,
		   void *ptr);


krb5_error_code
krb5_dbe_lookup_last_pwd_change( krb5_context          context,
				 krb5_db_entry       * entry,
				 krb5_timestamp      * stamp);

krb5_error_code
krb5_dbe_delete_tl_data( krb5_context    context,
                         krb5_db_entry * entry,
                         krb5_int16      tl_data_type);

krb5_error_code
krb5_dbe_update_tl_data( krb5_context          context,
			 krb5_db_entry       * entry,
			 krb5_tl_data        * new_tl_data);

krb5_error_code
krb5_dbe_cpw( krb5_context	  kcontext,
	      krb5_keyblock       * master_key,
	      krb5_key_salt_tuple	* ks_tuple,
	      int			  ks_tuple_count,
	      char 		* passwd,
	      int			  new_kvno,
	      krb5_boolean	  keepold,
	      krb5_db_entry	* db_entry);


krb5_error_code
krb5_dbe_ark( krb5_context	  context,
	      krb5_keyblock       * master_key,
	      krb5_key_salt_tuple	* ks_tuple,
	      int			  ks_tuple_count,
	      krb5_db_entry	* db_entry);

krb5_error_code
krb5_dbe_crk( krb5_context	  context,
	      krb5_keyblock       * master_key,
	      krb5_key_salt_tuple	* ks_tuple,
	      int			  ks_tuple_count,
	      krb5_boolean	  keepold,
	      krb5_db_entry	* db_entry);

krb5_error_code
krb5_dbe_apw( krb5_context	  context,
	      krb5_keyblock       * master_key,
	      krb5_key_salt_tuple	* ks_tuple,
	      int			  ks_tuple_count,
	      char 		* passwd,
	      krb5_db_entry	* db_entry);

int
krb5_db_get_key_data_kvno( krb5_context	   context,
			   int		   count,
			   krb5_key_data * data);


/* default functions. Should not be directly called */
/*
 *   Default functions prototype
 */

krb5_error_code
krb5_dbe_def_search_enctype( krb5_context kcontext, 
			     krb5_db_entry *dbentp, 
			     krb5_int32 *start, 
			     krb5_int32 ktype, 
			     krb5_int32 stype, 
			     krb5_int32 kvno, 
			     krb5_key_data **kdatap);

krb5_error_code
krb5_def_store_mkey( krb5_context context,
		     char *keyfile,
		     krb5_principal mname,
		     krb5_kvno kvno,
		     krb5_keyblock *key,
		     char *master_pwd);

krb5_error_code
krb5_def_store_mkey_list( krb5_context context,
			  char *keyfile,
			  krb5_principal mname,
			  krb5_keylist_node *keylist,
			  char *master_pwd);

krb5_error_code
krb5_db_def_fetch_mkey( krb5_context   context,
			krb5_principal mname,
			krb5_keyblock *key,
			krb5_kvno     *kvno,
			char          *db_args);

krb5_error_code
krb5_def_verify_master_key( krb5_context   context,
			    krb5_principal mprinc,
			    krb5_kvno      kvno,
			    krb5_keyblock *mkey);

krb5_error_code
krb5_def_fetch_mkey_list( krb5_context            context,
			    krb5_principal        mprinc,
			    const krb5_keyblock  *mkey,
			    krb5_kvno             mkvno,
			    krb5_keylist_node  **mkeys_list);

krb5_error_code kdb_def_set_mkey ( krb5_context kcontext,
				   char *pwd,
				   krb5_keyblock *key );

krb5_error_code kdb_def_set_mkey_list ( krb5_context kcontext,
				        krb5_keylist_node *keylist );

krb5_error_code kdb_def_get_mkey ( krb5_context kcontext,
				   krb5_keyblock **key );

krb5_error_code kdb_def_get_mkey_list ( krb5_context kcontext,
				        krb5_keylist_node **keylist );

krb5_error_code
krb5_dbe_def_cpw( krb5_context	  context,
		  krb5_keyblock       * master_key,
		  krb5_key_salt_tuple	* ks_tuple,
		  int			  ks_tuple_count,
		  char 		* passwd,
		  int			  new_kvno,
		  krb5_boolean	  keepold,
		  krb5_db_entry	* db_entry);

krb5_error_code
krb5_def_promote_db(krb5_context, char *, char **);

krb5_error_code
krb5_dbekd_def_decrypt_key_data( krb5_context		  context,
				 const krb5_keyblock	* mkey,
				 const krb5_key_data	* key_data,
				 krb5_keyblock		* dbkey,
				 krb5_keysalt		* keysalt);

krb5_error_code
krb5_dbekd_def_encrypt_key_data( krb5_context		  context,
				 const krb5_keyblock	* mkey,
				 const krb5_keyblock	* dbkey,
				 const krb5_keysalt	* keysalt,
				 int			  keyver,
				 krb5_key_data		* key_data);

krb5_error_code
krb5_dbekd_def_decrypt_key_data( krb5_context 	  context,
				 const krb5_keyblock	* mkey,
				 const krb5_key_data	* key_data,
				 krb5_keyblock 	* dbkey,
				 krb5_keysalt 	* keysalt);

krb5_error_code
krb5_dbekd_def_encrypt_key_data( krb5_context 		  context,
				 const krb5_keyblock	* mkey,
				 const krb5_keyblock 	* dbkey,
				 const krb5_keysalt	* keysalt,
				 int			  keyver,
				 krb5_key_data	        * key_data);

krb5_error_code 
krb5_db_create_policy( krb5_context kcontext, 
		       osa_policy_ent_t policy);

krb5_error_code 
krb5_db_get_policy ( krb5_context kcontext, 
		     char *name, 
		     osa_policy_ent_t *policy,
		     int *nentries);

krb5_error_code 
krb5_db_put_policy( krb5_context kcontext, 
		    osa_policy_ent_t policy);

krb5_error_code 
krb5_db_iter_policy( krb5_context kcontext,
		     char *match_entry,
		     osa_adb_iter_policy_func func,
		     void *data);

krb5_error_code 
krb5_db_delete_policy( krb5_context kcontext, 
		       char *policy);

void 
krb5_db_free_policy( krb5_context kcontext, 
		     osa_policy_ent_t policy);


krb5_error_code
krb5_db_set_context
	(krb5_context, void *db_context);

krb5_error_code
krb5_db_get_context
	(krb5_context, void **db_context);

void
krb5_dbe_free_key_data_contents(krb5_context, krb5_key_data *);

void
krb5_dbe_free_key_list(krb5_context, krb5_keylist_node *);

void
krb5_dbe_free_actkvno_list(krb5_context, krb5_actkvno_node *);

void
krb5_dbe_free_mkey_aux_list(krb5_context, krb5_mkey_aux_node *);

void
krb5_dbe_free_tl_data(krb5_context, krb5_tl_data *);

#define KRB5_KDB_DEF_FLAGS	0

#define KDB_MAX_DB_NAME			128
#define KDB_REALM_SECTION		"realms"
#define KDB_MODULE_POINTER		"database_module"
#define KDB_MODULE_DEF_SECTION		"dbdefaults"
#define KDB_MODULE_SECTION		"dbmodules"
#define KDB_LIB_POINTER			"db_library"
#define KDB_DATABASE_CONF_FILE		DEFAULT_SECURE_PROFILE_PATH
#define KDB_DATABASE_ENV_PROF		KDC_PROFILE_ENV

#define KRB5_KDB_OPEN_RW		0
#define KRB5_KDB_OPEN_RO		1

#define KRB5_KDB_OPT_SET_DB_NAME	0
#define KRB5_KDB_OPT_SET_LOCK_MODE	1

typedef struct _kdb_vftabl {
    short int maj_ver;
    short int min_ver;

    krb5_error_code (*init_library)();
    krb5_error_code (*fini_library)();
    krb5_error_code (*init_module) ( krb5_context kcontext,
				     char * conf_section,
				     char ** db_args,
				     int mode );

    krb5_error_code (*fini_module) ( krb5_context kcontext );

    krb5_error_code (*db_create) ( krb5_context kcontext,
				   char * conf_section,
				   char ** db_args );

    krb5_error_code (*db_destroy) ( krb5_context kcontext,
				    char *conf_section,
				    char ** db_args );

    krb5_error_code (*db_get_age) ( krb5_context kcontext, 
				    char *db_name, 
				    time_t *age );

    krb5_error_code (*db_set_option) ( krb5_context kcontext,
				       int option,
				       void *value );

    krb5_error_code (*db_lock) ( krb5_context kcontext,
				 int mode );

    krb5_error_code (*db_unlock) ( krb5_context kcontext);

    krb5_error_code (*db_get_principal) ( krb5_context kcontext,
					  krb5_const_principal search_for,
					  unsigned int flags,
					  krb5_db_entry *entries,
					  int *nentries,
					  krb5_boolean *more );

    krb5_error_code (*db_free_principal) ( krb5_context kcontext,
					   krb5_db_entry *entry,
					   int count );

    krb5_error_code (*db_put_principal) ( krb5_context kcontext,
					  krb5_db_entry *entries,
					  int *nentries,
					  char **db_args);

    krb5_error_code (*db_delete_principal) ( krb5_context kcontext,
					     krb5_const_principal search_for,
					     int *nentries );

    krb5_error_code (*db_iterate) ( krb5_context kcontext,
				    char *match_entry,
				    int (*func) (krb5_pointer, krb5_db_entry *),
				    krb5_pointer func_arg );

    krb5_error_code (*db_create_policy) ( krb5_context kcontext,
					  osa_policy_ent_t policy );

    krb5_error_code (*db_get_policy) ( krb5_context kcontext,
				       char *name,
				       osa_policy_ent_t *policy,
				       int *cnt);

    krb5_error_code (*db_put_policy) ( krb5_context kcontext,
				       osa_policy_ent_t policy );

    krb5_error_code (*db_iter_policy) ( krb5_context kcontext,
					char *match_entry,
					osa_adb_iter_policy_func func,
					void *data );


    krb5_error_code (*db_delete_policy) ( krb5_context kcontext,
					  char *policy );

    void (*db_free_policy) ( krb5_context kcontext,
			     osa_policy_ent_t val );

    krb5_error_code (*db_supported_realms) ( krb5_context kcontext,
					    char **realms );

    krb5_error_code (*db_free_supported_realms) ( krb5_context kcontext,
						  char **realms );


    const char * (*errcode_2_string) ( krb5_context kcontext,
				       long err_code );

    void (*release_errcode_string) (krb5_context kcontext, const char *msg);

    void * (*db_alloc) (krb5_context kcontext, void *ptr, size_t size);
    void   (*db_free)  (krb5_context kcontext, void *ptr);



    /* optional functions */
    krb5_error_code (*set_master_key)    ( krb5_context kcontext, 
					   char *pwd, 
					   krb5_keyblock *key);

    krb5_error_code (*get_master_key)    ( krb5_context kcontext,
					   krb5_keyblock **key);

    krb5_error_code (*set_master_key_list) ( krb5_context kcontext,
				             krb5_keylist_node *keylist);

    krb5_error_code (*get_master_key_list) ( krb5_context kcontext,
				             krb5_keylist_node **keylist);

    krb5_error_code (*setup_master_key_name) ( krb5_context kcontext,
					       char *keyname,
					       char *realm, 
					       char **fullname, 
					       krb5_principal  *principal);

    krb5_error_code (*store_master_key)  ( krb5_context kcontext, 
					   char *db_arg, 
					   krb5_principal mname,
					   krb5_kvno kvno,
					   krb5_keyblock *key,
					   char *master_pwd);

    krb5_error_code (*fetch_master_key)  ( krb5_context kcontext,
					   krb5_principal mname,
					   krb5_keyblock *key,
					   krb5_kvno *kvno,
					   char *db_args);

    krb5_error_code (*verify_master_key) ( krb5_context kcontext,
					   krb5_principal mprinc,
					   krb5_kvno kvno,
					   krb5_keyblock *mkey );

    krb5_error_code (*fetch_master_key_list) (krb5_context kcontext,
					      krb5_principal mname,
					      const krb5_keyblock *key,
					      krb5_kvno            kvno,
					      krb5_keylist_node  **mkeys_list);

    krb5_error_code (*store_master_key_list)  ( krb5_context kcontext, 
						char *db_arg, 
						krb5_principal mname,
						krb5_keylist_node *keylist,
						char *master_pwd);

    krb5_error_code (*dbe_search_enctype) ( krb5_context kcontext, 
					    krb5_db_entry *dbentp, 
					    krb5_int32 *start, 
					    krb5_int32 ktype, 
					    krb5_int32 stype, 
					    krb5_int32 kvno, 
					    krb5_key_data **kdatap);
    

    krb5_error_code
    (*db_change_pwd) ( krb5_context	  context,
		       krb5_keyblock       * master_key,
		       krb5_key_salt_tuple	* ks_tuple,
		       int			  ks_tuple_count,
		       char 		* passwd,
		       int			  new_kvno,
		       krb5_boolean	  keepold,
		       krb5_db_entry	* db_entry);

    /* Promote a temporary database to be the live one.  */
    krb5_error_code (*promote_db) (krb5_context context,
				   char *conf_section,
				   char **db_args);

    krb5_error_code (*dbekd_decrypt_key_data) ( krb5_context kcontext,
					        const krb5_keyblock *mkey,
						const krb5_key_data *key_data,
						krb5_keyblock *dbkey,
						krb5_keysalt *keysalt );

    krb5_error_code (*dbekd_encrypt_key_data) ( krb5_context kcontext,
						const krb5_keyblock *mkey,
						const krb5_keyblock *dbkey,
						const krb5_keysalt *keyselt,
						int keyver,
						krb5_key_data *key_data );

    krb5_error_code
    (*db_invoke) ( krb5_context context,
		   unsigned int method,
		   const krb5_data *req,
		   krb5_data *rep );
} kdb_vftabl;

#endif /* !defined(_WIN32) */

#endif /* KRB5_KDB5__ */