1 /* $OpenBSD: mktemp.c,v 1.19 2005/08/08 08:05:36 espie Exp $ */
2 /*
3 * Copyright (c) 1987, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <errno.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <ctype.h>
38 #include <unistd.h>
39
40 static int _gettemp(char *, int *, int, int);
41
42 extern uint32_t arc4random();
43
44 int
mkstemps(char * path,int slen)45 mkstemps(char *path, int slen)
46 {
47 int fd;
48
49 return (_gettemp(path, &fd, 0, slen) ? fd : -1);
50 }
51
52 int
mkstemp(char * path)53 mkstemp(char *path)
54 {
55 int fd;
56
57 return (_gettemp(path, &fd, 0, 0) ? fd : -1);
58 }
59
60 char *
mkdtemp(char * path)61 mkdtemp(char *path)
62 {
63 return(_gettemp(path, (int *)NULL, 1, 0) ? path : (char *)NULL);
64 }
65
66 char *_mktemp(char *);
67
68 __LIBC_HIDDEN__ char *
_mktemp(char * path)69 _mktemp(char *path)
70 {
71 return(_gettemp(path, (int *)NULL, 0, 0) ? path : (char *)NULL);
72 }
73
74 __warn_references(mktemp,
75 "warning: mktemp() possibly used unsafely; consider using mkstemp()");
76
77 char *
mktemp(char * path)78 mktemp(char *path)
79 {
80 return(_mktemp(path));
81 }
82
83
84 static int
_gettemp(char * path,int * doopen,int domkdir,int slen)85 _gettemp(char *path, int *doopen, int domkdir, int slen)
86 {
87 char *start, *trv, *suffp;
88 struct stat sbuf;
89 int rval;
90 pid_t pid;
91
92 if (doopen && domkdir) {
93 errno = EINVAL;
94 return(0);
95 }
96
97 for (trv = path; *trv; ++trv)
98 ;
99 trv -= slen;
100 suffp = trv;
101 --trv;
102 if (trv < path) {
103 errno = EINVAL;
104 return (0);
105 }
106 pid = getpid();
107 while (trv >= path && *trv == 'X' && pid != 0) {
108 *trv-- = (pid % 10) + '0';
109 pid /= 10;
110 }
111 while (trv >= path && *trv == 'X') {
112 char c;
113
114 pid = (arc4random() & 0xffff) % (26+26);
115 if (pid < 26)
116 c = pid + 'A';
117 else
118 c = (pid - 26) + 'a';
119 *trv-- = c;
120 }
121 start = trv + 1;
122
123 /*
124 * check the target directory; if you have six X's and it
125 * doesn't exist this runs for a *very* long time.
126 */
127 if (doopen || domkdir) {
128 for (;; --trv) {
129 if (trv <= path)
130 break;
131 if (*trv == '/') {
132 *trv = '\0';
133 rval = stat(path, &sbuf);
134 *trv = '/';
135 if (rval != 0)
136 return(0);
137 if (!S_ISDIR(sbuf.st_mode)) {
138 errno = ENOTDIR;
139 return(0);
140 }
141 break;
142 }
143 }
144 }
145
146 for (;;) {
147 if (doopen) {
148 if ((*doopen =
149 open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0)
150 return(1);
151 if (errno != EEXIST)
152 return(0);
153 } else if (domkdir) {
154 if (mkdir(path, 0700) == 0)
155 return(1);
156 if (errno != EEXIST)
157 return(0);
158 } else if (lstat(path, &sbuf))
159 return(errno == ENOENT ? 1 : 0);
160
161 /* tricky little algorithm for backward compatibility */
162 for (trv = start;;) {
163 if (!*trv)
164 return (0);
165 if (*trv == 'Z') {
166 if (trv == suffp)
167 return (0);
168 *trv++ = 'a';
169 } else {
170 if (isdigit(*trv))
171 *trv = 'a';
172 else if (*trv == 'z') /* inc from z to A */
173 *trv = 'A';
174 else {
175 if (trv == suffp)
176 return (0);
177 ++*trv;
178 }
179 break;
180 }
181 }
182 }
183 /*NOTREACHED*/
184 }
185