Ejemplo n.º 1
0
        /// <summary>Creates  multiple principals in the KDC and adds them to a keytab file.</summary>
        /// <param name="keytabFile">keytab file to add the created principal.s</param>
        /// <param name="principals">principals to add to the KDC, do not include the domain.
        ///     </param>
        /// <exception cref="System.Exception">
        /// thrown if the principals or the keytab file could not be
        /// created.
        /// </exception>
        public virtual void CreatePrincipal(FilePath keytabFile, params string[] principals
                                            )
        {
            string generatedPassword = UUID.RandomUUID().ToString();

            Org.Apache.Directory.Server.Kerberos.Shared.Keytab.Keytab keytab = new Org.Apache.Directory.Server.Kerberos.Shared.Keytab.Keytab
                                                                                   ();
            IList <KeytabEntry> entries = new AList <KeytabEntry>();

            foreach (string principal in principals)
            {
                CreatePrincipal(principal, generatedPassword);
                principal = principal + "@" + GetRealm();
                KerberosTime timestamp = new KerberosTime();
                foreach (KeyValuePair <EncryptionType, EncryptionKey> entry in KerberosKeyFactory.
                         GetKerberosKeys(principal, generatedPassword))
                {
                    EncryptionKey ekey       = entry.Value;
                    byte          keyVersion = unchecked ((byte)ekey.GetKeyVersion());
                    entries.AddItem(new KeytabEntry(principal, 1L, timestamp, keyVersion, ekey));
                }
            }
            keytab.SetEntries(entries);
            keytab.Write(keytabFile);
        }