Skip to content

Commit

Permalink
Merge pull request #26 from javalsai/env-fixes
Browse files Browse the repository at this point in the history
feat: add behavior.{source,user_source} for custom env
  • Loading branch information
javalsai authored Dec 25, 2024
2 parents 1e0ffcd + 2c283a1 commit 4fb2bb2
Show file tree
Hide file tree
Showing 27 changed files with 170 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .codespellrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[codespell]
skip = ./assets/pkg/aur/*/src
skip = ./assets/pkg/aur/*/src,./assets/pkg/aur/*/*/objects
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,9 @@ The name is just ly but changing "y" with "i", that had a reason but forgot it,
* cerealexperiments\_, found a missing newline (had the guts to read the source code, crazy ik)
* ChatGPT, in times of slow laptops where pages take ages to load, a single tab connected to a bunch of burning cloud GPUs feeding corporate hype is all you need to get quick answers for your questions, as long as you know how to filter AI crap ofc.
* [My lack of gf](https://www.instagram.com/reel/C8sa3Gltmtq), can't imagine this project being possible if somebody actually cared about me daily.

---

🌟 Finally, consider starring this repo or... 🔪

![star-history](https://api.star-history.com/svg?repos=javalsai/lidm&type=Date)
6 changes: 6 additions & 0 deletions assets/man/lidm-config.5
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ Text to display as the header for such sessions.
.TP
\fBbehavior.include_defshell\fP
"true"/"false" (invalid input defaults to false), if true, includes the user default shell as a session to launch
.TP
\fBbehavior.source\fP
Specify paths to source on login, simple KEY=VALUE format with comments (#) or empty'ish lines, quoting or escape sequences not supported yet. It is NOT an array, but you rather assign to it multiple times.
.TP
\fBbehavior.user_source\fP
Same as \fIbehavior.source\fP but relative to user home (if present).


.SH "SEE ALSO"
Expand Down
5 changes: 3 additions & 2 deletions include/auth.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

#include <stdbool.h>

#include <sessions.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
7 changes: 4 additions & 3 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
#include <stdbool.h>
#include <stdio.h>

#include <keys.h>
#include <sessions.h>
#include <users.h>
#include "keys.h"
#include "util.h"

// should be ansi escape codes under \x1b[...m
// if not prepared accordingly, it might break
Expand Down Expand Up @@ -63,6 +62,8 @@ struct strings {

struct behavior {
bool include_defshell;
struct Vector source;
struct Vector user_source;
};

struct config {
Expand Down
2 changes: 1 addition & 1 deletion include/sessions.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <sys/types.h>

#include <util.h>
#include "util.h"

enum session_type {
XORG,
Expand Down
4 changes: 2 additions & 2 deletions include/ui.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef _UIH_
#define _UIH_

#include <config.h>
#include <util.h>
#include "config.h"
#include "util.h"

void setup(struct config);
int load(struct Vector * users, struct Vector * sessions);
Expand Down
2 changes: 1 addition & 1 deletion include/users.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <sys/types.h>

#include <util.h>
#include "util.h"

struct user {
char *shell;
Expand Down
6 changes: 4 additions & 2 deletions include/util.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#ifndef _UTILH_
#define _UTILH_

#include <keys.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/types.h>

#include "keys.h"

enum keys find_keyname(char *);
enum keys find_ansi(char *);
void read_press(u_char *, char *);
Expand All @@ -24,6 +26,6 @@ void vec_free(struct Vector*);
void vec_clear(struct Vector*);
void vec_reset(struct Vector*);
void* vec_pop(struct Vector*); // won't free it, nor shrink vec list space
void* vec_get(struct Vector*, uint32_t index);
void* vec_get(struct Vector*, size_t index);

#endif
86 changes: 75 additions & 11 deletions src/auth.c
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
#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>

#include <auth.h>
#include <sessions.h>
#include <ui.h>
#include <unistd.h>
#include <util.h>
#include "auth.h"
#include "config.h"
#include "sessions.h"
#include "ui.h"
#include "unistd.h"
#include "util.h"

int pam_conversation(int num_msg, const struct pam_message **msg,
struct pam_response **resp, void *appdata_ptr) {
struct pam_response *reply =
(struct pam_response *)malloc(sizeof(struct pam_response) * num_msg);
for (int i = 0; i < num_msg; i++) {
for (size_t i = 0; i < num_msg; i++) {
reply[i].resp = NULL;
reply[i].resp_retcode = 0;
if (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF ||
Expand Down Expand Up @@ -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 (size_t 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");

Expand All @@ -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 (size_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 (size_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; // can't bother
strcpy(newbuf, pw->pw_dir);
newbuf[home_len] = '/'; // assume pw_dir doesn't 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);*/
Expand All @@ -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");
Expand Down Expand Up @@ -129,7 +190,7 @@ bool launch(char *user, char *passwd, struct session session,
print_errno("pam_getenvlist");
_exit(EXIT_FAILURE);
}
for (uint i = 0; envlist[i] != NULL; i++) {
for (size_t i = 0; envlist[i] != NULL; i++) {
putenv(envlist[i]);
}
// FIXME: path hotfix
Expand All @@ -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
Expand Down Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions src/chvt.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <stdio.h>
#include <stdlib.h>

#include <chvt.h>
#include "chvt.h"

static char *vterms[] = {"/dev/tty", "/dev/tty0", "/dev/vc/0", "/dev/systty",
"/dev/console"};
Expand All @@ -26,7 +26,7 @@ int chvt_str(char *str) {
int chvt(int n) {
fprintf(stderr, "activating vt %d\n", n);
char c = 0;
for (int i = 0; i < sizeof(vterms) / sizeof(vterms[0]); i++) {
for (size_t i = 0; i < sizeof(vterms) / sizeof(vterms[0]); i++) {
int fd = open(vterms[i], O_RDWR);
if (fd >= 0 && isatty(fd) && ioctl(fd, KDGKBTYPE, &c) == 0 && c < 3) {
if (ioctl(fd, VT_ACTIVATE, n) < 0 || ioctl(fd, VT_WAITACTIVE, n) < 0) {
Expand Down
11 changes: 9 additions & 2 deletions src/config.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "util.h"
#include <string.h>
#include <sys/stat.h>

#include <config.h>
#include "config.h"
#include "util.h"

bool line_parser(FILE *fd, ssize_t *blksize,
u_char (*cb)(char *key, char *value)) {
Expand Down Expand Up @@ -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;

Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/efield.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <string.h>

#include <efield.h>
#include <ui.h>
#include "efield.h"
#include "ui.h"

struct editable_field field_new(char *content) {
struct editable_field __efield;
Expand Down
12 changes: 6 additions & 6 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
#include <sys/types.h>
#include <unistd.h>

#include <chvt.h>
#include <config.h>
#include <sessions.h>
#include <ui.h>
#include <users.h>
#include <util.h>
#include "chvt.h"
#include "config.h"
#include "sessions.h"
#include "ui.h"
#include "users.h"
#include "util.h"

int main(int argc, char *argv[]) {
if (argc == 2)
Expand Down
6 changes: 3 additions & 3 deletions src/sessions.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#include <sys/stat.h>
#include <sys/types.h>

#include <sessions.h>
#include <util.h>
#include "sessions.h"
#include "util.h"

struct source_dir {
enum session_type type;
Expand Down Expand Up @@ -110,7 +110,7 @@ struct Vector get_avaliable_sessions() {
struct Vector sessions = vec_new();

cb_sessions = &sessions;
for (uint i = 0; i < (sizeof(sources) / sizeof(sources[0])); i++) {
for (size_t i = 0; i < (sizeof(sources) / sizeof(sources[0])); i++) {
/*printf("recurring into %s\n", sources[i].dir);*/
session_type = sources[i].type;
ftw(sources[i].dir, &fn, 1);
Expand Down
Loading

0 comments on commit 4fb2bb2

Please sign in to comment.