├── .gitignore
├── LICENSE
├── README.md
├── config.m4
├── package.xml
├── php_smbclient.h
├── phpunit.xml.dist
├── smb_streams.c
├── smbclient.c
├── smbclient.stub.php
├── smbclient_arginfo.h
├── smbclient_legacy_arginfo.h
└── tests
├── ClosedirTest.php
├── CreateTest.php
├── GetxattrTest.php
├── LseekTest.php
├── OpendirTest.php
├── OptionsTest.php
├── ReadTest.php
├── ReaddirTest.php
├── RenameTest.php
├── StateFreeTest.php
├── StateInitTest.php
├── StateNewTest.php
├── StreamsTest.php
├── VersionTest.php
├── VfsTest.php
├── WriteTest.php
└── setup-share.sh
/.gitignore:
--------------------------------------------------------------------------------
1 | # Local config
2 | phpunit.xml
3 |
4 | # Object files
5 | *.o
6 | *.lo
7 |
8 | # Libraries
9 | *.lib
10 | *.a
11 | *.la
12 |
13 | # Shared objects (inc. Windows DLLs)
14 | *.dll
15 | *.so
16 | *.so.*
17 | *.dylib
18 |
19 | # Executables
20 | *.exe
21 | *.out
22 | *.app
23 |
24 | # autotools
25 | .deps
26 | .libs
27 | config.cache
28 | config.guess
29 | config.h
30 | config.h.in
31 | config.h.in~
32 | config.log
33 | config.nice
34 | config.status
35 | config.sub
36 | configure
37 | configure.in
38 | configure.ac
39 | conftest
40 | conftest.c
41 | Makefile
42 | Makefile.fragments
43 | Makefile.global
44 | Makefile.objects
45 | acinclude.m4
46 | aclocal.m4
47 | autom4te.cache
48 | build
49 | install-sh
50 | libtool
51 | ltmain.sh
52 | ltmain.sh.backup
53 | missing
54 | mkinstalldirs
55 | modules
56 | run-tests.php
57 |
58 | # pecl archive
59 | smbclient-*.tgz
60 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2003, Matthew Sachs
2 | 2009 - 2014, Eduardo Bacchi Kienetz
3 | 2013 - 2015, Alfred Klomp
4 | 2015, Remi Collet
5 | All rights reserved.
6 |
7 | Redistribution and use in source and binary forms, with or without
8 | modification, are permitted provided that the following conditions
9 | are met:
10 |
11 | 1. Redistributions of source code must retain the above copyright
12 | notice, this list of conditions and the following disclaimer.
13 |
14 | 2. Redistributions in binary form must reproduce the above
15 | copyright notice, this list of conditions and the following
16 | disclaimer in the documentation and/or other materials provided
17 | with the distribution.
18 |
19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
20 | CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
21 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
24 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26 | TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
29 | TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30 | THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 | SUCH DAMAGE.
32 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | libsmbclient-php: a PHP wrapper for libsmbclient
2 | ================================================
3 |
4 | libsmbclient-php is a PHP extension that uses Samba's libsmbclient library
5 | to provide Samba related functions to PHP programs.
6 |
7 | Getting started
8 | ---------------
9 |
10 | ### Installation from PECL
11 |
12 | ```sh
13 | pecl install smbclient
14 | ```
15 |
16 |
17 | ### Binary package installation
18 |
19 | Some distributions provide binary packages:
20 |
21 | * RPM for Fedora / RHEL / CentOS: [php-smbclient](https://apps.fedoraproject.org/packages/php-smbclient)
22 | * DEB for Debian: [php-smbclient](https://packages.debian.org/search?keywords=php-smbclient)
23 | * DEB for Ubuntu: [php-smbclient](http://packages.ubuntu.com/search?keywords=php-smbclient)
24 |
25 |
26 | ### Installation from sources
27 |
28 | - Install the required libsmbclient header files, via package libsmbclient-dev (Debian/Ubuntu) or libsmbclient-devel (CentOS/Fedora/Red Hat).
29 |
30 | - Download a [release tarball](https://github.com/eduardok/libsmbclient-php/releases) or check out the source code using git:
31 |
32 | ```sh
33 | git clone git://github.com/eduardok/libsmbclient-php.git
34 | ```
35 |
36 | - phpize it:
37 |
38 | ```sh
39 | cd libsmbclient-php ; phpize
40 | ```
41 |
42 | - Build the module
43 |
44 | ```sh
45 | ./configure
46 | make
47 | ```
48 |
49 | - As root, install the module into the extensions directory:
50 |
51 | ```sh
52 | sudo make install
53 | ```
54 |
55 | - Or for packaging purposes, install to a specific root directory:
56 |
57 | ```sh
58 | make install INSTALL_ROOT=/tmp/smbc
59 | ```
60 |
61 | - Activate libsmbclient-php in php.ini:
62 |
63 | ```sh
64 | extension="smbclient.so"
65 | ```
66 |
67 | Contributions and bug reports
68 | -----------------------------
69 |
70 | If you encounter a bug or want to contribute, please file an [issue](https://github.com/eduardok/libsmbclient-php/issues) on GitHub.
71 | Sending pull requests on GitHub is the preferred method of contributing code.
72 |
73 | ## License
74 |
75 | Since version 0.7.0, libsmbclient-php is licensed under the [BSD 2-clause](http://opensource.org/licenses/BSD-2-Clause) license.
76 | See [Issue #15](https://github.com/eduardok/libsmbclient-php/issues/15) for background.
77 | The full license text can be found in the `LICENSE` file.
78 | Before that, libsmbclient-php was licensed under the [PHP license, version 2.02](http://www.php.net/license/2_02.txt).
79 |
80 | ## PHP interface
81 |
82 | ### URI's
83 |
84 | URI's have the format `smb://[[[workgroup;]user[:password@]]server[/share[/path[/file]]]]`.
85 | They should be urlencoded to escape special characters.
86 | Use PHP's [`rawurlencode`](http://php.net/manual/en/function.rawurlencode.php) function to encode an URI.
87 | If you need to specify a workgroup, username or password, you can either include them in the URI, or specify them when you create a state resource.
88 | Examples of valid URI's:
89 |
90 | ```
91 | smb://server
92 | smb://server/share
93 | smb://user:password@server/share/path/to/file.txt
94 | smb://server/share/Moscow%20is%20written%20%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0.txt
95 | ```
96 |
97 | ### Error handling
98 |
99 | As a low-level extension, libsmbclient-php does not throw exceptions.
100 | Success or failure is communicated the old-fashioned way, by the function's return value.
101 | You should always check if a function returns `false` for failure.
102 | If you really want exceptions, you can build your own high-level layer by translating return values and error codes to appropriate exceptions.
103 |
104 | For errors that occur in the Samba layer, the `smbclient_` functions will generally print a human-readable PHP warning with an interpretation of what went wrong.
105 | The interpretations come from Samba's `libsmbclient.h` header file.
106 | You can suppress the warnings by prefixing the function call with an `@`.
107 | Please don't attempt to parse the warning messages, their wording is not very consistent and likely to change in future versions.
108 |
109 | For some unlikely errors encountered by the extension itself, no warning is printed and the function just returns `false`.
110 |
111 | When an error occurs, you can get the error number with `smbclient_state_errno`.
112 | For errors outside of Samba (such as wrong arguments to the function), its value will be 0, but for errors originating within Samba, it will be a Unix `errno` value straight from the underlying library.
113 | For example, `smbclient_open` may set the error code to `13`, which corresponds with `EACCES`, which means that permission was denied.
114 | Please refer to Samba's `libsmbclient.h` for documentation on which error codes you can expect to see; each function has its own list of things that can go wrong.
115 |
116 | For convenience, here's a non-exhaustive list of popular error codes:
117 |
118 | name | value | description
119 | ---- | ----- | -----------
120 | `EPERM` | 1 | Operation not permitted
121 | `ENOENT` | 2 | No such file or directory
122 | `EBADF` | 9 | Bad file or directory resource
123 | `ENOMEM` | 12 | Out of memory
124 | `EACCES` | 13 | Permission denied
125 | `EBUSY` | 16 | Device or resource busy
126 | `EEXIST` | 17 | Resource exists
127 | `ENOTDIR` | 20 | Not a directory
128 | `EISDIR` | 21 | Is a directory
129 | `EINVAL` | 22 | Invalid argument
130 | `ENOSPC` | 28 | No space left on device
131 | `ENOTEMPTY` | 39 | Directory not empty
132 | `ECONNREFUSED` | 111 | Connection refused (Samba not running?)
133 |
134 | ### smbclient_version
135 |
136 | ```php
137 | string smbclient_version ( )
138 | ```
139 |
140 | Returns libsmbclient-php's own version string.
141 |
142 | ### smbclient_library_version
143 |
144 | ```php
145 | string smbclient_library_version ( )
146 | ```
147 |
148 | Returns libsmbclient's version string, which is the same as the Samba version string.
149 |
150 | ### smbclient_state_new
151 |
152 | ```php
153 | resource smbclient_state_new ( )
154 | ```
155 |
156 | Acquire a new smbclient state.
157 | Returns a state resource on success, or `false` on failure.
158 | The state resource holds persistent data about the current server connection, so that the backend can reuse the existing channel instead of reconnecting for every operation.
159 | The state resource must be passed on to most of the other functions in this extension.
160 | Before using the state resource in other functions, it must be initialized by calling `smbclient_state_init`.
161 | Between creating and initializing the resource, you can set certain options for the connection with `smbclient_option_set`.
162 | The state resource should be released when you're done with it by passing it to `smbclient_state_free` (although PHP will auto-destroy it when it goes out of scope).
163 |
164 | ### smbclient_client_protocols
165 |
166 | ```php
167 | bool smbclient_client_protocols ( resource $state, string $min_protocol = null, string $max_protocol = null )
168 | ```
169 |
170 | Sets the minimum and maximum protocols (client min protocol and client max protocol) for negotiation.
171 | Either can be set to null.
172 | Returns `true` on success, `false` on failure.
173 |
174 | ### smbclient_option_set
175 |
176 | ```php
177 | bool smbclient_option_set ( resource $state, int option, mixed value )
178 | ```
179 |
180 | Sets the value of an option to `libsmbclient`.
181 | Returns `true` if setting the option succeeded, `false` on failure.
182 | This function should be called before calling `smbclient_state_init` on your context.
183 | The second argument should be one of the constants below:
184 |
185 | * `SMBCLIENT_OPT_OPEN_SHAREMODE`
186 |
187 | The share mode to use when opening files.
188 | The value can be one of these constants:
189 | * `SMBCLIENT_SHAREMODE_DENY_DOS`
190 | * `SMBCLIENT_SHAREMODE_DENY_ALL`
191 | * `SMBCLIENT_SHAREMODE_DENY_WRITE`
192 | * `SMBCLIENT_SHAREMODE_DENY_READ`
193 | * `SMBCLIENT_SHAREMODE_DENY_NONE`
194 | * `SMBCLIENT_SHAREMODE_DENY_FCB`
195 |
196 | The default is `SMBCLIENT_SHAREMODE_DENY_NONE`.
197 |
198 | * `SMBCLIENT_OPT_ENCRYPT_LEVEL`
199 |
200 | The encryption level to adopt for the connection.
201 | The value can be one of these constants:
202 | * `SMBCLIENT_ENCRYPTLEVEL_NONE`
203 | * `SMBCLIENT_ENCRYPTLEVEL_REQUEST`
204 | * `SMBCLIENT_ENCRYPTLEVEL_REQUIRE`
205 |
206 | * `SMBCLIENT_OPT_CASE_SENSITIVE`
207 |
208 | Boolean.
209 | What to do when we can't determine from the file system attributes whether the file system is case sensitive.
210 | Assume that the filesystem is case sensitive (`true`), or that it isn't (`false`).
211 | Defaults to `false`, because only really old file systems aren't autodetected, and most of those are case insensitive.
212 |
213 | * `SMBCLIENT_OPT_BROWSE_MAX_LMB_COUNT`
214 |
215 | From how many servers to retrieve the list of workgroups, if you're doing that.
216 | See Samba's `libsmbclient.h` for details.
217 |
218 | * `SMBCLIENT_OPT_URLENCODE_READDIR_ENTRIES`
219 |
220 | Boolean.
221 | Whether the entries returned by `smbclient_readdir` are urlencoded.
222 | Defaults to `false`, the entries are returned "raw".
223 |
224 | * `SMBCLIENT_OPT_USE_KERBEROS`
225 |
226 | Boolean.
227 | Whether to use Kerberos authentication.
228 |
229 | * `SMBCLIENT_OPT_FALLBACK_AFTER_KERBEROS`
230 |
231 | Boolean.
232 | Whether to fall back on regular authentication if Kerberos didn't work out.
233 | The regular username and password given in `smbclient_state_init` will be queried.
234 |
235 | * `SMBCLIENT_OPT_AUTO_ANONYMOUS_LOGIN`
236 |
237 | Boolean.
238 | Whether to automatically select anonymous login.
239 |
240 | * `SMBCLIENT_OPT_USE_CCACHE`
241 |
242 | Boolean.
243 | Whether to use the Winbind cache.
244 |
245 | * `SMBCLIENT_OPT_USE_NT_HASH`
246 |
247 | Boolean.
248 | Whether the password supplied in `smbclient_state_init` is actually an NT hash.
249 | If you set this to `true` and work with NT hashes, you can avoid passing around plaintext passwords.
250 |
251 | The `smbc_getOptionUseNTHash` function is relatively new to libsmbclient (June 2012), so the configure script tests whether your libsmbclient has that symbol, and conditionally activates this option.
252 | If the option is not available, trying to set it will return `false`.
253 |
254 | * `SMBCLIENT_OPT_NETBIOS_NAME`
255 |
256 | String.
257 | The NetBIOS (host) name used for making connections.
258 |
259 | * `SMBCLIENT_OPT_WORKGROUP`
260 |
261 | String.
262 | The workgroup used for making connections.
263 |
264 | * `SMBCLIENT_OPT_USER`
265 |
266 | String.
267 | The username used to make connections.
268 | This appears to be something different from the username given in `smbclient_state_init`, and appears to correspond to the system user running PHP.
269 |
270 | * `SMBCLIENT_OPT_PORT`
271 |
272 | Int.
273 | The TCP port to connect to.
274 | `0` means "use the default".
275 |
276 | The `smbc_setPort` function is relatively new to libsmbclient (April 2013), so the configure script tests whether your libsmbclient has that symbol, and conditionally activates this option.
277 | If the option is not available, trying to set it will return `false`.
278 |
279 | * `SMBCLIENT_OPT_TIMEOUT`
280 |
281 | Int.
282 | The timeout value for connections and responses in milliseconds.
283 |
284 | ### smbclient_option_get
285 |
286 | ```php
287 | mixed smbclient_option_get ( resource $state, int option )
288 | ```
289 |
290 | This is a mirror function of `smbclient_option_set`.
291 | Everything settable is also gettable.
292 | See that function for the description of the available options and their return types/values.
293 | If a given option is not available, this function will return `null` and not `false`, to distinguish it from an option's legitimate `false` value.
294 |
295 | ### smbclient_state_init
296 |
297 | ```php
298 | int smbclient_state_init ( resource $state [, string $workgroup = null [, string $username = null [, string $password = null ] ] ] )
299 | ```
300 |
301 | Initialize the smbclient state resource.
302 | Returns `0` on success, `1` on failure.
303 | Before using the state resource in other functions, it must be initialized.
304 | Workgroup, username and password are optional parameters.
305 | You can specify any of them as `null` or `false` to indicate that the credential is not available.
306 | Such might be the case for anonymous or guest access.
307 |
308 | ### smbclient_state_free
309 |
310 | ```php
311 | bool smbclient_state_free ( resource $state )
312 | ```
313 |
314 | Release the state resource passed to it.
315 | Returns `true` on success, `false` on failure.
316 |
317 | ### smbclient_state_errno
318 |
319 | ```php
320 | int smbclient_state_errno ( resource $state )
321 | ```
322 |
323 | Returns the error number of the last error encountered by libsmbclient.
324 | Returns 0 on failure (invalid resource) or if no error has yet occurred for this resource.
325 | The numbers returned are the standard Posix constants as returned by libsmbclient itself, so check your system's `errno.h` or `man errno` for documentation.
326 |
327 | ### smbclient_opendir
328 |
329 | ```php
330 | resource smbclient_opendir ( resource $state, string $uri )
331 | ```
332 |
333 | Opens the given directory for reading with `smbclient_readdir`.
334 |
335 | Returns either a directory resource, or `false` on failure.
336 | The directory resource should be closed after use with `smbclient_closedir`.
337 |
338 | ### smbclient_readdir
339 |
340 | ```php
341 | array smbclient_readdir ( resource $state, resource $dir )
342 | ```
343 |
344 | Reads the next entry from the given directory resource obtained with `smbclient_opendir`.
345 | Call this in a `while` loop to read all entries in the directory.
346 |
347 | Returns an array with details for the directory entry on success, or `false` on
348 | failure or end-of-file. The returned array has the following structure:
349 |
350 | ```php
351 | array(
352 | 'type' => 'type string',
353 | 'comment' => 'comment string',
354 | 'name' => 'name string'
355 | )
356 | ```
357 |
358 | Comment and name are passed through from libsmbclient.
359 | By default, the name is *not* returned in urlencoded format, it's been decoded for convenience.
360 | You can toggle that by setting the `SMBCLIENT_OPT_URLENCODE_READDIR_ENTRIES` option to `true`.
361 | `type` is one of the following strings:
362 |
363 | * `'workgroup'`
364 | * `'server'`
365 | * `'file share'`
366 | * `'printer share'`
367 | * `'communication share'`
368 | * `'IPC share'`
369 | * `'directory'`
370 | * `'file'`
371 | * `'link'`
372 | * `'unknown'`
373 |
374 | ### smbclient_closedir
375 |
376 | ```php
377 | bool smbclient_closedir ( resource $state, resource $dir )
378 | ```
379 |
380 | Closes a directory resource obtained with `smbclient_opendir`.
381 | Returns `true` on success, `false` on failure.
382 |
383 | ### smbclient_rename
384 |
385 | ```php
386 | bool smbclient_rename ( resource $state_old, string $uri_old, resource $state_new, string $uri_new )
387 | ```
388 |
389 | Renames the old file/directory to the new file/directory.
390 | `$state_old` and `$state_new` refer to the states belonging to the old and new URI's.
391 | Due to a limitation of the underlying library, old and new locations must be on the same share.
392 | Due to the same limitation, `$state_old` and `$state_new` should point to the same resource.
393 | Returns `true` on success, `false` on failure.
394 |
395 | ### smbclient_unlink
396 |
397 | ```php
398 | bool smbclient_unlink ( resource $state, string $uri )
399 | ```
400 |
401 | Unlinks (deletes) the file.
402 | Does not work on directories; to delete those, use `smbclient_rmdir`.
403 | Returns `true` on success, `false` on failure.
404 |
405 | ### smbclient_mkdir
406 |
407 | ```php
408 | bool smbclient_mkdir ( resource $state, string $uri [, int $mask = 0777 ] )
409 | ```
410 |
411 | Creates the given directory.
412 | If `$mask` is given, use that as the creation mask (after subtracting the [umask](http://php.net/manual/en/function.umask.php)).
413 | Support for `$mask` may be absent; libsmbclient notes in its header file that umasks are not supported by SMB servers.
414 | Returns `true` on success, `false` on failure.
415 |
416 | ### smbclient_rmdir
417 |
418 | ```php
419 | bool smbclient_rmdir ( resource $state, string $uri )
420 | ```
421 |
422 | Deletes the given directory if empty.
423 | Returns `true` on success, `false` on failure.
424 |
425 | ### smbclient_stat
426 |
427 | ```php
428 | array smbclient_stat ( resource $state, string $uri )
429 | ```
430 |
431 | Returns information about the given file or directory.
432 | Returns an array with information on success, `false` on failure.
433 | The structure of the return array is the same as [PHP's native `stat`](http://php.net/manual/en/function.stat.php).
434 | See that manual for a complete description.
435 |
436 | ### smbclient_fstat
437 |
438 | ```php
439 | array smbclient_fstat ( resource $state, resource $file )
440 | ```
441 |
442 | Returns information about the given file or directory.
443 | Returns an array with information on success, `false` on failure.
444 | The structure of the return array is the same as [PHP's native `stat`](http://php.net/manual/en/function.stat.php).
445 | See that manual for a complete description.
446 |
447 | ### smbclient_open
448 |
449 | ```php
450 | resource smbclient_open ( resource $state, string $uri, string $mode [, int $mask = 0666 ] )
451 | ```
452 |
453 | Opens a file for reading or writing according to the `$mode` specified.
454 | Applies the creation mask in `$mask` (after subtracting the [umask](http://php.net/manual/en/function.umask.php)) if the file had to be created.
455 | Support for `$mask` may be absent; libsmbclient notes in its header file that umasks are not supported by SMB servers.
456 |
457 | `$mode` is in the same format as the `$mode` argument in [PHP's native `fopen`](http://php.net/manual/en/function.fopen.php).
458 | See that manual for more information.
459 | Summary:
460 |
461 | value | description
462 | ----- | -----------
463 | `'r'` | open read-only, place file pointer at start of file.
464 | `'r+'` | open read-write, place file pointer at start of file.
465 | `'w'` | open write-only, place file pointer at start of file; create file if not exists.
466 | `'w+'` | as above, but open read-write.
467 | `'a'` | open write-only, place file pointer at end of file; create file if not exists.
468 | `'a+'` | as above, but open read-write.
469 | `'x'` | exclusive open for write only; create file only if it doesn't already exist, else return error.
470 | `'x+'` | as above, but open read-write.
471 | `'c'` | open write-only, create if not exists; if it already exists, don't return error. Do not truncate, but place file pointer at start of file.
472 | `'c+'` | as above, but open read-write.
473 |
474 | Returns a file resource on success, or `false` on failure.
475 |
476 | ### smbclient_creat
477 |
478 | ```php
479 | resource smbclient_creat ( resource $state, string $uri [, int $mask = 0666 ] )
480 | ```
481 |
482 | Almost the same as calling `smbclient_open` with mode `'c'`, but will truncate the file to 0 bytes if it already exists.
483 | Opens the file write-only and creates it if it doesn't already exist.
484 |
485 | Returns a file resource on success, or `false` on failure.
486 |
487 | ### smbclient_read
488 |
489 | ```php
490 | string smbclient_read ( resource $state, resource $file, int $bytes )
491 | ```
492 |
493 | Reads data from a file resource obtained through `smbclient_open` or `smbclient_creat`.
494 | Tries to read the amount of bytes given in `$bytes`, but may return less.
495 | Returns a string longer than 0 bytes on success, a string of 0 bytes on end-of-file, or `false` on failure.
496 |
497 | Checking the string length to figure out EOF is primitive, but libsmbclient does not expose an `feof` equivalent.
498 | `strlen` in PHP is relatively efficient because PHP tracks string lengths internally.
499 |
500 | ### smbclient_write
501 |
502 | ```php
503 | int smbclient_write ( resource $state, resource $file, string $data [, int $length ] )
504 | ```
505 |
506 | Writes data to a file resource obtained through `smbclient_open` or `smbclient_creat`.
507 | If `$length` is not specified, write the whole contents of `$data`.
508 | If `$length` is specified, write either the whole content of `$data` or `$length` bytes, whichever is less.
509 | `$length`, if specified, must be larger than 0.
510 | If you want to write zero bytes for some reason, write the empty string and omit `$length`.
511 |
512 | Returns the number of bytes written on success, or `false` on failure.
513 |
514 | ### smbclient_lseek
515 |
516 | ```php
517 | int smbclient_lseek ( resource $state, resource $file, int offset, int whence )
518 | ```
519 |
520 | Places the internal file pointer at the given byte offset.
521 | The `whence` parameter indicates from where to count.
522 | It can take three possible constants, which are the same as for [PHP's native `fseek`](http://php.net/manual/en/function.fseek.php):
523 |
524 | * `SEEK_SET`: set position equal to offset bytes;
525 | * `SEEK_CUR`: set position to current location plus offset;
526 | * `SEEK_END`: set position to end of file plus offset.
527 |
528 | Returns the new file offset as measured from the start of the file on success, `false` on failure.
529 |
530 | ### smbclient_ftruncate
531 |
532 | ```php
533 | bool smbclient_ftruncate ( resource $state, resource $file, int size )
534 | ```
535 |
536 | Truncates the given file to the given file size.
537 | Returns `true` on success, `false` on failure.
538 |
539 | ### smbclient_close
540 |
541 | ```php
542 | bool smbclient_close ( resource $state, resource $file )
543 | ```
544 |
545 | Close a file resource obtained with `smbclient_open` or `smbclient_creat`.
546 | Returns `true` on success, `false` on failure.
547 |
548 | ### smbclient_chmod
549 |
550 | ```php
551 | bool smbclient_chmod ( resource $state, string $uri, int mode )
552 | ```
553 |
554 | Set the DOS attributes for a file.
555 | According to the libsmbclient header file, this function is not implemented.
556 | However, the Samba sources do seem to implement it, and use the following mapping:
557 |
558 | Permission | description
559 | ---------- | -----------
560 | Not u+w, g+w or o+w | File is read-only
561 | u+x | File is archived
562 | g+x | File is system
563 | o+x | File is hidden
564 |
565 | So to set a file to readable and hidden, you would use o+wx, or mode `003`.
566 | This function is a Posix compatibility shim; if you want better control over file attributes, use the more powerful `xattr` functions.
567 |
568 | ### smbclient_utimes
569 |
570 | ```php
571 | bool smbclient_utimes ( resource $state, string $uri [, int $mtime = time() [, int $atime = $mtime ] ] )
572 | ```
573 |
574 | Set the write time and access time for the given file or directory.
575 | These correspond to Unix mtime and atime.
576 | Timestamps are in Unix timestamp format.
577 | Returns `true` on success, `false` on failure.
578 |
579 | Beware of inconsistencies in how Samba stores and retrieves timestamps.
580 | When you change the mtime and atime for a file, then stat the file with `smbclient_stat`, the stat output will indicate that you changed ctime and mtime, in that order, instead.
581 | (This is likely a bug somewhere, but it's hard to pinpoint the cause.)
582 | When you use mount.cifs to mount the share and check the results of this function with the `stat` commandline tool, the `mtime` argument will set both the mtime and ctime, and the `atime` argument will set the atime.
583 | This is a Posix compatibility shim.
584 | Use the more powerful `xattr` functions if you need more control, such as setting the ctime.
585 |
586 | ### smbclient_listxattr
587 |
588 | ```php
589 | array smbclient_listxattr ( resource $state, string $uri )
590 | ```
591 |
592 | This function should, according to Samba documentation, return a list of all names of extended attributes applicable to the given file or directory.
593 | Instead, the function returns an array of the names of all extended attributes known to Samba, regardless of what the filesystem actually supports or which attributes are actually available on the resource.
594 | Since the underlying function always returns a static string without looking, don't take the output as gospel.
595 | It does provide you with a list of attribute names that you can use to fetch individually.
596 | Returns `false` on failure.
597 |
598 | ### smbclient_getxattr
599 |
600 | ```php
601 | string smbclient_getxattr ( resource $state, string $uri, string $key )
602 | ```
603 |
604 | Returns the value of the given extended attribute with name `$key`, or `false` on failure.
605 | The value returned is always a string.
606 |
607 | For example, to get a file's [extended attributes](http://msdn.microsoft.com/en-us/library/cc246322.aspx), query the `system.dos_attr.mode` key.
608 |
609 | ### smbclient_setxattr
610 |
611 | ```php
612 | bool smbclient_setxattr ( resource $state, string $uri, string $key, string $value [, int flags = 0 ] )
613 | ```
614 |
615 | Sets the extended attribute with name `$key` to value `$value`.
616 | For now, see `libsmbclient.h`, the section on `smbc_setxattr`, for details on how to specify keys and values.
617 | `flags` defaults to zero, meaning that the attribute will be created if it does not exist, and replaced if it already exists.
618 | You can also set `flags` to one of these values:
619 |
620 | Constant | description
621 | -------- | -----------
622 | `SMBCLIENT_XATTR_CREATE` | Only create the attribute: fail with `EEXIST` if it already exists
623 | `SMBCLIENT_XATTR_REPLACE` | Only replace the attribute: fail with `ENOATTR` if it does not exist
624 |
625 | Returns `true` on success, `false` on failure.
626 |
627 | ### smbclient_removexattr
628 |
629 | ```php
630 | bool smbclient_removexattr ( resource $state, string $uri, string $key )
631 | ```
632 |
633 | Removes the extended attribute with name `$key` from the file or directory pointed to by the URI.
634 | Returns `true` on success, `false` on failure.
635 |
636 | ### smbclient_statvfs
637 |
638 | ```php
639 | array smbclient_statvfs ( resource $state, string $uri )
640 | ```
641 |
642 | Returns an array with file system statistics for the given URI, or `false` on failure.
643 | The array contains the keys listed below, each with an integer value.
644 | See the manpage for the Unix `statvfs` function for more information on how to interpret the values.
645 |
646 | * `bsize`: file system block size;
647 | * `frsize`: fragment size;
648 | * `blocks`: size of filesystem in `frsize` units;
649 | * `bfree`: number of free blocks;
650 | * `bavail`: number of free blocks for unprivileged users;
651 | * `files`: number of inodes;
652 | * `ffree`: number of free inodes;
653 | * `favail`: number of free inodes for unprivileged users;
654 | * `fsid`: file system ID;
655 | * `flag`: mount flags;
656 | * `namemax`: maximum filename length.
657 |
658 | The `flag` value can contain a boolean `OR` of the following constants:
659 |
660 | * `SMBCLIENT_VFS_RDONLY`;
661 | * `SMBCLIENT_VFS_DFS`;
662 | * `SMBCLIENT_VFS_CASE_INSENSITIVE`;
663 | * `SMBCLIENT_VFS_NO_UNIXCIFS`;
664 |
665 | ### smbclient_fstatvfs
666 |
667 | ```php
668 | array smbclient_fstatvfs ( resource $state, resource $file_or_dir )
669 | ```
670 |
671 | Returns an array with file system statistics for the given file or directory resource, or `false` on failure.
672 | See `smbclient_statvfs` for a description of the returned array.
673 |
674 | ## streams support
675 |
676 | Starting with version 0.8.0, streams support is enabled.
677 | Most of standard functions work transparently with 'smb' URIs.
678 |
679 | ```php
680 | readfile('smb://user:password@smbserver/share/file.txt');
681 | scandir('smb://user:password@smbserver/share/somedir/');
682 | ```
683 |
684 | Which include: copy, file_get_contents, file_put_contents, fileperms, fopen, mkdir, opendir, rmdir, rename, stat, unlink...
685 | Notice: touch and chmod functions require PHP >= 5.4
686 |
687 | ## Examples
688 |
689 | Some bare-bones examples of how to use libsmbclient-php.
690 | These have deliberately been kept simple.
691 | In production, you should at least check whether the extension has been loaded.
692 | Also, you should urlencode your URI's, check the return value of each function, and handle errors appropriately.
693 |
694 | List the contents of a directory:
695 |
696 | ```php
697 |
2 |
8 | smbclient
9 | pecl.php.net
10 | A PHP wrapper for libsmbclient
11 | smbclient is a PHP extension that uses Samba's libsmbclient library to provide
12 | Samba related functions and 'smb' streams to PHP programs.
13 |
14 | Eduardo Bacchi Kienetz
15 | eduardo
16 | eduardo@kienetz.com
17 | yes
18 |
19 |
20 | Remi Collet
21 | remi
22 | remi@php.net
23 | yes
24 |
25 | 2024-12-10
26 |
27 |
28 | 1.2.0dev
29 | 0.8.0
30 |
31 |
32 | stable
33 | stable
34 |
35 | BSD 2-clause
36 |
37 | Removed support for PHP versions older than 7.4.
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 | 7.4.0
79 |
80 |
81 | 1.9.5
82 |
83 |
84 |
85 | smbclient
86 |
87 |
88 |
89 | 2024-12-09
90 |
91 | 1.2.0dev
92 | 0.8.0
93 |
94 |
95 | stable
96 | stable
97 |
98 | BSD 2-clause
99 |
100 | - Removed support for PHP older than 7.4.
101 |
102 |
103 |
104 | 2024-11-25
105 |
106 | 1.1.2
107 | 0.8.0
108 |
109 |
110 | stable
111 | stable
112 |
113 | BSD 2-clause
114 |
115 | - Build fixes.
116 |
117 |
118 |
119 | 2023-04-17
120 |
121 | 1.1.1
122 | 0.8.0
123 |
124 |
125 | stable
126 | stable
127 |
128 | BSD 2-clause
129 |
130 | - Workaround for libsmbclient regression.
131 |
132 |
133 |
134 | 2023-04-01
135 |
136 | 1.1.0
137 | 0.8.0
138 |
139 |
140 | stable
141 | stable
142 |
143 | BSD 2-clause
144 |
145 | - PHP 8 readiness, by Remi.
146 |
147 |
148 |
149 | 2023-04-01
150 |
151 | 1.0.7
152 | 0.8.0
153 |
154 |
155 | stable
156 | stable
157 |
158 | BSD 2-clause
159 |
160 | - xattr issue fixed by Remi.
161 |
162 |
163 |
164 | 2021-02-28
165 |
166 | 1.0.6
167 | 0.8.0
168 |
169 |
170 | stable
171 | stable
172 |
173 | BSD 2-clause
174 |
175 | - Remi implemented ftruncate on smb_streams.
176 |
177 |
178 |
179 | 2021-02-11
180 |
181 | 1.0.5
182 | 0.8.0
183 |
184 |
185 | stable
186 | stable
187 |
188 | BSD 2-clause
189 |
190 | - Remi fixed a possible segfault.
191 | - Minor documentation update.
192 |
193 |
194 |
195 | 2021-01-21
196 |
197 | 1.0.4
198 | 0.8.0
199 |
200 |
201 | stable
202 | stable
203 |
204 | BSD 2-clause
205 |
206 | - Check if smbc_setOptionProtocols() is available (it was introduced Sep 4, 2018).
207 |
208 |
209 |
210 | 2021-01-21
211 |
212 | 1.0.3
213 | 0.8.0
214 |
215 |
216 | stable
217 | stable
218 |
219 | BSD 2-clause
220 |
221 | - Add client min/max protocol to streams, and make either min/max optional.
222 |
223 |
224 |
225 | 2021-01-20
226 |
227 | 1.0.2
228 | 0.8.0
229 |
230 |
231 | stable
232 | stable
233 |
234 | BSD 2-clause
235 |
236 | - Introduces smbclient_client_protocols, to enable setting client min/max protocol.
237 |
238 |
239 |
240 | 2020-12-29
241 |
242 | 1.0.1
243 | 0.8.0
244 |
245 |
246 | stable
247 | stable
248 |
249 | BSD 2-clause
250 |
251 | - Remi fixed code for PHP 7.4 and 8, and memory free.
252 | - Remi added read test.
253 | - Eduardo updated Travis build config.
254 | - Peter fixed documentation for smbclient_state_init.
255 |
256 |
257 |
258 | 2018-12-24
259 |
260 | 1.0.0
261 | 0.8.0
262 |
263 |
264 | stable
265 | stable
266 |
267 | BSD 2-clause
268 |
269 | - Remi fixed duplicate symbol issue and added support for PHP 7.3 (due to changed API).
270 | - Mario fixed Travis setup.
271 |
272 |
273 |
274 | 2017-02-07
275 |
276 | 0.9.0
277 | 0.8.0
278 |
279 |
280 | stable
281 | stable
282 |
283 | BSD 2-clause
284 |
285 | - fix gh#47 Incorrect function definition for smbclient_read
286 | - optimization: enable stream wrapper reusing connections
287 |
288 |
289 |
290 | 2016-02-29
291 |
292 | 0.8.0dev
293 | 0.8.0
294 |
295 |
296 | stable
297 | stable
298 |
299 | BSD 2-clause
300 |
301 | - Promoting to stable after almost 3 months with no commits needed.
302 |
303 |
304 |
305 | 2015-12-08
306 |
307 | 0.8.0RC1
308 | 0.8.0
309 |
310 |
311 | beta
312 | stable
313 |
314 | BSD 2-clause
315 |
316 | - initial PECL release
317 | - add 'smb' streams support
318 | - rename extension to smbclient
319 | - PHP 7 compatibility
320 |
321 |
322 |
323 |
324 |
--------------------------------------------------------------------------------
/php_smbclient.h:
--------------------------------------------------------------------------------
1 | /* ------------------------------------------------------------------
2 | * This file is part of libsmbclient-php: Samba bindings for PHP.
3 | * Libsmbclient-php is licensed under the BSD 2-clause license:
4 | * ------------------------------------------------------------------
5 | *
6 | * Copyright (c) 2003, Matthew Sachs
7 | * 2009 - 2014, Eduardo Bacchi Kienetz
8 | * 2013 - 2015, Alfred Klomp
9 | * 2015, Remi Collet
10 | * All rights reserved.
11 | *
12 | * Redistribution and use in source and binary forms, with or without
13 | * modification, are permitted provided that the following conditions
14 | * are met:
15 | *
16 | * 1. Redistributions of source code must retain the above copyright
17 | * notice, this list of conditions and the following disclaimer.
18 | *
19 | * 2. Redistributions in binary form must reproduce the above
20 | * copyright notice, this list of conditions and the following
21 | * disclaimer in the documentation and/or other materials provided
22 | * with the distribution.
23 | *
24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
29 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
31 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
33 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
35 | * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 | * SUCH DAMAGE.
37 | *
38 | * ------------------------------------------------------------------
39 | */
40 |
41 | #ifndef PHP_SMBCLIENT_H
42 | #define PHP_SMBCLIENT_H
43 |
44 | #include
45 |
46 | #define PHP_SMBCLIENT_VERSION "1.2.0dev"
47 |
48 | extern zend_module_entry smbclient_module_entry;
49 | #define phpext_smbclient_ptr &smbclient_module_entry
50 |
51 | typedef struct _php_smbclient_state
52 | {
53 | SMBCCTX *ctx;
54 | char *wrkg;
55 | char *user;
56 | char *pass;
57 | int wrkglen;
58 | int userlen;
59 | int passlen;
60 | int err;
61 | }
62 | php_smbclient_state;
63 |
64 | struct _php_smb_pool {
65 | unsigned char hash[20];
66 | php_smbclient_state *state;
67 | struct _php_smb_pool *next;
68 | int nb;
69 | };
70 |
71 | ZEND_BEGIN_MODULE_GLOBALS(smbclient)
72 | struct _php_smb_pool *pool_first;
73 | ZEND_END_MODULE_GLOBALS(smbclient)
74 |
75 | extern ZEND_DECLARE_MODULE_GLOBALS(smbclient)
76 |
77 | PHP_MINIT_FUNCTION(smbclient);
78 | PHP_MSHUTDOWN_FUNCTION(smbclient);
79 | PHP_RSHUTDOWN_FUNCTION(smbclient);
80 | PHP_RINIT_FUNCTION(smbclient);
81 | PHP_MINFO_FUNCTION(smbclient);
82 | PHP_GINIT_FUNCTION(smbclient);
83 | PHP_FUNCTION(smbclient_version);
84 | PHP_FUNCTION(smbclient_library_version);
85 | PHP_FUNCTION(smbclient_state_new);
86 | PHP_FUNCTION(smbclient_state_init);
87 | PHP_FUNCTION(smbclient_state_errno);
88 | PHP_FUNCTION(smbclient_state_free);
89 | PHP_FUNCTION(smbclient_option_get);
90 | PHP_FUNCTION(smbclient_option_set);
91 | PHP_FUNCTION(smbclient_client_protocols);
92 | PHP_FUNCTION(smbclient_opendir);
93 | PHP_FUNCTION(smbclient_readdir);
94 | PHP_FUNCTION(smbclient_closedir);
95 | PHP_FUNCTION(smbclient_rename);
96 | PHP_FUNCTION(smbclient_unlink);
97 | PHP_FUNCTION(smbclient_mkdir);
98 | PHP_FUNCTION(smbclient_rmdir);
99 | PHP_FUNCTION(smbclient_stat);
100 | PHP_FUNCTION(smbclient_fstat);
101 | PHP_FUNCTION(smbclient_open);
102 | PHP_FUNCTION(smbclient_creat);
103 | PHP_FUNCTION(smbclient_read);
104 | PHP_FUNCTION(smbclient_write);
105 | PHP_FUNCTION(smbclient_lseek);
106 | PHP_FUNCTION(smbclient_ftruncate);
107 | PHP_FUNCTION(smbclient_close);
108 | PHP_FUNCTION(smbclient_chmod);
109 | PHP_FUNCTION(smbclient_utimes);
110 | PHP_FUNCTION(smbclient_listxattr);
111 | PHP_FUNCTION(smbclient_getxattr);
112 | PHP_FUNCTION(smbclient_setxattr);
113 | PHP_FUNCTION(smbclient_removexattr);
114 | PHP_FUNCTION(smbclient_statvfs);
115 | PHP_FUNCTION(smbclient_fstatvfs);
116 |
117 | /* If Zend Thread Safety (ZTS) is defined, each thread gets its own private
118 | * php_smbclient_globals structure, the elements of which it can access
119 | * through the SMBCLIENT() macro. Without ZTS, there is just one master
120 | * structure in which we access the members directly: */
121 |
122 | #define SMBCLIENT_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(smbclient, v)
123 |
124 | #if PHP_MAJOR_VERSION >= 8
125 | #define TSRMLS_D void
126 | #define TSRMLS_DC
127 | #define TSRMLS_C
128 | #define TSRMLS_CC
129 | #define TSRMLS_FETCH()
130 | #endif
131 |
132 | extern php_stream_wrapper php_stream_smb_wrapper;
133 | php_smbclient_state * php_smbclient_state_new (php_stream_context *context, int init TSRMLS_DC);
134 | void php_smbclient_state_free (php_smbclient_state *state TSRMLS_DC);
135 | int php_smbclient_state_init (php_smbclient_state *state TSRMLS_DC);
136 | int flagstring_to_smbflags (const char *flags, int flags_len, int *retval TSRMLS_DC);
137 | void php_smb_pool_cleanup(TSRMLS_D);
138 |
139 | #endif /* PHP_SMBCLIENT_H */
140 |
--------------------------------------------------------------------------------
/phpunit.xml.dist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 | tests
12 |
13 |
14 |
--------------------------------------------------------------------------------
/smb_streams.c:
--------------------------------------------------------------------------------
1 | /* ------------------------------------------------------------------
2 | * This file is part of libsmbclient-php: Samba bindings for PHP.
3 | * Libsmbclient-php is licensed under the BSD 2-clause license:
4 | * ------------------------------------------------------------------
5 | *
6 | * Copyright (c) 2003, Matthew Sachs
7 | * 2009 - 2014, Eduardo Bacchi Kienetz
8 | * 2013 - 2015, Alfred Klomp
9 | * 2015, Remi Collet
10 | * All rights reserved.
11 | *
12 | * Redistribution and use in source and binary forms, with or without
13 | * modification, are permitted provided that the following conditions
14 | * are met:
15 | *
16 | * 1. Redistributions of source code must retain the above copyright
17 | * notice, this list of conditions and the following disclaimer.
18 | *
19 | * 2. Redistributions in binary form must reproduce the above
20 | * copyright notice, this list of conditions and the following
21 | * disclaimer in the documentation and/or other materials provided
22 | * with the distribution.
23 | *
24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
29 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
31 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
33 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
35 | * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 | * SUCH DAMAGE.
37 | *
38 | * ------------------------------------------------------------------
39 | */
40 |
41 | #ifdef HAVE_CONFIG_H
42 | #include "config.h"
43 | #endif
44 |
45 | #include "php.h"
46 | #include "ext/standard/url.h"
47 | #include "ext/standard/info.h"
48 | #include "ext/standard/php_filestat.h"
49 | #include "ext/standard/sha1.h"
50 | #include "php_smbclient.h"
51 |
52 | #include
53 |
54 | #define STREAM_DATA_FROM_STREAM() \
55 | php_smb_stream_data *self = (php_smb_stream_data *) stream->abstract;
56 |
57 | typedef struct _php_smb_stream_data {
58 | php_smbclient_state *state;
59 | SMBCFILE *handle;
60 | /* pointers cache for multiple call */
61 | smbc_read_fn smbc_read;
62 | smbc_readdir_fn smbc_readdir;
63 | smbc_write_fn smbc_write;
64 | smbc_lseek_fn smbc_lseek;
65 | smbc_ftruncate_fn smbc_ftruncate;
66 | } php_smb_stream_data;
67 |
68 |
69 | static php_smbclient_state *php_smb_pool_get(php_stream_context *context, const char *url TSRMLS_DC)
70 | {
71 | PHP_SHA1_CTX sha1;
72 | unsigned char hash[20];
73 | struct _php_smb_pool *pool;
74 |
75 | /* Create a hash for connection parameter */
76 | PHP_SHA1Init(&sha1);
77 | if (!memcmp(url, "smb://", 6)) {
78 | char *p;
79 | p = strchr(url+6, '/'); // we only want smb://workgroup;user:pass@server/
80 | PHP_SHA1Update(&sha1, (const unsigned char *)url+6, p ? p - url - 6 : strlen(url+6));
81 | }
82 | if (context) {
83 | #if PHP_MAJOR_VERSION >= 7
84 | zval *tmpzval;
85 |
86 | if (NULL != (tmpzval = php_stream_context_get_option(context, "smb", "workgroup"))) {
87 | if (Z_TYPE_P(tmpzval) == IS_STRING) {
88 | PHP_SHA1Update(&sha1, (const unsigned char *)Z_STRVAL_P(tmpzval), Z_STRLEN_P(tmpzval)+1);
89 | }
90 | }
91 | if (NULL != (tmpzval = php_stream_context_get_option(context, "smb", "username"))) {
92 | if (Z_TYPE_P(tmpzval) == IS_STRING) {
93 | PHP_SHA1Update(&sha1, (const unsigned char *)Z_STRVAL_P(tmpzval), Z_STRLEN_P(tmpzval)+1);
94 | }
95 | }
96 | if (NULL != (tmpzval = php_stream_context_get_option(context, "smb", "password"))) {
97 | if (Z_TYPE_P(tmpzval) == IS_STRING) {
98 | PHP_SHA1Update(&sha1, (const unsigned char *)Z_STRVAL_P(tmpzval), Z_STRLEN_P(tmpzval)+1);
99 | }
100 | }
101 | #else
102 | zval **tmpzval;
103 |
104 | if (php_stream_context_get_option(context, "smb", "workgroup", &tmpzval) == SUCCESS) {
105 | if (Z_TYPE_PP(tmpzval) == IS_STRING) {
106 | PHP_SHA1Update(&sha1, (const unsigned char *)Z_STRVAL_PP(tmpzval), Z_STRLEN_PP(tmpzval)+1);
107 | }
108 | }
109 | if (php_stream_context_get_option(context, "smb", "username", &tmpzval) == SUCCESS) {
110 | if (Z_TYPE_PP(tmpzval) == IS_STRING) {
111 | PHP_SHA1Update(&sha1, (const unsigned char *)Z_STRVAL_PP(tmpzval), Z_STRLEN_PP(tmpzval)+1);
112 | }
113 | }
114 | if (php_stream_context_get_option(context, "smb", "password", &tmpzval) == SUCCESS) {
115 | if (Z_TYPE_PP(tmpzval) == IS_STRING) {
116 | PHP_SHA1Update(&sha1, (const unsigned char *)Z_STRVAL_PP(tmpzval), Z_STRLEN_PP(tmpzval)+1);
117 | }
118 | }
119 | #endif
120 | }
121 | PHP_SHA1Final(hash, &sha1);
122 |
123 | /* Reuse state from pool if exists */
124 | for (pool = SMBCLIENT_G(pool_first); pool; pool = pool->next) {
125 | if (!memcmp(hash, pool->hash, 20)) {
126 | pool->nb++;
127 | return pool->state;
128 | }
129 | }
130 |
131 | /* Create a new state and save it in the pool */
132 | pool = emalloc(sizeof(*pool));
133 | memcpy(pool->hash, hash, 20);
134 | pool->nb = 1;
135 | pool->next = SMBCLIENT_G(pool_first);
136 | pool->state = php_smbclient_state_new(context, 1 TSRMLS_CC);
137 | SMBCLIENT_G(pool_first) = pool;
138 |
139 | return pool->state;
140 | }
141 |
142 | static void php_smb_pool_drop(php_smbclient_state *state TSRMLS_DC)
143 | {
144 | struct _php_smb_pool *pool;
145 |
146 | for (pool = SMBCLIENT_G(pool_first); pool; pool = pool->next) {
147 | if (pool->state == state) {
148 | pool->nb--;
149 | return;
150 | }
151 | }
152 |
153 | /* Not found (after php_smb_pool_cleanup) so close it */
154 | php_smbclient_state_free(state TSRMLS_CC);
155 | }
156 |
157 | void php_smb_pool_cleanup(TSRMLS_D) {
158 | struct _php_smb_pool *pool;
159 |
160 | pool = SMBCLIENT_G(pool_first);
161 | while (pool) {
162 | if (!pool->nb) { /* Keep it when still used */
163 | php_smbclient_state_free(pool->state TSRMLS_CC);
164 | }
165 | pool = pool->next;
166 | efree(pool);
167 | }
168 | SMBCLIENT_G(pool_first) = NULL;
169 | }
170 |
171 | static int php_smb_ops_close(php_stream *stream, int close_handle TSRMLS_DC)
172 | {
173 | smbc_close_fn smbc_close;
174 | STREAM_DATA_FROM_STREAM();
175 |
176 | if (!self) {
177 | return EOF;
178 | }
179 |
180 | if (close_handle) {
181 | if (self->handle) {
182 | smbc_close = smbc_getFunctionClose(self->state->ctx);
183 | if (smbc_close) {
184 | smbc_close(self->state->ctx, self->handle);
185 | }
186 | self->handle = NULL;
187 | }
188 | }
189 |
190 | php_smb_pool_drop(self->state TSRMLS_CC);
191 | efree(self);
192 | stream->abstract = NULL;
193 | return EOF;
194 | }
195 |
196 | static int php_smb_ops_flush(php_stream *stream TSRMLS_DC)
197 | {
198 | return 0;
199 | }
200 |
201 | static ssize_t php_smb_ops_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
202 | {
203 | ssize_t n = 0;
204 | STREAM_DATA_FROM_STREAM();
205 |
206 | if (!self || !self->handle) {
207 | return 0;
208 | }
209 | if (!self->smbc_read) {
210 | self->smbc_read = smbc_getFunctionRead(self->state->ctx);
211 | }
212 | if (self->smbc_read) {
213 | n = self->smbc_read(self->state->ctx, self->handle, buf, count);
214 | }
215 | /* cast count to signed value to avoid possibly negative n being cast to unsigned value */
216 | if (n == 0 || n < (ssize_t)count) {
217 | stream->eof = 1;
218 | }
219 | return n;
220 | }
221 |
222 | static ssize_t php_smb_ops_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
223 | {
224 | ssize_t len = 0;
225 | STREAM_DATA_FROM_STREAM();
226 |
227 | if (!self || !self->handle) {
228 | return 0;
229 | }
230 | if (!self->smbc_write) {
231 | self->smbc_write = smbc_getFunctionWrite(self->state->ctx);
232 | }
233 | if (self->smbc_write) {
234 | len = self->smbc_write(self->state->ctx, self->handle, buf, count);
235 | }
236 |
237 | return len;
238 | }
239 |
240 | static int php_smb_ops_stat(php_stream *stream, php_stream_statbuf *ssb TSRMLS_DC) /* {{{ */
241 | {
242 | smbc_fstat_fn smbc_fstat;
243 | STREAM_DATA_FROM_STREAM();
244 |
245 | if (!self || !self->handle) {
246 | return -1;
247 | }
248 | if ((smbc_fstat = smbc_getFunctionFstat(self->state->ctx)) == NULL) {
249 | return -1;
250 | }
251 | if (smbc_fstat(self->state->ctx, self->handle, &ssb->sb) < 0) {
252 | return -1;
253 | }
254 | return 0;
255 | }
256 |
257 | static int php_smb_ops_seek(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffset TSRMLS_DC)
258 | {
259 | STREAM_DATA_FROM_STREAM();
260 |
261 | if (!self || !self->handle) {
262 | return -1;
263 | }
264 | if (!self->smbc_lseek) {
265 | self->smbc_lseek = smbc_getFunctionLseek(self->state->ctx);
266 | }
267 | if (self->smbc_lseek) {
268 | *newoffset = self->smbc_lseek(self->state->ctx, self->handle, offset, whence);
269 | return 0;
270 | }
271 | return -1;
272 | }
273 |
274 | static int php_smb_ops_set_option(php_stream *stream, int option, int value, void *ptrparam TSRMLS_DC)
275 | {
276 | size_t newsize;
277 | STREAM_DATA_FROM_STREAM();
278 |
279 | if (!self || !self->handle) {
280 | return PHP_STREAM_OPTION_RETURN_ERR;
281 | }
282 | if (!self->smbc_ftruncate) {
283 | self->smbc_ftruncate = smbc_getFunctionFtruncate(self->state->ctx);
284 | }
285 | if (!self->smbc_ftruncate) {
286 | return PHP_STREAM_OPTION_RETURN_ERR;
287 | }
288 |
289 | switch(option) {
290 | case PHP_STREAM_OPTION_TRUNCATE_API:
291 | switch (value) {
292 | case PHP_STREAM_TRUNCATE_SUPPORTED:
293 | return PHP_STREAM_OPTION_RETURN_OK;
294 |
295 | case PHP_STREAM_TRUNCATE_SET_SIZE:
296 | newsize = *(size_t*)ptrparam;
297 | if (self->smbc_ftruncate(self->state->ctx, self->handle, newsize) == 0) {
298 | return PHP_STREAM_OPTION_RETURN_OK;
299 | }
300 | return PHP_STREAM_OPTION_RETURN_ERR;
301 | }
302 | }
303 | return PHP_STREAM_OPTION_RETURN_NOTIMPL;
304 | }
305 |
306 | static php_stream_ops php_stream_smbio_ops = {
307 | php_smb_ops_write,
308 | php_smb_ops_read,
309 | php_smb_ops_close,
310 | php_smb_ops_flush,
311 | "smb",
312 | php_smb_ops_seek,
313 | NULL, /* cast */
314 | php_smb_ops_stat,
315 | php_smb_ops_set_option
316 | };
317 |
318 | static php_stream *
319 | php_stream_smb_opener(
320 | php_stream_wrapper *wrapper,
321 | const char *path,
322 | const char *mode,
323 | int options,
324 | zend_string **opened_path,
325 | php_stream_context *context
326 | STREAMS_DC TSRMLS_DC)
327 | {
328 | php_smbclient_state *state;
329 | int smbflags;
330 | long smbmode = 0666;
331 | smbc_open_fn smbc_open;
332 | SMBCFILE *handle;
333 | php_smb_stream_data *self;
334 |
335 | /* Context */
336 | state = php_smb_pool_get(context, path TSRMLS_CC);
337 | if (!state) {
338 | return NULL;
339 | }
340 | /* File */
341 | if (!strcmp(mode, "wb")) {
342 | mode = "w";
343 | } else if (!strcmp(mode, "rb")) {
344 | mode = "r";
345 | }
346 | if (flagstring_to_smbflags(mode, strlen(mode), &smbflags TSRMLS_CC) == 0) {
347 | php_smb_pool_drop(state TSRMLS_CC);
348 | return NULL;
349 | }
350 | if ((smbc_open = smbc_getFunctionOpen(state->ctx)) == NULL) {
351 | php_smb_pool_drop(state TSRMLS_CC);
352 | return NULL;
353 | }
354 | if ((handle = smbc_open(state->ctx, path, smbflags, smbmode)) == NULL) {
355 | php_smb_pool_drop(state TSRMLS_CC);
356 | return NULL;
357 | }
358 | self = ecalloc(sizeof(*self), 1);
359 | self->state = state;
360 | self->handle = handle;
361 |
362 | return php_stream_alloc(&php_stream_smbio_ops, self, NULL, mode);
363 | }
364 |
365 | static int
366 | php_stream_smb_unlink(
367 | php_stream_wrapper *wrapper,
368 | const char *url,
369 | int options,
370 | php_stream_context *context
371 | TSRMLS_DC)
372 | {
373 | php_smbclient_state *state;
374 | smbc_unlink_fn smbc_unlink;
375 |
376 | /* Context */
377 | state = php_smb_pool_get(context, url TSRMLS_CC);
378 | if (!state) {
379 | return 0;
380 | }
381 | /* Unlink */
382 | if ((smbc_unlink = smbc_getFunctionUnlink(state->ctx)) == NULL) {
383 | if (options & REPORT_ERRORS) {
384 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unlink not supported");
385 | }
386 | php_smb_pool_drop(state TSRMLS_CC);
387 | return 0;
388 | }
389 | if (smbc_unlink(state->ctx, url) == 0) {
390 | php_smb_pool_drop(state TSRMLS_CC);
391 | return 1;
392 | }
393 | if (options & REPORT_ERRORS) {
394 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unlink fails: %s", strerror(errno));
395 | }
396 | php_smb_pool_drop(state TSRMLS_CC);
397 | return 0;
398 | }
399 |
400 | static int
401 | php_stream_smb_mkdir(
402 | php_stream_wrapper *wrapper,
403 | const char *url,
404 | int mode,
405 | int options,
406 | php_stream_context *context
407 | TSRMLS_DC)
408 | {
409 | php_smbclient_state *state;
410 | smbc_mkdir_fn smbc_mkdir;
411 |
412 | if (options & PHP_STREAM_MKDIR_RECURSIVE) {
413 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Recursive mkdir not supported");
414 | return 0;
415 | }
416 | /* Context */
417 | state = php_smb_pool_get(context, url TSRMLS_CC);
418 | if (!state) {
419 | return 0;
420 | }
421 | /* Mkdir */
422 | if ((smbc_mkdir = smbc_getFunctionMkdir(state->ctx)) == NULL) {
423 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Mkdir not supported");
424 | php_smb_pool_drop(state TSRMLS_CC);
425 | return 0;
426 | }
427 | if (smbc_mkdir(state->ctx, url, (mode_t)mode) == 0) {
428 | php_smb_pool_drop(state TSRMLS_CC);
429 | return 1;
430 | }
431 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Mkdir fails: %s", strerror(errno));
432 | php_smb_pool_drop(state TSRMLS_CC);
433 | return 0;
434 | }
435 |
436 | static int
437 | php_stream_smb_rmdir(
438 | php_stream_wrapper *wrapper,
439 | const char *url,
440 | int options,
441 | php_stream_context *context
442 | TSRMLS_DC)
443 | {
444 | php_smbclient_state *state;
445 | smbc_rmdir_fn smbc_rmdir;
446 |
447 | /* Context */
448 | state = php_smb_pool_get(context, url TSRMLS_CC);
449 | if (!state) {
450 | return 0;
451 | }
452 | /* Rmdir */
453 | if ((smbc_rmdir = smbc_getFunctionRmdir(state->ctx)) == NULL) {
454 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Rmdir not supported");
455 | php_smb_pool_drop(state TSRMLS_CC);
456 | return 0;
457 | }
458 | if (smbc_rmdir(state->ctx, url) == 0) {
459 | php_smb_pool_drop(state TSRMLS_CC);
460 | return 1;
461 | }
462 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Rmdir fails: %s", strerror(errno));
463 | php_smb_pool_drop(state TSRMLS_CC);
464 | return 0;
465 | }
466 |
467 | static int
468 | php_stream_smb_rename(
469 | php_stream_wrapper *wrapper,
470 | const char *url_from,
471 | const char *url_to,
472 | int options,
473 | php_stream_context *context
474 | TSRMLS_DC)
475 | {
476 | php_smbclient_state *state;
477 | smbc_rename_fn smbc_rename;
478 |
479 | /* Context */
480 | state = php_smb_pool_get(context, url_from TSRMLS_CC);
481 | if (!state) {
482 | return 0;
483 | }
484 | if ((smbc_rename = smbc_getFunctionRename(state->ctx)) == NULL) {
485 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Rename not supported");
486 | php_smb_pool_drop(state TSRMLS_CC);
487 | return 0;
488 | }
489 | if (smbc_rename(state->ctx, url_from, state->ctx, url_to) == 0) {
490 | php_smb_pool_drop(state TSRMLS_CC);
491 | return 1;
492 | }
493 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Rename fails: %s", strerror(errno));
494 | php_smb_pool_drop(state TSRMLS_CC);
495 | return 0;
496 | }
497 |
498 | static int php_smbdir_ops_close(php_stream *stream, int close_handle TSRMLS_DC)
499 | {
500 | smbc_closedir_fn smbc_closedir;
501 | STREAM_DATA_FROM_STREAM();
502 |
503 | if (close_handle) {
504 | if (self->handle) {
505 | smbc_closedir = smbc_getFunctionClosedir(self->state->ctx);
506 | if (smbc_closedir) {
507 | smbc_closedir(self->state->ctx, self->handle);
508 | }
509 | self->handle = NULL;
510 | }
511 | }
512 | php_smb_pool_drop(self->state TSRMLS_CC);
513 | efree(self);
514 | stream->abstract = NULL;
515 | return EOF;
516 | }
517 |
518 | static ssize_t php_smbdir_ops_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
519 | {
520 | struct smbc_dirent *dirent;
521 | php_stream_dirent *ent = (php_stream_dirent*)buf;
522 | STREAM_DATA_FROM_STREAM();
523 |
524 | if (!self || !self->handle) {
525 | return 0;
526 | }
527 | /* avoid problems if someone mis-uses the stream */
528 | if (count != sizeof(php_stream_dirent)) {
529 | return 0;
530 | }
531 | if (!self->smbc_readdir) {
532 | self->smbc_readdir = smbc_getFunctionReaddir(self->state->ctx);
533 | }
534 | if (self->smbc_readdir) {
535 | if ((dirent = self->smbc_readdir(self->state->ctx, self->handle)) != NULL) {
536 | PHP_STRLCPY(ent->d_name, dirent->name, sizeof(ent->d_name), dirent->namelen);
537 | return sizeof(php_stream_dirent);
538 | }
539 | }
540 | stream->eof = 1;
541 | return 0;
542 | }
543 |
544 | static php_stream_ops php_stream_smbdir_ops = {
545 | NULL,
546 | php_smbdir_ops_read,
547 | php_smbdir_ops_close,
548 | NULL,
549 | "smbdir",
550 | NULL, /* rewind */
551 | NULL, /* cast */
552 | NULL, /* stat */
553 | NULL /* set_option */
554 | };
555 |
556 | static php_stream *
557 | php_stream_smbdir_opener(
558 | php_stream_wrapper *wrapper,
559 | const char *path,
560 | const char *mode,
561 | int options,
562 | zend_string **opened_path,
563 | php_stream_context *context
564 | STREAMS_DC TSRMLS_DC)
565 | {
566 | php_smbclient_state *state;
567 | smbc_opendir_fn smbc_opendir;
568 | SMBCFILE *handle;
569 | php_smb_stream_data *self;
570 |
571 | /* Context */
572 | state = php_smb_pool_get(context, path TSRMLS_CC);
573 | if (!state) {
574 | return NULL;
575 | }
576 | /* Directory */
577 | if ((smbc_opendir = smbc_getFunctionOpendir(state->ctx)) == NULL) {
578 | php_smb_pool_drop(state TSRMLS_CC);
579 | return NULL;
580 | }
581 | if ((handle = smbc_opendir(state->ctx, path)) == NULL) {
582 | php_smb_pool_drop(state TSRMLS_CC);
583 | return NULL;
584 | }
585 | self = ecalloc(sizeof(*self), 1);
586 | self->state = state;
587 | self->handle = handle;
588 |
589 | return php_stream_alloc(&php_stream_smbdir_ops, self, NULL, mode);
590 | }
591 |
592 | static int
593 | php_stream_smb_stat(
594 | php_stream_wrapper *wrapper,
595 | const char *url,
596 | int flags,
597 | php_stream_statbuf *ssb,
598 | php_stream_context *context
599 | TSRMLS_DC)
600 | {
601 | php_smbclient_state *state;
602 | smbc_stat_fn smbc_stat;
603 |
604 | /* Context */
605 | state = php_smb_pool_get(context, url TSRMLS_CC);
606 | if (!state) {
607 | return 0;
608 | }
609 | /* Stat */
610 | if ((smbc_stat = smbc_getFunctionStat(state->ctx)) == NULL) {
611 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Stat not supported");
612 | php_smb_pool_drop(state TSRMLS_CC);
613 | return -1;
614 | }
615 | if (smbc_stat(state->ctx, url, &ssb->sb) >= 0) {
616 | php_smb_pool_drop(state TSRMLS_CC);
617 | return 0;
618 | }
619 | /* dont display error as PHP use this method internally to check if file exists */
620 | php_smb_pool_drop(state TSRMLS_CC);
621 | return -1;
622 | }
623 |
624 | static int
625 | php_stream_smb_metadata(
626 | php_stream_wrapper *wrapper,
627 | const char *url,
628 | int option,
629 | void *value,
630 | php_stream_context *context
631 | TSRMLS_DC)
632 | {
633 | php_smbclient_state *state;
634 | smbc_chmod_fn smbc_chmod;
635 | smbc_open_fn smbc_open;
636 | smbc_utimes_fn smbc_utimes;
637 | smbc_close_fn smbc_close;
638 | mode_t mode;
639 | struct utimbuf *newtime;
640 | struct timeval times[2];
641 | SMBCFILE *handle;
642 | int ret = 0;
643 |
644 | switch(option) {
645 | case PHP_STREAM_META_TOUCH:
646 | newtime = (struct utimbuf *)value;
647 |
648 | /* Context */
649 | state = php_smb_pool_get(context, url TSRMLS_CC);
650 | if (!state) {
651 | return 0;
652 | }
653 | /* Create + Utimes */
654 | if ((smbc_open = smbc_getFunctionOpen(state->ctx)) == NULL
655 | || (smbc_close = smbc_getFunctionClose(state->ctx)) == NULL
656 | || (smbc_utimes = smbc_getFunctionUtimes(state->ctx)) == NULL) {
657 | ret = -1;
658 | break;
659 | }
660 | /* Create can fail if file exists, ignore result */
661 | handle = smbc_open(state->ctx, url, O_EXCL|O_CREAT, 0666);
662 | if (handle) {
663 | smbc_close(state->ctx, handle);
664 | }
665 | if (newtime) {
666 | times[0].tv_usec = 0;
667 | times[0].tv_sec = newtime->actime;
668 | times[1].tv_usec = 0;
669 | times[1].tv_sec = newtime->modtime;
670 |
671 | ret = smbc_utimes(state->ctx, url, times);
672 | }
673 | break;
674 |
675 | case PHP_STREAM_META_ACCESS:
676 | mode = (mode_t)*(long *)value;
677 | /* Context */
678 | state = php_smb_pool_get(context, url TSRMLS_CC);
679 | if (!state) {
680 | return 0;
681 | }
682 | /* Chmod */
683 | if ((smbc_chmod = smbc_getFunctionChmod(state->ctx)) == NULL) {
684 | ret = -1;
685 | break;
686 | }
687 | ret = smbc_chmod(state->ctx, url, (mode_t)mode);
688 | break;
689 |
690 | default:
691 | php_error_docref1(NULL TSRMLS_CC, url, E_WARNING, "Unknown option %d for stream_metadata", option);
692 | return 0;
693 | }
694 | php_smb_pool_drop(state TSRMLS_CC);
695 | if (ret == -1) {
696 | php_error_docref1(NULL TSRMLS_CC, url, E_WARNING, "Operation failed: %s", strerror(errno));
697 | return 0;
698 | }
699 | php_clear_stat_cache(0, NULL, 0 TSRMLS_CC);
700 | return 1;
701 | }
702 |
703 | static php_stream_wrapper_ops smb_stream_wops = {
704 | php_stream_smb_opener,
705 | NULL, /* close */
706 | NULL, /* fstat */
707 | php_stream_smb_stat,
708 | php_stream_smbdir_opener,
709 | "smb",
710 | php_stream_smb_unlink,
711 | php_stream_smb_rename,
712 | php_stream_smb_mkdir,
713 | php_stream_smb_rmdir
714 | , php_stream_smb_metadata
715 | };
716 |
717 | php_stream_wrapper php_stream_smb_wrapper = {
718 | &smb_stream_wops,
719 | NULL,
720 | 1 /* is_url */
721 | };
722 |
--------------------------------------------------------------------------------
/smbclient.c:
--------------------------------------------------------------------------------
1 | /* ------------------------------------------------------------------
2 | * This file is part of libsmbclient-php: Samba bindings for PHP.
3 | * Libsmbclient-php is licensed under the BSD 2-clause license:
4 | * ------------------------------------------------------------------
5 | *
6 | * Copyright (c) 2003, Matthew Sachs
7 | * 2009 - 2014, Eduardo Bacchi Kienetz
8 | * 2013 - 2015, Alfred Klomp
9 | * 2015, Remi Collet
10 | * All rights reserved.
11 | *
12 | * Redistribution and use in source and binary forms, with or without
13 | * modification, are permitted provided that the following conditions
14 | * are met:
15 | *
16 | * 1. Redistributions of source code must retain the above copyright
17 | * notice, this list of conditions and the following disclaimer.
18 | *
19 | * 2. Redistributions in binary form must reproduce the above
20 | * copyright notice, this list of conditions and the following
21 | * disclaimer in the documentation and/or other materials provided
22 | * with the distribution.
23 | *
24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
29 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
31 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
33 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
35 | * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 | * SUCH DAMAGE.
37 | *
38 | * ------------------------------------------------------------------
39 | */
40 |
41 | #define IS_EXT_MODULE
42 |
43 | #ifdef HAVE_CONFIG_H
44 | #include "config.h"
45 | #endif
46 |
47 | #include "php.h"
48 | #include "ext/standard/info.h"
49 | #include "php_smbclient.h"
50 |
51 | ZEND_DECLARE_MODULE_GLOBALS(smbclient)
52 |
53 | #define PHP_SMBCLIENT_STATE_NAME "smbclient state"
54 | #define PHP_SMBCLIENT_FILE_NAME "smbclient file"
55 |
56 | static int le_smbclient_state;
57 | static int le_smbclient_file;
58 |
59 | typedef size_t strsize_t;
60 |
61 | enum {
62 | SMBCLIENT_OPT_OPEN_SHAREMODE = 1,
63 | SMBCLIENT_OPT_ENCRYPT_LEVEL = 2,
64 | SMBCLIENT_OPT_CASE_SENSITIVE = 3,
65 | SMBCLIENT_OPT_BROWSE_MAX_LMB_COUNT = 4,
66 | SMBCLIENT_OPT_URLENCODE_READDIR_ENTRIES = 5,
67 | /* Ignore OneSharePerServer, not relevant to us. */
68 | SMBCLIENT_OPT_USE_KERBEROS = 6,
69 | SMBCLIENT_OPT_FALLBACK_AFTER_KERBEROS = 7,
70 | /* Reverse the sense of this option, the original
71 | * is the confusing "NoAutoAnonymousLogin": */
72 | SMBCLIENT_OPT_AUTO_ANONYMOUS_LOGIN = 8,
73 | SMBCLIENT_OPT_USE_CCACHE = 9,
74 | SMBCLIENT_OPT_USE_NT_HASH = 10,
75 | SMBCLIENT_OPT_NETBIOS_NAME = 11,
76 | SMBCLIENT_OPT_WORKGROUP = 12,
77 | SMBCLIENT_OPT_USER = 13,
78 | SMBCLIENT_OPT_PORT = 14,
79 | SMBCLIENT_OPT_TIMEOUT = 15,
80 | }
81 | php_smbclient_options;
82 |
83 | static char *
84 | find_char (char *start, char *last, char q)
85 | {
86 | char *c;
87 | for (c = start; c <= last; c++) {
88 | if (*c == q) {
89 | return c;
90 | }
91 | }
92 | return NULL;
93 | }
94 |
95 | static char *
96 | find_second_char (char *start, char *last, char q)
97 | {
98 | char *c;
99 | if ((c = find_char(start, last, q)) == NULL) {
100 | return NULL;
101 | }
102 | return find_char(c + 1, last, q);
103 | }
104 |
105 | static void
106 | astfill (char *start, char *last)
107 | {
108 | char *c;
109 | for (c = start; c <= last; c++) {
110 | *c = '*';
111 | }
112 | }
113 |
114 | static void
115 | hide_password (char *url, int len)
116 | {
117 | /* URL should have the form:
118 | * smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]]
119 | * Replace everything after the second colon and before the next @
120 | * with asterisks. */
121 | char *last = (url + len) - 1;
122 | char *second_colon;
123 | char *slash;
124 | char *at_sign;
125 |
126 | if (len <= 0) {
127 | return;
128 | }
129 | if ((second_colon = find_second_char(url, last, ':')) == NULL) {
130 | return;
131 | }
132 | if ((slash = find_char(second_colon + 1, last, '/')) == NULL) {
133 | slash = last + 1;
134 | }
135 | if ((at_sign = find_char(second_colon + 1, last, '@')) == NULL) {
136 | astfill(second_colon + 1, slash - 1);
137 | return;
138 | }
139 | if (at_sign > slash) {
140 | at_sign = slash;
141 | }
142 | astfill(second_colon + 1, at_sign - 1);
143 | }
144 |
145 | #if PHP_VERSION_ID < 80000
146 | #include "smbclient_legacy_arginfo.h"
147 | #else
148 | #include "smbclient_arginfo.h"
149 | #endif
150 |
151 | zend_module_entry smbclient_module_entry =
152 | { STANDARD_MODULE_HEADER
153 | , "smbclient" /* name */
154 | , ext_functions /* functions */
155 | , PHP_MINIT(smbclient) /* module_startup_func */
156 | , PHP_MSHUTDOWN(smbclient) /* module_shutdown_func */
157 | , PHP_RINIT(smbclient) /* request_startup_func */
158 | , PHP_RSHUTDOWN(smbclient) /* request_shutdown_func */
159 | , PHP_MINFO(smbclient) /* info_func */
160 | , PHP_SMBCLIENT_VERSION /* version */
161 | , PHP_MODULE_GLOBALS(smbclient)
162 | , PHP_GINIT(smbclient) /* globals ctor */
163 | , NULL /* globals dtor */
164 | , NULL /* post_deactivate_func */
165 | , STANDARD_MODULE_PROPERTIES_EX
166 | } ;
167 |
168 | zend_module_entry old_module_entry = {
169 | STANDARD_MODULE_HEADER,
170 | "libsmbclient",
171 | NULL,
172 | NULL,
173 | NULL,
174 | NULL,
175 | NULL,
176 | NULL,
177 | PHP_SMBCLIENT_VERSION,
178 | STANDARD_MODULE_PROPERTIES,
179 | };
180 |
181 |
182 | #ifdef COMPILE_DL_SMBCLIENT
183 | ZEND_GET_MODULE(smbclient)
184 | #endif
185 |
186 | static void
187 | auth_copy (char *dst, char *src, size_t srclen, size_t maxlen)
188 | {
189 | if (dst == NULL || maxlen == 0) {
190 | return;
191 | }
192 | if (src == NULL || srclen == 0) {
193 | *dst = '\0';
194 | return;
195 | }
196 | if (srclen < maxlen) {
197 | memcpy(dst, src, srclen);
198 | dst[srclen] = '\0';
199 | return;
200 | }
201 | memcpy(dst, src, maxlen - 1);
202 | dst[maxlen - 1] = '\0';
203 | }
204 |
205 | static void
206 | smbclient_auth_func (SMBCCTX *ctx, const char *server, const char *share, char *wrkg, int wrkglen, char *user, int userlen, char *pass, int passlen)
207 | {
208 | /* Given context, server and share, return workgroup, username and password.
209 | * String lengths are the max allowable lengths. */
210 |
211 | php_smbclient_state *state;
212 |
213 | if (ctx == NULL || (state = smbc_getOptionUserData(ctx)) == NULL) {
214 | return;
215 | }
216 | auth_copy(wrkg, state->wrkg, (size_t)state->wrkglen, (size_t)wrkglen);
217 | auth_copy(user, state->user, (size_t)state->userlen, (size_t)userlen);
218 | auth_copy(pass, state->pass, (size_t)state->passlen, (size_t)passlen);
219 | }
220 |
221 | void
222 | php_smbclient_state_free (php_smbclient_state *state TSRMLS_DC)
223 | {
224 | /* TODO: if smbc_free_context() returns 0, PHP will leak the handle: */
225 | if (state->ctx != NULL && smbc_free_context(state->ctx, 1) != 0) {
226 | switch (errno) {
227 | case EBUSY: php_error(E_WARNING, "Couldn't destroy SMB context: connection in use"); break;
228 | case EBADF: php_error(E_WARNING, "Couldn't destroy SMB context: invalid handle"); break;
229 | default: php_error(E_WARNING, "Couldn't destroy SMB context: unknown error (%d)", errno); break;
230 | }
231 | }
232 | if (state->wrkg != NULL) {
233 | memset(state->wrkg, 0, state->wrkglen);
234 | efree(state->wrkg);
235 | }
236 | if (state->user != NULL) {
237 | memset(state->user, 0, state->userlen);
238 | efree(state->user);
239 | }
240 | if (state->pass != NULL) {
241 | memset(state->pass, 0, state->passlen);
242 | efree(state->pass);
243 | }
244 | efree(state);
245 | }
246 |
247 | static inline void
248 | smbclient_state_dtor (
249 | #if PHP_MAJOR_VERSION >= 7
250 | zend_resource *rsrc
251 | #else
252 | zend_rsrc_list_entry *rsrc
253 | #endif
254 | TSRMLS_DC)
255 | {
256 | php_smbclient_state_free((php_smbclient_state *)rsrc->ptr TSRMLS_CC);
257 | }
258 |
259 | static void
260 | smbclient_file_dtor (
261 | zend_resource *rsrc
262 | TSRMLS_DC)
263 | {
264 | /* Because libsmbclient's file/dir close functions require a pointer to
265 | * a context which we don't have, we cannot reliably destroy a file
266 | * resource. One way of obtaining the context pointer could be to save
267 | * it in a structure along with the file context, but the pointer could
268 | * grow stale or otherwise spark a race condition. So it seems that the
269 | * best we can do is nothing. The PHP programmer can either manually
270 | * free the file resources, or wait for them to be cleaned up when the
271 | * associated context is destroyed. */
272 | }
273 |
274 | PHP_GINIT_FUNCTION(smbclient)
275 | {
276 | smbclient_globals->pool_first = NULL;
277 | }
278 |
279 | PHP_MINIT_FUNCTION(smbclient)
280 | {
281 | /* Constants for smbclient_setxattr: */
282 | REGISTER_LONG_CONSTANT("SMBCLIENT_XATTR_CREATE", SMBC_XATTR_FLAG_CREATE, CONST_PERSISTENT | CONST_CS);
283 | REGISTER_LONG_CONSTANT("SMBCLIENT_XATTR_REPLACE", SMBC_XATTR_FLAG_REPLACE, CONST_PERSISTENT | CONST_CS);
284 |
285 | /* Constants for getting/setting options: */
286 | #define SMBCLIENT_CONST(x) REGISTER_LONG_CONSTANT(#x, x, CONST_PERSISTENT | CONST_CS);
287 | SMBCLIENT_CONST(SMBCLIENT_OPT_OPEN_SHAREMODE);
288 | SMBCLIENT_CONST(SMBCLIENT_OPT_ENCRYPT_LEVEL);
289 | SMBCLIENT_CONST(SMBCLIENT_OPT_CASE_SENSITIVE);
290 | SMBCLIENT_CONST(SMBCLIENT_OPT_BROWSE_MAX_LMB_COUNT);
291 | SMBCLIENT_CONST(SMBCLIENT_OPT_URLENCODE_READDIR_ENTRIES);
292 | SMBCLIENT_CONST(SMBCLIENT_OPT_USE_KERBEROS);
293 | SMBCLIENT_CONST(SMBCLIENT_OPT_FALLBACK_AFTER_KERBEROS);
294 | SMBCLIENT_CONST(SMBCLIENT_OPT_AUTO_ANONYMOUS_LOGIN);
295 | SMBCLIENT_CONST(SMBCLIENT_OPT_USE_CCACHE);
296 | SMBCLIENT_CONST(SMBCLIENT_OPT_USE_NT_HASH);
297 | SMBCLIENT_CONST(SMBCLIENT_OPT_NETBIOS_NAME);
298 | SMBCLIENT_CONST(SMBCLIENT_OPT_WORKGROUP);
299 | SMBCLIENT_CONST(SMBCLIENT_OPT_USER);
300 | SMBCLIENT_CONST(SMBCLIENT_OPT_PORT);
301 | SMBCLIENT_CONST(SMBCLIENT_OPT_TIMEOUT);
302 | #undef SMBCLIENT_CONST
303 |
304 | /* Constants for use with SMBCLIENT_OPT_OPENSHAREMODE: */
305 | REGISTER_LONG_CONSTANT("SMBCLIENT_SHAREMODE_DENY_DOS", SMBC_SHAREMODE_DENY_DOS, CONST_PERSISTENT | CONST_CS);
306 | REGISTER_LONG_CONSTANT("SMBCLIENT_SHAREMODE_DENY_ALL", SMBC_SHAREMODE_DENY_ALL, CONST_PERSISTENT | CONST_CS);
307 | REGISTER_LONG_CONSTANT("SMBCLIENT_SHAREMODE_DENY_WRITE", SMBC_SHAREMODE_DENY_WRITE, CONST_PERSISTENT | CONST_CS);
308 | REGISTER_LONG_CONSTANT("SMBCLIENT_SHAREMODE_DENY_READ", SMBC_SHAREMODE_DENY_READ, CONST_PERSISTENT | CONST_CS);
309 | REGISTER_LONG_CONSTANT("SMBCLIENT_SHAREMODE_DENY_NONE", SMBC_SHAREMODE_DENY_NONE, CONST_PERSISTENT | CONST_CS);
310 | REGISTER_LONG_CONSTANT("SMBCLIENT_SHAREMODE_DENY_FCB", SMBC_SHAREMODE_DENY_FCB, CONST_PERSISTENT | CONST_CS);
311 |
312 | /* Constants for use with SMBCLIENT_OPT_ENCRYPTLEVEL: */
313 | REGISTER_LONG_CONSTANT("SMBCLIENT_ENCRYPTLEVEL_NONE", SMBC_ENCRYPTLEVEL_NONE, CONST_PERSISTENT | CONST_CS);
314 | REGISTER_LONG_CONSTANT("SMBCLIENT_ENCRYPTLEVEL_REQUEST", SMBC_ENCRYPTLEVEL_REQUEST, CONST_PERSISTENT | CONST_CS);
315 | REGISTER_LONG_CONSTANT("SMBCLIENT_ENCRYPTLEVEL_REQUIRE", SMBC_ENCRYPTLEVEL_REQUIRE, CONST_PERSISTENT | CONST_CS);
316 |
317 | /* Constants for the VFS functions: */
318 | REGISTER_LONG_CONSTANT("SMBCLIENT_VFS_RDONLY", SMBC_VFS_FEATURE_RDONLY, CONST_PERSISTENT | CONST_CS);
319 | REGISTER_LONG_CONSTANT("SMBCLIENT_VFS_DFS", SMBC_VFS_FEATURE_DFS, CONST_PERSISTENT | CONST_CS);
320 | REGISTER_LONG_CONSTANT("SMBCLIENT_VFS_CASE_INSENSITIVE", SMBC_VFS_FEATURE_CASE_INSENSITIVE, CONST_PERSISTENT | CONST_CS);
321 | REGISTER_LONG_CONSTANT("SMBCLIENT_VFS_NO_UNIXCIFS", SMBC_VFS_FEATURE_NO_UNIXCIFS, CONST_PERSISTENT | CONST_CS);
322 |
323 | le_smbclient_state = zend_register_list_destructors_ex(smbclient_state_dtor, NULL, PHP_SMBCLIENT_STATE_NAME, module_number);
324 | le_smbclient_file = zend_register_list_destructors_ex(smbclient_file_dtor, NULL, PHP_SMBCLIENT_FILE_NAME, module_number);
325 |
326 | php_register_url_stream_wrapper("smb", &php_stream_smb_wrapper TSRMLS_CC);
327 |
328 | /* register with old name for code using extension_loaded(libsmbclient) */
329 | zend_register_internal_module(&old_module_entry TSRMLS_CC);
330 |
331 | return SUCCESS;
332 | }
333 |
334 | PHP_RINIT_FUNCTION(smbclient)
335 | {
336 | return SUCCESS;
337 | }
338 |
339 | PHP_MSHUTDOWN_FUNCTION(smbclient)
340 | {
341 | return SUCCESS;
342 | }
343 |
344 | PHP_RSHUTDOWN_FUNCTION(smbclient)
345 | {
346 | php_smb_pool_cleanup(TSRMLS_C);
347 | return SUCCESS;
348 | }
349 |
350 | PHP_MINFO_FUNCTION(smbclient)
351 | {
352 | php_info_print_table_start();
353 | php_info_print_table_row(2, "smbclient Support", "enabled");
354 | php_info_print_table_row(2, "smbclient extension Version", PHP_SMBCLIENT_VERSION);
355 | php_info_print_table_row(2, "libsmbclient library Version", smbc_version());
356 | php_info_print_table_end();
357 | }
358 |
359 | static int
360 | ctx_init_getauth (zval *z, char **dest, int *destlen, char *varname)
361 | {
362 | if (*dest != NULL) {
363 | efree(*dest);
364 | *dest = NULL;
365 | }
366 | *destlen = 0;
367 |
368 | if (z == NULL) {
369 | return 1;
370 | }
371 | switch (Z_TYPE_P(z))
372 | {
373 | case IS_NULL:
374 | return 1;
375 |
376 | #if PHP_MAJOR_VERSION >= 7
377 | case IS_TRUE:
378 | php_error(E_WARNING, "invalid value for %s", varname);
379 | return 0;
380 |
381 | case IS_FALSE:
382 | return 1;
383 | #else
384 | case IS_BOOL:
385 | if (Z_LVAL_P(z) == 1) {
386 | php_error(E_WARNING, "invalid value for %s", varname);
387 | return 0;
388 | }
389 | return 1;
390 | #endif
391 |
392 | case IS_STRING:
393 | *destlen = Z_STRLEN_P(z);
394 | *dest = estrndup(Z_STRVAL_P(z), *destlen);
395 | return 1;
396 |
397 | default:
398 | php_error(E_WARNING, "invalid datatype for %s", varname);
399 | return 0;
400 | }
401 | }
402 |
403 | php_smbclient_state *
404 | php_smbclient_state_new (php_stream_context *context, int init TSRMLS_DC)
405 | {
406 | php_smbclient_state *state;
407 | SMBCCTX *ctx;
408 |
409 | if ((ctx = smbc_new_context()) == NULL) {
410 | switch (errno) {
411 | case ENOMEM: php_error(E_WARNING, "Couldn't create smbclient state: insufficient memory"); break;
412 | default: php_error(E_WARNING, "Couldn't create smbclient state: unknown error (%d)", errno); break;
413 | };
414 | return NULL;
415 | }
416 | state = emalloc(sizeof(php_smbclient_state));
417 | state->ctx = ctx;
418 | state->wrkg = NULL;
419 | state->user = NULL;
420 | state->pass = NULL;
421 | state->wrkglen = 0;
422 | state->userlen = 0;
423 | state->passlen = 0;
424 | state->err = 0;
425 |
426 | smbc_setFunctionAuthDataWithContext(state->ctx, smbclient_auth_func);
427 |
428 | /* Must also save a pointer to the state object inside the context, to
429 | * find the state from the context in the auth function: */
430 | smbc_setOptionUserData(ctx, (void *)state);
431 |
432 | /* Force full, modern timenames when getting xattrs: */
433 | smbc_setOptionFullTimeNames(state->ctx, 1);
434 |
435 | if (context) {
436 | #if PHP_MAJOR_VERSION >= 7
437 | zval *tmpzval;
438 |
439 | if (NULL != (tmpzval = php_stream_context_get_option(context, "smb", "workgroup"))) {
440 | if (ctx_init_getauth(tmpzval, &state->wrkg, &state->wrkglen, "workgroup") == 0) {
441 | php_smbclient_state_free(state);
442 | return NULL;
443 | }
444 | }
445 | if (NULL != (tmpzval = php_stream_context_get_option(context, "smb", "username"))) {
446 | if (ctx_init_getauth(tmpzval, &state->user, &state->userlen, "username") == 0) {
447 | php_smbclient_state_free(state);
448 | return NULL;
449 | }
450 | }
451 | if (NULL != (tmpzval = php_stream_context_get_option(context, "smb", "password"))) {
452 | if (ctx_init_getauth(tmpzval, &state->pass, &state->passlen, "password") == 0) {
453 | php_smbclient_state_free(state);
454 | return NULL;
455 | }
456 | }
457 | #if HAVE_SMBC_SETOPTIONPROTOCOLS
458 | if (NULL != (tmpzval = php_stream_context_get_option(context, "smb", "minproto"))) {
459 | smbc_setOptionProtocols(state->ctx, Z_STRVAL_P(tmpzval), NULL);
460 | }
461 | if (NULL != (tmpzval = php_stream_context_get_option(context, "smb", "maxproto"))) {
462 | smbc_setOptionProtocols(state->ctx, NULL, Z_STRVAL_P(tmpzval));
463 | }
464 | #endif
465 | #else
466 | zval **tmpzval;
467 |
468 | if (php_stream_context_get_option(context, "smb", "workgroup", &tmpzval) == SUCCESS) {
469 | if (ctx_init_getauth(*tmpzval, &state->wrkg, &state->wrkglen, "workgroup") == 0) {
470 | php_smbclient_state_free(state TSRMLS_CC);
471 | return NULL;
472 | }
473 | }
474 | if (php_stream_context_get_option(context, "smb", "username", &tmpzval) == SUCCESS) {
475 | if (ctx_init_getauth(*tmpzval, &state->user, &state->userlen, "username") == 0) {
476 | php_smbclient_state_free(state TSRMLS_CC);
477 | return NULL;
478 | }
479 | }
480 | if (php_stream_context_get_option(context, "smb", "password", &tmpzval) == SUCCESS) {
481 | if (ctx_init_getauth(*tmpzval, &state->pass, &state->passlen, "password") == 0) {
482 | php_smbclient_state_free(state TSRMLS_CC);
483 | return NULL;
484 | }
485 | }
486 | #if HAVE_SMBC_SETOPTIONPROTOCOLS
487 | if (php_stream_context_get_option(context, "smb", "minproto", &tmpzval) == SUCCESS) {
488 | smbc_setOptionProtocols(state->ctx, Z_STRVAL_PP(tmpzval), NULL);
489 | }
490 | if (php_stream_context_get_option(context, "smb", "maxproto", &tmpzval) == SUCCESS) {
491 | smbc_setOptionProtocols(state->ctx, NULL, Z_STRVAL_PP(tmpzval));
492 | }
493 | #endif
494 | #endif
495 | }
496 | if (init) {
497 | if (php_smbclient_state_init(state TSRMLS_CC)) {
498 | php_smbclient_state_free(state TSRMLS_CC);
499 | return NULL;
500 | }
501 | }
502 | return state;
503 | }
504 |
505 | PHP_FUNCTION(smbclient_state_new)
506 | {
507 | php_smbclient_state *state;
508 |
509 | if (zend_parse_parameters_none() == FAILURE) {
510 | RETURN_FALSE;
511 | }
512 | if ((state = php_smbclient_state_new(NULL, 0 TSRMLS_CC)) == NULL) {
513 | RETURN_FALSE;
514 | }
515 | #if PHP_MAJOR_VERSION >= 7
516 | ZVAL_RES(return_value, zend_register_resource(state, le_smbclient_state));
517 | #else
518 | ZEND_REGISTER_RESOURCE(return_value, state, le_smbclient_state);
519 | #endif
520 | }
521 |
522 | PHP_FUNCTION(smbclient_version)
523 | {
524 | if (zend_parse_parameters_none() == FAILURE) {
525 | RETURN_FALSE;
526 | }
527 | #if PHP_MAJOR_VERSION >= 7
528 | RETURN_STRING(PHP_SMBCLIENT_VERSION);
529 | #else
530 | RETURN_STRING(PHP_SMBCLIENT_VERSION, 1);
531 | #endif
532 | }
533 |
534 | PHP_FUNCTION(smbclient_library_version)
535 | {
536 | if (zend_parse_parameters_none() == FAILURE) {
537 | RETURN_FALSE;
538 | }
539 | #if PHP_MAJOR_VERSION >= 7
540 | RETURN_STRING(smbc_version());
541 | #else
542 | RETURN_STRING(smbc_version(), 1);
543 | #endif
544 | }
545 |
546 | int
547 | php_smbclient_state_init (php_smbclient_state *state TSRMLS_DC)
548 | {
549 | SMBCCTX *ctx;
550 |
551 | if ((ctx = smbc_init_context(state->ctx)) != NULL) {
552 | state->ctx = ctx;
553 | return 0;
554 | }
555 | switch (state->err = errno) {
556 | case EBADF: php_error(E_WARNING, "Couldn't init SMB context: null context given"); break;
557 | case ENOMEM: php_error(E_WARNING, "Couldn't init SMB context: insufficient memory"); break;
558 | case ENOENT: php_error(E_WARNING, "Couldn't init SMB context: cannot load smb.conf"); break;
559 | default: php_error(E_WARNING, "Couldn't init SMB context: unknown error (%d)", errno); break;
560 | }
561 | return 1;
562 | }
563 |
564 | #if PHP_MAJOR_VERSION >= 7
565 |
566 | #define SMB_FETCH_RESOURCE(_state, _type, _zval, _name, _le) \
567 | if ((_state = (_type)zend_fetch_resource(Z_RES_P(*_zval), _name, _le)) == NULL) { \
568 | RETURN_FALSE; \
569 | }
570 | #else
571 |
572 | #define SMB_FETCH_RESOURCE(_state, _type, _zval, _name, _le) \
573 | ZEND_FETCH_RESOURCE(_state, _type, _zval, -1, _name, _le);
574 |
575 | #endif
576 |
577 | #define STATE_FROM_ZSTATE \
578 | SMB_FETCH_RESOURCE(state, php_smbclient_state *, &zstate, PHP_SMBCLIENT_STATE_NAME, le_smbclient_state); \
579 | if (state == NULL || state->ctx == NULL) { \
580 | php_error(E_WARNING, PHP_SMBCLIENT_STATE_NAME " not found"); \
581 | RETURN_FALSE; \
582 | }
583 |
584 | #define FILE_FROM_ZFILE \
585 | SMB_FETCH_RESOURCE(file, SMBCFILE *, &zfile, PHP_SMBCLIENT_FILE_NAME, le_smbclient_file); \
586 | if (file == NULL) { \
587 | php_error(E_WARNING, PHP_SMBCLIENT_FILE_NAME " not found"); \
588 | RETURN_FALSE; \
589 | }
590 |
591 | PHP_FUNCTION(smbclient_state_init)
592 | {
593 | zval *zstate;
594 | zval *zwrkg = NULL;
595 | zval *zuser = NULL;
596 | zval *zpass = NULL;
597 | php_smbclient_state *state;
598 |
599 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|zzz", &zstate, &zwrkg, &zuser, &zpass) != SUCCESS) {
600 | RETURN_FALSE;
601 | }
602 | SMB_FETCH_RESOURCE(state, php_smbclient_state *, &zstate, PHP_SMBCLIENT_STATE_NAME, le_smbclient_state);
603 |
604 | if (state->ctx == NULL) {
605 | php_error(E_WARNING, "Couldn't init SMB context: context is null");
606 | RETURN_FALSE;
607 | }
608 | if (ctx_init_getauth(zwrkg, &state->wrkg, &state->wrkglen, "workgroup") == 0) {
609 | RETURN_FALSE;
610 | }
611 | if (ctx_init_getauth(zuser, &state->user, &state->userlen, "username") == 0) {
612 | RETURN_FALSE;
613 | }
614 | if (ctx_init_getauth(zpass, &state->pass, &state->passlen, "password") == 0) {
615 | RETURN_FALSE;
616 | }
617 | if (php_smbclient_state_init(state TSRMLS_CC)) {
618 | RETURN_FALSE;
619 | }
620 | RETURN_TRUE;
621 | }
622 |
623 | PHP_FUNCTION(smbclient_state_errno)
624 | {
625 | zval *zstate;
626 | php_smbclient_state *state;
627 |
628 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstate) != SUCCESS) {
629 | RETURN_LONG(0);
630 | }
631 | SMB_FETCH_RESOURCE(state, php_smbclient_state *, &zstate, PHP_SMBCLIENT_STATE_NAME, le_smbclient_state);
632 | RETURN_LONG(state->err);
633 | }
634 |
635 | PHP_FUNCTION(smbclient_state_free)
636 | {
637 | zval *zstate;
638 | php_smbclient_state *state;
639 |
640 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstate) != SUCCESS) {
641 | RETURN_FALSE;
642 | }
643 | SMB_FETCH_RESOURCE(state, php_smbclient_state *, &zstate, PHP_SMBCLIENT_STATE_NAME, le_smbclient_state);
644 |
645 | if (state->ctx == NULL) {
646 | #if PHP_MAJOR_VERSION >= 7
647 | zend_list_close(Z_RES_P(zstate));
648 | #else
649 | zend_list_delete(Z_LVAL_P(zstate));
650 | #endif
651 | RETURN_TRUE;
652 | }
653 | if (smbc_free_context(state->ctx, 1) == 0) {
654 | state->ctx = NULL;
655 | #if PHP_MAJOR_VERSION >= 7
656 | zend_list_close(Z_RES_P(zstate));
657 | #else
658 | zend_list_delete(Z_LVAL_P(zstate));
659 | #endif
660 | RETURN_TRUE;
661 | }
662 | switch (state->err = errno) {
663 | case EBUSY: php_error(E_WARNING, "Couldn't destroy smbclient state: connection in use"); break;
664 | case EBADF: php_error(E_WARNING, "Couldn't destroy smbclient state: invalid handle"); break;
665 | default: php_error(E_WARNING, "Couldn't destroy smbclient state: unknown error (%d)", errno); break;
666 | }
667 | RETURN_FALSE;
668 | }
669 |
670 | PHP_FUNCTION(smbclient_opendir)
671 | {
672 | char *path;
673 | strsize_t path_len;
674 | zval *zstate;
675 | SMBCFILE *dir;
676 | smbc_opendir_fn smbc_opendir;
677 | php_smbclient_state *state;
678 |
679 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &zstate, &path, &path_len) == FAILURE) {
680 | return;
681 | }
682 | STATE_FROM_ZSTATE;
683 |
684 | if ((smbc_opendir = smbc_getFunctionOpendir(state->ctx)) == NULL) {
685 | RETURN_FALSE;
686 | }
687 | if ((dir = smbc_opendir(state->ctx, path)) != NULL) {
688 | #if PHP_MAJOR_VERSION >= 7
689 | ZVAL_RES(return_value, zend_register_resource(dir, le_smbclient_file));
690 | #else
691 | ZEND_REGISTER_RESOURCE(return_value, dir, le_smbclient_file);
692 | #endif
693 | return;
694 | }
695 | hide_password(path, path_len);
696 | switch (state->err = errno) {
697 | case EACCES: php_error(E_WARNING, "Couldn't open SMB directory %s: Permission denied", path); break;
698 | case EINVAL: php_error(E_WARNING, "Couldn't open SMB directory %s: Invalid URL", path); break;
699 | case ENOENT: php_error(E_WARNING, "Couldn't open SMB directory %s: Path does not exist", path); break;
700 | case ENOMEM: php_error(E_WARNING, "Couldn't open SMB directory %s: Insufficient memory", path); break;
701 | case ENOTDIR: php_error(E_WARNING, "Couldn't open SMB directory %s: Not a directory", path); break;
702 | case EPERM: php_error(E_WARNING, "Couldn't open SMB directory %s: Workgroup not found", path); break;
703 | case ENODEV: php_error(E_WARNING, "Couldn't open SMB directory %s: Workgroup or server not found", path); break;
704 | default: php_error(E_WARNING, "Couldn't open SMB directory %s: unknown error (%d)", path, errno); break;
705 | }
706 | RETURN_FALSE;
707 | }
708 |
709 | static char *
710 | type_to_string (unsigned int type)
711 | {
712 | switch (type) {
713 | case SMBC_WORKGROUP: return "workgroup";
714 | case SMBC_SERVER: return "server";
715 | case SMBC_FILE_SHARE: return "file share";
716 | case SMBC_PRINTER_SHARE: return "printer share";
717 | case SMBC_COMMS_SHARE: return "communication share";
718 | case SMBC_IPC_SHARE: return "IPC share";
719 | case SMBC_DIR: return "directory";
720 | case SMBC_FILE: return "file";
721 | case SMBC_LINK: return "link";
722 | }
723 | return "unknown";
724 | }
725 |
726 | PHP_FUNCTION(smbclient_readdir)
727 | {
728 | struct smbc_dirent *dirent;
729 | zval *zstate;
730 | zval *zfile;
731 | SMBCFILE *file;
732 | smbc_readdir_fn smbc_readdir;
733 | php_smbclient_state *state;
734 |
735 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &zstate, &zfile) == FAILURE) {
736 | return;
737 | }
738 | STATE_FROM_ZSTATE;
739 | FILE_FROM_ZFILE;
740 |
741 | if ((smbc_readdir = smbc_getFunctionReaddir(state->ctx)) == NULL) {
742 | RETURN_FALSE;
743 | }
744 | errno = 0;
745 | if ((dirent = smbc_readdir(state->ctx, file)) == NULL) {
746 | switch (state->err = errno) {
747 | case 0: RETURN_FALSE;
748 | case EBADF: php_error(E_WARNING, "Couldn't read " PHP_SMBCLIENT_FILE_NAME ": Not a directory resource"); break;
749 | case EINVAL: php_error(E_WARNING, "Couldn't read " PHP_SMBCLIENT_FILE_NAME ": State resource not initialized"); break;
750 | default: php_error(E_WARNING, "Couldn't read " PHP_SMBCLIENT_FILE_NAME ": unknown error (%d)", errno); break;
751 | }
752 | RETURN_FALSE;
753 | }
754 | array_init(return_value);
755 | #if PHP_MAJOR_VERSION >= 7
756 | add_assoc_string(return_value, "type", type_to_string(dirent->smbc_type));
757 | add_assoc_stringl(return_value, "comment", dirent->comment, dirent->commentlen);
758 | add_assoc_stringl(return_value, "name", dirent->name, dirent->namelen);
759 | #else
760 | add_assoc_string(return_value, "type", type_to_string(dirent->smbc_type), 1);
761 | add_assoc_stringl(return_value, "comment", dirent->comment, dirent->commentlen, 1);
762 | add_assoc_stringl(return_value, "name", dirent->name, dirent->namelen, 1);
763 | #endif
764 | }
765 |
766 | PHP_FUNCTION(smbclient_closedir)
767 | {
768 | zval *zstate;
769 | zval *zfile;
770 | SMBCFILE *file;
771 | smbc_closedir_fn smbc_closedir;
772 | php_smbclient_state *state;
773 |
774 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &zstate, &zfile) == FAILURE) {
775 | return;
776 | }
777 | STATE_FROM_ZSTATE;
778 | FILE_FROM_ZFILE;
779 |
780 | if ((smbc_closedir = smbc_getFunctionClosedir(state->ctx)) == NULL) {
781 | RETURN_FALSE;
782 | }
783 | if (smbc_closedir(state->ctx, file) == 0) {
784 | #if PHP_MAJOR_VERSION >= 7
785 | zend_list_close(Z_RES_P(zfile));
786 | #else
787 | zend_list_delete(Z_LVAL_P(zfile));
788 | #endif
789 | RETURN_TRUE;
790 | }
791 | switch (state->err = errno) {
792 | case EBADF: php_error(E_WARNING, "Couldn't close " PHP_SMBCLIENT_FILE_NAME ": Not a directory resource"); break;
793 | default: php_error(E_WARNING, "Couldn't close " PHP_SMBCLIENT_FILE_NAME ": unknown error (%d)", errno); break;
794 | }
795 | RETURN_FALSE;
796 | }
797 |
798 | PHP_FUNCTION(smbclient_rename)
799 | {
800 | char *ourl, *nurl;
801 | strsize_t ourl_len, nurl_len;
802 | zval *zstate_old;
803 | zval *zstate_new;
804 | smbc_rename_fn smbc_rename;
805 | php_smbclient_state *state_old;
806 | php_smbclient_state *state_new;
807 |
808 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsrs", &zstate_old, &ourl, &ourl_len, &zstate_new, &nurl, &nurl_len) == FAILURE) {
809 | return;
810 | }
811 | SMB_FETCH_RESOURCE(state_old, php_smbclient_state *, &zstate_old, PHP_SMBCLIENT_STATE_NAME, le_smbclient_state);
812 | SMB_FETCH_RESOURCE(state_new, php_smbclient_state *, &zstate_new, PHP_SMBCLIENT_STATE_NAME, le_smbclient_state);
813 |
814 | if (state_old == NULL || state_old->ctx == NULL) {
815 | php_error(E_WARNING, "old " PHP_SMBCLIENT_STATE_NAME " is null");
816 | RETURN_FALSE;
817 | }
818 | if (state_new == NULL || state_new->ctx == NULL) {
819 | php_error(E_WARNING, "new " PHP_SMBCLIENT_STATE_NAME " is null");
820 | RETURN_FALSE;
821 | }
822 | if ((smbc_rename = smbc_getFunctionRename(state_old->ctx)) == NULL) {
823 | RETURN_FALSE;
824 | }
825 | if (smbc_rename(state_old->ctx, ourl, state_new->ctx, nurl) == 0) {
826 | RETURN_TRUE;
827 | }
828 | hide_password(ourl, ourl_len);
829 | switch (state_old->err = errno) {
830 | case EISDIR: php_error(E_WARNING, "Couldn't rename SMB directory %s: existing url is not a directory", ourl); break;
831 | case EACCES: php_error(E_WARNING, "Couldn't open SMB directory %s: Permission denied", ourl); break;
832 | case EINVAL: php_error(E_WARNING, "Couldn't open SMB directory %s: Invalid URL", ourl); break;
833 | case ENOENT: php_error(E_WARNING, "Couldn't open SMB directory %s: Path does not exist", ourl); break;
834 | case ENOMEM: php_error(E_WARNING, "Couldn't open SMB directory %s: Insufficient memory", ourl); break;
835 | case ENOTDIR: php_error(E_WARNING, "Couldn't open SMB directory %s: Not a directory", ourl); break;
836 | case EPERM: php_error(E_WARNING, "Couldn't open SMB directory %s: Workgroup not found", ourl); break;
837 | case EXDEV: php_error(E_WARNING, "Couldn't open SMB directory %s: Workgroup or server not found", ourl); break;
838 | case EEXIST: php_error(E_WARNING, "Couldn't rename SMB directory %s: new name already exists", ourl); break;
839 | default: php_error(E_WARNING, "Couldn't open SMB directory %s: unknown error (%d)", ourl, errno); break;
840 | }
841 | RETURN_FALSE;
842 | }
843 |
844 | PHP_FUNCTION(smbclient_unlink)
845 | {
846 | char *url;
847 | strsize_t url_len;
848 | zval *zstate;
849 | smbc_unlink_fn smbc_unlink;
850 | php_smbclient_state *state;
851 |
852 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &zstate, &url, &url_len) == FAILURE) {
853 | return;
854 | }
855 | STATE_FROM_ZSTATE;
856 |
857 | if ((smbc_unlink = smbc_getFunctionUnlink(state->ctx)) == NULL) {
858 | RETURN_FALSE;
859 | }
860 | if (smbc_unlink(state->ctx, url) == 0) {
861 | RETURN_TRUE;
862 | }
863 | hide_password(url, url_len);
864 | switch (state->err = errno) {
865 | case EACCES: php_error(E_WARNING, "Couldn't delete %s: Permission denied", url); break;
866 | case EINVAL: php_error(E_WARNING, "Couldn't delete %s: Invalid URL", url); break;
867 | case ENOENT: php_error(E_WARNING, "Couldn't delete %s: Path does not exist", url); break;
868 | case ENOMEM: php_error(E_WARNING, "Couldn't delete %s: Insufficient memory", url); break;
869 | case EPERM: php_error(E_WARNING, "Couldn't delete %s: Workgroup not found", url); break;
870 | case EISDIR: php_error(E_WARNING, "Couldn't delete %s: It is a Directory (use rmdir instead)", url); break;
871 | case EBUSY: php_error(E_WARNING, "Couldn't delete %s: Device or resource busy", url); break;
872 | default: php_error(E_WARNING, "Couldn't delete %s: unknown error (%d)", url, errno); break;
873 | }
874 | RETURN_FALSE;
875 | }
876 |
877 | PHP_FUNCTION(smbclient_mkdir)
878 | {
879 | char *path = NULL;
880 | strsize_t path_len;
881 | zend_long mode = 0777; /* Same as PHP's native mkdir() */
882 | zval *zstate;
883 | smbc_mkdir_fn smbc_mkdir;
884 | php_smbclient_state *state;
885 |
886 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|l", &zstate, &path, &path_len, &mode) == FAILURE) {
887 | return;
888 | }
889 | STATE_FROM_ZSTATE;
890 |
891 | if ((smbc_mkdir = smbc_getFunctionMkdir(state->ctx)) == NULL) {
892 | RETURN_FALSE;
893 | }
894 | if (smbc_mkdir(state->ctx, path, (mode_t)mode) == 0) {
895 | RETURN_TRUE;
896 | }
897 | hide_password(path, path_len);
898 | switch (state->err = errno) {
899 | case EACCES: php_error(E_WARNING, "Couldn't create SMB directory %s: Permission denied", path); break;
900 | case EINVAL: php_error(E_WARNING, "Couldn't create SMB directory %s: Invalid URL", path); break;
901 | case ENOENT: php_error(E_WARNING, "Couldn't create SMB directory %s: Path does not exist", path); break;
902 | case ENOMEM: php_error(E_WARNING, "Couldn't create SMB directory %s: Insufficient memory", path); break;
903 | case EEXIST: php_error(E_WARNING, "Couldn't create SMB directory %s: Directory already exists", path); break;
904 | default: php_error(E_WARNING, "Couldn't create SMB directory %s: unknown error (%d)", path, errno); break;
905 | }
906 | RETURN_FALSE;
907 | }
908 |
909 | PHP_FUNCTION(smbclient_rmdir)
910 | {
911 | char *url;
912 | strsize_t url_len;
913 | zval *zstate;
914 | smbc_rmdir_fn smbc_rmdir;
915 | php_smbclient_state *state;
916 |
917 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &zstate, &url, &url_len) == FAILURE) {
918 | return;
919 | }
920 | STATE_FROM_ZSTATE;
921 |
922 | if ((smbc_rmdir = smbc_getFunctionRmdir(state->ctx)) == NULL) {
923 | RETURN_FALSE;
924 | }
925 | if (smbc_rmdir(state->ctx, url) == 0) {
926 | RETURN_TRUE;
927 | }
928 | hide_password(url, url_len);
929 | switch (state->err = errno) {
930 | case EACCES: php_error(E_WARNING, "Couldn't delete %s: Permission denied", url); break;
931 | case EINVAL: php_error(E_WARNING, "Couldn't delete %s: Invalid URL", url); break;
932 | case ENOENT: php_error(E_WARNING, "Couldn't delete %s: Path does not exist", url); break;
933 | case ENOMEM: php_error(E_WARNING, "Couldn't delete %s: Insufficient memory", url); break;
934 | case EPERM: php_error(E_WARNING, "Couldn't delete %s: Workgroup not found", url); break;
935 | case ENOTEMPTY: php_error(E_WARNING, "Couldn't delete %s: It is not empty", url); break;
936 | default: php_error(E_WARNING, "Couldn't delete %s: unknown error (%d)", url, errno); break;
937 | }
938 | RETURN_FALSE;
939 | }
940 |
941 | PHP_FUNCTION(smbclient_stat)
942 | {
943 | char *file;
944 | struct stat statbuf;
945 | strsize_t file_len;
946 | zval *zstate;
947 | smbc_stat_fn smbc_stat;
948 | php_smbclient_state *state;
949 |
950 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &zstate, &file, &file_len) == FAILURE) {
951 | return;
952 | }
953 | STATE_FROM_ZSTATE;
954 |
955 | if ((smbc_stat = smbc_getFunctionStat(state->ctx)) == NULL) {
956 | RETURN_FALSE;
957 | }
958 | if (smbc_stat(state->ctx, file, &statbuf) < 0) {
959 | hide_password(file, file_len);
960 | switch (state->err = errno) {
961 | case ENOENT: php_error(E_WARNING, "Couldn't stat %s: Does not exist", file); break;
962 | case EINVAL: php_error(E_WARNING, "Couldn't stat: null URL or smbc_init failed"); break;
963 | case EACCES: php_error(E_WARNING, "Couldn't stat %s: Permission denied", file); break;
964 | case ENOMEM: php_error(E_WARNING, "Couldn't stat %s: Out of memory", file); break;
965 | case ENOTDIR: php_error(E_WARNING, "Couldn't stat %s: Not a directory", file); break;
966 | default: php_error(E_WARNING, "Couldn't stat %s: unknown error (%d)", file, errno); break;
967 | }
968 | RETURN_FALSE;
969 | }
970 | array_init(return_value);
971 | add_index_long(return_value, 0, statbuf.st_dev);
972 | add_index_long(return_value, 1, statbuf.st_ino);
973 | add_index_long(return_value, 2, statbuf.st_mode);
974 | add_index_long(return_value, 3, statbuf.st_nlink);
975 | add_index_long(return_value, 4, statbuf.st_uid);
976 | add_index_long(return_value, 5, statbuf.st_gid);
977 | add_index_long(return_value, 6, statbuf.st_rdev);
978 | add_index_long(return_value, 7, statbuf.st_size);
979 | add_index_long(return_value, 8, statbuf.st_atime);
980 | add_index_long(return_value, 9, statbuf.st_mtime);
981 | add_index_long(return_value, 10, statbuf.st_ctime);
982 | add_index_long(return_value, 11, statbuf.st_blksize);
983 | add_index_long(return_value, 12, statbuf.st_blocks);
984 |
985 | add_assoc_long(return_value, "dev", statbuf.st_dev);
986 | add_assoc_long(return_value, "ino", statbuf.st_ino);
987 | add_assoc_long(return_value, "mode", statbuf.st_mode);
988 | add_assoc_long(return_value, "nlink", statbuf.st_nlink);
989 | add_assoc_long(return_value, "uid", statbuf.st_uid);
990 | add_assoc_long(return_value, "gid", statbuf.st_gid);
991 | add_assoc_long(return_value, "rdev", statbuf.st_rdev);
992 | add_assoc_long(return_value, "size", statbuf.st_size);
993 | add_assoc_long(return_value, "atime", statbuf.st_atime);
994 | add_assoc_long(return_value, "mtime", statbuf.st_mtime);
995 | add_assoc_long(return_value, "ctime", statbuf.st_ctime);
996 | add_assoc_long(return_value, "blksize", statbuf.st_blksize);
997 | add_assoc_long(return_value, "blocks", statbuf.st_blocks);
998 | }
999 |
1000 | PHP_FUNCTION(smbclient_fstat)
1001 | {
1002 | struct stat statbuf;
1003 | zval *zstate;
1004 | zval *zfile;
1005 | SMBCFILE *file;
1006 | smbc_fstat_fn smbc_fstat;
1007 | php_smbclient_state *state;
1008 |
1009 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &zstate, &zfile) == FAILURE) {
1010 | return;
1011 | }
1012 | STATE_FROM_ZSTATE;
1013 | FILE_FROM_ZFILE;
1014 |
1015 | if ((smbc_fstat = smbc_getFunctionFstat(state->ctx)) == NULL) {
1016 | RETURN_FALSE;
1017 | }
1018 | if (smbc_fstat(state->ctx, file, &statbuf) < 0) {
1019 | switch (state->err = errno) {
1020 | case ENOENT: php_error(E_WARNING, "Couldn't fstat " PHP_SMBCLIENT_FILE_NAME ": Does not exist"); break;
1021 | case EINVAL: php_error(E_WARNING, "Couldn't fstat: null resource or smbc_init failed"); break;
1022 | case EACCES: php_error(E_WARNING, "Couldn't fstat " PHP_SMBCLIENT_FILE_NAME ": Permission denied"); break;
1023 | case ENOMEM: php_error(E_WARNING, "Couldn't fstat " PHP_SMBCLIENT_FILE_NAME ": Out of memory"); break;
1024 | case ENOTDIR: php_error(E_WARNING, "Couldn't fstat " PHP_SMBCLIENT_FILE_NAME ": Not a directory"); break;
1025 | default: php_error(E_WARNING, "Couldn't fstat " PHP_SMBCLIENT_FILE_NAME ": unknown error (%d)", errno); break;
1026 | }
1027 | RETURN_FALSE;
1028 | }
1029 | array_init(return_value);
1030 | add_index_long(return_value, 0, statbuf.st_dev);
1031 | add_index_long(return_value, 1, statbuf.st_ino);
1032 | add_index_long(return_value, 2, statbuf.st_mode);
1033 | add_index_long(return_value, 3, statbuf.st_nlink);
1034 | add_index_long(return_value, 4, statbuf.st_uid);
1035 | add_index_long(return_value, 5, statbuf.st_gid);
1036 | add_index_long(return_value, 6, statbuf.st_rdev);
1037 | add_index_long(return_value, 7, statbuf.st_size);
1038 | add_index_long(return_value, 8, statbuf.st_atime);
1039 | add_index_long(return_value, 9, statbuf.st_mtime);
1040 | add_index_long(return_value, 10, statbuf.st_ctime);
1041 | add_index_long(return_value, 11, statbuf.st_blksize);
1042 | add_index_long(return_value, 12, statbuf.st_blocks);
1043 |
1044 | add_assoc_long(return_value, "dev", statbuf.st_dev);
1045 | add_assoc_long(return_value, "ino", statbuf.st_ino);
1046 | add_assoc_long(return_value, "mode", statbuf.st_mode);
1047 | add_assoc_long(return_value, "nlink", statbuf.st_nlink);
1048 | add_assoc_long(return_value, "uid", statbuf.st_uid);
1049 | add_assoc_long(return_value, "gid", statbuf.st_gid);
1050 | add_assoc_long(return_value, "rdev", statbuf.st_rdev);
1051 | add_assoc_long(return_value, "size", statbuf.st_size);
1052 | add_assoc_long(return_value, "atime", statbuf.st_atime);
1053 | add_assoc_long(return_value, "mtime", statbuf.st_mtime);
1054 | add_assoc_long(return_value, "ctime", statbuf.st_ctime);
1055 | add_assoc_long(return_value, "blksize", statbuf.st_blksize);
1056 | add_assoc_long(return_value, "blocks", statbuf.st_blocks);
1057 | }
1058 |
1059 | int
1060 | flagstring_to_smbflags (const char *flags, int flags_len, int *retval TSRMLS_DC)
1061 | {
1062 | /* Returns 0 on failure, or 1 on success with *retval filled. */
1063 | if (flags_len != 1 && flags_len != 2) {
1064 | goto err;
1065 | }
1066 | if (flags_len == 2 && flags[1] != '+') {
1067 | goto err;
1068 | }
1069 | /* For both lengths, add the "core business" flags.
1070 | * See php_stream_parse_fopen_modes() in PHP's /main/streams/plain_wrapper.c
1071 | * for how PHP's native fopen() translates these flags: */
1072 | switch (flags[0]) {
1073 | case 'r': *retval = 0; break;
1074 | case 'w': *retval = O_CREAT | O_TRUNC; break;
1075 | case 'a': *retval = O_CREAT | O_APPEND; break;
1076 | case 'x': *retval = O_CREAT | O_EXCL; break;
1077 | case 'c': *retval = O_CREAT; break;
1078 | default: goto err;
1079 | }
1080 | /* If length is 1, enforce read-only or write-only: */
1081 | if (flags_len == 1) {
1082 | *retval |= (*retval == 0) ? O_RDONLY : O_WRONLY;
1083 | return 1;
1084 | }
1085 | /* Length is 2 and this is a '+' mode, so read/write everywhere: */
1086 | *retval |= O_RDWR;
1087 | return 1;
1088 |
1089 | err:
1090 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid flag string '%s'", flags);
1091 | return 0;
1092 | }
1093 |
1094 | PHP_FUNCTION(smbclient_open)
1095 | {
1096 | char *file, *flags;
1097 | strsize_t file_len, flags_len;
1098 | int smbflags;
1099 | zend_long mode = 0666;
1100 | zval *zstate;
1101 | SMBCFILE *handle;
1102 | smbc_open_fn smbc_open;
1103 | php_smbclient_state *state;
1104 |
1105 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss|l", &zstate, &file, &file_len, &flags, &flags_len, &mode) == FAILURE) {
1106 | return;
1107 | }
1108 | STATE_FROM_ZSTATE;
1109 |
1110 | /* The flagstring is in the same format as the native fopen() uses, so
1111 | * one of the characters r, w, a, x, c, optionally followed by a plus.
1112 | * Need to translate this to an integer value for smbc_open: */
1113 | if (flagstring_to_smbflags(flags, flags_len, &smbflags TSRMLS_CC) == 0) {
1114 | RETURN_FALSE;
1115 | }
1116 | if ((smbc_open = smbc_getFunctionOpen(state->ctx)) == NULL) {
1117 | RETURN_FALSE;
1118 | }
1119 | if ((handle = smbc_open(state->ctx, file, smbflags, mode)) != NULL) {
1120 | #if PHP_MAJOR_VERSION >= 7
1121 | ZVAL_RES(return_value, zend_register_resource(handle, le_smbclient_file));
1122 | #else
1123 | ZEND_REGISTER_RESOURCE(return_value, handle, le_smbclient_file);
1124 | #endif
1125 | return;
1126 | }
1127 | hide_password(file, file_len);
1128 | switch (state->err = errno) {
1129 | case ENOMEM: php_error(E_WARNING, "Couldn't open %s: Out of memory", file); break;
1130 | case EINVAL: php_error(E_WARNING, "Couldn't open %s: No file?", file); break;
1131 | case EEXIST: php_error(E_WARNING, "Couldn't open %s: Pathname already exists and O_CREAT and O_EXECL were specified", file); break;
1132 | case EISDIR: php_error(E_WARNING, "Couldn't open %s: Can't write to a directory", file); break;
1133 | case EACCES: php_error(E_WARNING, "Couldn't open %s: Access denied", file); break;
1134 | case ENODEV: php_error(E_WARNING, "Couldn't open %s: Requested share does not exist", file); break;
1135 | case ENOTDIR: php_error(E_WARNING, "Couldn't open %s: Path component isn't a directory", file); break;
1136 | case ENOENT: php_error(E_WARNING, "Couldn't open %s: Directory in path doesn't exist", file); break;
1137 | default: php_error(E_WARNING, "Couldn't open %s: unknown error (%d)", file, errno); break;
1138 | }
1139 | RETURN_FALSE;
1140 | }
1141 |
1142 | PHP_FUNCTION(smbclient_creat)
1143 | {
1144 | char *file;
1145 | strsize_t file_len;
1146 | zend_long mode = 0666;
1147 | zval *zstate;
1148 | SMBCFILE *handle;
1149 | smbc_creat_fn smbc_creat;
1150 | php_smbclient_state *state;
1151 |
1152 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|l", &zstate, &file, &file_len, &mode) == FAILURE) {
1153 | return;
1154 | }
1155 | STATE_FROM_ZSTATE;
1156 |
1157 | if ((smbc_creat = smbc_getFunctionCreat(state->ctx)) == NULL) {
1158 | RETURN_FALSE;
1159 | }
1160 | if ((handle = smbc_creat(state->ctx, file, (mode_t)mode)) != NULL) {
1161 | #if PHP_MAJOR_VERSION >= 7
1162 | ZVAL_RES(return_value, zend_register_resource(handle, le_smbclient_file));
1163 | #else
1164 | ZEND_REGISTER_RESOURCE(return_value, handle, le_smbclient_file);
1165 | #endif
1166 |
1167 | return;
1168 | }
1169 | hide_password(file, file_len);
1170 | switch (state->err = errno) {
1171 | case ENOMEM: php_error(E_WARNING, "Couldn't create %s: Out of memory", file); break;
1172 | case EINVAL: php_error(E_WARNING, "Couldn't create %s: No file?", file); break;
1173 | case EEXIST: php_error(E_WARNING, "Couldn't create %s: Pathname already exists and O_CREAT and O_EXECL were specified", file); break;
1174 | case EISDIR: php_error(E_WARNING, "Couldn't create %s: Can't write to a directory", file); break;
1175 | case EACCES: php_error(E_WARNING, "Couldn't create %s: Access denied", file); break;
1176 | case ENODEV: php_error(E_WARNING, "Couldn't create %s: Requested share does not exist", file); break;
1177 | case ENOENT: php_error(E_WARNING, "Couldn't create %s: Directory in path doesn't exist", file); break;
1178 | default: php_error(E_WARNING, "Couldn't create %s: unknown error (%d)", file, errno); break;
1179 | }
1180 | RETURN_FALSE;
1181 | }
1182 |
1183 | PHP_FUNCTION(smbclient_read)
1184 | {
1185 | zend_long count;
1186 | zval *zstate;
1187 | zval *zfile;
1188 | SMBCFILE *file;
1189 | smbc_read_fn smbc_read;
1190 | php_smbclient_state *state;
1191 |
1192 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrl", &zstate, &zfile, &count) == FAILURE) {
1193 | return;
1194 | }
1195 | if (count < 0) {
1196 | php_error(E_WARNING, "Negative byte count: " ZEND_LONG_FMT, count);
1197 | RETURN_FALSE;
1198 | }
1199 | STATE_FROM_ZSTATE;
1200 | FILE_FROM_ZFILE;
1201 |
1202 | if ((smbc_read = smbc_getFunctionRead(state->ctx)) == NULL) {
1203 | RETURN_FALSE;
1204 | }
1205 | #if PHP_MAJOR_VERSION >= 7
1206 | zend_string *buf = zend_string_alloc(count, 0);
1207 |
1208 | if ((ZSTR_LEN(buf) = smbc_read(state->ctx, file, ZSTR_VAL(buf), count)) >= 0) {
1209 | RETURN_STR(buf);
1210 | }
1211 | zend_string_release(buf);
1212 | #else
1213 | void *buf = emalloc(count);
1214 | ssize_t nbytes;
1215 |
1216 | if ((nbytes = smbc_read(state->ctx, file, buf, count)) >= 0) {
1217 | RETURN_STRINGL(buf, nbytes, 0);
1218 | }
1219 | efree(buf);
1220 | #endif
1221 | switch (state->err = errno) {
1222 | case EISDIR: php_error(E_WARNING, "Read error: Is a directory"); break;
1223 | case EBADF: php_error(E_WARNING, "Read error: Not a valid file resource or not open for reading"); break;
1224 | case EINVAL: php_error(E_WARNING, "Read error: Object not suitable for reading or bad buffer"); break;
1225 | default: php_error(E_WARNING, "Read error: unknown error (%d)", errno); break;
1226 | }
1227 | RETURN_FALSE;
1228 | }
1229 |
1230 | PHP_FUNCTION(smbclient_write)
1231 | {
1232 | zend_long count = 0;
1233 | strsize_t str_len;
1234 | char * str;
1235 | size_t nwrite;
1236 | ssize_t nbytes;
1237 | zval *zstate;
1238 | zval *zfile;
1239 | SMBCFILE *file;
1240 | smbc_write_fn smbc_write;
1241 | php_smbclient_state *state;
1242 |
1243 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrs|l", &zstate, &zfile, &str, &str_len, &count) == FAILURE) {
1244 | return;
1245 | }
1246 | if (count < 0) {
1247 | php_error(E_WARNING, "Negative byte count: " ZEND_LONG_FMT, count);
1248 | RETURN_FALSE;
1249 | }
1250 | if (count == 0 || count > str_len) {
1251 | nwrite = str_len;
1252 | }
1253 | else {
1254 | nwrite = count;
1255 | }
1256 | STATE_FROM_ZSTATE;
1257 | FILE_FROM_ZFILE;
1258 |
1259 | if ((smbc_write = smbc_getFunctionWrite(state->ctx)) == NULL) {
1260 | RETURN_FALSE;
1261 | }
1262 | if ((nbytes = smbc_write(state->ctx, file, str, nwrite)) >= 0) {
1263 | RETURN_LONG(nbytes);
1264 | }
1265 | switch (state->err = errno) {
1266 | case EISDIR: php_error(E_WARNING, "Write error: Is a directory"); break;
1267 | case EBADF: php_error(E_WARNING, "Write error: Not a valid file resource or not open for reading"); break;
1268 | case EINVAL: php_error(E_WARNING, "Write error: Object not suitable for reading or bad buffer"); break;
1269 | case EACCES: php_error(E_WARNING, "Write error: Permission denied"); break;
1270 | default: php_error(E_WARNING, "Write error: unknown error (%d)", errno); break;
1271 | }
1272 | RETURN_FALSE;
1273 | }
1274 |
1275 | PHP_FUNCTION(smbclient_lseek)
1276 | {
1277 | zend_long offset, whence, ret;
1278 | zval *zstate;
1279 | zval *zfile;
1280 | SMBCFILE *file;
1281 | smbc_lseek_fn smbc_lseek;
1282 | php_smbclient_state *state;
1283 |
1284 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrll", &zstate, &zfile, &offset, &whence) == FAILURE) {
1285 | return;
1286 | }
1287 | if ((int)whence != SEEK_SET && (int)whence != SEEK_CUR && (int)whence != SEEK_END) {
1288 | php_error(E_WARNING, "Invalid argument for whence");
1289 | RETURN_FALSE;
1290 | }
1291 | STATE_FROM_ZSTATE;
1292 | FILE_FROM_ZFILE;
1293 |
1294 | if ((smbc_lseek = smbc_getFunctionLseek(state->ctx)) == NULL) {
1295 | RETURN_FALSE;
1296 | }
1297 | if ((ret = smbc_lseek(state->ctx, file, (off_t)offset, (int)whence)) > -1) {
1298 | RETURN_LONG(ret);
1299 | }
1300 | switch (state->err = errno) {
1301 | case EBADF: php_error(E_WARNING, "Couldn't lseek: resource is invalid"); break;
1302 | case EINVAL: php_error(E_WARNING, "Couldn't lseek: invalid parameters or not initialized"); break;
1303 | default: php_error(E_WARNING, "Couldn't lseek: unknown error (%d)", errno); break;
1304 | }
1305 | RETURN_FALSE;
1306 | }
1307 |
1308 | PHP_FUNCTION(smbclient_ftruncate)
1309 | {
1310 | zend_long offset;
1311 | zval *zstate;
1312 | zval *zfile;
1313 | SMBCFILE *file;
1314 | smbc_ftruncate_fn smbc_ftruncate;
1315 | php_smbclient_state *state;
1316 |
1317 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrl", &zstate, &zfile, &offset) == FAILURE) {
1318 | return;
1319 | }
1320 | STATE_FROM_ZSTATE;
1321 | FILE_FROM_ZFILE;
1322 |
1323 | if ((smbc_ftruncate = smbc_getFunctionFtruncate(state->ctx)) == NULL) {
1324 | RETURN_FALSE;
1325 | }
1326 | if (smbc_ftruncate(state->ctx, file, offset) == 0) {
1327 | RETURN_TRUE;
1328 | }
1329 | switch (state->err = errno) {
1330 | case EBADF: php_error(E_WARNING, "Couldn't ftruncate: resource is invalid"); break;
1331 | case EACCES: php_error(E_WARNING, "Couldn't ftruncate: permission denied"); break;
1332 | case EINVAL: php_error(E_WARNING, "Couldn't ftruncate: invalid parameters or not initialized"); break;
1333 | case ENOMEM: php_error(E_WARNING, "Couldn't ftruncate: out of memory"); break;
1334 | default: php_error(E_WARNING, "Couldn't ftruncate: unknown error (%d)", errno); break;
1335 | }
1336 | RETURN_FALSE;
1337 | }
1338 |
1339 | PHP_FUNCTION(smbclient_close)
1340 | {
1341 | zval *zstate;
1342 | zval *zfile;
1343 | SMBCFILE *file;
1344 | smbc_close_fn smbc_close;
1345 | php_smbclient_state *state;
1346 |
1347 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &zstate, &zfile) == FAILURE) {
1348 | return;
1349 | }
1350 | STATE_FROM_ZSTATE;
1351 | FILE_FROM_ZFILE;
1352 |
1353 | if ((smbc_close = smbc_getFunctionClose(state->ctx)) == NULL) {
1354 | RETURN_FALSE;
1355 | }
1356 | if (smbc_close(state->ctx, file) == 0) {
1357 | #if PHP_MAJOR_VERSION >= 7
1358 | zend_list_close(Z_RES_P(zfile));
1359 | #else
1360 | zend_list_delete(Z_LVAL_P(zfile));
1361 | #endif
1362 | RETURN_TRUE;
1363 | }
1364 | switch (state->err = errno) {
1365 | case EBADF: php_error(E_WARNING, "Close error: Not a valid file resource or not open for reading"); break;
1366 | case EINVAL: php_error(E_WARNING, "Close error: State resource not initialized"); break;
1367 | default: php_error(E_WARNING, "Close error: unknown error (%d)", errno); break;
1368 | }
1369 | RETURN_FALSE;
1370 | }
1371 |
1372 | PHP_FUNCTION(smbclient_chmod)
1373 | {
1374 | char *file;
1375 | strsize_t file_len;
1376 | zend_long mode;
1377 | zval *zstate;
1378 | smbc_chmod_fn smbc_chmod;
1379 | php_smbclient_state *state;
1380 |
1381 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsl", &zstate, &file, &file_len, &mode) == FAILURE) {
1382 | return;
1383 | }
1384 | STATE_FROM_ZSTATE;
1385 |
1386 | if ((smbc_chmod = smbc_getFunctionChmod(state->ctx)) == NULL) {
1387 | RETURN_FALSE;
1388 | }
1389 | if (smbc_chmod(state->ctx, file, (mode_t)mode) == 0) {
1390 | RETURN_TRUE;
1391 | }
1392 | hide_password(file, file_len);
1393 | switch (state->err = errno) {
1394 | case EPERM: php_error(E_WARNING, "Couldn't chmod %s: the effective UID does not match the owner of the file, and is not zero", file); break;
1395 | case ENOENT: php_error(E_WARNING, "Couldn't chmod %s: file or directory does not exist", file); break;
1396 | case ENOMEM: php_error(E_WARNING, "Couldn't chmod %s: insufficient memory", file); break;
1397 | default: php_error(E_WARNING, "Couldn't chmod %s: unknown error (%d)", file, errno); break;
1398 | }
1399 | RETURN_FALSE;
1400 | }
1401 |
1402 | PHP_FUNCTION(smbclient_utimes)
1403 | {
1404 | char *file;
1405 | strsize_t file_len;
1406 | zend_long mtime = -1, atime = -1;
1407 | zval *zstate;
1408 | struct timeval times[2];
1409 | smbc_utimes_fn smbc_utimes;
1410 | php_smbclient_state *state;
1411 |
1412 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|ll", &zstate, &file, &file_len, &mtime, &atime) == FAILURE) {
1413 | return;
1414 | }
1415 | STATE_FROM_ZSTATE;
1416 |
1417 | times[0].tv_usec = 0; /* times[0] = access time (atime) */
1418 | times[1].tv_usec = 0; /* times[1] = write time (mtime) */
1419 |
1420 | /* TODO: we are a bit lazy here about the optional arguments and assume
1421 | * that if they are negative, they were omitted. This makes it
1422 | * impossible to use legitimate negative timestamps - a rare use-case. */
1423 | times[1].tv_sec = (mtime < 0) ? time(NULL) : mtime;
1424 |
1425 | /* If not given, atime defaults to value of mtime: */
1426 | times[0].tv_sec = (atime < 0) ? times[1].tv_sec : atime;
1427 |
1428 | if ((smbc_utimes = smbc_getFunctionUtimes(state->ctx)) == NULL) {
1429 | RETURN_FALSE;
1430 | }
1431 | if (smbc_utimes(state->ctx, file, times) == 0) {
1432 | RETURN_TRUE;
1433 | }
1434 | hide_password(file, file_len);
1435 | switch (state->err = errno) {
1436 | case EINVAL: php_error(E_WARNING, "Couldn't set times on %s: the client library is not properly initialized", file); break;
1437 | case EPERM: php_error(E_WARNING, "Couldn't set times on %s: permission was denied", file); break;
1438 | default: php_error(E_WARNING, "Couldn't set times on %s: unknown error (%d)", file, errno); break;
1439 | }
1440 | RETURN_FALSE;
1441 | }
1442 |
1443 | PHP_FUNCTION(smbclient_listxattr)
1444 | {
1445 | char *url, *s, *c;
1446 | strsize_t url_len;
1447 | char values[1000];
1448 | zval *zstate;
1449 | smbc_listxattr_fn smbc_listxattr;
1450 | php_smbclient_state *state;
1451 |
1452 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &zstate, &url, &url_len) == FAILURE) {
1453 | return;
1454 | }
1455 | STATE_FROM_ZSTATE;
1456 |
1457 | if ((smbc_listxattr = smbc_getFunctionListxattr(state->ctx)) == NULL) {
1458 | RETURN_FALSE;
1459 | }
1460 | /* This is a bit of a bogus function. Looking in the Samba source, it
1461 | * always returns all possible attribute names, regardless of what the
1462 | * file system supports or which attributes the file actually has.
1463 | * Because this list is static, we can get away with using a fixed
1464 | * buffer size.*/
1465 | if (smbc_listxattr(state->ctx, url, values, sizeof(values)) >= 0) {
1466 | array_init(return_value);
1467 | /* Each attribute is null-separated, the list itself terminates
1468 | * with an empty element (i.e. two null bytes in a row). */
1469 | for (s = c = values; c < values + sizeof(values); c++) {
1470 | if (*c != '\0') {
1471 | continue;
1472 | }
1473 | /* c and s identical: last element */
1474 | if (s == c) {
1475 | break;
1476 | }
1477 | #if PHP_MAJOR_VERSION >= 7
1478 | add_next_index_stringl(return_value, s, c - s);
1479 | #else
1480 | add_next_index_stringl(return_value, s, c - s, 1);
1481 | #endif
1482 | s = c + 1;
1483 | }
1484 | return;
1485 | }
1486 | switch (state->err = errno) {
1487 | case EINVAL: php_error(E_WARNING, "Couldn't get xattrs: library not initialized"); break;
1488 | case ENOMEM: php_error(E_WARNING, "Couldn't get xattrs: out of memory"); break;
1489 | case EPERM: php_error(E_WARNING, "Couldn't get xattrs: permission denied"); break;
1490 | case ENOTSUP: php_error(E_WARNING, "Couldn't get xattrs: file system does not support extended attributes"); break;
1491 | default: php_error(E_WARNING, "Couldn't get xattrs: unknown error (%d)", errno); break;
1492 | }
1493 | RETURN_FALSE;
1494 | }
1495 |
1496 |
1497 | /* loop from 16K to 256M */
1498 | #define DEFAULT_BUFFER_SIZE (16 << 10)
1499 | #define MAXIMUM_BUFFER_SIZE (256 << 20)
1500 |
1501 | PHP_FUNCTION(smbclient_getxattr)
1502 | {
1503 | char *url, *name;
1504 | strsize_t url_len, name_len;
1505 | int retsize;
1506 | int xattr_size;
1507 | zval *zstate;
1508 | smbc_getxattr_fn smbc_getxattr;
1509 | php_smbclient_state *state;
1510 | char *values = NULL;
1511 |
1512 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &zstate, &url, &url_len, &name, &name_len) == FAILURE) {
1513 | return;
1514 | }
1515 | STATE_FROM_ZSTATE;
1516 |
1517 | if ((smbc_getxattr = smbc_getFunctionGetxattr(state->ctx)) == NULL) {
1518 | RETURN_FALSE;
1519 | }
1520 |
1521 | xattr_size = smbc_getxattr(state->ctx, url, name, NULL, 0);
1522 |
1523 | if (xattr_size < 0) {
1524 | goto fail;
1525 | }
1526 |
1527 | if (xattr_size == 0) {
1528 | /* since version 4.16.9 and 4.17.5 this means success :(
1529 | * so there is no way to compute the buffer size
1530 | * see https://bugzilla.samba.org/show_bug.cgi?id=14808
1531 | * This regression is fixed in 4.18.7 and 4.19.0
1532 | */
1533 | xattr_size = DEFAULT_BUFFER_SIZE;
1534 | do {
1535 | if (values) {
1536 | efree(values);
1537 | xattr_size *= 4;
1538 | }
1539 | values = emalloc(xattr_size + 1);
1540 | retsize = smbc_getxattr(state->ctx, url, name, values, xattr_size + 1);
1541 | } while (retsize < 0 && xattr_size < MAXIMUM_BUFFER_SIZE);
1542 | } else {
1543 | values = emalloc(xattr_size + 1);
1544 | retsize = smbc_getxattr(state->ctx, url, name, values, xattr_size + 1);
1545 | }
1546 |
1547 | if (retsize == 0) { /* success, since 4.16.9 and 4.17.5 */
1548 | retsize = strlen(values);
1549 | } else if (retsize > xattr_size) { /* time-of-check, time-of-use error, never happen as recent versions return -1 */
1550 | retsize = xattr_size;
1551 | } else if (retsize < 0) {
1552 | efree(values);
1553 | goto fail;
1554 | }
1555 | /* realloc the string to its real size */
1556 | #if PHP_MAJOR_VERSION >= 7
1557 | RETVAL_STRINGL(values, retsize);
1558 | #else
1559 | RETVAL_STRINGL(values, retsize, 1);
1560 | #endif
1561 | efree(values);
1562 | return;
1563 |
1564 | fail:
1565 | hide_password(url, url_len);
1566 | switch (state->err = errno) {
1567 | case EINVAL: php_error(E_WARNING, "Couldn't get xattr for %s: library not initialized or incorrect parameter", url); break;
1568 | case ENOMEM: php_error(E_WARNING, "Couldn't get xattr for %s: out of memory", url); break;
1569 | case EPERM: php_error(E_WARNING, "Couldn't get xattr for %s: permission denied", url); break;
1570 | case ENOTSUP: php_error(E_WARNING, "Couldn't get xattr for %s: file system does not support extended attributes", url); break;
1571 | default:
1572 | if (xattr_size == MAXIMUM_BUFFER_SIZE) {
1573 | php_error(E_WARNING, "Couldn't get xattr for %s: internal buffer is too small", url); break;
1574 | } else {
1575 | php_error(E_WARNING, "Couldn't get xattr for %s: unknown error (%d)", url, errno); break;
1576 | }
1577 | }
1578 | RETURN_FALSE;
1579 | }
1580 |
1581 | PHP_FUNCTION(smbclient_setxattr)
1582 | {
1583 | char *url, *name, *val;
1584 | strsize_t url_len, name_len, val_len;
1585 | zend_long flags = 0;
1586 | zval *zstate;
1587 | smbc_setxattr_fn smbc_setxattr;
1588 | php_smbclient_state *state;
1589 |
1590 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsss|l", &zstate, &url, &url_len, &name, &name_len, &val, &val_len, &flags) == FAILURE) {
1591 | return;
1592 | }
1593 | STATE_FROM_ZSTATE;
1594 |
1595 | if ((smbc_setxattr = smbc_getFunctionSetxattr(state->ctx)) == NULL) {
1596 | RETURN_FALSE;
1597 | }
1598 | if (smbc_setxattr(state->ctx, url, name, val, val_len, flags) == 0) {
1599 | RETURN_TRUE;
1600 | }
1601 | hide_password(url, url_len);
1602 | switch (state->err = errno) {
1603 | case EINVAL: php_error(E_WARNING, "Couldn't set attribute on %s: client library not properly initialized", url); break;
1604 | case ENOMEM: php_error(E_WARNING, "Couldn't set attribute on %s: out of memory", url); break;
1605 | case EEXIST: php_error(E_WARNING, "Couldn't set attribute on %s: attribute already exists", url); break;
1606 | case ENOATTR: php_error(E_WARNING, "Couldn't set attribute on %s: attribute does not exist", url); break;
1607 | case ENOTSUP: php_error(E_WARNING, "Couldn't set attribute on %s: not supported by filesystem", url); break;
1608 | case EPERM: php_error(E_WARNING, "Couldn't set attribute on %s: permission denied", url); break;
1609 | default: php_error(E_WARNING, "Couldn't set attribute on %s: unknown error (%d)", url, errno); break;
1610 | }
1611 | RETURN_FALSE;
1612 | }
1613 |
1614 | PHP_FUNCTION(smbclient_removexattr)
1615 | {
1616 | char *url, *name;
1617 | strsize_t url_len, name_len;
1618 | zval *zstate;
1619 | smbc_removexattr_fn smbc_removexattr;
1620 | php_smbclient_state *state;
1621 |
1622 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &zstate, &url, &url_len, &name, &name_len) == FAILURE) {
1623 | return;
1624 | }
1625 | STATE_FROM_ZSTATE;
1626 |
1627 | if ((smbc_removexattr = smbc_getFunctionRemovexattr(state->ctx)) == NULL) {
1628 | RETURN_FALSE;
1629 | }
1630 | if (smbc_removexattr(state->ctx, url, name) == 0) {
1631 | RETURN_TRUE;
1632 | }
1633 | hide_password(url, url_len);
1634 | switch (state->err = errno) {
1635 | case EINVAL: php_error(E_WARNING, "Couldn't remove attribute on %s: client library not properly initialized", url); break;
1636 | case ENOMEM: php_error(E_WARNING, "Couldn't remove attribute on %s: out of memory", url); break;
1637 | case ENOTSUP: php_error(E_WARNING, "Couldn't remove attribute on %s: not supported by filesystem", url); break;
1638 | case EPERM: php_error(E_WARNING, "Couldn't remove attribute on %s: permission denied", url); break;
1639 | default: php_error(E_WARNING, "Couldn't remove attribute on %s: unknown error (%d)", url, errno); break;
1640 | }
1641 | RETURN_FALSE;
1642 | }
1643 |
1644 | PHP_FUNCTION(smbclient_option_get)
1645 | {
1646 | zend_long option;
1647 | const char *ret;
1648 | zval *zstate;
1649 | php_smbclient_state *state;
1650 |
1651 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zstate, &option) == FAILURE) {
1652 | return;
1653 | }
1654 | STATE_FROM_ZSTATE;
1655 |
1656 | switch (option)
1657 | {
1658 | case SMBCLIENT_OPT_OPEN_SHAREMODE:
1659 | RETURN_LONG(smbc_getOptionOpenShareMode(state->ctx));
1660 |
1661 | case SMBCLIENT_OPT_ENCRYPT_LEVEL:
1662 | RETURN_LONG(smbc_getOptionSmbEncryptionLevel(state->ctx));
1663 |
1664 | case SMBCLIENT_OPT_CASE_SENSITIVE:
1665 | RETURN_BOOL(smbc_getOptionCaseSensitive(state->ctx));
1666 |
1667 | case SMBCLIENT_OPT_BROWSE_MAX_LMB_COUNT:
1668 | RETURN_LONG(smbc_getOptionBrowseMaxLmbCount(state->ctx));
1669 |
1670 | case SMBCLIENT_OPT_URLENCODE_READDIR_ENTRIES:
1671 | RETURN_BOOL(smbc_getOptionUrlEncodeReaddirEntries(state->ctx));
1672 |
1673 | case SMBCLIENT_OPT_USE_KERBEROS:
1674 | RETURN_BOOL(smbc_getOptionUseKerberos(state->ctx));
1675 |
1676 | case SMBCLIENT_OPT_FALLBACK_AFTER_KERBEROS:
1677 | RETURN_BOOL(smbc_getOptionFallbackAfterKerberos(state->ctx));
1678 |
1679 | /* Reverse the sense of this option, the original is confusing: */
1680 | case SMBCLIENT_OPT_AUTO_ANONYMOUS_LOGIN:
1681 | RETURN_BOOL(!(smbc_getOptionNoAutoAnonymousLogin(state->ctx)));
1682 |
1683 | case SMBCLIENT_OPT_USE_CCACHE:
1684 | RETURN_BOOL(smbc_getOptionUseCCache(state->ctx));
1685 |
1686 | #ifdef HAVE_SMBC_SETOPTIONUSENTHASH
1687 | case SMBCLIENT_OPT_USE_NT_HASH:
1688 | RETURN_BOOL(smbc_getOptionUseNTHash(state->ctx));
1689 | #endif
1690 |
1691 | #ifdef HAVE_SMBC_SETPORT
1692 | case SMBCLIENT_OPT_PORT:
1693 | RETURN_LONG(smbc_getPort(state->ctx));
1694 | #endif
1695 |
1696 | case SMBCLIENT_OPT_TIMEOUT:
1697 | RETURN_LONG(smbc_getTimeout(state->ctx));
1698 |
1699 | case SMBCLIENT_OPT_NETBIOS_NAME:
1700 | if ((ret = smbc_getNetbiosName(state->ctx)) == NULL) {
1701 | RETURN_EMPTY_STRING();
1702 | }
1703 | if (strlen(ret) == 0) {
1704 | RETURN_EMPTY_STRING();
1705 | }
1706 | #if PHP_MAJOR_VERSION >= 7
1707 | RETURN_STRING(ret);
1708 | #else
1709 | RETURN_STRING(ret, 1);
1710 | #endif
1711 |
1712 | case SMBCLIENT_OPT_WORKGROUP:
1713 | if ((ret = smbc_getWorkgroup(state->ctx)) == NULL) {
1714 | RETURN_EMPTY_STRING();
1715 | }
1716 | if (strlen(ret) == 0) {
1717 | RETURN_EMPTY_STRING();
1718 | }
1719 | #if PHP_MAJOR_VERSION >= 7
1720 | RETURN_STRING(ret);
1721 | #else
1722 | RETURN_STRING(ret, 1);
1723 | #endif
1724 | case SMBCLIENT_OPT_USER:
1725 | if ((ret = smbc_getUser(state->ctx)) == NULL) {
1726 | RETURN_EMPTY_STRING();
1727 | }
1728 | if (strlen(ret) == 0) {
1729 | RETURN_EMPTY_STRING();
1730 | }
1731 | #if PHP_MAJOR_VERSION >= 7
1732 | RETURN_STRING(ret);
1733 | #else
1734 | RETURN_STRING(ret, 1);
1735 | #endif
1736 |
1737 | }
1738 | RETURN_NULL();
1739 | }
1740 |
1741 | PHP_FUNCTION(smbclient_option_set)
1742 | {
1743 | zend_long option, vbool = 0;
1744 | zval *zstate;
1745 | zval *zvalue;
1746 | php_smbclient_state *state;
1747 |
1748 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlz", &zstate, &option, &zvalue) == FAILURE) {
1749 | return;
1750 | }
1751 | STATE_FROM_ZSTATE;
1752 |
1753 | switch (Z_TYPE_P(zvalue))
1754 | {
1755 | #if PHP_MAJOR_VERSION >= 7
1756 | case IS_TRUE:
1757 | vbool = 1;
1758 | /* no break, fallthrough */
1759 | case IS_FALSE:
1760 | #else
1761 | case IS_BOOL:
1762 | vbool = Z_BVAL_P(zvalue);
1763 | #endif
1764 | switch (option)
1765 | {
1766 | case SMBCLIENT_OPT_CASE_SENSITIVE:
1767 | smbc_setOptionCaseSensitive(state->ctx, vbool);
1768 | RETURN_TRUE;
1769 |
1770 | case SMBCLIENT_OPT_URLENCODE_READDIR_ENTRIES:
1771 | smbc_setOptionUrlEncodeReaddirEntries(state->ctx, vbool);
1772 | RETURN_TRUE;
1773 |
1774 | case SMBCLIENT_OPT_USE_KERBEROS:
1775 | smbc_setOptionUseKerberos(state->ctx, vbool);
1776 | RETURN_TRUE;
1777 |
1778 | case SMBCLIENT_OPT_FALLBACK_AFTER_KERBEROS:
1779 | smbc_setOptionFallbackAfterKerberos(state->ctx, vbool);
1780 | RETURN_TRUE;
1781 |
1782 | /* Reverse the sense of this option: */
1783 | case SMBCLIENT_OPT_AUTO_ANONYMOUS_LOGIN:
1784 | smbc_setOptionNoAutoAnonymousLogin(state->ctx, !(vbool));
1785 | RETURN_TRUE;
1786 |
1787 | case SMBCLIENT_OPT_USE_CCACHE:
1788 | smbc_setOptionUseCCache(state->ctx, vbool);
1789 | RETURN_TRUE;
1790 |
1791 | #ifdef HAVE_SMBC_SETOPTIONUSENTHASH
1792 | case SMBCLIENT_OPT_USE_NT_HASH:
1793 | smbc_setOptionUseNTHash(state->ctx, vbool);
1794 | RETURN_TRUE;
1795 | #endif
1796 | }
1797 | break;
1798 |
1799 | case IS_LONG:
1800 |
1801 | switch (option)
1802 | {
1803 | case SMBCLIENT_OPT_OPEN_SHAREMODE:
1804 | smbc_setOptionOpenShareMode(state->ctx, Z_LVAL_P(zvalue));
1805 | RETURN_TRUE;
1806 |
1807 | case SMBCLIENT_OPT_ENCRYPT_LEVEL:
1808 | smbc_setOptionSmbEncryptionLevel(state->ctx, Z_LVAL_P(zvalue));
1809 | RETURN_TRUE;
1810 |
1811 | case SMBCLIENT_OPT_BROWSE_MAX_LMB_COUNT:
1812 | smbc_setOptionBrowseMaxLmbCount(state->ctx, Z_LVAL_P(zvalue));
1813 | RETURN_TRUE;
1814 |
1815 | #ifdef HAVE_SMBC_SETPORT
1816 | case SMBCLIENT_OPT_PORT:
1817 | smbc_setPort(state->ctx, Z_LVAL_P(zvalue));
1818 | RETURN_TRUE;
1819 | #endif
1820 |
1821 | case SMBCLIENT_OPT_TIMEOUT:
1822 | smbc_setTimeout(state->ctx, Z_LVAL_P(zvalue));
1823 | RETURN_TRUE;
1824 | }
1825 | break;
1826 |
1827 | case IS_STRING:
1828 |
1829 | switch (option)
1830 | {
1831 | case SMBCLIENT_OPT_NETBIOS_NAME:
1832 | smbc_setNetbiosName(state->ctx, Z_STRVAL_P(zvalue));
1833 | RETURN_TRUE;
1834 |
1835 | /* For the next two options, update our state object as well: */
1836 | case SMBCLIENT_OPT_WORKGROUP:
1837 | if (ctx_init_getauth(zvalue, &state->wrkg, &state->wrkglen, "workgroup") == 0) {
1838 | RETURN_FALSE;
1839 | }
1840 | smbc_setWorkgroup(state->ctx, Z_STRVAL_P(zvalue));
1841 | RETURN_TRUE;
1842 |
1843 | case SMBCLIENT_OPT_USER:
1844 | if (ctx_init_getauth(zvalue, &state->user, &state->userlen, "username") == 0) {
1845 | RETURN_FALSE;
1846 | }
1847 | smbc_setUser(state->ctx, Z_STRVAL_P(zvalue));
1848 | RETURN_TRUE;
1849 | }
1850 | break;
1851 | }
1852 | RETURN_FALSE;
1853 | }
1854 |
1855 | #if HAVE_SMBC_SETOPTIONPROTOCOLS
1856 | PHP_FUNCTION(smbclient_client_protocols)
1857 | {
1858 | zval *zstate;
1859 | char *minproto = NULL, *maxproto = NULL;
1860 | strsize_t minproto_len, maxproto_len;
1861 | php_smbclient_state *state;
1862 |
1863 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|s!s!", &zstate, &minproto, &minproto_len, &maxproto, &maxproto_len) == FAILURE) {
1864 | return;
1865 | }
1866 | STATE_FROM_ZSTATE;
1867 |
1868 | RETURN_BOOL(smbc_setOptionProtocols(state->ctx, minproto, maxproto));
1869 | }
1870 | #endif
1871 |
1872 | PHP_FUNCTION(smbclient_statvfs)
1873 | {
1874 | char *url;
1875 | strsize_t url_len;
1876 | zval *zstate;
1877 | struct statvfs st;
1878 | smbc_statvfs_fn smbc_statvfs;
1879 | php_smbclient_state *state;
1880 |
1881 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &zstate, &url, &url_len) == FAILURE) {
1882 | return;
1883 | }
1884 | STATE_FROM_ZSTATE;
1885 |
1886 | if ((smbc_statvfs = smbc_getFunctionStatVFS(state->ctx)) == NULL) {
1887 | RETURN_FALSE;
1888 | }
1889 | if (smbc_statvfs(state->ctx, url, &st) != 0) {
1890 | hide_password(url, url_len);
1891 | switch (state->err = errno) {
1892 | case EBADF: php_error(E_WARNING, "Couldn't statvfs %s: bad file descriptor", url); break;
1893 | case EACCES: php_error(E_WARNING, "Couldn't statvfs %s: permission denied", url); break;
1894 | case EINVAL: php_error(E_WARNING, "Couldn't statvfs %s: library not initalized or otherwise invalid", url); break;
1895 | case ENOMEM: php_error(E_WARNING, "Couldn't statvfs %s: out of memory", url); break;
1896 | default: php_error(E_WARNING, "Couldn't statvfs %s: unknown error (%d)", url, errno); break;
1897 | }
1898 | RETURN_FALSE;
1899 | }
1900 | array_init(return_value);
1901 | add_assoc_long(return_value, "bsize", st.f_bsize);
1902 | add_assoc_long(return_value, "frsize", st.f_frsize);
1903 | add_assoc_long(return_value, "blocks", st.f_blocks);
1904 | add_assoc_long(return_value, "bfree", st.f_bfree);
1905 | add_assoc_long(return_value, "bavail", st.f_bavail);
1906 | add_assoc_long(return_value, "files", st.f_files);
1907 | add_assoc_long(return_value, "ffree", st.f_ffree);
1908 | add_assoc_long(return_value, "favail", st.f_favail);
1909 | add_assoc_long(return_value, "fsid", st.f_fsid);
1910 | add_assoc_long(return_value, "flag", st.f_flag);
1911 | add_assoc_long(return_value, "namemax", st.f_namemax);
1912 | }
1913 |
1914 | PHP_FUNCTION(smbclient_fstatvfs)
1915 | {
1916 | zval *zstate;
1917 | zval *zfile;
1918 | SMBCFILE *file;
1919 | struct statvfs st;
1920 | smbc_fstatvfs_fn smbc_fstatvfs;
1921 | php_smbclient_state *state;
1922 |
1923 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &zstate, &zfile) == FAILURE) {
1924 | return;
1925 | }
1926 | STATE_FROM_ZSTATE;
1927 | FILE_FROM_ZFILE;
1928 |
1929 | if ((smbc_fstatvfs = smbc_getFunctionFstatVFS(state->ctx)) == NULL) {
1930 | RETURN_FALSE;
1931 | }
1932 | if (smbc_fstatvfs(state->ctx, file, &st) != 0) {
1933 | switch (state->err = errno) {
1934 | case EBADF: php_error(E_WARNING, "Couldn't fstatvfs: bad file descriptor"); break;
1935 | case EACCES: php_error(E_WARNING, "Couldn't fstatvfs: permission denied"); break;
1936 | case EINVAL: php_error(E_WARNING, "Couldn't fstatvfs: library not initalized or otherwise invalid"); break;
1937 | case ENOMEM: php_error(E_WARNING, "Couldn't fstatvfs: out of memory"); break;
1938 | default: php_error(E_WARNING, "Couldn't fstatvfs: unknown error (%d)", errno); break;
1939 | }
1940 | RETURN_FALSE;
1941 | }
1942 | array_init(return_value);
1943 | add_assoc_long(return_value, "bsize", st.f_bsize);
1944 | add_assoc_long(return_value, "frsize", st.f_frsize);
1945 | add_assoc_long(return_value, "blocks", st.f_blocks);
1946 | add_assoc_long(return_value, "bfree", st.f_bfree);
1947 | add_assoc_long(return_value, "bavail", st.f_bavail);
1948 | add_assoc_long(return_value, "files", st.f_files);
1949 | add_assoc_long(return_value, "ffree", st.f_ffree);
1950 | add_assoc_long(return_value, "favail", st.f_favail);
1951 | add_assoc_long(return_value, "fsid", st.f_fsid);
1952 | add_assoc_long(return_value, "flag", st.f_flag);
1953 | add_assoc_long(return_value, "namemax", st.f_namemax);
1954 | }
1955 |
--------------------------------------------------------------------------------
/smbclient.stub.php:
--------------------------------------------------------------------------------
1 | assertTrue(smbclient_closedir($state, $dir));
12 | }
13 |
14 | /**
15 | * @expectedException PHPUnit_Framework_Error_Warning
16 | */
17 | public function
18 | testClosedirEmpty ()
19 | {
20 | $state = smbclient_state_new();
21 | smbclient_state_init($state, null, SMB_USER, SMB_PASS);
22 | $this->assertFalse(smbclient_closedir($state));
23 | }
24 |
25 | /**
26 | * @expectedException PHPUnit_Framework_Error_Warning
27 | */
28 | public function
29 | testClosedirNull ()
30 | {
31 | $state = smbclient_state_new();
32 | smbclient_state_init($state, null, SMB_USER, SMB_PASS);
33 | $this->assertTrue(smbclient_closedir($state, null));
34 | }
35 |
36 | /**
37 | * @expectedException PHPUnit_Framework_Error_Warning
38 | */
39 | public function
40 | testClosedirDouble ()
41 | {
42 | $state = smbclient_state_new();
43 | smbclient_state_init($state, null, SMB_USER, SMB_PASS);
44 | $dir = smbclient_opendir($state, 'smb://'.SMB_HOST.'/'.SMB_SHARE);
45 | $this->assertTrue(smbclient_closedir($state, $dir));
46 | $this->assertFalse(smbclient_closedir($state, $dir));
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/tests/CreateTest.php:
--------------------------------------------------------------------------------
1 | assertTrue(is_resource($file));
15 | $this->assertFileExists($realfile);
16 | unlink($realfile);
17 | }
18 |
19 | public function
20 | testCreateUrlencodedSuccess ()
21 | {
22 | $testuri = 'smb://'.SMB_HOST.'/'.SMB_SHARE.'/ex%25%25%25ample.txt';
23 | $realfile = SMB_LOCAL . '/ex%%%ample.txt';
24 |
25 | $state = smbclient_state_new();
26 | smbclient_state_init($state, null, SMB_USER, SMB_PASS);
27 | $file = smbclient_creat($state, $testuri);
28 | $this->assertTrue(is_resource($file));
29 | $this->assertFileExists($realfile);
30 | #unlink($realfile);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/tests/GetxattrTest.php:
--------------------------------------------------------------------------------
1 | assertTrue(is_string($attr) && strlen($attr));
12 | }
13 |
14 | public function
15 | testGetxattrDirSuccess ()
16 | {
17 | $state = smbclient_state_new();
18 | smbclient_state_init($state, null, SMB_USER, SMB_PASS);
19 | $attr = smbclient_getxattr($state, 'smb://'.SMB_HOST.'/'.SMB_SHARE.'/testdir', 'system.*');
20 | $this->assertTrue(is_string($attr) && strlen($attr));
21 | }
22 |
23 | public function
24 | testGetxattrShareSuccess ()
25 | {
26 | $state = smbclient_state_new();
27 | smbclient_state_init($state, null, SMB_USER, SMB_PASS);
28 | $attr = smbclient_getxattr($state, 'smb://'.SMB_HOST.'/'.SMB_SHARE, 'system.*');
29 | $this->assertTrue(is_string($attr) && strlen($attr));
30 | }
31 |
32 | /**
33 | * @expectedException PHPUnit_Framework_Error_Warning
34 | */
35 | public function
36 | testGetxattrServer ()
37 | {
38 | $state = smbclient_state_new();
39 | smbclient_state_init($state, null, SMB_USER, SMB_PASS);
40 | $attr = smbclient_getxattr($state, 'smb://'.SMB_HOST, 'system.*');
41 | $this->assertFalse($attr);
42 | }
43 |
44 | /**
45 | * @expectedException PHPUnit_Framework_Error_Warning
46 | */
47 | public function
48 | testGetxattrFileNotFound ()
49 | {
50 | $state = smbclient_state_new();
51 | smbclient_state_init($state, null, SMB_USER, SMB_PASS);
52 | $attr = smbclient_getxattr($state, 'smb://'.SMB_HOST.'/'.SMB_SHARE.'/testdir/does-not-exist', 'system.dos_attr.mode');
53 | $this->assertFalse($attr);
54 | $this->assertEquals(smbclient_state_errno($state), 2); // ENOENT
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/tests/LseekTest.php:
--------------------------------------------------------------------------------
1 | testuri = 'smb://'.SMB_HOST.'/'.SMB_SHARE.'/lseektest.txt';
13 | $this->realfile = SMB_LOCAL.'/lseektest.txt';
14 | }
15 |
16 | public function
17 | testLseekSet ()
18 | {
19 | $state = smbclient_state_new();
20 | smbclient_state_init($state, null, SMB_USER, SMB_PASS);
21 | $file = smbclient_creat($state, $this->testuri);
22 | $ret = smbclient_write($state, $file, 'abcdefgh');
23 | $this->assertTrue(is_integer($ret));
24 |
25 | $ret = smbclient_lseek($state, $file, 3, SEEK_SET);
26 | $this->assertEquals(3, $ret);
27 |
28 | smbclient_write($state, $file, 'foo');
29 | smbclient_close($state, $file);
30 | $this->assertStringEqualsFile($this->realfile, 'abcfoogh');
31 | }
32 |
33 | public function
34 | testLseekSetPastEnd ()
35 | {
36 | $state = smbclient_state_new();
37 | smbclient_state_init($state, null, SMB_USER, SMB_PASS);
38 | $file = smbclient_creat($state, $this->testuri);
39 |
40 | $ret = smbclient_lseek($state, $file, 3, SEEK_SET);
41 | $this->assertEquals(3, $ret);
42 |
43 | smbclient_write($state, $file, 'foo');
44 | smbclient_close($state, $file);
45 | $this->assertStringEqualsFile($this->realfile, sprintf('%c%c%c%s', 0, 0, 0, 'foo'));
46 | }
47 |
48 | public function
49 | testLseekCurPositive ()
50 | {
51 | $state = smbclient_state_new();
52 | smbclient_state_init($state, null, SMB_USER, SMB_PASS);
53 | $file = smbclient_creat($state, $this->testuri);
54 | smbclient_write($state, $file, 'abcdefgh');
55 |
56 | $ret = smbclient_lseek($state, $file, 3, SEEK_CUR);
57 | $this->assertEquals(11, $ret);
58 |
59 | smbclient_write($state, $file, 'foo', 3);
60 | smbclient_close($state, $file);
61 | $this->assertStringEqualsFile($this->realfile, sprintf('%s%c%c%c%s', 'abcdefgh', 0, 0, 0, 'foo'));
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/tests/OpendirTest.php:
--------------------------------------------------------------------------------
1 | assertTrue(is_resource($dir));
12 | }
13 |
14 | /**
15 | * @expectedException PHPUnit_Framework_Error_Warning
16 | */
17 | public function
18 | testOpendirInvalidState ()
19 | {
20 | $dir = smbclient_opendir(null, 'smb://'.SMB_HOST.'/'.SMB_SHARE.'/testdir');
21 | $this->assertFalse($dir);
22 | }
23 |
24 | /**
25 | * @expectedException PHPUnit_Framework_Error_Warning
26 | */
27 | public function
28 | testOpendirNotFound ()
29 | {
30 | $state = smbclient_state_new();
31 | smbclient_state_init($state, null, SMB_USER, SMB_PASS);
32 | $dir = smbclient_opendir($state, 'smb://'.SMB_HOST.'/'.SMB_SHARE.'/does-not-exist');
33 | $this->assertFalse($dir);
34 | $errno = smbclient_state_errno($state);
35 | $this->assertEquals(2, $errno); // ENOENT
36 | }
37 |
38 | /**
39 | * @expectedException PHPUnit_Framework_Error_Warning
40 | */
41 | public function
42 | testOpendirPermissionDenied ()
43 | {
44 | $state = smbclient_state_new();
45 | smbclient_state_init($state, null, SMB_USER, SMB_PASS);
46 | $dir = smbclient_opendir($state, 'smb://'.SMB_HOST.'/'.SMB_SHARE.'/noaccess');
47 | $this->assertFalse($dir);
48 | $errno = smbclient_state_errno($state);
49 | $this->assertEquals(13, $errno); // EACCES
50 | }
51 |
52 | /**
53 | * @expectedException PHPUnit_Framework_Error_Warning
54 | */
55 | public function
56 | testOpendirNotDir ()
57 | {
58 | $state = smbclient_state_new();
59 | smbclient_state_init($state, null, SMB_USER, SMB_PASS);
60 | $dir = smbclient_opendir($state, 'smb://'.SMB_HOST.'/'.SMB_SHARE.'/testfile.txt');
61 | $this->assertFalse($dir);
62 | $errno = smbclient_state_errno($state);
63 | $this->assertEquals(20, $errno); // ENOTDIR
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/tests/OptionsTest.php:
--------------------------------------------------------------------------------
1 | assertTrue(is_long(SMBCLIENT_OPT_OPEN_SHAREMODE));
9 | $this->assertTrue(is_long(SMBCLIENT_OPT_ENCRYPT_LEVEL));
10 | $this->assertTrue(is_long(SMBCLIENT_OPT_CASE_SENSITIVE));
11 | $this->assertTrue(is_long(SMBCLIENT_OPT_BROWSE_MAX_LMB_COUNT));
12 | $this->assertTrue(is_long(SMBCLIENT_OPT_URLENCODE_READDIR_ENTRIES));
13 | $this->assertTrue(is_long(SMBCLIENT_OPT_USE_KERBEROS));
14 | $this->assertTrue(is_long(SMBCLIENT_OPT_FALLBACK_AFTER_KERBEROS));
15 | $this->assertTrue(is_long(SMBCLIENT_OPT_AUTO_ANONYMOUS_LOGIN));
16 | $this->assertTrue(is_long(SMBCLIENT_OPT_USE_CCACHE));
17 | $this->assertTrue(is_long(SMBCLIENT_OPT_USE_NT_HASH));
18 | $this->assertTrue(is_long(SMBCLIENT_OPT_NETBIOS_NAME));
19 | $this->assertTrue(is_long(SMBCLIENT_OPT_WORKGROUP));
20 | $this->assertTrue(is_long(SMBCLIENT_OPT_USER));
21 | $this->assertTrue(is_long(SMBCLIENT_OPT_PORT));
22 | $this->assertTrue(is_long(SMBCLIENT_OPT_TIMEOUT));
23 | }
24 |
25 | public function
26 | testOptionNotExists ()
27 | {
28 | $state = smbclient_state_new();
29 | $this->assertNull(smbclient_option_get($state, 9999));
30 | smbclient_state_free($state);
31 | }
32 |
33 | public function
34 | testOptionSetGetCaseSensitive ()
35 | {
36 | $state = smbclient_state_new();
37 | smbclient_state_init($state, null, SMB_USER, SMB_PASS);
38 | $this->assertTrue(smbclient_option_set($state, SMBCLIENT_OPT_CASE_SENSITIVE, true));
39 | $this->assertTrue(smbclient_option_get($state, SMBCLIENT_OPT_CASE_SENSITIVE));
40 |
41 | $this->assertTrue(smbclient_option_set($state, SMBCLIENT_OPT_CASE_SENSITIVE, false));
42 | $this->assertFalse(smbclient_option_get($state, SMBCLIENT_OPT_CASE_SENSITIVE));
43 | smbclient_state_free($state);
44 | }
45 |
46 | public function
47 | testOptionSetGetUser ()
48 | {
49 | $state = smbclient_state_new();
50 | smbclient_state_init($state, null, SMB_USER, SMB_PASS);
51 | $this->assertTrue(smbclient_option_set($state, SMBCLIENT_OPT_USER, SMB_USER));
52 | $this->assertEquals(SMB_USER, smbclient_option_get($state, SMBCLIENT_OPT_USER));
53 | smbclient_state_free($state);
54 | }
55 |
56 | public function
57 | testOptionSetGetUserWithOpen ()
58 | {
59 | $state = smbclient_state_new();
60 | smbclient_state_init($state, null, SMB_USER, SMB_PASS);
61 | $this->assertTrue(smbclient_option_set($state, SMBCLIENT_OPT_USER, SMB_USER));
62 | $this->assertEquals(SMB_USER, smbclient_option_get($state, SMBCLIENT_OPT_USER));
63 | $dir = smbclient_opendir($state, 'smb://'.SMB_HOST.'/'.SMB_SHARE);
64 | while (($out = smbclient_readdir($state, $dir)) !== false);
65 | smbclient_closedir($state, $dir);
66 | smbclient_state_free($state);
67 | }
68 |
69 | public function
70 | testOptionUrlencodeReaddir ()
71 | {
72 | $state = smbclient_state_new();
73 | smbclient_state_init($state, null, SMB_USER, SMB_PASS);
74 |
75 | // Create a file via the regular filesystem:
76 | touch(SMB_LOCAL.'/readdir%option.txt');
77 |
78 | // Technically options can only be set between new and init...
79 | smbclient_option_set($state, SMBCLIENT_OPT_URLENCODE_READDIR_ENTRIES, false);
80 | $dir = smbclient_opendir($state, 'smb://'.SMB_HOST.'/'.SMB_SHARE);
81 | $found = false;
82 | while (($out = smbclient_readdir($state, $dir)) !== false) {
83 | if ($out['name'] === 'readdir%option.txt') {
84 | $found = true;
85 | break;
86 | }
87 | }
88 | smbclient_closedir($state, $dir);
89 | $this->assertTrue($found);
90 |
91 | smbclient_option_set($state, SMBCLIENT_OPT_URLENCODE_READDIR_ENTRIES, true);
92 | $dir = smbclient_opendir($state, 'smb://'.SMB_HOST.'/'.SMB_SHARE);
93 | $found = false;
94 | while (($out = smbclient_readdir($state, $dir)) !== false) {
95 | if ($out['name'] === 'readdir%25option.txt') {
96 | $found = true;
97 | break;
98 | }
99 | }
100 | smbclient_closedir($state, $dir);
101 | smbclient_state_free($state);
102 | $this->assertTrue($found);
103 |
104 | unlink(SMB_LOCAL.'/readdir%option.txt');
105 | }
106 |
107 | public function
108 | testOptionNTHash ()
109 | {
110 | $state = smbclient_state_new();
111 |
112 | if (smbclient_option_get($state, SMBCLIENT_OPT_USE_NT_HASH) === null) {
113 | smbclient_state_free($state);
114 | return;
115 | }
116 | smbclient_option_set($state, SMBCLIENT_OPT_USE_NT_HASH, true);
117 |
118 | // NTLM hash of 'password' generated at http://www.tobtu.com/lmntlm.php
119 | smbclient_state_init($state, null, SMB_USER, SMB_HASH);
120 |
121 | $dir = smbclient_opendir($state, 'smb://'.SMB_HOST.'/'.SMB_SHARE);
122 | while (($out = smbclient_readdir($state, $dir)) !== false);
123 | $this->assertTrue(is_resource($dir));
124 | smbclient_closedir($state, $dir);
125 | smbclient_state_free($state);
126 | }
127 | }
128 |
--------------------------------------------------------------------------------
/tests/ReadTest.php:
--------------------------------------------------------------------------------
1 | testuri = 'smb://'.SMB_HOST.'/'.SMB_SHARE.'/readtest.txt';
19 | $this->realfile = SMB_LOCAL.'/readtest.txt';
20 |
21 | file_put_contents($this->realfile, $this->testdata);
22 | }
23 |
24 | public function
25 | tearDown()
26 | {
27 | @unlink($this->realfile);
28 | }
29 |
30 | public function
31 | testReadSuccess()
32 | {
33 | $state = smbclient_state_new();
34 | smbclient_state_init($state, null, SMB_USER, SMB_PASS);
35 | $file = smbclient_open($state, $this->testuri, 'r');
36 | $this->assertTrue(is_resource($file));
37 |
38 | for ($data = ''; $tmp = smbclient_read($state, $file, 42) ; $data .= $tmp) {
39 | $this->assertTrue(\strlen($tmp) > 0 && \strlen($tmp) <= 42);
40 | }
41 | $this->assertEmpty($tmp);
42 | $this->assertEquals($this->testdata, $data);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/tests/ReaddirTest.php:
--------------------------------------------------------------------------------
1 | assertEquals($smb, $local);
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/tests/RenameTest.php:
--------------------------------------------------------------------------------
1 | assertTrue(smbclient_rename($state, 'smb://'.SMB_HOST.'/'.SMB_SHARE.'/foo', $state, 'smb://'.SMB_HOST.'/'.SMB_SHARE.'/bar'));
15 |
16 | $moved = is_file(SMB_LOCAL.'/bar');
17 | $this->assertTrue($moved);
18 |
19 | $gone = is_file(SMB_LOCAL.'/foo');
20 | $this->assertFalse($gone);
21 |
22 | if ($moved) {
23 | $this->assertEquals('this is a test', file_get_contents(SMB_LOCAL.'/bar'));
24 | }
25 | @unlink(SMB_LOCAL.'/foo');
26 | @unlink(SMB_LOCAL.'/bar');
27 | }
28 |
29 | /**
30 | * @expectedException PHPUnit_Framework_Error_Warning
31 | */
32 | public function
33 | testRenameFileNotExists ()
34 | {
35 | $state = smbclient_state_new();
36 | smbclient_state_init($state, null, SMB_USER, SMB_PASS);
37 | $this->assertFalse(smbclient_rename($state, 'smb://'.SMB_HOST.'/'.SMB_SHARE.'/does-not-exist', $state, 'smb://'.SMB_HOST.'/'.SMB_SHARE.'/somewhere-else'));
38 | $this->assertEquals(2, smbclient_state_errno($state)); // ENOENT
39 | }
40 |
41 | public function
42 | testRenameDirSuccess ()
43 | {
44 | $state = smbclient_state_new();
45 | smbclient_state_init($state, null, SMB_USER, SMB_PASS);
46 |
47 | // Create mock dir "foo" via regular filesystem:
48 | mkdir(SMB_LOCAL.'/foo');
49 |
50 | $this->assertTrue(smbclient_rename($state, 'smb://'.SMB_HOST.'/'.SMB_SHARE.'/foo', $state, 'smb://'.SMB_HOST.'/'.SMB_SHARE.'/bar'));
51 |
52 | $moved = is_dir(SMB_LOCAL.'/bar');
53 | $this->assertTrue($moved);
54 |
55 | $gone = is_dir(SMB_LOCAL.'/foo');
56 | $this->assertFalse($gone);
57 |
58 | @rmdir(SMB_LOCAL.'/foo');
59 | @rmdir(SMB_LOCAL.'/bar');
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/tests/StateFreeTest.php:
--------------------------------------------------------------------------------
1 | assertTrue(smbclient_state_free($state));
10 | }
11 |
12 | /**
13 | * @expectedException PHPUnit_Framework_Error_Warning
14 | */
15 | public function
16 | testStateFreeEmpty ()
17 | {
18 | $this->assertFalse(smbclient_state_free());
19 | }
20 |
21 | /**
22 | * @expectedException PHPUnit_Framework_Error_Warning
23 | */
24 | public function
25 | testStateFreeNull ()
26 | {
27 | $this->assertFalse(smbclient_state_free(null));
28 | }
29 |
30 | /**
31 | * @expectedException PHPUnit_Framework_Error_Warning
32 | */
33 | public function
34 | testStateFreeDouble ()
35 | {
36 | $state = smbclient_state_new();
37 | $this->assertTrue(smbclient_state_free($state));
38 | $this->assertFalse(smbclient_state_free($state));
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/tests/StateInitTest.php:
--------------------------------------------------------------------------------
1 | assertTrue(smbclient_state_init($state, null, SMB_USER, SMB_PASS));
10 | }
11 |
12 | /**
13 | * @expectedException PHPUnit_Framework_Error_Warning
14 | */
15 | public function
16 | testStateInitInvalidState ()
17 | {
18 | $this->assertFalse(smbclient_state_init(null));
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/tests/StateNewTest.php:
--------------------------------------------------------------------------------
1 | assertTrue(is_resource($state));
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/tests/StreamsTest.php:
--------------------------------------------------------------------------------
1 | readuri = 'smb://'.SMB_USER.':'.SMB_PASS.'@'.SMB_HOST.'/'.SMB_SHARE.'/testdir/testfile.txt';
16 | $this->realread = SMB_LOCAL . '/testdir/testfile.txt';
17 |
18 | $this->writeuri = 'smb://'.SMB_USER.':'.SMB_PASS.'@'.SMB_HOST.'/'.SMB_SHARE.'/streamfile.txt';
19 | $this->writealt = 'smb://'.SMB_HOST.'/'.SMB_SHARE.'/streamfile.txt';
20 | $this->realfile = SMB_LOCAL . '/streamfile.txt';
21 |
22 | $this->diruri = 'smb://'.SMB_USER.':'.SMB_PASS.'@'.SMB_HOST.'/'.SMB_SHARE.'/streamdir';
23 | $this->realdir = SMB_LOCAL . '/streamdir';
24 | }
25 |
26 | public function
27 | tearDown()
28 | {
29 | @unlink($this->realfile);
30 | @rmdir($this->realdir);
31 | }
32 |
33 | public function
34 | testRegistered ()
35 | {
36 | $this->assertContains('smb', stream_get_wrappers());
37 | }
38 |
39 | public function
40 | testReadWrite ()
41 | {
42 | $fic = fopen($this->writeuri, "w+");
43 | $this->assertTrue(is_resource($fic), "Get a resource");
44 |
45 | $len = fwrite($fic, $this->testdata);
46 | $this->assertEquals(strlen($this->testdata), $len);
47 | $this->assertEquals($len, ftell($fic));
48 |
49 | $this->assertEquals(3, fwrite($fic, "foo"));
50 | $this->assertEquals($len, fwrite($fic, $this->testdata));
51 | $this->assertEquals(2*$len+3, ftell($fic));
52 |
53 | $this->assertEquals(0, fseek($fic, $len, SEEK_SET));
54 | $this->assertEquals($len, ftell($fic));
55 | $this->assertEquals('foo', fread($fic, 3));
56 |
57 | $stat = fstat($fic);
58 | $this->assertCount(26, $stat);
59 | $this->assertArrayHasKey('atime', $stat);
60 | $this->assertArrayHasKey('mtime', $stat);
61 | $this->assertArrayHasKey('ctime', $stat);
62 | $this->assertArrayHasKey('size', $stat);
63 | $this->assertEquals(2*$len+3, $stat['size']);
64 |
65 | $this->assertTrue(ftruncate($fic,42));
66 | $stat = fstat($fic);
67 | $this->assertEquals(42, $stat['size']);
68 |
69 | $this->assertTrue(fclose($fic));
70 | }
71 |
72 | public function
73 | testPutContents ()
74 | {
75 | $len = file_put_contents($this->writeuri, $this->testdata);
76 | $this->assertEquals(strlen($this->testdata), $len);
77 | $this->assertFileExists($this->realfile);
78 | }
79 |
80 | public function
81 | testPutContentsContext ()
82 | {
83 | $context = stream_context_create(array('smb' => array(
84 | 'username' => SMB_USER,
85 | 'password' => SMB_PASS
86 | )));
87 | $len = file_put_contents($this->writealt, $this->testdata, 0, $context);
88 | $this->assertEquals(strlen($this->testdata), $len);
89 | $this->assertFileExists($this->realfile);
90 | }
91 |
92 | public function
93 | testGetContents ()
94 | {
95 | $data = file_get_contents($this->readuri);
96 | $this->assertTrue(strlen($data) > 0);
97 | }
98 |
99 | public function
100 | testUnlink ()
101 | {
102 | $this->assertTrue(copy($this->readuri, $this->writeuri));
103 | $this->assertFileExists($this->realfile);
104 | $this->assertTrue(unlink($this->writeuri));
105 | $this->assertFileNotExists($this->realfile);
106 | }
107 |
108 | public function
109 | testMkdirRmdir ()
110 | {
111 | $this->assertTrue(mkdir($this->diruri));
112 | $this->assertTrue(is_dir($this->realdir), "Directory exists");
113 | $this->assertTrue(rmdir($this->diruri));
114 | clearstatcache();
115 | $this->assertFalse(is_dir($this->realdir), "Directory not exists");
116 | }
117 |
118 | public function
119 | testRenameFile ()
120 | {
121 | $this->assertTrue(copy($this->readuri, $this->writeuri));
122 | $this->assertTrue(rename($this->writeuri, $this->writeuri.'2'));
123 | $this->assertFileExists($this->realfile.'2');
124 | $this->assertTrue(unlink($this->writeuri.'2'));
125 | }
126 |
127 | public function
128 | testRenameDir ()
129 | {
130 | $this->assertTrue(mkdir($this->diruri));
131 | $this->assertTrue(is_dir($this->realdir), "Directory exists");
132 | $this->assertTrue(rename($this->diruri, $this->diruri.'2'));
133 | $this->assertTrue(is_dir($this->realdir.'2'), "Directory exists");
134 | $this->assertTrue(rmdir($this->diruri.'2'));
135 | }
136 |
137 | public function
138 | testReaddir ()
139 | {
140 | $local = scandir(dirname($this->realdir));
141 | $smb = scandir(dirname($this->diruri));
142 |
143 | $this->assertEquals($local, $smb);
144 | }
145 |
146 | public function
147 | testStat ()
148 | {
149 | $stat = stat($this->readuri);
150 | $this->assertCount(26, $stat);
151 | $this->assertArrayHasKey('atime', $stat);
152 | $this->assertArrayHasKey('mtime', $stat);
153 | $this->assertArrayHasKey('ctime', $stat);
154 | $this->assertArrayHasKey('size', $stat);
155 |
156 | $local = stat($this->realread);
157 | $this->assertEquals($local['size'], $stat['size']);
158 | /* can't compare other values as local/remote are different */
159 | }
160 |
161 | public function
162 | testChmod ()
163 | {
164 | if (version_compare(PHP_VERSION, '5.4.0', '<')) {
165 | $this->markTestSkipped("PHP > 5.4 needed");
166 | }
167 | $this->assertTrue(copy($this->readuri, $this->writeuri));
168 | $old = fileperms($this->writeuri) & 0777;
169 |
170 | /* noop only test */
171 | $this->assertTrue(chmod($this->writeuri, $old));
172 | $new = fileperms($this->writeuri) & 0777;
173 | $this->assertEquals($old, $new);
174 | }
175 |
176 | public function
177 | testTouch ()
178 | {
179 | if (version_compare(PHP_VERSION, '5.4.0', '<')) {
180 | $this->markTestSkipped("PHP > 5.4 needed");
181 | }
182 | $mt = mktime(0,0,0,9,5,2015);
183 | $at = mktime(0,0,0,9,6,2015);
184 | $this->assertTrue(touch($this->writeuri, $mt, $at));
185 | $this->assertFileExists($this->realfile);
186 |
187 | $stat = stat($this->writeuri);
188 | $this->assertEquals($mt, $stat['mtime'], "mtime");
189 | $this->assertEquals($at, $stat['atime'], "atime");
190 | }
191 |
192 | public function
193 | testFileinfo ()
194 | {
195 | if (!extension_loaded('fileinfo')) {
196 | $this->markTestSkipped('Fileinfo extension needed');
197 | }
198 | $fi = new finfo(FILEINFO_MIME_TYPE);
199 | $this->assertEquals('text/plain', $fi->file($this->readuri));
200 | }
201 |
202 | public function
203 | testListShares ()
204 | {
205 | $smb = scandir('smb://'.SMB_USER.':'.SMB_PASS.'@'.SMB_HOST.'/');
206 |
207 | $this->assertContains(SMB_SHARE, $smb, print_r($smb, true));
208 | }
209 | }
210 |
--------------------------------------------------------------------------------
/tests/VersionTest.php:
--------------------------------------------------------------------------------
1 | assertTrue(is_string(smbclient_version()));
9 | }
10 |
11 | public function
12 | testLibraryVersion ()
13 | {
14 | $this->assertTrue(is_string(smbclient_library_version()));
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/tests/VfsTest.php:
--------------------------------------------------------------------------------
1 | testuri = 'smb://'.SMB_HOST.'/'.SMB_SHARE.'/testdir/testfile.txt';
9 | }
10 |
11 | public function
12 | testHaveConstants ()
13 | {
14 | $this->assertTrue(defined('SMBCLIENT_VFS_RDONLY'));
15 | $this->assertTrue(defined('SMBCLIENT_VFS_DFS'));
16 | $this->assertTrue(defined('SMBCLIENT_VFS_CASE_INSENSITIVE'));
17 | $this->assertTrue(defined('SMBCLIENT_VFS_NO_UNIXCIFS'));
18 | }
19 |
20 | private function
21 | hasParts ($vfs)
22 | {
23 | $parts = array('bsize','frsize','blocks','bfree','bavail','files','ffree','favail','fsid','flag','namemax');
24 | foreach ($parts as $part) {
25 | $this->assertTrue(isset($vfs[$part]));
26 | $this->assertTrue(is_int($vfs[$part]));
27 | }
28 | }
29 |
30 | public function
31 | testStatVFS ()
32 | {
33 | $state = smbclient_state_new();
34 | smbclient_state_init($state, null, SMB_USER, SMB_PASS);
35 |
36 | $vfs = smbclient_statvfs($state, $this->testuri);
37 |
38 | smbclient_state_free($state);
39 |
40 | $this->assertTrue(is_array($vfs));
41 | $this->hasParts($vfs);
42 | }
43 |
44 | public function
45 | testFstatVFS ()
46 | {
47 | $state = smbclient_state_new();
48 | smbclient_state_init($state, null, SMB_USER, SMB_PASS);
49 | $file = smbclient_open($state, $this->testuri, 'r');
50 |
51 | $vfs = smbclient_fstatvfs($state, $file);
52 |
53 | smbclient_close($state, $file);
54 | smbclient_state_free($state);
55 |
56 | $this->assertTrue(is_array($vfs));
57 | $this->hasParts($vfs);
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/tests/WriteTest.php:
--------------------------------------------------------------------------------
1 | testuri = 'smb://'.SMB_HOST.'/'.SMB_SHARE.'/writetest.txt';
19 | $this->realfile = SMB_LOCAL.'/writetest.txt';
20 | $this->testuri2 = 'smb://'.SMB_USER.':'.SMB_PASS.'@'.SMB_HOST.'/'.SMB_SHARE.'/writetest.txt';
21 | }
22 |
23 | public function
24 | tearDown()
25 | {
26 | @unlink($this->realfile);
27 | }
28 |
29 | public function
30 | testWriteSuccess ()
31 | {
32 | /* credentials sent during init */
33 | $state = smbclient_state_new();
34 | smbclient_state_init($state, null, SMB_USER, SMB_PASS);
35 | $file = smbclient_creat($state, $this->testuri);
36 | $this->assertTrue(is_resource($file));
37 |
38 | $ret = smbclient_write($state, $file, $this->testdata);
39 | $this->assertTrue(is_integer($ret));
40 |
41 | $this->assertEquals(strlen($this->testdata), $ret);
42 |
43 | // Use the regular filesystem to check the file that was created:
44 | $this->assertFileExists($this->realfile);
45 | $this->assertEquals(filesize($this->realfile), $ret);
46 |
47 | $this->assertStringEqualsFile($this->realfile, $this->testdata);
48 | }
49 |
50 | public function
51 | testWriteSuccess2 ()
52 | {
53 | /* credentials in URI */
54 | $state = smbclient_state_new();
55 | smbclient_state_init($state);
56 | $file = smbclient_creat($state, $this->testuri2);
57 | $this->assertTrue(is_resource($file));
58 |
59 | $ret = smbclient_write($state, $file, $this->testdata);
60 | $this->assertTrue(is_integer($ret));
61 |
62 | $this->assertEquals(strlen($this->testdata), $ret);
63 |
64 | // Use the regular filesystem to check the file that was created:
65 | $this->assertFileExists($this->realfile);
66 | $this->assertEquals(filesize($this->realfile), $ret);
67 |
68 | $this->assertStringEqualsFile($this->realfile, $this->testdata);
69 | }
70 |
71 | public function
72 | testWritePartial ()
73 | {
74 | $state = smbclient_state_new();
75 | smbclient_state_init($state, null, SMB_USER, SMB_PASS);
76 | $file = smbclient_creat($state, $this->testuri);
77 | $this->assertTrue(is_resource($file));
78 |
79 | // Write just 30 bytes:
80 | $ret = smbclient_write($state, $file, $this->testdata, 30);
81 | $this->assertEquals(30, $ret);
82 |
83 | // Use the regular filesystem to check the file that was created:
84 | $this->assertFileExists($this->realfile);
85 | $this->assertEquals(filesize($this->realfile), $ret);
86 |
87 | $this->assertStringEqualsFile($this->realfile, substr($this->testdata, 0, 30));
88 | }
89 |
90 | public function
91 | testWriteOversized ()
92 | {
93 | $state = smbclient_state_new();
94 | smbclient_state_init($state, null, SMB_USER, SMB_PASS);
95 | $file = smbclient_creat($state, $this->testuri);
96 | $this->assertTrue(is_resource($file));
97 |
98 | // Write way more bytes than data:
99 | $ret = smbclient_write($state, $file, $this->testdata, 1000);
100 | $this->assertTrue(is_integer($ret));
101 |
102 | $this->assertEquals(strlen($this->testdata), $ret);
103 |
104 | // Use the regular filesystem to check the file that was created:
105 | $this->assertFileExists($this->realfile);
106 | $this->assertEquals(filesize($this->realfile), $ret);
107 |
108 | $this->assertStringEqualsFile($this->realfile, $this->testdata);
109 | }
110 | }
111 |
--------------------------------------------------------------------------------
/tests/setup-share.sh:
--------------------------------------------------------------------------------
1 | set -e
2 |
3 | rm -rf /home/testuser/testshare
4 | mkdir /home/testuser/testshare
5 | mkdir /home/testuser/testshare/testdir
6 | echo "this is a test file" > /home/testuser/testshare/testdir/testfile.txt
7 | chown -R testuser /home/testuser
8 | mkdir /home/testuser/testshare/noaccess
9 | chmod 0700 /home/testuser/testshare/noaccess
10 |
11 | # Our test user can write to /home/testuser/testshare:
12 | chmod 0777 /home/testuser/testshare
13 |
--------------------------------------------------------------------------------