Merge from vendor branch GCC:
[dragonfly.git] / crypto / heimdal-0.6.3 / lib / hdb / convert_db.c
1 /*
2  * Copyright (c) 1999 - 2001 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden). 
4  * All rights reserved. 
5  *
6  * Redistribution and use in source and binary forms, with or without 
7  * modification, are permitted provided that the following conditions 
8  * are met: 
9  *
10  * 1. Redistributions of source code must retain the above copyright 
11  *    notice, this list of conditions and the following disclaimer. 
12  *
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. 
16  *
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.
20  *
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. */
32
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
35  * DES-CBC-MD5).
36  *
37  * Use with care. 
38  */
39
40 #include "hdb_locl.h"
41 #include <getarg.h>
42 #include <err.h>
43
44 RCSID("$Id: convert_db.c,v 1.12 2001/02/20 01:44:53 assar Exp $");
45
46 static krb5_error_code
47 update_keytypes(krb5_context context, HDB *db, hdb_entry *entry, void *data)
48 {
49     int i;
50     int n = 0;
51     Key *k;
52     int save_len;
53     Key *save_val;
54     HDB *new = data;
55     krb5_error_code ret;
56
57     for(i = 0; i < entry->keys.len; i++) 
58         if(entry->keys.val[i].key.keytype == KEYTYPE_DES)
59             n += 2;
60         else if(entry->keys.val[i].key.keytype == KEYTYPE_DES3)
61             n += 1;
62     k = malloc(sizeof(*k) * (entry->keys.len + n));
63     n = 0;
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;
71             n += 2;
72         }
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;
76             n += 1;
77         }
78         n++;
79     }
80     save_len = entry->keys.len;
81     save_val = entry->keys.val;
82     entry->keys.len = n;
83     entry->keys.val = k;
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++) 
88         free_Key(&k[i]);
89     free(k);
90     return 0;
91 }
92
93 static krb5_error_code
94 update_version2(krb5_context context, HDB *db, hdb_entry *entry, void *data)
95 {
96     HDB *new = data;
97     if(!db->master_key_set) {
98         int i;
99         for(i = 0; i < entry->keys.len; i++) {
100             free(entry->keys.val[i].mkvno);
101             entry->keys.val[i].mkvno = NULL;
102         }
103     }
104     new->store(context, new, HDB_F_REPLACE, entry);
105     return 0;
106 }
107
108 char *old_database = HDB_DEFAULT_DB;
109 char *new_database = HDB_DEFAULT_DB ".new";
110 char *mkeyfile;
111 int update_version;
112 int help_flag;
113 int version_flag;
114
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 }
126 };
127
128 static int num_args = sizeof(args) / sizeof(args[0]);
129
130 int
131 main(int argc, char **argv)
132 {
133     krb5_error_code ret;
134     krb5_context context;
135     HDB *db, *new;
136     int optind = 0;
137     int master_key_set = 0;
138     
139     setprogname(argv[0]);
140
141     if(getarg(args, num_args, argc, argv, &optind))
142         krb5_std_usage(1, args, num_args);
143
144     if(help_flag)
145         krb5_std_usage(0, args, num_args);
146     
147     if(version_flag){
148         print_version(NULL);
149         exit(0);
150     }
151
152     ret = krb5_init_context(&context);
153     if(ret != 0)
154         errx(1, "krb5_init_context failed: %d", ret);
155     
156     ret = hdb_create(context, &db, old_database);
157     if(ret != 0)
158         krb5_err(context, 1, ret, "hdb_create");
159
160     ret = hdb_set_master_keyfile(context, db, mkeyfile);
161     if (ret)
162         krb5_err(context, 1, ret, "hdb_set_master_keyfile");
163     master_key_set = 1;
164     ret = hdb_create(context, &new, new_database);
165     if(ret != 0)
166         krb5_err(context, 1, ret, "hdb_create");
167     if (master_key_set) {
168         ret = hdb_set_master_keyfile(context, new, mkeyfile);
169         if (ret)
170             krb5_err(context, 1, ret, "hdb_set_master_keyfile");
171     }
172     ret = db->open(context, db, O_RDONLY, 0);
173     if(ret == HDB_ERR_BADVERSION) {
174         krb5_data tag;
175         krb5_data version;
176         int foo;
177         unsigned ver;
178         tag.data = HDB_DB_FORMAT_ENTRY;
179         tag.length = strlen(tag.data);
180         ret = (*db->_get)(context, db, tag, &version);
181         if(ret)
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);
187         if(foo != 1)
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", 
191                        ver, HDB_DB_FORMAT);
192             krb5_warnx(context, "rerun to do other conversions");
193             update_version = 1;
194         } else
195             krb5_errx(context, 1, 
196                       "don't know how to upgrade from version %d to %d", 
197                       ver, HDB_DB_FORMAT);
198     } else if(ret)
199         krb5_err(context, 1, ret, "%s", old_database);
200     ret = new->open(context, new, O_CREAT|O_EXCL|O_RDWR, 0600);
201     if(ret)
202         krb5_err(context, 1, ret, "%s", new_database);
203     if(update_version)
204         ret = hdb_foreach(context, db, 0, update_version2, new);
205     else
206         ret = hdb_foreach(context, db, 0, update_keytypes, new);
207     if(ret != 0)
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);
212     return 0;
213 }