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 }
func (a Auth) Login(username, password string) error { conn, err := ldap.DialTLS("tcp", a.addr, nil) if err != nil { return err } defer conn.Close() err = conn.Bind(fmt.Sprintf(a.dnTemplate, username), password) if err != nil { return err } return nil }
func main() { l, err := ldap.DialTLS("tcp", fmt.Sprintf("%s:%d", LdapServer, LdapPort), nil) if err != nil { log.Fatalf("ERROR: %s\n", err.Error()) } defer l.Close() // l.Debug = true 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) }