Download and verify before disruption, then activate under daemon locks. */
| 13177 | |
| 13178 | /* Download and verify before disruption, then activate under daemon locks. */ |
| 13179 | static int download_verify_install(const char *url, const char *ext, const char *os, |
| 13180 | const char *arch, const char *bin_dest, const char *home, |
| 13181 | bool delete_indexes) { |
| 13182 | char tmp_archive[CLI_BUF_256]; |
| 13183 | int archive_path_length = |
| 13184 | snprintf(tmp_archive, sizeof(tmp_archive), "%s/cbm-update-XXXXXX", cbm_tmpdir()); |
| 13185 | if (archive_path_length <= 0 || (size_t)archive_path_length >= sizeof(tmp_archive)) { |
| 13186 | return CLI_TRUE; |
| 13187 | } |
| 13188 | int archive_descriptor = cbm_mkstemp(tmp_archive); |
| 13189 | if (archive_descriptor < 0) { |
| 13190 | return CLI_TRUE; |
| 13191 | } |
| 13192 | #ifdef _WIN32 |
| 13193 | int archive_close_status = _close(archive_descriptor); |
| 13194 | #else |
| 13195 | int archive_close_status = close(archive_descriptor); |
| 13196 | #endif |
| 13197 | if (archive_close_status != 0) { |
| 13198 | cbm_unlink(tmp_archive); |
| 13199 | return CLI_TRUE; |
| 13200 | } |
| 13201 | |
| 13202 | int rc = cbm_download_to_file(url, tmp_archive); |
| 13203 | if (rc != 0) { |
| 13204 | (void)fprintf(stderr, "error: download failed (exit %d)\n", rc); |
| 13205 | cbm_unlink(tmp_archive); |
| 13206 | return CLI_TRUE; |
| 13207 | } |
| 13208 | |
| 13209 | char archive_name[CLI_BUF_256]; |
| 13210 | /* Must match build_update_url: linux uses the static "-portable" asset. */ |
| 13211 | const char *portable = (strcmp(os, "linux") == 0) ? "-portable" : ""; |
| 13212 | snprintf(archive_name, sizeof(archive_name), "codebase-memory-mcp-%s-%s%s.%s", os, arch, |
| 13213 | portable, ext); |
| 13214 | /* Fail closed: install only a positively-verified download. A mismatch, |
| 13215 | * a missing checksum entry, or an unavailable hash tool (crc != 0) all |
| 13216 | * abort rather than install an unverified binary. */ |
| 13217 | int crc = verify_download_checksum(tmp_archive, archive_name); |
| 13218 | if (crc != 0) { |
| 13219 | (void)fprintf(stderr, "error: refusing to install an unverified download\n"); |
| 13220 | cbm_unlink(tmp_archive); |
| 13221 | return CLI_TRUE; |
| 13222 | } |
| 13223 | |
| 13224 | if (extract_and_install_binary((extract_install_args_t){ |
| 13225 | .tmp_archive = tmp_archive, |
| 13226 | .ext = ext, |
| 13227 | .bin_dest = bin_dest, |
| 13228 | .home = home, |
| 13229 | .delete_indexes = delete_indexes, |
| 13230 | }) != 0) { |
| 13231 | return CLI_TRUE; |
| 13232 | } |
| 13233 | return 0; |
| 13234 | } |
| 13235 | |
| 13236 | /* Case-insensitive prefix match (portable — no strncasecmp dependency). */ |
no test coverage detected