summaryrefslogtreecommitdiff
path: root/pkg/auth/sip2/client.go
blob: ed7fc7354d596b18a55ff870ed8408d7dcb04de1 (plain)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright (C) 2019 LEAP
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package sip2

import (
	"0xacab.org/leap/vpnweb/pkg/auth/creds"
	"errors"
	"fmt"
	"github.com/linxiaozhi/go-telnet"
	"log"
	"time"
)

const (
	Label                 string = "sip2"
	loginRequestTemplate  string = "9300CN%s|CO%s|CP%s|"
	statusRequestTemplate string = "23000%s    %sAO%s|AA%s|AD%s|"
	scStatusRequest       string = "9901002.00"
	heartBeatSeconds             = 240
)

type sipClient struct {
	host           string
	port           string
	location       string
	passwordPolicy string
	user           string
	pass           string
	conn           gote.Connection
	heartBeatDone  chan bool
	reqQueue       chan request
	parser         *Parser
}

type request struct {
	msg      string
	respChan chan response
}

type response struct {
	msg string
	err error
}

func newClient(host, port, location, passwordPolicy string) sipClient {
	reqQ := make(chan request)
	parser := getParser()
	c := sipClient{host, port, location, passwordPolicy, "", "", nil, nil, reqQ, parser}
	return c
}

// starts a dispatcher goroutine that ensures that all sip requests are serialized
func (c *sipClient) startDispatcher() {
	go func() {
		for req := range c.reqQueue {
			resp, err := c.handleRequest(req.msg)
			req.respChan <- response{resp, err}
		}
	}()
}

func (c *sipClient) startHeartBeat() {
	ticker := time.NewTicker(heartBeatSeconds * time.Second)
	c.heartBeatDone = make(chan bool)
	go func() {
		for {
			select {
			case <-c.heartBeatDone:
				log.Println("Stopping heartbeat")
				return
			case <-ticker.C:
				resp, err := c.sendRequest(scStatusRequest)
				/* TODO
				   for now we are only interested in letting the server
				   know that we are alive.
				   but we could parse the response fields */
				if err != nil {
					log.Println(">> status error:", err)
				}
				log.Println(">> sip status:", resp)
			}
		}
	}()
}

func (c *sipClient) sendRequest(msg string) (string, error) {
	respChan := make(chan response)
	c.reqQueue <- request{msg, respChan}
	resp := <-respChan
	return resp.msg, resp.err
}

// handleRequest should not be used directly: dipatcher should be the only caller. use sendRequest instead.
func (c *sipClient) handleRequest(msg string) (string, error) {
	err := telnetSend(c.conn, msg)
	if err != nil {
		return "", err
	}
	resp, err := telnetRead(c.conn)
	return resp, err
}

func (c *sipClient) doConnectAndReact() (bool, error) {
	_, err := c.connect()
	if err != nil {
		return false, err
	}
	_, err = c.login()
	if err != nil {
		return false, err
	}
	c.startDispatcher()
	c.startHeartBeat()
	return true, nil

}

func (c *sipClient) setCredentials(user, pass string) {
	c.user = user
	c.pass = pass
}

func (c *sipClient) connect() (bool, error) {
	conn, err := gote.DialTimeout("tcp", c.host+":"+c.port, time.Second*2)
	if nil != err {
		log.Println("connect error:", err)
		return false, err
	}
	c.conn = conn
	return true, nil
}

func (c *sipClient) login() (bool, error) {
	loginStr := fmt.Sprintf(loginRequestTemplate, c.user, c.pass, c.location)
	if nil == c.conn {
		fmt.Println("error! null connection")
		return false, errors.New("null connection received")
	}

	err := telnetSend(c.conn, loginStr)
	if err != nil {
		log.Println("Error while sending request")
		return false, err
	}

	loginResp, err := telnetRead(c.conn)
	if err != nil {
		log.Println("login error on read: ", err)
		return false, err
	}

	msg, err := c.parseResponse(loginResp)
	if err != nil {
		log.Println("login error:", err)
		return false, err
	}
	if value, ok := c.parser.getFixedFieldValue(msg, okVal); ok && value == trueVal {
		log.Println("SIP admin login OK")
		return true, nil
	}
	return false, nil
}

func (c *sipClient) close() {
	c.heartBeatDone <- true
	c.conn.Close()
}

func (c *sipClient) parseResponse(txt string) (*message, error) {
	msg, err := c.parser.parseMessage(txt)
	return msg, err
}

/* Authenticator interface */

func (c *sipClient) GetLabel() string {
	return Label
}

func (c *sipClient) NeedsCredentials() bool {
	return true
}

func (c *sipClient) CheckCredentials(credentials *creds.Credentials) (bool, error) {

	currentTime := time.Now()
	user := credentials.User
	passwd := credentials.Password

	statusRequest := fmt.Sprintf(
		statusRequestTemplate,
		currentTime.Format("20060102"),
		currentTime.Format("150102"),
		c.location, user, passwd)

	statusMsg := &message{}
	resp, err := c.sendRequest(statusRequest)
	if err != nil {
		return false, err
	}

	statusMsg, err = c.parseResponse(resp)
	if err != nil {
		log.Println("Error while parsing response:", resp)
		return false, err
	}
	if valid, err := isValidUser(statusMsg); valid {
		if c.passwordPolicy == "ignore" {
			// passwordless library
			return true, nil
		}
		if valid, err := isValidPassword(statusMsg); valid {
			return true, nil
		} else {
			return false, err
		}
	} else {
		return false, err
	}
}

func isValidUser(m *message) (bool, error) {
	value, ok := m.getFieldValue(validPatron)
	if !ok {
		return false, errors.New("parse error: expected BL field")
	}
	return toBool(value)
}

func isValidPassword(m *message) (bool, error) {
	value, ok := m.getFieldValue(validPatronPassword)
	if !ok {
		return false, errors.New("parse error: expected CQ field")
	}
	return toBool(value)
}