2 * Copyright (c) 1999 - 2001 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of KTH nor the names of its contributors may be
18 * used to endorse or promote products derived from this software without
19 * specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
33 /* Converts a database from version 0.0* to 0.1. This is done by
34 * making three copies of each DES key (DES-CBC-CRC, DES-CBC-MD4, and
44 RCSID("$Id: convert_db.c,v 1.12 2001/02/20 01:44:53 assar Exp $");
46 static krb5_error_code
47 update_keytypes(krb5_context context, HDB *db, hdb_entry *entry, void *data)
57 for(i = 0; i < entry->keys.len; i++)
58 if(entry->keys.val[i].key.keytype == KEYTYPE_DES)
60 else if(entry->keys.val[i].key.keytype == KEYTYPE_DES3)
62 k = malloc(sizeof(*k) * (entry->keys.len + n));
64 for(i = 0; i < entry->keys.len; i++) {
65 copy_Key(&entry->keys.val[i], &k[n]);
66 if(entry->keys.val[i].key.keytype == KEYTYPE_DES) {
67 copy_Key(&entry->keys.val[i], &k[n+1]);
68 k[n+1].key.keytype = ETYPE_DES_CBC_MD4;
69 copy_Key(&entry->keys.val[i], &k[n+2]);
70 k[n+2].key.keytype = ETYPE_DES_CBC_MD5;
73 else if(entry->keys.val[i].key.keytype == KEYTYPE_DES3) {
74 copy_Key(&entry->keys.val[i], &k[n+1]);
75 k[n+1].key.keytype = ETYPE_DES3_CBC_MD5;
80 save_len = entry->keys.len;
81 save_val = entry->keys.val;
84 ret = new->store(context, new, HDB_F_REPLACE, entry);
85 entry->keys.len = save_len;
86 entry->keys.val = save_val;
87 for(i = 0; i < n; i++)
93 static krb5_error_code
94 update_version2(krb5_context context, HDB *db, hdb_entry *entry, void *data)
97 if(!db->master_key_set) {
99 for(i = 0; i < entry->keys.len; i++) {
100 free(entry->keys.val[i].mkvno);
101 entry->keys.val[i].mkvno = NULL;
104 new->store(context, new, HDB_F_REPLACE, entry);
108 char *old_database = HDB_DEFAULT_DB;
109 char *new_database = HDB_DEFAULT_DB ".new";
115 struct getargs args[] = {
116 { "old-database", 0, arg_string, &old_database,
117 "name of database to convert", "file" },
118 { "new-database", 0, arg_string, &new_database,
119 "name of converted database", "file" },
120 { "master-key", 0, arg_string, &mkeyfile,
121 "v5 master key file", "file" },
122 { "update-version", 0, arg_flag, &update_version,
123 "update the database to the current version" },
124 { "help", 'h', arg_flag, &help_flag },
125 { "version", 0, arg_flag, &version_flag }
128 static int num_args = sizeof(args) / sizeof(args[0]);
131 main(int argc, char **argv)
134 krb5_context context;
137 int master_key_set = 0;
139 setprogname(argv[0]);
141 if(getarg(args, num_args, argc, argv, &optind))
142 krb5_std_usage(1, args, num_args);
145 krb5_std_usage(0, args, num_args);
152 ret = krb5_init_context(&context);
154 errx(1, "krb5_init_context failed: %d", ret);
156 ret = hdb_create(context, &db, old_database);
158 krb5_err(context, 1, ret, "hdb_create");
160 ret = hdb_set_master_keyfile(context, db, mkeyfile);
162 krb5_err(context, 1, ret, "hdb_set_master_keyfile");
164 ret = hdb_create(context, &new, new_database);
166 krb5_err(context, 1, ret, "hdb_create");
167 if (master_key_set) {
168 ret = hdb_set_master_keyfile(context, new, mkeyfile);
170 krb5_err(context, 1, ret, "hdb_set_master_keyfile");
172 ret = db->open(context, db, O_RDONLY, 0);
173 if(ret == HDB_ERR_BADVERSION) {
178 tag.data = HDB_DB_FORMAT_ENTRY;
179 tag.length = strlen(tag.data);
180 ret = (*db->_get)(context, db, tag, &version);
182 krb5_errx(context, 1, "database is wrong version, "
183 "but couldn't find version key (%s)",
184 HDB_DB_FORMAT_ENTRY);
185 foo = sscanf(version.data, "%u", &ver);
186 krb5_data_free (&version);
188 krb5_errx(context, 1, "database version is not a number");
189 if(ver == 1 && HDB_DB_FORMAT == 2) {
190 krb5_warnx(context, "will upgrade database from version %d to %d",
192 krb5_warnx(context, "rerun to do other conversions");
195 krb5_errx(context, 1,
196 "don't know how to upgrade from version %d to %d",
199 krb5_err(context, 1, ret, "%s", old_database);
200 ret = new->open(context, new, O_CREAT|O_EXCL|O_RDWR, 0600);
202 krb5_err(context, 1, ret, "%s", new_database);
204 ret = hdb_foreach(context, db, 0, update_version2, new);
206 ret = hdb_foreach(context, db, 0, update_keytypes, new);
208 krb5_err(context, 1, ret, "hdb_foreach");
209 db->close(context, db);
210 new->close(context, new);
211 krb5_warnx(context, "wrote converted database to `%s'", new_database);