chiark / gitweb /
Import gnupg2_2.1.17.orig.tar.bz2
[gnupg2.git] / g10 / migrate.c
1 /* migrate.c - Migrate from earlier GnupG versions.
2  * Copyright (C) 2014 Werner Koch
3  *
4  * This file is part of GnuPG.
5  *
6  * GnuPG is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GnuPG 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <https://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <unistd.h>
26
27 #include "gpg.h"
28 #include "options.h"
29 #include "keydb.h"
30 #include "util.h"
31 #include "main.h"
32 #include "call-agent.h"
33
34
35 #ifdef HAVE_DOSISH_SYSTEM
36 # define V21_MIGRATION_FNAME "gpg-v21-migrated"
37 #else
38 # define V21_MIGRATION_FNAME ".gpg-v21-migrated"
39 #endif
40
41
42 /* Check whether a default secring.gpg from GnuPG < 2.1 exists and
43    import it if not yet done.  */
44 void
45 migrate_secring (ctrl_t ctrl)
46 {
47   dotlock_t lockhd = NULL;
48   char *secring = NULL;
49   char *flagfile = NULL;
50   char *agent_version = NULL;
51
52   secring = make_filename (gnupg_homedir (), "secring" EXTSEP_S "gpg", NULL);
53   if (access (secring, F_OK))
54     goto leave; /* Does not exist or is not readable.  */
55   flagfile = make_filename (gnupg_homedir (), V21_MIGRATION_FNAME, NULL);
56   if (!access (flagfile, F_OK))
57     goto leave; /* Does exist - fine.  */
58
59   log_info ("starting migration from earlier GnuPG versions\n");
60
61   lockhd = dotlock_create (flagfile, 0);
62   if (!lockhd)
63     {
64       log_error ("can't allocate lock for '%s': %s\n",
65                  flagfile, gpg_strerror (gpg_error_from_syserror ()));
66       goto leave;
67     }
68   if (dotlock_take (lockhd, -1))
69     {
70       log_error ("can't lock '%s': %s\n",
71                  flagfile, gpg_strerror (gpg_error_from_syserror ()));
72       dotlock_destroy (lockhd);
73       lockhd = NULL;
74       goto leave;
75     }
76
77   if (!agent_get_version (ctrl, &agent_version))
78     {
79       if (!gnupg_compare_version (agent_version, "2.1.0"))
80         {
81           log_error ("error: GnuPG agent version \"%s\" is too old. ",
82                      agent_version);
83           log_info ("Please make sure that a recent gpg-agent is running.\n");
84           log_info ("(restarting the user session may achieve this.)\n");
85           log_info ("migration aborted\n");
86           xfree (agent_version);
87           goto leave;
88         }
89       xfree (agent_version);
90     }
91   else
92     {
93       log_error ("error: GnuPG agent unusable. "
94                  "Please check that a GnuPG agent can be started.\n");
95       log_error ("migration aborted\n");
96       goto leave;
97     }
98
99   log_info ("porting secret keys from '%s' to gpg-agent\n", secring);
100   if (!import_old_secring (ctrl, secring))
101     {
102       FILE *fp = fopen (flagfile, "w");
103       if (!fp || fclose (fp))
104         log_error ("error creating flag file '%s': %s\n",
105                    flagfile, gpg_strerror (gpg_error_from_syserror ()));
106       else
107         log_info ("migration succeeded\n");
108     }
109
110  leave:
111   if (lockhd)
112     {
113       dotlock_release (lockhd);
114       dotlock_destroy (lockhd);
115     }
116   xfree (flagfile);
117   xfree (secring);
118 }