updated cert check and added linux_execv()
Bryan Roe committed
Jan 18, 2021 at 16:05 UTC
1df705e5abef37e14ae4a421b76b4fb9b1573f83
1 file changed
+97
-17
agents/recoverycore.js
+97
-17
@@ -106,6 +106,81 @@ function pathjoin()
106
// Replace a string with a number if the string is an exact number
107
function toNumberIfNumber(x) { if ((typeof x == 'string') && (+parseInt(x) === x)) { x = parseInt(x); } return x; }
108
109
+function linux_execv(name, agentfilename, sessionid)
110
+{
111
+ var libs = require('monitor-info').getLibInfo('libc');
112
+ var libc = null;
113
+
114
+ while (libs.length > 0)
115
+ {
116
+ try
117
+ {
118
+ libc = require('_GenericMarshal').CreateNativeProxy(libs.pop().path);
119
+ break;
120
+ }
121
+ catch (e)
122
+ {
123
+ libc = null;
124
+ continue;
125
+ }
126
+ }
127
+ if (libc != null)
128
+ {
129
+ try
130
+ {
131
+ libc.CreateMethod('execv');
132
+ }
133
+ catch (e)
134
+ {
135
+ libc = null;
136
+ }
137
+ }
138
+
139
+ if (libc == null)
140
+ {
141
+ // Couldn't find libc.so, fallback to using service manager to restart agent
142
+ if (sessionid != null) { sendConsoleText('Restarting service via service-manager...', sessionid) }
143
+ try
144
+ {
145
+ // restart service
146
+ var s = require('service-manager').manager.getService(name);
147
+ s.restart();
148
+ }
149
+ catch (zz)
150
+ {
151
+ sendConsoleText('Self Update encountered an error trying to restart service', sessionid);
152
+ sendAgentMessage('Self Update encountered an error trying to restart service', 3);
153
+ }
154
+ return;
155
+ }
156
+
157
+ if (sessionid != null) { sendConsoleText('Restarting service via execv()...', sessionid) }
158
+
159
+ var i;
160
+ var args;
161
+ var argarr = [];
162
+ var path = require('_GenericMarshal').CreateVariable(process.execPath);
163
+
164
+ if (require('MeshAgent').getStartupOptions != null)
165
+ {
166
+ var options = require('MeshAgent').getStartupOptions();
167
+ for (i in options)
168
+ {
169
+ argarr.push('--' + i + '="' + options[i] + '"');
170
+ }
171
+ }
172
+
173
+ args = require('_GenericMarshal').CreateVariable((1 + argarr.length) * require('_GenericMarshal').PointerSize);
174
+ for (i = 0; i < argarr.length; ++i)
175
+ {
176
+ var arg = require('_GenericMarshal').CreateVariable(argarr[i]);
177
+ arg.pointerBuffer().copy(args.toBuffer(), i * require('_GenericMarshal').PointerSize);
178
+ }
179
+
180
+ libc.execv(path, args);
181
+ if (sessionid != null) { sendConsoleText('Self Update failed because execv() failed', sessionid) }
182
+ sendAgentMessage('Self Update failed because execv() failed', 3);
183
+}
184
185
function bsd_execv(name, agentfilename, sessionid)
186
{
@@ -159,6 +234,7 @@ function bsd_execv(name, agentfilename, sessionid)
234
arg.pointerBuffer().copy(args.toBuffer(), i * require('_GenericMarshal').PointerSize);
235
}
236
237
+ if (sessionid != null) { sendConsoleText('Restarting service via service-manager', sessionid) }
238
libc.execv(path, args);
239
if (sessionid != null) { sendConsoleText('Self Update failed because execv() failed', sessionid) }
240
sendAgentMessage('Self Update failed because execv() failed', 3);
@@ -251,6 +327,7 @@ function agentUpdate_Start(updateurl, updateoptions)
327
sendAgentMessage('Self Update failed, because the url cannot be verified', 3);
328
throw new Error('BadCert');
329
}
330
+ if (certs[0].digest == null) { return; }
331
if ((checkServerIdentity.servertlshash != null) && (checkServerIdentity.servertlshash.toLowerCase() != certs[0].digest.split(':').join('').toLowerCase()))
332
{
333
sendConsoleText('Self Update failed, because the supplied certificate does not match', sessionid);
@@ -313,24 +390,27 @@ function agentUpdate_Start(updateurl, updateoptions)
390
// erase update
391
require('fs').unlinkSync(process.cwd() + agentfilename + '.update');
392
316
- if (sessionid != null) { sendConsoleText('Restarting service...', sessionid); }
317
- if (process.platform == 'freebsd')
393
+ switch(process.platform)
394
{
319
- bsd_execv(name, agentfilename, sessionid);
320
- }
321
- else
322
- {
323
- try
324
- {
325
- // restart service
326
- var s = require('service-manager').manager.getService(name);
327
- s.restart();
328
- }
329
- catch (zz)
330
- {
331
- sendConsoleText('Self Update encountered an error trying to restart service', sessionid);
332
- sendAgentMessage('Self Update encountered an error trying to restart service', 3);
333
- }
395
+ case 'freebsd':
396
+ bsd_execv(name, agentfilename, sessionid);
397
+ break;
398
+ case 'linux':
399
+ linux_execv(name, agentfilename, sessionid);
400
+ break;
401
+ default:
402
+ try
403
+ {
404
+ // restart service
405
+ var s = require('service-manager').manager.getService(name);
406
+ s.restart();
407
+ }
408
+ catch (zz)
409
+ {
410
+ sendConsoleText('Self Update encountered an error trying to restart service', sessionid);
411
+ sendAgentMessage('Self Update encountered an error trying to restart service', 3);
412
+ }
413
+ break;
414
}
415
}
416
});