smbserver.go 5.7 KB

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