瀏覽代碼

allow a client if its IP is both allowed and denied

this allows you to define a group deny policy that can be overridden
on a per-user basis.

This is a backward incompatible change

Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
Nicola Murino 2 年之前
父節點
當前提交
e54237ff70
共有 2 個文件被更改,包括 8 次插入7 次删除
  1. 5 5
      internal/dataprovider/user.go
  2. 3 2
      internal/sftpd/sftpd_test.go

+ 5 - 5
internal/dataprovider/user.go

@@ -1170,7 +1170,7 @@ func (u *User) GetBandwidthForIP(clientIP, connectionID string) (int64, int64) {
 // IsLoginFromAddrAllowed returns true if the login is allowed from the specified remoteAddr.
 // If AllowedIP is defined only the specified IP/Mask can login.
 // If DeniedIP is defined the specified IP/Mask cannot login.
-// If an IP is both allowed and denied then login will be denied
+// If an IP is both allowed and denied then login will be allowed
 func (u *User) IsLoginFromAddrAllowed(remoteAddr string) bool {
 	if len(u.Filters.AllowedIP) == 0 && len(u.Filters.DeniedIP) == 0 {
 		return true
@@ -1181,22 +1181,22 @@ func (u *User) IsLoginFromAddrAllowed(remoteAddr string) bool {
 		logger.Warn(logSender, "", "login allowed for invalid IP. remote address: %#v", remoteAddr)
 		return true
 	}
-	for _, IPMask := range u.Filters.DeniedIP {
+	for _, IPMask := range u.Filters.AllowedIP {
 		_, IPNet, err := net.ParseCIDR(IPMask)
 		if err != nil {
 			return false
 		}
 		if IPNet.Contains(remoteIP) {
-			return false
+			return true
 		}
 	}
-	for _, IPMask := range u.Filters.AllowedIP {
+	for _, IPMask := range u.Filters.DeniedIP {
 		_, IPNet, err := net.ParseCIDR(IPMask)
 		if err != nil {
 			return false
 		}
 		if IPNet.Contains(remoteIP) {
-			return true
+			return false
 		}
 	}
 	return len(u.Filters.AllowedIP) == 0

+ 3 - 2
internal/sftpd/sftpd_test.go

@@ -8370,8 +8370,9 @@ func TestUserFiltersIPMaskConditions(t *testing.T) {
 	assert.True(t, user.IsLoginFromAddrAllowed("192.168.2.6"))
 
 	user.Filters.AllowedIP = append(user.Filters.AllowedIP, "192.168.1.5/32")
-	// if the same ip/mask is both denied and allowed then login must be denied
-	assert.False(t, user.IsLoginFromAddrAllowed("192.168.1.5"))
+	// if the same ip/mask is both denied and allowed then login must be allowed
+	assert.True(t, user.IsLoginFromAddrAllowed("192.168.1.5"))
+	assert.False(t, user.IsLoginFromAddrAllowed("192.168.1.3"))
 	assert.False(t, user.IsLoginFromAddrAllowed("192.168.3.6"))
 
 	user.Filters.DeniedIP = []string{}