1/*	$OpenBSD: getusershell.c,v 1.18 2019/12/10 02:35:16 millert Exp $ */
2/*
3 * Copyright (c) 1985, 1993
4 *	The Regents of the University of California.  All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the University nor the names of its contributors
15 *    may be used to endorse or promote products derived from this software
16 *    without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <paths.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <unistd.h>
36
37/*
38 * Local shells should NOT be added here.  They should be added in
39 * /etc/shells.
40 */
41
42static char *okshells[] = { _PATH_BSHELL, _PATH_CSHELL, _PATH_KSHELL, NULL };
43static char **curshell, **shells;
44static char **initshells(void);
45
46/*
47 * Get a list of shells from _PATH_SHELLS, if it exists.
48 */
49char *
50getusershell(void)
51{
52	char *ret;
53
54	if (curshell == NULL)
55		curshell = initshells();
56	ret = *curshell;
57	if (ret != NULL)
58		curshell++;
59	return (ret);
60}
61
62void
63endusershell(void)
64{
65	char **s;
66
67	if ((s = shells))
68		while (*s)
69			free(*s++);
70	free(shells);
71	shells = NULL;
72
73	curshell = NULL;
74}
75
76void
77setusershell(void)
78{
79
80	curshell = initshells();
81}
82
83static char **
84initshells(void)
85{
86	size_t nshells, nalloc, linesize;
87	char *line;
88	FILE *fp;
89
90	free(shells);
91	shells = NULL;
92
93	if ((fp = fopen(_PATH_SHELLS, "re")) == NULL)
94		return (okshells);
95
96	line = NULL;
97	nalloc = 10; // just an initial guess
98	nshells = 0;
99	shells = reallocarray(NULL, nalloc, sizeof (char *));
100	if (shells == NULL)
101		goto fail;
102	linesize = 0;
103	while (getline(&line, &linesize, fp) != -1) {
104		if (*line != '/')
105			continue;
106		line[strcspn(line, "#\n")] = '\0';
107		if (!(shells[nshells] = strdup(line)))
108			goto fail;
109
110		if (nshells + 1 == nalloc) {
111			char **new = reallocarray(shells, nalloc * 2, sizeof(char *));
112			if (!new)
113				goto fail;
114			shells = new;
115			nalloc *= 2;
116		}
117		nshells++;
118	}
119	free(line);
120	shells[nshells] = NULL;
121	(void)fclose(fp);
122	return (shells);
123
124fail:
125	free(line);
126	while (nshells)
127		free(shells[nshells--]);
128	free(shells);
129	shells = NULL;
130	(void)fclose(fp);
131	return (okshells);
132}
133