Skip to content

Commit dd18b3d

Browse files
committed
preflight: add check, fix and cleanup funcs for enabling SMB share on windows
earlier these were part of the MSI since we want to enable this only when user has set 'enable-shared-dirs' setting, this is moved to the preflight package where we can check this config value before hand this should remove at least of the warnings reported by virustotal for the MSI https://www.virustotal.com/gui/file/31b402dcc1da24265074a21a26018d6cde8eef0b63c77a18f89eb079b6556790
1 parent 8b82bdd commit dd18b3d

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

pkg/crc/preflight/preflight_checks_windows.go

+63
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package preflight
22

33
import (
4+
"errors"
45
"fmt"
6+
"os/user"
57
"strconv"
68
"strings"
79

@@ -209,3 +211,64 @@ func checkAdminHelperNamedPipeAccessible() error {
209211
}
210212
return nil
211213
}
214+
215+
func checkFileAndPrinterSharingIsEnabled() error {
216+
cmd := `(Get-NetFirewallRule -Group '@FirewallAPI.dll,-28502' | Where-Object {$_.Profile -eq 'Private, Public'}).Enabled`
217+
stdout, stderr, err := powershell.Execute(cmd)
218+
if err != nil {
219+
return fmt.Errorf("unable to check if Printer and File Sharing is enabled %v: %s", err, stderr)
220+
}
221+
if strings.Contains(stdout, "False") {
222+
return errors.New("Printer and File Sharing is disabled")
223+
}
224+
return nil
225+
}
226+
227+
func fixFileAndPrinterSharing() error {
228+
cmd := `Set-NetFirewallRule -Group '@FirewallAPI.dll,-28502' -Enabled True -Profile 'Private,Public'`
229+
stdout, stderr, err := powershell.ExecuteAsAdmin("to enable Printer and File Sharing", cmd)
230+
if err != nil {
231+
return fmt.Errorf("unable to check if Printer and File Sharing is enabled %v: %s: %s", err, stdout, stderr)
232+
}
233+
return nil
234+
}
235+
236+
func checkCRCSmbShareCreated() error {
237+
cmd := `Get-SmbShare -Name crc-dir0`
238+
stdout, stderr, err := powershell.Execute(cmd)
239+
if err != nil {
240+
return fmt.Errorf("unable to check if Printer and File Sharing is enabled %v: %s: %s", err, stdout, stderr)
241+
}
242+
return nil
243+
}
244+
245+
func fixCRCSmbShareCreated() error {
246+
u, err := user.Current()
247+
if err != nil {
248+
return fmt.Errorf("unable to get user information for homedir and username: %v", err)
249+
}
250+
cmd := fmt.Sprintf(`New-SmbShare -Name 'crc-dir0' -Path '%s' -FullAccess '%s'`, u.HomeDir, username())
251+
_, stderr, err := powershell.ExecuteAsAdmin("create new SMB share for home directory", cmd)
252+
if err != nil {
253+
return fmt.Errorf("unable to get create new SMB share %v: %s", err, stderr)
254+
}
255+
return nil
256+
}
257+
258+
func removeSmbShare() error {
259+
cmd := `Remove-SmbShare -Name 'crc-dir0' -Force`
260+
_, stderr, err := powershell.ExecuteAsAdmin("remove SMB share for home directory", cmd)
261+
if err != nil {
262+
return fmt.Errorf("unable to get create new SMB share %v: %s", err, stderr)
263+
}
264+
return nil
265+
}
266+
267+
func removeFirewallRuleAllowingPrinterAndFileSharing() error {
268+
cmd := `Set-NetFirewallRule -Group '@FirewallAPI.dll,-28502' -Enabled False -Profile 'Private,Public'`
269+
stdout, stderr, err := powershell.ExecuteAsAdmin("to disable Printer and File Sharing", cmd)
270+
if err != nil {
271+
logging.Warnf("unable to turn off Printer and File Sharing %v: %s: %s", err, stdout, stderr)
272+
}
273+
return nil
274+
}

pkg/crc/preflight/preflight_windows.go

+27
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,32 @@ var userPartOfCrcUsersAndHypervAdminsGroupCheck = Check{
166166
labels: labels{Os: Windows},
167167
}
168168

169+
// Checks to verify and setup SMB share is created and file sharing is enabled
170+
var smbShareCreatedAndFileSharingEnabledChecks = []Check{
171+
{
172+
configKeySuffix: "check-file-sharing-enabled",
173+
checkDescription: "Checking if Printer and File Sharing is enabled",
174+
check: checkFileAndPrinterSharingIsEnabled,
175+
fixDescription: "Enabling Printer and File Sharing",
176+
fix: fixFileAndPrinterSharing,
177+
cleanupDescription: "Disabling Printer and File Sharing",
178+
cleanup: removeFirewallRuleAllowingPrinterAndFileSharing,
179+
180+
labels: labels{Os: Windows, SharedDir: Enabled},
181+
},
182+
{
183+
configKeySuffix: "check-smb-share-exists",
184+
checkDescription: "Checking if SMB share crc-dir0 exists",
185+
check: checkCRCSmbShareCreated,
186+
fixDescription: "Creating SMB share crc-dir0",
187+
fix: fixCRCSmbShareCreated,
188+
cleanupDescription: "Removing SMB share crc-dir0",
189+
cleanup: removeSmbShare,
190+
191+
labels: labels{Os: Windows, SharedDir: Enabled},
192+
},
193+
}
194+
169195
var errReboot = errors.New("Please reboot your system and run 'crc setup' to complete the setup process")
170196

171197
func username() string {
@@ -218,6 +244,7 @@ func getChecks(bundlePath string, preset crcpreset.Preset, enableBundleQuayFallb
218244
checks = append(checks, daemonTaskChecks...)
219245
checks = append(checks, adminHelperServiceCheks...)
220246
checks = append(checks, sshPortCheck())
247+
checks = append(checks, smbShareCreatedAndFileSharingEnabledChecks...)
221248
return checks
222249
}
223250

pkg/crc/preflight/preflight_windows_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
func TestCountConfigurationOptions(t *testing.T) {
1414
cfg := config.New(config.NewEmptyInMemoryStorage(), config.NewEmptyInMemorySecretStorage())
1515
RegisterSettings(cfg)
16-
assert.Len(t, cfg.AllConfigs(), 16)
16+
assert.Len(t, cfg.AllConfigs(), 18)
1717
}
1818

1919
func TestCountPreflights(t *testing.T) {

0 commit comments

Comments
 (0)