Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Share.php « Wrapped « src « smb « icewind « 3rdparty « files_external « apps - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: eb68d3800b382627261d2624fdf7a483a5d69795 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
<?php
/**
 * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
 * This file is licensed under the Licensed under the MIT license:
 * http://opensource.org/licenses/MIT
 */

namespace Icewind\SMB\Wrapped;

use Icewind\SMB\AbstractShare;
use Icewind\SMB\ACL;
use Icewind\SMB\Exception\AlreadyExistsException;
use Icewind\SMB\Exception\AuthenticationException;
use Icewind\SMB\Exception\ConnectException;
use Icewind\SMB\Exception\ConnectionException;
use Icewind\SMB\Exception\DependencyException;
use Icewind\SMB\Exception\Exception;
use Icewind\SMB\Exception\FileInUseException;
use Icewind\SMB\Exception\InvalidHostException;
use Icewind\SMB\Exception\InvalidTypeException;
use Icewind\SMB\Exception\NotFoundException;
use Icewind\SMB\Exception\InvalidRequestException;
use Icewind\SMB\IFileInfo;
use Icewind\SMB\INotifyHandler;
use Icewind\SMB\IServer;
use Icewind\SMB\ISystem;
use Icewind\Streams\CallbackWrapper;
use Icewind\SMB\Native\NativeShare;
use Icewind\SMB\Native\NativeServer;

class Share extends AbstractShare {
	/**
	 * @var IServer $server
	 */
	private $server;

	/**
	 * @var string $name
	 */
	private $name;

	/**
	 * @var Connection|null $connection
	 */
	public $connection = null;

	/**
	 * @var Parser
	 */
	protected $parser;

	/**
	 * @var ISystem
	 */
	private $system;

	const MODE_MAP = [
		FileInfo::MODE_READONLY => 'r',
		FileInfo::MODE_HIDDEN   => 'h',
		FileInfo::MODE_ARCHIVE  => 'a',
		FileInfo::MODE_SYSTEM   => 's'
	];

	const EXEC_CMD = 'exec';

	/**
	 * @param IServer $server
	 * @param string $name
	 * @param ISystem $system
	 */
	public function __construct(IServer $server, string $name, ISystem $system) {
		parent::__construct();
		$this->server = $server;
		$this->name = $name;
		$this->system = $system;
		$this->parser = new Parser($server->getTimeZone());
	}

	private function getAuthFileArgument(): string {
		if ($this->server->getAuth()->getUsername()) {
			return '--authentication-file=' . $this->system->getFD(3);
		} else {
			return '';
		}
	}

	protected function getConnection(): Connection {
		$maxProtocol = $this->server->getOptions()->getMaxProtocol();
		$minProtocol = $this->server->getOptions()->getMinProtocol();
		$smbClient = $this->system->getSmbclientPath();
		$stdBuf = $this->system->getStdBufPath();
		if ($smbClient === null) {
			throw new Exception("Backend not available");
		}
		$command = sprintf(
			'%s %s%s -t %s %s %s %s %s %s',
			self::EXEC_CMD,
			$stdBuf ? $stdBuf . ' -o0 ' : '',
			$smbClient,
			$this->server->getOptions()->getTimeout(),
			$this->getAuthFileArgument(),
			$this->server->getAuth()->getExtraCommandLineArguments(),
			$maxProtocol ? "--option='client max protocol=" . $maxProtocol . "'" : "",
			$minProtocol ? "--option='client min protocol=" . $minProtocol . "'" : "",
			escapeshellarg('//' . $this->server->getHost() . '/' . $this->name)
		);
		$connection = new Connection($command, $this->parser);
		$connection->writeAuthentication($this->server->getAuth()->getUsername(), $this->server->getAuth()->getPassword());
		$connection->connect();
		if (!$connection->isValid()) {
			throw new ConnectionException((string)$connection->readLine());
		}
		// some versions of smbclient add a help message in first of the first prompt
		$connection->clearTillPrompt();
		return $connection;
	}

	/**
	 * @throws ConnectionException
	 * @throws AuthenticationException
	 * @throws InvalidHostException
	 * @psalm-assert Connection $this->connection
	 */
	protected function connect(): Connection {
		if ($this->connection and $this->connection->isValid()) {
			return $this->connection;
		}
		$this->connection = $this->getConnection();
		return $this->connection;
	}

	/**
	 * @throws ConnectionException
	 * @throws AuthenticationException
	 * @throws InvalidHostException
	 * @psalm-assert Connection $this->connection
	 */
	protected function reconnect(): void {
		if ($this->connection === null) {
			$this->connect();
		} else {
			$this->connection->reconnect();
			if (!$this->connection->isValid()) {
				throw new ConnectionException();
			}
		}
	}

	/**
	 * Get the name of the share
	 *
	 * @return string
	 */
	public function getName(): string {
		return $this->name;
	}

	protected function simpleCommand(string $command, string $path): bool {
		$escapedPath = $this->escapePath($path);
		$cmd = $command . ' ' . $escapedPath;
		$output = $this->execute($cmd);
		return $this->parseOutput($output, $path);
	}

	/**
	 * List the content of a remote folder
	 *
	 * @param string $path
	 * @return IFileInfo[]
	 *
	 * @throws NotFoundException
	 * @throws InvalidTypeException
	 */
	public function dir(string $path): array {
		$escapedPath = $this->escapePath($path);
		$output = $this->execute('cd ' . $escapedPath);
		//check output for errors
		$this->parseOutput($output, $path);
		$output = $this->execute('dir');

		$this->execute('cd /');

		return $this->parser->parseDir($output, $path, function (string $path) {
			return $this->getAcls($path);
		});
	}

	/**
	 * @param string $path
	 * @return IFileInfo
	 */
	public function stat(string $path): IFileInfo {
		// some windows server setups don't seem to like the allinfo command
		// use the dir command instead to get the file info where possible
		if ($path !== "" && $path !== "/") {
			$parent = dirname($path);
			$dir = $this->dir($parent);
			$file = array_values(array_filter($dir, function (IFileInfo $info) use ($path) {
				return $info->getPath() === $path;
			}));
			if ($file) {
				return $file[0];
			}
		}

		$escapedPath = $this->escapePath($path);
		$output = $this->execute('allinfo ' . $escapedPath);
		// Windows and non Windows Fileserver may respond different
		// to the allinfo command for directories. If the result is a single
		// line = error line, redo it with a different allinfo parameter
		if ($escapedPath == '""' && count($output) < 2) {
			$output = $this->execute('allinfo ' . '"."');
		}
		if (count($output) < 3) {
			$this->parseOutput($output, $path);
		}
		$stat = $this->parser->parseStat($output);
		return new FileInfo($path, basename($path), $stat['size'], $stat['mtime'], $stat['mode'], function () use ($path) {
			return $this->getAcls($path);
		});
	}

	/**
	 * Create a folder on the share
	 *
	 * @param string $path
	 * @return bool
	 *
	 * @throws NotFoundException
	 * @throws AlreadyExistsException
	 */
	public function mkdir(string $path): bool {
		return $this->simpleCommand('mkdir', $path);
	}

	/**
	 * Remove a folder on the share
	 *
	 * @param string $path
	 * @return bool
	 *
	 * @throws NotFoundException
	 * @throws InvalidTypeException
	 */
	public function rmdir(string $path): bool {
		return $this->simpleCommand('rmdir', $path);
	}

	/**
	 * Delete a file on the share
	 *
	 * @param string $path
	 * @param bool $secondTry
	 * @return bool
	 * @throws InvalidTypeException
	 * @throws NotFoundException
	 * @throws \Exception
	 */
	public function del(string $path, bool $secondTry = false): bool {
		//del return a file not found error when trying to delete a folder
		//we catch it so we can check if $path doesn't exist or is of invalid type
		try {
			return $this->simpleCommand('del', $path);
		} catch (NotFoundException $e) {
			//no need to do anything with the result, we just check if this throws the not found error
			try {
				$this->simpleCommand('ls', $path);
			} catch (NotFoundException $e2) {
				throw $e;
			} catch (\Exception $e2) {
				throw new InvalidTypeException($path);
			}
			throw $e;
		} catch (FileInUseException $e) {
			if ($secondTry) {
				throw $e;
			}
			$this->reconnect();
			return $this->del($path, true);
		}
	}

	/**
	 * Rename a remote file
	 *
	 * @param string $from
	 * @param string $to
	 * @return bool
	 *
	 * @throws NotFoundException
	 * @throws AlreadyExistsException
	 */
	public function rename(string $from, string $to): bool {
		$path1 = $this->escapePath($from);
		$path2 = $this->escapePath($to);
		$output = $this->execute('rename ' . $path1 . ' ' . $path2);
		return $this->parseOutput($output, $to);
	}

	/**
	 * Upload a local file
	 *
	 * @param string $source local file
	 * @param string $target remove file
	 * @return bool
	 *
	 * @throws NotFoundException
	 * @throws InvalidTypeException
	 */
	public function put(string $source, string $target): bool {
		$path1 = $this->escapeLocalPath($source); //first path is local, needs different escaping
		$path2 = $this->escapePath($target);
		$output = $this->execute('put ' . $path1 . ' ' . $path2);
		return $this->parseOutput($output, $target);
	}

	/**
	 * Download a remote file
	 *
	 * @param string $source remove file
	 * @param string $target local file
	 * @return bool
	 *
	 * @throws NotFoundException
	 * @throws InvalidTypeException
	 */
	public function get(string $source, string $target): bool {
		$path1 = $this->escapePath($source);
		$path2 = $this->escapeLocalPath($target); //second path is local, needs different escaping
		$output = $this->execute('get ' . $path1 . ' ' . $path2);
		return $this->parseOutput($output, $source);
	}

	/**
	 * Open a readable stream to a remote file
	 *
	 * @param string $source
	 * @return resource a read only stream with the contents of the remote file
	 *
	 * @throws NotFoundException
	 * @throws InvalidTypeException
	 */
	public function read(string $source) {
		$source = $this->escapePath($source);
		// since returned stream is closed by the caller we need to create a new instance
		// since we can't re-use the same file descriptor over multiple calls
		$connection = $this->getConnection();
		stream_set_blocking($connection->getOutputStream(), false);

		$connection->write('get ' . $source . ' ' . $this->system->getFD(5));
		$connection->write('exit');
		$fh = $connection->getFileOutputStream();
		$fh = CallbackWrapper::wrap($fh, function() use ($connection) {
			$connection->write('');
		});
		if (!is_resource($fh)) {
			throw new Exception("Failed to wrap file output");
		}
		return $fh;
	}

	/**
	 * Open a writable stream to a remote file
	 *
	 * @param string $target
	 * @return resource a write only stream to upload a remote file
	 *
	 * @throws NotFoundException
	 * @throws InvalidTypeException
	 */
	public function write(string $target) {
		$target = $this->escapePath($target);
		// since returned stream is closed by the caller we need to create a new instance
		// since we can't re-use the same file descriptor over multiple calls
		$connection = $this->getConnection();

		$fh = $connection->getFileInputStream();
		$connection->write('put ' . $this->system->getFD(4) . ' ' . $target);
		$connection->write('exit');

		// use a close callback to ensure the upload is finished before continuing
		// this also serves as a way to keep the connection in scope
		$stream = CallbackWrapper::wrap($fh, function() use ($connection) {
			$connection->write('');
		}, null, function () use ($connection) {
			$connection->close(false); // dont terminate, give the upload some time
		});
		if (is_resource($stream)) {
			return $stream;
		} else {
			throw new InvalidRequestException($target);
		}
	}

	/**
	 * Append to stream
	 * Note: smbclient does not support this (Use php-libsmbclient)
	 *
	 * @param string $target
	 *
	 * @throws DependencyException
	 */
	public function append(string $target) {
		throw new DependencyException('php-libsmbclient is required for append');
	}

	/**
	 * @param string $path
	 * @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL
	 * @return mixed
	 */
	public function setMode(string $path, int $mode) {
		$modeString = '';
		foreach (self::MODE_MAP as $modeByte => $string) {
			if ($mode & $modeByte) {
				$modeString .= $string;
			}
		}
		$path = $this->escapePath($path);

		// first reset the mode to normal
		$cmd = 'setmode ' . $path . ' -rsha';
		$output = $this->execute($cmd);
		$this->parseOutput($output, $path);

		if ($mode !== FileInfo::MODE_NORMAL) {
			// then set the modes we want
			$cmd = 'setmode ' . $path . ' ' . $modeString;
			$output = $this->execute($cmd);
			return $this->parseOutput($output, $path);
		} else {
			return true;
		}
	}

	/**
	 * @param string $path
	 * @return INotifyHandler
	 * @throws ConnectionException
	 * @throws DependencyException
	 */
	public function notify(string $path): INotifyHandler {
		if (!$this->system->getStdBufPath()) { //stdbuf is required to disable smbclient's output buffering
			throw new DependencyException('stdbuf is required for usage of the notify command');
		}
		$connection = $this->getConnection(); // use a fresh connection since the notify command blocks the process
		$command = 'notify ' . $this->escapePath($path);
		$connection->write($command . PHP_EOL);
		return new NotifyHandler($connection, $path);
	}

	/**
	 * @param string $command
	 * @return string[]
	 */
	protected function execute(string $command): array {
		$this->connect()->write($command);
		return $this->connect()->read();
	}

	/**
	 * check output for errors
	 *
	 * @param string[] $lines
	 * @param string $path
	 *
	 * @return bool
	 * @throws AlreadyExistsException
	 * @throws \Icewind\SMB\Exception\AccessDeniedException
	 * @throws \Icewind\SMB\Exception\NotEmptyException
	 * @throws InvalidTypeException
	 * @throws \Icewind\SMB\Exception\Exception
	 * @throws NotFoundException
	 */
	protected function parseOutput(array $lines, string $path = ''): bool {
		if (count($lines) === 0) {
			return true;
		} else {
			$this->parser->checkForError($lines, $path);
		}
	}

	/**
	 * @param string $string
	 * @return string
	 */
	protected function escape(string $string): string {
		return escapeshellarg($string);
	}

	/**
	 * @param string $path
	 * @return string
	 */
	protected function escapePath(string $path): string {
		$this->verifyPath($path);
		if ($path === '/') {
			$path = '';
		}
		$path = str_replace('/', '\\', $path);
		$path = str_replace('"', '^"', $path);
		$path = ltrim($path, '\\');
		return '"' . $path . '"';
	}

	/**
	 * @param string $path
	 * @return string
	 */
	protected function escapeLocalPath(string $path): string {
		$path = str_replace('"', '\"', $path);
		return '"' . $path . '"';
	}

	/**
	 * @param string $path
	 * @return ACL[]
	 * @throws ConnectionException
	 * @throws ConnectException
	 */
	protected function getAcls(string $path): array {
		$commandPath = $this->system->getSmbcAclsPath();
		if (!$commandPath) {
			return [];
		}

		$command = sprintf(
			'%s %s %s %s/%s %s',
			$commandPath,
			$this->getAuthFileArgument(),
			$this->server->getAuth()->getExtraCommandLineArguments(),
			escapeshellarg('//' . $this->server->getHost()),
			escapeshellarg($this->name),
			escapeshellarg($path)
		);
		$connection = new RawConnection($command);
		$connection->writeAuthentication($this->server->getAuth()->getUsername(), $this->server->getAuth()->getPassword());
		$connection->connect();
		if (!$connection->isValid()) {
			throw new ConnectionException((string)$connection->readLine());
		}

		$rawAcls = $connection->readAll();
		return $this->parser->parseACLs($rawAcls);
	}

	public function getServer(): IServer {
		return $this->server;
	}

	public function __destruct() {
		unset($this->connection);
	}
}