add.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package ldap
  2. import (
  3. "log"
  4. ber "github.com/go-asn1-ber/asn1-ber"
  5. )
  6. // Attribute represents an LDAP attribute
  7. type Attribute struct {
  8. // Type is the name of the LDAP attribute
  9. Type string
  10. // Vals are the LDAP attribute values
  11. Vals []string
  12. }
  13. func (a *Attribute) encode() *ber.Packet {
  14. seq := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Attribute")
  15. seq.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, a.Type, "Type"))
  16. set := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSet, nil, "AttributeValue")
  17. for _, value := range a.Vals {
  18. set.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, value, "Vals"))
  19. }
  20. seq.AppendChild(set)
  21. return seq
  22. }
  23. // AddRequest represents an LDAP AddRequest operation
  24. type AddRequest struct {
  25. // DN identifies the entry being added
  26. DN string
  27. // Attributes list the attributes of the new entry
  28. Attributes []Attribute
  29. // Controls hold optional controls to send with the request
  30. Controls []Control
  31. }
  32. func (req *AddRequest) appendTo(envelope *ber.Packet) error {
  33. pkt := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationAddRequest, nil, "Add Request")
  34. pkt.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, req.DN, "DN"))
  35. attributes := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Attributes")
  36. for _, attribute := range req.Attributes {
  37. attributes.AppendChild(attribute.encode())
  38. }
  39. pkt.AppendChild(attributes)
  40. envelope.AppendChild(pkt)
  41. if len(req.Controls) > 0 {
  42. envelope.AppendChild(encodeControls(req.Controls))
  43. }
  44. return nil
  45. }
  46. // Attribute adds an attribute with the given type and values
  47. func (req *AddRequest) Attribute(attrType string, attrVals []string) {
  48. req.Attributes = append(req.Attributes, Attribute{Type: attrType, Vals: attrVals})
  49. }
  50. // NewAddRequest returns an AddRequest for the given DN, with no attributes
  51. func NewAddRequest(dn string, controls []Control) *AddRequest {
  52. return &AddRequest{
  53. DN: dn,
  54. Controls: controls,
  55. }
  56. }
  57. // Add performs the given AddRequest
  58. func (l *Conn) Add(addRequest *AddRequest) error {
  59. msgCtx, err := l.doRequest(addRequest)
  60. if err != nil {
  61. return err
  62. }
  63. defer l.finishMessage(msgCtx)
  64. packet, err := l.readPacket(msgCtx)
  65. if err != nil {
  66. return err
  67. }
  68. if packet.Children[1].Tag == ApplicationAddResponse {
  69. err := GetLDAPError(packet)
  70. if err != nil {
  71. return err
  72. }
  73. } else {
  74. log.Printf("Unexpected Response: %d", packet.Children[1].Tag)
  75. }
  76. return nil
  77. }