From d35f3e153496f21ff89bc0f08e0dc436766c48f0 Mon Sep 17 00:00:00 2001 From: "kali kaneko (leap communications)" Date: Sat, 25 Jan 2020 22:06:41 -0600 Subject: get sip2 telnet terminator from env var --- config/CONFIG | 1 + pkg/auth/middleware.go | 2 +- pkg/auth/sip2/auth.go | 17 +++++++++++++++-- pkg/auth/sip2/spec.go | 2 +- pkg/auth/sip2/telnet.go | 10 +++++----- pkg/config/main.go | 20 +++++++++++--------- 6 files changed, 34 insertions(+), 18 deletions(-) diff --git a/config/CONFIG b/config/CONFIG index cdd34ca..9a99584 100755 --- a/config/CONFIG +++ b/config/CONFIG @@ -13,3 +13,4 @@ export VPNWEB_SIP_PASS="Kohapassword1!" export VPNWEB_SIP_HOST="localhost" export VPNWEB_SIP_PORT="6001" export VPNWEB_SIP_LIBR_LOCATION=testlibrary +export VPNWEB_SIP_TERMINATOR="\r" diff --git a/pkg/auth/middleware.go b/pkg/auth/middleware.go index 37c204e..dfd4da3 100644 --- a/pkg/auth/middleware.go +++ b/pkg/auth/middleware.go @@ -14,7 +14,7 @@ const anonAuth string = "anon" const sip2Auth string = "sip" func bailOnBadAuthModule(module string) { - log.Fatal("Unknown auth module: '", module, "'. Should be one of: ", anonAuth, ", ", sipAuth, ".") + log.Fatal("Unknown auth module: '", module, "'. Should be one of: ", anonAuth, ", ", sip2Auth, ".") } func checkForAuthSecret(opts *config.Opts) { diff --git a/pkg/auth/sip2/auth.go b/pkg/auth/sip2/auth.go index f5ad0a4..58441e4 100644 --- a/pkg/auth/sip2/auth.go +++ b/pkg/auth/sip2/auth.go @@ -16,6 +16,9 @@ const SipPassVar string = "VPNWEB_SIP_PASS" const SipPortVar string = "VPNWEB_SIP_PORT" const SipHostVar string = "VPNWEB_SIP_HOST" const SipLibrLocVar string = "VPNWEB_SIP_LIBR_LOCATION" +const SipTerminatorVar string = "VPNWEB_SIP_TERMINATOR" + +const SipDefaultTerminator string = "\r\n" type Credentials struct { User string @@ -30,10 +33,18 @@ func getConfigFromEnv(envVar string) string { return val } +func setupTerminatorFromEnv() { + config.FallbackToEnv(&TelnetTerminator, SipTerminatorVar, SipDefaultTerminator) + if TelnetTerminator == "\\r" { + TelnetTerminator = "\r" + } else if TelnetTerminator == "\\r\\n" { + TelnetTerminator = "\r\n" + } +} + func SipAuthenticator(opts *config.Opts) http.HandlerFunc { - /* TODO -- catch connection errors */ - log.Println("Initializing sip2 authenticator") + log.Println("Initializing SIP2 authenticator") SipUser := getConfigFromEnv(SipUserVar) SipPass := getConfigFromEnv(SipPassVar) @@ -41,6 +52,8 @@ func SipAuthenticator(opts *config.Opts) http.HandlerFunc { SipPort := getConfigFromEnv(SipPortVar) SipLibrLoc := getConfigFromEnv(SipLibrLocVar) + setupTerminatorFromEnv() + sip := NewClient(SipHost, SipPort, SipLibrLoc) ok, err := sip.Connect() diff --git a/pkg/auth/sip2/spec.go b/pkg/auth/sip2/spec.go index 60a14d9..ba7c356 100644 --- a/pkg/auth/sip2/spec.go +++ b/pkg/auth/sip2/spec.go @@ -111,7 +111,7 @@ func getParser() *Parser { } parser.parseMessage = func(msg string) *Message { - txt := msg[:len(msg)-len(terminator)] + txt := msg[:len(msg)-len(TelnetTerminator)] code, err := strconv.Atoi(txt[:2]) if nil != err { log.Printf("Error parsing integer: %s\n", txt[:2]) diff --git a/pkg/auth/sip2/telnet.go b/pkg/auth/sip2/telnet.go index b5abd5f..faa72ff 100644 --- a/pkg/auth/sip2/telnet.go +++ b/pkg/auth/sip2/telnet.go @@ -4,9 +4,10 @@ import ( "github.com/reiver/go-telnet" ) -// TODO depends on how terminator is configured -- take it from config file -// const terminator string = "\r\n" -const terminator string = "\r" +// The terminator can be configured differently for different SIP endpoints. +// This gets set in sip2.auth according to an environment variable + +var TelnetTerminator string func telnetRead(conn *telnet.Conn) (out string) { var buffer [1]byte @@ -21,7 +22,7 @@ func telnetRead(conn *telnet.Conn) (out string) { } else { out += string(recvData) } - if len(out) > 1 && out[len(out)-len(terminator):] == terminator { + if len(out) > 1 && out[len(out)-len(TelnetTerminator):] == TelnetTerminator { break } } @@ -35,7 +36,6 @@ func telnetSend(conn *telnet.Conn, command string) { } var crlfBuffer [2]byte = [2]byte{'\r', '\n'} - crlf := crlfBuffer[:] conn.Write(commandBuffer) diff --git a/pkg/config/main.go b/pkg/config/main.go index fa23257..f5c0c35 100644 --- a/pkg/config/main.go +++ b/pkg/config/main.go @@ -6,7 +6,7 @@ import ( "os" ) -const DefaultAuthenticationModule = "anonymous" +const DefaultAuthenticationModule string = "anon" type Opts struct { Notls bool @@ -19,7 +19,9 @@ type Opts struct { AuthSecret string } -func fallbackToEnv(variable *string, envVar, defaultVar string) { +var SIPTelnetTerminator string = "" + +func FallbackToEnv(variable *string, envVar, defaultVar string) { if *variable == "" { val, exists := os.LookupEnv(envVar) @@ -60,13 +62,13 @@ func InitializeFlags(opts *Opts) { flag.StringVar(&opts.AuthSecret, "authSecret", "", "Authentication secret (optional)") flag.Parse() - fallbackToEnv(&opts.CaCrt, "VPNWEB_CACRT", "") - fallbackToEnv(&opts.CaKey, "VPNWEB_CAKEY", "") - fallbackToEnv(&opts.TlsCrt, "VPNWEB_TLSCRT", "") - fallbackToEnv(&opts.TlsKey, "VPNWEB_TLSKEY", "") - fallbackToEnv(&opts.Port, "VPNWEB_PORT", "8000") - fallbackToEnv(&opts.Auth, "VPNWEB_AUTH", DefaultAuthenticationModule) - fallbackToEnv(&opts.AuthSecret, "VPNWEB_AUTHSECRET", "") + FallbackToEnv(&opts.CaCrt, "VPNWEB_CACRT", "") + FallbackToEnv(&opts.CaKey, "VPNWEB_CAKEY", "") + FallbackToEnv(&opts.TlsCrt, "VPNWEB_TLSCRT", "") + FallbackToEnv(&opts.TlsKey, "VPNWEB_TLSKEY", "") + FallbackToEnv(&opts.Port, "VPNWEB_PORT", "8000") + FallbackToEnv(&opts.Auth, "VPNWEB_AUTH", DefaultAuthenticationModule) + FallbackToEnv(&opts.AuthSecret, "VPNWEB_AUTH_SECRET", "") } func CheckConfigurationOptions(opts *Opts) { -- cgit v1.2.3