summaryrefslogtreecommitdiff
path: root/usr/src/lib/libc/port/gen/getusershell.c
blob: df10f2f6fd091d42f6c8e9cf46de63704e374df9 (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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/

/*
 * Copyright (c) 1985 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

#include "lint.h"
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <ctype.h>
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <sys/file.h>
#include "libc.h"
#include <unistd.h>

#define	SHELLS "/etc/shells"

/*
 * Do not add local shells here.  They should be added in /etc/shells
 *
 * Do not add restricted shells:
 * Shells returned by getusershell traditionally allow:
 * - users to change away from (i.e., if you have an rksh in
 *   getusershell(), then users can change their shell to ksh)
 * - by default, ftp in is allowed only for shells returned by
 *   getusershell(); since FTP has no restrictions on directory
 *   movement, adding rksh to getusershell() would defeat that
 *   protection.
 */
const char *okshells[] = {
	"/usr/bin/sh",
	"/usr/bin/csh",
	"/usr/bin/ksh",
	"/usr/bin/ksh93",
	"/usr/bin/jsh",
	"/bin/sh",
	"/bin/csh",
	"/bin/ksh",
	"/bin/ksh93",
	"/bin/jsh",
	"/sbin/sh",
	"/sbin/jsh",
	"/usr/bin/pfsh",
	"/usr/bin/pfcsh",
	"/usr/bin/pfksh",
	"/usr/bin/pfksh93",
	"/usr/bin/bash",
	"/usr/bin/tcsh",
	"/usr/bin/zsh",
	"/usr/bin/pfbash",
	"/usr/bin/pftcsh",
	"/usr/bin/pfzsh",
	"/bin/pfsh",
	"/bin/pfcsh",
	"/bin/pfksh",
	"/bin/pfksh93",
	"/bin/bash",
	"/bin/tcsh",
	"/bin/zsh",
	"/bin/pfbash",
	"/bin/pftcsh",
	"/bin/pfzsh",
	"/usr/xpg4/bin/sh",
	"/usr/xpg4/bin/pfsh",
	"/sbin/pfsh",
	"/usr/sfw/bin/zsh",
	NULL
};

static char **shells, *strings;
static char **curshell;
static char **initshells(void);

/*
 * Get a list of shells from SHELLS, if it exists.
 */
char *
getusershell(void)
{
	char *ret;

	if (curshell == NULL)
		curshell = initshells();
	ret = *curshell;
	if (ret != NULL)
		curshell++;
	return (ret);
}

void
endusershell(void)
{

	if (shells != NULL)
		(void) free((char *)shells);
	shells = NULL;
	if (strings != NULL)
		(void) free(strings);
	strings = NULL;
	curshell = NULL;
}

void
setusershell(void)
{

	curshell = initshells();
}

static char **
initshells(void)
{
	char **sp, *cp;
	FILE *fp;
	struct stat statb;

	if (shells != NULL)
		(void) free((char *)shells);
	shells = NULL;
	if (strings != NULL)
		(void) free(strings);
	strings = NULL;
	if ((fp = fopen(SHELLS, "rF")) == (FILE *)0)
		return ((char **)okshells);
	/*
	 * The +1 in the malloc() below is needed to handle the final
	 * fgets() NULL terminator.  From fgets(3S):
	 *
	 * char *fgets(char *s, int n, FILE *stream);
	 *
	 * The  fgets()  function reads characters from the stream into
	 * the array pointed to by s, until n-1 characters are read, or
	 * a newline character is read and transferred to s, or an end-
	 * of-file condition is encountered.  The string is then termi-
	 * nated with a null character.
	 */
	if ((fstat(fileno(fp), &statb) == -1) || (statb.st_size > LONG_MAX) ||
	    ((strings = malloc((size_t)statb.st_size + 1)) == NULL)) {
		(void) fclose(fp);
		return ((char **)okshells);
	}
	shells = calloc((size_t)statb.st_size / 3, sizeof (char *));
	if (shells == NULL) {
		(void) fclose(fp);
		(void) free(strings);
		strings = NULL;
		return ((char **)okshells);
	}
	sp = shells;
	cp = strings;
	while (fgets(cp, MAXPATHLEN + 1, fp) != NULL) {
		while (*cp != '#' && *cp != '/' && *cp != '\0')
			cp++;
		if (*cp == '#' || *cp == '\0')
			continue;
		*sp++ = cp;
		while (!isspace(*cp) && *cp != '#' && *cp != '\0')
			cp++;
		*cp++ = '\0';
	}
	*sp = (char *)0;
	(void) fclose(fp);
	return (shells);
}