Download and verify before disruption, then activate under daemon locks. */
| 11840 | |
| 11841 | /* Download and verify before disruption, then activate under daemon locks. */ |
| 11842 | static int download_verify_install(const char *url, const char *ext, const char *os, |
| 11843 | const char *arch, const char *bin_dest, const char *home, |
| 11844 | bool delete_indexes) { |
| 11845 | char tmp_archive[CLI_BUF_256]; |
| 11846 | int archive_path_length = |
| 11847 | snprintf(tmp_archive, sizeof(tmp_archive), "%s/cbm-update-XXXXXX", cbm_tmpdir()); |
| 11848 | if (archive_path_length <= 0 || (size_t)archive_path_length >= sizeof(tmp_archive)) { |
| 11849 | return CLI_TRUE; |
| 11850 | } |
| 11851 | int archive_descriptor = cbm_mkstemp(tmp_archive); |
| 11852 | if (archive_descriptor < 0) { |
| 11853 | return CLI_TRUE; |
| 11854 | } |
| 11855 | #ifdef _WIN32 |
| 11856 | int archive_close_status = _close(archive_descriptor); |
| 11857 | #else |
| 11858 | int archive_close_status = close(archive_descriptor); |
| 11859 | #endif |
| 11860 | if (archive_close_status != 0) { |
| 11861 | cbm_unlink(tmp_archive); |
| 11862 | return CLI_TRUE; |
| 11863 | } |
| 11864 | |
| 11865 | int rc = cbm_download_to_file(url, tmp_archive); |
| 11866 | if (rc != 0) { |
| 11867 | (void)fprintf(stderr, "error: download failed (exit %d)\n", rc); |
| 11868 | cbm_unlink(tmp_archive); |
| 11869 | return CLI_TRUE; |
| 11870 | } |
| 11871 | |
| 11872 | char archive_name[CLI_BUF_256]; |
| 11873 | /* Must match build_update_url: linux uses the static "-portable" asset. */ |
| 11874 | const char *portable = (strcmp(os, "linux") == 0) ? "-portable" : ""; |
| 11875 | snprintf(archive_name, sizeof(archive_name), "codebase-memory-mcp-%s-%s%s.%s", os, arch, |
| 11876 | portable, ext); |
| 11877 | /* Fail closed: install only a positively-verified download. A mismatch, |
| 11878 | * a missing checksum entry, or an unavailable hash tool (crc != 0) all |
| 11879 | * abort rather than install an unverified binary. */ |
| 11880 | int crc = verify_download_checksum(tmp_archive, archive_name); |
| 11881 | if (crc != 0) { |
| 11882 | (void)fprintf(stderr, "error: refusing to install an unverified download\n"); |
| 11883 | cbm_unlink(tmp_archive); |
| 11884 | return CLI_TRUE; |
| 11885 | } |
| 11886 | |
| 11887 | if (extract_and_install_binary((extract_install_args_t){ |
| 11888 | .tmp_archive = tmp_archive, |
| 11889 | .ext = ext, |
| 11890 | .bin_dest = bin_dest, |
| 11891 | .home = home, |
| 11892 | .delete_indexes = delete_indexes, |
| 11893 | }) != 0) { |
| 11894 | return CLI_TRUE; |
| 11895 | } |
| 11896 | return 0; |
| 11897 | } |
| 11898 | |
| 11899 | /* Case-insensitive prefix match (portable — no strncasecmp dependency). */ |
no test coverage detected