2021-02-28 07:58:09 +08:00
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
2021-06-24 21:44:19 +08:00
|
|
|
"errors"
|
2022-02-22 19:45:50 +08:00
|
|
|
"fmt"
|
|
|
|
"regexp"
|
2021-11-05 06:15:17 +08:00
|
|
|
"strconv"
|
2022-02-22 19:45:50 +08:00
|
|
|
"strings"
|
2021-02-28 07:58:09 +08:00
|
|
|
"time"
|
|
|
|
|
2021-11-05 06:15:17 +08:00
|
|
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
2021-08-06 01:23:02 +08:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-11-05 06:15:17 +08:00
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
2021-06-24 21:44:19 +08:00
|
|
|
"gorm.io/gorm"
|
2021-02-28 07:58:09 +08:00
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
)
|
|
|
|
|
2021-11-05 06:15:17 +08:00
|
|
|
const (
|
2023-01-18 00:43:44 +08:00
|
|
|
ErrUserExists = Error("User already exists")
|
|
|
|
ErrUserNotFound = Error("User not found")
|
|
|
|
ErrUserStillHasNodes = Error("User not empty: node(s) found")
|
|
|
|
ErrInvalidUserName = Error("Invalid user name")
|
2021-11-05 06:15:17 +08:00
|
|
|
)
|
2021-05-09 23:12:05 +08:00
|
|
|
|
2022-02-23 04:05:39 +08:00
|
|
|
const (
|
|
|
|
// value related to RFC 1123 and 952.
|
|
|
|
labelHostnameLength = 63
|
|
|
|
)
|
|
|
|
|
2023-01-18 00:43:44 +08:00
|
|
|
var invalidCharsInUserRegex = regexp.MustCompile("[^a-z0-9-.]+")
|
2022-02-22 19:45:50 +08:00
|
|
|
|
2023-01-18 00:43:44 +08:00
|
|
|
// User is the way Headscale implements the concept of users in Tailscale
|
2021-02-28 07:58:09 +08:00
|
|
|
//
|
2023-01-18 00:43:44 +08:00
|
|
|
// At the end of the day, users in Tailscale are some kind of 'bubbles' or users
|
2021-02-28 07:58:09 +08:00
|
|
|
// that contain our machines.
|
2023-01-18 00:43:44 +08:00
|
|
|
type User struct {
|
2021-02-28 07:58:09 +08:00
|
|
|
gorm.Model
|
|
|
|
Name string `gorm:"unique"`
|
|
|
|
}
|
|
|
|
|
2023-01-18 00:43:44 +08:00
|
|
|
// CreateUser creates a new User. Returns error if could not be created
|
|
|
|
// or another user already exists.
|
|
|
|
func (h *Headscale) CreateUser(name string) (*User, error) {
|
2022-03-08 05:55:54 +08:00
|
|
|
err := CheckForFQDNRules(name)
|
2022-02-22 19:45:50 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-18 00:43:44 +08:00
|
|
|
user := User{}
|
|
|
|
if err := h.db.Where("name = ?", name).First(&user).Error; err == nil {
|
|
|
|
return nil, ErrUserExists
|
2021-02-28 07:58:09 +08:00
|
|
|
}
|
2023-01-18 00:43:44 +08:00
|
|
|
user.Name = name
|
|
|
|
if err := h.db.Create(&user).Error; err != nil {
|
2021-08-06 01:23:02 +08:00
|
|
|
log.Error().
|
2023-01-18 00:43:44 +08:00
|
|
|
Str("func", "CreateUser").
|
2021-08-06 01:23:02 +08:00
|
|
|
Err(err).
|
|
|
|
Msg("Could not create row")
|
2021-11-14 23:46:09 +08:00
|
|
|
|
2021-02-28 07:58:09 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-14 23:46:09 +08:00
|
|
|
|
2023-01-18 00:43:44 +08:00
|
|
|
return &user, nil
|
2021-02-28 07:58:09 +08:00
|
|
|
}
|
|
|
|
|
2023-01-18 00:43:44 +08:00
|
|
|
// DestroyUser destroys a User. Returns error if the User does
|
2021-05-09 23:12:05 +08:00
|
|
|
// not exist or if there are machines associated with it.
|
2023-01-18 00:43:44 +08:00
|
|
|
func (h *Headscale) DestroyUser(name string) error {
|
|
|
|
user, err := h.GetUser(name)
|
2021-05-09 23:12:05 +08:00
|
|
|
if err != nil {
|
2023-01-18 00:43:44 +08:00
|
|
|
return ErrUserNotFound
|
2021-05-09 23:12:05 +08:00
|
|
|
}
|
|
|
|
|
2023-01-18 00:43:44 +08:00
|
|
|
machines, err := h.ListMachinesByUser(name)
|
2021-05-09 23:12:05 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-15 03:32:03 +08:00
|
|
|
if len(machines) > 0 {
|
2023-01-18 00:43:44 +08:00
|
|
|
return ErrUserStillHasNodes
|
2021-11-14 03:01:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
keys, err := h.ListPreAuthKeys(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-15 03:32:03 +08:00
|
|
|
for _, key := range keys {
|
2021-11-16 02:31:52 +08:00
|
|
|
err = h.DestroyPreAuthKey(key)
|
2021-11-14 04:24:32 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-09 23:12:05 +08:00
|
|
|
}
|
|
|
|
|
2023-01-18 00:43:44 +08:00
|
|
|
if result := h.db.Unscoped().Delete(&user); result.Error != nil {
|
2021-10-16 23:14:37 +08:00
|
|
|
return result.Error
|
2021-05-09 23:12:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-18 00:43:44 +08:00
|
|
|
// RenameUser renames a User. Returns error if the User does
|
|
|
|
// not exist or if another User exists with the new name.
|
|
|
|
func (h *Headscale) RenameUser(oldName, newName string) error {
|
2022-02-22 19:45:50 +08:00
|
|
|
var err error
|
2023-01-18 00:43:44 +08:00
|
|
|
oldUser, err := h.GetUser(oldName)
|
2021-10-16 23:20:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-08 05:55:54 +08:00
|
|
|
err = CheckForFQDNRules(newName)
|
2022-02-22 19:45:50 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-18 00:43:44 +08:00
|
|
|
_, err = h.GetUser(newName)
|
2021-10-16 23:20:06 +08:00
|
|
|
if err == nil {
|
2023-01-18 00:43:44 +08:00
|
|
|
return ErrUserExists
|
2021-10-16 23:20:06 +08:00
|
|
|
}
|
2023-01-18 00:43:44 +08:00
|
|
|
if !errors.Is(err, ErrUserNotFound) {
|
2021-10-16 23:20:06 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-18 00:43:44 +08:00
|
|
|
oldUser.Name = newName
|
2021-10-16 23:20:06 +08:00
|
|
|
|
2023-01-18 00:43:44 +08:00
|
|
|
if result := h.db.Save(&oldUser); result.Error != nil {
|
2021-10-16 23:20:06 +08:00
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
2021-05-09 23:12:05 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-18 00:43:44 +08:00
|
|
|
// GetUser fetches a user by name.
|
|
|
|
func (h *Headscale) GetUser(name string) (*User, error) {
|
|
|
|
user := User{}
|
|
|
|
if result := h.db.First(&user, "name = ?", name); errors.Is(
|
2021-11-13 16:36:45 +08:00
|
|
|
result.Error,
|
|
|
|
gorm.ErrRecordNotFound,
|
|
|
|
) {
|
2023-01-18 00:43:44 +08:00
|
|
|
return nil, ErrUserNotFound
|
2021-02-28 07:58:09 +08:00
|
|
|
}
|
2021-11-14 23:46:09 +08:00
|
|
|
|
2023-01-18 00:43:44 +08:00
|
|
|
return &user, nil
|
2021-02-28 07:58:09 +08:00
|
|
|
}
|
|
|
|
|
2023-01-18 00:43:44 +08:00
|
|
|
// ListUsers gets all the existing users.
|
|
|
|
func (h *Headscale) ListUsers() ([]User, error) {
|
|
|
|
users := []User{}
|
|
|
|
if err := h.db.Find(&users).Error; err != nil {
|
2021-02-28 07:58:09 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-14 23:46:09 +08:00
|
|
|
|
2023-01-18 00:43:44 +08:00
|
|
|
return users, nil
|
2021-02-28 07:58:09 +08:00
|
|
|
}
|
|
|
|
|
2023-01-18 00:43:44 +08:00
|
|
|
// ListMachinesByUser gets all the nodes in a given user.
|
|
|
|
func (h *Headscale) ListMachinesByUser(name string) ([]Machine, error) {
|
2022-03-08 05:55:54 +08:00
|
|
|
err := CheckForFQDNRules(name)
|
2022-02-22 19:45:50 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-18 00:43:44 +08:00
|
|
|
user, err := h.GetUser(name)
|
2021-02-28 07:58:09 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
machines := []Machine{}
|
2023-01-18 00:43:44 +08:00
|
|
|
if err := h.db.Preload("AuthKey").Preload("AuthKey.User").Preload("User").Where(&Machine{UserID: user.ID}).Find(&machines).Error; err != nil {
|
2021-02-28 07:58:09 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-14 23:46:09 +08:00
|
|
|
|
2021-11-05 06:15:17 +08:00
|
|
|
return machines, nil
|
2021-02-28 07:58:09 +08:00
|
|
|
}
|
|
|
|
|
2023-01-18 00:43:44 +08:00
|
|
|
// SetMachineUser assigns a Machine to a user.
|
|
|
|
func (h *Headscale) SetMachineUser(machine *Machine, username string) error {
|
|
|
|
err := CheckForFQDNRules(username)
|
2022-02-22 19:45:50 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-18 00:43:44 +08:00
|
|
|
user, err := h.GetUser(username)
|
2021-02-28 07:58:09 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-18 00:43:44 +08:00
|
|
|
machine.User = *user
|
2022-05-02 17:47:21 +08:00
|
|
|
if result := h.db.Save(&machine); result.Error != nil {
|
|
|
|
return result.Error
|
|
|
|
}
|
2021-11-14 23:46:09 +08:00
|
|
|
|
2021-02-28 07:58:09 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-18 00:43:44 +08:00
|
|
|
func (n *User) toTailscaleUser() *tailcfg.User {
|
2021-11-15 03:32:03 +08:00
|
|
|
user := tailcfg.User{
|
2021-02-28 07:58:09 +08:00
|
|
|
ID: tailcfg.UserID(n.ID),
|
2021-07-11 22:39:19 +08:00
|
|
|
LoginName: n.Name,
|
2021-02-28 07:58:09 +08:00
|
|
|
DisplayName: n.Name,
|
|
|
|
ProfilePicURL: "",
|
2021-07-11 22:39:19 +08:00
|
|
|
Domain: "headscale.net",
|
2021-02-28 07:58:09 +08:00
|
|
|
Logins: []tailcfg.LoginID{},
|
|
|
|
Created: time.Time{},
|
|
|
|
}
|
2021-11-14 23:46:09 +08:00
|
|
|
|
2021-11-15 03:32:03 +08:00
|
|
|
return &user
|
2021-02-28 07:58:09 +08:00
|
|
|
}
|
2021-10-15 23:09:55 +08:00
|
|
|
|
2023-01-18 00:43:44 +08:00
|
|
|
func (n *User) toTailscaleLogin() *tailcfg.Login {
|
2021-11-15 03:32:03 +08:00
|
|
|
login := tailcfg.Login{
|
2021-10-15 23:09:55 +08:00
|
|
|
ID: tailcfg.LoginID(n.ID),
|
|
|
|
LoginName: n.Name,
|
|
|
|
DisplayName: n.Name,
|
|
|
|
ProfilePicURL: "",
|
|
|
|
Domain: "headscale.net",
|
|
|
|
}
|
2021-11-14 23:46:09 +08:00
|
|
|
|
2021-11-15 03:32:03 +08:00
|
|
|
return &login
|
2021-10-15 23:09:55 +08:00
|
|
|
}
|
2021-10-19 22:26:18 +08:00
|
|
|
|
2022-11-23 00:36:19 +08:00
|
|
|
func (h *Headscale) getMapResponseUserProfiles(
|
|
|
|
machine Machine,
|
|
|
|
peers Machines,
|
|
|
|
) []tailcfg.UserProfile {
|
2023-01-18 00:43:44 +08:00
|
|
|
userMap := make(map[string]User)
|
|
|
|
userMap[machine.User.Name] = machine.User
|
2021-11-15 03:32:03 +08:00
|
|
|
for _, peer := range peers {
|
2023-01-18 00:43:44 +08:00
|
|
|
userMap[peer.User.Name] = peer.User // not worth checking if already is there
|
2021-10-18 05:58:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
profiles := []tailcfg.UserProfile{}
|
2023-01-18 00:43:44 +08:00
|
|
|
for _, user := range userMap {
|
|
|
|
displayName := user.Name
|
2022-11-23 00:36:19 +08:00
|
|
|
|
|
|
|
if h.cfg.BaseDomain != "" {
|
2023-01-18 00:43:44 +08:00
|
|
|
displayName = fmt.Sprintf("%s@%s", user.Name, h.cfg.BaseDomain)
|
2022-11-23 00:36:19 +08:00
|
|
|
}
|
|
|
|
|
2021-10-18 05:58:09 +08:00
|
|
|
profiles = append(profiles,
|
|
|
|
tailcfg.UserProfile{
|
2023-01-18 00:43:44 +08:00
|
|
|
ID: tailcfg.UserID(user.ID),
|
|
|
|
LoginName: user.Name,
|
2022-11-23 00:36:19 +08:00
|
|
|
DisplayName: displayName,
|
2021-10-18 05:58:09 +08:00
|
|
|
})
|
|
|
|
}
|
2021-11-14 23:46:09 +08:00
|
|
|
|
2021-10-18 05:58:09 +08:00
|
|
|
return profiles
|
|
|
|
}
|
2021-11-05 06:15:17 +08:00
|
|
|
|
2023-01-18 00:43:44 +08:00
|
|
|
func (n *User) toProto() *v1.User {
|
|
|
|
return &v1.User{
|
2021-11-16 01:24:24 +08:00
|
|
|
Id: strconv.FormatUint(uint64(n.ID), Base10),
|
2021-11-05 06:15:17 +08:00
|
|
|
Name: n.Name,
|
|
|
|
CreatedAt: timestamppb.New(n.CreatedAt),
|
|
|
|
}
|
|
|
|
}
|
2022-02-22 19:45:50 +08:00
|
|
|
|
2023-01-18 00:43:44 +08:00
|
|
|
// NormalizeToFQDNRules will replace forbidden chars in user
|
|
|
|
// it can also return an error if the user doesn't respect RFC 952 and 1123.
|
2022-03-08 05:55:54 +08:00
|
|
|
func NormalizeToFQDNRules(name string, stripEmailDomain bool) (string, error) {
|
2022-02-22 19:45:50 +08:00
|
|
|
name = strings.ToLower(name)
|
|
|
|
name = strings.ReplaceAll(name, "'", "")
|
2022-02-23 21:28:20 +08:00
|
|
|
atIdx := strings.Index(name, "@")
|
|
|
|
if stripEmailDomain && atIdx > 0 {
|
|
|
|
name = name[:atIdx]
|
2022-02-23 21:03:07 +08:00
|
|
|
} else {
|
|
|
|
name = strings.ReplaceAll(name, "@", ".")
|
|
|
|
}
|
2023-01-18 00:43:44 +08:00
|
|
|
name = invalidCharsInUserRegex.ReplaceAllString(name, "-")
|
2022-02-22 19:45:50 +08:00
|
|
|
|
|
|
|
for _, elt := range strings.Split(name, ".") {
|
2022-02-23 04:05:39 +08:00
|
|
|
if len(elt) > labelHostnameLength {
|
2022-02-22 19:45:50 +08:00
|
|
|
return "", fmt.Errorf(
|
|
|
|
"label %v is more than 63 chars: %w",
|
|
|
|
elt,
|
2023-01-18 00:43:44 +08:00
|
|
|
ErrInvalidUserName,
|
2022-02-22 19:45:50 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return name, nil
|
|
|
|
}
|
2022-02-23 18:07:24 +08:00
|
|
|
|
2022-03-08 05:55:54 +08:00
|
|
|
func CheckForFQDNRules(name string) error {
|
2022-02-23 18:07:24 +08:00
|
|
|
if len(name) > labelHostnameLength {
|
|
|
|
return fmt.Errorf(
|
2022-05-17 02:31:32 +08:00
|
|
|
"DNS segment must not be over 63 chars. %v doesn't comply with this rule: %w",
|
2022-02-23 18:07:24 +08:00
|
|
|
name,
|
2023-01-18 00:43:44 +08:00
|
|
|
ErrInvalidUserName,
|
2022-02-23 18:07:24 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
if strings.ToLower(name) != name {
|
|
|
|
return fmt.Errorf(
|
2022-05-17 02:31:32 +08:00
|
|
|
"DNS segment should be lowercase. %v doesn't comply with this rule: %w",
|
2022-02-23 18:07:24 +08:00
|
|
|
name,
|
2023-01-18 00:43:44 +08:00
|
|
|
ErrInvalidUserName,
|
2022-02-23 18:07:24 +08:00
|
|
|
)
|
|
|
|
}
|
2023-01-18 00:43:44 +08:00
|
|
|
if invalidCharsInUserRegex.MatchString(name) {
|
2022-02-23 18:07:24 +08:00
|
|
|
return fmt.Errorf(
|
2022-05-17 02:31:32 +08:00
|
|
|
"DNS segment should only be composed of lowercase ASCII letters numbers, hyphen and dots. %v doesn't comply with theses rules: %w",
|
2022-02-23 18:07:24 +08:00
|
|
|
name,
|
2023-01-18 00:43:44 +08:00
|
|
|
ErrInvalidUserName,
|
2022-02-23 18:07:24 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|