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
|
#!/usr/bin/env escript
%% -*- erlang -*-
write_reply(Bin) ->
Length = size(Bin),
file:write(standard_io, <<Length:32, Bin/binary>>).
verify(RootCerts, DBEntry) ->
try
case catlfish:verify_entry(tlv:decodelist(DBEntry), RootCerts) of
{ok, _MTLHash} ->
write_reply(<<0:8>>);
{error, Reason} ->
ReasonBin = list_to_binary(io_lib:format("~p", [Reason])),
write_reply(<<1:8, ReasonBin/binary>>)
end
catch
Type:What ->
[CrashFunction | Stack] = erlang:get_stacktrace(),
ErrorBin = list_to_binary(io_lib:format("Crash: ~p ~p~n~p~n~p~n", [Type, What, CrashFunction, Stack])),
write_reply(<<2:8, ErrorBin/binary>>)
end.
loop(RootCerts) ->
{ok, LengthBin} = file:read(standard_io, 4),
<<Length:32>> = list_to_binary(LengthBin),
case Length of
0 ->
none;
_ ->
{ok, DBEntry} = file:read(standard_io, Length),
verify(RootCerts, list_to_binary(DBEntry)),
loop(RootCerts)
end.
add_lib(Dirs, Name) ->
add_lib(Dirs, Dirs, Name).
add_lib(AllDirs, [], Name) ->
{ok, Cwd} = file:get_cwd(),
io:format(standard_error,
"Could not add lib ~p: tried directories ~p cwd: ~p~n",
[Name, AllDirs, Cwd]),
halt(1);
add_lib(AllDirs, [Dir | Rest], Name) ->
Path = Dir ++ "/" ++ Name ++ ".ez/" ++ Name ++ "/ebin",
case code:add_path(Path) of
true ->
ok;
{error, bad_directory} ->
add_lib(AllDirs, Rest, Name)
end.
main([KnownRoots]) ->
[ScriptFile | _] = init:get_plain_arguments(),
BaseDir = filename:dirname(ScriptFile),
LibDirs = [BaseDir ++ "/../lib", "../lib"],
add_lib(LibDirs, "catlfish-0.9.0-dev"),
add_lib(LibDirs, "lager-2.1.1"),
add_lib(LibDirs, "plop-0.9.0-dev"),
Certs = x509:read_pemfiles_from_dir(KnownRoots),
loop(Certs).
|