users.pp 7.0 KB

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