Download and verify before disruption, then activate under daemon locks. */
| 13517 | |
| 13518 | /* Download and verify before disruption, then activate under daemon locks. */ |
| 13519 | static int download_verify_install(const char *url, const char *ext, const char *os, |
| 13520 | const char *arch, const char *bin_dest, const char *home, |
| 13521 | bool delete_indexes) { |
| 13522 | char tmp_archive[CLI_BUF_256]; |
| 13523 | int archive_path_length = |
| 13524 | snprintf(tmp_archive, sizeof(tmp_archive), "%s/cbm-update-XXXXXX", cbm_tmpdir()); |
| 13525 | if (archive_path_length <= 0 || (size_t)archive_path_length >= sizeof(tmp_archive)) { |
| 13526 | return CLI_TRUE; |
| 13527 | } |
| 13528 | int archive_descriptor = cbm_mkstemp(tmp_archive); |
| 13529 | if (archive_descriptor < 0) { |
| 13530 | return CLI_TRUE; |
| 13531 | } |
| 13532 | #ifdef _WIN32 |
| 13533 | int archive_close_status = _close(archive_descriptor); |
| 13534 | #else |
| 13535 | int archive_close_status = close(archive_descriptor); |
| 13536 | #endif |
| 13537 | if (archive_close_status != 0) { |
| 13538 | cbm_unlink(tmp_archive); |
| 13539 | return CLI_TRUE; |
| 13540 | } |
| 13541 | |
| 13542 | int rc = cbm_download_to_file(url, tmp_archive); |
| 13543 | if (rc != 0) { |
| 13544 | (void)fprintf(stderr, "error: download failed (exit %d)\n", rc); |
| 13545 | cbm_unlink(tmp_archive); |
| 13546 | return CLI_TRUE; |
| 13547 | } |
| 13548 | |
| 13549 | char archive_name[CLI_BUF_256]; |
| 13550 | /* Must match build_update_url: linux uses the static "-portable" asset. */ |
| 13551 | const char *portable = (strcmp(os, "linux") == 0) ? "-portable" : ""; |
| 13552 | snprintf(archive_name, sizeof(archive_name), "codebase-memory-mcp-%s-%s%s.%s", os, arch, |
| 13553 | portable, ext); |
| 13554 | /* Fail closed: install only a positively-verified download. A mismatch, |
| 13555 | * a missing checksum entry, or an unavailable hash tool (crc != 0) all |
| 13556 | * abort rather than install an unverified binary. */ |
| 13557 | int crc = verify_download_checksum(tmp_archive, archive_name); |
| 13558 | if (crc != 0) { |
| 13559 | (void)fprintf(stderr, "error: refusing to install an unverified download\n"); |
| 13560 | cbm_unlink(tmp_archive); |
| 13561 | return CLI_TRUE; |
| 13562 | } |
| 13563 | |
| 13564 | if (extract_and_install_binary((extract_install_args_t){ |
| 13565 | .tmp_archive = tmp_archive, |
| 13566 | .ext = ext, |
| 13567 | .bin_dest = bin_dest, |
| 13568 | .home = home, |
| 13569 | .delete_indexes = delete_indexes, |
| 13570 | }) != 0) { |
| 13571 | return CLI_TRUE; |
| 13572 | } |
| 13573 | return 0; |
| 13574 | } |
| 13575 | |
| 13576 | /* Case-insensitive prefix match (portable — no strncasecmp dependency). */ |
no test coverage detected