Download and verify before disruption, then activate under daemon locks. */
| 11754 | |
| 11755 | /* Download and verify before disruption, then activate under daemon locks. */ |
| 11756 | static int download_verify_install(const char *url, const char *ext, const char *os, |
| 11757 | const char *arch, const char *bin_dest, const char *home, |
| 11758 | bool delete_indexes) { |
| 11759 | char tmp_archive[CLI_BUF_256]; |
| 11760 | int archive_path_length = |
| 11761 | snprintf(tmp_archive, sizeof(tmp_archive), "%s/cbm-update-XXXXXX", cbm_tmpdir()); |
| 11762 | if (archive_path_length <= 0 || (size_t)archive_path_length >= sizeof(tmp_archive)) { |
| 11763 | return CLI_TRUE; |
| 11764 | } |
| 11765 | int archive_descriptor = cbm_mkstemp(tmp_archive); |
| 11766 | if (archive_descriptor < 0) { |
| 11767 | return CLI_TRUE; |
| 11768 | } |
| 11769 | #ifdef _WIN32 |
| 11770 | int archive_close_status = _close(archive_descriptor); |
| 11771 | #else |
| 11772 | int archive_close_status = close(archive_descriptor); |
| 11773 | #endif |
| 11774 | if (archive_close_status != 0) { |
| 11775 | cbm_unlink(tmp_archive); |
| 11776 | return CLI_TRUE; |
| 11777 | } |
| 11778 | |
| 11779 | int rc = cbm_download_to_file(url, tmp_archive); |
| 11780 | if (rc != 0) { |
| 11781 | (void)fprintf(stderr, "error: download failed (exit %d)\n", rc); |
| 11782 | cbm_unlink(tmp_archive); |
| 11783 | return CLI_TRUE; |
| 11784 | } |
| 11785 | |
| 11786 | char archive_name[CLI_BUF_256]; |
| 11787 | /* Must match build_update_url: linux uses the static "-portable" asset. */ |
| 11788 | const char *portable = (strcmp(os, "linux") == 0) ? "-portable" : ""; |
| 11789 | snprintf(archive_name, sizeof(archive_name), "codebase-memory-mcp-%s-%s%s.%s", os, arch, |
| 11790 | portable, ext); |
| 11791 | /* Fail closed: install only a positively-verified download. A mismatch, |
| 11792 | * a missing checksum entry, or an unavailable hash tool (crc != 0) all |
| 11793 | * abort rather than install an unverified binary. */ |
| 11794 | int crc = verify_download_checksum(tmp_archive, archive_name); |
| 11795 | if (crc != 0) { |
| 11796 | (void)fprintf(stderr, "error: refusing to install an unverified download\n"); |
| 11797 | cbm_unlink(tmp_archive); |
| 11798 | return CLI_TRUE; |
| 11799 | } |
| 11800 | |
| 11801 | if (extract_and_install_binary((extract_install_args_t){ |
| 11802 | .tmp_archive = tmp_archive, |
| 11803 | .ext = ext, |
| 11804 | .bin_dest = bin_dest, |
| 11805 | .home = home, |
| 11806 | .delete_indexes = delete_indexes, |
| 11807 | }) != 0) { |
| 11808 | return CLI_TRUE; |
| 11809 | } |
| 11810 | return 0; |
| 11811 | } |
| 11812 | |
| 11813 | /* Case-insensitive prefix match (portable — no strncasecmp dependency). */ |
no test coverage detected