9Vj ddlZddlZddlZddlZddlZddlZddlZddlZddlm Z ddl m Z ej e Zd dZejdZdZdZdd Zd Zd ZdS)N) clcaptain)ExternalProgramFailedTc$ t|d5}|cdddS#1swxYwYdS#ttf$r>t jd|ztjd|stjdYdSwxYw)z Helper for read file, process errors and make backup before read :param: filename `str` name of file for read :param: exit_on_error `bool` use sys.exit on error or raise exception rNzError: failed to read ) open readlinesOSErrorIOErrorsecureiologging SILENT_FLAGsysexit)filename exit_on_error_files Bopt/cloudlinux/venv/lib/python3.11/site-packages/clcagefslib/io.py read_filers  (C  %E??$$ % % % % % % % % % % % % % % % % % % W 1HRTUVVV    s-A3 A7A7AA BBc t|SN)r)paths rread_file_cachedr&s T??cztj|||||tj}tj||duS)N)logger)r create_dir_securer closefd)rpermuidgid parent_pathfds r make_userdirr$,sF  # dCk(2B   B R :rcB tj|j}n#t$rYdSwxYw||krdS t j|}n#t $rYdSwxYwtj|j tj|kS)a CLOS-4545 guard against stealing a shared home's ``.cagefs``. Return ``True`` when ``home_path`` is owned by a user *other than* ``target_uid`` and that owner is a real ``/etc/passwd`` user whose home directory is ``home_path`` itself. On every panel CageFS supports, the home directory is owned by the real subscription/account user, so its owner is the authoritative answer to "who does this home belong to". When several ``/etc/passwd`` entries share one home -- e.g. an orphan ``sys_user`` left behind by a reverted Plesk migration, which merely has the home as its ``pw_dir`` but does not own it -- an operation aimed at a uid that is *not* the home owner must not chown ``/.cagefs`` to it: that would strip the real owner of its own ``.cagefs`` and break the subscription (the next HTTP request fails with 503). Callers must skip the chown when this returns ``True``. Keying on the home owner (rather than the current ``.cagefs`` owner) means the legitimate owner -- and automated repair such as ``update_status(fix_owner=True)`` -- can always (re)claim ``.cagefs``, even after it was previously flipped to an orphan. ``False`` is also returned when ``target_uid`` owns the home, when the home cannot be stat'd, or when it is owned by an unrelated uid (e.g. root) that does not live in this home, so normal provisioning, brand-new subscriptions and ownership repair keep working. F) osstatst_uidr pwdgetpwuidKeyErrorrrealpathpw_dir) home_path target_uidhome_uid home_owners rhome_owned_by_other_userr25s679%%, uu:u\(++ uu 7  J- . ."'2B2B92M2M MMs **A AAFc |s tj|s2 tj|nj#t$r]}|jt jkrtd|dnt d||Yd}~nd}~wwxYw tj ||n#ttf$r}d|d|dt|dd }t |||rtj||d nt#|t$j Yd}~d Sd}~wwxYwd S) NzSymlink z does not existz Error: Unable to remove symlink )exc_infoz Error: failed to create symlink z to z : ErrnozErr codeT)fileF)r&rislinkunlinkr errnoENOENTrinfoerrorrsymlinkrstrreplacer r printrstderr) dest_path link_name write_logforcesilentemsgs rswitch_symlinkrIas BGNN9-- Y Ii  Y Y Yw%,&& AyAAABBBB K KKVW XXX  Y   i 3 3 3 3./   wYwwIwwRUVWRXRXR`R`ahjtRuRuwwC LLqL ) ) ) , fd3333c ++++44444  5s/9 B ABB $B::E A2 C+>BC+BC+C C$#C$+"D5D#"D5# D0-D5/D00D5c*tj|tjtjztjz} tj||tj|||tj|dS#tj|wxYwr)r&rO_RDONLY O_NOFOLLOW O_CLOEXECfchmodfchownclose)rmoder r!r#s rapply_metadata_nofollowrdss r{R]2R\A B BB "d "c3   s +A<<B)T)TTF)r9 functoolsr r&r)rrNr clcommonrclcommon.utilsr getLogger__name__rrcacherr$r2rIr[rdrrrls   000000  8 $ $"  )N)N)NX,"     r