示例#1
0
        public static bool DoSyncAddRemovePrograms()
        {
            try
            {
                Network net;
                net = Utilities.ConnectNetwork(0);
                if (net == null)
                {
                    return(false);
                }

                Status.UpdateMessage(0, "Collecting installed programs (HKLM)");

                ListAddRemoveApps Finaldata = new ListAddRemoveApps();
                Finaldata.Items     = new List <AddRemoveApp>();
                Finaldata.MachineID = SystemInfos.SysInfo.MachineID;
                Finaldata.SIDUsers  = new List <string>();

                ListAddRemoveApps data = CollectInfos(Registry.LocalMachine, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall", "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall", "");
                if (data == null)
                {
                    return(false);
                }
                if (data.Items != null)
                {
                    Finaldata.Items.AddRange(data.Items);
                }

                foreach (string kvp in Userregistries.GetLoadedUserRegistries())
                {
                    Status.UpdateMessage(0, "Collecting installed programs (" + kvp + ")");

                    Finaldata.SIDUsers.Add(kvp);

                    data = CollectInfos(Registry.Users, kvp + "\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall", kvp + "\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall", kvp);
                    if (data == null)
                    {
                        continue;
                    }

                    if (data.Items == null)
                    {
                        continue;
                    }

                    Finaldata.Items.AddRange(data.Items);
                }

                Status.UpdateMessage(0, "Reporting installed programs");
                net.ReportAddRemovePrograms(Finaldata);

                net.CloseConnection();
            }
            catch (Exception ee)
            {
                Debug.WriteLine(ee.ToString());
                FoxEventLog.WriteEventLog("Servere error while syncing AddRemovePrograms: " + ee.ToString(), EventLogEntryType.Error);
            }
            Status.UpdateMessage(0);

            return(true);
        }
示例#2
0
        static ListAddRemoveApps CollectInfos(RegistryKey RegPart, string RegBranch, string RegBranchWOW32, string HKCUUser)
        {
            ListAddRemoveApps data = new ListAddRemoveApps();

            data.MachineID = SystemInfos.SysInfo.MachineID;
            data.Items     = new List <AddRemoveApp>();

            RegistryKey reg = RegPart.OpenSubKey(RegBranch, false);

            if (reg != null)
            {
                foreach (string u in reg.GetSubKeyNames())
                {
                    RegistryKey ureg = reg.OpenSubKey(u, false);
                    if (ureg == null)
                    {
                        continue;
                    }

                    AddRemoveApp ara = new AddRemoveApp();
                    ara.ProductID       = u;
                    ara.HKCUUser        = HKCUUser;
                    ara.DisplayVersion  = ureg.GetValue("DisplayVersion", "").ToString();
                    ara.Name            = ureg.GetValue("DisplayName", "").ToString();
                    ara.UninstallString = ureg.GetValue("UninstallString", "").ToString();
                    if (ara.Name == "")
                    {
                        continue;
                    }
                    int msi = 0;
                    int.TryParse(ureg.GetValue("WindowsInstaller", "0").ToString(), out msi);
                    ara.IsMSI = msi == 1 ? true : false;
                    int.TryParse(ureg.GetValue("VersionMajor", "0").ToString(), out ara.VersionMajor);
                    int.TryParse(ureg.GetValue("VersionMinor", "0").ToString(), out ara.VersionMinor);
                    ara.IsWOWBranch = false;
                    int SystemComponent;
                    int.TryParse(ureg.GetValue("SystemComponent", "0").ToString(), out SystemComponent);
                    ara.IsSystemComponent = SystemComponent == 1 ? true : false;

                    int Language;
                    int.TryParse(ureg.GetValue("Language", 0).ToString(), out Language);
                    if (Language != 0)
                    {
                        try
                        {
                            CultureInfo ci = new CultureInfo(Language);
                            if (ci != null)
                            {
                                ara.DisplayLanguage = ci.EnglishName;
                                ara.Language        = ci.Name;
                            }
                        }
                        catch
                        {
                        }
                    }

                    data.Items.Add(ara);
                    ureg.Close();
                }
                reg.Close();
            }

            if (SystemInfos.SysInfo.CPU == "EM64T")
            {
                reg = RegPart.OpenSubKey(RegBranchWOW32, false);
                if (reg != null)
                {
                    foreach (string u in reg.GetSubKeyNames())
                    {
                        RegistryKey ureg = reg.OpenSubKey(u, false);
                        if (ureg == null)
                        {
                            continue;
                        }

                        AddRemoveApp ara = new AddRemoveApp();
                        ara.ProductID       = u;
                        ara.HKCUUser        = HKCUUser;
                        ara.DisplayVersion  = ureg.GetValue("DisplayVersion", "").ToString();
                        ara.Name            = ureg.GetValue("DisplayName", "").ToString();
                        ara.UninstallString = ureg.GetValue("UninstallString", "").ToString();
                        if (ara.Name == "")
                        {
                            continue;
                        }
                        int msi = 0;
                        int.TryParse(ureg.GetValue("WindowsInstaller", "0").ToString(), out msi);
                        ara.IsMSI = msi == 1 ? true : false;
                        int.TryParse(ureg.GetValue("VersionMajor", "0").ToString(), out ara.VersionMajor);
                        int.TryParse(ureg.GetValue("VersionMinor", "0").ToString(), out ara.VersionMinor);
                        ara.IsWOWBranch = true;

                        int Language;
                        int.TryParse(ureg.GetValue("Language", 0).ToString(), out Language);
                        if (Language != 0)
                        {
                            try
                            {
                                CultureInfo ci = new CultureInfo(Language);
                                if (ci != null)
                                {
                                    ara.DisplayLanguage = ci.EnglishName;
                                    ara.Language        = ci.Name;
                                }
                            }
                            catch
                            {
                            }
                        }

                        data.Items.Add(ara);
                        ureg.Close();
                    }
                    reg.Close();
                }
            }
            return(data);
        }
示例#3
0
        public RESTStatus ReportAddRemovePrograms(SQLLib sql, ListAddRemoveApps AddRemoveList, NetworkConnectionInfo ni)
        {
            if (ni.HasAcl(ACLFlags.ComputerLogin) == false)
            {
                ni.Error   = "Access denied";
                ni.ErrorID = ErrorFlags.AccessDenied;
                return(RESTStatus.Denied);
            }

            AddRemoveList.MachineID = ni.Username;

            lock (ni.sqllock)
            {
                if (Convert.ToInt32(sql.ExecSQLScalar("SELECT COUNT(*) FROM ComputerAccounts WHERE MachineID=@m",
                                                      new SQLParam("@m", AddRemoveList.MachineID))) == 0)
                {
                    ni.Error   = "Invalid MachineID";
                    ni.ErrorID = ErrorFlags.InvalidValue;
                    return(RESTStatus.Denied);
                }
            }

            if (AddRemoveList.Items == null)
            {
                ni.Error   = "Invalid Items";
                ni.ErrorID = ErrorFlags.InvalidValue;
                return(RESTStatus.Fail);
            }
            if (AddRemoveList.SIDUsers == null)
            {
                AddRemoveList.SIDUsers = new List <string>();
            }

            foreach (AddRemoveApp rep in AddRemoveList.Items)
            {
                if (rep.ProductID == null)
                {
                    rep.ProductID = "";
                }
                if (rep.Name == null)
                {
                    rep.Name = "";
                }
                if (rep.DisplayVersion == null)
                {
                    rep.DisplayVersion = "";
                }
                if (rep.UninstallString == null)
                {
                    rep.UninstallString = "";
                }
                if (rep.Language == null)
                {
                    rep.Language = "";
                }
                if (rep.DisplayLanguage == null)
                {
                    rep.DisplayLanguage = "";
                }
                if (rep.HKCUUser == null)
                {
                    rep.HKCUUser = "";
                }

                rep.ProductID       = rep.ProductID.Trim();
                rep.Name            = rep.Name.Trim();
                rep.DisplayVersion  = rep.DisplayVersion.Trim();
                rep.UninstallString = rep.UninstallString.Trim();
                rep.Language        = rep.Language.Trim();
                rep.DisplayLanguage = rep.DisplayLanguage.Trim();
                rep.HKCUUser        = rep.HKCUUser.Trim();
            }

            List <AddRemoveApp> Installed = new List <AddRemoveApp>();
            List <AddRemoveApp> Reported  = AddRemoveList.Items;

            lock (ni.sqllock)
            {
                SqlDataReader dr = sql.ExecSQLReader("SELECT * FROM AddRemovePrograms WHERE MachineID=@id", new SQLParam("@id", AddRemoveList.MachineID));
                while (dr.Read())
                {
                    AddRemoveApp ar = new AddRemoveApp();
                    ar.DisplayLanguage   = Convert.ToString(dr["DisplayLanguage"]);
                    ar.DisplayVersion    = Convert.ToString(dr["DisplayVersion"]);
                    ar.IsMSI             = Convert.ToBoolean(dr["IsMSI"]);
                    ar.IsWOWBranch       = Convert.ToBoolean(dr["IsWOWBranch"]);
                    ar.IsSystemComponent = Convert.ToBoolean(dr["IsSystemComponent"]);
                    ar.Language          = Convert.ToString(dr["Language"]);
                    ar.MachineID         = Convert.ToString(dr["MachineID"]);
                    ar.Name            = Convert.ToString(dr["Name"]);
                    ar.ProductID       = Convert.ToString(dr["ProductID"]);
                    ar.UninstallString = Convert.ToString(dr["UninstallString"]);
                    ar.VersionMajor    = Convert.ToInt32(dr["VersionMajor"]);
                    ar.VersionMinor    = Convert.ToInt32(dr["VersionMinor"]);
                    ar.HKCUUser        = Convert.ToString(dr["HKCUUser"]);
                    if (string.IsNullOrWhiteSpace(ar.HKCUUser) == true)
                    {
                        ar.HKCUUser = "";
                    }

                    Installed.Add(ar);
                }
                dr.Close();
            }

            List <AddRemoveApp> Updated   = new List <AddRemoveApp>();
            List <AddRemoveApp> Unchanged = new List <AddRemoveApp>();
            List <AddRemoveApp> Removed   = new List <AddRemoveApp>();
            List <AddRemoveApp> Added     = new List <AddRemoveApp>();

            foreach (AddRemoveApp inst in Installed)
            {
                if (inst.ProductID == null)
                {
                    inst.ProductID = "";
                }
                if (inst.Name == null)
                {
                    inst.Name = "";
                }
                if (inst.DisplayVersion == null)
                {
                    inst.DisplayVersion = "";
                }
                if (inst.UninstallString == null)
                {
                    inst.UninstallString = "";
                }
                if (inst.Language == null)
                {
                    inst.Language = "";
                }
                if (inst.DisplayLanguage == null)
                {
                    inst.DisplayLanguage = "";
                }

                inst.ProductID       = inst.ProductID.Trim();
                inst.Name            = inst.Name.Trim();
                inst.DisplayVersion  = inst.DisplayVersion.Trim();
                inst.UninstallString = inst.UninstallString.Trim();
                inst.Language        = inst.Language.Trim();
                inst.DisplayLanguage = inst.DisplayLanguage.Trim();
                inst.HKCUUser        = inst.HKCUUser.Trim();

                bool Found = false;
                foreach (AddRemoveApp rep in Reported)
                {
                    if (rep.ProductID.ToLower() == inst.ProductID.ToLower() && rep.IsWOWBranch == inst.IsWOWBranch && rep.HKCUUser == inst.HKCUUser)
                    {
                        if (rep.IsMSI == inst.IsMSI && rep.IsSystemComponent == inst.IsSystemComponent && rep.Name.ToLower() == inst.Name.ToLower() &&
                            rep.DisplayVersion.ToLower() == inst.DisplayVersion.ToLower() && inst.UninstallString.ToLower() == rep.UninstallString.ToLower() &&
                            rep.VersionMajor == inst.VersionMajor && rep.VersionMinor == inst.VersionMinor && rep.Language.ToLower() == inst.Language.ToLower() &&
                            rep.DisplayLanguage.ToLower() == inst.DisplayLanguage.ToLower())
                        {
                            Unchanged.Add(rep);
                            Found = true;
                            break;
                        }
                        else
                        {
                            Updated.Add(rep);
                            Found = true;
                            break;
                        }
                    }
                }
                if (Found == false)
                {
                    if (inst.HKCUUser != "" && AddRemoveList.SIDUsers.Contains(inst.HKCUUser, StringComparer.InvariantCultureIgnoreCase) == false)
                    {
                        //likely that this user is not logged on or such
                        Unchanged.Add(inst);
                    }
                    else
                    {
                        Removed.Add(inst);
                    }
                }
            }

            foreach (AddRemoveApp inst in Reported)
            {
                bool Found = false;
                foreach (AddRemoveApp rep in Installed)
                {
                    if (rep.ProductID.ToLower() == inst.ProductID.ToLower() && rep.IsWOWBranch == inst.IsWOWBranch && rep.HKCUUser == inst.HKCUUser)
                    {
                        Found = true;
                        break;
                    }
                }
                if (Found == false)
                {
                    Added.Add(inst);
                }
            }

            lock (ni.sqllock)
            {
                try
                {
                    sql.BeginTransaction();
                    sql.SEHError = true;
                    foreach (AddRemoveApp ar in Removed)
                    {
                        sql.ExecSQL("DELETE FROM AddRemovePrograms WHERE MachineID=@id AND IsWOWBranch=@wow AND ProductID=@prod AND HKCUUser=@user",
                                    new SQLParam("@id", AddRemoveList.MachineID),
                                    new SQLParam("@prod", ar.ProductID),
                                    new SQLParam("@user", ar.HKCUUser),
                                    new SQLParam("@wow", ar.IsWOWBranch));
                    }
                    foreach (AddRemoveApp ar in Updated)
                    {
                        sql.ExecSQL(@"UPDATE AddRemovePrograms SET
                            IsMSI=@IsMSI,
                            IsSystemComponent=@IsSystemComponent,
                            Name=@Name,
                            DisplayVersion=@DisplayVersion,
                            UninstallString=@UninstallString,
                            VersionMajor=@VersionMajor,
                            VersionMinor=@VersionMinor,
                            Language=@Language,
                            DisplayLanguage=@DisplayLanguage,
                            DT=@DT
                            WHERE MachineID=@id AND IsWOWBranch=@wow AND ProductID=@prod AND HKCUUser=@user",
                                    new SQLParam("@id", AddRemoveList.MachineID),
                                    new SQLParam("@prod", ar.ProductID),
                                    new SQLParam("@wow", ar.IsWOWBranch),
                                    new SQLParam("@IsMSI", ar.IsMSI),
                                    new SQLParam("@IsSystemComponent", ar.IsSystemComponent),
                                    new SQLParam("@Name", ar.Name.Trim()),
                                    new SQLParam("@DisplayVersion", ar.DisplayVersion),
                                    new SQLParam("@UninstallString", ar.UninstallString),
                                    new SQLParam("@VersionMajor", ar.VersionMajor),
                                    new SQLParam("@VersionMinor", ar.VersionMinor),
                                    new SQLParam("@Language", ar.Language),
                                    new SQLParam("@DisplayLanguage", ar.DisplayLanguage),
                                    new SQLParam("@user", ar.HKCUUser),
                                    new SQLParam("@DT", DateTime.UtcNow));
                    }
                    foreach (AddRemoveApp ar in Added)
                    {
                        sql.InsertMultiData("AddRemovePrograms",
                                            new SQLData("MachineID", AddRemoveList.MachineID),
                                            new SQLData("ProductID", ar.ProductID),
                                            new SQLData("IsWOWBranch", ar.IsWOWBranch),
                                            new SQLData("IsMSI", ar.IsMSI),
                                            new SQLData("IsSystemComponent", ar.IsSystemComponent),
                                            new SQLData("Name", ar.Name.Trim()),
                                            new SQLData("DisplayVersion", ar.DisplayVersion),
                                            new SQLData("UninstallString", ar.UninstallString),
                                            new SQLData("VersionMajor", ar.VersionMajor),
                                            new SQLData("VersionMinor", ar.VersionMinor),
                                            new SQLData("Language", ar.Language),
                                            new SQLData("DisplayLanguage", ar.DisplayLanguage),
                                            new SQLData("HKCUUser", ar.HKCUUser),
                                            new SQLData("DT", DateTime.UtcNow));
                    }
                    sql.CommitTransaction();
                }
                catch (Exception ee)
                {
                    sql.RollBackTransaction();
                    FoxEventLog.WriteEventLog("DB Error: Cannot update AddRemovePrograms: " + ee.ToString(), System.Diagnostics.EventLogEntryType.Error);
                    return(RESTStatus.ServerError);
                }
                finally
                {
                    sql.SEHError = false;
                }
            }

            AddRemoveProgramsLst l = new AddRemoveProgramsLst();

            l.Added     = Added;
            l.Removed   = Removed;
            l.Unchanged = Unchanged;
            l.Updated   = Updated;
            l.MachineID = AddRemoveList.MachineID;
            Thread t = new Thread(new ParameterizedThreadStart(new DReportingThread(ReportingThread)));

            t.Start(l);

            return(RESTStatus.Created);
        }