xref: /freebsd/lib/libc/stdio/mktemp.c (revision 1b6c76a2fe091c74f08427e6c870851025a9cf67)
1 /*
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. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #if defined(LIBC_SCCS) && !defined(lint)
35 #if 0
36 static char sccsid[] = "@(#)mktemp.c	8.1 (Berkeley) 6/4/93";
37 #endif
38 static const char rcsid[] =
39   "$FreeBSD$";
40 #endif /* LIBC_SCCS and not lint */
41 
42 /* #include "namespace.h" */
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <fcntl.h>
46 #include <errno.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <ctype.h>
51 #include <unistd.h>
52 /* #include "un-namespace.h" */
53 
54 char *_mktemp __P((char *));
55 
56 static int _gettemp __P((char *, int *, int, int));
57 
58 static const unsigned char padchar[] =
59 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
60 
61 int
62 mkstemps(path, slen)
63 	char *path;
64 	int slen;
65 {
66 	int fd;
67 
68 	return (_gettemp(path, &fd, 0, slen) ? fd : -1);
69 }
70 
71 int
72 mkstemp(path)
73 	char *path;
74 {
75 	int fd;
76 
77 	return (_gettemp(path, &fd, 0, 0) ? fd : -1);
78 }
79 
80 char *
81 mkdtemp(path)
82 	char *path;
83 {
84 	return(_gettemp(path, (int *)NULL, 1, 0) ? path : (char *)NULL);
85 }
86 
87 char *
88 _mktemp(path)
89 	char *path;
90 {
91 	return(_gettemp(path, (int *)NULL, 0, 0) ? path : (char *)NULL);
92 }
93 
94 __warn_references(mktemp,
95     "warning: mktemp() possibly used unsafely; consider using mkstemp()");
96 
97 char *
98 mktemp(path)
99 	char *path;
100 {
101 	return(_mktemp(path));
102 }
103 
104 static int
105 _gettemp(path, doopen, domkdir, slen)
106 	char *path;
107 	register int *doopen;
108 	int domkdir;
109 	int slen;
110 {
111 	register char *start, *trv, *suffp;
112 	char *pad;
113 	struct stat sbuf;
114 	int rval;
115 	uint32_t rand;
116 
117 	if (doopen && domkdir) {
118 		errno = EINVAL;
119 		return(0);
120 	}
121 
122 	for (trv = path; *trv; ++trv)
123 		;
124 	trv -= slen;
125 	suffp = trv;
126 	--trv;
127 	if (trv < path) {
128 		errno = EINVAL;
129 		return (0);
130 	}
131 
132 	/* Fill space with random characters */
133 	while (*trv == 'X') {
134 		rand = arc4random() % (sizeof(padchar) - 1);
135 		*trv-- = padchar[rand];
136 	}
137 	start = trv + 1;
138 
139 	/*
140 	 * check the target directory.
141 	 */
142 	if (doopen || domkdir) {
143 		for (;; --trv) {
144 			if (trv <= path)
145 				break;
146 			if (*trv == '/') {
147 				*trv = '\0';
148 				rval = stat(path, &sbuf);
149 				*trv = '/';
150 				if (rval != 0)
151 					return(0);
152 				if (!S_ISDIR(sbuf.st_mode)) {
153 					errno = ENOTDIR;
154 					return(0);
155 				}
156 				break;
157 			}
158 		}
159 	}
160 
161 	for (;;) {
162 		if (doopen) {
163 			if ((*doopen =
164 			    _open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0)
165 				return(1);
166 			if (errno != EEXIST)
167 				return(0);
168 		} else if (domkdir) {
169 			if (mkdir(path, 0700) == 0)
170 				return(1);
171 			if (errno != EEXIST)
172 				return(0);
173 		} else if (lstat(path, &sbuf))
174 			return(errno == ENOENT ? 1 : 0);
175 
176 		/* If we have a collision, cycle through the space of filenames */
177 		for (trv = start;;) {
178 			if (*trv == '\0' || trv == suffp)
179 				return(0);
180 			pad = strchr(padchar, *trv);
181 			if (pad == NULL || !*++pad)
182 				*trv++ = padchar[0];
183 			else {
184 				*trv++ = *pad;
185 				break;
186 			}
187 		}
188 	}
189 	/*NOTREACHED*/
190 }
191