Parcourir la source

fix(user): handle static addresses properly

Mustafa Arici il y a 8 ans
Parent
commit
9eef0563b0
11 fichiers modifiés avec 200 ajouts et 84 suppressions
  1. 4 1
      api/rpc.go
  2. 0 0
      bindata/bindata.go
  3. 60 17
      cmd/ovpm/user.go
  4. 2 6
      cmd/ovpm/vpn.go
  5. 3 0
      const.go
  6. 9 2
      net.go
  7. 80 41
      pb/user.pb.go
  8. 6 0
      pb/user.proto
  9. 12 0
      pb/user.swagger.json
  10. 18 16
      user.go
  11. 6 1
      vpn.go

+ 4 - 1
api/rpc.go

@@ -76,7 +76,10 @@ func (s *UserService) Update(ctx context.Context, req *pb.UserUpdateRequest) (*p
 
 	}
 
-	user.Update(req.Password, noGW, req.HostID)
+	err = user.Update(req.Password, noGW, req.HostID)
+	if err != nil {
+		return nil, err
+	}
 	pbUser := pb.UserResponse_User{
 		Username:           user.GetUsername(),
 		ServerSerialNumber: user.GetServerSerialNumber(),

Fichier diff supprimé car celui-ci est trop grand
+ 0 - 0
bindata/bindata.go


+ 60 - 17
cmd/ovpm/user.go

@@ -7,6 +7,7 @@ import (
 	"os"
 
 	"github.com/Sirupsen/logrus"
+	"github.com/asaskevich/govalidator"
 	"github.com/cad/ovpm"
 	"github.com/cad/ovpm/pb"
 	"github.com/olekukonko/tablewriter"
@@ -87,12 +88,17 @@ var userCreateCommand = cli.Command{
 			fmt.Println(cli.ShowSubcommandHelp(c))
 			os.Exit(1)
 		}
-
+		if static != "" && !govalidator.IsIPv4(static) {
+			fmt.Println("--static flag takes a valid ipv4 address")
+			fmt.Println()
+			fmt.Println(cli.ShowSubcommandHelp(c))
+			os.Exit(1)
+		}
 		var hostid uint32
 		if static != "" {
 			h := ovpm.IP2HostID(net.ParseIP(static).To4())
 			if h == 0 {
-				fmt.Println("--static flag takes a valid ipv4 address")
+				fmt.Printf("can not parse %s as IPv4", static)
 				fmt.Println()
 				fmt.Println(cli.ShowSubcommandHelp(c))
 				os.Exit(1)
@@ -142,6 +148,10 @@ var userUpdateCommand = cli.Command{
 			Name:  "static",
 			Usage: "ip address for the vpn user",
 		},
+		cli.BoolFlag{
+			Name:  "no-static",
+			Usage: "do not set static ip address for the vpn user",
+		},
 	},
 	Action: func(c *cli.Context) error {
 		action = "user:update"
@@ -150,32 +160,64 @@ var userUpdateCommand = cli.Command{
 		nogw := c.Bool("no-gw")
 		gw := c.Bool("gw")
 		static := c.String("static")
+		noStatic := c.Bool("no-static")
 
 		if username == "" {
 			fmt.Println(cli.ShowSubcommandHelp(c))
 			os.Exit(1)
 		}
 
-		if !(password != "" || gw || nogw) {
+		// Check wether if all flags are are empty.
+		if !(password != "" || gw || nogw || static != "" || noStatic) {
 			fmt.Println("nothing is updated!")
 			fmt.Println()
 			fmt.Println(cli.ShowSubcommandHelp(c))
 			os.Exit(1)
 		}
 
+		// Given that static is set, check wether it's IPv4.
+		if static != "" && !govalidator.IsIPv4(static) {
+			fmt.Println("--static flag takes a valid ipv4 address")
+			fmt.Println()
+			fmt.Println(cli.ShowSubcommandHelp(c))
+			os.Exit(1)
+		}
+		var staticPref pb.UserUpdateRequest_StaticPref
+		staticPref = pb.UserUpdateRequest_NOPREFSTATIC
 		var hostid uint32
-		if static != "" {
-			h := ovpm.IP2HostID(net.ParseIP(static).To4())
-			if h == 0 {
-				fmt.Println("--static flag takes a valid ipv4 address")
-				fmt.Println()
-				fmt.Println(cli.ShowSubcommandHelp(c))
-				os.Exit(1)
-			}
 
-			hostid = h
+		switch {
+		case static != "" && !noStatic:
+			// means static is set.
+			if static != "" {
+				h := ovpm.IP2HostID(net.ParseIP(static).To4())
+				if h == 0 {
+					fmt.Printf("can't parse %s as IPv4", static)
+					fmt.Println()
+					fmt.Println(cli.ShowSubcommandHelp(c))
+					os.Exit(1)
+				}
+
+				hostid = h
+			}
+			staticPref = pb.UserUpdateRequest_STATIC
+
+		case static == "" && noStatic:
+			// means no-static
+			hostid = 0
+			staticPref = pb.UserUpdateRequest_NOSTATIC
+		case static != "" && noStatic:
+			// means invalid
+			fmt.Println("--static flag and --no-static flag cannot be used together")
+			fmt.Println()
+			fmt.Println(cli.ShowSubcommandHelp(c))
+			os.Exit(1)
+		case static == "" && !noStatic:
+		default:
+			// means no pref
+			staticPref = pb.UserUpdateRequest_NOPREFSTATIC
+			hostid = 0
 		}
-
 		var gwPref pb.UserUpdateRequest_GWPref
 
 		switch {
@@ -200,10 +242,11 @@ var userUpdateCommand = cli.Command{
 		userSvc := pb.NewUserServiceClient(conn)
 
 		response, err := userSvc.Update(context.Background(), &pb.UserUpdateRequest{
-			Username: username,
-			Password: password,
-			Gwpref:   gwPref,
-			HostID:   hostid,
+			Username:   username,
+			Password:   password,
+			Gwpref:     gwPref,
+			HostID:     hostid,
+			Staticpref: staticPref,
 		})
 
 		if err != nil {

+ 2 - 6
cmd/ovpm/vpn.go

@@ -78,13 +78,9 @@ var vpnInitCommand = cli.Command{
 
 		tcp := c.Bool("tcp")
 
-		var proto pb.VPNProto
-
-		switch tcp {
-		case true:
+		proto := pb.VPNProto_UDP
+		if tcp {
 			proto = pb.VPNProto_TCP
-		default:
-			proto = pb.VPNProto_UDP
 		}
 
 		conn := getConn(c.GlobalString("daemon-port"))

+ 3 - 0
const.go

@@ -7,6 +7,9 @@ const (
 	// DefaultVPNPort is the default OpenVPN port to listen.
 	DefaultVPNPort = "1197"
 
+	// DefaultVPNProto is the default OpenVPN protocol to use.
+	DefaultVPNProto = UDPProto
+
 	etcBasePath = "/etc/ovpm/"
 	varBasePath = "/var/db/ovpm/"
 

+ 9 - 2
net.go

@@ -133,7 +133,7 @@ func CreateNewNetwork(name, cidr string, nettype NetworkType, via string) (*DBNe
 		return nil, fmt.Errorf("validation error: `%s` must be a network in the CIDR form", cidr)
 	}
 
-	if !govalidator.IsCIDR(via) && via != "" {
+	if via != "" && !govalidator.IsCIDR(via) {
 		return nil, fmt.Errorf("validation error: `%s` must be a network in the CIDR form", via)
 	}
 
@@ -507,7 +507,7 @@ func HostID2IP(hostid uint32) net.IP {
 	return net.IP(ip)
 }
 
-//IP2HostID converts an IP address to a host id (32-bit unsigned integer).
+// IP2HostID converts an IP address to a host id (32-bit unsigned integer).
 func IP2HostID(ip net.IP) uint32 {
 	hostid := binary.BigEndian.Uint32(ip)
 	return hostid
@@ -515,6 +515,13 @@ func IP2HostID(ip net.IP) uint32 {
 
 // IncrementIP will return next ip address within the network.
 func IncrementIP(ip, mask string) (string, error) {
+	if !govalidator.IsIPv4(ip) {
+		return "", fmt.Errorf("'ip' is expected to be a valid IPv4 %s", ip)
+	}
+	if !govalidator.IsIPv4(ip) {
+		return "", fmt.Errorf("'mask' is expected to be a valid IPv4 %s", mask)
+	}
+
 	ipAddr := net.ParseIP(ip).To4()
 	netMask := net.IPMask(net.ParseIP(mask).To4())
 	ipNet := net.IPNet{IP: ipAddr, Mask: netMask}

+ 80 - 41
pb/user.pb.go

@@ -86,6 +86,32 @@ func (x UserUpdateRequest_GWPref) String() string {
 }
 func (UserUpdateRequest_GWPref) EnumDescriptor() ([]byte, []int) { return fileDescriptor0, []int{2, 0} }
 
+type UserUpdateRequest_StaticPref int32
+
+const (
+	UserUpdateRequest_NOPREFSTATIC UserUpdateRequest_StaticPref = 0
+	UserUpdateRequest_NOSTATIC     UserUpdateRequest_StaticPref = 1
+	UserUpdateRequest_STATIC       UserUpdateRequest_StaticPref = 2
+)
+
+var UserUpdateRequest_StaticPref_name = map[int32]string{
+	0: "NOPREFSTATIC",
+	1: "NOSTATIC",
+	2: "STATIC",
+}
+var UserUpdateRequest_StaticPref_value = map[string]int32{
+	"NOPREFSTATIC": 0,
+	"NOSTATIC":     1,
+	"STATIC":       2,
+}
+
+func (x UserUpdateRequest_StaticPref) String() string {
+	return proto.EnumName(UserUpdateRequest_StaticPref_name, int32(x))
+}
+func (UserUpdateRequest_StaticPref) EnumDescriptor() ([]byte, []int) {
+	return fileDescriptor0, []int{2, 1}
+}
+
 type UserListRequest struct {
 }
 
@@ -135,10 +161,11 @@ func (m *UserCreateRequest) GetHostID() uint32 {
 }
 
 type UserUpdateRequest struct {
-	Username string                   `protobuf:"bytes,1,opt,name=Username" json:"Username,omitempty"`
-	Password string                   `protobuf:"bytes,2,opt,name=Password" json:"Password,omitempty"`
-	Gwpref   UserUpdateRequest_GWPref `protobuf:"varint,3,opt,name=gwpref,enum=pb.UserUpdateRequest_GWPref" json:"gwpref,omitempty"`
-	HostID   uint32                   `protobuf:"varint,4,opt,name=HostID" json:"HostID,omitempty"`
+	Username   string                       `protobuf:"bytes,1,opt,name=Username" json:"Username,omitempty"`
+	Password   string                       `protobuf:"bytes,2,opt,name=Password" json:"Password,omitempty"`
+	Gwpref     UserUpdateRequest_GWPref     `protobuf:"varint,3,opt,name=gwpref,enum=pb.UserUpdateRequest_GWPref" json:"gwpref,omitempty"`
+	HostID     uint32                       `protobuf:"varint,4,opt,name=HostID" json:"HostID,omitempty"`
+	Staticpref UserUpdateRequest_StaticPref `protobuf:"varint,5,opt,name=staticpref,enum=pb.UserUpdateRequest_StaticPref" json:"staticpref,omitempty"`
 }
 
 func (m *UserUpdateRequest) Reset()                    { *m = UserUpdateRequest{} }
@@ -174,6 +201,13 @@ func (m *UserUpdateRequest) GetHostID() uint32 {
 	return 0
 }
 
+func (m *UserUpdateRequest) GetStaticpref() UserUpdateRequest_StaticPref {
+	if m != nil {
+		return m.Staticpref
+	}
+	return UserUpdateRequest_NOPREFSTATIC
+}
+
 type UserDeleteRequest struct {
 	Username string `protobuf:"bytes,1,opt,name=Username" json:"Username,omitempty"`
 }
@@ -329,6 +363,7 @@ func init() {
 	proto.RegisterType((*UserResponse_User)(nil), "pb.UserResponse.User")
 	proto.RegisterType((*UserGenConfigResponse)(nil), "pb.UserGenConfigResponse")
 	proto.RegisterEnum("pb.UserUpdateRequest_GWPref", UserUpdateRequest_GWPref_name, UserUpdateRequest_GWPref_value)
+	proto.RegisterEnum("pb.UserUpdateRequest_StaticPref", UserUpdateRequest_StaticPref_name, UserUpdateRequest_StaticPref_value)
 }
 
 // Reference imports to suppress errors if they are not otherwise used.
@@ -571,41 +606,45 @@ var _UserService_serviceDesc = grpc.ServiceDesc{
 func init() { proto.RegisterFile("user.proto", fileDescriptor0) }
 
 var fileDescriptor0 = []byte{
-	// 575 bytes of a gzipped FileDescriptorProto
-	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x94, 0x4d, 0x6e, 0xd3, 0x40,
-	0x14, 0xc7, 0xb1, 0x9b, 0x98, 0xe4, 0xf5, 0x2b, 0x9d, 0xa6, 0xe0, 0x5a, 0x41, 0x8a, 0x66, 0x81,
-	0xa2, 0x22, 0xd9, 0x22, 0xb0, 0x2a, 0x2b, 0x94, 0x82, 0x29, 0x82, 0x34, 0x72, 0x55, 0x65, 0x89,
-	0x9c, 0xe4, 0x25, 0xb2, 0x94, 0x7a, 0xcc, 0xcc, 0xa4, 0xd9, 0x23, 0x6e, 0xc0, 0x89, 0xd8, 0x70,
-	0x01, 0xae, 0xc0, 0x01, 0x38, 0x02, 0x9a, 0x19, 0xc7, 0x21, 0x21, 0xa0, 0x2c, 0xd8, 0xcd, 0xfb,
-	0xfa, 0x8d, 0xdf, 0x7b, 0xff, 0x31, 0xc0, 0x4c, 0x20, 0xf7, 0x33, 0xce, 0x24, 0x23, 0x76, 0x36,
-	0xf0, 0x1a, 0x13, 0xc6, 0x26, 0x53, 0x0c, 0xe2, 0x2c, 0x09, 0xe2, 0x34, 0x65, 0x32, 0x96, 0x09,
-	0x4b, 0x85, 0xc9, 0xa0, 0x47, 0x70, 0x78, 0x23, 0x90, 0xbf, 0x4b, 0x84, 0x8c, 0xf0, 0xe3, 0x0c,
-	0x85, 0xa4, 0x73, 0x38, 0x52, 0xae, 0x0e, 0xc7, 0x58, 0x62, 0xee, 0x24, 0x1e, 0x54, 0x94, 0x33,
-	0x8d, 0x6f, 0xd1, 0xb5, 0x9a, 0x56, 0xab, 0x1a, 0x15, 0xb6, 0x8a, 0xf5, 0x62, 0x21, 0xe6, 0x8c,
-	0x8f, 0x5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, 0x71, 0x55, 0xa0, 0x58, 0x1f, 0xa0,
-	0x5a, 0xac, 0x92, 0xb8, 0x8b, 0xca, 0xf5, 0x87, 0xe1, 0x9d, 0x6e, 0x88, 0xe4, 0xf0, 0x47, 0x1a,
-	0xfe, 0x90, 0x92, 0x02, 0x3e, 0xc1, 0x74, 0xa8, 0x73, 0xce, 0xad, 0xb3, 0x81, 0xa3, 0x7f, 0x91,
-	0xcf, 0x7e, 0x05, 0x00, 0x00, 0xff, 0xff, 0xc8, 0x3d, 0xc7, 0xac, 0x52, 0x05, 0x00, 0x00,
+	// 628 bytes of a gzipped FileDescriptorProto
+	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x94, 0xc1, 0x6e, 0xd3, 0x4c,
+	0x10, 0xc7, 0x6b, 0x37, 0xf1, 0x97, 0x4c, 0xd3, 0xd6, 0xdd, 0xa6, 0x1f, 0x6e, 0x54, 0xa4, 0x68,
+	0x0f, 0x28, 0x2a, 0x52, 0x22, 0x02, 0x07, 0x54, 0x2e, 0x54, 0x29, 0x98, 0x22, 0x70, 0x23, 0xa7,
+	0x55, 0x8e, 0xc8, 0x49, 0xa6, 0x91, 0xa5, 0xd4, 0x36, 0xbb, 0x9b, 0xe6, 0x8e, 0x78, 0x03, 0x9e,
+	0x87, 0x2b, 0x2f, 0xc0, 0x2b, 0xf0, 0x00, 0x3c, 0x02, 0xda, 0x5d, 0xc7, 0x6e, 0x8a, 0x41, 0x39,
+	0x70, 0x9b, 0x99, 0x9d, 0xf9, 0x8d, 0x77, 0xf6, 0x3f, 0x06, 0x98, 0x73, 0x64, 0xed, 0x84, 0xc5,
+	0x22, 0x26, 0x66, 0x32, 0x6a, 0x1c, 0x4d, 0xe3, 0x78, 0x3a, 0xc3, 0x4e, 0x90, 0x84, 0x9d, 0x20,
+	0x8a, 0x62, 0x11, 0x88, 0x30, 0x8e, 0xb8, 0xce, 0xa0, 0x7b, 0xb0, 0x7b, 0x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 0xf5, 0x6d, 0xd8, 0xf7, 0x07, 0x4a, 0x9d, 0x4f, 0xdf, 0x7f, 0x7c, 0x31, 0x09, 0xdd,
+	0xee, 0xdc, 0x3e, 0xe9, 0xc8, 0xb9, 0x76, 0x66, 0x21, 0x17, 0x27, 0xc6, 0x31, 0x79, 0x0f, 0x96,
+	0xbe, 0x24, 0xc9, 0x9e, 0x61, 0x65, 0xed, 0x0b, 0x60, 0x0d, 0x05, 0xab, 0xd3, 0xdd, 0x0c, 0x36,
+	0x56, 0x15, 0x29, 0x4e, 0xaf, 0x42, 0x8e, 0x5b, 0x59, 0x8d, 0xb5, 0x70, 0x73, 0x55, 0x91, 0xe2,
+	0xb4, 0x96, 0x73, 0xdc, 0x8a, 0xb6, 0xd7, 0xc2, 0x4d, 0x54, 0x85, 0xc4, 0xbd, 0x85, 0xb2, 0x52,
+	0x3a, 0xa9, 0xe7, 0x65, 0xb9, 0xf0, 0x0b, 0x60, 0x87, 0x0a, 0xb6, 0x4f, 0x77, 0x32, 0x18, 0x93,
+	0x05, 0x92, 0xf5, 0x01, 0xaa, 0xd9, 0x53, 0x12, 0x67, 0x59, 0x79, 0x7f, 0x31, 0x1a, 0x87, 0x05,
+	0x27, 0x29, 0xfc, 0xa1, 0x82, 0x3f, 0xa0, 0x24, 0x83, 0x4f, 0x31, 0x1a, 0xab, 0x9c, 0x13, 0xe3,
+	0x78, 0x64, 0xa9, 0xdf, 0xf3, 0xd3, 0x5f, 0x01, 0x00, 0x00, 0xff, 0xff, 0x92, 0x5d, 0x0b, 0xf4,
+	0xce, 0x05, 0x00, 0x00,
 }

+ 6 - 0
pb/user.proto

@@ -25,6 +25,12 @@ message UserUpdateRequest {
   }
   GWPref gwpref = 3;
   uint32 HostID = 4;
+  enum StaticPref {
+    NOPREFSTATIC = 0;
+    NOSTATIC = 1;
+    STATIC = 2;
+  }
+  StaticPref staticpref = 5;
 }
 
 

+ 12 - 0
pb/user.swagger.json

@@ -210,6 +210,15 @@
       ],
       "default": "NOPREF"
     },
+    "UserUpdateRequestStaticPref": {
+      "type": "string",
+      "enum": [
+        "NOPREFSTATIC",
+        "NOSTATIC",
+        "STATIC"
+      ],
+      "default": "NOPREFSTATIC"
+    },
     "pbUserCreateRequest": {
       "type": "object",
       "properties": {
@@ -290,6 +299,9 @@
         "HostID": {
           "type": "integer",
           "format": "int64"
+        },
+        "staticpref": {
+          "$ref": "#/definitions/UserUpdateRequestStaticPref"
         }
       }
     }

+ 18 - 16
user.go

@@ -170,7 +170,6 @@ func (u *DBUser) Update(password string, nogw bool, hostid uint32) error {
 
 	u.NoGW = nogw
 	u.HostID = hostid
-	db.Save(u)
 
 	if hostid != 0 {
 		server, err := GetServerInstance()
@@ -192,6 +191,7 @@ func (u *DBUser) Update(password string, nogw bool, hostid uint32) error {
 			return fmt.Errorf("ip %s is already allocated", ip)
 		}
 	}
+	db.Save(u)
 
 	err := Emit()
 	if err != nil {
@@ -304,27 +304,29 @@ func (u *DBUser) getIP() net.IP {
 	mask := net.IPMask(net.ParseIP(_DefaultServerNetMask).To4())
 	network := net.ParseIP(_DefaultServerNetwork).To4().Mask(mask)
 
-	// Host is static?
+	// If the user has static ip address, return it immediately.
 	if u.HostID != 0 {
-		// Host is really static?
-		if hostIDsContains(staticHostIDs, u.HostID) {
-			return HostID2IP(u.HostID)
-		}
-		return nil
+		return HostID2IP(u.HostID)
 	}
 
-	// Host is dynamic.
-	for i, user := range users {
-		hostID := IP2HostID(network) + uint32(i+2)
-		if hostIDsContains(staticHostIDs, hostID) {
-			for hostIDsContains(staticHostIDs, hostID) {
-				i++
-				hostID = IP2HostID(network) + uint32(i+1)
-			}
+	// Calculate dynamic ip addresses from a deterministic address pool.
+	freeHostID := 0
+	for _, user := range users {
+		// Skip, if user is supposed to have static ip.
+		if user.HostID != 0 {
+			continue
+		}
+
+		// Try the next available host id.
+		hostID := IP2HostID(network) + uint32(freeHostID)
+		for hostIDsContains(staticHostIDs, hostID+2) {
+			freeHostID++ // Increase the host id and try again until it is available.
+			hostID = IP2HostID(network) + uint32(freeHostID)
 		}
 		if user.ID == u.ID {
-			return HostID2IP(hostID)
+			return HostID2IP(hostID + 2)
 		}
+		freeHostID++
 	}
 	return nil
 }

+ 6 - 1
vpn.go

@@ -384,6 +384,11 @@ func emitServerConf() error {
 		port = serverInstance.Port
 	}
 
+	proto := DefaultVPNProto
+	if serverInstance.Proto != "" {
+		proto = serverInstance.Proto
+	}
+
 	var result bytes.Buffer
 
 	server := _VPNServerConfig{
@@ -397,7 +402,7 @@ func emitServerConf() error {
 		Net:          _DefaultServerNetwork,
 		Mask:         _DefaultServerNetMask,
 		Port:         port,
-		Proto:        serverInstance.Proto,
+		Proto:        proto,
 	}
 	data, err := bindata.Asset("template/server.conf.tmpl")
 	if err != nil {

Certains fichiers n'ont pas été affichés car il y a eu trop de fichiers modifiés dans ce diff