Apache HTTPD
mktemp.c
Go to the documentation of this file.
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
49#include "apr_private.h"
50#include "apr_file_io.h" /* prototype of apr_mkstemp() */
51#include "apr_strings.h" /* prototype of apr_mkstemp() */
52#include "apr_arch_file_io.h" /* prototype of apr_mkstemp() */
53#include "apr_portable.h" /* for apr_os_file_put() */
54#include "apr_arch_inherit.h"
55
56#ifndef HAVE_MKSTEMP
57
58#if defined(SVR4) || defined(WIN32) || defined(NETWARE)
59#ifdef SVR4
60#if HAVE_INTTYPES_H
61#include <inttypes.h>
62#endif
63#endif
64#define arc4random() rand()
65#define seedrandom(a) srand(a)
66#else
67#if APR_HAVE_STDINT_H
68#include <stdint.h>
69#endif
70#define arc4random() random()
71#define seedrandom(a) srandom(a)
72#endif
73
74#if APR_HAVE_SYS_TYPES_H
75#include <sys/types.h>
76#endif
77#ifdef HAVE_SYS_STAT_H
78#include <sys/stat.h>
79#endif
80#if APR_HAVE_FCNTL_H
81#include <fcntl.h>
82#endif
83#include <stdio.h>
84#include <stdlib.h>
85#include <string.h>
86#include <ctype.h>
87#ifdef HAVE_TIME_H
88#include <time.h>
89#endif
90
91static const unsigned char padchar[] =
92"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
94
96{
97 register char *start, *trv, *suffp;
98 char *pad;
100 apr_status_t rv;
102
103 if (randseed==0) {
106 }
107
108 for (trv = path; *trv; ++trv)
109 ;
110 suffp = trv;
111 --trv;
112 if (trv < path) {
113 return APR_EINVAL;
114 }
115
116 /* Fill space with random characters */
117 while (*trv == 'X') {
118 randnum = arc4random() % (sizeof(padchar) - 1);
119 *trv-- = padchar[randnum];
120 }
121 start = trv + 1;
122
123 /*
124 * check the target directory.
125 */
126 for (;; --trv) {
127 if (trv <= path)
128 break;
129 if (*trv == '/') {
130 *trv = '\0';
132 *trv = '/';
133 if (rv != APR_SUCCESS)
134 return rv;
135 if (sbuf.filetype != APR_DIR) {
136 return APR_ENOTDIR;
137 }
138 break;
139 }
140 }
141
142 for (;;) {
143 if ((rv = apr_file_open(doopen, path, flags,
145 return APR_SUCCESS;
146 if (!APR_STATUS_IS_EEXIST(rv))
147 return rv;
148
149 /* If we have a collision, cycle through the space of filenames */
150 for (trv = start;;) {
151 if (*trv == '\0' || trv == suffp)
152 return APR_EINVAL; /* XXX: is this the correct return code? */
153 pad = strchr((char *)padchar, *trv);
154 if (pad == NULL || !*++pad) {
155 *trv++ = padchar[0];
156 }
157 else {
158 *trv++ = *pad;
159 break;
160 }
161 }
162 }
163 /*NOTREACHED*/
164}
165
166#else
167
168#if APR_HAVE_STDLIB_H
169#include <stdlib.h> /* for mkstemp() - Single Unix */
170#endif
171#if APR_HAVE_UNISTD_H
172#include <unistd.h> /* for mkstemp() - FreeBSD */
173#endif
174#endif /* !defined(HAVE_MKSTEMP) */
175
177{
178#ifdef HAVE_MKSTEMP
179 int fd;
180#endif
183#ifndef HAVE_MKSTEMP
184 return gettemp(template, fp, flags, p);
185#else
186
187#ifdef HAVE_MKSTEMP64
188 fd = mkstemp64(template);
189#else
190 fd = mkstemp(template);
191#endif
192
193 if (fd == -1) {
194 return errno;
195 }
196 /* XXX: We must reset several flags values as passed-in, since
197 * mkstemp didn't subscribe to our preference flags.
198 *
199 * We either have to unset the flags, or fix up the fd and other
200 * xthread and inherit bits appropriately. Since gettemp() above
201 * calls apr_file_open, our flags are respected in that code path.
202 */
203 apr_os_file_put(fp, &fd, flags, p);
204 (*fp)->fname = apr_pstrdup(p, template);
205
206 if (!(flags & APR_FOPEN_NOCLEANUP)) {
207 int flags;
208
209 if ((flags = fcntl(fd, F_GETFD)) == -1)
210 return errno;
211
212 flags |= FD_CLOEXEC;
213 if (fcntl(fd, F_SETFD, flags) == -1)
214 return errno;
215
216 apr_pool_cleanup_register((*fp)->pool, (void *)(*fp),
219
220 /* Clear APR_FOPEN_NOCLEANUP set by apr_os_file_put() */
221 (*fp)->flags &= ~APR_FOPEN_NOCLEANUP;
222 }
223#endif
224 return APR_SUCCESS;
225}
226
APR File I/O Handling.
APR Portability Routines.
APR Strings library.
request_rec int int apr_table_t const char * path
#define APR_ENOTDIR
Definition apr_errno.h:669
#define APR_EINVAL
Definition apr_errno.h:711
#define APR_STATUS_IS_EEXIST(s)
Definition apr_errno.h:1233
apr_file_t apr_off_t start
apr_file_t * fd
const char apr_ssize_t int flags
Definition apr_encode.h:168
const void apr_status_t(*) apr_status_t(* APR_DECLARE)(void) apr_pool_pre_cleanup_register(apr_pool_t *p
Definition apr_pools.h:646
apr_size_t size
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
@ APR_DIR
#define APR_FOPEN_NOCLEANUP
Definition apr_file_io.h:74
#define APR_FOPEN_DELONCLOSE
Definition apr_file_io.h:66
#define APR_FOPEN_EXCL
Definition apr_file_io.h:63
#define APR_FOPEN_WRITE
Definition apr_file_io.h:55
#define APR_FOPEN_READ
Definition apr_file_io.h:54
#define APR_FOPEN_CREATE
Definition apr_file_io.h:56
#define APR_UWRITE
#define APR_UREAD
#define APR_FINFO_TYPE
apr_pool_t * p
Definition md_event.c:32
return NULL
Definition mod_so.c:359
#define arc4random()
Definition mktemp.c:70
static const unsigned char padchar[]
Definition mktemp.c:91
#define seedrandom(a)
Definition mktemp.c:71
static int gettemp(char *path, apr_file_t **doopen, apr_int32_t flags, apr_pool_t *p)
Definition mktemp.c:95
static apr_uint32_t randseed
Definition mktemp.c:93
apr_status_t apr_unix_child_file_cleanup(void *thefile)
Definition open.c:85
apr_status_t apr_unix_file_cleanup(void *thefile)
Definition open.c:71
typedef int(WSAAPI *apr_winapi_fpt_WSAPoll)(IN OUT LPWSAPOLLFD fdArray