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