Beispiel #1
0
func main() {
	l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", ldapServer, ldapPort))
	if err != nil {
		log.Fatalf("ERROR: %s\n", err.Error())
	}
	defer l.Close()
	// l.Debug = true

	err = l.Bind(user, passwd)
	if err != nil {
		log.Printf("ERROR: Cannot bind: %s\n", err.Error())
		return
	}
	search := ldap.NewSearchRequest(
		baseDN,
		ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
		filter,
		Attributes,
		nil)

	sr, err := l.Search(search)
	if err != nil {
		log.Fatalf("ERROR: %s\n", err.Error())
		return
	}

	log.Printf("Search: %s -> num of entries = %d\n", search.Filter, len(sr.Entries))
	sr.PrettyPrint(0)
}
Beispiel #2
0
func (c *ConfigEntry) DialLDAP() (*ldap.Conn, error) {

	u, err := url.Parse(c.Url)
	if err != nil {
		return nil, err
	}
	host, port, err := net.SplitHostPort(u.Host)
	if err != nil {
		host = u.Host
	}

	var conn *ldap.Conn
	switch u.Scheme {
	case "ldap":
		if port == "" {
			port = "389"
		}
		conn, err = ldap.Dial("tcp", host+":"+port)
	case "ldaps":
		if port == "" {
			port = "636"
		}
		conn, err = ldap.DialTLS("tcp", host+":"+port, nil)
	default:
		return nil, fmt.Errorf("invalid LDAP scheme")
	}
	if err != nil {
		return nil, fmt.Errorf("cannot connect to LDAP: %v", err)
	}

	return conn, nil
}
Beispiel #3
0
func main() {
	l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", LdapServer, LdapPort))
	if err != nil {
		log.Fatalf("ERROR: %s\n", err.Error())
	}
	defer l.Close()
	// l.Debug = true

	l.Bind(BindDN, BindPW)

	log.Printf("The Search for Kirk ... %s\n", Filter)
	entry, err := search(l, Filter, []string{})
	if err != nil {
		log.Fatal("could not get entry")
	}
	entry.PrettyPrint(0)

	log.Printf("modify the mail address and add a description ... \n")
	modify := ldap.NewModifyRequest(entry.DN)
	modify.Add("description", []string{"Captain of the USS Enterprise"})
	modify.Replace("mail", []string{"*****@*****.**"})
	if err := l.Modify(modify); err != nil {
		log.Fatalf("ERROR: %s\n", err.Error())
	}

	entry, err = search(l, Filter, []string{})
	if err != nil {
		log.Fatal("could not get entry")
	}
	entry.PrettyPrint(0)

	log.Printf("reset the entry ... \n")
	modify = ldap.NewModifyRequest(entry.DN)
	modify.Delete("description", []string{})
	modify.Replace("mail", []string{"*****@*****.**"})
	if err := l.Modify(modify); err != nil {
		log.Fatalf("ERROR: %s\n", err.Error())
	}

	entry, err = search(l, Filter, []string{})
	if err != nil {
		log.Fatal("could not get entry")
	}
	entry.PrettyPrint(0)
}