Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 3 | from subprocess import call, Popen, PIPE |
Edward A. James | 75757c0 | 2016-07-15 09:11:18 -0500 | [diff] [blame] | 4 | from IPy import IP |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 5 | import sys |
| 6 | import subprocess |
| 7 | import dbus |
| 8 | import string |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 9 | import socket |
| 10 | import re |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 11 | import os |
| 12 | import fcntl |
| 13 | import glib |
| 14 | import gobject |
| 15 | import dbus.service |
| 16 | import dbus.mainloop.glib |
Vishwanatha Subbanna | 92a5d1e | 2016-08-30 21:23:32 +0530 | [diff] [blame] | 17 | from ConfigParser import SafeConfigParser |
| 18 | import glob |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 19 | |
Ratan Gupta | 8613b87 | 2016-10-07 17:15:58 +0530 | [diff] [blame] | 20 | #MAC address mask for locally administered. |
| 21 | MAC_LOCAL_ADMIN_MASK = 0x20000000000 |
| 22 | BROADCAST_MAC = 0xFFFFFFFFFFFF |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 23 | DBUS_NAME = 'org.openbmc.NetworkManager' |
| 24 | OBJ_NAME = '/org/openbmc/NetworkManager/Interface' |
| 25 | |
| 26 | network_providers = { |
Adriana Kobylak | 88c733b | 2016-02-03 16:46:58 -0600 | [diff] [blame] | 27 | 'networkd' : { |
| 28 | 'bus_name' : 'org.freedesktop.network1', |
| 29 | 'ip_object_name' : '/org/freedesktop/network1/network/default', |
| 30 | 'hw_object_name' : '/org/freedesktop/network1/link/_31', |
Hariharasubramanian R | 3a224e7 | 2016-02-10 12:32:08 -0600 | [diff] [blame] | 31 | 'ip_if_name' : 'org.freedesktop.network1.Network', |
| 32 | 'hw_if_name' : 'org.freedesktop.network1.Link', |
Adriana Kobylak | 88c733b | 2016-02-03 16:46:58 -0600 | [diff] [blame] | 33 | 'method' : 'org.freedesktop.network1.Network.SetAddr' |
| 34 | }, |
| 35 | 'NetworkManager' : { |
| 36 | 'bus_name' : 'org.freedesktop.NetworkManager', |
| 37 | 'ip_object_name' : '/org/freedesktop/NetworkManager', |
| 38 | 'hw_object_name' : '/org/freedesktop/NetworkManager', |
Hariharasubramanian R | 3a224e7 | 2016-02-10 12:32:08 -0600 | [diff] [blame] | 39 | 'ip_if_name' : 'org.freedesktop.NetworkManager', |
| 40 | 'hw_if_name' : 'org.freedesktop.NetworkManager', |
Adriana Kobylak | 88c733b | 2016-02-03 16:46:58 -0600 | [diff] [blame] | 41 | 'method' : 'org.freedesktop.NetworkManager' # FIXME: |
| 42 | }, |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | def getPrefixLen(mask): |
Adriana Kobylak | 88c733b | 2016-02-03 16:46:58 -0600 | [diff] [blame] | 46 | prefixLen = sum([bin(int(x)).count('1') for x in mask.split('.')]) |
| 47 | return prefixLen |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 48 | |
Vishwanatha Subbanna | 92a5d1e | 2016-08-30 21:23:32 +0530 | [diff] [blame] | 49 | # Enable / Disable the UseDHCP setting in .network file |
| 50 | def modifyNetConfig(confFile, usentp): |
| 51 | parser = SafeConfigParser() |
| 52 | parser.optionxform = str |
| 53 | parser.read(confFile) |
| 54 | sections = parser.sections() |
| 55 | |
| 56 | if "Match" not in sections: |
| 57 | raise NameError, "[Match] section not found" |
| 58 | |
| 59 | interface = parser.get('Match', 'Name') |
| 60 | if interface == '': |
| 61 | raise NameError, "Invalid interface" |
| 62 | |
| 63 | if "DHCP" not in sections: |
| 64 | parser.add_section("DHCP") |
| 65 | if usentp.lower() == "yes": |
| 66 | parser.set('DHCP', 'UseNTP', "true") |
| 67 | elif usentp.lower() == "no": |
| 68 | parser.set('DHCP', 'UseNTP', "false") |
| 69 | |
| 70 | print "Updating" + confFile + '\n' |
| 71 | with open(confFile, 'wb') as configfile: |
| 72 | parser.write(configfile) |
| 73 | |
| 74 | rc = call(["ip", "addr", "flush", interface]) |
| 75 | rc = call(["systemctl", "restart", "systemd-networkd.service"]) |
| 76 | rc = call(["systemctl", "try-restart", "systemd-timesyncd.service"]) |
| 77 | return rc |
| 78 | |
Ratan Gupta | 8613b87 | 2016-10-07 17:15:58 +0530 | [diff] [blame] | 79 | |
| 80 | def read_mac_provider_info(): |
| 81 | conf_file = os.path.join('/etc', 'network-manager.conf') |
| 82 | if not os.path.exists(conf_file): |
| 83 | raise IOError("Could not find %s" % conf_file) |
| 84 | |
| 85 | parser = SafeConfigParser() |
| 86 | parser.optionxform = str |
| 87 | parser.read(conf_file) |
| 88 | sections = parser.sections() |
| 89 | |
| 90 | path = parser.get('mac_loc', 'path') |
| 91 | if not path: |
| 92 | raise ValueError("Empty path") |
| 93 | |
| 94 | bus_name = parser.get('mac_loc', 'bus') |
| 95 | if not bus_name: |
| 96 | raise ValueError("Empty bus-name") |
| 97 | |
| 98 | intf = parser.get('mac_loc', 'interface') |
| 99 | if not intf: |
| 100 | raise ValueError("Empty intf-name") |
| 101 | |
| 102 | prop = parser.get('mac_loc', 'property') |
| 103 | if not prop: |
| 104 | raise ValueError("Empty prop-name") |
| 105 | |
| 106 | return bus_name, path, intf, prop |
| 107 | |
| 108 | |
| 109 | # Get Mac address from the eeprom |
| 110 | def get_mac_from_eeprom(): |
| 111 | bus = dbus.SystemBus() |
| 112 | bus_name, path, intf, prop = read_mac_provider_info() |
| 113 | obj = bus.get_object(bus_name, path) |
| 114 | dbus_method = obj.get_dbus_method("Get", dbus.PROPERTIES_IFACE) |
| 115 | return dbus_method(intf, prop) |
| 116 | |
| 117 | |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 118 | class IfAddr (): |
| 119 | def __init__ (self, family, scope, flags, prefixlen, addr, gw): |
| 120 | self.family = family |
| 121 | self.scope = scope |
| 122 | self.flags = flags |
| 123 | self.prefixlen = prefixlen |
| 124 | self.addr = addr |
| 125 | self.gw = gw |
| 126 | |
| 127 | class NetMan (dbus.service.Object): |
| 128 | def __init__(self, bus, name): |
| 129 | self.bus = bus |
| 130 | self.name = name |
| 131 | dbus.service.Object.__init__(self,bus,name) |
| 132 | |
| 133 | def setNetworkProvider(self, provider): |
| 134 | self.provider = provider |
| 135 | |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 136 | def _isvaliddev(self, device): |
| 137 | devices = os.listdir ("/sys/class/net") |
| 138 | if not device in devices : return False |
| 139 | else: return True |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 140 | |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 141 | def _ishwdev (self, device): |
| 142 | f = open ("/sys/class/net/"+device+"/type") |
| 143 | type = f.read() |
| 144 | return False if (int(type) == 772) else True |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 145 | |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 146 | def _isvalidmask (self, mask): |
| 147 | for x in mask.split('.'): |
| 148 | try: |
| 149 | y = int(x) |
| 150 | except: |
| 151 | return False |
| 152 | if y > 255: return False |
| 153 | return mask.count('.') == 3 |
| 154 | |
Ratan Gupta | 8613b87 | 2016-10-07 17:15:58 +0530 | [diff] [blame] | 155 | def validatemac(self, mac): |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 156 | macre = '([a-fA-F0-9]{2}[:|\-]?){6}' |
Ratan Gupta | 8613b87 | 2016-10-07 17:15:58 +0530 | [diff] [blame] | 157 | if re.compile(macre).search(mac) is None: |
| 158 | raise ValueError("Malformed MAC address") |
| 159 | |
| 160 | # Don't allow Broadcast or global unique mac |
| 161 | int_mac = int(mac.replace(":", ""), 16) |
| 162 | if not (int_mac ^ BROADCAST_MAC): |
| 163 | raise ValueError("Given Mac is BroadCast Mac Address") |
| 164 | |
| 165 | if not int_mac & MAC_LOCAL_ADMIN_MASK: |
| 166 | int_eep_mac = int(get_mac_from_eeprom(), 16) |
| 167 | if int_eep_mac != int_mac: |
| 168 | raise ValueError("Given MAC address is neither a local Admin type \ |
| 169 | nor is same as in eeprom") |
| 170 | |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 171 | |
Edward A. James | 75757c0 | 2016-07-15 09:11:18 -0500 | [diff] [blame] | 172 | def _isvalidipv4(self, ipstr, netmask): |
| 173 | ip_parts = ipstr.split(".") |
| 174 | if len(ip_parts) != 4: |
| 175 | return "Malformed" |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 176 | |
Edward A. James | 75757c0 | 2016-07-15 09:11:18 -0500 | [diff] [blame] | 177 | first, second, third, fourth = [int(part) for part in ip_parts] |
| 178 | if first == 0 and second == 0 and third == 0 and fourth == 0: |
| 179 | return "Invalid" # "this" network disallowed |
| 180 | if first == 169 and second == 254: |
| 181 | return "Link Local" |
| 182 | if first >= 224: |
| 183 | return "Invalid" # class D multicast and class E disallowed |
| 184 | if first == 192 and second == 88 and third == 99: |
| 185 | return "Invalid" # ipv6 relay |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 186 | |
Edward A. James | 75757c0 | 2016-07-15 09:11:18 -0500 | [diff] [blame] | 187 | # check validity against netmask |
| 188 | if netmask != '0': |
| 189 | ip_bin = (first << 24) + (second << 16) + (third << 8) + fourth |
| 190 | mask_parts = netmask.split(".") |
| 191 | if len(mask_parts) == 4: # long form netmask |
| 192 | mask_bin = (int(mask_parts[0]) << 24) + (int(mask_parts[1]) << 16) + (int(mask_parts[2]) << 8) + int(mask_parts[3]) |
| 193 | elif netmask.count(".") == 0: # short form netmask |
| 194 | mask_bin = 0xffffffff ^ (1 << 32 - int(netmask)) - 1 |
| 195 | else: |
| 196 | return "Malformed" # bad netmask |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 197 | |
Edward A. James | 75757c0 | 2016-07-15 09:11:18 -0500 | [diff] [blame] | 198 | if ip_bin & ~mask_bin == 0: |
| 199 | return "Invalid" # disallowed by this netmask |
| 200 | if ip_bin | mask_bin == 0xFFFFFFFF: |
| 201 | return "Invalid" # disallowed by this netmask |
| 202 | |
| 203 | return "Valid" |
| 204 | |
| 205 | |
| 206 | def _isvalidip(self, ipaddr, netmask = '0'): |
| 207 | try: |
| 208 | ip = IP(ipaddr) |
| 209 | except ValueError: |
| 210 | return "Malformed" |
| 211 | |
| 212 | ipstr = ip.strNormal(0) |
| 213 | ipstr_masked = ip.strNormal(2) |
| 214 | if ipstr_masked.count("/") != 0 and netmask == '0': |
| 215 | netmask = ipstr_masked.split("/")[1] |
| 216 | |
| 217 | if ip.version() == 4: # additional checks for ipv4 |
| 218 | return self._isvalidipv4(ipstr, netmask) |
| 219 | # TODO: check ipv6 openbmc/openbmc#496 |
| 220 | |
| 221 | return "Valid" |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 222 | |
| 223 | def _getAddr (self, target, device): |
| 224 | netprov = network_providers [self.provider] |
| 225 | bus_name = netprov ['bus_name'] |
| 226 | |
| 227 | if (target == "ip"): |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 228 | ipaddr = "" |
| 229 | defgw = "" |
| 230 | prefixlen = "0" |
| 231 | |
| 232 | proc = subprocess.Popen(["ip", "addr", "show", "dev", device], stdout=PIPE) |
| 233 | procout = proc.communicate() |
| 234 | if procout: |
| 235 | ipout = procout[0].splitlines()[2].strip() |
| 236 | ipaddr,prefixlen = ipout.split ()[1].split("/") |
| 237 | |
| 238 | proc = subprocess.Popen(["ip", "route", "show", "dev", device, "default", "0.0.0.0/0"], stdout=PIPE) |
| 239 | procout = proc.communicate() |
| 240 | if procout[0]: |
| 241 | ipout = procout[0].splitlines()[0].strip() |
| 242 | defgw = ipout.split ()[2] |
| 243 | |
| 244 | return 2, int(prefixlen), ipaddr, defgw |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 245 | |
| 246 | if (target == "mac"): |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 247 | proc = subprocess.Popen(["ip", "link", "show", "dev", device], stdout=PIPE) |
| 248 | ipout = proc.communicate()[0].splitlines()[1].strip() |
| 249 | mac = ipout.split ()[1] |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 250 | return mac |
| 251 | |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 252 | @dbus.service.method(DBUS_NAME, "", "") |
| 253 | def test(self): |
| 254 | print("TEST") |
| 255 | |
Vishwanatha Subbanna | 92a5d1e | 2016-08-30 21:23:32 +0530 | [diff] [blame] | 256 | @dbus.service.method(DBUS_NAME, "sas", "x") |
| 257 | def SetNtpServer (self, device, ntpservers): |
| 258 | if not self._isvaliddev (device) : raise ValueError, "Invalid Device" |
| 259 | |
| 260 | # Convert the array into space separated value string |
| 261 | ntp_ip = " ".join(ntpservers) |
| 262 | if not ntp_ip : raise ValueError, "Invalid Data" |
| 263 | |
| 264 | confFile = "/etc/systemd/network/00-bmc-" + device + ".network" |
| 265 | |
| 266 | parser = SafeConfigParser() |
| 267 | parser.optionxform = str |
| 268 | parser.read(confFile) |
| 269 | sections = parser.sections() |
| 270 | if "Match" not in sections: |
| 271 | raise NameError, "[Match] section not found" |
| 272 | |
| 273 | interface = parser.get('Match', 'Name') |
| 274 | if interface != device: |
| 275 | raise ValueError, "Device [" + device + "] Not Configured" |
| 276 | |
| 277 | if "Network" not in sections: |
| 278 | raise NameError, "[Network] section not found" |
| 279 | |
| 280 | parser.set('Network', 'NTP', ntp_ip) |
| 281 | print "Updating " + confFile + '\n' |
| 282 | with open(confFile, 'wb') as configfile: |
| 283 | parser.write(configfile) |
| 284 | rc = call(["ip", "addr", "flush", device]) |
| 285 | rc = call(["systemctl", "restart", "systemd-networkd.service"]) |
| 286 | rc = call(["systemctl", "try-restart", "systemd-timesyncd.service"]) |
| 287 | return rc |
| 288 | |
| 289 | @dbus.service.method(DBUS_NAME, "s", "x") |
| 290 | def UpdateUseNtpField (self, usentp): |
| 291 | filelist = glob.glob("/etc/systemd/network/*.network") |
| 292 | for configfile in filelist: |
| 293 | modifyNetConfig(configfile,usentp) |
| 294 | return 0 |
| 295 | |
Hariharasubramanian R | 3a224e7 | 2016-02-10 12:32:08 -0600 | [diff] [blame] | 296 | @dbus.service.method(DBUS_NAME, "s", "x") |
| 297 | def EnableDHCP (self, device): |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 298 | if not self._isvaliddev (device) : raise ValueError, "Invalid Device" |
| 299 | |
| 300 | confFile = "/etc/systemd/network/00-bmc-" + device + ".network" |
Hariharasubramanian R | 3a224e7 | 2016-02-10 12:32:08 -0600 | [diff] [blame] | 301 | |
| 302 | print("Making .network file...") |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 303 | try: |
| 304 | networkconf = open (confFile, "w+") |
| 305 | except IOError: |
| 306 | raise IOError, "Failed to open " + confFile |
| 307 | |
Hariharasubramanian R | 3a224e7 | 2016-02-10 12:32:08 -0600 | [diff] [blame] | 308 | networkconf.write ('[Match]'+ '\n') |
| 309 | networkconf.write ('Name=' + (device) + '\n') |
| 310 | networkconf.write ('[Network]' + '\n') |
Ratan Gupta | 968d203 | 2017-03-24 19:59:44 +0530 | [diff] [blame] | 311 | networkconf.write ('DHCP=yes' + '\n') |
Patrick Williams | ec01f6e | 2017-03-08 22:00:23 -0600 | [diff] [blame] | 312 | networkconf.write ('[DHCP]' + '\n') |
| 313 | networkconf.write ('ClientIdentifier=mac' + '\n') |
Hariharasubramanian R | 3a224e7 | 2016-02-10 12:32:08 -0600 | [diff] [blame] | 314 | networkconf.close () |
| 315 | |
| 316 | print("Restarting networkd service...") |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 317 | rc = call(["ip", "addr", "flush", device]) |
| 318 | rc = call(["systemctl", "restart", "systemd-networkd.service"]) |
| 319 | return rc |
Hariharasubramanian R | 3a224e7 | 2016-02-10 12:32:08 -0600 | [diff] [blame] | 320 | |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 321 | @dbus.service.method(DBUS_NAME, "ssss", "x") |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 322 | def SetAddress4 (self, device, ipaddr, netmask, gateway): |
| 323 | if not self._isvaliddev (device) : raise ValueError, "Invalid Device" |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 324 | if not self._isvalidmask (netmask) : raise ValueError, "Invalid Mask" |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 325 | prefixLen = getPrefixLen (netmask) |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 326 | if prefixLen == 0: raise ValueError, "Invalid Mask" |
Edward A. James | 75757c0 | 2016-07-15 09:11:18 -0500 | [diff] [blame] | 327 | valid = self._isvalidip (ipaddr, netmask) |
| 328 | if valid != "Valid": raise ValueError, valid + " IP Address" |
| 329 | valid = self._isvalidip (gateway) |
| 330 | if valid != "Valid": raise ValueError, valid + " IP Address" |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 331 | |
| 332 | confFile = "/etc/systemd/network/00-bmc-" + device + ".network" |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 333 | |
| 334 | print("Making .network file...") |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 335 | try: |
| 336 | networkconf = open (confFile, "w+") |
| 337 | except IOError: |
| 338 | raise IOError, "Failed to open " + confFile |
| 339 | |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 340 | networkconf.write ('[Match]'+ '\n') |
| 341 | networkconf.write ('Name=' + (device) + '\n') |
| 342 | networkconf.write ('[Network]' + '\n') |
| 343 | networkconf.write ('Address=' + ipaddr + '/' + str(prefixLen) + '\n') |
| 344 | networkconf.write ('Gateway=' + gateway + '\n') |
Chris Austen | b13a3cd | 2016-02-01 18:18:21 -0600 | [diff] [blame] | 345 | networkconf.close() |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 346 | |
| 347 | print("Restarting networkd service...") |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 348 | rc = call(["ip", "addr", "flush", device]) |
| 349 | rc = call(["systemctl", "restart", "systemd-networkd.service"]) |
| 350 | return rc |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 351 | |
Hariharasubramanian R | cc28a6f | 2016-04-29 12:25:12 -0500 | [diff] [blame] | 352 | @dbus.service.method(DBUS_NAME, "s", "s") |
| 353 | def GetAddressType (self, device): |
| 354 | if not self._isvaliddev (device) : raise ValueError, "Invalid Device" |
| 355 | |
| 356 | confFile = "/etc/systemd/network/00-bmc-" + device + ".network" |
| 357 | if not os.path.exists(confFile): |
| 358 | print "Config file (%s) not found !" % confFile |
| 359 | netprov = network_providers [self.provider] |
| 360 | bus_name = netprov ['bus_name'] |
| 361 | obj_name = netprov ['ip_object_name'] |
| 362 | o = self.bus.get_object(bus_name, obj_name, introspect=False) |
| 363 | i = dbus.Interface(o, 'org.freedesktop.DBus.Properties') |
| 364 | f = i.Get (netprov ['ip_if_name'], "SourcePath") |
| 365 | print "Using default networkd config file (%s)" % f |
| 366 | confFile = f |
| 367 | |
| 368 | with open(confFile, "r") as f: |
| 369 | for line in f: |
| 370 | config = line.split ("=") |
| 371 | if (len (config) < 2) : continue |
| 372 | if config [0].upper() == "DHCP": |
| 373 | v = config[1].strip().upper() |
| 374 | if (v=="YES" or v=="IPV4" or v=="IPV6"): |
| 375 | return "DHCP" |
| 376 | return "STATIC" |
| 377 | |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 378 | #family, prefixlen, ip, defgw |
| 379 | @dbus.service.method(DBUS_NAME, "s", "iyss") |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 380 | def GetAddress4 (self, device): |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 381 | if not self._isvaliddev (device) : raise ValueError, "Invalid Device" |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 382 | return self._getAddr ("ip", device) |
| 383 | |
| 384 | @dbus.service.method(DBUS_NAME, "s", "s") |
| 385 | def GetHwAddress (self, device): |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 386 | if not self._isvaliddev (device) : raise ValueError, "Invalid Device" |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 387 | return self._getAddr ("mac", device) |
| 388 | |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 389 | @dbus.service.method(DBUS_NAME, "ss", "i") |
| 390 | def SetHwAddress (self, device, mac): |
| 391 | if not self._isvaliddev (device) : raise ValueError, "Invalid Device" |
| 392 | if not self._ishwdev (device) : raise ValueError, "Not a Hardware Device" |
Ratan Gupta | 8613b87 | 2016-10-07 17:15:58 +0530 | [diff] [blame] | 393 | |
| 394 | self.validatemac(mac) |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 395 | |
Adriana Kobylak | 88c733b | 2016-02-03 16:46:58 -0600 | [diff] [blame] | 396 | rc = subprocess.call(["fw_setenv", "ethaddr", mac]) |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 397 | |
| 398 | print("Restarting networkd service...") |
| 399 | rc = call(["ip", "link", "set", "dev", device, "down"]) |
| 400 | rc = call(["ip", "link", "set", "dev", device, "address", mac]) |
| 401 | rc = call(["ip", "link", "set", "dev", device, "up"]) |
| 402 | |
| 403 | rc = call(["systemctl", "restart", "systemd-networkd.service"]) |
Adriana Kobylak | 88c733b | 2016-02-03 16:46:58 -0600 | [diff] [blame] | 404 | return rc |
| 405 | |
vishwa | 5c912c8 | 2016-03-24 07:59:28 -0500 | [diff] [blame] | 406 | #string of nameservers |
| 407 | @dbus.service.method(DBUS_NAME,"s", "s") |
| 408 | def SetNameServers (self, nameservers): |
| 409 | dns_entry = nameservers.split() |
| 410 | fail_msg = '' |
| 411 | dhcp_auto = False |
| 412 | file_opened = False |
| 413 | if len(dns_entry) > 0: |
| 414 | for dns in dns_entry: |
Edward A. James | 75757c0 | 2016-07-15 09:11:18 -0500 | [diff] [blame] | 415 | valid = self._isvalidip (dns) |
| 416 | if valid != "Valid": |
vishwa | 5c912c8 | 2016-03-24 07:59:28 -0500 | [diff] [blame] | 417 | if dns == "DHCP_AUTO=": |
| 418 | #This DNS is supplied by DHCP. |
| 419 | dhcp_auto = True |
| 420 | else: |
Edward A. James | 75757c0 | 2016-07-15 09:11:18 -0500 | [diff] [blame] | 421 | print valid + " DNS Address [" + dns + "]" |
vishwa | 5c912c8 | 2016-03-24 07:59:28 -0500 | [diff] [blame] | 422 | fail_msg = fail_msg + '[' + dns + ']' |
| 423 | else: |
| 424 | #Only over write on a first valid input |
| 425 | if file_opened == False: |
| 426 | resolv_conf = open("/etc/resolv.conf",'w') |
| 427 | file_opened = True |
| 428 | if dhcp_auto == True: |
| 429 | resolv_conf.write("### Generated automatically via DHCP ###\n") |
| 430 | else: |
| 431 | resolv_conf.write("### Generated manually via dbus settings ###\n") |
| 432 | dns_ip = 'nameserver ' + dns + '\n' |
| 433 | resolv_conf.write(dns_ip) |
| 434 | if file_opened == True: |
| 435 | resolv_conf.close() |
| 436 | else: |
| 437 | raise ValueError, "Invalid DNS entry" |
| 438 | if len(fail_msg) > 0: |
| 439 | return 'Failures encountered processing' + fail_msg |
| 440 | else: |
| 441 | return "DNS entries updated Successfully" |
| 442 | |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 443 | def main(): |
| 444 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 445 | bus = dbus.SystemBus() |
| 446 | name = dbus.service.BusName(DBUS_NAME, bus) |
| 447 | obj = NetMan (bus, OBJ_NAME) |
| 448 | obj.setNetworkProvider ("networkd") |
| 449 | mainloop = gobject.MainLoop() |
| 450 | print("Started") |
| 451 | mainloop.run() |
| 452 | |
| 453 | if __name__ == '__main__': |
Adriana Kobylak | e150bfd | 2016-02-01 21:47:34 -0600 | [diff] [blame] | 454 | sys.exit(main()) |