summaryrefslogtreecommitdiff
path: root/src/frontend.erl
blob: 6fc2fd56152cb44ee6a2d74fbeecfbbfd2d8589f (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
%%% Copyright (c) 2014, NORDUnet A/S.
%%% See LICENSE for licensing information.

%%% @doc Frontend node API

-module(frontend).
%% API (URL)
-export([request/3]).

request(post, "ct/frontend/sendentry", Input) ->
    case (catch mochijson2:decode(Input)) of
        {error, E} ->
            html("sendentry: bad input:", E);
        {struct, PropList} ->
            LogEntry = base64:decode(proplists:get_value(<<"entry">>, PropList)),
            TreeLeafHash = base64:decode(proplists:get_value(<<"treeleafhash">>, PropList)),

            ok = db:add(TreeLeafHash, LogEntry),
            success({[{result, <<"ok">>}]})
    end;

request(post, "ct/frontend/sendlog", Input) ->
    case (catch mochijson2:decode(Input)) of
        {error, E} ->
            html("sendentry: bad input:", E);
        {struct, PropList} ->
            Start = proplists:get_value(<<"start">>, PropList),
            Hashes = lists:map(fun (S) -> base64:decode(S) end, proplists:get_value(<<"hashes">>, PropList)),

            Indices = lists:seq(Start, Start + length(Hashes) - 1),
            lists:foreach(fun ({Hash, Index}) ->
                                  ok = db:add_index(Hash, Index)
                          end, lists:zip(Hashes, Indices)),
            success({[{result, <<"ok">>}]})
    end;

request(post, "ct/frontend/sendsth", Input) ->
    case (catch mochijson2:decode(Input)) of
        {error, E} ->
            html("sendentry: bad input:", E);
        {struct, PropList} ->
            OldSize = db:size(),
            Treesize = proplists:get_value(<<"tree_size">>, PropList),
            RootHash = base64:decode(proplists:get_value(<<"sha256_root_hash">>, PropList)),
            Indexsize = db:indexsize(),

            if
                Treesize < OldSize ->
                    html("Size is older than current size", OldSize);
                Treesize == OldSize ->
                    success({[{result, <<"ok">>}]});
                Treesize > Indexsize ->
                    html("Has too few entries", Indexsize);
                true ->
                    NewEntries = db:leafhash_for_indices(OldSize, Treesize - 1),
                    lager:debug("old size: ~p new size: ~p entries: ~p",
                                [OldSize, Treesize, NewEntries]),

                    Errors = check_entries(NewEntries, OldSize, Treesize - 1),

                    case Errors of
                        [] ->
                            ht:load_tree(Treesize - 1),
                            OwnRootHash = ht:root(Treesize - 1),
                            case OwnRootHash of
                                RootHash ->
                                    ok = db:set_treesize(Treesize),
                                    success({[{result, <<"ok">>}]});
                                _ ->
                                    html("Root hash not the same", hex:bin_to_hexstr(OwnRootHash))
                            end;
                        _ ->
                            html("Database not complete", Errors)
                    end
            end
    end;

request(get, "ct/frontend/currentposition", _Query) ->
    Size = db:size(),
    success({[{result, <<"ok">>},
              {position, Size}]});

request(get, "ct/frontend/missingentries", _Query) ->
    Size = db:size(),
    Missing = fetchmissingentries(Size),
    lager:debug("missingentries: ~p", [Missing]),
    success({[{result, <<"ok">>},
              {entries, lists:map(fun (Entry) -> base64:encode(Entry) end,
                                  Missing)}]}).
check_entries(Entries, Start, End) ->
    lists:foldl(fun ({Hash, Index}, Acc) ->
                        case check_entry(Hash, Index) of
                            ok ->
                                Acc;
                            Error ->
                                [Error | Acc]
                        end
                end, [], lists:zip(Entries, lists:seq(Start, End))).

check_entry(Hash, Index) ->
    case db:get_by_leaf_hash(Hash) of
        notfound ->
            {notfound, Index};
        _ ->
            ok
    end.

fetchmissingentries(Index) ->
    lists:reverse(fetchmissingentries(Index, [])).

fetchmissingentries(Index, Acc) ->
    lager:debug("index ~p", [Index]),
    case db:leafhash_for_index(Index) of
        noentry ->
            Acc;
        Hash ->
            case db:entry_for_leafhash(Hash) of
                noentry ->
                    lager:debug("didn't find hash ~p", [Hash]),
                    fetchmissingentries(Index + 1, [Hash | Acc]);
                _ ->
                    fetchmissingentries(Index + 1, Acc)
            end
    end.


%% Private functions.
html(Text, Input) ->
    {400, [{"Content-Type", "text/html"}],
     io_lib:format(
       "<html><body><p>~n" ++
           "~s~n" ++
           "~p~n" ++
           "</body></html>~n", [Text, Input])}.

success(Data) ->
    {200, [{"Content-Type", "text/json"}], mochijson2:encode(Data)}.