mktemp.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * Copyright (c) 1987, 1993
  18. * The Regents of the University of California. All rights reserved.
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. * 1. Redistributions of source code must retain the above copyright
  24. * notice, this list of conditions and the following disclaimer.
  25. * 2. Redistributions in binary form must reproduce the above copyright
  26. * notice, this list of conditions and the following disclaimer in the
  27. * documentation and/or other materials provided with the distribution.
  28. * 3. All advertising materials mentioning features or use of this software
  29. * must display the following acknowledgement:
  30. * This product includes software developed by the University of
  31. * California, Berkeley and its contributors.
  32. * 4. Neither the name of the University nor the names of its contributors
  33. * may be used to endorse or promote products derived from this software
  34. * without specific prior written permission.
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  37. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  39. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  40. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  41. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  42. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  44. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  45. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. */
  48. #include "apr_private.h"
  49. #include "apr_file_io.h" /* prototype of apr_mkstemp() */
  50. #include "apr_strings.h" /* prototype of apr_mkstemp() */
  51. #include "apr_arch_file_io.h" /* prototype of apr_mkstemp() */
  52. #include "apr_portable.h" /* for apr_os_file_put() */
  53. #include "apr_arch_inherit.h"
  54. #ifndef HAVE_MKSTEMP
  55. #if defined(SVR4) || defined(WIN32) || defined(NETWARE)
  56. #ifdef SVR4
  57. #if HAVE_INTTYPES_H
  58. #include <inttypes.h>
  59. #endif
  60. #endif
  61. #define arc4random() rand()
  62. #define seedrandom(a) srand(a)
  63. #else
  64. #if APR_HAVE_STDINT_H
  65. #include <stdint.h>
  66. #endif
  67. #define arc4random() random()
  68. #define seedrandom(a) srandom(a)
  69. #endif
  70. #if APR_HAVE_SYS_TYPES_H
  71. #include <sys/types.h>
  72. #endif
  73. #if APR_HAVE_SYS_STAT_H
  74. #include <sys/stat.h>
  75. #endif
  76. #if APR_HAVE_FCNTL_H
  77. #include <fcntl.h>
  78. #endif
  79. #include <stdio.h>
  80. #include <stdlib.h>
  81. #include <string.h>
  82. #include <ctype.h>
  83. #ifdef HAVE_TIME_H
  84. #include <time.h>
  85. #endif
  86. static const unsigned char padchar[] =
  87. "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  88. static apr_uint32_t randseed=0;
  89. static int gettemp(char *path, apr_file_t **doopen, apr_int32_t flags, apr_pool_t *p)
  90. {
  91. register char *start, *trv, *suffp;
  92. char *pad;
  93. apr_finfo_t sbuf;
  94. apr_status_t rv;
  95. apr_uint32_t randnum;
  96. if (randseed==0) {
  97. randseed = (int)apr_time_now();
  98. seedrandom(randseed);
  99. }
  100. for (trv = path; *trv; ++trv)
  101. ;
  102. suffp = trv;
  103. --trv;
  104. if (trv < path) {
  105. return APR_EINVAL;
  106. }
  107. /* Fill space with random characters */
  108. while (*trv == 'X') {
  109. randnum = arc4random() % (sizeof(padchar) - 1);
  110. *trv-- = padchar[randnum];
  111. }
  112. start = trv + 1;
  113. /*
  114. * check the target directory.
  115. */
  116. for (;; --trv) {
  117. if (trv <= path)
  118. break;
  119. if (*trv == '/') {
  120. *trv = '\0';
  121. rv = apr_stat(&sbuf, path, APR_FINFO_TYPE, p);
  122. *trv = '/';
  123. if (rv != APR_SUCCESS)
  124. return rv;
  125. if (sbuf.filetype != APR_DIR) {
  126. return APR_ENOTDIR;
  127. }
  128. break;
  129. }
  130. }
  131. for (;;) {
  132. if ((rv = apr_file_open(doopen, path, flags,
  133. APR_UREAD | APR_UWRITE, p)) == APR_SUCCESS)
  134. return APR_SUCCESS;
  135. if (!APR_STATUS_IS_EEXIST(rv))
  136. return rv;
  137. /* If we have a collision, cycle through the space of filenames */
  138. for (trv = start;;) {
  139. if (*trv == '\0' || trv == suffp)
  140. return APR_EINVAL; /* XXX: is this the correct return code? */
  141. pad = strchr((char *)padchar, *trv);
  142. if (pad == NULL || !*++pad) {
  143. *trv++ = padchar[0];
  144. }
  145. else {
  146. *trv++ = *pad;
  147. break;
  148. }
  149. }
  150. }
  151. /*NOTREACHED*/
  152. }
  153. #else
  154. #if APR_HAVE_STDLIB_H
  155. #include <stdlib.h> /* for mkstemp() - Single Unix */
  156. #endif
  157. #if APR_HAVE_UNISTD_H
  158. #include <unistd.h> /* for mkstemp() - FreeBSD */
  159. #endif
  160. #endif /* !defined(HAVE_MKSTEMP) */
  161. APR_DECLARE(apr_status_t) apr_file_mktemp(apr_file_t **fp, char *template, apr_int32_t flags, apr_pool_t *p)
  162. {
  163. #ifdef HAVE_MKSTEMP
  164. int fd;
  165. #endif
  166. flags = (!flags) ? APR_FOPEN_CREATE | APR_FOPEN_READ | APR_FOPEN_WRITE | APR_FOPEN_EXCL |
  167. APR_FOPEN_DELONCLOSE : flags;
  168. #ifndef HAVE_MKSTEMP
  169. return gettemp(template, fp, flags, p);
  170. #else
  171. #ifdef HAVE_MKSTEMP64
  172. fd = mkstemp64(template);
  173. #else
  174. fd = mkstemp(template);
  175. #endif
  176. if (fd == -1) {
  177. return errno;
  178. }
  179. /* XXX: We must reset several flags values as passed-in, since
  180. * mkstemp didn't subscribe to our preference flags.
  181. *
  182. * We either have to unset the flags, or fix up the fd and other
  183. * xthread and inherit bits appropriately. Since gettemp() above
  184. * calls apr_file_open, our flags are respected in that code path.
  185. */
  186. apr_os_file_put(fp, &fd, flags, p);
  187. (*fp)->fname = apr_pstrdup(p, template);
  188. if (!(flags & APR_FOPEN_NOCLEANUP)) {
  189. int flags;
  190. if ((flags = fcntl(fd, F_GETFD)) == -1)
  191. return errno;
  192. flags |= FD_CLOEXEC;
  193. if (fcntl(fd, F_SETFD, flags) == -1)
  194. return errno;
  195. apr_pool_cleanup_register((*fp)->pool, (void *)(*fp),
  196. apr_unix_file_cleanup,
  197. apr_unix_child_file_cleanup);
  198. }
  199. #endif
  200. return APR_SUCCESS;
  201. }