1 /* migrate.c - Migrate from earlier GnupG versions.
2 * Copyright (C) 2014 Werner Koch
4 * This file is part of GnuPG.
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.
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.
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/>.
32 #include "call-agent.h"
35 #ifdef HAVE_DOSISH_SYSTEM
36 # define V21_MIGRATION_FNAME "gpg-v21-migrated"
38 # define V21_MIGRATION_FNAME ".gpg-v21-migrated"
42 /* Check whether a default secring.gpg from GnuPG < 2.1 exists and
43 import it if not yet done. */
45 migrate_secring (ctrl_t ctrl)
47 dotlock_t lockhd = NULL;
49 char *flagfile = NULL;
50 char *agent_version = NULL;
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. */
59 log_info ("starting migration from earlier GnuPG versions\n");
61 lockhd = dotlock_create (flagfile, 0);
64 log_error ("can't allocate lock for '%s': %s\n",
65 flagfile, gpg_strerror (gpg_error_from_syserror ()));
68 if (dotlock_take (lockhd, -1))
70 log_error ("can't lock '%s': %s\n",
71 flagfile, gpg_strerror (gpg_error_from_syserror ()));
72 dotlock_destroy (lockhd);
77 if (!agent_get_version (ctrl, &agent_version))
79 if (!gnupg_compare_version (agent_version, "2.1.0"))
81 log_error ("error: GnuPG agent version \"%s\" is too old. ",
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);
89 xfree (agent_version);
93 log_error ("error: GnuPG agent unusable. "
94 "Please check that a GnuPG agent can be started.\n");
95 log_error ("migration aborted\n");
99 log_info ("porting secret keys from '%s' to gpg-agent\n", secring);
100 if (!import_old_secring (ctrl, secring))
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 ()));
107 log_info ("migration succeeded\n");
113 dotlock_release (lockhd);
114 dotlock_destroy (lockhd);