[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.13

1.13    ! millert     1: .\"    $OpenBSD: mktemp.1,v 1.12 1999/08/05 09:19:35 millert 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.13    ! millert    44: portion of it to create a unique file name.  The template may be
1.1       millert    45: any file name with some number of
                     46: .Ql X Ns s
                     47: appended
                     48: to it, for example
1.12      millert    49: .Pa /tmp/temp.XXXXXXXXXX .
1.1       millert    50: The trailing
                     51: .Ql X Ns s
1.13    ! millert    52: are replaced with a combination of the the current process number and
        !            53: random letters.  The name chosen depends both on the number of
        !            54: .Ql X Ns s
        !            55: in the template and the number of collisions with pre-existing files.
1.1       millert    56: The number of unique file names
                     57: .Nm
                     58: can return depends on the number of
                     59: .Ql X Ns s
1.13    ! millert    60: provided; ten
1.1       millert    61: .Ql X Ns s
                     62: will
                     63: result in
                     64: .Nm
1.13    ! millert    65: testing roughly 26 ** 10 combinations.
1.1       millert    66: .Pp
                     67: If
                     68: .Nm
1.13    ! millert    69: can successfully generate a unique file name, the file (or directory)
        !            70: is created with file permissions such that it is only readable and writable
        !            71: by its owner (unless the
1.1       millert    72: .Fl u
1.13    ! millert    73: flag is given) and the filename is printed to standard output.
1.8       millert    74: .Pp
1.10      aaron      75: .Nm mktemp
1.8       millert    76: is provided to allow shell scripts to safely use temporary files.
                     77: Traditionally, many shell scripts take the name of the program with
1.10      aaron      78: the PID as a suffix and use that as a temporary file name.  This
1.8       millert    79: kind of naming scheme is predictable and the race condition it creates
1.10      aaron      80: is easy for an attacker to win.  A safer, though still inferior approach
1.8       millert    81: is to make a temporary directory using the same naming scheme.  While
                     82: this does allow one to guarantee that a temporary file will not be
                     83: subverted, it still allows a simple denial of service attack.  For these
                     84: reasons it is suggested that
                     85: .Nm
                     86: be used instead.
1.1       millert    87: .Sh OPTIONS
                     88: .Bl -tag -width indent
                     89: The available options are as follows:
1.5       millert    90: .It Fl d
                     91: Make a directory instead of a file.
                     92: .It Fl q
                     93: Fail silently if an error occurs.  This is useful if
                     94: a script does not want error output to go to standard error.
1.1       millert    95: .It Fl u
                     96: Operate in
                     97: .Dq unsafe
                     98: mode.  The temp file will be unlinked before
                     99: .Nm
                    100: exits.  This is slightly better than
                    101: .Fn mktemp 3
                    102: but still introduces a race condition.  Use of this
                    103: option is not encouraged.
1.6       millert   104: .El
1.1       millert   105: .Sh RETURN VALUES
                    106: The
                    107: .Nm
                    108: utility
1.10      aaron     109: exits with a value of 0 on success or 1 on failure.
1.1       millert   110: .Sh EXAMPLES
                    111: The following
                    112: .Xr sh 1
                    113: fragment illustrates a simple use of
                    114: .Nm
                    115: where the script should quit if it cannot get a safe
                    116: temporary file.
                    117: .Bd -literal -offset indent
1.12      millert   118: TMPFILE=`mktemp /tmp/$0.XXXXXXXXXX` || exit 1
1.1       millert   119: echo "program output" >> $TMPFILE
                    120: .Ed
                    121: .Pp
1.12      millert   122: In this case, we want the script to catch the error ourselves.
1.1       millert   123: .Bd -literal -offset indent
1.12      millert   124: TMPFILE=`mktemp -q /tmp/$0.XXXXXXXXXX`
1.2       millert   125: if [ $? -ne 0 ]; then
1.1       millert   126:        echo "$0: Can't create temp file, exiting..."
                    127:        exit 1
                    128: fi
1.12      millert   129: .Ed
                    130: .Pp
                    131: Or perhaps you don't want to exit if
                    132: .Nm
                    133: is unable to create the file.  In this case you can protect the
                    134: part of the script thusly.
                    135: .Bd -literal -offset indent
                    136: TMPFILE=`mktemp /tmp/$0.XXXXXXXXXX` && {
                    137:        # Safe to use $TMPFILE in this block
                    138:        echo data > $TMPFILE
                    139:        ...
                    140:        rm -f $TMPFILE
                    141: }
1.1       millert   142: .Ed
                    143: .Sh SEE ALSO
1.8       millert   144: .Xr mkdtemp 3 ,
1.10      aaron     145: .Xr mkstemp 3 ,
                    146: .Xr mktemp 3
1.1       millert   147: .Sh HISTORY
                    148: The
                    149: .Nm
                    150: utility appeared in
1.7       millert   151: .Ox 2.1 .