feat: add behavior.{source,user_source} for custom env

This commit is contained in:
javalsai 2024-12-25 19:25:09 +01:00
parent 1e0ffcdf2f
commit 0ba5519937
Signed by: javalsai
SSH Key Fingerprint: SHA256:3G83yKhBUWVABVX/vPWH88xnK4+ptMtHkZGCRXD4Mk8
5 changed files with 82 additions and 6 deletions

View File

@ -3,8 +3,9 @@
#include <stdbool.h>
#include <config.h>
#include <sessions.h>
bool launch(char *user, char *passwd, struct session session, void (*cb)(void));
bool launch(char *user, char *passwd, struct session session, void (*cb)(void), struct behavior* behavior);
#endif

View File

@ -1,6 +1,7 @@
#ifndef _CONFIGH_
#define _CONFIGH_
#include "util.h"
#include <stdbool.h>
#include <stdio.h>
@ -63,6 +64,8 @@ struct strings {
struct behavior {
bool include_defshell;
struct Vector source;
struct Vector user_source;
};
struct config {

View File

@ -1,8 +1,11 @@
#include "config.h"
#include <grp.h>
#include <pwd.h>
#include <security/pam_misc.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/wait.h>
@ -59,7 +62,40 @@ void *shmalloc(size_t size) {
-1, 0);
}
void moarEnv(char *user, struct session session, struct passwd *pw) {
void sourceFileTry(char *file) {
FILE *file2source = fopen(file, "r");
if (file2source == NULL)
return;
char *line = NULL;
size_t len = 0;
ssize_t read;
while ((read = getline(&line, &len, file2source)) != -1) {
if (read == 0 || (read > 0 && *line == '#'))
continue;
if (line[read - 1] == '\n')
line[read - 1] = '\0';
/* printf("Retrieved line of length %zu:\n", read); */
/* printf("%s\n", line); */
for (uint i = 1; i < read; i++) {
if (line[i] == '=') {
/* printf("FOUND '='!\n"); */
line[i] = '\0';
setenv(line, &line[i + 1], 1);
break;
}
}
}
if (line)
free(line);
fclose(file2source);
}
void moarEnv(char *user, struct session session, struct passwd *pw,
struct behavior *behavior) {
if (chdir(pw->pw_dir) == -1)
print_errno("can't chdir to user home");
@ -81,6 +117,31 @@ void moarEnv(char *user, struct session session, struct passwd *pw) {
xdg_session_type = "wayland";
setenv("XDG_SESSION_TYPE", xdg_session_type, true);
printf("\n\n\n\n\x1b[1m");
for (uint32_t i = 0; i < behavior->source.length; i++) {
/* printf("DEBUG(source)!!!! %d %s\n", i, (char*)vec_get(&behavior->source,
* i)); */
sourceFileTry((char *)vec_get(&behavior->source, i));
}
/* printf("\n"); */
if (pw->pw_dir) {
uint home_len = strlen(pw->pw_dir);
for (uint32_t i = 0; i < behavior->user_source.length; i++) {
char *file2sourcepath = (char *)vec_get(&behavior->user_source, i);
char *newbuf =
malloc(home_len + strlen(file2sourcepath) + 2); // nullbyte and slash
if (newbuf == NULL)
continue; // cant bother
strcpy(newbuf, pw->pw_dir);
newbuf[home_len] = '/'; // assume pw_dir doesnt start with '/' :P
strcpy(&newbuf[home_len + 1], file2sourcepath);
/* printf("DEBUG(user_source)!!!! %d %s\n", i, newbuf); */
sourceFileTry(newbuf);
free(newbuf);
}
}
/*char *buf;*/
/*size_t bsize = snprintf(NULL, 0, "/run/user/%d", pw->pw_uid) + 1;*/
/*buf = malloc(bsize);*/
@ -92,8 +153,8 @@ void moarEnv(char *user, struct session session, struct passwd *pw) {
/*setenv("XDG_SEAT", "seat0", true);*/
}
bool launch(char *user, char *passwd, struct session session,
void (*cb)(void)) {
bool launch(char *user, char *passwd, struct session session, void (*cb)(void),
struct behavior *behavior) {
struct passwd *pw = getpwnam(user);
if (pw == NULL) {
print_err("could not get user info");
@ -140,7 +201,7 @@ bool launch(char *user, char *passwd, struct session session,
}
free(envlist);
moarEnv(user, session, pw);
moarEnv(user, session, pw, behavior);
// TODO: chown stdin to user
// does it inherit stdin from parent and
@ -168,11 +229,14 @@ bool launch(char *user, char *passwd, struct session session,
// TODO: these will be different due to TryExec
// and, Exec/TryExec might contain spaces as args
printf("\x1b[0m");
if (session.type == SHELL) {
clear_screen();
fflush(stdout);
execlp(session.exec, session.exec, NULL);
} else if (session.type == XORG || session.type == WAYLAND) {
clear_screen();
fflush(stdout);
execlp(session.exec, session.exec, NULL);
}
perror("execl error");

View File

@ -109,6 +109,10 @@ u_char config_line_handler(char *k, char *v) {
__config->strings.s_shell = v;
else if (strcmp(k, "behavior.include_defshell") == 0)
__config->behavior.include_defshell = strcmp(v, "true") == 0;
else if (strcmp(k, "behavior.source") == 0)
vec_push(&__config->behavior.source, v);
else if (strcmp(k, "behavior.user_source") == 0)
vec_push(&__config->behavior.user_source, v);
else
return 0b1111;
@ -124,6 +128,9 @@ struct config *parse_config(char *path) {
}
__config = malloc(sizeof(struct config));
__config->behavior.source = vec_new();
__config->behavior.user_source = vec_new();
if (__config == NULL)
return NULL;
bool ret = line_parser(fd, (ssize_t *)&sb.st_blksize, config_line_handler);

View File

@ -10,6 +10,7 @@
#include <string.h>
#include <sys/ioctl.h>
#include <sys/reboot.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <termios.h>
#include <time.h>
@ -394,7 +395,7 @@ int load(struct Vector *users, struct Vector *sessions) {
} else {
if (len == 1 && *seq == '\n') {
if (!launch(get_current_user().username, of_passwd.efield.content,
get_current_session(), &restore_all)) {
get_current_session(), &restore_all, &behavior)) {
print_passwd(box_start(), of_passwd.efield.length, true);
ffield_cursor_focus();
}