[BACK]Return to mktemp.1 CVS log [TXT][DIR] Up to [local] / src / usr.bin / mktemp

Annotation of src/usr.bin/mktemp/mktemp.1, Revision 1.15

1.15    ! aaron       1: .\"    $OpenBSD: mktemp.1,v 1.14 2000/03/05 00:28:57 aaron Exp $
1.1       millert     2: .\"
1.13      millert     3: .\" Copyright (c) 1996, 2000 Todd C. Miller <Todd.Miller@courtesan.com>
                      4: .\" All rights reserved.
1.1       millert     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.
1.13      millert    14: .\" 3. The name of the author may not be used to endorse or promote products
                     15: .\"    derived from this software without specific prior written permission.
1.1       millert    16: .\"
1.13      millert    17: .\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     18: .\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     19: .\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
                     20: .\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     21: .\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     22: .\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     23: .\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     24: .\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     25: .\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
                     26: .\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1       millert    27: .\"
1.11      aaron      28: .Dd November 20, 1996
1.1       millert    29: .Dt MKTEMP 1
                     30: .Os
                     31: .Sh NAME
                     32: .Nm mktemp
                     33: .Nd make temporary file name (unique)
                     34: .Sh SYNOPSIS
                     35: .Nm mktemp
1.5       millert    36: .Op Fl d
                     37: .Op Fl q
1.1       millert    38: .Op Fl u
                     39: .Ar template
                     40: .Sh DESCRIPTION
                     41: The
                     42: .Nm mktemp
                     43: utility takes the given file name template and overwrites a
1.15    ! aaron      44: portion of it to create a unique file name.
        !            45: The template may be any file name with some number of
1.1       millert    46: .Ql X Ns s
                     47: appended
                     48: to it, for example
1.12      millert    49: .Pa /tmp/temp.XXXXXXXXXX .
1.15    ! aaron      50: .Pp
1.1       millert    51: The trailing
                     52: .Ql X Ns s
1.13      millert    53: are replaced with a combination of the the current process number and
1.15    ! aaron      54: random letters.
        !            55: The name chosen depends both on the number of
1.13      millert    56: .Ql X Ns s
                     57: in the template and the number of collisions with pre-existing files.
1.1       millert    58: The number of unique file names
                     59: .Nm
                     60: can return depends on the number of
                     61: .Ql X Ns s
1.13      millert    62: provided; ten
1.1       millert    63: .Ql X Ns s
                     64: will
                     65: result in
                     66: .Nm
1.13      millert    67: testing roughly 26 ** 10 combinations.
1.1       millert    68: .Pp
                     69: If
                     70: .Nm
1.13      millert    71: can successfully generate a unique file name, the file (or directory)
                     72: is created with file permissions such that it is only readable and writable
                     73: by its owner (unless the
1.1       millert    74: .Fl u
1.13      millert    75: flag is given) and the filename is printed to standard output.
1.8       millert    76: .Pp
1.10      aaron      77: .Nm mktemp
1.8       millert    78: is provided to allow shell scripts to safely use temporary files.
                     79: Traditionally, many shell scripts take the name of the program with
1.15    ! aaron      80: the PID as a suffix and use that as a temporary file name.
        !            81: This kind of naming scheme is predictable and the race condition it creates
        !            82: is easy for an attacker to win.
        !            83: A safer, though still inferior approach
        !            84: is to make a temporary directory using the same naming scheme.
        !            85: While this does allow one to guarantee that a temporary file will not be
        !            86: subverted, it still allows a simple denial of service attack.
        !            87: For these reasons it is suggested that
1.8       millert    88: .Nm
                     89: be used instead.
1.14      aaron      90: .Pp
                     91: The options are as follows:
1.1       millert    92: .Bl -tag -width indent
1.5       millert    93: .It Fl d
                     94: Make a directory instead of a file.
                     95: .It Fl q
1.15    ! aaron      96: Fail silently if an error occurs.
        !            97: This is useful if
1.5       millert    98: a script does not want error output to go to standard error.
1.1       millert    99: .It Fl u
                    100: Operate in
                    101: .Dq unsafe
1.15    ! aaron     102: mode.
        !           103: The temp file will be unlinked before
1.1       millert   104: .Nm
1.15    ! aaron     105: exits.
        !           106: This is slightly better than
1.1       millert   107: .Fn mktemp 3
1.15    ! aaron     108: but still introduces a race condition.
        !           109: Use of this option is not encouraged.
1.6       millert   110: .El
1.15    ! aaron     111: .Pp
1.1       millert   112: The
                    113: .Nm
                    114: utility
1.10      aaron     115: exits with a value of 0 on success or 1 on failure.
1.1       millert   116: .Sh EXAMPLES
                    117: The following
                    118: .Xr sh 1
                    119: fragment illustrates a simple use of
                    120: .Nm
                    121: where the script should quit if it cannot get a safe
                    122: temporary file.
                    123: .Bd -literal -offset indent
1.12      millert   124: TMPFILE=`mktemp /tmp/$0.XXXXXXXXXX` || exit 1
1.1       millert   125: echo "program output" >> $TMPFILE
                    126: .Ed
                    127: .Pp
1.12      millert   128: In this case, we want the script to catch the error ourselves.
1.1       millert   129: .Bd -literal -offset indent
1.12      millert   130: TMPFILE=`mktemp -q /tmp/$0.XXXXXXXXXX`
1.2       millert   131: if [ $? -ne 0 ]; then
1.1       millert   132:        echo "$0: Can't create temp file, exiting..."
                    133:        exit 1
                    134: fi
1.12      millert   135: .Ed
                    136: .Pp
                    137: Or perhaps you don't want to exit if
                    138: .Nm
1.15    ! aaron     139: is unable to create the file.
        !           140: In this case you can protect the part of the script thusly.
1.12      millert   141: .Bd -literal -offset indent
                    142: TMPFILE=`mktemp /tmp/$0.XXXXXXXXXX` && {
                    143:        # Safe to use $TMPFILE in this block
                    144:        echo data > $TMPFILE
                    145:        ...
                    146:        rm -f $TMPFILE
                    147: }
1.1       millert   148: .Ed
                    149: .Sh SEE ALSO
1.8       millert   150: .Xr mkdtemp 3 ,
1.10      aaron     151: .Xr mkstemp 3 ,
                    152: .Xr mktemp 3
1.1       millert   153: .Sh HISTORY
                    154: The
                    155: .Nm
                    156: utility appeared in
1.7       millert   157: .Ox 2.1 .