1999-10-27 05:42:43 +02:00
|
|
|
/* THIS FILE HAS BEEN MODIFIED FROM THE ORIGINAL OPENBSD SOURCE */
|
|
|
|
/* Changes: Removed mktemp */
|
|
|
|
|
2011-09-23 02:56:29 +02:00
|
|
|
/* $OpenBSD: mktemp.c,v 1.30 2010/03/21 23:09:30 schwarze Exp $ */
|
1999-10-27 05:42:43 +02:00
|
|
|
/*
|
2011-09-23 02:56:29 +02:00
|
|
|
* Copyright (c) 1996-1998, 2008 Theo de Raadt
|
|
|
|
* Copyright (c) 1997, 2008-2009 Todd C. Miller
|
1999-10-27 05:42:43 +02:00
|
|
|
*
|
2011-09-23 02:56:29 +02:00
|
|
|
* 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.
|
1999-10-27 05:42:43 +02:00
|
|
|
*
|
2011-09-23 02:56:29 +02:00
|
|
|
* 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.
|
1999-10-27 05:42:43 +02:00
|
|
|
*/
|
|
|
|
|
2005-11-10 06:18:56 +01:00
|
|
|
/* OPENBSD ORIGINAL: lib/libc/stdio/mktemp.c */
|
|
|
|
|
2000-08-30 00:21:22 +02:00
|
|
|
#include "includes.h"
|
1999-11-19 05:32:34 +01:00
|
|
|
|
2006-03-15 04:42:54 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2011-09-23 02:56:29 +02:00
|
|
|
#include <errno.h>
|
2006-07-10 13:33:04 +02:00
|
|
|
#include <fcntl.h>
|
2011-09-23 02:56:29 +02:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2006-03-15 04:42:54 +01:00
|
|
|
#include <ctype.h>
|
2006-07-24 06:51:00 +02:00
|
|
|
#include <unistd.h>
|
2006-03-15 04:42:54 +01:00
|
|
|
|
2023-03-10 03:27:29 +01:00
|
|
|
#ifdef mkstemp
|
|
|
|
#undef mkstemp
|
|
|
|
#endif
|
2023-03-10 04:46:57 +01:00
|
|
|
int mkstemp(char *);
|
2023-03-10 03:27:29 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* From glibc man page: 'In glibc versions 2.06 and earlier, the file is
|
|
|
|
* created with permissions 0666, that is, read and write for all users.'
|
|
|
|
* Provide a wrapper to make sure the mask is reasonable (POSIX requires
|
|
|
|
* mode 0600, so mask off any other bits).
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
_ssh_mkstemp(char *template)
|
|
|
|
{
|
|
|
|
mode_t mask;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
mask = umask(0177);
|
|
|
|
ret = mkstemp(template);
|
|
|
|
(void)umask(mask);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-10-27 06:46:31 +01:00
|
|
|
#if !defined(HAVE_MKDTEMP)
|
1999-11-19 05:32:34 +01:00
|
|
|
|
2011-09-23 02:56:29 +02:00
|
|
|
#define MKTEMP_NAME 0
|
|
|
|
#define MKTEMP_FILE 1
|
|
|
|
#define MKTEMP_DIR 2
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2011-09-23 02:56:29 +02:00
|
|
|
#define TEMPCHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
|
|
|
#define NUM_CHARS (sizeof(TEMPCHARS) - 1)
|
|
|
|
|
|
|
|
static int
|
|
|
|
mktemp_internal(char *path, int slen, int mode)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2011-09-23 02:56:29 +02:00
|
|
|
char *start, *cp, *ep;
|
|
|
|
const char *tempchars = TEMPCHARS;
|
|
|
|
unsigned int r, tries;
|
|
|
|
struct stat sb;
|
|
|
|
size_t len;
|
1999-10-27 05:42:43 +02:00
|
|
|
int fd;
|
|
|
|
|
2011-09-23 02:56:29 +02:00
|
|
|
len = strlen(path);
|
2011-10-01 10:46:12 +02:00
|
|
|
if (len == 0 || slen < 0 || (size_t)slen >= len) {
|
2011-09-23 02:56:29 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
ep = path + len - slen;
|
|
|
|
|
|
|
|
tries = 1;
|
|
|
|
for (start = ep; start > path && start[-1] == 'X'; start--) {
|
|
|
|
if (tries < INT_MAX / NUM_CHARS)
|
|
|
|
tries *= NUM_CHARS;
|
|
|
|
}
|
|
|
|
tries *= 2;
|
|
|
|
|
|
|
|
do {
|
|
|
|
for (cp = start; cp != ep; cp++) {
|
|
|
|
r = arc4random_uniform(NUM_CHARS);
|
|
|
|
*cp = tempchars[r];
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case MKTEMP_NAME:
|
|
|
|
if (lstat(path, &sb) != 0)
|
|
|
|
return(errno == ENOENT ? 0 : -1);
|
|
|
|
break;
|
|
|
|
case MKTEMP_FILE:
|
|
|
|
fd = open(path, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
|
|
|
|
if (fd != -1 || errno != EEXIST)
|
|
|
|
return(fd);
|
|
|
|
break;
|
|
|
|
case MKTEMP_DIR:
|
|
|
|
if (mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR) == 0)
|
|
|
|
return(0);
|
|
|
|
if (errno != EEXIST)
|
|
|
|
return(-1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (--tries);
|
|
|
|
|
|
|
|
errno = EEXIST;
|
|
|
|
return(-1);
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
2011-09-23 02:56:29 +02:00
|
|
|
#if 0
|
|
|
|
char *_mktemp(char *);
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2011-09-23 02:56:29 +02:00
|
|
|
char *
|
|
|
|
_mktemp(char *path)
|
|
|
|
{
|
|
|
|
if (mktemp_internal(path, 0, MKTEMP_NAME) == -1)
|
|
|
|
return(NULL);
|
|
|
|
return(path);
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
2011-09-23 02:56:29 +02:00
|
|
|
__warn_references(mktemp,
|
|
|
|
"warning: mktemp() possibly used unsafely; consider using mkstemp()");
|
|
|
|
|
1999-10-27 05:42:43 +02:00
|
|
|
char *
|
2011-09-23 02:56:29 +02:00
|
|
|
mktemp(char *path)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2011-09-23 02:56:29 +02:00
|
|
|
return(_mktemp(path));
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
2011-09-23 02:56:29 +02:00
|
|
|
#endif
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2011-09-23 02:56:29 +02:00
|
|
|
int
|
|
|
|
mkstemp(char *path)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2011-09-23 02:56:29 +02:00
|
|
|
return(mktemp_internal(path, 0, MKTEMP_FILE));
|
|
|
|
}
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2011-09-23 02:56:29 +02:00
|
|
|
int
|
|
|
|
mkstemps(char *path, int slen)
|
|
|
|
{
|
|
|
|
return(mktemp_internal(path, slen, MKTEMP_FILE));
|
|
|
|
}
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2011-09-23 02:56:29 +02:00
|
|
|
char *
|
|
|
|
mkdtemp(char *path)
|
|
|
|
{
|
|
|
|
int error;
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2011-09-23 02:56:29 +02:00
|
|
|
error = mktemp_internal(path, 0, MKTEMP_DIR);
|
|
|
|
return(error ? NULL : path);
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
1999-10-28 06:12:54 +02:00
|
|
|
|
2020-10-27 06:46:31 +01:00
|
|
|
#endif /* !defined(HAVE_MKDTEMP) */
|