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