// SPDX-License-Identifier: GPL-2.0-only // Bananarchy Passcode & Duress Passcode Configurator DWM Plugin (240x320) // Configures the numeric Linux user password (/etc/shadow) and optional // lockscreen Duress Mode passcode (/etc/bananarchy_duress.hash). #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "../sxmo-dwm/dwm_plugin.h" #include "phone_data.h" namespace { using namespace bananarchy; volatile sig_atomic_t gRunning = 1; void handleSignal(int) { gRunning = 0; } bool queryDuressConfigured() { pid_t pid = ::fork(); if (pid == 0) { int nullfd = ::open("/dev/null", O_RDWR); if (nullfd >= 0) { ::dup2(nullfd, STDIN_FILENO); ::dup2(nullfd, STDOUT_FILENO); ::dup2(nullfd, STDERR_FILENO); if (nullfd > 2) { ::close(nullfd); } } ::execl("/usr/bin/doas", "doas", "-n", "/usr/bin/bananarchy-verify-passcode", "--has-duress", "user", static_cast(nullptr)); ::_exit(1); } int status = 0; if (pid > 0 && ::waitpid(pid, &status, 0) > 0) { return WIFEXITED(status) && WEXITSTATUS(status) == 0; } return false; } int runPasscodeHelper(const char* modeFlag, const std::string& stdinData, std::string& outMsg) { outMsg.clear(); int inPipe[2]; int outPipe[2]; if (::pipe(inPipe) != 0) { outMsg = "System pipe error"; return -1; } if (::pipe(outPipe) != 0) { ::close(inPipe[0]); ::close(inPipe[1]); outMsg = "System pipe error"; return -1; } pid_t pid = ::fork(); if (pid == 0) { ::close(inPipe[1]); ::close(outPipe[0]); ::dup2(inPipe[0], STDIN_FILENO); ::dup2(outPipe[1], STDOUT_FILENO); ::dup2(outPipe[1], STDERR_FILENO); if (inPipe[0] > 2) { ::close(inPipe[0]); } if (outPipe[1] > 2) { ::close(outPipe[1]); } ::execl("/usr/bin/doas", "doas", "-n", "/usr/bin/bananarchy-verify-passcode", modeFlag, "user", static_cast(nullptr)); ::_exit(127); } ::close(inPipe[0]); ::close(outPipe[1]); if (!stdinData.empty()) { (void)::write(inPipe[1], stdinData.data(), stdinData.size()); } ::close(inPipe[1]); char buf[256]; ssize_t n = ::read(outPipe[0], buf, sizeof(buf) - 1); ::close(outPipe[0]); if (n > 0) { buf[n] = '\0'; outMsg = trimStr(std::string(buf)); } int status = 0; if (pid > 0 && ::waitpid(pid, &status, 0) > 0 && WIFEXITED(status)) { return WEXITSTATUS(status); } return -1; } std::string maskDigits(const std::string& s, bool reveal) { if (s.empty()) { return ""; } if (reveal) { return s; } std::string out; out.reserve(s.size() * 2); for (size_t i = 0; i < s.size(); ++i) { if (i > 0) { out.push_back(' '); } out.push_back('*'); } return out; } int passcode_run_main(int argc, char** argv) { gRunning = 1; bool isPreload = false; for (int i = 1; i < argc; ++i) { if (argv[i] && std::strcmp(argv[i], "--preload") == 0) { isPreload = true; } } if (!isPreload && ::access("/tmp/bananarchy_duress_mode", F_OK) == 0) { return 0; } if (sendPluginCommand("passcode", isPreload ? "NOP" : "OPEN")) { return 0; } int daemonLockFd = acquirePluginDaemonLock("passcode"); if (daemonLockFd < 0) { for (int i = 0; i < 10; ++i) { ::usleep(30000); if (sendPluginCommand("passcode", isPreload ? "NOP" : "OPEN")) { return 0; } } return 0; } std::string daemonPidPath = getPluginDaemonPidPath("passcode"); std::string activePidPath = getRuntimeFile("xpasscode.pid"); writeIntFile(daemonPidPath, static_cast(::getpid())); int fifoFd = setupPluginDaemonFifo("passcode"); struct sigaction sa {}; sa.sa_handler = handleSignal; sigemptyset(&sa.sa_mask); sigaction(SIGTERM, &sa, nullptr); sigaction(SIGINT, &sa, nullptr); sigaction(SIGHUP, &sa, nullptr); NeoWindow neo; if (!neo.create("xpasscode", "XPasscode", "Passcode Settings", kWinX, kWinY, kWinWidth, kWinHeight, true, ExposureMask | KeyPressMask | KeyReleaseMask | VisibilityChangeMask | StructureNotifyMask)) { if (fifoFd >= 0) { ::close(fifoFd); } ::unlink(daemonPidPath.c_str()); return 1; } Display* dpy = neo.dpy; Window win = neo.win; Pixmap pix = neo.pix; GC gc = neo.gc; PangoLayout* layoutMain = neo.layoutMain; PangoLayout* layoutSmall = neo.layoutSmall; PangoLayout* layoutIcon = neo.layoutIcon; PluginTheme& theme = neo.theme; bool visible = false; int activeTab = 0; // 0 = User Passcode, 1 = Duress Passcode int focusIdx = 0; // 0=Current, 1=New, 2=Confirm, 3=Save, 4=Clear (tab 1) bool revealDigits = false; bool duressSet = false; std::string userFields[3]; // [0]=cur, [1]=new, [2]=confirm std::string duressFields[3]; // [0]=curUser, [1]=newDuress, [2]=confirmDuress std::string statusMsg; bool statusError = false; int64_t statusUntilMs = 0; auto resetFormState = [&]() { for (int i = 0; i < 3; ++i) { std::fill(userFields[i].begin(), userFields[i].end(), '\0'); userFields[i].clear(); std::fill(duressFields[i].begin(), duressFields[i].end(), '\0'); duressFields[i].clear(); } focusIdx = 0; revealDigits = false; statusMsg.clear(); statusError = false; statusUntilMs = 0; duressSet = queryDuressConfigured(); }; auto setStatus = [&](const std::string& msg, bool isErr) { statusMsg = msg; statusError = isErr; statusUntilMs = nowMs() + 3500; }; auto maxFocusForTab = [&]() -> int { if (activeTab == 0) { return 3; // 0, 1, 2, 3(Save) } return duressSet ? 4 : 3; // 0, 1, 2, 3(Save), 4(Clear) }; auto openWindow = [&]() { if (::access("/tmp/bananarchy_duress_mode", F_OK) == 0 || isPhonePasscodeLocked()) { return; } closeOtherPlugins("passcode"); writeIntFile(activePidPath, static_cast(::getpid())); resetFormState(); activeTab = 0; if (!visible) { XMapRaised(dpy, win); XMoveResizeWindow(dpy, win, kWinX, kWinY, kWinWidth, kWinHeight); visible = true; } grabPluginKeyboard(dpy, win); }; auto closeWindow = [&]() { resetFormState(); if (visible) { XUngrabKeyboard(dpy, CurrentTime); XUnmapWindow(dpy, win); XFlush(dpy); visible = false; } ::unlink(activePidPath.c_str()); }; auto submitUserPasscode = [&]() { const std::string& curP = userFields[0]; const std::string& newP = userFields[1]; const std::string& cnfP = userFields[2]; if (curP.size() < 4) { setStatus("Enter current passcode (4+ digits)", true); focusIdx = 0; return; } if (newP.size() < 4) { setStatus("New passcode must be 4+ digits", true); focusIdx = 1; return; } if (newP != cnfP) { setStatus("New passcodes do not match", true); focusIdx = 2; return; } std::string out; std::string payload = curP + "\n" + newP + "\n"; int rc = runPasscodeHelper("--set-user-pass", payload, out); std::fill(payload.begin(), payload.end(), '\0'); if (rc == 0) { for (int i = 0; i < 3; ++i) { std::fill(userFields[i].begin(), userFields[i].end(), '\0'); userFields[i].clear(); } focusIdx = 0; sendPluginCommand("locker", "RELOAD_HASH"); runAsyncCmd("sxmo_vibrate 40 >/dev/null 2>&1"); setStatus(out.empty() ? "User passcode updated" : out, false); } else { setStatus(out.empty() ? "Failed to update passcode" : out, true); } }; auto submitDuressPasscode = [&]() { const std::string& curP = duressFields[0]; const std::string& newP = duressFields[1]; const std::string& cnfP = duressFields[2]; if (curP.size() < 4) { setStatus("Enter current user passcode", true); focusIdx = 0; return; } if (newP.size() < 4) { setStatus("Duress passcode must be 4+ digits", true); focusIdx = 1; return; } if (newP != cnfP) { setStatus("Duress passcodes do not match", true); focusIdx = 2; return; } std::string out; std::string payload = curP + "\n" + newP + "\n"; int rc = runPasscodeHelper("--set-duress-pass", payload, out); std::fill(payload.begin(), payload.end(), '\0'); if (rc == 0) { for (int i = 0; i < 3; ++i) { std::fill(duressFields[i].begin(), duressFields[i].end(), '\0'); duressFields[i].clear(); } duressSet = queryDuressConfigured(); focusIdx = 0; sendPluginCommand("locker", "RELOAD_HASH"); runAsyncCmd("sxmo_vibrate 40 >/dev/null 2>&1"); setStatus(out.empty() ? "Duress passcode saved" : out, false); } else { setStatus(out.empty() ? "Failed to set duress passcode" : out, true); } }; auto clearDuressPasscode = [&]() { const std::string& curP = duressFields[0]; if (curP.size() < 4) { setStatus("Enter current user passcode first", true); focusIdx = 0; return; } std::string out; std::string payload = curP + "\n"; int rc = runPasscodeHelper("--clear-duress-pass", payload, out); std::fill(payload.begin(), payload.end(), '\0'); if (rc == 0) { for (int i = 0; i < 3; ++i) { std::fill(duressFields[i].begin(), duressFields[i].end(), '\0'); duressFields[i].clear(); } duressSet = queryDuressConfigured(); focusIdx = 0; sendPluginCommand("locker", "RELOAD_HASH"); runAsyncCmd("sxmo_vibrate 40 >/dev/null 2>&1"); setStatus(out.empty() ? "Duress passcode cleared" : out, false); } else { setStatus(out.empty() ? "Failed to clear duress passcode" : out, true); } }; auto render = [&]() { if (!visible) { return; } int64_t now = nowMs(); if (statusUntilMs > 0 && now >= statusUntilMs) { statusMsg.clear(); statusError = false; statusUntilMs = 0; } neo.clearBg(); // Header (Y=0..26) XSetForeground(dpy, gc, theme.spec.bgHeader); XFillRectangle(dpy, pix, gc, 0, 0, kWinWidth, 26); neo.drawPango(layoutIcon, &theme.colYellow, 6, 0, 26, kIconKey); neo.drawPango(layoutMain, &theme.colWhite, 28, 0, 26, "Passcode Setup", 150); std::string rightBadge = (activeTab == 1) ? (duressSet ? "SET" : "OFF") : "0-9"; int rbw = neo.pangoWidth(layoutSmall, rightBadge); neo.drawPango(layoutSmall, (activeTab == 1 && duressSet) ? &theme.colGreen : &theme.colMuted, kWinWidth - rbw - 8, 0, 26, rightBadge); XSetForeground(dpy, gc, theme.spec.border); XDrawLine(dpy, pix, gc, 0, 25, kWinWidth, 25); // Tab bar (Y=26..48) const char* tabs[2] = {"User PIN", "Duress PIN"}; for (int t = 0; t < 2; ++t) { int tx = t * (kWinWidth / 2); int tw = kWinWidth / 2; bool isAct = (activeTab == t); XSetForeground(dpy, gc, isAct ? theme.spec.selBg : theme.spec.bgAltEven); XFillRectangle(dpy, pix, gc, tx, 26, tw, 22); if (isAct) { XSetForeground(dpy, gc, theme.spec.accent); XFillRectangle(dpy, pix, gc, tx, 46, tw, 2); } neo.drawPango(layoutSmall, isAct ? &theme.colWhite : &theme.colMuted, tx, 26, 22, tabs[t], tw, 1); } std::string* fields = (activeTab == 0) ? userFields : duressFields; const char* labels[3] = { (activeTab == 0) ? "Current User Passcode" : "Current User Passcode", (activeTab == 0) ? "New User Passcode (4+ digits)" : "New Duress Passcode (4+ digits)", (activeTab == 0) ? "Confirm New Passcode" : "Confirm Duress Passcode", }; int y = 52; for (int i = 0; i < 3; ++i) { bool focused = (focusIdx == i); neo.drawPango(layoutSmall, focused ? &theme.colYellow : &theme.colMuted, 10, y, 15, labels[i], kWinWidth - 20); y += 15; int boxX = 8; int boxW = kWinWidth - 16; int boxH = 26; XSetForeground(dpy, gc, theme.spec.bgInput); XFillRectangle(dpy, pix, gc, boxX, y, boxW, boxH); XSetForeground(dpy, gc, focused ? theme.spec.selBg : theme.spec.border); XDrawRectangle(dpy, pix, gc, boxX, y, boxW - 1, boxH - 1); if (focused) { XDrawRectangle(dpy, pix, gc, boxX + 1, y + 1, boxW - 3, boxH - 3); } std::string valStr = maskDigits(fields[i], revealDigits); if (valStr.empty()) { std::string hint = focused ? "Type digits 0-9..." : "----"; neo.drawPango(layoutSmall, &theme.colMuted, boxX + 6, y + 2, boxH - 4, hint, boxW - 44); } else { neo.drawPango(layoutMain, &theme.colWhite, boxX + 6, y + 2, boxH - 4, valStr, boxW - 44); } std::string lenBadge = fields[i].empty() ? "" : (std::to_string(fields[i].size()) + "d"); if (!lenBadge.empty()) { int lw = neo.pangoWidth(layoutSmall, lenBadge); XftColor* lcol = (fields[i].size() >= 4) ? &theme.colGreen : &theme.colMuted; neo.drawPango(layoutSmall, lcol, boxX + boxW - lw - 6, y + 2, boxH - 4, lenBadge); } y += boxH + 4; } // Action buttons int btnY = y + 2; int btnH = 24; bool saveFocused = (focusIdx == 3); uint32_t saveBg = saveFocused ? theme.spec.selBg : theme.spec.bgAltOdd; XSetForeground(dpy, gc, saveBg); XFillRectangle(dpy, pix, gc, 8, btnY, kWinWidth - 16, btnH); XSetForeground(dpy, gc, saveFocused ? theme.spec.accent : theme.spec.border); XDrawRectangle(dpy, pix, gc, 8, btnY, kWinWidth - 17, btnH - 1); std::string saveLabel = (activeTab == 0) ? (std::string(kIconCheck) + " Save User Passcode") : (std::string(kIconShield) + " Save Duress Passcode"); neo.drawPango(layoutMain, saveFocused ? &theme.colWhite : &theme.colLight, 8, btnY, btnH, saveLabel, kWinWidth - 16, 1); btnY += btnH + 4; if (activeTab == 1 && duressSet) { bool clrFocused = (focusIdx == 4); uint32_t clrBg = clrFocused ? theme.spec.danger : theme.spec.bgAltEven; XSetForeground(dpy, gc, clrBg); XFillRectangle(dpy, pix, gc, 8, btnY, kWinWidth - 16, btnH); XSetForeground(dpy, gc, clrFocused ? theme.spec.danger : theme.spec.border); XDrawRectangle(dpy, pix, gc, 8, btnY, kWinWidth - 17, btnH - 1); std::string clrLabel = std::string(kIconTrash) + " Clear Duress Passcode"; neo.drawPango(layoutSmall, clrFocused ? &theme.colWhite : &theme.colRed, 8, btnY, btnH, clrLabel, kWinWidth - 16, 1); btnY += btnH + 4; } // Status message or help footer above softbar int statusY = kWinHeight - 46; if (!statusMsg.empty()) { XSetForeground(dpy, gc, theme.spec.bgHeader); XFillRectangle(dpy, pix, gc, 8, statusY, kWinWidth - 16, 20); XSetForeground(dpy, gc, statusError ? theme.spec.danger : theme.spec.accent); XDrawRectangle(dpy, pix, gc, 8, statusY, kWinWidth - 17, 19); neo.drawPango(layoutSmall, statusError ? &theme.colRed : &theme.colGreen, 12, statusY, 20, statusMsg, kWinWidth - 24, 1); } else { std::string footer = revealDigits ? "*: Hide digits | Numeric only (4+)" : "*: Show digits | Numeric only (4+)"; neo.drawPango(layoutSmall, &theme.colMuted, 8, statusY, 20, footer, kWinWidth - 16, 1); } // Softbar const char* leftLbl = (focusIdx < 2) ? "Next" : "Save"; if (focusIdx == 4) { leftLbl = "Clear"; } const char* rightLbl = (focusIdx <= 2 && !fields[focusIdx].empty()) ? "Del" : "Back"; neo.drawSoftbar(leftLbl, "OK", rightLbl, &theme.colYellow); neo.present(); }; if (!isPreload) { openWindow(); render(); } int xfd = ConnectionNumber(dpy); while (gRunning) { if (fifoFd >= 0) { for (const std::string& cmd : readFifoLines(fifoFd)) { if (cmd == "NOP") { continue; } if (cmd == "RELOAD_THEME") { neo.reloadTheme(); if (visible) { render(); } continue; } if (cmd == "OPEN" || cmd.rfind("OPEN ", 0) == 0) { openWindow(); render(); continue; } if (cmd == "CLOSE") { closeWindow(); continue; } } } if (visible && neo.checkReloadTheme()) { render(); } while (XPending(dpy) > 0) { XEvent ev; XNextEvent(dpy, &ev); if (ev.type == Expose) { if (visible && ev.xexpose.count == 0) { render(); } } else if (ev.type == KeyPress) { if (!visible) { continue; } unsigned int kc = ev.xkey.keycode; KeySym sym = XLookupKeysym(&ev.xkey, 0); if (querySoftkeysDown(dpy) == 3) { writeSoftkeySuppress(nowMs() + 400); closeWindow(); sendPluginCommand("xdrawer", "OPEN"); continue; } std::string* fields = (activeTab == 0) ? userFields : duressFields; int maxFocus = maxFocusForTab(); if (sym == XK_Left || sym == XK_KP_Left) { activeTab = (activeTab == 0) ? 1 : 0; if (focusIdx > maxFocusForTab()) { focusIdx = maxFocusForTab(); } statusMsg.clear(); render(); continue; } if (sym == XK_Right || sym == XK_KP_Right) { activeTab = (activeTab == 0) ? 1 : 0; if (focusIdx > maxFocusForTab()) { focusIdx = maxFocusForTab(); } statusMsg.clear(); render(); continue; } if (sym == XK_Up || sym == XK_KP_Up) { focusIdx = (focusIdx > 0) ? (focusIdx - 1) : maxFocus; render(); continue; } if (sym == XK_Down || sym == XK_KP_Down) { focusIdx = (focusIdx < maxFocus) ? (focusIdx + 1) : 0; render(); continue; } if (isStarKey(sym, kc)) { revealDigits = !revealDigits; render(); continue; } if (isRightSoftkey(sym, kc) || sym == XK_BackSpace || sym == XK_Delete) { if (focusIdx >= 0 && focusIdx <= 2 && !fields[focusIdx].empty()) { fields[focusIdx].pop_back(); render(); } else if (isRightSoftkey(sym, kc) || sym == XK_Escape) { writeSoftkeySuppress(nowMs() + 300); closeWindow(); } continue; } if (isPowerKey(sym, kc)) { closeWindow(); openPowerMenuFromPlugin(dpy); continue; } if (sym == XK_Escape || sym == XF86XK_HangupPhone || sym == XK_Cancel) { writeSoftkeySuppress(nowMs() + 300); closeWindow(); continue; } if (isLeftSoftkey(sym, kc) || sym == XK_Return || sym == XK_KP_Enter || isPoundKey(sym, kc)) { if (focusIdx < 2) { ++focusIdx; } else if (focusIdx == 2 || focusIdx == 3) { if (activeTab == 0) { submitUserPasscode(); } else { submitDuressPasscode(); } } else if (focusIdx == 4 && activeTab == 1 && duressSet) { clearDuressPasscode(); } render(); continue; } char ch = mapDialKey(sym, kc, ev.xkey.state); if (ch >= '0' && ch <= '9') { if (focusIdx > 2) { focusIdx = 0; } if (fields[focusIdx].size() < 32) { fields[focusIdx].push_back(ch); statusMsg.clear(); render(); } continue; } } } int timeoutMs = -1; if (visible && statusUntilMs > 0) { int64_t rem = statusUntilMs - nowMs(); timeoutMs = static_cast(std::max(50, rem)); } struct pollfd pfds[2] {}; pfds[0].fd = xfd; pfds[0].events = POLLIN; pfds[1].fd = fifoFd; pfds[1].events = POLLIN; int pr = ::poll(pfds, (fifoFd >= 0) ? 2 : 1, timeoutMs); if (pr == 0 && visible && statusUntilMs > 0 && nowMs() >= statusUntilMs) { render(); } } closeWindow(); if (fifoFd >= 0) { ::close(fifoFd); } if (daemonLockFd >= 0) { ::close(daemonLockFd); } ::unlink(getPluginFifoPath("passcode").c_str()); ::unlink(daemonPidPath.c_str()); return 0; } int passcode_plugin_init(const DwmHostApi* host) { if (host && host->spawn_plugin) { host->spawn_plugin("passcode", "--preload"); } return 0; } } // namespace extern "C" __attribute__((visibility("default"))) const DwmPlugin dwm_plugin_entry = { DWM_PLUGIN_API_VERSION, "passcode", "Bananarchy numeric user passcode and duress passcode configurator", passcode_plugin_init, nullptr, nullptr, nullptr, nullptr, passcode_run_main };