From 1a57451434b9941ea5497dba9017b715826608fd Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 18 Nov 2013 22:28:51 +0100 Subject: m MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/login.c | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 src/login.c (limited to 'src/login.c') diff --git a/src/login.c b/src/login.c new file mode 100644 index 0000000..5a97fb6 --- /dev/null +++ b/src/login.c @@ -0,0 +1,116 @@ +/** + * cerberus – Minimal login program + * + * Copyright © 2013 Mattias Andrée (maandree@member.fsf.org) + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include +#include +#include + +#include "config.h" + +#include "login.h" + + +/** + * The environment variables + */ +extern char** environ; + + +/** + * Change directory to the user's home directory + * + * @param entry The user entry in the password file + */ +void chdir_home(struct passwd* entry) +{ + if (chdir(entry->pw_dir)) + { + perror("chdir"); + if (chdir(DEFAULT_HOME)) + { + perror("chdir"); + sleep(ERROR_SLEEP); + _exit(1); + } + entry->pw_dir = DEFAULT_HOME; + } +} + + +/** + * Make sure the shell to use is definied + * + * @param entry The user entry in the password file + */ +void ensure_shell(struct passwd* entry) +{ + if ((entry->pw_shell && *(entry->pw_shell)) == 0) + entry->pw_shell = DEFAULT_SHELL; +} + + +/** + * Set environment variables + * + * @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) +{ + char* _term = getenv("TERM"); + char* term = NULL; + if (_term) + { + int n = 0, i; + while (*(_term + n++)) + ; + term = malloc(n * sizeof(char)); + if (term == NULL) + { + perror("malloc"); + sleep(ERROR_SLEEP); + _exit(1); + } + 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); + } + *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); +} + -- cgit v1.2.3-70-g09d2