spain's gonna win btw 🇪🇸

This commit is contained in:
javalsai 2024-07-14 20:21:17 +02:00
parent fbc272f2a1
commit f9bfc48a9f
9 changed files with 211 additions and 33 deletions

View File

@ -4,14 +4,14 @@ IDIR=include
ODIR=dist ODIR=dist
CC=gcc CC=gcc
CFLAGS=-I$(IDIR) CFLAGS=-O3 -I$(IDIR)
LIBS=-lm LIBS=-lm -lpam -lpam_misc
_DEPS = util.h ui.h efield.h keys.h users.h sessions.h _DEPS = util.h ui.h auth.h efield.h keys.h users.h sessions.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS)) DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
_OBJ = main.o util.o ui.o efield.o users.o sessions.o _OBJ = main.o util.o ui.o auth.o efield.o users.o sessions.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ)) OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
$(ODIR)/%.o: $(CDIR)/%.c $(DEPS) $(ODIR)/%.o: $(CDIR)/%.c $(DEPS)

View File

@ -4,6 +4,8 @@ Lidm is a really light display manager made in C, highly customizable and held t
![demo image](assets/lidm.png) ![demo image](assets/lidm.png)
> *this is shown as in a terminal emulator, actual linux console doesn't support as much color and decorations* > *this is shown as in a terminal emulator, actual linux console doesn't support as much color and decorations*
> *I'm open if anybody wants to contact me to record a proper demo of the program, my laptop can't handle it and idk how to config obs for hyprland*
## Features ## Features
* Builds fast af * Builds fast af
* Works everywhere you can get gcc to compile * Works everywhere you can get gcc to compile
@ -15,7 +17,7 @@ Lidm is a really light display manager made in C, highly customizable and held t
* Save last selection * Save last selection
* Show/hide passwd switch * Show/hide passwd switch
* Config parsing, it's fully customizable, but everything hardcoded for now :) * Config parsing, it's fully customizable, but everything hardcoded for now :)
* Long sessions, strings, usernames, passwords... they will just overflow or f*ck your terminal, I know it and I don't know if I'll fix it. * Long sessions, strings, usernames, passwords... they will just overflow or f\*ck your terminal, I know it and I don't know if I'll fix it.
## Forget it ## Forget it
* Any kind of arguments * Any kind of arguments

View File

@ -70,5 +70,17 @@
"-Iinclude" "-Iinclude"
], ],
"file": "src/efield.c" "file": "src/efield.c"
},
{
"directory": "/home/javalsai/coding/lidm",
"arguments": [
"gcc",
"-c",
"-o",
"dist/auth.o",
"src/auth.c",
"-Iinclude"
],
"file": "src/auth.c"
} }
] ]

11
include/auth.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef _AUTHH_
#define _AUTHH_
#include <stdbool.h>
#include <sessions.h>
bool check_passwd(char *user, char *passwd);
bool launch(char *user, char *passwd, struct session session, void (*cb)(void));
#endif

View File

@ -11,7 +11,8 @@ enum session_type {
struct session { struct session {
char *name; char *name;
char *path; char *exec;
char *tryexec;
enum session_type type; enum session_type type;
}; };

113
src/auth.c Normal file
View File

@ -0,0 +1,113 @@
#include <security/pam_appl.h>
#include <security/pam_misc.h>
#include <sys/wait.h>
#include <auth.h>
#include <stdlib.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++) {
reply[i].resp = NULL;
reply[i].resp_retcode = 0;
if (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF ||
msg[i]->msg_style == PAM_PROMPT_ECHO_ON) {
char *input = (char *)appdata_ptr;
reply[i].resp = strdup(input);
}
}
*resp = reply;
return PAM_SUCCESS;
}
bool check_passwd(char *user, char *passwd) {
pam_handle_t *pamh = NULL;
struct pam_conv pamc = {pam_conversation, (void *)passwd};
int retval;
retval = pam_start("login", user, &pamc, &pamh);
if (retval != PAM_SUCCESS) {
return false;
}
retval = pam_authenticate(pamh, 0);
if (retval != PAM_SUCCESS) {
pam_end(pamh, retval);
return false;
}
retval = pam_acct_mgmt(pamh, 0);
if (retval != PAM_SUCCESS) {
pam_end(pamh, retval);
return false;
}
pam_end(pamh, PAM_SUCCESS);
return true;
}
/*void run(char *user, char *passwd, char *type, char *command) {*/
/* int pipefd[2];*/
/* pid_t pid;*/
/**/
/* if (pipe(pipefd) == -1) {*/
/* perror("pipe");*/
/* exit(EXIT_FAILURE);*/
/* }*/
/**/
/* if ((pid = fork()) == -1) {*/
/* perror("fork");*/
/* exit(EXIT_FAILURE);*/
/* }*/
/**/
/* if (pid == 0) {*/
/* close(pipefd[0]);*/
/* write(pipefd[1], passwd, strlen(passwd));*/
/* close(pipefd[1]);*/
/* exit(EXIT_SUCCESS);*/
/* } else {*/
/* close(pipefd[1]);*/
/**/
/* if (dup2(pipefd[0], STDIN_FILENO) == -1) {*/
/* perror("dup2");*/
/* exit(EXIT_FAILURE);*/
/* }*/
/* close(pipefd[0]);*/
/**/
/* execlp("su", "-", user, type, command, (char *)NULL);*/
/* perror("execlp");*/
/* exit(EXIT_FAILURE);*/
/* }*/
/*}*/
void run(char *user, char *passwd, char *type, char *command) {
int pipefd[2];
pipe(pipefd);
close(STDIN_FILENO);
dup2(pipefd[0], STDIN_FILENO);
write(pipefd[1], passwd, strlen(passwd));
write(pipefd[1], "\n", 1);
char *const args[] = { "-", user, type, command, NULL };
execvp("su", args);
exit(1);
}
bool launch(char *user, char *passwd, struct session session, void (*cb)(void)) {
if (!check_passwd(user, passwd))
return false;
if (cb != NULL)
cb();
if (session.type == SHELL) {
system("clear");
run(user, passwd, "-s", session.exec);
} else if (session.type == XORG || session.type == WAYLAND) {
system("clear");
run(user, passwd, "-c", session.exec);
}
return true;
}

View File

@ -38,7 +38,6 @@ static const struct config default_config() {
__theme.colors = __colors; __theme.colors = __colors;
__theme.chars = __chars; __theme.chars = __chars;
struct functions __functions; struct functions __functions;
__functions.poweroff = F1; __functions.poweroff = F1;
__functions.reboot = F2; __functions.reboot = F2;
@ -66,6 +65,8 @@ static const struct config default_config() {
return __config; return __config;
} }
#include <stdio.h>
#include <auth.h>
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
setup(default_config()); setup(default_config());
@ -73,5 +74,6 @@ int main(int argc, char* argv[]) {
struct sessions_list *sessions = get_avaliable_sessions(); struct sessions_list *sessions = get_avaliable_sessions();
int ret = load(users, sessions); int ret = load(users, sessions);
if(ret == 0) execl(argv[0], argv[0], NULL); if (ret == 0)
execl(argv[0], argv[0], NULL);
} }

View File

@ -3,6 +3,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
@ -19,11 +20,13 @@ static const struct source_dir sources[] = {
}; };
static const size_t sources_size = sizeof(sources) / sizeof(sources[0]); static const size_t sources_size = sizeof(sources) / sizeof(sources[0]);
static struct session __new_session(enum session_type type, char *name, const char *path) { static struct session __new_session(enum session_type type, char *name,
const char *exec, const char *tryexec) {
struct session __session; struct session __session;
__session.type = type; __session.type = type;
strcln(&__session.name, name); strcln(&__session.name, name);
strcln(&__session.path, path); strcln(&__session.exec, exec);
strcln(&__session.tryexec, tryexec);
return __session; return __session;
} }
@ -53,35 +56,63 @@ static int fn(const char *fpath, const struct stat *sb, int typeflag) {
return 0; return 0;
} }
bool found = false; u_char found = 0;
size_t alloc_size = sb->st_blksize; size_t alloc_size = sb->st_blksize;
char *buf = malloc(sb->st_blksize);
while (true) {
buf = realloc(buf, sb->st_blksize);
ssize_t read_size = getline(&buf, &alloc_size, fd);
if (read_size == -1)
break;
uint read; char *name_buf = NULL;
if ((read = sscanf(buf, "Name=%s\n", buf)) != 0) { char *exec_buf = NULL;
found = true; char *tryexec_buf = NULL;
buf = realloc(buf, read); while (true) {
char *buf = malloc(sb->st_blksize);
ssize_t read_size = getline(&buf, &alloc_size, fd);
if (read_size == -1) {
free(buf);
break; break;
} }
uint read;
char *key = malloc(read_size);
if ((read = sscanf(buf, "%[^=]=%[^\n]\n", key, buf)) != 0) {
if (strcmp(key, "Name") == 0) {
found &= 0b001;
name_buf = realloc(buf, read);
} else if (strcmp(key, "Exec") == 0) {
found &= 0b010;
exec_buf = realloc(buf, read);
} else if (strcmp(key, "TryExec") == 0) {
found &= 0b100;
tryexec_buf = realloc(buf, read);
} else
free(buf);
} else
free(buf);
free(key);
if (found == 0b111)
break;
} }
// just add this to the list // just add this to the list
if (found) { if (name_buf != NULL && exec_buf != NULL) {
if (used_size >= alloc_size) { if (used_size >= alloc_size) {
alloc_size += bs; alloc_size += bs;
sessions = realloc(sessions, alloc_size * unit_size); sessions = realloc(sessions, alloc_size * unit_size);
} }
sessions[used_size] = __new_session(session_type, buf, fpath); /*printf("n %s\ne %s\nte %s\n", name_buf, exec_buf, tryexec_buf);*/
sessions[used_size] = __new_session(session_type, name_buf, exec_buf,
tryexec_buf == NULL ? "" : tryexec_buf);
used_size++; used_size++;
} }
free(buf); if (name_buf != NULL)
free(name_buf);
if (exec_buf != NULL)
free(exec_buf);
if (tryexec_buf != NULL)
free(tryexec_buf);
return 0; return 0;
} }

View File

@ -13,6 +13,7 @@
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include <auth.h>
#include <efield.h> #include <efield.h>
#include <keys.h> #include <keys.h>
#include <sessions.h> #include <sessions.h>
@ -231,14 +232,14 @@ struct session get_current_session() {
of_session.current_opt == gsessions->length + 1) { of_session.current_opt == gsessions->length + 1) {
struct session shell_session; struct session shell_session;
shell_session.type = SHELL; shell_session.type = SHELL;
shell_session.path = shell_session.name = get_current_user().shell; shell_session.exec = shell_session.name = get_current_user().shell;
return shell_session; return shell_session;
} else } else
return gsessions->sessions[of_session.current_opt - 1]; return gsessions->sessions[of_session.current_opt - 1];
} else { } else {
struct session custom_session; struct session custom_session;
custom_session.type = SHELL; custom_session.type = SHELL;
custom_session.name = custom_session.path = of_session.efield.content; custom_session.name = custom_session.exec = of_session.efield.content;
return custom_session; return custom_session;
} }
} }
@ -311,7 +312,7 @@ void ffield_type(char *text) {
start = get_current_user().username; start = get_current_user().username;
if (focused_input == SESSION && of_session.current_opt != 0 && if (focused_input == SESSION && of_session.current_opt != 0 &&
get_current_session().type == SHELL) get_current_session().type == SHELL)
start = get_current_session().path; start = get_current_session().exec;
ofield_type(field, text, start); ofield_type(field, text, start);
print_ffield(); print_ffield();
@ -390,7 +391,12 @@ int load(struct users_list *users, struct sessions_list *sessions) {
} }
} else { } else {
if (len == 1 && *seq == '\n') { if (len == 1 && *seq == '\n') {
printf("\x1b[HTODO: submit creds\n"); if (!launch(get_current_user().username, of_passwd.efield.content,
get_current_session(), &restore_all)) {
print_passwd(box_start(), of_passwd.efield.length, true);
ffield_cursor_focus();
continue;
}
} }
ffield_type(seq); ffield_type(seq);
} }