wg-portal/internal/server/usermanager.go

495 lines
13 KiB
Go
Raw Normal View History

2020-11-05 13:37:51 -05:00
package server
import (
2020-11-06 06:21:47 -05:00
"bytes"
2020-11-05 13:37:51 -05:00
"crypto/md5"
"errors"
"fmt"
"net"
"strings"
2020-11-06 06:21:47 -05:00
"text/template"
2020-11-05 13:37:51 -05:00
"time"
2020-11-07 12:36:23 -05:00
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/validator/v10"
2020-11-06 06:21:47 -05:00
"github.com/h44z/wg-portal/internal/wireguard"
2020-11-05 13:37:51 -05:00
"github.com/h44z/wg-portal/internal/common"
"github.com/h44z/wg-portal/internal/ldap"
log "github.com/sirupsen/logrus"
2020-11-06 06:21:47 -05:00
"github.com/skip2/go-qrcode"
2020-11-05 13:37:51 -05:00
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
2020-11-07 12:36:23 -05:00
//
// CUSTOM VALIDATORS ----------------------------------------------------------------------------
//
var cidrList validator.Func = func(fl validator.FieldLevel) bool {
cidrListStr := fl.Field().String()
2020-11-08 04:26:18 -05:00
cidrList := common.ParseStringList(cidrListStr)
2020-11-07 12:36:23 -05:00
for i := range cidrList {
_, _, err := net.ParseCIDR(cidrList[i])
if err != nil {
return false
}
}
return true
}
var ipList validator.Func = func(fl validator.FieldLevel) bool {
ipListStr := fl.Field().String()
2020-11-08 04:26:18 -05:00
ipList := common.ParseStringList(ipListStr)
2020-11-07 12:36:23 -05:00
for i := range ipList {
ip := net.ParseIP(ipList[i])
if ip == nil {
return false
}
}
return true
}
func init() {
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
v.RegisterValidation("cidrlist", cidrList)
v.RegisterValidation("iplist", ipList)
}
}
2020-11-07 04:31:48 -05:00
//
// USER ----------------------------------------------------------------------------------------
//
2020-11-05 13:37:51 -05:00
type User struct {
2020-11-07 04:31:48 -05:00
Peer *wgtypes.Peer `gorm:"-"`
LdapUser *ldap.UserCacheHolderEntry `gorm:"-"` // optional, it is still possible to have users without ldap
Config string `gorm:"-"`
2020-11-05 13:37:51 -05:00
2020-11-07 12:36:23 -05:00
UID string `form:"uid" binding:"alphanum"` // uid for html identification
2020-11-05 13:37:51 -05:00
IsOnline bool `gorm:"-"`
2020-11-07 12:36:23 -05:00
Identifier string `form:"identifier" binding:"required,lt=64"` // Identifier AND Email make a WireGuard peer unique
Email string `gorm:"index" form:"mail" binding:"required,email"`
2020-11-05 13:37:51 -05:00
2020-11-07 12:36:23 -05:00
IgnorePersistentKeepalive bool `form:"ignorekeepalive"`
PresharedKey string `form:"presharedkey" binding:"omitempty,base64"`
AllowedIPsStr string `form:"allowedip" binding:"cidrlist"`
IPsStr string `form:"ip" binding:"cidrlist"`
2020-11-05 13:37:51 -05:00
AllowedIPs []string `gorm:"-"` // IPs that are used in the client config file
IPs []string `gorm:"-"` // The IPs of the client
2020-11-07 12:36:23 -05:00
PrivateKey string `form:"privkey" binding:"omitempty,base64"`
PublicKey string `gorm:"primaryKey" form:"pubkey" binding:"required,base64"`
2020-11-05 13:37:51 -05:00
DeactivatedAt *time.Time
CreatedBy string
UpdatedBy string
CreatedAt time.Time
UpdatedAt time.Time
}
2020-11-07 04:31:48 -05:00
func (u User) GetClientConfigFile(device Device) ([]byte, error) {
tpl, err := template.New("client").Funcs(template.FuncMap{"StringsJoin": strings.Join}).Parse(wireguard.ClientCfgTpl)
if err != nil {
return nil, err
}
var tplBuff bytes.Buffer
err = tpl.Execute(&tplBuff, struct {
Client User
Server Device
}{
Client: u,
Server: device,
})
if err != nil {
return nil, err
}
return tplBuff.Bytes(), nil
}
2020-11-06 06:21:47 -05:00
func (u User) GetPeerConfig() wgtypes.PeerConfig {
2020-11-05 13:37:51 -05:00
publicKey, _ := wgtypes.ParseKey(u.PublicKey)
var presharedKey *wgtypes.Key
if u.PresharedKey != "" {
presharedKeyTmp, _ := wgtypes.ParseKey(u.PresharedKey)
presharedKey = &presharedKeyTmp
}
cfg := wgtypes.PeerConfig{
PublicKey: publicKey,
Remove: false,
UpdateOnly: false,
PresharedKey: presharedKey,
Endpoint: nil,
PersistentKeepaliveInterval: nil,
ReplaceAllowedIPs: true,
AllowedIPs: make([]net.IPNet, len(u.IPs)),
}
for i, ip := range u.IPs {
_, ipNet, err := net.ParseCIDR(ip)
if err == nil {
cfg.AllowedIPs[i] = *ipNet
}
}
return cfg
}
2020-11-06 06:21:47 -05:00
func (u User) GetQRCode() ([]byte, error) {
png, err := qrcode.Encode(u.Config, qrcode.Medium, 250)
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("failed to create qrcode")
return nil, err
}
return png, nil
}
2020-11-07 04:31:48 -05:00
func (u User) IsValid() bool {
if u.PublicKey == "" {
return false
}
return true
}
//
// DEVICE --------------------------------------------------------------------------------------
//
2020-11-05 13:37:51 -05:00
type Device struct {
2020-11-06 06:21:47 -05:00
Interface *wgtypes.Device `gorm:"-"`
2020-11-07 12:36:23 -05:00
DeviceName string `form:"device" gorm:"primaryKey" binding:"required,alphanum"`
PrivateKey string `form:"privkey" binding:"base64"`
PublicKey string `form:"pubkey" binding:"required,base64"`
PersistentKeepalive int `form:"keepalive" binding:"gte=0"`
ListenPort int `form:"port" binding:"required,gt=0"`
Mtu int `form:"mtu" binding:"gte=0,lte=1500"`
Endpoint string `form:"endpoint" binding:"required,hostname_port"`
AllowedIPsStr string `form:"allowedip" binding:"cidrlist"`
IPsStr string `form:"ip" binding:"required,cidrlist"`
2020-11-05 13:37:51 -05:00
AllowedIPs []string `gorm:"-"` // IPs that are used in the client config file
IPs []string `gorm:"-"` // The IPs of the client
2020-11-07 12:36:23 -05:00
DNSStr string `form:"dns" binding:"iplist"`
2020-11-05 13:37:51 -05:00
DNS []string `gorm:"-"` // The DNS servers of the client
2020-11-07 12:36:23 -05:00
PreUp string `form:"preup"`
PostUp string `form:"postup"`
PreDown string `form:"predown"`
PostDown string `form:"postdown"`
2020-11-05 13:37:51 -05:00
CreatedAt time.Time
UpdatedAt time.Time
}
2020-11-06 06:21:47 -05:00
func (d Device) IsValid() bool {
2020-11-07 04:31:48 -05:00
if d.PublicKey == "" {
return false
}
2020-11-05 13:37:51 -05:00
if len(d.IPs) == 0 {
return false
}
if d.Endpoint == "" {
return false
}
return true
}
2020-11-07 04:31:48 -05:00
func (d Device) GetDeviceConfig() wgtypes.Config {
var privateKey *wgtypes.Key
if d.PrivateKey != "" {
pKey, _ := wgtypes.ParseKey(d.PrivateKey)
privateKey = &pKey
}
cfg := wgtypes.Config{
PrivateKey: privateKey,
ListenPort: &d.ListenPort,
}
return cfg
}
//
// USER-MANAGER --------------------------------------------------------------------------------
//
2020-11-05 13:37:51 -05:00
type UserManager struct {
2020-11-07 04:31:48 -05:00
db *gorm.DB
wg *wireguard.Manager
ldapUsers *ldap.SynchronizedUserCacheHolder
2020-11-05 13:37:51 -05:00
}
2020-11-07 04:31:48 -05:00
func NewUserManager(wg *wireguard.Manager, ldapUsers *ldap.SynchronizedUserCacheHolder) *UserManager {
um := &UserManager{wg: wg, ldapUsers: ldapUsers}
2020-11-05 13:37:51 -05:00
var err error
um.db, err = gorm.Open(sqlite.Open("wg_portal.db"), &gorm.Config{})
if err != nil {
log.Errorf("failed to open sqlite database: %v", err)
return nil
}
err = um.db.AutoMigrate(&User{}, &Device{})
if err != nil {
log.Errorf("failed to migrate sqlite database: %v", err)
return nil
}
return um
}
2020-11-07 04:31:48 -05:00
func (u *UserManager) InitFromCurrentInterface() error {
peers, err := u.wg.GetPeerList()
2020-11-05 13:37:51 -05:00
if err != nil {
log.Errorf("failed to init user-manager from peers: %v", err)
2020-11-07 04:31:48 -05:00
return err
2020-11-05 13:37:51 -05:00
}
2020-11-07 04:31:48 -05:00
device, err := u.wg.GetDeviceInfo()
2020-11-05 13:37:51 -05:00
if err != nil {
log.Errorf("failed to init user-manager from device: %v", err)
2020-11-07 04:31:48 -05:00
return err
2020-11-05 13:37:51 -05:00
}
2020-11-07 04:31:48 -05:00
// Check if entries already exist in database, if not create them
for _, peer := range peers {
if err := u.validateOrCreateUserForPeer(peer); err != nil {
return err
}
2020-11-05 13:37:51 -05:00
}
2020-11-07 04:31:48 -05:00
if err := u.validateOrCreateDevice(*device); err != nil {
return err
2020-11-05 13:37:51 -05:00
}
2020-11-07 04:31:48 -05:00
return nil
2020-11-05 13:37:51 -05:00
}
2020-11-07 04:31:48 -05:00
func (u *UserManager) validateOrCreateUserForPeer(peer wgtypes.Peer) error {
2020-11-05 13:37:51 -05:00
user := User{}
u.db.Where("public_key = ?", peer.PublicKey.String()).FirstOrInit(&user)
if user.PublicKey == "" { // user not found, create
user.UID = fmt.Sprintf("u%x", md5.Sum([]byte(peer.PublicKey.String())))
user.PublicKey = peer.PublicKey.String()
user.PrivateKey = "" // UNKNOWN
if peer.PresharedKey != (wgtypes.Key{}) {
user.PresharedKey = peer.PresharedKey.String()
}
user.Email = "autodetected@example.com"
user.Identifier = "Autodetected (" + user.PublicKey[0:8] + ")"
user.UpdatedAt = time.Now()
user.CreatedAt = time.Now()
user.AllowedIPs = make([]string, 0) // UNKNOWN
user.IPs = make([]string, len(peer.AllowedIPs))
for i, ip := range peer.AllowedIPs {
user.IPs[i] = ip.String()
}
user.AllowedIPsStr = strings.Join(user.AllowedIPs, ", ")
user.IPsStr = strings.Join(user.IPs, ", ")
res := u.db.Create(&user)
if res.Error != nil {
log.Errorf("failed to create autodetected peer: %v", res.Error)
2020-11-07 04:31:48 -05:00
return res.Error
2020-11-05 13:37:51 -05:00
}
}
2020-11-07 04:31:48 -05:00
return nil
}
func (u *UserManager) validateOrCreateDevice(dev wgtypes.Device) error {
device := Device{}
u.db.Where("device_name = ?", dev.Name).FirstOrInit(&device)
if device.PublicKey == "" { // device not found, create
device.PublicKey = dev.PublicKey.String()
device.PrivateKey = dev.PrivateKey.String()
device.DeviceName = dev.Name
device.ListenPort = dev.ListenPort
device.Mtu = 0
device.PersistentKeepalive = 16 // Default
res := u.db.Create(&device)
if res.Error != nil {
log.Errorf("failed to create autodetected device: %v", res.Error)
return res.Error
}
}
return nil
}
func (u *UserManager) populateUserData(user *User) {
2020-11-05 13:37:51 -05:00
user.AllowedIPs = strings.Split(user.AllowedIPsStr, ", ")
2020-11-07 04:31:48 -05:00
user.IPs = strings.Split(user.IPsStr, ", ")
// Set config file
tmpCfg, _ := user.GetClientConfigFile(u.GetDevice())
2020-11-06 06:21:47 -05:00
user.Config = string(tmpCfg)
2020-11-07 04:31:48 -05:00
// set data from WireGuard interface
user.Peer, _ = u.wg.GetPeer(user.PublicKey)
user.IsOnline = false // todo: calculate online status
// set ldap data
user.LdapUser = u.ldapUsers.GetUserData(u.ldapUsers.GetUserDNByMail(user.Email))
}
func (u *UserManager) populateDeviceData(device *Device) {
device.AllowedIPs = strings.Split(device.AllowedIPsStr, ", ")
device.IPs = strings.Split(device.IPsStr, ", ")
device.DNS = strings.Split(device.DNSStr, ", ")
// set data from WireGuard interface
device.Interface, _ = u.wg.GetDeviceInfo()
}
func (u *UserManager) GetAllUsers() []User {
users := make([]User, 0)
u.db.Find(&users)
for i := range users {
u.populateUserData(&users[i])
}
return users
}
func (u *UserManager) GetDevice() Device {
devices := make([]Device, 0, 1)
u.db.Find(&devices)
for i := range devices {
u.populateDeviceData(&devices[i])
}
return devices[0] // use first device for now... more to come?
2020-11-06 06:21:47 -05:00
}
2020-11-07 04:31:48 -05:00
func (u *UserManager) GetUserByKey(publicKey string) User {
2020-11-06 06:21:47 -05:00
user := User{}
u.db.Where("public_key = ?", publicKey).FirstOrInit(&user)
2020-11-07 04:31:48 -05:00
u.populateUserData(&user)
return user
}
2020-11-06 06:21:47 -05:00
2020-11-07 04:31:48 -05:00
func (u *UserManager) GetUserByMail(mail string) User {
user := User{}
u.db.Where("email = ?", mail).FirstOrInit(&user)
u.populateUserData(&user)
2020-11-05 13:37:51 -05:00
return user
}
func (u *UserManager) CreateUser(user User) error {
user.UID = fmt.Sprintf("u%x", md5.Sum([]byte(user.PublicKey)))
user.UpdatedAt = time.Now()
user.CreatedAt = time.Now()
user.AllowedIPsStr = strings.Join(user.AllowedIPs, ", ")
user.IPsStr = strings.Join(user.IPs, ", ")
res := u.db.Create(&user)
if res.Error != nil {
log.Errorf("failed to create user: %v", res.Error)
return res.Error
}
return nil
}
func (u *UserManager) UpdateUser(user User) error {
user.UpdatedAt = time.Now()
user.AllowedIPsStr = strings.Join(user.AllowedIPs, ", ")
user.IPsStr = strings.Join(user.IPs, ", ")
res := u.db.Save(&user)
if res.Error != nil {
log.Errorf("failed to update user: %v", res.Error)
return res.Error
}
return nil
}
2020-11-07 04:31:48 -05:00
func (u *UserManager) UpdateDevice(device Device) error {
device.UpdatedAt = time.Now()
device.AllowedIPsStr = strings.Join(device.AllowedIPs, ", ")
device.IPsStr = strings.Join(device.IPs, ", ")
device.DNSStr = strings.Join(device.DNS, ", ")
res := u.db.Save(&device)
if res.Error != nil {
log.Errorf("failed to update device: %v", res.Error)
return res.Error
}
return nil
}
2020-11-05 13:37:51 -05:00
func (u *UserManager) GetAllReservedIps() ([]string, error) {
reservedIps := make([]string, 0)
users := u.GetAllUsers()
for _, user := range users {
for _, cidr := range user.IPs {
ip, _, err := net.ParseCIDR(cidr)
if err != nil {
log.WithFields(log.Fields{
"err": err,
"cidr": cidr,
}).Error("failed to ip from cidr")
} else {
reservedIps = append(reservedIps, ip.String())
}
}
}
2020-11-06 06:21:47 -05:00
device := u.GetDevice()
for _, cidr := range device.IPs {
ip, _, err := net.ParseCIDR(cidr)
if err != nil {
log.WithFields(log.Fields{
"err": err,
"cidr": cidr,
}).Error("failed to ip from cidr")
} else {
reservedIps = append(reservedIps, ip.String())
2020-11-05 13:37:51 -05:00
}
}
return reservedIps, nil
}
// GetAvailableIp search for an available ip in cidr against a list of reserved ips
func (u *UserManager) GetAvailableIp(cidr string, reserved []string) (string, error) {
ip, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
return "", err
}
// this two addresses are not usable
broadcastAddr := common.BroadcastAddr(ipnet).String()
networkAddr := ipnet.IP.String()
for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); common.IncreaseIP(ip) {
ok := true
address := ip.String()
for _, r := range reserved {
if address == r {
ok = false
break
}
}
if ok && address != networkAddr && address != broadcastAddr {
return address, nil
}
}
return "", errors.New("no more available address from cidr")
}