Skip to content

Commit

Permalink
Probably fix relogins for real
Browse files Browse the repository at this point in the history
  • Loading branch information
zunda-arrow committed Dec 26, 2023
1 parent a7954b3 commit 0eced02
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 46 deletions.
19 changes: 10 additions & 9 deletions aporia.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ func main() {

termState, _ := term.GetState(int(os.Stdin.Fd()))

for {
configObj, err := config.LoadConfig()
if err != nil {
config_ := config.DefaultConfig()
configObj = &config_
}
ui, _ := tui.New(*configObj, *termState)
ui.SetAsciiArt(configObj.GetAscii())
ui.Start()
configObj, err := config.LoadConfig()
if err != nil {
config_ := config.DefaultConfig()
configObj = &config_
}
ui, _ := tui.New(*configObj, *termState)
ui.SetAsciiArt(configObj.GetAscii())
ui.Start()

// Restart servie to start with a clean slate. This is a bit sus.
os.Exit(0)
}
1 change: 0 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ func (self *Config) GetAscii() AsciiArt {
}
if self.ascii != nil {
for _, file := range self.AsciiArts {
fmt.Println(file.name)
if file.name == *self.ascii {
return file
}
Expand Down
2 changes: 2 additions & 0 deletions extra/aporia.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ After=systemd-user-sessions.service plymouth-quit-wait.service
Conflicts[email protected]

[Service]
Restart=on-success
RestartSec=0.1
Type=idle
ExecStart=/bin/aporia
StandardInput=tty
Expand Down
22 changes: 14 additions & 8 deletions login/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,18 @@ func makeEnv(pam_handle *C.struct_pam_handle, pwnam *C.struct_passwd, desktopNam
envMap[k] = v
}

user := fmt.Sprint("/run/user/", pwnam.pw_uid)

setEnv("HOME", homeDir)
setEnv("PWD", homeDir)
setEnv("SHELL", C.GoString(pwnam.pw_shell))
setEnv("USER", C.GoString(pwnam.pw_name))
setEnv("LOGNAME", C.GoString(pwnam.pw_name))

// DBUS variable must be set for Wayland to work properly.
dbusAddress := fmt.Sprint("unix:path=", user, "/bus")
setEnv("DBUS_SESSION_BUS_ADDRESS", dbusAddress)

termValue, found := os.LookupEnv("TERM")
if found {
setEnv("TERM", termValue)
Expand All @@ -57,15 +63,15 @@ func makeEnv(pam_handle *C.struct_pam_handle, pwnam *C.struct_passwd, desktopNam

setEnv("PATH", "/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin")

// XDG Variables
user := fmt.Sprint("/run/user/", pwnam.pw_uid)
setEnv("XDG_RUNTIME_DIR", user)
setEnv("XDG_SESSION_CLASS", "user")
setEnv("XDG_SESSION_ID", "1")
setEnv("XDG_SESSION_DESKTOP", desktopName)
setEnv("XDG_SEAT", "seat0")
setEnv("XDG_VTNR", "1")
// pam_systemd options
setEnv("XDG_SESSION_TYPE", sessionType)
setEnv("XDG_SESSION_DESKTOP", desktopName)
setEnv("XDG_SESSION_DESKTOP", desktopName)
setEnv("XDG_SESSION_CLASS", "user")
// setEnv("XDG_RUNTIME_DIR", user)
// setEnv("XDG_SESSION_ID", "1")
// setEnv("XDG_SEAT", "seat0")
// setEnv("XDG_VTNR", "1")

os.Chown(user, int(pwnam.pw_uid), int(pwnam.pw_gid))

Expand Down
15 changes: 10 additions & 5 deletions login/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ package login
// #include <utmp.h>
import "C"
import (
"aporia/ansi"
"aporia/config"
"aporia/constants"
"os"
"fmt"
"syscall"
)

Expand All @@ -18,6 +19,7 @@ func launch(session config.Session, pam_handle *C.struct_pam_handle, pwnam *C.st

if pid == 0 {
// Child
fmt.Println("Launching " + session.SessionType + "...")
becomeUser(pwnam)
shell := C.GoString(pwnam.pw_shell)
env := makeEnv(pam_handle, pwnam, session.Name, string(session.SessionType))
Expand All @@ -29,6 +31,8 @@ func launch(session config.Session, pam_handle *C.struct_pam_handle, pwnam *C.st
launchX11(env, shell, session.Exec, session.Filepath)
case config.WaylandSession:
launchWayland(env, shell, session.Exec, session.Filepath)
default:
fmt.Println("Invalid session type. This should never happen.")
}
}

Expand All @@ -40,13 +44,16 @@ func launch(session config.Session, pam_handle *C.struct_pam_handle, pwnam *C.st
var status C.int
for C.waitpid(-1, &status, 0) > 0 {}

closePamSession(pam_handle)
fmt.Println("Child process has closed, beginning cleanup...")

removeUtmpEntry(&utmpEntry)
closePamSession(pam_handle)
}

func launchShell(env []string, shell string) {
// We don't want logs showing up on shell login.
ansi.Clear()
syscall.Exec(shell, []string{shell}, env)
os.Exit(0)
}

func launchX11(env []string, shell string, exec *string, filepath *string) {
Expand All @@ -56,7 +63,6 @@ func launchX11(env []string, shell string, exec *string, filepath *string) {
env = append(env, constants.AporiaExec+"="+*exec)
}
syscall.Exec(shell, []string{shell, "-c", constants.X11StartupCommand}, env)
os.Exit(0)
}

func launchWayland(env []string, shell string, exec *string, filepath *string) {
Expand All @@ -65,5 +71,4 @@ func launchWayland(env []string, shell string, exec *string, filepath *string) {
} else {
syscall.Exec(shell, []string{shell, "-c", *exec}, env)
}
os.Exit(0)
}
28 changes: 14 additions & 14 deletions login/pam.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ package login
import "C"
import (
"aporia/ansi"
"aporia/constants"
"aporia/config"
"aporia/constants"
"errors"
"fmt"
"unsafe"
Expand All @@ -29,6 +29,7 @@ func Authenticate(username string, password string, session config.Session) erro
defer C.free(unsafe.Pointer(usernameStr))
defer C.free(unsafe.Pointer(serviceStr))
defer C.free(unsafe.Pointer(passwordStr))
defer C.free(unsafe.Pointer(handle))

{
ret := C.pam_start(serviceStr, usernameStr, &conv, &handle)
Expand All @@ -48,7 +49,7 @@ func Authenticate(username string, password string, session config.Session) erro
{
ret := C.pam_acct_mgmt(handle, 0)
if ret != C.PAM_SUCCESS {
return errors.New("pam_acct_mgmt")
return errors.New("Account is not valid.")
}
}

Expand All @@ -57,36 +58,35 @@ func Authenticate(username string, password string, session config.Session) erro

// Child shell must be cleared here
ansi.Clear()

fmt.Println("Setting credentials...")
{
ret := C.pam_setcred(handle, C.PAM_ESTABLISH_CRED)
if ret != C.PAM_SUCCESS {
return errors.New("pam_setcred")
}
}

fmt.Println("Opening session...")
// This is where the bug is happening.
{
ret := C.pam_open_session(handle, 0)
// Silenced to hide the distro's login message
ret := C.pam_open_session(handle, 1)
if ret != C.PAM_SUCCESS {
fmt.Println("There was an error opening the session." + pamReason(ret))
C.pam_setcred(handle, C.PAM_DELETE_CRED)
return errors.New("pam_open_session " + pamReason(ret))
}
fmt.Println("Session opened successfully.")
}

C.set_pam_env(handle)

// DBUS variables need to be set for wayland to work properly
loc := C.CString(fmt.Sprint("unix:path=/run/user/", pwnam.pw_uid, "/bus"))
runtimeDir := C.CString(fmt.Sprint("/run/user/", pwnam.pw_uid))

C.pam_misc_setenv(handle, C.CString("XDG_RUNTIME_DIR"), runtimeDir, 0)
C.pam_misc_setenv(handle, C.CString("DBUS_SESSION_BUS_ADDRESS"), loc, 0)

// Login was successful, so lets save the choices for next time.
fmt.Println("Saving last session...")
config.SaveSession(session.Name, username)

fmt.Println("Launching session...")
launch(session, handle, pwnam)

fmt.Println("Session closed, restarting Aporia")
return nil
}

Expand Down Expand Up @@ -128,7 +128,7 @@ func pamReason(err C.int) string {
}

func closePamSession(handle *C.struct_pam_handle) {
result := C.pam_setcred(handle, C.PAM_DELETE_CRED)
C.pam_close_session(handle, 0)
result := C.pam_setcred(handle, C.PAM_DELETE_CRED)
C.pam_end(handle, result)
}
7 changes: 0 additions & 7 deletions login/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,3 @@ char* index_string_array(char** arr, int index) {
printf("%s", arr[index]);
return arr[index];
}

// Set pam env for logind support
void set_pam_env(pam_handle_t *handle) {
pam_misc_setenv(handle, "XDG_SESSION_CLASS", "greeter", 0);
pam_misc_setenv(handle, "XDG_SEAT", "seat0", 0);
pam_misc_setenv(handle, "XDG_VTNR", "1", 0);
}
2 changes: 0 additions & 2 deletions tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,6 @@ func (self *Tui) login() {
// Reset the fields when the password is wrong.
self.failedPasswordReset()
self.message = fmt.Sprint(err)
// Only reset the state if the TUI isn't being discarded, this is so
// we can restore to a proper state in our next tui.
_, _ = term.MakeRaw(int(os.Stdin.Fd()))
} else {
self.message = "Success!"
Expand Down

0 comments on commit 0eced02

Please sign in to comment.