]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/CIFSPlugin.pm
cifs: fix uninitialized warning in check_connection
[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 {
17 my ($server, $share, $mountpoint, $mountdata) = @_;
18
19 $server = "[$server]" if Net::IP::ip_is_ipv6($server);
20 my $source = "//${server}/$share";
21 $mountdata = PVE::ProcFSTools::parse_proc_mounts() if !$mountdata;
22
23 return $mountpoint if grep {
24 $_->[2] =~ /^cifs/ &&
25 $_->[0] =~ m|^\Q$source\E/?$| &&
26 $_->[1] eq $mountpoint
27 } @$mountdata;
28 return undef;
29 }
30
31 sub cifs_cred_file_name {
32 my ($storeid) = @_;
33
34 return "/etc/pve/priv/${storeid}.cred";
35 }
36
37 sub cifs_set_credentials {
38 my ($password, $storeid) = @_;
39
40 my $cred_file = cifs_cred_file_name($storeid);
41
42 PVE::Tools::file_set_contents($cred_file, "password=$password\n");
43
44 return $cred_file;
45 }
46
47 sub get_cred_file {
48 my ($storeid) = @_;
49
50 my $cred_file = cifs_cred_file_name($storeid);
51
52 return -e $cred_file ? $cred_file : undef;
53 }
54
55 sub cifs_mount {
56 my ($server, $share, $mountpoint, $storeid, $smbver, $user, $domain) = @_;
57
58 $server = "[$server]" if Net::IP::ip_is_ipv6($server);
59 my $source = "//${server}/$share";
60
61 my $cmd = ['/bin/mount', '-t', 'cifs', $source, $mountpoint, '-o', 'soft', '-o'];
62
63 if (my $cred_file = get_cred_file($storeid)) {
64 push @$cmd, "username=$user", '-o', "credentials=$cred_file";
65 push @$cmd, '-o', "domain=$domain" if defined($domain);
66 } else {
67 push @$cmd, 'guest,username=guest';
68 }
69
70 push @$cmd, '-o', defined($smbver) ? "vers=$smbver" : "vers=3.0";
71
72 run_command($cmd, errmsg => "mount error");
73 }
74
75 # Configuration
76
77 sub type {
78 return 'cifs';
79 }
80
81 sub plugindata {
82 return {
83 content => [ { images => 1, rootdir => 1, vztmpl => 1, iso => 1,
84 backup => 1}, { images => 1 }],
85 format => [ { raw => 1, qcow2 => 1, vmdk => 1 } , 'raw' ],
86 };
87 }
88
89 sub properties {
90 return {
91 share => {
92 description => "CIFS share.",
93 type => 'string',
94 },
95 password => {
96 description => "Password for CIFS share.",
97 type => 'string',
98 maxLength => 256,
99 },
100 domain => {
101 description => "CIFS domain.",
102 type => 'string',
103 optional => 1,
104 maxLength => 256,
105 },
106 smbversion => {
107 description => "",
108 type => 'string',
109 optional => 1,
110 },
111 };
112 }
113
114 sub options {
115 return {
116 path => { fixed => 1 },
117 server => { fixed => 1 },
118 share => { fixed => 1 },
119 nodes => { optional => 1 },
120 disable => { optional => 1 },
121 maxfiles => { optional => 1 },
122 content => { optional => 1 },
123 format => { optional => 1 },
124 username => { optional => 1 },
125 password => { optional => 1},
126 domain => { optional => 1},
127 smbversion => { optional => 1},
128 };
129 }
130
131
132 sub check_config {
133 my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
134
135 $config->{path} = "/mnt/pve/$sectionId" if $create && !$config->{path};
136
137 return $class->SUPER::check_config($sectionId, $config, $create, $skipSchemaCheck);
138 }
139
140 # Storage implementation
141
142 sub status {
143 my ($class, $storeid, $scfg, $cache) = @_;
144
145 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
146 if !$cache->{mountdata};
147
148 my $path = $scfg->{path};
149 my $server = $scfg->{server};
150 my $share = $scfg->{share};
151
152 return undef
153 if !cifs_is_mounted($server, $share, $path, $cache->{mountdata});
154
155 return $class->SUPER::status($storeid, $scfg, $cache);
156 }
157
158 sub activate_storage {
159 my ($class, $storeid, $scfg, $cache) = @_;
160
161 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
162 if !$cache->{mountdata};
163
164 my $path = $scfg->{path};
165 my $server = $scfg->{server};
166 my $share = $scfg->{share};
167
168 if (!cifs_is_mounted($server, $share, $path, $cache->{mountdata})) {
169
170 mkpath $path;
171
172 die "unable to activate storage '$storeid' - " .
173 "directory '$path' does not exist\n" if ! -d $path;
174
175 cifs_mount($server, $share, $path, $storeid, $scfg->{smbversion},
176 $scfg->{username}, $scfg->{domain});
177 }
178
179 $class->SUPER::activate_storage($storeid, $scfg, $cache);
180 }
181
182 sub deactivate_storage {
183 my ($class, $storeid, $scfg, $cache) = @_;
184
185 $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts()
186 if !$cache->{mountdata};
187
188 my $path = $scfg->{path};
189 my $server = $scfg->{server};
190 my $share = $scfg->{share};
191
192 if (cifs_is_mounted($server, $share, $path, $cache->{mountdata})) {
193 my $cmd = ['/bin/umount', $path];
194 run_command($cmd, errmsg => 'umount error');
195 }
196 }
197
198 sub check_connection {
199 my ($class, $storeid, $scfg) = @_;
200
201 my $server = $scfg->{server};
202
203 my $cmd = ['/usr/bin/smbclient', '-L', $server, '-d', '0', '-m'];
204
205 push @$cmd, $scfg->{smbversion} ? "smb".int($scfg->{smbversion}) : 'smb3';
206
207 if (my $cred_file = get_cred_file($storeid)) {
208 push @$cmd, '-U', $scfg->{username}, '-A', $cred_file;
209 push @$cmd, '-W', $scfg->{domain} if defined($scfg->{domain});
210 } else {
211 push @$cmd, '-U', 'Guest','-N';
212 }
213
214 my $out_str;
215 eval {
216 run_command($cmd, timeout => 2, outfunc => sub {$out_str .= shift;},
217 errfunc => sub {});
218 };
219
220 if (my $err = $@) {
221 die "$out_str\n" if defined($out_str) &&
222 ($out_str =~ m/NT_STATUS_ACCESS_DENIED/);
223 return 0;
224 }
225
226 return 1;
227 }
228
229 1;