<s> import grp <EOL> import logging <EOL> import os <EOL> import pwd <EOL> from freenasUI . common . cmd import cmd_pipe <EOL> from freenasUI . common . freenascache import * <EOL> from freenasUI . common . system import nis_objects <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> YPCAT = "<STR_LIT>" <EOL> YPBIND = "<STR_LIT>" <EOL> DOMAINNAME = "<STR_LIT>" <EOL> FLAGS_DBINIT = <NUM_LIT> <EOL> class nis_pipe ( cmd_pipe ) : <EOL> pass <EOL> class FreeNAS_NIS_Base ( object ) : <EOL> def __keys ( self ) : <EOL> return [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> def __set_defaults ( self ) : <EOL> self . domain = None <EOL> self . servers = None <EOL> self . secure_mode = None <EOL> self . manycast = None <EOL> self . flags = <NUM_LIT:0> <EOL> def set_kwargs ( self ) : <EOL> kwargs = self . kwargs <EOL> if '<STR_LIT>' in kwargs and ( kwargs [ '<STR_LIT>' ] & FLAGS_DBINIT ) : <EOL> nis = nis_objects ( ) [ <NUM_LIT:0> ] <EOL> for key in nis . __dict__ . keys ( ) : <EOL> if not key . startswith ( "<STR_LIT>" ) : <EOL> continue <EOL> newkey = key . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if newkey in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . __dict__ [ newkey ] = False if long ( nis . __dict__ [ key ] ) == <NUM_LIT:0> else True <EOL> else : <EOL> self . __dict__ [ newkey ] = nis . __dict__ [ key ] if nis . __dict__ [ key ] else None <EOL> for key in kwargs : <EOL> if key in self . __keys ( ) : <EOL> self . __dict__ [ key ] = kwargs [ key ] <EOL> def __init__ ( self , ** kwargs ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> self . kwargs = kwargs <EOL> self . __set_defaults ( ) <EOL> super ( FreeNAS_NIS_Base , self ) . __init__ ( ) <EOL> self . set_kwargs ( ) <EOL> if self . flags & FLAGS_DBINIT : <EOL> self . ypbind = nis_pipe ( '<STR_LIT>' % ( <EOL> YPBIND , <EOL> self . domain , <EOL> self . servers , <EOL> '<STR_LIT>' if self . secure_mode else '<STR_LIT>' , <EOL> '<STR_LIT>' if self . manycast else '<STR_LIT>' , <EOL> ) ) <EOL> self . _settings = [ ] <EOL> log . debug ( "<STR_LIT>" ) <EOL> def _save ( self ) : <EOL> _s = { } <EOL> _s . update ( self . __dict__ ) <EOL> self . _settings . append ( _s ) <EOL> def _restore ( self ) : <EOL> if self . _settings : <EOL> _s = self . _settings . pop ( ) <EOL> self . __dict__ . update ( _s ) <EOL> def __ypcat ( self , args ) : <EOL> pobj = nis_pipe ( "<STR_LIT>" % ( YPCAT , args ) ) <EOL> self . _ypcerror = pobj . error <EOL> return ( pobj . returncode , str ( pobj ) ) <EOL> def __domainname ( self ) : <EOL> pobj = nis_pipe ( "<STR_LIT:%s>" % DOMAINNAME ) <EOL> self . _dnerror = pobj . error <EOL> return ( pobj . returncode , str ( pobj ) ) <EOL> def get_domain ( self ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> domain = None <EOL> ( res , out ) = self . __domainname ( ) <EOL> if res == <NUM_LIT:0> : <EOL> domain = out . strip ( ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> return domain <EOL> def get_domains ( self , domain = None ) : <EOL> return [ self . get_domain ( ) ] <EOL> def get_user ( self , who , domain = None ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> log . debug ( "<STR_LIT>" , who ) <EOL> user = None <EOL> users = self . get_users ( ) <EOL> for u in users : <EOL> if who and u [ '<STR_LIT>' ] . strip ( ) . lower ( ) == str ( who ) . strip ( ) . lower ( ) : <EOL> user = u <EOL> break <EOL> log . debug ( "<STR_LIT>" ) <EOL> return user <EOL> def get_users ( self , domain = None ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> users = [ ] <EOL> ( ypcres , ypcout ) = self . __ypcat ( "<STR_LIT>" ) <EOL> if ypcres == <NUM_LIT:0> : <EOL> for line in ypcout . splitlines ( ) : <EOL> parts = line . split ( '<STR_LIT::>' ) <EOL> if not parts or len ( parts ) < <NUM_LIT:7> : <EOL> continue <EOL> user = { <EOL> "<STR_LIT:name>" : parts [ <NUM_LIT:0> ] , <EOL> "<STR_LIT>" : parts [ <NUM_LIT:0> ] , <EOL> "<STR_LIT>" : parts [ <NUM_LIT:2> ] , <EOL> "<STR_LIT>" : parts [ <NUM_LIT:3> ] , <EOL> "<STR_LIT>" : parts [ <NUM_LIT:4> ] , <EOL> "<STR_LIT>" : parts [ <NUM_LIT:5> ] , <EOL> "<STR_LIT>" : parts [ <NUM_LIT:6> ] <EOL> } <EOL> users . append ( user ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> return users <EOL> def get_group ( self , who , domain = None ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> log . debug ( "<STR_LIT>" , who ) <EOL> group = None <EOL> groups = self . get_groups ( ) <EOL> for g in groups : <EOL> if who and g [ '<STR_LIT>' ] . strip ( ) . lower ( ) == str ( who ) . strip ( ) . lower ( ) : <EOL> group = g <EOL> break <EOL> log . debug ( "<STR_LIT>" ) <EOL> return group <EOL> def get_groups ( self , domain = None ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> groups = [ ] <EOL> ( ypcres , ypcout ) = self . __ypcat ( "<STR_LIT>" ) <EOL> if ypcres == <NUM_LIT:0> : <EOL> for line in ypcout . splitlines ( ) : <EOL> parts = line . split ( '<STR_LIT::>' ) <EOL> if not parts or len ( parts ) < <NUM_LIT:4> : <EOL> continue <EOL> group = { <EOL> "<STR_LIT:name>" : parts [ <NUM_LIT:0> ] , <EOL> "<STR_LIT>" : parts [ <NUM_LIT:0> ] , <EOL> "<STR_LIT>" : parts [ <NUM_LIT:2> ] , <EOL> "<STR_LIT>" : parts [ <NUM_LIT:3> ] <EOL> } <EOL> groups . append ( group ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> return groups <EOL> class FreeNAS_NIS ( FreeNAS_NIS_Base ) : <EOL> def __init__ ( self , ** kwargs ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> super ( FreeNAS_NIS , self ) . __init__ ( ** kwargs ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> class FreeNAS_NIS_Users ( FreeNAS_NIS ) : <EOL> def __init__ ( self , ** kwargs ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> super ( FreeNAS_NIS_Users , self ) . __init__ ( ** kwargs ) <EOL> self . __users = { } <EOL> self . __usernames = [ ] <EOL> self . __ucache = { } <EOL> self . __ducache = { } <EOL> if '<STR_LIT>' in kwargs and kwargs [ '<STR_LIT>' ] : <EOL> self . __domains = self . get_domains ( domain = kwargs [ '<STR_LIT>' ] ) <EOL> else : <EOL> self . __domains = self . get_domains ( ) <EOL> if ( <EOL> ( self . flags & FLAGS_CACHE_READ_USER ) or <EOL> ( self . flags & FLAGS_CACHE_WRITE_USER ) <EOL> ) : <EOL> for d in self . __domains : <EOL> self . __ucache [ d ] = FreeNAS_UserCache ( dir = d ) <EOL> self . __ducache [ d ] = FreeNAS_Directory_UserCache ( dir = d ) <EOL> self . __get_users ( ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> def __loaded ( self , index , domain , write = False ) : <EOL> ret = False <EOL> paths = { } <EOL> ucachedir = self . __ucache [ domain ] . cachedir <EOL> paths [ '<STR_LIT:u>' ] = os . path . join ( ucachedir , "<STR_LIT>" ) <EOL> ducachedir = self . __ducache [ domain ] . cachedir <EOL> paths [ '<STR_LIT>' ] = os . path . join ( ducachedir , "<STR_LIT>" ) <EOL> file = None <EOL> try : <EOL> file = paths [ index ] <EOL> except : <EOL> file = None <EOL> if file and write : <EOL> try : <EOL> with open ( file , '<STR_LIT>' ) as f : <EOL> f . close ( ) <EOL> ret = True <EOL> except : <EOL> ret = False <EOL> elif file : <EOL> if os . access ( file , os . F_OK ) : <EOL> ret = True <EOL> return ret <EOL> def _get_uncached_usernames ( self ) : <EOL> return self . __usernames <EOL> def __get_users ( self ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> self . __usernames = [ ] <EOL> if self . flags & FLAGS_CACHE_READ_USER : <EOL> dcount = len ( self . __domains ) <EOL> count = <NUM_LIT:0> <EOL> for d in self . __domains : <EOL> if self . __loaded ( '<STR_LIT:u>' , d ) : <EOL> self . __users [ d ] = self . __ucache [ d ] <EOL> count += <NUM_LIT:1> <EOL> if count == dcount : <EOL> log . debug ( "<STR_LIT>" ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> return <EOL> self . _save ( ) <EOL> for d in self . __domains : <EOL> self . __users [ d ] = [ ] <EOL> if ( self . flags & FLAGS_CACHE_READ_USER ) and self . __loaded ( '<STR_LIT>' , d ) : <EOL> log . debug ( <EOL> "<STR_LIT>" , <EOL> d <EOL> ) <EOL> nis_users = self . __ducache [ d ] <EOL> else : <EOL> log . debug ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> d <EOL> ) <EOL> nis_users = self . get_users ( domain = d ) <EOL> for u in nis_users : <EOL> uid = u [ '<STR_LIT>' ] <EOL> self . __usernames . append ( uid ) <EOL> if self . flags & FLAGS_CACHE_WRITE_USER : <EOL> self . __ducache [ d ] [ uid ] = u <EOL> try : <EOL> pw = pwd . getpwnam ( uid ) <EOL> except Exception , e : <EOL> log . debug ( "<STR_LIT>" , e ) <EOL> continue <EOL> self . __users [ d ] . append ( pw ) <EOL> if self . flags & FLAGS_CACHE_WRITE_USER : <EOL> self . __ucache [ d ] [ uid ] = pw <EOL> pw = None <EOL> if self . flags & FLAGS_CACHE_WRITE_USER : <EOL> self . __loaded ( '<STR_LIT:u>' , d , True ) <EOL> self . __loaded ( '<STR_LIT>' , d , True ) <EOL> self . _restore ( ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> def __len__ ( self ) : <EOL> length = <NUM_LIT:0> <EOL> for d in self . __domains : <EOL> length += len ( self . __users [ d ] ) <EOL> return length <EOL> def __iter__ ( self ) : <EOL> for d in self . __domains : <EOL> for user in self . __users [ d ] : <EOL> yield user <EOL> class FreeNAS_NIS_Groups ( FreeNAS_NIS ) : <EOL> def __init__ ( self , ** kwargs ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> super ( FreeNAS_NIS_Groups , self ) . __init__ ( ** kwargs ) <EOL> self . __groups = { } <EOL> self . __groupnames = [ ] <EOL> self . __gcache = { } <EOL> self . __dgcache = { } <EOL> if '<STR_LIT>' in kwargs and kwargs [ '<STR_LIT>' ] : <EOL> self . __domains = self . get_domains ( domain = kwargs [ '<STR_LIT>' ] ) <EOL> else : <EOL> self . __domains = self . get_domains ( ) <EOL> if ( <EOL> ( self . flags & FLAGS_CACHE_READ_GROUP ) or <EOL> ( self . flags & FLAGS_CACHE_WRITE_GROUP ) <EOL> ) : <EOL> for d in self . __domains : <EOL> self . __gcache [ d ] = FreeNAS_GroupCache ( dir = d ) <EOL> self . __dgcache [ d ] = FreeNAS_Directory_GroupCache ( dir = d ) <EOL> self . __get_groups ( ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> def __loaded ( self , index , domain = None , write = False ) : <EOL> ret = False <EOL> paths = { } <EOL> gcachedir = self . __gcache [ domain ] . cachedir <EOL> paths [ '<STR_LIT:g>' ] = os . path . join ( gcachedir , "<STR_LIT>" ) <EOL> dgcachedir = self . __dgcache [ domain ] . cachedir <EOL> paths [ '<STR_LIT>' ] = os . path . join ( dgcachedir , "<STR_LIT>" ) <EOL> file = None <EOL> try : <EOL> file = paths [ index ] <EOL> except : <EOL> file = None <EOL> if file and write : <EOL> try : <EOL> with open ( file , '<STR_LIT>' ) as f : <EOL> f . close ( ) <EOL> ret = True <EOL> except : <EOL> ret = False <EOL> elif file : <EOL> if os . access ( file , os . F_OK ) : <EOL> ret = True <EOL> return ret <EOL> def _get_uncached_groupnames ( self ) : <EOL> return self . __groupnames <EOL> def __get_groups ( self ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> self . __groupnames = [ ] <EOL> if ( self . flags & FLAGS_CACHE_READ_GROUP ) : <EOL> dcount = len ( self . __domains ) <EOL> count = <NUM_LIT:0> <EOL> for d in self . __domains : <EOL> if self . __loaded ( '<STR_LIT:u>' , d ) : <EOL> self . __groups [ d ] = self . __gcache [ d ] <EOL> count += <NUM_LIT:1> <EOL> if count == dcount : <EOL> log . debug ( "<STR_LIT>" ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> return <EOL> self . _save ( ) <EOL> for d in self . __domains : <EOL> self . __groups [ d ] = [ ] <EOL> if ( self . flags & FLAGS_CACHE_READ_GROUP ) and self . __loaded ( '<STR_LIT>' , d ) : <EOL> log . debug ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> d <EOL> ) <EOL> nis_groups = self . __dgcache [ d ] <EOL> else : <EOL> log . debug ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> d <EOL> ) <EOL> nis_groups = self . get_groups ( ) <EOL> for g in nis_groups : <EOL> group = g [ '<STR_LIT>' ] <EOL> if self . flags & FLAGS_CACHE_WRITE_GROUP : <EOL> self . __dgcache [ d ] [ group ] = g <EOL> self . __groupnames . append ( group ) <EOL> try : <EOL> gr = grp . getgrnam ( group ) <EOL> except : <EOL> continue <EOL> self . __groups [ d ] . append ( gr ) <EOL> if self . flags & FLAGS_CACHE_WRITE_GROUP : <EOL> self . __gcache [ d ] [ group ] = gr <EOL> gr = None <EOL> if self . flags & FLAGS_CACHE_WRITE_GROUP : <EOL> self . __loaded ( '<STR_LIT:g>' , d , True ) <EOL> self . __loaded ( '<STR_LIT>' , d , True ) <EOL> self . _restore ( ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> def __len__ ( self ) : <EOL> length = <NUM_LIT:0> <EOL> for d in self . __domains : <EOL> length += len ( self . __groups [ d ] ) <EOL> return length <EOL> def __iter__ ( self ) : <EOL> for d in self . __domains : <EOL> for group in self . __groups [ d ] : <EOL> yield group <EOL> class FreeNAS_NIS_User ( FreeNAS_NIS ) : <EOL> def __init__ ( self , user , ** kwargs ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> log . debug ( "<STR_LIT>" , user ) <EOL> self . _pw = None <EOL> domain = self . get_domain ( ) <EOL> if user : <EOL> user = user . encode ( '<STR_LIT:utf-8>' ) <EOL> kwargs [ '<STR_LIT>' ] = domain <EOL> super ( FreeNAS_NIS_User , self ) . __init__ ( ** kwargs ) <EOL> if ( <EOL> ( self . flags & FLAGS_CACHE_READ_USER ) or <EOL> ( self . flags & FLAGS_CACHE_WRITE_USER ) <EOL> ) : <EOL> self . __ucache = FreeNAS_UserCache ( dir = domain ) <EOL> self . __ukey = user <EOL> self . __ducache = FreeNAS_Directory_UserCache ( dir = domain ) <EOL> self . __dukey = user <EOL> self . __get_user ( user , domain ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> def __get_user ( self , user , domain ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> log . debug ( "<STR_LIT>" , user ) <EOL> log . debug ( "<STR_LIT>" , domain ) <EOL> if ( self . flags & FLAGS_CACHE_READ_USER ) and self . __ucache . has_key ( user ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> return self . __ucache [ self . __ukey ] <EOL> pw = None <EOL> if ( self . flags & FLAGS_CACHE_READ_USER ) and self . __ducache . has_key ( self . __key ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> nis_user = self . __ducache [ self . __dukey ] <EOL> else : <EOL> log . debug ( "<STR_LIT>" ) <EOL> nis_user = self . get_user ( user ) <EOL> if not nis_user : <EOL> u = user <EOL> else : <EOL> u = nis_user [ '<STR_LIT>' ] <EOL> try : <EOL> pw = pwd . getpwnam ( u ) <EOL> if ( self . flags & FLAGS_CACHE_WRITE_USER ) and pw : <EOL> self . __ucache [ self . __ukey ] = pw <EOL> self . __ducache [ self . __dukey ] = nis_user <EOL> except : <EOL> pw = None <EOL> self . _pw = pw <EOL> log . debug ( "<STR_LIT>" ) <EOL> class FreeNAS_NIS_Group ( FreeNAS_NIS ) : <EOL> def __init__ ( self , group , ** kwargs ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> log . debug ( "<STR_LIT>" , group ) <EOL> self . _gr = None <EOL> domain = self . get_domain ( ) <EOL> if group : <EOL> group = group . encode ( '<STR_LIT:utf-8>' ) <EOL> kwargs [ '<STR_LIT>' ] = domain <EOL> super ( FreeNAS_NIS_Group , self ) . __init__ ( ** kwargs ) <EOL> if ( <EOL> ( self . flags & FLAGS_CACHE_READ_GROUP ) or <EOL> ( self . flags & FLAGS_CACHE_WRITE_GROUP ) <EOL> ) : <EOL> self . __gcache = FreeNAS_GroupCache ( dir = domain ) <EOL> self . __gkey = group <EOL> self . __dgcache = FreeNAS_Directory_GroupCache ( dir = domain ) <EOL> self . __dgkey = group <EOL> self . __get_group ( group , domain ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> def __get_group ( self , group , domain ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> log . debug ( "<STR_LIT>" , group ) <EOL> log . debug ( "<STR_LIT>" , domain ) <EOL> if ( self . flags & FLAGS_CACHE_READ_GROUP ) and self . __gcache . has_key ( group ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> return self . __gcache [ self . __gkey ] <EOL> g = gr = None <EOL> if ( self . flags & FLAGS_CACHE_READ_GROUP ) and self . __dgcache . has_key ( self . __key ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> nis_group = self . __dgcache [ self . __dgkey ] <EOL> else : <EOL> log . debug ( "<STR_LIT>" ) <EOL> nis_group = self . get_group ( group ) <EOL> if not nis_group : <EOL> g = group <EOL> else : <EOL> g = nis_group [ '<STR_LIT>' ] <EOL> try : <EOL> gr = grp . getgrnam ( g ) <EOL> if ( self . flags & FLAGS_CACHE_WRITE_GROUP ) and gr : <EOL> self . __gcache [ self . __gkey ] = gr <EOL> self . __dgcache [ self . __dgkey ] = nis_group <EOL> except : <EOL> gr = None <EOL> self . _gr = gr <EOL> log . debug ( "<STR_LIT>" ) </s>
