Ejemplo n.º 1
0
        public override KnownHostValue GetKey(string Host)
        {
            var hostbytes = Encoding.ASCII.GetBytes(Host);

            if (HostKeys.TryGetValue(Host, out var keyData))
            {
                return(keyData);
            }
            foreach (var hashedKey in hashedKeys)
            {
                using (HMACSHA1 hmac = new HMACSHA1(hashedKey.Salt))
                {
                    var hostHash = Convert.ToBase64String(hmac.ComputeHash(hostbytes));
                    if (hostHash.Equals(hashedKey.HostHash))
                    {
                        return(new KnownHostValue()
                        {
                            HostKeyName = hashedKey.KeyName,
                            Fingerprint = hashedKey.Fingerprint
                        });
                    }
                }
            }
            foreach (var wildcardKey in wildcardKeys)
            {
                if (wildcardKey.Pattern.IsMatch(Host))
                {
                    return(new KnownHostValue()
                    {
                        HostKeyName = wildcardKey.KeyName,
                        Fingerprint = wildcardKey.Fingerprint,
                    });
                }
            }
            return(default);
Ejemplo n.º 2
0
        public DesignHostDevice()
        {
            HostKeys.Add(new Data.HostKey
            {
                Name        = "`Gamepad A`",
                TexturePath = "pack://application:,,,/Design/Images/a.png"
            });

            HostKeys.Add(new Data.HostKey
            {
                Name        = "`Gamepad B`",
                TexturePath = "pack://application:,,,/Design/Images/b.png"
            });

            HostKeys.Add(new Data.HostKey
            {
                Name        = "`Gamepad C`",
                TexturePath = "pack://application:,,,/Design/Images/c.png"
            });

            HostKeys.Add(new Data.HostKey
            {
                Name        = "`Gamepad D`",
                TexturePath = "pack://application:,,,/Design/Images/d.png"
            });
        }
Ejemplo n.º 3
0
        private void WriteToDisk()
        {
            var jsonString = JsonConvert.SerializeObject(new ConfigFileStruct()
            {
                Keys = HostKeys.ToDictionary(x => x.Key, x => x.Value)
            },
                                                         Formatting.Indented
                                                         );

            File.WriteAllText(FileName, jsonString);
        }
Ejemplo n.º 4
0
        private void WriteToDisk()
        {
            var jsonString = JsonConvert.SerializeObject(new ConfigFileStruct()
            {
                Keys = HostKeys.ToDictionary(x => x.Key, x => x.Value)
            },
                                                         Formatting.Indented
                                                         );
            var d = Directory.CreateDirectory(Path.GetDirectoryName(FileName));

            if (d.Exists)
            {
                File.WriteAllText(FileName, jsonString);
            }
        }
Ejemplo n.º 5
0
        public override void Execute()
        {
            Source      = new College(SourceProgID);
            Destination = new College(DestinationProgID);

            IEnumerable <IResourceObject> group = null;

            if (!string.IsNullOrEmpty(GroupName))
            {
                GroupName = GroupName.Replace("{DATE}", DateTime.Now.ToString("yyyyMMdd")).Replace("{TIME}", DateTime.Now.ToString("HH_mm"));
                if (TargetType == ResourceType.Location)
                {
                    var gr = new LocationGroup(Destination)
                    {
                        Name = GroupName, Description = "CopyResourceInfoTask"
                    };
                    group = gr.Members;
                }
                else
                {
                    var gr = new StaffMemberGroup(Destination)
                    {
                        Name = GroupName, Description = "CopyResourceInfoTask"
                    };
                    group = gr.Members;
                }
            }

            var dict = new Dictionary <string, IResourceObject>();

            foreach (var key in HostKeys)
            {
                var sRes = GetObject(Source, key);
                if (sRes == null)
                {
                    throw new InvalidOperationException("Object " + key + " does not exist");
                }
                var  dRes  = GetObject(Destination, key);
                bool isNew = dRes == null;

                Log.WriteLine("{2} {0} ({1})", key, sRes.Name, isNew ? "Creating" : "Updating");
                if (isNew)
                {
                    dRes = TargetType == ResourceType.Staff ? (IResourceObject) new StaffMember(Destination) : new Location(Destination);
                }
                dict.Add(key, dRes);
                if (group != null)
                {
                    AddObject(group, dRes);
                }
                foreach (var propName in (isNew ? AllProperties : Properties))
                {
                    var prop = sRes.GetType().GetProperty(propName);

                    switch (propName)
                    {
                    case "Availability":
                        if (sRes.NamedAvailability != null)
                        {
                            dRes.NamedAvailability = Destination.AvailabilityPatterns.FindByHostKey(sRes.NamedAvailability.HostKey);
                            if (dRes.NamedAvailability == null)
                            {
                                throw new InvalidOperationException("Named availability not found: " + sRes.NamedAvailability.HostKey);
                            }
                        }
                        else
                        {
                            dRes.NamedAvailability = null;
                            dRes.BaseAvailability  = new PeriodInYearPattern(Destination)
                            {
                                PatternAsArray = sRes.BaseAvailability.PatternAsArray
                            };
                        }
                        break;

                    case "Suitabilities":
                        break;

                    case "AvoidConcurrency":
                        dynamic col = dRes.AvoidConcurrencyWith;
                        col.Clear();
                        foreach (var obj in sRes.AvoidConcurrencyWith)
                        {
                            var res = GetObject(Destination, obj.HostKey);
                            if (res == null)
                            {
                                Log.WriteLine("Warning: concurrent location {0} not found", obj.HostKey);
                            }
                            else
                            {
                                AddObject(dRes.AvoidConcurrencyWith, res);
                            }
                        }
                        break;

                    case "Department":
                        if (sRes.Department == null)
                        {
                            dRes.Department = null;
                        }
                        else
                        {
                            dRes.Department = Destination.Departments.FindByHostKey(sRes.Department.HostKey);
                            if (dRes.Department == null)
                            {
                                throw new InvalidOperationException("Department not found: " + sRes.Department.HostKey);
                            }
                        }
                        break;

                    case "Zone":
                        if (sRes.Zone == null)
                        {
                            dRes.Zone = null;
                        }
                        else
                        {
                            dRes.Zone = Destination.Zones.FindByHostKey(sRes.Zone.HostKey);
                            if (dRes.Zone == null)
                            {
                                throw new InvalidOperationException("Zone not found: " + sRes.Zone.HostKey);
                            }
                        }
                        break;

                    case "Tags":
                        CopyCollection(sRes.Tags, dRes.Tags, Destination.Tags);
                        break;

                    case "SharedWith":
                        CopyCollection(sRes.SharedWith, dRes.SharedWith, Destination.Departments);
                        break;

                    default:
                        if (prop == null)
                        {
                            if (!isNew)
                            {
                                Log.WriteLine("Warning: property {0} does not exist", propName);
                            }
                            continue;
                        }
                        var val = prop.GetValue(sRes);
                        prop.SetValue(dRes, val);
                        break;
                    }
                }
            }

            int count = Destination.Suitabilities.Count, cur = 0;

            Log.WriteLine();
            foreach (var dSuit in Destination.Suitabilities)
            {
                Console.SetCursorPosition(0, Console.CursorTop - 1);
                Log.WriteLine("Updating suitabilities: {0}/{1}", ++cur, count);
                var sSuit = Source.Suitabilities.FindByHostKey(dSuit.HostKey);
                if (sSuit == null)
                {
                    continue;
                }

                var sCol  = TargetType == ResourceType.Staff ? (IEnumerable <IResourceObject>)sSuit.PrimaryStaff : sSuit.PrimaryLocations;
                var dCol  = TargetType == ResourceType.Staff ? (IEnumerable <IResourceObject>)dSuit.PrimaryStaff : dSuit.PrimaryLocations;
                var sKeys = sCol.Select(l => l.HostKey).ToArray();
                var dKeys = dCol.Select(l => l.HostKey).ToArray();
                HostKeys.Where(l => sKeys.Contains(l) && !dKeys.Contains(l)).ForEach(l => AddObject(dCol, dict[l]));
                HostKeys.Where(l => !sKeys.Contains(l) && dKeys.Contains(l)).ForEach(l => RemoveObject(dCol, dict[l]));

                sCol  = TargetType == ResourceType.Staff ? (IEnumerable <IResourceObject>)sSuit.OtherStaff : sSuit.OtherLocations;
                dCol  = TargetType == ResourceType.Staff ? (IEnumerable <IResourceObject>)dSuit.OtherStaff : dSuit.OtherLocations;
                sKeys = sCol.Select(l => l.HostKey).ToArray();
                dKeys = dCol.Select(l => l.HostKey).ToArray();
                HostKeys.Where(l => sKeys.Contains(l) && !dKeys.Contains(l)).ForEach(l => AddObject(dCol, dict[l]));
                HostKeys.Where(l => !sKeys.Contains(l) && dKeys.Contains(l)).ForEach(l => RemoveObject(dCol, dict[l]));
            }
        }
Ejemplo n.º 6
0
        public void LoadFromDisk()
        {
            if (File.Exists(FileName))
            {
                foreach (var line in File.ReadAllLines(FileName))
                {
                    // skip emty lines or comments
                    // skip @cert-authority and @revoked because we do not validate
                    if (line.Length < 1 || line[0] == '#' || line[0] == '@')
                    {
                        continue;
                    }

                    var hostparts = line.Split(' ');
                    // Skip invalid lines
                    if (hostparts.Length < 3 || hostparts[0].Length < 1)
                    {
                        continue;
                    }
                    var(hostname, keyName, pubKey) = (hostparts[0], hostparts[1], hostparts[2]);

                    string fingerprint;
                    using (var md5 = MD5.Create())
                    {
                        var pubkey      = Convert.FromBase64String(pubKey);
                        var fp_as_bytes = md5.ComputeHash(pubkey);
                        // commented out because realization below encode bytes 10,01,10 as 10:1:10 instead of classic 10:01:10
                        // so make it compatible
                        // fingerprint = System.BitConverter.ToString(fp_as_bytes).Replace('-', ':').ToLower();
                        var sb = new StringBuilder();
                        foreach (var b in fp_as_bytes)
                        {
                            sb.AppendFormat("{0:x}:", b);
                        }
                        fingerprint = sb.ToString().Remove(sb.ToString().Length - 1);
                    }

                    // hashed hostname, can be only one on line
                    if (hostname[0] == '|')
                    {
                        var hashparts = hostname.Split('|');
                        // skip invalid or unsupported lines
                        if (hashparts.Length < 4 || hashparts[1] != "1")
                        {
                            continue;
                        }
                        hashedKeys.Add(
                            new HashedKeysStruct()
                        {
                            Salt        = Convert.FromBase64String(hashparts[2]),
                            HostHash    = hashparts[3],
                            KeyName     = keyName,
                            Fingerprint = fingerprint,
                        }
                            );
                    }
                    else
                    {
                        foreach (var host in hostname.Split(','))
                        {
                            // TODO: there can be [host]:port values.
                            // We do not support it because we do not know the port
                            if (host.Length < 1 || host[0] == '[')
                            {
                                continue;
                            }
                            var(tmpHost, tmpFingerprint) = (host, fingerprint);
                            if (host[0] == '!')                     // Host connection denied
                            {
                                tmpHost        = host.Substring(1); // clean '!'
                                tmpFingerprint = '!' + fingerprint; // make fingerprint for this host invalid
                            }
                            // wildcard pattern
                            else if (WildcardPattern.ContainsWildcardCharacters(host))
                            {
                                wildcardKeys.Add(
                                    new WildcardKeysStruct()
                                {
                                    Pattern     = new WildcardPattern(tmpHost),
                                    KeyName     = keyName,
                                    Fingerprint = tmpFingerprint,
                                }
                                    );
                            }
                            // simple host
                            else
                            {
                                var hostData = new KnownHostValue()
                                {
                                    HostKeyName = keyName,
                                    Fingerprint = tmpFingerprint,
                                };
                                HostKeys.AddOrUpdate(tmpHost, hostData, (key, oldValue) => {
                                    return(hostData);
                                });
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 7
0
 private void DeleteKey(HostKey key)
 {
     HostKeys.Remove(key);
 }