-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathdecode_ldap.c
90 lines (71 loc) · 1.69 KB
/
decode_ldap.c
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
/*
* decode_ldap.c
*
* Lightweight Directory Access Protocol.
*
* Copyright (c) 2000 Dug Song <[email protected]>
*
* $Id: decode_ldap.c,v 1.5 2001/03/15 08:33:01 dugsong Exp $
*/
#include "config.h"
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include "buf.h"
#include "asn1.h"
#include "decode.h"
int
decode_ldap(u_char *buf, int len, u_char *obuf, int olen)
{
struct buf *msg, inbuf, outbuf;
int i, type;
u_char *p;
buf_init(&inbuf, buf, len);
buf_init(&outbuf, obuf, olen);
while (buf_len(&inbuf) > 10) {
/* LDAPMessage */
type = asn1_type(&inbuf);
i = asn1_len(&inbuf);
if (i <= 0 || (msg = buf_tok(&inbuf, NULL, i)) == NULL)
break;
if (type != ASN1_SEQUENCE)
continue;
/* messageID */
type = asn1_type(msg);
i = asn1_len(msg);
if (type != ASN1_INTEGER || i <= 0 || buf_skip(msg, i) < 0)
continue;
/* bindRequest op - APPLICATION[0] SEQUENCE */
if (buf_cmp(msg, "\x60", 1) != 0)
continue;
asn1_type(msg);
asn1_len(msg);
/* version */
type = asn1_type(msg);
i = asn1_len(msg);
if (type != ASN1_INTEGER || i <= 0 || buf_skip(msg, i) < 0)
continue;
/* name */
type = asn1_type(msg);
i = asn1_len(msg);
p = buf_ptr(msg);
if (type != ASN1_STRING || i <= 0 || buf_skip(msg, i) < 0)
continue;
/* simple auth [0] */
if (buf_cmp(msg, "\x80", 1) != 0)
continue;
*(buf_ptr(msg)) = '\0';
buf_skip(msg, 1);
/* passwd */
i = asn1_len(msg);
if (i <= 0 || i > buf_len(msg))
continue;
if (buf_tell(&outbuf) > 0)
buf_put(&outbuf, "\n", 1);
buf_putf(&outbuf, "%s\n", p);
buf_put(&outbuf, buf_ptr(msg), i);
buf_put(&outbuf, "\n", 1);
}
buf_end(&outbuf);
return (buf_len(&outbuf));
}