Download and verify before disruption, then activate under daemon locks. */
| 12012 | |
| 12013 | /* Download and verify before disruption, then activate under daemon locks. */ |
| 12014 | static int download_verify_install(const char *url, const char *ext, const char *os, |
| 12015 | const char *arch, const char *bin_dest, const char *home, |
| 12016 | bool delete_indexes) { |
| 12017 | char tmp_archive[CLI_BUF_256]; |
| 12018 | int archive_path_length = |
| 12019 | snprintf(tmp_archive, sizeof(tmp_archive), "%s/cbm-update-XXXXXX", cbm_tmpdir()); |
| 12020 | if (archive_path_length <= 0 || (size_t)archive_path_length >= sizeof(tmp_archive)) { |
| 12021 | return CLI_TRUE; |
| 12022 | } |
| 12023 | int archive_descriptor = cbm_mkstemp(tmp_archive); |
| 12024 | if (archive_descriptor < 0) { |
| 12025 | return CLI_TRUE; |
| 12026 | } |
| 12027 | #ifdef _WIN32 |
| 12028 | int archive_close_status = _close(archive_descriptor); |
| 12029 | #else |
| 12030 | int archive_close_status = close(archive_descriptor); |
| 12031 | #endif |
| 12032 | if (archive_close_status != 0) { |
| 12033 | cbm_unlink(tmp_archive); |
| 12034 | return CLI_TRUE; |
| 12035 | } |
| 12036 | |
| 12037 | int rc = cbm_download_to_file(url, tmp_archive); |
| 12038 | if (rc != 0) { |
| 12039 | (void)fprintf(stderr, "error: download failed (exit %d)\n", rc); |
| 12040 | cbm_unlink(tmp_archive); |
| 12041 | return CLI_TRUE; |
| 12042 | } |
| 12043 | |
| 12044 | char archive_name[CLI_BUF_256]; |
| 12045 | /* Must match build_update_url: linux uses the static "-portable" asset. */ |
| 12046 | const char *portable = (strcmp(os, "linux") == 0) ? "-portable" : ""; |
| 12047 | snprintf(archive_name, sizeof(archive_name), "codebase-memory-mcp-%s-%s%s.%s", os, arch, |
| 12048 | portable, ext); |
| 12049 | /* Fail closed: install only a positively-verified download. A mismatch, |
| 12050 | * a missing checksum entry, or an unavailable hash tool (crc != 0) all |
| 12051 | * abort rather than install an unverified binary. */ |
| 12052 | int crc = verify_download_checksum(tmp_archive, archive_name); |
| 12053 | if (crc != 0) { |
| 12054 | (void)fprintf(stderr, "error: refusing to install an unverified download\n"); |
| 12055 | cbm_unlink(tmp_archive); |
| 12056 | return CLI_TRUE; |
| 12057 | } |
| 12058 | |
| 12059 | if (extract_and_install_binary((extract_install_args_t){ |
| 12060 | .tmp_archive = tmp_archive, |
| 12061 | .ext = ext, |
| 12062 | .bin_dest = bin_dest, |
| 12063 | .home = home, |
| 12064 | .delete_indexes = delete_indexes, |
| 12065 | }) != 0) { |
| 12066 | return CLI_TRUE; |
| 12067 | } |
| 12068 | return 0; |
| 12069 | } |
| 12070 | |
| 12071 | /* Case-insensitive prefix match (portable — no strncasecmp dependency). */ |
no test coverage detected