forked from fiveai/goipa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnszone.go
executable file
·139 lines (110 loc) · 3.72 KB
/
dnszone.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2015 Andrew E. Bruno. All rights reserved.
// Use of this source code is governed by a BSD style
// license that can be found in the LICENSE file.
package ipa
import (
"encoding/json"
"errors"
"fmt"
"log"
"github.com/davecgh/go-spew/spew"
)
// DNSZone encapsulates DNS DNSZone data returned from ipa DNS commands
type DNSZone struct {
Dn string `json:"dn"`
Name IpaDNSName `json:"idnsname"`
NameFromIP IpaString `json:"name_from_ip"`
Active IpaBool `json:"idnszoneactive"`
Forwarders []string `json:"idnsforwarders"`
ForwardPolicy IpaString `json:"idnsforwardpolicy"`
ManagedBy IpaString `json:"managedby"`
AuthoritativeNameserver IpaDNSName `json:"idnssoamname"`
AdministratorEmail IpaDNSName `json:"idnssoarname"`
SOASerial IpaInt `json:"idnssoaserial"`
SOARefresh IpaInt `json:"idnssoarefresh"`
SOARetry IpaInt `json:"idnssoaretry"`
SOAExpire IpaInt `json:"idnssoaexpire"`
SOAMinimum IpaInt `json:"idnssoaminimum"`
TTL IpaInt `json:"dnsttl"`
DefaultTTL IpaInt `json:"dnsdefaultttl"`
DNSClass IpaString `json:"dnsclass"`
BINDUpdatePolicy IpaString `json:"idnsupdatepolicy"`
DynamicUpdate IpaBool `json:"idnsallowdynupdate"`
AllowQuery IpaString `json:"idnsallowquery"`
AllowTransfer IpaString `json:"idnsallowtransfer"`
AllowPTRSync IpaBool `json:"idnsallowsyncptr"`
AllowInLineDNSSECSigning IpaBool `json:"idnssecinlinesigning"`
NSEC3ParamRecord IpaString `json:"nsec3paramrecord"`
}
func (c *LdapClient) GetDNSZone(ns string) (*string, error) {
sr, err := c.Search("cn=dns",
fmt.Sprintf("(idnsname=%s)", ns),
[]string{"idnsname"})
if err != nil {
return nil, err
}
if len(sr.Entries) > 1 {
log.Printf(spew.Sdump(sr.Entries))
return nil, errors.New("too many entries returned")
}
if len(sr.Entries) < 1 {
return nil, errors.New("no entries returned")
}
uid := sr.Entries[0].GetAttributeValue("idnsname")
return &uid, nil
}
func (c *LdapClient) DNSZoneExists(ns string) (bool, error) {
sr, err := c.Search("cn=dns",
fmt.Sprintf("(idnsname=%s)", ns),
[]string{})
if err != nil {
return false, err
}
if len(sr.Entries) > 1 {
return false, errors.New("too many entries returned")
}
return len(sr.Entries) == 1, nil
}
// Fetch DNS zone details by call the FreeIPA user-show method
func (c *Client) GetDNSZone(ns string) (*DNSZone, error) {
options := map[string]interface{}{"all": true}
res, err := c.rpc("dnszone_show", []string{ns}, options)
if err != nil {
return nil, err
}
var dnsRec DNSZone
err = json.Unmarshal(res.Result.Data, &dnsRec)
if err != nil {
return nil, err
}
return &dnsRec, nil
}
// Create DNS DNSZone
func (c *Client) CreateDNSZone(options map[string]interface{}) (*DNSZone, error) {
options["version"] = "2.228"
res, err := c.rpc("dnszone_add", []string{}, options)
if err != nil {
return nil, err
}
var dns DNSZone
log.Printf("[TRACE] DNS zone: %s", string(res.Result.Data))
err = json.Unmarshal(res.Result.Data, &dns)
if err != nil {
return nil, err
}
return &dns, nil
}
// Delete DNS Zone
func (c *Client) DeleteDNSZone(ns string) error {
var options = map[string]interface{}{
"version": "2.231"}
_, err := c.rpc("dnszone_del", []string{ns}, options)
return err
}
func (c *Client) DNSZoneMod(ns string, key string, value interface{}) error {
options := map[string]interface{}{
key: value,
"version": "2.228"}
_, err := c.rpc("dnszone_mod", []string{ns}, options)
return err
}