<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import os <EOL> import datetime <EOL> try : <EOL> import spwd <EOL> except ImportError : <EOL> pass <EOL> import salt . utils <EOL> from salt . exceptions import CommandExecutionError <EOL> try : <EOL> import salt . utils . pycrypto <EOL> HAS_CRYPT = True <EOL> except ImportError : <EOL> HAS_CRYPT = False <EOL> def __virtual__ ( ) : <EOL> return __grains__ . get ( '<STR_LIT>' , '<STR_LIT>' ) == '<STR_LIT>' <EOL> def default_hash ( ) : <EOL> '''<STR_LIT>''' <EOL> return '<STR_LIT:!>' <EOL> def info ( name ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> data = spwd . getspnam ( name ) <EOL> ret = { <EOL> '<STR_LIT:name>' : data . sp_nam , <EOL> '<STR_LIT>' : data . sp_pwd , <EOL> '<STR_LIT>' : data . sp_lstchg , <EOL> '<STR_LIT>' : data . sp_min , <EOL> '<STR_LIT>' : data . sp_max , <EOL> '<STR_LIT>' : data . sp_warn , <EOL> '<STR_LIT>' : data . sp_inact , <EOL> '<STR_LIT>' : data . sp_expire } <EOL> except KeyError : <EOL> return { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> return ret <EOL> def set_inactdays ( name , inactdays ) : <EOL> '''<STR_LIT>''' <EOL> pre_info = info ( name ) <EOL> if inactdays == pre_info [ '<STR_LIT>' ] : <EOL> return True <EOL> cmd = '<STR_LIT>' . format ( inactdays , name ) <EOL> __salt__ [ '<STR_LIT>' ] ( cmd , python_shell = False ) <EOL> post_info = info ( name ) <EOL> if post_info [ '<STR_LIT>' ] != pre_info [ '<STR_LIT>' ] : <EOL> return post_info [ '<STR_LIT>' ] == inactdays <EOL> def set_maxdays ( name , maxdays ) : <EOL> '''<STR_LIT>''' <EOL> pre_info = info ( name ) <EOL> if maxdays == pre_info [ '<STR_LIT>' ] : <EOL> return True <EOL> cmd = '<STR_LIT>' . format ( maxdays , name ) <EOL> __salt__ [ '<STR_LIT>' ] ( cmd , python_shell = False ) <EOL> post_info = info ( name ) <EOL> if post_info [ '<STR_LIT>' ] != pre_info [ '<STR_LIT>' ] : <EOL> return post_info [ '<STR_LIT>' ] == maxdays <EOL> def set_mindays ( name , mindays ) : <EOL> '''<STR_LIT>''' <EOL> pre_info = info ( name ) <EOL> if mindays == pre_info [ '<STR_LIT>' ] : <EOL> return True <EOL> cmd = '<STR_LIT>' . format ( mindays , name ) <EOL> __salt__ [ '<STR_LIT>' ] ( cmd , python_shell = False ) <EOL> post_info = info ( name ) <EOL> if post_info [ '<STR_LIT>' ] != pre_info [ '<STR_LIT>' ] : <EOL> return post_info [ '<STR_LIT>' ] == mindays <EOL> return False <EOL> def gen_password ( password , crypt_salt = None , algorithm = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> if not HAS_CRYPT : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> return salt . utils . pycrypto . gen_hash ( crypt_salt , password , algorithm ) <EOL> def del_password ( name ) : <EOL> '''<STR_LIT>''' <EOL> cmd = '<STR_LIT>' . format ( name ) <EOL> __salt__ [ '<STR_LIT>' ] ( cmd , python_shell = False , output_loglevel = '<STR_LIT>' ) <EOL> uinfo = info ( name ) <EOL> return not uinfo [ '<STR_LIT>' ] <EOL> def set_password ( name , password , use_usermod = False ) : <EOL> '''<STR_LIT>''' <EOL> if not salt . utils . is_true ( use_usermod ) : <EOL> if __grains__ [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> s_file = '<STR_LIT>' . format ( name ) <EOL> else : <EOL> s_file = '<STR_LIT>' <EOL> ret = { } <EOL> if not os . path . isfile ( s_file ) : <EOL> return ret <EOL> lines = [ ] <EOL> with salt . utils . fopen ( s_file , '<STR_LIT:rb>' ) as fp_ : <EOL> for line in fp_ : <EOL> comps = line . strip ( ) . split ( '<STR_LIT::>' ) <EOL> if comps [ <NUM_LIT:0> ] != name : <EOL> lines . append ( line ) <EOL> continue <EOL> changed_date = datetime . datetime . today ( ) - datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> comps [ <NUM_LIT:1> ] = password <EOL> comps [ <NUM_LIT:2> ] = str ( changed_date . days ) <EOL> line = '<STR_LIT::>' . join ( comps ) <EOL> lines . append ( '<STR_LIT>' . format ( line ) ) <EOL> with salt . utils . fopen ( s_file , '<STR_LIT>' ) as fp_ : <EOL> fp_ . writelines ( lines ) <EOL> uinfo = info ( name ) <EOL> return uinfo [ '<STR_LIT>' ] == password <EOL> else : <EOL> cmd = '<STR_LIT>' . format ( name , password ) <EOL> __salt__ [ '<STR_LIT>' ] ( cmd , python_shell = False , output_loglevel = '<STR_LIT>' ) <EOL> uinfo = info ( name ) <EOL> return uinfo [ '<STR_LIT>' ] == password <EOL> def set_warndays ( name , warndays ) : <EOL> '''<STR_LIT>''' <EOL> pre_info = info ( name ) <EOL> if warndays == pre_info [ '<STR_LIT>' ] : <EOL> return True <EOL> cmd = '<STR_LIT>' . format ( warndays , name ) <EOL> __salt__ [ '<STR_LIT>' ] ( cmd , python_shell = False ) <EOL> post_info = info ( name ) <EOL> if post_info [ '<STR_LIT>' ] != pre_info [ '<STR_LIT>' ] : <EOL> return post_info [ '<STR_LIT>' ] == warndays <EOL> return False <EOL> def set_date ( name , date ) : <EOL> '''<STR_LIT>''' <EOL> cmd = '<STR_LIT>' . format ( date , name ) <EOL> __salt__ [ '<STR_LIT>' ] ( cmd , python_shell = False ) <EOL> def set_expire ( name , expire ) : <EOL> '''<STR_LIT>''' <EOL> cmd = '<STR_LIT>' . format ( expire , name ) <EOL> __salt__ [ '<STR_LIT>' ] ( cmd , python_shell = False ) </s>
