Skip to content

Instantly share code, notes, and snippets.

@EBNull
Created April 15, 2024 03:03
Show Gist options
  • Save EBNull/0b84b8aedc730aa45cc50e86b715d5cb to your computer and use it in GitHub Desktop.
Save EBNull/0b84b8aedc730aa45cc50e86b715d5cb to your computer and use it in GitHub Desktop.
# This is a systemd user service
#
[Unit]
Description=Start krfb
After=graphical-session.target
[Service]
Type=exec
ExecStart=krfb --display $DISPLAY --nodialog
Restart=always
RestartSec=3
[Install]
WantedBy=graphical-session.target
#!/usr/bin/python
#
# krfbpass.py - set the unattended password for krfb
#
# Usage:
# krfbpass.py newpassword -w
#
import os
import sys
import codecs
import configparser
import tempfile
import shutil
import argparse
# KStringHandler::obscure - https://api.kde.org/frameworks/kcoreaddons/html/kstringhandler_8cpp_source.html#l00164
# InvitationsRfbServer::saveSecuritySettings - https://github.com/KDE/krfb/blob/76ab4450456a94f774dd4fdc52af75942cd996ad/krfb/invitationsrfbserver.cpp#L242-L247
def obscure(x):
r = ""
for c in x:
r += chr(0x1001F - ord(c))
return r
def printsec(e):
return """allowUnattendedAccess=true
desktopPassword=%s
unattendedPassword=%s
"""%(e, e)
def linetoken(state, line):
if state is None:
state = {
"section":"",
"seenSecurity": False,
}
if line.startswith("["):
state["section"] = line.strip().strip("[]").lower()
if state["section"] == "security":
state["seenSecurity"] = True
return (state, "section", state["section"])
return (state, "entry", line.strip())
def printpass(fn, e):
ret = ""
state = None
wrotePass=False
with open(fn, "rt", encoding="utf-8") as f:
for line in f.readlines():
state, toktype, data = linetoken(state, line)
if toktype == "section" and data != "security" and state["seenSecurity"]:
# Exiting security section early
if not wrotePass:
wrotePass = true
ret += printsec(e)
if toktype == "entry" and state["section"] == "security":
# Filter away things we're changing
key, _, _ = data.partition("=")
if key.lower() in ["desktoppassword", "unattendedpassword", "allowunattendedaccess"]:
continue
# Keep this line
ret += line
if not wrotePass:
if state["section"] != "security":
ret += "\n[Security]\n"
ret += printsec(e)
return ret
def main(argv):
p = argparse.ArgumentParser(description='Modify krfb passwords for unattended access.')
p.add_argument('pwd', type=str, help='password to set')
g = p.add_mutually_exclusive_group()
g.add_argument('-p', '--print', dest='action', action='store_const', const='print', help='print the modified file')
g.add_argument('-w', '--write', dest='action', action='store_const', const='write', help='write the modified configuration')
args = p.parse_args(args=argv[1:])
fn = os.path.expanduser("~/.config/krfbrc")
e = obscure(args.pwd)
if not args.action:
print(e)
return 0
if args.action == "print":
print(printpass(fn, e), end='')
return 0
if args.action == "write":
new = printpass(fn, e)
with open(fn, "wt", encoding="utf-8") as f:
f.write(new)
return 0
@EBNull
Copy link
Author

EBNull commented Apr 15, 2024

$ curl -O https://gist.githubusercontent.com/EBNull/0b84b8aedc730aa45cc50e86b715d5cb/raw/6cc778d23557240e095909a736c39e86858d89fb/krfbpass.py
$ python ./krfbpass.py foo -w
$ sudo mv krfb.service /etc/systemd/user/krfb.service
$ systemctl --user enable krfb
$ systemctl --user start krfb
$ systemctl --user status krfb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment