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