summaryrefslogtreecommitdiffstats
path: root/lib/shells.c
blob: 6693ab0054160ef2c0d6b6e69ed61ce4583d1b38 (plain)
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
/*
 * SPDX-License-Identifier: GPL-2.0-or-later
 */
#include <sys/syslog.h>
#if defined (HAVE_LIBECONF) && defined (USE_VENDORDIR)
#include <libeconf.h>
#endif

#include "closestream.h"
#include "shells.h"

/*
 *  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 defined (HAVE_LIBECONF) && defined (USE_VENDORDIR)
	size_t size = 0;
	econf_err error;
	char **keys;
	econf_file *key_file;

	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));
		exit(EXIT_FAILURE);
	}

	error = econf_getKeys(key_file, NULL, &size, &keys);
	if (error) {
		syslog(LOG_ALERT,
		       _("Cannot evaluate entries in shells files: %s"),
		       econf_errString(error));
		econf_free (key_file);
		exit(EXIT_FAILURE);
	}

	for (size_t i = 0; i < size; i++) {
		if (strcmp (keys[i], shell_name) == 0) {
			ret = 1;
			break;
		}
	}
	econf_free (key_file);	
#else
	char *s;

	if (!shell_name)
		return 0;

	setusershell();
	while ((s = getusershell())) {
		if (*s != '#' && strcmp(shell_name, s) == 0) {
			ret = 1;
			break;
		}
	}
	endusershell();
#endif
	return ret;
}