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