fakepwd.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # -*- test-case-name: twisted.python.test.test_fakepwd -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. L{twisted.python.fakepwd} provides a fake implementation of the L{pwd} API.
  6. """
  7. from __future__ import absolute_import, division
  8. __all__ = ['UserDatabase', 'ShadowDatabase']
  9. class _UserRecord(object):
  10. """
  11. L{_UserRecord} holds the user data for a single user in L{UserDatabase}.
  12. It corresponds to L{pwd.struct_passwd}. See that class for attribute
  13. documentation.
  14. """
  15. def __init__(self, name, password, uid, gid, gecos, home, shell):
  16. self.pw_name = name
  17. self.pw_passwd = password
  18. self.pw_uid = uid
  19. self.pw_gid = gid
  20. self.pw_gecos = gecos
  21. self.pw_dir = home
  22. self.pw_shell = shell
  23. def __len__(self):
  24. return 7
  25. def __getitem__(self, index):
  26. return (
  27. self.pw_name, self.pw_passwd, self.pw_uid,
  28. self.pw_gid, self.pw_gecos, self.pw_dir, self.pw_shell)[index]
  29. class UserDatabase(object):
  30. """
  31. L{UserDatabase} holds a traditional POSIX user data in memory and makes it
  32. available via the same API as L{pwd}.
  33. @ivar _users: A C{list} of L{_UserRecord} instances holding all user data
  34. added to this database.
  35. """
  36. def __init__(self):
  37. self._users = []
  38. def addUser(self, username, password, uid, gid, gecos, home, shell):
  39. """
  40. Add a new user record to this database.
  41. @param username: The value for the C{pw_name} field of the user
  42. record to add.
  43. @type username: C{str}
  44. @param password: The value for the C{pw_passwd} field of the user
  45. record to add.
  46. @type password: C{str}
  47. @param uid: The value for the C{pw_uid} field of the user record to
  48. add.
  49. @type uid: C{int}
  50. @param gid: The value for the C{pw_gid} field of the user record to
  51. add.
  52. @type gid: C{int}
  53. @param gecos: The value for the C{pw_gecos} field of the user record
  54. to add.
  55. @type gecos: C{str}
  56. @param home: The value for the C{pw_dir} field of the user record to
  57. add.
  58. @type home: C{str}
  59. @param shell: The value for the C{pw_shell} field of the user record to
  60. add.
  61. @type shell: C{str}
  62. """
  63. self._users.append(_UserRecord(
  64. username, password, uid, gid, gecos, home, shell))
  65. def getpwuid(self, uid):
  66. """
  67. Return the user record corresponding to the given uid.
  68. """
  69. for entry in self._users:
  70. if entry.pw_uid == uid:
  71. return entry
  72. raise KeyError()
  73. def getpwnam(self, name):
  74. """
  75. Return the user record corresponding to the given username.
  76. """
  77. for entry in self._users:
  78. if entry.pw_name == name:
  79. return entry
  80. raise KeyError()
  81. def getpwall(self):
  82. """
  83. Return a list of all user records.
  84. """
  85. return self._users
  86. class _ShadowRecord(object):
  87. """
  88. L{_ShadowRecord} holds the shadow user data for a single user in
  89. L{ShadowDatabase}. It corresponds to C{spwd.struct_spwd}. See that class
  90. for attribute documentation.
  91. """
  92. def __init__(self, username, password, lastChange, min, max, warn, inact,
  93. expire, flag):
  94. self.sp_nam = username
  95. self.sp_pwd = password
  96. self.sp_lstchg = lastChange
  97. self.sp_min = min
  98. self.sp_max = max
  99. self.sp_warn = warn
  100. self.sp_inact = inact
  101. self.sp_expire = expire
  102. self.sp_flag = flag
  103. def __len__(self):
  104. return 9
  105. def __getitem__(self, index):
  106. return (
  107. self.sp_nam, self.sp_pwd, self.sp_lstchg, self.sp_min,
  108. self.sp_max, self.sp_warn, self.sp_inact, self.sp_expire,
  109. self.sp_flag)[index]
  110. class ShadowDatabase(object):
  111. """
  112. L{ShadowDatabase} holds a shadow user database in memory and makes it
  113. available via the same API as C{spwd}.
  114. @ivar _users: A C{list} of L{_ShadowRecord} instances holding all user data
  115. added to this database.
  116. @since: 12.0
  117. """
  118. def __init__(self):
  119. self._users = []
  120. def addUser(self, username, password, lastChange, min, max, warn, inact,
  121. expire, flag):
  122. """
  123. Add a new user record to this database.
  124. @param username: The value for the C{sp_nam} field of the user record to
  125. add.
  126. @type username: C{str}
  127. @param password: The value for the C{sp_pwd} field of the user record to
  128. add.
  129. @type password: C{str}
  130. @param lastChange: The value for the C{sp_lstchg} field of the user
  131. record to add.
  132. @type lastChange: C{int}
  133. @param min: The value for the C{sp_min} field of the user record to add.
  134. @type min: C{int}
  135. @param max: The value for the C{sp_max} field of the user record to add.
  136. @type max: C{int}
  137. @param warn: The value for the C{sp_warn} field of the user record to
  138. add.
  139. @type warn: C{int}
  140. @param inact: The value for the C{sp_inact} field of the user record to
  141. add.
  142. @type inact: C{int}
  143. @param expire: The value for the C{sp_expire} field of the user record
  144. to add.
  145. @type expire: C{int}
  146. @param flag: The value for the C{sp_flag} field of the user record to
  147. add.
  148. @type flag: C{int}
  149. """
  150. self._users.append(_ShadowRecord(
  151. username, password, lastChange,
  152. min, max, warn, inact, expire, flag))
  153. def getspnam(self, username):
  154. """
  155. Return the shadow user record corresponding to the given username.
  156. """
  157. for entry in self._users:
  158. if entry.sp_nam == username:
  159. return entry
  160. raise KeyError
  161. def getspall(self):
  162. """
  163. Return a list of all shadow user records.
  164. """
  165. return self._users