xref: /freebsd/crypto/krb5/src/tests/create/kdb5_mkdums.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* tests/create/kdb5_mkdums.c */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert  * Copyright 1990,1991 by the Massachusetts Institute of Technology.
5*7f2fe78bSCy Schubert  * All Rights Reserved.
6*7f2fe78bSCy Schubert  *
7*7f2fe78bSCy Schubert  * Export of this software from the United States of America may
8*7f2fe78bSCy Schubert  *   require a specific license from the United States Government.
9*7f2fe78bSCy Schubert  *   It is the responsibility of any person or organization contemplating
10*7f2fe78bSCy Schubert  *   export to obtain such a license before exporting.
11*7f2fe78bSCy Schubert  *
12*7f2fe78bSCy Schubert  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13*7f2fe78bSCy Schubert  * distribute this software and its documentation for any purpose and
14*7f2fe78bSCy Schubert  * without fee is hereby granted, provided that the above copyright
15*7f2fe78bSCy Schubert  * notice appear in all copies and that both that copyright notice and
16*7f2fe78bSCy Schubert  * this permission notice appear in supporting documentation, and that
17*7f2fe78bSCy Schubert  * the name of M.I.T. not be used in advertising or publicity pertaining
18*7f2fe78bSCy Schubert  * to distribution of the software without specific, written prior
19*7f2fe78bSCy Schubert  * permission.  Furthermore if you modify this software you must label
20*7f2fe78bSCy Schubert  * your software as modified software and not distribute it in such a
21*7f2fe78bSCy Schubert  * fashion that it might be confused with the original M.I.T. software.
22*7f2fe78bSCy Schubert  * M.I.T. makes no representations about the suitability of
23*7f2fe78bSCy Schubert  * this software for any purpose.  It is provided "as is" without express
24*7f2fe78bSCy Schubert  * or implied warranty.
25*7f2fe78bSCy Schubert  */
26*7f2fe78bSCy Schubert 
27*7f2fe78bSCy Schubert /*
28*7f2fe78bSCy Schubert  *
29*7f2fe78bSCy Schubert  * Edit a KDC database.
30*7f2fe78bSCy Schubert  */
31*7f2fe78bSCy Schubert 
32*7f2fe78bSCy Schubert #include "k5-int.h"
33*7f2fe78bSCy Schubert #include "kdb.h"
34*7f2fe78bSCy Schubert #include "com_err.h"
35*7f2fe78bSCy Schubert #include <ss/ss.h>
36*7f2fe78bSCy Schubert #include <stdio.h>
37*7f2fe78bSCy Schubert 
38*7f2fe78bSCy Schubert 
39*7f2fe78bSCy Schubert #define REALM_SEP       '@'
40*7f2fe78bSCy Schubert #define REALM_SEP_STR   "@"
41*7f2fe78bSCy Schubert 
42*7f2fe78bSCy Schubert struct mblock {
43*7f2fe78bSCy Schubert     krb5_deltat max_life;
44*7f2fe78bSCy Schubert     krb5_deltat max_rlife;
45*7f2fe78bSCy Schubert     krb5_timestamp expiration;
46*7f2fe78bSCy Schubert     krb5_flags flags;
47*7f2fe78bSCy Schubert     krb5_kvno mkvno;
48*7f2fe78bSCy Schubert } mblock = {                            /* XXX */
49*7f2fe78bSCy Schubert     KRB5_KDB_MAX_LIFE,
50*7f2fe78bSCy Schubert     KRB5_KDB_MAX_RLIFE,
51*7f2fe78bSCy Schubert     KRB5_KDB_EXPIRATION,
52*7f2fe78bSCy Schubert     KRB5_KDB_DEF_FLAGS,
53*7f2fe78bSCy Schubert     1
54*7f2fe78bSCy Schubert };
55*7f2fe78bSCy Schubert 
56*7f2fe78bSCy Schubert int set_dbname_help (char *, char *);
57*7f2fe78bSCy Schubert 
58*7f2fe78bSCy Schubert static void
usage(who,status)59*7f2fe78bSCy Schubert usage(who, status)
60*7f2fe78bSCy Schubert     char *who;
61*7f2fe78bSCy Schubert     int status;
62*7f2fe78bSCy Schubert {
63*7f2fe78bSCy Schubert     fprintf(stderr,
64*7f2fe78bSCy Schubert             "usage: %s -p prefix -n num_to_create [-d dbpathname] [-r realmname]\n",
65*7f2fe78bSCy Schubert             who);
66*7f2fe78bSCy Schubert     fprintf(stderr, "\t [-D depth] [-k enctype] [-M mkeyname]\n");
67*7f2fe78bSCy Schubert 
68*7f2fe78bSCy Schubert     exit(status);
69*7f2fe78bSCy Schubert }
70*7f2fe78bSCy Schubert 
71*7f2fe78bSCy Schubert int master_princ_set = 0;
72*7f2fe78bSCy Schubert krb5_keyblock master_keyblock;
73*7f2fe78bSCy Schubert krb5_principal master_princ;
74*7f2fe78bSCy Schubert krb5_pointer master_random;
75*7f2fe78bSCy Schubert krb5_context test_context;
76*7f2fe78bSCy Schubert 
77*7f2fe78bSCy Schubert static char *progname;
78*7f2fe78bSCy Schubert static char *cur_realm = 0;
79*7f2fe78bSCy Schubert static char *mkey_name = 0;
80*7f2fe78bSCy Schubert static char *mkey_password = 0;
81*7f2fe78bSCy Schubert static krb5_boolean manual_mkey = FALSE;
82*7f2fe78bSCy Schubert 
83*7f2fe78bSCy Schubert void add_princ (krb5_context, char *);
84*7f2fe78bSCy Schubert 
85*7f2fe78bSCy Schubert int
main(argc,argv)86*7f2fe78bSCy Schubert main(argc, argv)
87*7f2fe78bSCy Schubert     int argc;
88*7f2fe78bSCy Schubert     char *argv[];
89*7f2fe78bSCy Schubert {
90*7f2fe78bSCy Schubert     extern char *optarg;
91*7f2fe78bSCy Schubert     int optchar, i, n;
92*7f2fe78bSCy Schubert     char tmp[4096], tmp2[BUFSIZ], *str_newprinc;
93*7f2fe78bSCy Schubert 
94*7f2fe78bSCy Schubert     krb5_error_code retval;
95*7f2fe78bSCy Schubert     char *dbname = 0;
96*7f2fe78bSCy Schubert     int enctypedone = 0;
97*7f2fe78bSCy Schubert     int num_to_create;
98*7f2fe78bSCy Schubert     char principal_string[BUFSIZ];
99*7f2fe78bSCy Schubert     char *suffix = 0;
100*7f2fe78bSCy Schubert     size_t suffix_size = 0;
101*7f2fe78bSCy Schubert     int depth;
102*7f2fe78bSCy Schubert 
103*7f2fe78bSCy Schubert     krb5_init_context(&test_context);
104*7f2fe78bSCy Schubert 
105*7f2fe78bSCy Schubert     if (strrchr(argv[0], '/'))
106*7f2fe78bSCy Schubert         argv[0] = strrchr(argv[0], '/')+1;
107*7f2fe78bSCy Schubert 
108*7f2fe78bSCy Schubert     progname = argv[0];
109*7f2fe78bSCy Schubert 
110*7f2fe78bSCy Schubert     memset(principal_string, 0, sizeof(principal_string));
111*7f2fe78bSCy Schubert     num_to_create = 0;
112*7f2fe78bSCy Schubert     depth = 1;
113*7f2fe78bSCy Schubert 
114*7f2fe78bSCy Schubert     while ((optchar = getopt(argc, argv, "D:P:p:n:d:r:k:M:e:m")) != -1) {
115*7f2fe78bSCy Schubert         switch(optchar) {
116*7f2fe78bSCy Schubert         case 'D':
117*7f2fe78bSCy Schubert             depth = atoi(optarg);       /* how deep to go */
118*7f2fe78bSCy Schubert             break;
119*7f2fe78bSCy Schubert         case 'P':               /* Only used for testing!!! */
120*7f2fe78bSCy Schubert             mkey_password = optarg;
121*7f2fe78bSCy Schubert             break;
122*7f2fe78bSCy Schubert         case 'p':                       /* prefix name to create */
123*7f2fe78bSCy Schubert             strncpy(principal_string, optarg, sizeof(principal_string) - 1);
124*7f2fe78bSCy Schubert             principal_string[sizeof(principal_string) - 1] = '\0';
125*7f2fe78bSCy Schubert             suffix = principal_string + strlen(principal_string);
126*7f2fe78bSCy Schubert             suffix_size = sizeof(principal_string) -
127*7f2fe78bSCy Schubert                 (suffix - principal_string);
128*7f2fe78bSCy Schubert             break;
129*7f2fe78bSCy Schubert         case 'n':                        /* how many to create */
130*7f2fe78bSCy Schubert             num_to_create = atoi(optarg);
131*7f2fe78bSCy Schubert             break;
132*7f2fe78bSCy Schubert         case 'd':                       /* set db name */
133*7f2fe78bSCy Schubert             dbname = optarg;
134*7f2fe78bSCy Schubert             break;
135*7f2fe78bSCy Schubert         case 'r':
136*7f2fe78bSCy Schubert             cur_realm = optarg;
137*7f2fe78bSCy Schubert             break;
138*7f2fe78bSCy Schubert         case 'k':
139*7f2fe78bSCy Schubert             master_keyblock.enctype = atoi(optarg);
140*7f2fe78bSCy Schubert             enctypedone++;
141*7f2fe78bSCy Schubert             break;
142*7f2fe78bSCy Schubert         case 'M':                       /* master key name in DB */
143*7f2fe78bSCy Schubert             mkey_name = optarg;
144*7f2fe78bSCy Schubert             break;
145*7f2fe78bSCy Schubert         case 'm':
146*7f2fe78bSCy Schubert             manual_mkey = TRUE;
147*7f2fe78bSCy Schubert             break;
148*7f2fe78bSCy Schubert         case '?':
149*7f2fe78bSCy Schubert         default:
150*7f2fe78bSCy Schubert             usage(progname, 1);
151*7f2fe78bSCy Schubert             /*NOTREACHED*/
152*7f2fe78bSCy Schubert         }
153*7f2fe78bSCy Schubert     }
154*7f2fe78bSCy Schubert 
155*7f2fe78bSCy Schubert     if (!(num_to_create && suffix)) usage(progname, 1);
156*7f2fe78bSCy Schubert 
157*7f2fe78bSCy Schubert     if (!enctypedone)
158*7f2fe78bSCy Schubert         master_keyblock.enctype = DEFAULT_KDC_ENCTYPE;
159*7f2fe78bSCy Schubert 
160*7f2fe78bSCy Schubert     if (!krb5_c_valid_enctype(master_keyblock.enctype)) {
161*7f2fe78bSCy Schubert         com_err(progname, KRB5_PROG_ETYPE_NOSUPP,
162*7f2fe78bSCy Schubert                 "while setting up enctype %d", master_keyblock.enctype);
163*7f2fe78bSCy Schubert         exit(1);
164*7f2fe78bSCy Schubert     }
165*7f2fe78bSCy Schubert 
166*7f2fe78bSCy Schubert     if (!dbname)
167*7f2fe78bSCy Schubert         dbname = DEFAULT_KDB_FILE;      /* XXX? */
168*7f2fe78bSCy Schubert 
169*7f2fe78bSCy Schubert     if (!cur_realm) {
170*7f2fe78bSCy Schubert         if ((retval = krb5_get_default_realm(test_context, &cur_realm))) {
171*7f2fe78bSCy Schubert             com_err(progname, retval, "while retrieving default realm name");
172*7f2fe78bSCy Schubert             exit(1);
173*7f2fe78bSCy Schubert         }
174*7f2fe78bSCy Schubert     }
175*7f2fe78bSCy Schubert     if ((retval = set_dbname_help(progname, dbname)))
176*7f2fe78bSCy Schubert         exit(retval);
177*7f2fe78bSCy Schubert 
178*7f2fe78bSCy Schubert     for (n = 1; n <= num_to_create; n++) {
179*7f2fe78bSCy Schubert         /* build the new principal name */
180*7f2fe78bSCy Schubert         /* we can't pick random names because we need to generate all the names
181*7f2fe78bSCy Schubert            again given a prefix and count to test the db lib and kdb */
182*7f2fe78bSCy Schubert         (void) snprintf(suffix, suffix_size, "%d", n);
183*7f2fe78bSCy Schubert         (void) snprintf(tmp, sizeof(tmp), "%s-DEPTH-1", principal_string);
184*7f2fe78bSCy Schubert         tmp[sizeof(tmp) - 1] = '\0';
185*7f2fe78bSCy Schubert         str_newprinc = tmp;
186*7f2fe78bSCy Schubert         add_princ(test_context, str_newprinc);
187*7f2fe78bSCy Schubert 
188*7f2fe78bSCy Schubert         for (i = 2; i <= depth; i++) {
189*7f2fe78bSCy Schubert             (void) snprintf(tmp2, sizeof(tmp2), "/%s-DEPTH-%d",
190*7f2fe78bSCy Schubert                             principal_string, i);
191*7f2fe78bSCy Schubert             tmp2[sizeof(tmp2) - 1] = '\0';
192*7f2fe78bSCy Schubert             strncat(tmp, tmp2, sizeof(tmp) - 1 - strlen(tmp));
193*7f2fe78bSCy Schubert             str_newprinc = tmp;
194*7f2fe78bSCy Schubert             add_princ(test_context, str_newprinc);
195*7f2fe78bSCy Schubert         }
196*7f2fe78bSCy Schubert     }
197*7f2fe78bSCy Schubert 
198*7f2fe78bSCy Schubert     retval = krb5_db_fini(test_context);
199*7f2fe78bSCy Schubert     memset(master_keyblock.contents, 0,
200*7f2fe78bSCy Schubert            (size_t) master_keyblock.length);
201*7f2fe78bSCy Schubert     if (retval && retval != KRB5_KDB_DBNOTINITED) {
202*7f2fe78bSCy Schubert         com_err(progname, retval, "while closing database");
203*7f2fe78bSCy Schubert         exit(1);
204*7f2fe78bSCy Schubert     }
205*7f2fe78bSCy Schubert     if (master_princ_set)
206*7f2fe78bSCy Schubert         krb5_free_principal(test_context, master_princ);
207*7f2fe78bSCy Schubert     krb5_free_context(test_context);
208*7f2fe78bSCy Schubert     exit(0);
209*7f2fe78bSCy Schubert }
210*7f2fe78bSCy Schubert 
211*7f2fe78bSCy Schubert void
add_princ(context,str_newprinc)212*7f2fe78bSCy Schubert add_princ(context, str_newprinc)
213*7f2fe78bSCy Schubert     krb5_context          context;
214*7f2fe78bSCy Schubert     char                * str_newprinc;
215*7f2fe78bSCy Schubert {
216*7f2fe78bSCy Schubert     krb5_error_code       retval;
217*7f2fe78bSCy Schubert     krb5_principal        newprinc;
218*7f2fe78bSCy Schubert     krb5_db_entry         *newentry;
219*7f2fe78bSCy Schubert     char                  princ_name[4096];
220*7f2fe78bSCy Schubert 
221*7f2fe78bSCy Schubert     newentry = calloc(1, sizeof(*newentry));
222*7f2fe78bSCy Schubert     if (newentry == NULL) {
223*7f2fe78bSCy Schubert         com_err(progname, ENOMEM, "while allocating DB entry");
224*7f2fe78bSCy Schubert         return;
225*7f2fe78bSCy Schubert     }
226*7f2fe78bSCy Schubert     snprintf(princ_name, sizeof(princ_name), "%s@%s", str_newprinc, cur_realm);
227*7f2fe78bSCy Schubert     if ((retval = krb5_parse_name(context, princ_name, &newprinc))) {
228*7f2fe78bSCy Schubert         com_err(progname, retval, "while parsing '%s'", princ_name);
229*7f2fe78bSCy Schubert         return;
230*7f2fe78bSCy Schubert     }
231*7f2fe78bSCy Schubert 
232*7f2fe78bSCy Schubert     /* Add basic data */
233*7f2fe78bSCy Schubert     newentry->len = KRB5_KDB_V1_BASE_LENGTH;
234*7f2fe78bSCy Schubert     newentry->attributes = mblock.flags;
235*7f2fe78bSCy Schubert     newentry->max_life = mblock.max_life;
236*7f2fe78bSCy Schubert     newentry->max_renewable_life = mblock.max_rlife;
237*7f2fe78bSCy Schubert     newentry->expiration = mblock.expiration;
238*7f2fe78bSCy Schubert     newentry->pw_expiration = mblock.expiration;
239*7f2fe78bSCy Schubert 
240*7f2fe78bSCy Schubert     /* Add princ to db entry */
241*7f2fe78bSCy Schubert     if ((retval = krb5_copy_principal(context, newprinc, &newentry->princ))) {
242*7f2fe78bSCy Schubert         com_err(progname, retval, "while encoding princ to db entry for '%s'",
243*7f2fe78bSCy Schubert                 princ_name);
244*7f2fe78bSCy Schubert         krb5_free_principal(context, newprinc);
245*7f2fe78bSCy Schubert         goto error;
246*7f2fe78bSCy Schubert     }
247*7f2fe78bSCy Schubert 
248*7f2fe78bSCy Schubert     {
249*7f2fe78bSCy Schubert         /* Add mod princ to db entry */
250*7f2fe78bSCy Schubert         krb5_timestamp now;
251*7f2fe78bSCy Schubert 
252*7f2fe78bSCy Schubert         retval = krb5_timeofday(context, &now);
253*7f2fe78bSCy Schubert         if (retval) {
254*7f2fe78bSCy Schubert             com_err(progname, retval, "while fetching date");
255*7f2fe78bSCy Schubert             krb5_free_principal(context, newprinc);
256*7f2fe78bSCy Schubert             goto error;
257*7f2fe78bSCy Schubert         }
258*7f2fe78bSCy Schubert         retval = krb5_dbe_update_mod_princ_data(context, newentry, now,
259*7f2fe78bSCy Schubert                                                 master_princ);
260*7f2fe78bSCy Schubert         if (retval) {
261*7f2fe78bSCy Schubert             com_err(progname, retval, "while encoding mod_princ data");
262*7f2fe78bSCy Schubert             krb5_free_principal(context, newprinc);
263*7f2fe78bSCy Schubert             goto error;
264*7f2fe78bSCy Schubert         }
265*7f2fe78bSCy Schubert     }
266*7f2fe78bSCy Schubert 
267*7f2fe78bSCy Schubert     {   /* Add key and salt data to db entry */
268*7f2fe78bSCy Schubert         krb5_data pwd, salt;
269*7f2fe78bSCy Schubert         krb5_keyblock key;
270*7f2fe78bSCy Schubert 
271*7f2fe78bSCy Schubert         if ((retval = krb5_principal2salt(context, newprinc, &salt))) {
272*7f2fe78bSCy Schubert             com_err(progname, retval, "while converting princ to salt for '%s'",
273*7f2fe78bSCy Schubert                     princ_name);
274*7f2fe78bSCy Schubert             krb5_free_principal(context, newprinc);
275*7f2fe78bSCy Schubert             goto error;
276*7f2fe78bSCy Schubert         }
277*7f2fe78bSCy Schubert 
278*7f2fe78bSCy Schubert         krb5_free_principal(context, newprinc);
279*7f2fe78bSCy Schubert 
280*7f2fe78bSCy Schubert         pwd.length = strlen(princ_name);
281*7f2fe78bSCy Schubert         pwd.data = princ_name;  /* must be able to regenerate */
282*7f2fe78bSCy Schubert         if ((retval = krb5_c_string_to_key(context, master_keyblock.enctype,
283*7f2fe78bSCy Schubert                                            &pwd, &salt, &key))) {
284*7f2fe78bSCy Schubert             com_err(progname,retval,"while converting password to key for '%s'",
285*7f2fe78bSCy Schubert                     princ_name);
286*7f2fe78bSCy Schubert             krb5_free_data_contents(context, &salt);
287*7f2fe78bSCy Schubert             goto error;
288*7f2fe78bSCy Schubert         }
289*7f2fe78bSCy Schubert         krb5_free_data_contents(context, &salt);
290*7f2fe78bSCy Schubert 
291*7f2fe78bSCy Schubert         if ((retval = krb5_dbe_create_key_data(context, newentry))) {
292*7f2fe78bSCy Schubert             com_err(progname, retval, "while creating key_data for '%s'",
293*7f2fe78bSCy Schubert                     princ_name);
294*7f2fe78bSCy Schubert             free(key.contents);
295*7f2fe78bSCy Schubert             goto error;
296*7f2fe78bSCy Schubert         }
297*7f2fe78bSCy Schubert 
298*7f2fe78bSCy Schubert         if ((retval = krb5_dbe_encrypt_key_data(context, &master_keyblock,
299*7f2fe78bSCy Schubert                                                 &key, NULL, 1,
300*7f2fe78bSCy Schubert                                                 newentry->key_data))) {
301*7f2fe78bSCy Schubert             com_err(progname, retval, "while encrypting key for '%s'",
302*7f2fe78bSCy Schubert                     princ_name);
303*7f2fe78bSCy Schubert             free(key.contents);
304*7f2fe78bSCy Schubert             goto error;
305*7f2fe78bSCy Schubert         }
306*7f2fe78bSCy Schubert         free(key.contents);
307*7f2fe78bSCy Schubert     }
308*7f2fe78bSCy Schubert 
309*7f2fe78bSCy Schubert     if ((retval = krb5_db_put_principal(context, newentry))) {
310*7f2fe78bSCy Schubert         com_err(progname, retval, "while storing principal date");
311*7f2fe78bSCy Schubert         goto error;
312*7f2fe78bSCy Schubert     }
313*7f2fe78bSCy Schubert 
314*7f2fe78bSCy Schubert error: /* Do cleanup of newentry regardless of error */
315*7f2fe78bSCy Schubert     krb5_db_free_principal(context, newentry);
316*7f2fe78bSCy Schubert     return;
317*7f2fe78bSCy Schubert }
318*7f2fe78bSCy Schubert 
319*7f2fe78bSCy Schubert int
set_dbname_help(pname,dbname)320*7f2fe78bSCy Schubert set_dbname_help(pname, dbname)
321*7f2fe78bSCy Schubert     char *pname;
322*7f2fe78bSCy Schubert     char *dbname;
323*7f2fe78bSCy Schubert {
324*7f2fe78bSCy Schubert     krb5_error_code retval;
325*7f2fe78bSCy Schubert     krb5_data pwd, scratch;
326*7f2fe78bSCy Schubert     char *args[2];
327*7f2fe78bSCy Schubert     krb5_db_entry *master_entry;
328*7f2fe78bSCy Schubert 
329*7f2fe78bSCy Schubert     /* assemble & parse the master key name */
330*7f2fe78bSCy Schubert 
331*7f2fe78bSCy Schubert     if ((retval = krb5_db_setup_mkey_name(test_context, mkey_name, cur_realm,
332*7f2fe78bSCy Schubert                                           0, &master_princ))) {
333*7f2fe78bSCy Schubert         com_err(pname, retval, "while setting up master key name");
334*7f2fe78bSCy Schubert         return(1);
335*7f2fe78bSCy Schubert     }
336*7f2fe78bSCy Schubert     master_princ_set = 1;
337*7f2fe78bSCy Schubert     if (mkey_password) {
338*7f2fe78bSCy Schubert         pwd.data = mkey_password;
339*7f2fe78bSCy Schubert         pwd.length = strlen(mkey_password);
340*7f2fe78bSCy Schubert         retval = krb5_principal2salt(test_context, master_princ, &scratch);
341*7f2fe78bSCy Schubert         if (retval) {
342*7f2fe78bSCy Schubert             com_err(pname, retval, "while calculated master key salt");
343*7f2fe78bSCy Schubert             return(1);
344*7f2fe78bSCy Schubert         }
345*7f2fe78bSCy Schubert         if ((retval = krb5_c_string_to_key(test_context,
346*7f2fe78bSCy Schubert                                            master_keyblock.enctype,
347*7f2fe78bSCy Schubert                                            &pwd, &scratch,
348*7f2fe78bSCy Schubert                                            &master_keyblock))) {
349*7f2fe78bSCy Schubert             com_err(pname, retval,
350*7f2fe78bSCy Schubert                     "while transforming master key from password");
351*7f2fe78bSCy Schubert             return(1);
352*7f2fe78bSCy Schubert         }
353*7f2fe78bSCy Schubert         free(scratch.data);
354*7f2fe78bSCy Schubert     } else {
355*7f2fe78bSCy Schubert         if ((retval = krb5_db_fetch_mkey(test_context, master_princ,
356*7f2fe78bSCy Schubert                                          master_keyblock.enctype, manual_mkey,
357*7f2fe78bSCy Schubert                                          FALSE, 0, NULL, NULL,
358*7f2fe78bSCy Schubert                                          &master_keyblock))) {
359*7f2fe78bSCy Schubert             com_err(pname, retval, "while reading master key");
360*7f2fe78bSCy Schubert             return(1);
361*7f2fe78bSCy Schubert         }
362*7f2fe78bSCy Schubert     }
363*7f2fe78bSCy Schubert 
364*7f2fe78bSCy Schubert     /* Ick!  Current DAL interface requires that the default_realm
365*7f2fe78bSCy Schubert        field be set in the krb5_context.  */
366*7f2fe78bSCy Schubert     if ((retval = krb5_set_default_realm(test_context, cur_realm))) {
367*7f2fe78bSCy Schubert         com_err(pname, retval, "setting default realm");
368*7f2fe78bSCy Schubert         return 1;
369*7f2fe78bSCy Schubert     }
370*7f2fe78bSCy Schubert     /* Pathname is passed to db2 via 'args' parameter.  */
371*7f2fe78bSCy Schubert     args[1] = NULL;
372*7f2fe78bSCy Schubert     if (asprintf(&args[0], "dbname=%s", dbname) < 0) {
373*7f2fe78bSCy Schubert         com_err(pname, errno, "while setting up db parameters");
374*7f2fe78bSCy Schubert         return 1;
375*7f2fe78bSCy Schubert     }
376*7f2fe78bSCy Schubert 
377*7f2fe78bSCy Schubert     if ((retval = krb5_db_open(test_context, args, KRB5_KDB_OPEN_RO))) {
378*7f2fe78bSCy Schubert         com_err(pname, retval, "while initializing database");
379*7f2fe78bSCy Schubert         return(1);
380*7f2fe78bSCy Schubert     }
381*7f2fe78bSCy Schubert     /* Done with args */
382*7f2fe78bSCy Schubert     free(args[0]);
383*7f2fe78bSCy Schubert 
384*7f2fe78bSCy Schubert     if ((retval = krb5_db_fetch_mkey_list(test_context, master_princ,
385*7f2fe78bSCy Schubert                                           &master_keyblock))){
386*7f2fe78bSCy Schubert         com_err(pname, retval, "while verifying master key");
387*7f2fe78bSCy Schubert         (void) krb5_db_fini(test_context);
388*7f2fe78bSCy Schubert         return(1);
389*7f2fe78bSCy Schubert     }
390*7f2fe78bSCy Schubert     if ((retval = krb5_db_get_principal(test_context, master_princ, 0,
391*7f2fe78bSCy Schubert                                         &master_entry))) {
392*7f2fe78bSCy Schubert         com_err(pname, retval, "while retrieving master entry");
393*7f2fe78bSCy Schubert         (void) krb5_db_fini(test_context);
394*7f2fe78bSCy Schubert         return(1);
395*7f2fe78bSCy Schubert     }
396*7f2fe78bSCy Schubert 
397*7f2fe78bSCy Schubert     mblock.max_life = master_entry->max_life;
398*7f2fe78bSCy Schubert     mblock.max_rlife = master_entry->max_renewable_life;
399*7f2fe78bSCy Schubert     mblock.expiration = master_entry->expiration;
400*7f2fe78bSCy Schubert 
401*7f2fe78bSCy Schubert     /* don't set flags, master has some extra restrictions */
402*7f2fe78bSCy Schubert     mblock.mkvno = master_entry->key_data[0].key_data_kvno;
403*7f2fe78bSCy Schubert 
404*7f2fe78bSCy Schubert     krb5_db_free_principal(test_context, master_entry);
405*7f2fe78bSCy Schubert     return 0;
406*7f2fe78bSCy Schubert }
407