Allow create_tempfile to support a suffix.

Ping-Bug: 10203
Change-Id: Ifa24870d711449b87e9839dd46af614e4aa28fde
Reviewed-on: https://code.wireshark.org/review/15608
Petri-Dish: Michael Mann <mmann78@netscape.net>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Martin Mathieson <martin.r.mathieson@googlemail.com>
This commit is contained in:
Michael Mann 2016-05-29 13:52:19 -04:00 committed by Martin Mathieson
parent b2845a6ac8
commit acc018b8d1
16 changed files with 47 additions and 41 deletions

View File

@ -119,7 +119,7 @@ check_function_exists("inet_ntop" HAVE_INET_NTOP)
check_function_exists("inet_pton" HAVE_INET_PTON)
check_function_exists("issetugid" HAVE_ISSETUGID)
check_function_exists("mkdtemp" HAVE_MKDTEMP)
check_function_exists("mkstemp" HAVE_MKSTEMP)
check_function_exists("mkstemps" HAVE_MKSTEMPS)
check_function_exists("popcount" HAVE_POPCOUNT)
check_function_exists("setresgid" HAVE_SETRESGID)
check_function_exists("setresuid" HAVE_SETRESUID)

View File

@ -178,8 +178,8 @@
/* Define to 1 if you have the `mkdtemp' function. */
#cmakedefine HAVE_MKDTEMP 1
/* Define to 1 if you have the `mkstemp' function. */
#cmakedefine HAVE_MKSTEMP 1
/* Define to 1 if you have the `mkstemps' function. */
#cmakedefine HAVE_MKSTEMPS 1
/* Define to 1 if you have the `mmap' function. */
#cmakedefine HAVE_MMAP 1

View File

@ -41,7 +41,7 @@
#define HAVE_PLUGINS 1
#define HAVE_EXTCAP 1
/* #undef HAVE_MKSTEMP */
/* #undef HAVE_MKSTEMPS */
/* #undef HAVE_MKDTEMP */
@HAVE_LIBPCAP@

View File

@ -2381,7 +2381,7 @@ AC_REPLACE_FUNCS(inet_ntop)
AC_REPLACE_FUNCS(strptime)
AC_REPLACE_FUNCS(popcount)
AC_CHECK_FUNCS(mkstemp mkdtemp)
AC_CHECK_FUNCS(mkstemps mkdtemp)
AC_CHECK_FUNCS(getprotobynumber)
AC_CHECK_FUNCS(issetugid)
AC_CHECK_FUNCS(sysconf)

View File

@ -2733,7 +2733,7 @@ capture_loop_open_output(capture_options *capture_opts, int *save_file_fd,
{
char *tmpname;
gchar *capfile_name;
gchar *prefix;
gchar *prefix, *suffix;
gboolean is_tempfile;
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_open_output: %s",
@ -2787,6 +2787,7 @@ capture_loop_open_output(capture_options *capture_opts, int *save_file_fd,
/* Choose a random name for the temporary capture buffer */
if (global_capture_opts.ifaces->len > 1) {
prefix = g_strdup_printf("wireshark_%d_interfaces", global_capture_opts.ifaces->len);
suffix = NULL;
} else {
gchar *basename;
basename = g_path_get_basename(g_array_index(global_capture_opts.ifaces, interface_options, 0).console_display_name);
@ -2802,17 +2803,17 @@ capture_loop_open_output(capture_options *capture_opts, int *save_file_fd,
g_string_free(iface, TRUE);
}
#endif
/* generate the temp file name prefix...
* It would be nice if we could specify a pcapng/pcap filename suffix,
* create_tempfile() however currently uses mkstemp() which doesn't allow this - one day perhaps*/
/* generate the temp file name prefix and suffix */
if (capture_opts->use_pcapng) {
prefix = g_strconcat("wireshark_pcapng_", basename, NULL);
prefix = g_strconcat("wireshark_", basename, NULL);
suffix = ".pcapng";
}else{
prefix = g_strconcat("wireshark_pcap_", basename, NULL);
prefix = g_strconcat("wireshark_", basename, NULL);
suffix = ".pcap";
}
g_free(basename);
}
*save_file_fd = create_tempfile(&tmpname, prefix);
*save_file_fd = create_tempfile(&tmpname, prefix, suffix);
g_free(prefix);
capfile_name = g_strdup(tmpname);
is_tempfile = TRUE;

View File

@ -974,7 +974,7 @@ gboolean extcap_create_pipe(char ** fifo)
gchar *temp_name = NULL;
int fd = 0;
if ((fd = create_tempfile(&temp_name, EXTCAP_PIPE_PREFIX)) < 0 )
if ((fd = create_tempfile(&temp_name, EXTCAP_PIPE_PREFIX, NULL)) < 0 )
return FALSE;
ws_close(fd);

2
file.c
View File

@ -1345,7 +1345,7 @@ cf_merge_files(char **out_filenamep, int in_file_count,
if (out_fd == -1)
err = errno;
} else {
out_fd = create_tempfile(&tmpname, "wireshark");
out_fd = create_tempfile(&tmpname, "wireshark", NULL);
if (out_fd == -1)
err = errno;
out_filename = g_strdup(tmpname);

View File

@ -50,7 +50,7 @@ exp_pdu_file_open(exp_pdu_t *exp_pdu_tap_data)
int err;
/* Choose a random name for the temporary import buffer */
import_file_fd = create_tempfile(&tmpname, "Wireshark_PDU_");
import_file_fd = create_tempfile(&tmpname, "Wireshark_PDU_", NULL);
capfile_name = g_strdup(tmpname);
err = exp_pdu_open(exp_pdu_tap_data, import_file_fd,

View File

@ -601,7 +601,7 @@ follow_print_stream(GtkWidget * w _U_, gpointer data)
/* Don't use tmpnam() or such, as this will fail under some ACL */
/* circumstances: http://bugs.wireshark.org/bugzilla/show_bug.cgi?id=358 */
/* Also: tmpnam is "insecure" and should not be used. */
tmp_fd = create_tempfile(&tmp_namebuf, "wshprint");
tmp_fd = create_tempfile(&tmp_namebuf, "wshprint", NULL);
if(tmp_fd == -1) {
simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
"Couldn't create temporary file for printing:\n%s", tmp_namebuf);

View File

@ -3443,7 +3443,7 @@ iax2_analysis(
/* file names for storing sound data */
fd = create_tempfile(&tempname, "wireshark_iax2_f");
fd = create_tempfile(&tempname, "wireshark_iax2_f", NULL);
if (fd < 0) {
simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
"Can't create temporary file for IAX2 analysis:\n%s.",
@ -3453,7 +3453,7 @@ iax2_analysis(
}
user_data->f_tempname = g_strdup(tempname);
ws_close(fd);
fd = create_tempfile(&tempname, "wireshark_iax2_r");
fd = create_tempfile(&tempname, "wireshark_iax2_r", NULL);
if (fd < 0) {
simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
"Can't create temporary file for IAX2 analysis:\n%s.",

View File

@ -992,7 +992,7 @@ print_ok_cb(GtkWidget *ok_bt, gpointer parent_w)
/* Don't use tmpnam() or such, as this will fail under some ACL */
/* circumstances: http://bugs.wireshark.org/bugzilla/show_bug.cgi?id=358 */
/* Also: tmpnam is "insecure" and should not be used. */
tmp_fd = create_tempfile(&tmp_namebuf, "wshprint");
tmp_fd = create_tempfile(&tmp_namebuf, "wshprint", NULL);
if(tmp_fd == -1) {
simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
"Couldn't create a temporary file for printing:\n%s", tmp_namebuf);

View File

@ -3769,7 +3769,7 @@ rtp_analysis(address *src_fwd,
/* file names for storing sound data */
fd = create_tempfile(&tempname, "wireshark_rtp_f");
fd = create_tempfile(&tempname, "wireshark_rtp_f", NULL);
if (fd < 0) {
simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
"Can't create temporary file for RTP analysis:\n%s.",
@ -3779,7 +3779,7 @@ rtp_analysis(address *src_fwd,
}
user_data->f_tempname = g_strdup(tempname);
ws_close(fd);
fd = create_tempfile(&tempname, "wireshark_rtp_r");
fd = create_tempfile(&tempname, "wireshark_rtp_r", NULL);
if (fd < 0) {
simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
"Can't create temporary file for RTP analysis:\n%s.",

View File

@ -2289,7 +2289,7 @@ wtap_dump_open_tempfile_ng(char **filenamep, const char *pfx,
return NULL;
/* Choose a random name for the file */
fd = create_tempfile(&tmpname, pfx);
fd = create_tempfile(&tmpname, pfx, ".pcapng");
if (fd == -1) {
*err = errno;
g_free(wdh);

View File

@ -751,7 +751,7 @@ create_temp_pcapng_file(wtap *wth, int *err, gchar **err_info, nettrace_3gpp_32_
exported_pdu_info.dst_port = 0;
exported_pdu_info.proto_col_str = NULL;
import_file_fd = create_tempfile(&(file_info->tmpname), "Wireshark_PDU_");
import_file_fd = create_tempfile(&(file_info->tmpname), "Wireshark_PDU_", NULL);
/* Now open a file and dump to it */
/* Create data for SHB */

View File

@ -44,36 +44,40 @@
#define INITIAL_PATH_SIZE 128
#define TMP_FILE_SUFFIX "XXXXXX"
#ifndef HAVE_MKSTEMP
#ifndef HAVE_MKSTEMPS
/* Generate a unique temporary file name from TEMPLATE.
The last six characters of TEMPLATE must be TMP_FILE_SUFFIX;
they are replaced with a string that makes the filename unique.
The last six characters before the suffix length of TEMPLATE
must be TMP_FILE_SUFFIX; they are replaced with a string that
makes the filename unique.
Returns a file descriptor open on the file for reading and writing. */
static int
mkstemp (char *path_template)
mkstemps(char *path_template, int suffixlen)
{
static const char letters[]
= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
char uniqueness[6];
size_t len;
size_t i;
len = strlen (path_template);
if (len < 6 || strcmp (&path_template[len - 6], TMP_FILE_SUFFIX))
if (len < 6 || strncmp (&path_template[len - 6 - suffixlen], TMP_FILE_SUFFIX, 6))
{
__set_errno (EINVAL);
return -1;
}
if (g_snprintf (&path_template[len - 5], 6, "%.5u",
if (g_snprintf (uniqueness, 6, "%.5u",
(unsigned int) ws_getpid () % 100000) != 5)
/* Inconceivable lossage. */
return -1;
memcpy(&path_template[len - 5 - suffixlen], uniqueness, 5);
for (i = 0; i < sizeof (letters); ++i)
{
int fd;
path_template[len - 6] = letters[i];
path_template[len - 6 - suffixlen] = letters[i];
fd = ws_open (path_template, O_RDWR|O_BINARY|O_CREAT|O_EXCL, 0600);
if (fd >= 0)
@ -86,7 +90,7 @@ mkstemp (char *path_template)
return -1;
}
#endif /* HAVE_MKSTEMP */
#endif /* HAVE_MKSTEMPS */
#ifndef HAVE_MKDTEMP
/* Generate a unique temporary directory name from TEMPLATE.
@ -149,13 +153,12 @@ char *get_tempfile_path(const char *filename)
* @param namebuf If not NULL, receives the full path of the temp file.
* Should NOT be freed.
* @param pfx A prefix for the temporary file.
* @return The file descriptor of the new tempfile, from mkstemp().
* @todo Switch from mkstemp() to something like mkstemps(), so the caller
* can optionally indicate that part of the pfx is actually a suffix,
* such as "pcap" or "pcapng".
* @param sfx [in] A file extension for the temporary file. NULL can be passed
* if no file extension is needed
* @return The file descriptor of the new tempfile, from mkstemps().
*/
int
create_tempfile(char **namebuf, const char *pfx)
create_tempfile(char **namebuf, const char *pfx, const char *sfx)
{
static struct _tf {
char *path;
@ -203,7 +206,7 @@ create_tempfile(char **namebuf, const char *pfx)
current_time = time(NULL);
strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S", localtime(&current_time));
sep[0] = G_DIR_SEPARATOR;
tmp_file = g_strconcat(tmp_dir, sep, safe_pfx, "_", timestr, "_", TMP_FILE_SUFFIX, NULL);
tmp_file = g_strconcat(tmp_dir, sep, safe_pfx, "_", timestr, "_", TMP_FILE_SUFFIX, sfx, NULL);
g_free(safe_pfx);
if (strlen(tmp_file) > tf[idx].len) {
tf[idx].len = strlen(tmp_file) + 1;
@ -215,14 +218,14 @@ create_tempfile(char **namebuf, const char *pfx)
if (namebuf) {
*namebuf = tf[idx].path;
}
/* The Single UNIX Specification doesn't say that "mkstemp()"
/* The Single UNIX Specification doesn't say that "mkstemps()"
creates the temporary file with mode rw-------, so we
won't assume that all UNIXes will do so; instead, we set
the umask to 0077 to take away all group and other
permissions, attempt to create the file, and then put
the umask back. */
old_umask = ws_umask(0077);
fd = mkstemp(tf[idx].path);
fd = mkstemps(tf[idx].path, sfx ? strlen(sfx) : 0);
ws_umask(old_umask);
return fd;
}

View File

@ -50,9 +50,11 @@ WS_DLL_PUBLIC char *get_tempfile_path(const char *filename);
* @param namebuf [in,out] If not NULL, receives the full path of the temp file.
* Must NOT be freed.
* @param pfx [in] A prefix for the temporary file.
* @return The file descriptor of the new tempfile, from mkstemp().
* @param sfx [in] A file extension for the temporary file. NULL can be passed
* if no file extension is needed
* @return The file descriptor of the new tempfile, from mkstemps().
*/
WS_DLL_PUBLIC int create_tempfile(char **namebuf, const char *pfx);
WS_DLL_PUBLIC int create_tempfile(char **namebuf, const char *pfx, const char *sfx);
/**
* Create a directory with the given prefix (e.g. "wireshark"). The path