diff options
author | millert <> | 2024-01-19 16:30:28 +0000 |
---|---|---|
committer | millert <> | 2024-01-19 16:30:28 +0000 |
commit | 1d17a25a597033d38c420a0a3add7e5e82dd4c02 (patch) | |
tree | bd07cfcf290c1cc6a0ac3df3a16498fa87c30876 | |
parent | b3ad0ad004d3d681310fe0d75cda8331381c5869 (diff) | |
download | openbsd-1d17a25a597033d38c420a0a3add7e5e82dd4c02.tar.gz openbsd-1d17a25a597033d38c420a0a3add7e5e82dd4c02.tar.bz2 openbsd-1d17a25a597033d38c420a0a3add7e5e82dd4c02.zip |
Move mktemp.c to stdlib where it belongs.
OK deraadt@
-rw-r--r-- | src/lib/libc/stdlib/Makefile.inc | 8 | ||||
-rw-r--r-- | src/lib/libc/stdlib/mktemp.3 | 415 | ||||
-rw-r--r-- | src/lib/libc/stdlib/mktemp.c | 163 |
3 files changed, 582 insertions, 4 deletions
diff --git a/src/lib/libc/stdlib/Makefile.inc b/src/lib/libc/stdlib/Makefile.inc index 55b8018522..fa4836f42b 100644 --- a/src/lib/libc/stdlib/Makefile.inc +++ b/src/lib/libc/stdlib/Makefile.inc | |||
@@ -1,4 +1,4 @@ | |||
1 | # $OpenBSD: Makefile.inc,v 1.64 2017/12/16 20:06:55 guenther Exp $ | 1 | # $OpenBSD: Makefile.inc,v 1.65 2024/01/19 16:30:28 millert Exp $ |
2 | 2 | ||
3 | # stdlib sources | 3 | # stdlib sources |
4 | .PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/stdlib ${LIBCSRCDIR}/stdlib | 4 | .PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/stdlib ${LIBCSRCDIR}/stdlib |
@@ -6,7 +6,7 @@ | |||
6 | SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c atoll.c bsearch.c \ | 6 | SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c atoll.c bsearch.c \ |
7 | exit.c ecvt.c gcvt.c getenv.c getopt_long.c \ | 7 | exit.c ecvt.c gcvt.c getenv.c getopt_long.c \ |
8 | getsubopt.c hcreate.c heapsort.c imaxabs.c imaxdiv.c insque.c \ | 8 | getsubopt.c hcreate.c heapsort.c imaxabs.c imaxdiv.c insque.c \ |
9 | l64a.c llabs.c lldiv.c lsearch.c malloc.c reallocarray.c \ | 9 | l64a.c llabs.c lldiv.c lsearch.c malloc.c mktemp.c reallocarray.c \ |
10 | merge.c posix_pty.c qsort.c radixsort.c rand.c random.c \ | 10 | merge.c posix_pty.c qsort.c radixsort.c rand.c random.c \ |
11 | realpath.c remque.c setenv.c strtoimax.c \ | 11 | realpath.c remque.c setenv.c strtoimax.c \ |
12 | strtol.c strtoll.c strtonum.c strtoul.c strtoull.c strtoumax.c \ | 12 | strtol.c strtoll.c strtonum.c strtoul.c strtoull.c strtoumax.c \ |
@@ -28,6 +28,6 @@ SRCS+= abs.c div.c labs.c ldiv.c | |||
28 | MAN+= a64l.3 abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 atoll.3 \ | 28 | MAN+= a64l.3 abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 atoll.3 \ |
29 | bsearch.3 div.3 ecvt.3 exit.3 getenv.3 getopt.3 getopt_long.3 \ | 29 | bsearch.3 div.3 ecvt.3 exit.3 getenv.3 getopt.3 getopt_long.3 \ |
30 | getsubopt.3 hcreate.3 imaxabs.3 imaxdiv.3 insque.3 labs.3 ldiv.3 \ | 30 | getsubopt.3 hcreate.3 imaxabs.3 imaxdiv.3 insque.3 labs.3 ldiv.3 \ |
31 | lldiv.3 lsearch.3 malloc.3 posix_memalign.3 posix_openpt.3 ptsname.3 \ | 31 | lldiv.3 lsearch.3 malloc.3 mktemp.3 posix_memalign.3 posix_openpt.3 \ |
32 | qsort.3 radixsort.3 rand48.3 rand.3 random.3 realpath.3 \ | 32 | ptsname.3 qsort.3 radixsort.3 rand48.3 rand.3 random.3 realpath.3 \ |
33 | strtod.3 strtonum.3 strtol.3 strtoul.3 system.3 tsearch.3 | 33 | strtod.3 strtonum.3 strtol.3 strtoul.3 system.3 tsearch.3 |
diff --git a/src/lib/libc/stdlib/mktemp.3 b/src/lib/libc/stdlib/mktemp.3 new file mode 100644 index 0000000000..d4bd7bdc91 --- /dev/null +++ b/src/lib/libc/stdlib/mktemp.3 | |||
@@ -0,0 +1,415 @@ | |||
1 | .\" $OpenBSD: mktemp.3,v 1.1 2024/01/19 16:30:28 millert Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1989, 1991, 1993 | ||
4 | .\" The Regents of the University of California. All rights reserved. | ||
5 | .\" | ||
6 | .\" Redistribution and use in source and binary forms, with or without | ||
7 | .\" modification, are permitted provided that the following conditions | ||
8 | .\" are met: | ||
9 | .\" 1. Redistributions of source code must retain the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer. | ||
11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
12 | .\" notice, this list of conditions and the following disclaimer in the | ||
13 | .\" documentation and/or other materials provided with the distribution. | ||
14 | .\" 3. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .Dd $Mdocdate: January 19 2024 $ | ||
31 | .Dt MKTEMP 3 | ||
32 | .Os | ||
33 | .Sh NAME | ||
34 | .Nm mktemp , | ||
35 | .Nm mkstemp , | ||
36 | .Nm mkostemp , | ||
37 | .Nm mkstemps , | ||
38 | .Nm mkostemps , | ||
39 | .Nm mkdtemp | ||
40 | .Nd make temporary file name (unique) | ||
41 | .Sh SYNOPSIS | ||
42 | .In stdlib.h | ||
43 | .Ft char * | ||
44 | .Fn mktemp "char *template" | ||
45 | .Ft int | ||
46 | .Fn mkstemp "char *template" | ||
47 | .Ft int | ||
48 | .Fn mkstemps "char *template" "int suffixlen" | ||
49 | .Ft char * | ||
50 | .Fn mkdtemp "char *template" | ||
51 | .In stdlib.h | ||
52 | .In fcntl.h | ||
53 | .Ft int | ||
54 | .Fn mkostemp "char *template" "int flags" | ||
55 | .Ft int | ||
56 | .Fn mkostemps "char *template" "int suffixlen" "int flags" | ||
57 | .Sh DESCRIPTION | ||
58 | The | ||
59 | .Fn mktemp | ||
60 | family of functions take the given file name template and overwrite | ||
61 | a portion of it to create a new file name. | ||
62 | This file name is unique and suitable for use by the application. | ||
63 | The template may be any file name with at least six trailing | ||
64 | .Em X Ns s , | ||
65 | for example | ||
66 | .Pa /tmp/temp.XXXXXXXX . | ||
67 | The trailing | ||
68 | .Em X Ns s | ||
69 | are replaced with a unique digit and letter combination. | ||
70 | The number of unique file names that can be returned | ||
71 | depends on the number of | ||
72 | .Em X Ns s | ||
73 | provided; | ||
74 | .Fn mktemp | ||
75 | will try at least 2 ** 31 combinations before giving up. | ||
76 | At least six | ||
77 | .Em X Ns s | ||
78 | must be used, though 10 is much better. | ||
79 | .Pp | ||
80 | The | ||
81 | .Fn mktemp | ||
82 | function generates a temporary file name based on a template as | ||
83 | described above. | ||
84 | Because | ||
85 | .Fn mktemp | ||
86 | does not actually create the temporary file, there is a window of | ||
87 | opportunity during which another process can open the file instead. | ||
88 | Because of this race condition, | ||
89 | .Fn mktemp | ||
90 | should not be used where | ||
91 | .Fn mkstemp | ||
92 | can be used instead. | ||
93 | .Fn mktemp | ||
94 | was marked as a legacy interface in | ||
95 | .St -p1003.1-2001 . | ||
96 | .Pp | ||
97 | The | ||
98 | .Fn mkstemp | ||
99 | function makes the same replacement to the template and creates the template | ||
100 | file, mode 0600, returning a file descriptor opened for reading and writing. | ||
101 | This avoids the race between testing for a file's existence and opening it | ||
102 | for use. | ||
103 | .Pp | ||
104 | The | ||
105 | .Fn mkostemp | ||
106 | function acts the same as | ||
107 | .Fn mkstemp , | ||
108 | except that the | ||
109 | .Fa flags | ||
110 | argument may contain zero or more of the following flags for the underlying | ||
111 | .Xr open 2 | ||
112 | system call: | ||
113 | .Pp | ||
114 | .Bl -tag -width "O_CLOEXECXX" -offset indent -compact | ||
115 | .It Dv O_APPEND | ||
116 | Append on each write. | ||
117 | .It Dv O_CLOEXEC | ||
118 | Set the close-on-exec flag on the new file descriptor. | ||
119 | .It Dv O_SYNC | ||
120 | Perform synchronous I/O operations. | ||
121 | .El | ||
122 | .Pp | ||
123 | The | ||
124 | .Fn mkstemps | ||
125 | and | ||
126 | .Fn mkostemps | ||
127 | functions act the same as | ||
128 | .Fn mkstemp | ||
129 | and | ||
130 | .Fn mkostemp , | ||
131 | except they permit a suffix to exist in the template. | ||
132 | The template should be of the form | ||
133 | .Pa /tmp/tmpXXXXXXXXXXsuffix . | ||
134 | .Fn mkstemps | ||
135 | and | ||
136 | .Fn mkostemps | ||
137 | are told the length of the suffix string, i.e., | ||
138 | .Li strlen("suffix") . | ||
139 | .Pp | ||
140 | The | ||
141 | .Fn mkdtemp | ||
142 | function makes the same replacement to the template as in | ||
143 | .Fn mktemp | ||
144 | and creates the template directory, mode 0700. | ||
145 | .Sh RETURN VALUES | ||
146 | The | ||
147 | .Fn mktemp | ||
148 | and | ||
149 | .Fn mkdtemp | ||
150 | functions return a pointer to the template on success and | ||
151 | .Dv NULL | ||
152 | on failure. | ||
153 | The | ||
154 | .Fn mkstemp , | ||
155 | .Fn mkostemp , | ||
156 | .Fn mkstemps , | ||
157 | and | ||
158 | .Fn mkostemps | ||
159 | functions return \-1 if no suitable file could be created. | ||
160 | If any call fails, an error code is placed in the global variable | ||
161 | .Va errno . | ||
162 | .Sh EXAMPLES | ||
163 | Quite often a programmer will want to replace a use of | ||
164 | .Fn mktemp | ||
165 | with | ||
166 | .Fn mkstemp , | ||
167 | usually to avoid the problems described above. | ||
168 | Doing this correctly requires a good understanding of the code in question. | ||
169 | .Pp | ||
170 | For instance, code of this form: | ||
171 | .Bd -literal -offset indent | ||
172 | char sfn[19]; | ||
173 | FILE *sfp; | ||
174 | |||
175 | strlcpy(sfn, "/tmp/ed.XXXXXXXXXX", sizeof(sfn)); | ||
176 | if (mktemp(sfn) == NULL || (sfp = fopen(sfn, "w+")) == NULL) { | ||
177 | warn("%s", sfn); | ||
178 | return (NULL); | ||
179 | } | ||
180 | return (sfp); | ||
181 | .Ed | ||
182 | .Pp | ||
183 | should be rewritten like this: | ||
184 | .Bd -literal -offset indent | ||
185 | char sfn[19]; | ||
186 | FILE *sfp; | ||
187 | int fd; | ||
188 | |||
189 | strlcpy(sfn, "/tmp/ed.XXXXXXXXXX", sizeof(sfn)); | ||
190 | if ((fd = mkstemp(sfn)) == -1 || | ||
191 | (sfp = fdopen(fd, "w+")) == NULL) { | ||
192 | if (fd != -1) { | ||
193 | unlink(sfn); | ||
194 | close(fd); | ||
195 | } | ||
196 | warn("%s", sfn); | ||
197 | return (NULL); | ||
198 | } | ||
199 | return (sfp); | ||
200 | .Ed | ||
201 | .Pp | ||
202 | Often one will find code which uses | ||
203 | .Fn mktemp | ||
204 | very early on, perhaps to globally initialize the template nicely, but the | ||
205 | code which calls | ||
206 | .Xr open 2 | ||
207 | or | ||
208 | .Xr fopen 3 | ||
209 | on that file name will occur much later. | ||
210 | (In almost all cases, the use of | ||
211 | .Xr fopen 3 | ||
212 | will mean that the flags | ||
213 | .Dv O_CREAT | ||
214 | | | ||
215 | .Dv O_EXCL | ||
216 | are not given to | ||
217 | .Xr open 2 , | ||
218 | and thus a symbolic link race becomes possible, hence making | ||
219 | necessary the use of | ||
220 | .Xr fdopen 3 | ||
221 | as seen above.) | ||
222 | Furthermore, one must be careful about code which opens, closes, and then | ||
223 | re-opens the file in question. | ||
224 | Finally, one must ensure that upon error the temporary file is | ||
225 | removed correctly. | ||
226 | .Pp | ||
227 | There are also cases where modifying the code to use | ||
228 | .Fn mktemp , | ||
229 | in concert with | ||
230 | .Xr open 2 | ||
231 | using the flags | ||
232 | .Dv O_CREAT | ||
233 | | | ||
234 | .Dv O_EXCL , | ||
235 | is better, as long as the code retries a new template if | ||
236 | .Xr open 2 | ||
237 | fails with an | ||
238 | .Va errno | ||
239 | of | ||
240 | .Er EEXIST . | ||
241 | .Sh ERRORS | ||
242 | The | ||
243 | .Fn mktemp , | ||
244 | .Fn mkstemp , | ||
245 | .Fn mkostemp , | ||
246 | and | ||
247 | .Fn mkdtemp | ||
248 | functions may set | ||
249 | .Va errno | ||
250 | to one of the following values: | ||
251 | .Bl -tag -width Er | ||
252 | .It Bq Er EINVAL | ||
253 | The | ||
254 | .Ar template | ||
255 | argument has fewer than six trailing | ||
256 | .Em X Ns s . | ||
257 | .It Bq Er EEXIST | ||
258 | All file names tried are already in use. | ||
259 | Consider appending more | ||
260 | .Em X Ns s to the | ||
261 | .Ar template . | ||
262 | .El | ||
263 | .Pp | ||
264 | The | ||
265 | .Fn mkstemps | ||
266 | and | ||
267 | .Fn mkostemps | ||
268 | functions may set | ||
269 | .Va errno | ||
270 | to | ||
271 | .Bl -tag -width Er | ||
272 | .It Bq Er EINVAL | ||
273 | The | ||
274 | .Ar template | ||
275 | argument length is less than | ||
276 | .Ar suffixlen | ||
277 | or it has fewer than six | ||
278 | .Em X Ns s | ||
279 | before the suffix. | ||
280 | .It Bq Er EEXIST | ||
281 | All file names tried are already in use. | ||
282 | Consider appending more | ||
283 | .Em X Ns s to the | ||
284 | .Ar template . | ||
285 | .El | ||
286 | .Pp | ||
287 | In addition, the | ||
288 | .Fn mkostemp | ||
289 | and | ||
290 | .Fn mkostemps | ||
291 | functions may also set | ||
292 | .Va errno | ||
293 | to | ||
294 | .Bl -tag -width Er | ||
295 | .It Bq Er EINVAL | ||
296 | .Fa flags | ||
297 | is invalid. | ||
298 | .El | ||
299 | .Pp | ||
300 | The | ||
301 | .Fn mktemp | ||
302 | function may also set | ||
303 | .Va errno | ||
304 | to any value specified by the | ||
305 | .Xr lstat 2 | ||
306 | function. | ||
307 | .Pp | ||
308 | The | ||
309 | .Fn mkstemp , | ||
310 | .Fn mkostemp , | ||
311 | .Fn mkstemps , | ||
312 | and | ||
313 | .Fn mkostemps | ||
314 | functions may also set | ||
315 | .Va errno | ||
316 | to any value specified by the | ||
317 | .Xr open 2 | ||
318 | function. | ||
319 | .Pp | ||
320 | The | ||
321 | .Fn mkdtemp | ||
322 | function may also set | ||
323 | .Va errno | ||
324 | to any value specified by the | ||
325 | .Xr mkdir 2 | ||
326 | function. | ||
327 | .Sh SEE ALSO | ||
328 | .Xr chmod 2 , | ||
329 | .Xr lstat 2 , | ||
330 | .Xr mkdir 2 , | ||
331 | .Xr open 2 , | ||
332 | .Xr tempnam 3 , | ||
333 | .Xr tmpfile 3 , | ||
334 | .Xr tmpnam 3 | ||
335 | .Sh STANDARDS | ||
336 | The | ||
337 | .Fn mkdtemp | ||
338 | and | ||
339 | .Fn mkstemp | ||
340 | functions conform to the | ||
341 | .St -p1003.1-2008 | ||
342 | specification. | ||
343 | The ability to specify more than six | ||
344 | .Em X Ns s | ||
345 | is an extension to that standard. | ||
346 | The | ||
347 | .Fn mkostemp | ||
348 | function is expected to conform to a future revision of that standard. | ||
349 | .Pp | ||
350 | The | ||
351 | .Fn mktemp | ||
352 | function conforms to | ||
353 | .St -p1003.1-2001 ; | ||
354 | as of | ||
355 | .St -p1003.1-2008 | ||
356 | it is no longer a part of the standard. | ||
357 | .Pp | ||
358 | The | ||
359 | .Fn mkstemps | ||
360 | and | ||
361 | .Fn mkostemps | ||
362 | functions are non-standard and should not be used if portability is required. | ||
363 | .Sh HISTORY | ||
364 | A | ||
365 | .Fn mktemp | ||
366 | function appeared in | ||
367 | .At v7 . | ||
368 | The | ||
369 | .Fn mkdtemp | ||
370 | function appeared in | ||
371 | .Ox 2.2 . | ||
372 | The | ||
373 | .Fn mkstemp | ||
374 | function appeared in | ||
375 | .Bx 4.3 . | ||
376 | The | ||
377 | .Fn mkstemps | ||
378 | function appeared in | ||
379 | .Ox 2.3 . | ||
380 | The | ||
381 | .Fn mkostemp | ||
382 | and | ||
383 | .Fn mkostemps | ||
384 | functions appeared in | ||
385 | .Ox 5.7 . | ||
386 | .Sh BUGS | ||
387 | For | ||
388 | .Fn mktemp | ||
389 | there is an obvious race between file name selection and file | ||
390 | creation and deletion: the program is typically written to call | ||
391 | .Xr tmpnam 3 , | ||
392 | .Xr tempnam 3 , | ||
393 | or | ||
394 | .Fn mktemp . | ||
395 | Subsequently, the program calls | ||
396 | .Xr open 2 | ||
397 | or | ||
398 | .Xr fopen 3 | ||
399 | and erroneously opens a file (or symbolic link, FIFO or other | ||
400 | device) that the attacker has created in the expected file location. | ||
401 | Hence | ||
402 | .Fn mkstemp | ||
403 | is recommended, since it atomically creates the file. | ||
404 | An attacker can guess the file names produced by | ||
405 | .Fn mktemp . | ||
406 | Whenever it is possible, | ||
407 | .Fn mkstemp | ||
408 | or | ||
409 | .Fn mkdtemp | ||
410 | should be used instead. | ||
411 | .Pp | ||
412 | For this reason, | ||
413 | .Xr ld 1 | ||
414 | will output a warning message whenever it links code that uses | ||
415 | .Fn mktemp . | ||
diff --git a/src/lib/libc/stdlib/mktemp.c b/src/lib/libc/stdlib/mktemp.c new file mode 100644 index 0000000000..3b8bba7846 --- /dev/null +++ b/src/lib/libc/stdlib/mktemp.c | |||
@@ -0,0 +1,163 @@ | |||
1 | /* $OpenBSD: mktemp.c,v 1.1 2024/01/19 16:30:28 millert Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1996-1998, 2008 Theo de Raadt | ||
4 | * Copyright (c) 1997, 2008-2009 Todd C. Miller | ||
5 | * | ||
6 | * Permission to use, copy, modify, and distribute this software for any | ||
7 | * purpose with or without fee is hereby granted, provided that the above | ||
8 | * copyright notice and this permission notice appear in all copies. | ||
9 | * | ||
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
17 | */ | ||
18 | |||
19 | #include <sys/types.h> | ||
20 | #include <sys/stat.h> | ||
21 | #include <errno.h> | ||
22 | #include <fcntl.h> | ||
23 | #include <limits.h> | ||
24 | #include <stdio.h> | ||
25 | #include <stdlib.h> | ||
26 | #include <string.h> | ||
27 | #include <ctype.h> | ||
28 | #include <unistd.h> | ||
29 | |||
30 | #define MKTEMP_NAME 0 | ||
31 | #define MKTEMP_FILE 1 | ||
32 | #define MKTEMP_DIR 2 | ||
33 | |||
34 | #define TEMPCHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" | ||
35 | #define NUM_CHARS (sizeof(TEMPCHARS) - 1) | ||
36 | #define MIN_X 6 | ||
37 | |||
38 | #define MKOTEMP_FLAGS (O_APPEND | O_CLOEXEC | O_DSYNC | O_RSYNC | O_SYNC) | ||
39 | |||
40 | #ifndef nitems | ||
41 | #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0])) | ||
42 | #endif | ||
43 | |||
44 | static int | ||
45 | mktemp_internal(char *path, int slen, int mode, int flags) | ||
46 | { | ||
47 | char *start, *cp, *ep; | ||
48 | const char tempchars[] = TEMPCHARS; | ||
49 | unsigned int tries; | ||
50 | struct stat sb; | ||
51 | size_t len; | ||
52 | int fd; | ||
53 | |||
54 | len = strlen(path); | ||
55 | if (len < MIN_X || slen < 0 || (size_t)slen > len - MIN_X) { | ||
56 | errno = EINVAL; | ||
57 | return(-1); | ||
58 | } | ||
59 | ep = path + len - slen; | ||
60 | |||
61 | for (start = ep; start > path && start[-1] == 'X'; start--) | ||
62 | ; | ||
63 | if (ep - start < MIN_X) { | ||
64 | errno = EINVAL; | ||
65 | return(-1); | ||
66 | } | ||
67 | |||
68 | if (flags & ~MKOTEMP_FLAGS) { | ||
69 | errno = EINVAL; | ||
70 | return(-1); | ||
71 | } | ||
72 | flags |= O_CREAT | O_EXCL | O_RDWR; | ||
73 | |||
74 | tries = INT_MAX; | ||
75 | do { | ||
76 | cp = start; | ||
77 | do { | ||
78 | unsigned short rbuf[16]; | ||
79 | unsigned int i; | ||
80 | |||
81 | /* | ||
82 | * Avoid lots of arc4random() calls by using | ||
83 | * a buffer sized for up to 16 Xs at a time. | ||
84 | */ | ||
85 | arc4random_buf(rbuf, sizeof(rbuf)); | ||
86 | for (i = 0; i < nitems(rbuf) && cp != ep; i++) | ||
87 | *cp++ = tempchars[rbuf[i] % NUM_CHARS]; | ||
88 | } while (cp != ep); | ||
89 | |||
90 | switch (mode) { | ||
91 | case MKTEMP_NAME: | ||
92 | if (lstat(path, &sb) != 0) | ||
93 | return(errno == ENOENT ? 0 : -1); | ||
94 | break; | ||
95 | case MKTEMP_FILE: | ||
96 | fd = open(path, flags, S_IRUSR|S_IWUSR); | ||
97 | if (fd != -1 || errno != EEXIST) | ||
98 | return(fd); | ||
99 | break; | ||
100 | case MKTEMP_DIR: | ||
101 | if (mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR) == 0) | ||
102 | return(0); | ||
103 | if (errno != EEXIST) | ||
104 | return(-1); | ||
105 | break; | ||
106 | } | ||
107 | } while (--tries); | ||
108 | |||
109 | errno = EEXIST; | ||
110 | return(-1); | ||
111 | } | ||
112 | |||
113 | char * | ||
114 | _mktemp(char *path) | ||
115 | { | ||
116 | if (mktemp_internal(path, 0, MKTEMP_NAME, 0) == -1) | ||
117 | return(NULL); | ||
118 | return(path); | ||
119 | } | ||
120 | |||
121 | __warn_references(mktemp, | ||
122 | "mktemp() possibly used unsafely; consider using mkstemp()"); | ||
123 | |||
124 | char * | ||
125 | mktemp(char *path) | ||
126 | { | ||
127 | return(_mktemp(path)); | ||
128 | } | ||
129 | |||
130 | int | ||
131 | mkostemps(char *path, int slen, int flags) | ||
132 | { | ||
133 | return(mktemp_internal(path, slen, MKTEMP_FILE, flags)); | ||
134 | } | ||
135 | |||
136 | int | ||
137 | mkstemp(char *path) | ||
138 | { | ||
139 | return(mktemp_internal(path, 0, MKTEMP_FILE, 0)); | ||
140 | } | ||
141 | DEF_WEAK(mkstemp); | ||
142 | |||
143 | int | ||
144 | mkostemp(char *path, int flags) | ||
145 | { | ||
146 | return(mktemp_internal(path, 0, MKTEMP_FILE, flags)); | ||
147 | } | ||
148 | DEF_WEAK(mkostemp); | ||
149 | |||
150 | int | ||
151 | mkstemps(char *path, int slen) | ||
152 | { | ||
153 | return(mktemp_internal(path, slen, MKTEMP_FILE, 0)); | ||
154 | } | ||
155 | |||
156 | char * | ||
157 | mkdtemp(char *path) | ||
158 | { | ||
159 | int error; | ||
160 | |||
161 | error = mktemp_internal(path, 0, MKTEMP_DIR, 0); | ||
162 | return(error ? NULL : path); | ||
163 | } | ||