]> git.saurik.com Git - apple/libc.git/blame - stdio/FreeBSD/mktemp.3
Libc-997.1.1.tar.gz
[apple/libc.git] / stdio / FreeBSD / mktemp.3
CommitLineData
5b2abdfb
A
1.\" Copyright (c) 1989, 1991, 1993
2.\" The Regents of the University of California. All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\" notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\" notice, this list of conditions and the following disclaimer in the
11.\" documentation and/or other materials provided with the distribution.
5b2abdfb
A
12.\" 4. Neither the name of the University nor the names of its contributors
13.\" may be used to endorse or promote products derived from this software
14.\" without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\" @(#)mktemp.3 8.1 (Berkeley) 6/4/93
1f2f436a 29.\" $FreeBSD: src/lib/libc/stdio/mktemp.3,v 1.22 2007/01/09 00:28:07 imp Exp $
5b2abdfb
A
30.\"
31.Dd February 11, 1998
32.Dt MKTEMP 3
33.Os
34.Sh NAME
ad3c9f2a
A
35.Nm mkdtemp ,
36.Nm mkstemp ,
37.Nm mkstemps ,
5b2abdfb
A
38.Nm mktemp
39.Nd make temporary file name (unique)
40.Sh LIBRARY
41.Lb libc
42.Sh SYNOPSIS
43.In unistd.h
44.Ft char *
ad3c9f2a
A
45.Fo mkdtemp
46.Fa "char *template"
47.Fc
5b2abdfb 48.Ft int
ad3c9f2a
A
49.Fo mkstemps
50.Fa "char *template"
51.Fa "int suffixlen"
52.Fc
53.In stdlib.h
5b2abdfb 54.Ft int
ad3c9f2a
A
55.Fo mkstemp
56.Fa "char *template"
57.Fc
5b2abdfb 58.Ft char *
ad3c9f2a
A
59.Fo mktemp
60.Fa "char *template"
61.Fc
5b2abdfb
A
62.Sh DESCRIPTION
63The
64.Fn mktemp
65function
66takes the given file name template and overwrites a portion of it
67to create a file name.
68This file name is guaranteed not to exist at the time of function invocation
69and is suitable for use
70by the application.
71The template may be any file name with some number of
72.Ql X Ns s
73appended
74to it, for example
75.Pa /tmp/temp.XXXXXX .
76The trailing
77.Ql X Ns s
78are replaced with a
79unique alphanumeric combination.
80The number of unique file names
81.Fn mktemp
82can return depends on the number of
83.Ql X Ns s
84provided; six
85.Ql X Ns s
86will
87result in
88.Fn mktemp
89selecting one of 56800235584 (62 ** 6) possible temporary file names.
90.Pp
91The
92.Fn mkstemp
93function
94makes the same replacement to the template and creates the template file,
95mode 0600, returning a file descriptor opened for reading and writing.
96This avoids the race between testing for a file's existence and opening it
97for use.
98.Pp
99The
100.Fn mkstemps
101function acts the same as
102.Fn mkstemp ,
103except it permits a suffix to exist in the template.
104The template should be of the form
105.Pa /tmp/tmpXXXXXXsuffix .
9385eb3d 106The
5b2abdfb 107.Fn mkstemps
9385eb3d 108function
5b2abdfb
A
109is told the length of the suffix string.
110.Pp
111The
112.Fn mkdtemp
113function makes the same replacement to the template as in
9385eb3d 114.Fn mktemp
5b2abdfb
A
115and creates the template directory, mode 0700.
116.Sh RETURN VALUES
117The
118.Fn mktemp
119and
120.Fn mkdtemp
121functions return a pointer to the template on success and
122.Dv NULL
123on failure.
124The
125.Fn mkstemp
126and
127.Fn mkstemps
128functions
129return \-1 if no suitable file could be created.
130If either call fails an error code is placed in the global variable
131.Va errno .
132.Sh ERRORS
133The
134.Fn mkstemp ,
135.Fn mkstemps
136and
137.Fn mkdtemp
138functions
139may set
140.Va errno
141to one of the following values:
142.Bl -tag -width Er
143.It Bq Er ENOTDIR
144The pathname portion of the template is not an existing directory.
145.El
146.Pp
147The
148.Fn mkstemp ,
ad3c9f2a 149.Fn mkstemps ,
5b2abdfb
A
150and
151.Fn mkdtemp
152functions
153may also set
154.Va errno
155to any value specified by the
156.Xr stat 2
157function.
158.Pp
159The
160.Fn mkstemp
161and
162.Fn mkstemps
163functions
164may also set
165.Va errno
166to any value specified by the
167.Xr open 2
168function.
169.Pp
170The
171.Fn mkdtemp
172function
173may also set
174.Va errno
175to any value specified by the
176.Xr mkdir 2
177function.
178.Sh NOTES
179A common problem that results in a core dump is that the programmer
180passes in a read-only string to
181.Fn mktemp ,
182.Fn mkstemp ,
ad3c9f2a 183.Fn mkstemps ,
5b2abdfb
A
184or
185.Fn mkdtemp .
186This is common with programs that were developed before
187.St -isoC
188compilers were common.
189For example, calling
190.Fn mkstemp
191with an argument of
192.Qq /tmp/tempfile.XXXXXX
193will result in a core dump due to
194.Fn mkstemp
195attempting to modify the string constant that was given.
196If the program in question makes heavy use of that type
197of function call, you do have the option of compiling the program
198so that it will store string constants in a writable segment of memory.
199See
200.Xr gcc 1
201for more information.
ad3c9f2a
A
202.Sh LEGACY SYNOPSIS
203.Fd #include <unistd.h>
204.Pp
205The include file
206.In unistd.h
207is necessary and sufficient for all functions.
1f2f436a
A
208.Sh SEE ALSO
209.Xr chmod 2 ,
210.Xr getpid 2 ,
211.Xr mkdir 2 ,
212.Xr open 2 ,
ad3c9f2a
A
213.Xr stat 2 ,
214.Xr compat 5
1f2f436a
A
215.Sh HISTORY
216A
217.Fn mktemp
218function appeared in
219.At v7 .
220The
221.Fn mkstemp
222function appeared in
223.Bx 4.4 .
224The
225.Fn mkdtemp
226function first appeared in
227.Ox 2.2 ,
228and later in
229.Fx 3.2 .
230The
231.Fn mkstemps
232function first appeared in
233.Ox 2.4 ,
234and later in
235.Fx 3.4 .
5b2abdfb
A
236.Sh BUGS
237This family of functions produces filenames which can be guessed,
238though the risk is minimized when large numbers of
239.Ql X Ns s
240are used to
241increase the number of possible temporary filenames.
242This makes the race in
243.Fn mktemp ,
244between testing for a file's existence (in the
245.Fn mktemp
246function call)
247and opening it for use
248(later in the user application)
249particularly dangerous from a security perspective.
250Whenever it is possible,
251.Fn mkstemp
252should be used instead, since it does not have the race condition.
253If
254.Fn mkstemp
255cannot be used, the filename created by
256.Fn mktemp
257should be created using the
258.Dv O_EXCL
259flag to
260.Xr open 2
261and the return status of the call should be tested for failure.
262This will ensure that the program does not continue blindly
263in the event that an attacker has already created the file
264with the intention of manipulating or reading its contents.