usershel.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*++
  2. Copyright (c) 2015 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. usershel.c
  9. Abstract:
  10. This module implements support for the getusershell family of functions.
  11. Author:
  12. Evan Green 9-Mar-2015
  13. Environment:
  14. User Mode C Library
  15. --*/
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. #include "libcp.h"
  20. #include <assert.h>
  21. #include <paths.h>
  22. #include <stdio.h>
  23. #include <unistd.h>
  24. //
  25. // ---------------------------------------------------------------- Definitions
  26. //
  27. #define USER_SHELLS_PATH _PATH_SHELLS
  28. //
  29. // ------------------------------------------------------ Data Type Definitions
  30. //
  31. //
  32. // ----------------------------------------------- Internal Function Prototypes
  33. //
  34. //
  35. // -------------------------------------------------------------------- Globals
  36. //
  37. FILE *ClUserShellsFile = NULL;
  38. char *ClUserShellsLine = NULL;
  39. size_t ClUserShellsLineSize = 0;
  40. UINTN ClUserShellsIndex = 0;
  41. PSTR ClBuiltinUserShells[] = {
  42. "/bin/sh",
  43. "/bin/csh",
  44. NULL
  45. };
  46. //
  47. // ------------------------------------------------------------------ Functions
  48. //
  49. LIBC_API
  50. char *
  51. getusershell (
  52. void
  53. )
  54. /*++
  55. Routine Description:
  56. This routine returns the next permitted user shell in the database of
  57. valid shells. This opens the file if necessary. This routine is neither
  58. thread-safe nor reentrant.
  59. Arguments:
  60. None.
  61. Return Value:
  62. Returns a pointer to a string containing the next shell on success. This
  63. buffer may be overwritten by subsequent calls to getusershell.
  64. NULL on failure or end-of-database.
  65. --*/
  66. {
  67. ssize_t LineSize;
  68. char *Result;
  69. if (ClUserShellsFile == NULL) {
  70. ClUserShellsFile = fopen(USER_SHELLS_PATH, "r");
  71. if (ClUserShellsFile == NULL) {
  72. assert(ClUserShellsIndex <
  73. (sizeof(ClBuiltinUserShells) /
  74. sizeof(ClBuiltinUserShells[0])));
  75. //
  76. // If there is no user shells file, pretend /bin/sh and /bin/csh
  77. // are there.
  78. //
  79. Result = ClBuiltinUserShells[ClUserShellsIndex];
  80. if (Result != NULL) {
  81. ClUserShellsIndex += 1;
  82. }
  83. return Result;
  84. }
  85. }
  86. //
  87. // Loop trying to get a valid line.
  88. //
  89. while (TRUE) {
  90. LineSize = getline(&ClUserShellsLine,
  91. &ClUserShellsLineSize,
  92. ClUserShellsFile);
  93. if (LineSize < 0) {
  94. //
  95. // If the file was unreadable, return the builtin shells.
  96. //
  97. if (ferror(ClUserShellsFile) != 0) {
  98. assert(ClUserShellsIndex <
  99. (sizeof(ClBuiltinUserShells) /
  100. sizeof(ClBuiltinUserShells[0])));
  101. Result = ClBuiltinUserShells[ClUserShellsIndex];
  102. if (Result != NULL) {
  103. ClUserShellsIndex += 1;
  104. }
  105. return Result;
  106. }
  107. return NULL;
  108. }
  109. while ((LineSize != 0) && (isspace(ClUserShellsLine[LineSize - 1]))) {
  110. LineSize -= 1;
  111. }
  112. Result = ClUserShellsLine;
  113. Result[LineSize] = '\0';
  114. //
  115. // Get past whitespace.
  116. //
  117. while (isspace(*Result)) {
  118. Result += 1;
  119. }
  120. //
  121. // Skip any commented lines.
  122. //
  123. if ((*Result == '\0') || (*Result == '#')) {
  124. continue;
  125. }
  126. break;
  127. }
  128. return Result;
  129. }
  130. LIBC_API
  131. void
  132. setusershell (
  133. void
  134. )
  135. /*++
  136. Routine Description:
  137. This routine rewinds the user shells database back to the beginning.
  138. Arguments:
  139. None.
  140. Return Value:
  141. None.
  142. --*/
  143. {
  144. if (ClUserShellsFile != NULL) {
  145. if (fseek(ClUserShellsFile, 0, SEEK_SET) != 0) {
  146. fclose(ClUserShellsFile);
  147. ClUserShellsFile = NULL;
  148. }
  149. }
  150. ClUserShellsIndex = 0;
  151. return;
  152. }
  153. LIBC_API
  154. void
  155. endusershell (
  156. void
  157. )
  158. /*++
  159. Routine Description:
  160. This routine closes the permitted user shells database.
  161. Arguments:
  162. None.
  163. Return Value:
  164. None.
  165. --*/
  166. {
  167. if (ClUserShellsFile != NULL) {
  168. fclose(ClUserShellsFile);
  169. ClUserShellsFile = NULL;
  170. }
  171. ClUserShellsIndex = 0;
  172. return;
  173. }
  174. //
  175. // --------------------------------------------------------- Internal Functions
  176. //