1*0a6a1f1dSLionel Sambuc /* $NetBSD: gettemp.c,v 1.20 2015/02/05 16:05:20 christos Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras * Copyright (c) 1987, 1993
52fe8fb19SBen Gras * The Regents of the University of California. All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras * are met:
102fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras * 3. Neither the name of the University nor the names of its contributors
162fe8fb19SBen Gras * may be used to endorse or promote products derived from this software
172fe8fb19SBen Gras * without specific prior written permission.
182fe8fb19SBen Gras *
192fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
202fe8fb19SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212fe8fb19SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222fe8fb19SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
232fe8fb19SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242fe8fb19SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252fe8fb19SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262fe8fb19SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
272fe8fb19SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
282fe8fb19SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
292fe8fb19SBen Gras * SUCH DAMAGE.
302fe8fb19SBen Gras */
312fe8fb19SBen Gras
32*0a6a1f1dSLionel Sambuc #include "gettemp.h"
332fe8fb19SBen Gras
342fe8fb19SBen Gras #if !HAVE_NBTOOL_CONFIG_H || !HAVE_MKSTEMP || !HAVE_MKDTEMP
352fe8fb19SBen Gras
362fe8fb19SBen Gras #include <sys/cdefs.h>
372fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
382fe8fb19SBen Gras #if 0
392fe8fb19SBen Gras static char sccsid[] = "@(#)mktemp.c 8.1 (Berkeley) 6/4/93";
402fe8fb19SBen Gras #else
41*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: gettemp.c,v 1.20 2015/02/05 16:05:20 christos Exp $");
422fe8fb19SBen Gras #endif
432fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
442fe8fb19SBen Gras
45*0a6a1f1dSLionel Sambuc #include <sys/param.h>
462fe8fb19SBen Gras #include <fcntl.h>
47*0a6a1f1dSLionel Sambuc #include <string.h>
482fe8fb19SBen Gras
49*0a6a1f1dSLionel Sambuc static const unsigned char padchar[] =
50*0a6a1f1dSLionel Sambuc "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
512fe8fb19SBen Gras
522fe8fb19SBen Gras int
GETTEMP(char * path,int * doopen,int domkdir,int slen,int oflags)53*0a6a1f1dSLionel Sambuc GETTEMP(char *path, int *doopen, int domkdir, int slen, int oflags)
542fe8fb19SBen Gras {
55*0a6a1f1dSLionel Sambuc char *start, *trv, *suffp, *carryp;
56*0a6a1f1dSLionel Sambuc char *pad;
572fe8fb19SBen Gras struct stat sbuf;
58*0a6a1f1dSLionel Sambuc int rval;
59*0a6a1f1dSLionel Sambuc uint32_t r;
60*0a6a1f1dSLionel Sambuc char carrybuf[MAXPATHLEN];
612fe8fb19SBen Gras
622fe8fb19SBen Gras _DIAGASSERT(path != NULL);
632fe8fb19SBen Gras /* doopen may be NULL */
64*0a6a1f1dSLionel Sambuc if ((doopen != NULL && domkdir) || slen < 0 ||
65*0a6a1f1dSLionel Sambuc (oflags & ~(O_APPEND | O_DIRECT | O_SHLOCK | O_EXLOCK | O_SYNC |
66*0a6a1f1dSLionel Sambuc O_CLOEXEC)) != 0) {
67*0a6a1f1dSLionel Sambuc errno = EINVAL;
68*0a6a1f1dSLionel Sambuc return 0;
692fe8fb19SBen Gras }
702fe8fb19SBen Gras
71*0a6a1f1dSLionel Sambuc for (trv = path; *trv != '\0'; ++trv)
72*0a6a1f1dSLionel Sambuc continue;
73*0a6a1f1dSLionel Sambuc
74*0a6a1f1dSLionel Sambuc if (trv - path >= MAXPATHLEN) {
75*0a6a1f1dSLionel Sambuc errno = ENAMETOOLONG;
76*0a6a1f1dSLionel Sambuc return 0;
772fe8fb19SBen Gras }
78*0a6a1f1dSLionel Sambuc trv -= slen;
79*0a6a1f1dSLionel Sambuc suffp = trv;
80*0a6a1f1dSLionel Sambuc --trv;
81*0a6a1f1dSLionel Sambuc if (trv < path || NULL != strchr(suffp, '/')) {
82*0a6a1f1dSLionel Sambuc errno = EINVAL;
83*0a6a1f1dSLionel Sambuc return 0;
84*0a6a1f1dSLionel Sambuc }
85*0a6a1f1dSLionel Sambuc
86*0a6a1f1dSLionel Sambuc /* Fill space with random characters */
87*0a6a1f1dSLionel Sambuc while (trv >= path && *trv == 'X') {
88*0a6a1f1dSLionel Sambuc r = arc4random_uniform((unsigned int)(sizeof(padchar) - 1));
89*0a6a1f1dSLionel Sambuc *trv-- = padchar[r];
90*0a6a1f1dSLionel Sambuc }
91*0a6a1f1dSLionel Sambuc start = trv + 1;
92*0a6a1f1dSLionel Sambuc
93*0a6a1f1dSLionel Sambuc /* save first combination of random characters */
94*0a6a1f1dSLionel Sambuc memcpy(carrybuf, start, (size_t)(suffp - start));
952fe8fb19SBen Gras
962fe8fb19SBen Gras /*
97*0a6a1f1dSLionel Sambuc * check the target directory.
982fe8fb19SBen Gras */
99*0a6a1f1dSLionel Sambuc if (doopen != NULL || domkdir) {
100*0a6a1f1dSLionel Sambuc for (; trv > path; --trv) {
1012fe8fb19SBen Gras if (*trv == '/') {
1022fe8fb19SBen Gras *trv = '\0';
103*0a6a1f1dSLionel Sambuc rval = stat(path, &sbuf);
10484d9c625SLionel Sambuc *trv = '/';
105*0a6a1f1dSLionel Sambuc if (rval != 0)
106*0a6a1f1dSLionel Sambuc return 0;
1072fe8fb19SBen Gras if (!S_ISDIR(sbuf.st_mode)) {
1082fe8fb19SBen Gras errno = ENOTDIR;
109*0a6a1f1dSLionel Sambuc return 0;
1102fe8fb19SBen Gras }
1112fe8fb19SBen Gras break;
1122fe8fb19SBen Gras }
1132fe8fb19SBen Gras }
114*0a6a1f1dSLionel Sambuc }
1152fe8fb19SBen Gras
1162fe8fb19SBen Gras for (;;) {
1172fe8fb19SBen Gras if (doopen) {
118*0a6a1f1dSLionel Sambuc if ((*doopen = open(path, O_CREAT|O_EXCL|O_RDWR|oflags,
119*0a6a1f1dSLionel Sambuc 0600)) != -1)
120f14fb602SLionel Sambuc return 1;
1212fe8fb19SBen Gras if (errno != EEXIST)
122f14fb602SLionel Sambuc return 0;
1232fe8fb19SBen Gras } else if (domkdir) {
124*0a6a1f1dSLionel Sambuc if (mkdir(path, 0700) != -1)
125f14fb602SLionel Sambuc return 1;
1262fe8fb19SBen Gras if (errno != EEXIST)
127f14fb602SLionel Sambuc return 0;
1282fe8fb19SBen Gras } else if (lstat(path, &sbuf))
129*0a6a1f1dSLionel Sambuc return errno == ENOENT;
1302fe8fb19SBen Gras
131*0a6a1f1dSLionel Sambuc /*
132*0a6a1f1dSLionel Sambuc * If we have a collision,
133*0a6a1f1dSLionel Sambuc * cycle through the space of filenames
134*0a6a1f1dSLionel Sambuc */
135*0a6a1f1dSLionel Sambuc for (trv = start, carryp = carrybuf;;) {
136*0a6a1f1dSLionel Sambuc /* have we tried all possible permutations? */
137*0a6a1f1dSLionel Sambuc if (trv == suffp)
138*0a6a1f1dSLionel Sambuc return 0; /* yes - exit with EEXIST */
139*0a6a1f1dSLionel Sambuc pad = strchr((const char *)padchar, *trv);
140*0a6a1f1dSLionel Sambuc if (pad == NULL) {
141*0a6a1f1dSLionel Sambuc /* this should never happen */
142*0a6a1f1dSLionel Sambuc errno = EIO;
143f14fb602SLionel Sambuc return 0;
144*0a6a1f1dSLionel Sambuc }
145*0a6a1f1dSLionel Sambuc /* increment character */
146*0a6a1f1dSLionel Sambuc *trv = (*++pad == '\0') ? padchar[0] : *pad;
147*0a6a1f1dSLionel Sambuc /* carry to next position? */
148*0a6a1f1dSLionel Sambuc if (*trv == *carryp) {
149*0a6a1f1dSLionel Sambuc /* increment position and loop */
150*0a6a1f1dSLionel Sambuc ++trv;
151*0a6a1f1dSLionel Sambuc ++carryp;
152*0a6a1f1dSLionel Sambuc } else {
153*0a6a1f1dSLionel Sambuc /* try with new name */
1542fe8fb19SBen Gras break;
1552fe8fb19SBen Gras }
1562fe8fb19SBen Gras }
1572fe8fb19SBen Gras }
1582fe8fb19SBen Gras /*NOTREACHED*/
1592fe8fb19SBen Gras }
1602fe8fb19SBen Gras
1612fe8fb19SBen Gras #endif /* !HAVE_NBTOOL_CONFIG_H || !HAVE_MKSTEMP || !HAVE_MKDTEMP */
162