]> git.ipfire.org Git - thirdparty/util-linux.git/blame - lib/shells.c
chsh: use libeconf to read /etc/shells
[thirdparty/util-linux.git] / lib / shells.c
CommitLineData
faeb1b64
KZ
1/*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 */
825feef8
SS
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
825feef8 12#if defined (HAVE_LIBECONF) && defined (USE_VENDORDIR)
b8ecc2d3
TK
13static econf_file *open_etc_shells(void)
14{
825feef8 15 econf_err error;
f644f330 16 econf_file *key_file = NULL;
825feef8
SS
17
18 error = econf_readDirs(&key_file,
19 _PATH_VENDORDIR,
20 "/etc",
21 "shells",
22 NULL,
23 "", /* key only */
24 "#" /* comment */);
25 if (error) {
26 syslog(LOG_ALERT,
27 _("Cannot parse shells files: %s"),
28 econf_errString(error));
b8ecc2d3 29 return NULL;
825feef8
SS
30 }
31
b8ecc2d3
TK
32 return key_file;
33}
34#endif
825feef8 35
b8ecc2d3
TK
36/*
37 * print_shells () -- /etc/shells is outputted to stdout.
38 */
39extern void print_shells(FILE *out, const char *format)
40{
41#if defined (HAVE_LIBECONF) && defined (USE_VENDORDIR)
42 size_t size = 0;
43 econf_err error;
44 char **keys = NULL;
45 econf_file *key_file = open_etc_shells();
46
47 if (!key_file)
48 return;
49
50 error = econf_getKeys(key_file, NULL, &size, &keys);
51 if (error) {
52 econf_free(key_file);
53 errx(EXIT_FAILURE,
54 _("Cannot evaluate entries in shells files: %s"),
55 econf_errString(error));
56 }
57
58 for (size_t i = 0; i < size; i++) {
59 fprintf(out, format, keys[i]);
60 }
61 econf_free(keys);
62 econf_free(key_file);
825feef8 63#else
b8ecc2d3
TK
64 char *s;
65
66 while ((s = getusershell()))
67 fprintf(out, format, s);
68 endusershell();
69#endif
70}
71
72
73/*
74 * is_known_shell() -- if the given shell appears in /etc/shells
75 * or vendor defined files.
76 * Return 1 if found and return 0 if not found.
77 */
78extern int is_known_shell(const char *shell_name)
79{
80 int ret = 0;
825feef8
SS
81
82 if (!shell_name)
83 return 0;
84
b8ecc2d3
TK
85#if defined (HAVE_LIBECONF) && defined (USE_VENDORDIR)
86 char *val = NULL;
87 econf_err error;
88 econf_file *key_file = open_etc_shells();
89
90 if (!key_file)
91 return 0;
92
93 error = econf_getStringValue (key_file, NULL, shell_name, &val);
94 if (error) {
95 if (error != ECONF_NOKEY)
96 syslog(LOG_ALERT,
97 _("Cannot evaluate entries in shells files: %s"),
98 econf_errString(error));
99 } else
100 ret = 1;
101
102 free(val);
103 econf_free(key_file);
104#else
105 char *s;
106
825feef8
SS
107 setusershell();
108 while ((s = getusershell())) {
5099e004 109 if (*s != '#' && strcmp(shell_name, s) == 0) {
825feef8
SS
110 ret = 1;
111 break;
112 }
113 }
114 endusershell();
115#endif
116 return ret;
117}