/src/curl/lib/curl_fopen.c
Line | Count | Source |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | #include "curl_setup.h" |
25 | | |
26 | | #if !defined(CURL_DISABLE_COOKIES) || !defined(CURL_DISABLE_ALTSVC) || \ |
27 | | !defined(CURL_DISABLE_HSTS) |
28 | | |
29 | | #include "urldata.h" |
30 | | #include "rand.h" |
31 | | #include "curl_fopen.h" |
32 | | |
33 | | /* The dirslash() function breaks a null-terminated pathname string into |
34 | | directory and filename components then returns the directory component up |
35 | | to, *AND INCLUDING*, a final '/'. If there is no directory in the path, |
36 | | this instead returns a "" string. |
37 | | |
38 | | This function returns a pointer to malloc'ed memory. |
39 | | |
40 | | The input path to this function is expected to have a filename part. |
41 | | */ |
42 | | |
43 | | #ifdef _WIN32 |
44 | | #define PATHSEP "\\" |
45 | | #define IS_SEP(x) (((x) == '/') || ((x) == '\\')) |
46 | | #elif defined(MSDOS) || defined(OS2) |
47 | | #define PATHSEP "\\" |
48 | | #define IS_SEP(x) ((x) == '\\') |
49 | | #else |
50 | 0 | #define PATHSEP "/" |
51 | 0 | #define IS_SEP(x) ((x) == '/') |
52 | | #endif |
53 | | |
54 | | static char *dirslash(const char *path) |
55 | 0 | { |
56 | 0 | size_t n; |
57 | 0 | struct dynbuf out; |
58 | 0 | DEBUGASSERT(path); |
59 | 0 | curlx_dyn_init(&out, CURL_MAX_INPUT_LENGTH); |
60 | 0 | n = strlen(path); |
61 | 0 | if(n) { |
62 | | /* find the rightmost path separator, if any */ |
63 | 0 | while(n && !IS_SEP(path[n - 1])) |
64 | 0 | --n; |
65 | | /* skip over all the path separators, if any */ |
66 | 0 | while(n && IS_SEP(path[n - 1])) |
67 | 0 | --n; |
68 | 0 | } |
69 | 0 | if(curlx_dyn_addn(&out, path, n)) |
70 | 0 | return NULL; |
71 | | /* if there was a directory, append a single trailing slash */ |
72 | 0 | if(n && curlx_dyn_addn(&out, PATHSEP, 1)) |
73 | 0 | return NULL; |
74 | 0 | return curlx_dyn_ptr(&out); |
75 | 0 | } |
76 | | |
77 | | /* |
78 | | * Curl_fopen() opens a file for writing with a temp name, to be renamed |
79 | | * to the final name when completed. If there is an existing file using this |
80 | | * name at the time of the open, this function will clone the mode from that |
81 | | * file. if 'tempname' is non-NULL, it needs a rename after the file is |
82 | | * written. |
83 | | */ |
84 | | CURLcode Curl_fopen(struct Curl_easy *data, const char *filename, |
85 | | FILE **fh, char **tempname) |
86 | 0 | { |
87 | 0 | CURLcode result = CURLE_WRITE_ERROR; |
88 | 0 | unsigned char randbuf[41]; |
89 | 0 | char *tempstore = NULL; |
90 | 0 | #ifndef _WIN32 |
91 | 0 | curlx_struct_stat sb; |
92 | 0 | #endif |
93 | 0 | int fd = -1; |
94 | 0 | char *dir = NULL; |
95 | 0 | *tempname = NULL; |
96 | |
|
97 | 0 | #ifndef _WIN32 |
98 | 0 | *fh = curlx_fopen(filename, FOPEN_WRITETEXT); |
99 | 0 | if(!*fh) |
100 | 0 | goto fail; |
101 | 0 | if(curlx_fstat(fileno(*fh), &sb) == -1 || !S_ISREG(sb.st_mode)) { |
102 | 0 | return CURLE_OK; |
103 | 0 | } |
104 | 0 | curlx_fclose(*fh); |
105 | 0 | #ifdef HAVE_GETEUID |
106 | | /* If the existing file is not owned by the user, do not inherit |
107 | | * its permissions at the temp file created below. The permissions |
108 | | * might be unsuitable for holding user private data. */ |
109 | 0 | if(sb.st_uid != geteuid()) |
110 | 0 | sb.st_mode = 0; |
111 | 0 | #endif |
112 | 0 | #endif /* !_WIN32 */ |
113 | 0 | *fh = NULL; |
114 | |
|
115 | 0 | result = Curl_rand_alnum(data, randbuf, sizeof(randbuf)); |
116 | 0 | if(result) |
117 | 0 | goto fail; |
118 | | |
119 | 0 | dir = dirslash(filename); |
120 | 0 | if(dir) { |
121 | | /* The temp filename should not end up too long for the target file |
122 | | system */ |
123 | 0 | tempstore = curl_maprintf("%s%s.tmp", dir, randbuf); |
124 | 0 | curlx_free(dir); |
125 | 0 | } |
126 | |
|
127 | 0 | if(!tempstore) { |
128 | 0 | result = CURLE_OUT_OF_MEMORY; |
129 | 0 | goto fail; |
130 | 0 | } |
131 | | |
132 | 0 | result = CURLE_WRITE_ERROR; |
133 | | #ifdef _WIN32 |
134 | | fd = curlx_open(tempstore, _O_WRONLY | _O_CREAT | _O_EXCL, |
135 | | _S_IREAD | _S_IWRITE); |
136 | | #elif (defined(ANDROID) || defined(__ANDROID__)) && \ |
137 | | (defined(__i386__) || defined(__arm__)) |
138 | | fd = curlx_open(tempstore, O_WRONLY | O_CREAT | O_EXCL, |
139 | | (mode_t)(S_IRUSR | S_IWUSR | sb.st_mode)); |
140 | | #else |
141 | 0 | fd = curlx_open(tempstore, O_WRONLY | O_CREAT | O_EXCL, |
142 | 0 | S_IRUSR | S_IWUSR | sb.st_mode); |
143 | 0 | #endif |
144 | 0 | if(fd == -1) |
145 | 0 | goto fail; |
146 | | |
147 | 0 | *fh = curlx_fdopen(fd, FOPEN_WRITETEXT); |
148 | 0 | if(!*fh) |
149 | 0 | goto fail; |
150 | | |
151 | 0 | *tempname = tempstore; |
152 | 0 | return CURLE_OK; |
153 | | |
154 | 0 | fail: |
155 | 0 | if(fd != -1) { |
156 | 0 | curlx_close(fd); |
157 | 0 | unlink(tempstore); |
158 | 0 | } |
159 | |
|
160 | 0 | curlx_free(tempstore); |
161 | 0 | return result; |
162 | 0 | } |
163 | | |
164 | | #endif /* !disabled */ |