]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/CIFSPlugin.pm
cifs: use empty string instead of / as default directory
[pve-storage.git] / PVE / Storage / CIFSPlugin.pm
1 package PVE::Storage::CIFSPlugin;
2
3 use strict;
4 use warnings;
5 use Net::IP;
6 use PVE::Tools qw(run_command);
7 use PVE::ProcFSTools;
8 use File::Path;
9 use PVE::Storage::Plugin;
10 use PVE::JSONSchema qw(get_standard_option);
11
12 use base qw(PVE::Storage::Plugin);
13
14 # CIFS helper functions
15
16 sub cifs_is_mounted : prototype($$) {
17 my ($scfg, $mountdata) = @_;
18
19 my ($mountpoint, $server, $share) = $scfg->@{'path', 'server', 'share'};
20 my $subdir = $scfg->{subdir} // '';
21
22 $server = "[$server]" if Net::IP::ip_is_ipv6($server);
23 my $source = "//${server}/$share$subdir";
24 $mountdata = PVE::ProcFSTools::parse_proc_mounts() if !$mountdata;
25
26 return $mountpoint if grep {
27 $_->[2] =~ /^cifs/ &&
28 $_->[0] =~ m|^\Q$source\E/?$| &&
29 $_->[1] eq $mountpoint
30 } @$mountdata;
31 return undef;
32 }
33
34 sub cifs_cred_file_name {
35 my ($storeid) = @_;
36 return "/etc/pve/priv/storage/${storeid}.pw";
37 }
38
39 sub cifs_delete_credentials {
40 my ($storeid) = @_;
41
42 if (my $cred_file = get_cred_file($storeid)) {
43 unlink($cred_file) or warn "removing cifs credientials '$cred_file' failed: $!\n";
44 }
45 }
46
47 sub cifs_set_credentials {
48 my ($password, $storeid) = @_;
49
50 my $cred_file = cifs_cred_file_name($storeid);
51 mkdir "/etc/pve/priv/storage";
52
53 PVE::Tools::file_set_contents($cred_file, "password=$password\n");
54
55 return $cred_file;
56 }
57
58 sub get_cred_file {
59 my ($storeid) = @_;
60
61 my $cred_file = cifs_cred_file_name($storeid);
62
63 if (-e $cred_file) {
64 return $cred_file;
65 }
66 return undef;
67 }
68
69 sub cifs_mount : prototype($$$$$) {
70 my ($scfg, $storeid, $smbver, $user, $domain) = @_;
71
72 my ($mountpoint, $server, $share) = $scfg->@{'path', 'server', 'share'};
73 my $subdir = $scfg->{subdir} // '';
74
75 $server = "[$server]" if Net::IP::ip_is_ipv6($server);
76 my $source = "//${server}/$share$subdir";
77
78 my $cmd = ['/bin/mount', '-t', 'cifs', $source, $mountpoint, '-o', 'soft', '-o'];
79
80 if (my $cred_file = get_cred_file($storeid)) {
81 push @$cmd, "username=$user", '-o', "credentials=$cred_file";
82 push @$cmd, '-o', "domain=$domain" if defined($domain);
83 } else {
84 push @$cmd, 'guest,username=guest';
85 }
86
87 push @$cmd, '-o', defined($smbver) ? "vers=$smbver" : "vers=default";
88
89 run_command($cmd, errmsg => "mount error");
90 }
91
92 # Configuration
93
94 sub type {
95 return 'cifs';
96 }
97
98 sub plugindata {
99 return {
100 content => [ { images => 1, rootdir => 1, vztmpl => 1, iso => 1,
101 backup => 1, snippets => 1}, { images => 1 }],
102 format => [ { raw => 1, qcow2 => 1, vmdk => 1 } , 'raw' ],
103 };
104 }
105
106 sub properties {
107 return {
108 share => {
109 description => "CIFS share.",
110 type => 'string',
111 },
112 password => {
113 description => "Password for accessing the share/datastore.",
114 type => 'string',
115 maxLength => 256,
116 },
117 domain => {
118 description => "CIFS domain.",
119 type => 'string',
120 optional => 1,
121 maxLength => 256,
122 },
123 smbversion => {
124 description => "SMB protocol version. 'default' if not set, negotiates the highest SMB2+"
125 ." version supported by both the client and server.",
126 type => 'string',
127 default => 'default',
128 enum => ['default', '2.0', '2.1', '3', '3.0', '3.11'],
129 optional => 1,
130 },
131 };
132 }
133
134 sub options {
135 return {
136 path => { fixed => 1 },
137 'content-dirs' => { optional => 1 },
138 server => { fixed => 1 },
139 share => { fixed => 1 },
140 subdir => { optional => 1 },
141 nodes => { optional => 1 },
142 disable => { optional => 1 },
143 maxfiles => { optional => 1 },
144 'prune-backups' => { optional => 1 },
145 'max-protected-backups' => { optional => 1 },
146 content => { optional => 1 },
147 format => { optional => 1 },
148 username => { optional => 1 },
149 password => { optional => 1},
150 domain => { optional => 1},
151 smbversion => { optional => 1},
152 mkdir => { optional => 1 },
153 bwlimit => { optional => 1 },
154 preallocation => { optional => 1 },
155 };
156 }
157
158
159 sub check_config {
160 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
161
162 $config->{path} = "/mnt/pve/$sectionId" if $create && !$config->{path};
163
164 return $class->SUPER::check_config($sectionId, $config, $create, $skipSchemaCheck);
165 }
166
167 # Storage implementation
168
169 sub on_add_hook {
170 my ($class, $storeid, $scfg, %sensitive) = @_;
171
172 if (defined($sensitive{password})) {
173 cifs_set_credentials($sensitive{password}, $storeid);
174 if (!exists($scfg->{username})) {
175 warn "storage $storeid: ignoring password parameter, no user set\n";
176 }
177 } else {
178 cifs_delete_credentials($storeid);
179 }
180
181 return;
182 }
183
184 sub on_update_hook {
185 my ($class, $storeid, $scfg, %sensitive) = @_;
186
187 return if !exists($sensitive{password});
188
189 if (defined($sensitive{password})) {
190 cifs_set_credentials($sensitive{password}, $storeid);
191 if (!exists($scfg->{username})) {
192 warn "storage $storeid: ignoring password parameter, no user set\n";
193 }
194 } else {
195 cifs_delete_credentials($storeid);
196 }
197
198 return;
199 }
200
201 sub on_delete_hook {
202 my ($class, $storeid, $scfg) = @_;
203
204 cifs_delete_credentials($storeid);
205
206 return;
207 }
208
209 sub status {
210 my ($class, $storeid, $scfg, $cache) = @_;
211
212 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
213 if !$cache->{mountdata};
214
215 return undef
216 if !cifs_is_mounted($scfg, $cache->{mountdata});
217
218 return $class->SUPER::status($storeid, $scfg, $cache);
219 }
220
221 sub activate_storage {
222 my ($class, $storeid, $scfg, $cache) = @_;
223
224 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
225 if !$cache->{mountdata};
226
227 my $path = $scfg->{path};
228
229 if (!cifs_is_mounted($scfg, $cache->{mountdata})) {
230
231 mkpath $path if !(defined($scfg->{mkdir}) && !$scfg->{mkdir});
232
233 die "unable to activate storage '$storeid' - " .
234 "directory '$path' does not exist\n" if ! -d $path;
235
236 cifs_mount($scfg, $storeid, $scfg->{smbversion},
237 $scfg->{username}, $scfg->{domain});
238 }
239
240 $class->SUPER::activate_storage($storeid, $scfg, $cache);
241 }
242
243 sub deactivate_storage {
244 my ($class, $storeid, $scfg, $cache) = @_;
245
246 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
247 if !$cache->{mountdata};
248
249 my $path = $scfg->{path};
250
251 if (cifs_is_mounted($scfg, $cache->{mountdata})) {
252 my $cmd = ['/bin/umount', $path];
253 run_command($cmd, errmsg => 'umount error');
254 }
255 }
256
257 sub check_connection {
258 my ($class, $storeid, $scfg) = @_;
259
260 my $servicename = '//'.$scfg->{server}.'/'.$scfg->{share};
261
262 my $cmd = ['/usr/bin/smbclient', $servicename, '-d', '0'];
263
264 if (defined($scfg->{smbversion}) && $scfg->{smbversion} ne 'default') {
265 # max-protocol version, so basically only relevant for smb2 vs smb3
266 push @$cmd, '-m', "smb" . int($scfg->{smbversion});
267 }
268
269 if (my $cred_file = get_cred_file($storeid)) {
270 push @$cmd, '-U', $scfg->{username}, '-A', $cred_file;
271 push @$cmd, '-W', $scfg->{domain} if defined($scfg->{domain});
272 } else {
273 push @$cmd, '-U', 'Guest','-N';
274 }
275 push @$cmd, '-c', 'echo 1 0';
276
277 my $out_str;
278 my $out = sub { $out_str .= shift };
279
280 eval { run_command($cmd, timeout => 10, outfunc => $out, errfunc => sub {}) };
281
282 if (my $err = $@) {
283 die "$out_str\n" if defined($out_str) &&
284 ($out_str =~ m/NT_STATUS_(ACCESS_DENIED|LOGON_FAILURE)/);
285 return 0;
286 }
287
288 return 1;
289 }
290
291 # FIXME remove on the next APIAGE reset.
292 # Deprecated, use get_volume_attribute instead.
293 sub get_volume_notes {
294 my $class = shift;
295 PVE::Storage::DirPlugin::get_volume_notes($class, @_);
296 }
297
298 # FIXME remove on the next APIAGE reset.
299 # Deprecated, use update_volume_attribute instead.
300 sub update_volume_notes {
301 my $class = shift;
302 PVE::Storage::DirPlugin::update_volume_notes($class, @_);
303 }
304
305 sub get_volume_attribute {
306 return PVE::Storage::DirPlugin::get_volume_attribute(@_);
307 }
308
309 sub update_volume_attribute {
310 return PVE::Storage::DirPlugin::update_volume_attribute(@_);
311 }
312
313 1;