Download and verify before disruption, then activate under daemon locks. */
| 11345 | |
| 11346 | /* Download and verify before disruption, then activate under daemon locks. */ |
| 11347 | static int download_verify_install(const char *url, const char *ext, const char *os, |
| 11348 | const char *arch, const char *bin_dest, const char *home, |
| 11349 | bool delete_indexes) { |
| 11350 | char tmp_archive[CLI_BUF_256]; |
| 11351 | int archive_path_length = |
| 11352 | snprintf(tmp_archive, sizeof(tmp_archive), "%s/cbm-update-XXXXXX", cbm_tmpdir()); |
| 11353 | if (archive_path_length <= 0 || (size_t)archive_path_length >= sizeof(tmp_archive)) { |
| 11354 | return CLI_TRUE; |
| 11355 | } |
| 11356 | int archive_descriptor = cbm_mkstemp(tmp_archive); |
| 11357 | if (archive_descriptor < 0) { |
| 11358 | return CLI_TRUE; |
| 11359 | } |
| 11360 | #ifdef _WIN32 |
| 11361 | int archive_close_status = _close(archive_descriptor); |
| 11362 | #else |
| 11363 | int archive_close_status = close(archive_descriptor); |
| 11364 | #endif |
| 11365 | if (archive_close_status != 0) { |
| 11366 | cbm_unlink(tmp_archive); |
| 11367 | return CLI_TRUE; |
| 11368 | } |
| 11369 | |
| 11370 | int rc = cbm_download_to_file(url, tmp_archive); |
| 11371 | if (rc != 0) { |
| 11372 | (void)fprintf(stderr, "error: download failed (exit %d)\n", rc); |
| 11373 | cbm_unlink(tmp_archive); |
| 11374 | return CLI_TRUE; |
| 11375 | } |
| 11376 | |
| 11377 | char archive_name[CLI_BUF_256]; |
| 11378 | /* Must match build_update_url: linux uses the static "-portable" asset. */ |
| 11379 | const char *portable = (strcmp(os, "linux") == 0) ? "-portable" : ""; |
| 11380 | snprintf(archive_name, sizeof(archive_name), "codebase-memory-mcp-%s-%s%s.%s", os, arch, |
| 11381 | portable, ext); |
| 11382 | /* Fail closed: install only a positively-verified download. A mismatch, |
| 11383 | * a missing checksum entry, or an unavailable hash tool (crc != 0) all |
| 11384 | * abort rather than install an unverified binary. */ |
| 11385 | int crc = verify_download_checksum(tmp_archive, archive_name); |
| 11386 | if (crc != 0) { |
| 11387 | (void)fprintf(stderr, "error: refusing to install an unverified download\n"); |
| 11388 | cbm_unlink(tmp_archive); |
| 11389 | return CLI_TRUE; |
| 11390 | } |
| 11391 | |
| 11392 | if (extract_and_install_binary((extract_install_args_t){ |
| 11393 | .tmp_archive = tmp_archive, |
| 11394 | .ext = ext, |
| 11395 | .bin_dest = bin_dest, |
| 11396 | .home = home, |
| 11397 | .delete_indexes = delete_indexes, |
| 11398 | }) != 0) { |
| 11399 | return CLI_TRUE; |
| 11400 | } |
| 11401 | return 0; |
| 11402 | } |
| 11403 | |
| 11404 | /* Case-insensitive prefix match (portable — no strncasecmp dependency). */ |
no test coverage detected