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 272370 2008-12-31 11:15: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 : char *link;
59 : int link_len;
60 : char buff[MAXPATHLEN];
61 : int ret;
62 :
63 52 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &link, &link_len) == FAILURE) {
64 3 : return;
65 : }
66 :
67 49 : if (PG(safe_mode) && !php_checkuid(link, NULL, CHECKUID_CHECK_FILE_AND_DIR)) {
68 0 : RETURN_FALSE;
69 : }
70 :
71 49 : if (php_check_open_basedir(link TSRMLS_CC)) {
72 6 : RETURN_FALSE;
73 : }
74 :
75 43 : ret = readlink(link, buff, MAXPATHLEN-1);
76 :
77 43 : if (ret == -1) {
78 29 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno));
79 29 : 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 : char *link;
93 : int link_len;
94 : struct stat sb;
95 : int ret;
96 :
97 31 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &link, &link_len) == FAILURE) {
98 2 : return;
99 : }
100 :
101 29 : ret = VCWD_LSTAT(link, &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 177 : {
115 : char *topath, *frompath;
116 : int topath_len, frompath_len;
117 : int ret;
118 : char source_p[MAXPATHLEN];
119 : char dest_p[MAXPATHLEN];
120 : char dirname[MAXPATHLEN];
121 : size_t len;
122 :
123 177 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &topath, &topath_len, &frompath, &frompath_len) == FAILURE) {
124 2 : return;
125 : }
126 :
127 175 : if (!expand_filepath(frompath, source_p TSRMLS_CC)) {
128 3 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "No such file or directory");
129 3 : RETURN_FALSE;
130 : }
131 :
132 172 : memcpy(dirname, source_p, sizeof(source_p));
133 172 : len = php_dirname(dirname, strlen(dirname));
134 :
135 172 : if (!expand_filepath_ex(topath, dest_p, dirname, len TSRMLS_CC)) {
136 3 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "No such file or directory");
137 3 : RETURN_FALSE;
138 : }
139 :
140 169 : if (php_stream_locate_url_wrapper(source_p, NULL, STREAM_LOCATE_WRAPPERS_ONLY TSRMLS_CC) ||
141 : php_stream_locate_url_wrapper(dest_p, NULL, STREAM_LOCATE_WRAPPERS_ONLY TSRMLS_CC) )
142 : {
143 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to symlink to a URL");
144 0 : RETURN_FALSE;
145 : }
146 :
147 169 : if (PG(safe_mode) && !php_checkuid(dest_p, NULL, CHECKUID_CHECK_FILE_AND_DIR)) {
148 0 : RETURN_FALSE;
149 : }
150 :
151 169 : if (PG(safe_mode) && !php_checkuid(source_p, NULL, CHECKUID_CHECK_FILE_AND_DIR)) {
152 0 : RETURN_FALSE;
153 : }
154 :
155 169 : if (php_check_open_basedir(dest_p TSRMLS_CC)) {
156 5 : RETURN_FALSE;
157 : }
158 :
159 164 : if (php_check_open_basedir(source_p TSRMLS_CC)) {
160 6 : RETURN_FALSE;
161 : }
162 :
163 : /* For the source, an expanded path must be used (in ZTS an other thread could have changed the CWD).
164 : * For the target the exact string given by the user must be used, relative or not, existing or not.
165 : * The target is relative to the link itself, not to the CWD. */
166 158 : ret = symlink(topath, source_p);
167 :
168 158 : if (ret == -1) {
169 8 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno));
170 8 : RETURN_FALSE;
171 : }
172 :
173 150 : RETURN_TRUE;
174 : }
175 : /* }}} */
176 :
177 : /* {{{ proto int link(string target, string link)
178 : Create a hard link */
179 : PHP_FUNCTION(link)
180 131 : {
181 : char *topath, *frompath;
182 : int topath_len, frompath_len;
183 : int ret;
184 : char source_p[MAXPATHLEN];
185 : char dest_p[MAXPATHLEN];
186 :
187 131 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &topath, &topath_len, &frompath, &frompath_len) == FAILURE) {
188 2 : return;
189 : }
190 :
191 129 : if (!expand_filepath(frompath, source_p TSRMLS_CC) || !expand_filepath(topath, dest_p TSRMLS_CC)) {
192 6 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "No such file or directory");
193 6 : RETURN_FALSE;
194 : }
195 :
196 123 : if (php_stream_locate_url_wrapper(source_p, NULL, STREAM_LOCATE_WRAPPERS_ONLY TSRMLS_CC) ||
197 : php_stream_locate_url_wrapper(dest_p, NULL, STREAM_LOCATE_WRAPPERS_ONLY TSRMLS_CC) )
198 : {
199 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to link to a URL");
200 0 : RETURN_FALSE;
201 : }
202 :
203 123 : if (PG(safe_mode) && !php_checkuid(dest_p, NULL, CHECKUID_CHECK_FILE_AND_DIR)) {
204 0 : RETURN_FALSE;
205 : }
206 :
207 123 : if (PG(safe_mode) && !php_checkuid(source_p, NULL, CHECKUID_CHECK_FILE_AND_DIR)) {
208 0 : RETURN_FALSE;
209 : }
210 :
211 123 : if (php_check_open_basedir(dest_p TSRMLS_CC)) {
212 5 : RETURN_FALSE;
213 : }
214 :
215 118 : if (php_check_open_basedir(source_p TSRMLS_CC)) {
216 4 : RETURN_FALSE;
217 : }
218 :
219 : #ifndef ZTS
220 114 : ret = link(topath, frompath);
221 : #else
222 : ret = link(dest_p, source_p);
223 : #endif
224 114 : if (ret == -1) {
225 10 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno));
226 10 : RETURN_FALSE;
227 : }
228 :
229 104 : RETURN_TRUE;
230 : }
231 : /* }}} */
232 :
233 : #endif
234 :
235 : /*
236 : * Local variables:
237 : * tab-width: 4
238 : * c-basic-offset: 4
239 : * End:
240 : * vim600: noet sw=4 ts=4 fdm=marker
241 : * vim<600: noet sw=4 ts=4
242 : */
|