From ba63e46b5e11bddc1bc5a636ef6622b60da579f1 Mon Sep 17 00:00:00 2001
From: Robin McCorkell <rmccorkell@karoshi.org.uk>
Date: Fri, 21 Mar 2014 14:22:48 +0000
Subject: [PATCH] SMB/CIFS mount using OwnCloud logon credentials

Selecting 'SMB/CIFS Auto' in the mounts configuration allows an SMB/CIFS
mount to be configured that uses the credentials of the user logging in to
authenticate to the server.

Optionally, the username can be used as the share name, permitting home shares
to be dynamically mounted.
---
 apps/files_external/appinfo/app.php  |  3 ++
 apps/files_external/lib/config.php   | 14 +++++++--
 apps/files_external/lib/smb_auto.php | 46 ++++++++++++++++++++++++++++
 3 files changed, 60 insertions(+), 3 deletions(-)
 create mode 100644 apps/files_external/lib/smb_auto.php

diff --git a/apps/files_external/appinfo/app.php b/apps/files_external/appinfo/app.php
index 0e83660f84..f98d000b98 100644
--- a/apps/files_external/appinfo/app.php
+++ b/apps/files_external/appinfo/app.php
@@ -1,6 +1,7 @@
 <?php
 /**
  * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
+ * Copyright (c) 2014 Robin McCorkell <rmccorkell@karoshi.org.uk>
  * This file is licensed under the Affero General Public License version 3 or
  * later.
  * See the COPYING-README file.
@@ -13,6 +14,7 @@ OC::$CLASSPATH['OC\Files\Storage\OwnCloud'] = 'files_external/lib/owncloud.php';
 OC::$CLASSPATH['OC\Files\Storage\Google'] = 'files_external/lib/google.php';
 OC::$CLASSPATH['OC\Files\Storage\Swift'] = 'files_external/lib/swift.php';
 OC::$CLASSPATH['OC\Files\Storage\SMB'] = 'files_external/lib/smb.php';
+OC::$CLASSPATH['OC\Files\Storage\SMB_Auto'] = 'files_external/lib/smb_auto.php';
 OC::$CLASSPATH['OC\Files\Storage\AmazonS3'] = 'files_external/lib/amazons3.php';
 OC::$CLASSPATH['OC\Files\Storage\Dropbox'] = 'files_external/lib/dropbox.php';
 OC::$CLASSPATH['OC\Files\Storage\SFTP'] = 'files_external/lib/sftp.php';
@@ -27,4 +29,5 @@ if (OCP\Config::getAppValue('files_external', 'allow_user_mounting', 'yes') == '
 // connecting hooks
 OCP\Util::connectHook('OC_Filesystem', 'post_initMountPoints', '\OC_Mount_Config', 'initMountPointsHook');
 OCP\Util::connectHook('OC_User', 'post_login', 'OC\Files\Storage\iRODS', 'login');
+OCP\Util::connectHook('OC_User', 'post_login', 'OC\Files\Storage\SMB_Auto', 'login');
 
diff --git a/apps/files_external/lib/config.php b/apps/files_external/lib/config.php
index f7caafb74a..3512071aeb 100755
--- a/apps/files_external/lib/config.php
+++ b/apps/files_external/lib/config.php
@@ -5,6 +5,7 @@
 * @author Michael Gapczynski
 * @copyright 2012 Michael Gapczynski mtgap@owncloud.com
 * @copyright 2014 Vincent Petry <pvince81@owncloud.com>
+* @copyright 2014 Robin McCorkell <rmccorkell@karoshi.org.uk>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
@@ -122,11 +123,18 @@ class OC_Mount_Config {
 						'password' => '*Password',
 						'share' => 'Share',
 						'root' => '&Root'));
+				$backends['\OC\Files\Storage\SMB_Auto'] = array(
+					'backend' => 'SMB / CIFS Auto',
+					'configuration' => array(
+						'host' => 'URL',
+						'username_as_share' => '!Username as share',
+						'share' => '&Share',
+						'root' => '&Root'));
 			}
 		}
 
 		if(OC_Mount_Config::checkcurl()){
-		   	$backends['\OC\Files\Storage\DAV']=array(
+			$backends['\OC\Files\Storage\DAV']=array(
 				'backend' => 'WebDAV',
 				'configuration' => array(
 					'host' => 'URL',
@@ -134,7 +142,7 @@ class OC_Mount_Config {
 					'password' => '*Password',
 					'root' => '&Root',
 					'secure' => '!Secure https://'));
-		   	$backends['\OC\Files\Storage\OwnCloud']=array(
+			$backends['\OC\Files\Storage\OwnCloud']=array(
 				'backend' => 'ownCloud',
 				'configuration' => array(
 					'host' => 'URL',
@@ -185,7 +193,7 @@ class OC_Mount_Config {
 	 * @return array of mount point string as key, mountpoint config as value
 	 */
 	public static function getAbsoluteMountPoints($user) {
-		$mountPoints = array();	
+		$mountPoints = array();
 
 		$datadir = \OC_Config::getValue("datadirectory", \OC::$SERVERROOT . "/data");
 		$mount_file = \OC_Config::getValue("mount_file", $datadir . "/mount.json");
diff --git a/apps/files_external/lib/smb_auto.php b/apps/files_external/lib/smb_auto.php
new file mode 100644
index 0000000000..52fceea64f
--- /dev/null
+++ b/apps/files_external/lib/smb_auto.php
@@ -0,0 +1,46 @@
+<?php
+/**
+ * Copyright (c) 2014 Robin McCorkell <rmccorkell@karoshi.org.uk>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OC\Files\Storage;
+
+class SMB_Auto extends \OC\Files\Storage\SMB{
+	public function __construct($params) {
+		if (isset($params['host']) && \OC::$session->exists('smb-credentials')) {
+			$host=$params['host'];
+			$username_as_share = ($params['username_as_share'] === 'true');
+
+			$params_auth = \OC::$session->get('smb-credentials');
+			$user = \OC_User::getDisplayName($params_auth['uid']);
+			$password = $params_auth['password'];
+
+			$root=isset($params['root'])?$params['root']:'/';
+			$share = '';
+
+			if ($username_as_share) {
+				$share = '/'.$user;
+			} elseif (isset($params['share'])) {
+				$share = $params['share'];
+			} else {
+				throw new \Exception();
+			}
+			parent::__construct(array(
+				"user" => $user,
+				"password" => $password,
+				"host" => $host,
+				"share" => $share,
+				"root" => $root
+			));
+		} else {
+			throw new \Exception();
+		}
+	}
+
+	public static function login( $params ) {
+		\OC::$session->set('smb-credentials', $params);
+	}
+}
-- 
GitLab