core icon indicating copy to clipboard operation
core copied to clipboard

Share keys not created with new nested directories - encryption

Open JasonEades opened this issue 12 years ago • 11 comments

Steps to reproduce

  1. Create a folder named "root".
  2. Open "root"
  3. Create a folder named "child"
  4. Open "child"
  5. Try to upload a document
  6. Application errors
  7. Refresh and note that the document uploaded but bombed when trying to create the new sharekey file.

Expected behaviour

New directory structure must be created and keys created in the share-keys folder.

Actual behaviour

Document is actually uploaded and encryption keyfiles are created but share keys are not created because the directory structure for the share keys isn't found in share-keys. The document can not be opened.

Server configuration

Operating system: Windows Server 2008R2 Web server: IIS 7.5 Database: MySql PHP version: 5.5.12 ownCloud version: (see ownCloud admin page) 6.0.3 (stable) Updated from an older ownCloud or fresh install: Fresh install List of activated apps: Activity Deleted Files Documents Encryption First Run Wizard Full Text Search PDF Viewer Share Files Versions Video Viewer The content of config/config.php:

<?php
$CONFIG = array (
'instanceid' => 'removed',
'passwordsalt' => 'removed',
'trusted_domains' => 
array (
0 => 'localhost',
1 => 'myDomainIp',
2 => 'myDomain',
),
'datadirectory' => 'E:\datadirector',
'logdateformat' => 'F d, Y H:i:s',
'log_authfailip' => true,
'logtimezone' => 'America/New_York',
'dbtype' => 'mysql',
'version' => '6.0.3.1',
'dbname' => 'ownclouddbname',
'dbhost' => 'localhost',
'dbtableprefix' => 'oc_',
'dbuser' => 'aUser',
'dbpassword' => 'removed',
'installed' => true,
'mail_smtpdebug' => false,
'mail_domain' => 'mail_domain',
'mail_smtpmode' => 'smtp',
'mail_smtphost' => 'ip',
'mail_smtpport' => port,
'mail_smtptimeout' => 50,
'mail_smtpsecure' => 'tls',
'mail_smtpauth' => true,
'mail_smtpauthtype' => 'LOGIN',
'mail_smtpname' => 'no-reply',
'knowledgebaseenabled' => true,
'‘enable_avatars’' => true,
'maintenance' => false,
'mail_smtppassword' => 'aPassword', 
'forcessl' => true,
);

Are you using external storage, if yes which one: local/smb/sftp/... local only Are you using encryption: yes/no yes

Client configuration

Browser: IE8+, Chome, Firefox Operating system: Win 7

Logs

Web server error log

ownCloud log (data/owncloud.log)

{"app":"PHP","message":"file_put_contents(C:\\Applications\\WebApplications\\ownCloudData\/ocadmin\/files-encryption\/share-keys\/root\/child\/100supervisorsinvestigationreport.pdf.ocadmin.sharekey): failed to open stream: No such file or directory at C:\\inetpub\\wwwroot\\OwnCloud\\lib\\private\\files\\storage\\mappedlocal.php#156","level":3,"time":"June 13, 2014 17:09:32"}

Browser log


JasonEades avatar Jun 13 '14 17:06 JasonEades

I just tested this again using the following steps:

  1. Create a folder named "root2".
  2. Open "root2"
  3. Upload document (it works)
  4. Create a folder named "child"
  5. Open "child"
  6. Upload a document (it works!)

Something related to the recursive creation of the folder structure when creating sharekeys? I'll add some logging but this seems somewhat related to the other issue I reported: https://github.com/owncloud/core/issues/8936

JasonEades avatar Jun 13 '14 17:06 JasonEades

@schiesbn

karlitschek avatar Jun 13 '14 17:06 karlitschek

I needed to get this working so as a quick fix I modified setShareKey inside apps/file_encryption/libs/keymanager.php to recursively create the directories just like is being done in setFileKey. It now looks like this:

private static function setShareKey(\OC_FilesystemView $view, $path, $shareKey) {

        \OCP\Util::writeLog('keymanager library logging', utf8_encode('path = ' . $path . ' sharekey = ' . $shareKey), \OCP\Util::WARN);

        $proxyStatus = \OC_FileProxy::$enabled;
        \OC_FileProxy::$enabled = false;

        if (!$view->is_dir($path)) {

            \OCP\Util::writeLog('keymanager library logging', 'path is not a dir', \OCP\Util::WARN);

            // create all parent folders
            $info = pathinfo($path);
            $keyfileFolderName = $view->getLocalFolder($info['dirname']);

            \OCP\Util::writeLog('keymanager library logging', 'share key file folder name = ' . $keyfileFolderName, \OCP\Util::WARN);

            if (!file_exists($keyfileFolderName)) {
                \OCP\Util::writeLog('keymanager library logging', 'key file folder name does not exist, calling mkdir', \OCP\Util::WARN);
                mkdir($keyfileFolderName, 0750, true);
            }
        } else {
            \OCP\Util::writeLog('keymanager library logging', 'path IS a dir.  not creating dirs', \OCP\Util::WARN);
        }

        $result = $view->file_put_contents($path, $shareKey);

        \OC_FileProxy::$enabled = $proxyStatus;

        if (is_int($result) && $result > 0) {
            return true;
        } else {
            return false;
        }
    }

This seems to have gotten me working for now.

JasonEades avatar Jun 13 '14 20:06 JasonEades

@schiesbn didn't you fix this recently ?

PVince81 avatar Jun 20 '14 08:06 PVince81

@PVince81 that would be great if he did! I'm not familiar with this codebase or PHP in general so my dirty hacks to fix these issues are going to come back and bite me I'm sure. These problems only seem to occur with encryption and sharing enabled on windows but with this configuration, I've ran into multiple problems with the share keys during renaming.

JasonEades avatar Jun 20 '14 17:06 JasonEades

@PVince81 At least for rename: https://github.com/owncloud/core/pull/8988

nosignleft avatar Jun 23 '14 09:06 nosignleft

setShareKey() is private method, it will always be called from the setSareKeys() method at the same class. In setShareKeys() we call keySetPreparation() (the same function also called in setFileKey()) https://github.com/owncloud/core/blob/master/apps/files_encryption/lib/keymanager.php#L519

This works just fine here. I wounder why it works on your system for the file-key but not for the share-keys? Maybe again a windows case-sensitivity issue? Can you try to debug it to see what goes wrong there?

schiessle avatar Jun 23 '14 10:06 schiessle

@schiesbn Basically keySetPreparation() doesn't handle creating the directories because explode is not working with my paths.

Review my logging below. You see that the only sub_dir inside sub_dirs array contains multiple directories and we call mkdir() without recursion so it doesn't really do anything. However, the process still works for the key-files because we are calling the recursive overload of mkdir() inside of setFileKey() if the file doesn't exist. Basically keySetPreparation() is not handling the directory creation for file-keys because explode isn't working, setFileKey() is handling it. The same problem exists for shareKeys except setShareKey() was not doing this extra check and creating.

// inside keySetPreparation for key-file
{"app":"keymanager logging","message":"inside keySetPreparation.  path = \/aRootFolder\/anotherFolder\/something\/100SupervisorsInvestigationReport.pdf basepath = \/ocadmin\/files_encryption\/keyfiles","level":2,"time":"June 23, 2014 13:40:31"}
{"app":"keymanager logging","message":"path_parts dirname = aRootFolder\/anotherFolder\/something","level":2,"time":"June 23, 2014 13:40:31"}
{"app":"keymanager logging","message":"file exists in subdirectory, attempting to explode and create.  basepath: \/ocadmin\/files_encryption\/keyfiles path_parts[dirname]: aRootFolder\/anotherFolder\/something","level":2,"time":"June 23, 2014 13:40:31"}
{"app":"keymanager logging","message":"explode params.  directory_separator: \\ path to explode: \/ocadmin\/files_encryption\/keyfiles\/aRootFolder\/anotherFolder\/something","level":2,"time":"June 23, 2014 13:40:31"}
{"app":"keymanager logging","message":"looping subdirs. subdir = \/ocadmin\/files_encryption\/keyfiles\/aRootFolder\/anotherFolder\/something","level":2,"time":"June 23, 2014 13:40:31"}
{"app":"keymanager logging","message":"dir = \/\/ocadmin\/files_encryption\/keyfiles\/aRootFolder\/anotherFolder\/something","level":2,"time":"June 23, 2014 13:40:31"}
{"app":"keymanager logging","message":"not is_dir.  calling mkdir on dir \/\/ocadmin\/files_encryption\/keyfiles\/aRootFolder\/anotherFolder\/something","level":2,"time":"June 23, 2014 13:40:31"}

// inside keySetPreparation for share key
{"app":"keymanager logging","message":"inside keySetPreparation.  path = \/aRootFolder\/anotherFolder\/something\/100SupervisorsInvestigationReport.pdf basepath = \/ocadmin\/files_encryption\/share-keys","level":2,"time":"June 23, 2014 13:40:31"}
{"app":"keymanager logging","message":"path_parts dirname = aRootFolder\/anotherFolder\/something","level":2,"time":"June 23, 2014 13:40:31"}
{"app":"keymanager logging","message":"file exists in subdirectory, attempting to explode and create.  basepath: \/ocadmin\/files_encryption\/share-keys path_parts[dirname]: aRootFolder\/anotherFolder\/something","level":2,"time":"June 23, 2014 13:40:31"}
{"app":"keymanager logging","message":"explode params.  directory_separator: \\ path to explode: \/ocadmin\/files_encryption\/share-keys\/aRootFolder\/anotherFolder\/something","level":2,"time":"June 23, 2014 13:40:31"}
{"app":"keymanager logging","message":"looping subdirs. subdir = \/ocadmin\/files_encryption\/share-keys\/aRootFolder\/anotherFolder\/something","level":2,"time":"June 23, 2014 13:40:31"}
{"app":"keymanager logging","message":"dir = \/\/ocadmin\/files_encryption\/share-keys\/aRootFolder\/anotherFolder\/something","level":2,"time":"June 23, 2014 13:40:31"}
{"app":"keymanager logging","message":"not is_dir.  calling mkdir on dir \/\/ocadmin\/files_encryption\/share-keys\/aRootFolder\/anotherFolder\/something","level":2,"time":"June 23, 2014 13:40:31"}

// basically the same right? so why is key-file created but not share key?
// because we create the directories in setKeyFile
{"app":"keymanager logging","message":"inside setFileKey.  file does not exist.  calling mkdir with recursion.  keyfileFolderName: C:\\Applications\\WebApplications\\ownCloudData\/ocadmin\/files-encryption\/keyfiles\/aRootFolder\/anotherFolder\/something","level":2,"time":"June 23, 2014 13:57:28"}

I didn't know what I would break modifying keySetPreparation() and trying to fix the explode so I just added the additional mkdir() recursive check to setShareKey()

JasonEades avatar Jun 23 '14 14:06 JasonEades

@JasonEades can you try this changes https://github.com/owncloud/core/pull/9181/files (line number can be different because this patch is against master, but the same code also exists in OC6)

schiessle avatar Jun 24 '14 07:06 schiessle

Excellent! This is working now. Thanks @schiesbn for your help with this.

JasonEades avatar Jun 24 '14 12:06 JasonEades

fixed in master and stable6, will be released wiith OC7 and OC6.0.5

schiessle avatar Jun 25 '14 10:06 schiessle