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 5*7c4dcc55Scasper * Common Development and Distribution License (the "License"). 6*7c4dcc55Scasper * 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 */ 217c478bd9Sstevel@tonic-gate /* 22*7c4dcc55Scasper * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */ 297c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate /* 337c478bd9Sstevel@tonic-gate * mktemp() expects a string with up to six trailing 'X's. 347c478bd9Sstevel@tonic-gate * These will be overlaid with letters, digits and symbols from 357c478bd9Sstevel@tonic-gate * the portable filename character set. If every combination thus 367c478bd9Sstevel@tonic-gate * inserted leads to an existing file name, the string is shortened 377c478bd9Sstevel@tonic-gate * to length zero and a pointer to a null string is returned. 387c478bd9Sstevel@tonic-gate * 397c478bd9Sstevel@tonic-gate * The guarantee made by mktime() to the caller is that the 407c478bd9Sstevel@tonic-gate * generated file name string will not match the string 417c478bd9Sstevel@tonic-gate * produced by any other concurrent process using mktemp(). 427c478bd9Sstevel@tonic-gate * To guarantee uniqueness across the process-id space, 437c478bd9Sstevel@tonic-gate * the process-id of the caller is encoded into the string. 447c478bd9Sstevel@tonic-gate * To allow repeated calls within the same process to generate 457c478bd9Sstevel@tonic-gate * different strings on each call, a sequence number is encoded 467c478bd9Sstevel@tonic-gate * into the string along with process-id. 477c478bd9Sstevel@tonic-gate * 487c478bd9Sstevel@tonic-gate * The encoding is performed using radix-64 (6 bits per character), 497c478bd9Sstevel@tonic-gate * with 64 characters taken from the portable file name character set. 507c478bd9Sstevel@tonic-gate * This allows the six X's to be a representation of a 36-bit integer 517c478bd9Sstevel@tonic-gate * composed of bit fields: 527c478bd9Sstevel@tonic-gate * ( pid | seq ) 537c478bd9Sstevel@tonic-gate * where the process-id occupies the high-order bits and the sequence 547c478bd9Sstevel@tonic-gate * number occupies the low-order bits. The size of the pid field is 557c478bd9Sstevel@tonic-gate * not fixed at the traditional 15 bits (MAXPID = 30000); the system 567c478bd9Sstevel@tonic-gate * now allows a larger process-id space and MAXPID is obtained from 577c478bd9Sstevel@tonic-gate * the system with a call to sysconf(_SC_MAXPID). 587c478bd9Sstevel@tonic-gate * 597c478bd9Sstevel@tonic-gate * mktime() should fail if fewer than six X's are presented to it. 607c478bd9Sstevel@tonic-gate * However, this has been traditionally accepted and is preserved 617c478bd9Sstevel@tonic-gate * in the present code. The consequence is that the 36-bit integer 627c478bd9Sstevel@tonic-gate * is reduced to a (6*N)-bit integer, where N is the number of X's. 637c478bd9Sstevel@tonic-gate * mktime() fails immediately if the resulting integer is not large 647c478bd9Sstevel@tonic-gate * enough to contain MAXPID. 657c478bd9Sstevel@tonic-gate * 667c478bd9Sstevel@tonic-gate * In an attempt to confuse and thwart hackers, the starting 677c478bd9Sstevel@tonic-gate * sequence number is randomized using the current time. 687c478bd9Sstevel@tonic-gate */ 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate #define XCNT 6 717c478bd9Sstevel@tonic-gate #pragma weak mktemp = _mktemp 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate #include "synonyms.h" 747c478bd9Sstevel@tonic-gate #include "mtlib.h" 757c478bd9Sstevel@tonic-gate #include <sys/types.h> 767c478bd9Sstevel@tonic-gate #include <string.h> 777c478bd9Sstevel@tonic-gate #include <unistd.h> 787c478bd9Sstevel@tonic-gate #include <thread.h> 797c478bd9Sstevel@tonic-gate #include <synch.h> 807c478bd9Sstevel@tonic-gate #include <sys/stat.h> 817c478bd9Sstevel@tonic-gate #include <errno.h> 827c478bd9Sstevel@tonic-gate #include <sys/time.h> 837c478bd9Sstevel@tonic-gate #include <stdlib.h> 847c478bd9Sstevel@tonic-gate #include <stdio.h> 857c478bd9Sstevel@tonic-gate #include <sys/param.h> 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate /* 887c478bd9Sstevel@tonic-gate * 64-bit digits, must be from the POSIX "portable file name character set". 897c478bd9Sstevel@tonic-gate */ 907c478bd9Sstevel@tonic-gate static char 917c478bd9Sstevel@tonic-gate chars[64] = { 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 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 957c478bd9Sstevel@tonic-gate 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 967c478bd9Sstevel@tonic-gate '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '_', 977c478bd9Sstevel@tonic-gate }; 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate /* 1007c478bd9Sstevel@tonic-gate * Find highest one bit set. 1017c478bd9Sstevel@tonic-gate * Returns bit number of highest bit that is set. 1027c478bd9Sstevel@tonic-gate * Low order bit is number 0, high order bit is number 31. 1037c478bd9Sstevel@tonic-gate */ 1047c478bd9Sstevel@tonic-gate static int 1057c478bd9Sstevel@tonic-gate highbit(uint_t i) 1067c478bd9Sstevel@tonic-gate { 1077c478bd9Sstevel@tonic-gate int h = 0; 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate if (i & 0xffff0000) 1107c478bd9Sstevel@tonic-gate h += 16, i >>= 16; 1117c478bd9Sstevel@tonic-gate if (i & 0xff00) 1127c478bd9Sstevel@tonic-gate h += 8, i >>= 8; 1137c478bd9Sstevel@tonic-gate if (i & 0xf0) 1147c478bd9Sstevel@tonic-gate h += 4, i >>= 4; 1157c478bd9Sstevel@tonic-gate if (i & 0xc) 1167c478bd9Sstevel@tonic-gate h += 2, i >>= 2; 1177c478bd9Sstevel@tonic-gate if (i & 0x2) 1187c478bd9Sstevel@tonic-gate h += 1; 1197c478bd9Sstevel@tonic-gate return (h); 1207c478bd9Sstevel@tonic-gate } 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate char * 123*7c4dcc55Scasper libc_mktemps(char *as, int slen) 1247c478bd9Sstevel@tonic-gate { 1257c478bd9Sstevel@tonic-gate /* statics are protected by this static mutex */ 1267c478bd9Sstevel@tonic-gate static mutex_t mktemp_lock = DEFAULTMUTEX; 1277c478bd9Sstevel@tonic-gate static int pidshift = 0; 1287c478bd9Sstevel@tonic-gate static int previous_try = 0; 1297c478bd9Sstevel@tonic-gate static pid_t previous_pid = 0; 1307c478bd9Sstevel@tonic-gate static int previous_xcnt = XCNT; 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate pid_t pid; 1337c478bd9Sstevel@tonic-gate int try; 1347c478bd9Sstevel@tonic-gate int tryshift; 1357c478bd9Sstevel@tonic-gate int max_try; 1367c478bd9Sstevel@tonic-gate char *s; 1377c478bd9Sstevel@tonic-gate char *first_x; 1387c478bd9Sstevel@tonic-gate int len; 1397c478bd9Sstevel@tonic-gate uint_t xcnt; 1407c478bd9Sstevel@tonic-gate struct stat64 buf; 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate if (as == NULL || *as == '\0') /* If the string passed is null then */ 1437c478bd9Sstevel@tonic-gate return (as); /* a pointer to a null string is returned. */ 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate lmutex_lock(&mktemp_lock); 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate pid = getpid(); 1487c478bd9Sstevel@tonic-gate if (pid != previous_pid) { /* first time or first after fork() */ 1497c478bd9Sstevel@tonic-gate /* 1507c478bd9Sstevel@tonic-gate * Randomize the starting sequence number in 1517c478bd9Sstevel@tonic-gate * an attempt to confuse and thwart hackers. 1527c478bd9Sstevel@tonic-gate * Use the low 12 bits of the time in milliseconds. 1537c478bd9Sstevel@tonic-gate */ 1547c478bd9Sstevel@tonic-gate struct timeval tm; 1557c478bd9Sstevel@tonic-gate 1567c478bd9Sstevel@tonic-gate (void) gettimeofday(&tm, NULL); 1577c478bd9Sstevel@tonic-gate previous_try = (tm.tv_sec * 1000 + tm.tv_usec / 1000) & 0xfff; 1587c478bd9Sstevel@tonic-gate previous_pid = pid; 1597c478bd9Sstevel@tonic-gate previous_xcnt = XCNT; 1607c478bd9Sstevel@tonic-gate } 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate /* for all possible values of pid, 0 <= pid < (1 << pidshift) */ 1637c478bd9Sstevel@tonic-gate if (pidshift == 0) /* one-time initialization */ 1647c478bd9Sstevel@tonic-gate pidshift = highbit((uint_t)MAXPID) + 1; 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate /* count the X's */ 1677c478bd9Sstevel@tonic-gate xcnt = 0; 1687c478bd9Sstevel@tonic-gate len = (int)strlen(as); 169*7c4dcc55Scasper if (slen >= len || slen < 0) 170*7c4dcc55Scasper goto fail; 171*7c4dcc55Scasper len -= slen; 1727c478bd9Sstevel@tonic-gate s = as + (len - 1); 1737c478bd9Sstevel@tonic-gate while ((len != 0) && (xcnt < XCNT) && (*s == 'X')) { 1747c478bd9Sstevel@tonic-gate xcnt++; 1757c478bd9Sstevel@tonic-gate len--; 1767c478bd9Sstevel@tonic-gate --s; 1777c478bd9Sstevel@tonic-gate } 1787c478bd9Sstevel@tonic-gate first_x = s + 1; /* Remember pointer to the first X */ 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate /* fail if we don't have enough X's to represent MAXPID */ 1817c478bd9Sstevel@tonic-gate if ((tryshift = xcnt * 6 - pidshift) < 0) { 1827c478bd9Sstevel@tonic-gate /* 1837c478bd9Sstevel@tonic-gate * Some broken programs call mktemp() repeatedly, 1847c478bd9Sstevel@tonic-gate * passing the same string without reinserting the X's. 1857c478bd9Sstevel@tonic-gate * Check to see if this is such a call by testing 1867c478bd9Sstevel@tonic-gate * the trailing characters of the string for a 1877c478bd9Sstevel@tonic-gate * match with the process-id. 1887c478bd9Sstevel@tonic-gate */ 1897c478bd9Sstevel@tonic-gate uint64_t xpid = 0; /* reconstructed pid */ 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate s = as + len; 1927c478bd9Sstevel@tonic-gate for (xcnt = previous_xcnt; xcnt && s > as; xcnt--) { 1937c478bd9Sstevel@tonic-gate int c; 1947c478bd9Sstevel@tonic-gate int i; 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate c = *--s; 1977c478bd9Sstevel@tonic-gate for (i = 0; i < 64; i++) 1987c478bd9Sstevel@tonic-gate if (c == chars[i]) 1997c478bd9Sstevel@tonic-gate break; 2007c478bd9Sstevel@tonic-gate if (i == 64) 2017c478bd9Sstevel@tonic-gate goto fail; 2027c478bd9Sstevel@tonic-gate xpid = xpid * 64 + i; 2037c478bd9Sstevel@tonic-gate } 2047c478bd9Sstevel@tonic-gate xpid >>= (previous_xcnt * 6 - pidshift); 2057c478bd9Sstevel@tonic-gate xpid &= ((1 << pidshift) - 1); 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate if (xpid == pid && 2087c478bd9Sstevel@tonic-gate lstat64(as, &buf) == -1 && errno == ENOENT) { 2097c478bd9Sstevel@tonic-gate lmutex_unlock(&mktemp_lock); 2107c478bd9Sstevel@tonic-gate return (as); 2117c478bd9Sstevel@tonic-gate } 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate goto fail; 2147c478bd9Sstevel@tonic-gate } 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate /* we can try sequence numbers in the range 0 <= try < max_try */ 2177c478bd9Sstevel@tonic-gate max_try = 1 << tryshift; 2187c478bd9Sstevel@tonic-gate if (previous_try >= max_try) 2197c478bd9Sstevel@tonic-gate previous_try = 0; 2207c478bd9Sstevel@tonic-gate 2217c478bd9Sstevel@tonic-gate try = previous_try; 2227c478bd9Sstevel@tonic-gate for (;;) { 2237c478bd9Sstevel@tonic-gate /* num is up to a 36-bit integer ... */ 2247c478bd9Sstevel@tonic-gate uint64_t num = ((uint64_t)pid << tryshift) + (uint64_t)try; 2257c478bd9Sstevel@tonic-gate int i; 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gate /* ... which we represent backwards in base 64 */ 2287c478bd9Sstevel@tonic-gate for (i = 0, s = first_x; i < xcnt; i++) { 2297c478bd9Sstevel@tonic-gate *s++ = chars[num & 077]; 2307c478bd9Sstevel@tonic-gate num >>= 6; 2317c478bd9Sstevel@tonic-gate } 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gate if (lstat64(as, &buf) == -1) { 2347c478bd9Sstevel@tonic-gate if (errno != ENOENT) 2357c478bd9Sstevel@tonic-gate break; /* unrecoverable error */ 2367c478bd9Sstevel@tonic-gate /* remember where we left off for the next call */ 2377c478bd9Sstevel@tonic-gate previous_try = try + 1; 2387c478bd9Sstevel@tonic-gate previous_xcnt = xcnt; 2397c478bd9Sstevel@tonic-gate lmutex_unlock(&mktemp_lock); 2407c478bd9Sstevel@tonic-gate return (as); 2417c478bd9Sstevel@tonic-gate } 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate if (++try == max_try) 2447c478bd9Sstevel@tonic-gate try = 0; 2457c478bd9Sstevel@tonic-gate if (try == previous_try) 2467c478bd9Sstevel@tonic-gate break; 2477c478bd9Sstevel@tonic-gate } 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate fail: 2507c478bd9Sstevel@tonic-gate lmutex_unlock(&mktemp_lock); 2517c478bd9Sstevel@tonic-gate *as = '\0'; 2527c478bd9Sstevel@tonic-gate return (as); 2537c478bd9Sstevel@tonic-gate } 254*7c4dcc55Scasper 255*7c4dcc55Scasper char * 256*7c4dcc55Scasper mktemp(char *template) 257*7c4dcc55Scasper { 258*7c4dcc55Scasper return (libc_mktemps(template, 0)); 259*7c4dcc55Scasper } 260