Esempio n. 1
0
        static int Main(string[] args)
        {
            var username = Environment.GetEnvironmentVariable("username");
            var password = Environment.GetEnvironmentVariable("password");

            if (String.IsNullOrEmpty(username) || String.IsNullOrEmpty(password))
            {
                Console.WriteLine("environment variables username or password undefined");
                return 1;
            }

            if(Config.Settings == null)
            {
                Console.WriteLine("Config is empty/unreadable");
                return 1;
            }

            if (Config.Settings.Servers == null || Config.Settings.Servers.Count == 0)
            {
                Console.WriteLine("No servers found in config");
                return 1;
            }

            var res = Parallel.ForEach(Config.Settings.Servers.Cast<ServerElement>(), (server, state) =>
            {
                    // Console.WriteLine("server.name = {0}, sharedsecret={1}, retries={2}, wait={3}, authport={4}", server.Name, server.sharedsecret, server.retries, server.wait, server.authport);

                    var rc = new RadiusClient(server.Name, server.sharedsecret, server.wait * 1000, server.authport);
                    var authPacket = rc.Authenticate(username, password);

                    if (Config.Settings.NAS_IDENTIFIER != null)
                        authPacket.SetAttribute(new RadiusAttribute(RadiusAttributeType.NAS_IDENTIFIER, Encoding.ASCII.GetBytes(Config.Settings.NAS_IDENTIFIER)));

                    authPacket.SetAttribute(new RadiusAttribute(RadiusAttributeType.NAS_PORT_TYPE, BitConverter.GetBytes((int)NasPortType.ASYNC)));

                    for (int i = 0; i < server.retries; i++)
                        try
                        {
                            var receivedPacket = rc.SendAndReceivePacket(authPacket).Result;

                            if (receivedPacket != null && receivedPacket.PacketType == RadiusCode.ACCESS_ACCEPT)
                                state.Stop();

                        }
                        catch { }

            });

            if (res.IsCompleted)
            {
                Console.WriteLine("Auth failed");
                return 1;
            }
            else
            {
                Console.WriteLine("Auth Ok");
                return 0;
            }
        }
Esempio n. 2
0
        public async static Task Authentication(string[] args)
        {
            try
            {
                RadiusClient rc         = new RadiusClient(args[0], args[1]);
                RadiusPacket authPacket = rc.Authenticate(args[2], args[3]);
                authPacket.SetAttribute(new VendorSpecificAttribute(10135, 1, UTF8Encoding.UTF8.GetBytes("Testing")));
                authPacket.SetAttribute(new VendorSpecificAttribute(10135, 2, new[] { (byte)7 }));

                RadiusPacket receivedPacket = await rc.SendAndReceivePacket(authPacket);

                if (receivedPacket == null)
                {
                    throw new Exception("Can't contact remote radius server !");
                }

                switch (receivedPacket.PacketType)
                {
                case RadiusCode.ACCESS_ACCEPT:
                    Console.WriteLine("Access-Accept");
                    foreach (var attr in receivedPacket.Attributes)
                    {
                        Console.WriteLine(attr.Type.ToString() + " = " + attr.Value);
                    }
                    break;

                case RadiusCode.ACCESS_CHALLENGE:
                    Console.WriteLine("Access-Challenge");
                    break;

                case RadiusCode.ACCESS_REJECT:
                    Console.WriteLine("Access-Reject");
                    if (!rc.VerifyAuthenticator(authPacket, receivedPacket))
                    {
                        Console.WriteLine("Authenticator check failed: Check your secret");
                    }
                    break;

                default:
                    Console.WriteLine("Rejected");
                    break;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 3
0
		private async static Task Authenticate(string[] args)
		{
			RadiusClient rc = new RadiusClient(args[0], args[1]);
			RadiusPacket authPacket = rc.Authenticate(args[2], args[3]);
			authPacket.SetAttribute(new VendorSpecificAttribute(10135, 1, UTF8Encoding.UTF8.GetBytes("Testing")));
			authPacket.SetAttribute(new VendorSpecificAttribute(10135, 2, new[] { (byte)7 }));
			RadiusPacket receivedPacket = await rc.SendAndReceivePacket(authPacket);
			if (receivedPacket == null) throw new Exception("Can't contact remote radius server !");
			switch (receivedPacket.PacketType)
			{
				case RadiusCode.ACCESS_ACCEPT:
					Console.WriteLine("Accepted");
					foreach (var attr in receivedPacket.Attributes)
						Console.WriteLine(attr.Type.ToString() + " = " + attr.Value);
					break;
				case RadiusCode.ACCESS_CHALLENGE:
					Console.WriteLine("Challenged");
					break;
				default:
					Console.WriteLine("Rejected");
					break;
			}	
		}