Coverage Report

Created: 2025-04-03 08:46

/src/wireshark/wsutil/tempfile.c
Line
Count
Source (jump to first uncovered line)
1
/* tempfile.c
2
 * Routines to create temporary files
3
 *
4
 * Wireshark - Network traffic analyzer
5
 * By Gerald Combs <gerald@wireshark.org>
6
 * Copyright 1998 Gerald Combs
7
 *
8
 * SPDX-License-Identifier: GPL-2.0-or-later
9
 */
10
11
#include "config.h"
12
#include "tempfile.h"
13
14
#include <errno.h>
15
16
#include "file_util.h"
17
18
static char *
19
sanitize_prefix(const char *prefix)
20
0
{
21
0
  if (!prefix) {
22
0
      return NULL;
23
0
  }
24
25
  /* The characters in "delimiters" come from:
26
   * https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions.
27
   * Add to the list as necessary for other OS's.
28
   */
29
0
  const char *delimiters = "<>:\"/\\|?*"
30
0
    "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a"
31
0
    "\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14"
32
0
    "\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
33
34
  /* Sanitize the prefix to resolve bug 7877 */
35
0
  char *safe_prefx = g_strdup(prefix);
36
0
  safe_prefx = g_strdelimit(safe_prefx, delimiters, '-');
37
0
  return safe_prefx;
38
0
}
39
40
 /**
41
 * Create a tempfile with the given prefix (e.g. "wireshark"). The path
42
 * is created using g_file_open_tmp.
43
 *
44
 * @param tempdir [in] If not NULL, the directory in which to create the file.
45
 * @param namebuf [in,out] If not NULL, receives the full path of the temp file.
46
 *                Must be freed.
47
 * @param pfx [in] A prefix for the temporary file.
48
 * @param sfx [in] A file extension for the temporary file. NULL can be passed
49
 *                 if no file extension is needed
50
 * @param err [out] Any error returned by g_file_open_tmp. May be NULL
51
 * @return The file descriptor of the new tempfile, from mkstemps().
52
 */
53
int
54
create_tempfile(const char *tempdir, char **namebuf, const char *pfx, const char *sfx, GError **err)
55
0
{
56
0
  int fd;
57
0
  char *safe_pfx = sanitize_prefix(pfx);
58
59
0
  if (tempdir == NULL || tempdir[0] == '\0') {
60
    /* Use OS default tempdir behaviour */
61
0
    char* filetmpl = ws_strdup_printf("%sXXXXXX%s", safe_pfx ? safe_pfx : "", sfx ? sfx : "");
62
0
    g_free(safe_pfx);
63
64
0
    fd = g_file_open_tmp(filetmpl, namebuf, err);
65
0
    g_free(filetmpl);
66
0
  }
67
0
  else {
68
    /* User-specified tempdir.
69
     * We don't get libc's help generating a random name here.
70
     */
71
0
    const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
72
0
    const int32_t a_len = 64;
73
0
    char* filetmpl = NULL;
74
75
0
    while(1) {
76
0
      g_free(filetmpl);
77
0
      filetmpl = ws_strdup_printf("%s%c%s%c%c%c%c%c%c%s",
78
0
          tempdir,
79
0
          G_DIR_SEPARATOR,
80
0
          safe_pfx ? safe_pfx : "",
81
0
          alphabet[g_random_int_range(0, a_len)],
82
0
          alphabet[g_random_int_range(0, a_len)],
83
0
          alphabet[g_random_int_range(0, a_len)],
84
0
          alphabet[g_random_int_range(0, a_len)],
85
0
          alphabet[g_random_int_range(0, a_len)],
86
0
          alphabet[g_random_int_range(0, a_len)],
87
0
          sfx ? sfx : "");
88
89
0
      fd = ws_open(filetmpl, O_CREAT|O_EXCL|O_BINARY|O_WRONLY, 0600);
90
0
      if (fd >= 0) {
91
0
        break;
92
0
      }
93
0
      if (errno != EEXIST) {
94
0
        g_set_error_literal(err, G_FILE_ERROR,
95
0
            g_file_error_from_errno(errno), g_strerror(errno));
96
0
        g_free(filetmpl);
97
0
        filetmpl = NULL;
98
0
        break;
99
0
      }
100
      /* Loop continues if error was EEXIST, meaning the file we tried
101
       * to make already existed at the destination
102
       */
103
0
    }
104
105
0
    if (namebuf == NULL) {
106
0
      g_free(filetmpl);
107
0
    }
108
0
    else {
109
0
      *namebuf = filetmpl;
110
0
    }
111
0
    g_free(safe_pfx);
112
0
  }
113
114
0
  return fd;
115
0
}
116
117
char *
118
create_tempdir(const char *parent_dir, const char *tmpl, GError **err)
119
0
{
120
0
  if (parent_dir == NULL || parent_dir[0] == '\0') {
121
0
      parent_dir = g_get_tmp_dir();
122
0
  }
123
124
0
  char *safe_pfx = sanitize_prefix(tmpl);
125
0
  if (safe_pfx == NULL) {
126
0
    safe_pfx = g_strdup("wireshark_XXXXXX");
127
0
  }
128
129
0
  char *temp_subdir = g_build_path(G_DIR_SEPARATOR_S, parent_dir, safe_pfx, NULL);
130
0
  g_free(safe_pfx);
131
0
  if (g_mkdtemp(temp_subdir) == NULL)
132
0
  {
133
0
      g_free(temp_subdir);
134
0
      g_set_error_literal(err, G_FILE_ERROR,
135
0
          g_file_error_from_errno(errno), g_strerror(errno));
136
0
      return NULL;
137
0
  }
138
139
0
  return temp_subdir;
140
0
}
141
142
/*
143
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
144
 *
145
 * Local Variables:
146
 * c-basic-offset: 2
147
 * tab-width: 8
148
 * indent-tabs-mode: nil
149
 * End:
150
 *
151
 * ex: set shiftwidth=2 tabstop=8 expandtab:
152
 * :indentSize=2:tabSize=8:noTabs=true:
153
 */