10 Commits

Author SHA1 Message Date
yo f59d548f04 v1.0.3 : updateCache have its own connection, minor msg modeling and error check 2026-07-14 17:16:48 +02:00
yo 0e73dac143 Reconnect to LDAP backend after network issue, version bump to 1.0.2 2023-01-20 10:16:34 +01:00
yo 8ac556ad8b Fix sigsev when conLdap.Close() after connection was droped remotely 2023-01-16 10:39:35 +01:00
yo 6e9947809c Change "Invalid IP" msgs to info loglevel 2022-05-17 15:54:12 +02:00
yo 874b1c541a Duh! 2022-05-17 11:51:23 +02:00
yo 28dd17ccee Dont create pidfile if not defined 2022-05-17 11:43:36 +02:00
yo a84e22ce61 Clean + version bump 2022-05-16 13:53:01 +02:00
yo 365b7b9792 Add timeout in config file 2022-05-13 14:40:34 +02:00
yo b2fff3789b License file 2022-05-13 14:35:08 +02:00
yo b9807f0ba7 go fmt 2022-05-13 10:42:47 +02:00
3 changed files with 129 additions and 83 deletions
+30
View File
@@ -0,0 +1,30 @@
Copyright (C) 2020, yo000 <johan@nosd.in>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
- Neither the name of the Mumble Developers nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
`AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+2
View File
@@ -5,3 +5,5 @@ ldapUser cn=mynettcptable,ou=services,dc=example,dc=org
ldapPass here_lies_the_password ldapPass here_lies_the_password
# networks cache refresh interval # networks cache refresh interval
refresh 300 refresh 300
# Inactive connection timeout
timeout 30
+47 -33
View File
@@ -7,26 +7,26 @@
package main package main
import ( import (
"os"
"fmt" "fmt"
"net" "net"
// "log" "os"
// "log"
"errors"
"flag" "flag"
"log/syslog"
"strings"
"sync" "sync"
"time" "time"
"errors"
"strings"
"log/syslog"
"github.com/tabalt/pidfile"
"github.com/go-ldap/ldap/v3" "github.com/go-ldap/ldap/v3"
"github.com/peterbourgon/ff" "github.com/peterbourgon/ff"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
lSyslog "github.com/sirupsen/logrus/hooks/syslog" lSyslog "github.com/sirupsen/logrus/hooks/syslog"
"github.com/tabalt/pidfile"
) )
const ( const (
version = "1.0.0-rc" version = "1.0.3"
) )
var ( var (
@@ -95,7 +95,6 @@ func unsetNetCachePresentFlag() {
} }
} }
func buildNetCacheFromIPNetwork(conLdap *ldap.Conn) error { func buildNetCacheFromIPNetwork(conLdap *ldap.Conn) error {
attribute := "ipNetworkNumber" attribute := "ipNetworkNumber"
@@ -117,17 +116,13 @@ func buildNetCacheFromIPNetwork(conLdap *ldap.Conn) error {
logstream.Info(fmt.Sprintf("Error searching into LDAP: Attribute %s not found for entry %s\n", attribute, r)) logstream.Info(fmt.Sprintf("Error searching into LDAP: Attribute %s not found for entry %s\n", attribute, r))
continue continue
} else { } else {
// Explode the network to individual IPs
// 1: Verify format : Either CIDR, or netmask is in ipNetworkMask (do we want to support this?)
// 2: n := iplib.NewNet4(net.ParseIP("192.168.0.0"), 16)
// n.Enumerate(
_, ipnet, err := net.ParseCIDR(r.Attributes[0].Values[0]) _, ipnet, err := net.ParseCIDR(r.Attributes[0].Values[0])
if err != nil { if err != nil {
logstream.Info(err.Error()) logstream.Info(err.Error())
continue continue
} }
if false == checkNetCacheContainsAndFlag(ipnet) { if false == checkNetCacheContainsAndFlag(ipnet) {
netCache = append(netCache, NoAuthNet{ Net: ipnet, Present: true }) netCache = append(netCache, NoAuthNet{Net: ipnet, Present: true})
} }
} }
} }
@@ -143,7 +138,6 @@ func buildNetCacheFromIPNetwork(conLdap *ldap.Conn) error {
return nil return nil
} }
func isIPContainedInNetCache(string_ip string) (bool, error) { func isIPContainedInNetCache(string_ip string) (bool, error) {
ip := net.ParseIP(string_ip) ip := net.ParseIP(string_ip)
if ip == nil { if ip == nil {
@@ -182,9 +176,9 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
// Dont notice if client closed connection // Dont notice if client closed connection
if err.Error() != "EOF" && !strings.HasSuffix(err.Error(), "i/o timeout") { if err.Error() != "EOF" && !strings.HasSuffix(err.Error(), "i/o timeout") {
logstream.Errorf("Error reading connection: %v\n", err.Error()) logstream.Errorf("Error reading connection: %v\n", err.Error())
} else {
logstream.Debug("Error reading on connection : %s", err.Error())
} }
//sendResponse(connClt, err.Error(), 500)
//continue
return return
} }
@@ -214,7 +208,7 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
// "set loglevel level" sent on listening port will set current loglevel // "set loglevel level" sent on listening port will set current loglevel
if readlen > 14 && strings.EqualFold(string(buf[:12]), "set loglevel") { if readlen > 14 && strings.EqualFold(string(buf[:12]), "set loglevel") {
logstream.Infof("Received \"%s\" instruction from %s", string(buf[:readlen-1]), connClt.RemoteAddr().String()) logstream.Infof("Received \"%s\" instruction from %s", string(buf[:readlen-1]), connClt.RemoteAddr().String())
level, err := logrus.ParseLevel(string(buf[13:readlen-1])) level, err := logrus.ParseLevel(string(buf[13 : readlen-1]))
if err != nil { if err != nil {
sendResponse(connClt, fmt.Sprintf("Invalid log level: %s", string(buf[13:readlen-1])), 500) sendResponse(connClt, fmt.Sprintf("Invalid log level: %s", string(buf[13:readlen-1])), 500)
} else { } else {
@@ -230,12 +224,17 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
sendResponse(connClt, fmt.Sprintf("Invalid request: %s", buf[:readlen-1]), 500) sendResponse(connClt, fmt.Sprintf("Invalid request: %s", buf[:readlen-1]), 500)
continue continue
} }
ip := string(buf[4:readlen-1]) ip := string(buf[4 : readlen-1])
// First query netCache built with ipNetworkNumber // First query netCache built with ipNetworkNumber
res, err := isIPContainedInNetCache(ip) res, err := isIPContainedInNetCache(ip)
if err != nil { if err != nil {
if strings.EqualFold(err.Error(), fmt.Sprintf("Invalid IP: %s", ip)) {
// We don't want those msg to pollute logs
logstream.Info(err.Error())
} else {
logstream.Error(err.Error()) logstream.Error(err.Error())
}
sendResponse(connClt, err.Error(), 500) sendResponse(connClt, err.Error(), 500)
continue continue
} }
@@ -279,15 +278,21 @@ func sendResponse(con net.Conn, respMsg string, respCode int) error {
return nil return nil
} }
// attempt is useless now. It was introduced to break after N attempts, but this is not implemented. Should it be?
func searchLdap(searchReq *ldap.SearchRequest, attempt int) (*ldap.SearchResult, error) { func searchLdap(searchReq *ldap.SearchRequest, attempt int) (*ldap.SearchResult, error) {
mutex.Lock() mutex.Lock()
result, err := conLdap.Search(searchReq) result, err := conLdap.Search(searchReq)
mutex.Unlock() mutex.Unlock()
// Let's just manage connection errors here // Let's just manage connection errors here
if err != nil && strings.HasSuffix(err.Error(), "ldap: connection closed") { if err != nil {
if strings.HasSuffix(err.Error(), "ldap: connection closed") || err == ldap.ErrNilConnection {
logstream.Error("LDAP connection closed, retrying") logstream.Error("LDAP connection closed, retrying")
mutex.Lock() mutex.Lock()
// 16/01/2023: panic: runtime error: invalid memory address or nil pointer dereference
// probably bc connection is already closed
if conLdap != nil {
conLdap.Close() conLdap.Close()
}
conLdap, err = connectLdap() conLdap, err = connectLdap()
mutex.Unlock() mutex.Unlock()
if err != nil { if err != nil {
@@ -297,6 +302,7 @@ func searchLdap(searchReq *ldap.SearchRequest, attempt int) (*ldap.SearchResult,
return searchLdap(searchReq, attempt) return searchLdap(searchReq, attempt)
} }
} }
}
return result, err return result, err
} }
@@ -305,23 +311,32 @@ func connectLdap() (*ldap.Conn, error) {
conLdap, err = ldap.DialURL(*ldapURL) conLdap, err = ldap.DialURL(*ldapURL)
if err != nil { if err != nil {
logstream.Errorf("Error dialing LDAP on %s: %v\n", *ldapURL, err) logstream.Errorf("Error dialing LDAP on %s: %v\n", *ldapURL, err)
return conLdap, err return nil, err
} }
err = conLdap.Bind(*ldapUser, *ldapPass) err = conLdap.Bind(*ldapUser, *ldapPass)
if err != nil { if err != nil {
logstream.Errorf("Error binding LDAP: ", err) logstream.Errorf("Error binding LDAP: %s", err)
return conLdap, err return nil, err
} }
return conLdap, err return conLdap, err
} }
// TODO : buildNetCache should have its own LDAP connection func periodicallyUpdateCache() {
func periodicallyUpdateCache(conLdap *ldap.Conn) { myLdap, err := connectLdap()
if err != nil {
return
}
// On initialise immediatement le cache // On initialise immediatement le cache
buildNetCacheFromIPNetwork(conLdap) buildNetCacheFromIPNetwork(myLdap)
myLdap.Close()
for range time.Tick(time.Second * time.Duration(*refreshInterval)) { for range time.Tick(time.Second * time.Duration(*refreshInterval)) {
buildNetCacheFromIPNetwork(conLdap) myLdap, err := connectLdap()
if err != nil {
return
}
buildNetCacheFromIPNetwork(myLdap)
myLdap.Close()
} }
} }
@@ -335,7 +350,7 @@ func run() {
return return
} }
go periodicallyUpdateCache(conLdap) go periodicallyUpdateCache()
// Spawn a go routine for incoming connection // Spawn a go routine for incoming connection
for { for {
@@ -357,7 +372,7 @@ func main() {
ldapBaseDN = fs.String("ldapDN", "", "LDAP base DN (also via LDAPDN env var)") ldapBaseDN = fs.String("ldapDN", "", "LDAP base DN (also via LDAPDN env var)")
ldapUser = fs.String("ldapUser", "", "LDAP user DN (also via LDAPUSER env var)") ldapUser = fs.String("ldapUser", "", "LDAP user DN (also via LDAPUSER env var)")
ldapPass = fs.String("ldapPass", "", "LDAP user password (also via LDAPPASS env var)") ldapPass = fs.String("ldapPass", "", "LDAP user password (also via LDAPPASS env var)")
pidFilePath = fs.String("pidfile", "/var/run/mynettcptable/mynettcptable.pid", "PID File (also via PIDFILE env var)") pidFilePath = fs.String("pidfile", "", "PID File (also via PIDFILE env var). Creates pidfile only if defined")
refreshInterval = fs.Int("refresh", 300, "Net cache update interval in seconds") refreshInterval = fs.Int("refresh", 300, "Net cache update interval in seconds")
timeout = fs.Int("timeout", 5, "timeout in seconds") timeout = fs.Int("timeout", 5, "timeout in seconds")
_ = fs.String("config", "", "config file (optional)") _ = fs.String("config", "", "config file (optional)")
@@ -374,7 +389,7 @@ func main() {
return return
} }
fmt.Printf("MyNetTCPTable v.%s\n", version) fmt.Printf("%s: MyNetTCPTable v.%s listening on %s\n", time.Now().Format(time.RFC3339), version, *listen)
logstream = logrus.New() logstream = logrus.New()
level, err := logrus.ParseLevel(*logLevel) level, err := logrus.ParseLevel(*logLevel)
@@ -391,7 +406,7 @@ func main() {
if strings.EqualFold(*logTo, "syslog") { if strings.EqualFold(*logTo, "syslog") {
// level != priority // level != priority
prio := syslog.LOG_MAIL prio := syslog.LOG_MAIL
switch (*logLevel) { switch *logLevel {
case "fatal": case "fatal":
prio += syslog.LOG_CRIT prio += syslog.LOG_CRIT
case "error": case "error":
@@ -411,15 +426,14 @@ func main() {
logstream.Hooks.Add(hook) logstream.Hooks.Add(hook)
} }
if len(*pidFilePath) > 0 {
if pid, err := pidfile.Create(*pidFilePath); err != nil { if pid, err := pidfile.Create(*pidFilePath); err != nil {
logstream.Fatal(err) logstream.Fatal(err)
} else { } else {
defer pid.Clear() defer pid.Clear()
} }
}
//defer logstream.Close()
logstream.Infof("Start listening for incoming connections on %s\n", *listen) logstream.Infof("Start listening for incoming connections on %s\n", *listen)
run() run()
} }