Turn on memory scrubbing when fuzz testing.
[obnox/wireshark/wip.git] / tempfile.c
1 /* tempfile.c
2  * Routines to create temporary files
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <glib.h>
30
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdio.h>
34 #include <errno.h>
35
36 #ifdef HAVE_FCNTL_H
37 #include <fcntl.h>
38 #endif
39
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
43
44 #ifdef HAVE_WINDOWS_H
45 #include <windows.h>
46 #endif
47
48 #ifdef _WIN32
49 #include <process.h>    /* For getpid() */
50 #endif
51
52 #include "tempfile.h"
53 #include <wsutil/file_util.h>
54
55 #ifndef __set_errno
56 #define __set_errno(x) errno=(x)
57 #endif
58
59 #ifndef HAVE_MKSTEMP
60 /* Generate a unique temporary file name from TEMPLATE.
61    The last six characters of TEMPLATE must be "XXXXXX";
62    they are replaced with a string that makes the filename unique.
63    Returns a file descriptor open on the file for reading and writing.  */
64 static int
65 mkstemp (template)
66      char *template;
67 {
68   static const char letters[]
69     = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
70   size_t len;
71   size_t i;
72
73   len = strlen (template);
74   if (len < 6 || strcmp (&template[len - 6], "XXXXXX"))
75     {
76       __set_errno (EINVAL);
77       return -1;
78     }
79
80   if (g_snprintf (&template[len - 5], 6, "%.5u",
81                (unsigned int) getpid () % 100000) != 5)
82     /* Inconceivable lossage.  */
83     return -1;
84
85   for (i = 0; i < sizeof (letters); ++i)
86     {
87       int fd;
88
89       template[len - 6] = letters[i];
90
91       fd = ws_open (template, O_RDWR|O_BINARY|O_CREAT|O_EXCL, 0600);
92       if (fd >= 0)
93         return fd;
94     }
95
96   /* We return the null string if we can't find a unique file name.  */
97
98   template[0] = '\0';
99   return -1;
100 }
101
102 #endif /* HAVE_MKSTEMP */
103
104 #ifndef HAVE_MKDTEMP
105 /* Generate a unique temporary directory name from TEMPLATE.
106    The last six characters of TEMPLATE must be "XXXXXX";
107    they are replaced with a string that makes the filename unique.
108    Returns 0 on success or -1 on error (from mkdir(2)).  */
109 char *
110 mkdtemp (template)
111      char *template;
112 {
113   static const char letters[]
114     = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
115   size_t len;
116   size_t i;
117
118   len = strlen (template);
119   if (len < 6 || strcmp (&template[len - 6], "XXXXXX"))
120     {
121       __set_errno (EINVAL);
122       return NULL;
123     }
124
125   if (g_snprintf (&template[len - 5], 6, "%.5u",
126                (unsigned int) getpid () % 100000) != 5)
127     /* Inconceivable lossage.  */
128     return NULL;
129
130   for (i = 0; i < sizeof (letters); ++i)
131     {
132       int ret;
133
134       template[len - 6] = letters[i];
135
136       ret = ws_mkdir(template, 0700);
137       if (ret >= 0)
138         return template;
139     }
140
141   /* We return the null string if we can't find a unique file name.  */
142
143   template[0] = '\0';
144   return NULL;
145 }
146
147 #endif /* HAVE_MKDTEMP */
148
149
150 #define INITIAL_PATH_SIZE 128
151 #define TMP_FILE_SUFFIX "XXXXXXXXXX"
152
153 /**
154  * Create a tempfile with the given prefix (e.g. "wireshark").
155  * 
156  * @param namebuf If not NULL, receives the full path of the temp file.
157  *                Should NOT be freed.
158  * @param pfx A prefix for the temporary file.
159  * @return The file descriptor of the new tempfile, from mkstemp().
160  */
161 int
162 create_tempfile(char **namebuf, const char *pfx)
163 {
164         static char *tf_path[3];
165         static int tf_path_len[3];
166         static int idx;
167         const char *tmp_dir;
168         int old_umask;
169         int fd;
170
171         idx = (idx + 1) % 3;
172         
173         /*
174          * Allocate the buffer if it's not already allocated.
175          */
176         if (tf_path[idx] == NULL) {
177                 tf_path_len[idx] = INITIAL_PATH_SIZE;
178                 tf_path[idx] = (char *)g_malloc(tf_path_len[idx]);
179         }
180
181         /*
182          * We can't use get_tempfile_path here because we're called from dumpcap.c.
183          */
184         tmp_dir = g_get_tmp_dir();
185
186         while (g_snprintf(tf_path[idx], tf_path_len[idx], "%s%c%s" TMP_FILE_SUFFIX, tmp_dir, G_DIR_SEPARATOR, pfx) > tf_path_len[idx]) {
187                 tf_path_len[idx] *= 2;
188                 tf_path[idx] = (char *)g_realloc(tf_path[idx], tf_path_len[idx]);
189         }
190
191         if (namebuf) {
192                 *namebuf = tf_path[idx];
193         }
194         /* The Single UNIX Specification doesn't say that "mkstemp()"
195            creates the temporary file with mode rw-------, so we
196            won't assume that all UNIXes will do so; instead, we set
197            the umask to 0077 to take away all group and other
198            permissions, attempt to create the file, and then put
199            the umask back. */
200         old_umask = umask(0077);
201         fd = mkstemp(tf_path[idx]);
202         umask(old_umask);
203         return fd;
204 }
205
206 /**
207  * Create a directory with the given prefix (e.g. "wireshark"). The path
208  * is created using g_get_tmp_dir and mkdtemp.
209  * 
210  * @param pfx A prefix for the temporary directory.
211  * @return The temporary directory path on success, or NULL on failure.
212  *         Must NOT be freed.
213  */
214 const char *
215 create_tempdir(char **namebuf, const char *pfx)
216 {
217         static char *td_path[3];
218         static int td_path_len[3];
219         static int idx;
220         const char *tmp_dir;
221
222         idx = (idx + 1) % 3;
223         
224         /*
225          * Allocate the buffer if it's not already allocated.
226          */
227         if (td_path[idx] == NULL) {
228                 td_path_len[idx] = INITIAL_PATH_SIZE;
229                 td_path[idx] = (char *)g_malloc(td_path_len[idx]);
230         }
231
232         /*
233          * We can't use get_tempfile_path here because we're called from dumpcap.c.
234          */
235         tmp_dir = g_get_tmp_dir();
236
237         while (g_snprintf(td_path[idx], td_path_len[idx], "%s%c%s" TMP_FILE_SUFFIX, tmp_dir, G_DIR_SEPARATOR, pfx) > td_path_len[idx]) {
238                 td_path_len[idx] *= 2;
239                 td_path[idx] = (char *)g_realloc(td_path[idx], td_path_len[idx]);
240         }
241
242         if (namebuf) {
243                 *namebuf = td_path[idx];
244         }
245         return mkdtemp(td_path[idx]);
246 }