Skip to content

Commit

Permalink
Allow cron to call private methods
Browse files Browse the repository at this point in the history
  • Loading branch information
themylogin committed Dec 17, 2024
1 parent fa65cf8 commit 7e34e26
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/middlewared/middlewared/api/base/server/ws_handler/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,16 @@ def _can_call_private_methods(self, app: RpcWebSocketApp):
# System-initiated calls to `midclt`
return True

if ppids := app.origin.ppids():
try:
with open("/run/crond.pid") as f:
cron_pid = int(f.read())
except (FileNotFoundError, ValueError):
return False

if cron_pid in ppids:
return True

return False

async def process_method_call(self, app: RpcWebSocketApp, id_: Any, method: Method, params: list):
Expand Down
26 changes: 25 additions & 1 deletion src/middlewared/middlewared/utils/origin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass
from functools import cached_property
import re
from socket import AF_INET, AF_INET6, AF_UNIX, SO_PEERCRED, SOL_SOCKET
from struct import calcsize, unpack

Expand Down Expand Up @@ -113,6 +113,30 @@ def loginuid(self) -> int | None:

return loginuid

def ppids(self) -> set[int]:
if self.pid is None:
return set()

pid = self.pid
ppids = set()
while True:
try:
with open(f"/proc/{pid}/status") as f:
status = f.read()
except FileNotFoundError:
break

if m := re.search(r"PPid:\t([0-9]+)", status):
pid = int(m.group(1))
if pid <= 1:
break

ppids.add(pid)
else:
break

return ppids


def get_tcp_ip_info(sock, request) -> tuple:
# All API connections are terminated by nginx reverse
Expand Down

0 comments on commit 7e34e26

Please sign in to comment.