54ecf23
diff --exclude-from=exclude -N -u -r nsapolicycoreutils/scripts/genhomedircon policycoreutils-1.27.26/scripts/genhomedircon
54ecf23
--- nsapolicycoreutils/scripts/genhomedircon	2005-09-12 16:33:30.000000000 -0400
77fc72c
+++ policycoreutils-1.27.26/scripts/genhomedircon	2005-11-09 16:08:15.000000000 -0500
54ecf23
@@ -15,30 +15,16 @@
54ecf23
 # The file CONTEXTDIR/files/homedir_template exists.  This file is used to
54ecf23
 # set up the home directory context for each real user.
54ecf23
 # 
54ecf23
-# If a user has more than one role in CONTEXTDIR/local.users, genhomedircon uses
54ecf23
-#  the first role in the list.
54ecf23
+# If a user has more than one role, genhomedircon uses the first role in the list.
54ecf23
 #
54ecf23
-# If a user is not listed in CONTEXTDIR/local.users, he will default to user_u, role user
54ecf23
+# If a user is not listed in CONTEXTDIR/seusers, he will default to user_u, role user
54ecf23
 #
54ecf23
 # "Real" users (as opposed to system users) are those whose UID is greater than
54ecf23
 #  or equal STARTING_UID (usually 500) and whose login is not a member of
54ecf23
-#  EXCLUDE_LOGINS.  Users who are explicitly defined in CONTEXTDIR/local.users
54ecf23
+#  EXCLUDE_LOGINS.  Users who are explicitly defined in CONTEXTDIR/seusers
54ecf23
 #  are always "real" (including root, in the default configuration).
54ecf23
 #
54ecf23
 #  
54ecf23
-# Old ASSUMPTIONS:
54ecf23
-#
54ecf23
-# If a user has more than one role in FILECONTEXTDIR/users, genhomedircon uses
54ecf23
-#  the first role in the list.
54ecf23
-#
54ecf23
-# If a user is not listed in FILECONTEXTDIR/users, genhomedircon assumes that
54ecf23
-#  the user's home dir will be found in one of the HOME_ROOTs.
54ecf23
-#
54ecf23
-# "Real" users (as opposed to system users) are those whose UID is greater than
54ecf23
-#  or equal STARTING_UID (usually 500) and whose login is not a member of
54ecf23
-#  EXCLUDE_LOGINS.  Users who are explicitly defined in FILECONTEXTDIR/users
54ecf23
-#  are always "real" (including root, in the default configuration).
54ecf23
-#
54ecf23
 
54ecf23
 import commands, sys, os, pwd, string, getopt, re
54ecf23
 
54ecf23
@@ -67,169 +53,6 @@
54ecf23
 		starting_uid = 500
54ecf23
 	return starting_uid
54ecf23
 
54ecf23
-#############################################################################
54ecf23
-#
54ecf23
-# This section is just for backwards compatability
54ecf23
-#
54ecf23
-#############################################################################
54ecf23
-def getPrefixes():
54ecf23
-	ulist = pwd.getpwall()
54ecf23
-	STARTING_UID=getStartingUID()
54ecf23
-	prefixes = {}
54ecf23
-	for u in ulist:
54ecf23
-		if u[2] >= STARTING_UID and \
54ecf23
-				not u[6] in EXCLUDE_LOGINS and \
54ecf23
-				u[5] != "/" and \
54ecf23
-				string.count(u[5], "/") > 1:
54ecf23
-			prefix = u[5][:string.rfind(u[5], "/")]
54ecf23
-			if not prefixes.has_key(prefix):
54ecf23
-				prefixes[prefix] = ""
54ecf23
-	return prefixes
54ecf23
- 
54ecf23
-def getUsers(filecontextdir):
54ecf23
-	rc = commands.getstatusoutput("grep ^user %s/users" % filecontextdir)
54ecf23
-	udict = {}
54ecf23
-	if rc[0] == 0:
54ecf23
-		ulist = rc[1].strip().split("\n")
54ecf23
-		for u in ulist:
54ecf23
-			user = u.split()
54ecf23
-			try:
54ecf23
-				if user[1] == "user_u" or user[1] == "system_u":
54ecf23
-					continue
54ecf23
-				# !!! chooses first role in the list to use in the file context !!!
54ecf23
-				role = user[3]
54ecf23
-				if role == "{":
54ecf23
-					role = user[4]
54ecf23
-				role = role.split("_r")[0]
54ecf23
-				home = pwd.getpwnam(user[1])[5]
54ecf23
-				if home == "/":
54ecf23
-					continue
54ecf23
-				prefs = {}
54ecf23
-				prefs["role"] = role
54ecf23
-				prefs["home"] = home
54ecf23
-				udict[user[1]] = prefs
54ecf23
-			except KeyError:
54ecf23
-				sys.stderr.write("The user \"%s\" is not present in the passwd file, skipping...\n" % user[1])
54ecf23
-	return udict
54ecf23
-
54ecf23
-def update(filecontext, user, prefs):
54ecf23
-	rc=commands.getstatusoutput("grep -h '^HOME_DIR' %s | grep -v vmware | sed -e 's|HOME_DIR|%s|' -e 's/ROLE/%s/' -e 's/system_u/%s/'" % (filecontext, prefs["home"], prefs["role"], user))
54ecf23
-	if rc[0] == 0:
54ecf23
-		print rc[1]
54ecf23
-	else:
54ecf23
-		errorExit(string.join("grep/sed error ", rc[1]))
54ecf23
-	return rc
54ecf23
-
54ecf23
-def oldgenhomedircon(filecontextdir, filecontext):
54ecf23
-	sys.stderr.write("Using genhomedircon in this fashion is supported for backwards compatability\n")
54ecf23
-	sys.stderr.write("Please update to the latest policy\n")
54ecf23
-	sys.stderr.flush()
54ecf23
-
54ecf23
-	if os.path.isdir(filecontextdir) == 0:
54ecf23
-		sys.stderr.write("New usage is the following\n")
54ecf23
-		usage()
54ecf23
-        #We are going to define home directory used by libuser and show-utils as a home directory root
54ecf23
-        prefixes = {}
54ecf23
-        rc=commands.getstatusoutput("grep -h '^HOME' /etc/default/useradd")
54ecf23
-        if rc[0] == 0:
54ecf23
-                homedir = rc[1].split("=")[1]
54ecf23
-                homedir = homedir.split("#")[0]
54ecf23
-                homedir = homedir.strip()
54ecf23
-                if not prefixes.has_key(homedir):
54ecf23
-                        prefixes[homedir] = ""
54ecf23
-        else:
54ecf23
-                #rc[0] == 256 means the file was there, we read it, but the grep didn't match
54ecf23
-                if rc[0] != 256:
54ecf23
-                        sys.stderr.write("%s\n" % rc[1])
54ecf23
-                        sys.stderr.write("You do not have access to /etc/default/useradd HOME=\n")
54ecf23
-                        sys.stderr.flush()
54ecf23
-
54ecf23
-
54ecf23
-        rc=commands.getstatusoutput("grep -h '^LU_HOMEDIRECTORY' /etc/libuser.conf")
54ecf23
-        if rc[0] == 0:
54ecf23
-                homedir = rc[1].split("=")[1]
54ecf23
-                homedir = homedir.split("#")[0]
54ecf23
-                homedir = homedir.strip()
54ecf23
-                homedir = re.sub(r"[^/a-zA-Z0-9].*$", "", homedir)
54ecf23
-                if not prefixes.has_key(homedir):
54ecf23
-                        prefixes[homedir] = ""
54ecf23
-        else:
54ecf23
-                if rc[0] != 256:
54ecf23
-                        sys.stderr.write("%s\n" % rc[1])
54ecf23
-                        sys.stderr.write("You do not have access to /etc/libuser.conf LU_HOMEDIRECTORY=\n")
54ecf23
-                        sys.stderr.flush()
54ecf23
-
54ecf23
-        #the idea is that we need to find all of the home_root_t directories we do this by just accepting
54ecf23
-        #any default home directory defined by either /etc/libuser.conf or /etc/default/useradd
54ecf23
-        #we then get the potential home directory roots from /etc/passwd or nis or whereever and look at
54ecf23
-        #the defined homedir for all users with UID > STARTING_UID.  This list of possible root homedirs
54ecf23
-        #is then checked to see if it has an explicite context defined in the file_contexts.  Explicit
54ecf23
-        #is any regex that would match it which does not end with .*$ or .+$ since those are general
54ecf23
-        #recursive matches.  We then take any regex which ends with [pattern](/.*)?$ and just check against
54ecf23
-        #[pattern]
54ecf23
-        potential_prefixes = getPrefixes()
54ecf23
-        prefix_regex = {}
54ecf23
-        #this works by grepping the file_contexts for
54ecf23
-        # 1. ^/ makes sure this is not a comment
54ecf23
-        # 2. prints only the regex in the first column first cut on \t then on space
54ecf23
-        rc=commands.getstatusoutput("grep \"^/\" %s | cut -f 1 | cut -f 1 -d \" \" " %  (sys.argv[2]) )
54ecf23
-        if rc[0] == 0:
54ecf23
-                prefix_regex = rc[1].split("\n")
54ecf23
-        else:
54ecf23
-                sys.stderr.write("%s\n" % rc[1])
54ecf23
-                sys.stderr.write("You do not have access to grep/cut/the file contexts\n")
54ecf23
-                sys.stderr.flush()
54ecf23
-        for potential in potential_prefixes.keys():
54ecf23
-                addme = 1
54ecf23
-                for regex in prefix_regex:
54ecf23
-                        #match a trailing (/*)? which is actually a bug in rpc_pipefs
54ecf23
-                        regex = re.sub("\(/\*\)\?$", "", regex)
54ecf23
-                        #match a trailing .+
54ecf23
-                        regex = re.sub("\.+$", "", regex)
54ecf23
-                        #match a trailing .*
54ecf23
-                        regex = re.sub("\.\*$", "", regex)
54ecf23
-                        #strip a (/.*)? which matches anything trailing to a /*$ which matches trailing /'s
54ecf23
-                        regex = re.sub("\(\/\.\*\)\?", "", regex)
54ecf23
-                        regex = regex + "/*$"
54ecf23
-                        if re.search(regex, potential, 0):
54ecf23
-                                addme = 0
54ecf23
-                if addme == 1:
54ecf23
-                        if not prefixes.has_key(potential):
54ecf23
-                                prefixes[potential] = ""
54ecf23
-
54ecf23
-
54ecf23
-        if prefixes.__eq__({}):
54ecf23
-                sys.stderr.write("LU_HOMEDIRECTORY not set in /etc/libuser.conf\n")
54ecf23
-                sys.stderr.write("HOME= not set in /etc/default/useradd\n")
54ecf23
-                sys.stderr.write("And no users with a reasonable homedir found in passwd/nis/ldap/etc...\n")
54ecf23
-                sys.stderr.write("Assuming /home is the root of home directories\n")
54ecf23
-                sys.stderr.flush()
54ecf23
-                prefixes["/home"] = ""
54ecf23
-
54ecf23
-	# There may be a more elegant sed script to expand a macro to multiple lines, but this works
54ecf23
-	sed_root = "h; s|^HOME_ROOT|%s|" % (string.join(prefixes.keys(), "|; p; g; s|^HOME_ROOT|"),)
54ecf23
-	sed_dir = "h; s|^HOME_DIR|%s/[^/]+|; s|ROLE_|user_|" % (string.join(prefixes.keys(), "/[^/]+|; s|ROLE_|user_|; p; g; s|^HOME_DIR|"),)
54ecf23
-
54ecf23
-	# Fill in HOME_ROOT, HOME_DIR, and ROLE for users not explicitly defined in /etc/security/selinux/src/policy/users
54ecf23
-	rc=commands.getstatusoutput("sed -e \"/^HOME_ROOT/{%s}\" -e \"/^HOME_DIR/{%s}\" %s" % (sed_root, sed_dir, filecontext))
54ecf23
-	if rc[0] == 0:
54ecf23
-		print rc[1]
54ecf23
-	else:
54ecf23
-		errorExit(string.join("sed error ", rc[1]))
54ecf23
-
54ecf23
-	users = getUsers(filecontextdir)
54ecf23
-	print "\n#\n# User-specific file contexts\n#\n"
54ecf23
-
54ecf23
-	# Fill in HOME and ROLE for users that are defined
54ecf23
-	for u in users.keys():
54ecf23
-		update(filecontext, u, users[u]) 
54ecf23
-
54ecf23
-#############################################################################
54ecf23
-#
54ecf23
-# End of backwards compatability section
54ecf23
-#
54ecf23
-#############################################################################
54ecf23
-
54ecf23
 def getDefaultHomeDir():
54ecf23
 	ret = []
54ecf23
 	rc=commands.getstatusoutput("grep -h '^HOME' /etc/default/useradd")
54ecf23
@@ -313,11 +136,8 @@
54ecf23
 			errorExit(string.join("sed error ", rc[1]))
54ecf23
 
54ecf23
 	def getUsersFile(self):
54ecf23
-		return self.selinuxdir+self.type+"/users/local.users"
54ecf23
+		return self.selinuxdir+self.type+"/seusers"
54ecf23
 
54ecf23
-	def getSystemUsersFile(self):
54ecf23
-		return self.selinuxdir+self.type+"/users/system.users"
54ecf23
-		
54ecf23
 	def heading(self):
54ecf23
 		ret = "\n#\n#\n# User-specific file contexts, generated via %s\n" % sys.argv[0]
54ecf23
 		ret += "# edit %s to change file_context\n#\n#\n" % self.getUsersFile()
54ecf23
@@ -325,10 +145,7 @@
54ecf23
 
54ecf23
 	def getUsers(self):
54ecf23
 		users=""
54ecf23
-		rc = commands.getstatusoutput('grep "^user" %s' % self.getSystemUsersFile())
54ecf23
-		if rc[0] == 0:
54ecf23
-			users+=rc[1]+"\n"
54ecf23
-		rc = commands.getstatusoutput("grep ^user %s" % self.getUsersFile())
54ecf23
+		rc = commands.getstatusoutput("grep -v '^ *#' %s" % self.getUsersFile())
54ecf23
 		if rc[0] == 0:
54ecf23
 			users+=rc[1]
54ecf23
 		udict = {}
77fc72c
@@ -336,24 +153,27 @@
54ecf23
 		if users != "":
54ecf23
 			ulist = users.split("\n")
54ecf23
 			for u in ulist:
54ecf23
-				user = u.split()
54ecf23
+				if len(u)==0:
54ecf23
+					continue
54ecf23
+				user = u.split(":")
54ecf23
 				try:
54ecf23
-					if len(user)==0 or user[1] == "user_u" or user[1] == "system_u":
77fc72c
+					if len(user)==0 or user[1] == "user_u":
54ecf23
 						continue
54ecf23
 					# !!! chooses first role in the list to use in the file context !!!
54ecf23
-					role = user[3]
54ecf23
-					if role == "{":
54ecf23
-						role = user[4]
54ecf23
-					role = role.split("_r")[0]
54ecf23
-					home = pwd.getpwnam(user[1])[5]
77fc72c
+					if user[0] == "root":
77fc72c
+						role="user_u"
77fc72c
+					else:
77fc72c
+						role = user[1]
54ecf23
+					role = role.split("_u")[0]
54ecf23
+					home = pwd.getpwnam(user[0])[5]
54ecf23
 					if home == "/":
54ecf23
 						continue
54ecf23
 					prefs = {}
54ecf23
 					prefs["role"] = role
54ecf23
 					prefs["home"] = home
54ecf23
-					udict[user[1]] = prefs
54ecf23
+					udict[user[0]] = prefs
54ecf23
 				except KeyError:
54ecf23
-					sys.stderr.write("The user \"%s\" is not present in the passwd file, skipping...\n" % user[1])
54ecf23
+					sys.stderr.write("The user \"%s\" is not present in the passwd file, skipping...\n" % user[0])
54ecf23
 		return udict
54ecf23
 
54ecf23
 	def getHomeDirContext(self, user, home, role):
77fc72c
@@ -478,10 +298,6 @@
54ecf23
 	if type==None:
54ecf23
 		type=getSELinuxType(directory)
54ecf23
 
54ecf23
-	if len(cmds) == 2:
54ecf23
-		oldgenhomedircon(cmds[0], cmds[1])
54ecf23
-		sys.exit(0)
54ecf23
-
54ecf23
 	if len(cmds) != 0:
54ecf23
 		usage()
54ecf23
 	selconf=selinuxConfig(directory, type, usepwd)