283 lines
13 KiB
C#
283 lines
13 KiB
C#
// /**
|
|
// * File: RadiusAttribute.cs
|
|
// * Author: haraldwolff
|
|
// *
|
|
// * This file and it's content is copyrighted by the Author and / or copyright holder.
|
|
// * Any use wihtout proper permission is illegal and may lead to legal actions.
|
|
// *
|
|
// *
|
|
// **/
|
|
using System;
|
|
using System.IO;
|
|
using ln.types;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using ln.types.net;
|
|
namespace ln.radius
|
|
{
|
|
public delegate RadiusAttribute RadiusAttributeFactory(byte attributeType);
|
|
|
|
public class RadiusAttribute
|
|
{
|
|
public byte Type { get; private set; }
|
|
public string Name => AttributeName(Type);
|
|
|
|
public virtual byte[] Bytes { get; set; }
|
|
public String AsText
|
|
{
|
|
get => Encoding.UTF8.GetString(Bytes);
|
|
set => Bytes = Encoding.UTF8.GetBytes(value);
|
|
}
|
|
|
|
public RadiusAttribute(byte type)
|
|
{
|
|
Type = type;
|
|
}
|
|
|
|
public byte[] ToBytes()
|
|
{
|
|
byte[] vBytes = Bytes;
|
|
byte[] bytes = new byte[vBytes.Length + 2];
|
|
bytes[0] = Type;
|
|
bytes[1] = (byte)bytes.Length;
|
|
Array.Copy(vBytes, 0, bytes, 2, vBytes.Length);
|
|
return bytes;
|
|
}
|
|
|
|
public RadiusAttribute SetBytes(byte[] bytes)
|
|
{
|
|
this.Bytes = bytes;
|
|
return this;
|
|
}
|
|
|
|
public RadiusAttribute SetText(string text)
|
|
{
|
|
this.AsText = text;
|
|
return this;
|
|
}
|
|
|
|
|
|
|
|
public static RadiusAttribute Read(Stream stream)
|
|
{
|
|
byte atype = (byte)stream.ReadByte();
|
|
byte alength = (byte)stream.ReadByte();
|
|
byte[] abytes = stream.ReadBytes(alength - 2);
|
|
|
|
RadiusAttribute radiusAttribute = RadiusAttribute.Create(atype);
|
|
radiusAttribute.Bytes = abytes;
|
|
|
|
return radiusAttribute;
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
foreach (byte by in Bytes)
|
|
{
|
|
if (by < 0x20)
|
|
return String.Format("[RadiusAttribute Type={0} {1}]", Name, BitConverter.ToString(Bytes));
|
|
}
|
|
return String.Format("[RadiusAttribute Type={0} {1}]", Name, Encoding.ASCII.GetString(Bytes));
|
|
}
|
|
|
|
static internal Dictionary<byte, string> attributeNames = new Dictionary<byte, string>();
|
|
static internal Dictionary<byte, RadiusAttributeFactory> attributeFactories = new Dictionary<byte, RadiusAttributeFactory>();
|
|
public static RadiusAttribute Create(byte attributeType)
|
|
{
|
|
if (attributeFactories.ContainsKey(attributeType))
|
|
return attributeFactories[attributeType](attributeType);
|
|
|
|
return new RadiusAttribute(attributeType);
|
|
}
|
|
public static String AttributeName(byte type)
|
|
{
|
|
if (attributeNames.ContainsKey(type))
|
|
return attributeNames[type];
|
|
return type.ToString();
|
|
}
|
|
|
|
public static void RegisterAttributeFactory(byte attributeType, string attributeName, RadiusAttributeFactory factory)
|
|
{
|
|
attributeFactories.Add(attributeType, factory);
|
|
attributeNames.Add(attributeType, attributeName);
|
|
}
|
|
|
|
static RadiusAttribute()
|
|
{
|
|
RegisterAttributeFactory(0x01, "User-Name", (type) => new UserName());
|
|
RegisterAttributeFactory(0x02, "User-Password", (type) => new UserPassword());
|
|
RegisterAttributeFactory(0x03, "CHAP-Password", (type) => new ChapPassword());
|
|
RegisterAttributeFactory(0x04, "NAS-IP-Address", (type) => new NASIPAddress());
|
|
RegisterAttributeFactory(0x05, "NAS-Port", (type) => new NASPort());
|
|
RegisterAttributeFactory(0x06, "Service-Type", (type) => new ServiceType());
|
|
RegisterAttributeFactory(0x07, "Framed-Protocol", (type) => new FramedProtocol());
|
|
RegisterAttributeFactory(0x08, "Framed-IP-Address", (type) => new FramedIPAddress());
|
|
RegisterAttributeFactory(0x09, "Framed-IP-Netmask", (type) => new FramedIPNetmask());
|
|
RegisterAttributeFactory(0x0A, "Framed-Routing", (type) => new FramedRouting());
|
|
RegisterAttributeFactory(0x0B, "Filter-Id", (type) => new FilterID());
|
|
RegisterAttributeFactory(0x0C, "Framed-MTU", (type) => new FramedMTU());
|
|
RegisterAttributeFactory(0x0D, "Framed-Compression", (type) => new FramedCompression());
|
|
RegisterAttributeFactory(0x0E, "Login-IP-Host", (type) => new LoginIPHost());
|
|
RegisterAttributeFactory(0x0F, "Login-Service", (type) => new LoginService());
|
|
|
|
RegisterAttributeFactory(0x10, "Login-TCP-Port", (type) => new LoginTCPPort());
|
|
//RegisterAttributeFactory(0x11, "", (type) => new RadiusAttribute(type)); // Reserved
|
|
RegisterAttributeFactory(0x12, "Reply-Message", (type) => new ReplyMessage());
|
|
RegisterAttributeFactory(0x13, "Callback-Number", (type) => new CallbackNumber());
|
|
RegisterAttributeFactory(0x14, "Callback-Id", (type) => new CallbackID());
|
|
//RegisterAttributeFactory(0x15, "", (type) => new RadiusAttribute(type)); // Reserved
|
|
RegisterAttributeFactory(0x16, "Framed-Route", (type) => new FramedRoute());
|
|
RegisterAttributeFactory(0x17, "Framed-IPX-Network", (type) => new FramedIPXNetwork());
|
|
RegisterAttributeFactory(0x18, "State", (type) => new State());
|
|
RegisterAttributeFactory(0x19, "Class", (type) => new Class());
|
|
RegisterAttributeFactory(0x1A, "Vendor-Specific", (type) => new VendorSpecific());
|
|
RegisterAttributeFactory(0x1B, "Session-Timeout", (type) => new SessionTimeout());
|
|
RegisterAttributeFactory(0x1C, "Idle-Timeout", (type) => new IdleTimeout());
|
|
RegisterAttributeFactory(0x1D, "Termination-Action", (type) => new TerminationAction());
|
|
RegisterAttributeFactory(0x1E, "Called-Station-Id", (type) => new CalledStationID());
|
|
RegisterAttributeFactory(0x1F, "Calling-Station-Id", (type) => new CallingStationID());
|
|
|
|
RegisterAttributeFactory(0x20, "NAS-Identifier", (type) => new NASIdentifier());
|
|
RegisterAttributeFactory(0x21, "Proxy-State", (type) => new ProxyState());
|
|
RegisterAttributeFactory(0x22, "Login-LAT-Service", (type) => new LoginLATService());
|
|
RegisterAttributeFactory(0x23, "Login-LAT-Node", (type) => new LoginLATNode());
|
|
RegisterAttributeFactory(0x24, "Login-LAT-Group", (type) => new LoginLATGroup());
|
|
RegisterAttributeFactory(0x25, "Framed-AppleTalk-Link", (type) => new FramedAppleTalkLink());
|
|
RegisterAttributeFactory(0x26, "Framed-AppleTalk-Network", (type) => new FramedAppleTalkNetwork());
|
|
RegisterAttributeFactory(0x27, "Framed-AppleTalk-Zone", (type) => new FramedAppleTalkZone());
|
|
|
|
RegisterAttributeFactory(0x3C, "CHAP-Challenge", (type) => new ChapChallenge());
|
|
RegisterAttributeFactory(0x3D, "NAS-Port-Type", (type) => new NASPortType());
|
|
RegisterAttributeFactory(0x3E, "Port-Limit", (type) => new PortLimit());
|
|
RegisterAttributeFactory(0x3F, "Login-LAT-Port", (type) => new LoginLATPort());
|
|
}
|
|
|
|
public class TextAttribute : RadiusAttribute
|
|
{
|
|
public TextAttribute(byte type)
|
|
: base(type)
|
|
{ }
|
|
|
|
public override string ToString()
|
|
{
|
|
return string.Format("[RadiusAttribute Type={0} {1}]", Name, AsText);
|
|
}
|
|
}
|
|
public class IPv4Attribute : RadiusAttribute
|
|
{
|
|
public IPv4 IP { get; set; }
|
|
public override byte[] Bytes
|
|
{
|
|
get => IP.IPBytes;
|
|
set => IP = new IPv4(value);
|
|
}
|
|
|
|
public IPv4Attribute(byte type)
|
|
: base(type)
|
|
{ }
|
|
|
|
public override string ToString()
|
|
{
|
|
return string.Format("[RadiusAttribute Type={0} {1}]", Name, IP.ToString());
|
|
}
|
|
|
|
public IPv4Attribute SetIP(IPv4 ip)
|
|
{
|
|
this.IP = ip;
|
|
return this;
|
|
}
|
|
|
|
}
|
|
|
|
public class IntAttribute : RadiusAttribute
|
|
{
|
|
public UInt32 UInt32 { get; set; }
|
|
public override byte[] Bytes
|
|
{
|
|
get => UInt32.GetBytes(true);
|
|
set => UInt32 = BitConverter.ToUInt32(value.BigEndian(), 0);
|
|
}
|
|
|
|
public IntAttribute(byte type)
|
|
: base(type)
|
|
{ }
|
|
|
|
public override string ToString()
|
|
{
|
|
return string.Format("[RadiusAttribute Type={0} {1}]", Name, UInt32);
|
|
}
|
|
|
|
public IntAttribute SetUInt(uint value)
|
|
{
|
|
this.UInt32 = value;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
public class TimeAttribute : RadiusAttribute
|
|
{
|
|
DateTimeOffset DateTimeOffset { get; set; }
|
|
public override byte[] Bytes
|
|
{
|
|
get => ((uint)DateTimeOffset.ToUnixTimeSeconds()).GetBytes(true);
|
|
set => DateTimeOffset = DateTimeOffset.FromUnixTimeSeconds(BitConverter.ToUInt32(value.BigEndian(), 0));
|
|
}
|
|
|
|
public TimeAttribute(byte type)
|
|
: base(type)
|
|
{ }
|
|
|
|
public override string ToString()
|
|
{
|
|
return string.Format("[RadiusAttribute Type={0} {1}]", Name, DateTimeOffset);
|
|
}
|
|
}
|
|
|
|
|
|
/* */
|
|
public class UserName : RadiusAttribute { public UserName() : base(0x01) { } }
|
|
public class UserPassword : RadiusAttribute { public UserPassword() : base(0x02) { } }
|
|
public class ChapPassword : RadiusAttribute { public ChapPassword() : base(0x03) { } }
|
|
public class NASIPAddress : IPv4Attribute { public NASIPAddress() : base(0x04) { } }
|
|
public class NASPort : IntAttribute { public NASPort() : base(0x05) { } }
|
|
public class ServiceType : IntAttribute { public ServiceType() : base(0x06) { } }
|
|
public class FramedProtocol : IntAttribute { public FramedProtocol() : base(0x07) { } }
|
|
public class FramedIPAddress : IPv4Attribute { public FramedIPAddress() : base(0x08) { } }
|
|
public class FramedIPNetmask : IPv4Attribute { public FramedIPNetmask() : base(0x09) { } }
|
|
public class FramedRouting : IntAttribute { public FramedRouting() : base(0x0a) { } }
|
|
public class FilterID : TextAttribute { public FilterID() : base(0x0b) { } }
|
|
public class FramedMTU : IntAttribute { public FramedMTU() : base(0x0c) { } }
|
|
public class FramedCompression : IntAttribute { public FramedCompression() : base(0x0d) { } }
|
|
public class LoginIPHost : IPv4Attribute { public LoginIPHost() : base(0x0e) { } }
|
|
public class LoginService : IntAttribute { public LoginService() : base(0x0f) { } }
|
|
|
|
public class LoginTCPPort : IntAttribute { public LoginTCPPort() : base(0x10) { } }
|
|
public class ReplyMessage : TextAttribute { public ReplyMessage() : base(0x12) { } }
|
|
public class CallbackNumber : RadiusAttribute { public CallbackNumber() : base(0x13) { } }
|
|
public class CallbackID : RadiusAttribute { public CallbackID() : base(0x14) { } }
|
|
public class FramedRoute : TextAttribute { public FramedRoute() : base(0x16) { } }
|
|
public class FramedIPXNetwork : IntAttribute { public FramedIPXNetwork() : base(0x17) { } }
|
|
public class State : RadiusAttribute { public State() : base(0x18) { } }
|
|
public class Class : RadiusAttribute { public Class() : base(0x19) { } }
|
|
public class VendorSpecific : RadiusAttribute { public VendorSpecific() : base(0x1A) { } }
|
|
public class SessionTimeout : IntAttribute { public SessionTimeout() : base(0x1B) { } }
|
|
public class IdleTimeout : IntAttribute { public IdleTimeout() : base(0x1C) { } }
|
|
public class TerminationAction : IntAttribute { public TerminationAction() : base(0x1D) { } }
|
|
public class CalledStationID : RadiusAttribute { public CalledStationID() : base(0x1E) { } }
|
|
public class CallingStationID : RadiusAttribute { public CallingStationID() : base(0x1F) { } }
|
|
public class NASIdentifier : RadiusAttribute { public NASIdentifier() : base(0x20) { } }
|
|
public class ProxyState : RadiusAttribute { public ProxyState() : base(0x21) { } }
|
|
public class LoginLATService : RadiusAttribute { public LoginLATService() : base(0x22) { } }
|
|
public class LoginLATNode : RadiusAttribute { public LoginLATNode() : base(0x23) { } }
|
|
public class LoginLATGroup : RadiusAttribute { public LoginLATGroup() : base(0x24) { } }
|
|
public class FramedAppleTalkLink : IntAttribute { public FramedAppleTalkLink() : base(0x25) { } }
|
|
public class FramedAppleTalkNetwork : IntAttribute { public FramedAppleTalkNetwork() : base(0x26) { } }
|
|
public class FramedAppleTalkZone : RadiusAttribute { public FramedAppleTalkZone() : base(0x27) { } }
|
|
public class ChapChallenge : RadiusAttribute { public ChapChallenge() : base(0x3c) { } }
|
|
public class NASPortType : RadiusAttribute { public NASPortType() : base(0x3d) { } }
|
|
public class PortLimit : RadiusAttribute { public PortLimit() : base(0x3e) { } }
|
|
public class LoginLATPort : RadiusAttribute { public LoginLATPort() : base(0x3f) { } }
|
|
|
|
}
|
|
}
|