Debian Codegraph @

pkcs11/gnome2-store/frob-gnome2-file.c

raw
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* dump-data-file.c: Dump a gkm data file
3
4 Copyright (C) 2009 Stefan Walter
5
6 The Gnome Keyring Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
10
11 The Gnome Keyring Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public
17 License along with the Gnome Library; see the file COPYING.LIB. If not,
18 <http://www.gnu.org/licenses/>.
19
20 Author: Stef Walter <[email protected]>
21 */
22
23 #include "config.h"
24
25 #include "gnome2-store/gkm-gnome2-file.h"
26
27 #include "gkm/gkm-crypto.h"
28
29 #include "egg/egg-secure-memory.h"
30
31 #include <glib.h>
32
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40
41 EGG_SECURE_DEFINE_GLIB_GLOBALS ();
42
43 static void G_GNUC_NORETURN
44 failure (const gchar* message, ...)
45 {
46 va_list va;
47 va_start (va, message);
48 vfprintf (stderr, message, va);
49 fputc ('\n', stderr);
50 va_end (va);
51 exit (1);
52 }
53
54 int
55 main(int argc, char* argv[])
56 {
57 const gchar *password;
58 GkmDataResult res;
59 GkmGnome2File *file;
60 GkmSecret *login;
61 int fd;
62
63 #if !GLIB_CHECK_VERSION(2,35,0)
64 g_type_init ();
65 #endif
66 gkm_crypto_initialize ();
67
68 if (argc != 2)
69 failure ("usage: dump-data-file filename");
70
71 fd = open (argv[1], O_RDONLY, 0);
72 if (fd == -1)
73 failure ("dump-data-file: couldn't open file: %s: %s", argv[1], g_strerror (errno));
74
75 password = getpass ("Password: ");
76 login = gkm_secret_new ((guchar*)password, strlen (password));
77
78 file = gkm_gnome2_file_new ();
79 res = gkm_gnome2_file_read_fd (file, fd, login);
80 g_object_unref (login);
81
82 switch(res) {
83 case GKM_DATA_FAILURE:
84 failure ("dump-data-file: failed to read file: %s", argv[1]);
85 case GKM_DATA_LOCKED:
86 failure ("dump-data-file: invalid password for file: %s", argv[1]);
87 case GKM_DATA_UNRECOGNIZED:
88 failure ("dump-data-file: unparseable file format: %s", argv[1]);
89 case GKM_DATA_SUCCESS:
90 break;
91 default:
92 g_assert_not_reached ();
93 }
94
95 gkm_gnome2_file_dump (file);
96 g_object_unref (file);
97
98 return 0;
99 }