(* Relinquish the capabilities which allowed a non-root user to create a socket
in /var/run or with a port number < 1024.
*)
procedure RelinquishCapabilities;
var
cap: boolean;
begin
(* When not running as root, relinquish any capabilities we've been granted. *)
(* Even if running as root do this silently, to allow for the case where a *)
(* capability has been explicitly added. Note that I'm avoiding "permissions" *)
(* etc. here as ambiguous, I don't think there's any need to translate *)
(* "capability" etc. in this context. *)
(* WARNING: visibility of capabilities might be modified if running under the *)
(* control of the debugger. Always test outside the debugger before jumping to *)
(* any conclusions. *)
if (FpGetgid <> 0) or (FpGetuid <> 0) then begin (* Give up capability *)
if GetCapability(cap, CAP_DAC_OVERRIDE) then
if cap then begin
Write(StdErr, 'Relinquishing CAP_DAC_OVERRIDE capability... ');
if SetCapability(false, CAP_DAC_OVERRIDE) then
WriteLn(StdErr, 'OK')
else
WriteLn(StdErr, 'failed')
end else begin end;
if GetCapability(cap, CAP_DAC_OVERRIDE, CAP_PERMITTED) then
if cap then begin
Write(StdErr, 'Relinquishing CAP_DAC_OVERRIDE permittivity... ');
if SetCapability(false, CAP_DAC_OVERRIDE, CAP_PERMITTED) then
WriteLn(StdErr, 'OK')
else
WriteLn(StdErr, 'failed')
end else begin end;
if GetCapability(cap, CAP_NET_BIND_SERVICE) then
if cap then begin
Write(StdErr, 'Relinquishing NET_BIND_SERVICE capability... ');
if SetCapability(false, CAP_NET_BIND_SERVICE) then
WriteLn(StdErr, 'OK')
else
WriteLn(StdErr, 'failed')
end else begin end;
if GetCapability(cap, CAP_NET_BIND_SERVICE, CAP_PERMITTED) then
if cap then begin
Write(StdErr, 'Relinquishing NET_BIND_SERVICE permittivity... ');
if SetCapability(false, CAP_NET_BIND_SERVICE, CAP_PERMITTED) then
WriteLn(StdErr, 'OK')
else
WriteLn(StdErr, 'failed')
end else begin end;
if GetCapability(cap, CAP_NET_RAW) then
if cap then begin
Write(StdErr, 'Relinquishing NET_RAW capability... ');
if SetCapability(false, CAP_NET_RAW) then
WriteLn(StdErr, 'OK')
else
WriteLn(StdErr, 'failed')
end else begin end;
if GetCapability(cap, CAP_NET_RAW, CAP_PERMITTED) then
if cap then begin
Write(StdErr, 'Relinquishing NET_RAW permittivity... ');
if SetCapability(false, CAP_NET_RAW, CAP_PERMITTED) then
WriteLn(StdErr, 'OK')
else
WriteLn(StdErr, 'failed')
end else begin end
end else begin
SetCapability(false, CAP_DAC_OVERRIDE);
SetCapability(false, CAP_DAC_OVERRIDE, CAP_PERMITTED);
SetCapability(false, CAP_NET_BIND_SERVICE);
SetCapability(false, CAP_NET_BIND_SERVICE, CAP_PERMITTED);
SetCapability(false, CAP_NET_RAW);
SetCapability(false, CAP_NET_RAW, CAP_PERMITTED)
end
end { RelinquishCapabilities } ;