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
|
// 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"
"fmt"
"github.com/reiver/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|"
)
type sipClient struct {
host string
port string
location string
conn *telnet.Conn
parser *Parser
}
func newClient(host, port, location string) sipClient {
c := sipClient{host, port, location, nil, nil}
c.parser = getParser()
return c
}
func (c *sipClient) Connect() (bool, error) {
conn, err := telnet.DialTo(c.host + ":" + c.port)
if nil != err {
log.Println("error", err)
return false, err
}
c.conn = conn
return true, nil
}
func (c *sipClient) Login(user, pass string) bool {
loginStr := fmt.Sprintf(loginRequestTemplate, user, pass, c.location)
if nil == c.conn {
fmt.Println("error! null connection")
}
telnetSend(c.conn, loginStr)
loginResp := telnetRead(c.conn)
msg := c.parseResponse(loginResp)
if value, ok := c.parser.getFixedFieldValue(msg, okVal); ok && value == trueVal {
return true
}
return false
}
func (c *sipClient) parseResponse(txt string) *message {
msg := c.parser.parseMessage(txt)
return msg
}
/* Authenticator interface */
func (c *sipClient) GetLabel() string {
return Label
}
func (c *sipClient) NeedsCredentials() bool {
return true
}
func (c *sipClient) CheckCredentials(credentials *creds.Credentials) bool {
currentTime := time.Now()
user := credentials.User
passwd := credentials.Password
statusRequest := fmt.Sprintf(
statusRequestTemplate,
currentTime.Format("20060102"),
currentTime.Format("150102"),
c.location, user, passwd)
telnetSend(c.conn, statusRequest)
statusMsg := c.parseResponse(telnetRead(c.conn))
if value, ok := c.parser.getFieldValue(statusMsg, validPatron); ok && value == yes {
if value, ok := c.parser.getFieldValue(statusMsg, validPatronPassword); ok && value == yes {
return true
}
}
// TODO log whatever error we can find (AF, Screen Message, for instance)
return false
}
|