2019-02-15 15:12:02 +00:00
|
|
|
package settings
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"log"
|
|
|
|
"os"
|
2021-12-20 22:16:35 +00:00
|
|
|
"path/filepath"
|
2019-02-15 15:12:02 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2019-04-20 13:15:28 +00:00
|
|
|
|
|
|
|
"github.com/spf13/afero"
|
2019-02-15 15:12:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
invalidFilenameChars = regexp.MustCompile(`[^0-9A-Za-z@_\-.]`)
|
|
|
|
|
|
|
|
dashes = regexp.MustCompile(`[\-]+`)
|
|
|
|
)
|
|
|
|
|
2019-04-20 13:15:28 +00:00
|
|
|
// MakeUserDir makes the user directory according to settings.
|
2020-05-31 23:12:36 +00:00
|
|
|
func (s *Settings) MakeUserDir(username, userScope, serverRoot string) (string, error) {
|
2019-02-15 15:12:02 +00:00
|
|
|
var err error
|
|
|
|
userScope = strings.TrimSpace(userScope)
|
2019-04-20 13:15:28 +00:00
|
|
|
if userScope == "" || userScope == "./" {
|
2019-02-15 15:12:02 +00:00
|
|
|
userScope = "."
|
|
|
|
}
|
|
|
|
|
2020-05-31 23:12:36 +00:00
|
|
|
if !s.CreateUserDir {
|
2019-02-15 15:12:02 +00:00
|
|
|
return userScope, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fs := afero.NewBasePathFs(afero.NewOsFs(), serverRoot)
|
|
|
|
|
2019-04-20 13:22:19 +00:00
|
|
|
// Use the default auto create logic only if specific scope is not the default scope
|
2020-05-31 23:12:36 +00:00
|
|
|
if userScope != s.Defaults.Scope {
|
2019-04-20 13:22:19 +00:00
|
|
|
// Try create the dir, for example: settings.Defaults.Scope == "." and userScope == "./foo"
|
2019-02-15 15:12:02 +00:00
|
|
|
if userScope != "." {
|
|
|
|
err = fs.MkdirAll(userScope, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("create user: failed to mkdir user home dir: [%s]", userScope)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return userScope, err
|
|
|
|
}
|
|
|
|
|
2019-04-20 13:22:19 +00:00
|
|
|
// Clean username first
|
2019-02-15 15:12:02 +00:00
|
|
|
username = cleanUsername(username)
|
|
|
|
if username == "" || username == "-" || username == "." {
|
|
|
|
log.Printf("create user: invalid user for home dir creation: [%s]", username)
|
|
|
|
return "", errors.New("invalid user for home dir creation")
|
|
|
|
}
|
|
|
|
|
2019-04-20 13:22:19 +00:00
|
|
|
// Create default user dir
|
2021-12-20 22:16:35 +00:00
|
|
|
userHomeBase := filepath.Join(s.Defaults.Scope, "users")
|
|
|
|
userHome := filepath.Join(userHomeBase, username)
|
2019-02-15 15:12:02 +00:00
|
|
|
err = fs.MkdirAll(userHome, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("create user: failed to mkdir user home dir: [%s]", userHome)
|
|
|
|
} else {
|
|
|
|
log.Printf("create user: mkdir user home dir: [%s] successfully.", userHome)
|
|
|
|
}
|
2019-04-20 13:15:28 +00:00
|
|
|
return userHome, err
|
2019-02-15 15:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func cleanUsername(s string) string {
|
|
|
|
// Remove any trailing space to avoid ending on -
|
|
|
|
s = strings.Trim(s, " ")
|
|
|
|
s = strings.Replace(s, "..", "", -1)
|
|
|
|
|
|
|
|
// Replace all characters which not in the list `0-9A-Za-z@_\-.` with a dash
|
|
|
|
s = invalidFilenameChars.ReplaceAllString(s, "-")
|
|
|
|
|
|
|
|
// Remove any multiple dashes caused by replacements above
|
|
|
|
s = dashes.ReplaceAllString(s, "-")
|
|
|
|
return s
|
2019-04-20 13:15:28 +00:00
|
|
|
}
|