summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/addrbook/modules/LDAPMessage.jsm
blob: 6ee7574605951589e32bc060b6116ddd6f9f1882 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const EXPORTED_SYMBOLS = [
  "AbandonRequest",
  "BindRequest",
  "UnbindRequest",
  "SearchRequest",
  "LDAPResponse",
];

var { asn1js } = ChromeUtils.importESModule("resource:///modules/asn1js.mjs");

/**
 * A base class for all LDAP request and response messages, see
 * rfc4511#section-4.1.1.
 *
 * @property {number} messageId - The message id.
 * @property {LocalBaseBlock} protocolOp - The message content, in a data
 *   structure provided by asn1js.
 */
class LDAPMessage {
  /**
   * Encode the current message by Basic Encoding Rules (BER).
   *
   * @param {number} messageId - The id of the current message.
   * @returns {ArrayBuffer} BER encoded message.
   */
  toBER(messageId = this.messageId) {
    let msg = new asn1js.Sequence({
      value: [new asn1js.Integer({ value: messageId }), this.protocolOp],
    });
    return msg.toBER();
  }

  static TAG_CLASS_APPLICATION = 2;
  static TAG_CLASS_CONTEXT = 3;

  /**
   * Get the idBlock of [APPLICATION n].
   *
   * @param {number} tagNumber - The tag number of this block.
   */
  _getApplicationId(tagNumber) {
    return {
      tagClass: LDAPMessage.TAG_CLASS_APPLICATION,
      tagNumber,
    };
  }

  /**
   * Get the idBlock of context-specific [n].
   *
   * @param {number} tagNumber - The tag number of this block.
   */
  _getContextId(tagNumber) {
    return {
      tagClass: LDAPMessage.TAG_CLASS_CONTEXT,
      tagNumber,
    };
  }

  /**
   * Create a string block with context-specific [n].
   *
   * @param {number} tagNumber - The tag number of this block.
   * @param {string} value - The string value of this block.
   * @returns {LocalBaseBlock}
   */
  _contextStringBlock(tagNumber, value) {
    return new asn1js.Primitive({
      idBlock: this._getContextId(tagNumber),
      valueHex: new TextEncoder().encode(value),
    });
  }
}

class BindRequest extends LDAPMessage {
  static APPLICATION = 0;

  AUTH_SIMPLE = 0;
  AUTH_SASL = 3;

  /**
   * @param {string} dn - The name to bind.
   * @param {string} password - The password.
   * @param {object} sasl - The SASL configs.
   * @param {string} sasl.mechanism - The SASL mechanism e.g. sasl-gssapi.
   * @param {Uint8Array} sasl.credentials - The credential token for the request.
   */
  constructor(dn, password, sasl) {
    super();
    let authBlock;
    if (sasl) {
      authBlock = new asn1js.Constructed({
        idBlock: this._getContextId(this.AUTH_SASL),
        value: [
          new asn1js.OctetString({
            valueHex: new TextEncoder().encode(sasl.mechanism),
          }),
          new asn1js.OctetString({
            valueHex: sasl.credentials,
          }),
        ],
      });
    } else {
      authBlock = new asn1js.Primitive({
        idBlock: this._getContextId(this.AUTH_SIMPLE),
        valueHex: new TextEncoder().encode(password),
      });
    }
    this.protocolOp = new asn1js.Constructed({
      // [APPLICATION 0]
      idBlock: this._getApplicationId(BindRequest.APPLICATION),
      value: [
        // version
        new asn1js.Integer({ value: 3 }),
        // name
        new asn1js.OctetString({
          valueHex: new TextEncoder().encode(dn),
        }),
        // authentication
        authBlock,
      ],
    });
  }
}

class UnbindRequest extends LDAPMessage {
  static APPLICATION = 2;

  protocolOp = new asn1js.Primitive({
    // [APPLICATION 2]
    idBlock: this._getApplicationId(UnbindRequest.APPLICATION),
  });
}

class SearchRequest extends LDAPMessage {
  static APPLICATION = 3;

  // Filter CHOICE.
  FILTER_AND = 0;
  FILTER_OR = 1;
  FILTER_NOT = 2;
  FILTER_EQUALITY_MATCH = 3;
  FILTER_SUBSTRINGS = 4;
  FILTER_GREATER_OR_EQUAL = 5;
  FILTER_LESS_OR_EQUAL = 6;
  FILTER_PRESENT = 7;
  FILTER_APPROX_MATCH = 8;
  FILTER_EXTENSIBLE_MATCH = 9;

  // SubstringFilter SEQUENCE.
  SUBSTRINGS_INITIAL = 0;
  SUBSTRINGS_ANY = 1;
  SUBSTRINGS_FINAL = 2;

  // MatchingRuleAssertion SEQUENCE.
  MATCHING_RULE = 1; // optional
  MATCHING_TYPE = 2; // optional
  MATCHING_VALUE = 3;
  MATCHING_DN = 4; // default to FALSE

  /**
   * @param {string} dn - The name to search.
   * @param {number} scope - The scope to search.
   * @param {string} filter - The filter string, e.g. "(&(|(k1=v1)(k2=v2)))".
   * @param {string} attributes - Attributes to include in the search result.
   * @param {number} timeout - The seconds to wait.
   * @param {number} limit - Maximum number of entries to return.
   */
  constructor(dn, scope, filter, attributes, timeout, limit) {
    super();
    this.protocolOp = new asn1js.Constructed({
      // [APPLICATION 3]
      idBlock: this._getApplicationId(SearchRequest.APPLICATION),
      value: [
        // base DN
        new asn1js.OctetString({
          valueHex: new TextEncoder().encode(dn),
        }),
        // scope
        new asn1js.Enumerated({
          value: scope,
        }),
        // derefAliases
        new asn1js.Enumerated({
          value: 0,
        }),
        // sizeLimit
        new asn1js.Integer({ value: limit }),
        // timeLimit
        new asn1js.Integer({ value: timeout }),
        // typesOnly
        new asn1js.Boolean({ value: false }),
        // filter
        this._convertFilterToBlock(filter),
        // attributes
        new asn1js.Sequence({
          value: attributes
            .split(",")
            .filter(Boolean)
            .map(
              attr =>
                new asn1js.OctetString({
                  valueHex: new TextEncoder().encode(attr),
                })
            ),
        }),
      ],
    });
  }

  /**
   * Parse a single filter value "key=value" to [filterId, key, value].
   *
   * @param {string} filter - A single filter value without parentheses.
   * @returns {(number|string)[]} An array [filterId, key, value] as
   *   [number, string, string]
   */
  _parseFilterValue(filter) {
    for (let cond of [">=", "<=", "~=", ":=", "="]) {
      let index = filter.indexOf(cond);
      if (index > 0) {
        let k = filter.slice(0, index);
        let v = filter.slice(index + cond.length);
        let filterId = {
          ">=": this.FILTER_GREATER_OR_EQUAL,
          "<=": this.FILTER_LESS_OR_EQUAL,
          "~=": this.FILTER_APPROX_MATCH,
          ":=": this.FILTER_EXTENSIBLE_MATCH,
        }[cond];
        if (!filterId) {
          if (v == "*") {
            filterId = this.FILTER_PRESENT;
          } else if (!v.includes("*")) {
            filterId = this.FILTER_EQUALITY_MATCH;
          } else {
            filterId = this.FILTER_SUBSTRINGS;
            v = v.split("*");
          }
        }
        return [filterId, k, v];
      }
    }
    throw Components.Exception(
      `Invalid filter: ${filter}`,
      Cr.NS_ERROR_ILLEGAL_VALUE
    );
  }

  /**
   * Parse a full filter string to an array of tokens.
   *
   * @param {string} filter - The full filter string to parse.
   * @param {number} depth - The depth of a token.
   * @param {object[]} tokens - The tokens to return.
   * @param {"op"|"field"} tokens[].type - The token type.
   * @param {number} tokens[].depth - The token depth.
   * @param {string|string[]} tokens[].value - The token value.
   */
  _parseFilter(filter, depth = 0, tokens = []) {
    while (filter[0] == ")" && depth > 0) {
      depth--;
      filter = filter.slice(1);
    }
    if (filter.length == 0) {
      // End of input.
      return tokens;
    }
    if (filter[0] != "(") {
      throw Components.Exception(
        `Invalid filter: ${filter}`,
        Cr.NS_ERROR_ILLEGAL_VALUE
      );
    }
    filter = filter.slice(1);
    let nextOpen = filter.indexOf("(");
    let nextClose = filter.indexOf(")");

    if (nextOpen != -1 && nextOpen < nextClose) {
      // Case: "OP("
      depth++;
      tokens.push({
        type: "op",
        depth,
        value: {
          "&": this.FILTER_AND,
          "|": this.FILTER_OR,
          "!": this.FILTER_NOT,
        }[filter.slice(0, nextOpen)],
      });
      this._parseFilter(filter.slice(nextOpen), depth, tokens);
    } else if (nextClose != -1) {
      // Case: "key=value)"
      tokens.push({
        type: "field",
        depth,
        value: this._parseFilterValue(filter.slice(0, nextClose)),
      });
      this._parseFilter(filter.slice(nextClose + 1), depth, tokens);
    }
    return tokens;
  }

  /**
   * Parse a filter string to a LocalBaseBlock.
   *
   * @param {string} filter - The filter string to parse.
   * @returns {LocalBaseBlock}
   */
  _convertFilterToBlock(filter) {
    if (!filter.startsWith("(")) {
      // Make sure filter is wrapped in parens, see rfc2254#section-4.
      filter = `(${filter})`;
    }
    let tokens = this._parseFilter(filter);
    let stack = [];
    for (let { type, depth, value } of tokens) {
      while (depth < stack.length) {
        // We are done with the current block, go one level up.
        stack.pop();
      }
      if (type == "op") {
        if (depth == stack.length) {
          // We are done with the current block, go one level up.
          stack.pop();
        }
        // Found a new block, go one level down.
        let parent = stack.slice(-1)[0];
        let curBlock = new asn1js.Constructed({
          idBlock: this._getContextId(value),
        });
        stack.push(curBlock);
        if (parent) {
          parent.valueBlock.value.push(curBlock);
        }
      } else if (type == "field") {
        let [tagNumber, field, fieldValue] = value;
        let block;
        let idBlock = this._getContextId(tagNumber);
        if (tagNumber == this.FILTER_PRESENT) {
          // A present filter.
          block = new asn1js.Primitive({
            idBlock,
            valueHex: new TextEncoder().encode(field),
          });
        } else if (tagNumber == this.FILTER_EXTENSIBLE_MATCH) {
          // An extensibleMatch filter is in the form of
          // <type>:dn:<rule>:=<value>. We need to further parse the field.
          let parts = field.split(":");
          let value = [];
          if (parts.length == 3) {
            // field is <type>:dn:<rule>.
            if (parts[2]) {
              value.push(
                this._contextStringBlock(this.MATCHING_RULE, parts[2])
              );
            }
            if (parts[0]) {
              value.push(
                this._contextStringBlock(this.MATCHING_TYPE, parts[0])
              );
            }
            value.push(
              this._contextStringBlock(this.MATCHING_VALUE, fieldValue)
            );
            if (parts[1] == "dn") {
              let dn = new asn1js.Boolean({
                value: true,
              });
              dn.idBlock.tagClass = LDAPMessage.TAG_CLASS_CONTEXT;
              dn.idBlock.tagNumber = this.MATCHING_DN;
              value.push(dn);
            }
          } else if (parts.length == 2) {
            // field is <type>:<rule>.
            if (parts[1]) {
              value.push(
                this._contextStringBlock(this.MATCHING_RULE, parts[1])
              );
            }

            if (parts[0]) {
              value.push(
                this._contextStringBlock(this.MATCHING_TYPE, parts[0])
              );
            }
            value.push(
              this._contextStringBlock(this.MATCHING_VALUE, fieldValue)
            );
          } else {
            // field is <type>.
            value = [
              this._contextStringBlock(this.MATCHING_TYPE, field),
              this._contextStringBlock(this.MATCHING_VALUE, fieldValue),
            ];
          }
          block = new asn1js.Constructed({
            idBlock,
            value,
          });
        } else if (tagNumber != this.FILTER_SUBSTRINGS) {
          // A filter that is not substrings filter.
          block = new asn1js.Constructed({
            idBlock,
            value: [
              new asn1js.OctetString({
                valueHex: new TextEncoder().encode(field),
              }),
              new asn1js.OctetString({
                valueHex: new TextEncoder().encode(fieldValue),
              }),
            ],
          });
        } else {
          // A substrings filter.
          let substringsSeq = new asn1js.Sequence();
          block = new asn1js.Constructed({
            idBlock,
            value: [
              new asn1js.OctetString({
                valueHex: new TextEncoder().encode(field),
              }),
              substringsSeq,
            ],
          });
          for (let i = 0; i < fieldValue.length; i++) {
            let v = fieldValue[i];
            if (!v.length) {
              // Case: *
              continue;
            } else if (i < fieldValue.length - 1) {
              // Case: abc*
              substringsSeq.valueBlock.value.push(
                new asn1js.Primitive({
                  idBlock: this._getContextId(
                    i == 0 ? this.SUBSTRINGS_INITIAL : this.SUBSTRINGS_ANY
                  ),
                  valueHex: new TextEncoder().encode(v),
                })
              );
            } else {
              // Case: *abc
              substringsSeq.valueBlock.value.push(
                new asn1js.Primitive({
                  idBlock: this._getContextId(this.SUBSTRINGS_FINAL),
                  valueHex: new TextEncoder().encode(v),
                })
              );
            }
          }
        }
        let curBlock = stack.slice(-1)[0];
        if (curBlock) {
          curBlock.valueBlock.value.push(block);
        } else {
          stack.push(block);
        }
      }
    }

    return stack[0];
  }
}

class AbandonRequest extends LDAPMessage {
  static APPLICATION = 16;

  /**
   * @param {string} messageId - The messageId to abandon.
   */
  constructor(messageId) {
    super();
    this.protocolOp = new asn1js.Integer({ value: messageId });
    // [APPLICATION 16]
    this.protocolOp.idBlock.tagClass = LDAPMessage.TAG_CLASS_APPLICATION;
    this.protocolOp.idBlock.tagNumber = AbandonRequest.APPLICATION;
  }
}

class LDAPResult {
  /**
   * @param {number} resultCode - The result code.
   * @param {string} matchedDN - For certain result codes, matchedDN is the last entry used.
   * @param {string} diagnosticMessage - A diagnostic message returned by the server.
   */
  constructor(resultCode, matchedDN, diagnosticMessage) {
    this.resultCode = resultCode;
    this.matchedDN = matchedDN;
    this.diagnosticMessage = diagnosticMessage;
  }
}

/**
 * A base class for all LDAP response messages.
 *
 * @property {LDAPResult} result - The result of a response.
 */
class LDAPResponse extends LDAPMessage {
  /**
   * @param {number} messageId - The message id.
   * @param {LocalBaseBlock} protocolOp - The message content.
   * @param {number} byteLength - The byte size of this message in raw BER form.
   */
  constructor(messageId, protocolOp, byteLength) {
    super();
    this.messageId = messageId;
    this.protocolOp = protocolOp;
    this.byteLength = byteLength;
  }

  /**
   * Find the corresponding response class name from a tag number.
   *
   * @param {number} tagNumber - The tag number of a block.
   * @returns {LDAPResponse}
   */
  static _getResponseClassFromTagNumber(tagNumber) {
    return [
      SearchResultEntry,
      SearchResultDone,
      SearchResultReference,
      BindResponse,
      ExtendedResponse,
    ].find(x => x.APPLICATION == tagNumber);
  }

  /**
   * Decode a raw server response to LDAPResponse instance.
   *
   * @param {ArrayBuffer} buffer - The raw message received from the server.
   * @returns {LDAPResponse} A concrete instance of LDAPResponse subclass.
   */
  static fromBER(buffer) {
    let decoded = asn1js.fromBER(buffer);
    if (decoded.offset == -1 || decoded.result.error) {
      throw Components.Exception(
        decoded.result.error,
        Cr.NS_ERROR_CANNOT_CONVERT_DATA
      );
    }
    let value = decoded.result.valueBlock.value;
    let protocolOp = value[1];
    if (protocolOp.idBlock.tagClass != this.TAG_CLASS_APPLICATION) {
      throw Components.Exception(
        `Unexpected tagClass ${protocolOp.idBlock.tagClass}`,
        Cr.NS_ERROR_ILLEGAL_VALUE
      );
    }
    let ProtocolOp = this._getResponseClassFromTagNumber(
      protocolOp.idBlock.tagNumber
    );
    if (!ProtocolOp) {
      throw Components.Exception(
        `Unexpected tagNumber ${protocolOp.idBlock.tagNumber}`,
        Cr.NS_ERROR_ILLEGAL_VALUE
      );
    }
    let op = new ProtocolOp(
      value[0].valueBlock.valueDec,
      protocolOp,
      decoded.offset
    );
    op.parse();
    return op;
  }

  /**
   * Parse the protocolOp part of a LDAPMessage to LDAPResult. For LDAP
   * responses that are simply LDAPResult, reuse this function. Other responses
   * need to implement this function.
   */
  parse() {
    let value = this.protocolOp.valueBlock.value;
    let resultCode = value[0].valueBlock.valueDec;
    let matchedDN = new TextDecoder().decode(value[1].valueBlock.valueHex);
    let diagnosticMessage = new TextDecoder().decode(
      value[2].valueBlock.valueHex
    );
    this.result = new LDAPResult(resultCode, matchedDN, diagnosticMessage);
  }
}

class BindResponse extends LDAPResponse {
  static APPLICATION = 1;

  parse() {
    super.parse();
    let serverSaslCredsBlock = this.protocolOp.valueBlock.value[3];
    if (serverSaslCredsBlock) {
      this.result.serverSaslCreds = serverSaslCredsBlock.valueBlock.valueHex;
    }
  }
}

class SearchResultEntry extends LDAPResponse {
  static APPLICATION = 4;

  parse() {
    let value = this.protocolOp.valueBlock.value;
    let objectName = new TextDecoder().decode(value[0].valueBlock.valueHex);
    let attributes = {};
    for (let attr of value[1].valueBlock.value) {
      let attrValue = attr.valueBlock.value;
      let type = new TextDecoder().decode(attrValue[0].valueBlock.valueHex);
      let vals = attrValue[1].valueBlock.value.map(v => v.valueBlock.valueHex);
      attributes[type] = vals;
    }
    this.result = { objectName, attributes };
  }
}

class SearchResultDone extends LDAPResponse {
  static APPLICATION = 5;
}

class SearchResultReference extends LDAPResponse {
  static APPLICATION = 19;

  parse() {
    let value = this.protocolOp.valueBlock.value;
    this.result = value.map(block =>
      new TextDecoder().decode(block.valueBlock.valueHex)
    );
  }
}

class ExtendedResponse extends LDAPResponse {
  static APPLICATION = 24;
}