1
# Authors: Simo Sorce <ssorce redhat com>
2
# Alexander Bokovoy <abokovoy redhat com>
3
# Marko Myllynen <myllynen redhat com>
5
# Copyright (C) 2007-2011 Red Hat
6
# see file 'COPYING' for use and warranty information
8
# This program is free software; you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
24
from ipapython import ipautil
25
from ipapython.platform import base
27
# All what we allow exporting directly from this module
28
# Everything else is made available through these symbols when they directly imported into ipapython.services:
29
# authconfig -- class reference for platform-specific implementation of authconfig(8)
30
# service -- class reference for platform-specific implementation of a PlatformService class
31
# knownservices -- factory instance to access named services IPA cares about, names are ipapython.services.wellknownservices
32
# backup_and_replace_hostname -- platform-specific way to set hostname and make it persistent over reboots
33
# restore_context -- platform-sepcific way to restore security context, if applicable
34
__all__ = ['authconfig', 'service', 'knownservices', 'backup_and_replace_hostname', 'restore_context']
36
class DebianService(base.PlatformService):
37
def stop(self, instance_name="", capture_output=True):
38
ipautil.run(["/usr/sbin/service", self.service_name, "stop", instance_name], capture_output=capture_output)
40
def start(self, instance_name="", capture_output=True):
41
ipautil.run(["/usr/sbin/service", self.service_name, "start", instance_name], capture_output=capture_output)
43
def restart(self, instance_name="", capture_output=True):
44
ipautil.run(["/usr/sbin/service", self.service_name, "restart", instance_name], capture_output=capture_output)
46
def is_running(self, instance_name=""):
49
(sout,serr,rcode) = ipautil.run(["/usr/sbin/service", self.service_name, "status", instance_name])
50
if sout.find("NOT running") >= 0:
52
if sout.find("stop") >= 0:
54
except ipautil.CalledProcessError:
58
def is_installed(self):
61
ipautil.run(["/usr/sbin/service", self.service_name, "status"])
62
except ipautil.CalledProcessError, e:
64
# service is not installed or there is other serious issue
71
(sout,serr,rcode) = ipautil.run(["/sbin/chkconfig", self.service_name])
72
if sout.find("off") >= 0:
74
if sout.find("unknown service") >= 0:
76
except ipautil.CalledProcessError:
81
ipautil.run(["/sbin/chkconfig", self.service_name, "on"])
84
ipautil.run(["/sbin/chkconfig", self.service_name, "off"])
87
ipautil.run(["/sbin/chkconfig", "--add", self.service_name])
90
ipautil.run(["/sbin/chkconfig", "--del", self.service_name])
92
class DebianAuthConfig(base.AuthConfig):
94
AuthConfig class implements system-independent interface to configure
95
system authentication resources. In Red Hat-produced systems this is done with
96
authconfig(8) utility.
98
def __build_args(self):
100
for (option, value) in self.parameters.items():
101
if type(value) is bool:
103
args.append("--enable%s" % (option))
105
args.append("--disable%s" % (option))
106
elif type(value) in (tuple, list):
107
args.append("--%s" % (option))
108
args.append("%s" % (value[0]))
110
args.append("--%s" % (option))
112
args.append("--%s%s" % (option,value))
116
args = self.__build_args()
117
print "Would run on a Red Hat platform: /usr/sbin/authconfig " + " ".join(args)
118
ipautil.user_input("Please do the corresponding changes manually and press Enter")
119
#ipautil.run(["/usr/sbin/authconfig"]+args)
121
class DebianServices(base.KnownServices):
124
for s in base.wellknownservices:
125
if s == "certmonger":
126
services[s] = DebianService("certmonger")
127
elif s == "messagebus":
128
services[s] = DebianService("dbus")
130
services[s] = DebianService("ntp")
132
services[s] = DebianService(s)
133
# Call base class constructor. This will lock services to read-only
134
super(DebianServices, self).__init__(services)
136
authconfig = DebianAuthConfig
137
service = DebianService
138
knownservices = DebianServices()
140
def restore_context(filepath):
142
restore security context on the file path
143
SELinux equivalent is /sbin/restorecon <filepath>
145
restorecon's return values are not reliable so we have to
146
ignore them (BZ #739604).
148
ipautil.run() will do the logging.
150
if os.path.exists("/sbin/restorecon"):
151
ipautil.run(["/sbin/restorecon", filepath], raiseonerr=False)
153
def backup_and_replace_hostname(fstore, statestore, hostname):
154
network_filename = "/etc/hostname"
155
# Backup original /etc/hostname
156
fstore.backup_file(network_filename)
158
# Write new configuration
159
f = open(network_filename, 'w')
160
f.write(hostname + "\n")
164
ipautil.run(['/bin/hostname', hostname])
165
except ipautil.CalledProcessError, e:
166
print >>sys.stderr, "Failed to set this machine hostname to %s (%s)." % (hostname, str(e))
168
# For SE Linux environments it is important to reset SE labels to the expected ones
170
restore_context(network_filename)
171
except ipautil.CalledProcessError, e:
172
print >>sys.stderr, "Failed to set permissions for %s (%s)." % (network_filename, str(e))