radius

package module
v0.0.0-...-afd8bae Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 16, 2019 License: MIT Imports: 13 Imported by: 14

README

Radius

Build Status GoDoc docs examples Total views GitHub issues GitHub stars GitHub forks MIT License

A golang radius library. This project forks from jeesta/radius

Documentation
Example
package main

import (
	"fmt"
	"github.com/bronze1man/radius"
)

type radiusService struct{}

func (p radiusService) RadiusHandle(request *radius.Packet) *radius.Packet {
    // a pretty print of the request.
	fmt.Printf("[Authenticate] %s\n", request.String())
	npac := request.Reply()
	switch request.Code {
	case radius.AccessRequest:
		// check username and password
		if request.GetUsername() == "a" && request.GetPassword() == "a" {
			npac.Code = radius.AccessAccept
			// add Vendor-specific attribute - Vendor Cisco (code 9) Attribute h323-remote-address (code 23)
			npac.AddVSA( radius.VSA{Vendor: 9, Type: 23, Value: []byte("10.20.30.40")} )
		} else {
			npac.Code = radius.AccessReject
			npac.AddAVP( radius.AVP{Type: radius.ReplyMessage, Value: []byte("you dick!")} )
		}
	case radius.AccountingRequest:
		// accounting start or end
		npac.Code = radius.AccountingResponse
	default:
		npac.Code = radius.AccessAccept
	}
	return npac
}

func main() {
	s := radius.NewServer(":1812", "secret", radiusService{})

	// or you can convert it to a server that accept request
	// from some host with different secret
	// cls := radius.NewClientList([]radius.Client{
	// 		radius.NewClient("127.0.0.1", "secret1"),
	// 		radius.NewClient("10.10.10.10", "secret2"),
	// })
	// s.WithClientList(cls)

	signalChan := make(chan os.Signal, 1)
	signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
	errChan := make(chan error)
	go func() {
		fmt.Println("waiting for packets...")
		err := s.ListenAndServe()
		if err != nil {
			errChan <- err
		}
	}()
	select {
	case <-signalChan:
		log.Println("stopping server...")
		s.Stop()
	case err := <-errChan:
		log.Println("[ERR] %v", err.Error())
	}
}
Implemented
  • A radius server can handle AccessRequest request from strongswan with ikev1-xauth-psk
  • A radius server can handle AccountingRequest request from strongswan with ikev1-xauth-psk
Notice
  • A radius client has not yet been implement.
  • It works , but it is not stable.
Reference
TODO
  • avpEapMessaget.Value error handle.
  • Implement eap-MSCHAPV2 server side.
  • Implement radius client side.

Documentation

Index

Constants

View Source
const ACCOUNTING_PORT = 1813
View Source
const AUTH_PORT = 1812

Variables

View Source
var ErrMessageAuthenticatorCheckFail = fmt.Errorf("RADIUS Response-Authenticator verification failed")

Functions

This section is empty.

Types

type AVP

type AVP struct {
	Type  AttributeType
	Value []byte
}

func (AVP) Copy

func (a AVP) Copy() AVP

func (AVP) Decode

func (a AVP) Decode(p *Packet) interface{}

func (AVP) Encode

func (a AVP) Encode(b []byte) (n int, err error)

func (AVP) String

func (a AVP) String() string

func (AVP) StringWithPacket

func (a AVP) StringWithPacket(p *Packet) string

type AcctStatusTypeEnum

type AcctStatusTypeEnum uint32
const (
	AcctStatusTypeEnumStart         AcctStatusTypeEnum = 1
	AcctStatusTypeEnumStop          AcctStatusTypeEnum = 2
	AcctStatusTypeEnumInterimUpdate AcctStatusTypeEnum = 3
	AcctStatusTypeEnumAccountingOn  AcctStatusTypeEnum = 7
	AcctStatusTypeEnumAccountingOff AcctStatusTypeEnum = 8
)

func (AcctStatusTypeEnum) String

func (e AcctStatusTypeEnum) String() string

type AcctTerminateCauseEnum

type AcctTerminateCauseEnum uint32
const (
	AcctTerminateCauseEnumUserRequest        AcctTerminateCauseEnum = 1
	AcctTerminateCauseEnumLostCarrier        AcctTerminateCauseEnum = 2
	AcctTerminateCauseEnumLostService        AcctTerminateCauseEnum = 3
	AcctTerminateCauseEnumIdleTimeout        AcctTerminateCauseEnum = 4
	AcctTerminateCauseEnumSessionTimout      AcctTerminateCauseEnum = 5
	AcctTerminateCauseEnumAdminReset         AcctTerminateCauseEnum = 6
	AcctTerminateCauseEnumAdminReboot        AcctTerminateCauseEnum = 7
	AcctTerminateCauseEnumPortError          AcctTerminateCauseEnum = 8
	AcctTerminateCauseEnumNASError           AcctTerminateCauseEnum = 9
	AcctTerminateCauseEnumNASRequest         AcctTerminateCauseEnum = 10
	AcctTerminateCauseEnumNASReboot          AcctTerminateCauseEnum = 11
	AcctTerminateCauseEnumPortUnneeded       AcctTerminateCauseEnum = 12
	AcctTerminateCauseEnumPortPreempted      AcctTerminateCauseEnum = 13
	AcctTerminateCauseEnumPortSuspended      AcctTerminateCauseEnum = 14
	AcctTerminateCauseEnumServiceUnavailable AcctTerminateCauseEnum = 15
	AcctTerminateCauseEnumCallbkack          AcctTerminateCauseEnum = 16
	AcctTerminateCauseEnumUserError          AcctTerminateCauseEnum = 17
	AcctTerminateCauseEnumHostRequest        AcctTerminateCauseEnum = 18
)

func (AcctTerminateCauseEnum) String

func (e AcctTerminateCauseEnum) String() string

type AttributeType

type AttributeType uint8
const (
	UserName          AttributeType = iota //1
	UserPassword      AttributeType = iota //2
	CHAPPassword      AttributeType = iota //3
	NASIPAddress      AttributeType = iota //4
	NASPort           AttributeType = iota //5
	ServiceType       AttributeType = iota //6
	FramedProtocol    AttributeType = iota //7
	FramedIPAddress   AttributeType = iota //8
	FramedIPNetmask   AttributeType = iota //9
	FramedRouting     AttributeType = iota //10
	FilterId          AttributeType = iota //11
	FramedMTU         AttributeType = iota //12
	FramedCompression AttributeType = iota //13
	LoginIPHost       AttributeType = iota //14
	LoginService      AttributeType = iota //15
	LoginTCPPort      AttributeType = iota //16

	ReplyMessage   AttributeType = iota //18
	CallbackNumber AttributeType = iota //19
	CallbackId     AttributeType = iota //20

	FramedRoute            AttributeType = iota //22
	FramedIPXNetwork       AttributeType = iota //23
	State                  AttributeType = iota //24
	Class                  AttributeType = iota //25
	VendorSpecific         AttributeType = iota
	SessionTimeout         AttributeType = iota
	IdleTimeout            AttributeType = iota
	TerminationAction      AttributeType = iota
	CalledStationId        AttributeType = iota
	CallingStationId       AttributeType = iota
	NASIdentifier          AttributeType = iota
	ProxyState             AttributeType = iota
	LoginLATService        AttributeType = iota
	LoginLATNode           AttributeType = iota
	LoginLATGroup          AttributeType = iota
	FramedAppleTalkLink    AttributeType = iota
	FramedAppleTalkNetwork AttributeType = iota
	FramedAppleTalkZone    AttributeType = iota
	AcctStatusType         AttributeType = iota
	AcctDelayTime          AttributeType = iota
	AcctInputOctets        AttributeType = iota
	AcctOutputOctets       AttributeType = iota
	AcctSessionId          AttributeType = iota
	AcctAuthentic          AttributeType = iota
	AcctSessionTime        AttributeType = iota
	AcctInputPackets       AttributeType = iota
	AcctOutputPackets      AttributeType = iota
	AcctTerminateCause     AttributeType = iota
	AcctMultiSessionId     AttributeType = iota
	AcctLinkCount          AttributeType = iota
	AcctInputGigawords     AttributeType = iota //52
	AcctOutputGigawords    AttributeType = iota
	Unassigned1            AttributeType = iota
	EventTimestamp         AttributeType = iota
	EgressVLANID           AttributeType = iota
	IngressFilters         AttributeType = iota
	EgressVLANName         AttributeType = iota
	UserPriorityTable      AttributeType = iota //59
	CHAPChallenge          AttributeType = 60
	NASPortType            AttributeType = 61
	PortLimit              AttributeType = 62
	LoginLATPort           AttributeType = 63
	//end rfc2865 rfc 2866
	TunnelType                   AttributeType = iota
	TunnelMediumType             AttributeType = iota
	TunnelClientEndpoint         AttributeType = iota
	TunnelServerEndpoint         AttributeType = iota
	AcctTunnelConnection         AttributeType = iota
	TunnelPassword               AttributeType = iota
	ARAPPassword                 AttributeType = iota
	ARAPFeatures                 AttributeType = iota
	ARAPZoneAccess               AttributeType = iota
	ARAPSecurity                 AttributeType = iota
	ARAPSecurityData             AttributeType = iota
	PasswordRetry                AttributeType = iota
	Prompt                       AttributeType = iota
	ConnectInfo                  AttributeType = iota
	ConfigurationToken           AttributeType = iota
	EAPMessage                   AttributeType = iota
	MessageAuthenticator         AttributeType = iota
	TunnelPrivateGroupID         AttributeType = iota
	TunnelAssignmentID           AttributeType = iota
	TunnelPreference             AttributeType = iota
	ARAPChallengeResponse        AttributeType = iota
	AcctInterimInterval          AttributeType = iota
	AcctTunnelPacketsLost        AttributeType = iota
	NASPortId                    AttributeType = iota
	FramedPool                   AttributeType = iota
	CUI                          AttributeType = iota
	TunnelClientAuthID           AttributeType = iota
	TunnelServerAuthID           AttributeType = iota
	NASFilterRule                AttributeType = iota
	Unassigned                   AttributeType = iota
	OriginatingLineInfo          AttributeType = iota
	NASIPv6Address               AttributeType = iota
	FramedInterfaceId            AttributeType = iota
	FramedIPv6Prefix             AttributeType = iota
	LoginIPv6Host                AttributeType = iota
	FramedIPv6Route              AttributeType = iota
	FramedIPv6Pool               AttributeType = iota
	ErrorCause                   AttributeType = iota
	EAPKeyName                   AttributeType = iota
	DigestResponse               AttributeType = iota
	DigestRealm                  AttributeType = iota
	DigestNonce                  AttributeType = iota
	DigestResponseAuth           AttributeType = iota
	DigestNextnonce              AttributeType = iota
	DigestMethod                 AttributeType = iota
	DigestURI                    AttributeType = iota
	DigestQop                    AttributeType = iota
	DigestAlgorithm              AttributeType = iota
	DigestEntityBodyHash         AttributeType = iota
	DigestCNonce                 AttributeType = iota
	DigestNonceCount             AttributeType = iota
	DigestUsername               AttributeType = iota
	DigestOpaque                 AttributeType = iota
	DigestAuthParam              AttributeType = iota
	DigestAKAAuts                AttributeType = iota
	DigestDomain                 AttributeType = iota
	DigestStale                  AttributeType = iota
	DigestHA1                    AttributeType = iota
	SIPAOR                       AttributeType = iota
	DelegatedIPv6Prefix          AttributeType = iota
	MIP6FeatureVector            AttributeType = iota
	MIP6HomeLinkPrefix           AttributeType = iota
	OperatorName                 AttributeType = iota
	LocationInformation          AttributeType = iota
	LocationData                 AttributeType = iota
	BasicLocationPolicyRules     AttributeType = iota
	ExtendedLocationPolicyRules  AttributeType = iota
	LocationCapable              AttributeType = iota
	RequestedLocationInfo        AttributeType = iota
	FramedManagementProtocol     AttributeType = iota
	ManagementTransportProtectio AttributeType = iota
	ManagementPolicyId           AttributeType = iota
	ManagementPrivilegeLevel     AttributeType = iota
	PKMSSCert                    AttributeType = iota
	PKMCACert                    AttributeType = iota
	PKMConfigSettings            AttributeType = iota
	PKMCryptosuiteList           AttributeType = iota
	PKMSAID                      AttributeType = iota
	PKMSADescriptor              AttributeType = iota
	PKMAuthKey                   AttributeType = iota
	DSLiteTunnelName             AttributeType = iota
	MobileNodeIdentifier         AttributeType = iota
	ServiceSelection             AttributeType = iota
	PMIP6HomeLMAIPv6Address      AttributeType = iota
	PMIP6VisitedLMAIPv6Address   AttributeType = iota
	PMIP6HomeLMAIPv4Address      AttributeType = iota
	PMIP6VisitedLMAIPv4Address   AttributeType = iota
	PMIP6HomeHNPrefix            AttributeType = iota
	PMIP6VisitedHNPrefix         AttributeType = iota
	PMIP6HomeInterfaceID         AttributeType = iota
	PMIP6VisitedInterfaceID      AttributeType = iota
	PMIP6HomeIPv4HoA             AttributeType = iota
	PMIP6VisitedIPv4HoA          AttributeType = iota
	PMIP6HomeDHCP4ServerAddress  AttributeType = iota
	PMIP6VisitedDHCP4ServerAddre AttributeType = iota
	PMIP6HomeDHCP6ServerAddress  AttributeType = iota
	PMIP6VisitedDHCP6ServerAddre AttributeType = iota
	UnassignedStart              AttributeType = 161
	UnassignedEnd                AttributeType = 191

	ExperimentalStart           AttributeType = 192
	ExperimentalEnd             AttributeType = 223
	ImplementationSpecificStart AttributeType = 224
	ImplementationSpecificEnd   AttributeType = 240
	ReservedStart               AttributeType = 241
	ReservedEnd                 AttributeType = 254
)

func (AttributeType) String

func (a AttributeType) String() string

type Client

type Client interface {
	// GetHost get the client host
	GetHost() string
	// GetSecret get shared secret
	GetSecret() string
}

Client represent a client to connect to radius server

func NewClient

func NewClient(host, secret string) Client

NewClient return new client

type ClientList

type ClientList struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

ClientList are list of client allowed to communicate with server

func NewClientList

func NewClientList(cs []Client) *ClientList

func (*ClientList) AddOrUpdate

func (cls *ClientList) AddOrUpdate(cl Client)

Add new client or reset existing client based on host

func (*ClientList) Get

func (cls *ClientList) Get(host string) Client

Get client from list of clients based on host

func (*ClientList) GetHerd

func (cls *ClientList) GetHerd() []Client

func (*ClientList) Remove

func (cls *ClientList) Remove(host string)

Remove client based on host

func (*ClientList) SetHerd

func (cls *ClientList) SetHerd(herd []Client)

SetHerd reset/initialize the herd of clients

type DefaultClient

type DefaultClient struct {
	Host   string
	Secret string
}

DefaultClient is default client implementation

func (*DefaultClient) GetHost

func (cl *DefaultClient) GetHost() string

GetHost get the client host

func (*DefaultClient) GetSecret

func (cl *DefaultClient) GetSecret() string

GetSecret get shared secret

type EapCode

type EapCode uint8
const (
	EapCodeRequest  EapCode = 1
	EapCodeResponse EapCode = 2
	EapCodeSuccess  EapCode = 3
	EapCodeFailure  EapCode = 4
)

func (EapCode) String

func (c EapCode) String() string

type EapPacket

type EapPacket struct {
	Code       EapCode
	Identifier uint8
	Type       EapType
	Data       []byte
}

func EapDecode

func EapDecode(b []byte) (eap *EapPacket, err error)

func (*EapPacket) Copy

func (a *EapPacket) Copy() *EapPacket

func (*EapPacket) Encode

func (a *EapPacket) Encode() (b []byte)

func (*EapPacket) String

func (a *EapPacket) String() string

func (*EapPacket) ToEAPMessage

func (a *EapPacket) ToEAPMessage() *AVP

type EapType

type EapType uint8
const (
	EapTypeIdentity         EapType = 1
	EapTypeNotification     EapType = 2
	EapTypeNak              EapType = 3 //Response only
	EapTypeMd5Challenge     EapType = 4
	EapTypeOneTimePassword  EapType = 5 //otp
	EapTypeGenericTokenCard EapType = 6 //gtc
	EapTypeMSCHAPV2         EapType = 26
	EapTypeExpandedTypes    EapType = 254
	EapTypeExperimentalUse  EapType = 255
)

func (EapType) String

func (c EapType) String() string

type MsChapV2OpCode

type MsChapV2OpCode uint8
const (
	MsChapV2OpCodeChallenge      MsChapV2OpCode = 1
	MsChapV2OpCodeResponse       MsChapV2OpCode = 2
	MsChapV2OpCodeSuccess        MsChapV2OpCode = 3
	MsChapV2OpCodeFailure        MsChapV2OpCode = 4
	MsChapV2OpCodeChangePassword MsChapV2OpCode = 7
)

func (MsChapV2OpCode) String

func (c MsChapV2OpCode) String() string

type MsChapV2Packet

type MsChapV2Packet struct {
	Eap    *EapPacket //解密的时候的eap信息,不使用里面的data
	OpCode MsChapV2OpCode
	Data   []byte
}

func MsChapV2PacketFromEap

func MsChapV2PacketFromEap(eap *EapPacket) (p *MsChapV2Packet, err error)

func (*MsChapV2Packet) String

func (p *MsChapV2Packet) String() string

不包括eap的信息

func (*MsChapV2Packet) ToEap

func (p *MsChapV2Packet) ToEap() *EapPacket

type NASPortTypeEnum

type NASPortTypeEnum uint32
const (
	NASPortTypeEnumAsync            NASPortTypeEnum = 0
	NASPortTypeEnumSync             NASPortTypeEnum = 1
	NASPortTypeEnumISDNSync         NASPortTypeEnum = 2
	NASPortTypeEnumISDNSyncV120     NASPortTypeEnum = 3
	NASPortTypeEnumISDNSyncV110     NASPortTypeEnum = 4
	NASPortTypeEnumVirtual          NASPortTypeEnum = 5
	NASPortTypeEnumPIAFS            NASPortTypeEnum = 6
	NASPortTypeEnumHDLCClearChannel NASPortTypeEnum = 7
	NASPortTypeEnumEthernet         NASPortTypeEnum = 15
	NASPortTypeEnumCable            NASPortTypeEnum = 17
)

TODO finish it

func (NASPortTypeEnum) String

func (e NASPortTypeEnum) String() string

type Packet

type Packet struct {
	Secret        string
	Code          PacketCode
	Identifier    uint8
	Authenticator [16]byte
	AVPs          []AVP
	ClientAddr    string
}

func DecodePacket

func DecodePacket(Secret string, buf []byte) (p *Packet, err error)

func (*Packet) AddAVP

func (p *Packet) AddAVP(avp AVP)

func (*Packet) AddVSA

func (p *Packet) AddVSA(vsa VSA)

func (*Packet) Copy

func (p *Packet) Copy() *Packet

func (*Packet) DeleteAVP

func (p *Packet) DeleteAVP(avp *AVP)

删除一个AVP Delete a AVP

func (*Packet) DeleteOneType

func (p *Packet) DeleteOneType(attrType AttributeType)

delete all avps with this type

func (*Packet) Encode

func (p *Packet) Encode() (b []byte, err error)

此方法保证不修改包的内容 This method does not modify the contents of the package to ensure

func (*Packet) GetAVP

func (p *Packet) GetAVP(attrType AttributeType) *AVP

get one avp

func (*Packet) GetAcctSessionId

func (p *Packet) GetAcctSessionId() string

func (*Packet) GetAcctStatusType

func (p *Packet) GetAcctStatusType() AcctStatusTypeEnum

func (*Packet) GetAcctTotalInputOctets

func (p *Packet) GetAcctTotalInputOctets() uint64

func (*Packet) GetAcctTotalOutputOctets

func (p *Packet) GetAcctTotalOutputOctets() uint64

func (*Packet) GetCalledStationId

func (p *Packet) GetCalledStationId() string

func (*Packet) GetCallingStationId

func (p *Packet) GetCallingStationId() string

func (*Packet) GetEAPMessage

func (p *Packet) GetEAPMessage() *EapPacket

func (*Packet) GetFramedIPAddress

func (p *Packet) GetFramedIPAddress() (ip net.IP)

func (*Packet) GetNASIdentifier

func (p *Packet) GetNASIdentifier() string

func (*Packet) GetNASPort

func (p *Packet) GetNASPort() uint32

it is ike_id in strongswan client

func (*Packet) GetNASPortId

func (p *Packet) GetNASPortId() string

func (*Packet) GetNasIpAddress

func (p *Packet) GetNasIpAddress() (ip net.IP)

func (*Packet) GetPassword

func (p *Packet) GetPassword() (password string)

func (*Packet) GetTerminateCause

func (p *Packet) GetTerminateCause() string

func (*Packet) GetUsername

func (p *Packet) GetUsername() (username string)

func (*Packet) GetVSA

func (p *Packet) GetVSA(vendor uint32, attr uint8) *VSA

get one vsa

func (*Packet) HasAVP

func (p *Packet) HasAVP(attrType AttributeType) bool

func (*Packet) Reply

func (p *Packet) Reply() *Packet

func (*Packet) Send

func (p *Packet) Send(c net.PacketConn, addr net.Addr) error

func (*Packet) SetAVP

func (p *Packet) SetAVP(avp AVP)

set one avp,remove all other same type

func (*Packet) String

func (p *Packet) String() string

type PacketCode

type PacketCode uint8
const (
	AccessRequest      PacketCode = 1
	AccessAccept       PacketCode = 2
	AccessReject       PacketCode = 3
	AccountingRequest  PacketCode = 4
	AccountingResponse PacketCode = 5
	AccessChallenge    PacketCode = 11
	StatusServer       PacketCode = 12 //(experimental)
	StatusClient       PacketCode = 13 //(experimental)
	DisconnectRequest  PacketCode = 40
	DisconnectAccept   PacketCode = 41
	DisconnectReject   PacketCode = 42
	CoARequest         PacketCode = 43
	CoAAccept          PacketCode = 44
	CoaReject          PacketCode = 45
	Reserved           PacketCode = 255
)

func (PacketCode) String

func (p PacketCode) String() string

type PasswordService

type PasswordService struct{}

func (*PasswordService) Authenticate

func (p *PasswordService) Authenticate(request *Packet) (*Packet, error)

type Server

type Server struct {
	// contains filtered or unexported fields
}

func NewServer

func NewServer(addr string, secret string, service Service) *Server

NewServer return a new Server given a addr, secret, and service

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

ListenAndServe listen on the UDP network address

func (*Server) Stop

func (s *Server) Stop()

Stop will stop the server

func (*Server) WithClientList

func (s *Server) WithClientList(cl *ClientList)

WithClientList set a list of clients that have it's own secret

type Service

type Service interface {
	RadiusHandle(request *Packet) *Packet
}

type ServiceTypeEnum

type ServiceTypeEnum uint32
const (
	ServiceTypeEnumLogin          ServiceTypeEnum = 1
	ServiceTypeEnumFramed         ServiceTypeEnum = 2
	ServiceTypeEnumCallbackLogin  ServiceTypeEnum = 3
	ServiceTypeEnumCallbackFramed ServiceTypeEnum = 4
	ServiceTypeEnumOutbound       ServiceTypeEnum = 5
)

TODO finish it

func (ServiceTypeEnum) String

func (e ServiceTypeEnum) String() string

type VSA

type VSA struct {
	Vendor uint32
	Type   uint8
	Value  []byte
}

Vendor

func (VSA) ToAVP

func (vsa VSA) ToAVP() AVP

encode VSA attribute under Vendor-Specific AVP

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL