shadow.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. shadow.h
  9. Abstract:
  10. This header contains definitions for shadow passwords.
  11. Author:
  12. Evan Green 3-Mar-2015
  13. --*/
  14. #ifndef _SHADOW_H
  15. #define _SHADOW_H
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. #include <paths.h>
  20. #include <stdio.h>
  21. #include <stddef.h>
  22. //
  23. // ---------------------------------------------------------------- Definitions
  24. //
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. //
  29. // Define the path to the shadow password file.
  30. //
  31. #define SHADOW _PATH_SHADOW
  32. //
  33. // ------------------------------------------------------ Data Type Definitions
  34. //
  35. /*++
  36. Structure Description:
  37. This structure describes the structure of the shadow password file.
  38. Members:
  39. sp_namp - Stores a pointer to the login name.
  40. sp_pwdp - Stores a pointer to the encrypted password.
  41. sp_lstchg - Stores the date of the last password change, in number of days
  42. since 1970 UTC.
  43. sp_min - Stores the minimum number of days between password changes.
  44. sp_max - Stores the maximum number of days between password changes.
  45. sp_warn - Stores the number of days after which the user should be warned
  46. to change their password.
  47. sp_inact - Stores the number of days after the password expires that an
  48. account is disabled.
  49. sp_expire - Stores the date the account expires, in number of days since
  50. 1970 UTC.
  51. sp_flag - Stores a reserved flags field.
  52. --*/
  53. struct spwd {
  54. char *sp_namp;
  55. char *sp_pwdp;
  56. long int sp_lstchg;
  57. long int sp_min;
  58. long int sp_max;
  59. long int sp_warn;
  60. long int sp_inact;
  61. long int sp_expire;
  62. unsigned long int sp_flag;
  63. };
  64. //
  65. // -------------------------------------------------------------------- Globals
  66. //
  67. //
  68. // -------------------------------------------------------- Function Prototypes
  69. //
  70. LIBC_API
  71. struct spwd *
  72. getspnam (
  73. const char *UserName
  74. );
  75. /*++
  76. Routine Description:
  77. This routine searches the shadow password database for a user matching the
  78. given name, and returns information about that user's password. This
  79. routine is neither reentrant nor thread safe.
  80. Arguments:
  81. UserName - Supplies a pointer to the null terminated string containing the
  82. user name to search for.
  83. Return Value:
  84. Returns a pointer to the user password information on success. This buffer
  85. may be overwritten by subsequent calls to getspent or getspnam.
  86. NULL on failure or if the given user was not found. On failure, errno will
  87. be set to provide more information.
  88. --*/
  89. LIBC_API
  90. int
  91. getspnam_r (
  92. const char *UserName,
  93. struct spwd *PasswordInformation,
  94. char *Buffer,
  95. size_t BufferSize,
  96. struct spwd **Result
  97. );
  98. /*++
  99. Routine Description:
  100. This routine searches the shadow password database for a user matching the
  101. given name, and returns information about that user's password.
  102. Arguments:
  103. UserName - Supplies a pointer to the null terminated string containing the
  104. user name to search for.
  105. PasswordInformation - Supplies a pointer where the user's password
  106. information will be returned.
  107. Buffer - Supplies a buffer used to allocate strings that the user
  108. information points to out of. The maximum size needed for this buffer
  109. can be determined with the _SC_GETPW_R_SIZE_MAX sysconf parameter.
  110. BufferSize - Supplies the size of the supplied buffer in bytes.
  111. Result - Supplies a pointer where a pointer to the user information
  112. parameter will be returned on success, or NULL will be returned if the
  113. specified user could not be found.
  114. Return Value:
  115. 0 on success.
  116. Returns an error value on failure.
  117. --*/
  118. LIBC_API
  119. struct spwd *
  120. getspent (
  121. void
  122. );
  123. /*++
  124. Routine Description:
  125. This routine returns a pointer to the broken out fields of the next entry
  126. in the user database. This function is neither thread-safe nor reentrant.
  127. Arguments:
  128. None.
  129. Return Value:
  130. Returns a pointer to the next entry in the user database. The caller may
  131. not modify or free this memory, and it may be overwritten by subsequent
  132. calls to getspent.
  133. NULL if the end of the user database is reached or on error.
  134. --*/
  135. LIBC_API
  136. int
  137. getspent_r (
  138. struct spwd *Information,
  139. char *Buffer,
  140. size_t BufferSize,
  141. struct spwd **ReturnPointer
  142. );
  143. /*++
  144. Routine Description:
  145. This routine returns a pointer to the broken out fields of the next entry
  146. in the user password database. This is the reentrant version of getspent.
  147. Arguments:
  148. Information - Supplies a pointer where the user password information will
  149. be returned on success.
  150. Buffer - Supplies a pointer to the buffer where strings will be stored.
  151. BufferSize - Supplies the size of the given buffer in bytes.
  152. ReturnPointer - Supplies a pointer to a pointer to a user password
  153. information structure. On success, the information pointer parameter
  154. will be returned here.
  155. Return Value:
  156. 0 on success.
  157. ENOENT if there are no more entries.
  158. Returns an error value on failure.
  159. --*/
  160. LIBC_API
  161. struct spwd *
  162. fgetspent (
  163. FILE *File
  164. );
  165. /*++
  166. Routine Description:
  167. This routine returns a pointer to the broken out fields of the next entry
  168. in the user password database.
  169. Arguments:
  170. File - Supplies a pointer to a file to read the information from.
  171. Return Value:
  172. Returns a pointer to the next entry in the user database. The caller may
  173. not modify or free this memory, and it may be overwritten by subsequent
  174. calls to getspent.
  175. NULL if the end of the user database is reached or on error.
  176. --*/
  177. LIBC_API
  178. int
  179. fgetspent_r (
  180. FILE *File,
  181. struct spwd *Information,
  182. char *Buffer,
  183. size_t BufferSize,
  184. struct spwd **ReturnPointer
  185. );
  186. /*++
  187. Routine Description:
  188. This routine returns a pointer to the broken out fields of the next entry
  189. in the user password database.
  190. Arguments:
  191. File - Supplies a pointer to a file to read the information from.
  192. Information - Supplies a pointer where the user password information will
  193. be returned on success.
  194. Buffer - Supplies a pointer to the buffer where strings will be stored.
  195. BufferSize - Supplies the size of the given buffer in bytes.
  196. ReturnPointer - Supplies a pointer to a pointer to a user password
  197. information structure. On success, the information pointer parameter
  198. will be returned here.
  199. Return Value:
  200. 0 on success.
  201. ENOENT if there are no more entries.
  202. Returns an error value on failure.
  203. --*/
  204. LIBC_API
  205. struct spwd *
  206. sgetspent (
  207. const char *String
  208. );
  209. /*++
  210. Routine Description:
  211. This routine returns a pointer to the broken out fields of the next entry
  212. in the user database based on a shadow password file line.
  213. Arguments:
  214. String - Supplies a pointer to the shadow password line.
  215. Return Value:
  216. Returns a pointer to the next entry in the user database. The caller may
  217. not modify or free this memory, and it may be overwritten by subsequent
  218. calls to getspent.
  219. NULL if the end of the user database is reached or on error.
  220. --*/
  221. LIBC_API
  222. int
  223. sgetspent_r (
  224. const char *String,
  225. struct spwd *Information,
  226. char *Buffer,
  227. size_t BufferSize,
  228. struct spwd **ReturnPointer
  229. );
  230. /*++
  231. Routine Description:
  232. This routine returns a pointer to the broken out fields of the next entry
  233. in the user database based on a shadow password file line.
  234. Arguments:
  235. String - Supplies a pointer to the shadow password line.
  236. Information - Supplies a pointer where the user password information will
  237. be returned on success.
  238. Buffer - Supplies a pointer to the buffer where strings will be stored.
  239. BufferSize - Supplies the size of the given buffer in bytes.
  240. ReturnPointer - Supplies a pointer to a pointer to a user password
  241. information structure. On success, the information pointer parameter
  242. will be returned here.
  243. Return Value:
  244. 0 on success.
  245. ENOENT if there are no more entries.
  246. Returns an error value on failure.
  247. --*/
  248. LIBC_API
  249. void
  250. setspent (
  251. void
  252. );
  253. /*++
  254. Routine Description:
  255. This routine rewinds the user password shadow database handle back to the
  256. beginning of the database. The next call to getspent will return the first
  257. entry in the user database.
  258. Arguments:
  259. None.
  260. Return Value:
  261. None.
  262. --*/
  263. LIBC_API
  264. void
  265. endspent (
  266. void
  267. );
  268. /*++
  269. Routine Description:
  270. This routine closes an open handle to the user password database
  271. established with setspent or getspent.
  272. Arguments:
  273. None.
  274. Return Value:
  275. None.
  276. --*/
  277. LIBC_API
  278. int
  279. putspent (
  280. const struct spwd *Information,
  281. FILE *Stream
  282. );
  283. /*++
  284. Routine Description:
  285. This routine writes a shadow password entry into the given shadow password
  286. database file stream. This routine is neither thread safe nor reentrant.
  287. Arguments:
  288. Information - Supplies a pointer to the password information to write.
  289. Stream - Supplies a pointer to the file stream to write it out to.
  290. Return Value:
  291. 0 on success.
  292. -1 on failure, and errno will be set to contain more information.
  293. --*/
  294. LIBC_API
  295. int
  296. lckpwdf (
  297. void
  298. );
  299. /*++
  300. Routine Description:
  301. This routine locks the shadow password database file. This routine is not
  302. multi-thread safe. This mechanism does not prevent someone from writing
  303. directly to the shadow password file, as the locks it uses are
  304. discretionary.
  305. Arguments:
  306. None.
  307. Return Value:
  308. 0 on success.
  309. -1 if the lock could not be acquired within 15 seconds, and errno may be
  310. set to contain more information.
  311. --*/
  312. LIBC_API
  313. int
  314. ulckpwdf (
  315. void
  316. );
  317. /*++
  318. Routine Description:
  319. This routine unlocks the shadow password file.
  320. Arguments:
  321. None.
  322. Return Value:
  323. 0 on success.
  324. -1 on failure.
  325. --*/
  326. #ifdef __cplusplus
  327. }
  328. #endif
  329. #endif