-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathinventory.c
More file actions
718 lines (628 loc) · 26.7 KB
/
inventory.c
File metadata and controls
718 lines (628 loc) · 26.7 KB
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
/******************************************************************************/
/* Copyright 2021 Keyfactor */
/* 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 */
/* thespecific language governing permissions and limitations under the */
/* License. */
/******************************************************************************/
#include "inventory.h"
#include "config.h"
#include "constants.h"
#include "httpclient.h"
#include "lib/base64.h"
#include "logging.h"
#include "utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#ifdef __WOLF_SSL__
#include "wolfssl_wrapper/wolfssl_wrapper.h"
#else
#ifdef __OPEN_SSL__
#include "openssl_wrapper/openssl_wrapper.h"
#else
#ifdef __TPM__
#else
#endif
#endif
#endif
/******************************************************************************/
/***************************** LOCAL DEFINES *********************************/
/******************************************************************************/
/******************************************************************************/
/************************ LOCAL GLOBAL STRUCTURES *****************************/
/******************************************************************************/
/******************************************************************************/
/************************* LOCAL GLOBAL VARIABLES *****************************/
/******************************************************************************/
/******************************************************************************/
/************************ LOCAL FUNCTION DEFINITIONS **************************/
/******************************************************************************/
/**
* @brief Requests the inventory job configuration from the platform.
*
* @param[in] sessionToken GUID for the current curl session.
* @param[in] jobId Platform GUID identifying this job.
* @param[in] endpoint Relative URL for the configuration endpoint.
* @param[out] pInvConf Receives the parsed platform response.
* @return 0 on success, non-zero on failure.
*/
static int get_inventory_config(const char *sessionToken, const char *jobId,
const char *endpoint,
InventoryConfigResp_t **pInvConf) {
char *url = NULL;
char *jsonReq = NULL;
char *jsonResp = NULL;
int res = 0;
log_verbose("%s::%s(%d) : Sending inventory config request: %s", LOG_INF,
jobId);
CommonConfigReq_t *req = CommonConfigReq_new();
if (!req) {
log_error("%s::%s(%d) : Out of memory in CommonConfigReq_new()",
LOG_INF);
return -1;
}
req->JobId = strdup(jobId);
req->SessionToken = strdup(sessionToken);
log_trace("%s::%s(%d) : Set job ID to %s", LOG_INF, jobId);
log_trace("%s::%s(%d) : Set session token to %s", LOG_INF, sessionToken);
jsonReq = CommonConfigReq_toJson(req);
log_trace("%s::%s(%d) : Config request JSON: %s", LOG_INF, jsonReq);
url = config_build_url(endpoint, true);
log_trace("%s::%s(%d) : POSTing to %s", LOG_INF, url);
res = http_post_json(url, ConfigData->Username, ConfigData->Password,
ConfigData->TrustStore, ConfigData->AgentCert,
ConfigData->AgentKey, ConfigData->AgentKeyPassword,
jsonReq, &jsonResp, ConfigData->httpRetries,
ConfigData->retryInterval);
if (res == 0) {
*pInvConf = InventoryConfigResp_fromJson(jsonResp);
} else {
log_error("%s::%s(%d) : Config retrieval failed with error code %d",
LOG_INF, res);
}
free(jsonReq);
free(jsonResp);
free(url);
CommonConfigReq_free(req);
return res;
} /* get_inventory_config */
/**
* @brief Submits the computed inventory delta to the platform.
*
* @param[in] sessionToken GUID for the current curl session.
* @param[in] jobId Platform GUID identifying this job.
* @param[in] endpoint Relative URL for the inventory update endpoint.
* @param[in] newInv The computed list of ADD, REMOVE, and UNCHANGED
* items.
* @param[out] pUpdResp Receives the parsed platform acknowledgement.
* @return 0 on success, non-zero on failure.
*/
static int send_inventory_update(const char *sessionToken, const char *jobId,
const char *endpoint,
InventoryUpdateList_t *newInv,
InventoryUpdateResp_t **pUpdResp) {
char *url = NULL;
char *jsonReq = NULL;
char *jsonResp = NULL;
int res = 0;
log_verbose("%s::%s(%d) : Sending inventory update request: %s", LOG_INF,
jobId);
InventoryUpdateReq_t *updReq = calloc(1, sizeof(*updReq));
if (!updReq) {
log_error(
"%s::%s(%d) : Error couldn't allocate update request structure",
LOG_INF);
return 999;
}
updReq->SessionToken = strdup(sessionToken);
updReq->JobId = strdup(jobId);
updReq->Inventory = *newInv;
jsonReq = InventoryUpdateReq_toJson(updReq);
url = config_build_url(endpoint, true);
res = http_post_json(url, ConfigData->Username, ConfigData->Password,
ConfigData->TrustStore, ConfigData->AgentCert,
ConfigData->AgentKey, ConfigData->AgentKeyPassword,
jsonReq, &jsonResp, ConfigData->httpRetries,
ConfigData->retryInterval);
if (res == 0) {
*pUpdResp = InventoryUpdateResp_fromJson(jsonResp);
} else {
log_error("%s::%s(%d) : Update submission failed with error code %d",
LOG_INF, res);
}
if (jsonReq)
free(jsonReq);
if (jsonResp)
free(jsonResp);
if (url)
free(url);
if (updReq)
InventoryUpdateReq_free(updReq);
return res;
} /* send_inventory_update */
/**
* @brief Sends job completion status and message to the platform.
*
* @param[in] sessionToken GUID for the current curl session.
* @param[in] jobId Platform GUID identifying this job.
* @param[in] endpoint Relative URL for the completion endpoint.
* @param[in] jobStatus Numeric status code to report.
* @param[in] auditId Audit record ID associated with this job.
* @param[in] message Human-readable result or error message.
* @param[out] pInvComp Receives the parsed platform acknowledgement.
* @return 0 on success, non-zero on failure.
*/
static int send_inventory_job_complete(const char *sessionToken,
const char *jobId, const char *endpoint,
int jobStatus, long auditId,
const char *message,
CommonCompleteResp_t **pInvComp) {
char *url = NULL;
char *jsonReq = NULL;
char *jsonResp = NULL;
int res = 0;
log_verbose("%s::%s(%d) : Sending inventory complete request: %ld "
"for session: %s",
LOG_INF, auditId, sessionToken);
CommonCompleteReq_t *req = CommonCompleteReq_new();
if (!req) {
log_error("%s::%s(%d) : Error allocating request structure", LOG_INF);
return 999;
}
req->SessionToken = strdup(sessionToken);
req->JobId = strdup(jobId);
req->Status = jobStatus;
req->AuditId = auditId;
req->Message = strdup(message);
jsonReq = CommonCompleteReq_toJson(req);
url = config_build_url(endpoint, true);
res = http_post_json(url, ConfigData->Username, ConfigData->Password,
ConfigData->TrustStore, ConfigData->AgentCert,
ConfigData->AgentKey, ConfigData->AgentKeyPassword,
jsonReq, &jsonResp, ConfigData->httpRetries,
ConfigData->retryInterval);
if (res == 0) {
*pInvComp = CommonCompleteResp_fromJson(jsonResp);
} else {
log_error("%s::%s(%d) : Job completion failed with error code %d",
LOG_INF, res);
}
if (jsonReq)
free(jsonReq);
if (jsonResp)
free(jsonResp);
if (url)
free(url);
if (req)
CommonCompleteReq_free(req);
return res;
} /* send_inventory_job_complete */
/**
* @brief Appends a single item to an inventory update list.
*
* Grows the list's item array by one and stores the pointer. Logs an error
* if the realloc fails; the list is left unmodified in that case.
*
* @param[in,out] list The list to append to.
* @param[in] item The item to append.
*/
static void InventoryUpdateList_add(InventoryUpdateList_t *list,
InventoryUpdateItem_t *item) {
if (!list || !item)
return;
InventoryUpdateItem_t **tmp = realloc(
list->items, (list->count + 1) * sizeof(InventoryUpdateItem_t *));
if (!tmp) {
log_error("%s::%s(%d) : Out of memory", LOG_INF);
return;
}
list->items = tmp;
list->items[list->count] = item;
list->count++;
} /* InventoryUpdateList_add */
/**
* @brief Frees an inventory update list and all items it contains.
*
* @param[in] list The list to free. Safe to call with NULL.
*/
static void InventoryUpdateList_free(InventoryUpdateList_t *list) {
if (!list)
return;
for (int i = 0; i < list->count; i++) {
InventoryUpdateItem_t *item = list->items[i];
if (!item)
continue;
if (item->Alias)
free(item->Alias);
if (item->Certificates) {
for (int j = 0; j < item->Certificates_count; j++) {
if (item->Certificates[j])
free(item->Certificates[j]);
}
}
free(item);
}
if (list->items)
free(list->items);
free(list);
} /* InventoryUpdateList_free */
/**
* @brief Validates the inventory store configuration received from the
* platform.
*
* Checks that a store path was provided, that it is a file and not a directory,
* that it is not the agent's own certificate store, and that it exists on disk.
*
* @param[in] invConf Inventory configuration response to validate.
* @param[out] statusMessage Accumulates human-readable validation failure
* messages.
* @return true if all checks pass, false if any check fails.
*/
static bool inventory_store_config_valid(const InventoryConfigResp_t *invConf,
char **statusMessage) {
if (!invConf->Job.StorePath) {
log_error("%s::%s(%d) : Job doesn't contain a store to inventory.",
LOG_INF);
append_linef(statusMessage,
"Job doesn't contain a store to inventory.");
return false;
}
if (is_directory(invConf->Job.StorePath)) {
log_error(
"%s::%s(%d) : The store path must be a file and not a directory.",
LOG_INF);
append_linef(statusMessage,
"The store path must be a file and not a directory.");
return false;
}
if (ConfigData->UseAgentCert && ConfigData->AgentCert &&
0 == strcasecmp(ConfigData->AgentCert, invConf->Job.StorePath)) {
log_warn("%s::%s(%d) : Attempting to inventory the agent cert store "
"is not allowed.",
LOG_INF);
append_linef(
statusMessage,
"Attempting to inventory the agent cert store is not allowed.");
return false;
}
if (!file_exists(invConf->Job.StorePath)) {
log_warn("%s::%s(%d) : Attempting to inventory a certificate store "
"that does not exist yet.",
LOG_INF);
append_linef(statusMessage,
"Attempting to inventory a certificate store that does "
"not exist yet.");
return false;
}
return true;
} /* inventory_store_config_valid */
/**
* @brief Classifies each certificate in the on-disk store as ADD or UNCHANGED.
*
* Walks the agent's local PEM inventory. If a certificate's thumbprint is
* found in the platform's list it is marked UNCHANGED; otherwise it is marked
* ADD and its PEM content is included so the platform can record it.
*
* @param[in] cmsItems Array of inventory items the platform knows
* about.
* @param[in] cmsItemCount Number of entries in cmsItems.
* @param[in] fileItemList Certificates found in the on-disk store.
* @param[in,out] updateList Receives the classified update items.
*/
static void classify_file_certs(InventoryCurrentItem_t **cmsItems,
int cmsItemCount,
const struct PemInventoryList *fileItemList,
InventoryUpdateList_t *updateList) {
for (int i = 0; i < fileItemList->item_count; i++) {
PemInventoryItem *currentPem = fileItemList->items[i];
bool inCms = false;
for (int j = 0; j < cmsItemCount; j++) {
if (0 !=
strcasecmp(currentPem->thumbprint_string, cmsItems[j]->Alias))
continue;
log_verbose("%s::%s(%d) : Alias %s is UNCHANGED", LOG_INF,
currentPem->thumbprint_string);
inCms = true;
InventoryUpdateItem_t *item = calloc(1, sizeof(*item));
if (!item) {
log_error("%s::%s(%d) : Out of memory", LOG_INF);
return;
}
item->Alias = strdup(cmsItems[j]->Alias ? cmsItems[j]->Alias : "");
item->ItemStatus = INV_STAT_UNCH;
item->PrivateKeyEntry = cmsItems[j]->PrivateKeyEntry;
item->UseChainLevel = false;
InventoryUpdateList_add(updateList, item);
break;
}
if (inCms)
continue;
log_verbose("%s::%s(%d) : Alias %s is ADDED", LOG_INF,
currentPem->thumbprint_string);
InventoryUpdateItem_t *item = calloc(1, sizeof(*item));
if (!item) {
log_error("%s::%s(%d) : Out of memory", LOG_INF);
return;
}
item->Alias = strdup(
currentPem->thumbprint_string ? currentPem->thumbprint_string : "");
item->ItemStatus = INV_STAT_ADD;
item->PrivateKeyEntry = currentPem->has_private_key;
item->UseChainLevel = false;
item->Certificates = calloc(1, sizeof(char *));
item->Certificates[0] = strdup(currentPem->cert);
item->Certificates_count = 1;
InventoryUpdateList_add(updateList, item);
}
} /* classify_file_certs */
/**
* @brief Classifies platform certificates that no longer exist on disk as
* REMOVE.
*
* Walks the platform's known inventory. Any certificate whose thumbprint is
* not found in the agent's local store is marked for removal.
*
* @param[in] cmsItems Array of inventory items the platform knows
* about.
* @param[in] cmsItemCount Number of entries in cmsItems.
* @param[in] fileItemList Certificates found in the on-disk store.
* @param[in,out] updateList Receives the classified REMOVE items.
*/
static void classify_deleted_certs(InventoryCurrentItem_t **cmsItems,
int cmsItemCount,
const struct PemInventoryList *fileItemList,
InventoryUpdateList_t *updateList) {
for (int m = 0; m < cmsItemCount; m++) {
bool inFile = false;
for (int n = 0; n < fileItemList->item_count; n++) {
if (0 == strcasecmp(fileItemList->items[n]->thumbprint_string,
cmsItems[m]->Alias)) {
inFile = true;
break;
}
}
if (inFile)
continue;
log_verbose("%s::%s(%d) : Alias %s is DELETED", LOG_INF,
cmsItems[m]->Alias);
InventoryUpdateItem_t *item = calloc(1, sizeof(*item));
if (!item) {
log_error("%s::%s(%d) : Out of Memory", LOG_INF);
return;
}
item->Alias = strdup(cmsItems[m]->Alias);
item->ItemStatus = INV_STAT_REM;
item->PrivateKeyEntry = false;
item->UseChainLevel = false;
InventoryUpdateList_add(updateList, item);
}
} /* classify_deleted_certs */
/**
* @brief Builds the full inventory delta by classifying all certificates.
*
* Allocates a new update list, then delegates to classify_file_certs to find
* ADDs and UNCHANGEDs, and classify_deleted_certs to find REMOVEs.
*
* NOTE: The returned updateList is heap-allocated and must be freed by the
* caller using InventoryUpdateList_free().
*
* @param[in] cmsItems Array of inventory items the platform knows about.
* @param[in] cmsItemCount Number of entries in cmsItems.
* @param[in] fileItemList Certificates found in the on-disk store.
* @param[out] updateList Receives the fully classified update list.
* @return 0 on success.
*/
static int compute_inventory_update(InventoryCurrentItem_t **cmsItems,
int cmsItemCount,
const struct PemInventoryList *fileItemList,
InventoryUpdateList_t **updateList) {
*updateList = calloc(1, sizeof(InventoryUpdateList_t));
if (!*updateList) {
log_error("%s::%s(%d) : Out of memory allocating update list", LOG_INF);
return -1;
}
classify_file_certs(cmsItems, cmsItemCount, fileItemList, *updateList);
classify_deleted_certs(cmsItems, cmsItemCount, fileItemList, *updateList);
return 0;
} /* compute_inventory_update */
/**
* @brief Computes the inventory delta and submits it to the platform.
*
* Compares the on-disk PEM store against the platform's known inventory,
* sends the resulting ADD/REMOVE/UNCHANGED list, and processes the response.
*
* @param[in] sessionToken GUID for the current curl session.
* @param[in] jobInfo Job descriptor containing endpoint and ID fields.
* @param[in] invConf Inventory configuration from the platform.
* @param[in] pemList Certificates read from the on-disk store.
* @param[out] pStatus Receives the result status of the update.
* @param[out] pMessage Accumulates human-readable status messages.
* @return 0 on success, non-zero on failure.
*/
static int send_inventory_comparison(const char *sessionToken,
const SessionJob_t *jobInfo,
const InventoryConfigResp_t *invConf,
const PemInventoryList *pemList,
enum AgentApiResultStatus *pStatus,
char **pMessage) {
InventoryUpdateList_t *updateList = NULL;
InventoryUpdateResp_t *updResp = NULL;
int res = 0;
compute_inventory_update(invConf->Job.Inventory,
invConf->Job.Inventory_count, pemList,
&updateList);
res =
send_inventory_update(sessionToken, jobInfo->JobId,
invConf->InventoryEndpoint, updateList, &updResp);
if (res != 0) {
log_error("%s::%s(%d) : Failed to send inventory update", LOG_INF);
*pStatus = STAT_ERR;
} else if (updResp) {
AgentApiResult_log(updResp->Result, pMessage, pStatus);
}
if (updateList)
InventoryUpdateList_free(updateList);
if (updResp)
InventoryUpdateResp_free(updResp);
return res;
} /* send_inventory_comparison */
/**
* @brief Reads the on-disk store and drives the full inventory comparison
* cycle.
*
* Reads the PEM store into memory, delegates comparison and submission to
* send_inventory_comparison, then frees the PEM list.
*
* @param[in] sessionToken GUID for the current curl session.
* @param[in] jobInfo Job descriptor containing endpoint and ID fields.
* @param[in] invConf Inventory configuration from the platform.
* @param[out] pStatus Receives the result status of the operation.
* @param[out] pMessage Accumulates human-readable status messages.
* @return 0 on success, non-zero on failure.
*/
static int run_inventory_operations(const char *sessionToken,
const SessionJob_t *jobInfo,
const InventoryConfigResp_t *invConf,
enum AgentApiResultStatus *pStatus,
char **pMessage) {
PemInventoryList *pemList = NULL;
int res = 0;
log_trace("%s::%s(%d) : Reading inventory store at %s", LOG_INF,
invConf->Job.StorePath);
res = ssl_read_store_inventory(invConf->Job.StorePath,
invConf->Job.StorePassword, &pemList);
if (res != 0) {
log_error("%s::%s(%d) : Failed to read store inventory", LOG_INF);
*pStatus = STAT_ERR;
append_line(pMessage, strerror(res));
return res;
}
res = send_inventory_comparison(sessionToken, jobInfo, invConf, pemList,
pStatus, pMessage);
if (pemList) {
log_trace("%s::%s(%d) : Freeing pemList containing %d items", LOG_INF,
pemList->item_count);
PemInventoryList_free(pemList);
}
return res;
} /* run_inventory_operations */
/**
* @brief Sends job completion to the platform and logs the final outcome.
*
* Transmits the final status, then logs success, warning, or error depending
* on the reported status value.
*
* @param[in] sessionToken GUID for the current curl session.
* @param[in] jobInfo Job descriptor containing endpoint and ID fields.
* @param[in] status Final result status of the inventory operation.
* @param[in] auditId Audit record ID associated with this job.
* @param[in] statusMessage Human-readable result or error message to send.
* @return 0 on success, 999 if the completion POST itself fails.
*/
static int finalize_inventory_job(const char *sessionToken,
const SessionJob_t *jobInfo,
enum AgentApiResultStatus status,
long auditId, const char *statusMessage) {
CommonCompleteResp_t *invComp = NULL;
int res = send_inventory_job_complete(
sessionToken, jobInfo->JobId, jobInfo->CompletionEndpoint, status + 1,
auditId, statusMessage, &invComp);
if (res != 0) {
log_error("%s::%s(%d) : Failed to send inventory job complete",
LOG_INF);
CommonCompleteResp_free(invComp);
return 999;
}
if (invComp)
AgentApiResult_log(invComp->Result, NULL, NULL);
if (status >= STAT_ERR) {
log_error("%s::%s(%d) : Inventory job %s failed with error: %s",
LOG_INF, jobInfo->JobId, statusMessage);
} else if (status == STAT_WARN) {
log_warn("%s::%s(%d) : Inventory job %s completed with warning: %s",
LOG_INF, jobInfo->JobId, statusMessage);
} else {
log_info("%s::%s(%d) : Inventory job %s completed successfully",
LOG_INF, jobInfo->JobId);
}
CommonCompleteResp_free(invComp);
return (status >= STAT_ERR) ? 999 : 0;
} /* finalize_inventory_job */
/******************************************************************************/
/*********************** GLOBAL FUNCTION DEFINITIONS **************************/
/******************************************************************************/
/**
* @brief Entry point for the certificate inventory job.
*
* Orchestrates the full inventory job lifecycle:
* 1. Fetches the job configuration from the platform.
* 2. Validates the store configuration.
* 3. Exits early if the job was cancelled.
* 4. Reads the store, computes the delta, and submits the update.
* 5. Reports job completion back to the platform.
*
* @param[in] jobInfo Job descriptor received from the scheduler.
* @param[in] sessionToken GUID for the current curl session.
* @return 0 on success, 1 if the job was cancelled, 999 on error.
*/
int cms_job_inventory(SessionJob_t *jobInfo, char *sessionToken) {
InventoryConfigResp_t *invConf = NULL;
char *statusMessage = strdup("");
enum AgentApiResultStatus status = STAT_UNK;
int returnable = 0;
int res = 0;
log_info("%s::%s(%d) : Starting inventory job %s", LOG_INF, jobInfo->JobId);
res = get_inventory_config(sessionToken, jobInfo->JobId,
jobInfo->ConfigurationEndpoint, &invConf);
if (res != 0) {
log_error("%s::%s(%d) : Failed to get inventory config", LOG_INF);
free(statusMessage);
return 999;
}
if (!invConf) {
log_error("%s::%s(%d) : No inventory configuration returned from "
"the platform.",
LOG_INF);
free(statusMessage);
return 999;
}
if (!inventory_store_config_valid(invConf, &statusMessage)) {
CommonCompleteResp_t *invComp = NULL;
send_inventory_job_complete(sessionToken, jobInfo->JobId,
jobInfo->CompletionEndpoint, STAT_ERR,
invConf->AuditId, statusMessage, &invComp);
CommonCompleteResp_free(invComp);
returnable = 999;
goto exit;
}
if (!AgentApiResult_log(invConf->Result, &statusMessage, &status)) {
returnable = 999;
goto exit;
}
if (invConf->JobCancelled) {
log_info("%s::%s(%d) : Job has been cancelled and will not be run",
LOG_INF);
returnable = 1;
goto exit;
}
log_verbose("%s::%s(%d) : Audit Id: %ld", LOG_INF, invConf->AuditId);
res = run_inventory_operations(sessionToken, jobInfo, invConf, &status,
&statusMessage);
if (res != 0)
returnable = 999;
returnable = finalize_inventory_job(sessionToken, jobInfo, status,
invConf->AuditId, statusMessage);
exit:
InventoryConfigResp_free(invConf);
free(statusMessage);
return returnable;
} /* cms_job_inventory */
/******************************************************************************/
/******************************* END OF FILE **********************************/
/******************************************************************************/