....................................../////.===Shadow-Here===./////................................................ > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < ------------------------------------------------------------------------------------------------------------------- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// RIFF¤ WEBPVP8 ˜ ðÑ *ôô>‘HŸK¥¤"§£±¨àð enü¹%½_F‘åè¿2ºQú³íªú`N¿­3ÿƒügµJžaÿ¯ÿ°~¼ÎùnúîÞÖô•òíôÁÉß®Sm¥Ü/ ‡ó˜f£Ùà<˜„xëJ¢Ù€SO3x<ªÔ©4¿+ç¶A`q@Ì“Úñè™ÍÿJÌ´ª-˜ÆtÊÛL]Ïq*‘Ý”ì#ŸÌÏãY]@ê`¿ /ªfkØB4·®£ó z—Üw¥Pxù–ÞLШKÇN¾AkÙTf½è'‰g gÆv›Øuh~ a˜Z— ïj*á¥t d£“uÒ ¨`K˜¹ßþ]b>˜]_ÏÔ6W—è2r4x•íÖ…"ƒÖNîä!¦å Ú}ýxGøÌ —@ ;ÆÚŠ=ɾ1ý8lªË¥ô ^yf®Œ¢u&2©nÙÇ›ñÂñŒ³ aPo['½»øFùà­+4ê“$!lövlüÞ=;N®3ð‚õ›DÉKòÞ>ÄÍ ¥ˆuߤ#ˆ$6ù™¥îЇy’ÍB¼ çxÛ;X"WL£R÷͝*ó-¶Zu}º.s¸sšXqù–DþÿvªhüïwyŸ ¯é³lÀ:KCûÄ£Ëá\…­ ~—ýóî ¼ûûÜTÓüÇy…ŽÆvc»¾×U ñ¸žþоP÷¦ó:Ò¨¨5;Ð#&#ÖúñläÿÁœ GxÉ­/ñ‡áQðìYÉtÒw޼GÔ´zàÒò ð*ëzƒ•4~H]Ø‹f ñÓÈñ`NåWçs'ÆÏW^ø¹!XžµmQ5ÃËoLœÎ: ÞËÍ¥J ù…î èo£ßPÎñ¶ž8.Œ]ʵ~5›ÙË-ù*8ÙÖß±~ ©¹rÓê‚j¶d¸{^Q'˜±Crß ÚH—#¥¥QlÀ×ëã‡DÜ«èî þ&Çæžî;ŽÏºò6ÒLÃXy&ZŒ'j‚¢Ù€IßÚù+–MGi‰*jE€‘JcÜ ÓÌ EÏÚj]o˜ Þr <¾U ûŪæÍ/šÝH¥˜b”¼ ÁñßX GP›ï2›4WŠÏà×£…íÓk†¦H·ÅíMh–*nó÷à]ÁjCº€b7<ب‹¨5車bp2:Á[UªM„QŒçiNMa#<5›áËó¸HýÊ"…×Éw¹¦ì2º–x<›»a±¸3Weü®FÝ⑱ö–î–³|LPÈ~çð~Çå‡|º kD¢µÏàÆAI %1À% ¹Ò – ”ϝS¦‰4&¶£°à Öý”û_Ò Áw°A«Å€?mÇÛgHÉ/8)á¾ÛìáöŽP í¨PŸNÙµº¦‡§Ùš"ÿ«>+ªÕ`Ê÷‡‚ß Õû˜þãÇ-PÍ.¾XV‘€ dÜ"þ4¹ ±Oú‘©t¥¦FªÄÃÄ•b‚znýu½—#cDs˜ÃiÑOˆñ×QO=*IAÊ,¶ŽZƒ;‡wøXè%EÐk:F±Ú” .Ѽ+Áu&Ç`."pÈÉw o&¿dE6‘’EqTuK@Ì¥ã™À(Êk(h‰,H}RÀIXÛš3µ1©_OqÚÒJAñ$ÊÙÜ;D3çŒ[þùœh¬Ã³™ö6ç†NY".Ú‰ï[ªŸŒ '²Ð öø_¨ÂÉ9ué¶³ÒŠõTàîMØ#û¯gN‡bÙ놚X„ö …ÉeüÌ^J ‹€.œ$Æ)βÄeæW#óüßĺŸ€ ÀzwV 9oä»f4V*uB «Ë†¹ì¯žR霓æHXa=&“I4K;¯ç‹h×·"UŠ~<•╪Vêª&ÍSÃÆÅ?ÔqÎ*mTM ˜›µwêd#[C¡©§‘D<©àb†–ÁœøvH/,í:¯( ²£|4-„Æövv„Yͼ™^Á$ˆ„¢Û[6yB.åH*V¨æ?$=˜Ñ€•ñ·­(VlŸ‘ nÀt8W÷´Bûba?q9ú¶Xƒl«ÿ\ù¶’þòUÐj/õ¢Ìµ³g$ƒÎR!¸»|Oߍë’BhîÚÑ¢ñåŒJ„®„£2Ð3•ô02Nt…!£Í]Ïc½Qÿ?ˆ<&ÃA¾Ú,JˆijÌ#5yz„‰Î|ÊŽ5QÏ:‹ÐaóVÔxW—CpeÏzÐïíçôÿÅ_[hãsÐ_/ŽTÝ?BîˆííV$<¿i>²F¬_Eß¿ †bÊŒº­ÿ®Z H“C}”¬,Mp ý/Bá£w>˜YV°aƒúh+cŠ- r/[%|üUMHäQ°X»|û/@|°¥Ð !BÔ Ç¢Ä©š+Õì D«7ìN¶ŽðÔ " ƶ’ÖçtA‰Û×}{tþz­¾GÍ›k¹OEJR$ Â׃ «ëÁ"oÉôž$oUK(Ä)Ãz³Ê-‹êN[Ò3Œñbï8P 4ƒ×q¢bo|?<ÛX¬òÄͰL–±›(™ûG?ýË©ÚÄ–ÂDØÐ_Ç¡ô ¾–ÄÏø ×e8Ë©$ÄF¹Å‹ì[©óìl:F¾f´‹‹Xì²ï®\¬ôùƒ ÿat¥óèÒùHß0äe‚;ü×h:ÆWðHž=Ã8骣"kœ'Y?³}Tûè€>?0l›e1Lòñ„aæKÆw…hÖŠùW…ÈÆÄ0ši·›[pcwËþñiêíY/~-Á5˜!¿†A›™Mÿþ(±“t@â“ö2­´TG5yé]çå僳 .·ÍïçÝ7UÚ±Ð/Nè»,_Ï ùdj7\ï Wì4›„»c¸àešg#ÒÊ⥭áØo5‘?ÌdÝô¯ ¹kzsƒ=´#ëÉK›Ø´±-¥eW?‡çßtòTã…$Ý+qÿ±ƒ÷_3Ô¥í÷:æ–ž<·Ö‡‰Å¢ š‡%Ô—utÌÈìðžgÖÀz²À—ï÷Óîäõ{K'´È÷³yaÏÁjƒô}ž§®æÊydÕÈë5¯èˆõvÕ©ã*çD„ “z„Ó‡^^xÂ3M§A´JG‚öï 3W'ˆ.OvXè¡ÊÕª?5º7†˜(˜Ç¶#çê’¶!ÌdZK§æ 0fãaN]òY³RV ™î$®K2R¨`W!1Ôó\;Ý ýB%qæK•&ÓÈe9È0êI±žeŸß -ú@žQr¦ ö4»M¼Áè¹µmw 9 EÆE_°2ó„ŸXKWÁ×Hóì^´²GѝF©óäR†¦‰ç"V»eØ<3ùd3ÿÚ¤Žú“Gi" —‘_ÙËÎ~Üö¯¥½Î»üŸEÚŽåmÞþí ;ÞólËΦMzA"Âf(´òá;Éï(/7½ûñÌ­cïÕçлþÝz¾-ÍvÑ“pH­–ðÓj$¸Äû¤‚‘ãUBË-n“2åPkS5&‹Â|+g^œ®Ì͆d!OïäîU«c;{Û!ÅŽ«ëZ9Ókóˆ]¯ƒ›né `ÇÒ+tÆš (ØKá¾—=3œ®•vuMñg²\ï Ec€ 05±d™‡×iÇ×›UúvÌ¢£Èþ¡ÕØô¶ßÎA"ß±#Ö²ˆÊŸ¦*Ä~ij|àø.-¼'»Ú¥£h ofº¦‡VsR=N½„Î v˜Z*SÌ{=jÑB‹tê…;’HžH¯8–îDù8ñ¢|Q•bÛçš–‹m³“ê¨ åÏ^m¬Žãþ©ïêO‡½6] µÆ„Ooòü ²x}N¦Ë3ïé¿»€›HA˜m%çÞ/¿í7Fø“‹léUk)É°Œµ8Q8›:ÀŠeT*šõ~ôڝG6 ¢}`ùH­–”¡k ‰P1>š†®9z11!X wKfmÁ¦xÑ,N1Q”–æB¶M…ÒÃv6SMˆhU¬ÊPŽï‘öj=·CŒ¯u¹ƒVIЃsx4’ömÛýcå¡¶7ßŠß 57^\wÒÐÆ k§h,Œý î«q^R½3]J¸ÇðN ‚çU¬ôº^Áì} ³f©Õœ§ˆã:FÄÈ‚é(€™?àýÓüè1Gô£¼éj‚OÅñ  #>×—ßtà 0G¥Åa뀐kßhc™À_ÉñÞ#±)GD" YîäË-ÿÙ̪ ¹™a¯´¢E\ÝÒö‚;™„ë]_ p8‰o¡ñ+^÷ 3‘'dT4œŽ ðVë½° :¬víÑ«£tßÚS-3¶“þ2 †üüʨòrš¹M{É_¤`Û¨0ìjœøJ‡:÷ÃáZ˜†@GP&œÑDGÏs¡þ¦þDGú‘1Yá9Ôþ¼ ûø…§÷8&–ÜÑnÄ_m®^üÆ`;ÉVÁJ£?â€-ßê}suÍ2sõA NÌúA磸‘îÿÚ»ƒìö·á¿±tÑÐ"Tÿü˜[@/äj¬€uüªìù¥Ý˜á8Ý´sõj 8@rˆð äþZÇD®ÿUÏ2ùôõrBzÆÏÞž>Ì™xœ“ wiÎ×7_… ¸ \#€MɁV¶¥üÕÿPÔ9Z‡ø§É8#H:ƒ5ÀÝå9ÍIŒ5åKÙŠ÷qÄ>1AÈøžj"µÂд/ªnÀ qªã}"iŸBå˜ÓÛŽ¦…&ݧ;G@—³b¯“•"´4í¨ôM¨åñC‹ïùÉó¯ÓsSH2Ý@ßáM‡ˆKÀªÛUeø/4\gnm¥‹ŸŒ qÄ b9ÞwÒNÏ_4Ég³ú=܆‚´ •â¥õeíþkjz>éÚyU«Íӝ݃6"8/ø{=Ô¢»G¥ äUw°W«,ô—¿ãㆅү¢³xŠUû™yŒ (øSópÐ 9\åTâ»—*oG$/×ÍT†Y¿1¤Þ¢_‡ ¼ „±ÍçèSaÓ 3ÛMÁBkxs‰’R/¡¤ˆÙçª(*õ„üXÌ´ƒ E§´¬EF"Ù”R/ÐNyÆÂ^°?™6¡œïJ·±$§?º>ÖüœcNÌù¯G ‹ñ2ЁBB„^·úìaz¨k:#¨Æ¨8LÎõލ£^§S&cŒÐU€ü(‡F±Š¼&P>8ÙÁ ‰ p5?0ÊÆƒZl¸aô š¼¡}gÿ¶zÆC²¹¬ÎÖG*HB¡O<º2#ñŒAƒ–¡B˜´É$¥›É:FÀÔx¾u?XÜÏÓvN©RS{2ʈãk9rmP¼Qq̳ è¼ÐFׄ^¡Öì fE“F4A…!ì/…¦Lƒ… … $%´¾yã@CI¬ á—3PþBÏNÿ<ý°4Ü ËÃ#ØÍ~âW«rEñw‹eùMMHß²`¬Öó½íf³:‹k˜¯÷}Z!ã¿<¥,\#öµÀ¯aÒNÆIé,Ћ–lŽ#Àæ9ÀÒS·I’½-Ïp Äz¤Š Â* ­íÄ9­< h>׍3ZkËU¹§˜ŒŠ±f­’¤º³Q ÏB?‹#µíÃ¥®@(Gs«†vI¥Mµ‹Á©e~2ú³ÁP4ìÕi‚²Ê^ö@-DþÓàlÜOÍ]n"µã:žpsŽ¢:! Aõ.ç~ÓBûH÷JCÌ]õVƒd «ú´QÙEA–¯¯Œ!.ˆˆëQ±ù œ·Ì!Õâ )ùL„ÅÀlÚè5@B…o´Æ¸XÓ&Û…O«˜”_#‡ƒ„ûÈt!¤ÁÏ›ÎÝŠ?c9 â\>lÓÁVÄÑ™£eØY]:fÝ–—ù+p{™ðè û³”g±OƒÚSù£áÁÊ„ä,ï7š²G ÕÌBk)~ÑiCµ|h#u¤¶îK¨² #²vݯGãeÖ϶ú…¾múÀ¶þÔñ‚Š9'^($¤§ò “š½{éúp÷J›ušS¹áªCÂubÃH9™D™/ZöØÁ‡¦ÝÙŸ·kð*_”.C‹{áXó€‡c¡c€§/šò/&éš÷,àéJþ‰X›fµ“C¨œ®r¬"kL‰Â_q…Z–.ÉL~O µ›zn‚¹À¦Öª7\àHµšÖ %»ÇníV[¥*Õ;ƒ#½¾HK-ÖIÊdÏEÚ#=o÷Óò³´Š: Ç?{¾+9›–‘OEáU·S€˜j"ÄaÜ ŒÛWt› á–c#a»pÔZÞdŽtWê=9éöÊ¢µ~ ë ;Öe‡Œ®:bî3±ýê¢wà¼îpêñ¹¾4 zc¾ðÖÿzdêŒÑÒŝÀ‰s6¤í³ÎÙB¿OZ”+F¤á‡3@Ñëäg©·Ž ˆèª<ù@É{&S„œÕúÀA)‰h:YÀ5^ÂÓŒ°õäU\ ùËÍû#²?Xe¬tu‰^zÒÔãë¼ÛWtEtû …‚g¶Úüâî*moGè¨7%u!]PhÏd™Ý%Îx: VÒ¦ôÊD3ÀŽKÛËãvÆî…N¯ä>Eró–ð`5 Œ%u5XkñÌ*NU%¶áœÊ:Qÿú»“úzyÏ6å-၇¾ ´ ÒÊ]y žO‘w2Äøæ…H’²f±ÎÇ.ª|¥'gîV•Ü .̘¯€šòü¤U~Ù†*¢!?ò wý,}´°ÔÞnïoKq5µb!áÓ3"vAßH¡³¡·G(ÐÎ0Îò¼MG!/ài®@—¬04*`…«é8ªøøló“ˆÊ”èù¤…ßÊoÿé'ËuÌÖ5×È¡§ˆˆfŽë9}hìâ_!!¯  B&Ëö¶‰ÀAÙNVŸ Wh›¸®XÑJì¨ú“¿÷3uj²˜¨ÍÎìë±aúŠÝå¯ð*Ó¨ôJ“yºØ)m°WýOè68†ŸÏ2—‰Ïüꪫٚ¥‹l1 ø ÏÄFjêµvÌbü¦èÝx:X±¢H=MÐß—,ˆÉÇ´(9ú¾^ÅÚ4¿m‡$âX‘å%(AlZo@½¨UOÌÕ”1ø¸jÎÀÃÃ_ µ‘Ü.œº¦Ut: Æï’!=¯uwû#,“pþÇúŒø(é@?³ü¥‘Mo §—s@Œ#)§ŒùkL}NOÆêA›¸~r½¼ÙA—HJ«eˆÖ´*¡ÓpÌŸö.m<-"³ûÈ$¬_6­åf£ïÚâj1y§ÕJ½@dÞÁr&Í\Z%D£Íñ·AZ Û³øüd/ªAi†/Й~  ‡âĮҮÏh§°b—›Û«mJžòG'[ÈYýŒ¦9psl ýÁ ®±f¦x,‰½tN ‚Xª9 ÙÖH.«Lo0×?͹m¡å†Ѽ+›2ƒF ±Ê8 7Hցϓ²Æ–m9…òŸï]Â1äN†VLâCˆU .ÿ‰Ts +ÅÎx(%¦u]6AF Š ØF鈄‘ |¢¶c±soŒ/t[a¾–û:s·`i햍ê›ËchÈ…8ßÀUÜewŒðNOƒõD%q#éû\9¤x¹&UE×G¥ Í—™$ð E6-‡¼!ýpãÔM˜ Âsìe¯ñµK¢Ç¡ùôléœ4Ö£”À Š®Ðc ^¨À}ÙËŸ§›ºê{ÊuÉC ×Sr€¤’fÉ*j!úÓ’Gsùìoîßîn%ò· àc Wp÷$¨˜)û»H ×8ŽÒ€Zj¤3ÀÙºY'Ql¦py{-6íÔCeiØp‘‡XÊîÆUߢ܂ž£Xé¼Y8þ©ëgñß}é.ÎógÒ„ÃØËø¯»™§Xýy M%@NŠ À(~áÐvu7&•,Ù˜ó€uP‡^^®=_E„jt’ 403WebShell
403Webshell
Server IP : 198.54.126.4  /  Your IP : 216.73.216.178
Web Server : Apache
System : Linux host55.registrar-servers.com 4.18.0-513.18.1.lve.2.el8.x86_64 #1 SMP Sat Mar 30 15:36:11 UTC 2024 x86_64
User : aeaw ( 7508)
PHP Version : 8.1.33
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : OFF  |  Pkexec : OFF
Directory :  /opt/imunify360/venv/lib64/python3.11/site-packages/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /opt/imunify360/venv/lib64/python3.11/site-packages//secureio.py
# coding=utf-8

# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2018 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT

# This module provides functions for secure I/O and filesystem operations

import grp
import sys
import tempfile
import contextlib
import os
from functools import lru_cache
from typing import TYPE_CHECKING
from ctypes import (cdll, c_long, Structure, c_ushort, c_ubyte, c_char, POINTER,
                    c_int, c_void_p, c_char_p)

from clcommon import ClPwd
from clcommon.clpwd import drop_user_privileges


def __getattr__(name):
    # NOTE(vlebedev): Delay shared libraries loading until they are actually used.
    #                 It makes module loadable even in case those shared libraries are not available
    #                 and that is useful for e.g. unit testing on local non-CL system.
    if name == 'libc':
        return _load_libc()
    elif name == 'liblve':
        return _load_liblve()
    raise AttributeError(f"module {__name__} has no attribute {name}")


def __dir__():
    return ['libc', 'liblve', *globals().keys()]


# --- libc functions -----------------------------------------------
@lru_cache(maxsize=None)
def _load_libc():
    libc = cdll.LoadLibrary("libc.so.6")

    libc.fchown.argtypes = [c_int, c_int, c_int]
    libc.fchown.restype = c_int

    libc.fchmod.argtypes = [c_int, c_int]
    libc.fchmod.restype = c_int

    # accepts file/dir descriptor (integer)
    libc.fdopendir.argtypes = [c_int]
    # returns pointer to DIR structure
    libc.fdopendir.restype = c_void_p

    # accepts pointer to DIR structure
    libc.readdir.argtypes = [c_void_p]
    # returns pointer to DIRENTRY structure
    libc.readdir.restype = DIRENTRY_P

    # accepts pointer to DIR structure
    libc.rewinddir.argtypes = [c_void_p]
    # returns void
    libc.rewinddir.restype = None

    # accepts pointer to DIR structure
    libc.closedir.argtypes = [c_void_p]
    libc.closedir.restype = c_int

    return libc


if TYPE_CHECKING:
    libc = _load_libc()


ino_t = c_long
off_t = c_long


class DIRENTRY(Structure):
    _fields_ = [
        ('d_ino', ino_t),  # inode number
        ('d_off', off_t),  # offset to the next dirent
        ('d_reclen', c_ushort),  # length of this record
        ('d_type', c_ubyte),  # type of file; not supported by all file system types
        ('d_name', c_char * 256),  # filename
    ]


DIRENTRY_P = POINTER(DIRENTRY)


def fchown(fd, uid, gid):
    return _load_libc().fchown(fd, uid, gid)


def fchmod(fd, mode):
    return _load_libc().fchmod(fd, mode)


def fdopen(fd):
    return _load_libc().fdopen(fd)


def readdir(dirp):
    return _load_libc().readdir(dirp)


def rewinddir(dirp):
    return _load_libc().rewinddir(dirp)


def closedir(dirp):
    return _load_libc().closedir(dirp)


class StubLVE:
    """Stub implementation of LVE functions for systems without liblve"""

    def open_not_symlink(self, path, parent_path):
        return -1

    def check_dir(self, fd):
        return -1

    def isdir(self, path, descriptor, parent_path):
        return -1

    def set_perm_dir_secure(self, path, perm, fd, parent_path):
        return -1

    def set_owner_dir_secure(self, path, uid, gid, fd, parent_path):
        return -1

    def create_dir_secure(self, path, perm, uid, gid, fd, parent_path):
        return -1

    def makedirs_secure(self, path, perm, uid, gid, parent_path):
        return -1

    def get_path_from_descriptor(self, fd, buf):
        return None

    def is_subdir(self, dir, subdir):
        return 0

    def enable_quota_capability(self):
        return 0

    def disable_quota_capability(self):
        return 0


# --- liblve functions -----------------------------------------------
@lru_cache(maxsize=None)
def _load_liblve():
    """Load liblve if available, otherwise return stub implementation"""
    try:
        liblve = cdll.LoadLibrary("libsecureio.so.0")
    except OSError:
        try:
            liblve = cdll.LoadLibrary("liblve.so.0")
        except OSError:
            return StubLVE()

    # Opens path for reading not following symlinks and verifies that opened path is inside parent_path
    # Returns:
    # descriptor if successful
    # -1 if path does not exist or is a symlink
    # -2 if opened path is NOT inside parent_path or cannot be determined
    # accepts path, parent_path
    liblve.open_not_symlink.argtypes = [c_char_p, c_char_p]
    liblve.open_not_symlink.restype = c_int

    # Closes descriptor (if it is not equal -1)
    # accepts file/dir descriptor (integer)
    liblve.closefd.argtypes = [c_int]
    # returns void
    liblve.closefd.restype = None

    # Tries to read first directory entry in order to ensure that descriptor is valid
    # Returns 0 if reading succeeded or -1 if error has occured
    # accepts descriptor
    liblve.check_dir.argtypes = [c_int]
    liblve.check_dir.restype = c_int

    # Checks if path is a directory (in secure manner)
    # Also opens path (if descriptor fd == -1) and then checks that opened path is inside parent_path
    # Returns descriptor if path refers to directory
    # Returns -1 if path does not exist or is not a directory
    # Returns -2 if opened path is NOT inside parent_path or cannot be determined
    # accepts path, descriptor, parent_path
    liblve.isdir.argtypes = [c_char_p, c_int, c_char_p]
    liblve.isdir.restype = c_int

    # Sets permissions to directory (in secure manner)
    # Returns descriptor if successful
    # Returns -1 if error has occured
    # Returns -2 if opened path is NOT inside parent_path or cannot be determined
    # accepts: const char *path, mode_t perm, int fd, const char *parent_path
    liblve.set_perm_dir_secure.argtypes = [c_char_p, c_int, c_int, c_char_p]
    liblve.set_perm_dir_secure.restype = c_int

    # Sets owner and group of directory (in secure manner)
    # Returns descriptor if successful
    # Returns -1 if error has occured
    # Returns -2 if opened path is NOT inside parent_path or cannot be determined
    # accepts: const char *path, uid_t uid, gid_t gid, int fd, const char *parent_path
    liblve.set_owner_dir_secure.argtypes = [c_char_p, c_int, c_int, c_int, c_char_p]
    liblve.set_owner_dir_secure.restype = c_int

    # Creates directory if it does not exist, sets permissions/owner otherwise
    # Returns descriptor if successful
    # Returns -1 if error has occured
    # accepts: const char *path, mode_t perm, uid_t uid, gid_t gid, int fd, const char *parent_path
    liblve.create_dir_secure.argtypes = [c_char_p, c_int, c_int, c_int, c_int, c_char_p]
    liblve.create_dir_secure.restype = c_int

    # Recursive directory creation function
    # Returns 0 if successful
    # Returns -1 if error has occured
    # accepts: const char *path, mode_t perm, uid_t uid, gid_t gid, const char *parent_path
    liblve.makedirs_secure.argtypes = [c_char_p, c_int, c_int, c_int, c_char_p]
    liblve.makedirs_secure.restype = c_int

    # Writes absolute path pointed by descriptor fd to buffer *buf
    # Returns buf if successful
    # Returns NULL if error has occured
    liblve.get_path_from_descriptor.argtypes = [c_int, c_char_p]
    liblve.get_path_from_descriptor.restype = c_char_p

    # Returns 1 if subdir is subdirectory of dir, 0 otherwise
    liblve.is_subdir.argtypes = [c_char_p, c_char_p]
    liblve.is_subdir.restype = c_int

    return liblve


if TYPE_CHECKING:
    liblve = _load_liblve()


def _open_not_symlink(path, parent_path):
    return _load_liblve().open_not_symlink(path, parent_path)


def check_dir(fd):
    return _load_liblve().check_dir(fd)


def isdir(path, descriptor, parent_path):
    return _load_liblve().isdir(path, descriptor, parent_path)


def get_path_from_descriptor(fd, buf):
    return _load_liblve().get_path_from_descriptor(fd, buf)


def is_subdir(dir, subdir):
    return _load_liblve().is_subdir(dir, subdir)


# True :  euid/egid == 0/0
# False : euid/egid == user/user
# set by set_user_perm() and set_root_perm() functions
root_flag = True

LOGFILE = "/var/log/cagefs-update.log"

MIN_UID = 500

SILENT_FLAG = False


def open_not_symlink(path):
    return os.open(path, os.O_RDONLY | os.O_NOFOLLOW)


def open_file_not_symlink(path):
    return os.fdopen(open_not_symlink(path), 'r')


def flistdir(fd):
    """Returns list of entries of directory pointed by descriptor"""
    # Duplicate descriptor, because closedir() closes descriptor associated with directory stream
    fd2 = os.dup(fd)

    # Open directory stream
    dirp = fdopendir(fd2)  # NOQA
    if not dirp:
        raise RuntimeError("fdopendir error")

    # Reset position of directory stream
    # (so it will be possible to read content of directory multiple times
    # via other descriptors that refer to the directory)
    rewinddir(dirp)

    dirlist = []
    while True:
        entryp = readdir(dirp)
        if not entryp:
            break
        entry = entryp.contents
        dirlist.append(entry.d_name)

    rewinddir(dirp)
    closedir(dirp)
    return dirlist


def closefd(fd):
    if fd is not None:
        try:
            os.close(fd)
        except OSError:
            pass


def set_perm_dir_secure(path, perm, parent_path, fd=None, logger=None):
    """Sets permissions to directory (in secure manner)
    Returns descriptor if successful
    Returns None if error has occured"""
    if fd is None:
        fd = -1
    fd = _load_liblve().set_perm_dir_secure(path.encode(), perm, fd, parent_path.encode())
    if fd > 0:
        return fd
    if logger is not None:
        logger('Error: failed to set permissions of directory ' + path, False, True)
    return None


def set_owner_dir_secure(path, uid, gid, parent_path, fd=None, logger=None):
    """Sets owner and group of directory (in secure manner)
    Returns descriptor if successful
    Returns None if error has occured"""
    if fd is None:
        fd = -1
    fd = _load_liblve().set_owner_dir_secure(path.encode(), uid, gid, fd, parent_path.encode())
    if fd > 0:
        return fd
    if logger is not None:
        logger('Error: failed to set owner of directory ' + path, False, True)
    return None


def create_dir_secure(path, perm, uid, gid, parent_path, fd=None, logger=None):
    """Creates directory if it does not exist, sets permissions/owner otherwise
    Returns descriptor if successful
    Returns None if error has occured"""
    if fd is None:
        fd = -1
    fd = _load_liblve().create_dir_secure(path.encode(), perm, uid, gid, fd, parent_path.encode())
    if fd > 0:
        return fd
    if logger is not None:
        logger('Error : failed to create directory ' + path, False, True)
    return None


def makedirs_secure(path, perm, uid, gid, parent_path, logger=None):
    """Recursive directory creation function
    Returns 0 if successful
    Returns -1 if error has occured"""
    res = _load_liblve().makedirs_secure(path.encode(), perm, uid, gid, parent_path.encode())
    if res and logger:
        logger('Error : failed to create directory ' + path, False, True)
    return res


def read_file_secure(filename, uid=None, gid=None, exit_on_error=True, write_log=True):
    """read file not following symlinks"""
    if (uid is None and gid is not None) or (uid is not None and gid is None):
        raise RuntimeError("read_file_secure: uid and gid should be both null or be both not null")

    drop_perm = (uid is not None) and (gid is not None)
    if drop_perm:
        set_user_perm(uid, gid)

    try:
        file_object = open_file_not_symlink(filename)
        content = file_object.readlines()
        file_object.close()
        if drop_perm:
            set_root_perm()
        return content
    except (OSError, IOError) as e:
        if drop_perm:
            set_root_perm()
        logging('Error: failed to read ' + filename + ' : ' + str(e), SILENT_FLAG, 1, write_log)
        if not exit_on_error:
            raise
        sys.exit(1)


def write_file_secure(content, ini_path, uid, gid, drop_perm=True, perm=0o644, write_log=True):
    """Returns True if error has occured"""
    dirpath = os.path.dirname(ini_path)

    if drop_perm:
        set_user_perm(uid, gid)

    fd = None
    temp_path = None
    try:
        fd, temp_path = tempfile.mkstemp(prefix='cagefs_', dir=dirpath)
        file_object = os.fdopen(fd, 'w')
        file_object.write(''.join(content))
        if not drop_perm and uid is not None and gid is not None:
            if fchown(fd, uid, gid):
                raise OSError('fchown failed')
        if fchmod(fd, perm):
            raise OSError('fchmod failed')
        file_object.close()
    except (IOError, OSError) as e:
        try:
            file_object.close()
        except Exception:
            pass
        try:
            os.close(fd)
        except Exception:
            pass
        try:
            os.unlink(temp_path)
        except Exception:
            pass
        if drop_perm:
            set_root_perm()
        logging(
            f"Error: failed to write file {ini_path} : {str(e).replace('Errno', 'Err code')}",
            SILENT_FLAG,
            1,
            write_log,
        )
        return True
    except Exception as e:
        logging(f'Error: {str(e)}', SILENT_FLAG, 1)
        sys.exit(1)

    error = False
    try:
        os.rename(temp_path, ini_path)
    except OSError as e:
        error = True
        logging('Error: failed to rename tempfile to ' + ini_path + ' : ' + str(e), SILENT_FLAG, 1, write_log)
        try:
            os.unlink(temp_path)
        except OSError:
            pass

    if drop_perm:
        set_root_perm()

    return error


def write_file_via_tempfile(
        content, dest_path, perm, prefix='', suffix='', as_user=None):
    """
    Safely write string content to a file
    :param content: str
    :param dest_path: str -> path to a file
    :param perm: int -> permissions for the file
    :param prefix: str -> add to temporary file name
    :param suffix: str -> add to temporary file name
    :param as_user: str -> name of the user to drop privileges to
    """
    if as_user is not None:
        old_groups = os.getgroups()
        drop_user_privileges(as_user, effective_or_real=True, set_env=False)

    dirpath = os.path.dirname(dest_path)
    fd, temp_path = None, None
    try:
        fd, temp_path = tempfile.mkstemp(
                prefix=prefix, suffix=suffix, dir=dirpath)
        with os.fdopen(fd, 'w', errors='surrogateescape') as f_temp:
            f_temp.write(content)
    except (IOError, OSError):
        if fd is None or temp_path is None:
            raise
        try:
            os.close(fd)
        except (IOError, OSError):
            pass
        try:
            os.unlink(temp_path)
        except (IOError, OSError):
            pass
        raise

    try:
        os.chmod(temp_path, perm)
        os.rename(temp_path, dest_path)
    except (OSError, IOError, TypeError):
        try:
            os.unlink(temp_path)
        except (OSError, IOError):
            pass
        raise

    if as_user is not None:
        ruid = os.getuid()
        os.seteuid(ruid)
        os.setegid(os.getgid())
        # All of the above can be called from user named as_user
        if ruid == 0:
            os.setgroups(old_groups)


def set_user_perm(uid, gid, exit=True):
    global root_flag

    try:
        os.setegid(gid)
    except (OSError,) as e:
        if exit:
            print_error('failed to set egid to ' + str(gid) + ': ' + str(e))
            sys.exit(1)
        else:
            return -1

    groups = get_groups(uid, gid)
    try:
        os.setgroups(groups)
    except (OSError,) as e:
        if exit:
            print_error('failed to set supplementary groups to :', groups, str(e))
            sys.exit(1)
        else:
            return -1

    try:
        os.seteuid(uid)
    except (OSError,) as e:
        if exit:
            print_error('failed to set euid to ' + str(uid) + ': ' + str(e))
            sys.exit(1)
        else:
            return -1

    if uid == 0:
        root_flag = True
    else:
        # If it's possible, switch on CAP_SYS_RESOURCE
        _load_liblve().enable_quota_capability()
        root_flag = False


def set_root_perm(exit=True):
    global root_flag

    try:
        os.seteuid(0)
    except (OSError,) as e:
        if exit:
            print_error('failed to set euid to 0 :', str(e))
            sys.exit(1)
        else:
            return -1

    try:
        os.setegid(0)
    except (OSError,) as e:
        if exit:
            print_error('Error: failed to set egid to 0 :', str(e))
            sys.exit(1)
        else:
            return -1

    groups = get_groups(0, 0)
    try:
        os.setgroups(groups)
    except (OSError,) as e:
        if exit:
            print_error('Error: failed to set supplementary groups to :', groups, str(e))
            sys.exit(1)
        else:
            return -1

    root_flag = True


def print_error(*args):
    print("Error:", end=' ', file=sys.stderr)
    for a in args:
        print(a, end=' ', file=sys.stderr)
    print(file=sys.stderr)


def get_groups(uid, gid):
    """Returns supplementary groups for uid"""
    gr = get_grp_dict()
    pw = get_pwd_dict()
    groups = set()
    for group in gr:
        members = gr[group].gr_mem
        for user in members:
            try:
                member_uid = pw[user].pw_uid
            except KeyError:
                continue
            if member_uid == uid:
                groups.add(gr[group].gr_gid)
    groups.add(gid)
    return list(groups)


grp_dict = None


def get_grp_dict():
    global grp_dict
    if grp_dict is None:
        grp_dict = {}
        gr = grp.getgrall()
        for line in gr:
            grp_dict[line.gr_name] = line
    return grp_dict


clpwd = ClPwd(min_uid=MIN_UID)


def get_pwd_dict():
    return clpwd.get_user_dict()



log_file = None

def logging(msg, silent=False, verbose=True, write_log=True):
    global log_file
    if not silent:
        if verbose:
            print(msg)

    if write_log:
        root_flag_saved = root_flag
        if not root_flag:
            uid, gid = get_perm()
            set_root_perm()
        try:
            if log_file is None:
                umask_saved = os.umask(0o22)
                # log_file is opened in "line buffered" mode
                log_file = open(LOGFILE, 'w', 1)  # NOQA
                os.umask(umask_saved)
            log_file.write(msg)
            log_file.write("\n")
        except (OSError, IOError) as e:
            print_error("writing to ", LOGFILE, str(e))
            sys.exit(1)
        if not root_flag_saved:
            set_user_perm(uid, gid)


def get_perm():
    try:
        uid = os.geteuid()
        gid = os.getegid()
    except (OSError,) as e:
        print_error('failed to get (euid,egid)', str(e))
        sys.exit(1)
    return uid, gid


def set_capability(clear=False):
    """
    Set CAP_SYS_RESOURCE capability

    :param bool clear: Set on if it's true, set off otherwise
    :return: 0 for success, -1 otherwise
    :rtype: int
    """
    return _load_liblve().disable_quota_capability() if clear \
        else _load_liblve().enable_quota_capability()


def change_uid(uid):
    """
    Change effective uid of current process and set CAP_SYS_RESOURCE capbality
    to prevent "Disk quota exceeded" error

    :param int euid: User ID to set it as current effective UID
    :return: 0 if capability was set successfuly, -1 otherwise
    :rtype: int
    """
    os.seteuid(uid)
    return set_capability()


def _set_quota_checks_status(enabled):
    """
    Disable quota kernel check to allow us to write
    more than user can by quota.
    """
    if not enabled:
        _load_liblve().enable_quota_capability()
    else:
        _load_liblve().disable_quota_capability()


@contextlib.contextmanager
def disable_quota():
    _set_quota_checks_status(enabled=False)
    try:
        yield
    finally:
        _set_quota_checks_status(enabled=True)


@contextlib.contextmanager
def set_umask(umask_value):
    saved_umask = os.umask(umask_value)
    try:
        yield
    finally:
        os.umask(saved_umask)

Youez - 2016 - github.com/yon3zu
LinuXploit