Пример #1
0
        public Object Clone()
        {
            packet pkt = new packet(base.ToBytes());

            pkt.body = this.body;
            return(pkt);
        }
Пример #2
0
        private void pingTimerEvent(Object state)
        {
            if (communicating)
            {
                return;
            }

            try
            {
                envia_pacote(internalSock, null, 0, FWAUT_OP_PING, FWAUT_ST_OK);
                packet pkt = recebe_pacote(internalSock);

                if (OnPing != null)
                {
                    OnPing(pkt, null);
                }
            }
            catch (Exception ex)
            {
                if (OnPing != null)
                {
                    OnPing(null, ex);
                }
            }
        }
Пример #3
0
        public AuthTestStatus TesteUsuario(String username, String password)
        {
            try
            {
                communicating = true;

                AuthTestStatus retStatus = null;

                String[] grupos = new String[0];

                Byte[] data = Encoding.GetEncoding("iso-8859-1").GetBytes(username + "\0" + password + "\0\0");

                envia_pacote(internalSock, data, data.Length, FWAUT_OP_REQ, FWAUT_ST_OK);
                packet pkt = recebe_pacote(internalSock);

                switch (pkt.status)
                {
                case FWAUT_ST_BAD_PWD:
                    retStatus = new AuthTestStatus(pkt.status);
                    //throw new Exception("Senha inválida");
                    break;

                case FWAUT_ST_NO_USER:
                    retStatus = new AuthTestStatus(pkt.status);
                    //throw new Exception("Usuário inexistente");
                    break;

                case FWAUT_ST_OK:
                    String groups = Encoding.GetEncoding("iso-8859-1").GetString(pkt.body);

                    Int32 end = groups.IndexOf("\0\0");
                    if (end == -1)
                    {
                        end = groups.Length;
                    }

                    groups = groups.Substring(0, end);

                    grupos    = groups.Trim("\0".ToCharArray()).Split("\0".ToCharArray());
                    retStatus = new AuthTestStatus(pkt.status, grupos);
                    break;
                }

                if (retStatus == null)
                {
                    retStatus = new AuthTestStatus(999);
                }

                return(retStatus);
            }
            finally
            {
                communicating = false;
            }
        }
Пример #4
0
        public String[] ListaGrupos()
        {
            try
            {
                communicating = true;

                String groups = "";

                envia_pacote(internalSock, null, 0, FWAUT_OP_GET_GROUPS, FWAUT_ST_OK);

                packet pkt = null;
                do
                {
                    pkt = recebe_pacote(internalSock);

                    if (((pkt == null) || (pkt.status != FWAUT_ST_OK && pkt.status != FWAUT_ST_MORE)))
                    {
                        throw new Exception("Erro a listar os grupos");
                    }

                    groups += Encoding.GetEncoding("iso-8859-1").GetString(pkt.body);
                } while ((pkt != null) && (pkt.status == FWAUT_ST_MORE));

                Int32 end = groups.IndexOf("\0\0");
                if (end == -1)
                {
                    end = groups.Length;
                }

                groups = groups.Substring(0, end);

                String[] grupos = groups.Trim("\0".ToCharArray()).Split("\0".ToCharArray());

                return(grupos);
            }
            finally
            {
                communicating = false;
            }
        }
Пример #5
0
        private void pingTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            Socket iSock = null;

            _Connect(ref iSock);

            //Console.WriteLine("Ping");

            //NetworkStream ns = client.GetStream();

            envia_pacote(iSock, null, 0, FWAUT_OP_PING, FWAUT_ST_OK);
            packet pkt = recebe_pacote(iSock);

            if ((pkt == null) || (pkt.status != FWAUT_ST_OK))
            {
                throw new Exception("Ping Error");
            }

            //Console.WriteLine("Pong");

            _Disconnect(iSock);
        }
Пример #6
0
        private void _Connect(ref Socket iSock)
        {
            iSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            iSock.Connect(remoteEP);

            chave = gera_chave(password);

            header_key header = new header_key();

            header.chave = new Byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
            header.n     = 1;
            header.nop   = BitConverter.ToUInt16(header.chave, 0);
            Byte[] buffer = header.ToBytes();

            buffer = aut_encripta_buffer(buffer, buffer.Length, 0, chave);

            String tst = "0x" + BitConverter.ToString(buffer).Replace("-", ", 0x");


            aut_seta_n(header.n);

            //Envia chave se sessão
            iSock.Send(buffer, 0, buffer.Length, SocketFlags.None);

            //Separa chave, com a propria chave gerada anteriormente
            //Para criptografia posteriores
            chave = new chave_aut(header.chave);

            packet pkt = recebe_pacote(iSock);

            aut_seta_n(pkt.n);

            if (pkt.operacao == 5)
            {
                greeting = new ag_greeting(pkt.body);
            }
        }
Пример #7
0
        public String[] ListaUsuarios()
        {
            Socket sock = null;

            _Connect(ref sock);

            envia_pacote(sock, null, 0, FWAUT_OP_GET_USERS, FWAUT_ST_OK);
            String users = "";

            packet pkt = null;

            do
            {
                pkt = recebe_pacote(sock);

                if (((pkt == null) || (pkt.status != FWAUT_ST_OK && pkt.status != FWAUT_ST_MORE)))
                {
                    throw new Exception("Erro ao listar os usuário");
                }

                users += Encoding.GetEncoding("iso-8859-1").GetString(pkt.body);
            } while ((pkt != null) && (pkt.status == FWAUT_ST_MORE));

            Int32 end = users.IndexOf("\0\0");

            if (end == -1)
            {
                end = users.Length;
            }

            users = users.Substring(0, end);

            String[] usuarios = users.Trim("\0".ToCharArray()).Split("\0".ToCharArray());

            _Disconnect(sock);
            return(usuarios);
        }
Пример #8
0
        internal packet recebe_pacote(Socket sock)
        {
            if (type == AuthType.None)
            {
                throw new Exception("Auth type not defined");
            }

            packet pkt = null;

            Byte[] buffer = new Byte[32]; // Tamanho do header

            sock.ReceiveTimeout = 0;
            Int32 size = sock.Receive(buffer, SocketFlags.None);

            if (size >= 32)
            {
                //Decripta e trata o header separadamente do body (32 bytes)
                Byte[] decHeader = aut_decripta_buffer(buffer, 32, 0, chave);
                pkt = new packet(decHeader);

                //Define o nr e ne
                if (type == AuthType.Client)
                {
                    nr = htonlInt32(pkt.n);
                }

                if (type == AuthType.Server)
                {
                    if (++nr == 0xFFFFFFFF)
                    {
                        nr = 1;
                    }
                }

                if (pkt.tam_pack - 32 > 0)
                {
                    size = 0;
                    Int32        expectedSize = pkt.tam_pack - 32;
                    MemoryStream stm          = new MemoryStream();
                    buffer = new Byte[pkt.tam_pack - 32];

                    //Loop para caso a quantidade de dados recebida seja menor que o esperado
                    do
                    {
                        size = sock.Receive(buffer);
                        if (size == 0)
                        {
                            throw new Exception("Invalid packet size");
                        }

                        stm.Write(buffer, 0, size);

                        //Redefine o tamanho do buffer para não receber mais dados do que o necessário
                        buffer = new Byte[expectedSize - stm.Length];
                    } while (stm.Length < expectedSize);

                    buffer = stm.ToArray();
                    stm.Dispose();
                    stm.Close();
                    stm = null;

                    //Define o vetor do IV
                    //Byte[] bVet = BitConverter.GetBytes(nr);
                    //Array.Reverse(bVet);
                    //Int32 vet = BitConverter.ToInt32(bVet, 0);

                    //Decriptografa e trata o body
                    pkt.body = aut_decripta_buffer(buffer, buffer.Length, htonlInt32(nr), chave);
                }

                if (!pkt.CheckMD5(chave))
                {
                    throw new Exception("MD5 inválido");
                }
            }

            if (OnPacketReceive != null)
            {
                OnPacketReceive((header_aut)pkt.Clone(), pkt.body);
            }

            return(pkt);
        }
Пример #9
0
        private void EsperaPacotes()
        {
            while (_isRunning)
            {
                try
                {
                    packet pkt = recebe_pacote(_mainSock);

                    if (pkt == null)
                    {
                        Dispose();
                        return;
                    }

                    switch (pkt.operacao)
                    {
                    case FWAUT_OP_PING:
                        envia_pacote(_mainSock, null, 0, FWAUT_OP_PING, FWAUT_ST_OK);
                        Dispose();
                        break;

                    case FWAUT_OP_REQ:
                    case FWAUT_OP_REQ_GROUPS:
                        //Retorna os grupos que o usuário existe
                        //return FWAUT_ST_OK - Autenticacao OK
                        //return FWAUT_ST_BAD_PWD - Senha invalida
                        //return FWAUT_NO_USER - Usuario inexistente
                        String[] request = Encoding.ASCII.GetString(pkt.body).Trim("\0".ToCharArray()).Split("\0".ToCharArray());

                        //AuthUserResult res = _users.ValidaUser(request[0], request[1]);

                        AuthUserResult res = new AuthUserResult(request[0]);
                        if (OnUserValidate != null)
                        {
                            OnUserValidate(_remoteEP, request[0], request[1], ref res);
                        }

                        ushort status = FWAUT_ST_OK;
                        Byte[] grpRet = new Byte[0];
                        switch (res.Result)
                        {
                        case AuthResult.OK:
                            status = FWAUT_ST_OK;
                            String grupos = "";
                            foreach (String item in res.Groups)
                            {
                                grupos += item + "\0";
                            }
                            grupos += "\0\0";
                            grpRet  = Encoding.GetEncoding(869).GetBytes(grupos);
                            break;

                        case AuthResult.BadPassword:
                            status = FWAUT_ST_BAD_PWD;
                            break;

                        case AuthResult.NoUser:
                            status = FWAUT_ST_NO_USER;
                            break;
                        }


                        envia_pacote(_mainSock, grpRet, grpRet.Length, pkt.operacao, status);
                        break;


                    case FWAUT_OP_GET_USERS:
                        List <String> users = new List <String>();

                        if (OnListUsers != null)
                        {
                            OnListUsers(_remoteEP, ref users);
                        }

                        String tstU = "";
                        foreach (String item in users)
                        {
                            tstU += item + "\0";
                        }
                        tstU += "\0\0";

                        Byte[] usr = Encoding.GetEncoding(869).GetBytes(tstU);
                        envia_pacote(_mainSock, usr, usr.Length, FWAUT_OP_GET_USERS, FWAUT_ST_OK);
                        //trata_lst_users(ns);
                        break;

                    case FWAUT_OP_GET_GROUPS:
                        List <String> groups = new List <String>();

                        if (OnListGroups != null)
                        {
                            OnListGroups(_remoteEP, ref groups);
                        }

                        String tst = "";
                        foreach (String item in groups)
                        {
                            tst += item + "\0";
                        }
                        tst += "\0\0";

                        //Teste de encoding do FW
                        Byte[] tmp = new Byte[0];
                        foreach (EncodingInfo encInfo in Encoding.GetEncodings())
                        {
                            Int32  offSet = tmp.Length;
                            Byte[] b      = encInfo.GetEncoding().GetBytes(encInfo.CodePage.ToString() + ": ção");
                            Array.Resize(ref tmp, offSet + (b.Length + 1));
                            Array.Copy(b, 0, tmp, offSet, b.Length);
                            tmp[b.Length + offSet] = 0;
                        }

                        Byte[] grp = Encoding.GetEncoding(869).GetBytes(tst);
                        //Byte[] grp = tmp;
                        envia_pacote(_mainSock, grp, grp.Length, FWAUT_OP_GET_GROUPS, FWAUT_ST_OK);
                        //trata_lst_groups(ns);
                        break;

                    case FWAUT_OP_GET_SKEY:
                        /*
                         * if (len != sizeof(ag_get_skey))
                         * {
                         *  if ((erro = envia_pacote(ns, NULL, 0, op, FWAUT_ST_ERROR)) != 0)
                         *  {
                         *      if (erro == 2)
                         *          syslog(fila | LOG_ERR, "%s\n", ag_msg[AG_MSG_ERRO_MEMORIA]);
                         *      exit(1);
                         *  }
                         * }
                         * else
                         * {
                         *  retorna_opie(ns, (ag_get_skey*)dados);
                         * }*/
                        envia_pacote(_mainSock, null, 0, pkt.operacao, FWAUT_ST_ERROR);
                        break;

                    default:
                        envia_pacote(_mainSock, null, 0, pkt.operacao, FWAUT_ST_ERROR);
                        break;
                    }
                }
                catch {
                    _isRunning = false;
                }
            }
        }