Coverage Report

Created: 2026-01-17 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnupg/kbx/keybox-util.c
Line
Count
Source
1
/* keybox-util.c - Utility functions for Keybox
2
 *  Copyright (C) 2001 Free Software Foundation, Inc.
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 <stdlib.h>
22
#include <stdio.h>
23
#include <string.h>
24
#ifdef  HAVE_DOSISH_SYSTEM
25
# define WIN32_LEAN_AND_MEAN  /* We only need the OS core stuff.  */
26
# include <windows.h>
27
#endif
28
29
#include "keybox-defs.h"
30
31
32
/* Store the two malloced temporary file names used for keybox updates
33
   of file FILENAME at R_BAKNAME and R_TMPNAME.  On error an error
34
   code is returned and NULL stored at R_BAKNAME and R_TMPNAME.  If
35
   FOR_KEYRING is true the returned names match those used by GnuPG's
36
   keyring code.  */
37
gpg_error_t
38
keybox_tmp_names (const char *filename, int for_keyring,
39
                  char **r_bakname, char **r_tmpname)
40
1
{
41
1
  gpg_error_t err;
42
1
  char *bak_name, *tmp_name;
43
44
1
  *r_bakname = NULL;
45
1
  *r_tmpname = NULL;
46
47
# ifdef USE_ONLY_8DOT3
48
  /* Here is another Windoze bug?:
49
   * you can't rename("pubring.kbx.tmp", "pubring.kbx");
50
   * but  rename("pubring.kbx.tmp", "pubring.aaa");
51
   * works.  So we replace ".kbx" by ".kb_" or ".k__".  Note that we
52
   * can't use ".bak" and ".tmp", because these suffixes are used by
53
   * gpg's keyrings and would lead to a sharing violation or data
54
   * corruption.  If the name does not end in ".kbx" we assume working
55
   * on a modern file system and append the suffix.  */
56
  {
57
    const char *ext   = for_keyring? EXTSEP_S GPGEXT_GPG : EXTSEP_S "kbx";
58
    const char *b_ext = for_keyring? EXTSEP_S "bak"      : EXTSEP_S "kb_";
59
    const char *t_ext = for_keyring? EXTSEP_S "tmp"      : EXTSEP_S "k__";
60
    int repl;
61
62
    if (strlen (ext) != 4 || strlen (b_ext) != 4)
63
      BUG ();
64
    repl = (strlen (filename) > 4
65
            && !strcmp (filename + strlen (filename) - 4, ext));
66
    bak_name = xtrymalloc (strlen (filename) + (repl?0:4) + 1);
67
    if (!bak_name)
68
      return gpg_error_from_syserror ();
69
    strcpy (bak_name, filename);
70
    strcpy (bak_name + strlen (filename) - (repl?4:0), b_ext);
71
72
    tmp_name = xtrymalloc (strlen (filename) + (repl?0:4) + 1);
73
    if (!tmp_name)
74
      {
75
        err = gpg_error_from_syserror ();
76
        xfree (bak_name);
77
        return err;
78
      }
79
    strcpy (tmp_name, filename);
80
    strcpy (tmp_name + strlen (filename) - (repl?4:0), t_ext);
81
  }
82
# else /* Posix file names */
83
1
  (void)for_keyring;
84
1
  bak_name = xtrymalloc (strlen (filename) + 2);
85
1
  if (!bak_name)
86
0
    return gpg_error_from_syserror ();
87
1
  strcpy (stpcpy (bak_name, filename), "~");
88
89
1
  tmp_name = xtrymalloc (strlen (filename) + 5);
90
1
  if (!tmp_name)
91
0
    {
92
0
      err = gpg_error_from_syserror ();
93
0
      xfree (bak_name);
94
0
      return err;
95
0
    }
96
1
  strcpy (stpcpy (tmp_name,filename), EXTSEP_S "tmp");
97
1
# endif /* Posix filename */
98
99
1
  *r_bakname = bak_name;
100
1
  *r_tmpname = tmp_name;
101
1
  return 0;
102
1
}