fix: use getuser() syscall for determining username rather than session.toml hack

This commit is contained in:
2026-04-01 11:44:13 +02:00
parent 8e63b75fe7
commit 6276f8b162
8 changed files with 40 additions and 67 deletions
+6 -6
View File
@@ -519,11 +519,11 @@ bool config::unset(toml::Doc* doc, const char* key);
### Example
```cpp
auto doc = montauk::config::load("session");
const char* user = doc.get_string("session.username", "guest");
auto doc = montauk::config::load("desktop");
const char* theme = doc.get_string("appearance.theme", "light");
montauk::config::set_string(&doc, "session.theme", "dark");
montauk::config::save("session", &doc);
montauk::config::set_string(&doc, "appearance.theme", "dark");
montauk::config::save("desktop", &doc);
```
---
@@ -543,8 +543,8 @@ bool user::change_password(const char* username, const char* new_password);
void user::set_session(const char* username); // Log in
void user::clear_session(); // Log out
bool user::get_session_username(char* buf, int sz); // Get current user
bool user::get_home_dir(char* buf, int sz); // Get user's home directory
bool user::get_session_username(char* buf, int sz); // Get current process user
bool user::get_home_dir(char* buf, int sz); // Get current process home directory
```
---
+8 -13
View File
@@ -297,10 +297,10 @@ namespace user {
return false;
}
// ---- Session (current logged-in user) ----
// ---- Session state and current user helpers ----
// Write the current session after successful login.
// Stores username in 0:/config/session.toml so any app can query it.
// Persist login session state after successful login.
// Current processes should use getuser() for their active username.
inline void set_session(const char* username) {
montauk::toml::Doc doc;
doc.init();
@@ -314,19 +314,14 @@ namespace user {
montauk::fdelete("0:/config/session.toml");
}
// Read the current username into buf. Returns true if a session exists.
// Read the current process username into buf. Returns true if one is set.
inline bool get_session_username(char* buf, int bufSz) {
auto doc = config::load("session");
const char* name = doc.get_string("session.username", "");
bool found = (name[0] != '\0');
if (found) {
montauk::strncpy(buf, name, bufSz);
}
doc.destroy();
return found;
if (buf == nullptr || bufSz <= 0) return false;
int len = montauk::getuser(buf, (uint64_t)bufSz);
return len > 0 && buf[0] != '\0';
}
// Get the current user's home directory. Returns true if a session exists.
// Get the current process user's home directory. Returns true if a user is set.
inline bool get_home_dir(char* buf, int bufSz) {
char username[32];
if (!get_session_username(username, sizeof(username))) return false;