| 1 |
/* getusershell.c -- Return names of valid user shells. |
| 2 |
|
| 3 |
Copyright (C) 1991, 1997, 2000-2001, 2003-2006, 2008-2026 Free Software |
| 4 |
Foundation, Inc. |
| 5 |
|
| 6 |
This program is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or |
| 9 |
(at your option) any later version. |
| 10 |
|
| 11 |
This program is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 18 |
|
| 19 |
/* Written by David MacKenzie <[email protected]> */ |
| 20 |
|
| 21 |
#include <config.h> |
| 22 |
|
| 23 |
/* Specification. */ |
| 24 |
#include <unistd.h> |
| 25 |
|
| 26 |
#ifndef SHELLS_FILE |
| 27 |
# ifndef __DJGPP__ |
| 28 |
/* File containing a list of nonrestricted shells, one per line. */ |
| 29 |
# define SHELLS_FILE "/etc/shells" |
| 30 |
# else |
| 31 |
/* This is a horrible kludge. Isn't there a better way? */ |
| 32 |
# define SHELLS_FILE "/dev/env/DJDIR/etc/shells" |
| 33 |
# endif |
| 34 |
#endif |
| 35 |
|
| 36 |
#include <stdio.h> |
| 37 |
#include <stdlib.h> |
| 38 |
#include <string.h> |
| 39 |
#include <ctype.h> |
| 40 |
|
| 41 |
#include "filename.h" |
| 42 |
#include "xalloc.h" |
| 43 |
|
| 44 |
#if GNULIB_FOPEN_SAFER |
| 45 |
# include "stdio--.h" |
| 46 |
#endif |
| 47 |
|
| 48 |
#if ! defined ADDITIONAL_DEFAULT_SHELLS && defined __MSDOS__ |
| 49 |
# define ADDITIONAL_DEFAULT_SHELLS \ |
| 50 |
"c:/dos/command.com", "c:/windows/command.com", "c:/command.com", |
| 51 |
#else |
| 52 |
# define ADDITIONAL_DEFAULT_SHELLS /* empty */ |
| 53 |
#endif |
| 54 |
|
| 55 |
/* List of shells to use if the shells file is missing. */ |
| 56 |
static char const* const default_shells[] = |
| 57 |
{ |
| 58 |
ADDITIONAL_DEFAULT_SHELLS |
| 59 |
"/bin/sh", "/bin/csh", "/usr/bin/sh", "/usr/bin/csh", NULL |
| 60 |
}; |
| 61 |
|
| 62 |
/* Index of the next shell in 'default_shells' to return. |
| 63 |
0 means we are not using 'default_shells'. */ |
| 64 |
static size_t default_index = 0; |
| 65 |
|
| 66 |
/* Input stream from the shells file. */ |
| 67 |
static FILE *shellstream = NULL; |
| 68 |
|
| 69 |
/* Line of input from the shells file. */ |
| 70 |
static char *line = NULL; |
| 71 |
|
| 72 |
/* Number of bytes allocated for 'line'. */ |
| 73 |
static size_t line_size = 0; |
| 74 |
|
| 75 |
/* Return an entry from the shells file, ignoring comment lines. |
| 76 |
If the file doesn't exist, use the list in DEFAULT_SHELLS (above). |
| 77 |
In any case, the returned string is in memory allocated through malloc. |
| 78 |
Return NULL if there are no more entries. */ |
| 79 |
|
| 80 |
char * |
| 81 |
getusershell (void) |
| 82 |
{ |
| 83 |
if (default_index > 0) |
| 84 |
{ |
| 85 |
if (default_shells[default_index]) |
| 86 |
/* Not at the end of the list yet. */ |
| 87 |
return xstrdup (default_shells[default_index++]); |
| 88 |
return NULL; |
| 89 |
} |
| 90 |
|
| 91 |
if (shellstream == NULL) |
| 92 |
{ |
| 93 |
shellstream = fopen (SHELLS_FILE, "r"); |
| 94 |
if (shellstream == NULL) |
| 95 |
{ |
| 96 |
/* No shells file. Use the default list. */ |
| 97 |
default_index = 1; |
| 98 |
return xstrdup (default_shells[0]); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
for (;;) |
| 103 |
{ |
| 104 |
ssize_t nread = getline (&line, &line_size, shellstream); |
| 105 |
|
| 106 |
/* End of file. */ |
| 107 |
if (nread == -1) |
| 108 |
return NULL; |
| 109 |
/* Skip empty lines. */ |
| 110 |
else if (nread > 1) |
| 111 |
{ |
| 112 |
char *start = line; |
| 113 |
char *comment = strchr (start, '#'); |
| 114 |
char *end; |
| 115 |
|
| 116 |
if (comment != NULL) |
| 117 |
{ |
| 118 |
/* Trim the comment mark. */ |
| 119 |
*comment = '\0'; |
| 120 |
end = comment; |
| 121 |
} |
| 122 |
else |
| 123 |
{ |
| 124 |
/* Trim the newline. */ |
| 125 |
end = start + nread; |
| 126 |
if (end[-1] == '\n') |
| 127 |
*--end = '\0'; |
| 128 |
} |
| 129 |
|
| 130 |
/* Skip leading whitespace. */ |
| 131 |
while (start < end && isspace ((unsigned char) start[0])) |
| 132 |
start++; |
| 133 |
/* Trim trailing whitespace. */ |
| 134 |
while (start < end && isspace ((unsigned char) end[-1])) |
| 135 |
*--end = '\0'; |
| 136 |
|
| 137 |
/* Only return absolute file names. */ |
| 138 |
if (start < end && IS_ABSOLUTE_FILE_NAME (start)) |
| 139 |
return start; |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/* Rewind the shells file. */ |
| 145 |
|
| 146 |
void |
| 147 |
setusershell (void) |
| 148 |
{ |
| 149 |
default_index = 0; |
| 150 |
if (shellstream) |
| 151 |
rewind (shellstream); |
| 152 |
} |
| 153 |
|
| 154 |
/* Close the shells file. */ |
| 155 |
|
| 156 |
void |
| 157 |
endusershell (void) |
| 158 |
{ |
| 159 |
if (shellstream) |
| 160 |
{ |
| 161 |
fclose (shellstream); |
| 162 |
shellstream = NULL; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
#ifdef TEST |
| 167 |
int |
| 168 |
main (void) |
| 169 |
{ |
| 170 |
char *s; |
| 171 |
|
| 172 |
while (s = getusershell ()) |
| 173 |
puts (s); |
| 174 |
exit (0); |
| 175 |
} |
| 176 |
#endif |