xref: /freebsd/lib/libc/stdio/mktemp.c (revision 9911028f53d5c7504256289d3e56f6592a5a1247)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
458f0484fSRodney W. Grimes  * Copyright (c) 1987, 1993
558f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
658f0484fSRodney W. Grimes  *
758f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
858f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
958f0484fSRodney W. Grimes  * are met:
1058f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1158f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1258f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1358f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1458f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
151d8053c5SEd Maste  * 3. Neither the name of the University nor the names of its contributors
1658f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1758f0484fSRodney W. Grimes  *    without specific prior written permission.
1858f0484fSRodney W. Grimes  *
1958f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2058f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2158f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2258f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2358f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2458f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2558f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2658f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2758f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2858f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2958f0484fSRodney W. Grimes  * SUCH DAMAGE.
3058f0484fSRodney W. Grimes  */
3158f0484fSRodney W. Grimes 
32e0554a53SJacques Vidrine #include "namespace.h"
33e68d07ffSJohn Baldwin #include <sys/param.h>
3458f0484fSRodney W. Grimes #include <sys/stat.h>
3558f0484fSRodney W. Grimes #include <fcntl.h>
3658f0484fSRodney W. Grimes #include <errno.h>
3758f0484fSRodney W. Grimes #include <stdio.h>
382f253e75SWarner Losh #include <stdlib.h>
39f1303ab4SKris Kennaway #include <string.h>
4058f0484fSRodney W. Grimes #include <ctype.h>
41ce51cf03SJames Raynard #include <unistd.h>
42e0554a53SJacques Vidrine #include "un-namespace.h"
4358f0484fSRodney W. Grimes 
44c05ac53bSDavid E. O'Brien char *_mktemp(char *);
4587ad1267SBruce Evans 
4607657474SMark Johnston static int _gettemp(int, char *, int *, int, int, int);
4706b6a8abSWarner Losh 
48f1303ab4SKris Kennaway static const unsigned char padchar[] =
49f1303ab4SKris Kennaway "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
50f1303ab4SKris Kennaway 
5106b6a8abSWarner Losh int
mkostempsat(int dfd,char * path,int slen,int oflags)5207657474SMark Johnston mkostempsat(int dfd, char *path, int slen, int oflags)
5307657474SMark Johnston {
5407657474SMark Johnston 	int fd;
5507657474SMark Johnston 
5607657474SMark Johnston 	return (_gettemp(dfd, path, &fd, 0, slen, oflags) ? fd : -1);
5707657474SMark Johnston }
5807657474SMark Johnston 
5907657474SMark Johnston int
mkostemps(char * path,int slen,int oflags)6065ba8dffSJilles Tjoelker mkostemps(char *path, int slen, int oflags)
6165ba8dffSJilles Tjoelker {
6265ba8dffSJilles Tjoelker 	int fd;
6365ba8dffSJilles Tjoelker 
6407657474SMark Johnston 	return (_gettemp(AT_FDCWD, path, &fd, 0, slen, oflags) ? fd : -1);
6565ba8dffSJilles Tjoelker }
6665ba8dffSJilles Tjoelker 
6765ba8dffSJilles Tjoelker int
mkstemps(char * path,int slen)685a6307cfSEd Maste mkstemps(char *path, int slen)
6906b6a8abSWarner Losh {
7006b6a8abSWarner Losh 	int fd;
7106b6a8abSWarner Losh 
7207657474SMark Johnston 	return (_gettemp(AT_FDCWD, path, &fd, 0, slen, 0) ? fd : -1);
7365ba8dffSJilles Tjoelker }
7465ba8dffSJilles Tjoelker 
7565ba8dffSJilles Tjoelker int
mkostemp(char * path,int oflags)7665ba8dffSJilles Tjoelker mkostemp(char *path, int oflags)
7765ba8dffSJilles Tjoelker {
7865ba8dffSJilles Tjoelker 	int fd;
7965ba8dffSJilles Tjoelker 
8007657474SMark Johnston 	return (_gettemp(AT_FDCWD, path, &fd, 0, 0, oflags) ? fd : -1);
8106b6a8abSWarner Losh }
8258f0484fSRodney W. Grimes 
83ce51cf03SJames Raynard int
mkstemp(char * path)845a6307cfSEd Maste mkstemp(char *path)
8558f0484fSRodney W. Grimes {
86ed1bbda8SPeter Wemm 	int fd;
8758f0484fSRodney W. Grimes 
8807657474SMark Johnston 	return (_gettemp(AT_FDCWD, path, &fd, 0, 0, 0) ? fd : -1);
8958f0484fSRodney W. Grimes }
9058f0484fSRodney W. Grimes 
9158f0484fSRodney W. Grimes char *
mkdtemp(char * path)925a6307cfSEd Maste mkdtemp(char *path)
932f253e75SWarner Losh {
9407657474SMark Johnston 	return (_gettemp(AT_FDCWD, path, (int *)NULL, 1, 0, 0) ? path : (char *)NULL);
952f253e75SWarner Losh }
962f253e75SWarner Losh 
972f253e75SWarner Losh char *
_mktemp(char * path)985a6307cfSEd Maste _mktemp(char *path)
992f253e75SWarner Losh {
10007657474SMark Johnston 	return (_gettemp(AT_FDCWD, path, (int *)NULL, 0, 0, 0) ? path : (char *)NULL);
1012f253e75SWarner Losh }
1022f253e75SWarner Losh 
1032f253e75SWarner Losh __warn_references(mktemp,
1042f253e75SWarner Losh     "warning: mktemp() possibly used unsafely; consider using mkstemp()");
1052f253e75SWarner Losh 
1062f253e75SWarner Losh char *
mktemp(char * path)1075a6307cfSEd Maste mktemp(char *path)
10858f0484fSRodney W. Grimes {
1092f253e75SWarner Losh 	return (_mktemp(path));
11058f0484fSRodney W. Grimes }
11158f0484fSRodney W. Grimes 
112ce51cf03SJames Raynard static int
_gettemp(int dfd,char * path,int * doopen,int domkdir,int slen,int oflags)11307657474SMark Johnston _gettemp(int dfd, char *path, int *doopen, int domkdir, int slen, int oflags)
11458f0484fSRodney W. Grimes {
115e68d07ffSJohn Baldwin 	char *start, *trv, *suffp, *carryp;
116f1303ab4SKris Kennaway 	char *pad;
11758f0484fSRodney W. Grimes 	struct stat sbuf;
118f1303ab4SKris Kennaway 	uint32_t rand;
119e68d07ffSJohn Baldwin 	char carrybuf[MAXPATHLEN];
120b22fdf45SMateusz Guzik 	int saved;
1212f253e75SWarner Losh 
12265ba8dffSJilles Tjoelker 	if ((doopen != NULL && domkdir) || slen < 0 ||
12365ba8dffSJilles Tjoelker 	    (oflags & ~(O_APPEND | O_DIRECT | O_SHLOCK | O_EXLOCK | O_SYNC |
124*9911028fSRicardo Branco 	    O_CLOEXEC | O_CLOFORK)) != 0) {
1252f253e75SWarner Losh 		errno = EINVAL;
1262f253e75SWarner Losh 		return (0);
1272f253e75SWarner Losh 	}
12858f0484fSRodney W. Grimes 
1296fe328acSMateusz Guzik 	trv = path + strlen(path);
1306ca7812aSJaakko Heinonen 	if (trv - path >= MAXPATHLEN) {
1316ca7812aSJaakko Heinonen 		errno = ENAMETOOLONG;
1326ca7812aSJaakko Heinonen 		return (0);
1336ca7812aSJaakko Heinonen 	}
13406b6a8abSWarner Losh 	trv -= slen;
13506b6a8abSWarner Losh 	suffp = trv;
1362f253e75SWarner Losh 	--trv;
137e68d07ffSJohn Baldwin 	if (trv < path || NULL != strchr(suffp, '/')) {
13806b6a8abSWarner Losh 		errno = EINVAL;
13906b6a8abSWarner Losh 		return (0);
14006b6a8abSWarner Losh 	}
1412f253e75SWarner Losh 
142f1303ab4SKris Kennaway 	/* Fill space with random characters */
143ccccc4e1SRobert Drehmel 	while (trv >= path && *trv == 'X') {
144bc8541b1SAndrey A. Chernov 		rand = arc4random_uniform(sizeof(padchar) - 1);
145f1303ab4SKris Kennaway 		*trv-- = padchar[rand];
1462f253e75SWarner Losh 	}
14787ad1267SBruce Evans 	start = trv + 1;
14858f0484fSRodney W. Grimes 
149b22fdf45SMateusz Guzik 	saved = 0;
15007657474SMark Johnston 	oflags |= O_CREAT | O_EXCL | O_RDWR;
15158f0484fSRodney W. Grimes 	for (;;) {
15258f0484fSRodney W. Grimes 		if (doopen) {
15307657474SMark Johnston 			*doopen = _openat(dfd, path, oflags, 0600);
15407657474SMark Johnston 			if (*doopen >= 0)
15558f0484fSRodney W. Grimes 				return (1);
15658f0484fSRodney W. Grimes 			if (errno != EEXIST)
15758f0484fSRodney W. Grimes 				return (0);
1582f253e75SWarner Losh 		} else if (domkdir) {
1592f253e75SWarner Losh 			if (mkdir(path, 0700) == 0)
1602f253e75SWarner Losh 				return (1);
1612f253e75SWarner Losh 			if (errno != EEXIST)
1622f253e75SWarner Losh 				return (0);
1632f253e75SWarner Losh 		} else if (lstat(path, &sbuf))
164fb08c048SRobert Drehmel 			return (errno == ENOENT);
16558f0484fSRodney W. Grimes 
166b22fdf45SMateusz Guzik 		/* save first combination of random characters */
167b22fdf45SMateusz Guzik 		if (!saved) {
168b22fdf45SMateusz Guzik 			memcpy(carrybuf, start, suffp - start);
169b22fdf45SMateusz Guzik 			saved = 1;
170b22fdf45SMateusz Guzik 		}
171b22fdf45SMateusz Guzik 
172f1303ab4SKris Kennaway 		/* If we have a collision, cycle through the space of filenames */
173e68d07ffSJohn Baldwin 		for (trv = start, carryp = carrybuf;;) {
174e68d07ffSJohn Baldwin 			/* have we tried all possible permutations? */
175e68d07ffSJohn Baldwin 			if (trv == suffp)
176e68d07ffSJohn Baldwin 				return (0); /* yes - exit with EEXIST */
177f1303ab4SKris Kennaway 			pad = strchr(padchar, *trv);
178e68d07ffSJohn Baldwin 			if (pad == NULL) {
179e68d07ffSJohn Baldwin 				/* this should never happen */
180e68d07ffSJohn Baldwin 				errno = EIO;
181e68d07ffSJohn Baldwin 				return (0);
182e68d07ffSJohn Baldwin 			}
183e68d07ffSJohn Baldwin 			/* increment character */
184e68d07ffSJohn Baldwin 			*trv = (*++pad == '\0') ? padchar[0] : *pad;
185e68d07ffSJohn Baldwin 			/* carry to next position? */
186e68d07ffSJohn Baldwin 			if (*trv == *carryp) {
187e68d07ffSJohn Baldwin 				/* increment position and loop */
188e68d07ffSJohn Baldwin 				++trv;
189e68d07ffSJohn Baldwin 				++carryp;
190e68d07ffSJohn Baldwin 			} else {
191e68d07ffSJohn Baldwin 				/* try with new name */
19258f0484fSRodney W. Grimes 				break;
19358f0484fSRodney W. Grimes 			}
19458f0484fSRodney W. Grimes 		}
19558f0484fSRodney W. Grimes 	}
19658f0484fSRodney W. Grimes 	/*NOTREACHED*/
19758f0484fSRodney W. Grimes }
198