smbserver.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package main
  2. import (
  3. "errors"
  4. "net/http"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "time"
  9. "unicode"
  10. "imuslab.com/arozos/mod/disk/diskcapacity"
  11. "imuslab.com/arozos/mod/fileservers"
  12. "imuslab.com/arozos/mod/fileservers/servers/smbserv"
  13. "imuslab.com/arozos/mod/filesystem"
  14. prout "imuslab.com/arozos/mod/prouter"
  15. "imuslab.com/arozos/mod/storage/smbserver/aroz"
  16. )
  17. /*
  18. smbserver.go
  19. Wires the built in SMB server into the file server list. It is off until
  20. an admin turns it on, and each user then sets an SMB password of their own.
  21. */
  22. // SMBManager is nil when the manager could not start.
  23. var SMBManager *smbserv.Manager
  24. // netbiosName turns the host name into a NetBIOS style name.
  25. func netbiosName(host string) string {
  26. var b strings.Builder
  27. for _, r := range strings.ToUpper(host) {
  28. if b.Len() == 15 {
  29. break
  30. }
  31. if r == '-' || r < unicode.MaxASCII && (unicode.IsLetter(r) || unicode.IsDigit(r)) {
  32. b.WriteRune(r)
  33. }
  34. }
  35. if b.Len() == 0 {
  36. return "AROZOS"
  37. }
  38. return b.String()
  39. }
  40. // SMBServerInit sets up the manager, its endpoints and its list entry. It runs
  41. // after the Samba wrapper, which it has to share port 445 with.
  42. func SMBServerInit(router, adminRouter *prout.RouterDef) {
  43. hostname, err := os.Hostname()
  44. if err != nil {
  45. hostname = *host_name
  46. }
  47. resolver := diskcapacity.NewCapacityResolver(userHandler)
  48. option := &smbserv.ManagerOption{
  49. ServerName: netbiosName(*host_name),
  50. DNSName: hostname,
  51. DeviceID: deviceUUID,
  52. Sysdb: sysdb,
  53. SecretDir: filepath.Join("system", "smb"),
  54. CaptureDir: *smb_capture,
  55. DisplayName: *host_name,
  56. AllowMDNS: *allow_mdns,
  57. Trash: smbTrash,
  58. //The tmp folder is emptied at every boot, which clears stale copies,
  59. //so a copy whose upload failed is kept in system/ instead
  60. StageDir: filepath.Join(*tmp_directory, "smb"),
  61. KeepDir: filepath.Join("system", "smb", "failed"),
  62. CurrentUser: func(w http.ResponseWriter, r *http.Request) (string, error) {
  63. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  64. if err != nil {
  65. return "", err
  66. }
  67. return userinfo.Username, nil
  68. },
  69. ConfirmPassword: func(w http.ResponseWriter, r *http.Request) bool {
  70. return AuthValidateSecureRequest(w, r, false)
  71. },
  72. LookupUser: func(username string) (aroz.Account, aroz.Quota, error) {
  73. userinfo, err := userHandler.GetUserInfoFromUsername(username)
  74. if err != nil {
  75. return nil, nil, err
  76. }
  77. return userinfo, userinfo.StorageQuota, nil
  78. },
  79. Capacity: func(username, vpath string) (uint64, uint64, bool) {
  80. info, err := resolver.ResolveCapacityInfo(username, vpath)
  81. if err != nil || info == nil || info.Total <= 0 {
  82. return 0, 0, false
  83. }
  84. return uint64(info.Total), uint64(info.Available), true
  85. },
  86. AllowIP: func(ip string) bool {
  87. allowed, _ := authAgent.ValidateLoginIpAccess(ip)
  88. return allowed
  89. },
  90. LogAuth: func(username, remote string, ok bool) {
  91. authAgent.Logger.LogAuthByRequestInfo(username, remote, time.Now().Unix(), ok, "smb")
  92. },
  93. UserExists: authAgent.UserExists,
  94. PortConflict: func() string {
  95. if SambaShareManager != nil && SambaShareManager.IsEnabled() {
  96. return "Samba"
  97. }
  98. return ""
  99. },
  100. }
  101. if SambaShareManager != nil {
  102. option.SambaHashes = SambaShareManager.ListNTHashes
  103. }
  104. SMBManager, err = smbserv.NewManager(option)
  105. if err != nil {
  106. systemWideLogger.PrintAndLog("SMB", "The SMB server is not available", err)
  107. return
  108. }
  109. router.HandleFunc("/system/storage/smb/status", SMBManager.HandleStatus)
  110. router.HandleFunc("/system/storage/smb/activate", SMBManager.HandleActivate)
  111. router.HandleFunc("/system/storage/smb/deactivate", SMBManager.HandleDeactivate)
  112. adminRouter.HandleFunc("/system/storage/smb/config", SMBManager.HandleConfig)
  113. adminRouter.HandleFunc("/system/storage/smb/clients", SMBManager.HandleClients)
  114. adminRouter.HandleFunc("/system/storage/smb/users", SMBManager.HandleUsers)
  115. adminRouter.HandleFunc("/system/storage/smb/revoke", SMBManager.HandleRevoke)
  116. adminRouter.HandleFunc("/system/storage/smb/import", SMBManager.HandleImport)
  117. networkFileServerDaemon = append(networkFileServerDaemon, &fileservers.Server{
  118. ID: "smb",
  119. Name: "SMB (Experimental)",
  120. Desc: "Built in Windows file sharing, no Samba needed",
  121. IconPath: "img/system/network-samba.svg",
  122. DefaultPorts: []int{smbserv.DefaultPort},
  123. Ports: []int{},
  124. ForwardPortIfUpnp: false, //Port 445 must never reach the internet
  125. ConnInstrPage: "SystemAO/disk/instr/smb.html",
  126. ConfigPage: "SystemAO/disk/smb.html",
  127. EnableCheck: SMBManager.IsEnabled,
  128. ToggleFunc: SMBManager.ServerToggle,
  129. GetEndpoints: SMBManager.GetEndpoints,
  130. })
  131. }
  132. // sambaToggle starts or stops Samba, which cannot run beside a built in server
  133. // on the same port.
  134. func sambaToggle(enabled bool) error {
  135. if enabled && SMBManager != nil && SMBManager.HoldsDefaultPort() {
  136. return errors.New("the built in SMB server holds port 445. Only one of them can run at a time")
  137. }
  138. return SambaShareManager.ServerToggle(enabled)
  139. }
  140. // SMBServerShutdown stops the SMB server if it is running.
  141. func SMBServerShutdown() {
  142. if SMBManager != nil {
  143. SMBManager.Close()
  144. }
  145. }
  146. // smbTrash moves a file deleted over SMB into the trash bin, as the File
  147. // Manager does, so it can be restored from there.
  148. func smbTrash(fsh *filesystem.FileSystemHandler, subpath, rpath, username string) error {
  149. err := system_fs_moveToTrash(fsh, subpath, rpath, username)
  150. if errors.Is(err, errTrashQuota) {
  151. return aroz.ErrTrashFull
  152. }
  153. return err
  154. }