Skip to contentSkip to conversation

genhomedircon

PATCH In review Michael C Thompson 3 participants 4 messages Nov 16, 2006 – Nov 27, 2006 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 genhomedircon

I've noticed that genhomedircon does not have the proper return codes on some error and success paths. This patch addresses these return codes as follow:

* usage function by default returns 0, and the desired return code can be specified via a parameter. This facilitates the fix to the current behaviour that 1 is returned on 'genhomedircon -h'.

* I have noticed that as secadm (this is a bug? will start a separate thread) fails to successfully call semanage_connect(). The result of this operation is now checked, and the script will exit on error.

* If the attempt to write the homedir contexts out fails, a proper error code will be returned (previously, 1 would be returned).

This also moves the parsing of /etc/shells to after the uid check for a minimal time savings.

Thanks,
Mike

scripts/genhomedircon +31 -28
diff -Naur policycoreutils-1.33.1/scripts/genhomedircon policycoreutils-1.33.1.dev/scripts/genhomedircon
@@ -29,17 +29,6 @@
 import gettext
 gettext.install('policycoreutils')
 
-try:
-	fd = open("/etc/shells", 'r')
-	VALID_SHELLS = fd.read().split("\n")
-	fd.close()
-	if "/sbin/nologin" in VALID_SHELLS:
-		VALID_SHELLS.remove("/sbin/nologin")
-	if "" in VALID_SHELLS:
-		VALID_SHELLS.remove("")
-except:
-	VALID_SHELLS = ['/bin/sh', '/bin/bash', '/bin/ash', '/bin/bsh', '/bin/ksh', '/usr/bin/ksh', '/usr/bin/pdksh', '/bin/tcsh', '/bin/csh', '/bin/zsh']
-
 def grep(file, var):
 	ret = ""
 	fd = open(file, 'r')
@@ -114,12 +103,13 @@
 		return val
 	return "targeted"
 
-def usage(error = ""):
+def usage(rc=0, error = ""):
 	if error != "":
 		sys.stderr.write("%s\n" % error)
+		rc = 1
 	sys.stderr.write("Usage: %s [ -d selinuxdir ] [-n | --nopasswd] [-t selinuxtype ]\n" % sys.argv[0])
 	sys.stderr.flush()
-	sys.exit(1)
+	sys.exit(rc)
 
 def warning(warning = ""):
 	sys.stderr.write("%s\n" % warning)
@@ -136,7 +126,9 @@
 		self.semanageHandle = semanage_handle_create()
 		self.semanaged = semanage_is_managed(self.semanageHandle)
 		if self.semanaged:
-			semanage_connect(self.semanageHandle)
+			rc = semanage_connect(self.semanageHandle)
+			if rc:
+				errorExit("Unable to connect to semanage")
 			(status, self.ulist) = semanage_user_list(self.semanageHandle)
 		self.type = type
 		self.selinuxdir = selinuxdir +"/"
@@ -336,18 +328,25 @@
 		print self.genoutput()
 
 	def write(self):
-		try:
-			fd = open(self.getFileContextDir()+"/file_contexts.homedirs", "w")
-			fd.write(self.genoutput())
-			fd.close()
-		except IOError, error:
-			sys.stderr.write("%s: %s\n" % ( sys.argv[0], error ))
-
+		fd = open(self.getFileContextDir()+"/file_contexts.homedirs", "w")
+		fd.write(self.genoutput())
+		fd.close()
 
 if os.getuid() > 0 or os.geteuid() > 0:
 	print _("You must be root to run %s.") % sys.argv[0]
 	sys.exit(1)
 
+try:
+	fd = open("/etc/shells", 'r')
+	VALID_SHELLS = fd.read().split("\n")
+	fd.close()
+	if "/sbin/nologin" in VALID_SHELLS:
+		VALID_SHELLS.remove("/sbin/nologin")
+	if "" in VALID_SHELLS:
+		VALID_SHELLS.remove("")
+except:
+	VALID_SHELLS = ['/bin/sh', '/bin/bash', '/bin/ash', '/bin/bsh', '/bin/ksh', '/usr/bin/ksh', '/usr/bin/pdksh', '/bin/tcsh', '/bin/csh', '/bin/zsh']
+
 #
 # This script will generate home dir file context
 # based off the homedir_template file, entries in the password file, and
@@ -369,15 +368,19 @@
 			directory = a
 		if o == '--help'  or o == "-h":
 			usage()
+except getopt.error, error:
+	errorExit(_("Options Error %s ") % error)
 
+if type == None:
+	type = getSELinuxType(directory)
 
-	if type == None:
-		type = getSELinuxType(directory)
+if len(cmds) != 0:
+	usage(1)
 
-	if len(cmds) != 0:
-		usage()
-	selconf = selinuxConfig(directory, type, usepwd)
+selconf = selinuxConfig(directory, type, usepwd)
+try:
 	selconf.write()
+except IOError, error:
+	sys.stderr.write("%s: %s\n" % ( sys.argv[0], error ))
+	sys.exit(1)
 
-except getopt.error, error:
-	errorExit(_("Options Error %s ") % error)
Acked-by on v1 1/1
#1 lore raw
Quoted 116 lines · Michael C Thompson

I've noticed that genhomedircon does not have the proper return codes on some error and success paths. This patch addresses these return codes as follow:

* usage function by default returns 0, and the desired return code can be specified via a parameter. This facilitates the fix to the current behaviour that 1 is returned on 'genhomedircon -h'.

* I have noticed that as secadm (this is a bug? will start a separate thread) fails to successfully call semanage_connect(). The result of this operation is now checked, and the script will exit on error.

* If the attempt to write the homedir contexts out fails, a proper error code will be returned (previously, 1 would be returned).

This also moves the parsing of /etc/shells to after the uid check for a minimal time savings.

Thanks,
Mike

------------------------------------------------------------------------

diff -Naur policycoreutils-1.33.1/scripts/genhomedircon policycoreutils-1.33.1.dev/scripts/genhomedircon

scripts/genhomedircon +26 -25
@@ -29,17 +29,6 @@
 import gettext
 gettext.install('policycoreutils')
 
-try:
-	fd = open("/etc/shells", 'r')
-	VALID_SHELLS = fd.read().split("\n")
-	fd.close()
-	if "/sbin/nologin" in VALID_SHELLS:
-		VALID_SHELLS.remove("/sbin/nologin")
-	if "" in VALID_SHELLS:
-		VALID_SHELLS.remove("")
-except:
-	VALID_SHELLS = ['/bin/sh', '/bin/bash', '/bin/ash', '/bin/bsh', '/bin/ksh', '/usr/bin/ksh', '/usr/bin/pdksh', '/bin/tcsh', '/bin/csh', '/bin/zsh']
-
 def grep(file, var):
 	ret = ""
 	fd = open(file, 'r')
@@ -114,12 +103,13 @@
 		return val
 	return "targeted"
 
-def usage(error = ""):
+def usage(rc=0, error = ""):
 	if error != "":
 		sys.stderr.write("%s\n" % error)
+		rc = 1
 	sys.stderr.write("Usage: %s [ -d selinuxdir ] [-n | --nopasswd] [-t selinuxtype ]\n" % sys.argv[0])
 	sys.stderr.flush()
-	sys.exit(1)
+	sys.exit(rc)
 
 def warning(warning = ""):
 	sys.stderr.write("%s\n" % warning)
@@ -136,7 +126,9 @@
 		self.semanageHandle = semanage_handle_create()
 		self.semanaged = semanage_is_managed(self.semanageHandle)
 		if self.semanaged:
-			semanage_connect(self.semanageHandle)
+			rc = semanage_connect(self.semanageHandle)
+			if rc:
+				errorExit("Unable to connect to semanage")
 			(status, self.ulist) = semanage_user_list(self.semanageHandle)
 		self.type = type
 		self.selinuxdir = selinuxdir +"/"
@@ -336,18 +328,25 @@
 		print self.genoutput()
 
 	def write(self):
-		try:
-			fd = open(self.getFileContextDir()+"/file_contexts.homedirs", "w")
-			fd.write(self.genoutput())
-			fd.close()
-		except IOError, error:
-			sys.stderr.write("%s: %s\n" % ( sys.argv[0], error ))
-
+		fd = open(self.getFileContextDir()+"/file_contexts.homedirs", "w")
+		fd.write(self.genoutput())
+		fd.close()
 
 if os.getuid() > 0 or os.geteuid() > 0:
 	print _("You must be root to run %s.") % sys.argv[0]
 	sys.exit(1)
 
+try:
+	fd = open("/etc/shells", 'r')
+	VALID_SHELLS = fd.read().split("\n")
+	fd.close()
+	if "/sbin/nologin" in VALID_SHELLS:
+		VALID_SHELLS.remove("/sbin/nologin")
+	if "" in VALID_SHELLS:
+		VALID_SHELLS.remove("")
+except:
+	VALID_SHELLS = ['/bin/sh', '/bin/bash', '/bin/ash', '/bin/bsh', '/bin/ksh', '/usr/bin/ksh', '/usr/bin/pdksh', '/bin/tcsh', '/bin/csh', '/bin/zsh']
+
 #
 # This script will generate home dir file context
 # based off the homedir_template file, entries in the password file, and
@@ -369,15 +368,19 @@
 			directory = a
 		if o == '--help'  or o == "-h":
 			usage()
+except getopt.error, error:
+	errorExit(_("Options Error %s ") % error)
 
+if type == None:
+	type = getSELinuxType(directory)
 
-	if type == None:
-		type = getSELinuxType(directory)
+if len(cmds) != 0:
+	usage(1)
 
-	if len(cmds) != 0:
-		usage()

scripts/genhomedircon line 383

Quoted 10 lines
scripts/genhomedircon +5 -3
-	selconf = selinuxConfig(directory, type, usepwd)
+selconf = selinuxConfig(directory, type, usepwd)
+try:
 	selconf.write()
+except IOError, error:
+	sys.stderr.write("%s: %s\n" % ( sys.argv[0], error ))
+	sys.exit(1)
 
-except getopt.error, error:
-	errorExit(_("Options Error %s ") % error)

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#2 lore raw
Quoted 133 lines · Karl MacMillan
Quoted 132 lines · Michael C Thompson

I've noticed that genhomedircon does not have the proper return codes on some error and success paths. This patch addresses these return codes as follow:

* usage function by default returns 0, and the desired return code can be specified via a parameter. This facilitates the fix to the current behaviour that 1 is returned on 'genhomedircon -h'.

* I have noticed that as secadm (this is a bug? will start a separate thread) fails to successfully call semanage_connect(). The result of this operation is now checked, and the script will exit on error.

* If the attempt to write the homedir contexts out fails, a proper error code will be returned (previously, 1 would be returned).

This also moves the parsing of /etc/shells to after the uid check for a minimal time savings.

Thanks,
Mike

------------------------------------------------------------------------

diff -Naur policycoreutils-1.33.1/scripts/genhomedircon policycoreutils-1.33.1.dev/scripts/genhomedircon

scripts/genhomedircon +31 -26
@@ -29,17 +29,6 @@
 import gettext
 gettext.install('policycoreutils')
 
-try:
-	fd = open("/etc/shells", 'r')
-	VALID_SHELLS = fd.read().split("\n")
-	fd.close()
-	if "/sbin/nologin" in VALID_SHELLS:
-		VALID_SHELLS.remove("/sbin/nologin")
-	if "" in VALID_SHELLS:
-		VALID_SHELLS.remove("")
-except:
-	VALID_SHELLS = ['/bin/sh', '/bin/bash', '/bin/ash', '/bin/bsh', '/bin/ksh', '/usr/bin/ksh', '/usr/bin/pdksh', '/bin/tcsh', '/bin/csh', '/bin/zsh']
-
 def grep(file, var):
 	ret = ""
 	fd = open(file, 'r')
@@ -114,12 +103,13 @@
 		return val
 	return "targeted"
 
-def usage(error = ""):
+def usage(rc=0, error = ""):
 	if error != "":
 		sys.stderr.write("%s\n" % error)
+		rc = 1
 	sys.stderr.write("Usage: %s [ -d selinuxdir ] [-n | --nopasswd] [-t selinuxtype ]\n" % sys.argv[0])
 	sys.stderr.flush()
-	sys.exit(1)
+	sys.exit(rc)
 
 def warning(warning = ""):
 	sys.stderr.write("%s\n" % warning)
@@ -136,7 +126,9 @@
 		self.semanageHandle = semanage_handle_create()
 		self.semanaged = semanage_is_managed(self.semanageHandle)
 		if self.semanaged:
-			semanage_connect(self.semanageHandle)
+			rc = semanage_connect(self.semanageHandle)
+			if rc:
+				errorExit("Unable to connect to semanage")
 			(status, self.ulist) = semanage_user_list(self.semanageHandle)
 		self.type = type
 		self.selinuxdir = selinuxdir +"/"
@@ -336,18 +328,25 @@
 		print self.genoutput()
 
 	def write(self):
-		try:
-			fd = open(self.getFileContextDir()+"/file_contexts.homedirs", "w")
-			fd.write(self.genoutput())
-			fd.close()
-		except IOError, error:
-			sys.stderr.write("%s: %s\n" % ( sys.argv[0], error ))
-
+		fd = open(self.getFileContextDir()+"/file_contexts.homedirs", "w")
+		fd.write(self.genoutput())
+		fd.close()
 
 if os.getuid() > 0 or os.geteuid() > 0:
 	print _("You must be root to run %s.") % sys.argv[0]
 	sys.exit(1)
 
+try:
+	fd = open("/etc/shells", 'r')
+	VALID_SHELLS = fd.read().split("\n")
+	fd.close()
+	if "/sbin/nologin" in VALID_SHELLS:
+		VALID_SHELLS.remove("/sbin/nologin")
+	if "" in VALID_SHELLS:
+		VALID_SHELLS.remove("")
+except:
+	VALID_SHELLS = ['/bin/sh', '/bin/bash', '/bin/ash', '/bin/bsh', '/bin/ksh', '/usr/bin/ksh', '/usr/bin/pdksh', '/bin/tcsh', '/bin/csh', '/bin/zsh']
+
 #
 # This script will generate home dir file context
 # based off the homedir_template file, entries in the password file, and
@@ -369,15 +368,19 @@
 			directory = a
 		if o == '--help'  or o == "-h":
 			usage()
+except getopt.error, error:
+	errorExit(_("Options Error %s ") % error)
 
+if type == None:
+	type = getSELinuxType(directory)
 
-	if type == None:
-		type = getSELinuxType(directory)
+if len(cmds) != 0:
+	usage(1)
 
-	if len(cmds) != 0:
-		usage()
-	selconf = selinuxConfig(directory, type, usepwd)
+selconf = selinuxConfig(directory, type, usepwd)
+try:
 	selconf.write()
+except IOError, error:
+	sys.stderr.write("%s: %s\n" % ( sys.argv[0], error ))
+	sys.exit(1)
 
Quoted 4 lines

scripts/genhomedircon line 383

Quoted 2 lines
scripts/genhomedircon +0 -2
-except getopt.error, error:
-	errorExit(_("Options Error %s ") % error)
Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#3 lore raw

Merged as part of policycoreutils 1.33.5

Quoted 126 lines · Stephen Smalley
Quoted 125 lines · Karl MacMillan
Quoted 124 lines · Michael C Thompson

I've noticed that genhomedircon does not have the proper return codes on some error and success paths. This patch addresses these return codes as follow:

* usage function by default returns 0, and the desired return code can be specified via a parameter. This facilitates the fix to the current behaviour that 1 is returned on 'genhomedircon -h'.

* I have noticed that as secadm (this is a bug? will start a separate thread) fails to successfully call semanage_connect(). The result of this operation is now checked, and the script will exit on error.

* If the attempt to write the homedir contexts out fails, a proper error code will be returned (previously, 1 would be returned).

This also moves the parsing of /etc/shells to after the uid check for a minimal time savings.

Thanks,
Mike

------------------------------------------------------------------------

diff -Naur policycoreutils-1.33.1/scripts/genhomedircon policycoreutils-1.33.1.dev/scripts/genhomedircon

scripts/genhomedircon +31 -26
@@ -29,17 +29,6 @@
 import gettext
 gettext.install('policycoreutils')
 
-try:
-	fd = open("/etc/shells", 'r')
-	VALID_SHELLS = fd.read().split("\n")
-	fd.close()
-	if "/sbin/nologin" in VALID_SHELLS:
-		VALID_SHELLS.remove("/sbin/nologin")
-	if "" in VALID_SHELLS:
-		VALID_SHELLS.remove("")
-except:
-	VALID_SHELLS = ['/bin/sh', '/bin/bash', '/bin/ash', '/bin/bsh', '/bin/ksh', '/usr/bin/ksh', '/usr/bin/pdksh', '/bin/tcsh', '/bin/csh', '/bin/zsh']
-
 def grep(file, var):
 	ret = ""
 	fd = open(file, 'r')
@@ -114,12 +103,13 @@
 		return val
 	return "targeted"
 
-def usage(error = ""):
+def usage(rc=0, error = ""):
 	if error != "":
 		sys.stderr.write("%s\n" % error)
+		rc = 1
 	sys.stderr.write("Usage: %s [ -d selinuxdir ] [-n | --nopasswd] [-t selinuxtype ]\n" % sys.argv[0])
 	sys.stderr.flush()
-	sys.exit(1)
+	sys.exit(rc)
 
 def warning(warning = ""):
 	sys.stderr.write("%s\n" % warning)
@@ -136,7 +126,9 @@
 		self.semanageHandle = semanage_handle_create()
 		self.semanaged = semanage_is_managed(self.semanageHandle)
 		if self.semanaged:
-			semanage_connect(self.semanageHandle)
+			rc = semanage_connect(self.semanageHandle)
+			if rc:
+				errorExit("Unable to connect to semanage")
 			(status, self.ulist) = semanage_user_list(self.semanageHandle)
 		self.type = type
 		self.selinuxdir = selinuxdir +"/"
@@ -336,18 +328,25 @@
 		print self.genoutput()
 
 	def write(self):
-		try:
-			fd = open(self.getFileContextDir()+"/file_contexts.homedirs", "w")
-			fd.write(self.genoutput())
-			fd.close()
-		except IOError, error:
-			sys.stderr.write("%s: %s\n" % ( sys.argv[0], error ))
-
+		fd = open(self.getFileContextDir()+"/file_contexts.homedirs", "w")
+		fd.write(self.genoutput())
+		fd.close()
 
 if os.getuid() > 0 or os.geteuid() > 0:
 	print _("You must be root to run %s.") % sys.argv[0]
 	sys.exit(1)
 
+try:
+	fd = open("/etc/shells", 'r')
+	VALID_SHELLS = fd.read().split("\n")
+	fd.close()
+	if "/sbin/nologin" in VALID_SHELLS:
+		VALID_SHELLS.remove("/sbin/nologin")
+	if "" in VALID_SHELLS:
+		VALID_SHELLS.remove("")
+except:
+	VALID_SHELLS = ['/bin/sh', '/bin/bash', '/bin/ash', '/bin/bsh', '/bin/ksh', '/usr/bin/ksh', '/usr/bin/pdksh', '/bin/tcsh', '/bin/csh', '/bin/zsh']
+
 #
 # This script will generate home dir file context
 # based off the homedir_template file, entries in the password file, and
@@ -369,15 +368,19 @@
 			directory = a
 		if o == '--help'  or o == "-h":
 			usage()
+except getopt.error, error:
+	errorExit(_("Options Error %s ") % error)
 
+if type == None:
+	type = getSELinuxType(directory)
 
-	if type == None:
-		type = getSELinuxType(directory)
+if len(cmds) != 0:
+	usage(1)
 
-	if len(cmds) != 0:
-		usage()
-	selconf = selinuxConfig(directory, type, usepwd)
+selconf = selinuxConfig(directory, type, usepwd)
+try:
 	selconf.write()
+except IOError, error:
+	sys.stderr.write("%s: %s\n" % ( sys.argv[0], error ))
+	sys.exit(1)
 
Quoted 6 lines
Quoted 3 lines

scripts/genhomedircon line 383

Quoted 2 lines
scripts/genhomedircon +0 -2
-except getopt.error, error:
-	errorExit(_("Options Error %s ") % error)

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.