blob: 0b2e933732a3024e2c5dab748744aca01dc35c61 [file] [log] [blame]
maxwen27116ba2015-08-14 21:41:28 +02001/*
2 * Copyright (c) 1987, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if defined(LIBC_SCCS) && !defined(lint)
31static char sccsid[] = "@(#)mktemp.c 8.1 (Berkeley) 6/4/93";
32#endif /* LIBC_SCCS and not lint */
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD$");
35
36#include <sys/param.h>
37#include <sys/stat.h>
38#include <fcntl.h>
39#include <errno.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <ctype.h>
44#include <unistd.h>
45
46extern u_int32_t arc4random_uniform(u_int32_t);
47#define _open open
48
49static int _gettemp(char *, int *, int, int);
50
51static const char padchar[] =
52"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
53
54static char *
55_mktemp(char *path)
56{
57 return (_gettemp(path, (int *)NULL, 0, 0) ? path : (char *)NULL);
58}
59
60char *
61bb_mktemp(char *path)
62{
63 return (_mktemp(path));
64}
65
66static int
67_gettemp(char *path, int *doopen, int domkdir, int slen)
68{
69 char *start, *trv, *suffp, *carryp;
70 char *pad;
71 struct stat sbuf;
72 int rval;
73 uint32_t rand;
74 char carrybuf[MAXPATHLEN];
75
76 if ((doopen != NULL && domkdir) || slen < 0) {
77 errno = EINVAL;
78 return (0);
79 }
80
81 for (trv = path; *trv != '\0'; ++trv)
82 ;
83 if (trv - path >= MAXPATHLEN) {
84 errno = ENAMETOOLONG;
85 return (0);
86 }
87 trv -= slen;
88 suffp = trv;
89 --trv;
90 if (trv < path || NULL != strchr(suffp, '/')) {
91 errno = EINVAL;
92 return (0);
93 }
94
95 /* Fill space with random characters */
96 while (trv >= path && *trv == 'X') {
97 rand = arc4random_uniform(sizeof(padchar) - 1);
98 *trv-- = padchar[rand];
99 }
100 start = trv + 1;
101
102 /* save first combination of random characters */
103 memcpy(carrybuf, start, suffp - start);
104
105 /*
106 * check the target directory.
107 */
108 if (doopen != NULL || domkdir) {
109 for (; trv > path; --trv) {
110 if (*trv == '/') {
111 *trv = '\0';
112 rval = stat(path, &sbuf);
113 *trv = '/';
114 if (rval != 0)
115 return (0);
116 if (!S_ISDIR(sbuf.st_mode)) {
117 errno = ENOTDIR;
118 return (0);
119 }
120 break;
121 }
122 }
123 }
124
125 for (;;) {
126 if (doopen) {
127 if ((*doopen =
128 _open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0)
129 return (1);
130 if (errno != EEXIST)
131 return (0);
132 } else if (domkdir) {
133 if (mkdir(path, 0700) == 0)
134 return (1);
135 if (errno != EEXIST)
136 return (0);
137 } else if (lstat(path, &sbuf))
138 return (errno == ENOENT);
139
140 /* If we have a collision, cycle through the space of filenames */
141 for (trv = start, carryp = carrybuf;;) {
142 /* have we tried all possible permutations? */
143 if (trv == suffp)
144 return (0); /* yes - exit with EEXIST */
145 pad = strchr(padchar, *trv);
146 if (pad == NULL) {
147 /* this should never happen */
148 errno = EIO;
149 return (0);
150 }
151 /* increment character */
152 *trv = (*++pad == '\0') ? padchar[0] : *pad;
153 /* carry to next position? */
154 if (*trv == *carryp) {
155 /* increment position and loop */
156 ++trv;
157 ++carryp;
158 } else {
159 /* try with new name */
160 break;
161 }
162 }
163 }
164 /*NOTREACHED*/
165}