Download and verify before disruption, then activate under daemon locks. */
| 11401 | |
| 11402 | /* Download and verify before disruption, then activate under daemon locks. */ |
| 11403 | static int download_verify_install(const char *url, const char *ext, const char *os, |
| 11404 | const char *arch, const char *bin_dest, const char *home, |
| 11405 | bool delete_indexes) { |
| 11406 | char tmp_archive[CLI_BUF_256]; |
| 11407 | int archive_path_length = |
| 11408 | snprintf(tmp_archive, sizeof(tmp_archive), "%s/cbm-update-XXXXXX", cbm_tmpdir()); |
| 11409 | if (archive_path_length <= 0 || (size_t)archive_path_length >= sizeof(tmp_archive)) { |
| 11410 | return CLI_TRUE; |
| 11411 | } |
| 11412 | int archive_descriptor = cbm_mkstemp(tmp_archive); |
| 11413 | if (archive_descriptor < 0) { |
| 11414 | return CLI_TRUE; |
| 11415 | } |
| 11416 | #ifdef _WIN32 |
| 11417 | int archive_close_status = _close(archive_descriptor); |
| 11418 | #else |
| 11419 | int archive_close_status = close(archive_descriptor); |
| 11420 | #endif |
| 11421 | if (archive_close_status != 0) { |
| 11422 | cbm_unlink(tmp_archive); |
| 11423 | return CLI_TRUE; |
| 11424 | } |
| 11425 | |
| 11426 | int rc = cbm_download_to_file(url, tmp_archive); |
| 11427 | if (rc != 0) { |
| 11428 | (void)fprintf(stderr, "error: download failed (exit %d)\n", rc); |
| 11429 | cbm_unlink(tmp_archive); |
| 11430 | return CLI_TRUE; |
| 11431 | } |
| 11432 | |
| 11433 | char archive_name[CLI_BUF_256]; |
| 11434 | /* Must match build_update_url: linux uses the static "-portable" asset. */ |
| 11435 | const char *portable = (strcmp(os, "linux") == 0) ? "-portable" : ""; |
| 11436 | snprintf(archive_name, sizeof(archive_name), "codebase-memory-mcp-%s-%s%s.%s", os, arch, |
| 11437 | portable, ext); |
| 11438 | /* Fail closed: install only a positively-verified download. A mismatch, |
| 11439 | * a missing checksum entry, or an unavailable hash tool (crc != 0) all |
| 11440 | * abort rather than install an unverified binary. */ |
| 11441 | int crc = verify_download_checksum(tmp_archive, archive_name); |
| 11442 | if (crc != 0) { |
| 11443 | (void)fprintf(stderr, "error: refusing to install an unverified download\n"); |
| 11444 | cbm_unlink(tmp_archive); |
| 11445 | return CLI_TRUE; |
| 11446 | } |
| 11447 | |
| 11448 | if (extract_and_install_binary((extract_install_args_t){ |
| 11449 | .tmp_archive = tmp_archive, |
| 11450 | .ext = ext, |
| 11451 | .bin_dest = bin_dest, |
| 11452 | .home = home, |
| 11453 | .delete_indexes = delete_indexes, |
| 11454 | }) != 0) { |
| 11455 | return CLI_TRUE; |
| 11456 | } |
| 11457 | return 0; |
| 11458 | } |
| 11459 | |
| 11460 | /* Case-insensitive prefix match (portable — no strncasecmp dependency). */ |
no test coverage detected