1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 6 |
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 277841 2009-03-26 20:02:53Z felipe $ */
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 "php_link.h"
51 : #include "ext/standard/file.h"
52 : #include "php_string.h"
53 :
54 : /* {{{ proto string readlink(string filename) U
55 : Return the target of a symbolic link */
56 : PHP_FUNCTION(readlink)
57 52 : {
58 : zval **pp_link;
59 : char *link;
60 : UChar *target;
61 : int link_len, target_len;
62 : char buff[MAXPATHLEN];
63 : int ret;
64 :
65 52 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &pp_link) == FAILURE ||
66 : php_stream_path_param_encode(pp_link, &link, &link_len, REPORT_ERRORS, FG(default_context)) == FAILURE) {
67 2 : return;
68 : }
69 :
70 50 : if (php_check_open_basedir(link TSRMLS_CC)) {
71 6 : RETURN_FALSE;
72 : }
73 :
74 44 : ret = readlink(link, buff, MAXPATHLEN-1);
75 :
76 44 : if (ret == -1) {
77 30 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno));
78 30 : RETURN_FALSE;
79 : }
80 : /* Append NULL to the end of the string */
81 14 : buff[ret] = '\0';
82 :
83 14 : if (SUCCESS == php_stream_path_decode(NULL, &target, &target_len, buff, strlen(buff), REPORT_ERRORS, FG(default_context))) {
84 14 : RETURN_UNICODEL(target, target_len, 0);
85 : } else {
86 0 : RETURN_FALSE;
87 : }
88 : }
89 : /* }}} */
90 :
91 : /* {{{ proto int linkinfo(string filename) U
92 : Returns the st_dev field of the UNIX C stat structure describing the link */
93 : PHP_FUNCTION(linkinfo)
94 31 : {
95 : zval **pp_link;
96 : char *link;
97 : int link_len;
98 : struct stat sb;
99 : int ret;
100 :
101 31 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &pp_link) == FAILURE ||
102 : php_stream_path_param_encode(pp_link, &link, &link_len, REPORT_ERRORS, FG(default_context)) == FAILURE) {
103 2 : return;
104 : }
105 :
106 29 : ret = VCWD_LSTAT(link, &sb);
107 29 : if (ret == -1) {
108 9 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno));
109 9 : RETURN_LONG(-1L);
110 : }
111 :
112 20 : RETURN_LONG((long) sb.st_dev);
113 : }
114 : /* }}} */
115 :
116 : /* {{{ proto int symlink(string target, string link) U
117 : Create a symbolic link */
118 : PHP_FUNCTION(symlink)
119 177 : {
120 : zval **pp_topath, **pp_frompath;
121 : char *topath, *frompath;
122 : int topath_len, frompath_len;
123 : int ret;
124 : char source_p[MAXPATHLEN];
125 : char dest_p[MAXPATHLEN];
126 : char dirname[MAXPATHLEN];
127 : size_t len;
128 :
129 :
130 177 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ZZ", &pp_topath, &pp_frompath) == FAILURE ||
131 : php_stream_path_param_encode(pp_topath, &topath, &topath_len, REPORT_ERRORS, FG(default_context)) == FAILURE ||
132 : php_stream_path_param_encode(pp_frompath, &frompath, &frompath_len, REPORT_ERRORS, FG(default_context)) == FAILURE) {
133 2 : return;
134 : }
135 :
136 175 : if (!expand_filepath(frompath, source_p TSRMLS_CC)) {
137 3 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "No such file or directory");
138 3 : RETURN_FALSE;
139 : }
140 :
141 172 : memcpy(dirname, source_p, sizeof(source_p));
142 172 : len = php_dirname(dirname, strlen(dirname));
143 :
144 172 : if (!expand_filepath_ex(topath, dest_p, dirname, len TSRMLS_CC)) {
145 3 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "No such file or directory");
146 3 : RETURN_FALSE;
147 : }
148 :
149 169 : if (php_stream_locate_url_wrapper(source_p, NULL, STREAM_LOCATE_WRAPPERS_ONLY TSRMLS_CC) ||
150 : php_stream_locate_url_wrapper(dest_p, NULL, STREAM_LOCATE_WRAPPERS_ONLY TSRMLS_CC) )
151 : {
152 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to symlink to a URL");
153 0 : RETURN_FALSE;
154 : }
155 :
156 169 : if (php_check_open_basedir(dest_p TSRMLS_CC)) {
157 5 : RETURN_FALSE;
158 : }
159 :
160 164 : 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 158 : ret = symlink(topath, source_p);
168 :
169 158 : if (ret == -1) {
170 8 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno));
171 8 : RETURN_FALSE;
172 : }
173 :
174 150 : RETURN_TRUE;
175 : }
176 : /* }}} */
177 :
178 : /* {{{ proto int link(string target, string link) U
179 : Create a hard link */
180 : PHP_FUNCTION(link)
181 131 : {
182 : zval **pp_topath, **pp_frompath;
183 : char *topath, *frompath;
184 : int topath_len, frompath_len;
185 : int ret;
186 : char source_p[MAXPATHLEN];
187 : char dest_p[MAXPATHLEN];
188 :
189 131 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ZZ", &pp_topath, &pp_frompath) == FAILURE ||
190 : php_stream_path_param_encode(pp_topath, &topath, &topath_len, REPORT_ERRORS, FG(default_context)) == FAILURE ||
191 : php_stream_path_param_encode(pp_frompath, &frompath, &frompath_len, REPORT_ERRORS, FG(default_context)) == FAILURE) {
192 2 : return;
193 : }
194 :
195 129 : if (!expand_filepath(frompath, source_p TSRMLS_CC) || !expand_filepath(topath, dest_p TSRMLS_CC)) {
196 6 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "No such file or directory");
197 6 : RETURN_FALSE;
198 : }
199 :
200 123 : if (php_stream_locate_url_wrapper(source_p, NULL, STREAM_LOCATE_WRAPPERS_ONLY TSRMLS_CC) ||
201 : php_stream_locate_url_wrapper(dest_p, NULL, STREAM_LOCATE_WRAPPERS_ONLY TSRMLS_CC) )
202 : {
203 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to link to a URL");
204 0 : RETURN_FALSE;
205 : }
206 :
207 123 : if (php_check_open_basedir(dest_p TSRMLS_CC)) {
208 5 : RETURN_FALSE;
209 : }
210 :
211 118 : if (php_check_open_basedir(source_p TSRMLS_CC)) {
212 4 : RETURN_FALSE;
213 : }
214 :
215 : #ifndef ZTS
216 114 : ret = link(topath, frompath);
217 : #else
218 : ret = link(dest_p, source_p);
219 : #endif
220 114 : if (ret == -1) {
221 10 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno));
222 10 : RETURN_FALSE;
223 : }
224 :
225 104 : RETURN_TRUE;
226 : }
227 : /* }}} */
228 :
229 : #endif
230 :
231 : /*
232 : * Local variables:
233 : * tab-width: 4
234 : * c-basic-offset: 4
235 : * End:
236 : * vim600: noet sw=4 ts=4 fdm=marker
237 : * vim<600: noet sw=4 ts=4
238 : */
|