users.pp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. unit users;
  2. Interface
  3. uses pwd,shadow,grp,Linux,SysUtils,Classes;
  4. Type
  5. EUserLookupError = Class(Exception);
  6. EGroupLookupError = Class(Exception);
  7. EShadowLookupError = Class(Exception);
  8. { User functions }
  9. Function getpwnam(Const UserName: String) : PPasswordRecord;
  10. Procedure GetUserData(Const UserName : String; Var Data : TPasswordRecord); overload;
  11. Procedure GetUserData(Uid : Integer; Var Data : TPasswordRecord); overload;
  12. function GetUserName(UID : Integer) : String;
  13. function GetUserId(Const UserName : String) : Integer;
  14. function GetUserGid(Const UserName : String) : Integer;
  15. function GetUserDir(Const UserName : String): String;
  16. function GetUserDescription(Const UserName : String): String;
  17. Procedure GetUserList(List : Tstrings);overload;
  18. Procedure GetUserList(List : TStrings; WithIDs : Boolean);overload;
  19. { Group functions }
  20. Function getgrnam(Const GroupName: String) : PGroup;
  21. Procedure GetGroupData(Const GroupName : String; Var Data : TGroup); overload;
  22. Procedure GetGroupData(Gid : Integer; Var Data : TGroup); overload;
  23. function GetGroupName(GID : Integer) : String;
  24. function GetGroupId(Const GroupName : String) : Integer;
  25. Procedure GetGroupList(List : Tstrings);overload;
  26. Procedure GetGroupList(List : TStrings; WithIDs : Boolean);overload;
  27. Procedure GetGroupMembers(GID : Integer;List : TStrings);overload;
  28. Procedure GetGroupMembers(Const GroupName : String;List : TStrings);overload;
  29. { Shadow password functions }
  30. function getspnam(UserName : String): PPasswordFileEntry;
  31. function sgetspent(Line : String): PPasswordFileEntry;
  32. Procedure GetUserShadowData(Const UserName : String; Var Data : TPasswordFileEntry);overload;
  33. Procedure GetUserShadowData(UID : Integer; Var Data : TPasswordFileEntry);overload;
  34. { Extra functions }
  35. Function GetUserGroup(Const UserName : String) : String;
  36. Implementation
  37. ResourceString
  38. EnoSuchUserName = 'Unknown username: "%s"';
  39. EnoSuchUserID = 'Unknown user ID: %d';
  40. EnoSuchGroupName = 'Unknown groupname: "%s"';
  41. EnoSuchGroupID = 'Unknown group ID: %d';
  42. ENoShadowEntry = 'No shadow file entry for "%s"';
  43. EShadowNotPermitted = 'Not enough permissions to access shadow password file';
  44. Function getpwnam(Const UserName: String) : PPasswordRecord;
  45. begin
  46. Result:=pwd.getpwnam(Pchar(UserName));
  47. end;
  48. Procedure GetUserData(Const UserName : String; Var Data : TPasswordRecord);
  49. Var P : PPasswordRecord;
  50. begin
  51. P:=Getpwnam(UserName);
  52. If P<>Nil then
  53. Data:=P^
  54. else
  55. Raise EUserLookupError.CreateFmt(ENoSuchUserName,[UserName]);
  56. end;
  57. Procedure GetUserData(Uid : Integer; Var Data : TPasswordRecord);
  58. Var P : PPasswordRecord;
  59. begin
  60. P:=Getpwuid(Uid);
  61. If P<>Nil then
  62. Data:=P^
  63. else
  64. Raise EUserLookupError.CreateFmt(ENoSuchUserID,[Uid]);
  65. end;
  66. function GetUserName(UID : Integer) : String;
  67. Var
  68. UserData : TPasswordRecord;
  69. begin
  70. GetuserData(UID,UserData);
  71. Result:=strpas(UserData.pw_Name);
  72. end;
  73. function GetUserId(Const UserName : String) : Integer;
  74. Var
  75. UserData : TPasswordRecord;
  76. begin
  77. GetUserData(UserName,UserData);
  78. Result:=UserData.pw_uid;
  79. end;
  80. function GetUserGId(Const UserName : String) : Integer;
  81. Var
  82. UserData : TPasswordRecord;
  83. begin
  84. GetUserData(UserName,UserData);
  85. Result:=UserData.pw_gid;
  86. end;
  87. function GetUserDir(Const UserName : String): String;
  88. Var
  89. UserData : TPasswordRecord;
  90. begin
  91. GetUserData(UserName,UserData);
  92. Result:=strpas(UserData.pw_dir);
  93. end;
  94. function GetUserDescription(Const UserName : String): String;
  95. Var
  96. UserData : TPasswordRecord;
  97. begin
  98. GetUserData(UserName,UserData);
  99. Result:=strpas(UserData.pw_gecos);
  100. end;
  101. Procedure GetUserList(List : Tstrings);
  102. begin
  103. GetUserList(List,False);
  104. end;
  105. Procedure GetUserList(List : TStrings; WithIDs : Boolean);
  106. Var
  107. P : PPasswordRecord;
  108. begin
  109. List.Clear;
  110. setpwent;
  111. try
  112. Repeat
  113. P:=getpwent;
  114. If P<>Nil then
  115. begin
  116. If WithIDs then
  117. List.Add(Format('%d=%s',[P^.pw_uid,strpas(p^.pw_name)]))
  118. else
  119. List.Add(strpas(p^.pw_name));
  120. end;
  121. until (P=Nil);
  122. finally
  123. endpwent;
  124. end;
  125. end;
  126. { ---------------------------------------------------------------------
  127. Group Functions
  128. ---------------------------------------------------------------------}
  129. Function getgrnam(Const GroupName: String) : PGroup;
  130. begin
  131. Result:=grp.getgrnam(Pchar(GroupName));
  132. end;
  133. Procedure GetGroupData(Const GroupName : String; Var Data : TGroup); overload;
  134. Var P : PGroup;
  135. begin
  136. P:=Getgrnam(GroupName);
  137. If P<>Nil then
  138. Data:=P^
  139. else
  140. Raise EGroupLookupError.CreateFmt(ENoSuchGroupName,[GroupName]);
  141. end;
  142. Procedure GetGroupData(Gid : Integer; Var Data : TGroup); overload;
  143. Var P : PGroup;
  144. begin
  145. P:=Getgrgid(gid);
  146. If P<>Nil then
  147. Data:=P^
  148. else
  149. Raise EGroupLookupError.CreateFmt(ENoSuchGroupID,[Gid]);
  150. end;
  151. function GetGroupName(GID : Integer) : String;
  152. Var
  153. G : TGroup;
  154. begin
  155. GetGroupData(Gid,G);
  156. Result:=StrPas(G.gr_name);
  157. end;
  158. function GetGroupId(Const GroupName : String) : Integer;
  159. Var
  160. G : TGroup;
  161. begin
  162. GetGroupData(GroupName,G);
  163. Result:=G.gr_gid;
  164. end;
  165. Procedure GetGroupList(List : Tstrings);overload;
  166. begin
  167. GetGroupList(List,False);
  168. end;
  169. Procedure GetGroupList(List : TStrings; WithIDs : Boolean);overload;
  170. Var
  171. G : PGroup;
  172. begin
  173. List.Clear;
  174. setgrent;
  175. try
  176. Repeat
  177. G:=getgrent;
  178. If G<>Nil then
  179. begin
  180. If WithIDs then
  181. List.Add(Format('%d=%s',[G^.gr_gid,strpas(G^.gr_name)]))
  182. else
  183. List.Add(strpas(G^.gr_name));
  184. end;
  185. until (G=Nil);
  186. finally
  187. endgrent;
  188. end;
  189. end;
  190. Function PCharListToStrings(P : PPChar; List : TStrings) : Integer;
  191. begin
  192. List.Clear;
  193. While P^<>Nil do
  194. begin
  195. List.Add(StrPas(P^));
  196. P:=PPChar(PChar(P)+SizeOf(PChar));
  197. end;
  198. Result:=List.Count;
  199. end;
  200. Procedure GetGroupMembers(GID : Integer;List : TStrings);
  201. Var
  202. G : TGroup;
  203. begin
  204. GetGroupData(GID,G);
  205. PCharListToStrings(G.gr_mem,List);
  206. end;
  207. Procedure GetGroupMembers(Const GroupName : String;List : TStrings);
  208. Var
  209. G : TGroup;
  210. begin
  211. GetGroupData(GroupName,G);
  212. PCharListToStrings(g.gr_mem,List);
  213. end;
  214. { Shadow password functions }
  215. function getspnam(UserName : String): PPasswordFileEntry;
  216. begin
  217. result:=shadow.getspnam(Pchar(UserName));
  218. end;
  219. function sgetspent(Line : String): PPasswordFileEntry;
  220. begin
  221. Result:=shadow.sgetspent(Pchar(Line));
  222. end;
  223. Procedure GetUserShadowData(Const UserName : String; Var Data : TPasswordFileEntry);
  224. Var
  225. P : PPasswordFileEntry;
  226. begin
  227. P:=getspnam(UserName);
  228. If P=Nil then
  229. If (GetUID<>0) and (GetEUID<>0) then
  230. Raise EShadowLookupError.Create(EShadowNotPermitted)
  231. else
  232. Raise EShadowLookupError.CreateFmt(ENoShadowEntry,[UserName])
  233. else
  234. Data:=P^;
  235. end;
  236. Procedure GetUserShadowData(UID : Integer; Var Data : TPasswordFileEntry);
  237. begin
  238. GetUserShadowData(GetUserName(UID),Data);
  239. end;
  240. { Extra functions }
  241. Function GetUserGroup(Const UserName : String) : String;
  242. begin
  243. GetGroupName(GetUserGid(UserName));
  244. end;
  245. end.