Home | History | Annotate | Download | only in acl
      1 #!/usr/bin/env python3
      2 import subprocess
      3 import random
      4 import re
      5 
      6 alphabet = 'azertyuiopqsdfghjklmwxcvbnAZERTYUIOPQSDFGHJKLMWXCVBN123456789-_'
      7 a_length = len(alphabet)
      8 
      9 """ ACL support attribute """
     10 ACL4_SUPPORT_ALLOW_ACL = 0x00000001
     11 ACL4_SUPPORT_DENY_ACL = 0x00000002
     12 ACL4_SUPPORT_AUDIT_ACL = 0x00000004
     13 ACL4_SUPPORT_ALARM_ACL = 0x00000008
     14 
     15 class RandomGen(object):
     16 
     17 
     18 	"""  List of ACE possible who fields """
     19 	ace_who=["OWNER@","GROUP@","EVERYONE@","ANONYMOUS@","AUTHENTICATED@"]
     20 
     21 	""" List of GID than can be used to do the tests """
     22 	gList=[]
     23 	gListSize = len(gList)
     24 	uList = []
     25 	uListSize = len(uList)
     26 
     27 	fList=[]
     28 	fListSize = len(fList)
     29 
     30 	""" Create a user in available groups to do the tests """
     31 	def createUser(self,username):
     32 		group = self.gList[random.randint(0,len(self.gList)-1)][0]
     33 		opts = "-g" + group + " -p" + "1pilot" + " -m " + username
     34 		u = subprocess.getoutput('/usr/sbin/useradd '+ opts)
     35 		if u != "":
     36 			print("create user " + username + "failed" + u)
     37 
     38 	def createFile(self,path,n):
     39 		for i in range(n):
     40 			fName = 'file' + str(i)
     41 			u = subprocess.getoutput('touch ' + path + '/'+ fName)
     42 			self.fList.append(fName)
     43 
     44 	def createGroup(self, grpname, gid):
     45 		u = subprocess.getoutput('/usr/sbin/groupadd -g' + gid + " " + grpname)
     46 		if u != "":
     47 			print(u)
     48 
     49 	def createNGroup(self, n):
     50 		for i in range(n):
     51 			gName = 'grp' + str(i)
     52 			gid = str(500+i)
     53 			self.createGroup(gName, gid)
     54 
     55 
     56 	""" Random creation of n user """
     57 	def createNUser(self,n):
     58 		for i in range(n):
     59 			userName= "user" + str(i)
     60 			self.createUser(userName)
     61 
     62 	""" clean all users created to do the tests """
     63 	def cleanUsers(self):
     64 		for name in self.uList:
     65 			u = subprocess.getoutput('/usr/sbin/userdel -r '+ name)
     66 		self.uList = []
     67 
     68 	""" clean all users created to do the tests """
     69 	def cleanGroups(self):
     70 		for name in self.gList:
     71 			u = subprocess.getoutput('/usr/sbin/groupdel '+ name[0])
     72 		self.gList = []
     73 
     74 	""" Retrieve the list of user from /etc/passwd file """
     75 	def getUserList(self):
     76 		f = open('/etc/passwd','r')
     77 		lines = f.readlines()
     78 		for line in lines:
     79 			splitedline = line.split(':')
     80 			userName = splitedline[0]
     81 			gid = splitedline[3]
     82 		# TO FIX: verify that the group is OK (in the right range)
     83 			NameOK = re.match("user",userName)
     84 			# We keep only usernames starting with "user"
     85 			if NameOK != None:
     86 				self.uList.append(userName)
     87 		f.close()
     88 
     89 	def getFileList(self,path):
     90 		u = subprocess.getoutput('ls ' + path)
     91 		tmp = u.split('\n')
     92 		for i in range (len(tmp)-1):
     93 			NameOK = re.match("file",tmp[i])
     94 			if NameOK != None:
     95 				self.fList.append(tmp[i])
     96 
     97 	def getNUserList(self,nb):
     98 		f = open('/etc/passwd','r')
     99 		lines = f.readlines()
    100 		n = 0
    101 		for line in lines:
    102 			splitedline = line.split(':');
    103 			userName = splitedline[0]
    104 			gid = splitedline[3]
    105 		# TO FIX: verify that the group is OK (in the right range)
    106 			NameOK = re.match("user",userName)
    107 			# We keep only usernames starting with "user"
    108 			if NameOK != None:
    109 				self.uList.append(userName)
    110 				n = n+1
    111 			if n==nb:
    112 				break;
    113 		f.close()
    114 
    115 	""" Get group list """
    116 	def getGroupList(self):
    117 		f = open('/etc/group','r')
    118 		lines = f.readlines()
    119 		for line in lines:
    120 			splitedline = line.split(':');
    121 			groupName = splitedline[0]
    122 			gid = splitedline[2]
    123 			NameOK = re.match("grp",groupName)
    124 			if NameOK != None:
    125 				self.gList.append([groupName,gid])
    126 		f.close()
    127 
    128 	""" Get a list of n group """
    129 	def getNGroupList(self,nb):
    130 		f = open('/etc/group','r')
    131 		lines = f.readlines()
    132 		n = 0
    133 		for line in lines:
    134 			splitedline = line.split(':');
    135 			groupName = splitedline[0]
    136 			gid = splitedline[2]
    137 			NameOK = re.match("grp",groupName)
    138 			if NameOK != None:
    139 				self.gList.append([groupName,gid])
    140 				n = n+1
    141 			if n==nb:
    142 				break;
    143 		f.close()
    144 
    145 	def printUserList(self):
    146 		print(self.uList)
    147 
    148 	def printGroupList(self):
    149 		print(self.gList)
    150 
    151 	""" Create a random name of random length """
    152 	def createOneNameRandomLength(self,maxlength):
    153 		outputString =""
    154 		l=random.randint(0,maxlength)
    155 		for i in range(l):
    156 			a = random.randint(0,a_length-1)
    157 			outputString =outputString  + alphabet[a]
    158 		return outputString
    159 
    160 	""" Create a random name of fixed length """
    161 	def createOneName(self,lenght):
    162 		outputString =""
    163 		for i in range(length):
    164 			a = random.randint(0,a_length-1)
    165 			outputString = outputString + alphabet[a]
    166 		return outputString
    167 
    168 	""" Create Random User List with fixed length user names """
    169 	def createRandomUserList(self,listlength,usernamelength):
    170 		userlist = []
    171 		for i in range(listlength):
    172 			user = createOneName(lenght)
    173 			userlist.append(user)
    174 		return userlist
    175 
    176 	""" Create Random ACE for a file and a given usr """
    177 	def createRandomACE(self,user):
    178 		type = ace_type[random.randint(0,len(ace_type))]
    179 		flag = ace_flags[random.randint(0,len(ace_flags))]
    180 		mask = ace_mask[random.randint(0,len(ace_mask))]
    181 		who = ace_who[random.randint(0,len(ace_who))]
    182 		return nfsace4(type, flag, mask, who)
    183 
    184 	""" Create Random ACL for a file with a fixed number a entries """
    185 	def createRandomACL(self,acl_size):
    186 		acl = []
    187 		userList = uList
    188 		userListSize = uListSize
    189 		for i in range(acl_size):
    190 			n = random.randint(0,userListSize-1)
    191 			usr = userList.pop(n)
    192 			newace = createRandomACE(usr)
    193 			acl.append(newace)
    194 		return acl
    195 
    196 	""" Return a mode string like 'xwr' or 'x' """
    197 	def createRandomMode(self):
    198 		out_str = ""
    199 		while (out_str == ""):
    200 				if random.randint(0,1) == 1:
    201 					out_str += 'x'
    202 				if random.randint(0,1) == 1:
    203 					out_str += 'w'
    204 				if random.randint(0,1) == 1:
    205 					out_str += 'r'
    206 		return out_str
    207 
    208 	""" Create a random ACL operation (delete / remove / modify on user / group ) """
    209 	def randomOp(self,path):
    210 		a = random.randint(1,4)
    211 		mode = self.createRandomMode()
    212 		file = self.fList[random.randint(0,len(self.fList)-1)]
    213 		if a == 1:	# creation/modification
    214 			user = self.uList[random.randint(0,len(self.uList)-1)]
    215 			u = subprocess.getoutput('setfacl -m u:' + user + ':' + mode + " " + path + "/" + file)
    216 
    217 		if a == 2:	# with group
    218 			group = self.gList[random.randint(0,len(self.gList)-1)][0]
    219 			u = subprocess.getoutput('setfacl -m g:' + group + ':' + mode + " " + path + "/" + file)
    220 
    221 		if a == 3:	# deletation
    222 			user = self.uList[random.randint(0,len(self.uList)-1)]
    223 			u = subprocess.getoutput('setfacl -x u:' + user + " " + path + "/" + file)
    224 
    225 		if a == 4:	# with group
    226 			group = self.gList[random.randint(0,len(self.gList)-1)][0]
    227 			u = subprocess.getoutput('setfacl -x g:' + group + " " + path + "/" + file)
    228 
    229 		# request on a unexisting group
    230 		'''if a == 5:
    231 			group = self.createOneNameRandomLength(16)
    232 			print 'setfacl -x g:' + group + " " + path + "/" + file
    233 			u = commands.getoutput('setfacl -x g:' + group + " " + path + "/" + file)
    234 		if a == 6:
    235 			user = self.createOneNameRandomLength(16)
    236 			u = commands.getoutput('setfacl -x u:' + user + " " + path + "/" + file)
    237 
    238 		if a == 7:	# creation/modification
    239 			user = self.createOneNameRandomLength(16)
    240 			u = commands.getoutput('setfacl -m u:' + user + ':' + mode + " " + path + "/" + file)
    241 
    242 		if a == 8:	# with group
    243 			group = self.createOneNameRandomLength(16)
    244 			u = commands.getoutput('setfacl -m g:' + group + ':' + mode + " " + path + "/" + file)
    245 
    246 		if a == 9:     	#Copying the ACL of one file to another
    247 			file2 = self.fList[random.randint(0,len(self.fList)-1)]
    248               		u = commands.getoutput('getfacl ' + path + "/" + file + "| setfacl --set-file=- " + path + "/" + file2)
    249 		if u!="":
    250 			print u'''
    251 
    252