Esempio n. 1
0
        private static IAMKeyData CheckKey(String installKey, IAMVersion version, String sKey)
        {
            IAMKeyData kData = new IAMKeyData();

            byte[] key = new byte[0];

            kData.InstallKey = "installkey://safeid/" + version.ToString() + "/" + installKey;

            key = StringToByteArray(sKey.Replace("-", "").Replace("/", "").Replace("\\", ""));

            kData.NumLic = (UInt32)((key[4] << 8) | key[6]);
            UInt32 totalSeconds = (UInt32)((key[2] << 24) | (key[12] << 16) | (key[9] << 8) | (key[7]));

            kData.IsServerKey = (key[3] == 1);

            if (totalSeconds > 0)
            {
                kData.IsTemp   = true;
                kData.TempDate = new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(totalSeconds);
            }
            else
            {
                kData.IsTemp = false;
            }

            String cKey = GeraKey(installKey, kData.IsServerKey, kData.NumLic, kData.IsTemp, kData.TempDate, version);

            if (cKey.ToUpper().Replace("-", "").Replace("/", "").Replace("\\", "") != sKey.ToUpper().Replace("-", "").Replace("/", "").Replace("\\", ""))
            {
                throw new Exception("Invalid key");
            }

            return(kData);
        }
Esempio n. 2
0
        public static LicenseControl GetLicenseData(SqlConnection conn, SqlTransaction trans, Int64 enterpriseId)
        {
            //Retorna zero para ilimitado



            String installKey = "";

            try
            {
                using (IAMDatabase db = new IAMDatabase(conn))
                {
                    //Server installation key
                    using (IAM.Config.ServerKey2 sk = new IAM.Config.ServerKey2(db.Connection))
                        installKey = sk.ServerInstallationKey.AbsoluteUri;

                    //Resgata todas as licenças desta empresa e de servidor
                    DataTable dtLic = db.ExecuteDataTable("select * from license where enterprise_id in (0, " + enterpriseId + ")", trans);
                    if (dtLic == null)
                    {
                        return(new LicenseControl(1, "Error on get licenses on server", installKey));
                    }

                    if (dtLic.Rows.Count == 0)
                    {
                        return(new LicenseControl(1, "License not found", installKey));
                    }

                    //Localiza a licença menos restrita
                    IAMKeyData key = null;
                    foreach (DataRow dr in dtLic.Rows)
                    {
                        try
                        {
                            IAMKeyData k = IAMKey.ExtractFromCert(dr["license_data"].ToString());

                            //Checa a validade da licença
                            if ((k.IsTemp) && (k.TempDate.Value.CompareTo(DateTime.Now) < 0))
                            {
                                continue;
                            }

                            if (key == null)
                            {
                                key = k;
                            }

                            if (k.NumLic > key.NumLic)
                            {
                                key = k;
                            }
                        }
                        catch { }
                    }

                    if (key == null)
                    {
                        return(new LicenseControl(1, "License not found", installKey));
                    }

                    //Resgata do banco a contagem atual de entidades
                    Int32 count = db.ExecuteScalar <Int32>(conn, "select count(e.id) from entity e with(nolock) inner join context c with(nolock) on c.id = e.context_id where e.deleted = 0 and c.enterprise_id = " + enterpriseId, CommandType.Text, null, trans);

                    LicenseControl lc = new LicenseControl((Int32)key.NumLic, count, installKey);

                    return(lc);
                }
            }
            catch (Exception ex)
            {
                return(new LicenseControl(0, ex.Message, installKey));
            }
        }