acls.go 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  1. package logic
  2. import (
  3. "context"
  4. "errors"
  5. "maps"
  6. "net"
  7. "github.com/gravitl/netmaker/db"
  8. "github.com/gravitl/netmaker/logic"
  9. "github.com/gravitl/netmaker/models"
  10. "github.com/gravitl/netmaker/schema"
  11. )
  12. func getStaticUserNodesByNetwork(network models.NetworkID) (staticNode []models.Node) {
  13. extClients, err := logic.GetAllExtClients()
  14. if err != nil {
  15. return
  16. }
  17. for _, extI := range extClients {
  18. if extI.Network == network.String() {
  19. if extI.RemoteAccessClientID != "" {
  20. n := extI.ConvertToStaticNode()
  21. staticNode = append(staticNode, n)
  22. }
  23. }
  24. }
  25. return
  26. }
  27. func GetFwRulesForUserNodesOnGw(node models.Node, nodes []models.Node) (rules []models.FwRule) {
  28. defaultUserPolicy, _ := logic.GetDefaultPolicy(models.NetworkID(node.Network), models.UserPolicy)
  29. userNodes := getStaticUserNodesByNetwork(models.NetworkID(node.Network))
  30. for _, userNodeI := range userNodes {
  31. if !userNodeI.StaticNode.Enabled {
  32. continue
  33. }
  34. if defaultUserPolicy.Enabled {
  35. if userNodeI.StaticNode.Address != "" {
  36. rules = append(rules, models.FwRule{
  37. SrcIP: userNodeI.StaticNode.AddressIPNet4(),
  38. DstIP: net.IPNet{},
  39. AllowedProtocol: models.ALL,
  40. AllowedPorts: []string{},
  41. Allow: true,
  42. })
  43. }
  44. if userNodeI.StaticNode.Address6 != "" {
  45. rules = append(rules, models.FwRule{
  46. SrcIP: userNodeI.StaticNode.AddressIPNet6(),
  47. DstIP: net.IPNet{},
  48. AllowedProtocol: models.ALL,
  49. AllowedPorts: []string{},
  50. Allow: true,
  51. })
  52. }
  53. continue
  54. }
  55. for _, peer := range nodes {
  56. if peer.IsUserNode {
  57. continue
  58. }
  59. if ok, allowedPolicies := IsUserAllowedToCommunicate(userNodeI.StaticNode.OwnerID, peer); ok {
  60. if peer.IsStatic {
  61. peer = peer.StaticNode.ConvertToStaticNode()
  62. }
  63. for _, policy := range allowedPolicies {
  64. if userNodeI.StaticNode.Address != "" {
  65. rules = append(rules, models.FwRule{
  66. SrcIP: userNodeI.StaticNode.AddressIPNet4(),
  67. DstIP: net.IPNet{
  68. IP: peer.Address.IP,
  69. Mask: net.CIDRMask(32, 32),
  70. },
  71. AllowedProtocol: policy.Proto,
  72. AllowedPorts: policy.Port,
  73. Allow: true,
  74. })
  75. }
  76. if userNodeI.StaticNode.Address6 != "" {
  77. rules = append(rules, models.FwRule{
  78. SrcIP: userNodeI.StaticNode.AddressIPNet6(),
  79. DstIP: net.IPNet{
  80. IP: peer.Address6.IP,
  81. Mask: net.CIDRMask(128, 128),
  82. },
  83. AllowedProtocol: policy.Proto,
  84. AllowedPorts: policy.Port,
  85. Allow: true,
  86. })
  87. }
  88. // add egress ranges
  89. for _, dstI := range policy.Dst {
  90. if dstI.Value == "*" {
  91. rules = append(rules, models.FwRule{
  92. SrcIP: userNodeI.StaticNode.AddressIPNet4(),
  93. DstIP: net.IPNet{},
  94. AllowedProtocol: policy.Proto,
  95. AllowedPorts: policy.Port,
  96. Allow: true,
  97. })
  98. break
  99. }
  100. if dstI.ID == models.EgressID {
  101. e := schema.Egress{ID: dstI.Value}
  102. err := e.Get(db.WithContext(context.TODO()))
  103. if err != nil {
  104. continue
  105. }
  106. if e.Range != "" {
  107. dstI.Value = e.Range
  108. ip, cidr, err := net.ParseCIDR(dstI.Value)
  109. if err == nil {
  110. if ip.To4() != nil && userNodeI.StaticNode.Address != "" {
  111. rules = append(rules, models.FwRule{
  112. SrcIP: userNodeI.StaticNode.AddressIPNet4(),
  113. DstIP: *cidr,
  114. AllowedProtocol: policy.Proto,
  115. AllowedPorts: policy.Port,
  116. Allow: true,
  117. })
  118. } else if ip.To16() != nil && userNodeI.StaticNode.Address6 != "" {
  119. rules = append(rules, models.FwRule{
  120. SrcIP: userNodeI.StaticNode.AddressIPNet6(),
  121. DstIP: *cidr,
  122. AllowedProtocol: policy.Proto,
  123. AllowedPorts: policy.Port,
  124. Allow: true,
  125. })
  126. }
  127. }
  128. } else if len(e.DomainAns) > 0 {
  129. for _, domainAns := range e.DomainAns {
  130. dstI.Value = domainAns
  131. ip, cidr, err := net.ParseCIDR(dstI.Value)
  132. if err == nil {
  133. if ip.To4() != nil && userNodeI.StaticNode.Address != "" {
  134. rules = append(rules, models.FwRule{
  135. SrcIP: userNodeI.StaticNode.AddressIPNet4(),
  136. DstIP: *cidr,
  137. AllowedProtocol: policy.Proto,
  138. AllowedPorts: policy.Port,
  139. Allow: true,
  140. })
  141. } else if ip.To16() != nil && userNodeI.StaticNode.Address6 != "" {
  142. rules = append(rules, models.FwRule{
  143. SrcIP: userNodeI.StaticNode.AddressIPNet6(),
  144. DstIP: *cidr,
  145. AllowedProtocol: policy.Proto,
  146. AllowedPorts: policy.Port,
  147. Allow: true,
  148. })
  149. }
  150. }
  151. }
  152. }
  153. }
  154. }
  155. }
  156. }
  157. }
  158. }
  159. return
  160. }
  161. func GetFwRulesForNodeAndPeerOnGw(node, peer models.Node, allowedPolicies []models.Acl) (rules []models.FwRule) {
  162. for _, policy := range allowedPolicies {
  163. // if static peer dst rule not for ingress node -> skip
  164. if node.Address.IP != nil {
  165. rules = append(rules, models.FwRule{
  166. SrcIP: net.IPNet{
  167. IP: node.Address.IP,
  168. Mask: net.CIDRMask(32, 32),
  169. },
  170. DstIP: net.IPNet{
  171. IP: peer.Address.IP,
  172. Mask: net.CIDRMask(32, 32),
  173. },
  174. AllowedProtocol: policy.Proto,
  175. AllowedPorts: policy.Port,
  176. Allow: true,
  177. })
  178. }
  179. if node.Address6.IP != nil {
  180. rules = append(rules, models.FwRule{
  181. SrcIP: net.IPNet{
  182. IP: node.Address6.IP,
  183. Mask: net.CIDRMask(128, 128),
  184. },
  185. DstIP: net.IPNet{
  186. IP: peer.Address6.IP,
  187. Mask: net.CIDRMask(128, 128),
  188. },
  189. AllowedProtocol: policy.Proto,
  190. AllowedPorts: policy.Port,
  191. Allow: true,
  192. })
  193. }
  194. if policy.AllowedDirection == models.TrafficDirectionBi {
  195. if node.Address.IP != nil {
  196. rules = append(rules, models.FwRule{
  197. SrcIP: net.IPNet{
  198. IP: peer.Address.IP,
  199. Mask: net.CIDRMask(32, 32),
  200. },
  201. DstIP: net.IPNet{
  202. IP: node.Address.IP,
  203. Mask: net.CIDRMask(32, 32),
  204. },
  205. AllowedProtocol: policy.Proto,
  206. AllowedPorts: policy.Port,
  207. Allow: true,
  208. })
  209. }
  210. if node.Address6.IP != nil {
  211. rules = append(rules, models.FwRule{
  212. SrcIP: net.IPNet{
  213. IP: peer.Address6.IP,
  214. Mask: net.CIDRMask(128, 128),
  215. },
  216. DstIP: net.IPNet{
  217. IP: node.Address6.IP,
  218. Mask: net.CIDRMask(128, 128),
  219. },
  220. AllowedProtocol: policy.Proto,
  221. AllowedPorts: policy.Port,
  222. Allow: true,
  223. })
  224. }
  225. }
  226. if len(node.StaticNode.ExtraAllowedIPs) > 0 {
  227. for _, additionalAllowedIPNet := range node.StaticNode.ExtraAllowedIPs {
  228. _, ipNet, err := net.ParseCIDR(additionalAllowedIPNet)
  229. if err != nil {
  230. continue
  231. }
  232. if ipNet.IP.To4() != nil && peer.Address.IP != nil {
  233. rules = append(rules, models.FwRule{
  234. SrcIP: net.IPNet{
  235. IP: peer.Address.IP,
  236. Mask: net.CIDRMask(32, 32),
  237. },
  238. DstIP: *ipNet,
  239. Allow: true,
  240. })
  241. } else if peer.Address6.IP != nil {
  242. rules = append(rules, models.FwRule{
  243. SrcIP: net.IPNet{
  244. IP: peer.Address6.IP,
  245. Mask: net.CIDRMask(128, 128),
  246. },
  247. DstIP: *ipNet,
  248. Allow: true,
  249. })
  250. }
  251. }
  252. }
  253. if len(peer.StaticNode.ExtraAllowedIPs) > 0 {
  254. for _, additionalAllowedIPNet := range peer.StaticNode.ExtraAllowedIPs {
  255. _, ipNet, err := net.ParseCIDR(additionalAllowedIPNet)
  256. if err != nil {
  257. continue
  258. }
  259. if ipNet.IP.To4() != nil && node.Address.IP != nil {
  260. rules = append(rules, models.FwRule{
  261. SrcIP: net.IPNet{
  262. IP: node.Address.IP,
  263. Mask: net.CIDRMask(32, 32),
  264. },
  265. DstIP: *ipNet,
  266. Allow: true,
  267. })
  268. } else if node.Address6.IP != nil {
  269. rules = append(rules, models.FwRule{
  270. SrcIP: net.IPNet{
  271. IP: node.Address6.IP,
  272. Mask: net.CIDRMask(128, 128),
  273. },
  274. DstIP: *ipNet,
  275. Allow: true,
  276. })
  277. }
  278. }
  279. }
  280. // add egress range rules
  281. for _, dstI := range policy.Dst {
  282. if dstI.ID == models.EgressID {
  283. e := schema.Egress{ID: dstI.Value}
  284. err := e.Get(db.WithContext(context.TODO()))
  285. if err != nil {
  286. continue
  287. }
  288. if e.Range != "" {
  289. dstI.Value = e.Range
  290. ip, cidr, err := net.ParseCIDR(dstI.Value)
  291. if err == nil {
  292. if ip.To4() != nil {
  293. if node.Address.IP != nil {
  294. rules = append(rules, models.FwRule{
  295. SrcIP: net.IPNet{
  296. IP: node.Address.IP,
  297. Mask: net.CIDRMask(32, 32),
  298. },
  299. DstIP: *cidr,
  300. AllowedProtocol: policy.Proto,
  301. AllowedPorts: policy.Port,
  302. Allow: true,
  303. })
  304. }
  305. } else {
  306. if node.Address6.IP != nil {
  307. rules = append(rules, models.FwRule{
  308. SrcIP: net.IPNet{
  309. IP: node.Address6.IP,
  310. Mask: net.CIDRMask(128, 128),
  311. },
  312. DstIP: *cidr,
  313. AllowedProtocol: policy.Proto,
  314. AllowedPorts: policy.Port,
  315. Allow: true,
  316. })
  317. }
  318. }
  319. }
  320. } else if len(e.DomainAns) > 0 {
  321. for _, domainAnsI := range e.DomainAns {
  322. dstI.Value = domainAnsI
  323. ip, cidr, err := net.ParseCIDR(dstI.Value)
  324. if err == nil {
  325. if ip.To4() != nil {
  326. if node.Address.IP != nil {
  327. rules = append(rules, models.FwRule{
  328. SrcIP: net.IPNet{
  329. IP: node.Address.IP,
  330. Mask: net.CIDRMask(32, 32),
  331. },
  332. DstIP: *cidr,
  333. AllowedProtocol: policy.Proto,
  334. AllowedPorts: policy.Port,
  335. Allow: true,
  336. })
  337. }
  338. } else {
  339. if node.Address6.IP != nil {
  340. rules = append(rules, models.FwRule{
  341. SrcIP: net.IPNet{
  342. IP: node.Address6.IP,
  343. Mask: net.CIDRMask(128, 128),
  344. },
  345. DstIP: *cidr,
  346. AllowedProtocol: policy.Proto,
  347. AllowedPorts: policy.Port,
  348. Allow: true,
  349. })
  350. }
  351. }
  352. }
  353. }
  354. }
  355. }
  356. }
  357. }
  358. return
  359. }
  360. func checkIfAclTagisValid(a models.Acl, t models.AclPolicyTag, isSrc bool) (err error) {
  361. switch t.ID {
  362. case models.NodeTagID:
  363. if a.RuleType == models.UserPolicy && isSrc {
  364. return errors.New("user policy source mismatch")
  365. }
  366. // check if tag is valid
  367. _, err := GetTag(models.TagID(t.Value))
  368. if err != nil {
  369. return errors.New("invalid tag " + t.Value)
  370. }
  371. case models.NodeID:
  372. if a.RuleType == models.UserPolicy && isSrc {
  373. return errors.New("user policy source mismatch")
  374. }
  375. _, nodeErr := logic.GetNodeByID(t.Value)
  376. if nodeErr != nil {
  377. _, staticNodeErr := logic.GetExtClient(t.Value, a.NetworkID.String())
  378. if staticNodeErr != nil {
  379. return errors.New("invalid node " + t.Value)
  380. }
  381. }
  382. case models.EgressID, models.EgressRange:
  383. e := schema.Egress{
  384. ID: t.Value,
  385. }
  386. err := e.Get(db.WithContext(context.TODO()))
  387. if err != nil {
  388. return errors.New("invalid egress")
  389. }
  390. case models.UserAclID:
  391. if a.RuleType == models.DevicePolicy {
  392. return errors.New("device policy source mismatch")
  393. }
  394. if !isSrc {
  395. return errors.New("user cannot be added to destination")
  396. }
  397. _, err := logic.GetUser(t.Value)
  398. if err != nil {
  399. return errors.New("invalid user " + t.Value)
  400. }
  401. case models.UserGroupAclID:
  402. if a.RuleType == models.DevicePolicy {
  403. return errors.New("device policy source mismatch")
  404. }
  405. if !isSrc {
  406. return errors.New("user cannot be added to destination")
  407. }
  408. err := IsGroupValid(models.UserGroupID(t.Value))
  409. if err != nil {
  410. return errors.New("invalid user group " + t.Value)
  411. }
  412. // check if group belongs to this network
  413. netGrps := GetUserGroupsInNetwork(a.NetworkID)
  414. if _, ok := netGrps[models.UserGroupID(t.Value)]; !ok {
  415. return errors.New("invalid user group " + t.Value)
  416. }
  417. default:
  418. return errors.New("invalid policy")
  419. }
  420. return nil
  421. }
  422. // IsAclPolicyValid - validates if acl policy is valid
  423. func IsAclPolicyValid(acl models.Acl) (err error) {
  424. //check if src and dst are valid
  425. if acl.AllowedDirection != models.TrafficDirectionBi &&
  426. acl.AllowedDirection != models.TrafficDirectionUni {
  427. return errors.New("invalid traffic direction")
  428. }
  429. switch acl.RuleType {
  430. case models.UserPolicy:
  431. // src list should only contain users
  432. for _, srcI := range acl.Src {
  433. if srcI.Value == "*" {
  434. continue
  435. }
  436. // check if user group is valid
  437. if err = checkIfAclTagisValid(acl, srcI, true); err != nil {
  438. return
  439. }
  440. }
  441. for _, dstI := range acl.Dst {
  442. if dstI.Value == "*" {
  443. continue
  444. }
  445. // check if user group is valid
  446. if err = checkIfAclTagisValid(acl, dstI, false); err != nil {
  447. return
  448. }
  449. }
  450. case models.DevicePolicy:
  451. for _, srcI := range acl.Src {
  452. if srcI.Value == "*" {
  453. continue
  454. }
  455. // check if user group is valid
  456. if err = checkIfAclTagisValid(acl, srcI, true); err != nil {
  457. return err
  458. }
  459. }
  460. for _, dstI := range acl.Dst {
  461. if dstI.Value == "*" {
  462. continue
  463. }
  464. // check if user group is valid
  465. if err = checkIfAclTagisValid(acl, dstI, false); err != nil {
  466. return
  467. }
  468. }
  469. }
  470. return nil
  471. }
  472. // ListUserPolicies - lists all acl policies enforced on an user
  473. func ListUserPolicies(u models.User) []models.Acl {
  474. allAcls := logic.ListAcls()
  475. userAcls := []models.Acl{}
  476. for _, acl := range allAcls {
  477. if acl.RuleType == models.UserPolicy {
  478. srcMap := logic.ConvAclTagToValueMap(acl.Src)
  479. if _, ok := srcMap[u.UserName]; ok {
  480. userAcls = append(userAcls, acl)
  481. } else {
  482. // check for user groups
  483. for gID := range u.UserGroups {
  484. if _, ok := srcMap[gID.String()]; ok {
  485. userAcls = append(userAcls, acl)
  486. break
  487. }
  488. }
  489. }
  490. }
  491. }
  492. return userAcls
  493. }
  494. // listPoliciesOfUser - lists all user acl policies applied to user in an network
  495. func listPoliciesOfUser(user models.User, netID models.NetworkID) []models.Acl {
  496. allAcls := logic.ListAcls()
  497. userAcls := []models.Acl{}
  498. if _, ok := user.UserGroups[globalNetworksAdminGroupID]; ok {
  499. user.UserGroups[GetDefaultNetworkAdminGroupID(netID)] = struct{}{}
  500. }
  501. if _, ok := user.UserGroups[globalNetworksUserGroupID]; ok {
  502. user.UserGroups[GetDefaultNetworkUserGroupID(netID)] = struct{}{}
  503. }
  504. if user.PlatformRoleID == models.AdminRole || user.PlatformRoleID == models.SuperAdminRole {
  505. user.UserGroups[GetDefaultNetworkAdminGroupID(netID)] = struct{}{}
  506. }
  507. for _, acl := range allAcls {
  508. if acl.NetworkID == netID && acl.RuleType == models.UserPolicy {
  509. srcMap := logic.ConvAclTagToValueMap(acl.Src)
  510. if _, ok := srcMap[user.UserName]; ok {
  511. userAcls = append(userAcls, acl)
  512. continue
  513. }
  514. for netRole := range user.NetworkRoles {
  515. if _, ok := srcMap[netRole.String()]; ok {
  516. userAcls = append(userAcls, acl)
  517. continue
  518. }
  519. }
  520. for userG := range user.UserGroups {
  521. if _, ok := srcMap[userG.String()]; ok {
  522. userAcls = append(userAcls, acl)
  523. continue
  524. }
  525. }
  526. }
  527. }
  528. return userAcls
  529. }
  530. // listUserPolicies - lists all user policies in a network
  531. func listUserPolicies(netID models.NetworkID) []models.Acl {
  532. allAcls := logic.ListAcls()
  533. deviceAcls := []models.Acl{}
  534. for _, acl := range allAcls {
  535. if acl.NetworkID == netID && acl.RuleType == models.UserPolicy {
  536. deviceAcls = append(deviceAcls, acl)
  537. }
  538. }
  539. return deviceAcls
  540. }
  541. // IsUserAllowedToCommunicate - check if user is allowed to communicate with peer
  542. func IsUserAllowedToCommunicate(userName string, peer models.Node) (bool, []models.Acl) {
  543. var peerId string
  544. if peer.IsStatic {
  545. peerId = peer.StaticNode.ClientID
  546. peer = peer.StaticNode.ConvertToStaticNode()
  547. } else {
  548. peerId = peer.ID.String()
  549. }
  550. var peerTags map[models.TagID]struct{}
  551. if peer.Mutex != nil {
  552. peer.Mutex.Lock()
  553. peerTags = maps.Clone(peer.Tags)
  554. peer.Mutex.Unlock()
  555. } else {
  556. peerTags = peer.Tags
  557. }
  558. if peerTags == nil {
  559. peerTags = make(map[models.TagID]struct{})
  560. }
  561. peerTags[models.TagID(peerId)] = struct{}{}
  562. peerTags[models.TagID("*")] = struct{}{}
  563. acl, _ := logic.GetDefaultPolicy(models.NetworkID(peer.Network), models.UserPolicy)
  564. if acl.Enabled {
  565. return true, []models.Acl{acl}
  566. }
  567. user, err := logic.GetUser(userName)
  568. if err != nil {
  569. return false, []models.Acl{}
  570. }
  571. allowedPolicies := []models.Acl{}
  572. policies := listPoliciesOfUser(*user, models.NetworkID(peer.Network))
  573. for _, policy := range policies {
  574. if !policy.Enabled {
  575. continue
  576. }
  577. dstMap := logic.ConvAclTagToValueMap(policy.Dst)
  578. for _, dst := range policy.Dst {
  579. if dst.ID == models.EgressID {
  580. e := schema.Egress{ID: dst.Value}
  581. err := e.Get(db.WithContext(context.TODO()))
  582. if err == nil && e.Status {
  583. for nodeID := range e.Nodes {
  584. dstMap[nodeID] = struct{}{}
  585. }
  586. }
  587. }
  588. }
  589. if _, ok := dstMap["*"]; ok {
  590. allowedPolicies = append(allowedPolicies, policy)
  591. continue
  592. }
  593. if _, ok := dstMap[peer.ID.String()]; ok {
  594. allowedPolicies = append(allowedPolicies, policy)
  595. continue
  596. }
  597. for tagID := range peerTags {
  598. if _, ok := dstMap[tagID.String()]; ok {
  599. allowedPolicies = append(allowedPolicies, policy)
  600. break
  601. }
  602. }
  603. }
  604. if len(allowedPolicies) > 0 {
  605. return true, allowedPolicies
  606. }
  607. return false, []models.Acl{}
  608. }
  609. // IsPeerAllowed - checks if peer needs to be added to the interface
  610. func IsPeerAllowed(node, peer models.Node, checkDefaultPolicy bool) bool {
  611. var nodeId, peerId string
  612. // if peer.IsFailOver && node.FailedOverBy != uuid.Nil && node.FailedOverBy == peer.ID {
  613. // return true
  614. // }
  615. // if node.IsFailOver && peer.FailedOverBy != uuid.Nil && peer.FailedOverBy == node.ID {
  616. // return true
  617. // }
  618. // if node.IsGw && peer.IsRelayed && peer.RelayedBy == node.ID.String() {
  619. // return true
  620. // }
  621. // if peer.IsGw && node.IsRelayed && node.RelayedBy == peer.ID.String() {
  622. // return true
  623. // }
  624. if node.IsStatic {
  625. nodeId = node.StaticNode.ClientID
  626. node = node.StaticNode.ConvertToStaticNode()
  627. } else {
  628. nodeId = node.ID.String()
  629. }
  630. if peer.IsStatic {
  631. peerId = peer.StaticNode.ClientID
  632. peer = peer.StaticNode.ConvertToStaticNode()
  633. } else {
  634. peerId = peer.ID.String()
  635. }
  636. var nodeTags, peerTags map[models.TagID]struct{}
  637. if node.Mutex != nil {
  638. node.Mutex.Lock()
  639. nodeTags = maps.Clone(node.Tags)
  640. node.Mutex.Unlock()
  641. } else {
  642. nodeTags = node.Tags
  643. }
  644. if peer.Mutex != nil {
  645. peer.Mutex.Lock()
  646. peerTags = maps.Clone(peer.Tags)
  647. peer.Mutex.Unlock()
  648. } else {
  649. peerTags = peer.Tags
  650. }
  651. if nodeTags == nil {
  652. nodeTags = make(map[models.TagID]struct{})
  653. }
  654. if peerTags == nil {
  655. peerTags = make(map[models.TagID]struct{})
  656. }
  657. nodeTags[models.TagID(nodeId)] = struct{}{}
  658. peerTags[models.TagID(peerId)] = struct{}{}
  659. if checkDefaultPolicy {
  660. // check default policy if all allowed return true
  661. defaultPolicy, err := logic.GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
  662. if err == nil {
  663. if defaultPolicy.Enabled {
  664. return true
  665. }
  666. }
  667. }
  668. // list device policies
  669. policies := logic.ListDevicePolicies(models.NetworkID(peer.Network))
  670. srcMap := make(map[string]struct{})
  671. dstMap := make(map[string]struct{})
  672. defer func() {
  673. srcMap = nil
  674. dstMap = nil
  675. }()
  676. for _, policy := range policies {
  677. if !policy.Enabled {
  678. continue
  679. }
  680. srcMap = logic.ConvAclTagToValueMap(policy.Src)
  681. dstMap = logic.ConvAclTagToValueMap(policy.Dst)
  682. for _, dst := range policy.Dst {
  683. if dst.ID == models.EgressID {
  684. e := schema.Egress{ID: dst.Value}
  685. err := e.Get(db.WithContext(context.TODO()))
  686. if err == nil && e.Status {
  687. for nodeID := range e.Nodes {
  688. dstMap[nodeID] = struct{}{}
  689. }
  690. }
  691. }
  692. }
  693. if logic.CheckTagGroupPolicy(srcMap, dstMap, node, peer, nodeTags, peerTags) {
  694. return true
  695. }
  696. }
  697. return false
  698. }
  699. func RemoveUserFromAclPolicy(userName string) {
  700. acls := logic.ListAcls()
  701. for _, acl := range acls {
  702. delete := false
  703. update := false
  704. if acl.RuleType == models.UserPolicy {
  705. for i := len(acl.Src) - 1; i >= 0; i-- {
  706. if acl.Src[i].ID == models.UserAclID && acl.Src[i].Value == userName {
  707. if len(acl.Src) == 1 {
  708. // delete policy
  709. delete = true
  710. break
  711. } else {
  712. acl.Src = append(acl.Src[:i], acl.Src[i+1:]...)
  713. update = true
  714. }
  715. }
  716. }
  717. if delete {
  718. logic.DeleteAcl(acl)
  719. continue
  720. }
  721. if update {
  722. logic.UpsertAcl(acl)
  723. }
  724. }
  725. }
  726. }
  727. // UpdateDeviceTag - updates device tag on acl policies
  728. func UpdateDeviceTag(OldID, newID models.TagID, netID models.NetworkID) {
  729. acls := logic.ListDevicePolicies(netID)
  730. update := false
  731. for _, acl := range acls {
  732. for i, srcTagI := range acl.Src {
  733. if srcTagI.ID == models.NodeTagID {
  734. if OldID.String() == srcTagI.Value {
  735. acl.Src[i].Value = newID.String()
  736. update = true
  737. }
  738. }
  739. }
  740. for i, dstTagI := range acl.Dst {
  741. if dstTagI.ID == models.NodeTagID {
  742. if OldID.String() == dstTagI.Value {
  743. acl.Dst[i].Value = newID.String()
  744. update = true
  745. }
  746. }
  747. }
  748. if update {
  749. logic.UpsertAcl(acl)
  750. }
  751. }
  752. }
  753. func CheckIfTagAsActivePolicy(tagID models.TagID, netID models.NetworkID) bool {
  754. acls := logic.ListDevicePolicies(netID)
  755. for _, acl := range acls {
  756. for _, srcTagI := range acl.Src {
  757. if srcTagI.ID == models.NodeTagID {
  758. if tagID.String() == srcTagI.Value {
  759. return true
  760. }
  761. }
  762. }
  763. for _, dstTagI := range acl.Dst {
  764. if dstTagI.ID == models.NodeTagID {
  765. if tagID.String() == dstTagI.Value {
  766. return true
  767. }
  768. }
  769. }
  770. }
  771. return false
  772. }
  773. // RemoveDeviceTagFromAclPolicies - remove device tag from acl policies
  774. func RemoveDeviceTagFromAclPolicies(tagID models.TagID, netID models.NetworkID) error {
  775. acls := logic.ListDevicePolicies(netID)
  776. update := false
  777. for _, acl := range acls {
  778. for i := len(acl.Src) - 1; i >= 0; i-- {
  779. if acl.Src[i].ID == models.NodeTagID {
  780. if tagID.String() == acl.Src[i].Value {
  781. acl.Src = append(acl.Src[:i], acl.Src[i+1:]...)
  782. update = true
  783. }
  784. }
  785. }
  786. for i := len(acl.Dst) - 1; i >= 0; i-- {
  787. if acl.Dst[i].ID == models.NodeTagID {
  788. if tagID.String() == acl.Dst[i].Value {
  789. acl.Dst = append(acl.Dst[:i], acl.Dst[i+1:]...)
  790. update = true
  791. }
  792. }
  793. }
  794. if update {
  795. logic.UpsertAcl(acl)
  796. }
  797. }
  798. return nil
  799. }
  800. func GetEgressUserRulesForNode(targetnode *models.Node,
  801. rules map[string]models.AclRule) map[string]models.AclRule {
  802. userNodes := getStaticUserNodesByNetwork(models.NetworkID(targetnode.Network))
  803. userGrpMap := GetUserGrpMap()
  804. allowedUsers := make(map[string][]models.Acl)
  805. acls := listUserPolicies(models.NetworkID(targetnode.Network))
  806. var targetNodeTags = make(map[models.TagID]struct{})
  807. targetNodeTags["*"] = struct{}{}
  808. egs, _ := (&schema.Egress{Network: targetnode.Network}).ListByNetwork(db.WithContext(context.TODO()))
  809. if len(egs) == 0 {
  810. return rules
  811. }
  812. defaultPolicy, _ := logic.GetDefaultPolicy(models.NetworkID(targetnode.Network), models.UserPolicy)
  813. for _, egI := range egs {
  814. if !egI.Status {
  815. continue
  816. }
  817. if _, ok := egI.Nodes[targetnode.ID.String()]; ok {
  818. if egI.Range != "" {
  819. targetNodeTags[models.TagID(egI.Range)] = struct{}{}
  820. } else if len(egI.DomainAns) > 0 {
  821. for _, domainAnsI := range egI.DomainAns {
  822. targetNodeTags[models.TagID(domainAnsI)] = struct{}{}
  823. }
  824. }
  825. targetNodeTags[models.TagID(egI.ID)] = struct{}{}
  826. }
  827. }
  828. if !defaultPolicy.Enabled {
  829. for _, acl := range acls {
  830. if !acl.Enabled {
  831. continue
  832. }
  833. dstTags := logic.ConvAclTagToValueMap(acl.Dst)
  834. for _, dst := range acl.Dst {
  835. if dst.ID == models.EgressID {
  836. e := schema.Egress{ID: dst.Value}
  837. err := e.Get(db.WithContext(context.TODO()))
  838. if err == nil && e.Status {
  839. for nodeID := range e.Nodes {
  840. dstTags[nodeID] = struct{}{}
  841. }
  842. if e.Range != "" {
  843. dstTags[e.Range] = struct{}{}
  844. } else if len(e.DomainAns) > 0 {
  845. for _, domainAnsI := range e.DomainAns {
  846. dstTags[domainAnsI] = struct{}{}
  847. }
  848. }
  849. }
  850. }
  851. }
  852. _, all := dstTags["*"]
  853. addUsers := false
  854. if !all {
  855. for nodeTag := range targetNodeTags {
  856. if _, ok := dstTags[nodeTag.String()]; ok {
  857. addUsers = true
  858. break
  859. }
  860. }
  861. } else {
  862. addUsers = true
  863. }
  864. if addUsers {
  865. // get all src tags
  866. for _, srcAcl := range acl.Src {
  867. if srcAcl.ID == models.UserAclID {
  868. allowedUsers[srcAcl.Value] = append(allowedUsers[srcAcl.Value], acl)
  869. } else if srcAcl.ID == models.UserGroupAclID {
  870. // fetch all users in the group
  871. if usersMap, ok := userGrpMap[models.UserGroupID(srcAcl.Value)]; ok {
  872. for userName := range usersMap {
  873. allowedUsers[userName] = append(allowedUsers[userName], acl)
  874. }
  875. }
  876. }
  877. }
  878. }
  879. }
  880. }
  881. if defaultPolicy.Enabled {
  882. r := models.AclRule{
  883. ID: defaultPolicy.ID,
  884. AllowedProtocol: defaultPolicy.Proto,
  885. AllowedPorts: defaultPolicy.Port,
  886. Direction: defaultPolicy.AllowedDirection,
  887. Allowed: true,
  888. }
  889. for _, userNode := range userNodes {
  890. if !userNode.StaticNode.Enabled {
  891. continue
  892. }
  893. // Get peers in the tags and add allowed rules
  894. if userNode.StaticNode.Address != "" {
  895. r.IPList = append(r.IPList, userNode.StaticNode.AddressIPNet4())
  896. }
  897. if userNode.StaticNode.Address6 != "" {
  898. r.IP6List = append(r.IP6List, userNode.StaticNode.AddressIPNet6())
  899. }
  900. }
  901. rules[defaultPolicy.ID] = r
  902. } else {
  903. for _, userNode := range userNodes {
  904. if !userNode.StaticNode.Enabled {
  905. continue
  906. }
  907. acls, ok := allowedUsers[userNode.StaticNode.OwnerID]
  908. if !ok {
  909. continue
  910. }
  911. for _, acl := range acls {
  912. if !acl.Enabled {
  913. continue
  914. }
  915. r := models.AclRule{
  916. ID: acl.ID,
  917. AllowedProtocol: acl.Proto,
  918. AllowedPorts: acl.Port,
  919. Direction: acl.AllowedDirection,
  920. Allowed: true,
  921. }
  922. // Get peers in the tags and add allowed rules
  923. if userNode.StaticNode.Address != "" {
  924. r.IPList = append(r.IPList, userNode.StaticNode.AddressIPNet4())
  925. }
  926. if userNode.StaticNode.Address6 != "" {
  927. r.IP6List = append(r.IP6List, userNode.StaticNode.AddressIPNet6())
  928. }
  929. for _, dstI := range acl.Dst {
  930. if dstI.ID == models.EgressID {
  931. e := schema.Egress{ID: dstI.Value}
  932. err := e.Get(db.WithContext(context.TODO()))
  933. if err != nil {
  934. continue
  935. }
  936. if e.Range != "" {
  937. ip, cidr, err := net.ParseCIDR(e.Range)
  938. if err == nil {
  939. if ip.To4() != nil {
  940. r.Dst = append(r.Dst, *cidr)
  941. } else {
  942. r.Dst6 = append(r.Dst6, *cidr)
  943. }
  944. }
  945. } else if len(e.DomainAns) > 0 {
  946. for _, domainAnsI := range e.DomainAns {
  947. ip, cidr, err := net.ParseCIDR(domainAnsI)
  948. if err == nil {
  949. if ip.To4() != nil {
  950. r.Dst = append(r.Dst, *cidr)
  951. } else {
  952. r.Dst6 = append(r.Dst6, *cidr)
  953. }
  954. }
  955. }
  956. }
  957. }
  958. }
  959. if userNode.StaticNode.Address6 != "" {
  960. r.IP6List = append(r.IP6List, userNode.StaticNode.AddressIPNet6())
  961. }
  962. if aclRule, ok := rules[acl.ID]; ok {
  963. aclRule.IPList = append(aclRule.IPList, r.IPList...)
  964. aclRule.IP6List = append(aclRule.IP6List, r.IP6List...)
  965. aclRule.Dst = append(aclRule.Dst, r.Dst...)
  966. aclRule.Dst6 = append(aclRule.Dst6, r.Dst6...)
  967. aclRule.IPList = logic.UniqueIPNetList(aclRule.IPList)
  968. aclRule.IP6List = logic.UniqueIPNetList(aclRule.IP6List)
  969. aclRule.Dst = logic.UniqueIPNetList(aclRule.Dst)
  970. aclRule.Dst6 = logic.UniqueIPNetList(aclRule.Dst6)
  971. rules[acl.ID] = aclRule
  972. } else {
  973. r.IPList = logic.UniqueIPNetList(r.IPList)
  974. r.IP6List = logic.UniqueIPNetList(r.IP6List)
  975. r.Dst = logic.UniqueIPNetList(r.Dst)
  976. r.Dst6 = logic.UniqueIPNetList(r.Dst6)
  977. rules[acl.ID] = r
  978. }
  979. }
  980. }
  981. }
  982. return rules
  983. }
  984. func GetUserAclRulesForNode(targetnode *models.Node,
  985. rules map[string]models.AclRule) map[string]models.AclRule {
  986. userNodes := getStaticUserNodesByNetwork(models.NetworkID(targetnode.Network))
  987. userGrpMap := GetUserGrpMap()
  988. allowedUsers := make(map[string][]models.Acl)
  989. acls := listUserPolicies(models.NetworkID(targetnode.Network))
  990. var targetNodeTags = make(map[models.TagID]struct{})
  991. if targetnode.Mutex != nil {
  992. targetnode.Mutex.Lock()
  993. targetNodeTags = maps.Clone(targetnode.Tags)
  994. targetnode.Mutex.Unlock()
  995. } else {
  996. targetNodeTags = maps.Clone(targetnode.Tags)
  997. }
  998. if targetNodeTags == nil {
  999. targetNodeTags = make(map[models.TagID]struct{})
  1000. }
  1001. defaultPolicy, _ := logic.GetDefaultPolicy(models.NetworkID(targetnode.Network), models.UserPolicy)
  1002. targetNodeTags[models.TagID(targetnode.ID.String())] = struct{}{}
  1003. if !defaultPolicy.Enabled {
  1004. for _, acl := range acls {
  1005. if !acl.Enabled {
  1006. continue
  1007. }
  1008. dstTags := logic.ConvAclTagToValueMap(acl.Dst)
  1009. _, all := dstTags["*"]
  1010. addUsers := false
  1011. if !all {
  1012. for _, dst := range acl.Dst {
  1013. if dst.ID == models.EgressID {
  1014. e := schema.Egress{ID: dst.Value}
  1015. err := e.Get(db.WithContext(context.TODO()))
  1016. if err == nil && e.Status && len(e.Nodes) > 0 {
  1017. if _, ok := e.Nodes[targetnode.ID.String()]; ok {
  1018. dstTags[targetnode.ID.String()] = struct{}{}
  1019. }
  1020. }
  1021. }
  1022. }
  1023. for nodeTag := range targetNodeTags {
  1024. if _, ok := dstTags[nodeTag.String()]; ok {
  1025. addUsers = true
  1026. break
  1027. }
  1028. }
  1029. } else {
  1030. addUsers = true
  1031. }
  1032. if addUsers {
  1033. // get all src tags
  1034. for _, srcAcl := range acl.Src {
  1035. if srcAcl.ID == models.UserAclID {
  1036. allowedUsers[srcAcl.Value] = append(allowedUsers[srcAcl.Value], acl)
  1037. } else if srcAcl.ID == models.UserGroupAclID {
  1038. // fetch all users in the group
  1039. if usersMap, ok := userGrpMap[models.UserGroupID(srcAcl.Value)]; ok {
  1040. for userName := range usersMap {
  1041. allowedUsers[userName] = append(allowedUsers[userName], acl)
  1042. }
  1043. }
  1044. }
  1045. }
  1046. }
  1047. }
  1048. }
  1049. if defaultPolicy.Enabled {
  1050. r := models.AclRule{
  1051. ID: defaultPolicy.ID,
  1052. AllowedProtocol: defaultPolicy.Proto,
  1053. AllowedPorts: defaultPolicy.Port,
  1054. Direction: defaultPolicy.AllowedDirection,
  1055. Allowed: true,
  1056. }
  1057. for _, userNode := range userNodes {
  1058. if !userNode.StaticNode.Enabled {
  1059. continue
  1060. }
  1061. // Get peers in the tags and add allowed rules
  1062. if userNode.StaticNode.Address != "" {
  1063. r.IPList = append(r.IPList, userNode.StaticNode.AddressIPNet4())
  1064. }
  1065. if userNode.StaticNode.Address6 != "" {
  1066. r.IP6List = append(r.IP6List, userNode.StaticNode.AddressIPNet6())
  1067. }
  1068. }
  1069. rules[defaultPolicy.ID] = r
  1070. } else {
  1071. for _, userNode := range userNodes {
  1072. if !userNode.StaticNode.Enabled {
  1073. continue
  1074. }
  1075. acls, ok := allowedUsers[userNode.StaticNode.OwnerID]
  1076. if !ok {
  1077. continue
  1078. }
  1079. for _, acl := range acls {
  1080. if !acl.Enabled {
  1081. continue
  1082. }
  1083. egressRanges4 := []net.IPNet{}
  1084. egressRanges6 := []net.IPNet{}
  1085. for _, dst := range acl.Dst {
  1086. if dst.Value == "*" {
  1087. e := schema.Egress{Network: targetnode.Network}
  1088. eli, _ := e.ListByNetwork(db.WithContext(context.Background()))
  1089. for _, eI := range eli {
  1090. if !eI.Status || len(eI.Nodes) == 0 {
  1091. continue
  1092. }
  1093. if _, ok := eI.Nodes[targetnode.ID.String()]; ok {
  1094. if eI.Range != "" {
  1095. _, cidr, err := net.ParseCIDR(eI.Range)
  1096. if err == nil {
  1097. if cidr.IP.To4() != nil {
  1098. egressRanges4 = append(egressRanges4, *cidr)
  1099. } else {
  1100. egressRanges6 = append(egressRanges6, *cidr)
  1101. }
  1102. }
  1103. } else if len(eI.DomainAns) > 0 {
  1104. for _, domainAnsI := range eI.DomainAns {
  1105. _, cidr, err := net.ParseCIDR(domainAnsI)
  1106. if err == nil {
  1107. if cidr.IP.To4() != nil {
  1108. egressRanges4 = append(egressRanges4, *cidr)
  1109. } else {
  1110. egressRanges6 = append(egressRanges6, *cidr)
  1111. }
  1112. }
  1113. }
  1114. }
  1115. }
  1116. }
  1117. break
  1118. }
  1119. if dst.ID == models.EgressID {
  1120. e := schema.Egress{ID: dst.Value}
  1121. err := e.Get(db.WithContext(context.TODO()))
  1122. if err == nil && e.Status && len(e.Nodes) > 0 {
  1123. if _, ok := e.Nodes[targetnode.ID.String()]; ok {
  1124. if e.Range != "" {
  1125. _, cidr, err := net.ParseCIDR(e.Range)
  1126. if err == nil {
  1127. if cidr.IP.To4() != nil {
  1128. egressRanges4 = append(egressRanges4, *cidr)
  1129. } else {
  1130. egressRanges6 = append(egressRanges6, *cidr)
  1131. }
  1132. }
  1133. } else if len(e.DomainAns) > 0 {
  1134. for _, domainAnsI := range e.DomainAns {
  1135. _, cidr, err := net.ParseCIDR(domainAnsI)
  1136. if err == nil {
  1137. if cidr.IP.To4() != nil {
  1138. egressRanges4 = append(egressRanges4, *cidr)
  1139. } else {
  1140. egressRanges6 = append(egressRanges6, *cidr)
  1141. }
  1142. }
  1143. }
  1144. }
  1145. }
  1146. }
  1147. }
  1148. }
  1149. r := models.AclRule{
  1150. ID: acl.ID,
  1151. AllowedProtocol: acl.Proto,
  1152. AllowedPorts: acl.Port,
  1153. Direction: acl.AllowedDirection,
  1154. Dst: []net.IPNet{targetnode.AddressIPNet4()},
  1155. Dst6: []net.IPNet{targetnode.AddressIPNet6()},
  1156. Allowed: true,
  1157. }
  1158. if len(egressRanges4) > 0 {
  1159. r.Dst = append(r.Dst, egressRanges4...)
  1160. }
  1161. if len(egressRanges6) > 0 {
  1162. r.Dst6 = append(r.Dst6, egressRanges6...)
  1163. }
  1164. // Get peers in the tags and add allowed rules
  1165. if userNode.StaticNode.Address != "" {
  1166. r.IPList = append(r.IPList, userNode.StaticNode.AddressIPNet4())
  1167. }
  1168. if userNode.StaticNode.Address6 != "" {
  1169. r.IP6List = append(r.IP6List, userNode.StaticNode.AddressIPNet6())
  1170. }
  1171. if aclRule, ok := rules[acl.ID]; ok {
  1172. aclRule.IPList = append(aclRule.IPList, r.IPList...)
  1173. aclRule.IP6List = append(aclRule.IP6List, r.IP6List...)
  1174. aclRule.Dst = append(aclRule.Dst, r.Dst...)
  1175. aclRule.Dst6 = append(aclRule.Dst6, r.Dst6...)
  1176. aclRule.IPList = logic.UniqueIPNetList(aclRule.IPList)
  1177. aclRule.IP6List = logic.UniqueIPNetList(aclRule.IP6List)
  1178. aclRule.Dst = logic.UniqueIPNetList(aclRule.Dst)
  1179. aclRule.Dst6 = logic.UniqueIPNetList(aclRule.Dst6)
  1180. rules[acl.ID] = aclRule
  1181. } else {
  1182. r.IPList = logic.UniqueIPNetList(r.IPList)
  1183. r.IP6List = logic.UniqueIPNetList(r.IP6List)
  1184. r.Dst = logic.UniqueIPNetList(r.Dst)
  1185. r.Dst6 = logic.UniqueIPNetList(r.Dst6)
  1186. rules[acl.ID] = r
  1187. }
  1188. }
  1189. }
  1190. }
  1191. return rules
  1192. }
  1193. func CheckIfAnyPolicyisUniDirectional(targetNode models.Node, acls []models.Acl) bool {
  1194. var targetNodeTags = make(map[models.TagID]struct{})
  1195. if targetNode.Mutex != nil {
  1196. targetNode.Mutex.Lock()
  1197. targetNodeTags = maps.Clone(targetNode.Tags)
  1198. targetNode.Mutex.Unlock()
  1199. } else {
  1200. targetNodeTags = maps.Clone(targetNode.Tags)
  1201. }
  1202. if targetNodeTags == nil {
  1203. targetNodeTags = make(map[models.TagID]struct{})
  1204. }
  1205. targetNodeTags[models.TagID(targetNode.ID.String())] = struct{}{}
  1206. targetNodeTags["*"] = struct{}{}
  1207. for _, acl := range acls {
  1208. if !acl.Enabled {
  1209. continue
  1210. }
  1211. if acl.AllowedDirection == models.TrafficDirectionBi && acl.Proto == models.ALL && acl.ServiceType == models.Any {
  1212. continue
  1213. }
  1214. if acl.Proto != models.ALL || acl.ServiceType != models.Any {
  1215. return true
  1216. }
  1217. srcTags := logic.ConvAclTagToValueMap(acl.Src)
  1218. dstTags := logic.ConvAclTagToValueMap(acl.Dst)
  1219. for nodeTag := range targetNodeTags {
  1220. if acl.RuleType == models.DevicePolicy {
  1221. if _, ok := srcTags[nodeTag.String()]; ok {
  1222. return true
  1223. }
  1224. if _, ok := srcTags[targetNode.ID.String()]; ok {
  1225. return true
  1226. }
  1227. }
  1228. if _, ok := dstTags[nodeTag.String()]; ok {
  1229. return true
  1230. }
  1231. if _, ok := dstTags[targetNode.ID.String()]; ok {
  1232. return true
  1233. }
  1234. }
  1235. }
  1236. return false
  1237. }
  1238. func GetTagMapWithNodesByNetwork(netID models.NetworkID, withStaticNodes bool) (tagNodesMap map[models.TagID][]models.Node) {
  1239. tagNodesMap = make(map[models.TagID][]models.Node)
  1240. nodes, _ := logic.GetNetworkNodes(netID.String())
  1241. for _, nodeI := range nodes {
  1242. tagNodesMap[models.TagID(nodeI.ID.String())] = []models.Node{
  1243. nodeI,
  1244. }
  1245. if nodeI.Tags == nil {
  1246. continue
  1247. }
  1248. if nodeI.Mutex != nil {
  1249. nodeI.Mutex.Lock()
  1250. }
  1251. for nodeTagID := range nodeI.Tags {
  1252. if nodeTagID == models.TagID(nodeI.ID.String()) {
  1253. continue
  1254. }
  1255. tagNodesMap[nodeTagID] = append(tagNodesMap[nodeTagID], nodeI)
  1256. }
  1257. if nodeI.Mutex != nil {
  1258. nodeI.Mutex.Unlock()
  1259. }
  1260. }
  1261. tagNodesMap["*"] = nodes
  1262. if !withStaticNodes {
  1263. return
  1264. }
  1265. return AddTagMapWithStaticNodes(netID, tagNodesMap)
  1266. }
  1267. func AddTagMapWithStaticNodes(netID models.NetworkID,
  1268. tagNodesMap map[models.TagID][]models.Node) map[models.TagID][]models.Node {
  1269. extclients, err := logic.GetNetworkExtClients(netID.String())
  1270. if err != nil {
  1271. return tagNodesMap
  1272. }
  1273. for _, extclient := range extclients {
  1274. if extclient.RemoteAccessClientID != "" {
  1275. continue
  1276. }
  1277. tagNodesMap[models.TagID(extclient.ClientID)] = []models.Node{
  1278. {
  1279. IsStatic: true,
  1280. StaticNode: extclient,
  1281. },
  1282. }
  1283. if extclient.Tags == nil {
  1284. continue
  1285. }
  1286. if extclient.Mutex != nil {
  1287. extclient.Mutex.Lock()
  1288. }
  1289. for tagID := range extclient.Tags {
  1290. if tagID == models.TagID(extclient.ClientID) {
  1291. continue
  1292. }
  1293. tagNodesMap[tagID] = append(tagNodesMap[tagID], extclient.ConvertToStaticNode())
  1294. tagNodesMap["*"] = append(tagNodesMap["*"], extclient.ConvertToStaticNode())
  1295. }
  1296. if extclient.Mutex != nil {
  1297. extclient.Mutex.Unlock()
  1298. }
  1299. }
  1300. return tagNodesMap
  1301. }