113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci ***************************************************************************/
2413498266Sopenharmony_ci
2513498266Sopenharmony_ci#include "curl_setup.h"
2613498266Sopenharmony_ci
2713498266Sopenharmony_ci#if !defined(CURL_DISABLE_COOKIES) || !defined(CURL_DISABLE_ALTSVC) ||  \
2813498266Sopenharmony_ci  !defined(CURL_DISABLE_HSTS)
2913498266Sopenharmony_ci
3013498266Sopenharmony_ci#ifdef HAVE_FCNTL_H
3113498266Sopenharmony_ci#include <fcntl.h>
3213498266Sopenharmony_ci#endif
3313498266Sopenharmony_ci
3413498266Sopenharmony_ci#include "urldata.h"
3513498266Sopenharmony_ci#include "rand.h"
3613498266Sopenharmony_ci#include "fopen.h"
3713498266Sopenharmony_ci/* The last 3 #include files should be in this order */
3813498266Sopenharmony_ci#include "curl_printf.h"
3913498266Sopenharmony_ci#include "curl_memory.h"
4013498266Sopenharmony_ci#include "memdebug.h"
4113498266Sopenharmony_ci
4213498266Sopenharmony_ci/*
4313498266Sopenharmony_ci  The dirslash() function breaks a null-terminated pathname string into
4413498266Sopenharmony_ci  directory and filename components then returns the directory component up
4513498266Sopenharmony_ci  to, *AND INCLUDING*, a final '/'.  If there is no directory in the path,
4613498266Sopenharmony_ci  this instead returns a "" string.
4713498266Sopenharmony_ci
4813498266Sopenharmony_ci  This function returns a pointer to malloc'ed memory.
4913498266Sopenharmony_ci
5013498266Sopenharmony_ci  The input path to this function is expected to have a file name part.
5113498266Sopenharmony_ci*/
5213498266Sopenharmony_ci
5313498266Sopenharmony_ci#ifdef _WIN32
5413498266Sopenharmony_ci#define PATHSEP "\\"
5513498266Sopenharmony_ci#define IS_SEP(x) (((x) == '/') || ((x) == '\\'))
5613498266Sopenharmony_ci#elif defined(MSDOS) || defined(__EMX__) || defined(OS2)
5713498266Sopenharmony_ci#define PATHSEP "\\"
5813498266Sopenharmony_ci#define IS_SEP(x) ((x) == '\\')
5913498266Sopenharmony_ci#else
6013498266Sopenharmony_ci#define PATHSEP "/"
6113498266Sopenharmony_ci#define IS_SEP(x) ((x) == '/')
6213498266Sopenharmony_ci#endif
6313498266Sopenharmony_ci
6413498266Sopenharmony_cistatic char *dirslash(const char *path)
6513498266Sopenharmony_ci{
6613498266Sopenharmony_ci  size_t n;
6713498266Sopenharmony_ci  struct dynbuf out;
6813498266Sopenharmony_ci  DEBUGASSERT(path);
6913498266Sopenharmony_ci  Curl_dyn_init(&out, CURL_MAX_INPUT_LENGTH);
7013498266Sopenharmony_ci  n = strlen(path);
7113498266Sopenharmony_ci  if(n) {
7213498266Sopenharmony_ci    /* find the rightmost path separator, if any */
7313498266Sopenharmony_ci    while(n && !IS_SEP(path[n-1]))
7413498266Sopenharmony_ci      --n;
7513498266Sopenharmony_ci    /* skip over all the path separators, if any */
7613498266Sopenharmony_ci    while(n && IS_SEP(path[n-1]))
7713498266Sopenharmony_ci      --n;
7813498266Sopenharmony_ci  }
7913498266Sopenharmony_ci  if(Curl_dyn_addn(&out, path, n))
8013498266Sopenharmony_ci    return NULL;
8113498266Sopenharmony_ci  /* if there was a directory, append a single trailing slash */
8213498266Sopenharmony_ci  if(n && Curl_dyn_addn(&out, PATHSEP, 1))
8313498266Sopenharmony_ci    return NULL;
8413498266Sopenharmony_ci  return Curl_dyn_ptr(&out);
8513498266Sopenharmony_ci}
8613498266Sopenharmony_ci
8713498266Sopenharmony_ci/*
8813498266Sopenharmony_ci * Curl_fopen() opens a file for writing with a temp name, to be renamed
8913498266Sopenharmony_ci * to the final name when completed. If there is an existing file using this
9013498266Sopenharmony_ci * name at the time of the open, this function will clone the mode from that
9113498266Sopenharmony_ci * file.  if 'tempname' is non-NULL, it needs a rename after the file is
9213498266Sopenharmony_ci * written.
9313498266Sopenharmony_ci */
9413498266Sopenharmony_ciCURLcode Curl_fopen(struct Curl_easy *data, const char *filename,
9513498266Sopenharmony_ci                    FILE **fh, char **tempname)
9613498266Sopenharmony_ci{
9713498266Sopenharmony_ci  CURLcode result = CURLE_WRITE_ERROR;
9813498266Sopenharmony_ci  unsigned char randbuf[41];
9913498266Sopenharmony_ci  char *tempstore = NULL;
10013498266Sopenharmony_ci  struct_stat sb;
10113498266Sopenharmony_ci  int fd = -1;
10213498266Sopenharmony_ci  char *dir = NULL;
10313498266Sopenharmony_ci  *tempname = NULL;
10413498266Sopenharmony_ci
10513498266Sopenharmony_ci  *fh = fopen(filename, FOPEN_WRITETEXT);
10613498266Sopenharmony_ci  if(!*fh)
10713498266Sopenharmony_ci    goto fail;
10813498266Sopenharmony_ci  if(fstat(fileno(*fh), &sb) == -1 || !S_ISREG(sb.st_mode)) {
10913498266Sopenharmony_ci    return CURLE_OK;
11013498266Sopenharmony_ci  }
11113498266Sopenharmony_ci  fclose(*fh);
11213498266Sopenharmony_ci  *fh = NULL;
11313498266Sopenharmony_ci
11413498266Sopenharmony_ci  result = Curl_rand_alnum(data, randbuf, sizeof(randbuf));
11513498266Sopenharmony_ci  if(result)
11613498266Sopenharmony_ci    goto fail;
11713498266Sopenharmony_ci
11813498266Sopenharmony_ci  dir = dirslash(filename);
11913498266Sopenharmony_ci  if(dir) {
12013498266Sopenharmony_ci    /* The temp file name should not end up too long for the target file
12113498266Sopenharmony_ci       system */
12213498266Sopenharmony_ci    tempstore = aprintf("%s%s.tmp", dir, randbuf);
12313498266Sopenharmony_ci    free(dir);
12413498266Sopenharmony_ci  }
12513498266Sopenharmony_ci
12613498266Sopenharmony_ci  if(!tempstore) {
12713498266Sopenharmony_ci    result = CURLE_OUT_OF_MEMORY;
12813498266Sopenharmony_ci    goto fail;
12913498266Sopenharmony_ci  }
13013498266Sopenharmony_ci
13113498266Sopenharmony_ci  result = CURLE_WRITE_ERROR;
13213498266Sopenharmony_ci  fd = open(tempstore, O_WRONLY | O_CREAT | O_EXCL, 0600|sb.st_mode);
13313498266Sopenharmony_ci  if(fd == -1)
13413498266Sopenharmony_ci    goto fail;
13513498266Sopenharmony_ci
13613498266Sopenharmony_ci  *fh = fdopen(fd, FOPEN_WRITETEXT);
13713498266Sopenharmony_ci  if(!*fh)
13813498266Sopenharmony_ci    goto fail;
13913498266Sopenharmony_ci
14013498266Sopenharmony_ci  *tempname = tempstore;
14113498266Sopenharmony_ci  return CURLE_OK;
14213498266Sopenharmony_ci
14313498266Sopenharmony_cifail:
14413498266Sopenharmony_ci  if(fd != -1) {
14513498266Sopenharmony_ci    close(fd);
14613498266Sopenharmony_ci    unlink(tempstore);
14713498266Sopenharmony_ci  }
14813498266Sopenharmony_ci
14913498266Sopenharmony_ci  free(tempstore);
15013498266Sopenharmony_ci  return result;
15113498266Sopenharmony_ci}
15213498266Sopenharmony_ci
15313498266Sopenharmony_ci#endif /* ! disabled */
154