xref: /openbsd/usr.bin/mktemp/mktemp.1 (revision d415bd75)
1.\"	$OpenBSD: mktemp.1,v 1.31 2022/03/31 17:27:25 naddy Exp $
2.\"
3.\" Copyright (c) 1996, 2000, 2001, 2003, 2010, 2013
4.\"	Todd C. Miller <millert@openbsd.org>
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.Dd $Mdocdate: March 31 2022 $
19.Dt MKTEMP 1
20.Os
21.Sh NAME
22.Nm mktemp
23.Nd make temporary filename (unique)
24.Sh SYNOPSIS
25.Nm mktemp
26.Op Fl dqtu
27.Op Fl p Ar directory
28.Op Ar template
29.Sh DESCRIPTION
30The
31.Nm mktemp
32utility takes the given filename
33.Ar template
34and overwrites a portion of it to create a unique filename.
35The
36.Ar template
37may be any filename with at least six
38.Ql X Ns s
39appended
40to it, for example
41.Pa /tmp/tfile.XXXXXXXXXX .
42If no
43.Ar template
44is specified, a default of
45.Pa tmp.XXXXXXXXXX
46is used and the
47.Fl t
48flag is implied (see below).
49.Pp
50The trailing
51.Ql X Ns s
52are replaced with a unique digit and letter combination.
53The name chosen depends both on the number of
54.Ql X Ns s
55in the
56.Ar template
57and the number of collisions with pre-existing files.
58The number of unique filenames
59.Nm
60can return depends on the number of
61.Ql X Ns s
62provided; ten
63.Ql X Ns s
64will
65result in
66.Nm
67testing roughly 26 ** 10 combinations.
68.Pp
69If
70.Nm
71can successfully generate a unique filename, the file (or directory)
72is created with file permissions such that it is only readable and writable
73by its owner (unless the
74.Fl u
75flag is given) and the filename is printed to standard output.
76.Pp
77.Nm mktemp
78is provided to allow shell scripts to safely use temporary files.
79Traditionally, many shell scripts take the name of the program with
80the PID as a suffix and use that as a temporary filename.
81This kind of naming scheme is predictable and the race condition it creates
82is easy for an attacker to win.
83A safer, though still inferior approach
84is to make a temporary directory using the same naming scheme.
85While this does allow one to guarantee that a temporary file will not be
86subverted, it still allows a simple denial of service attack.
87For these reasons it is suggested that
88.Nm
89be used instead.
90.Pp
91The options are as follows:
92.Bl -tag -width Ds
93.It Fl d
94Make a directory instead of a file.
95.It Fl p Ar directory
96Use the specified
97.Ar directory
98as a prefix when generating the temporary filename.
99The
100.Ar directory
101will be overridden by the user's
102.Ev TMPDIR
103environment variable if it is set.
104This option implies the
105.Fl t
106flag (see below).
107.It Fl q
108Fail silently if an error occurs.
109This is useful if
110a script does not want error output to go to standard error.
111.It Fl t
112Generate a path rooted in a temporary directory.
113This directory is chosen as follows:
114.Bl -bullet
115.It
116If the user's
117.Ev TMPDIR
118environment variable is set, the directory contained therein is used.
119.It
120Otherwise, if the
121.Fl p
122flag was given the specified directory is used.
123.It
124If none of the above apply,
125.Pa /tmp
126is used.
127.El
128.Pp
129In this mode, the
130.Ar template
131(if specified) should be a directory component (as opposed to a full path)
132and thus should not contain any forward slashes.
133.It Fl u
134Operate in
135.Dq unsafe
136mode.
137The temp file will be unlinked before
138.Nm
139exits.
140This is slightly better than
141.Xr mktemp 3
142but still introduces a race condition.
143Use of this option is not encouraged.
144.El
145.Sh ENVIRONMENT
146.Bl -tag -width TMPDIR
147.It Ev TMPDIR
148directory in which to place the temporary file when in
149.Fl t
150mode
151.El
152.Sh EXIT STATUS
153.Ex -std mktemp
154.Sh EXAMPLES
155The following
156.Xr sh 1
157fragment illustrates a simple use of
158.Nm
159where the script should quit if it cannot get a safe
160temporary file.
161.Bd -literal -offset indent
162TMPFILE=`mktemp /tmp/example.XXXXXXXXXX` || exit 1
163echo "program output" >> $TMPFILE
164.Ed
165.Pp
166The same fragment with support for a user's
167.Ev TMPDIR
168environment variable can be written as follows.
169.Bd -literal -offset indent
170TMPFILE=`mktemp -t example.XXXXXXXXXX` || exit 1
171echo "program output" >> $TMPFILE
172.Ed
173.Pp
174This can be further simplified if we don't care about the actual name of
175the temporary file.
176In this case the
177.Fl t
178flag is implied.
179.Bd -literal -offset indent
180TMPFILE=`mktemp` || exit 1
181echo "program output" >> $TMPFILE
182.Ed
183.Pp
184In some cases, it may be desirable to use a default temporary directory
185other than
186.Pa /tmp .
187In this example the temporary file will be created in
188.Pa /extra/tmp
189unless the user's
190.Ev TMPDIR
191environment variable specifies otherwise.
192.Bd -literal -offset indent
193TMPFILE=`mktemp -p /extra/tmp example.XXXXXXXXXX` || exit 1
194echo "program output" >> $TMPFILE
195.Ed
196.Pp
197In other cases, we want the script to catch the error.
198For instance, if we attempt to create two temporary files and
199the second one fails we need to remove the first before exiting.
200.Bd -literal -offset indent
201TMP1=`mktemp -t example.1.XXXXXXXXXX` || exit 1
202TMP2=`mktemp -t example.2.XXXXXXXXXX`
203if [ $? -ne 0 ]; then
204	rm -f $TMP1
205	exit 1
206fi
207.Ed
208.Pp
209Or perhaps you don't want to exit if
210.Nm
211is unable to create the file.
212In this case you can protect that part of the script thusly.
213.Bd -literal -offset indent
214TMPFILE=`mktemp -q -t example.XXXXXXXXXX` && {
215	# Safe to use $TMPFILE in this block
216	echo data > $TMPFILE
217	...
218	rm -f $TMPFILE
219}
220.Ed
221.Sh DIAGNOSTICS
222One of the following error messages may be displayed if
223.Nm
224does not succeed and the
225.Fl q
226option was not specified:
227.Bl -tag -width indent
228.It Li "insufficient number of Xs in template"
229The specified
230.Ar template
231contained fewer than six
232.Ql X Ns s
233at the end.
234.It Li "template must not contain directory separators in -t mode"
235The
236.Ar template
237contained one or more directory components and the
238.Fl t
239option was specified.
240.It Li "cannot make temp dir"
241.Nm
242was unable to create the temporary directory for any of the reasons
243specified by
244.Xr mkdtemp 3 .
245.It Li "cannot make temp file"
246.Nm
247was unable to create the temporary file for any of the reasons
248specified by
249.Xr mkstemp 3 .
250.It Li "cannot allocate memory"
251.Nm
252was unable to allocate memory for any of the reasons specified by
253.Xr malloc 3 .
254.El
255.Sh SEE ALSO
256.Xr mktemp 3
257.Sh HISTORY
258The
259.Nm
260utility first appeared in
261.Ox 2.1 .
262