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