1b528cefcSMark Murray /* 2b528cefcSMark Murray * Copyright (c) 1985, 1993 3b528cefcSMark Murray * The Regents of the University of California. All rights reserved. 4b528cefcSMark Murray * 5b528cefcSMark Murray * Redistribution and use in source and binary forms, with or without 6b528cefcSMark Murray * modification, are permitted provided that the following conditions 7b528cefcSMark Murray * are met: 8b528cefcSMark Murray * 1. Redistributions of source code must retain the above copyright 9b528cefcSMark Murray * notice, this list of conditions and the following disclaimer. 10b528cefcSMark Murray * 2. Redistributions in binary form must reproduce the above copyright 11b528cefcSMark Murray * notice, this list of conditions and the following disclaimer in the 12b528cefcSMark Murray * documentation and/or other materials provided with the distribution. 13b528cefcSMark Murray * 3. All advertising materials mentioning features or use of this software 14b528cefcSMark Murray * must display the following acknowledgement: 15b528cefcSMark Murray * This product includes software developed by the University of 16b528cefcSMark Murray * California, Berkeley and its contributors. 17b528cefcSMark Murray * 4. Neither the name of the University nor the names of its contributors 18b528cefcSMark Murray * may be used to endorse or promote products derived from this software 19b528cefcSMark Murray * without specific prior written permission. 20b528cefcSMark Murray * 21b528cefcSMark Murray * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22b528cefcSMark Murray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23b528cefcSMark Murray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24b528cefcSMark Murray * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25b528cefcSMark Murray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26b528cefcSMark Murray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27b528cefcSMark Murray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28b528cefcSMark Murray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29b528cefcSMark Murray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30b528cefcSMark Murray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31b528cefcSMark Murray * SUCH DAMAGE. 32b528cefcSMark Murray */ 33b528cefcSMark Murray 34b528cefcSMark Murray #ifdef HAVE_CONFIG_H 35b528cefcSMark Murray #include <config.h> 36b528cefcSMark Murray #endif 37b528cefcSMark Murray 385e9cd1aeSAssar Westerlund RCSID("$Id: getusershell.c,v 1.10 2000/05/22 09:11:59 joda Exp $"); 39b528cefcSMark Murray 40b528cefcSMark Murray #ifndef HAVE_GETUSERSHELL 41b528cefcSMark Murray 42b528cefcSMark Murray #include <stdio.h> 43b528cefcSMark Murray #include <stdlib.h> 445e9cd1aeSAssar Westerlund #include <string.h> 45b528cefcSMark Murray #ifdef HAVE_PATHS_H 46b528cefcSMark Murray #include <paths.h> 47b528cefcSMark Murray #endif 48b528cefcSMark Murray #ifdef HAVE_SYS_TYPES_H 49b528cefcSMark Murray #include <sys/types.h> 50b528cefcSMark Murray #endif 51b528cefcSMark Murray #ifdef HAVE_SYS_STAT_H 52b528cefcSMark Murray #include <sys/stat.h> 53b528cefcSMark Murray #endif 54b528cefcSMark Murray #ifdef HAVE_SYS_PARAM_H 55b528cefcSMark Murray #include <sys/param.h> 56b528cefcSMark Murray #endif 57b528cefcSMark Murray 585e9cd1aeSAssar Westerlund #ifdef HAVE_USERSEC_H 595e9cd1aeSAssar Westerlund struct aud_rec; 605e9cd1aeSAssar Westerlund #include <usersec.h> 615e9cd1aeSAssar Westerlund #endif 625e9cd1aeSAssar Westerlund #ifdef HAVE_USERCONF_H 635e9cd1aeSAssar Westerlund #include <userconf.h> 645e9cd1aeSAssar Westerlund #endif 655e9cd1aeSAssar Westerlund 66b528cefcSMark Murray #ifndef _PATH_SHELLS 67b528cefcSMark Murray #define _PATH_SHELLS "/etc/shells" 68b528cefcSMark Murray #endif 69b528cefcSMark Murray 70b528cefcSMark Murray #ifndef _PATH_BSHELL 71b528cefcSMark Murray #define _PATH_BSHELL "/bin/sh" 72b528cefcSMark Murray #endif 73b528cefcSMark Murray 74b528cefcSMark Murray #ifndef _PATH_CSHELL 75b528cefcSMark Murray #define _PATH_CSHELL "/bin/csh" 76b528cefcSMark Murray #endif 77b528cefcSMark Murray 78b528cefcSMark Murray /* 79b528cefcSMark Murray * Local shells should NOT be added here. They should be added in 80b528cefcSMark Murray * /etc/shells. 81b528cefcSMark Murray */ 82b528cefcSMark Murray 83b528cefcSMark Murray static char *okshells[] = { _PATH_BSHELL, _PATH_CSHELL, NULL }; 84b528cefcSMark Murray static char **curshell, **shells, *strings; 85b528cefcSMark Murray static char **initshells (void); 86b528cefcSMark Murray 87b528cefcSMark Murray /* 88b528cefcSMark Murray * Get a list of shells from _PATH_SHELLS, if it exists. 89b528cefcSMark Murray */ 90b528cefcSMark Murray char * 91b528cefcSMark Murray getusershell() 92b528cefcSMark Murray { 93b528cefcSMark Murray char *ret; 94b528cefcSMark Murray 95b528cefcSMark Murray if (curshell == NULL) 96b528cefcSMark Murray curshell = initshells(); 97b528cefcSMark Murray ret = *curshell; 98b528cefcSMark Murray if (ret != NULL) 99b528cefcSMark Murray curshell++; 100b528cefcSMark Murray return (ret); 101b528cefcSMark Murray } 102b528cefcSMark Murray 103b528cefcSMark Murray void 104b528cefcSMark Murray endusershell() 105b528cefcSMark Murray { 106b528cefcSMark Murray if (shells != NULL) 107b528cefcSMark Murray free(shells); 108b528cefcSMark Murray shells = NULL; 109b528cefcSMark Murray if (strings != NULL) 110b528cefcSMark Murray free(strings); 111b528cefcSMark Murray strings = NULL; 112b528cefcSMark Murray curshell = NULL; 113b528cefcSMark Murray } 114b528cefcSMark Murray 115b528cefcSMark Murray void 116b528cefcSMark Murray setusershell() 117b528cefcSMark Murray { 118b528cefcSMark Murray curshell = initshells(); 119b528cefcSMark Murray } 120b528cefcSMark Murray 121b528cefcSMark Murray static char ** 122b528cefcSMark Murray initshells() 123b528cefcSMark Murray { 124b528cefcSMark Murray char **sp, *cp; 1255e9cd1aeSAssar Westerlund #ifdef HAVE_GETCONFATTR 1265e9cd1aeSAssar Westerlund char *tmp; 1275e9cd1aeSAssar Westerlund int nsh; 1285e9cd1aeSAssar Westerlund #else 129b528cefcSMark Murray FILE *fp; 1305e9cd1aeSAssar Westerlund #endif 131b528cefcSMark Murray struct stat statb; 132b528cefcSMark Murray 133b528cefcSMark Murray free(shells); 134b528cefcSMark Murray shells = NULL; 135b528cefcSMark Murray free(strings); 136b528cefcSMark Murray strings = NULL; 1375e9cd1aeSAssar Westerlund #ifdef HAVE_GETCONFATTR 1385e9cd1aeSAssar Westerlund if(getconfattr(SC_SYS_LOGIN, SC_SHELLS, &tmp, SEC_LIST) != 0) 1395e9cd1aeSAssar Westerlund return okshells; 1405e9cd1aeSAssar Westerlund 1415e9cd1aeSAssar Westerlund for(cp = tmp, nsh = 0; *cp; cp += strlen(cp) + 1, nsh++); 1425e9cd1aeSAssar Westerlund 1435e9cd1aeSAssar Westerlund shells = calloc(nsh + 1, sizeof(*shells)); 1445e9cd1aeSAssar Westerlund if(shells == NULL) 1455e9cd1aeSAssar Westerlund return okshells; 1465e9cd1aeSAssar Westerlund 1475e9cd1aeSAssar Westerlund strings = malloc(cp - tmp); 1485e9cd1aeSAssar Westerlund if(strings == NULL) { 1495e9cd1aeSAssar Westerlund free(shells); 1505e9cd1aeSAssar Westerlund shells = NULL; 1515e9cd1aeSAssar Westerlund return okshells; 1525e9cd1aeSAssar Westerlund } 1535e9cd1aeSAssar Westerlund memcpy(strings, tmp, cp - tmp); 1545e9cd1aeSAssar Westerlund for(sp = shells, cp = strings; *cp; cp += strlen(cp) + 1, sp++) 1555e9cd1aeSAssar Westerlund *sp = cp; 1565e9cd1aeSAssar Westerlund #else 157b528cefcSMark Murray if ((fp = fopen(_PATH_SHELLS, "r")) == NULL) 158b528cefcSMark Murray return (okshells); 159b528cefcSMark Murray if (fstat(fileno(fp), &statb) == -1) { 160b528cefcSMark Murray fclose(fp); 161b528cefcSMark Murray return (okshells); 162b528cefcSMark Murray } 163b528cefcSMark Murray if ((strings = malloc((u_int)statb.st_size)) == NULL) { 164b528cefcSMark Murray fclose(fp); 165b528cefcSMark Murray return (okshells); 166b528cefcSMark Murray } 167b528cefcSMark Murray shells = calloc((unsigned)statb.st_size / 3, sizeof (char *)); 168b528cefcSMark Murray if (shells == NULL) { 169b528cefcSMark Murray fclose(fp); 170b528cefcSMark Murray free(strings); 171b528cefcSMark Murray strings = NULL; 172b528cefcSMark Murray return (okshells); 173b528cefcSMark Murray } 174b528cefcSMark Murray sp = shells; 175b528cefcSMark Murray cp = strings; 176b528cefcSMark Murray while (fgets(cp, MaxPathLen + 1, fp) != NULL) { 177b528cefcSMark Murray while (*cp != '#' && *cp != '/' && *cp != '\0') 178b528cefcSMark Murray cp++; 179b528cefcSMark Murray if (*cp == '#' || *cp == '\0') 180b528cefcSMark Murray continue; 181b528cefcSMark Murray *sp++ = cp; 182b528cefcSMark Murray while (!isspace(*cp) && *cp != '#' && *cp != '\0') 183b528cefcSMark Murray cp++; 184b528cefcSMark Murray *cp++ = '\0'; 185b528cefcSMark Murray } 186b528cefcSMark Murray fclose(fp); 1875e9cd1aeSAssar Westerlund #endif 1885e9cd1aeSAssar Westerlund *sp = NULL; 189b528cefcSMark Murray return (shells); 190b528cefcSMark Murray } 191b528cefcSMark Murray #endif /* HAVE_GETUSERSHELL */ 192