1/* Copyright (C) 1999, 2001-2003, 2006 Free Software Foundation, Inc.
2   This file is part of the GNU C Library.
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 2, or (at your option)
7   any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License along
15   with this program; if not, write to the Free Software Foundation,
16   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18/* Extracted from misc/mkdtemp.c and sysdeps/posix/tempname.c.  */
19
20#include <config.h>
21
22/* Specification.  */
23#include "mkdtemp.h"
24
25#include <errno.h>
26#ifndef __set_errno
27# define __set_errno(Val) errno = (Val)
28#endif
29
30#include <stddef.h>
31#include <stdint.h>
32#include <stdlib.h>
33#include <string.h>
34
35#include <stdio.h>
36#ifndef TMP_MAX
37# define TMP_MAX 238328
38#endif
39
40#include <unistd.h>
41
42#if HAVE_GETTIMEOFDAY || _LIBC
43# if HAVE_SYS_TIME_H || _LIBC
44#  include <sys/time.h>
45# endif
46#else
47# if HAVE_TIME_H || _LIBC
48#  include <time.h>
49# endif
50#endif
51
52#include <sys/stat.h>
53#if !defined S_ISDIR && defined S_IFDIR
54# define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
55#endif
56#if !S_IRUSR && S_IREAD
57# define S_IRUSR S_IREAD
58#endif
59#if !S_IRUSR
60# define S_IRUSR 00400
61#endif
62#if !S_IWUSR && S_IWRITE
63# define S_IWUSR S_IWRITE
64#endif
65#if !S_IWUSR
66# define S_IWUSR 00200
67#endif
68#if !S_IXUSR && S_IEXEC
69# define S_IXUSR S_IEXEC
70#endif
71#if !S_IXUSR
72# define S_IXUSR 00100
73#endif
74
75#ifdef __MINGW32__
76# include <io.h>
77/* mingw's _mkdir() function has 1 argument, but we pass 2 arguments.
78   Therefore we have to disable the argument count checking.  */
79# define mkdir ((int (*)()) _mkdir)
80#endif
81
82#if !_LIBC
83# define __getpid getpid
84# define __gettimeofday gettimeofday
85# define __mkdir mkdir
86#endif
87
88/* Use the widest available unsigned type if uint64_t is not
89   available.  The algorithm below extracts a number less than 62**6
90   (approximately 2**35.725) from uint64_t, so ancient hosts where
91   uintmax_t is only 32 bits lose about 3.725 bits of randomness,
92   which is better than not having mkstemp at all.  */
93#if !defined UINT64_MAX && !defined uint64_t
94# define uint64_t uintmax_t
95#endif
96
97/* These are the characters used in temporary filenames.  */
98static const char letters[] =
99"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
100
101/* Generate a temporary file name based on TMPL.  TMPL must match the
102   rules for mk[s]temp (i.e. end in "XXXXXX").  The name constructed
103   does not exist at the time of the call to __gen_tempname.  TMPL is
104   overwritten with the result.
105
106   KIND is:
107   __GT_DIR:		create a directory, which will be mode 0700.
108
109   We use a clever algorithm to get hard-to-predict names. */
110static int
111gen_tempname (char *tmpl)
112{
113  int len;
114  char *XXXXXX;
115  static uint64_t value;
116  uint64_t random_time_bits;
117  unsigned int count;
118  int fd = -1;
119  int save_errno = errno;
120
121  /* A lower bound on the number of temporary files to attempt to
122     generate.  The maximum total number of temporary file names that
123     can exist for a given template is 62**6.  It should never be
124     necessary to try all these combinations.  Instead if a reasonable
125     number of names is tried (we define reasonable as 62**3) fail to
126     give the system administrator the chance to remove the problems.  */
127#define ATTEMPTS_MIN (62 * 62 * 62)
128
129  /* The number of times to attempt to generate a temporary file.  To
130     conform to POSIX, this must be no smaller than TMP_MAX.  */
131#if ATTEMPTS_MIN < TMP_MAX
132  unsigned int attempts = TMP_MAX;
133#else
134  unsigned int attempts = ATTEMPTS_MIN;
135#endif
136
137  len = strlen (tmpl);
138  if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
139    {
140      __set_errno (EINVAL);
141      return -1;
142    }
143
144  /* This is where the Xs start.  */
145  XXXXXX = &tmpl[len - 6];
146
147  /* Get some more or less random data.  */
148#ifdef RANDOM_BITS
149  RANDOM_BITS (random_time_bits);
150#else
151# if HAVE_GETTIMEOFDAY || _LIBC
152  {
153    struct timeval tv;
154    __gettimeofday (&tv, NULL);
155    random_time_bits = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec;
156  }
157# else
158  random_time_bits = time (NULL);
159# endif
160#endif
161  value += random_time_bits ^ __getpid ();
162
163  for (count = 0; count < attempts; value += 7777, ++count)
164    {
165      uint64_t v = value;
166
167      /* Fill in the random bits.  */
168      XXXXXX[0] = letters[v % 62];
169      v /= 62;
170      XXXXXX[1] = letters[v % 62];
171      v /= 62;
172      XXXXXX[2] = letters[v % 62];
173      v /= 62;
174      XXXXXX[3] = letters[v % 62];
175      v /= 62;
176      XXXXXX[4] = letters[v % 62];
177      v /= 62;
178      XXXXXX[5] = letters[v % 62];
179
180      fd = __mkdir (tmpl, S_IRUSR | S_IWUSR | S_IXUSR);
181
182      if (fd >= 0)
183	{
184	  __set_errno (save_errno);
185	  return fd;
186	}
187      else if (errno != EEXIST)
188	return -1;
189    }
190
191  /* We got out of the loop because we ran out of combinations to try.  */
192  __set_errno (EEXIST);
193  return -1;
194}
195
196/* Generate a unique temporary directory from TEMPLATE.
197   The last six characters of TEMPLATE must be "XXXXXX";
198   they are replaced with a string that makes the filename unique.
199   The directory is created, mode 700, and its name is returned.
200   (This function comes from OpenBSD.) */
201char *
202mkdtemp (char *template)
203{
204  if (gen_tempname (template))
205    return NULL;
206  else
207    return template;
208}
209