xref: /openbsd-src/lib/libc/gen/getusershell.c (revision cb39b41371628601fbe4c618205356d538b9d08a)
1 /*	$OpenBSD: getusershell.c,v 1.15 2015/02/06 23:21:58 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 <sys/stat.h>
32 
33 #include <ctype.h>
34 #include <limits.h>
35 #include <paths.h>
36 #include <stdint.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 
41 /*
42  * Local shells should NOT be added here.  They should be added in
43  * /etc/shells.
44  */
45 
46 static char *okshells[] = { _PATH_BSHELL, _PATH_CSHELL, _PATH_KSHELL, NULL };
47 static char **curshell, **shells, *strings;
48 static char **initshells(void);
49 
50 /*
51  * Get a list of shells from _PATH_SHELLS, if it exists.
52  */
53 char *
54 getusershell(void)
55 {
56 	char *ret;
57 
58 	if (curshell == NULL)
59 		curshell = initshells();
60 	ret = *curshell;
61 	if (ret != NULL)
62 		curshell++;
63 	return (ret);
64 }
65 
66 void
67 endusershell(void)
68 {
69 
70 	if (shells != NULL)
71 		free(shells);
72 	shells = NULL;
73 	if (strings != NULL)
74 		free(strings);
75 	strings = NULL;
76 	curshell = NULL;
77 }
78 
79 void
80 setusershell(void)
81 {
82 
83 	curshell = initshells();
84 }
85 
86 static char **
87 initshells(void)
88 {
89 	char **sp, *cp;
90 	FILE *fp;
91 	struct stat statb;
92 
93 	if (shells != NULL)
94 		free(shells);
95 	shells = NULL;
96 	if (strings != NULL)
97 		free(strings);
98 	strings = NULL;
99 	if ((fp = fopen(_PATH_SHELLS, "re")) == NULL)
100 		return (okshells);
101 	if (fstat(fileno(fp), &statb) == -1) {
102 		(void)fclose(fp);
103 		return (okshells);
104 	}
105 	if (statb.st_size > SIZE_MAX) {
106 		(void)fclose(fp);
107 		return (okshells);
108 	}
109 	if ((strings = malloc((size_t)statb.st_size)) == NULL) {
110 		(void)fclose(fp);
111 		return (okshells);
112 	}
113 	shells = calloc((size_t)(statb.st_size / 3 + 2), sizeof (char *));
114 	if (shells == NULL) {
115 		(void)fclose(fp);
116 		free(strings);
117 		strings = NULL;
118 		return (okshells);
119 	}
120 	sp = shells;
121 	cp = strings;
122 	while (fgets(cp, PATH_MAX + 1, fp) != NULL) {
123 		while (*cp != '#' && *cp != '/' && *cp != '\0')
124 			cp++;
125 		if (*cp == '#' || *cp == '\0')
126 			continue;
127 		*sp++ = cp;
128 		while (!isspace((unsigned char)*cp) && *cp != '#' && *cp != '\0')
129 			cp++;
130 		*cp++ = '\0';
131 	}
132 	*sp = NULL;
133 	(void)fclose(fp);
134 	return (shells);
135 }
136