如何使用 golang 查询检索 LDAP 条目的所有属性?

How to retrieve all attributes of an LDAP entry with golang query?

我正在使用 gopkg.in/ldap.v2 api 来查询 LDAP 服务器,但我想知道如何在条目出现在查询结果中后检索它的所有属性?这是我的完整程序:


/*In order to use this program, the user needs to get the package by running the following command:
go get gopkg.in/ldap.v2*/
package main

import (
  "fmt"
  "strings"
  "gopkg.in/ldap.v2"
  "os"
)
//Gives constants to be used for binding to and searching the LDAP server.
const (
  ldapServer = "127.0.0.1:389"
  ldapBind = "cn=admin,dc=test123,dc=com"
  ldapPassword = "Password"

  filterDN = "(objectclass=*)"
  baseDN = "dc=test123,dc=com"

  loginUsername = "admin"
  loginPassword = "Password"
)

//Main function, which is executed.
func main() {
  conn, err := connect()

  //If there is an error connecting to server, prints this
  if err != nil {
    fmt.Printf("Failed to connect. %s", err)
    return
  }
  //Close the connection at a later time.
  defer conn.Close()
  //Declares err to be list(conn), and checks if any errors. It prints the error(s) if there are any.
  if err := list(conn); err != nil {
    fmt.Printf("%v", err)
    return
  }

  /*
  //Declares err to be auth(conn), and checks if any errors. It prints the error(s) if there are any.
  if err := auth(conn); err != nil {
    fmt.Printf("%v", err)
    return
  }*/
}

//This function is used to connect to the LDAP server.
func connect() (*ldap.Conn, error) {
  conn, err := ldap.Dial("tcp", ldapServer)

  if err != nil {
    return nil, fmt.Errorf("Failed to connect. %s", err)
  }

  if err := conn.Bind(ldapBind, ldapPassword); err != nil {
    return nil, fmt.Errorf("Failed to bind. %s", err)
  }

  return conn, nil
}

//This function is used to search the LDAP server as well as output the attributes of the entries.
func list(conn *ldap.Conn) error {
  //This gets the command line argument and saves it in the form "(argument=*)"
  arg := ""
  filter := ""
  if len(os.Args) > 1{
    arg = os.Args[1]
    fmt.Println(arg)

    filter = "(" + arg + "=*)"
  } else{
    fmt.Println("You need to input an argument for an attribute to search. I.E. : \"go run anonymous_query.go cn\"")
  }

  result, err := conn.Search(ldap.NewSearchRequest(
    baseDN,
    ldap.ScopeWholeSubtree,
    ldap.NeverDerefAliases,
    0,
    0,
    false,
    fmt.Sprintf(filter),

    //To add anymore strings to the search, you need to add it here.
    []string{"dn", "o", "cn", "ou", "uidNumber", "objectClass",
    "uid", "uidNumber", "gidNumber", "homeDirectory", "loginShell", "gecos",
    "shadowMax", "shadowWarning", "shadowLastChange", "dc", "description", "entryCSN"},
    nil,
  ))

  if err != nil {
    return fmt.Errorf("Failed to search users. %s", err)
  }

  //Prints all the attributes per entry
  for _, entry := range result.Entries {
    entry.Print()
    fmt.Println()
  }

  return nil
}

//This function authorizes the user and binds to the LDAP server.
func auth(conn *ldap.Conn) error {
  result, err := conn.Search(ldap.NewSearchRequest(
    baseDN,
    ldap.ScopeWholeSubtree,
    ldap.NeverDerefAliases,
    0,
    0,
    false,
    filter(loginUsername),
    []string{"dn"},
    nil,
  ))

  if err != nil {
    return fmt.Errorf("Failed to find user. %s", err)
  }

  if len(result.Entries) < 1 {
    return fmt.Errorf("User does not exist")
  }

  if len(result.Entries) > 1 {
    return fmt.Errorf("")
  }

  if err := conn.Bind(result.Entries[0].DN, loginPassword); err != nil {
    fmt.Printf("Failed to auth. %s", err)
  } else {
    fmt.Printf("Authenticated successfuly!")
  }

  return nil
}

func filter(needle string) string {
  res := strings.Replace(
    filterDN,
    "{username}",
    needle,
    -1,
  )

  return res
}

我遇到的问题是这一行:

    //To add anymore strings to the search, you need to add it here.
    []string{"dn", "o", "cn", "ou", "uidNumber", "objectClass",
    "uid", "uidNumber", "gidNumber", "homeDirectory", "loginShell", "gecos",
    "shadowMax", "shadowWarning", "shadowLastChange", "dc", "description", "entryCSN"}

我想要检索 LDAP 条目的所有属性,而不是必须从查询结果中手动键入我想要的所有属性。另一个原因是因为我不知道一个条目可能有什么属性。

如有任何帮助,我们将不胜感激。谢谢!

在 LDAP 搜索操作中,如果您没有为搜索指定属性,它将 return 具有所有属性的条目,所以这将完成工作:

result, err := conn.Search(ldap.NewSearchRequest(
    baseDN,
    ldap.ScopeWholeSubtree,
    ldap.NeverDerefAliases,
    0,
    0,
    false,
    fmt.Sprintf(filter),
    []string{},
    nil,
  ))