From: Todd C. Miller Date: Wed, 20 Aug 2008 11:40:15 +0000 (+0000) Subject: Use my replacement mkstemp() from the mktemp package. X-Git-Tag: SUDO_1_7_0~94 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=da6ffd067d3cd39bec9adfaab00616afa20da5d1;p=sudo Use my replacement mkstemp() from the mktemp package. --- diff --git a/LICENSE b/LICENSE index 97c722dd8..9d291d959 100644 --- a/LICENSE +++ b/LICENSE @@ -19,8 +19,8 @@ Sudo is distributed under the following ISC-style license: Agency (DARPA) and Air Force Research Laboratory, Air Force Materiel Command, USAF, under agreement number F39502-99-1-0512. -Additionally, fnmatch.c, fnmatch.h, getcwd.c, glob.c, glob.h, mkstemp.c -and snprintf.c bear the following UCB license: +Additionally, fnmatch.c, fnmatch.h, getcwd.c, glob.c, glob.h and snprintf.c +bear the following UCB license: Copyright (c) 1987, 1989, 1990, 1991, 1992, 1993, 1994 The Regents of the University of California. All rights reserved. diff --git a/mkstemp.c b/mkstemp.c index 1ed924a4a..c126383fb 100644 --- a/mkstemp.c +++ b/mkstemp.c @@ -1,44 +1,25 @@ -/* $OpenBSD: mktemp.c,v 1.19 2005/08/08 08:05:36 espie Exp $ */ - /* - * Copyright (c) 2000, 2001, 2005 Todd C. Miller - * Copyright (c) 1987, 1993 - * The Regents of the University of California. All rights reserved. + * Copyright (c) 2001, 2003, 2008 Todd C. Miller * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include "config.h" #include #include -#if defined(TIME_WITH_SYS_TIME) || defined(HAVE_SYS_TIME_H) -# include -#endif -#include #include +#include #include #ifdef HAVE_STDLIB_H # include @@ -61,98 +42,31 @@ int mkstemp(path) char *path; { - char *start, *trv; - struct stat sbuf; - int fd, rval; - pid_t pid; + char *start, *cp; + int fd, r; char *alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; if (*path == '\0') { errno = EINVAL; - return (-1); /* zero length path */ - } - pid = getpid(); - for (trv = path; *trv; ++trv) - ; - --trv; - while (trv >= path && *trv == 'X' && pid != 0) { - *trv-- = (pid % 10) + '0'; - pid /= 10; - } - while (trv >= path && *trv == 'X') { - char c; - - /* assumes pid_t is at least 16 bits */ - pid = (get_random() & 0xffff) % (26 + 26); - c = alphabet[pid]; - *trv-- = c; + return(0); } - start = trv + 1; - /* - * check the target directory; if you have six X's and it - * doesn't exist this runs for a *very* long time. - */ - for (;; --trv) { - if (trv <= path) - break; - if (*trv == '/') { - *trv = '\0'; - rval = stat(path, &sbuf); - *trv = '/'; - if (rval != 0) - return (-1); - if (!S_ISDIR(sbuf.st_mode)) { - errno = ENOTDIR; - return (-1); - } - break; - } - } + for (cp = path; *cp; cp++) + ; + do { + cp--; + } while (cp >= path && *cp == 'X'); + start = cp + 1; for (;;) { - if ((fd = open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0) - return (fd); - if (errno != EEXIST) - return (-1); - - /* tricky little algorithm for backward compatibility */ - for (trv = start;;) { - if (!*trv) - return (-1); - if (*trv == 'Z') - *trv++ = 'a'; - else { - if (isdigit((unsigned char)(*trv))) - *trv = 'a'; - else if (*trv == 'z') /* wrap from z to A */ - *trv = 'A'; - else { -#ifdef HAVE_EBCDIC - switch(*trv) { - case 'i': - *trv = 'j'; - break; - case 'r': - *trv = 's'; - break; - case 'I': - *trv = 'J'; - break; - case 'R': - *trv = 'S'; - break; - default: - ++*trv; - break; - } -#else - ++*trv; -#endif - } - break; - } + for (cp = start; *cp; cp++) { + r = get_random % (26 + 26); + *cp = alphabet[r]; } + + fd = open(path, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR); + if (fd != -1 || errno != EEXIST) + return(fd); } /*NOTREACHED*/ }