@@ -310,6 +310,50 @@ static int remove_cert_from_store(const char *storePath,
310310 return ret ;
311311} /* remove_cert_from_store */
312312
313+ /**
314+ * @brief Validates the management store configuration from the platform
315+ *
316+ * Performs validation checks on the management job configuration including:
317+ * - Verifying the store path is a file and not a directory
318+ * - Ensuring the target store is not the agent's certificate store
319+ * - Confirming the target store exists
320+ * - Validating that a store path was provided
321+ *
322+ * @param[in] manConf The management configuration response from the platform
323+ * @param[out] statusMessage String array to append validation error messages
324+ * @return false if validation failed, false if validation passed
325+ */
326+ static bool validate_management_store_configuration (ManagementConfigResp_t * manConf , char * * statusMessage )
327+ {
328+ bool failed = false;
329+ if (manConf -> Job .StorePath ) {
330+ /* Is the target store a directory and not a file? */
331+ if (is_directory (manConf -> Job .StorePath )) {
332+ log_error ("%s::%s(%d) : The store path must be a file and "
333+ "not a directory." , LOG_INF );
334+ append_linef (statusMessage , "The store path must be a file and not a directory." );
335+ failed = true;
336+ }
337+ /* Is the target store the Agent store? */
338+ if (0 == strcasecmp (ConfigData -> AgentCert , manConf -> Job .StorePath )) {
339+ log_warn ("%s::%s(%d) : Attempting a Management job on the agent cert store is not allowed." , LOG_INF );
340+ append_linef (statusMessage , "Attempting a Management job the agent cert store is not allowed." );
341+ failed = true;
342+ }
343+ /* Verify the target store exists */
344+ if (!file_exists (manConf -> Job .StorePath )) {
345+ log_warn ("%s::%s(%d) : Attempting to manage a certificate store that does not exist yet." , LOG_INF );
346+ append_linef (statusMessage , "Attempting to manage a certificate store that does not exist yet." );
347+ failed = true;
348+ }
349+ } else {
350+ log_error ("%s::%s(%d) : Job doesn't contain a target store to manage." , LOG_INF );
351+ append_linef (statusMessage , "Job doesn't contain a target store to manage." );
352+ failed = true;
353+ }
354+ return failed ;
355+ } /* validate_management_store_configuration */
356+
313357/******************************************************************************/
314358/*********************** GLOBAL FUNCTION DEFINITIONS **************************/
315359/******************************************************************************/
@@ -327,13 +371,13 @@ static int remove_cert_from_store(const char *storePath,
327371/* @param - [Input] : chainJob = any additional jobs required to run after */
328372/* this one -- typically an Inventory job */
329373/* @return - job was run : 0 */
330- /* - job was canceled : 1 */
374+ /* - job was canceled : 1 */
331375/* */
332376int cms_job_manage (SessionJob_t * jobInfo , char * sessionToken ,
333377 char * * chainJob )
334378{
335379 int res = 0 ;
336- ManagementConfigResp_t * manConf = NULL ;
380+ ManagementConfigResp_t * manConf = NULL ;
337381 char * statusMessage = strdup ("" );
338382 enum AgentApiResultStatus status = STAT_UNK ;
339383 int returnable = 0 ;
@@ -342,101 +386,80 @@ int cms_job_manage(SessionJob_t * jobInfo, char *sessionToken,
342386
343387 res = get_management_config (sessionToken , jobInfo -> JobId ,
344388 jobInfo -> ConfigurationEndpoint , & manConf );
389+ if (res != 0 ) {
390+ log_error ("%s::%s(%d) : Failed to get management config" , LOG_INF );
391+ return res ;
392+ }
345393
346394 /* Validate data */
347395 if (manConf ) {
348- bool failed = false;
349- if (manConf -> Job .StorePath ) {
350- /* Is the target store a directory and not a file? */
351- if (is_directory (manConf -> Job .StorePath )) {
352- log_error ("%s::%s(%d) : The store path must be a file and "
353- "not a directory." , LOG_INF );
354- append_linef (& statusMessage , "The store path must be a file "
355- "and not a directory." );
356- failed = true;
357- }
358- /* Is the target store the Agent store? */
359- if (0 == strcasecmp (ConfigData -> AgentCert , manConf -> Job .StorePath )) {
360-
361- log_warn ("%s::%s(%d) : Attempting a Management job on the "
362- "agent cert store is not allowed." , LOG_INF );
363- append_linef (& statusMessage , "Attempting a Management job the"
364- " agent cert store is not allowed." );
365- failed = true;
366- }
367- /* Verify the target store exists */
368- if (!file_exists (manConf -> Job .StorePath )) {
369- log_warn ("%s::%s(%d) : Attempting to manage a certificate"
370- " store that does not exist yet." , LOG_INF );
371- append_linef (& statusMessage , "Attempting to manage a "
372- "certificate store that does not exist yet." );
373- failed = true;
374- }
375- } else {
376- log_error ("%s::%s(%d) : Job doesn't contain a target store "
377- "to manage." , LOG_INF );
378- append_linef (& statusMessage , "Job doesn't contain a target "
379- "store to manage." );
380- failed = true;
381- }
382396 /* if any test failed, then let the platform know about it. */
383- if (failed ) {
397+ if (false == validate_management_store_configuration ( manConf , & statusMessage ) ) {
384398 ManagementCompleteResp_t * manComp = NULL ;
385399 send_management_job_complete (sessionToken , jobInfo -> JobId ,
386400 jobInfo -> CompletionEndpoint , STAT_ERR , manConf -> AuditId ,
387- statusMessage , & manComp );
401+ statusMessage , & manComp );
388402 ManagementCompleteResp_free (manComp );
403+ res = 999 ;
389404 goto exit ;
390405 }
391406 } else {
392- log_error ("%s::%s(%d) : No managment configuration was returned"
393- " from the platform." , LOG_INF ) ;
407+ log_error ("%s::%s(%d) : No management configuration was returned from the platform." , LOG_INF );
408+ res = 999 ;
394409 goto exit ;
395410 }
396411
397412 /* Data ok, process job */
398413 if (res == 0 && AgentApiResult_log (manConf -> Result , & statusMessage , & status )) {
399414 if (manConf -> JobCancelled ) {
400415 returnable = 1 ;
401- log_info ("%s::%s(%d) : Job has been cancelled and will not be run" ,
402- LOG_INF );
416+ log_info ("%s::%s(%d) : Job has been cancelled and will not be run" , LOG_INF );
403417 } else {
404418 long auditId = manConf -> AuditId ;
405419 log_verbose ("%s::%s(%d) : Audit Id: %ld" , LOG_INF , auditId );
406420
407421 int opType = manConf -> Job .OperationType ;
408422 switch (opType ) {
409- case OP_ADD :
410- log_verbose ("%s::%s(%d) : Add certificate operation" , LOG_INF );
411-
412- if (manConf -> Job .PrivateKeyEntry ) {
413- const char * msg = "Adding a PFX is not supported at"
414- " this time" ;
415- log_info ("%s::%s(%d) : %s" , LOG_INF , msg );
423+ case OP_ADD :
424+ log_verbose ("%s::%s(%d) : Add certificate operation" , LOG_INF );
425+
426+ if (manConf -> Job .PrivateKeyEntry ) {
427+ const char * msg = "Adding a PFX is not supported at this time" ;
428+ log_info ("%s::%s(%d) : %s" , LOG_INF , msg );
429+ status = STAT_ERR ;
430+ res = 999 ;
431+ append_line (& statusMessage , msg );
432+ } else {
433+ log_info ("%s::%s(%d) : Attempting to add certificate to the store:\n%s" , LOG_INF ,
434+ manConf -> Job .EntryContents );
435+ res = add_cert_to_store (manConf -> Job .StorePath ,
436+ manConf -> Job .EntryContents , & statusMessage , & status );
437+ if (res != 0 ) {
438+ log_error ("%s::%s(%d) : Failed to add certificate to the store" , LOG_INF );
439+ res = 999 ;
440+ }
441+ }
442+ break ;
443+ case OP_REM :
444+ log_verbose ("%s::%s(%d) : Remove certificate operation" ,
445+ LOG_INF );
446+ res = remove_cert_from_store (manConf -> Job .StorePath ,
447+ manConf -> Job .Alias , manConf -> Job .PrivateKeyPath ,
448+ manConf -> Job .StorePassword , & statusMessage , & status );
449+ if (res != 0 ) {
450+ log_error ("%s::%s(%d) : Failed to add certificate to the store" , LOG_INF );
451+ res = 999 ;
452+ }
453+ break ;
454+ default :
455+ log_error ("%s::%s(%d) : Unsupported operation type: %d" , LOG_INF , opType );
456+ append_linef (& statusMessage , "Unsupported operation type: %d" , opType );
416457 status = STAT_ERR ;
417- append_line (& statusMessage , msg );
418- } else {
419- log_info ("%s::%s(%d) : Attempting to add certificate"
420- " to the store:\n%s" , LOG_INF ,
421- manConf -> Job .EntryContents );
422- res = add_cert_to_store (manConf -> Job .StorePath ,
423- manConf -> Job .EntryContents , & statusMessage , & status );
424- }
425- break ;
426- case OP_REM :
427- log_verbose ("%s::%s(%d) : Remove certificate operation" ,
428- LOG_INF );
429- res = remove_cert_from_store (manConf -> Job .StorePath ,
430- manConf -> Job .Alias , manConf -> Job .PrivateKeyPath ,
431- manConf -> Job .StorePassword , & statusMessage , & status );
432- break ;
433- default :
434- log_error ("%s::%s(%d) : Unsupported operation type: %d" ,
435- LOG_INF , opType );
436- append_linef (& statusMessage , "Unsupported operation type: %d" ,
437- opType );
438- status = STAT_ERR ;
439- break ;
458+ break ;
459+ }
460+
461+ if (res != 0 ) {
462+ log_error ("%s::%s(%d) : Failed to add or remove certificate" , LOG_INF );
440463 }
441464
442465 ManagementCompleteResp_t * manComp = NULL ;
@@ -445,6 +468,11 @@ int cms_job_manage(SessionJob_t * jobInfo, char *sessionToken,
445468 auditId , statusMessage , & manComp );
446469 /* NOTE: Removed chain job due to inventory job running twice */
447470
471+ if (res != 0 ) {
472+ log_error ("%s::%s(%d) : Failed to send management job complete" , LOG_INF );
473+ goto exit ;
474+ }
475+
448476 if (status >= STAT_ERR ) {
449477 log_error ("%s::%s(%d) : Management job %s failed with "
450478 "error: %s" , LOG_INF , jobInfo -> JobId , statusMessage );
0 commit comments