-
Notifications
You must be signed in to change notification settings - Fork 1
/
mount_manager.h
144 lines (114 loc) · 5.43 KB
/
mount_manager.h
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
// Copyright 2017 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SMBPROVIDER_MOUNT_MANAGER_H_
#define SMBPROVIDER_MOUNT_MANAGER_H_
#include <memory>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <base/callback.h>
#include <base/files/file_util.h>
#include <base/macros.h>
#include <base/memory/weak_ptr.h>
#include <libpasswordprovider/password.h>
#include "smbprovider/constants.h"
#include "smbprovider/id_map.h"
#include "smbprovider/metadata_cache.h"
#include "smbprovider/mount_config.h"
#include "smbprovider/mount_tracker.h"
#include "smbprovider/samba_interface.h"
#include "smbprovider/smb_credential.h"
namespace base {
class TickClock;
};
namespace smbprovider {
// Gets a password_provider::Password object from |password_fd|. The data has to
// be in the format of "{password_length}{password}". If the read fails, this
// returns an empty unique_ptr.
std::unique_ptr<password_provider::Password> GetPassword(
const base::ScopedFD& password_fd);
// MountManager maintains a mapping of open mounts and the metadata associated
// with each mount.
class MountManager : public base::SupportsWeakPtr<MountManager> {
public:
using SambaInterfaceFactory = base::Callback<std::unique_ptr<SambaInterface>(
MountManager*, const MountConfig& mount_config)>;
MountManager(std::unique_ptr<MountTracker> mount_tracker,
SambaInterfaceFactory samba_interface_factory);
MountManager(const MountManager&) = delete;
MountManager& operator=(const MountManager&) = delete;
~MountManager();
// Returns true if |mount_id| is already mounted.
bool IsAlreadyMounted(int32_t mount_id) const;
// Adds |mount_root| to the |mounts_| map and outputs the |mount_id|
// that was assigned to this mount. Ids are >=0 and are not
// re-used within the lifetime of this class. |mount_config| holds the mount
// options set by the client.
// TODO(zentaro): Review if this should have a maximum number of mounts,
// even if it is relatively large. It may already be enforced at a higher
// level.
void AddMount(const std::string& mount_root,
SmbCredential credential,
const MountConfig& mount_config,
int32_t* mount_id);
// Returns true if |mount_id| was mounted and removes the mount.
bool RemoveMount(int32_t mount_id);
// Returns the number of mounts.
size_t MountCount() const { return mount_tracker_->MountCount(); }
// Uses the mount root associated with |mount_id| and appends |entry_path|
// to form |full_path|.
bool GetFullPath(int32_t mount_id,
const std::string& entry_path,
std::string* full_path) const;
// Gets a pointer to the metadata cache for |mount_id|.
bool GetMetadataCache(int32_t mount_id, MetadataCache** cache) const;
// Uses the mount root associated with |mount_id| to remove the root path
// from |full_path| to yield a relative path.
std::string GetRelativePath(int32_t mount_id,
const std::string& full_path) const;
// Returns a pointer to the SambaInterface corresponding to |mount_id|.
bool GetSambaInterface(int32_t mount_id,
SambaInterface** samba_interface) const;
// Returns a pointer to the system SambaInterface.
SambaInterface* GetSystemSambaInterface() const;
// Samba authentication function callback. DCHECKS that the buffer lengths are
// non-zero. Returns false when buffer lengths cannot support credential
// length or when credential are not found for |share_path|.
bool GetAuthentication(SambaInterface::SambaInterfaceId samba_interface_id,
const std::string& share_path,
char* workgroup,
int32_t workgroup_length,
char* username,
int32_t username_length,
char* password,
int32_t password_length) const;
// Updates the SmbCredential for the given mount. Returns true if updating the
// mount's credential was successful. Returns false if the mount does not
// exist.
bool UpdateMountCredential(int32_t mount_id, SmbCredential credential);
// Updates the share path for the given mount.Returns false if the mount does
// not exist.
bool UpdateSharePath(int32_t mount_id, const std::string& share_path);
// Write the password for |mount_id| to the password file given in the mount
// credentials. Return false if unable to write the password.
bool SavePasswordToFile(int32_t mount_id);
// Delete the password file for |mount_id|, if it exists. Return false if
// unable to erase the file.
bool ErasePasswordFile(int32_t mount_id);
private:
// Runs |samba_interface_factory_|.
std::unique_ptr<SambaInterface> CreateSambaInterface(
const MountConfig& mount_config);
// Returns the SambaInterfaceId from |system_samba_interface_|.
SambaInterface::SambaInterfaceId GetSystemSambaInterfaceId();
// Returns the SmbCredential for |samba_interface_id|.
const SmbCredential& GetCredential(
SambaInterface::SambaInterfaceId samba_interface_id) const;
const std::unique_ptr<MountTracker> mount_tracker_;
const SambaInterfaceFactory samba_interface_factory_;
const std::unique_ptr<SambaInterface> system_samba_interface_;
};
} // namespace smbprovider
#endif // SMBPROVIDER_MOUNT_MANAGER_H_