Coverage Report

Created: 2022-02-19 20:31

/src/php-src/ext/standard/link.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
   +----------------------------------------------------------------------+
3
   | Copyright (c) The PHP Group                                          |
4
   +----------------------------------------------------------------------+
5
   | This source file is subject to version 3.01 of the PHP license,      |
6
   | that is bundled with this package in the file LICENSE, and is        |
7
   | available through the world-wide-web at the following url:           |
8
   | http://www.php.net/license/3_01.txt                                  |
9
   | If you did not receive a copy of the PHP license and are unable to   |
10
   | obtain it through the world-wide-web, please send a note to          |
11
   | license@php.net so we can mail you a copy immediately.               |
12
   +----------------------------------------------------------------------+
13
   | Author:                                                              |
14
   +----------------------------------------------------------------------+
15
 */
16
17
#include "php.h"
18
#include "php_filestat.h"
19
#include "php_globals.h"
20
21
#if defined(HAVE_SYMLINK) || defined(PHP_WIN32)
22
23
#ifdef PHP_WIN32
24
#include <WinBase.h>
25
#endif
26
27
#include <stdlib.h>
28
#if HAVE_UNISTD_H
29
#include <unistd.h>
30
#endif
31
#ifndef PHP_WIN32
32
#include <sys/stat.h>
33
#endif
34
#include <string.h>
35
#if HAVE_PWD_H
36
#ifdef PHP_WIN32
37
#include "win32/pwd.h"
38
#else
39
#include <pwd.h>
40
#endif
41
#endif
42
#if HAVE_GRP_H
43
#ifdef PHP_WIN32
44
#include "win32/grp.h"
45
#else
46
#include <grp.h>
47
#endif
48
#endif
49
#include <errno.h>
50
#include <ctype.h>
51
52
#include "php_string.h"
53
54
#ifndef VOLUME_NAME_NT
55
#define VOLUME_NAME_NT 0x2
56
#endif
57
58
#ifndef VOLUME_NAME_DOS
59
#define VOLUME_NAME_DOS 0x0
60
#endif
61
62
/* {{{ Return the target of a symbolic link */
63
PHP_FUNCTION(readlink)
64
0
{
65
0
  char *link;
66
0
  size_t link_len;
67
0
  char buff[MAXPATHLEN];
68
0
  ssize_t ret;
69
70
0
  ZEND_PARSE_PARAMETERS_START(1, 1)
71
0
    Z_PARAM_PATH(link, link_len)
72
0
  ZEND_PARSE_PARAMETERS_END();
73
74
0
  if (php_check_open_basedir(link)) {
75
0
    RETURN_FALSE;
76
0
  }
77
78
0
  ret = php_sys_readlink(link, buff, MAXPATHLEN-1);
79
80
0
  if (ret == -1) {
81
#ifdef PHP_WIN32
82
    php_error_docref(NULL, E_WARNING, "readlink failed to read the symbolic link (%s), error %d)", link, GetLastError());
83
#else
84
0
    php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
85
0
#endif
86
0
    RETURN_FALSE;
87
0
  }
88
  /* Append NULL to the end of the string */
89
0
  buff[ret] = '\0';
90
91
0
  RETURN_STRINGL(buff, ret);
92
0
}
93
/* }}} */
94
95
/* {{{ Returns the st_dev field of the UNIX C stat structure describing the link */
96
PHP_FUNCTION(linkinfo)
97
0
{
98
0
  char *link;
99
0
  char *dirname;
100
0
  size_t link_len;
101
0
  zend_stat_t sb;
102
0
  int ret;
103
104
0
  ZEND_PARSE_PARAMETERS_START(1, 1)
105
0
    Z_PARAM_PATH(link, link_len)
106
0
  ZEND_PARSE_PARAMETERS_END();
107
108
0
  dirname = estrndup(link, link_len);
109
0
  php_dirname(dirname, link_len);
110
111
0
  if (php_check_open_basedir(dirname)) {
112
0
    efree(dirname);
113
0
    RETURN_FALSE;
114
0
  }
115
116
0
  ret = VCWD_LSTAT(link, &sb);
117
0
  if (ret == -1) {
118
0
    php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
119
0
    efree(dirname);
120
0
    RETURN_LONG(Z_L(-1));
121
0
  }
122
123
0
  efree(dirname);
124
0
  RETURN_LONG((zend_long) sb.st_dev);
125
0
}
126
/* }}} */
127
128
/* {{{ Create a symbolic link */
129
PHP_FUNCTION(symlink)
130
0
{
131
0
  char *topath, *frompath;
132
0
  size_t topath_len, frompath_len;
133
0
  int ret;
134
0
  char source_p[MAXPATHLEN];
135
0
  char dest_p[MAXPATHLEN];
136
0
  char dirname[MAXPATHLEN];
137
0
  size_t len;
138
139
0
  ZEND_PARSE_PARAMETERS_START(2, 2)
140
0
    Z_PARAM_PATH(topath, topath_len)
141
0
    Z_PARAM_PATH(frompath, frompath_len)
142
0
  ZEND_PARSE_PARAMETERS_END();
143
144
0
  if (!expand_filepath(frompath, source_p)) {
145
0
    php_error_docref(NULL, E_WARNING, "No such file or directory");
146
0
    RETURN_FALSE;
147
0
  }
148
149
0
  memcpy(dirname, source_p, sizeof(source_p));
150
0
  len = php_dirname(dirname, strlen(dirname));
151
152
0
  if (!expand_filepath_ex(topath, dest_p, dirname, len)) {
153
0
    php_error_docref(NULL, E_WARNING, "No such file or directory");
154
0
    RETURN_FALSE;
155
0
  }
156
157
0
  if (php_stream_locate_url_wrapper(source_p, NULL, STREAM_LOCATE_WRAPPERS_ONLY) ||
158
0
    php_stream_locate_url_wrapper(dest_p, NULL, STREAM_LOCATE_WRAPPERS_ONLY) )
159
0
  {
160
0
    php_error_docref(NULL, E_WARNING, "Unable to symlink to a URL");
161
0
    RETURN_FALSE;
162
0
  }
163
164
0
  if (php_check_open_basedir(dest_p)) {
165
0
    RETURN_FALSE;
166
0
  }
167
168
0
  if (php_check_open_basedir(source_p)) {
169
0
    RETURN_FALSE;
170
0
  }
171
172
  /* For the source, an expanded path must be used (in ZTS an other thread could have changed the CWD).
173
   * For the target the exact string given by the user must be used, relative or not, existing or not.
174
   * The target is relative to the link itself, not to the CWD. */
175
0
  ret = php_sys_symlink(topath, source_p);
176
177
0
  if (ret == -1) {
178
0
    php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
179
0
    RETURN_FALSE;
180
0
  }
181
182
0
  RETURN_TRUE;
183
0
}
184
/* }}} */
185
186
/* {{{ Create a hard link */
187
PHP_FUNCTION(link)
188
0
{
189
0
  char *topath, *frompath;
190
0
  size_t topath_len, frompath_len;
191
0
  int ret;
192
0
  char source_p[MAXPATHLEN];
193
0
  char dest_p[MAXPATHLEN];
194
195
0
  ZEND_PARSE_PARAMETERS_START(2, 2)
196
0
    Z_PARAM_PATH(topath, topath_len)
197
0
    Z_PARAM_PATH(frompath, frompath_len)
198
0
  ZEND_PARSE_PARAMETERS_END();
199
200
0
  if (!expand_filepath(frompath, source_p) || !expand_filepath(topath, dest_p)) {
201
0
    php_error_docref(NULL, E_WARNING, "No such file or directory");
202
0
    RETURN_FALSE;
203
0
  }
204
205
0
  if (php_stream_locate_url_wrapper(source_p, NULL, STREAM_LOCATE_WRAPPERS_ONLY) ||
206
0
    php_stream_locate_url_wrapper(dest_p, NULL, STREAM_LOCATE_WRAPPERS_ONLY) )
207
0
  {
208
0
    php_error_docref(NULL, E_WARNING, "Unable to link to a URL");
209
0
    RETURN_FALSE;
210
0
  }
211
212
0
  if (php_check_open_basedir(dest_p)) {
213
0
    RETURN_FALSE;
214
0
  }
215
216
0
  if (php_check_open_basedir(source_p)) {
217
0
    RETURN_FALSE;
218
0
  }
219
220
0
#ifndef ZTS
221
0
  ret = php_sys_link(topath, frompath);
222
#else
223
  ret = php_sys_link(dest_p, source_p);
224
#endif
225
0
  if (ret == -1) {
226
0
    php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
227
0
    RETURN_FALSE;
228
0
  }
229
230
0
  RETURN_TRUE;
231
0
}
232
/* }}} */
233
234
#endif