8003db49b3
Samba shares now support an 'invalid users' list (deny list), written as 'invalid users = u1,u2' in smb.conf. The UI shows a ChipPicker for valid_users and invalid_users, mutually exclusive, sourced from the system user list. feat: add ImportSystemUsers for fresh installations When NASCTL_IMPORT_ON_BOOT=true, nasctl now imports existing system users from /etc/passwd (UID 1000-60000) and /etc/group (supplemental groups), and detects which have Samba accounts via 'pdbedit -L'. Imported users are marked dirty so the admin can review before applying. New POST /api/import/users endpoint for manual re-import. This mirrors the existing import-on-boot flow for smb.conf and /etc/exports.
170 lines
4.6 KiB
Go
170 lines
4.6 KiB
Go
package web
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/darroyo/nasctl/internal/db"
|
|
"github.com/darroyo/nasctl/internal/importer"
|
|
"github.com/darroyo/nasctl/internal/modules/nfs"
|
|
"github.com/darroyo/nasctl/internal/modules/samba"
|
|
"github.com/darroyo/nasctl/internal/modules/users"
|
|
)
|
|
|
|
type importStatus struct {
|
|
Samba moduleImportStatus `json:"samba"`
|
|
NFS moduleImportStatus `json:"nfs"`
|
|
Users moduleImportStatus `json:"users"`
|
|
}
|
|
|
|
type moduleImportStatus struct {
|
|
Done bool `json:"done"`
|
|
LastImportAt string `json:"last_import_at,omitempty"`
|
|
Count int `json:"count,omitempty"`
|
|
}
|
|
|
|
func (s *Server) handleImportStatus(w http.ResponseWriter, r *http.Request) {
|
|
sambaDone, _, _ := s.DB.GetSetting("import.samba.done")
|
|
nfsDone, _, _ := s.DB.GetSetting("import.nfs.done")
|
|
usersDone, _, _ := s.DB.GetSetting("import.users.done")
|
|
|
|
shares, _ := s.DB.ListSambaShares()
|
|
exports, _ := s.DB.ListNFSExports()
|
|
userList, _ := s.DB.ListUsers()
|
|
|
|
status := importStatus{
|
|
Samba: moduleImportStatus{
|
|
Done: sambaDone == "true",
|
|
Count: len(shares),
|
|
},
|
|
NFS: moduleImportStatus{
|
|
Done: nfsDone == "true",
|
|
Count: len(exports),
|
|
},
|
|
Users: moduleImportStatus{
|
|
Done: usersDone == "true",
|
|
Count: len(userList),
|
|
},
|
|
}
|
|
|
|
if ts, ok, _ := s.DB.GetSetting("import.samba.at"); ok {
|
|
status.Samba.LastImportAt = ts
|
|
}
|
|
if ts, ok, _ := s.DB.GetSetting("import.nfs.at"); ok {
|
|
status.NFS.LastImportAt = ts
|
|
}
|
|
if ts, ok, _ := s.DB.GetSetting("import.users.at"); ok {
|
|
status.Users.LastImportAt = ts
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, status)
|
|
}
|
|
|
|
func (s *Server) handleImportSamba(w http.ResponseWriter, r *http.Request) {
|
|
if err := importer.ResetImportFlags(r.Context(), s.DB); err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
shares, err := importer.ImportSambaShares(s.SMBConfPath)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
if len(shares) == 0 {
|
|
writeJSON(w, http.StatusOK, map[string]any{"imported": 0, "message": "no shares found in smb.conf"})
|
|
return
|
|
}
|
|
|
|
if err := s.DB.ReplaceSambaShares(shares); err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
if err := s.DB.MarkDirty(samba.ModuleName); err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
_ = s.DB.SetSetting("import.samba.done", "true")
|
|
_ = s.DB.SetSetting("import.samba.at", time.Now().Format(time.RFC3339))
|
|
|
|
writeJSON(w, http.StatusOK, map[string]any{"imported": len(shares)})
|
|
}
|
|
|
|
func (s *Server) handleImportNFS(w http.ResponseWriter, r *http.Request) {
|
|
if err := importer.ResetImportFlags(r.Context(), s.DB); err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
exports, err := importer.ImportNFSExports(s.ExportsPath)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
if len(exports) == 0 {
|
|
writeJSON(w, http.StatusOK, map[string]any{"imported": 0, "message": "no exports found in exports file"})
|
|
return
|
|
}
|
|
|
|
if err := s.DB.ReplaceNFSExports(exports); err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
if err := s.DB.MarkDirty(nfs.ModuleName); err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
_ = s.DB.SetSetting("import.nfs.done", "true")
|
|
_ = s.DB.SetSetting("import.nfs.at", time.Now().Format(time.RFC3339))
|
|
|
|
writeJSON(w, http.StatusOK, map[string]any{"imported": len(exports)})
|
|
}
|
|
|
|
func (s *Server) handleImportUsers(w http.ResponseWriter, r *http.Request) {
|
|
if err := importer.ResetImportFlags(r.Context(), s.DB); err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
imported, err := importer.ImportSystemUsers(r.Context(), s.AdminUsername)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
if len(imported) == 0 {
|
|
writeJSON(w, http.StatusOK, map[string]any{"imported": 0, "message": "no system users found to import"})
|
|
return
|
|
}
|
|
|
|
if err := s.DB.ReplaceUsers(imported); err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
if err := s.DB.MarkDirty(users.ModuleName); err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
_ = s.DB.SetSetting("import.users.done", "true")
|
|
_ = s.DB.SetSetting("import.users.at", time.Now().Format(time.RFC3339))
|
|
|
|
writeJSON(w, http.StatusOK, map[string]any{"imported": len(imported)})
|
|
}
|
|
|
|
type dbExporter interface {
|
|
ReplaceSambaShares(shares []db.SambaShare) error
|
|
ReplaceNFSExports(exports []db.NFSExport) error
|
|
ReplaceUsers(users []db.User) error
|
|
MarkDirty(module string) error
|
|
}
|
|
|
|
var _ dbExporter = (*db.DB)(nil)
|