-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcrl.js
158 lines (134 loc) · 3.11 KB
/
crl.js
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
/* vim: set ts=8 sw=4 tw=0 noet : */
/*
* Copyright (c) 2020, Indian Institute of Science, Bengaluru
*
* Authors:
* --------
* Arun Babu {barun <at> iisc <dot> ac <dot> in}
* Bryan Robert {bryanrobert <at> iisc <dot> ac <dot> in}
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
"use strict";
const fs = require("fs");
const os = require("os");
const logger = require("node-color-log");
const request = require("sync-request");
const pgNativeClient = require("pg-native");
const pg = new pgNativeClient();
const db_password = fs.readFileSync (
"passwords/update_crl.db.password",
"ascii"
).trim();
const is_openbsd = os.type() === "OpenBSD";
const EUID = process.geteuid();
const pledge = is_openbsd ? require("node-pledge") : null;
const unveil = is_openbsd ? require("openbsd-unveil"): null;
pg.connectSync (
"postgresql://update_crl:"+ db_password+ "@127.0.0.1:5432/postgres",
(err) => {
if(err)
throw err;
}
);
let crl = [];
function log(color, msg)
{
const message = new Date() + " | " + msg;
logger.color(color).log(message);
}
function update_crl (body)
{
let new_crl;
try
{
new_crl = JSON.parse(body);
}
catch (x)
{
const err = String(x).replace(/\n/g," ");
log("red","IUDX CA did not return a valid JSON : " + err);
return;
}
let updated = false;
if (new_crl.length !== crl.length)
{
updated = true;
}
else
{
// compare both
for (let i = 0; i < crl.length; ++i)
{
for (const key in crl[i])
{
if (crl[i][key] !== new_crl[i][key])
{
updated = true;
break;
}
}
if (updated)
break;
}
}
if (updated)
{
const results = pg.querySync (
"UPDATE crl SET crl = $1::json",
[JSON.stringify(new_crl)]
);
if (results.rowCount === 0) {
log("red","CRL update failed!");
}
else
{
crl = new_crl;
log("yellow","CRL updated!");
}
}
else
{
log("green","CRL is the same");
}
}
if (EUID === 0)
{
process.setgid("nogroup");
process.setuid("nobody");
}
if (is_openbsd)
{
unveil("/usr/local/bin/node", "x");
unveil("/usr/bin/nc", "x");
unveil("/usr/lib", "r");
unveil("/usr/libexec/ld.so", "r");
pledge.init ("stdio tty prot_exec rpath inet recvfd exec proc");
}
function run()
{
let res;
try
{
res = request("GET","https://ca.iudx.org.in/crl");
}
catch (x)
{
const err = String(x).replace(/\n/g," ");
log("red", "Error in getting CRL :" + err);
return;
}
update_crl(res.getBody());
}
run();
setInterval(run,3600000);