235a937
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/scripts/genhomedircon policycoreutils-1.29.2/scripts/genhomedircon
7238655
--- nsapolicycoreutils/scripts/genhomedircon	2005-12-07 07:28:00.000000000 -0500
235a937
+++ policycoreutils-1.29.2/scripts/genhomedircon	2005-12-19 18:17:05.000000000 -0500
235a937
@@ -26,64 +26,70 @@
235a937
 #
235a937
 #  
235a937
 
235a937
-import commands, sys, os, pwd, string, getopt, re
235a937
+import sys, os, pwd, string, getopt, re
7238655
 from semanage import *;
2af5d4e
 
7238655
-fd=open("/etc/shells", 'r')
7238655
-VALID_SHELLS=fd.read().split('\n')
7238655
-fd.close()
7238655
-if "/sbin/nologin" in VALID_SHELLS:
7238655
-	VALID_SHELLS.remove("/sbin/nologin")
7238655
+try:
7238655
+	fd=open("/etc/shells", 'r')
7238655
+	VALID_SHELLS=fd.read().split('\n')
7238655
+	fd.close()
7238655
+	if "/sbin/nologin" in VALID_SHELLS:
7238655
+		VALID_SHELLS.remove("/sbin/nologin")
7238655
+except:
7238655
+	VALID_SHELLS = ['/bin/sh', '/bin/bash', '/bin/ash', '/bin/bsh', '/bin/ksh', '/usr/bin/ksh', '/usr/bin/pdksh', '/bin/tcsh', '/bin/csh', '/bin/zsh']
235a937
+
235a937
+def findval(file, var, delim=""):
235a937
+	val=""
235a937
+	fd=open(file, 'r')
235a937
+	for i in  fd.read().split('\n'):
235a937
+		if i.startswith(var) == 1:
235a937
+			if delim == "":
235a937
+				val = i.split()[1]
235a937
+			else:
235a937
+				val = i.split(delim)[1]
235a937
+			val = val.split("#")[0]
235a937
+			val = val.strip()
235a937
+	fd.close()
235a937
+	return val
80b61a6
 
7238655
 def getStartingUID():
7238655
 	starting_uid = sys.maxint
235a937
-	rc=commands.getstatusoutput("grep -h '^UID_MIN' /etc/login.defs")
235a937
-	if rc[0] == 0:
235a937
-		uid_min = re.sub("^UID_MIN[^0-9]*", "", rc[1])
235a937
-		#stip any comment from the end of the line
235a937
+	uid_min= findval("/etc/login.defs", "UID_MIN")
235a937
+	if uid_min != "":
235a937
 		uid_min = uid_min.split("#")[0]
235a937
 		uid_min = uid_min.strip()
235a937
 		if int(uid_min) < starting_uid:
235a937
 			starting_uid = int(uid_min)
235a937
-	rc=commands.getstatusoutput("grep -h '^LU_UIDNUMBER' /etc/libuser.conf")
235a937
-	if rc[0] == 0:
235a937
-		lu_uidnumber = re.sub("^LU_UIDNUMBER[^0-9]*", "", rc[1])
235a937
-		#stip any comment from the end of the line
235a937
-		lu_uidnumber = re.sub("[ \t].*", "", lu_uidnumber)
235a937
-		lu_uidnumber = lu_uidnumber.split("#")[0]
235a937
-		lu_uidnumber = lu_uidnumber.strip()
235a937
-		if int(lu_uidnumber) < starting_uid:
235a937
-			starting_uid = int(lu_uidnumber)
235a937
+
235a937
+	uid_min= findval("/etc/libuser.conf", "LU_UIDNUMBER", "=")
235a937
+	if uid_min != "":
235a937
+		uid_min = uid_min.split("#")[0]
235a937
+		uid_min = uid_min.strip()
235a937
+		if int(uid_min) < starting_uid:
235a937
+			starting_uid = int(uid_min)
235a937
+
235a937
 	if starting_uid == sys.maxint:
235a937
 		starting_uid = 500
235a937
 	return starting_uid
235a937
 
235a937
 def getDefaultHomeDir():
235a937
 	ret = []
235a937
-	rc=commands.getstatusoutput("grep -h '^HOME' /etc/default/useradd")
235a937
-	if rc[0] == 0:
235a937
-		homedir = rc[1].split("=")[1]
235a937
-		homedir = homedir.split("#")[0]
235a937
-		homedir = homedir.strip()
235a937
-		if not homedir in ret:
235a937
-			ret.append(homedir)
235a937
-
235a937
-	rc=commands.getstatusoutput("grep -h '^LU_HOMEDIRECTORY' /etc/libuser.conf")
235a937
-	if rc[0] == 0:
235a937
-		homedir = rc[1].split("=")[1]
235a937
-		homedir = homedir.split("#")[0]
235a937
-		homedir = homedir.strip()
235a937
-		if not homedir in ret:
235a937
-			ret.append(homedir)
235a937
-
235a937
+	homedir=findval("/etc/default/useradd", "HOME", "=")
235a937
+	if homedir != "" and not homedir in ret:
235a937
+		ret.append(homedir)
235a937
+	
235a937
+	homedir=findval("/etc/libuser.conf", "LU_HOMEDIRECTORY", "=")
235a937
+	if homedir != "" and not homedir in ret:
235a937
+		ret.append(homedir)
235a937
+	
235a937
 	if ret == []:
235a937
 		ret.append("/home")
235a937
 	return ret
235a937
 
235a937
 def getSELinuxType(directory):
235a937
-	rc=commands.getstatusoutput("grep ^SELINUXTYPE= %s/config" % directory)
235a937
-	if rc[0]==0:
235a937
-		return rc[1].split("=")[-1].strip()
235a937
+	val=findval(directory+"/config", "SELINUXTYPE", "=")
235a937
+	if val != "":
235a937
+		return val
235a937
 	return "targeted"
235a937
 
235a937
 def usage(error = ""):
235a937
@@ -129,11 +135,17 @@
235a937
 		return self.getFileContextDir()+"/homedir_template"
235a937
 
235a937
 	def getHomeRootContext(self, homedir):
235a937
-		rc=commands.getstatusoutput("grep HOME_ROOT  %s | sed -e \"s|^HOME_ROOT|%s|\"" % ( self.getHomeDirTemplate(), homedir))
235a937
-		if rc[0] == 0:
235a937
-			return rc[1]+"\n"
235a937
-		else:
235a937
-			errorExit("sed error %s" % rc[1])
235a937
+		ret=""
235a937
+		fd=open(self.getHomeDirTemplate(), 'r')
235a937
+
235a937
+		for i in  fd.read().split('\n'):
235a937
+			if i.find("HOME_ROOT") == 0:
235a937
+				i=i.replace("HOME_ROOT", homedir)
235a937
+				ret = i+"\n"
235a937
+		fd.close()
235a937
+		if ret=="":
235a937
+			errorExit("No Home Root Context Found")
235a937
+		return ret
235a937
 
235a937
 	def heading(self):
235a937
 		ret = "\n#\n#\n# User-specific file contexts, generated via %s\n" % sys.argv[0]
235a937
@@ -152,32 +164,40 @@
235a937
 				return "user_r"
235a937
 		return name
235a937
 	def getOldRole(self, role):
235a937
-		rc = commands.getstatusoutput('grep "^user %s" %s' % (role, self.selinuxdir+self.type+"/users/system.users"))
235a937
-		if rc[0] != 0:					    
235a937
-			rc = commands.getstatusoutput('grep "^user %s" %s' % (role, self.selinuxdir+self.type+"/users/local.users"))
235a937
-		if rc[0] == 0:
235a937
-			user=rc[1].split()
235a937
+		rc=findval(self.selinuxdir+self.type+"/users/system.users", 'grep "^user %s" %s' % role, "=")
235a937
+		if rc == "":					    
235a937
+			rc=findval(self.selinuxdir+self.type+"/users/local.users", 'grep "^user %s" %s' % role, "=")
235a937
+		if rc != "":
235a937
+			user=rc.split()
235a937
 			role = user[3]
235a937
 			if role == "{":
235a937
 				role = user[4]
7238655
 		return role
7238655
 		
7238655
 	def adduser(self, udict, user, seuser, role):
7238655
+		if seuser == "user_u" or user == "__default__":
7238655
+			return
7238655
+		# !!! chooses first role in the list to use in the file context !!!
7238655
+		if role[-2:] == "_r" or role[-2:] == "_u":
7238655
+			role = role[:-2]
7238655
 		try:
7238655
-			if seuser == "user_u" or user == "__default__":
7238655
-				return
7238655
-			# !!! chooses first role in the list to use in the file context !!!
7238655
-			if role[-2:] == "_r" or role[-2:] == "_u":
7238655
-				role = role[:-2]
7238655
 			home = pwd.getpwnam(user)[5]
7238655
 			if home == "/":
7238655
-				return
7238655
-			prefs = {}
7238655
-			prefs["role"] = role
7238655
-			prefs["home"] = home
7238655
-			udict[seuser] = prefs
7238655
+				# Probably install so hard code to /root
7238655
+				if user == "root":
7238655
+					home="/root"
7238655
+				else:
7238655
+					return
7238655
 		except KeyError:
7238655
-			sys.stderr.write("The user \"%s\" is not present in the passwd file, skipping...\n" % user)
7238655
+			if user == "root":
7238655
+				home = "/root"
7238655
+			else:
7238655
+				sys.stderr.write("The user \"%s\" is not present in the passwd file, skipping...\n" % user)
7238655
+				return
7238655
+		prefs = {}
7238655
+		prefs["role"] = role
7238655
+		prefs["home"] = home
7238655
+		udict[seuser] = prefs
b36bfa0
 
7238655
 	def getUsers(self):
7238655
 		udict = {}
235a937
@@ -190,30 +210,45 @@
235a937
 				self.adduser(udict, semanage_seuser_get_name(seuser), seusername, self.defaultrole(seusername))
235a937
 				
235a937
 		else:
235a937
-			rc = commands.getstatusoutput("grep -v '^ *#' %s" % self.selinuxdir+self.type+"/seusers")
235a937
-			if rc[0] == 0 and rc[1] != "":
235a937
-				ulist = rc[1].split("\n")
235a937
-				for u in ulist:
235a937
-					if len(u)==0:
235a937
-						continue
235a937
-					user = u.split(":")
235a937
-					if len(user) < 3:
235a937
-						continue
235a937
-					role=self.getOldRole(user[1])
235a937
-					self.adduser(udict, user[0], user[1], role)
235a937
+			fd =open(self.selinuxdir+self.type+"/seusers")
235a937
+			for u in  fd.read().split('\n'):
235a937
+				u=u.strip()
235a937
+				if len(u)==0 or u[0]=="#":
235a937
+					continue
235a937
+				user = u.split(":")
235a937
+				if len(user) < 3:
235a937
+					continue
235a937
+				role=self.getOldRole(user[1])
235a937
+				self.adduser(udict, user[0], user[1], role)
235a937
+			fd.close()
235a937
 		return udict
235a937
 
235a937
 	def getHomeDirContext(self, user, home, role):
235a937
 		ret="\n\n#\n# Home Context for user %s\n#\n\n" % user
235a937
-		rc=commands.getstatusoutput("grep '^HOME_DIR' %s | sed -e 's|HOME_DIR|%s|' -e 's/ROLE/%s/' -e 's/system_u/%s/'" % (self.getHomeDirTemplate(), home, role, user))
235a937
-		return ret + rc[1] + "\n"
235a937
+		fd=open(self.getHomeDirTemplate(), 'r')
235a937
+		for i in  fd.read().split('\n'):
235a937
+			if i.startswith("HOME_DIR") == 1:
235a937
+				i=i.replace("HOME_DIR", home)
235a937
+				i=i.replace("ROLE", role)
235a937
+				i=i.replace("system_u", user)
235a937
+				ret = ret+i+"\n"
235a937
+		fd.close()
235a937
+		return ret
235a937
 
235a937
 	def getUserContext(self, user, sel_user, role):
235a937
-		rc=commands.getstatusoutput("grep 'USER' %s | sed -e 's/USER/%s/' -e 's/ROLE/%s/' -e 's/system_u/%s/'" % (self.getHomeDirTemplate(), user, role, sel_user))
235a937
-		return rc[1] + "\n"
235a937
+		ret=""
235a937
+		fd=open(self.getHomeDirTemplate(), 'r')
235a937
+		for i in  fd.read().split('\n'):
235a937
+			if i.find("USER") == 1:
235a937
+				i=i.replace("USER", user)
235a937
+				i=i.replace("ROLE", role)
235a937
+				i=i.replace("system_u", sel_user)
235a937
+				ret=ret+i+"\n"
235a937
+		fd.close()
235a937
+		return ret
235a937
 
235a937
 	def genHomeDirContext(self):
235a937
-		if commands.getstatusoutput("grep -q 'ROLE' %s" % self.getHomeDirTemplate())[0] == 0 and self.semanaged:
235a937
+		if self.semanaged and findval(self.getHomeDirTemplate(), "ROLE", "=") != "":
235a937
 			warning("genhomedircon:  Warning!  No support yet for expanding ROLE macros in the %s file when using libsemanage." % self.getHomeDirTemplate());
235a937
 			warning("genhomedircon:  You must manually update file_contexts.homedirs for any non-user_r users (including root).");
235a937
 		users = self.getUsers()
235a937
@@ -225,40 +260,23 @@
235a937
 		return ret+"\n"
235a937
 
235a937
 	def checkExists(self, home):
235a937
-		if commands.getstatusoutput("grep -E '^%s[^[:alnum:]_-]' %s" % (home, self.getFileContextFile()))[0] == 0:
235a937
-			return 0
235a937
-		#this works by grepping the file_contexts for
235a937
-		# 1. ^/ makes sure this is not a comment
235a937
-		# 2. prints only the regex in the first column first cut on \t then on space
235a937
-		rc=commands.getstatusoutput("grep \"^/\" %s | cut -f 1 | cut -f 1 -d \" \" " %  self.getFileContextFile() )
235a937
-		if rc[0] == 0:
235a937
-			prefix_regex = rc[1].split("\n")
235a937
-		else:
235a937
-			warning("%s\nYou do not have access to read %s\n" % (rc[1], self.getFileContextFile()))
235a937
-
235a937
-		exists=1
235a937
-		for regex in prefix_regex:
235a937
-			#match a trailing (/*)? which is actually a bug in rpc_pipefs
235a937
-			regex = re.sub("\(/\*\)\?$", "", regex)
235a937
-			#match a trailing .+
235a937
-			regex = re.sub("\.+$", "", regex)
235a937
-			#match a trailing .*
235a937
-			regex = re.sub("\.\*$", "", regex)
235a937
-			#strip a (/.*)? which matches anything trailing to a /*$ which matches trailing /'s
235a937
-			regex = re.sub("\(\/\.\*\)\?", "", regex)
235a937
-			regex = regex + "/*$"
235a937
-			if re.search(regex, home, 0):
235a937
-				exists = 0
235a937
-				break
235a937
-		if exists == 1:
235a937
-			return 1
235a937
-		else:
235a937
-			return 0
235a937
-
235a937
+		fd=open(self.getFileContextFile())
235a937
+                for i in  fd.read().split('\n'):
235a937
+                    if len(i)==0:
235a937
+                        return
235a937
+                    regex=i.split()[0]
235a937
+                    #match a trailing .+
235a937
+                    regex = re.sub("\.+$", "", regex)
235a937
+                    regex = re.sub("\.\*$", "", regex)
235a937
+                    #strip a (/.*)? which matches anything trailing to a /*$ which matches trailing /'s
235a937
+                    regex = re.sub("\(\/\.\*\)\?", "", regex)
235a937
+                    regex = regex + "/*$"
235a937
+                    if re.search(home, regex, 0):
235a937
+                        return 1
235a937
+		return 0
235a937
 
235a937
 	def getHomeDirs(self):
235a937
-		homedirs = []
235a937
-		homedirs = homedirs + getDefaultHomeDir()
235a937
+		homedirs = getDefaultHomeDir()
235a937
 		starting_uid=getStartingUID()
235a937
 		if self.usepwd==0:
235a937
 			return homedirs
235a937
@@ -270,7 +288,7 @@
235a937
 					string.count(u[5], "/") > 1:
235a937
 				homedir = u[5][:string.rfind(u[5], "/")]
235a937
 				if not homedir in homedirs:
235a937
-					if self.checkExists(homedir)==0:
235a937
+					if self.checkExists(homedir)==1:
235a937
 						warning("%s homedir %s or its parent directoy conflicts with a\ndefined context in %s,\n%s will not create a new context." % (u[0], u[5], self.getFileContextFile(), sys.argv[0]))
235a937
 					else:
235a937
 						homedirs.append(homedir)
235a937
@@ -336,4 +354,4 @@
235a937
 except ValueError, error:
235a937
 	errorExit("ValueError %s" % error)
235a937
 except IndexError, error:
235a937
-	errorExit("IndexError")
235a937
+	errorExit("IndexError %s" % error)