]> git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/shells.c
Merge branch 'libmount/mount_setattr' of https://github.com/t-8ch/util-linux
[thirdparty/util-linux.git] / lib / shells.c
1 /*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 */
4 #include <sys/syslog.h>
5 #if defined (HAVE_LIBECONF) && defined (USE_VENDORDIR)
6 #include <libeconf.h>
7 #endif
8
9 #include "closestream.h"
10 #include "shells.h"
11
12 /*
13 * is_known_shell() -- if the given shell appears in /etc/shells
14 * or vendor defined files.
15 * Return 1 if found and return 0 if not found.
16 */
17 extern int is_known_shell(const char *shell_name)
18 {
19 int ret = 0;
20
21 #if defined (HAVE_LIBECONF) && defined (USE_VENDORDIR)
22 size_t size = 0;
23 econf_err error;
24 char **keys = NULL;
25 econf_file *key_file = NULL;
26
27 error = econf_readDirs(&key_file,
28 _PATH_VENDORDIR,
29 "/etc",
30 "shells",
31 NULL,
32 "", /* key only */
33 "#" /* comment */);
34 if (error) {
35 syslog(LOG_ALERT,
36 _("Cannot parse shells files: %s"),
37 econf_errString(error));
38 exit(EXIT_FAILURE);
39 }
40
41 error = econf_getKeys(key_file, NULL, &size, &keys);
42 if (error) {
43 syslog(LOG_ALERT,
44 _("Cannot evaluate entries in shells files: %s"),
45 econf_errString(error));
46 econf_free(key_file);
47 exit(EXIT_FAILURE);
48 }
49
50 for (size_t i = 0; i < size; i++) {
51 if (strcmp(keys[i], shell_name) == 0) {
52 ret = 1;
53 break;
54 }
55 }
56 econf_free(keys);
57 econf_free(key_file);
58 #else
59 char *s;
60
61 if (!shell_name)
62 return 0;
63
64 setusershell();
65 while ((s = getusershell())) {
66 if (*s != '#' && strcmp(shell_name, s) == 0) {
67 ret = 1;
68 break;
69 }
70 }
71 endusershell();
72 #endif
73 return ret;
74 }
75