Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 1 | #!/usr/bin/python -u |
| 2 | |
| 3 | import gobject |
| 4 | import dbus |
| 5 | import dbus.service |
| 6 | import dbus.mainloop.glib |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 7 | import os |
| 8 | import os.path as path |
Adriana Kobylak | 256be78 | 2016-08-24 15:43:16 -0500 | [diff] [blame] | 9 | import sys |
Brad Bishop | 5ef7fe6 | 2016-05-17 08:39:17 -0400 | [diff] [blame] | 10 | from obmc.dbuslib.bindings import DbusProperties, get_dbus |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 11 | from IPy import IP |
Adriana Kobylak | 256be78 | 2016-08-24 15:43:16 -0500 | [diff] [blame] | 12 | |
Brad Bishop | 966fcd5 | 2016-09-29 09:22:32 -0400 | [diff] [blame] | 13 | settings_file_path = os.path.join( |
| 14 | sys.prefix, |
| 15 | 'share', |
| 16 | 'phosphor-settings') |
Adriana Kobylak | 256be78 | 2016-08-24 15:43:16 -0500 | [diff] [blame] | 17 | sys.path.insert(1, settings_file_path) |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 18 | import settings_file as s |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 19 | import re |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 20 | |
| 21 | DBUS_NAME = 'org.openbmc.settings.Host' |
| 22 | OBJ_NAME = '/org/openbmc/settings/host0' |
| 23 | CONTROL_INTF = 'org.openbmc.Settings' |
| 24 | |
Brad Bishop | 2a9fe66 | 2016-08-31 12:37:35 -0400 | [diff] [blame] | 25 | |
Brad Bishop | 5ef7fe6 | 2016-05-17 08:39:17 -0400 | [diff] [blame] | 26 | class HostSettingsObject(DbusProperties): |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 27 | def __init__(self, bus, name, settings, path): |
Brad Bishop | c1e5e9f | 2016-09-29 09:40:01 -0400 | [diff] [blame^] | 28 | super(HostSettingsObject, self).__init__( |
| 29 | conn=bus, |
| 30 | object_path=name, |
| 31 | validator=self.input_validator) |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 32 | self.bus = bus |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 33 | self.path = path |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 34 | # Needed to ignore the validation on default networkconfig values as |
| 35 | # opposed to user giving the same. |
| 36 | self.adminmode = True |
| 37 | |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 38 | if not os.path.exists(path): |
| 39 | os.mkdir(path) |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 40 | |
| 41 | # Listen to changes in the property values and sync them to the BMC |
Brad Bishop | 2a9fe66 | 2016-08-31 12:37:35 -0400 | [diff] [blame] | 42 | bus.add_signal_receiver( |
| 43 | self.settings_signal_handler, |
| 44 | dbus_interface="org.freedesktop.DBus.Properties", |
| 45 | signal_name="PropertiesChanged", |
| 46 | path="/org/openbmc/settings/host0") |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 47 | |
| 48 | # Create the dbus properties |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 49 | for i in settings[DBUS_NAME].iterkeys(): |
| 50 | shk = settings[DBUS_NAME][i] |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 51 | self.set_settings_property(shk['name'], |
| 52 | shk['type'], |
| 53 | shk['default']) |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 54 | # Done with consuming factory settings. |
| 55 | self.adminmode = False |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 56 | |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 57 | def get_bmc_value(self, name): |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 58 | try: |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 59 | with open(path.join(self.path, name), 'r') as f: |
| 60 | return f.read() |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 61 | except (IOError): |
| 62 | pass |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 63 | return None |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 64 | |
Brad Bishop | 2a9fe66 | 2016-08-31 12:37:35 -0400 | [diff] [blame] | 65 | # Create dbus properties based on bmc value. |
| 66 | # This will be either a value previously set, |
| 67 | # or the default file value if the BMC value |
| 68 | # does not exist. |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 69 | def set_settings_property(self, name, type, value): |
| 70 | bmcv = self.get_bmc_value(name) |
| 71 | if bmcv: |
| 72 | value = bmcv |
Brad Bishop | 2a9fe66 | 2016-08-31 12:37:35 -0400 | [diff] [blame] | 73 | if type == "i": |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 74 | self.Set(DBUS_NAME, name, int(value)) |
Brad Bishop | 2a9fe66 | 2016-08-31 12:37:35 -0400 | [diff] [blame] | 75 | elif type == "s": |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 76 | self.Set(DBUS_NAME, name, str(value)) |
Brad Bishop | 2a9fe66 | 2016-08-31 12:37:35 -0400 | [diff] [blame] | 77 | elif type == "b": |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 78 | self.Set(DBUS_NAME, name, bool(value)) |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 79 | |
| 80 | # Save the settings to the BMC. This will write the settings value in |
| 81 | # individual files named by the property name to the BMC. |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 82 | def set_system_settings(self, name, value): |
| 83 | bmcv = self.get_bmc_value(name) |
| 84 | if bmcv != value: |
| 85 | filepath = path.join(self.path, name) |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 86 | with open(filepath, 'w') as f: |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 87 | f.write(str(value)) |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 88 | |
| 89 | # Signal handler for when one ore more settings properties were updated. |
| 90 | # This will sync the changes to the BMC. |
Brad Bishop | 2a9fe66 | 2016-08-31 12:37:35 -0400 | [diff] [blame] | 91 | def settings_signal_handler( |
| 92 | self, interface_name, changed_properties, invalidated_properties): |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 93 | for name, value in changed_properties.items(): |
| 94 | self.set_system_settings(name, value) |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 95 | |
| 96 | # Placeholder signal. Needed to register the settings interface. |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 97 | @dbus.service.signal(DBUS_NAME, signature='s') |
| 98 | def SettingsUpdated(self, sname): |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 99 | pass |
| 100 | |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 101 | def validate_regex(self, regex, value): |
| 102 | if not re.compile(regex).search(value): |
Brad Bishop | c1e5e9f | 2016-09-29 09:40:01 -0400 | [diff] [blame^] | 103 | raise ValueError("Invalid input. Data does not satisfy regex") |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 104 | |
| 105 | def validate_range(self, min, max, value): |
| 106 | if value not in range(min, max): |
Brad Bishop | c1e5e9f | 2016-09-29 09:40:01 -0400 | [diff] [blame^] | 107 | raise ValueError("Invalid input. Data not in allowed range") |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 108 | |
| 109 | def validate_list_ignore_case(self, lst, value): |
| 110 | if value.lower() not in map(lambda val: val.lower(), lst): |
Brad Bishop | c1e5e9f | 2016-09-29 09:40:01 -0400 | [diff] [blame^] | 111 | raise ValueError("Invalid input. Data not in allowed values") |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 112 | |
| 113 | # validate host network configuration |
| 114 | # need "ipaddress=,prefix=,gateway=,mac=,addr_type=" |
| 115 | # Must be able to handle any order |
| 116 | def validate_net_config(self, value): |
| 117 | if self.adminmode: |
| 118 | return |
| 119 | |
| 120 | # Need all of these to be given by the user. |
| 121 | user_config = [] |
| 122 | all_config = ['ipaddress', 'prefix', 'gateway', 'mac', 'addr_type'] |
| 123 | |
| 124 | # This has a hard data format mentioned above so no blanks allowed. |
| 125 | if value.count(" ") or value.count("=") != 5: |
Brad Bishop | c1e5e9f | 2016-09-29 09:40:01 -0400 | [diff] [blame^] | 126 | raise ValueError("Invalid Network Data. No white spaces allowed") |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 127 | |
| 128 | config = value.split(',') |
| 129 | for key_value in config: |
Brad Bishop | c1e5e9f | 2016-09-29 09:40:01 -0400 | [diff] [blame^] | 130 | key, value = key_value.split('=') |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 131 | if not key or not value: |
Brad Bishop | c1e5e9f | 2016-09-29 09:40:01 -0400 | [diff] [blame^] | 132 | raise ValueError("Invalid key or Data") |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 133 | |
| 134 | # Add the current key seen so we can compare at the end to see |
| 135 | # if all values have been given |
| 136 | user_config.append(key.lower()) |
| 137 | |
| 138 | if key.lower() == 'ipaddress' or key.lower() == 'gateway': |
Brad Bishop | c1e5e9f | 2016-09-29 09:40:01 -0400 | [diff] [blame^] | 139 | IP(value) |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 140 | |
| 141 | elif key.lower() == 'mac': |
| 142 | regex = '([a-fA-F0-9]{2}[:|\-]?){6}' |
| 143 | self.validate_regex(regex, value) |
| 144 | |
| 145 | elif key.lower() == 'prefix': |
| 146 | self.validate_range(0, 33, int(value)) |
| 147 | |
| 148 | elif key.lower() == 'addr_type': |
| 149 | allowed = ["STATIC", "DYNAMIC"] |
| 150 | self.validate_list_ignore_case(allowed, value) |
| 151 | |
| 152 | # Did user pass everything ?? |
| 153 | if set(all_config) - set(user_config): |
Brad Bishop | c1e5e9f | 2016-09-29 09:40:01 -0400 | [diff] [blame^] | 154 | raise ValueError( |
| 155 | "Invalid Network Data. All information is mandatory") |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 156 | |
| 157 | # Validate to see if the changes are in order |
| 158 | def input_validator(self, iface, proprty, value): |
| 159 | settings = s.SETTINGS |
| 160 | shk = {} |
| 161 | for key in settings[iface].iterkeys(): |
| 162 | if proprty == settings[iface][key]['name']: |
| 163 | shk = settings[iface][key] |
| 164 | break |
| 165 | |
| 166 | # User entered key is not present |
Brad Bishop | c1e5e9f | 2016-09-29 09:40:01 -0400 | [diff] [blame^] | 167 | if not shk: |
| 168 | raise KeyError("Invalid Property") |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 169 | |
| 170 | if shk['validation'] == 'list': |
| 171 | self.validate_list_ignore_case(shk['allowed'], value) |
| 172 | |
| 173 | elif shk['validation'] == 'range': |
| 174 | self.validate_range(shk['min'], shk['max']+1, value) |
| 175 | |
| 176 | elif shk['validation'] == 'regex': |
| 177 | self.validate_regex(shk['regex'], value) |
| 178 | |
| 179 | elif shk['validation'] == 'custom': |
Brad Bishop | c1e5e9f | 2016-09-29 09:40:01 -0400 | [diff] [blame^] | 180 | getattr(self, shk['method'])(value) |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 181 | |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 182 | if __name__ == '__main__': |
| 183 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 184 | |
Brad Bishop | 5ef7fe6 | 2016-05-17 08:39:17 -0400 | [diff] [blame] | 185 | bus = get_dbus() |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 186 | obj = HostSettingsObject(bus, OBJ_NAME, s.SETTINGS, "/var/lib/obmc/") |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 187 | mainloop = gobject.MainLoop() |
| 188 | |
Brad Bishop | 2cbef3d | 2016-08-31 12:40:13 -0400 | [diff] [blame] | 189 | obj.unmask_signals() |
| 190 | name = dbus.service.BusName(DBUS_NAME, bus) |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 191 | print "Running HostSettingsService" |
| 192 | mainloop.run() |
Brad Bishop | 31c42f0 | 2016-09-29 09:35:32 -0400 | [diff] [blame] | 193 | |
| 194 | # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |