10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*1475Scasper * Common Development and Distribution License (the "License"). 6*1475Scasper * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*1475Scasper * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */ 290Sstevel@tonic-gate /* All Rights Reserved */ 300Sstevel@tonic-gate 310Sstevel@tonic-gate 320Sstevel@tonic-gate /* 330Sstevel@tonic-gate * mktemp() expects a string with up to six trailing 'X's. 340Sstevel@tonic-gate * These will be overlaid with letters, digits and symbols from 350Sstevel@tonic-gate * the portable filename character set. If every combination thus 360Sstevel@tonic-gate * inserted leads to an existing file name, the string is shortened 370Sstevel@tonic-gate * to length zero and a pointer to a null string is returned. 380Sstevel@tonic-gate * 390Sstevel@tonic-gate * The guarantee made by mktime() to the caller is that the 400Sstevel@tonic-gate * generated file name string will not match the string 410Sstevel@tonic-gate * produced by any other concurrent process using mktemp(). 420Sstevel@tonic-gate * To guarantee uniqueness across the process-id space, 430Sstevel@tonic-gate * the process-id of the caller is encoded into the string. 440Sstevel@tonic-gate * To allow repeated calls within the same process to generate 450Sstevel@tonic-gate * different strings on each call, a sequence number is encoded 460Sstevel@tonic-gate * into the string along with process-id. 470Sstevel@tonic-gate * 480Sstevel@tonic-gate * The encoding is performed using radix-64 (6 bits per character), 490Sstevel@tonic-gate * with 64 characters taken from the portable file name character set. 500Sstevel@tonic-gate * This allows the six X's to be a representation of a 36-bit integer 510Sstevel@tonic-gate * composed of bit fields: 520Sstevel@tonic-gate * ( pid | seq ) 530Sstevel@tonic-gate * where the process-id occupies the high-order bits and the sequence 540Sstevel@tonic-gate * number occupies the low-order bits. The size of the pid field is 550Sstevel@tonic-gate * not fixed at the traditional 15 bits (MAXPID = 30000); the system 560Sstevel@tonic-gate * now allows a larger process-id space and MAXPID is obtained from 570Sstevel@tonic-gate * the system with a call to sysconf(_SC_MAXPID). 580Sstevel@tonic-gate * 590Sstevel@tonic-gate * mktime() should fail if fewer than six X's are presented to it. 600Sstevel@tonic-gate * However, this has been traditionally accepted and is preserved 610Sstevel@tonic-gate * in the present code. The consequence is that the 36-bit integer 620Sstevel@tonic-gate * is reduced to a (6*N)-bit integer, where N is the number of X's. 630Sstevel@tonic-gate * mktime() fails immediately if the resulting integer is not large 640Sstevel@tonic-gate * enough to contain MAXPID. 650Sstevel@tonic-gate * 660Sstevel@tonic-gate * In an attempt to confuse and thwart hackers, the starting 670Sstevel@tonic-gate * sequence number is randomized using the current time. 680Sstevel@tonic-gate */ 690Sstevel@tonic-gate 700Sstevel@tonic-gate #define XCNT 6 710Sstevel@tonic-gate #pragma weak mktemp = _mktemp 720Sstevel@tonic-gate 730Sstevel@tonic-gate #include "synonyms.h" 740Sstevel@tonic-gate #include "mtlib.h" 750Sstevel@tonic-gate #include <sys/types.h> 760Sstevel@tonic-gate #include <string.h> 770Sstevel@tonic-gate #include <unistd.h> 780Sstevel@tonic-gate #include <thread.h> 790Sstevel@tonic-gate #include <synch.h> 800Sstevel@tonic-gate #include <sys/stat.h> 810Sstevel@tonic-gate #include <errno.h> 820Sstevel@tonic-gate #include <sys/time.h> 830Sstevel@tonic-gate #include <stdlib.h> 840Sstevel@tonic-gate #include <stdio.h> 850Sstevel@tonic-gate #include <sys/param.h> 860Sstevel@tonic-gate 870Sstevel@tonic-gate /* 880Sstevel@tonic-gate * 64-bit digits, must be from the POSIX "portable file name character set". 890Sstevel@tonic-gate */ 900Sstevel@tonic-gate static char 910Sstevel@tonic-gate chars[64] = { 920Sstevel@tonic-gate 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 930Sstevel@tonic-gate 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 940Sstevel@tonic-gate 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 950Sstevel@tonic-gate 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 960Sstevel@tonic-gate '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '_', 970Sstevel@tonic-gate }; 980Sstevel@tonic-gate 990Sstevel@tonic-gate /* 1000Sstevel@tonic-gate * Find highest one bit set. 1010Sstevel@tonic-gate * Returns bit number of highest bit that is set. 1020Sstevel@tonic-gate * Low order bit is number 0, high order bit is number 31. 1030Sstevel@tonic-gate */ 1040Sstevel@tonic-gate static int 1050Sstevel@tonic-gate highbit(uint_t i) 1060Sstevel@tonic-gate { 1070Sstevel@tonic-gate int h = 0; 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate if (i & 0xffff0000) 1100Sstevel@tonic-gate h += 16, i >>= 16; 1110Sstevel@tonic-gate if (i & 0xff00) 1120Sstevel@tonic-gate h += 8, i >>= 8; 1130Sstevel@tonic-gate if (i & 0xf0) 1140Sstevel@tonic-gate h += 4, i >>= 4; 1150Sstevel@tonic-gate if (i & 0xc) 1160Sstevel@tonic-gate h += 2, i >>= 2; 1170Sstevel@tonic-gate if (i & 0x2) 1180Sstevel@tonic-gate h += 1; 1190Sstevel@tonic-gate return (h); 1200Sstevel@tonic-gate } 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate char * 123*1475Scasper libc_mktemps(char *as, int slen) 1240Sstevel@tonic-gate { 1250Sstevel@tonic-gate /* statics are protected by this static mutex */ 1260Sstevel@tonic-gate static mutex_t mktemp_lock = DEFAULTMUTEX; 1270Sstevel@tonic-gate static int pidshift = 0; 1280Sstevel@tonic-gate static int previous_try = 0; 1290Sstevel@tonic-gate static pid_t previous_pid = 0; 1300Sstevel@tonic-gate static int previous_xcnt = XCNT; 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate pid_t pid; 1330Sstevel@tonic-gate int try; 1340Sstevel@tonic-gate int tryshift; 1350Sstevel@tonic-gate int max_try; 1360Sstevel@tonic-gate char *s; 1370Sstevel@tonic-gate char *first_x; 1380Sstevel@tonic-gate int len; 1390Sstevel@tonic-gate uint_t xcnt; 1400Sstevel@tonic-gate struct stat64 buf; 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate if (as == NULL || *as == '\0') /* If the string passed is null then */ 1430Sstevel@tonic-gate return (as); /* a pointer to a null string is returned. */ 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate lmutex_lock(&mktemp_lock); 1460Sstevel@tonic-gate 1470Sstevel@tonic-gate pid = getpid(); 1480Sstevel@tonic-gate if (pid != previous_pid) { /* first time or first after fork() */ 1490Sstevel@tonic-gate /* 1500Sstevel@tonic-gate * Randomize the starting sequence number in 1510Sstevel@tonic-gate * an attempt to confuse and thwart hackers. 1520Sstevel@tonic-gate * Use the low 12 bits of the time in milliseconds. 1530Sstevel@tonic-gate */ 1540Sstevel@tonic-gate struct timeval tm; 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate (void) gettimeofday(&tm, NULL); 1570Sstevel@tonic-gate previous_try = (tm.tv_sec * 1000 + tm.tv_usec / 1000) & 0xfff; 1580Sstevel@tonic-gate previous_pid = pid; 1590Sstevel@tonic-gate previous_xcnt = XCNT; 1600Sstevel@tonic-gate } 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate /* for all possible values of pid, 0 <= pid < (1 << pidshift) */ 1630Sstevel@tonic-gate if (pidshift == 0) /* one-time initialization */ 1640Sstevel@tonic-gate pidshift = highbit((uint_t)MAXPID) + 1; 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate /* count the X's */ 1670Sstevel@tonic-gate xcnt = 0; 1680Sstevel@tonic-gate len = (int)strlen(as); 169*1475Scasper if (slen >= len || slen < 0) 170*1475Scasper goto fail; 171*1475Scasper len -= slen; 1720Sstevel@tonic-gate s = as + (len - 1); 1730Sstevel@tonic-gate while ((len != 0) && (xcnt < XCNT) && (*s == 'X')) { 1740Sstevel@tonic-gate xcnt++; 1750Sstevel@tonic-gate len--; 1760Sstevel@tonic-gate --s; 1770Sstevel@tonic-gate } 1780Sstevel@tonic-gate first_x = s + 1; /* Remember pointer to the first X */ 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate /* fail if we don't have enough X's to represent MAXPID */ 1810Sstevel@tonic-gate if ((tryshift = xcnt * 6 - pidshift) < 0) { 1820Sstevel@tonic-gate /* 1830Sstevel@tonic-gate * Some broken programs call mktemp() repeatedly, 1840Sstevel@tonic-gate * passing the same string without reinserting the X's. 1850Sstevel@tonic-gate * Check to see if this is such a call by testing 1860Sstevel@tonic-gate * the trailing characters of the string for a 1870Sstevel@tonic-gate * match with the process-id. 1880Sstevel@tonic-gate */ 1890Sstevel@tonic-gate uint64_t xpid = 0; /* reconstructed pid */ 1900Sstevel@tonic-gate 1910Sstevel@tonic-gate s = as + len; 1920Sstevel@tonic-gate for (xcnt = previous_xcnt; xcnt && s > as; xcnt--) { 1930Sstevel@tonic-gate int c; 1940Sstevel@tonic-gate int i; 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate c = *--s; 1970Sstevel@tonic-gate for (i = 0; i < 64; i++) 1980Sstevel@tonic-gate if (c == chars[i]) 1990Sstevel@tonic-gate break; 2000Sstevel@tonic-gate if (i == 64) 2010Sstevel@tonic-gate goto fail; 2020Sstevel@tonic-gate xpid = xpid * 64 + i; 2030Sstevel@tonic-gate } 2040Sstevel@tonic-gate xpid >>= (previous_xcnt * 6 - pidshift); 2050Sstevel@tonic-gate xpid &= ((1 << pidshift) - 1); 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate if (xpid == pid && 2080Sstevel@tonic-gate lstat64(as, &buf) == -1 && errno == ENOENT) { 2090Sstevel@tonic-gate lmutex_unlock(&mktemp_lock); 2100Sstevel@tonic-gate return (as); 2110Sstevel@tonic-gate } 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate goto fail; 2140Sstevel@tonic-gate } 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate /* we can try sequence numbers in the range 0 <= try < max_try */ 2170Sstevel@tonic-gate max_try = 1 << tryshift; 2180Sstevel@tonic-gate if (previous_try >= max_try) 2190Sstevel@tonic-gate previous_try = 0; 2200Sstevel@tonic-gate 2210Sstevel@tonic-gate try = previous_try; 2220Sstevel@tonic-gate for (;;) { 2230Sstevel@tonic-gate /* num is up to a 36-bit integer ... */ 2240Sstevel@tonic-gate uint64_t num = ((uint64_t)pid << tryshift) + (uint64_t)try; 2250Sstevel@tonic-gate int i; 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate /* ... which we represent backwards in base 64 */ 2280Sstevel@tonic-gate for (i = 0, s = first_x; i < xcnt; i++) { 2290Sstevel@tonic-gate *s++ = chars[num & 077]; 2300Sstevel@tonic-gate num >>= 6; 2310Sstevel@tonic-gate } 2320Sstevel@tonic-gate 2330Sstevel@tonic-gate if (lstat64(as, &buf) == -1) { 2340Sstevel@tonic-gate if (errno != ENOENT) 2350Sstevel@tonic-gate break; /* unrecoverable error */ 2360Sstevel@tonic-gate /* remember where we left off for the next call */ 2370Sstevel@tonic-gate previous_try = try + 1; 2380Sstevel@tonic-gate previous_xcnt = xcnt; 2390Sstevel@tonic-gate lmutex_unlock(&mktemp_lock); 2400Sstevel@tonic-gate return (as); 2410Sstevel@tonic-gate } 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate if (++try == max_try) 2440Sstevel@tonic-gate try = 0; 2450Sstevel@tonic-gate if (try == previous_try) 2460Sstevel@tonic-gate break; 2470Sstevel@tonic-gate } 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate fail: 2500Sstevel@tonic-gate lmutex_unlock(&mktemp_lock); 2510Sstevel@tonic-gate *as = '\0'; 2520Sstevel@tonic-gate return (as); 2530Sstevel@tonic-gate } 254*1475Scasper 255*1475Scasper char * 256*1475Scasper mktemp(char *template) 257*1475Scasper { 258*1475Scasper return (libc_mktemps(template, 0)); 259*1475Scasper } 260