1*1045db0bSsimonb.\" $NetBSD: mktemp.1,v 1.24 2021/07/25 08:52:03 simonb Exp $ 216e75e8bStron.\" From: $FreeBSD: src/usr.bin/mktemp/mktemp.1,v 1.5 1999/08/28 01:04:13 peter Exp $ 3bd999492Stron.\" From: $OpenBSD: mktemp.1,v 1.8 1998/03/19 06:13:37 millert Exp $ 4bd999492Stron.\" 5bd999492Stron.\" Copyright (c) 1989, 1991, 1993 6bd999492Stron.\" The Regents of the University of California. All rights reserved. 7bd999492Stron.\" 8bd999492Stron.\" Redistribution and use in source and binary forms, with or without 9bd999492Stron.\" modification, are permitted provided that the following conditions 10bd999492Stron.\" are met: 11bd999492Stron.\" 1. Redistributions of source code must retain the above copyright 12bd999492Stron.\" notice, this list of conditions and the following disclaimer. 13bd999492Stron.\" 2. Redistributions in binary form must reproduce the above copyright 14bd999492Stron.\" notice, this list of conditions and the following disclaimer in the 15bd999492Stron.\" documentation and/or other materials provided with the distribution. 1689aaa1bbSagc.\" 3. Neither the name of the University nor the names of its contributors 17bd999492Stron.\" may be used to endorse or promote products derived from this software 18bd999492Stron.\" without specific prior written permission. 19bd999492Stron.\" 20bd999492Stron.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21bd999492Stron.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22bd999492Stron.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23bd999492Stron.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24bd999492Stron.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25bd999492Stron.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26bd999492Stron.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27bd999492Stron.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28bd999492Stron.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29bd999492Stron.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30bd999492Stron.\" SUCH DAMAGE. 31bd999492Stron.\" 32bd999492Stron.\" $FreeBSD: src/usr.bin/mktemp/mktemp.1,v 1.5 1999/08/28 01:04:13 peter Exp $ 33bd999492Stron.\" 34*1045db0bSsimonb.Dd July 25, 2021 35bd999492Stron.Dt MKTEMP 1 36bd999492Stron.Os 37bd999492Stron.Sh NAME 38bd999492Stron.Nm mktemp 39bd999492Stron.Nd make temporary file name (unique) 40bd999492Stron.Sh SYNOPSIS 41bd999492Stron.Nm mktemp 42071cbdf1Swiz.Op Fl dqu 43bcbc23bdSchristos.Op Fl p Ar tmpdir 4447ac9788Shubertf.Bro 4572335cc2Swiz.Fl t Ar prefix 4647ac9788Shubertf.No | 4747ac9788Shubertf.Ar template ... 4847ac9788Shubertf.Brc 49bd999492Stron.Sh DESCRIPTION 50bd999492StronThe 51bd999492Stron.Nm 52cd19fc2eSapbutility 53cd19fc2eSapbis provided to allow shell scripts to safely use temporary files. 54cd19fc2eSapbIt creates temporary files or directories using unique names, 55cd19fc2eSapband prints the names. 56cd19fc2eSapb.Pp 57cd19fc2eSapbThe name of each temporary file or directory is derived from a 58cd19fc2eSapbtemplate that includes several trailing 59cd19fc2eSapb.Ql X 60cd19fc2eSapbcharacters, such as 61cd19fc2eSapb.Pa /tmp/prefix.XXXX . 62bd999492StronThe trailing 63cd19fc2eSapb.Ql X 64*1045db0bSsimonbcharacters in the template are replaced with a unique letter and number 65*1045db0bSsimonbcombination. 66cd19fc2eSapbAny 67cd19fc2eSapb.Ql X 68cd19fc2eSapbcharacters other than at the end of the template are taken literally. 69bd999492StronThe number of unique file names 70bd999492Stron.Nm 71cd19fc2eSapbcan return depends on the number of trailing 72bd999492Stron.Ql X Ns s 73cd19fc2eSapbin the template; six 74bd999492Stron.Ql X Ns s 75071cbdf1Swizwill result in 76bd999492Stron.Nm 77*1045db0bSsimonbtesting roughly 62 ** 6 (56800235584) combinations. 78bd999492Stron.Pp 79cd19fc2eSapbThe templates used to create the unique names are derived from the 80cd19fc2eSapb.Fl t Ar prefix 81cd19fc2eSapboption, or the 82cd19fc2eSapb.Ar template 83cd19fc2eSapbarguments, possibly modified by other options. 84cd19fc2eSapbAny number of temporary files or directories may be created 85cd19fc2eSapbin a single invocation using multiple 86cd19fc2eSapb.Ar template 87cd19fc2eSapbarguments. 88cd19fc2eSapbIt is possible to specify both a 89cd19fc2eSapb.Fl t Ar prefix 90cd19fc2eSapboption and one or more 91cd19fc2eSapb.Ar template 92cd19fc2eSapbarguments, 93cd19fc2eSapbbut this is not usually done. 94cd19fc2eSapb.Pp 95cd19fc2eSapbIf neither a 96cd19fc2eSapb.Fl t Ar prefix 97cd19fc2eSapboption, nor any 98cd19fc2eSapb.Ar template 99cd19fc2eSapbarguments are specified, then the default is equivalent to 100cd19fc2eSapb.Fl t Li mktemp . 101cd19fc2eSapb.Pp 102bd999492StronIf 103bd999492Stron.Nm 104bd999492Stroncan successfully generate a unique file name, the file 105bd999492Stronis created with mode 0600 (unless the 106bd999492Stron.Fl u 107071cbdf1Swizflag is given) and the filename is printed to standard output. 108bd999492Stron.Sh OPTIONS 109bd999492StronThe available options are as follows: 110bd999492Stron.Bl -tag -width indent 111bd999492Stron.It Fl d 112bd999492StronMake a directory instead of a file. 113cd19fc2eSapb.It Fl p Ar tmpdir 114cd19fc2eSapbSpecifies a directory in which temporary files should be created. 115cd19fc2eSapbIf this option is specified, then it applies to all temporary files, 116cd19fc2eSapbincluding those created as a result of a 117cd19fc2eSapb.Fl t Ar prefix 118cd19fc2eSapboption, and those created as a result of a 119cd19fc2eSapb.Ar template 120cd19fc2eSapbargument. 121cd19fc2eSapb.Pp 122cd19fc2eSapbIf the 123cd19fc2eSapb.Fl p Ar tmpdir 124cd19fc2eSapboption is not specified, then 125cd19fc2eSapbtemporary files created as a result of a 126cd19fc2eSapb.Fl t Ar prefix 127cd19fc2eSapboption will use a default temporary directory 128cd19fc2eSapb(as described under the 129cd19fc2eSapb.Fl t 130cd19fc2eSapboption), 131cd19fc2eSapbbut temporary files created as a result of a 132cd19fc2eSapb.Ar template 133cd19fc2eSapbargument will not use a default temporary directory 134cd19fc2eSapb(so they will be created relative to the current working directory, if the 135cd19fc2eSapb.Ar template 136cd19fc2eSapbdoes not begin with 137cd19fc2eSapb.Ql \&/ ) . 138cd19fc2eSapb.It Fl t Ar prefix 139cd19fc2eSapbGenerate a template using an appropriate directory name, followed by the 140cd19fc2eSapbsupplied 141cd19fc2eSapb.Ar prefix , 142cd19fc2eSapbfollowed by 143cd19fc2eSapb.Ql \&.XXXXXXXX . 144cd19fc2eSapbAny 145cd19fc2eSapb.Ql X 146cd19fc2eSapbcharacters in the supplied 147cd19fc2eSapb.Ar prefix 148cd19fc2eSapbare taken literally, but the trailing 149cd19fc2eSapb.Ql X 150cd19fc2eSapbcharacters in the appended 151cd19fc2eSapb.Ql \&.XXXXXXXX 152cd19fc2eSapbare replaced by unique values. 153cd19fc2eSapb.Pp 154cd19fc2eSapbThe directory name used for the template generated by the 155cd19fc2eSapb.Fl t Ar prefix 156cd19fc2eSapboption is taken from the 157cd19fc2eSapb.Fl p Ar tmpdir 158cd19fc2eSapboption, or from the 159cd19fc2eSapb.Ev TMPDIR 160cd19fc2eSapbenvironment variable, or 161cd19fc2eSapb.Pa /tmp 162cd19fc2eSapbas a default. 163cd19fc2eSapb.Pp 164cd19fc2eSapbIf one or more 165cd19fc2eSapb.Ar template 166cd19fc2eSapbarguments are used in addition to the 167cd19fc2eSapb.Fl t Ar prefix 168cd19fc2eSapboption, then the 169cd19fc2eSapb.Ar prefix 170cd19fc2eSapbdoes not apply to the 171cd19fc2eSapb.Ar template 172cd19fc2eSapbarguments. 173bd999492Stron.It Fl q 174be8ae688SgrantFail silently if an error occurs. 175be8ae688SgrantThis is useful if 176bd999492Strona script does not want error output to go to standard error. 177bd999492Stron.It Fl u 178bd999492StronOperate in 179bd999492Stron.Dq unsafe 180be8ae688Sgrantmode. 181be8ae688SgrantThe temp file will be unlinked before 182bd999492Stron.Nm 183be8ae688Sgrantexits. 184be8ae688SgrantThis is slightly better than 185bd999492Stron.Xr mktemp 3 186be8ae688Sgrantbut still introduces a race condition. 187071cbdf1SwizUse of this option is not encouraged. 188bd999492Stron.El 189cd19fc2eSapb.Sh NOTES 190cd19fc2eSapb.Nm 191cd19fc2eSapbtakes care to create the files or directories in a way that is 192cd19fc2eSapbsafe from race conditions (provided the 193cd19fc2eSapb.Fl u 194cd19fc2eSapboption is not used). 195cd19fc2eSapb.Pp 196cd19fc2eSapbTraditionally, without 197cd19fc2eSapb.Nm , 198cd19fc2eSapbmany shell scripts created temporary files 199cd19fc2eSapbusing the name of the program with 200cd19fc2eSapbthe pid as a suffix. 201cd19fc2eSapbThis kind of naming scheme is predictable and creates a race condition that 202cd19fc2eSapballows an attacker to subvert the program by 203cd19fc2eSapbcreating a different file, directory, or symbolic link 204cd19fc2eSapbunder the same name. 205cd19fc2eSapbA safer, though still inferior, approach 2068aed8f1aSsnjis to make a temporary directory using the same naming scheme 207cd19fc2eSapbWhile this does allow one to guarantee that a temporary file will 208cd19fc2eSapbnot be subverted, it still allows a simple denial of service attack. 209cd19fc2eSapbFor these reasons it is recommended that 210cd19fc2eSapb.Nm 2118aed8f1aSsnjbe used instead of simpler schemes. 212cd19fc2eSapb.Pp 213cd19fc2eSapbCare should be taken to ensure that it is appropriate to use an 214cd19fc2eSapbenvironment variable potentially supplied by the user. 2154918722aSkleink.Sh EXIT STATUS 216bd999492StronThe 217bd999492Stron.Nm 218071cbdf1Swizutility exits with a value of 0 on success, and 1 on any failure. 219bd999492Stron.Sh EXAMPLES 220bd999492StronThe following 221bd999492Stron.Xr sh 1 222bd999492Stronfragment illustrates a simple use of 223bd999492Stron.Nm 224bd999492Stronwhere the script should quit if it cannot get a safe 225bd999492Strontemporary file. 226bd999492Stron.Bd -literal -offset indent 227cc48ee25SjmmvTMPFILE=`mktemp /tmp/${0##*/}.XXXXXX` || exit 1 22801869ca4Swizecho "program output" >> $TMPFILE 229bd999492Stron.Ed 230bd999492Stron.Pp 231bd999492StronTo allow the use of $TMPDIR: 232bd999492Stron.Bd -literal -offset indent 233cc48ee25SjmmvTMPFILE=`mktemp -t ${0##*/}` || exit 1 23401869ca4Swizecho "program output" >> $TMPFILE 235bd999492Stron.Ed 236bd999492Stron.Pp 237bd999492StronIn this case, we want the script to catch the error itself. 238bd999492Stron.Bd -literal -offset indent 239cc48ee25SjmmvTMPFILE=`mktemp -q /tmp/${0##*/}.XXXXXX` 240bd999492Stronif [ $? -ne 0 ]; then 241bd999492Stron echo "$0: Can't create temp file, exiting..." 242bd999492Stron exit 1 243bd999492Stronfi 244bd999492Stron.Ed 245bd999492Stron.Sh SEE ALSO 246bd999492Stron.Xr mkdtemp 3 , 247bd999492Stron.Xr mkstemp 3 , 248bd999492Stron.Xr mktemp 3 , 249bd999492Stron.Xr environ 7 250bd999492Stron.Sh HISTORY 25116e75e8bStronThe 252bd999492Stron.Nm 253bd999492Stronutility appeared in 25416e75e8bStron.Nx 1.5 . 2558aed8f1aSsnjIt was imported from 2566a2b4183Sgarbled.Fx , 2578aed8f1aSsnjand the idea and the manual page were taken from 2586a2b4183Sgarbled.Ox . 259