Blame sysdeps/posix/tempname.c

Packit 6c4009
/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#if !_LIBC
Packit 6c4009
# include <config.h>
Packit 6c4009
# include "tempname.h"
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <assert.h>
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#ifndef __set_errno
Packit 6c4009
# define __set_errno(Val) errno = (Val)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#ifndef P_tmpdir
Packit 6c4009
# define P_tmpdir "/tmp"
Packit 6c4009
#endif
Packit 6c4009
#ifndef TMP_MAX
Packit 6c4009
# define TMP_MAX 238328
Packit 6c4009
#endif
Packit 6c4009
#ifndef __GT_FILE
Packit 6c4009
# define __GT_FILE	0
Packit 6c4009
# define __GT_DIR	1
Packit 6c4009
# define __GT_NOCREATE	2
Packit 6c4009
#endif
Packit 6c4009
#if !_LIBC && (GT_FILE != __GT_FILE || GT_DIR != __GT_DIR	\
Packit 6c4009
	       || GT_NOCREATE != __GT_NOCREATE)
Packit 6c4009
# error report this to bug-gnulib@gnu.org
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#include <stddef.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <sys/time.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
Packit 6c4009
#if _LIBC
Packit 6c4009
# define struct_stat64 struct stat64
Packit 6c4009
# define __secure_getenv __libc_secure_getenv
Packit 6c4009
#else
Packit 6c4009
# define struct_stat64 struct stat
Packit 6c4009
# define __gen_tempname gen_tempname
Packit 6c4009
# define __getpid getpid
Packit 6c4009
# define __gettimeofday gettimeofday
Packit 6c4009
# define __mkdir mkdir
Packit 6c4009
# define __open open
Packit 6c4009
# define __lxstat64(version, file, buf) lstat (file, buf)
Packit 6c4009
# define __secure_getenv secure_getenv
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#ifdef _LIBC
Packit 6c4009
# include <hp-timing.h>
Packit 6c4009
# if HP_TIMING_AVAIL
Packit 6c4009
#  define RANDOM_BITS(Var) \
Packit 6c4009
  if (__glibc_unlikely (value == UINT64_C (0)))				      \
Packit 6c4009
    {									      \
Packit 6c4009
      /* If this is the first time this function is used initialize	      \
Packit 6c4009
	 the variable we accumulate the value in to some somewhat	      \
Packit 6c4009
	 random value.  If we'd not do this programs at startup time	      \
Packit 6c4009
	 might have a reduced set of possible names, at least on slow	      \
Packit 6c4009
	 machines.  */							      \
Packit 6c4009
      struct timeval tv;						      \
Packit 6c4009
      __gettimeofday (&tv, NULL);					      \
Packit 6c4009
      value = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec;		      \
Packit 6c4009
    }									      \
Packit 6c4009
  HP_TIMING_NOW (Var)
Packit 6c4009
# endif
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* Use the widest available unsigned type if uint64_t is not
Packit 6c4009
   available.  The algorithm below extracts a number less than 62**6
Packit 6c4009
   (approximately 2**35.725) from uint64_t, so ancient hosts where
Packit 6c4009
   uintmax_t is only 32 bits lose about 3.725 bits of randomness,
Packit 6c4009
   which is better than not having mkstemp at all.  */
Packit 6c4009
#if !defined UINT64_MAX && !defined uint64_t
Packit 6c4009
# define uint64_t uintmax_t
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#if _LIBC
Packit 6c4009
/* Return nonzero if DIR is an existent directory.  */
Packit 6c4009
static int
Packit 6c4009
direxists (const char *dir)
Packit 6c4009
{
Packit 6c4009
  struct_stat64 buf;
Packit 6c4009
  return __xstat64 (_STAT_VER, dir, &buf) == 0 && S_ISDIR (buf.st_mode);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Path search algorithm, for tmpnam, tmpfile, etc.  If DIR is
Packit 6c4009
   non-null and exists, uses it; otherwise uses the first of $TMPDIR,
Packit 6c4009
   P_tmpdir, /tmp that exists.  Copies into TMPL a template suitable
Packit 6c4009
   for use with mk[s]temp.  Will fail (-1) if DIR is non-null and
Packit 6c4009
   doesn't exist, none of the searched dirs exists, or there's not
Packit 6c4009
   enough space in TMPL. */
Packit 6c4009
int
Packit 6c4009
__path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx,
Packit 6c4009
	       int try_tmpdir)
Packit 6c4009
{
Packit 6c4009
  const char *d;
Packit 6c4009
  size_t dlen, plen;
Packit 6c4009
Packit 6c4009
  if (!pfx || !pfx[0])
Packit 6c4009
    {
Packit 6c4009
      pfx = "file";
Packit 6c4009
      plen = 4;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      plen = strlen (pfx);
Packit 6c4009
      if (plen > 5)
Packit 6c4009
	plen = 5;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (try_tmpdir)
Packit 6c4009
    {
Packit 6c4009
      d = __secure_getenv ("TMPDIR");
Packit 6c4009
      if (d != NULL && direxists (d))
Packit 6c4009
	dir = d;
Packit 6c4009
      else if (dir != NULL && direxists (dir))
Packit 6c4009
	/* nothing */ ;
Packit 6c4009
      else
Packit 6c4009
	dir = NULL;
Packit 6c4009
    }
Packit 6c4009
  if (dir == NULL)
Packit 6c4009
    {
Packit 6c4009
      if (direxists (P_tmpdir))
Packit 6c4009
	dir = P_tmpdir;
Packit 6c4009
      else if (strcmp (P_tmpdir, "/tmp") != 0 && direxists ("/tmp"))
Packit 6c4009
	dir = "/tmp";
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  __set_errno (ENOENT);
Packit 6c4009
	  return -1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  dlen = strlen (dir);
Packit 6c4009
  while (dlen > 1 && dir[dlen - 1] == '/')
Packit 6c4009
    dlen--;			/* remove trailing slashes */
Packit 6c4009
Packit 6c4009
  /* check we have room for "${dir}/${pfx}XXXXXX\0" */
Packit 6c4009
  if (tmpl_len < dlen + 1 + plen + 6 + 1)
Packit 6c4009
    {
Packit 6c4009
      __set_errno (EINVAL);
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  sprintf (tmpl, "%.*s/%.*sXXXXXX", (int) dlen, dir, (int) plen, pfx);
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
#endif /* _LIBC */
Packit 6c4009
Packit 6c4009
/* These are the characters used in temporary file names.  */
Packit 6c4009
static const char letters[] =
Packit 6c4009
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Packit 6c4009
Packit 6c4009
/* Generate a temporary file name based on TMPL.  TMPL must match the
Packit 6c4009
   rules for mk[s]temp (i.e. end in "XXXXXX", possibly with a suffix).
Packit 6c4009
   The name constructed does not exist at the time of the call to
Packit 6c4009
   __gen_tempname.  TMPL is overwritten with the result.
Packit 6c4009
Packit 6c4009
   KIND may be one of:
Packit 6c4009
   __GT_NOCREATE:	simply verify that the name does not exist
Packit 6c4009
			at the time of the call.
Packit 6c4009
   __GT_FILE:		create the file using open(O_CREAT|O_EXCL)
Packit 6c4009
			and return a read-write fd.  The file is mode 0600.
Packit 6c4009
   __GT_DIR:		create a directory, which will be mode 0700.
Packit 6c4009
Packit 6c4009
   We use a clever algorithm to get hard-to-predict names. */
Packit 6c4009
int
Packit 6c4009
__gen_tempname (char *tmpl, int suffixlen, int flags, int kind)
Packit 6c4009
{
Packit 6c4009
  int len;
Packit 6c4009
  char *XXXXXX;
Packit 6c4009
  static uint64_t value;
Packit 6c4009
  uint64_t random_time_bits;
Packit 6c4009
  unsigned int count;
Packit 6c4009
  int fd = -1;
Packit 6c4009
  int save_errno = errno;
Packit 6c4009
  struct_stat64 st;
Packit 6c4009
Packit 6c4009
  /* A lower bound on the number of temporary files to attempt to
Packit 6c4009
     generate.  The maximum total number of temporary file names that
Packit 6c4009
     can exist for a given template is 62**6.  It should never be
Packit 6c4009
     necessary to try all of these combinations.  Instead if a reasonable
Packit 6c4009
     number of names is tried (we define reasonable as 62**3) fail to
Packit 6c4009
     give the system administrator the chance to remove the problems.  */
Packit 6c4009
#define ATTEMPTS_MIN (62 * 62 * 62)
Packit 6c4009
Packit 6c4009
  /* The number of times to attempt to generate a temporary file.  To
Packit 6c4009
     conform to POSIX, this must be no smaller than TMP_MAX.  */
Packit 6c4009
#if ATTEMPTS_MIN < TMP_MAX
Packit 6c4009
  unsigned int attempts = TMP_MAX;
Packit 6c4009
#else
Packit 6c4009
  unsigned int attempts = ATTEMPTS_MIN;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  len = strlen (tmpl);
Packit 6c4009
  if (len < 6 + suffixlen || memcmp (&tmpl[len - 6 - suffixlen], "XXXXXX", 6))
Packit 6c4009
    {
Packit 6c4009
      __set_errno (EINVAL);
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* This is where the Xs start.  */
Packit 6c4009
  XXXXXX = &tmpl[len - 6 - suffixlen];
Packit 6c4009
Packit 6c4009
  /* Get some more or less random data.  */
Packit 6c4009
#ifdef RANDOM_BITS
Packit 6c4009
  RANDOM_BITS (random_time_bits);
Packit 6c4009
#else
Packit 6c4009
  {
Packit 6c4009
    struct timeval tv;
Packit 6c4009
    __gettimeofday (&tv, NULL);
Packit 6c4009
    random_time_bits = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec;
Packit 6c4009
  }
Packit 6c4009
#endif
Packit 6c4009
  value += random_time_bits ^ __getpid ();
Packit 6c4009
Packit 6c4009
  for (count = 0; count < attempts; value += 7777, ++count)
Packit 6c4009
    {
Packit 6c4009
      uint64_t v = value;
Packit 6c4009
Packit 6c4009
      /* Fill in the random bits.  */
Packit 6c4009
      XXXXXX[0] = letters[v % 62];
Packit 6c4009
      v /= 62;
Packit 6c4009
      XXXXXX[1] = letters[v % 62];
Packit 6c4009
      v /= 62;
Packit 6c4009
      XXXXXX[2] = letters[v % 62];
Packit 6c4009
      v /= 62;
Packit 6c4009
      XXXXXX[3] = letters[v % 62];
Packit 6c4009
      v /= 62;
Packit 6c4009
      XXXXXX[4] = letters[v % 62];
Packit 6c4009
      v /= 62;
Packit 6c4009
      XXXXXX[5] = letters[v % 62];
Packit 6c4009
Packit 6c4009
      switch (kind)
Packit 6c4009
	{
Packit 6c4009
	case __GT_FILE:
Packit 6c4009
	  fd = __open (tmpl,
Packit 6c4009
		       (flags & ~O_ACCMODE)
Packit 6c4009
		       | O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	case __GT_DIR:
Packit 6c4009
	  fd = __mkdir (tmpl, S_IRUSR | S_IWUSR | S_IXUSR);
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	case __GT_NOCREATE:
Packit 6c4009
	  /* This case is backward from the other three.  __gen_tempname
Packit 6c4009
	     succeeds if __xstat fails because the name does not exist.
Packit 6c4009
	     Note the continue to bypass the common logic at the bottom
Packit 6c4009
	     of the loop.  */
Packit 6c4009
	  if (__lxstat64 (_STAT_VER, tmpl, &st) < 0)
Packit 6c4009
	    {
Packit 6c4009
	      if (errno == ENOENT)
Packit 6c4009
		{
Packit 6c4009
		  __set_errno (save_errno);
Packit 6c4009
		  return 0;
Packit 6c4009
		}
Packit 6c4009
	      else
Packit 6c4009
		/* Give up now. */
Packit 6c4009
		return -1;
Packit 6c4009
	    }
Packit 6c4009
	  continue;
Packit 6c4009
Packit 6c4009
	default:
Packit 6c4009
	  assert (! "invalid KIND in __gen_tempname");
Packit 6c4009
	  abort ();
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (fd >= 0)
Packit 6c4009
	{
Packit 6c4009
	  __set_errno (save_errno);
Packit 6c4009
	  return fd;
Packit 6c4009
	}
Packit 6c4009
      else if (errno != EEXIST)
Packit 6c4009
	return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* We got out of the loop because we ran out of combinations to try.  */
Packit 6c4009
  __set_errno (EEXIST);
Packit 6c4009
  return -1;
Packit 6c4009
}