2021-09-17 04:44:35 +00:00
|
|
|
package ntp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2021-09-17 09:53:59 +00:00
|
|
|
"errors"
|
2023-05-07 06:39:17 +00:00
|
|
|
"fmt"
|
2021-09-17 04:44:35 +00:00
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
2021-11-23 09:45:38 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/logging"
|
2021-09-17 04:44:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewProvider instantiate a ntp provider given a configuration.
|
|
|
|
func NewProvider(config *schema.NTPConfiguration) *Provider {
|
2021-11-23 09:45:38 +00:00
|
|
|
return &Provider{
|
|
|
|
config: config,
|
|
|
|
log: logging.Logger(),
|
|
|
|
}
|
2021-09-17 04:44:35 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 09:53:59 +00:00
|
|
|
// StartupCheck implements the startup check provider interface.
|
2021-11-23 09:45:38 +00:00
|
|
|
func (p *Provider) StartupCheck() (err error) {
|
2023-05-07 06:39:17 +00:00
|
|
|
var offset time.Duration
|
|
|
|
|
|
|
|
if offset, err = p.GetOffset(); err != nil {
|
|
|
|
p.log.WithError(err).Warnf("Could not determine the clock offset due to an error")
|
2021-09-17 09:53:59 +00:00
|
|
|
|
|
|
|
return nil
|
2021-09-17 04:44:35 +00:00
|
|
|
}
|
|
|
|
|
2023-05-07 06:39:17 +00:00
|
|
|
if offset > p.config.MaximumDesync {
|
|
|
|
return errors.New("the system clock is not synchronized accurately enough with the configured NTP server")
|
|
|
|
}
|
2021-09-17 04:44:35 +00:00
|
|
|
|
2023-05-07 06:39:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-09-17 09:53:59 +00:00
|
|
|
|
2023-05-07 06:39:17 +00:00
|
|
|
// GetOffset returns the current offset for this provider.
|
|
|
|
func (p *Provider) GetOffset() (offset time.Duration, err error) {
|
|
|
|
var conn net.Conn
|
|
|
|
|
|
|
|
if conn, err = net.Dial(p.config.Address.Network(), p.config.Address.NetworkAddress()); err != nil {
|
|
|
|
return offset, fmt.Errorf("error occurred during dial: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if closeErr := conn.Close(); closeErr != nil {
|
|
|
|
p.log.WithError(err).Error("Error occurred closing connection with NTP sever")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err = conn.SetDeadline(time.Now().Add(5 * time.Second)); err != nil {
|
|
|
|
return offset, fmt.Errorf("error occurred setting connection deadline: %w", err)
|
2021-09-17 04:44:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
version := ntpV4
|
|
|
|
if p.config.Version == 3 {
|
|
|
|
version = ntpV3
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:54:17 +00:00
|
|
|
req := &ntpPacket{LeapVersionMode: ntpLeapVersionClientMode(version)}
|
2021-09-17 04:44:35 +00:00
|
|
|
|
2023-05-07 06:39:17 +00:00
|
|
|
if err = binary.Write(conn, binary.BigEndian, req); err != nil {
|
|
|
|
return offset, fmt.Errorf("error occurred writing ntp packet request to the connection: %w", err)
|
2021-09-17 04:44:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
resp := &ntpPacket{}
|
|
|
|
|
2023-05-07 06:39:17 +00:00
|
|
|
if err = binary.Read(conn, binary.BigEndian, resp); err != nil {
|
|
|
|
return offset, fmt.Errorf("error occurred reading ntp packet response to the connection: %w", err)
|
2021-09-17 04:44:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ntpTime := ntpPacketToTime(resp)
|
|
|
|
|
2023-05-07 06:39:17 +00:00
|
|
|
return calcOffset(now, ntpTime), nil
|
2021-09-17 04:44:35 +00:00
|
|
|
}
|