|
| 1 | +package ldap |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + ber "github.com/go-asn1-ber/asn1-ber" |
| 9 | +) |
| 10 | + |
| 11 | +// Response defines an interface to get data from an LDAP server |
| 12 | +type Response interface { |
| 13 | + Entry() *Entry |
| 14 | + Referral() string |
| 15 | + Controls() []Control |
| 16 | + Err() error |
| 17 | + Next() bool |
| 18 | +} |
| 19 | + |
| 20 | +type searchResponse struct { |
| 21 | + conn *Conn |
| 22 | + ch chan *SearchSingleResult |
| 23 | + |
| 24 | + entry *Entry |
| 25 | + referral string |
| 26 | + controls []Control |
| 27 | + err error |
| 28 | +} |
| 29 | + |
| 30 | +// Entry returns an entry from the given search request |
| 31 | +func (r *searchResponse) Entry() *Entry { |
| 32 | + return r.entry |
| 33 | +} |
| 34 | + |
| 35 | +// Referral returns a referral from the given search request |
| 36 | +func (r *searchResponse) Referral() string { |
| 37 | + return r.referral |
| 38 | +} |
| 39 | + |
| 40 | +// Controls returns controls from the given search request |
| 41 | +func (r *searchResponse) Controls() []Control { |
| 42 | + return r.controls |
| 43 | +} |
| 44 | + |
| 45 | +// Err returns an error when the given search request was failed |
| 46 | +func (r *searchResponse) Err() error { |
| 47 | + return r.err |
| 48 | +} |
| 49 | + |
| 50 | +// Next returns whether next data exist or not |
| 51 | +func (r *searchResponse) Next() bool { |
| 52 | + res, ok := <-r.ch |
| 53 | + if !ok { |
| 54 | + return false |
| 55 | + } |
| 56 | + if res == nil { |
| 57 | + return false |
| 58 | + } |
| 59 | + r.err = res.Error |
| 60 | + if r.err != nil { |
| 61 | + return false |
| 62 | + } |
| 63 | + r.err = r.conn.GetLastError() |
| 64 | + if r.err != nil { |
| 65 | + return false |
| 66 | + } |
| 67 | + r.entry = res.Entry |
| 68 | + r.referral = res.Referral |
| 69 | + r.controls = res.Controls |
| 70 | + return true |
| 71 | +} |
| 72 | + |
| 73 | +func (r *searchResponse) start(ctx context.Context, searchRequest *SearchRequest) { |
| 74 | + go func() { |
| 75 | + defer func() { |
| 76 | + close(r.ch) |
| 77 | + if err := recover(); err != nil { |
| 78 | + r.conn.err = fmt.Errorf("ldap: recovered panic in searchResponse: %v", err) |
| 79 | + } |
| 80 | + }() |
| 81 | + |
| 82 | + if r.conn.IsClosing() { |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + packet := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "LDAP Request") |
| 87 | + packet.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, r.conn.nextMessageID(), "MessageID")) |
| 88 | + // encode search request |
| 89 | + err := searchRequest.appendTo(packet) |
| 90 | + if err != nil { |
| 91 | + r.ch <- &SearchSingleResult{Error: err} |
| 92 | + return |
| 93 | + } |
| 94 | + r.conn.Debug.PrintPacket(packet) |
| 95 | + |
| 96 | + msgCtx, err := r.conn.sendMessage(packet) |
| 97 | + if err != nil { |
| 98 | + r.ch <- &SearchSingleResult{Error: err} |
| 99 | + return |
| 100 | + } |
| 101 | + defer r.conn.finishMessage(msgCtx) |
| 102 | + |
| 103 | + foundSearchSingleResultDone := false |
| 104 | + for !foundSearchSingleResultDone { |
| 105 | + select { |
| 106 | + case <-ctx.Done(): |
| 107 | + r.conn.Debug.Printf("%d: %s", msgCtx.id, ctx.Err().Error()) |
| 108 | + return |
| 109 | + default: |
| 110 | + r.conn.Debug.Printf("%d: waiting for response", msgCtx.id) |
| 111 | + packetResponse, ok := <-msgCtx.responses |
| 112 | + if !ok { |
| 113 | + err := NewError(ErrorNetwork, errors.New("ldap: response channel closed")) |
| 114 | + r.ch <- &SearchSingleResult{Error: err} |
| 115 | + return |
| 116 | + } |
| 117 | + packet, err = packetResponse.ReadPacket() |
| 118 | + r.conn.Debug.Printf("%d: got response %p", msgCtx.id, packet) |
| 119 | + if err != nil { |
| 120 | + r.ch <- &SearchSingleResult{Error: err} |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + if r.conn.Debug { |
| 125 | + if err := addLDAPDescriptions(packet); err != nil { |
| 126 | + r.ch <- &SearchSingleResult{Error: err} |
| 127 | + return |
| 128 | + } |
| 129 | + ber.PrintPacket(packet) |
| 130 | + } |
| 131 | + |
| 132 | + switch packet.Children[1].Tag { |
| 133 | + case ApplicationSearchResultEntry: |
| 134 | + r.ch <- &SearchSingleResult{ |
| 135 | + Entry: &Entry{ |
| 136 | + DN: packet.Children[1].Children[0].Value.(string), |
| 137 | + Attributes: unpackAttributes(packet.Children[1].Children[1].Children), |
| 138 | + }, |
| 139 | + } |
| 140 | + |
| 141 | + case ApplicationSearchResultDone: |
| 142 | + if err := GetLDAPError(packet); err != nil { |
| 143 | + r.ch <- &SearchSingleResult{Error: err} |
| 144 | + return |
| 145 | + } |
| 146 | + if len(packet.Children) == 3 { |
| 147 | + result := &SearchSingleResult{} |
| 148 | + for _, child := range packet.Children[2].Children { |
| 149 | + decodedChild, err := DecodeControl(child) |
| 150 | + if err != nil { |
| 151 | + werr := fmt.Errorf("failed to decode child control: %w", err) |
| 152 | + r.ch <- &SearchSingleResult{Error: werr} |
| 153 | + return |
| 154 | + } |
| 155 | + result.Controls = append(result.Controls, decodedChild) |
| 156 | + } |
| 157 | + r.ch <- result |
| 158 | + } |
| 159 | + foundSearchSingleResultDone = true |
| 160 | + |
| 161 | + case ApplicationSearchResultReference: |
| 162 | + ref := packet.Children[1].Children[0].Value.(string) |
| 163 | + r.ch <- &SearchSingleResult{Referral: ref} |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + r.conn.Debug.Printf("%d: returning", msgCtx.id) |
| 168 | + }() |
| 169 | +} |
| 170 | + |
| 171 | +func newSearchResponse(conn *Conn, bufferSize int) *searchResponse { |
| 172 | + var ch chan *SearchSingleResult |
| 173 | + if bufferSize > 0 { |
| 174 | + ch = make(chan *SearchSingleResult, bufferSize) |
| 175 | + } else { |
| 176 | + ch = make(chan *SearchSingleResult) |
| 177 | + } |
| 178 | + return &searchResponse{ |
| 179 | + conn: conn, |
| 180 | + ch: ch, |
| 181 | + } |
| 182 | +} |
0 commit comments