xref: /illumos-gate/usr/src/lib/libc/port/gen/mktemp.c (revision 6a634c9d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c4dcc55Scasper  * Common Development and Distribution License (the "License").
67c4dcc55Scasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217257d1b4Sraf 
227c478bd9Sstevel@tonic-gate /*
23*23a1cceaSRoger A. Faulkner  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  *	mktemp() expects a string with up to six trailing 'X's.
317c478bd9Sstevel@tonic-gate  *	These will be overlaid with letters, digits and symbols from
327c478bd9Sstevel@tonic-gate  *	the portable filename character set.  If every combination thus
337c478bd9Sstevel@tonic-gate  *	inserted leads to an existing file name, the string is shortened
347c478bd9Sstevel@tonic-gate  *	to length zero and a pointer to a null string is returned.
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  *	The guarantee made by mktime() to the caller is that the
377c478bd9Sstevel@tonic-gate  *	generated file name string will not match the string
387c478bd9Sstevel@tonic-gate  *	produced by any other concurrent process using mktemp().
397c478bd9Sstevel@tonic-gate  *	To guarantee uniqueness across the process-id space,
407c478bd9Sstevel@tonic-gate  *	the process-id of the caller is encoded into the string.
417c478bd9Sstevel@tonic-gate  *	To allow repeated calls within the same process to generate
427c478bd9Sstevel@tonic-gate  *	different strings on each call, a sequence number is encoded
437c478bd9Sstevel@tonic-gate  *	into the string along with process-id.
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  *	The encoding is performed using radix-64 (6 bits per character),
467c478bd9Sstevel@tonic-gate  *	with 64 characters taken from the portable file name character set.
477c478bd9Sstevel@tonic-gate  *	This allows the six X's to be a representation of a 36-bit integer
487c478bd9Sstevel@tonic-gate  *	composed of bit fields:
497c478bd9Sstevel@tonic-gate  *		( pid | seq )
507c478bd9Sstevel@tonic-gate  *	where the process-id occupies the high-order bits and the sequence
517c478bd9Sstevel@tonic-gate  *	number occupies the low-order bits.  The size of the pid field is
527c478bd9Sstevel@tonic-gate  *	not fixed at the traditional 15 bits (MAXPID = 30000); the system
537c478bd9Sstevel@tonic-gate  *	now allows a larger process-id space and MAXPID is obtained from
547c478bd9Sstevel@tonic-gate  *	the system with a call to sysconf(_SC_MAXPID).
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  *	mktime() should fail if fewer than six X's are presented to it.
577c478bd9Sstevel@tonic-gate  *	However, this has been traditionally accepted and is preserved
587c478bd9Sstevel@tonic-gate  *	in the present code.  The consequence is that the 36-bit integer
597c478bd9Sstevel@tonic-gate  *	is reduced to a (6*N)-bit integer, where N is the number of X's.
607c478bd9Sstevel@tonic-gate  *	mktime() fails immediately if the resulting integer is not large
617c478bd9Sstevel@tonic-gate  *	enough to contain MAXPID.
627c478bd9Sstevel@tonic-gate  *
637c478bd9Sstevel@tonic-gate  *	In an attempt to confuse and thwart hackers, the starting
647c478bd9Sstevel@tonic-gate  *	sequence number is randomized using the current time.
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate 
677257d1b4Sraf #pragma weak _mktemp = mktemp
687257d1b4Sraf 
697c478bd9Sstevel@tonic-gate #define	XCNT  6
707c478bd9Sstevel@tonic-gate 
717257d1b4Sraf #include "lint.h"
727c478bd9Sstevel@tonic-gate #include "mtlib.h"
737c478bd9Sstevel@tonic-gate #include <sys/types.h>
747c478bd9Sstevel@tonic-gate #include <string.h>
757c478bd9Sstevel@tonic-gate #include <unistd.h>
767c478bd9Sstevel@tonic-gate #include <thread.h>
777c478bd9Sstevel@tonic-gate #include <synch.h>
787c478bd9Sstevel@tonic-gate #include <sys/stat.h>
797c478bd9Sstevel@tonic-gate #include <errno.h>
807c478bd9Sstevel@tonic-gate #include <sys/time.h>
817c478bd9Sstevel@tonic-gate #include <stdlib.h>
827c478bd9Sstevel@tonic-gate #include <stdio.h>
837c478bd9Sstevel@tonic-gate #include <sys/param.h>
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /*
867c478bd9Sstevel@tonic-gate  * 64-bit digits, must be from the POSIX "portable file name character set".
877c478bd9Sstevel@tonic-gate  */
887c478bd9Sstevel@tonic-gate static char
897c478bd9Sstevel@tonic-gate chars[64] = {
907c478bd9Sstevel@tonic-gate 	'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
917c478bd9Sstevel@tonic-gate 	'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
927c478bd9Sstevel@tonic-gate 	'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
937c478bd9Sstevel@tonic-gate 	'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
947c478bd9Sstevel@tonic-gate 	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '_',
957c478bd9Sstevel@tonic-gate };
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate char *
libc_mktemps(char * as,int slen)987c4dcc55Scasper libc_mktemps(char *as, int slen)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate 	/* statics are protected by this static mutex */
1017c478bd9Sstevel@tonic-gate 	static mutex_t	mktemp_lock = DEFAULTMUTEX;
1027c478bd9Sstevel@tonic-gate 	static int	pidshift = 0;
1037c478bd9Sstevel@tonic-gate 	static int	previous_try = 0;
1047c478bd9Sstevel@tonic-gate 	static pid_t	previous_pid = 0;
1057c478bd9Sstevel@tonic-gate 	static int	previous_xcnt = XCNT;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	pid_t		pid;
1087c478bd9Sstevel@tonic-gate 	int		try;
1097c478bd9Sstevel@tonic-gate 	int		tryshift;
1107c478bd9Sstevel@tonic-gate 	int		max_try;
1117c478bd9Sstevel@tonic-gate 	char		*s;
1127c478bd9Sstevel@tonic-gate 	char		*first_x;
1137c478bd9Sstevel@tonic-gate 	int		len;
1147c478bd9Sstevel@tonic-gate 	uint_t		xcnt;
1157c478bd9Sstevel@tonic-gate 	struct stat64	buf;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if (as == NULL || *as == '\0')	/* If the string passed is null then */
1187c478bd9Sstevel@tonic-gate 		return (as);	/* a pointer to a null string is returned. */
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	lmutex_lock(&mktemp_lock);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	pid = getpid();
1237c478bd9Sstevel@tonic-gate 	if (pid != previous_pid) {	/* first time or first after fork() */
1247c478bd9Sstevel@tonic-gate 		/*
1257c478bd9Sstevel@tonic-gate 		 * Randomize the starting sequence number in
1267c478bd9Sstevel@tonic-gate 		 * an attempt to confuse and thwart hackers.
1277c478bd9Sstevel@tonic-gate 		 * Use the low 12 bits of the time in milliseconds.
1287c478bd9Sstevel@tonic-gate 		 */
1297c478bd9Sstevel@tonic-gate 		struct timeval tm;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 		(void) gettimeofday(&tm, NULL);
1327c478bd9Sstevel@tonic-gate 		previous_try = (tm.tv_sec * 1000 + tm.tv_usec / 1000) & 0xfff;
1337c478bd9Sstevel@tonic-gate 		previous_pid = pid;
1347c478bd9Sstevel@tonic-gate 		previous_xcnt = XCNT;
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	/* for all possible values of pid, 0 <= pid < (1 << pidshift) */
1387c478bd9Sstevel@tonic-gate 	if (pidshift == 0)	/* one-time initialization */
139*23a1cceaSRoger A. Faulkner 		pidshift = fls((uint_t)MAXPID);		/* high bit number */
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	/* count the X's */
1427c478bd9Sstevel@tonic-gate 	xcnt = 0;
1437c478bd9Sstevel@tonic-gate 	len = (int)strlen(as);
1447c4dcc55Scasper 	if (slen >= len || slen < 0)
1457c4dcc55Scasper 		goto fail;
1467c4dcc55Scasper 	len -= slen;
1477c478bd9Sstevel@tonic-gate 	s = as + (len - 1);
1487c478bd9Sstevel@tonic-gate 	while ((len != 0) && (xcnt < XCNT) && (*s == 'X')) {
1497c478bd9Sstevel@tonic-gate 		xcnt++;
1507c478bd9Sstevel@tonic-gate 		len--;
1517c478bd9Sstevel@tonic-gate 		--s;
1527c478bd9Sstevel@tonic-gate 	}
1537c478bd9Sstevel@tonic-gate 	first_x = s + 1;	/* Remember pointer to the first X */
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	/* fail if we don't have enough X's to represent MAXPID */
1567c478bd9Sstevel@tonic-gate 	if ((tryshift = xcnt * 6 - pidshift) < 0) {
1577c478bd9Sstevel@tonic-gate 		/*
1587c478bd9Sstevel@tonic-gate 		 * Some broken programs call mktemp() repeatedly,
1597c478bd9Sstevel@tonic-gate 		 * passing the same string without reinserting the X's.
1607c478bd9Sstevel@tonic-gate 		 * Check to see if this is such a call by testing
1617c478bd9Sstevel@tonic-gate 		 * the trailing characters of the string for a
1627c478bd9Sstevel@tonic-gate 		 * match with the process-id.
1637c478bd9Sstevel@tonic-gate 		 */
1647c478bd9Sstevel@tonic-gate 		uint64_t xpid = 0;		/* reconstructed pid */
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 		s = as + len;
1677c478bd9Sstevel@tonic-gate 		for (xcnt = previous_xcnt; xcnt && s > as; xcnt--) {
1687c478bd9Sstevel@tonic-gate 			int c;
1697c478bd9Sstevel@tonic-gate 			int i;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 			c = *--s;
1727c478bd9Sstevel@tonic-gate 			for (i = 0; i < 64; i++)
1737c478bd9Sstevel@tonic-gate 				if (c == chars[i])
1747c478bd9Sstevel@tonic-gate 					break;
1757c478bd9Sstevel@tonic-gate 			if (i == 64)
1767c478bd9Sstevel@tonic-gate 				goto fail;
1777c478bd9Sstevel@tonic-gate 			xpid = xpid * 64 + i;
1787c478bd9Sstevel@tonic-gate 		}
1797c478bd9Sstevel@tonic-gate 		xpid >>= (previous_xcnt * 6 - pidshift);
1807c478bd9Sstevel@tonic-gate 		xpid &= ((1 << pidshift) - 1);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 		if (xpid == pid &&
1837c478bd9Sstevel@tonic-gate 		    lstat64(as, &buf) == -1 && errno == ENOENT) {
1847c478bd9Sstevel@tonic-gate 			lmutex_unlock(&mktemp_lock);
1857c478bd9Sstevel@tonic-gate 			return (as);
1867c478bd9Sstevel@tonic-gate 		}
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 		goto fail;
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	/* we can try sequence numbers in the range 0 <= try < max_try */
1927c478bd9Sstevel@tonic-gate 	max_try = 1 << tryshift;
1937c478bd9Sstevel@tonic-gate 	if (previous_try >= max_try)
1947c478bd9Sstevel@tonic-gate 		previous_try = 0;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	try = previous_try;
1977c478bd9Sstevel@tonic-gate 	for (;;) {
1987c478bd9Sstevel@tonic-gate 		/* num is up to a 36-bit integer ... */
1997c478bd9Sstevel@tonic-gate 		uint64_t num = ((uint64_t)pid << tryshift) + (uint64_t)try;
2007c478bd9Sstevel@tonic-gate 		int i;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 		/* ... which we represent backwards in base 64 */
2037c478bd9Sstevel@tonic-gate 		for (i = 0, s = first_x; i < xcnt; i++) {
2047c478bd9Sstevel@tonic-gate 			*s++ = chars[num & 077];
2057c478bd9Sstevel@tonic-gate 			num >>= 6;
2067c478bd9Sstevel@tonic-gate 		}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 		if (lstat64(as, &buf) == -1) {
2097c478bd9Sstevel@tonic-gate 			if (errno != ENOENT)
2107c478bd9Sstevel@tonic-gate 				break;		/* unrecoverable error */
2117c478bd9Sstevel@tonic-gate 			/* remember where we left off for the next call */
2127c478bd9Sstevel@tonic-gate 			previous_try = try + 1;
2137c478bd9Sstevel@tonic-gate 			previous_xcnt = xcnt;
2147c478bd9Sstevel@tonic-gate 			lmutex_unlock(&mktemp_lock);
2157c478bd9Sstevel@tonic-gate 			return (as);
2167c478bd9Sstevel@tonic-gate 		}
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 		if (++try == max_try)
2197c478bd9Sstevel@tonic-gate 			try = 0;
2207c478bd9Sstevel@tonic-gate 		if (try == previous_try)
2217c478bd9Sstevel@tonic-gate 			break;
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate fail:
2257c478bd9Sstevel@tonic-gate 	lmutex_unlock(&mktemp_lock);
2267c478bd9Sstevel@tonic-gate 	*as = '\0';
2277c478bd9Sstevel@tonic-gate 	return (as);
2287c478bd9Sstevel@tonic-gate }
2297c4dcc55Scasper 
2307c4dcc55Scasper char *
mktemp(char * template)2317c4dcc55Scasper mktemp(char *template)
2327c4dcc55Scasper {
2337c4dcc55Scasper 	return (libc_mktemps(template, 0));
2347c4dcc55Scasper }
235