Add platform-specific profile handling and sanitize usernames

This commit is contained in:
Hakan Sariman 2025-06-17 10:44:26 +03:00
parent ee8957b052
commit bccf672dc0
6 changed files with 62 additions and 0 deletions

View File

@ -1,3 +1,6 @@
//go:build windows
// +build windows
package server
import (

View File

@ -2,6 +2,7 @@ package server
import (
"context"
"strings"
"github.com/netbirdio/netbird/client/proto"
)
@ -63,3 +64,14 @@ func (s *Server) RemoveProfile(ctx context.Context, req *proto.RemoveProfileRequ
Error: "",
}, nil
}
// sanitazeUsername sanitizes the username by removing any invalid characters
func sanitazeUsername(username string) string {
// Remove invalid characters for a username in a file path
return strings.Map(func(r rune) rune {
if r == '/' || r == '\\' || r == ':' || r == '*' || r == '?' || r == '"' || r == '<' || r == '>' || r == '|' {
return -1 // remove this character
}
return r
}, username)
}

View File

@ -0,0 +1,14 @@
//go:build darwin
// +build darwin
package server
import "path/filepath"
func getSystemProfilesDir() string {
return "/Library/Application Support/Netbird/Profiles"
}
func getUserProfilesDir(username string) string {
return filepath.Join("/Users", sanitazeUsername(username), "Library", "Application Support", "Netbird", "Profiles")
}

View File

@ -0,0 +1,15 @@
//go:build linux || dragonfly || freebsd || netbsd || openbsd
// +build linux dragonfly freebsd netbsd openbsd
package server
import "path/filepath"
func getSystemProfilesDir() string {
return "/var/lib/netbird/profiles"
}
func getUserProfilesDir(username string) string {
// ~/.config/netbird/profiles
return filepath.Join("/home", sanitazeUsername(username), ".config", "netbird", "profiles")
}

View File

@ -0,0 +1,17 @@
//go:build windows
// +build windows
package server
import (
"os"
"path/filepath"
)
func getSystemProfilesDir() string {
return filepath.Join(os.Getenv("PROGRAMDATA"), "Netbird", "Profiles")
}
func getUserProfilesDir(username string) string {
return filepath.Join(os.Getenv("LOCALAPPDATA"), "Netbird", "Profiles")
}

View File

@ -434,6 +434,7 @@ func (s *Server) Login(callerCtx context.Context, msg *proto.LoginRequest) (*pro
inputConfig.PreSharedKey = msg.OptionalPreSharedKey
}
fmt.Printf("LOGGED IN!!!!!!: %s\n", inputConfig.ConfigPath)
config, err := internal.UpdateOrCreateConfig(inputConfig)
if err != nil {
return nil, err