MKTEMP(3)

NAME
mktemp - make temporary file name (unique)

LIBRARY
libc

SYNOPSIS
#include <unistd.h>
char *
mktemp(char *template);
int
mkstemp(char *template);
int
mkstemps(char *template, int suffixlen);
char *
mkdtemp(char *template);

DESCRIPTION
The mktemp() function takes the given file name template and overwrites a portion of it to create a file name. This file name is guaranteed not to exist at the time of function invocation and is suitable for use by the application. The template may be any file name with some number of ‘Xs’ appended to it, for example /tmp/temp.XXXXXX. The trailing ‘Xs’ are replaced with a unique alphanumeric combination. The number of unique file names mktemp() can return depends on the number of ‘Xs’ provided; six ‘Xs’ will result in mktemp() selecting one of 56800235584 (62 ** 6) possible temporary file names.

The mkstemp() function makes the same replacement to the template and creates the template file, mode 0600, returning a file descriptor opened for reading and writing. This avoids the race between testing for a file's existence and opening it for use.

The mkstemps() function acts the same as mkstemp(), except it permits a suffix to exist in the template. The template should be of the form /tmp/tmpXXXXXXsuffix. The mkstemps() function is told the length of the suffix string.

The mkdtemp() function makes the same replacement to the template as in mktemp() and creates the template directory, mode 0700.

RETURN VALUES
The mktemp() and mkdtemp() functions return a pointer to the template on success and NULL on failure. The mkstemp() and mkstemps() functions return -1 if no suitable file could be created. If either call fails an error code is placed in the global variable errno.

ERRORS
The mkstemp(), mkstemps() and mkdtemp() functions may set errno to one of the following values:

[ENOTDIR]
    The pathname portion of the template is not an existing directory.

The mkstemp(), mkstemps() and mkdtemp() functions may also set errno to any value specified by the stat(2) function.

The mkstemp() and mkstemps() functions may also set errno to any value specified by the open(2) function.

The mkdtemp() function may also set errno to any value specified by the mkdir(2) function.

NOTES
A common problem that results in a core dump is that the programmer passes in a read-only string to mktemp(), mkstemp(), mkstemps() or mkdtemp(). This is common with programs that were developed before isoC compilers were common. For example, calling mkstemp() with an argument of "/tmp/tempfile.XXXXXX" will result in a core dump due to mkstemp() attempting to modify the string constant that was given.

SEE ALSO
chmod(2), getpid(2), mkdir(2), open(2), stat(2)

HISTORY
A mktemp() function appeared in AT&T UNIX. The mkstemp() function appeared in 4.4BSD. The mkdtemp() function first appeared in OpenBSD 2.2, and later in FreeBSD 3.2. The mkstemps() function first appeared in OpenBSD 2.4, and later in FreeBSD 3.4.

BUGS
This family of functions produces filenames which can be guessed, though the risk is minimized when large numbers of ‘Xs’ are used to increase the number of possible temporary filenames. This makes the race in mktemp(), between testing for a file's existence (in the mktemp() function call) and opening it for use (later in the user application) particularly dangerous from a security perspective. Whenever it is possible, mkstemp() should be used instead, since it does not have the race condition. If mkstemp() cannot be used, the filename created by mktemp() should be created using the O_EXCL flag to open(2) and the return status of the call should be tested for failure. This will ensure that the program does not continue blindly in the event that an attacker has already created the file with the intention of manipulating or reading its contents.