Skip to content

Commit

Permalink
fix[close #320]: Newly created users are required to create a new user
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkobrombin authored and taukakao committed Jul 30, 2024
1 parent 5d8a17b commit 970f53c
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 28 deletions.
44 changes: 28 additions & 16 deletions __first_setup_reset_session
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,37 @@ fi

echo '[daemon]' > /etc/gdm3/daemon.conf


export highest_uid=$(grep -E "^UID_MIN" /etc/login.defs | awk '{$1=$1};1' | cut -d$' ' -f2)
export REAL_USER=""

# Gets the latest added user from /etc/passwd
# It gets the minimum UID for users using /etc/login.defs
# and loops over all the entries in /etc/passwd
# checking if the uid of the selected user is higher than the minimum uid
# if it is, then this uid gets set as the new minimum uid
# and it gets repeated until the last line of passwd is processed
# NOTE: This assumes that new users always have a higher uid than the previously added user and that no system user has a higher UID than the users
# it generally is a safe assumption to make, but should still be noted in case something goes wrong.
while read entry; do
uid=$(echo "$entry" | awk 'BEGIN {FS=":"}; {print $3}')
name=$(echo "$entry" | awk 'BEGIN {FS=":"}; {print $1}')
if [[ $((uid)) -gt $((highest_uid)) && $name != "nobody" ]]; then
export highest_uid=$uid
export REAL_USER=$name
# Check if the file /etc/vos-user-1000-busy exists
if [ -f /etc/vos-user-1000-busy ]; then
# File exists, find the user with the highest UID
while read entry; do
uid=$(echo "$entry" | awk 'BEGIN {FS=":"}; {print $3}')
name=$(echo "$entry" | awk 'BEGIN {FS=":"}; {print $1}')
if [[ $((uid)) -gt $((highest_uid)) && $name != "nobody" ]]; then
export highest_uid=$uid
export REAL_USER=$name
fi
done < <(getent passwd)
else
# File does not exist, check if UID 1000 exists
if id -u 1000 >/dev/null 2>&1; then
# UID 1000 exists, create the /etc/vos-user-1000-busy file and set REAL_USER to the user with UID 1000
touch /etc/vos-user-1000-busy
REAL_USER=$(getent passwd 1000 | cut -d: -f1)
else
# UID 1000 does not exist, find the user with the highest UID
while read entry; do
uid=$(echo "$entry" | awk 'BEGIN {FS=":"}; {print $3}')
name=$(echo "$entry" | awk 'BEGIN {FS=":"}; {print $1}')
if [[ $((uid)) -gt $((highest_uid)) && $name != "nobody" ]]; then
export highest_uid=$uid
export REAL_USER=$name
fi
done < <(getent passwd)
fi
done < <(getent passwd)
fi

echo -e '[User]\nSession=gnome' > /var/lib/AccountsService/users/$REAL_USER
33 changes: 30 additions & 3 deletions vanilla_first_setup/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.


import getpass
import gi

gi.require_version("Gtk", "4.0")
Expand Down Expand Up @@ -56,8 +57,24 @@ def __init__(self):

# disable the lock screen and password for the default user
if self.user == "vanilla":
subprocess.run(["/usr/bin/gsettings", "set", "org.gnome.desktop.lockdown", "disable-lock-screen", "true"])
subprocess.run(["/usr/bin/gsettings", "set", "org.gnome.desktop.screensaver", "lock-enabled", "false"])
subprocess.run(
[
"/usr/bin/gsettings",
"set",
"org.gnome.desktop.lockdown",
"disable-lock-screen",
"true",
]
)
subprocess.run(
[
"/usr/bin/gsettings",
"set",
"org.gnome.desktop.screensaver",
"lock-enabled",
"false",
]
)

def __register_arguments(self):
"""Register the command line arguments."""
Expand Down Expand Up @@ -87,10 +104,20 @@ def do_command_line(self, command_line):
self.post_script = options.lookup_value("run-post-script").get_string()

if options.contains("new-user"):
logger.info("Running as a new user")
self.user = None
self.new_user = True

# FIXME: this is a workaround to avoid running as a new user when the user is not vanilla
# this should simply never happen. Anyway we are already working on a new backend for the
# first setup, so this is just a temporary fix
if getpass.getuser() != "vanilla":
self.new_user = False
logger.warning(
"Asked to run as a new user, but the current user is not vanilla, meaning a new user was already created, turning off the new user mode"
)
else:
logger.info("Running as a new user")

self.activate()

def do_activate(self):
Expand Down
8 changes: 3 additions & 5 deletions vanilla_first_setup/utils/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ def __load(self):
output.decode("utf-8") == ""
or output.decode("utf-8") == "1"
):
logger.info(
"Step {key} skipped due to display-conditions"
)
logger.info("Step {key} skipped due to display-conditions")
break
except subprocess.CalledProcessError:
logger.info(f"Step {key} skipped due to display-conditions")
Expand All @@ -109,8 +107,8 @@ def __load(self):
if not _condition_met:
continue

if step.get("new-user-only") and not self.__new_user:
continue
if step.get("new-user-only") and not self.__new_user:
continue

_status = not step.get("is-advanced", False)
_protected = step.get("protected", False)
Expand Down
13 changes: 10 additions & 3 deletions vanilla_first_setup/utils/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import os
import shutil
import subprocess
import getpass
import logging


Expand Down Expand Up @@ -54,11 +55,14 @@ def get_setup_commands(log_path, pre_run, post_run, commands):
command = command.replace("!noRoot", "")
command = command.replace('"', '\\"')
command = command.replace("'", "\\'")
command = "systemd-run --user --machine=\"[email protected]\" -P -q /usr/bin/bash -c \"%s\"" % command
command = (
'systemd-run --user --machine="[email protected]" -P -q /usr/bin/bash -c "%s"'
% command
)

next_boot.append(command)

subprocess.run([pkexec_bin, "/usr/bin/vanilla-first-setup-prepare-files"])
subprocess.run([pkexec_bin, "/usr/bin/vanilla-first-setup-prepare-files", getpass.getuser()]) # type: ignore

# generating the commannds and writing them to a file to run them all at once
with open(commands_script_path, "w") as f:
Expand Down Expand Up @@ -113,7 +117,10 @@ def get_setup_commands(log_path, pre_run, post_run, commands):
command = command.replace("!noRoot", "")
command = command.replace('"', '\\"')
command = command.replace("'", "\\'")
command = "systemd-run --user --machine=\"[email protected]\" -P -q /usr/bin/bash -c \"%s\"" % command
command = (
'systemd-run --user --machine="[email protected]" -P -q /usr/bin/bash -c "%s"'
% command
)

# outRun bang is used to run a command outside of the main
# shell script.
Expand Down
4 changes: 3 additions & 1 deletion vanilla_first_setup/vanilla-first-setup-prepare-files.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/bin/bash
_user=$1

touch /etc/org.vanillaos.FirstSetup.commands
touch /etc/org.vanillaos.FirstSetup.nextBoot

chmod +wx /etc/org.vanillaos.FirstSetup.commands
chown vanilla:vanilla /etc/org.vanillaos.FirstSetup.commands
chown $_user:$_user /etc/org.vanillaos.FirstSetup.commands
chmod +wx /etc/org.vanillaos.FirstSetup.nextBoot
5 changes: 5 additions & 0 deletions vanilla_first_setup/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from gi.repository import Gtk, GObject, Adw

import contextlib
import getpass

from vanilla_first_setup.utils.builder import Builder
from vanilla_first_setup.utils.parser import Parser
Expand Down Expand Up @@ -56,6 +57,10 @@ def __init__(self, post_script: str, user: str, new_user: bool = False, **kwargs
# True/False = managed result
self.__last_result = None

self.__user = user
if user is None:
self.__user = getpass.getuser()

# if a post_script is provided, we are in the post setup
# so we can skip the builder and just run the post script
# in the Vte terminal
Expand Down

0 comments on commit 970f53c

Please sign in to comment.