Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package ldap
  2. import (
  3. "log"
  4. ber "github.com/go-asn1-ber/asn1-ber"
  5. )
  6. // Change operation choices
  7. const (
  8. AddAttribute = 0
  9. DeleteAttribute = 1
  10. ReplaceAttribute = 2
  11. IncrementAttribute = 3 // (https://tools.ietf.org/html/rfc4525)
  12. )
  13. // PartialAttribute for a ModifyRequest as defined in https://tools.ietf.org/html/rfc4511
  14. type PartialAttribute struct {
  15. // Type is the type of the partial attribute
  16. Type string
  17. // Vals are the values of the partial attribute
  18. Vals []string
  19. }
  20. func (p *PartialAttribute) encode() *ber.Packet {
  21. seq := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "PartialAttribute")
  22. seq.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, p.Type, "Type"))
  23. set := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSet, nil, "AttributeValue")
  24. for _, value := range p.Vals {
  25. set.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, value, "Vals"))
  26. }
  27. seq.AppendChild(set)
  28. return seq
  29. }
  30. // Change for a ModifyRequest as defined in https://tools.ietf.org/html/rfc4511
  31. type Change struct {
  32. // Operation is the type of change to be made
  33. Operation uint
  34. // Modification is the attribute to be modified
  35. Modification PartialAttribute
  36. }
  37. func (c *Change) encode() *ber.Packet {
  38. change := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Change")
  39. change.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagEnumerated, uint64(c.Operation), "Operation"))
  40. change.AppendChild(c.Modification.encode())
  41. return change
  42. }
  43. // ModifyRequest as defined in https://tools.ietf.org/html/rfc4511
  44. type ModifyRequest struct {
  45. // DN is the distinguishedName of the directory entry to modify
  46. DN string
  47. // Changes contain the attributes to modify
  48. Changes []Change
  49. // Controls hold optional controls to send with the request
  50. Controls []Control
  51. }
  52. // Add appends the given attribute to the list of changes to be made
  53. func (req *ModifyRequest) Add(attrType string, attrVals []string) {
  54. req.appendChange(AddAttribute, attrType, attrVals)
  55. }
  56. // Delete appends the given attribute to the list of changes to be made
  57. func (req *ModifyRequest) Delete(attrType string, attrVals []string) {
  58. req.appendChange(DeleteAttribute, attrType, attrVals)
  59. }
  60. // Replace appends the given attribute to the list of changes to be made
  61. func (req *ModifyRequest) Replace(attrType string, attrVals []string) {
  62. req.appendChange(ReplaceAttribute, attrType, attrVals)
  63. }
  64. // Increment appends the given attribute to the list of changes to be made
  65. func (req *ModifyRequest) Increment(attrType string, attrVal string) {
  66. req.appendChange(IncrementAttribute, attrType, []string{attrVal})
  67. }
  68. func (req *ModifyRequest) appendChange(operation uint, attrType string, attrVals []string) {
  69. req.Changes = append(req.Changes, Change{operation, PartialAttribute{Type: attrType, Vals: attrVals}})
  70. }
  71. func (req *ModifyRequest) appendTo(envelope *ber.Packet) error {
  72. pkt := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationModifyRequest, nil, "Modify Request")
  73. pkt.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, req.DN, "DN"))
  74. changes := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Changes")
  75. for _, change := range req.Changes {
  76. changes.AppendChild(change.encode())
  77. }
  78. pkt.AppendChild(changes)
  79. envelope.AppendChild(pkt)
  80. if len(req.Controls) > 0 {
  81. envelope.AppendChild(encodeControls(req.Controls))
  82. }
  83. return nil
  84. }
  85. // NewModifyRequest creates a modify request for the given DN
  86. func NewModifyRequest(dn string, controls []Control) *ModifyRequest {
  87. return &ModifyRequest{
  88. DN: dn,
  89. Controls: controls,
  90. }
  91. }
  92. // Modify performs the ModifyRequest
  93. func (l *Conn) Modify(modifyRequest *ModifyRequest) error {
  94. msgCtx, err := l.doRequest(modifyRequest)
  95. if err != nil {
  96. return err
  97. }
  98. defer l.finishMessage(msgCtx)
  99. packet, err := l.readPacket(msgCtx)
  100. if err != nil {
  101. return err
  102. }
  103. if packet.Children[1].Tag == ApplicationModifyResponse {
  104. err := GetLDAPError(packet)
  105. if err != nil {
  106. return err
  107. }
  108. } else {
  109. log.Printf("Unexpected Response: %d", packet.Children[1].Tag)
  110. }
  111. return nil
  112. }