99
1010import abc
1111import contextlib
12+ import enum
1213import errno
1314import fcntl
1415import io
@@ -2034,6 +2035,19 @@ def use_update_alternatives() -> bool:
20342035 return os .path .exists ("/var/lib/dpkg/alternatives/omd" )
20352036
20362037
2038+ def _crontab_access () -> bool :
2039+ return (
2040+ subprocess .run (
2041+ ["crontab" , "-e" ],
2042+ env = {"VISUAL" : "true" , "EDITOR" : "true" },
2043+ check = False ,
2044+ stdout = subprocess .DEVNULL ,
2045+ stderr = subprocess .DEVNULL ,
2046+ ).returncode
2047+ == 0
2048+ )
2049+
2050+
20372051def main_create (
20382052 version_info : VersionInfo ,
20392053 site : SiteContext ,
@@ -2218,13 +2232,19 @@ def finalize_site(
22182232
22192233 # avoid executing hook 'TMPFS' and cleaning an initialized tmp directory
22202234 # see CMK-3067
2221- finalize_site_as_user (version_info , site , command_type , ignored_hooks = ["TMPFS" ])
2222- sys .exit (0 )
2235+ outcome = finalize_site_as_user (
2236+ version_info , site , command_type , ignored_hooks = ["TMPFS" ]
2237+ )
2238+ sys .exit (outcome .value )
22232239 except Exception as e :
2224- bail_out ("Failed to finalize site: %s" % e )
2240+ sys .stderr .write (f"Failed to finalize site: { e } \n " )
2241+ sys .exit (FinalizeOutcome .ABORTED .value )
22252242 else :
22262243 _wpid , status = os .waitpid (pid , 0 )
2227- if status :
2244+ if (
2245+ not os .WIFEXITED (status )
2246+ or (outcome := FinalizeOutcome (os .WEXITSTATUS (status ))) is FinalizeOutcome .ABORTED
2247+ ):
22282248 bail_out ("Error in non-priviledged sub-process." )
22292249
22302250 # The config changes above, made with the site user, have to be also available for
@@ -2241,14 +2261,21 @@ def finalize_site(
22412261 apache_reload ,
22422262 verbose = verbose ,
22432263 )
2264+ sys .exit (outcome .value )
2265+
2266+
2267+ class FinalizeOutcome (enum .Enum ):
2268+ OK = 0
2269+ ABORTED = 1
2270+ WARN = 2
22442271
22452272
22462273def finalize_site_as_user (
22472274 version_info : VersionInfo ,
22482275 site : SiteContext ,
22492276 command_type : CommandType ,
22502277 ignored_hooks : list [str ] | None = None ,
2251- ) -> None :
2278+ ) -> FinalizeOutcome :
22522279 # Mount and create contents of tmpfs. This must be done as normal
22532280 # user. We also could do this at 'omd start', but this might confuse
22542281 # users. They could create files below tmp which would be shadowed
@@ -2268,6 +2295,10 @@ def finalize_site_as_user(
22682295 save_instance_id (file_path = get_instance_id_file_path (Path (site .dir )), instance_id = uuid4 ())
22692296
22702297 call_scripts (site , "post-" + command_type .short , open_pty = sys .stdout .isatty ())
2298+ if not _crontab_access ():
2299+ sys .stderr .write ("Warning: site user cannot access crontab\n " )
2300+ return FinalizeOutcome .WARN
2301+ return FinalizeOutcome .OK
22712302
22722303
22732304def main_rm (
0 commit comments