pwd.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*++
  2. Copyright (c) 2013 Minoca Corp.
  3. This file is licensed under the terms of the GNU Lesser General Public
  4. License version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details.
  6. Module Name:
  7. pwd.h
  8. Abstract:
  9. This header contains definitions for working with the current user.
  10. Author:
  11. Evan Green 23-Jun-2013
  12. --*/
  13. #ifndef _PWD_H
  14. #define _PWD_H
  15. //
  16. // ------------------------------------------------------------------- Includes
  17. //
  18. #include <stdio.h>
  19. #include <sys/types.h>
  20. //
  21. // ---------------------------------------------------------------- Definitions
  22. //
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. //
  27. // ------------------------------------------------------ Data Type Definitions
  28. //
  29. /*++
  30. Structure Description:
  31. This structure describes information about a user.
  32. Members:
  33. pw_name - Stores a pointer to a string containing the user's login name.
  34. pw_passwd - Stores a pointer to a string containing the user's encrypted
  35. password.
  36. pw_uid - Stores the user's unique identifier.
  37. pw_gid - Stores the user's group identifier.
  38. pw_gecos - Stores a pointer to a string containing the user's real name,
  39. and possibly other information such as a phone number.
  40. pw_dir - Stores a pointer to a string containing the user's home directory
  41. path.
  42. pw_shell - Stores a pointer to a string containing the path to a program
  43. the user should use as a shell.
  44. --*/
  45. struct passwd {
  46. char *pw_name;
  47. char *pw_passwd;
  48. uid_t pw_uid;
  49. gid_t pw_gid;
  50. char *pw_gecos;
  51. char *pw_dir;
  52. char *pw_shell;
  53. };
  54. //
  55. // -------------------------------------------------------------------- Globals
  56. //
  57. //
  58. // -------------------------------------------------------- Function Prototypes
  59. //
  60. LIBC_API
  61. struct passwd *
  62. getpwnam (
  63. const char *UserName
  64. );
  65. /*++
  66. Routine Description:
  67. This routine searches the user database for a user matching the given
  68. name, and returns information about that user. This routine is neither
  69. reentrant nor thread safe.
  70. Arguments:
  71. UserName - Supplies a pointer to the null terminated string containing the
  72. user name to search for.
  73. Return Value:
  74. Returns a pointer to the user information on success. This buffer may be
  75. overwritten by subsequent calls to getpwent, getpwnam, or getpwuid.
  76. NULL on failure or if the given user was not found. On failure, errno will
  77. be set to provide more information.
  78. --*/
  79. LIBC_API
  80. int
  81. getpwnam_r (
  82. const char *UserName,
  83. struct passwd *UserInformation,
  84. char *Buffer,
  85. size_t BufferSize,
  86. struct passwd **Result
  87. );
  88. /*++
  89. Routine Description:
  90. This routine searches the user database for a user matching the given
  91. name, and returns information about that user. This routine is reentrant
  92. and thread safe.
  93. Arguments:
  94. UserName - Supplies a pointer to the null terminated string containing the
  95. user name to search for.
  96. UserInformation - Supplies a pointer where the user information will be
  97. returned.
  98. Buffer - Supplies a buffer used to allocate strings that the user
  99. information points to out of. The maximum size needed for this buffer
  100. can be determined with the _SC_GETPW_R_SIZE_MAX sysconf parameter.
  101. BufferSize - Supplies the size of the supplied buffer in bytes.
  102. Result - Supplies a pointer where a pointer to the user information
  103. parameter will be returned on success, or NULL will be returned if the
  104. specified user could not be found.
  105. Return Value:
  106. 0 on success.
  107. Returns an error value on failure.
  108. --*/
  109. LIBC_API
  110. struct passwd *
  111. getpwuid (
  112. uid_t UserId
  113. );
  114. /*++
  115. Routine Description:
  116. This routine searches the user database for a user matching the given
  117. ID, and returns information about that user. This routine is neither
  118. reentrant nor thread safe.
  119. Arguments:
  120. UserId - Supplies the ID of the user to search for.
  121. Return Value:
  122. Returns a pointer to the user information on success. This buffer may be
  123. overwritten by subsequent calls to getpwent, getpwnam, or getpwuid.
  124. NULL on failure or if the given user was not found. On failure, errno will
  125. be set to provide more information.
  126. --*/
  127. LIBC_API
  128. int
  129. getpwuid_r (
  130. uid_t UserId,
  131. struct passwd *UserInformation,
  132. char *Buffer,
  133. size_t BufferSize,
  134. struct passwd **Result
  135. );
  136. /*++
  137. Routine Description:
  138. This routine searches the user database for a user matching the given
  139. ID, and returns information about that user. This routine is reentrant
  140. and thread safe.
  141. Arguments:
  142. UserId - Supplies the user ID to look up.
  143. UserInformation - Supplies a pointer where the user information will be
  144. returned.
  145. Buffer - Supplies a buffer used to allocate strings that the user
  146. information points to out of. The maximum size needed for this buffer
  147. can be determined with the _SC_GETPW_R_SIZE_MAX sysconf parameter.
  148. BufferSize - Supplies the size of the supplied buffer in bytes.
  149. Result - Supplies a pointer where a pointer to the user information
  150. parameter will be returned on success, or NULL will be returned if the
  151. specified user could not be found.
  152. Return Value:
  153. 0 on success.
  154. Returns an error value on failure.
  155. --*/
  156. LIBC_API
  157. struct passwd *
  158. getpwent (
  159. void
  160. );
  161. /*++
  162. Routine Description:
  163. This routine returns a pointer to the broken out fields of the next entry
  164. in the user database. This function is neither thread-safe nor reentrant.
  165. Arguments:
  166. None.
  167. Return Value:
  168. Returns a pointer to the next entry in the user database. The caller may
  169. not modify or free this memory, and it may be overwritten by subsequent
  170. calls to getpwent.
  171. NULL if the end of the user database is reached or on error.
  172. --*/
  173. LIBC_API
  174. int
  175. getpwent_r (
  176. struct passwd *Information,
  177. char *Buffer,
  178. size_t BufferSize,
  179. struct passwd **ReturnPointer
  180. );
  181. /*++
  182. Routine Description:
  183. This routine returns a pointer to the broken out fields of the next entry
  184. in the user database. This is the reentrant version of getpwent.
  185. Arguments:
  186. Information - Supplies a pointer where the user information will be
  187. returned on success.
  188. Buffer - Supplies a pointer to the buffer where strings will be stored.
  189. BufferSize - Supplies the size of the given buffer in bytes.
  190. ReturnPointer - Supplies a pointer to a pointer to a user information
  191. structure. On success, the information pointer parameter will be
  192. returned here.
  193. Return Value:
  194. 0 on success.
  195. -1 on failure, and errno will be set to contain more information.
  196. --*/
  197. LIBC_API
  198. int
  199. fgetpwent_r (
  200. FILE *File,
  201. struct passwd *Information,
  202. char *Buffer,
  203. size_t BufferSize,
  204. struct passwd **ReturnPointer
  205. );
  206. /*++
  207. Routine Description:
  208. This routine returns a pointer to the broken out fields of the next entry
  209. in the user database.
  210. Arguments:
  211. File - Supplies a pointer to a file to read the information from.
  212. Information - Supplies a pointer where the user information will be
  213. returned on success.
  214. Buffer - Supplies a pointer to the buffer where strings will be stored.
  215. BufferSize - Supplies the size of the given buffer in bytes.
  216. ReturnPointer - Supplies a pointer to a pointer to a user information
  217. structure. On success, the information pointer parameter will be
  218. returned here.
  219. Return Value:
  220. 0 on success.
  221. ENOENT if there are no more entries.
  222. Returns an error value on failure.
  223. --*/
  224. LIBC_API
  225. void
  226. setpwent (
  227. void
  228. );
  229. /*++
  230. Routine Description:
  231. This routine rewinds the user database handle back to the beginning of the
  232. user database. The next call to getpwent will return the first entry in the
  233. user database.
  234. Arguments:
  235. None.
  236. Return Value:
  237. None.
  238. --*/
  239. LIBC_API
  240. void
  241. endpwent (
  242. void
  243. );
  244. /*++
  245. Routine Description:
  246. This routine closes an open handle to the user database established with
  247. setpwent or getpwent.
  248. Arguments:
  249. None.
  250. Return Value:
  251. None.
  252. --*/
  253. LIBC_API
  254. int
  255. putpwent (
  256. const struct passwd *Record,
  257. FILE *Stream
  258. );
  259. /*++
  260. Routine Description:
  261. This routine records a new password record in the given stream.
  262. Arguments:
  263. Record - Supplies the password record to add.
  264. Stream - Supplies the stream to write the record to.
  265. Return Value:
  266. 0 on success.
  267. -1 on failure, and errno will be set to contain more information.
  268. --*/
  269. #ifdef __cplusplus
  270. }
  271. #endif
  272. #endif