dns.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. package logic
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "net"
  8. "os"
  9. "regexp"
  10. "sort"
  11. "strings"
  12. validator "github.com/go-playground/validator/v10"
  13. "github.com/gravitl/netmaker/database"
  14. "github.com/gravitl/netmaker/db"
  15. "github.com/gravitl/netmaker/logger"
  16. "github.com/gravitl/netmaker/models"
  17. "github.com/gravitl/netmaker/schema"
  18. "github.com/gravitl/netmaker/servercfg"
  19. "github.com/txn2/txeh"
  20. )
  21. var GetNameserversForNode = getNameserversForNode
  22. var GetNameserversForHost = getNameserversForHost
  23. var ValidateNameserverReq = validateNameserverReq
  24. type GlobalNs struct {
  25. ID string `json:"id"`
  26. IPs []string `json:"ips"`
  27. }
  28. var GlobalNsList = map[string]GlobalNs{
  29. "Google": {
  30. ID: "Google",
  31. IPs: []string{
  32. "8.8.8.8",
  33. "8.8.4.4",
  34. "2001:4860:4860::8888",
  35. "2001:4860:4860::8844",
  36. },
  37. },
  38. "Cloudflare": {
  39. ID: "Cloudflare",
  40. IPs: []string{
  41. "1.1.1.1",
  42. "1.0.0.1",
  43. "2606:4700:4700::1111",
  44. "2606:4700:4700::1001",
  45. },
  46. },
  47. "Quad9": {
  48. ID: "Quad9",
  49. IPs: []string{
  50. "9.9.9.9",
  51. "149.112.112.112",
  52. "2620:fe::fe",
  53. "2620:fe::9",
  54. },
  55. },
  56. }
  57. // SetDNS - sets the dns on file
  58. func SetDNS() error {
  59. hostfile, err := txeh.NewHosts(&txeh.HostsConfig{})
  60. if err != nil {
  61. return err
  62. }
  63. var corefilestring string
  64. networks, err := GetNetworks()
  65. if err != nil && !database.IsEmptyRecord(err) {
  66. return err
  67. }
  68. for _, net := range networks {
  69. corefilestring = corefilestring + net.NetID + " "
  70. dns, err := GetDNS(net.NetID)
  71. if err != nil && !database.IsEmptyRecord(err) {
  72. return err
  73. }
  74. for _, entry := range dns {
  75. hostfile.AddHost(entry.Address, entry.Name)
  76. }
  77. }
  78. dns := GetExtclientDNS()
  79. for _, entry := range dns {
  80. hostfile.AddHost(entry.Address, entry.Name)
  81. }
  82. if corefilestring == "" {
  83. corefilestring = "example.com"
  84. }
  85. err = hostfile.SaveAs("./config/dnsconfig/netmaker.hosts")
  86. if err != nil {
  87. return err
  88. }
  89. /* if something goes wrong with server DNS, check here
  90. // commented out bc we were not using IsSplitDNS
  91. if servercfg.IsSplitDNS() {
  92. err = SetCorefile(corefilestring)
  93. }
  94. */
  95. return err
  96. }
  97. // GetDNS - gets the DNS of a current network
  98. func GetDNS(network string) ([]models.DNSEntry, error) {
  99. dns, err := GetNodeDNS(network)
  100. if err != nil && !database.IsEmptyRecord(err) {
  101. return dns, err
  102. }
  103. customdns, err := GetCustomDNS(network)
  104. if err != nil && !database.IsEmptyRecord(err) {
  105. return dns, err
  106. }
  107. dns = append(dns, customdns...)
  108. return dns, nil
  109. }
  110. func EgressDNs(network string) (entries []models.DNSEntry) {
  111. egs, _ := (&schema.Egress{
  112. Network: network,
  113. }).ListByNetwork(db.WithContext(context.TODO()))
  114. for _, egI := range egs {
  115. if egI.Domain != "" && len(egI.DomainAns) > 0 {
  116. entry := models.DNSEntry{
  117. Name: egI.Domain,
  118. }
  119. for _, domainAns := range egI.DomainAns {
  120. ip, _, err := net.ParseCIDR(domainAns)
  121. if err == nil {
  122. if ip.To4() != nil {
  123. entry.Address = ip.String()
  124. } else {
  125. entry.Address6 = ip.String()
  126. }
  127. }
  128. }
  129. entries = append(entries, entry)
  130. }
  131. }
  132. return
  133. }
  134. // GetExtclientDNS - gets all extclients dns entries
  135. func GetExtclientDNS() []models.DNSEntry {
  136. extclients, err := GetAllExtClients()
  137. if err != nil {
  138. return []models.DNSEntry{}
  139. }
  140. var dns []models.DNSEntry
  141. for _, extclient := range extclients {
  142. var entry = models.DNSEntry{}
  143. entry.Name = fmt.Sprintf("%s.%s", extclient.ClientID, extclient.Network)
  144. entry.Network = extclient.Network
  145. if extclient.Address != "" {
  146. entry.Address = extclient.Address
  147. }
  148. if extclient.Address6 != "" {
  149. entry.Address6 = extclient.Address6
  150. }
  151. dns = append(dns, entry)
  152. }
  153. return dns
  154. }
  155. // GetNodeDNS - gets the DNS of a network node
  156. func GetNodeDNS(network string) ([]models.DNSEntry, error) {
  157. var dns []models.DNSEntry
  158. nodes, err := GetNetworkNodes(network)
  159. if err != nil {
  160. return dns, err
  161. }
  162. defaultDomain := GetDefaultDomain()
  163. for _, node := range nodes {
  164. if node.Network != network {
  165. continue
  166. }
  167. host, err := GetHost(node.HostID.String())
  168. if err != nil {
  169. continue
  170. }
  171. var entry = models.DNSEntry{}
  172. if defaultDomain == "" {
  173. entry.Name = fmt.Sprintf("%s.%s", host.Name, network)
  174. } else {
  175. entry.Name = fmt.Sprintf("%s.%s.%s", host.Name, network, defaultDomain)
  176. }
  177. entry.Network = network
  178. if node.Address.IP != nil {
  179. entry.Address = node.Address.IP.String()
  180. }
  181. if node.Address6.IP != nil {
  182. entry.Address6 = node.Address6.IP.String()
  183. }
  184. dns = append(dns, entry)
  185. }
  186. return dns, nil
  187. }
  188. func GetGwDNS(node *models.Node) string {
  189. if !servercfg.GetManageDNS() {
  190. return ""
  191. }
  192. h, err := GetHost(node.HostID.String())
  193. if err != nil {
  194. return ""
  195. }
  196. if h.DNS != "yes" {
  197. return ""
  198. }
  199. dns := []string{}
  200. if node.Address.IP != nil {
  201. dns = append(dns, node.Address.IP.String())
  202. }
  203. if node.Address6.IP != nil {
  204. dns = append(dns, node.Address6.IP.String())
  205. }
  206. return strings.Join(dns, ",")
  207. }
  208. func SetDNSOnWgConfig(gwNode *models.Node, extclient *models.ExtClient) {
  209. extclient.DNS = GetGwDNS(gwNode)
  210. }
  211. // GetCustomDNS - gets the custom DNS of a network
  212. func GetCustomDNS(network string) ([]models.DNSEntry, error) {
  213. var dns []models.DNSEntry
  214. collection, err := database.FetchRecords(database.DNS_TABLE_NAME)
  215. if err != nil {
  216. return dns, err
  217. }
  218. for _, value := range collection { // filter for entries based on network
  219. var entry models.DNSEntry
  220. if err := json.Unmarshal([]byte(value), &entry); err != nil {
  221. continue
  222. }
  223. if entry.Network == network {
  224. dns = append(dns, entry)
  225. }
  226. }
  227. return dns, err
  228. }
  229. // SetCorefile - sets the core file of the system
  230. func SetCorefile(domains string) error {
  231. dir, err := os.Getwd()
  232. if err != nil {
  233. return err
  234. }
  235. err = os.MkdirAll(dir+"/config/dnsconfig", 0744)
  236. if err != nil {
  237. logger.Log(0, "couldnt find or create /config/dnsconfig")
  238. return err
  239. }
  240. corefile := domains + ` {
  241. reload 15s
  242. hosts /root/dnsconfig/netmaker.hosts {
  243. fallthrough
  244. }
  245. forward . 8.8.8.8 8.8.4.4
  246. log
  247. }
  248. `
  249. err = os.WriteFile(dir+"/config/dnsconfig/Corefile", []byte(corefile), 0644)
  250. if err != nil {
  251. return err
  252. }
  253. return err
  254. }
  255. // GetAllDNS - gets all dns entries
  256. func GetAllDNS() ([]models.DNSEntry, error) {
  257. var dns []models.DNSEntry
  258. networks, err := GetNetworks()
  259. if err != nil && !database.IsEmptyRecord(err) {
  260. return []models.DNSEntry{}, err
  261. }
  262. for _, net := range networks {
  263. netdns, err := GetDNS(net.NetID)
  264. if err != nil {
  265. return []models.DNSEntry{}, nil
  266. }
  267. dns = append(dns, netdns...)
  268. }
  269. return dns, nil
  270. }
  271. // GetDNSEntryNum - gets which entry the dns was
  272. func GetDNSEntryNum(domain string, network string) (int, error) {
  273. num := 0
  274. entries, err := GetDNS(network)
  275. if err != nil {
  276. return 0, err
  277. }
  278. for i := 0; i < len(entries); i++ {
  279. if domain == entries[i].Name {
  280. num++
  281. }
  282. }
  283. return num, nil
  284. }
  285. // SortDNSEntrys - Sorts slice of DNSEnteys by their Address alphabetically with numbers first
  286. func SortDNSEntrys(unsortedDNSEntrys []models.DNSEntry) {
  287. sort.Slice(unsortedDNSEntrys, func(i, j int) bool {
  288. return unsortedDNSEntrys[i].Address < unsortedDNSEntrys[j].Address
  289. })
  290. }
  291. // IsNetworkNameValid - checks if a netid of a network uses valid characters
  292. func IsDNSEntryValid(d string) bool {
  293. re := regexp.MustCompile(`^[A-Za-z0-9-.]+$`)
  294. return re.MatchString(d)
  295. }
  296. // ValidateDNSCreate - checks if an entry is valid
  297. func ValidateDNSCreate(entry models.DNSEntry) error {
  298. if !IsDNSEntryValid(entry.Name) {
  299. return errors.New("invalid input. Only uppercase letters (A-Z), lowercase letters (a-z), numbers (0-9), minus sign (-) and dots (.) are allowed")
  300. }
  301. v := validator.New()
  302. _ = v.RegisterValidation("whitespace", func(f1 validator.FieldLevel) bool {
  303. match, _ := regexp.MatchString(`\s`, entry.Name)
  304. return !match
  305. })
  306. _ = v.RegisterValidation("name_unique", func(fl validator.FieldLevel) bool {
  307. num, err := GetDNSEntryNum(entry.Name, entry.Network)
  308. return err == nil && num == 0
  309. })
  310. _ = v.RegisterValidation("network_exists", func(fl validator.FieldLevel) bool {
  311. _, err := GetParentNetwork(entry.Network)
  312. return err == nil
  313. })
  314. err := v.Struct(entry)
  315. if err != nil {
  316. for _, e := range err.(validator.ValidationErrors) {
  317. logger.Log(1, e.Error())
  318. }
  319. }
  320. return err
  321. }
  322. // ValidateDNSUpdate - validates a DNS update
  323. func ValidateDNSUpdate(change models.DNSEntry, entry models.DNSEntry) error {
  324. v := validator.New()
  325. _ = v.RegisterValidation("whitespace", func(f1 validator.FieldLevel) bool {
  326. match, _ := regexp.MatchString(`\s`, entry.Name)
  327. return !match
  328. })
  329. _ = v.RegisterValidation("name_unique", func(fl validator.FieldLevel) bool {
  330. //if name & net not changing name we are good
  331. if change.Name == entry.Name && change.Network == entry.Network {
  332. return true
  333. }
  334. num, err := GetDNSEntryNum(change.Name, change.Network)
  335. return err == nil && num == 0
  336. })
  337. _ = v.RegisterValidation("network_exists", func(fl validator.FieldLevel) bool {
  338. _, err := GetParentNetwork(change.Network)
  339. return err == nil
  340. })
  341. err := v.Struct(change)
  342. if err != nil {
  343. for _, e := range err.(validator.ValidationErrors) {
  344. logger.Log(1, e.Error())
  345. }
  346. }
  347. return err
  348. }
  349. // DeleteDNS - deletes a DNS entry
  350. func DeleteDNS(domain string, network string) error {
  351. key, err := GetRecordKey(domain, network)
  352. if err != nil {
  353. return err
  354. }
  355. err = database.DeleteRecord(database.DNS_TABLE_NAME, key)
  356. return err
  357. }
  358. // CreateDNS - creates a DNS entry
  359. func CreateDNS(entry models.DNSEntry) (models.DNSEntry, error) {
  360. k, err := GetRecordKey(entry.Name, entry.Network)
  361. if err != nil {
  362. return models.DNSEntry{}, err
  363. }
  364. data, err := json.Marshal(&entry)
  365. if err != nil {
  366. return models.DNSEntry{}, err
  367. }
  368. err = database.Insert(k, string(data), database.DNS_TABLE_NAME)
  369. return entry, err
  370. }
  371. func validateNameserverReq(ns schema.Nameserver) error {
  372. if ns.Name == "" {
  373. return errors.New("name is required")
  374. }
  375. if ns.NetworkID == "" {
  376. return errors.New("network is required")
  377. }
  378. if len(ns.Servers) == 0 {
  379. return errors.New("atleast one nameserver should be specified")
  380. }
  381. network, err := GetNetwork(ns.NetworkID)
  382. if err != nil {
  383. return errors.New("invalid network id")
  384. }
  385. _, cidr, err4 := net.ParseCIDR(network.AddressRange)
  386. _, cidr6, err6 := net.ParseCIDR(network.AddressRange6)
  387. for _, nsIPStr := range ns.Servers {
  388. nsIP := net.ParseIP(nsIPStr)
  389. if nsIP == nil {
  390. return errors.New("invalid nameserver " + nsIPStr)
  391. }
  392. if err4 == nil && nsIP.To4() != nil {
  393. if cidr.Contains(nsIP) {
  394. return errors.New("cannot use netmaker IP as nameserver")
  395. }
  396. } else if err6 == nil && cidr6.Contains(nsIP) {
  397. return errors.New("cannot use netmaker IP as nameserver")
  398. }
  399. }
  400. if !ns.MatchAll && len(ns.MatchDomains) == 0 {
  401. return errors.New("atleast one match domain is required")
  402. }
  403. if !ns.MatchAll {
  404. for _, matchDomain := range ns.MatchDomains {
  405. if !IsValidMatchDomain(matchDomain) {
  406. return errors.New("invalid match domain")
  407. }
  408. }
  409. }
  410. // check if valid broadcast peers are added
  411. if len(ns.Nodes) > 0 {
  412. for nodeID := range ns.Nodes {
  413. node, err := GetNodeByID(nodeID)
  414. if err != nil {
  415. return errors.New("invalid node")
  416. }
  417. if node.Network != ns.NetworkID {
  418. return errors.New("invalid network node")
  419. }
  420. }
  421. }
  422. return nil
  423. }
  424. func getNameserversForNode(node *models.Node) (returnNsLi []models.Nameserver) {
  425. ns := &schema.Nameserver{
  426. NetworkID: node.Network,
  427. }
  428. nsLi, _ := ns.ListByNetwork(db.WithContext(context.TODO()))
  429. for _, nsI := range nsLi {
  430. if !nsI.Status {
  431. continue
  432. }
  433. _, all := nsI.Tags["*"]
  434. if all {
  435. for _, matchDomain := range nsI.MatchDomains {
  436. returnNsLi = append(returnNsLi, models.Nameserver{
  437. IPs: nsI.Servers,
  438. MatchDomain: matchDomain,
  439. })
  440. }
  441. continue
  442. }
  443. if _, ok := nsI.Nodes[node.ID.String()]; ok {
  444. for _, matchDomain := range nsI.MatchDomains {
  445. returnNsLi = append(returnNsLi, models.Nameserver{
  446. IPs: nsI.Servers,
  447. MatchDomain: matchDomain,
  448. })
  449. }
  450. }
  451. }
  452. if node.IsInternetGateway {
  453. globalNs := models.Nameserver{
  454. MatchDomain: ".",
  455. }
  456. for _, nsI := range GlobalNsList {
  457. globalNs.IPs = append(globalNs.IPs, nsI.IPs...)
  458. }
  459. returnNsLi = append(returnNsLi, globalNs)
  460. }
  461. return
  462. }
  463. func getNameserversForHost(h *models.Host) (returnNsLi []models.Nameserver) {
  464. if h.DNS != "yes" {
  465. return
  466. }
  467. for _, nodeID := range h.Nodes {
  468. node, err := GetNodeByID(nodeID)
  469. if err != nil {
  470. continue
  471. }
  472. ns := &schema.Nameserver{
  473. NetworkID: node.Network,
  474. }
  475. nsLi, _ := ns.ListByNetwork(db.WithContext(context.TODO()))
  476. for _, nsI := range nsLi {
  477. if !nsI.Status {
  478. continue
  479. }
  480. _, all := nsI.Tags["*"]
  481. if all {
  482. for _, matchDomain := range nsI.MatchDomains {
  483. returnNsLi = append(returnNsLi, models.Nameserver{
  484. IPs: nsI.Servers,
  485. MatchDomain: matchDomain,
  486. })
  487. }
  488. continue
  489. }
  490. if _, ok := nsI.Nodes[node.ID.String()]; ok {
  491. for _, matchDomain := range nsI.MatchDomains {
  492. returnNsLi = append(returnNsLi, models.Nameserver{
  493. IPs: nsI.Servers,
  494. MatchDomain: matchDomain,
  495. })
  496. }
  497. }
  498. }
  499. if node.IsInternetGateway {
  500. globalNs := models.Nameserver{
  501. MatchDomain: ".",
  502. }
  503. for _, nsI := range GlobalNsList {
  504. globalNs.IPs = append(globalNs.IPs, nsI.IPs...)
  505. }
  506. returnNsLi = append(returnNsLi, globalNs)
  507. }
  508. }
  509. return
  510. }
  511. // IsValidMatchDomain reports whether s is a valid "match domain".
  512. // Rules (simple/ASCII):
  513. // - "~." is allowed (match all).
  514. // - Optional leading "~" allowed (e.g., "~example.com").
  515. // - Optional single trailing "." allowed (FQDN form).
  516. // - No wildcards "*", no leading ".", no underscores.
  517. // - Labels: letters/digits/hyphen (LDH), 1–63 chars, no leading/trailing hyphen.
  518. // - Total length (without trailing dot) ≤ 253.
  519. func IsValidMatchDomain(s string) bool {
  520. s = strings.TrimSpace(s)
  521. if s == "" {
  522. return false
  523. }
  524. if s == "~." { // special case: match-all
  525. return true
  526. }
  527. // Strip optional leading "~"
  528. if strings.HasPrefix(s, "~") {
  529. s = s[1:]
  530. if s == "" {
  531. return false
  532. }
  533. }
  534. // Allow exactly one trailing dot
  535. if strings.HasSuffix(s, ".") {
  536. s = s[:len(s)-1]
  537. if s == "" {
  538. return false
  539. }
  540. }
  541. // Disallow leading dot, wildcards, underscores
  542. if strings.HasPrefix(s, ".") || strings.Contains(s, "*") || strings.Contains(s, "_") {
  543. return false
  544. }
  545. // Lowercase for ASCII checks
  546. s = strings.ToLower(s)
  547. // Length check
  548. if len(s) > 253 {
  549. return false
  550. }
  551. // Label regex: LDH, 1–63, no leading/trailing hyphen
  552. reLabel := regexp.MustCompile(`^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$`)
  553. parts := strings.Split(s, ".")
  554. for _, lbl := range parts {
  555. if len(lbl) == 0 || len(lbl) > 63 {
  556. return false
  557. }
  558. if !reLabel.MatchString(lbl) {
  559. return false
  560. }
  561. }
  562. return true
  563. }