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