From ebb8fe3a849c633a180724c865b7eeb44a85bbc7 Mon Sep 17 00:00:00 2001 From: Kinzie Date: Tue, 14 May 2024 08:34:16 +0100 Subject: [PATCH] Fix xauth command for some shells and fix building in ReleaseSafe --- build.zig | 10 ++++++++++ src/auth.zig | 35 +++++++++++++++++++++++++++++++++-- src/config/Config.zig | 2 +- src/interop.zig | 5 +---- 4 files changed, 45 insertions(+), 7 deletions(-) diff --git a/build.zig b/build.zig index 97852f4..832d6bc 100644 --- a/build.zig +++ b/build.zig @@ -40,6 +40,16 @@ pub fn build(b: *std.Build) !void { exe.linkSystemLibrary("xcb"); exe.linkLibC(); + // Only fails with ReleaseSafe, so we'll override it. + const translate_c = b.addTranslateC(.{ + .root_source_file = .{ .path = "include/termbox2.h" }, + .target = target, + .optimize = if (optimize == .ReleaseSafe) .ReleaseFast else optimize, + }); + translate_c.defineCMacroRaw("TB_IMPL"); + const termbox2 = translate_c.addModule("termbox2"); + exe.root_module.addImport("termbox2", termbox2); + b.installArtifact(exe); const run_cmd = b.addRunArtifact(exe); diff --git a/src/auth.zig b/src/auth.zig index 8aab524..5b194e2 100644 --- a/src/auth.zig +++ b/src/auth.zig @@ -327,7 +327,36 @@ fn createXauthFile(pwd: [:0]const u8) ![:0]const u8 { return xauthority; } -fn xauth(display_name: [:0]u8, shell: [*:0]const u8, pw_dir: [*:0]const u8, xauth_cmd: []const u8, mcookie_cmd: []const u8) !void { +pub fn mcookie(cmd: [:0]const u8) ![32]u8 { + const pipe = try std.posix.pipe(); + defer std.posix.close(pipe[1]); + + const output = std.fs.File{ .handle = pipe[0] }; + defer output.close(); + + const pid = try std.posix.fork(); + if (pid == 0) { + std.posix.close(pipe[0]); + + std.posix.dup2(pipe[1], std.posix.STDOUT_FILENO) catch std.process.exit(1); + std.posix.close(pipe[1]); + + const args = [_:null]?[*:0]u8{}; + std.posix.execveZ(cmd.ptr, &args, std.c.environ) catch {}; + std.process.exit(1); + } + + const result = std.posix.waitpid(pid, 0); + + if (result.status != 0) return error.CommandFailed; + + var buf: [32]u8 = undefined; + const len = try output.read(&buf); + if (len != 32) return error.UnexpectedLength; + return buf; +} + +fn xauth(display_name: [:0]u8, shell: [*:0]const u8, pw_dir: [*:0]const u8, xauth_cmd: []const u8, mcookie_cmd: [:0]const u8) !void { var pwd_buf: [100]u8 = undefined; const pwd = try std.fmt.bufPrintZ(&pwd_buf, "{s}", .{pw_dir}); @@ -335,10 +364,12 @@ fn xauth(display_name: [:0]u8, shell: [*:0]const u8, pw_dir: [*:0]const u8, xaut _ = interop.setenv("XAUTHORITY", xauthority, 1); _ = interop.setenv("DISPLAY", display_name, 1); + const mcookie_output = try mcookie(mcookie_cmd); + const pid = try std.posix.fork(); if (pid == 0) { var cmd_buffer: [1024]u8 = undefined; - const cmd_str = std.fmt.bufPrintZ(&cmd_buffer, "{s} add {s} . $({s})", .{ xauth_cmd, display_name, mcookie_cmd }) catch std.process.exit(1); + const cmd_str = std.fmt.bufPrintZ(&cmd_buffer, "{s} add {s} . {s}", .{ xauth_cmd, display_name, mcookie_output }) catch std.process.exit(1); const args = [_:null]?[*:0]const u8{ shell, "-c", cmd_str }; std.posix.execveZ(shell, &args, std.c.environ) catch {}; std.process.exit(1); diff --git a/src/config/Config.zig b/src/config/Config.zig index a3baab3..bddda19 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -27,7 +27,7 @@ margin_box_v: u8 = 1, max_desktop_len: u8 = 100, max_login_len: u8 = 255, max_password_len: u8 = 255, -mcookie_cmd: []const u8 = "/usr/bin/mcookie", +mcookie_cmd: [:0]const u8 = "/usr/bin/mcookie", min_refresh_delta: u16 = 5, numlock: bool = false, path: ?[:0]const u8 = "/sbin:/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin", diff --git a/src/interop.zig b/src/interop.zig index 1fe89fc..998065f 100644 --- a/src/interop.zig +++ b/src/interop.zig @@ -2,10 +2,7 @@ const std = @import("std"); const builtin = @import("builtin"); const Allocator = std.mem.Allocator; -pub const termbox = @cImport({ - @cDefine("TB_IMPL", {}); - @cInclude("termbox2.h"); -}); +pub const termbox = @import("termbox2"); pub const pam = @cImport({ @cInclude("security/pam_appl.h");