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