summaryrefslogtreecommitdiff
path: root/monitor/josef_leveldb.py
blob: 1d3bdbd6cd4d2cb05da32917ff51a8632d5a2c98 (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
#!/usr/bin/python
# -*- coding: utf-8 -*-     

import sys
from josef_lib import *
import leveldb

SEP = ";"
# db = None


def match_domain(d1, d2):
    # Exact match
    if d1 == d2:
        return True
    
    # Wildcard match
    d1l = d1.split('.')
    d2l = d2.split('.')

    if d1l[0] == '*':
        if d1l[1:] == d2l[-(len(d1l)-1):]:
            return True

    if d2l[0] == '*':
        if d2l[1:] == d1l[-(len(d2l)-1):]:
            return True

    # No match
    return False


def db_open(fn='./cert_db'):
    db = leveldb.LevelDB(fn)
    return db

def db_append(db, key, val):
    # print "Appending " + val
    if db is None:
        print "ERROR: NO DATABASE SET!"
        return

    try:
        tmp = db.Get(key)
    except KeyError:
        tmp = ""
    # print "Key: ", key, "Val: ", val, "Found: ", tmp
    tmpl = tmp.split(SEP)
    if val in tmpl:
        pass
    else:
        # print tmpl
        tmpl.append(val)
        # print tmpl
        db.Put(key,SEP.join(tmpl))
        # print "Storing " + SEP.join(tmpl)

def db_add_domain_2(db, key, val):
    # print "Calling: key:",key," val:",val
    # print "Appending " + val
    if db is None:
        print "ERROR: NO DATABASE SET!"
        return

    try:
        tmp = db.Get(key)
        tmpl = tmp.split(SEP)
        if val in tmpl:
            return
        else:
            tmpl.append(val)
            # print "Putting key:",key," val:",SEP.join(tmpl)
            db.Put(key,SEP.join(tmpl))
    except KeyError:
        # print "Putting key:",key," val:",SEP.join([val])
        db.Put(key,SEP.join([val]))
        new_key = key.split('.',1)[-1]
        if new_key != key:
            db_add_domain_2(db,new_key,key)


    # tmpl = tmp.split(SEP)
    # if val in tmpl:
    #     pass
    # else:
    #     # print tmpl
    #     tmpl.append(val)
    #     # print tmpl
    #     db.Put(key,SEP.join(tmpl))
        # print "Storing " + SEP.join(tmpl)


def db_add_domain(db, domain, data):
    if db is None:
        print "ERROR: NO DATABASE SET!"
        return

    tmpl = domain.split('.')
    k = ""
    for item in reversed(tmpl):
        if k == "":
            next_k = item
        else:
            next_k = item + '.' + k
            # print "Appending ", k, next_k
            db_append(db, k, next_k)
        k = next_k
    # print "Appending ", k, data
    db_append(db, k, data)


def db_add_certs(db, data):
    if db is None:
        print "ERROR: NO DATABASE SET!"
        return
    for cert in data:
        try:
            db_add_domain(db, cert["subject"].split("CN=")[1], str(cert))
        except IndexError:
            pass
        try:
            for line in cert["SAN"].split("DNS:")[1:]:
                db_add_domain(db, line, str(cert))
        except KeyError:
            pass
        except IndexError:
            pass



def db_lookup_domain(db, domain):
    domain_list = domain.split('.')
    res = []

    cur_domain = domain_list.pop()
    intermediate = db.Get(cur_domain).split(SEP)
    last = False
    one_more = False
    while True:
        try:
            intermediate.remove("")
        except ValueError:
            pass

        try:
            cur_domain = domain_list.pop() + "." + cur_domain
        except IndexError:
            return res
        # Prune
        next_level = []
        # print intermediate
        for item in intermediate:
            # print item
            # if item[0] == '{':
            #     res.append(item)
            if match_domain(cur_domain, item):
                # print item
                try:
                    tmp = db.Get(item).split(SEP)
                    # print tmp
                    for tmp_item in tmp:
                        if tmp_item[0] == '{':
                            res.append(tmp_item)
                        else:
                            next_level.append(tmp_item)
                except KeyError:
                    # print "Could not find " + item
                    pass
                    
        intermediate = next_level
        # if last:
        #     return res
    return res