forked from rdkcentral/rdkservices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuploadlogs.cpp
439 lines (360 loc) · 12.1 KB
/
uploadlogs.cpp
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/**
* If not stated otherwise in this file or this component's LICENSE
* file the following copyright and licenses apply:
*
* Copyright 2020 RDK Management
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
#include "uploadlogs.h"
#include <curl/curl.h>
#include <sstream>
#include <map>
#include "SystemServicesHelper.h"
#include "rfcapi.h"
#include "UtilsCStr.h"
#include "UtilsLogging.h"
#include "UtilscRunScript.h"
#include "UtilsfileExists.h"
#define TR181_MTLS_LOGUPLOAD "Device.DeviceInfo.X_RDKCENTRAL-COM_RFC.Feature.MTLS.mTlsLogUpload.Enable"
#define TR181_LOGUPLOAD_BEF_DEEPSLEEP "Device.DeviceInfo.X_RDKCENTRAL-COM_RFC.Feature.LogUploadBeforeDeepSleep.Enable"
namespace WPEFramework
{
namespace Plugin
{
namespace UploadLogs
{
namespace
{
const string DEFAULT_SSR_URL = "https://ssr.ccp.xcal.tv/cgi-bin/rdkb_snmp.cgi";
err_t getFilename(string& filename)
{
err_t ret = OK;
string mac = Utils::cRunScript(". /lib/rdk/utils.sh && getMacAddressOnly");
removeCharsFromString(mac, "\n\r ");
if (mac.empty())
ret = FilenameFail;
else
filename = mac + "_Logs_" + currentDateTimeUtc("%m-%d-%y-%I-%M%p") + ".tgz";
return ret;
}
size_t ssrWrite(char *data, size_t size, size_t nitems, void *userdata)
{
size_t len = size * nitems;
((std::stringstream *)userdata)->write(data, len);
return len;
}
err_t acquireUploadUrl(const string& ssrUrl, const string& filename, string& uploadUrl)
{
err_t ret = OK;
CURL *curl;
CURLcode res = CURLE_UPLOAD_FAILED;
long http_code = 0;
string data = "filename=" + filename;
std::stringstream write;
curl = curl_easy_init();
if (curl)
{
// curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_URL, C_STR(ssrUrl));
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ssrWrite);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&write);
LOGINFO("curl request to: %s with data: %s", C_STR(ssrUrl), data.c_str());
res = curl_easy_perform(curl);
if (res != CURLE_OK)
LOGERR("curl_easy_perform() failed: %s", curl_easy_strerror(res));
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
LOGINFO("curl response code: %ld", http_code);
curl_easy_cleanup(curl);
}
if (res != CURLE_OK || http_code != 200)
ret = SsrFail;
else
{
uploadUrl = write.str();
LOGINFO("curl response: %s", C_STR(uploadUrl));
}
return ret;
}
err_t archiveLogs(const string& filename, string& path)
{
err_t ret = OK;
string tmp = "/tmp/" + filename;
string cmd = "tar -C /opt/logs -zcf " + tmp + " ./";
Utils::cRunScript(C_STR(cmd));
if (!Utils::fileExists(C_STR(tmp)))
ret = TarFail;
else
path = tmp;
return ret;
}
size_t uploadRead(void *data, size_t size, size_t nitems, void *userdata)
{
return fread(data, size, nitems, (FILE *)userdata);
}
err_t uploadLogs(const string& path, const string& uploadUrl)
{
err_t ret = OK;
CURL *curl;
CURLcode res = CURLE_UPLOAD_FAILED;
long http_code = 0;
FILE *fd;
struct stat file_info;
stat(C_STR(path), &file_info);
fd = fopen(C_STR(path), "rb");
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_READFUNCTION, uploadRead);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl, CURLOPT_PUT, 1L);
curl_easy_setopt(curl, CURLOPT_URL, C_STR(uploadUrl));
curl_easy_setopt(curl, CURLOPT_READDATA, fd);
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 120L);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 60L);
LOGINFO("curl request to: %s", C_STR(uploadUrl));
res = curl_easy_perform(curl);
if (res != CURLE_OK)
LOGERR("curl_easy_perform() failed: %s", curl_easy_strerror(res));
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
LOGINFO("curl response code: %ld", http_code);
curl_easy_cleanup(curl);
}
fclose(fd);
if (res != CURLE_OK || http_code != 200)
ret = UploadFail;
return ret;
}
} // namespace
// similar to /lib/rdk/UploadLogsNow.sh
err_t upload(const std::string& ssrUrl)
{
err_t ret = OK;
string ssr = ssrUrl;
if (ssr.empty())
ssr = DEFAULT_SSR_URL;
if (ssr.rfind("https://", 0) != 0)
ret = BadUrl;
string filename;
if (ret == OK)
{
LOGINFO("ssr: %s", C_STR(ssr));
ret = getFilename(filename);
}
string uploadUrl;
if (ret == OK)
{
LOGINFO("filename: %s", C_STR(filename));
ret = acquireUploadUrl(ssr, filename, uploadUrl);
}
string path;
if (ret == OK)
{
LOGINFO("uploadUrl: %s", C_STR(uploadUrl));
ret = archiveLogs(filename, path);
}
if (ret == OK)
{
LOGINFO("path: %s", C_STR(path));
ret = uploadLogs(path, uploadUrl);
int removeStatus = remove(C_STR(path));
LOGINFO("remove %s exit code %d", C_STR(path), removeStatus);
}
return ret;
}
std::string errToText(err_t err)
{
static std::map<err_t, string> _map =
{
{OK, "OK"},
{BadUrl, "invalid or insecure input url"},
{FilenameFail, "can't generate logs filename"},
{SsrFail, "ssr fail"},
{TarFail, "tar fail"},
{UploadFail, "upload fail"},
};
auto it = _map.find(err);
if (it != _map.end())
return it->second;
return "";
}
bool checkXpkiMtlsBasedLogUpload(){
if ( Utils::fileExists("/usr/bin/rdkssacli") &&
Utils::fileExists("/opt/certs/devicecert_1.pk12") ){
return true;
}
else {
return false;
}
}
bool checkmTlsLogUploadFlag(){
bool ret=false;
RFC_ParamData_t param;
WDMP_STATUS wdmpStatus = getRFCParameter(const_cast<char *>("SystemServices"),TR181_MTLS_LOGUPLOAD, ¶m);
if (wdmpStatus == WDMP_SUCCESS || wdmpStatus == WDMP_ERR_DEFAULT_VALUE){
if( param.type == WDMP_BOOLEAN ){
if(strncasecmp(param.value,"true",4) == 0 ){
ret=true;
}
}
}
LOGINFO(" mTlsLogUpload.Enable = %s , call value %d ", (ret == true)?"true":"false", wdmpStatus);
return ret;
}
bool checkLogUploadBeforeDeepSleepFlag(){
bool ret=false;
RFC_ParamData_t param;
WDMP_STATUS wdmpStatus = getRFCParameter(const_cast<char *>("SystemServices"),TR181_LOGUPLOAD_BEF_DEEPSLEEP, ¶m);
if (wdmpStatus == WDMP_SUCCESS || wdmpStatus == WDMP_ERR_DEFAULT_VALUE){
if( param.type == WDMP_BOOLEAN ){
if(strncasecmp(param.value,"true",4) == 0 ){
ret=true;
}
}
}
LOGINFO(" mTlsLogUpload.Enable = %s , call value %d ", (ret == true)?"true":"false", wdmpStatus);
return ret;
}
bool getDCMconfigDetails(string &upload_protocol,string &httplink, string &uploadCheck){
string dcminfo;
if (!getFileContent(TMP_DCM_SETTINGS,dcminfo)) {
return false;
}
if (dcminfo.length() < 1){
return false;
}
smatch match;
string temp;
if (regex_search(dcminfo, match, regex("LogUploadSettings:UploadRepository:uploadProtocol=([^\\n]+)"))
&& match.size() > 1) temp = trim(match[1]);
if (temp.size() > 0) upload_protocol = temp;
if (regex_search(dcminfo, match, regex("LogUploadSettings:UploadRepository:URL=([^\\n]+)"))
&& match.size() > 1) temp = trim(match[1]);
if (temp.size() > 0) httplink = temp;
if (regex_search(dcminfo, match, regex("LogUploadSettings:UploadOnReboot=([^\\n]+)"))
&& match.size() > 1) temp = trim(match[1]);
if (temp.size() > 0) uploadCheck = temp;
return true;
}
std::int32_t getUploadLogParameters(string &tftp_server, string &upload_protocol, string &upload_httplink)
{
string build_type;
string httplink;
string uploadcheck;
string dcmFile, force_mtls;
string calledFromPlugin="1";
bool mTlsLogUpload = false;
mTlsLogUpload = checkmTlsLogUploadFlag();
if ( !parseConfigFile(DEVICE_PROPERTIES,"BUILD_TYPE",build_type) ){
LOGINFO("Failed to get BUILD Type\n");
return E_NOK;
}
if ( ( "prod" != build_type ) && ( Utils::fileExists(OPT_DCM_PROPERTIES) )){
dcmFile=OPT_DCM_PROPERTIES;
}
else{
dcmFile=ETC_DCM_PROPERTIES;
}
if ( !parseConfigFile(dcmFile.c_str(),"LOG_SERVER",tftp_server) ){
LOGINFO("Failed to get Log server\n");
return E_NOK;
}
if (parseConfigFile(DEVICE_PROPERTIES,"FORCE_MTLS",force_mtls) ){
if ( "true" == force_mtls ){
mTlsLogUpload = "true";
}
}
if ( !getDCMconfigDetails(upload_protocol,httplink,uploadcheck)){
LOGINFO("Failed to get DCM configDetails\n");
return E_NOK;
}
upload_httplink = httplink;
if ( mTlsLogUpload || checkXpkiMtlsBasedLogUpload() ){
//Sky endpoint dont use /secure extension
if( "true" != force_mtls ){
//append secure with the url
upload_httplink=regex_replace(httplink,regex("cgi-bin"),"secure/cgi-bin");
}
}
return E_OK;
}
/* Call uploadSTBLogs.sh direclty by preparing the url
* and other details
*/
std::int32_t LogUploadBeforeDeepSleep()
{
/* Check the RFC flag */
if( !checkLogUploadBeforeDeepSleepFlag()){
LOGINFO("LogUploadBeforeDeepSleep RFC is False.\n");
return E_NOK;
}
if ( !Utils::fileExists("/lib/rdk/uploadSTBLogs.sh") ){
return E_NOK;
}
string tftp_server;
string upload_protocol;
string upload_httplink;
string calledFromPlugin="1";
if (E_NOK == getUploadLogParameters(tftp_server, upload_protocol, upload_httplink))
return E_NOK;
string cmd;
cmd = "nice -n 19 /bin/busybox sh /lib/rdk/uploadSTBLogs.sh " + tftp_server + " 0 1 0 " + upload_protocol + " " + upload_httplink + " " + calledFromPlugin + " & " + " \0";
int exec_status = system(cmd.c_str());
LOGINFO("CMD %s [exec_status:%d]",cmd.c_str(), exec_status);
return E_OK;
}
pid_t logUploadAsync(void)
{
if ( !Utils::fileExists("/lib/rdk/uploadSTBLogs.sh") ){
return -1;
}
string tftp_server;
string upload_protocol;
string upload_httplink;
if (E_NOK == getUploadLogParameters(tftp_server, upload_protocol, upload_httplink))
return -1;
const char *argArray[] = {
"/bin/sh",
"/lib/rdk/uploadSTBLogs.sh",
tftp_server.c_str(),
"0", //FLAG,
"1", //DCM_FLAG,
"0", //UploadOnReboot,
upload_protocol.c_str(),
upload_httplink.c_str(),
"1",
0
};
pid_t pid = fork();
if (-1 == pid)
{
LOGERR("Fork failed for %s", argArray[2]);
}
else if (0 == pid)
{
if (execve(argArray[0], (char **)argArray, environ) == -1)
{
LOGERR("Execve failed: %s", strerror(errno));
_Exit(127);
}
}
LOGINFO("Started %d process with %s", pid, argArray[1]);
return pid;
}
} // namespace UploadLogs
} // namespace Plugin
} // namespace WPEFramework