1b528cefcSMark Murray /*
2c19800e8SDoug Rabson * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
3b528cefcSMark Murray * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray * All rights reserved.
5b528cefcSMark Murray *
6b528cefcSMark Murray * Redistribution and use in source and binary forms, with or without
7b528cefcSMark Murray * modification, are permitted provided that the following conditions
8b528cefcSMark Murray * are met:
9b528cefcSMark Murray *
10b528cefcSMark Murray * 1. Redistributions of source code must retain the above copyright
11b528cefcSMark Murray * notice, this list of conditions and the following disclaimer.
12b528cefcSMark Murray *
13b528cefcSMark Murray * 2. Redistributions in binary form must reproduce the above copyright
14b528cefcSMark Murray * notice, this list of conditions and the following disclaimer in the
15b528cefcSMark Murray * documentation and/or other materials provided with the distribution.
16b528cefcSMark Murray *
17b528cefcSMark Murray * 3. Neither the name of the Institute nor the names of its contributors
18b528cefcSMark Murray * may be used to endorse or promote products derived from this software
19b528cefcSMark Murray * without specific prior written permission.
20b528cefcSMark Murray *
21b528cefcSMark Murray * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22b528cefcSMark Murray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b528cefcSMark Murray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b528cefcSMark Murray * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25b528cefcSMark Murray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26b528cefcSMark Murray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27b528cefcSMark Murray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28b528cefcSMark Murray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29b528cefcSMark Murray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30b528cefcSMark Murray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b528cefcSMark Murray * SUCH DAMAGE.
32b528cefcSMark Murray */
33b528cefcSMark Murray
34b528cefcSMark Murray #define KRB5_DEPRECATED /* uses v4 functions that will die */
35b528cefcSMark Murray
36c19800e8SDoug Rabson #include "hprop.h"
37b528cefcSMark Murray
38b528cefcSMark Murray static int version_flag;
39b528cefcSMark Murray static int help_flag;
408373020dSJacques Vidrine static const char *ktname = HPROP_KEYTAB;
418373020dSJacques Vidrine static const char *database;
42b528cefcSMark Murray static char *mkeyfile;
43b528cefcSMark Murray static int to_stdout;
44b528cefcSMark Murray static int verbose_flag;
45b528cefcSMark Murray static int encrypt_flag;
46b528cefcSMark Murray static int decrypt_flag;
475e9cd1aeSAssar Westerlund static hdb_master_key mkey5;
48b528cefcSMark Murray
495e9cd1aeSAssar Westerlund static char *source_type;
505e9cd1aeSAssar Westerlund
51b528cefcSMark Murray static char *local_realm=NULL;
524137ff4cSJacques Vidrine
53b528cefcSMark Murray static int
open_socket(krb5_context context,const char * hostname,const char * port)54b528cefcSMark Murray open_socket(krb5_context context, const char *hostname, const char *port)
555e9cd1aeSAssar Westerlund {
56b528cefcSMark Murray struct addrinfo *ai, *a;
575e9cd1aeSAssar Westerlund struct addrinfo hints;
585e9cd1aeSAssar Westerlund int error;
59b528cefcSMark Murray
605e9cd1aeSAssar Westerlund memset (&hints, 0, sizeof(hints));
61b528cefcSMark Murray hints.ai_socktype = SOCK_STREAM;
62b528cefcSMark Murray hints.ai_protocol = IPPROTO_TCP;
63b528cefcSMark Murray
64b528cefcSMark Murray error = getaddrinfo (hostname, port, &hints, &ai);
65b528cefcSMark Murray if (error) {
66b528cefcSMark Murray warnx ("%s: %s", hostname, gai_strerror(error));
67b528cefcSMark Murray return -1;
68b528cefcSMark Murray }
69b528cefcSMark Murray
705e9cd1aeSAssar Westerlund for (a = ai; a != NULL; a = a->ai_next) {
71b528cefcSMark Murray int s;
72b528cefcSMark Murray
73b528cefcSMark Murray s = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
74b528cefcSMark Murray if (s < 0)
75b528cefcSMark Murray continue;
76b528cefcSMark Murray if (connect (s, a->ai_addr, a->ai_addrlen) < 0) {
77b528cefcSMark Murray warn ("connect(%s)", hostname);
78b528cefcSMark Murray close (s);
79b528cefcSMark Murray continue;
80b528cefcSMark Murray }
81b528cefcSMark Murray freeaddrinfo (ai);
82b528cefcSMark Murray return s;
83b528cefcSMark Murray }
84b528cefcSMark Murray warnx ("failed to contact %s", hostname);
85b528cefcSMark Murray freeaddrinfo (ai);
86b528cefcSMark Murray return -1;
87b528cefcSMark Murray }
88b528cefcSMark Murray
89b528cefcSMark Murray krb5_error_code
v5_prop(krb5_context context,HDB * db,hdb_entry_ex * entry,void * appdata)90b528cefcSMark Murray v5_prop(krb5_context context, HDB *db, hdb_entry_ex *entry, void *appdata)
91b528cefcSMark Murray {
92b528cefcSMark Murray krb5_error_code ret;
93b528cefcSMark Murray struct prop_data *pd = appdata;
94b528cefcSMark Murray krb5_data data;
955e9cd1aeSAssar Westerlund
96c19800e8SDoug Rabson if(encrypt_flag) {
97b528cefcSMark Murray ret = hdb_seal_keys_mkey(context, &entry->entry, mkey5);
98b528cefcSMark Murray if (ret) {
99b528cefcSMark Murray krb5_warn(context, ret, "hdb_seal_keys_mkey");
100b528cefcSMark Murray return ret;
101b528cefcSMark Murray }
1025e9cd1aeSAssar Westerlund }
103c19800e8SDoug Rabson if(decrypt_flag) {
1045e9cd1aeSAssar Westerlund ret = hdb_unseal_keys_mkey(context, &entry->entry, mkey5);
1055e9cd1aeSAssar Westerlund if (ret) {
1065e9cd1aeSAssar Westerlund krb5_warn(context, ret, "hdb_unseal_keys_mkey");
1075e9cd1aeSAssar Westerlund return ret;
1085e9cd1aeSAssar Westerlund }
1095e9cd1aeSAssar Westerlund }
110c19800e8SDoug Rabson
1115e9cd1aeSAssar Westerlund ret = hdb_entry2value(context, &entry->entry, &data);
1125e9cd1aeSAssar Westerlund if(ret) {
1135e9cd1aeSAssar Westerlund krb5_warn(context, ret, "hdb_entry2value");
1145e9cd1aeSAssar Westerlund return ret;
1155e9cd1aeSAssar Westerlund }
116b528cefcSMark Murray
117c19800e8SDoug Rabson if(to_stdout)
1185e9cd1aeSAssar Westerlund ret = krb5_write_message(context, &pd->sock, &data);
1195e9cd1aeSAssar Westerlund else
1205e9cd1aeSAssar Westerlund ret = krb5_write_priv_message(context, pd->auth_context,
1215e9cd1aeSAssar Westerlund &pd->sock, &data);
122b528cefcSMark Murray krb5_data_free(&data);
123b528cefcSMark Murray return ret;
1245e9cd1aeSAssar Westerlund }
125b528cefcSMark Murray
1265e9cd1aeSAssar Westerlund struct getargs args[] = {
1275e9cd1aeSAssar Westerlund { "master-key", 'm', arg_string, &mkeyfile, "v5 master key file", "file" },
128b528cefcSMark Murray { "database", 'd', arg_string, rk_UNCONST(&database), "database", "file" },
129b528cefcSMark Murray { "source", 0, arg_string, &source_type, "type of database to read",
130b528cefcSMark Murray "heimdal"
131b528cefcSMark Murray "|mit-dump"
1325e9cd1aeSAssar Westerlund },
1335e9cd1aeSAssar Westerlund
134b528cefcSMark Murray { "keytab", 'k', arg_string, rk_UNCONST(&ktname),
135b528cefcSMark Murray "keytab to use for authentication", "keytab" },
136c19800e8SDoug Rabson { "v5-realm", 'R', arg_string, &local_realm, "v5 realm to use", NULL },
137b528cefcSMark Murray { "decrypt", 'D', arg_flag, &decrypt_flag, "decrypt keys", NULL },
138b528cefcSMark Murray { "encrypt", 'E', arg_flag, &encrypt_flag, "encrypt keys", NULL },
139b528cefcSMark Murray { "stdout", 'n', arg_flag, &to_stdout, "dump to stdout", NULL },
140b528cefcSMark Murray { "verbose", 'v', arg_flag, &verbose_flag, NULL, NULL },
1414137ff4cSJacques Vidrine { "version", 0, arg_flag, &version_flag, NULL, NULL },
142c19800e8SDoug Rabson { "help", 'h', arg_flag, &help_flag, NULL, NULL }
143b528cefcSMark Murray };
144b528cefcSMark Murray
145b528cefcSMark Murray static int num_args = sizeof(args) / sizeof(args[0]);
1464137ff4cSJacques Vidrine
147b528cefcSMark Murray static void
usage(int ret)148b528cefcSMark Murray usage(int ret)
149b528cefcSMark Murray {
150b528cefcSMark Murray arg_printusage (args, num_args, NULL, "[host[:port]] ...");
151b528cefcSMark Murray exit (ret);
152c19800e8SDoug Rabson }
153b528cefcSMark Murray
154b528cefcSMark Murray static void
get_creds(krb5_context context,krb5_ccache * cache)155b528cefcSMark Murray get_creds(krb5_context context, krb5_ccache *cache)
156b528cefcSMark Murray {
157c19800e8SDoug Rabson krb5_keytab keytab;
158c19800e8SDoug Rabson krb5_principal client;
159c19800e8SDoug Rabson krb5_error_code ret;
160c19800e8SDoug Rabson krb5_get_init_creds_opt *init_opts;
161c19800e8SDoug Rabson krb5_preauthtype preauth = KRB5_PADATA_ENC_TIMESTAMP;
1625e9cd1aeSAssar Westerlund krb5_creds creds;
163c19800e8SDoug Rabson
164c19800e8SDoug Rabson ret = krb5_kt_register(context, &hdb_kt_ops);
165c19800e8SDoug Rabson if(ret) krb5_err(context, 1, ret, "krb5_kt_register");
166c19800e8SDoug Rabson
1675e9cd1aeSAssar Westerlund ret = krb5_kt_resolve(context, ktname, &keytab);
168c19800e8SDoug Rabson if(ret) krb5_err(context, 1, ret, "krb5_kt_resolve");
169c19800e8SDoug Rabson
170c19800e8SDoug Rabson ret = krb5_make_principal(context, &client, NULL,
171c19800e8SDoug Rabson "kadmin", HPROP_NAME, NULL);
172c19800e8SDoug Rabson if(ret) krb5_err(context, 1, ret, "krb5_make_principal");
173c19800e8SDoug Rabson
174c19800e8SDoug Rabson ret = krb5_get_init_creds_opt_alloc(context, &init_opts);
175c19800e8SDoug Rabson if(ret) krb5_err(context, 1, ret, "krb5_get_init_creds_opt_alloc");
176b528cefcSMark Murray krb5_get_init_creds_opt_set_preauth_list(init_opts, &preauth, 1);
177c19800e8SDoug Rabson
178c19800e8SDoug Rabson ret = krb5_get_init_creds_keytab(context, &creds, client, keytab, 0, NULL, init_opts);
179c19800e8SDoug Rabson if(ret) krb5_err(context, 1, ret, "krb5_get_init_creds");
180c19800e8SDoug Rabson
181b528cefcSMark Murray krb5_get_init_creds_opt_free(context, init_opts);
1825e9cd1aeSAssar Westerlund
183c19800e8SDoug Rabson ret = krb5_kt_close(context, keytab);
1845e9cd1aeSAssar Westerlund if(ret) krb5_err(context, 1, ret, "krb5_kt_close");
185c19800e8SDoug Rabson
1865e9cd1aeSAssar Westerlund ret = krb5_cc_new_unique(context, krb5_cc_type_memory, NULL, cache);
1875e9cd1aeSAssar Westerlund if(ret) krb5_err(context, 1, ret, "krb5_cc_new_unique");
1885e9cd1aeSAssar Westerlund
1895e9cd1aeSAssar Westerlund ret = krb5_cc_initialize(context, *cache, client);
1905e9cd1aeSAssar Westerlund if(ret) krb5_err(context, 1, ret, "krb5_cc_initialize");
1915e9cd1aeSAssar Westerlund
192c19800e8SDoug Rabson krb5_free_principal(context, client);
193c19800e8SDoug Rabson
1945e9cd1aeSAssar Westerlund ret = krb5_cc_store_cred(context, *cache, &creds);
195b528cefcSMark Murray if(ret) krb5_err(context, 1, ret, "krb5_cc_store_cred");
196b528cefcSMark Murray
197c19800e8SDoug Rabson krb5_free_cred_contents(context, &creds);
198c19800e8SDoug Rabson }
1995e9cd1aeSAssar Westerlund
200c19800e8SDoug Rabson enum hprop_source {
2014137ff4cSJacques Vidrine HPROP_HEIMDAL = 1,
202b528cefcSMark Murray HPROP_MIT_DUMP
203b528cefcSMark Murray };
204b528cefcSMark Murray
205b528cefcSMark Murray struct {
206b528cefcSMark Murray int type;
207b528cefcSMark Murray const char *name;
208b528cefcSMark Murray } types[] = {
209b528cefcSMark Murray { HPROP_HEIMDAL, "heimdal" },
210c19800e8SDoug Rabson { HPROP_MIT_DUMP, "mit-dump" }
211c19800e8SDoug Rabson };
212b528cefcSMark Murray
213c19800e8SDoug Rabson static int
parse_source_type(const char * s)214b528cefcSMark Murray parse_source_type(const char *s)
2154137ff4cSJacques Vidrine {
216c19800e8SDoug Rabson size_t i;
217b528cefcSMark Murray for(i = 0; i < sizeof(types) / sizeof(types[0]); i++) {
218b528cefcSMark Murray if(strstr(types[i].name, s) == types[i].name)
219b528cefcSMark Murray return types[i].type;
220c19800e8SDoug Rabson }
221b528cefcSMark Murray return 0;
222c19800e8SDoug Rabson }
223c19800e8SDoug Rabson
224c19800e8SDoug Rabson static int
iterate(krb5_context context,const char * database_name,HDB * db,int type,struct prop_data * pd)225c19800e8SDoug Rabson iterate (krb5_context context,
226c19800e8SDoug Rabson const char *database_name,
227c19800e8SDoug Rabson HDB *db,
228b528cefcSMark Murray int type,
229b528cefcSMark Murray struct prop_data *pd)
230b528cefcSMark Murray {
231b528cefcSMark Murray int ret;
232c19800e8SDoug Rabson
233c19800e8SDoug Rabson switch(type) {
234c19800e8SDoug Rabson case HPROP_MIT_DUMP:
235c19800e8SDoug Rabson ret = mit_prop_dump(pd, database_name);
236c19800e8SDoug Rabson if (ret)
237c19800e8SDoug Rabson krb5_warn(context, ret, "mit_prop_dump");
238b528cefcSMark Murray break;
239b528cefcSMark Murray case HPROP_HEIMDAL:
240b528cefcSMark Murray ret = hdb_foreach(context, db, HDB_F_DECRYPT, v5_prop, pd);
241b528cefcSMark Murray if(ret)
242b528cefcSMark Murray krb5_warn(context, ret, "hdb_foreach");
2434137ff4cSJacques Vidrine break;
244c19800e8SDoug Rabson default:
245b528cefcSMark Murray krb5_errx(context, 1, "unknown prop type: %d", type);
2464137ff4cSJacques Vidrine }
247c19800e8SDoug Rabson return ret;
248b528cefcSMark Murray }
249b528cefcSMark Murray
250b528cefcSMark Murray static int
dump_database(krb5_context context,int type,const char * database_name,HDB * db)251b528cefcSMark Murray dump_database (krb5_context context, int type,
252b528cefcSMark Murray const char *database_name, HDB *db)
253b528cefcSMark Murray {
254b528cefcSMark Murray krb5_error_code ret;
255b528cefcSMark Murray struct prop_data pd;
256b528cefcSMark Murray krb5_data data;
257b528cefcSMark Murray
258b528cefcSMark Murray pd.context = context;
259b528cefcSMark Murray pd.auth_context = NULL;
260b528cefcSMark Murray pd.sock = STDOUT_FILENO;
261b528cefcSMark Murray
262b528cefcSMark Murray ret = iterate (context, database_name, db, type, &pd);
263b528cefcSMark Murray if (ret)
2644137ff4cSJacques Vidrine krb5_errx(context, 1, "iterate failure");
2654137ff4cSJacques Vidrine krb5_data_zero (&data);
2664137ff4cSJacques Vidrine ret = krb5_write_message (context, &pd.sock, &data);
2674137ff4cSJacques Vidrine if (ret)
268b528cefcSMark Murray krb5_err(context, 1, ret, "krb5_write_message");
269b528cefcSMark Murray
270b528cefcSMark Murray return 0;
271b528cefcSMark Murray }
2724137ff4cSJacques Vidrine
2734137ff4cSJacques Vidrine static int
propagate_database(krb5_context context,int type,const char * database_name,HDB * db,krb5_ccache ccache,int optidx,int argc,char ** argv)274b528cefcSMark Murray propagate_database (krb5_context context, int type,
2754137ff4cSJacques Vidrine const char *database_name,
276b528cefcSMark Murray HDB *db, krb5_ccache ccache,
277b528cefcSMark Murray int optidx, int argc, char **argv)
278b528cefcSMark Murray {
2794137ff4cSJacques Vidrine krb5_principal server;
280b528cefcSMark Murray krb5_error_code ret;
281b528cefcSMark Murray int i, failed = 0;
282b528cefcSMark Murray
283c19800e8SDoug Rabson for(i = optidx; i < argc; i++){
284b528cefcSMark Murray krb5_auth_context auth_context;
285b528cefcSMark Murray int fd;
286b528cefcSMark Murray struct prop_data pd;
287b528cefcSMark Murray krb5_data data;
288b528cefcSMark Murray
2894137ff4cSJacques Vidrine char *port, portstr[NI_MAXSERV];
290c19800e8SDoug Rabson char *host = argv[i];
291b528cefcSMark Murray
292b528cefcSMark Murray port = strchr(host, ':');
293b528cefcSMark Murray if(port == NULL) {
2944137ff4cSJacques Vidrine snprintf(portstr, sizeof(portstr), "%u",
295b528cefcSMark Murray ntohs(krb5_getportbyname (context, "hprop", "tcp",
296b528cefcSMark Murray HPROP_PORT)));
297c19800e8SDoug Rabson port = portstr;
298c19800e8SDoug Rabson } else
299c19800e8SDoug Rabson *port++ = '\0';
300c19800e8SDoug Rabson
301c19800e8SDoug Rabson fd = open_socket(context, host, port);
302c19800e8SDoug Rabson if(fd < 0) {
303c19800e8SDoug Rabson failed++;
304c19800e8SDoug Rabson krb5_warn (context, errno, "connect %s", host);
305c19800e8SDoug Rabson continue;
306c19800e8SDoug Rabson }
3075e9cd1aeSAssar Westerlund
308c19800e8SDoug Rabson ret = krb5_sname_to_principal(context, argv[i],
309c19800e8SDoug Rabson HPROP_NAME, KRB5_NT_SRV_HST, &server);
310c19800e8SDoug Rabson if(ret) {
3115e9cd1aeSAssar Westerlund failed++;
312c19800e8SDoug Rabson krb5_warn(context, ret, "krb5_sname_to_principal(%s)", host);
313c19800e8SDoug Rabson close(fd);
314c19800e8SDoug Rabson continue;
315c19800e8SDoug Rabson }
316c19800e8SDoug Rabson
3175e9cd1aeSAssar Westerlund if (local_realm) {
318b528cefcSMark Murray krb5_realm my_realm;
319c19800e8SDoug Rabson krb5_get_default_realm(context,&my_realm);
320c19800e8SDoug Rabson krb5_principal_set_realm(context,server,my_realm);
321c19800e8SDoug Rabson krb5_xfree(my_realm);
322c19800e8SDoug Rabson }
323c19800e8SDoug Rabson
324c19800e8SDoug Rabson auth_context = NULL;
325c19800e8SDoug Rabson ret = krb5_sendauth(context,
326c19800e8SDoug Rabson &auth_context,
327b528cefcSMark Murray &fd,
328c19800e8SDoug Rabson HPROP_VERSION,
329c19800e8SDoug Rabson NULL,
330b528cefcSMark Murray server,
331c19800e8SDoug Rabson AP_OPTS_MUTUAL_REQUIRED | AP_OPTS_USE_SUBKEY,
332c19800e8SDoug Rabson NULL, /* in_data */
333c19800e8SDoug Rabson NULL, /* in_creds */
3345e9cd1aeSAssar Westerlund ccache,
3355e9cd1aeSAssar Westerlund NULL,
3365e9cd1aeSAssar Westerlund NULL,
3375e9cd1aeSAssar Westerlund NULL);
3385e9cd1aeSAssar Westerlund
339c19800e8SDoug Rabson krb5_free_principal(context, server);
340c19800e8SDoug Rabson
3415e9cd1aeSAssar Westerlund if(ret) {
342b528cefcSMark Murray failed++;
343b528cefcSMark Murray krb5_warn(context, ret, "krb5_sendauth (%s)", host);
344c19800e8SDoug Rabson close(fd);
3454137ff4cSJacques Vidrine goto next_host;
346b528cefcSMark Murray }
347b528cefcSMark Murray
348b528cefcSMark Murray pd.context = context;
349c19800e8SDoug Rabson pd.auth_context = auth_context;
350b528cefcSMark Murray pd.sock = fd;
351b528cefcSMark Murray
352b528cefcSMark Murray ret = iterate (context, database_name, db, type, &pd);
353c19800e8SDoug Rabson if (ret) {
354b528cefcSMark Murray krb5_warnx(context, "iterate to host %s failed", host);
355b528cefcSMark Murray failed++;
3564137ff4cSJacques Vidrine goto next_host;
357c19800e8SDoug Rabson }
358c19800e8SDoug Rabson
359b528cefcSMark Murray krb5_data_zero (&data);
360b528cefcSMark Murray ret = krb5_write_priv_message(context, auth_context, &fd, &data);
361b528cefcSMark Murray if(ret) {
362c19800e8SDoug Rabson krb5_warn(context, ret, "krb5_write_priv_message");
363c19800e8SDoug Rabson failed++;
364c19800e8SDoug Rabson goto next_host;
365c19800e8SDoug Rabson }
366b528cefcSMark Murray
367b528cefcSMark Murray ret = krb5_read_priv_message(context, auth_context, &fd, &data);
368b528cefcSMark Murray if(ret) {
369b528cefcSMark Murray krb5_warn(context, ret, "krb5_read_priv_message: %s", host);
370b528cefcSMark Murray failed++;
371c19800e8SDoug Rabson goto next_host;
372c19800e8SDoug Rabson } else
373b528cefcSMark Murray krb5_data_free (&data);
374b528cefcSMark Murray
375b528cefcSMark Murray next_host:
376b528cefcSMark Murray krb5_auth_con_free(context, auth_context);
377b528cefcSMark Murray close(fd);
378b528cefcSMark Murray }
379b528cefcSMark Murray if (failed)
3804137ff4cSJacques Vidrine return 1;
381b528cefcSMark Murray return 0;
382b528cefcSMark Murray }
383b528cefcSMark Murray
384b528cefcSMark Murray int
main(int argc,char ** argv)385b528cefcSMark Murray main(int argc, char **argv)
386b528cefcSMark Murray {
387b528cefcSMark Murray krb5_error_code ret;
388b528cefcSMark Murray krb5_context context;
389b528cefcSMark Murray krb5_ccache ccache = NULL;
390adb0ddaeSAssar Westerlund HDB *db = NULL;
391adb0ddaeSAssar Westerlund int optidx = 0;
392b528cefcSMark Murray
393adb0ddaeSAssar Westerlund int type, exit_code;
394adb0ddaeSAssar Westerlund
395b528cefcSMark Murray setprogname(argv[0]);
396b528cefcSMark Murray
397b528cefcSMark Murray if(getarg(args, num_args, argc, argv, &optidx))
398b528cefcSMark Murray usage(1);
399b528cefcSMark Murray
4004137ff4cSJacques Vidrine if(help_flag)
401b528cefcSMark Murray usage(0);
402b528cefcSMark Murray
403b528cefcSMark Murray if(version_flag){
404b528cefcSMark Murray print_version(NULL);
405b528cefcSMark Murray exit(0);
406b528cefcSMark Murray }
407b528cefcSMark Murray
408b528cefcSMark Murray ret = krb5_init_context(&context);
409b528cefcSMark Murray if(ret)
410b528cefcSMark Murray exit(1);
411b528cefcSMark Murray
4125e9cd1aeSAssar Westerlund /* We may be reading an old database encrypted with a DES master key. */
4135e9cd1aeSAssar Westerlund ret = krb5_allow_weak_crypto(context, 1);
4145e9cd1aeSAssar Westerlund if(ret)
4155e9cd1aeSAssar Westerlund krb5_err(context, 1, ret, "krb5_allow_weak_crypto");
4165e9cd1aeSAssar Westerlund
4175e9cd1aeSAssar Westerlund if(local_realm)
4185e9cd1aeSAssar Westerlund krb5_set_default_realm(context, local_realm);
4194137ff4cSJacques Vidrine
420b528cefcSMark Murray if(encrypt_flag && decrypt_flag)
421b528cefcSMark Murray krb5_errx(context, 1,
422b528cefcSMark Murray "only one of `--encrypt' and `--decrypt' is meaningful");
4235e9cd1aeSAssar Westerlund
424b528cefcSMark Murray if(source_type != NULL) {
425b528cefcSMark Murray type = parse_source_type(source_type);
426b528cefcSMark Murray if(type == 0)
427b528cefcSMark Murray krb5_errx(context, 1, "unknown source type `%s'", source_type);
428b528cefcSMark Murray } else
429b528cefcSMark Murray type = HPROP_HEIMDAL;
430b528cefcSMark Murray
431b528cefcSMark Murray if(!to_stdout)
432b528cefcSMark Murray get_creds(context, &ccache);
433b528cefcSMark Murray
434b528cefcSMark Murray if(decrypt_flag || encrypt_flag) {
435b528cefcSMark Murray ret = hdb_read_master_key(context, mkeyfile, &mkey5);
436b528cefcSMark Murray if(ret && ret != ENOENT)
4374137ff4cSJacques Vidrine krb5_err(context, 1, ret, "hdb_read_master_key");
438b528cefcSMark Murray if(ret)
439b528cefcSMark Murray krb5_errx(context, 1, "No master key file found");
440b528cefcSMark Murray }
441b528cefcSMark Murray
442b528cefcSMark Murray switch(type) {
443b528cefcSMark Murray case HPROP_MIT_DUMP:
444b528cefcSMark Murray if (database == NULL)
445b528cefcSMark Murray krb5_errx(context, 1, "no dump file specified");
446b528cefcSMark Murray break;
447c19800e8SDoug Rabson case HPROP_HEIMDAL:
448b528cefcSMark Murray ret = hdb_create (context, &db, database);
449b528cefcSMark Murray if(ret)
450b528cefcSMark Murray krb5_err(context, 1, ret, "hdb_create: %s", database);
4515e9cd1aeSAssar Westerlund ret = db->hdb_open(context, db, O_RDONLY, 0);
4525e9cd1aeSAssar Westerlund if(ret)
4535e9cd1aeSAssar Westerlund krb5_err(context, 1, ret, "db->hdb_open");
454b528cefcSMark Murray break;
455b528cefcSMark Murray default:
456b528cefcSMark Murray krb5_errx(context, 1, "unknown dump type `%d'", type);
457b528cefcSMark Murray break;
458b528cefcSMark Murray }
459b528cefcSMark Murray
460b528cefcSMark Murray if (to_stdout)
461c19800e8SDoug Rabson exit_code = dump_database (context, type, database, db);
462c19800e8SDoug Rabson else
463c19800e8SDoug Rabson exit_code = propagate_database (context, type, database,
464b528cefcSMark Murray db, ccache, optidx, argc, argv);
465c19800e8SDoug Rabson
466b528cefcSMark Murray if(ccache != NULL)
467b528cefcSMark Murray krb5_cc_destroy(context, ccache);
468c19800e8SDoug Rabson
469c19800e8SDoug Rabson if(db != NULL)
470b528cefcSMark Murray (*db->hdb_destroy)(context, db);
471b528cefcSMark Murray
472b528cefcSMark Murray krb5_free_context(context);
473b528cefcSMark Murray return exit_code;
474b528cefcSMark Murray }
475b528cefcSMark Murray