v0.0.8 - Add FIPS npub-aware approval prompt and simplify qube installer to nsigner-only startup flow
This commit is contained in:
165
src/server.c
165
src/server.c
@@ -469,6 +469,157 @@ int transport_recv_framed(int fd, char **out_payload, size_t max_size);
|
||||
static int g_prompt_always_allow = 0;
|
||||
static int g_noninteractive_prompt_default = -1;
|
||||
|
||||
static int caller_id_extract_ipv6(const char *caller_id, char *out_ipv6, size_t out_sz) {
|
||||
const char *start;
|
||||
const char *end;
|
||||
size_t len;
|
||||
|
||||
if (caller_id == NULL || out_ipv6 == NULL || out_sz == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (strncmp(caller_id, "tcp:[", 5) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
start = caller_id + 5;
|
||||
end = strchr(start, ']');
|
||||
if (end == NULL || end[1] != ':') {
|
||||
return -1;
|
||||
}
|
||||
|
||||
len = (size_t)(end - start);
|
||||
if (len == 0 || len >= out_sz) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(out_ipv6, start, len);
|
||||
out_ipv6[len] = '\0';
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int read_cmd_output(const char *cmd, char **out_buf) {
|
||||
FILE *fp;
|
||||
char chunk[512];
|
||||
char *buf = NULL;
|
||||
size_t used = 0;
|
||||
size_t cap = 0;
|
||||
|
||||
if (cmd == NULL || out_buf == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
*out_buf = NULL;
|
||||
fp = popen(cmd, "r");
|
||||
if (fp == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
while (fgets(chunk, sizeof(chunk), fp) != NULL) {
|
||||
size_t n = strlen(chunk);
|
||||
if (used + n + 1 > cap) {
|
||||
size_t new_cap = (cap == 0) ? 2048 : cap * 2;
|
||||
while (new_cap < used + n + 1) {
|
||||
new_cap *= 2;
|
||||
}
|
||||
{
|
||||
char *tmp = (char *)realloc(buf, new_cap);
|
||||
if (tmp == NULL) {
|
||||
free(buf);
|
||||
(void)pclose(fp);
|
||||
return -1;
|
||||
}
|
||||
buf = tmp;
|
||||
cap = new_cap;
|
||||
}
|
||||
}
|
||||
memcpy(buf + used, chunk, n);
|
||||
used += n;
|
||||
}
|
||||
|
||||
(void)pclose(fp);
|
||||
|
||||
if (buf == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
buf[used] = '\0';
|
||||
*out_buf = buf;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lookup_fips_peer_for_ipv6(const char *ipv6,
|
||||
char *out_npub,
|
||||
size_t out_npub_sz,
|
||||
char *out_name,
|
||||
size_t out_name_sz) {
|
||||
char *json = NULL;
|
||||
cJSON *root = NULL;
|
||||
cJSON *peers = NULL;
|
||||
int i;
|
||||
|
||||
if (ipv6 == NULL || out_npub == NULL || out_npub_sz == 0 || out_name == NULL || out_name_sz == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
out_npub[0] = '\0';
|
||||
out_name[0] = '\0';
|
||||
|
||||
if (read_cmd_output("fipsctl show peers 2>/dev/null", &json) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
root = cJSON_Parse(json);
|
||||
free(json);
|
||||
if (root == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
peers = cJSON_GetObjectItemCaseSensitive(root, "peers");
|
||||
if (!cJSON_IsArray(peers)) {
|
||||
cJSON_Delete(root);
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (i = 0; i < cJSON_GetArraySize(peers); ++i) {
|
||||
cJSON *peer = cJSON_GetArrayItem(peers, i);
|
||||
cJSON *peer_ip;
|
||||
cJSON *peer_npub;
|
||||
cJSON *peer_name;
|
||||
|
||||
if (!cJSON_IsObject(peer)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
peer_ip = cJSON_GetObjectItemCaseSensitive(peer, "ipv6_addr");
|
||||
if (!cJSON_IsString(peer_ip) || peer_ip->valuestring == NULL) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strcmp(peer_ip->valuestring, ipv6) != 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
peer_npub = cJSON_GetObjectItemCaseSensitive(peer, "npub");
|
||||
peer_name = cJSON_GetObjectItemCaseSensitive(peer, "display_name");
|
||||
|
||||
if (cJSON_IsString(peer_npub) && peer_npub->valuestring != NULL) {
|
||||
strncpy(out_npub, peer_npub->valuestring, out_npub_sz - 1);
|
||||
out_npub[out_npub_sz - 1] = '\0';
|
||||
}
|
||||
if (cJSON_IsString(peer_name) && peer_name->valuestring != NULL) {
|
||||
strncpy(out_name, peer_name->valuestring, out_name_sz - 1);
|
||||
out_name[out_name_sz - 1] = '\0';
|
||||
}
|
||||
|
||||
cJSON_Delete(root);
|
||||
return (out_npub[0] != '\0') ? 0 : -1;
|
||||
}
|
||||
|
||||
cJSON_Delete(root);
|
||||
return -1;
|
||||
}
|
||||
|
||||
void server_set_prompt_always_allow(int enabled) {
|
||||
g_prompt_always_allow = enabled ? 1 : 0;
|
||||
}
|
||||
@@ -501,6 +652,20 @@ static int prompt_for_policy_decision(const caller_identity_t *caller,
|
||||
|
||||
printf("\nApproval required\n");
|
||||
printf("caller: %s\n", (caller != NULL) ? caller->caller_id : "unknown");
|
||||
if (caller != NULL && caller->kind == NSIGNER_LISTEN_TCP) {
|
||||
char ipv6[INET6_ADDRSTRLEN];
|
||||
char npub[128];
|
||||
char display_name[128];
|
||||
|
||||
if (caller_id_extract_ipv6(caller->caller_id, ipv6, sizeof(ipv6)) == 0 &&
|
||||
lookup_fips_peer_for_ipv6(ipv6, npub, sizeof(npub), display_name, sizeof(display_name)) == 0) {
|
||||
if (display_name[0] != '\0') {
|
||||
printf("fips peer: %s (%s)\n", npub, display_name);
|
||||
} else {
|
||||
printf("fips peer: %s\n", npub);
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("method: %s\n", (method != NULL) ? method : "unknown");
|
||||
printf("role: %s\n", (role_name != NULL) ? role_name : "unknown");
|
||||
printf("purpose: %s\n", (purpose != NULL) ? purpose : "unknown");
|
||||
|
||||
Reference in New Issue
Block a user