-
Notifications
You must be signed in to change notification settings - Fork 0
/
dhcpScripter.go
232 lines (201 loc) · 5.69 KB
/
dhcpScripter.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package main
import (
"encoding/json"
"errors"
"flag"
"io/ioutil"
"log/syslog"
"net"
"os"
"os/exec"
"strconv"
"strings"
"time"
)
const (
// Message Types we are interested in
DHCPDiscover = 1
// DHCP Options
DHCPMessageType = 53
HostName = 12
// Default DHCP Port
DHCPUDPPort = 67
// Duplicate Packet Timeout
DuplicatePacketTimeout = 20 * time.Second
)
type config struct {
Port int
SysLog bool
FileLog string
NICs map[string]NIC // Commands
/*XMPPServer string
XMPPLogin string
XMPPPassword string
XMPPLog []string*/
}
type NIC struct {
Name string
Cmd []string // Single Command
SysLog *bool // Override for this nic
FileLog *string // Override for this nic
// XMPPLog *[]string // Override for this
// Cmds [][]string // Multiple Commands (run consecutively)
}
type dhcpMessage struct { // Overkill for this program, but here to learn
Raw []byte
Op *byte // 1 = Request, 2 = Reply
HType *byte // 1 = Ethernet
HLen *byte // MAC length, 6 for ethernet
Hops *byte //
XId []byte // Client transaction id
Secs []byte // Client filled - reflect
Flags []byte // ???
CIAddr net.IP // Requested IP
YIAddr net.IP // Granted IP
SIAddr net.IP // Server IP
GIAddr net.IP // Gateway IP?
CHAddr net.HardwareAddr // Client hardware address
Cookie []byte
Options []byte
}
func (dm *dhcpMessage) DecodeOptions() map[byte][]byte {
opts := dm.Options
options := make(map[byte][]byte, 10)
for len(opts) >= 2 {
size := int(opts[1])
if len(opts) >= 2+size {
options[opts[0]] = opts[2 : 2+size]
}
opts = opts[2+size:]
}
return options
}
func parseDHCP(r []byte) (*dhcpMessage, error) { // Overkill for this program, but here to learn
if len(r) < 240 {
return nil, errors.New("Packet not long enough.")
}
return &dhcpMessage{
Raw: r,
Op: &r[0],
HType: &r[1],
HLen: &r[2],
Hops: &r[3],
XId: r[4:8],
Secs: r[8:10],
Flags: r[10:12],
CIAddr: net.IP(r[12:16]),
YIAddr: net.IP(r[16:20]),
SIAddr: net.IP(r[20:24]),
GIAddr: net.IP(r[24:28]),
CHAddr: net.HardwareAddr(r[28 : 28+r[2]]), // max endPos 44
// 192 bytes of zeros BOOTP legacy
Cookie: r[236:240],
Options: r[240:],
}, nil
}
func main() {
configFile := flag.String("conf", "dhcps.conf", "Location of config file")
flag.Parse()
// Read Config File
data, err := ioutil.ReadFile(*configFile)
if err != nil {
os.Stderr.WriteString("Error: " + err.Error())
return
}
config := &config{} // Empty config struct
// Load data into config struct
if err := json.Unmarshal(data, config); err != nil {
os.Stderr.WriteString("Error:" + err.Error())
return
}
if config.Port < 1 { // If not specified will be zero, so use default port
config.Port = DHCPUDPPort
}
// Listen
conn, err := net.ListenPacket("udp", ":"+strconv.Itoa(config.Port))
if err != nil {
os.Stderr.WriteString("Error:" + err.Error())
return
}
// Make all NICs lower case so matching works
for k, v := range config.NICs {
delete(config.NICs, k)
config.NICs[strings.ToLower(k)] = v
}
buffer := make([]byte, 1500)
lastFire := make(map[string]time.Time, 1) // Some devices send multiple packets in a row
currentlyExecuting := make(map[string]bool, 1)
for {
n, _, err := conn.ReadFrom(buffer)
if err != nil {
panic(err.Error())
}
//fmt.Println("Packet In:", buffer[:n])
dhcpPacket, err := parseDHCP(buffer[:n])
if err != nil {
continue
}
nicAddr := dhcpPacket.CHAddr.String()
now := time.Now()
expire := now.Add(-DuplicatePacketTimeout)
for i, t := range lastFire {
if t.Before(expire) {
delete(lastFire, i)
}
}
if _, ok := lastFire[nicAddr]; ok { // Filter duplicate packet
continue
}
nic, ok := config.NICs[nicAddr]
if !ok {
nic = config.NICs["default"]
}
options := dhcpPacket.DecodeOptions()
if msgType := options[DHCPMessageType]; len(msgType) != 1 || msgType[0] != DHCPDiscover {
continue
}
hostname := string(options[HostName])
if hostname == "" {
hostname = "*"
}
if (nic.SysLog != nil && *nic.SysLog == true) || (nic.SysLog == nil && config.SysLog == true) {
if w, err := syslog.New(syslog.LOG_NOTICE, "DHCPScripter"); err == nil {
if err = w.Notice(nicAddr + " (" + hostname + ") " + nic.Name + " Connected"); err != nil {
os.Stderr.WriteString("Could not write to SysLog: " + err.Error() + "\n")
}
w.Close()
} else {
os.Stderr.WriteString("Could not connect to SysLog: " + err.Error() + "\n")
}
}
logFile := ""
if nic.FileLog != nil && len(*nic.FileLog) > 0 {
logFile = *nic.FileLog
} else if nic.FileLog == nil && len(config.FileLog) > 0 {
logFile = config.FileLog
}
if logFile != "" {
file, err := os.OpenFile(logFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
os.Stderr.WriteString("Error opening " + logFile + " for writing: " + err.Error() + "\n")
}
file.WriteString(now.Format("2006-01-02 15:04:05") + " - " + nicAddr + " (" + hostname + ") " + nic.Name + " Connected\n")
file.Close()
}
if nic.Cmd != nil && currentlyExecuting[nicAddr] == false {
currentlyExecuting[nicAddr] = true
cmd := append([]string{}, nic.Cmd...)
for i, v := range cmd {
cmd[i] = strings.Replace(strings.Replace(v, "%hostname", hostname, -1), "%nic", nicAddr, -1)
}
go func(nicAddr string, cmd []string) { // Run in background
if result, err := exec.Command(cmd[0], cmd[1:]...).Output(); err != nil {
os.Stderr.WriteString("Error:" + err.Error())
} else if len(result) > 0 {
os.Stdout.Write(result)
}
delete(currentlyExecuting, nicAddr)
}(nicAddr, cmd)
}
}
}