#!/usr/bin/env python
import subprocess
import os
class GetUserInfo:
def __init__(self):
#self.common_share=[("ATREIDES","CARPETA_MOLONA","~/CARPETA_MOLONA"),("JUPITER","CARPETA_ADSASDA","~/CARPETA_ADSASDA")]
self.common_share=[]
self.user_info=self.get_user_info()
self.user_share=self.get_windows_home_directory()
self.mount_volume_skel=''
ret=self.generate_xml()
#def init
def get_user_info(self):
user=os.environ["PAM_USER"]
p=subprocess.Popen(["getent passwd | grep -i %s"%user],shell=True,stdout=subprocess.PIPE)
tmp=p.communicate()[0].split(":")
return (tmp[2],tmp[3],tmp[5])
#def get_user_info
def get_custom_volumes(self):
home=self.user_info[2]
uid=self.user_info[0]
gid=self.user_info[1]
file_path=home+"/.pam_custom_volumes"
ret=""
if os.path.exists(file_path):
f=open(file_path)
lines=f.readlines()
f.close()
for line in lines:
server,path,mountpoint=line.split(",")
mountpoint=mountpoint.strip("\n")
ret+="\t"+self.mount_volume_skel%(server,path,"~/"+mountpoint)+"\n"
path=home+"/"+mountpoint
if not os.path.exists(path):
os.mkdir(path)
os.system("chown %s:%s %s"%(uid,gid,path))
return ret
return None
#def get_custom_volumes
def generate_xml(self):
first='\n\
\n'
last='\n'
uid,gid,user_home=self.get_user_info()
ret=first
for item in self.common_share:
server,path,mount_point=item
ret+="\t"
ret+=self.mount_volume_skel%(server,path,mount_point)
ret+="\n"
if self.user_share!=None:
ret+="\t"
tmp=self.user_share.split("/")
server=tmp[2]
path=tmp[3]
user_path=user_home+"/"+path
if not os.path.exists(user_path):
os.mkdir(user_path)
os.system("chown %s:%s %s"%(uid,gid,user_path))
mountpoint=user_path
ret+=self.mount_volume_skel%(server,path,user_path)
ret+="\n"
lines=self.get_custom_volumes()
if lines!=None:
ret+=lines
ret+=last
file_path=user_home+"/.pam_mount.conf.xml"
f=open(file_path,"w")
f.write(ret)
f.close()
os.system("chown %s:%s %s"%(uid,gid,file_path))
return ret
#def generate_xml
def get_windows_home_directory(self,user_name=None):
if user_name==None:
user_name=os.environ["PAM_USER"]
command = subprocess.Popen(['/opt/pbis/bin/find-user-by-name', user_name], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = command.communicate()
user_SID = None
result = None
for out in stdout.split('\n'):
if out.startswith('SID'):
user_SID = out.split(':')[1].strip()
stderr, stdout, command = None, None, None
if user_SID != None:
command = subprocess.Popen(['/opt/pbis/bin/find-objects', '--by-sid', user_SID], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = command.communicate()
for out in stdout.split('\n'):
if out.startswith('Windows home directory'):
result = out.split(':')[1].strip()
ret=result.replace("\\", "/")
ret+="/%s"%user_name.upper()
return ret
return None
#def get_windows_home_directory
#class GetUserInfo
if __name__=="__main__":
gui=GetUserInfo()