Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

1210

1211

1212

1213

1214

1215

1216

1217

1218

1219

1220

1221

1222

1223

1224

1225

1226

1227

1228

1229

1230

1231

1232

1233

1234

1235

1236

1237

1238

1239

1240

1241

1242

1243

1244

1245

1246

1247

1248

# Network configuration proxy to NetworkManager 

# 

# Copyright (C) 2013,2017 Red Hat, Inc. 

# 

# This copyrighted material is made available to anyone wishing to use, 

# modify, copy, or redistribute it subject to the terms and conditions of 

# the GNU General Public License v.2, or (at your option) any later version. 

# This program is distributed in the hope that it will be useful, but WITHOUT 

# ANY WARRANTY expressed or implied, including the implied warranties of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 

# Public License for more details. You should have received a copy of the 

# GNU General Public License along with this program; if not, write to the 

# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 

# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the 

# source code or documentation are not subject to the GNU General Public 

# License and may only be used or replicated with the express permission of 

# Red Hat, Inc. 

# 

 

import gi 

gi.require_version("Gio", "2.0") 

gi.require_version("NM", "1.0") 

 

from gi.repository import Gio 

from gi.repository import NM 

from pyanaconda.core.glib import GError, Variant, VariantType 

import struct 

import socket 

 

from pyanaconda.anaconda_loggers import get_module_logger 

log = get_module_logger(__name__) 

 

from pyanaconda.core.constants import DEFAULT_DBUS_TIMEOUT 

from pyanaconda.flags import flags, can_touch_runtime_system 

 

supported_device_types = [ 

NM.DeviceType.ETHERNET, 

NM.DeviceType.WIFI, 

NM.DeviceType.INFINIBAND, 

NM.DeviceType.BOND, 

NM.DeviceType.VLAN, 

NM.DeviceType.BRIDGE, 

NM.DeviceType.TEAM, 

] 

 

class UnknownDeviceError(ValueError): 

"""Device of specified name was not found by NM""" 

def __str__(self): 

return self.__repr__() 

 

class UnmanagedDeviceError(Exception): 

"""Device of specified name is not managed by NM or unavailable""" 

def __str__(self): 

return self.__repr__() 

 

class DeviceNotActiveError(Exception): 

"""Device of specified name is not active""" 

def __str__(self): 

return self.__repr__() 

 

class PropertyNotFoundError(ValueError): 

"""Property of NM object was not found""" 

def __str__(self): 

return self.__repr__() 

 

class SettingsNotFoundError(ValueError): 

"""Settings NMRemoteConnection object was not found""" 

def __str__(self): 

return self.__repr__() 

 

class MultipleSettingsFoundError(ValueError): 

"""Too many NMRemoteConnection objects were found""" 

def __str__(self): 

return self.__repr__() 

 

class UnknownMethodGetError(Exception): 

"""Object does not have Get, most probably being invalid""" 

def __str__(self): 

return self.__repr__() 

 

# bug #1062417 e.g. for ethernet device without link 

class UnknownConnectionError(Exception): 

"""Connection is not available for the device""" 

def __str__(self): 

return self.__repr__() 

 

class AddConnectionError(Exception): 

"""Connection is not available for the device""" 

def __str__(self): 

return self.__repr__() 

 

# bug #1039006 

class BondOptionsError(AddConnectionError): 

pass 

 

def _get_proxy(bus_type=Gio.BusType.SYSTEM, 

proxy_flags=Gio.DBusProxyFlags.NONE, 

info=None, 

name="org.freedesktop.NetworkManager", 

object_path="/org/freedesktop/NetworkManager", 

interface_name="org.freedesktop.NetworkManager", 

cancellable=None): 

try: 

proxy = Gio.DBusProxy.new_for_bus_sync(bus_type, 

proxy_flags, 

info, 

name, 

object_path, 

interface_name, 

cancellable) 

except GError as e: 

if can_touch_runtime_system("raise GLib.GError", touch_live=True): 

raise 

 

log.error("_get_proxy failed: %s", e) 

proxy = None 

 

return proxy 

 

def _get_property(object_path, prop, interface_name_suffix=""): 

interface_name = "org.freedesktop.NetworkManager" + interface_name_suffix 

proxy = _get_proxy(object_path=object_path, interface_name="org.freedesktop.DBus.Properties") 

if not proxy: 

return None 

 

try: 

prop = proxy.Get('(ss)', interface_name, prop) 

except GError as e: 

if ("org.freedesktop.DBus.Error.AccessDenied" in e.message or 

"org.freedesktop.DBus.Error.InvalidArgs" in e.message): 

return None 

elif "org.freedesktop.DBus.Error.UnknownMethod" in e.message: 

raise UnknownMethodGetError 

else: 

raise 

 

return prop 

 

def nm_state(): 

"""Return state of NetworkManager 

 

:return: state of NetworkManager 

:rtype: integer 

""" 

prop = _get_property("/org/freedesktop/NetworkManager", "State") 

 

# If this is an image/dir install assume the network is up 

if not prop and (flags.imageInstall or flags.dirInstall): 

return NM.State.CONNECTED_GLOBAL 

else: 

return prop 

 

# FIXME - use just GLOBAL? There is some connectivity checking 

# for GLOBAL in NM (nm_connectivity_get_connected), not sure if 

# and how it is implemented. 

# Also see Gio g_network_monitor_can_reach. 

def nm_is_connected(): 

"""Is NetworkManager connected? 

 

:return: True if NM is connected, False otherwise. 

:rtype: bool 

""" 

return nm_state() in (NM.State.CONNECTED_GLOBAL, 

NM.State.CONNECTED_SITE, 

NM.State.CONNECTED_LOCAL) 

 

def nm_is_connecting(): 

"""Is NetworkManager connecting? 

 

:return: True if NM is in CONNECTING state, False otherwise. 

:rtype: bool 

""" 

return nm_state() == NM.State.CONNECTING 

 

def nm_devices(): 

"""Return names of network devices supported in installer. 

 

:return: names of network devices supported in installer 

:rtype: list of strings 

""" 

 

interfaces = [] 

 

proxy = _get_proxy() 

if not proxy: 

return [] 

 

devices = proxy.GetDevices() 

for device in devices: 

device_type = _get_property(device, "DeviceType", ".Device") 

if device_type not in supported_device_types: 

continue 

iface = _get_property(device, "Interface", ".Device") 

interfaces.append(iface) 

 

return interfaces 

 

def nm_activated_devices(): 

"""Return names of activated network devices. 

 

:return: names of activated network devices 

:rtype: list of strings 

""" 

 

interfaces = [] 

 

active_connections = _get_property("/org/freedesktop/NetworkManager", "ActiveConnections") 

if not active_connections: 

return [] 

 

for ac in active_connections: 

try: 

state = _get_property(ac, "State", ".Connection.Active") 

except UnknownMethodGetError: 

continue 

if state != NM.ActiveConnectionState.ACTIVATED: 

continue 

devices = _get_property(ac, "Devices", ".Connection.Active") 

for device in devices: 

iface = _get_property(device, "IpInterface", ".Device") 

if not iface: 

iface = _get_property(device, "Interface", ".Device") 

interfaces.append(iface) 

 

return interfaces 

 

def _get_object_iface_names(object_path): 

connection = Gio.bus_get_sync(Gio.BusType.SYSTEM, None) 

res_xml = connection.call_sync("org.freedesktop.NetworkManager", 

object_path, 

"org.freedesktop.DBus.Introspectable", 

"Introspect", 

None, 

VariantType.new("(s)"), 

Gio.DBusCallFlags.NONE, 

-1, 

None) 

node_info = Gio.DBusNodeInfo.new_for_xml(res_xml[0]) 

return [iface.name for iface in node_info.interfaces] 

 

def _device_type_specific_interface(device): 

ifaces = _get_object_iface_names(device) 

for iface in ifaces: 

if iface.startswith("org.freedesktop.NetworkManager.Device.") \ 

and iface != "org.freedesktop.NetworkManager.Device.Statistics": 

return iface 

return None 

 

def nm_device_property(name, prop): 

"""Return value of device NM property 

 

:param name: name of device 

:type name: str 

:param prop: property 

:type name: str 

:return: value of device's property 

:rtype: unpacked GDBus value 

:raise UnknownDeviceError: if device is not found 

:raise PropertyNotFoundError: if property is not found 

""" 

 

retval = None 

 

proxy = _get_proxy() 

try: 

device = proxy.GetDeviceByIpIface('(s)', name) 

except GError as e: 

if "org.freedesktop.NetworkManager.UnknownDevice" in e.message: 

raise UnknownDeviceError(name, e) 

raise 

 

retval = _get_property(device, prop, ".Device") 

if not retval: 

# Look in device type based interface 

interface = _device_type_specific_interface(device) 

if interface: 

retval = _get_property(device, prop, interface[30:]) 

if not retval: 

raise PropertyNotFoundError(prop) 

else: 

raise PropertyNotFoundError(prop) 

 

return retval 

 

def nm_device_type_is_wifi(name): 

"""Is the type of device wifi? 

 

:param name: name of device 

:type name: str 

:return: True if type of device is WIFI, False otherwise 

:rtype: bool 

:raise UnknownDeviceError: if device is not found 

:raise PropertyNotFoundError: if property is not found 

""" 

return nm_device_type(name) == NM.DeviceType.WIFI 

 

def nm_device_type_is_ethernet(name): 

"""Is the type of device ethernet? 

 

:param name: name of device 

:type name: str 

:return: True if type of device is ETHERNET, False otherwise 

:rtype: bool 

:raise UnknownDeviceError: if device is not found 

:raise PropertyNotFoundError: if property is not found 

""" 

return nm_device_type(name) == NM.DeviceType.ETHERNET 

 

def nm_device_type_is_infiniband(name): 

"""Is the type of device infiniband? 

 

Exceptions: 

UnknownDeviceError if device is not found 

PropertyNotFoundError if type is not found 

""" 

return nm_device_type(name) == NM.DeviceType.INFINIBAND 

 

def nm_device_type_is_bond(name): 

"""Is the type of device bond? 

 

:param name: name of device 

:type name: str 

:return: True if type of device is BOND, False otherwise 

:rtype: bool 

:raise UnknownDeviceError: if device is not found 

:raise PropertyNotFoundError: if property is not found 

""" 

return nm_device_type(name) == NM.DeviceType.BOND 

 

def nm_device_type_is_team(name): 

"""Is the type of device team? 

 

:param name: name of device 

:type name: str 

:return: True if type of device is TEAM, False otherwise 

:rtype: bool 

:raise UnknownDeviceError: if device is not found 

:raise PropertyNotFoundError: if property is not found 

""" 

return nm_device_type(name) == NM.DeviceType.TEAM 

 

def nm_device_type_is_bridge(name): 

"""Is the type of device bridge? 

 

:param name: name of device 

:type name: str 

:return: True if type of device is BRIDGE, False otherwise 

:rtype: bool 

:raise UnknownDeviceError: if device is not found 

:raise PropertyNotFoundError: if property is not found 

""" 

return nm_device_type(name) == NM.DeviceType.BRIDGE 

 

def nm_device_type_is_vlan(name): 

"""Is the type of device vlan? 

 

:param name: name of device 

:type name: str 

:return: True if type of device is VLAN, False otherwise 

:rtype: bool 

:raise UnknownDeviceError: if device is not found 

:raise PropertyNotFoundError: if property is not found 

""" 

return nm_device_type(name) == NM.DeviceType.VLAN 

 

def nm_device_is_slave(name): 

"""Is the device a slave? 

 

Exceptions: 

UnknownDeviceError if device is not found 

""" 

active_con = nm_device_property(name, 'ActiveConnection') 

if active_con == "/": 

return False 

 

try: 

master = _get_property(active_con, "Master", ".Connection.Active") 

except UnknownMethodGetError: 

# don't crash on obsolete ActiveConnection objects 

return False 

 

return master and master != "/" 

 

def nm_device_hwaddress(name): 

"""Return active hardware address of device ('HwAddress' property) 

 

:param name: name of device 

:type name: str 

:return: active hardware address of device ('HwAddress' property) 

:rtype: str 

:raise UnknownDeviceError: if device is not found 

:raise PropertyNotFoundError: if 'HwAddress' property is not found 

""" 

return nm_device_property(name, "HwAddress") 

 

def nm_device_perm_hwaddress(name): 

"""Return active hardware address of device ('PermHwAddress' property) 

 

:param name: name of device 

:type name: str 

:return: active hardware address of device ('PermHwAddress' property) 

:rtype: str 

:raise UnknownDeviceError: if device is not found 

:raise PropertyNotFoundError: if 'PermHwAddress' property is not found 

""" 

return nm_device_property(name, "PermHwAddress") 

 

def nm_device_valid_hwaddress(name): 

"""Return valid hardware address of device depending on type of the device 

('PermHwAddress' property for wired and wireless or 'HwAddress' property for others) 

 

:param name: name of device 

:type name: str 

:return: active hardware address of device 

('HwAddress' or 'PermHwAddress' property) 

:rtype: str 

:raise UnknownDeviceError: if device is not found 

:raise PropertyNotFoundError: if property is not found 

""" 

if nm_device_type_is_ethernet(name) or nm_device_type_is_wifi(name): 

try: 

return nm_device_perm_hwaddress(name) 

except PropertyNotFoundError: 

# TODO: Remove this if everything will work well 

# fallback solution 

log.warning("Device %s don't have property PermHwAddress", name) 

return nm_device_hwaddress(name) 

else: 

return nm_device_hwaddress(name) 

 

def nm_device_active_con_uuid(name): 

"""Return uuid of device's active connection 

 

Exceptions: 

UnknownDeviceError if device is not found 

""" 

active_con = nm_device_property(name, 'ActiveConnection') 

if active_con == "/": 

return None 

 

uuid = _get_property(active_con, "Uuid", ".Connection.Active") 

return uuid 

 

def nm_device_type(name): 

"""Return device's type ('DeviceType' property). 

 

:param name: name of device 

:type name: str 

:return: device type 

:rtype: integer 

:raise UnknownDeviceError: if device is not found 

:raise PropertyNotFoundError: if 'DeviceType' property is not found 

""" 

return nm_device_property(name, "DeviceType") 

 

def nm_device_carrier(name): 

"""Return whether physical carrier of device is found. 

('Carrier' property) 

 

:param name: name of device 

:type name: str 

:return: True if physical carrier is found, False otherwise 

:rtype: bool 

:raise UnknownDeviceError: if device is not found 

:raise PropertyNotFoundError: if 'Carrier' property is not found 

""" 

return nm_device_property(name, "Carrier") 

 

def nm_device_ip_addresses(name, version=4): 

"""Return IP addresses of device in ACTIVATED state. 

 

:param name: name of device 

:type name: str 

:param version: version of IP protocol (value 4 or 6) 

:type version: int 

:return: IP addresses of device, empty list if device is not 

in ACTIVATED state 

:rtype: list of strings 

:raise UnknownDeviceError: if device is not found 

:raise PropertyNotFoundError: if IP configuration is not found 

""" 

retval = [] 

config = nm_device_ip_config(name, version) 

if config: 

retval = [addrs[0] for addrs in config[0]] 

 

return retval 

 

def nm_device_active_ssid(name): 

"""Return ssid of device's active access point. 

 

:param name: name of device 

:type name: str 

:return: ssid of active access point, ``None`` if device has no active AP 

:rtype: str 

:raise UnknownDeviceError: if device is not found 

""" 

 

try: 

aap = nm_device_property(name, "ActiveAccessPoint") 

except PropertyNotFoundError: 

return None 

 

if aap == "/": 

return None 

 

ssid_ay = _get_property(aap, "Ssid", ".AccessPoint") 

ssid = "".join(chr(b) for b in ssid_ay) 

 

return ssid 

 

def nm_device_ip_config(name, version=4): 

"""Return IP configurations of device in ACTIVATED state. 

 

:param name: name of device 

:type name: str 

:param version: version of IP protocol (value 4 or 6) 

:type version: int 

:return: IP configuration of device, empty list if device is not 

in ACTIVATED state 

:rtype: 

| [[[address1, prefix1, gateway1], [address2, prefix2, gateway2], ...], 

| [nameserver1, nameserver2]] 

| addressX, gatewayX: string 

| prefixX: int 

 

:raise UnknownDeviceError: if device is not found 

:raise PropertyNotFoundError: if ip configuration is not found 

""" 

state = nm_device_property(name, "State") 

if state != NM.DeviceState.ACTIVATED: 

return [] 

 

if version == 4: 

dbus_iface = ".IP4Config" 

prop = "Ip4Config" 

elif version == 6: 

dbus_iface = ".IP6Config" 

prop = "Ip6Config" 

else: 

return [] 

 

config = nm_device_property(name, prop) 

if config == "/": 

return [] 

 

try: 

addresses = _get_property(config, "Addresses", dbus_iface) 

# object is valid only if device is in ACTIVATED state (racy) 

except UnknownMethodGetError: 

return [] 

 

addr_list = [] 

for addr, prefix, gateway in addresses: 

# NOTE: There is an ipaddress for IP validation but 

# byte order of dbus value would need to be switched 

if version == 4: 

addr_str = nm_dbus_int_to_ipv4(addr) 

gateway_str = nm_dbus_int_to_ipv4(gateway) 

elif version == 6: 

addr_str = nm_dbus_ay_to_ipv6(addr) 

gateway_str = nm_dbus_ay_to_ipv6(gateway) 

addr_list.append([addr_str, prefix, gateway_str]) 

 

try: 

nameservers = _get_property(config, "Nameservers", dbus_iface) 

# object is valid only if device is in ACTIVATED state (racy) 

except UnknownMethodGetError: 

return [] 

 

ns_list = [] 

for ns in nameservers: 

# TODO - look for a library function 

if version == 4: 

ns_str = nm_dbus_int_to_ipv4(ns) 

elif version == 6: 

ns_str = nm_dbus_ay_to_ipv6(ns) 

ns_list.append(ns_str) 

 

return [addr_list, ns_list] 

 

def nm_device_slaves(name): 

"""Return slaves of device. 

 

:param name: name of device 

:type name: str 

:return: names of slaves of device or None if device has no 'Slaves' property 

:rtype: list of strings or None 

:raise UnknownDeviceError: if device is not found 

""" 

 

try: 

slaves = nm_device_property(name, "Slaves") 

except PropertyNotFoundError: 

return None 

 

slave_ifaces = [] 

for slave in slaves: 

iface = _get_property(slave, "Interface", ".Device") 

slave_ifaces.append(iface) 

 

return slave_ifaces 

 

def nm_hwaddr_to_device_name(hwaddr): 

"""Return device name of interface with given hardware address. 

 

:param hwaddr: hardware address 

:type hwaddr: str 

:return: device name of interface having hwaddr 

:rtype: str 

""" 

for device in nm_devices(): 

if nm_device_valid_hwaddress(device).upper() == hwaddr.upper(): 

return device 

return None 

 

def nm_ntp_servers_from_dhcp(): 

"""Return NTP servers obtained by DHCP. 

 

return: NTP servers obtained by DHCP 

rtype: list of str 

""" 

ntp_servers = [] 

# get paths for all actively connected interfaces 

active_devices = nm_activated_devices() 

for device in active_devices: 

# harvest NTP server addresses from DHCPv4 

dhcp4_path = nm_device_property(device, "Dhcp4Config") 

try: 

options = _get_property(dhcp4_path, "Options", ".DHCP4Config") 

# object is valid only if device is in ACTIVATED state (racy) 

except UnknownMethodGetError: 

options = None 

if options and 'ntp_servers' in options: 

# NTP server addresses returned by DHCP are whitespace delimited 

ntp_servers_string = options["ntp_servers"] 

for ip in ntp_servers_string.split(" "): 

ntp_servers.append(ip) 

 

# NetworkManager does not request NTP/SNTP options for DHCP6 

return ntp_servers 

 

def _is_s390_setting(path): 

"""Check if setting of given object path is an s390 setting 

 

:param path: object path of setting object 

:type path: str 

:return: True if the setting is s390 setting, False otherwise 

:rtype: bool 

""" 

 

proxy = _get_proxy(object_path=path, interface_name="org.freedesktop.NetworkManager.Settings.Connection") 

settings = proxy.GetSettings() 

return "s390-subchannels" in settings["802-3-ethernet"] 

 

def _device_settings(name): 

"""Return list of object paths of device settings 

 

:param name: name of device 

:type name: str 

:return: list of paths of settings of device 

:rtype: [] 

:raise UnknownDeviceError: if device is not found 

""" 

devtype = nm_device_type(name) 

if devtype == NM.DeviceType.BOND: 

settings = _find_settings(name, 'bond', 'interface-name') 

elif devtype == NM.DeviceType.BRIDGE: 

settings = _find_settings(name, 'bridge', 'interface-name') 

elif devtype == NM.DeviceType.VLAN: 

settings = _find_settings(name, 'vlan', 'interface-name') 

if not settings: 

# connections generated by NM from iBFT 

_parent, _sep, vlanid = name.partition(".") 

 

# If we are not able to convert the VLAN id to an int this 

# is probably a FCoE interface and we're not going to be able 

# to do much with it. 

try: 

vlanid = int(vlanid) 

except ValueError: 

return [] 

 

settings = _find_settings(vlanid, 'vlan', 'id') 

else: 

# device name bound settings 

settings = _find_settings(name, 'connection', 'interface-name') 

# mac address bound settings 

try: 

hwaddr_str = nm_device_valid_hwaddress(name) 

except PropertyNotFoundError: 

log.debug("hwaddress of device %s not found", name) 

else: 

mac_bound_settings = _settings_for_hwaddr(hwaddr_str) 

for ms in mac_bound_settings: 

if ms not in settings: 

settings.append(ms) 

if not settings: 

# s390 setting generated in dracut with net.ifnames=0 

# has neither DEVICE nor HWADDR (#1249750) 

settings = [s for s in _find_settings(name, 'connection', 'id') 

if _is_s390_setting(s)] 

 

return settings 

 

def _settings_for_hwaddr(hwaddr): 

"""Return list of object paths of settings of device specified by hw address. 

 

:param hwaddr: hardware address (uppercase) 

:type hwaddr: str 

:return: list of paths of settings found for hw address 

:rtype: list 

""" 

return _find_settings(hwaddr, '802-3-ethernet', 'mac-address', 

format_value=lambda ba: ":".join("%02X" % b for b in ba)) 

 

def _find_settings(value, key1, key2, format_value=lambda x: x): 

"""Return list of object paths of settings having given value of key1, key2 setting 

 

:param value: required value of setting 

:type value: corresponds to dbus type of setting 

:param key1: first-level key of setting (eg "connection") 

:type key1: str 

:param key2: second-level key of setting (eg "uuid") 

:type key2: str 

:param format_value: function to be called on setting value before 

comparing 

:type format_value: function taking one argument (setting value) 

:return: list of paths of settings 

:rtype: list 

""" 

retval = [] 

 

proxy = _get_proxy(object_path="/org/freedesktop/NetworkManager/Settings", interface_name="org.freedesktop.NetworkManager.Settings") 

 

connections = proxy.ListConnections() 

for con in connections: 

proxy = _get_proxy(object_path=con, interface_name="org.freedesktop.NetworkManager.Settings.Connection") 

try: 

settings = proxy.GetSettings() 

except GError as e: 

log.debug("Exception raised in _find_settings: %s", e) 

continue 

try: 

v = settings[key1][key2] 

except KeyError: 

continue 

if format_value(v) == value: 

retval.append(con) 

 

return retval 

 

def nm_get_settings(value, key1, key2, format_value=lambda x: x): 

"""Return settings having given value of key1, key2 setting 

 

Returns list of settings(dicts) , None if settings were not found. 

""" 

retval = [] 

settings_paths = _find_settings(value, key1, key2, format_value) 

for settings_path in settings_paths: 

proxy = _get_proxy(object_path=settings_path, interface_name="org.freedesktop.NetworkManager.Settings.Connection") 

settings = proxy.GetSettings() 

retval.append(settings) 

 

return retval 

 

def nm_get_all_settings(): 

"""Return all settings for logging.""" 

retval = [] 

 

proxy = _get_proxy(object_path="/org/freedesktop/NetworkManager/Settings", interface_name="org.freedesktop.NetworkManager.Settings") 

 

connections = proxy.ListConnections() 

for con in connections: 

proxy = _get_proxy(object_path=con, interface_name="org.freedesktop.NetworkManager.Settings.Connection") 

try: 

settings = proxy.GetSettings() 

except GError as e: 

# The connection may be deleted asynchronously by NM 

log.debug("Exception raised in nm_get_all_settings: %s", e) 

continue 

retval.append(settings) 

 

return retval 

 

def nm_device_setting_value(name, key1, key2): 

"""Return value of device's setting specified by key1 and key2. 

 

:param name: name of device 

:type name: str 

:param key1: first-level key of setting (eg "connection") 

:type key1: str 

:param key2: second-level key of setting (eg "uuid") 

:type key2: str 

:return: value of setting or None if the setting was not found 

which means it does not exist or default value is used 

by NM 

:rtype: unpacked GDBus variant or None 

:raise UnknownDeviceError: if device is not found 

:raise SettingsNotFoundError: if settings were not found (eg for "wlan0") 

:raise MultipleSettingsFoundError: if multiple settings were found 

""" 

settings_paths = _device_settings(name) 

if not settings_paths: 

raise SettingsNotFoundError(name) 

elif len(settings_paths) > 1: 

raise MultipleSettingsFoundError(name) 

else: 

settings_path = settings_paths[0] 

proxy = _get_proxy(object_path=settings_path, interface_name="org.freedesktop.NetworkManager.Settings.Connection") 

try: 

settings = proxy.GetSettings() 

except GError as e: 

log.debug("nm_device_setting_value: %s", e) 

raise SettingsNotFoundError(name) 

try: 

value = settings[key1][key2] 

except KeyError: 

value = None 

return value 

 

def nm_disconnect_device(name): 

"""Disconnect the device. 

 

:raise UnknownDeviceError: if device is not found 

""" 

proxy = _get_proxy() 

try: 

device = proxy.GetDeviceByIpIface('(s)', name) 

except GError as e: 

if "org.freedesktop.NetworkManager.UnknownDevice" in e.message: 

raise UnknownDeviceError(name, e) 

raise 

 

device_proxy = _get_proxy(object_path=device, interface_name="org.freedesktop.NetworkManager.Device") 

try: 

device_proxy.Disconnect() 

except GError as e: 

if "org.freedesktop.NetworkManager.Device.NotActive" in e.message: 

raise DeviceNotActiveError(name, e) 

raise 

 

def nm_activate_device_connection(dev_name, con_uuid): 

"""Activate device with specified connection. 

 

:param dev_name: name of device or None for virtual devices 

:type dev_name: str or None 

:param con_uuid: uuid of connection to be activated on device 

:type con_uuid: str 

:raise UnknownDeviceError: if device is not found 

:raise UnmanagedDeviceError: if device is not managed by NM 

or unavailable 

:raise SettingsNotFoundError: if conneciton with given uuid was not found 

:raise UnknownConnectionError: if connection is not available for the device 

""" 

 

if dev_name is None: 

# virtual devices (eg bond, vlan) 

device_path = "/" 

else: 

proxy = _get_proxy() 

try: 

device_path = proxy.GetDeviceByIpIface('(s)', dev_name) 

except GError as e: 

if "org.freedesktop.NetworkManager.UnknownDevice" in e.message: 

raise UnknownDeviceError(dev_name, e) 

raise 

 

con_paths = _find_settings(con_uuid, 'connection', 'uuid') 

if not con_paths: 

raise SettingsNotFoundError(con_uuid) 

 

nm_proxy = _get_proxy() 

try: 

nm_proxy.ActivateConnection('(ooo)', con_paths[0], device_path, "/") 

except GError as e: 

if "org.freedesktop.NetworkManager.UnmanagedDevice" in e.message: 

raise UnmanagedDeviceError(dev_name, e) 

elif "org.freedesktop.NetworkManager.UnknownConnection" in e.message: 

raise UnknownConnectionError(dev_name, e) 

if "org.freedesktop.NetworkManager.UnknownDevice" in e.message: 

raise UnknownDeviceError(dev_name, e) 

raise 

 

def nm_add_connection(values): 

"""Add new connection specified by values. 

 

:param values: 

| list of settings with new values and its types 

| [[key1, key2, value, type_str], ...] 

| key1: first-level key of setting (eg "connection") 

| key2: second-level key of setting (eg "uuid") 

| value: new value 

| type_str: dbus type of new value (eg "ay") 

:type values: 

| [[key1, key2, value, type_str], ...] 

| key1: str 

| key2: str 

| value: object 

| type_str: str 

""" 

 

settings = {} 

for key1, key2, value, type_str in values: 

gvalue = Variant(type_str, value) 

if key1 not in settings: 

settings[key1] = {} 

settings[key1][key2] = gvalue 

 

proxy = _get_proxy(object_path="/org/freedesktop/NetworkManager/Settings", 

interface_name="org.freedesktop.NetworkManager.Settings") 

try: 

connection = proxy.AddConnection('(a{sa{sv}})', settings) 

except GError as e: 

if "bond.options: invalid option" in e.message: 

raise BondOptionsError(e) 

raise 

return connection 

 

def nm_update_settings(uuid, new_values): 

"""Update settings of connection given by uuid. 

 

The type of value is determined from existing settings of device. 

If setting for key1, key2 does not exist, default_type_str is used or 

if None, the type is inferred from the value supplied (string and bool only). 

 

:param uuid: uuid of the connection 

:type name: str 

:param new_values: list of settings with new values and its types 

[[key1, key2, value, default_type_str]] 

key1: first-level key of setting (eg "connection") 

key2: second-level key of setting (eg "uuid") 

value: new value 

default_type_str: dbus type of new value to be used 

if the setting does not already exist; 

if None, the type is inferred from 

value (string and bool only) 

:type new_values: [[key1, key2, value, default_type_str], ...] 

key1: str 

key2: str 

value: 

default_type_str: str 

:raise SettingsNotFoundError: if settings were not found (eg for "wlan0") 

""" 

settings_paths = _find_settings(uuid, "connection", "uuid") 

if not settings_paths: 

raise SettingsNotFoundError(uuid) 

return _update_settings(settings_paths[0], new_values) 

 

def nm_update_settings_of_device(name, new_values): 

"""Update setting of device. 

 

The type of value is determined from existing settings of device. 

If setting for key1, key2 does not exist, default_type_str is used or 

if ``None``, the type is inferred from the value supplied (string and bool only). 

 

:param name: name of device 

:type name: str 

:param new_values: 

| list of settings with new values and its types 

| [[key1, key2, value, default_type_str]] 

| key1: first-level key of setting (eg "connection") 

| key2: second-level key of setting (eg "uuid") 

| value: new value 

| default_type_str: 

 

dbus type of new value to be used 

if the setting does not already exist; 

if ``None``, the type is inferred from 

value (string and bool only) 

:type new_values: 

| [[key1, key2, value, default_type_str], ...] 

| key1: str 

| key2: str 

| value: 

| default_type_str: str 

:raise UnknownDeviceError: if device is not found 

:raise SettingsNotFoundError: if settings were not found 

(eg for "wlan0") 

:raise MultipleSettingsFoundError: if multiple settings were found 

""" 

settings_paths = _device_settings(name) 

if not settings_paths: 

raise SettingsNotFoundError(name) 

elif len(settings_paths) > 1: 

raise MultipleSettingsFoundError(name) 

else: 

settings_path = settings_paths[0] 

return _update_settings(settings_path, new_values) 

 

def _update_settings(settings_path, new_values): 

"""Update setting of object specified by settings_path with value. 

 

If the value is None, the setting is removed. 

 

The type of value is determined from existing setting. 

If setting for key1, key2 does not exist, default_type_str is used or 

if None, the type is inferred from the value supplied (string and bool only). 

 

:param settings_path: path of settings object 

:type settings_path: str 

:param new_values: list of settings with new values and its types 

[[key1, key2, value, default_type_str]] 

key1: first-level key of setting (eg "connection") 

key2: second-level key of setting (eg "uuid") 

value: new value, if None, the setting is removed 

default_type_str: dbus type of new value to be used 

if the setting does not already exist; 

if None, the type is inferred from 

value (string and bool only) 

:type new_values: [[key1, key2, value, default_type_str], ...] 

key1: str 

key2: str 

value: 

default_type_str: str 

""" 

proxy = _get_proxy(object_path=settings_path, interface_name="org.freedesktop.NetworkManager.Settings.Connection") 

args = None 

settings = proxy.call_sync("GetSettings", 

args, 

Gio.DBusCallFlags.NONE, 

DEFAULT_DBUS_TIMEOUT, 

None) 

for key1, key2, value, default_type_str in new_values: 

settings = _gvariant_settings(settings, key1, key2, value, default_type_str) 

 

proxy.call_sync("Update", 

settings, 

Gio.DBusCallFlags.NONE, 

DEFAULT_DBUS_TIMEOUT, 

None) 

 

def _gvariant_settings(settings, updated_key1, updated_key2, value, default_type_str=None): 

"""Update setting of updated_key1, updated_key2 of settings object with value. 

 

If the value is None, the setting is removed. 

 

The type of value is determined from existing setting. 

If setting for key1, key2 does not exist, default_type_str is used or 

the type is inferred from the value supplied (string and bool only). 

""" 

 

type_str = default_type_str 

 

# build copy of GVariant settings as mutable python object 

new_settings = {} 

dict1 = settings.get_child_value(0) 

 

# loop over first level dict (key1) 

for key1_idx in range(dict1.n_children()): 

 

key_dict2 = dict1.get_child_value(key1_idx) 

key1 = key_dict2.get_child_value(0).unpack() 

new_settings[key1] = {} 

dict2 = key_dict2.get_child_value(1) 

 

# loop over second level dict (key2) 

for key2_idx in range(dict2.n_children()): 

 

key_val = dict2.get_child_value(key2_idx) 

key2 = key_val.get_child_value(0).unpack() 

val = key_val.get_child_value(1).get_child_value(0) 

 

# get type string of updated value 

if key1 == updated_key1 and key2 == updated_key2: 

type_str = val.get_type_string() 

 

# copy old value to new python object 

new_settings[key1][key2] = val 

 

if type_str is None: 

# infer the new value type for string and boolean 

if isinstance(value, bool): 

type_str = 'b' 

elif isinstance(value, str): 

type_str = 's' 

 

if type_str is not None: 

if updated_key1 not in new_settings: 

new_settings[updated_key1] = {} 

if value is None: 

new_settings[updated_key1].pop(updated_key2, None) 

else: 

new_settings[updated_key1][updated_key2] = Variant(type_str, value) 

 

return Variant(settings.get_type_string(), (new_settings,)) 

 

def nm_ipv6_to_dbus_ay(address): 

"""Convert ipv6 address from string to list of bytes 'ay' for dbus 

 

:param address: IPv6 address 

:type address: str 

:return: address in format 'ay' for NM dbus setting 

:rtype: list of bytes 

""" 

return [int(byte) for byte in bytearray(socket.inet_pton(socket.AF_INET6, address))] 

 

def nm_ipv4_to_dbus_int(address): 

"""Convert ipv4 address from string to int for dbus (switched endianess). 

 

:param address: IPv4 address 

:type address: str 

:return: IPv4 address as an integer 'u' for NM dbus setting 

:rtype: integer 

""" 

return struct.unpack("=L", socket.inet_aton(address))[0] 

 

def nm_dbus_ay_to_ipv6(bytelist): 

"""Convert ipv6 address from list of bytes (dbus 'ay') to string. 

 

:param address: IPv6 address as list of bytes returned by dbus ('ay') 

:type address: list of bytes - dbus 'ay' 

:return: IPv6 address 

:rtype: str 

""" 

return socket.inet_ntop(socket.AF_INET6, bytes(bytelist)) 

 

def nm_dbus_int_to_ipv4(address): 

"""Convert ipv4 address from dus int 'u' (switched endianess) to string. 

 

:param address: IPv4 address as integer returned by dbus ('u') 

:type address: integer - dbus 'u' 

:return: IPv6 address 

:rtype: str 

""" 

return socket.inet_ntop(socket.AF_INET, struct.pack('=L', address)) 

 

def test(): 

print("NM state: %s:" % nm_state()) 

print("NM is connected: %s" % nm_is_connected()) 

 

print("Devices: %s" % nm_devices()) 

print("Activated devices: %s" % nm_activated_devices()) 

 

wireless_device = "" 

 

devs = nm_devices() 

devs.append("nonexisting") 

for devname in devs: 

 

print(devname) 

 

try: 

devtype = nm_device_type(devname) 

except UnknownDeviceError as e: 

print(" %s" % e) 

devtype = None 

if devtype == NM.DeviceType.ETHERNET: 

print(" type %s" % "ETHERNET") 

elif devtype == NM.DeviceType.WIFI: 

print(" type %s" % "WIFI") 

wireless_device = devname 

 

try: 

print(" Wifi device: %s" % nm_device_type_is_wifi(devname)) 

except UnknownDeviceError as e: 

print(" %s" % e) 

 

try: 

hwaddr = nm_device_hwaddress(devname) 

print(" HwAaddress: %s" % hwaddr) 

except ValueError as e: 

print(" %s" % e) 

hwaddr = "" 

 

try: 

print(" Carrier: %s" % nm_device_carrier(devname)) 

except ValueError as e: 

print(" %s" % e) 

 

try: 

print(" IP4 config: %s" % nm_device_ip_config(devname)) 

print(" IP6 config: %s" % nm_device_ip_config(devname, version=6)) 

print(" IP4 addrs: %s" % nm_device_ip_addresses(devname)) 

print(" IP6 addrs: %s" % nm_device_ip_addresses(devname, version=6)) 

print(" Udi: %s" % nm_device_property(devname, "Udi")) 

except UnknownDeviceError as e: 

print(" %s" % e) 

 

if devname in nm_devices(): 

try: 

print(" Nonexisting: %s" % nm_device_property(devname, "Nonexisting")) 

except PropertyNotFoundError as e: 

print(" %s" % e) 

try: 

print(" Nonexisting: %s" % nm_device_property(devname, "Nonexisting")) 

except ValueError as e: 

print(" %s" % e) 

 

try: 

print(" Settings: %s" % _device_settings(devname)) 

except UnknownDeviceError as e: 

print(" %s" % e) 

try: 

print(" Settings for hwaddr %s: %s" % (hwaddr, _settings_for_hwaddr(hwaddr))) 

except UnknownDeviceError as e: 

print(" %s" % e) 

try: 

print(" Setting value %s %s: %s" % ("ipv6", "method", nm_device_setting_value(devname, "ipv6", "method"))) 

except ValueError as e: 

print(" %s" % e) 

try: 

print(" Setting value %s %s: %s" % ("ipv7", "method", nm_device_setting_value(devname, "ipv7", "method"))) 

except ValueError as e: 

print(" %s" % e) 

 

devname = devs[0] 

key1 = "connection" 

key2 = "autoconnect" 

original_value = nm_device_setting_value(devname, key1, key2) 

print("Value of setting %s %s: %s" % (key1, key2, original_value)) 

# None means default in this case, which is true 

if original_value in (None, True): 

new_value = False 

else: 

new_value = True 

 

print("Updating to %s" % new_value) 

nm_update_settings_of_device(devname, [[key1, key2, new_value, None]]) 

print("Value of setting %s %s: %s" % (key1, key2, nm_device_setting_value(devname, key1, key2))) 

nm_update_settings_of_device(devname, [[key1, key2, original_value, None]]) 

print("Value of setting %s %s: %s" % (key1, key2, nm_device_setting_value(devname, key1, key2))) 

nm_update_settings_of_device(devname, [[key1, key2, original_value, "b"]]) 

print("Value of setting %s %s: %s" % (key1, key2, nm_device_setting_value(devname, key1, key2))) 

 

nm_update_settings_of_device(devname, [[key1, "nonexisting", new_value, None]]) 

nm_update_settings_of_device(devname, [["nonexisting", "nonexisting", new_value, None]]) 

try: 

nm_update_settings_of_device("nonexixting", [[key1, key2, new_value, None]]) 

except UnknownDeviceError as e: 

print("%s" % e) 

 

if wireless_device: 

try: 

nm_update_settings_of_device(wireless_device, [[key1, key2, new_value, None]]) 

except SettingsNotFoundError as e: 

print("%s" % e) 

 

#nm_update_settings_of_device(devname, [["connection", "id", "test", None]]) 

#nm_update_settings_of_device(devname, [["connection", "timestamp", 11111111, None]]) 

#nm_update_settings_of_device(devname, [["802-3-ethernet", "mac-address", [55,55,55,55,55,55], None]]) 

#nm_update_settings_of_device(devname, [["ipv6", "method", "auto", None]]) 

#nm_update_settings_of_device(devname, [["ipv6", "addressess", [[[32,1,0,0,0,0,0,0,0,0,0,0,0,0,0,a], 64, [0]*16]], None]]) 

#nm_update_settings_of_device(devname, [["connection", "autoconnect", True, None]]) 

#nm_update_settings_of_device(devname, [["connection", "autoconnect", False, "b"]]) 

 

if __name__ == "__main__": 

test()