1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
/*
* SPDX-License-Identifier: ISC
*
* Copyright (c) 2019-2020 Todd C. Miller <Todd.Miller@sudo.ws>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* This is an open source non-commercial project. Dear PVS-Studio, please check it.
* PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sudo_compat.h>
#include <sudo_debug.h>
#include <sudo_util.h>
#ifdef TESTSUDOERS
# include <tsgetgrpw.h>
#endif
static const char *shellfile = "/etc/shell";
static char **allowed_shells, * const *current_shell;
static const char *default_shells[] = {
"/bin/sh",
"/bin/ksh",
"/bin/ksh93",
"/bin/bash",
"/bin/dash",
"/bin/zsh",
"/bin/csh",
"/bin/tcsh",
NULL
};
static char **
read_shells(void)
{
size_t maxshells = 16, nshells = 0;
size_t linesize = 0;
char *line = NULL;
FILE *fp;
debug_decl(read_shells, SUDO_DEBUG_UTIL);
if ((fp = fopen(shellfile, "r")) == NULL)
goto bad;
free(allowed_shells);
allowed_shells = reallocarray(NULL, maxshells, sizeof(char *));
if (allowed_shells == NULL)
goto bad;
while (sudo_parseln(&line, &linesize, NULL, fp, PARSELN_CONT_IGN) != -1) {
if (nshells + 1 >= maxshells) {
char **new_shells;
new_shells = reallocarray(NULL, maxshells + 16, sizeof(char *));
if (new_shells == NULL)
goto bad;
allowed_shells = new_shells;
maxshells += 16;
}
if ((allowed_shells[nshells] = strdup(line)) == NULL)
goto bad;
nshells++;
}
allowed_shells[nshells] = NULL;
free(line);
fclose(fp);
debug_return_ptr(allowed_shells);
bad:
free(line);
if (fp != NULL)
fclose(fp);
while (nshells != 0)
free(allowed_shells[--nshells]);
free(allowed_shells);
allowed_shells = NULL;
debug_return_ptr(default_shells);
}
void
sudo_setusershell(void)
{
debug_decl(setusershell, SUDO_DEBUG_UTIL);
current_shell = read_shells();
debug_return;
}
void
sudo_endusershell(void)
{
debug_decl(endusershell, SUDO_DEBUG_UTIL);
if (allowed_shells != NULL) {
char **shell;
for (shell = allowed_shells; *shell != NULL; shell++)
free(*shell);
free(allowed_shells);
allowed_shells = NULL;
}
current_shell = NULL;
debug_return;
}
char *
sudo_getusershell(void)
{
debug_decl(getusershell, SUDO_DEBUG_UTIL);
if (current_shell == NULL)
current_shell = read_shells();
debug_return_str(*current_shell++);
}
#ifdef TESTSUDOERS
void
testsudoers_setshellfile(const char *file)
{
testsudoers_endusershell();
shellfile = file;
}
#endif /* TESTSUDOERS */
|