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
|
/*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <sys/types.h>
#include <pwd.h>
#include <stdlib.h>
#include <paths.h>
#include <unistd.h>
#include <sys/syslog.h>
#if defined (HAVE_LIBECONF) && defined (USE_VENDORDIR)
#include <libeconf.h>
#endif
#include "closestream.h"
#include "shells.h"
#if defined (HAVE_LIBECONF) && defined (USE_VENDORDIR)
static econf_file *open_etc_shells(void)
{
econf_err error;
econf_file *key_file = NULL;
error = econf_readDirs(&key_file,
_PATH_VENDORDIR,
"/etc",
"shells",
NULL,
"", /* key only */
"#" /* comment */);
if (error) {
syslog(LOG_ALERT,
_("Cannot parse shells files: %s"),
econf_errString(error));
return NULL;
}
return key_file;
}
#endif
/*
* print_shells () -- /etc/shells is outputted to stdout.
*/
extern void print_shells(FILE *out, const char *format)
{
#if defined (HAVE_LIBECONF) && defined (USE_VENDORDIR)
size_t size = 0;
econf_err error;
char **keys = NULL;
econf_file *key_file = open_etc_shells();
if (!key_file)
return;
error = econf_getKeys(key_file, NULL, &size, &keys);
if (error) {
econf_free(key_file);
errx(EXIT_FAILURE,
_("Cannot evaluate entries in shells files: %s"),
econf_errString(error));
}
for (size_t i = 0; i < size; i++) {
fprintf(out, format, keys[i]);
}
econf_free(keys);
econf_free(key_file);
#else
char *s;
setusershell();
while ((s = getusershell()))
fprintf(out, format, s);
endusershell();
#endif
}
/*
* is_known_shell() -- if the given shell appears in /etc/shells
* or vendor defined files.
* Return 1 if found and return 0 if not found.
*/
extern int is_known_shell(const char *shell_name)
{
int ret = 0;
if (!shell_name)
return 0;
#if defined (HAVE_LIBECONF) && defined (USE_VENDORDIR)
char *val = NULL;
econf_err error;
econf_file *key_file = open_etc_shells();
if (!key_file)
return 0;
error = econf_getStringValue (key_file, NULL, shell_name, &val);
if (error) {
if (error != ECONF_NOKEY)
syslog(LOG_ALERT,
_("Cannot evaluate entries in shells files: %s"),
econf_errString(error));
} else
ret = 1;
free(val);
econf_free(key_file);
#else
char *s;
setusershell();
while ((s = getusershell())) {
if (*s != '#' && strcmp(shell_name, s) == 0) {
ret = 1;
break;
}
}
endusershell();
#endif
return ret;
}
const char *ul_default_shell(int flags, const struct passwd *pw)
{
const char *shell = NULL;
if (!(flags & UL_SHELL_NOENV)) {
shell = getenv("SHELL");
if (shell && *shell)
return shell;
}
if (!(flags & UL_SHELL_NOPWD)) {
if (!pw)
pw = getpwuid(getuid());
if (pw)
shell = pw->pw_shell;
if (shell && *shell)
return shell;
}
return _PATH_BSHELL;
}
|