~ubuntu-branches/ubuntu/jaunty/gnupg2/jaunty-security

« back to all changes in this revision

Viewing changes to gl/mkdtemp.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 1999, 2001-2003 Free Software Foundation, Inc.
 
1
/* Copyright (C) 1999, 2001-2003, 2006 Free Software Foundation, Inc.
2
2
   This file is part of the GNU C Library.
3
3
 
4
4
   This program is free software; you can redistribute it and/or modify
5
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)
 
6
   the Free Software Foundation; either version 3, or (at your option)
7
7
   any later version.
8
8
 
9
9
   This program is distributed in the hope that it will be useful,
12
12
   GNU General Public License for more details.
13
13
 
14
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.  */
 
15
   with this program; if not, see <http://www.gnu.org/licenses/>.  */
17
16
 
18
17
/* Extracted from misc/mkdtemp.c and sysdeps/posix/tempname.c.  */
19
18
 
20
 
#ifdef HAVE_CONFIG_H
21
 
# include "config.h"
22
 
#endif
 
19
#include <config.h>
23
20
 
24
21
/* Specification.  */
25
22
#include "mkdtemp.h"
30
27
#endif
31
28
 
32
29
#include <stddef.h>
 
30
#include <stdint.h>
33
31
#include <stdlib.h>
34
32
#include <string.h>
35
33
 
38
36
# define TMP_MAX 238328
39
37
#endif
40
38
 
41
 
#if HAVE_STDINT_H || _LIBC
42
 
# include <stdint.h>
43
 
#endif
44
 
#if HAVE_INTTYPES_H
45
 
# include <inttypes.h>
46
 
#endif
47
 
 
48
 
#if HAVE_UNISTD_H || _LIBC
49
 
# include <unistd.h>
50
 
#endif
 
39
#include <unistd.h>
51
40
 
52
41
#if HAVE_GETTIMEOFDAY || _LIBC
53
42
# if HAVE_SYS_TIME_H || _LIBC
60
49
#endif
61
50
 
62
51
#include <sys/stat.h>
63
 
#if STAT_MACROS_BROKEN
64
 
# undef S_ISDIR
65
 
#endif
66
52
#if !defined S_ISDIR && defined S_IFDIR
67
53
# define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
68
54
#endif
86
72
#endif
87
73
 
88
74
#ifdef __MINGW32__
89
 
/* mingw's mkdir() function has 1 argument, but we pass 2 arguments.
90
 
   Therefore we have to disable the argument count checking.  */
91
 
# define mkdir ((int (*)()) mkdir)
 
75
# include <io.h>
92
76
#endif
93
77
 
94
78
#if !_LIBC
126
110
  char *XXXXXX;
127
111
  static uint64_t value;
128
112
  uint64_t random_time_bits;
129
 
  int count, fd = -1;
 
113
  unsigned int count;
 
114
  int fd = -1;
130
115
  int save_errno = errno;
131
116
 
 
117
  /* A lower bound on the number of temporary files to attempt to
 
118
     generate.  The maximum total number of temporary file names that
 
119
     can exist for a given template is 62**6.  It should never be
 
120
     necessary to try all these combinations.  Instead if a reasonable
 
121
     number of names is tried (we define reasonable as 62**3) fail to
 
122
     give the system administrator the chance to remove the problems.  */
 
123
#define ATTEMPTS_MIN (62 * 62 * 62)
 
124
 
 
125
  /* The number of times to attempt to generate a temporary file.  To
 
126
     conform to POSIX, this must be no smaller than TMP_MAX.  */
 
127
#if ATTEMPTS_MIN < TMP_MAX
 
128
  unsigned int attempts = TMP_MAX;
 
129
#else
 
130
  unsigned int attempts = ATTEMPTS_MIN;
 
131
#endif
 
132
 
132
133
  len = strlen (tmpl);
133
134
  if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
134
135
    {
155
156
#endif
156
157
  value += random_time_bits ^ __getpid ();
157
158
 
158
 
  for (count = 0; count < TMP_MAX; value += 7777, ++count)
 
159
  for (count = 0; count < attempts; value += 7777, ++count)
159
160
    {
160
161
      uint64_t v = value;
161
162
 
172
173
      v /= 62;
173
174
      XXXXXX[5] = letters[v % 62];
174
175
 
 
176
#ifdef MKDIR_TAKES_ONE_ARG
 
177
      fd = mkdir (tmpl);
 
178
#else
175
179
      fd = __mkdir (tmpl, S_IRUSR | S_IWUSR | S_IXUSR);
 
180
#endif
176
181
 
177
182
      if (fd >= 0)
178
183
        {