aboutsummaryrefslogtreecommitdiffstats
path: root/src/login.c
diff options
context:
space:
mode:
authorMattias Andrée <m@maandree.se>2026-02-22 13:49:08 +0100
committerMattias Andrée <m@maandree.se>2026-02-22 13:49:08 +0100
commit86138fc92d6e5f92d9d3fcceb32b849e8504f619 (patch)
treef361b9cea99dbe4d77d0dd8f9cbc00f67c105d6b /src/login.c
parentUdpdate for new version of glibc (diff)
downloadcerberus-86138fc92d6e5f92d9d3fcceb32b849e8504f619.tar.gz
cerberus-86138fc92d6e5f92d9d3fcceb32b849e8504f619.tar.bz2
cerberus-86138fc92d6e5f92d9d3fcceb32b849e8504f619.tar.xz
m fixesHEADmaster
Signed-off-by: Mattias Andrée <m@maandree.se>
Diffstat (limited to 'src/login.c')
-rw-r--r--src/login.c229
1 files changed, 109 insertions, 120 deletions
diff --git a/src/login.c b/src/login.c
index e99b77f..3cb0877 100644
--- a/src/login.c
+++ b/src/login.c
@@ -1,7 +1,7 @@
/**
* cerberus – Minimal login program
*
- * Copyright © 2013, 2014, 2015, 2016, 2020 Mattias Andrée (maandree@kth.se)
+ * Copyright © 2013, 2014, 2015, 2016, 2020 Mattias Andrée (m@maandree.se)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -30,7 +30,7 @@
/**
* The environment variables
*/
-extern char** environ;
+extern char **environ;
/**
@@ -38,19 +38,18 @@ extern char** environ;
*
* @param entry The user entry in the password file
*/
-void set_user(struct passwd* entry)
+void
+set_user(struct passwd *entry)
{
- if (setgid(entry->pw_gid) && entry->pw_gid)
- {
- perror("setgid");
- _exit(1);
- }
-
- if (setuid(entry->pw_uid) && entry->pw_uid)
- {
- perror("setuid");
- _exit(1);
- }
+ if (setgid(entry->pw_gid) && entry->pw_gid) {
+ perror("setgid");
+ _exit(1);
+ }
+
+ if (setuid(entry->pw_uid) && entry->pw_uid) {
+ perror("setuid");
+ _exit(1);
+ }
}
@@ -59,19 +58,18 @@ void set_user(struct passwd* entry)
*
* @param entry The user entry in the password file
*/
-void chdir_home(struct passwd* entry)
+void
+chdir_home(struct passwd *entry)
{
- if (chdir(entry->pw_dir))
- {
- perror("chdir");
- if (chdir(DEFAULT_HOME))
- {
- perror("chdir");
- sleep(ERROR_SLEEP);
- _exit(1);
+ if (chdir(entry->pw_dir)) {
+ perror("chdir");
+ if (chdir(DEFAULT_HOME)) {
+ perror("chdir");
+ sleep(ERROR_SLEEP);
+ _exit(1);
+ }
+ entry->pw_dir = strdup(DEFAULT_HOME);
}
- entry->pw_dir = strdup(DEFAULT_HOME);
- }
}
@@ -80,10 +78,11 @@ void chdir_home(struct passwd* entry)
*
* @param entry The user entry in the password file
*/
-void ensure_shell(struct passwd* entry)
+void
+ensure_shell(struct passwd *entry)
{
- if ((entry->pw_shell && *(entry->pw_shell)) == 0)
- entry->pw_shell = strdup(DEFAULT_SHELL);
+ if (!entry->pw_shell || !*entry->pw_shell)
+ entry->pw_shell = strdup(DEFAULT_SHELL);
}
@@ -93,47 +92,43 @@ void ensure_shell(struct passwd* entry)
* @param entry The user entry in the password file
* @param preserve_env Whether to preserve the environment
*/
-void set_environ(struct passwd* entry, char preserve_env)
+void
+set_environ(struct passwd *entry, char preserve_env)
{
- char* _term = getenv("TERM");
- char* term = NULL;
- if (_term)
- {
- size_t n = 0, i;
- while (*(_term + n++))
- ;
- term = malloc(n * sizeof(char));
- if (term == NULL)
- {
- perror("malloc");
- sleep(ERROR_SLEEP);
- _exit(1);
+ char *_term = getenv("TERM");
+ char *term = NULL;
+ if (_term) {
+ size_t n = 0, i;
+ while (_term[n++]) ;
+ term = malloc(n * sizeof(char));
+ if (!term) {
+ perror("malloc");
+ sleep(ERROR_SLEEP);
+ _exit(1);
+ }
+ for (i = 0; i < n; i++)
+ *(term + i) = *(_term + i);
}
- for (i = 0; i < n; i++)
- *(term + i) = *(_term + i);
- }
-
- if (preserve_env == 0)
- {
- environ = malloc(sizeof(char*));
- if (environ == NULL)
- {
- perror("malloc");
- sleep(ERROR_SLEEP);
- _exit(1);
+
+ if (!preserve_env) {
+ environ = malloc(sizeof(char*));
+ if (!environ) {
+ perror("malloc");
+ sleep(ERROR_SLEEP);
+ _exit(1);
+ }
+ *environ = NULL;
}
- *environ = NULL;
- }
-
- setenv("HOME", entry->pw_dir, 1);
- setenv("USER", entry->pw_name, 1);
- setenv("LOGUSER", entry->pw_name, 1);
- setenv("SHELL", entry->pw_shell, 1);
- setenv("TERM", term ?: DEFAULT_TERM, 1);
- setenv("PATH", entry->pw_uid ? PATH : PATH_ROOT, 1);
-
- if (term)
- free(term);
+
+ setenv("HOME", entry->pw_dir, 1);
+ setenv("USER", entry->pw_name, 1);
+ setenv("LOGUSER", entry->pw_name, 1);
+ setenv("SHELL", entry->pw_shell, 1);
+ setenv("TERM", term ?: DEFAULT_TERM, 1);
+ setenv("PATH", entry->pw_uid ? PATH : PATH_ROOT, 1);
+
+ if (term)
+ free(term);
}
@@ -142,59 +137,53 @@ void set_environ(struct passwd* entry, char preserve_env)
*
* @param entry The user entry in the password file
*/
-void exec_shell(struct passwd* entry)
+void
+exec_shell(struct passwd *entry)
{
- int child_argc = 0;
- char** child_argv = malloc(5 * sizeof(char*));
- size_t n = 0;
- char* sh = entry->pw_shell;
- char* login_sh;
-
- while (*(sh + n++))
- ;
-
- login_sh = malloc((n + (strchr(sh, ' ') ? 5 : 1)) * sizeof(char));
- if (login_sh == NULL)
- {
- perror("malloc");
- sleep(ERROR_SLEEP);
- _exit(1);
- }
-
- if (strchr(sh, ' '))
- {
- login_sh += 5;
- strcpy(login_sh, "exec ");
- *(login_sh + n) = 0;
-
- *(child_argv + child_argc++) = strdup(DEFAULT_SHELL);
- *(child_argv + child_argc++) = strdup("-" DEFAULT_SH);
- *(child_argv + child_argc++) = strdup("-c");
- *(child_argv + child_argc++) = login_sh - 5;
- }
- else
- {
- ssize_t i;
- for (i = (ssize_t)n - 1; i >= 0; i--)
- if (*(sh + i) == '/')
- {
- i++;
- break;
- }
-
- login_sh = malloc((n + 1) * sizeof(char));
- *login_sh++ = '-';
- strcpy(login_sh, sh + i);
- *(login_sh + n) = 0;
-
- *(child_argv + child_argc++) = sh;
- *(child_argv + child_argc++) = login_sh - 1;
- }
-
- *(child_argv + child_argc) = NULL;
- execvp(*child_argv, child_argv + 1);
- perror("execvp");
- sleep(ERROR_SLEEP);
- _exit(1);
-}
+ int child_argc = 0;
+ char **child_argv = malloc(5 * sizeof(char *));
+ size_t n = 0;
+ char *sh = entry->pw_shell;
+ char *login_sh;
+
+ while (sh[n++]) ;
+ login_sh = malloc((n + (strchr(sh, ' ') ? 5 : 1)) * sizeof(char));
+ if (!login_sh) {
+ perror("malloc");
+ sleep(ERROR_SLEEP);
+ _exit(1);
+ }
+
+ if (strchr(sh, ' ')) {
+ login_sh += 5;
+ strcpy(login_sh, "exec ");
+ login_sh[n] = 0;
+
+ child_argv[child_argc++] = strdup(DEFAULT_SHELL);
+ child_argv[child_argc++] = strdup("-" DEFAULT_SH);
+ child_argv[child_argc++] = strdup("-c");
+ child_argv[child_argc++] = login_sh - 5;
+ } else {
+ ssize_t i;
+ for (i = (ssize_t)n - 1; i >= 0; i--)
+ if (sh[i] == '/') {
+ i++;
+ break;
+ }
+
+ login_sh = malloc((n + 1U) * sizeof(char));
+ *login_sh++ = '-';
+ strcpy(login_sh, &sh[i]);
+ login_sh[n] = 0;
+
+ child_argv[child_argc++] = sh;
+ child_argv[child_argc++] = &login_sh[-1];
+ }
+
+ child_argv[child_argc] = NULL;
+ execvp(child_argv[0], &child_argv[1]);
+ perror("execvp");
+ sleep(ERROR_SLEEP);
+ _exit(1);
+}