コード例 #1
0
        private void Query()
        {
            try
            {
                Console.WriteLine("Input path:");
                var path = Console.ReadLine();

                Console.WriteLine("Input your query:");
                var query = Console.ReadLine();

                Console.WriteLine("Recursive search? (y/n):");
                var searchStrategy = LdapConnection.SCOPE_ONE;
                if (Console.ReadKey().Key == ConsoleKey.Y)
                {
                    searchStrategy = LdapConnection.SCOPE_SUB;
                }
                Console.WriteLine();

                LdapSearchConstraints c = new LdapSearchConstraints();
                c.MaxResults = 50;

                LdapSearchResults searchResults = this.connect.Search(path, searchStrategy, query, null, false, c);
                while (searchResults.hasMore())
                {
                    LdapEntry entry = null;
                    try
                    {
                        entry = searchResults.next();
                        //Console.WriteLine(entry.getAttribute("name"));
                        Console.WriteLine(entry.ToString());
                        Console.WriteLine();
                        Console.WriteLine("===============");
                        Console.WriteLine();
                    }
                    catch (LdapException e)
                    {
                        Console.WriteLine("Error: " + e.LdapErrorMessage);
                    }

                    /*
                     * // Get the attribute set of the entry
                     * LdapAttributeSet attributeSet = entry.getAttributeSet();
                     * System.Collections.IEnumerator ienum = attributeSet.GetEnumerator();
                     *
                     * // Parse through the attribute set to get the attributes and the corresponding values
                     * while (ienum.MoveNext())
                     * {
                     *  LdapAttribute attribute = (LdapAttribute)ienum.Current;
                     *  string attributeName = attribute.Name;
                     *  string attributeVal = attribute.StringValue;
                     *  Console.WriteLine(attributeName + "value:" + attributeVal);
                     * }
                     */
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Query ERROR:");
                Console.WriteLine(e.ToString());
                Console.ReadLine();
            }
        }
コード例 #2
0
        public IActionResult OnPost()
        {
            Startup.ldap.EnsureConnection();
            // update details
            if (Startup.migrateSecret.Length < 1)
            {
                ModelState.AddModelError("Secret", "Secret not loaded into the app");
            }
            if (formData.Secret != Startup.migrateSecret)
            {
                ModelState.AddModelError("Secret", "Invalid secret");
            }
            if (!ModelState.IsValid)
            {
                return(OnGet());
            }
            Dictionary <int, IntranetUtils.User> iusers = IntranetUtils.GetCurrentUserList(Startup.Configuration);
            List <LDAPUtils.MemberInfo>          lusers = Startup.ldap.FetchAllMembersInfo();
            var llut = new Dictionary <int, LDAPUtils.MemberInfo>();

            // fill out any missing django account numbers
            foreach (var mi in lusers)
            {
                if (mi.DjangoAccount <= 0)
                {
                    var q = from i in iusers.Values
                            where string.Equals(i.Username, mi.UID, StringComparison.OrdinalIgnoreCase) ||
                            string.Equals(i.Email, mi.Mail, StringComparison.OrdinalIgnoreCase)
                            select i.Id;
                    mi.DjangoAccount = q.SingleOrDefault();
                }
                //Console.WriteLine("Matching user " + mi.UID + " with Django user " + mi.DjangoAccount);
                if (mi.DjangoAccount > 0)
                {
                    llut.Add(mi.DjangoAccount, mi);
                }
            }

            int newUsers = 0, modUsers = 0;

            // update/create user data from intranet in ldap
            foreach (var iu in iusers.Values)
            {
                //Console.WriteLine("Intranet user: <" + iu.Username + ">");
                string   ldapPwd = iu.PasswordHash;
                string[] parts   = iu.PasswordHash.Split('$');
                if (parts.Length >= 4)
                {
                    string algo    = parts[0];
                    string iters   = parts[1];
                    string salt    = parts[2];
                    string bdk     = parts[3].Replace('+', '.').TrimEnd('=');
                    string b64salt = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(salt));
                    ldapPwd = "{PBKDF2-SHA256}" + iters + "$" + b64salt + "$" + bdk;
                }
                string ldapCn = iu.FirstName + " " + iu.LastName;
                if (!llut.ContainsKey(iu.Id))
                {
                    // create entry
                    newUsers++;
                    string dn  = Startup.ldap.Params.DN("uid=" + iu.Username + ",ou=Members");
                    var    ast = new LdapAttributeSet();
                    ast.Add(new LdapAttribute("objectClass", new string[] {
                        "top", "person", "organizationalPerson", "shadowAccount", "inetOrgPerson", "Nextcloud"
                    }));
                    ast.Add(new LdapAttribute("cn", ldapCn));
                    if (iu.FirstName.Length > 0)
                    {
                        ast.Add(new LdapAttribute("givenName", iu.FirstName));
                    }
                    if (iu.LastName.Length > 0)
                    {
                        ast.Add(new LdapAttribute("sn", iu.LastName));
                    }
                    ast.Add(new LdapAttribute("uid", iu.Username));
                    if (iu.PreferredName.Length > 0)
                    {
                        ast.Add(new LdapAttribute("displayName", iu.PreferredName));
                    }
                    ast.Add(new LdapAttribute("employeeNumber", iu.Id.ToString()));
                    if (iu.Email.Length > 0)
                    {
                        ast.Add(new LdapAttribute("mail", iu.Email));
                    }
                    ast.Add(new LdapAttribute("NextcloudQuota", "1GB"));
                    if (iu.PermanentAddress.Length > 0)
                    {
                        ast.Add(new LdapAttribute("postalAddress", iu.PermanentAddress));
                    }
                    if (iu.Room.Length > 0)
                    {
                        ast.Add(new LdapAttribute("roomNumber", iu.Room));
                    }
                    if (iu.PhoneNumber.Length > 0)
                    {
                        ast.Add(new LdapAttribute("telephoneNumber", iu.PhoneNumber));
                    }
                    ast.Add(new LdapAttribute("userPassword", ldapPwd));
                    try
                    {
                        LdapEntry e = new LdapEntry(dn, ast);
                        // update ldap db
                        Startup.ldap.Connection.Add(e);
                        // update model
                        var nmi = new LDAPUtils.MemberInfo(e, Startup.ldap);
                        lusers.Add(nmi);
                        llut.Add(nmi.DjangoAccount, nmi); // */
                    }
                    catch (LdapException e)
                    {
                        Console.WriteLine("Invalid LDAP conversion for " + iu.Username);
                        Console.WriteLine(e.ToString());
                    }
                }
                else
                {
                    // update entry
                    var lu = llut[iu.Id];
                    List <LdapModification> modifications = new List <LdapModification>();
                    if (ldapCn != lu.FullName)
                    {
                        modifications.Add(new LdapModification(LdapModification.Replace,
                                                               new LdapAttribute("cn", ldapCn)));
                    }
                    if (iu.FirstName != lu.FirstName && iu.FirstName.Length > 0)
                    {
                        modifications.Add(new LdapModification(LdapModification.Replace,
                                                               new LdapAttribute("givenName", iu.FirstName)));
                    }
                    if (iu.LastName != lu.Surname && iu.LastName.Length > 0)
                    {
                        modifications.Add(new LdapModification(LdapModification.Replace,
                                                               new LdapAttribute("sn", iu.LastName)));
                    }
                    if (iu.PreferredName != lu.DisplayName && iu.PreferredName.Length > 0)
                    {
                        modifications.Add(new LdapModification(LdapModification.Replace,
                                                               new LdapAttribute("displayName", iu.PreferredName)));
                    }
                    if (iu.Email != lu.Mail && iu.Email.Length > 0)
                    {
                        modifications.Add(new LdapModification(LdapModification.Replace,
                                                               new LdapAttribute("mail", iu.Email)));
                    }
                    if (iu.Room != lu.Flat && iu.Room.Length > 0)
                    {
                        modifications.Add(new LdapModification(LdapModification.Replace,
                                                               new LdapAttribute("roomNumber", iu.Room)));
                    }
                    if (iu.PermanentAddress != lu.Address && iu.PermanentAddress.Length > 0)
                    {
                        modifications.Add(new LdapModification(LdapModification.Replace,
                                                               new LdapAttribute("postalAddress", iu.PermanentAddress)));
                    }
                    if (iu.PhoneNumber != lu.TelephoneNumber && iu.PhoneNumber.Length > 0)
                    {
                        modifications.Add(new LdapModification(LdapModification.Replace,
                                                               new LdapAttribute("telephoneNumber", iu.PhoneNumber)));
                    }
                    if (ldapPwd != lu.Password)
                    {
                        modifications.Add(new LdapModification(LdapModification.Replace,
                                                               new LdapAttribute("userPassword", ldapPwd)));
                    }
                    if (modifications.Count < 1)
                    {
                        continue;
                    }
                    modUsers++;
                    try
                    {
                        Startup.ldap.Connection.Modify(lu.DN, modifications.ToArray());
                    }
                    catch (LdapException e)
                    {
                        Console.WriteLine("Invalid LDAP conversion for " + lu.DN);
                        Console.WriteLine(e.ToString());
                    }
                }
            }

            // move old users (TODO)

            Console.WriteLine("Users: new: {0} modified: {1}", newUsers, modUsers);

            return(Page());
        }