簡體   English   中英

如何使用 Golang 向 LDAP 服務器添加新條目?

[英]How to add new entry to LDAP server with Golang?

我在嘗試向 LDAP 服務器添加新條目時遇到問題。 我得到的錯誤如下: LDAP 結果代碼 65 “對象 Class 違規”:沒有提供結構 object ZA2F2ED4F8EBF6EFE22788E03DDEAF0AFZ。 如果有人可以就為什么給我一些意見,那將很有幫助。

我綁定到服務器,所以看起來我只是對實際條目屬性有問題..但我不確定在哪里修復它。

package main

import (
  "fmt"
//  "github.com/go-ldap/ldap"
  "gopkg.in/ldap.v2"
  "log"
)

//List of constants
const (
  host = "127.0.0.1"
  port = "389"
  hostPort = host + ":" + port
  userID = "cn=admin,dc=test123,dc=com"
  password = "password"
)

//Main function to be called
func main() {
  addEntries()
}

//Add entries function
func addEntries(){
  fmt.Println("Adding started")

  //Initialize connection
  l, err := ldap.Dial("tcp", hostPort)

  if err != nil {
    log.Fatal(err)
  }
  defer l.Close()

  //Bind to the LDAP server
  bindusername := "cn=admin,dc=test123,dc=com"
  bindpassword := "password"

  err = l.Bind(bindusername, bindpassword)
  if err != nil {
    log.Fatal(err)
    return
  }

  fmt.Println("Testing.")

  //Create new Add request object to be added to LDAP server.
  a := ldap.NewAddRequest("ou=groups,dc=test123,dc=com")
  a.Attribute("cn", []string{"gotest"})
  a.Attribute("objectClass" ,[]string{"top"})
  a.Attribute("description", []string{"this is a test to add an entry using golang"})
  a.Attribute("sn" ,[]string{"Google"})

  fmt.Println("Testing.")
  add(a , l)




}
func add(addRequest *ldap.AddRequest , l *ldap.Conn) {
  err := l.Add(addRequest)
  if err != nil {
    fmt.Println("Entry NOT done",err)
  } else {
    fmt.Println("Entry DONE",err)
  }
}

創建 LDAP 對象時最常見的錯誤是缺少objectClass和/或對象的強制屬性(例如uidcn等)。下面是一些識別這些要求的技術。


您可以像這樣查詢 LDAP 服務器的架構:

ldapsearch -x -h my.example.com -b "cn=schema" -s base "(objectclass=*)"

output 的可讀性不是很強,但是如果您知道要查找的確切objectClass ,事情就會變得更清楚一些。

例如objectClass: person定義可能如下所示:

objectClasses: ( 2.5.6.6 NAME 'person' DESC 'Defines entries that generically
 represent people.' SUP top STRUCTURAL MUST ( cn $ sn ) MAY ( description $ se
 eAlso $ telephoneNumber $ userPassword ) )

所以你可以看到這是一個STRUCTURAL objectClass(補充top ),如果要創建這樣一個person LDAP object,它:

  • 必須包括: cnsn
  • 和可選: descriptionseeAlsotelephoneNumberuserPassword

各個屬性類型的定義如下:

attributeTypes: ( 2.5.4.3 NAME ( 'cn' 'commonName' ) DESC 'This is the X.500 c
 ommonName attribute, which contains a name of an object.  If the object corre
 sponds to a person, it is typically the persons full name.' SUP 2.5.4.41 EQUA
 LITY 2.5.13.2 ORDERING 2.5.13.3 SUBSTR 2.5.13.4 )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM