blob: 19d25e60ea5a01128a1311f63e8ab244a9d79147 [file] [log] [blame]
Scott Bakered4efab2020-01-13 19:12:25 -08001package client
2
3import (
4 "bytes"
5 "encoding/binary"
6 "errors"
7 "fmt"
8 "io"
9 "net"
10 "time"
11
12 "gopkg.in/jcmturner/gokrb5.v7/iana/errorcode"
13 "gopkg.in/jcmturner/gokrb5.v7/messages"
14)
15
16// SendToKDC performs network actions to send data to the KDC.
17func (cl *Client) sendToKDC(b []byte, realm string) ([]byte, error) {
18 var rb []byte
19 if cl.Config.LibDefaults.UDPPreferenceLimit == 1 {
20 //1 means we should always use TCP
21 rb, errtcp := cl.sendKDCTCP(realm, b)
22 if errtcp != nil {
23 if e, ok := errtcp.(messages.KRBError); ok {
24 return rb, e
25 }
26 return rb, fmt.Errorf("communication error with KDC via TCP: %v", errtcp)
27 }
28 return rb, nil
29 }
30 if len(b) <= cl.Config.LibDefaults.UDPPreferenceLimit {
31 //Try UDP first, TCP second
32 rb, errudp := cl.sendKDCUDP(realm, b)
33 if errudp != nil {
34 if e, ok := errudp.(messages.KRBError); ok && e.ErrorCode != errorcode.KRB_ERR_RESPONSE_TOO_BIG {
35 // Got a KRBError from KDC
36 // If this is not a KRB_ERR_RESPONSE_TOO_BIG we will return immediately otherwise will try TCP.
37 return rb, e
38 }
39 // Try TCP
40 r, errtcp := cl.sendKDCTCP(realm, b)
41 if errtcp != nil {
42 if e, ok := errtcp.(messages.KRBError); ok {
43 // Got a KRBError
44 return r, e
45 }
46 return r, fmt.Errorf("failed to communicate with KDC. Attempts made with UDP (%v) and then TCP (%v)", errudp, errtcp)
47 }
48 rb = r
49 }
50 return rb, nil
51 }
52 //Try TCP first, UDP second
53 rb, errtcp := cl.sendKDCTCP(realm, b)
54 if errtcp != nil {
55 if e, ok := errtcp.(messages.KRBError); ok {
56 // Got a KRBError from KDC so returning and not trying UDP.
57 return rb, e
58 }
59 rb, errudp := cl.sendKDCUDP(realm, b)
60 if errudp != nil {
61 if e, ok := errudp.(messages.KRBError); ok {
62 // Got a KRBError
63 return rb, e
64 }
65 return rb, fmt.Errorf("failed to communicate with KDC. Attempts made with TCP (%v) and then UDP (%v)", errtcp, errudp)
66 }
67 }
68 return rb, nil
69}
70
71// dialKDCTCP establishes a UDP connection to a KDC.
72func dialKDCUDP(count int, kdcs map[int]string) (conn *net.UDPConn, err error) {
73 i := 1
74 for i <= count {
75 udpAddr, e := net.ResolveUDPAddr("udp", kdcs[i])
76 if e != nil {
77 err = fmt.Errorf("error resolving KDC address: %v", e)
78 return
79 }
80 conn, err = net.DialUDP("udp", nil, udpAddr)
81 if err == nil {
82 err = conn.SetDeadline(time.Now().Add(5 * time.Second))
83 if err != nil {
84 return
85 }
86 return
87 }
88 i++
89 }
90 err = errors.New("error in getting a UDP connection to any of the KDCs")
91 return
92}
93
94// dialKDCTCP establishes a TCP connection to a KDC.
95func dialKDCTCP(count int, kdcs map[int]string) (conn *net.TCPConn, err error) {
96 i := 1
97 for i <= count {
98 tcpAddr, e := net.ResolveTCPAddr("tcp", kdcs[i])
99 if e != nil {
100 err = fmt.Errorf("error resolving KDC address: %v", e)
101 return
102 }
103 conn, err = net.DialTCP("tcp", nil, tcpAddr)
104 if err == nil {
105 err = conn.SetDeadline(time.Now().Add(5 * time.Second))
106 if err != nil {
107 return
108 }
109 return
110 }
111 i++
112 }
113 err = errors.New("error in getting a TCP connection to any of the KDCs")
114 return
115}
116
117// sendKDCUDP sends bytes to the KDC via UDP.
118func (cl *Client) sendKDCUDP(realm string, b []byte) ([]byte, error) {
119 var r []byte
120 count, kdcs, err := cl.Config.GetKDCs(realm, false)
121 if err != nil {
122 return r, err
123 }
124 conn, err := dialKDCUDP(count, kdcs)
125 if err != nil {
126 return r, err
127 }
128 r, err = cl.sendUDP(conn, b)
129 if err != nil {
130 return r, err
131 }
132 return checkForKRBError(r)
133}
134
135// sendKDCTCP sends bytes to the KDC via TCP.
136func (cl *Client) sendKDCTCP(realm string, b []byte) ([]byte, error) {
137 var r []byte
138 count, kdcs, err := cl.Config.GetKDCs(realm, true)
139 if err != nil {
140 return r, err
141 }
142 conn, err := dialKDCTCP(count, kdcs)
143 if err != nil {
144 return r, err
145 }
146 rb, err := cl.sendTCP(conn, b)
147 if err != nil {
148 return r, err
149 }
150 return checkForKRBError(rb)
151}
152
153// sendUDP sends bytes to connection over UDP.
154func (cl *Client) sendUDP(conn *net.UDPConn, b []byte) ([]byte, error) {
155 var r []byte
156 defer conn.Close()
157 _, err := conn.Write(b)
158 if err != nil {
159 return r, fmt.Errorf("error sending to (%s): %v", conn.RemoteAddr().String(), err)
160 }
161 udpbuf := make([]byte, 4096)
162 n, _, err := conn.ReadFrom(udpbuf)
163 r = udpbuf[:n]
164 if err != nil {
165 return r, fmt.Errorf("sending over UDP failed to %s: %v", conn.RemoteAddr().String(), err)
166 }
167 if len(r) < 1 {
168 return r, fmt.Errorf("no response data from %s", conn.RemoteAddr().String())
169 }
170 return r, nil
171}
172
173// sendTCP sends bytes to connection over TCP.
174func (cl *Client) sendTCP(conn *net.TCPConn, b []byte) ([]byte, error) {
175 defer conn.Close()
176 var r []byte
177 /*
178 RFC https://tools.ietf.org/html/rfc4120#section-7.2.2
179 Each request (KRB_KDC_REQ) and response (KRB_KDC_REP or KRB_ERROR)
180 sent over the TCP stream is preceded by the length of the request as
181 4 octets in network byte order. The high bit of the length is
182 reserved for future expansion and MUST currently be set to zero. If
183 a KDC that does not understand how to interpret a set high bit of the
184 length encoding receives a request with the high order bit of the
185 length set, it MUST return a KRB-ERROR message with the error
186 KRB_ERR_FIELD_TOOLONG and MUST close the TCP stream.
187 NB: network byte order == big endian
188 */
189 var buf bytes.Buffer
190 err := binary.Write(&buf, binary.BigEndian, uint32(len(b)))
191 if err != nil {
192 return r, err
193 }
194 b = append(buf.Bytes(), b...)
195
196 _, err = conn.Write(b)
197 if err != nil {
198 return r, fmt.Errorf("error sending to KDC (%s): %v", conn.RemoteAddr().String(), err)
199 }
200
201 sh := make([]byte, 4, 4)
202 _, err = conn.Read(sh)
203 if err != nil {
204 return r, fmt.Errorf("error reading response size header: %v", err)
205 }
206 s := binary.BigEndian.Uint32(sh)
207
208 rb := make([]byte, s, s)
209 _, err = io.ReadFull(conn, rb)
210 if err != nil {
211 return r, fmt.Errorf("error reading response: %v", err)
212 }
213 if len(rb) < 1 {
214 return r, fmt.Errorf("no response data from KDC %s", conn.RemoteAddr().String())
215 }
216 return rb, nil
217}
218
219// checkForKRBError checks if the response bytes from the KDC are a KRBError.
220func checkForKRBError(b []byte) ([]byte, error) {
221 var KRBErr messages.KRBError
222 if err := KRBErr.Unmarshal(b); err == nil {
223 return b, KRBErr
224 }
225 return b, nil
226}