#define _POSIX_C_SOURCE 200809L #include "geminux-dns-valid.h" #include #include #include #include #include #include #include #include #include #define GEMINUX_DNS_CONF_DIR "/etc/geminux" #define GEMINUX_DNS_CONF_PATH "/etc/geminux/dns.conf" #define GEMINUX_NM_DNS_DROPIN "/etc/NetworkManager/conf.d/30-geminux-dns.conf" static int run_argv(const char *const argv[], char *out, size_t out_sz, bool quiet) { int pfd[2] = {-1, -1}; if (out && out_sz > 0) { out[0] = '\0'; if (pipe(pfd) < 0) return -1; } pid_t pid = fork(); if (pid < 0) { if (pfd[0] >= 0) { close(pfd[0]); close(pfd[1]); } return -1; } if (pid == 0) { if (pfd[1] >= 0) { close(pfd[0]); dup2(pfd[1], STDOUT_FILENO); if (pfd[1] > 2) close(pfd[1]); } if (quiet || pfd[1] >= 0) { int devnull = open("/dev/null", O_RDWR); if (devnull >= 0) { if (quiet && pfd[1] < 0) dup2(devnull, STDOUT_FILENO); if (quiet) dup2(devnull, STDERR_FILENO); if (devnull > 2) close(devnull); } } execvp(argv[0], (char *const *)argv); _exit(127); } if (pfd[1] >= 0) close(pfd[1]); if (pfd[0] >= 0) { size_t pos = 0; while (pos + 1 < out_sz) { ssize_t n = read(pfd[0], out + pos, out_sz - 1 - pos); if (n > 0) pos += (size_t)n; else if (n < 0 && errno == EINTR) continue; else break; } out[pos] = '\0'; close(pfd[0]); } int status = 0; while (waitpid(pid, &status, 0) < 0) { if (errno != EINTR) return -1; } if (WIFEXITED(status)) return WEXITSTATUS(status); return 1; } static int write_file_atomic(const char *path, const char *content, mode_t mode) { char tmp[512]; snprintf(tmp, sizeof(tmp), "%s.tmp", path); int fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC, mode); if (fd < 0) return -1; size_t len = strlen(content); size_t off = 0; while (off < len) { ssize_t w = write(fd, content + off, len - off); if (w < 0) { if (errno == EINTR) continue; close(fd); unlink(tmp); return -1; } off += (size_t)w; } close(fd); chmod(tmp, mode); if (rename(tmp, path) != 0) { unlink(tmp); return -1; } return 0; } static void read_saved_conf(char *mode_out, size_t mode_sz, char *custom_out, size_t custom_sz) { snprintf(mode_out, mode_sz, "default"); custom_out[0] = '\0'; FILE *f = fopen(GEMINUX_DNS_CONF_PATH, "r"); if (!f) return; char line[256]; while (fgets(line, sizeof(line), f)) { size_t n = strlen(line); while (n > 0 && (line[n - 1] == '\n' || line[n - 1] == '\r')) line[--n] = '\0'; if (strncmp(line, "mode=", 5) == 0 && geminux_dns_mode_valid(line + 5)) { snprintf(mode_out, mode_sz, "%s", line + 5); } else if (strncmp(line, "custom=", 7) == 0) { snprintf(custom_out, custom_sz, "%s", line + 7); } } fclose(f); } static bool ipv6_default_route_present(void) { FILE *f = fopen("/proc/net/ipv6_route", "r"); if (!f) return false; char line[512]; bool found = false; while (fgets(line, sizeof(line), f)) { char dst[64], plen[16], src[64], splen[16], nh[64]; unsigned int metric, refcnt, use, flags; char iface[32]; if (sscanf(line, "%63s %15s %63s %15s %63s %x %x %x %x %31s", dst, plen, src, splen, nh, &metric, &refcnt, &use, &flags, iface) == 10) { if (strcmp(dst, "00000000000000000000000000000000") == 0 && strcmp(plen, "00") == 0 && strcmp(iface, "lo") != 0 && (flags & 1U)) { found = true; break; } } } fclose(f); return found; } static void join_space(char *dst, size_t dst_sz, char arr[][16], int n) { dst[0] = '\0'; size_t pos = 0; for (int i = 0; i < n; i++) { int w = snprintf(dst + pos, dst_sz - pos, "%s%s", i ? " " : "", arr[i]); if (w > 0 && (size_t)w < dst_sz - pos) pos += (size_t)w; } } static void join_space_v6(char *dst, size_t dst_sz, char arr[][46], int n) { dst[0] = '\0'; size_t pos = 0; for (int i = 0; i < n; i++) { int w = snprintf(dst + pos, dst_sz - pos, "%s%s", i ? " " : "", arr[i]); if (w > 0 && (size_t)w < dst_sz - pos) pos += (size_t)w; } } static bool nm_type_managed_dns(const char *type) { return strcmp(type, "802-11-wireless") == 0 || strcmp(type, "gsm") == 0 || strcmp(type, "802-3-ethernet") == 0; } static void update_nm_profiles(const char *v4_space, const char *v6_space, bool reset_default) { char list[8192]; const char *show[] = {"nmcli", "-t", "-f", "UUID,TYPE", "connection", "show", NULL}; if (run_argv(show, list, sizeof(list), true) != 0) return; char *save = NULL; for (char *ln = strtok_r(list, "\n", &save); ln; ln = strtok_r(NULL, "\n", &save)) { char *sep = strchr(ln, ':'); if (!sep) continue; *sep = '\0'; const char *uuid = ln; const char *type = sep + 1; if (!uuid[0] || !nm_type_managed_dns(type)) continue; if (reset_default) { const char *mod[] = { "nmcli", "connection", "modify", "uuid", uuid, "ipv4.dns", "", "ipv4.ignore-auto-dns", "no", "ipv6.dns", "", "ipv6.ignore-auto-dns", "no", NULL }; run_argv(mod, NULL, 0, true); } else { const char *mod[] = { "nmcli", "connection", "modify", "uuid", uuid, "ipv4.dns", v4_space ? v4_space : "", "ipv4.ignore-auto-dns", "yes", "ipv6.dns", v6_space ? v6_space : "", "ipv6.ignore-auto-dns", "yes", NULL }; run_argv(mod, NULL, 0, true); } } } static void write_resolv_fallback(char v4[][16], int n_v4, char v6[][46], int n_v6, bool use_v6) { char buf[512]; size_t pos = 0; int written = 0; int w = snprintf(buf + pos, sizeof(buf) - pos, "# Managed by geminux-dnsctl\n"); if (w > 0 && (size_t)w < sizeof(buf) - pos) pos += (size_t)w; for (int i = 0; i < n_v4 && written < 3; i++) { w = snprintf(buf + pos, sizeof(buf) - pos, "nameserver %s\n", v4[i]); if (w > 0 && (size_t)w < sizeof(buf) - pos) pos += (size_t)w; written++; } if (use_v6) { for (int i = 0; i < n_v6 && written < 3; i++) { w = snprintf(buf + pos, sizeof(buf) - pos, "nameserver %s\n", v6[i]); if (w > 0 && (size_t)w < sizeof(buf) - pos) pos += (size_t)w; written++; } } w = snprintf(buf + pos, sizeof(buf) - pos, "options timeout:2 attempts:2\n"); if (w > 0 && (size_t)w < sizeof(buf) - pos) pos += (size_t)w; write_file_atomic("/etc/resolv.conf", buf, 0644); } static bool resolv_has_first_expected(const char *expected_first) { if (!expected_first || !expected_first[0]) return false; FILE *f = fopen("/etc/resolv.conf", "r"); if (!f) return false; char line[256]; bool found = false; while (fgets(line, sizeof(line), f)) { char ns[64]; if (sscanf(line, "nameserver %63s", ns) == 1) { if (strcmp(ns, expected_first) == 0) found = true; break; } } fclose(f); return found; } static int apply_dns(const char *mode, const char *custom_arg, bool save_conf) { int idx = geminux_dns_mode_index(mode); if (idx < 0) { fprintf(stderr, "geminux-dnsctl: invalid DNS mode '%s'\n", mode ? mode : ""); return 1; } char prev_mode[32], prev_custom[256]; read_saved_conf(prev_mode, sizeof(prev_mode), prev_custom, sizeof(prev_custom)); char saved_custom[256]; snprintf(saved_custom, sizeof(saved_custom), "%s", prev_custom); if (idx == GEMINUX_DNS_DEFAULT) { unlink(GEMINUX_NM_DNS_DROPIN); if (save_conf) { mkdir(GEMINUX_DNS_CONF_DIR, 0755); char cfg[384]; snprintf(cfg, sizeof(cfg), "mode=default\ncustom=%s\n", saved_custom); if (write_file_atomic(GEMINUX_DNS_CONF_PATH, cfg, 0644) != 0) { perror("geminux-dnsctl: write dns.conf"); return 1; } } update_nm_profiles("", "", true); const char *rel[] = {"nmcli", "general", "reload", NULL}; run_argv(rel, NULL, 0, true); const char *reapp[] = {"nmcli", "device", "reapply", "wlan0", NULL}; run_argv(reapp, NULL, 0, true); return 0; } const char *raw_servers = NULL; char combined_preset[192]; if (idx == GEMINUX_DNS_CUSTOM) { raw_servers = (custom_arg && custom_arg[0]) ? custom_arg : prev_custom; } else { const geminux_dns_preset_t *p = &GEMINUX_DNS_PRESETS[idx]; if (p->v6_csv[0]) { snprintf(combined_preset, sizeof(combined_preset), "%s,%s", p->v4_csv, p->v6_csv); raw_servers = combined_preset; } else { raw_servers = p->v4_csv; } } char v4[GEMINUX_DNS_MAX_SERVERS][16]; char v6[GEMINUX_DNS_MAX_SERVERS][46]; int n_v4 = 0, n_v6 = 0; char norm_csv[256]; if (geminux_parse_dns_servers(raw_servers, v4, &n_v4, GEMINUX_DNS_MAX_SERVERS, v6, &n_v6, GEMINUX_DNS_MAX_SERVERS, norm_csv, sizeof(norm_csv)) <= 0) { fprintf(stderr, "geminux-dnsctl: invalid nameserver list '%s'\n", raw_servers ? raw_servers : ""); return 1; } if (idx == GEMINUX_DNS_CUSTOM) { snprintf(saved_custom, sizeof(saved_custom), "%s", norm_csv); } bool has_v6_route = ipv6_default_route_present(); bool include_v6_global = (idx == GEMINUX_DNS_CUSTOM) ? (n_v6 > 0 && (n_v4 == 0 || has_v6_route)) : (n_v6 > 0 && has_v6_route); char global_servers[256] = {0}; size_t gpos = 0; for (int i = 0; i < n_v4; i++) { int w = snprintf(global_servers + gpos, sizeof(global_servers) - gpos, "%s%s", gpos ? "," : "", v4[i]); if (w > 0 && (size_t)w < sizeof(global_servers) - gpos) gpos += (size_t)w; } if (include_v6_global) { for (int i = 0; i < n_v6; i++) { int w = snprintf(global_servers + gpos, sizeof(global_servers) - gpos, "%s%s", gpos ? "," : "", v6[i]); if (w > 0 && (size_t)w < sizeof(global_servers) - gpos) gpos += (size_t)w; } } if (save_conf) { mkdir(GEMINUX_DNS_CONF_DIR, 0755); char cfg[384]; snprintf(cfg, sizeof(cfg), "mode=%s\ncustom=%s\n", mode, saved_custom); if (write_file_atomic(GEMINUX_DNS_CONF_PATH, cfg, 0644) != 0) { perror("geminux-dnsctl: write dns.conf"); return 1; } } mkdir("/etc/NetworkManager", 0755); mkdir("/etc/NetworkManager/conf.d", 0755); char dropin[512]; snprintf(dropin, sizeof(dropin), "# Managed by geminux-dnsctl\n" "[main]\n" "dns=default\n\n" "[global-dns]\n" "options=timeout:2,attempts:2\n\n" "[global-dns-domain-*]\n" "servers=%s\n", global_servers); if (write_file_atomic(GEMINUX_NM_DNS_DROPIN, dropin, 0644) != 0) { perror("geminux-dnsctl: write NM dropin"); return 1; } char v4_space[128], v6_space[256]; join_space(v4_space, sizeof(v4_space), v4, n_v4); join_space_v6(v6_space, sizeof(v6_space), v6, include_v6_global ? n_v6 : 0); update_nm_profiles(v4_space, v6_space, false); const char *rel[] = {"nmcli", "general", "reload", NULL}; run_argv(rel, NULL, 0, true); const char *reapp[] = {"nmcli", "device", "reapply", "wlan0", NULL}; run_argv(reapp, NULL, 0, true); const char *first_expected = (n_v4 > 0) ? v4[0] : (n_v6 > 0 ? v6[0] : ""); if (first_expected[0] && !resolv_has_first_expected(first_expected)) { write_resolv_fallback(v4, n_v4, v6, n_v6, include_v6_global); } return 0; } int main(int argc, char **argv) { if (argc >= 3 && strcmp(argv[1], "set") == 0) { const char *mode = argv[2]; const char *custom = (argc >= 4) ? argv[3] : ""; if (argc > 4) { fprintf(stderr, "Usage: %s \n", argv[0]); return 1; } return apply_dns(mode, custom, true); } if (argc == 2 && strcmp(argv[1], "apply") == 0) { char mode[32], custom[256]; read_saved_conf(mode, sizeof(mode), custom, sizeof(custom)); if (strcmp(mode, "default") == 0) return 0; return apply_dns(mode, custom, false); } fprintf(stderr, "Usage: %s \n", argv[0]); return 1; }