#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)mktemp.c 8.1 (Berkeley) 6/4/93";
#endif
#include <sys/cdefs.h>
#include "namespace.h"
#include <sys/param.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include "un-namespace.h"
#define ALLOWED_MKOSTEMP_FLAGS (O_APPEND | O_SHLOCK | O_EXLOCK | O_CLOEXEC)
char *_mktemp(char *);
typedef enum {
FTPP_DONE, FTPP_TRY_NEXT, FTPP_ERROR
} find_temp_path_progress_t;
typedef find_temp_path_progress_t (*find_temp_path_action_t)(
char *path, void *ctx, void *result);
static int find_temp_path(char *path, int slen, bool stat_base_dir,
find_temp_path_action_t action, void *action_ctx, void *action_result);
static find_temp_path_progress_t _mkostemps_action(
char *path, void *ctx, void *result);
static find_temp_path_progress_t _mktemp_action(
char *path, void *ctx, void *result);
static find_temp_path_progress_t _mkdtemp_action(
char *path, void *ctx, void *result);
static find_temp_path_progress_t _mkstemp_dprotected_np_action(
char *path, void *ctx, void *result);
static const char padchar[] =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int
mkostemps(char *path, int slen, int oflags)
{
int fd;
if (oflags & ~ALLOWED_MKOSTEMP_FLAGS) {
errno = EINVAL;
return -1;
}
return (find_temp_path(path, slen, TRUE, _mkostemps_action, &oflags, &fd) ? fd : -1);
}
int
mkstemps(char *path, int slen)
{
int fd;
return (find_temp_path(path, slen, TRUE, _mkostemps_action, NULL, &fd) ? fd : -1);
}
int
mkostemp(char *path, int oflags)
{
int fd;
if (oflags & ~ALLOWED_MKOSTEMP_FLAGS) {
errno = EINVAL;
return -1;
}
return (find_temp_path(path, 0, TRUE, _mkostemps_action, &oflags, &fd) ? fd : -1);
}
int
mkstemp(char *path)
{
int fd;
return (find_temp_path(path, 0, TRUE, _mkostemps_action, NULL, &fd) ? fd : -1);
}
char *
mkdtemp(char *path)
{
return (find_temp_path(path, 0, TRUE, _mkdtemp_action, NULL, NULL) ?
path : (char *)NULL);
}
char *
_mktemp(char *path)
{
return (find_temp_path(path, 0, FALSE, _mktemp_action, NULL, NULL) ?
path : (char *)NULL);
}
__warn_references(mktemp,
"warning: mktemp() possibly used unsafely; consider using mkstemp()");
char *
mktemp(char *path)
{
return (_mktemp(path));
}
int
mkstemp_dprotected_np(char *path, int class, int dpflags)
{
int fd;
int ctx[2] = { class, dpflags };
return (find_temp_path(path, 0, TRUE, _mkstemp_dprotected_np_action, &ctx, &fd) ? fd : -1);
}
static int
find_temp_path(char *path, int slen, bool stat_base_dir,
find_temp_path_action_t action, void *action_ctx, void *action_result)
{
char *start, *trv, *suffp, *carryp;
char *pad;
struct stat sbuf;
int rval;
uint32_t rand;
char carrybuf[MAXPATHLEN];
if (slen < 0) {
errno = EINVAL;
return (0);
}
for (trv = path; *trv != '\0'; ++trv)
;
if (trv - path >= MAXPATHLEN) {
errno = ENAMETOOLONG;
return (0);
}
trv -= slen;
suffp = trv;
--trv;
if (trv < path || NULL != strchr(suffp, '/')) {
errno = EINVAL;
return (0);
}
while (trv >= path && *trv == 'X') {
rand = arc4random_uniform(sizeof(padchar) - 1);
*trv-- = padchar[rand];
}
start = trv + 1;
memcpy(carrybuf, start, suffp - start);
if (stat_base_dir) {
for (; trv > path; --trv) {
if (*trv == '/') {
*trv = '\0';
rval = stat(path, &sbuf);
*trv = '/';
if (rval != 0)
return (0);
if (!S_ISDIR(sbuf.st_mode)) {
errno = ENOTDIR;
return (0);
}
break;
}
}
}
for (;;) {
switch (action(path, action_ctx, action_result)) {
case FTPP_DONE:
return (1);
case FTPP_ERROR:
return (0); default:
; }
for (trv = start, carryp = carrybuf;;) {
if (trv == suffp) {
errno = EEXIST;
return (0);
}
pad = strchr(padchar, *trv);
if (pad == NULL) {
errno = EIO;
return (0);
}
*trv = (*++pad == '\0') ? padchar[0] : *pad;
if (*trv == *carryp) {
++trv;
++carryp;
} else {
break;
}
}
}
}
static find_temp_path_progress_t
_mkostemps_action(char *path, void *ctx, void *result)
{
int oflags = (ctx != NULL) ? *((int *) ctx) : 0;
int fd = _open(path, O_CREAT|O_EXCL|O_RDWR|oflags, 0600);
if (fd >= 0) {
*((int *) result) = fd;
return FTPP_DONE;
}
return (errno == EEXIST) ?
FTPP_TRY_NEXT :
FTPP_ERROR; }
static find_temp_path_progress_t
_mktemp_action(char *path, void *ctx __unused, void *result __unused)
{
struct stat sbuf;
if (lstat(path, &sbuf)) {
return (errno == ENOENT) ?
FTPP_DONE : FTPP_ERROR; }
return FTPP_TRY_NEXT;
}
static find_temp_path_progress_t
_mkdtemp_action(char *path, void *ctx __unused, void *result __unused)
{
if (mkdir(path, 0700) == 0)
return FTPP_DONE;
return (errno == EEXIST) ?
FTPP_TRY_NEXT :
FTPP_ERROR; }
static find_temp_path_progress_t
_mkstemp_dprotected_np_action(char *path, void *ctx, void *result)
{
int class = ((int *) ctx)[0];
int dpflags = ((int *) ctx)[1];
int fd = open_dprotected_np(path, O_CREAT|O_EXCL|O_RDWR, class, dpflags, 0600);
if (fd >= 0) {
*((int *) result) = fd;
return FTPP_DONE;
}
return (errno == EEXIST) ?
FTPP_TRY_NEXT :
FTPP_ERROR; }