2 # cnetworkmanager: Command Line Interface for NetworkManager
3 # by: http://en.opensuse.org/User:Mvidner
4 # license: http://www.gnu.org/licenses/gpl-2.0.html or later
7 print "cnetworkmanager %s - Command Line Interface for NetworkManager" % VERSION
18 import ConfigParser
# knm config
19 from optparse
import OptionParser
25 print "Install python-1-dbus.rpm or or python-dbus.rpm or python-dbus.deb"
27 import xml
.dom
.minidom
31 # todo - only if loop wanted
32 print "Install python-gobject2.rpm or pygobject2.rpm or python-gobject.deb"
34 # python-gnome.rpm has gconf for nm-applet...
38 from dbus
.mainloop
.glib
import DBusGMainLoop
39 DBusGMainLoop(set_as_default
=True)
43 bus
= dbus
.SystemBus()
45 # FOOC = connection (service) string
46 # FOOI = interface string
49 # foopi = property interface
50 NMC
= 'org.freedesktop.NetworkManager'
52 PI
= 'org.freedesktop.DBus.Properties'
53 SSC
= "org.freedesktop.NetworkManagerSystemSettings"
54 USC
= "org.freedesktop.NetworkManagerUserSettings"
55 NMIC
= "org.freedesktop.NetworkManagerInfo"
58 ii
= dbus
.Interface(obj
, 'org.freedesktop.DBus.Introspectable')
62 # TODO: pull them from introspection.xml
63 NM_STATE
= ["UNKNOWN", "ASLEEP", "CONNECTING", "CONNECTED", "DISCONNECTED",]
65 def __init__(self
, opath
):
67 self
.nmo
= bus
.get_object(NMC
, self
.opath
)
68 self
.nmi
= dbus
.Interface(self
.nmo
, NMI
)
69 self
.nmpi
= dbus
.Interface(self
.nmo
, PI
)
75 "Dumps its own info (not owned objects)."
81 for device
in self
.Devices():
85 print "Active Connections"
86 aconns
= self
.ActiveConnections()
91 print "Wifi Networks:"
92 for dev
in self
.Devices():
95 def reply_handler(self
, opath
):
96 print "Connected:", opath
98 def err_handler(self
, *args
):
101 def silent_handler(self
, *args
):
105 def quitter_handler(self
, *args
):
106 # exit the loop that runs only because of us
112 "Detects NM version and chooses appropriate class"
114 nmo
= bus
.get_object(NMC
, opath
)
115 nmi
= dbus
.Interface(nmo
, NMI
)
117 dummy
= nmi
.getDevices()
119 except dbus
.exceptions
.DBusException
:
126 def SetWifiEnabled(self
, v
):
127 # TODO: async call, catch the state signal and exit
128 # weird: synchronous call works, but times out
129 # asynchronous call does not work
130 self
.nmi
.setWirelessEnabled(v
,
131 reply_handler
=self
.quitter_handler
,
132 error_handler
=self
.quitter_handler
)
136 def SetOnline(self
, v
):
139 reply_handler
=self
.quitter_handler
,
140 error_handler
=self
.quitter_handler
)
143 reply_handler
=self
.quitter_handler
,
144 error_handler
=self
.quitter_handler
)
149 print "State:", self
.NM_STATE
[self
.nmi
.state()]
150 we
= self
.nmi
.getWirelessEnabled()
151 if isinstance(we
, tuple):
152 print "Wifi enabled:", bool(we
[0])
153 print "Wifi HW enabled:", bool(we
[1])
155 print "Wifi enabled:", bool(we
)
158 dup
= self
.nmi
.getDialup()
160 except dbus
.exceptions
.DBusException
, e
:
161 #if e.get_dbus_name() == "org.freedesktop.NetworkManager.NoDialup":
167 opaths
= self
.nmi
.getDevices()
168 return map(cDevice_06
, opaths
)
170 def ActiveConnections(self
):
171 return [] # at most one active connection, FIXME find it
173 def reply_handler(self
):
174 print "Connection requested"
176 def err_handler(self
, *args
):
179 def ActivateConnection(self
, conn
, device
, ap
):
180 # passing *_handler makes the call asynchronous
181 self
.nmi
.setActiveDevice(device
.opath
, ssid_str(conn
.Ssid()),
182 reply_handler
=self
.reply_handler
,
183 error_handler
=self
.err_handler
,
190 def SetWifiEnabled(self
, v
):
191 self
.nmpi
.Set(NMI
, "WirelessEnabled", v
)
193 def SetOnline(self
, v
):
194 self
.nmi
.Sleep(not v
)
197 print "State:", self
.NM_STATE
[self
.nmpi
.Get(NMI
, "State")]
198 print "Wifi enabled:", self
.nmpi
.Get(NMI
, "WirelessEnabled")
199 print "Wifi HW enabled:", self
.nmpi
.Get(NMI
, "WirelessHardwareEnabled")
202 opaths
= self
.nmi
.GetDevices()
203 return map(cDevice_07
, opaths
)
205 def ActiveConnections(self
):
206 aconns
= self
.nmpi
.Get(NMI
, "ActiveConnections")
207 return map(cActiveConnection
, aconns
)
209 def ActivateConnection(self
, conn
, device
, ap
):
210 # passing *_handler makes the call asynchronous
211 self
.nmi
.ActivateConnection(USC
,
212 conn
.__dbus
_object
_path
__,
215 reply_handler
=self
.reply_handler
,
216 error_handler
=self
.err_handler
,
220 class cActiveConnection
:
221 def __init__(self
, opath
):
226 co
= bus
.get_object(NMC
, self
.opath
)
227 copi
= dbus
.Interface(co
, PI
)
228 for P
in ["ServiceName", "Connection", "SharedServiceName", "SharedConnection", "SpecificObject",]:
229 print " %s: %s" % (P
, copi
.Get(NMI
, P
))
230 devs
= copi
.Get(NMI
, "Devices")
235 def bitmask_str(map, value
):
237 for mask
in sorted(map.keys()):
238 if value
& mask
: ret
.append(map[mask
])
243 def __init__(self
, opath
):
245 self
.devo
= bus
.get_object(NMC
, self
.opath
)
246 self
.devi
= dbus
.Interface(self
.devo
, NMI
+ ".Device")
247 self
.devpi
= dbus
.Interface(self
.devo
, PI
)
251 DEVICE_TYPE
= ["UNKNOWN", "802_3_ETHERNET", "802_11_WIRELESS",
252 "GSM", "CDMA",] #OLPC: 3 is MESH
254 def DeviceType(self
):
255 return self
.DEVICE_TYPE
[self
.DeviceType0()]
257 def ip_str(self
, i32
):
259 ret
.append("%d" % (i32
% 256))
261 ret
.append("%d" % (i32
% 256))
263 ret
.append("%d" % (i32
% 256))
265 ret
.append("%d" % (i32
% 256))
269 def DumpIp4Config(self
, opath
):
270 print " Ip4Config:", opath
271 o
= bus
.get_object(NMC
, opath
)
272 pi
= dbus
.Interface(o
, PI
)
274 for P
in ["Address", "Netmask", "Broadcast", "Gateway",]: # beta2?
275 print " %s: %s" % (P
, self
.ip_str(pi
.Get(NMI
, P
)))
278 addrs
= pi
.Get(NMI
, "Addresses")
280 print " %s/%s via %s" % tuple(map(self
.ip_str
, addr
))
281 hn
= pi
.Get(NMI
, "Hostname")
282 print " Hostname:", hn
283 nss
= pi
.Get(NMI
, "Nameservers")
284 print " Nameservers:", " ".join(map(self
.ip_str
, nss
))
285 doms
= pi
.Get(NMI
, "Domains")
286 print " Domains:", " ".join(doms
)
287 nisd
= pi
.Get(NMI
, "NisDomain")
288 print " NisDomain:", nisd
289 niss
= pi
.Get(NMI
, "NisServers")
290 print " NisServers:", " ".join(map(self
.ip_str
, niss
))
292 NM_DEVICE_CAP
= {1: "NM_SUPPORTED", 2: "CARRIER_DETECT", 4: "SCANNING", }
296 print "Device:", self
.opath
299 IW_MODE
= ["AUTO", "ADHOC", "INFRA", "MASTER",
300 "REPEAT", "SECOND", "MONITOR",]
306 for ap
in self
.APs():
313 class cDevice_06(cDevice
):
314 def DeviceType0(self
):
316 self
.dt
= self
.devi
.getProperties()[2]
318 self
.__class
__ = cDeviceEth_06
320 self
.__class
__ = cDeviceWifi_06
324 "UNKNOWN", "DEVICE_PREPARE", "DEVICE_CONFIG", "NEED_USER_KEY",
325 "IP_CONFIG_START", "IP_CONFIG_GET", "IP_CONFIG_COMMIT",
326 "ACTIVATED", "FAILED", "CANCELLED", ]
330 print " Driver:", self
.devi
.getDriver()
331 props
= self
.devi
.getProperties() # osusb ussss sssii biuus as
332 print " Self:", props
[0] # o
333 print " Interface:", props
[1] # s
334 print " Type:", self
.DEVICE_TYPE
[props
[2]] # u
335 print " UDI:", props
[3] # s
336 print " Active:", bool(props
[4]) # b
337 print " Activation Stage:", self
.NM_ACT_STAGE
[props
[5]] # u
338 print " IP:", props
[6] # s
339 print " Mask:", props
[7] # s
340 print " Bcast:", props
[8] # s
341 print " HwAddress:", props
[9] # s
342 print " GW:", props
[10] # s
343 print " NS1:", props
[11] # s
344 print " NS2:", props
[12] # s
348 print " (unknown device type, not dumping more)"
350 class cDeviceEth_06(cDevice_06
, cDeviceEth
):
352 props
= self
.devi
.getProperties() # osusb ussss sssii biuus as
353 print " Link Active:", bool(props
[15]) # b
354 print " Speed:", props
[16] # i
355 print " Generic Capabilities:", bitmask_str(self
.NM_DEVICE_CAP
, props
[17]) # u
357 class cDeviceWifi_06(cDevice_06
):
359 0x00000001: "PROTO_NONE",
360 0x00000002: "PROTO_WEP",
361 0x00000004: "PROTO_WPA",
362 0x00000008: "PROTO_WPA2",
363 0x00000010: "RESERVED1",
364 0x00000020: "RESERVED2",
365 0x00000040: "KEY_MGMT_PSK",
366 0x00000080: "KEY_MGMT_802_1X",
367 0x00000100: "RESERVED3",
368 0x00000200: "RESERVED4",
369 0x00000400: "RESERVED5",
370 0x00000800: "RESERVED6",
371 0x00001000: "CIPHER_WEP40",
372 0x00002000: "CIPHER_WEP104",
373 0x00004000: "CIPHER_TKIP",
374 0x00008000: "CIPHER_CCMP",
378 self
.wdevi
= dbus
.Interface(self
.devo
, NMI
+ ".Device.Wireless")
379 aps
= self
.devi
.getProperties()[20]
380 return map(cAP_06
, aps
)
383 props
= self
.devi
.getProperties() # osusb ussss sssii biuus as
384 print " Mode:", self
.IW_MODE
[props
[13]] # i
385 print " Strength:", props
[14] # i
386 print " Link Active:", bool(props
[15]) # b
387 print " Speed:", props
[16] # i
388 print " Generic Capabilities:", bitmask_str(self
.NM_DEVICE_CAP
, props
[17]) # u
389 print " Capabilities:", bitmask_str(self
.NM_802_11_CAP
, props
[18]) # u
390 print " Current net:", props
[19] # s
391 nets
= props
[20] # as
392 print " Seen nets:", " ".join(nets
)
394 print " Access Points"
395 for ap
in self
.APs():
398 class cDevice_07(cDevice
):
399 def DeviceType0(self
):
401 self
.dt
= self
.devpi
.Get(NMI
, "DeviceType")
403 self
.__class
__ = cDeviceEth_07
405 self
.__class
__ = cDeviceWifi_07
409 "UNKNOWN", "UNMANAGED", "UNAVAILABLE", "DISCONNECTED", "PREPARE",
410 "CONFIG", "NEED_AUTH", "IP_CONFIG", "ACTIVATED", "FAILED",]
415 # "Ip4Config", only for NM_DEVICE_STATE_ACTIVATED
416 for P
in ["Udi", "Interface", "Driver",]:
417 print " %s: %s" % (P
, self
.devpi
.Get(NMI
, P
))
418 addr
= self
.devpi
.Get(NMI
, "Ip4Address")
419 print " Ip4Address:", self
.ip_str(addr
)
420 caps
= self
.devpi
.Get(NMI
, "Capabilities")
421 print " Capabilities:", bitmask_str(self
.NM_DEVICE_CAP
, caps
)
422 state
= self
.NM_DEVICE_STATE
[self
.devpi
.Get(NMI
, "State")]
423 print " Dev State:", state
424 if state
== "ACTIVATED":
425 self
.DumpIp4Config(self
.devpi
.Get(NMI
, "Ip4Config"))
427 dt
= self
.DeviceType()
428 print " Dev Type:", dt
431 class cDeviceEth_07(cDevice_07
, cDeviceEth
):
433 for P
in ["HwAddress", "Speed", "Carrier"]:
434 print " %s: %s" % (P
, self
.devpi
.Get(NMI
, P
))
436 class cDeviceWifi_07(cDevice_07
):
437 NM_802_11_DEVICE_CAP
= {1:"CIPHER_WEP40", 2:"CIPHER_WEP104",
438 4:"CIPHER_TKIP", 8:"CIPHER_CCMP",
442 self
.wdevi
= dbus
.Interface(self
.devo
, NMI
+ ".Device.Wireless")
443 aps
= self
.wdevi
.GetAccessPoints()
444 return map(cAP_07
, aps
)
447 print " Dev Mode:", self
.IW_MODE
[self
.devpi
.Get(NMI
, "Mode")]
448 wcaps
= self
.devpi
.Get(NMI
, "WirelessCapabilities")
449 print " Wifi Capabilities:", bitmask_str(self
.NM_802_11_DEVICE_CAP
, wcaps
)
450 for P
in ["HwAddress", "Bitrate", "ActiveAccessPoint"]:
451 print " %s: %s" % (P
, self
.devpi
.Get(NMI
, P
))
453 print " Access Points"
454 for ap
in self
.APs():
457 """An AP found around us"""
459 def __init__(self
, opath
):
461 self
.apo
= bus
.get_object(NMC
, self
.opath
)
462 self
.appi
= dbus
.Interface(self
.apo
, PI
)
464 self
.devi
= dbus
.Interface(self
.apo
, NMI
+ ".Devices")
466 NM_802_11_AP_FLAGS
= {1: "PRIVACY",}
469 1: "PAIR_WEP40", 2: "PAIR_WEP104", 4: "PAIR_TKIP", 8: "PAIR_CCMP",
470 16: "GROUP_WEP40", 32: "GROUP_WEP104", 64: "GROUP_TKIP",
471 128: "GROUP_CCMP", 256: "KEY_MGMT_PSK", 512: "KEY_MGMT_802_1X",}
473 def ListNets(self
, marker
= " "):
475 mbr
= self
.Mbr() / 1024 # 07 1000, 06 1024?
476 priv_s
= self
.PrivS()
477 print "%s%3d: %s (%dMb%s)" % (marker
, self
.Strength(), self
.Ssid(), mbr
, priv_s
)
480 def Mbr(self
, props
=None):
482 props
= self
.devi
.getProperties()
487 props
= self
.devi
.getProperties()
488 caps_s
= bitmask_str(cDeviceWifi_06
.NM_802_11_CAP
, props
[7]) + ","
490 if caps_s
.find("PROTO_WEP,") != -1:
492 if caps_s
.find("PROTO_WPA,") != -1:
494 if caps_s
.find("PROTO_WPA2,") != -1:
496 if caps_s
.find("KEY_MGMT_802_1X,") != -1:
497 priv_s
+= " Enterprise"
500 def Strength(self
, props
=None):
502 props
= self
.devi
.getProperties()
505 def Ssid(self
, props
=None):
507 props
= self
.devi
.getProperties()
512 props
= self
.devi
.getProperties() # ossid iiib
513 print " Self:", props
[0]
514 print " Ssid:", self
.Ssid(props
)
515 print " HwAddress:", props
[2]
516 print " Strength:", self
.Strength(props
)
517 print " Frequency:", props
[4]
518 print " MaxBitrate:", self
.Mbr(props
)
519 print " AP Mode:", cDevice
.IW_MODE
[props
[6]]
520 print " Capabilities:", bitmask_str(cDeviceWifi_06
.NM_802_11_CAP
, props
[7])
521 print " Broadcast:", props
[8]
529 def opath_validchar(c
):
530 # _ is also escaped even though it is valid
532 string
.ascii_letters
.find(c
) != -1 or \
533 string
.digits
.find(c
) != -1
538 # TODO find a more elegant way
539 if not opath_validchar(c
):
545 def opath_unescape(s
):
547 unhex
= lambda xx
: chr(eval("0x"+xx
))
549 return re
.sub("_.._", lambda p
: unhex(p
.group()[1:3]), s
)
553 return self
.appi
.Get(NMI
, "MaxBitrate")
556 priv
= self
.appi
.Get(NMI
, "Flags") != 0
557 wpa
= self
.appi
.Get(NMI
, "WpaFlags") != 0
558 wpa2
= self
.appi
.Get(NMI
, "RsnFlags") != 0
561 if not wpa
and not wpa2
:
562 priv_s
= priv_s
+ " WEP"
564 priv_s
= priv_s
+ " WPA"
566 priv_s
= priv_s
+ " WPA2"
570 return int(self
.appi
.Get(NMI
, "Strength"))
573 return ssid_str(self
.appi
.Get(NMI
, "Ssid"))
576 print " AP:", self
.opath
577 print " Ssid:", self
.Ssid()
578 for P
in ["Frequency", "HwAddress", "MaxBitrate",]:
579 print " %s: %s" % (P
, self
.appi
.Get(NMI
, P
))
580 print " Strength:", self
.Strength()
581 print " AP Mode:", cDevice
.IW_MODE
[self
.appi
.Get(NMI
, "Mode")]
582 print " AP Flags:", bitmask_str(self
.NM_802_11_AP_FLAGS
,
583 self
.appi
.Get(NMI
, "Flags"))
584 print " AP WPA Flags:", bitmask_str(self
.NM_802_11_AP_SEC
,
585 self
.appi
.Get(NMI
, "WpaFlags"))
586 print " AP RSN Flags:", bitmask_str(self
.NM_802_11_AP_SEC
,
587 self
.appi
.Get(NMI
, "RsnFlags"))
589 # this is the client side of the applet; see also UserSettings
591 def __init__(self
, svc
, opath
):
594 self
.so
= bus
.get_object(self
.svc
, self
.opath
)
595 self
.si
= dbus
.Interface(self
.so
, 'org.freedesktop.NetworkManagerSettings')
598 return self
.svc
== SSC
;
601 for conn
in self
.Connections():
606 def DumpSystem(self
):
607 sspi
= dbus
.Interface(self
.so
, PI
)
608 print "Unmanaged Devices"
609 umds
= sspi
.Get(NMI
, "UnmanagedDevices")
612 # dump_settings_conn(svc, conn) umd?
615 def myConnection(self
, opath
):
616 return cConnection(self
.svc
, opath
)
618 def Connections(self
):
619 opaths
= self
.si
.ListConnections()
620 return map(self
.myConnection
, opaths
)
622 NETWORK_TYPE_ALLOWED
= 1
623 class cApplet_06(cApplet
):
624 def __init__(self
, svc
, opath
):
627 self
.io
= bus
.get_object(self
.svc
, self
.opath
)
628 self
.ii
= dbus
.Interface(self
.io
, 'org.freedesktop.NetworkManagerInfo')
633 def myConnection(self
, opath
):
634 return cConnection_06(self
, opath
)
636 # TODO also VPN conns
637 def Connections(self
):
638 names
= self
.ii
.getNetworks(NETWORK_TYPE_ALLOWED
)
639 return map(self
.myConnection
, names
)
642 def __init__(self
, svc
, opath
):
645 self
.co
= bus
.get_object(self
.svc
, self
.opath
)
646 self
.ci
= dbus
.Interface(self
.co
, 'org.freedesktop.NetworkManagerSettings.Connection')
649 print "Conn:", self
.opath
650 print " Id:", self
.ci
.GetID()
651 settings
= self
.Settings()
654 si
= dbus
.Interface(self
.co
, 'org.freedesktop.NetworkManagerSettings.Connection.Secrets')
655 security
= settings
.Security()
657 print " SECRETS:", security
659 secrets
= cSettings(si
.GetSecrets(security
,[],False))
663 return cSettings(self
.ci
.GetSettings())
665 def dump_time(unixtime
):
666 return time
.asctime(time
.localtime(unixtime
))
668 class cConnection_06
:
669 def __init__(self
, applet
, id):
674 print "Conn:", self
.id
676 np
= self
.applet
.ii
.getNetworkProperties(self
.id, NETWORK_TYPE_ALLOWED
)
679 print " time:", dump_time(np
[1])
680 print " trusted:", bool(np
[2])
681 print " bssids:", ", ".join(np
[3])
683 print " we_cipher:", enctype
685 print " secret:", np
[5]
687 print " wep_auth_algorithm:", np
[6]
689 print " wpa_psk_key_mgt:", np
[6]
690 print " wpa_psk_wpa_version:", np
[7]
692 return # nm-applet will not tell kfn anyway
693 devp
= "/org/freedesktop/NetworkManager/Devices/ath0" #FIXME
694 netp
= devp
+ "/Networks/" + opath_escape(self
.id)
697 kfn
= self
.applet
.ii
.getKeyForNetwork(devp
, netp
, ssid
, attempt
, newkey
)
702 NM_AUTH_TYPE_WPA_PSK_AUTO
= 0x00000000
703 NM_AUTH_TYPE_NONE
= 0x00000001
704 NM_AUTH_TYPE_WEP40
= 0x00000002
705 NM_AUTH_TYPE_WPA_PSK_TKIP
= 0x00000004
706 NM_AUTH_TYPE_WPA_PSK_CCMP
= 0x00000008
707 NM_AUTH_TYPE_WEP104
= 0x00000010
708 NM_AUTH_TYPE_WPA_EAP
= 0x00000020
709 NM_AUTH_TYPE_LEAP
= 0x00000040
711 IW_AUTH_ALG_OPEN_SYSTEM
= 0x00000001
712 IW_AUTH_ALG_SHARED_KEY
= 0x00000002
713 IW_AUTH_ALG_LEAP
= 0x00000004
716 def __init__(self
, conmap
):
717 #print "INIT", conmap
721 return self
.conmap
["connection"]["type"]
724 return self
.conmap
["connection"]["id"]
727 s
= self
.conmap
["802-11-wireless"]["ssid"]
733 return self
.conmap
["connection"]["timestamp"]
741 def SeenBssids(self
):
743 return self
.conmap
["802-11-wireless"]["seen-bssids"]
751 return NM_AUTH_TYPE_WEP104
753 return NM_AUTH_TYPE_WPA_PSK_AUTO
755 return NM_AUTH_TYPE_NONE
756 print "Defaulting cipher type to none"
757 return NM_AUTH_TYPE_NONE
761 return self
.conmap
["802-11-wireless-security"]["psk"]
765 return self
.conmap
["802-11-wireless-security"]["wep-key0"]
771 def WepAuthAlgorithm(self
):
772 print "FIXME Defaulting WEP auth alg to open"
773 return IW_AUTH_ALG_OPEN_SYSTEM
776 print "FIXME Defaulting PSK key mgmt to 2"
779 def PskWpaVersion(self
):
780 print "FIXME Defaulting WPA version to 2"
785 return self
.conmap
[self
.Type()]["security"]
789 def isNet(self
, net_name
):
790 return self
.ID() == net_name
or self
.Ssid() == net_name
792 # FIXME check spec/NM what to censor
793 secrets
= dict.fromkeys(["wep-key0", "psk"])
796 "For GetSettings: censor secrets."
799 for n1
, v1
in self
.conmap
.iteritems():
801 for n2
, v2
in v1
.iteritems():
803 if self
.secrets
.has_key(n2
):
809 "For GetSecrets: only secrets."
818 for n1
, v1
in self
.conmap
.iteritems():
820 for n2
, v2
in v1
.iteritems():
821 print " %s: %s" % (n2
, v2
)
823 def mkconmap_wifi(ssid
):
826 'id': '_cnm_handcrafted_',
827 'uuid': str(uuid
.uuid1()), # new in oS 11.1
828 'type': '802-11-wireless',
831 'ssid': dbus
.ByteArray(ssid
),
832 'mode': 'infrastructure',
833 'security': '802-11-wireless-security',
835 '802-11-wireless-security': {
840 def elongate(s
, tlen
):
841 "repeat string s to target length tlen"
844 copies_needed
= int(math
.ceil(tlen
/ float(len(s
))))
845 return (s
* copies_needed
)[:tlen
]
847 # http://www.mail-archive.com/networkmanager-list@gnome.org/msg07935.html
848 def wep_passphrase_to_hash(p
):
849 return hashlib
.md5(elongate(p
, 64)).hexdigest()
851 def mkconmap_wep_pass(ssid
, key
):
852 cm
= mkconmap_wifi(ssid
)
853 cm
["802-11-wireless-security"]["key-mgmt"] = "none"
854 cm
["802-11-wireless-security"]["wep-tx-keyidx"] = 0
855 cm
["802-11-wireless-security"]["wep-key0"] = wep_passphrase_to_hash(key
)
858 def mkconmap_wep(ssid
, key
):
859 cm
= mkconmap_wifi(ssid
)
860 cm
["802-11-wireless-security"]["key-mgmt"] = "none"
861 cm
["802-11-wireless-security"]["wep-tx-keyidx"] = 0
862 cm
["802-11-wireless-security"]["wep-key0"] = key
865 def mkconmap_psk(ssid
, key
):
866 cm
= mkconmap_wifi(ssid
)
867 cm
["802-11-wireless-security"]["key-mgmt"] = "wpa-psk"
868 cm
["802-11-wireless-security"]["wep-tx-keyidx"] = 0
869 cm
["802-11-wireless-security"]["psk"] = key
873 # server analog of cApplet
874 class UserSettings(dbus
.service
.Object
):
876 def __init__(self
, opath
, conmaps
):
877 dbus
.service
.Object
.__init
__(self
, bus
, opath
)
878 #print "CONMAPS:", conmaps
879 self
.conns
= map(self
.newCon
, conmaps
)
881 def addCon(self
, conmap
):
882 c
= self
.newCon(conmap
)
887 def newCon(self
, conmap
):
888 cpath
= "/MyConnection/%d" % self
.counter
889 self
.counter
= self
.counter
+ 1
890 c
= Connection(cpath
, conmap
)
891 self
.NewConnection(cpath
) # announce it
894 @dbus.service
.method(dbus_interface
='org.freedesktop.NetworkManagerSettings',
895 in_signature
='', out_signature
='ao')
896 def ListConnections(self
):
897 return [c
.__dbus
_object
_path
__ for c
in self
.conns
]
899 #this is for EMITTING a signal, not receiving it
900 @dbus.service
.signal(dbus_interface
='org.freedesktop.NetworkManagerSettings',
902 def NewConnection(self
, opath
):
904 #print "signalling newconn:", opath
906 def GetByNet(self
, net_name
):
907 "Returns connection, or None"
909 if c
.isNet(net_name
):
914 class UserSettings_06(UserSettings
):
916 def __init__(self
, opath
, conmaps
):
917 dbus
.service
.Object
.__init
__(self
, bus
, opath
)
918 #print "CONMAPS:", conmaps
919 self
.conns
= map(self
.newCon
, conmaps
)
922 def newCon(self
, conmap
):
923 cpath
= "/MyConnection/%d" % self
.counter
924 self
.counter
= self
.counter
+ 1
925 c
= Connection_06(cpath
, conmap
)
926 #self.NewConnection(cpath) # announce it
929 @dbus.service
.method(dbus_interface
="org.freedesktop.NetworkManagerInfo",
930 in_signature
="i", out_signature
='as')
931 def getNetworks(self
, i
):
932 # FIXME bytearray to str WHERE?
933 #n = [ssid_str(c.Ssid()) for c in self.conns]
934 n
= [c
.ID() for c
in self
.conns
]
935 print "getNetworks:", n
938 @dbus.service
.method(dbus_interface
="org.freedesktop.NetworkManagerInfo",
939 in_signature
="", out_signature
='ao') # out??
940 def getVPNConnections(self
):
943 @dbus.service
.method(dbus_interface
="org.freedesktop.NetworkManagerInfo",
945 #out_signature='sibasi') #varies
946 def getNetworkProperties(self
, net
, type):
948 # type is 1, NETWORK_TYPE_ALLOWED
949 c
= self
.GetByNet(net
)
951 return c
.getNetworkProperties()
952 print "Oops, could not getNetworkProperties for " + net
955 @dbus.service
.method(dbus_interface
="org.freedesktop.NetworkManagerInfo",
956 in_signature
="oosib")
957 #out_signature="isi") varies
958 def getKeyForNetwork(self
, dev
, net
, ssid
, attempt
, newkey
):
959 print "GKFN", dev
, net
, ssid
, attempt
, bool(newkey
)
961 m
= "Cannot ask for key"
963 raise dbus
.exceptions
.DBusException(m
)
965 snet
= opath_unescape(net
[net
.rfind("/")+1 : ]) # only stuff after /
966 c
= self
.GetByNet(snet
)
968 return c
.getKeyForNetwork()
969 print "Oops, could not getKeyForNetwork " + net
971 @dbus.service
.method(dbus_interface
="org.freedesktop.NetworkManagerInfo",
973 #in_signature="sbs isi", varies
974 def updateNetworkInfo(self
, ssid
, automatic
, bssid
, *security
):
975 print "Connected successfully"
979 print " automatic:", bool(automatic
)
980 print " bssid:", bssid
981 print " security:", security
984 def GetByNet(self
, net_name
):
985 "Returns connection, or None"
987 if c
.isNet(net_name
):
992 # server analog of cConnection
993 class Connection(dbus
.service
.Object
):
994 def __init__(self
, opath
, conmap
):
995 dbus
.service
.Object
.__init
__(self
, bus
, opath
)
996 self
.settings
= cSettings(conmap
)
998 @dbus.service
.method(dbus_interface
='org.freedesktop.NetworkManagerSettings.Connection',
999 sender_keyword
='sender',
1000 in_signature
='', out_signature
='a{sa{sv}}')
1001 def GetSettings(self
, sender
):
1002 #print "Getting settings:", self. __dbus_object_path__
1003 # return self.settings.ConMap()
1004 # grr, censoring secrets makes NM complain!?
1005 # bnc#479566#c3: Until I figure out how to make it work with
1006 # censored secrets, only pass the settings to the same user.
1007 sender_uid
= bus
.get_unix_user(sender
)
1008 if sender_uid
!= 0 and sender_uid
!= os
.geteuid():
1009 e
= "User %u is not permitted to read the settings" % sender_uid
1011 raise dbus
.exceptions
.DBusException(e
) # could do NM_SETTINGS_ERROR_* instead
1012 return self
.settings
.conmap
1014 @dbus.service
.method(dbus_interface
='org.freedesktop.NetworkManagerSettings.Connection.Secrets',
1015 in_signature
='sasb', out_signature
='a{sa{sv}}')
1016 def GetSecrets(self
, tag
, hints
, ask
):
1017 # FIXME respect args
1018 print "Getting secrets:", self
.__dbus
_object
_path
__
1019 return self
.settings
.SecMap()
1021 @dbus.service
.method(dbus_interface
='org.freedesktop.NetworkManagerSettings.Connection',
1022 in_signature
='', out_signature
='s')
1024 return self
.settings
.ID()
1027 return self
.settings
.Ssid()
1029 def isNet(self
, net_name
):
1030 return self
.settings
.isNet(net_name
)
1032 class Connection_06(Connection
):
1033 def __init__(self
, opath
, conmap
):
1034 dbus
.service
.Object
.__init
__(self
, bus
, opath
)
1036 self
.settings
= cSettings(conmap
)
1038 # dbus.service.method
1039 def getNetworkProperties(self
):
1040 # essid, timestamp, ?, bssids, we_cipher, ?, ...
1041 # we_cipher=16: i wep_auth_algorithm
1042 # we_cipher=0: i wpa_psk_key_mgt, i wpa_psk_wpa_version
1043 ssid
= ssid_str(self
.settings
.Ssid())
1044 time
= self
.settings
.Timestamp() # last sucessfully connected? seen?
1045 trusted
= self
.settings
.Trusted()
1046 bssids
= dbus
.Array(self
.settings
.SeenBssids(), signature
="s")
1047 r
= [ssid
, time
, trusted
, bssids
]
1048 security
= self
.getKeyForNetwork("fake key")
1052 # dbus.service.method
1053 def getKeyForNetwork(self
, fake
="no"):
1055 key
= self
.settings
.Key()
1060 cip
= self
.settings
.WeCipher()
1061 if cip
== NM_AUTH_TYPE_NONE
:
1062 security
= tuple([cip
])
1063 elif cip
== NM_AUTH_TYPE_WEP40
or cip
== NM_AUTH_TYPE_WEP104
:
1064 wep_auth_algorithm
= self
.settings
.WepAuthAlgorithm()
1065 security
= (cip
, key
, wep_auth_algorithm
)
1066 elif cip
== NM_AUTH_TYPE_WPA_PSK_AUTO
or cip
== NM_AUTH_TYPE_TKIP
or \
1067 cip
== NM_AUTH_TYPE_CCMP
:
1068 wpa_psk_key_mgt
= self
.settings
.PskKeyMgt()
1069 wpa_psk_wpa_version
= self
.settings
.PskWpaVersion()
1070 security
= (cip
, key
, wpa_psk_key_mgt
, wpa_psk_wpa_version
)
1071 elif cip
== NM_AUTH_TYPE_WPA_EAP
:
1072 security
= tuple([cip
]) # TODO more...
1073 elif cip
== NM_AUTH_TYPE_LEAP
:
1074 security
= tuple([cip
]) # TODO more...
1078 class ConfigParserKNM
:
1079 "Parse ~/.kde/share/config/knetworkmanagerrc"
1082 p
= ConfigParser
.RawConfigParser()
1083 ok
= p
.read(os
.getenv("HOME") + "/.kde/share/config/knetworkmanagerrc")
1086 for s
in p
.sections():
1089 if path
[0] in ["ConnectionSetting", "ConnectionSecrets"]:
1091 self
.conmaps_d
.setdefault(cid
, {})
1095 for (n
, v
) in p
.items(s
):
1096 # WTF, Value_ is transfrmed to value_
1097 if n
[:6] == "value_":
1099 v
= self
.ParseValue(v
)
1101 if len(values
) != 0: # empty 802-1x confuses NM!?
1102 self
.conmaps_d
[cid
].setdefault(part
, {})
1103 self
.conmaps_d
[cid
][part
].update(**values
)
1104 #print "PARSED", cid, part, values
1107 return self
.conmaps_d
.values()
1109 def ParseValue(self
, v
):
1110 v
= eval('"%s"' % v
) # unescape backslashes
1111 dom
= xml
.dom
.minidom
.parseString(v
)
1112 return self
.ParseNode(dom
.documentElement
)
1114 def ParseNode(self
, n
):
1117 v
= self
.NodeText(n
)
1122 return dbus
.Byte(int(v
))
1125 elif t
== "int32" or t
== "uint32":
1131 if c
.localName
!= None: # whitespace
1132 v
.append(self
.ParseNode(c
))
1136 def NodeText(self
, n
):
1137 if n
.hasChildNodes():
1138 return n
.firstChild
.wholeText
1142 def abbr_signal_handler(*args
, **kwargs
):
1143 ifc
= kwargs
["interface"]
1144 sig
= kwargs
["member"]
1145 opath
= kwargs
["path"]
1146 line
= "SIG %s: %s.%s%s" % (abbrev(opath
,"/"), abbrev(ifc
,"."), sig
, args
)
1152 bus
.add_signal_receiver(self
.abbr_signal_handler
,
1153 path_keyword
="path",
1154 interface_keyword
="interface",
1155 member_keyword
="member")
1157 def abbr_signal_handler(self
, *args
, **kwargs
):
1158 ifc
= kwargs
["interface"]
1159 sig
= kwargs
["member"]
1160 opath
= kwargs
["path"]
1161 line
= "SIG %s: %s.%s%s" % (self
.abbrev(opath
,"/"),
1162 self
.abbrev(ifc
,"."),
1166 def abbrev(self
, s
, sep
):
1167 words
= s
.split(sep
)
1168 words
= map (self
.a1
, words
)
1169 result
= sep
.join(words
)
1170 if not self
.amap
.has_key(s
):
1171 print "ABBR %s is %s" % (result
, s
)
1172 self
.amap
[s
] = result
1174 if self
.amap
[s
] != result
:
1175 print "ABBR COLLISION %s was %s now %s" % (s
, self
.amap
[s
], result
)
1186 op
= OptionParser(version
="%prog " + VERSION
)
1187 op
.add_option("-d", "--dev",
1188 action
="store_true", default
=False,
1189 help="list devices")
1190 op
.add_option("-c", "--actcon",
1191 action
="store_true", default
=False,
1192 help="list active connections")
1193 op
.add_option("-u", "--usrcon",
1194 action
="store_true", default
=False,
1195 help="list user connection settings (can CRASH nm-applet)")
1196 op
.add_option("-s", "--syscon",
1197 action
="store_true", default
=False,
1198 help="list system connection settings")
1199 op
.add_option("-a", "--ap",
1200 action
="store_true", default
=False,
1201 help="list found access points")
1202 op
.add_option("-n", "--nets",
1203 action
="store_true", default
=False,
1204 help="list found wireless networks")
1205 # TODO http://docs.python.org/lib/optparse-adding-new-types.html
1206 op
.add_option("-w", "--wifi",
1207 choices
=["0","1","off","on","no","yes","false","true"],
1209 help="enable or disable wireless")
1210 op
.add_option("-o", "--online",
1211 choices
=["0","1","off","on","no","yes","false","true"],
1213 help="enable or disable network at all")
1215 op
.add_option("-C", "--connect",
1216 help="connect to a wireless network NET (using knetworkmanagerrc or the key options below)",
1218 op
.add_option("--unprotected",
1219 action
="store_true", default
=False,
1220 help="network does not require a key")
1221 op
.add_option("--wep-hex",
1223 help="use this WEP key of 26 hex digits")
1224 op
.add_option("--wep-pass",
1226 help="use this WEP passphrase")
1227 op
.add_option("--wpa-psk-hex",
1229 help="use this WPA key of 64 hex digits")
1230 op
.add_option("-m", "--monitor",
1231 action
="store_true", default
=False,
1232 help="loop to show dbus signals")
1235 (options
, args
) = op
.parse_args()
1243 nmp
= '/org/freedesktop/NetworkManager'
1246 except dbus
.exceptions
.DBusException
, e
:
1248 print "NetworkManager is not running"
1250 if options
.dev
or options
.actcon
:
1253 true_choices
= ["1", "on", "yes", "true"]
1254 if options
.wifi
!= None:
1255 nm
.SetWifiEnabled(options
.wifi
in true_choices
)
1256 if options
.online
!= None:
1257 nm
.SetOnline(options
.online
in true_choices
)
1263 print "SYSTEM Connections"
1264 if nm
.Api() == "06":
1265 print "Cannot do that with NM 0.6"
1267 ss
= cApplet(SSC
, '/org/freedesktop/NetworkManagerSettings')
1271 print "USER Connections"
1273 if nm
.Api() == "06":
1274 us
= cApplet_06(NMIC
, "/org/freedesktop/NetworkManagerInfo")
1276 us
= cApplet(USC
, '/org/freedesktop/NetworkManagerSettings')
1278 except dbus
.exceptions
.DBusException
, e
:
1280 #if e.get_dbus_name() == "org.freedesktop.DBus.Error.ServiceUnknown":
1281 print "Applet is not running"
1283 nmo
= bus
.get_object(NMC
, nmp
)
1284 nmi
= dbus
.Interface(nmo
, NMI
)
1286 def service_pid(name
):
1287 DBS
= 'org.freedesktop.DBus'
1289 dbo
= bus
.get_object(DBS
, '/')
1290 dbi
= dbus
.Interface(dbo
, DBI
)
1291 owner
= dbi
.GetNameOwner(name
)
1292 pid
= dbi
.GetConnectionUnixProcessID(owner
)
1295 # TODO UserSettings_06
1296 if options
.connect
!= None:
1297 if nm
.Api() == "06":
1301 brn
= bus
.request_name(name
, _dbus_bindings
.NAME_FLAG_DO_NOT_QUEUE
)
1302 if brn
== _dbus_bindings
.REQUEST_NAME_REPLY_EXISTS
:
1303 print "Could not provide settings service, another applet is running (pid %s)" % service_pid(name
)
1305 cfg
= ConfigParserKNM()
1306 if nm
.Api() == "06":
1307 us
= UserSettings_06("/org/freedesktop/NetworkManagerInfo",
1310 us
= UserSettings("/org/freedesktop/NetworkManagerSettings",
1313 def Connect(wanted_net
): # any. or take arg. net is config name or ssid name
1314 # ... in general, look for string in all config data. ssid for wifi, whatever for dialup
1315 # TODO also respect autoconnect
1317 # ActivateConn wants setting device ap; can find device from ap? ap is "specific" for wifi devices
1318 #print "Connection wanted to", wanted_net
1319 found_con
= found_ap
= found_dev
= None
1320 for dev
in nm
.Devices():
1321 for ap
in dev
.APs():
1322 if wanted_net
== ap
.Ssid():
1325 break # FIXME both loops
1326 found_con
= us
.GetByNet(wanted_net
)
1327 if found_ap
== None:
1328 print "No AP found with SSID", wanted_net
1330 if found_con
== None:
1331 print "No settings for net %s, assuming no key is needed" % wanted_net
1332 c
= mkconmap_wifi(wanted_net
)
1333 found_con
= us
.addCon(c
)
1334 nm
.ActivateConnection(found_con
, found_dev
, found_ap
) # TODO async
1335 # TODO run loop, exit it when we have serviced the required calls
1338 if options
.connect
!= None:
1339 if options
.unprotected
:
1340 c
= mkconmap_wifi(options
.connect
)
1342 if options
.wep_hex
!= None:
1343 c
= mkconmap_wep(options
.connect
, options
.wep_hex
)
1345 if options
.wep_pass
!= None:
1346 c
= mkconmap_wep_pass(options
.connect
, options
.wep_pass
)
1348 if options
.wpa_psk_hex
!= None:
1349 c
= mkconmap_psk(options
.connect
, options
.wpa_psk_hex
)
1351 if Connect(options
.connect
):
1358 loop
= gobject
.MainLoop()