Separated the client classes.
[cnetworkmanager.git] / cnetworkmanager
blob39c07c2fd75c1bcc1d907a86264bcaa218cbe56a
1 #! /usr/bin/python
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
6 VERSION = "0.8.4"
7 print "cnetworkmanager %s - Command Line Interface for NetworkManager" % VERSION
9 norpm = False
10 import sys
11 # find other modules in our prefix, if specified
12 if len(sys.argv) > 2 and sys.argv[1] == "--prefix":
13 prefix = sys.argv[2]
14 sys.argv[1:] = sys.argv[3:]
15 sys.path.append(prefix + "/share/cnetworkmanager");
17 import os
18 import string
19 import re
20 import time
21 from optparse import OptionParser
22 try:
23 import dbus
24 import dbus.service
25 import _dbus_bindings
26 except:
27 print "Install python-1-dbus.rpm or or python-dbus.rpm or python-dbus.deb"
28 norpm = True
29 try:
30 import gobject
31 except:
32 # todo - only if loop wanted
33 print "Install python-gobject2.rpm or pygobject2.rpm or python-gobject.deb"
34 norpm = True
35 # python-gnome.rpm has gconf for nm-applet...
36 if norpm:
37 sys.exit(1)
39 from dbus.mainloop.glib import DBusGMainLoop
40 DBusGMainLoop(set_as_default=True)
42 # private modules:
43 from monitor import MonitorBase
44 from configparser_knm import ConfigParserKNM
45 from mkconmap import *
46 from util import *
48 from object import *
49 from manager import *
50 from manager06 import cNM_06
51 from manager07 import cNM_07
52 from device import cDevice
53 from device06 import cDevice_06
54 from device07 import cDevice_07
55 from ap import cAP
56 from ap06 import cAP_06
57 from ap07 import cAP_07
59 LOOP = False
61 bus = dbus.SystemBus()
63 # FOOC = connection (service) string
64 # FOOI = interface string
65 # fooo = object
66 # fooi = interface
67 # foopi = property interface
68 SSC = "org.freedesktop.NetworkManagerSystemSettings"
69 USC = "org.freedesktop.NetworkManagerUserSettings"
70 NMIC = "org.freedesktop.NetworkManagerInfo"
72 def introspect(obj):
73 ii = dbus.Interface(obj, 'org.freedesktop.DBus.Introspectable')
74 print ii.Introspect()
76 def make_nm(opath):
77 "Detects NM version and chooses appropriate class"
79 nmo = bus.get_object(NMC, opath)
80 nmi = dbus.Interface(nmo, NMI)
81 try:
82 dummy = nmi.getDevices()
83 return cNM_06(opath, options)
84 except dbus.exceptions.DBusException, e:
85 if e.get_dbus_name() == 'org.freedesktop.DBus.Error.AccessDenied':
86 raise
87 return cNM_07(opath, options)
89 def opath_validchar(c):
90 # _ is also escaped even though it is valid
91 return \
92 string.ascii_letters.find(c) != -1 or \
93 string.digits.find(c) != -1
95 def opath_escape(s):
96 r = ""
97 for c in s:
98 # TODO find a more elegant way
99 if not opath_validchar(c):
100 # "-" -> "_2d_"
101 c = "_%2x_" % ord(c)
102 r = r + c
103 return r
105 def opath_unescape(s):
106 # "2d" -> "-"
107 unhex = lambda xx: chr(eval("0x"+xx))
108 # all "_2d_" -> "-"
109 return re.sub("_.._", lambda p: unhex(p.group()[1:3]), s)
111 # this is the client side of the applet; see also UserSettings
112 class cApplet:
113 def __init__(self, svc, opath):
114 self.svc = svc
115 self.opath = opath
116 self.so = bus.get_object(self.svc, self.opath)
117 self.si = dbus.Interface(self.so, 'org.freedesktop.NetworkManagerSettings')
119 def isSystem(self):
120 return self.svc == SSC;
122 def Dump(self):
123 for conn in self.Connections():
124 conn.Dump()
125 if self.isSystem():
126 self.DumpSystem()
128 def DumpSystem(self):
129 sspi = dbus.Interface(self.so, PI)
130 print "Unmanaged Devices"
131 umds = sspi.Get(NMI, "UnmanagedDevices")
132 for umd in umds:
133 print " ", umd
134 # dump_settings_conn(svc, conn) umd?
137 def myConnection(self, opath):
138 return cConnection(self.svc, opath)
140 def Connections(self):
141 opaths = self.si.ListConnections()
142 return map(self.myConnection, opaths)
144 NETWORK_TYPE_ALLOWED = 1
145 class cApplet_06(cApplet):
146 def __init__(self, svc, opath):
147 self.svc = svc
148 self.opath = opath
149 self.io = bus.get_object(self.svc, self.opath)
150 self.ii = dbus.Interface(self.io, 'org.freedesktop.NetworkManagerInfo')
152 def isSystem(self):
153 return False;
155 def myConnection(self, opath):
156 return cConnection_06(self, opath)
158 # TODO also VPN conns
159 def Connections(self):
160 names = self.ii.getNetworks(NETWORK_TYPE_ALLOWED)
161 return map(self.myConnection, names)
163 class cConnection:
164 def __init__(self, svc, opath):
165 self.svc = svc
166 self.opath = opath
167 self.co = bus.get_object(self.svc, self.opath)
168 self.ci = dbus.Interface(self.co, 'org.freedesktop.NetworkManagerSettings.Connection')
170 def Dump(self):
171 print "Conn:", self.opath
172 settings = self.Settings()
173 settings.Dump()
175 si = dbus.Interface(self.co, 'org.freedesktop.NetworkManagerSettings.Connection.Secrets')
176 security = settings.Security()
177 if security != "":
178 print " SECRETS:", security
179 try:
180 # TODO merge them
181 secrets = cSettings(si.GetSecrets(security,[],False))
182 secrets.Dump()
183 except dbus.exceptions.DBusException, e:
184 if e.get_dbus_name() == "org.freedesktop.DBus.Error.AccessDenied":
185 print " Access denied"
186 else:
187 print " ", e
188 print " FIXME figure out 802-1x secrets"
190 def Settings(self):
191 return cSettings(self.ci.GetSettings())
193 def dump_time(unixtime):
194 return time.asctime(time.localtime(unixtime))
196 class cConnection_06:
197 def __init__(self, applet, id):
198 self.id = id
199 self.applet = applet
201 def Dump(self):
202 print "Conn:", self.id
204 np = self.applet.ii.getNetworkProperties(self.id, NETWORK_TYPE_ALLOWED)
205 ssid = np[0]
206 print " ssid:", ssid
207 print " time:", dump_time(np[1])
208 print " trusted:", bool(np[2])
209 print " bssids:", ", ".join(np[3])
210 enctype = np[4]
211 print " we_cipher:", enctype
212 if enctype != 1:
213 print " secret:", np[5]
214 if enctype == 16:
215 print " wep_auth_algorithm:", np[6]
216 elif enctype == 0:
217 print " wpa_psk_key_mgt:", np[6]
218 print " wpa_psk_wpa_version:", np[7]
220 return # nm-applet will not tell kfn anyway
221 devp = "/org/freedesktop/NetworkManager/Devices/ath0" #FIXME
222 netp = devp + "/Networks/" + opath_escape(self.id)
223 attempt = 1
224 newkey = False
225 kfn = self.applet.ii.getKeyForNetwork(devp, netp, ssid, attempt, newkey)
226 print " kfn:", kfn
229 # 06
230 NM_AUTH_TYPE_WPA_PSK_AUTO = 0x00000000
231 NM_AUTH_TYPE_NONE = 0x00000001
232 NM_AUTH_TYPE_WEP40 = 0x00000002
233 NM_AUTH_TYPE_WPA_PSK_TKIP = 0x00000004
234 NM_AUTH_TYPE_WPA_PSK_CCMP = 0x00000008
235 NM_AUTH_TYPE_WEP104 = 0x00000010
236 NM_AUTH_TYPE_WPA_EAP = 0x00000020
237 NM_AUTH_TYPE_LEAP = 0x00000040
239 IW_AUTH_ALG_OPEN_SYSTEM = 0x00000001
240 IW_AUTH_ALG_SHARED_KEY = 0x00000002
241 IW_AUTH_ALG_LEAP = 0x00000004
243 class cSettings:
244 def __init__(self, conmap):
245 #print "INIT", conmap
246 self.conmap = conmap
248 def Type(self):
249 return self.conmap["connection"]["type"]
251 def ID(self):
252 return self.conmap["connection"]["id"]
254 def Ssid(self):
255 try:
256 return self.conmap["802-11-wireless"]["ssid"]
257 except KeyError:
258 pass
259 # probably 802-3-ethernet
260 return ""
262 def Timestamp(self):
263 try:
264 return self.conmap["connection"]["timestamp"]
265 except KeyError:
266 return 0
268 def Trusted(self):
269 # false by default
270 return False
272 def SeenBssids(self):
273 try:
274 return self.conmap["802-11-wireless"]["seen-bssids"]
275 except KeyError:
276 return []
278 # for 06
279 def WeCipher(self):
280 k = self.Key()
281 if len(k) == 26:
282 return NM_AUTH_TYPE_WEP104
283 elif len(k) == 64:
284 return NM_AUTH_TYPE_WPA_PSK_AUTO
285 elif len(k) == 0:
286 return NM_AUTH_TYPE_NONE
287 print "Defaulting cipher type to none"
288 return NM_AUTH_TYPE_NONE
290 def Key(self):
291 try:
292 return self.conmap["802-11-wireless-security"]["psk"]
293 except KeyError:
294 pass
295 try:
296 return self.conmap["802-11-wireless-security"]["wep-key0"]
297 except KeyError:
298 pass
299 # no key
300 return ""
302 def WepAuthAlgorithm(self):
303 print "FIXME Defaulting WEP auth alg to open"
304 return IW_AUTH_ALG_OPEN_SYSTEM
306 def PskKeyMgt(self):
307 print "FIXME Defaulting PSK key mgmt to 2"
308 return 2
310 def PskWpaVersion(self):
311 print "FIXME Defaulting WPA version to 2"
312 return 2
314 def Security(self):
315 try:
316 return self.conmap[self.Type()]["security"]
317 except KeyError:
318 return ""
320 def isNet(self, net_name):
321 return self.ID() == net_name or self.Ssid() == net_name
323 # FIXME check spec/NM what to censor
324 secrets = dict.fromkeys(["wep-key0", "psk"])
326 def ConMap(self):
327 "For GetSettings: censor secrets."
329 cm = dict()
330 for n1, v1 in self.conmap.iteritems():
331 cm[n1] = dict()
332 for n2, v2 in v1.iteritems():
333 cv2 = v2
334 if self.secrets.has_key(n2):
335 cv2 = ""
336 cm[n1][n2] = cv2
337 return cm
339 def SecMap(self):
340 "For GetSecrets: only secrets."
341 s = self.Security()
342 r = {
343 s: self.conmap[s]
345 print "SECMAP", r
346 return r
348 def Dump(self):
349 for n1, v1 in self.conmap.iteritems():
350 print " ",n1
351 for n2, v2 in v1.iteritems():
352 print " %s: %s" % (n2, v2)
354 # server analog of cApplet
355 class UserSettings(dbus.service.Object):
356 # conmaps is a list
357 def __init__(self, opath, conmaps):
358 dbus.service.Object.__init__(self, bus, opath)
359 #print "CONMAPS:", conmaps
360 self.conns = map(self.newCon, conmaps)
362 def addCon(self, conmap):
363 c = self.newCon(conmap)
364 self.conns.append(c)
365 return c
367 counter = 1
368 def newCon(self, conmap):
369 cpath = "/MyConnection/%d" % self.counter
370 self.counter = self.counter + 1
371 c = Connection(cpath, conmap)
372 self.NewConnection(cpath) # announce it
373 return c
375 @dbus.service.method(dbus_interface='org.freedesktop.NetworkManagerSettings',
376 in_signature='', out_signature='ao')
377 def ListConnections(self):
378 return [c.__dbus_object_path__ for c in self.conns]
380 #this is for EMITTING a signal, not receiving it
381 @dbus.service.signal(dbus_interface='org.freedesktop.NetworkManagerSettings',
382 signature='o')
383 def NewConnection(self, opath):
384 pass
385 #print "signalling newconn:", opath
387 def GetByNet(self, net_name):
388 "Returns connection, or None"
389 for c in self.conns:
390 if c.isNet(net_name):
391 return c
392 return None
395 class UserSettings_06(UserSettings):
396 # conmaps is a list
397 def __init__(self, opath, conmaps):
398 dbus.service.Object.__init__(self, bus, opath)
399 #print "CONMAPS:", conmaps
400 self.conns = map(self.newCon, conmaps)
402 counter = 1
403 def newCon(self, conmap):
404 cpath = "/MyConnection/%d" % self.counter
405 self.counter = self.counter + 1
406 c = Connection_06(cpath, conmap)
407 #self.NewConnection(cpath) # announce it
408 return c
410 @dbus.service.method(dbus_interface="org.freedesktop.NetworkManagerInfo",
411 in_signature="i", out_signature='as')
412 def getNetworks(self, i):
413 # FIXME bytearray to str WHERE?
414 #n = [ssid_str(c.Ssid()) for c in self.conns]
415 n = [c.ID() for c in self.conns]
416 print "getNetworks:", n
417 return n
419 @dbus.service.method(dbus_interface="org.freedesktop.NetworkManagerInfo",
420 in_signature="", out_signature='ao') # out??
421 def getVPNConnections(self):
422 return [] # FIXME
424 @dbus.service.method(dbus_interface="org.freedesktop.NetworkManagerInfo",
425 in_signature="si")
426 #out_signature='sibasi') #varies
427 def getNetworkProperties(self, net, type):
428 print "GNP", net
429 # type is 1, NETWORK_TYPE_ALLOWED
430 c = self.GetByNet(net)
431 if c != None:
432 return c.getNetworkProperties()
433 print "Oops, could not getNetworkProperties for " + net
436 @dbus.service.method(dbus_interface="org.freedesktop.NetworkManagerInfo",
437 in_signature="oosib")
438 #out_signature="isi") varies
439 def getKeyForNetwork(self, dev, net, ssid, attempt, newkey):
440 print "GKFN", dev, net, ssid, attempt, bool(newkey)
441 if newkey:
442 m = "Cannot ask for key"
443 print m
444 raise dbus.exceptions.DBusException(m)
446 snet = opath_unescape(net[net.rfind("/")+1 : ]) # only stuff after /
447 c = self.GetByNet(snet)
448 if c != None:
449 return c.getKeyForNetwork()
450 print "Oops, could not getKeyForNetwork " + net
452 @dbus.service.method(dbus_interface="org.freedesktop.NetworkManagerInfo",
453 out_signature='')
454 #in_signature="sbs isi", varies
455 def updateNetworkInfo(self, ssid, automatic, bssid, *security):
456 print "Connected successfully"
457 return
458 print "UNI"
459 print " ssid:", ssid
460 print " automatic:", bool(automatic)
461 print " bssid:", bssid
462 print " security:", security
465 def GetByNet(self, net_name):
466 "Returns connection, or None"
467 for c in self.conns:
468 if c.isNet(net_name):
469 return c
470 return None
473 # server analog of cConnection
474 class Connection(dbus.service.Object):
475 def __init__(self, opath, conmap):
476 dbus.service.Object.__init__(self, bus, opath)
477 self.settings = cSettings(conmap)
479 @dbus.service.method(dbus_interface='org.freedesktop.NetworkManagerSettings.Connection',
480 sender_keyword='sender',
481 in_signature='', out_signature='a{sa{sv}}')
482 def GetSettings(self, sender):
483 #print "Getting settings:", self. __dbus_object_path__
484 # return self.settings.ConMap()
485 # grr, censoring secrets makes NM complain!?
486 # bnc#479566#c3: Until I figure out how to make it work with
487 # censored secrets, only pass the settings to the same user.
488 sender_uid = bus.get_unix_user(sender)
489 if sender_uid != 0 and sender_uid != os.geteuid():
490 e = "User %u is not permitted to read the settings" % sender_uid
491 print e
492 raise dbus.exceptions.DBusException(e) # could do NM_SETTINGS_ERROR_* instead
493 return self.settings.conmap
495 @dbus.service.method(dbus_interface='org.freedesktop.NetworkManagerSettings.Connection.Secrets',
496 in_signature='sasb', out_signature='a{sa{sv}}')
497 def GetSecrets(self, tag, hints, ask):
498 # FIXME respect args
499 print "Getting secrets:", self.__dbus_object_path__
500 return self.settings.SecMap()
502 @dbus.service.method(dbus_interface='org.freedesktop.NetworkManagerSettings.Connection',
503 in_signature='', out_signature='s')
504 def ID(self):
505 return self.settings.ID()
507 def Ssid(self):
508 return self.settings.Ssid()
510 def isNet(self, net_name):
511 return self.settings.isNet(net_name)
513 class Connection_06(Connection):
514 def __init__(self, opath, conmap):
515 dbus.service.Object.__init__(self, bus, opath)
516 #print "C6", conmap
517 self.settings = cSettings(conmap)
519 # dbus.service.method
520 def getNetworkProperties(self):
521 # essid, timestamp, ?, bssids, we_cipher, ?, ...
522 # we_cipher=16: i wep_auth_algorithm
523 # we_cipher=0: i wpa_psk_key_mgt, i wpa_psk_wpa_version
524 ssid = ssid_str(self.settings.Ssid())
525 time = self.settings.Timestamp() # last sucessfully connected? seen?
526 trusted = self.settings.Trusted()
527 bssids = dbus.Array(self.settings.SeenBssids(), signature="s")
528 r = [ssid, time, trusted, bssids]
529 security = self.getKeyForNetwork("fake key")
530 r.extend(security)
531 return tuple(r)
533 # dbus.service.method
534 def getKeyForNetwork(self, fake="no"):
535 if fake == "no":
536 key = self.settings.Key()
537 else:
538 key = ""
540 # security
541 cip = self.settings.WeCipher()
542 if cip == NM_AUTH_TYPE_NONE:
543 security = tuple([cip])
544 elif cip == NM_AUTH_TYPE_WEP40 or cip == NM_AUTH_TYPE_WEP104:
545 wep_auth_algorithm = self.settings.WepAuthAlgorithm()
546 security = (cip, key, wep_auth_algorithm)
547 elif cip == NM_AUTH_TYPE_WPA_PSK_AUTO or cip == NM_AUTH_TYPE_TKIP or \
548 cip == NM_AUTH_TYPE_CCMP:
549 wpa_psk_key_mgt = self.settings.PskKeyMgt()
550 wpa_psk_wpa_version = self.settings.PskWpaVersion()
551 security = (cip, key, wpa_psk_key_mgt, wpa_psk_wpa_version)
552 elif cip == NM_AUTH_TYPE_WPA_EAP:
553 security = tuple([cip]) # TODO more...
554 elif cip == NM_AUTH_TYPE_LEAP:
555 security = tuple([cip]) # TODO more...
556 return security
560 class Monitor(MonitorBase):
561 def __init__(self, bus):
562 MonitorBase.__init__(self, bus)
564 self.watch(
565 self.propc_h,
566 dbus_interface="org.freedesktop.NetworkManager.Device.Wireless",
567 signal_name="PropertiesChanged")
568 self.watch(
569 self.propc_h,
570 dbus_interface="org.freedesktop.NetworkManager.AccessPoint",
571 signal_name="PropertiesChanged")
573 self.ignore("org.freedesktop.Hal.Device", "PropertyModified")
574 self.ignore("fi.epitest.hostap.WPASupplicant.Interface", "ScanResultsAvailable")
575 self.ignore("com.redhat.PrinterSpooler", "QueueChanged")
576 self.ignore("org.freedesktop.NetworkManager", "StateChange") # deprecated
577 self.watch(self.nm_sc_h, "org.freedesktop.NetworkManager", "StateChanged")
578 self.watch(self.wpas_isc_h, "fi.epitest.hostap.WPASupplicant.Interface", "StateChange")
579 self.watch(self.nmd_sc_h, "org.freedesktop.NetworkManager.Device", "StateChanged")
580 self.watch(self.bus_noc_h, "org.freedesktop.DBus", "NameOwnerChanged")
582 def bus_noc_h(self, *args, **kwargs):
583 (name, old, new) = args
584 if new == "":
585 new = "gone"
586 else:
587 new = "at " + new
588 print "\tBUS NOC\t%s %s" % (name, new)
590 def wpas_isc_h(self, *args, **kwargs):
591 opath = kwargs["path"]
592 (new, old) = args
593 print "\tWPAS %s\t(%s, was %s)" % (new, opath, old.lower())
595 def nmd_sc_h(self, *args, **kwargs):
596 opath = kwargs["path"]
597 (new, old, reason) = args
598 news = cDevice_07.NM_DEVICE_STATE[new]
599 olds = cDevice_07.NM_DEVICE_STATE[old]
600 reasons = ""
601 if reason != 0:
602 reasons = "reason %d" % reason
603 print "\tDevice State %s\t(%s, was %s%s)" % (news, opath, olds.lower(), reasons)
605 def nm_sc_h(self, *args, **kwargs):
606 s = args[0]
607 ss = cNM.NM_STATE[s]
608 print "\tNM State:", ss
610 def propc_h(self, *args, **kwargs):
611 opath = kwargs["path"]
612 props = args[0]
613 for k, v in props.iteritems():
614 if k == "Strength":
615 v = "%u" % v
616 line = "\tPROP\t%s\t%s\t(%s)" % (k, v, opath)
617 print line
619 # main
621 fail = False
623 op = OptionParser(version="%prog " + VERSION)
624 op.add_option("-d", "--dev",
625 action="store_true", default=False,
626 help="list devices")
627 op.add_option("-c", "--actcon",
628 action="store_true", default=False,
629 help="list active connections")
630 op.add_option("-u", "--usrcon",
631 action="store_true", default=False,
632 help="list user connection settings (can CRASH nm-applet)")
633 op.add_option("-s", "--syscon",
634 action="store_true", default=False,
635 help="list system connection settings")
636 op.add_option("-a", "--ap",
637 action="store_true", default=False,
638 help="list found access points")
639 op.add_option("-n", "--nets",
640 action="store_true", default=False,
641 help="list found wireless networks")
642 # TODO http://docs.python.org/lib/optparse-adding-new-types.html
643 op.add_option("-w", "--wifi",
644 choices=["0","1","off","on","no","yes","false","true"],
645 metavar="BOOL",
646 help="enable or disable wireless")
647 op.add_option("-o", "--online",
648 choices=["0","1","off","on","no","yes","false","true"],
649 metavar="BOOL",
650 help="enable or disable network at all")
652 op.add_option("--activate-connection",
653 help="raw API: activate the KIND(user/system) connection CON on device DEV using AP",
654 metavar="[KIND],CON,DEV,[AP]")
655 op.add_option("-C", "--connect",
656 help="connect to a wireless network NET (using knetworkmanagerrc or the key options below)",
657 metavar="NET")
658 op.add_option("--unprotected",
659 action="store_true", default=False,
660 help="network does not require a key")
661 op.add_option("--wep-hex",
662 metavar="KEY",
663 help="use this WEP key of 26 hex digits")
664 op.add_option("--wep-pass",
665 metavar="KEY",
666 help="use this WEP passphrase")
667 op.add_option("--wpa-psk-hex",
668 metavar="KEY",
669 help="use this WPA key of 64 hex digits")
670 op.add_option("--wpa-pass",
671 metavar="KEY",
672 help="use this WPA passphrase")
673 op.add_option("-m", "--monitor",
674 action="store_true", default=False,
675 help="loop to show dbus signals")
678 (options, args) = op.parse_args()
680 if options.ap:
681 options.dev = True
682 if options.monitor:
683 LOOP = True
686 nmp = '/org/freedesktop/NetworkManager'
687 try:
688 nm = make_nm(nmp)
689 except dbus.exceptions.DBusException, e:
690 print "NetworkManager is not running or running as an other user"
691 fail = True
692 if options.dev or options.actcon:
693 nm.Dump()
695 true_choices = ["1", "on", "yes", "true"]
696 if options.wifi != None:
697 nm.SetWifiEnabled(options.wifi in true_choices)
698 if options.online != None:
699 nm.SetOnline(options.online in true_choices)
701 if options.nets:
702 nm.ListNets()
704 if options.syscon:
705 print "SYSTEM Connections"
706 if nm.Api() == "06":
707 print "Cannot do that with NM 0.6"
708 fail = True
709 else:
710 ss = cApplet(SSC, '/org/freedesktop/NetworkManagerSettings')
711 ss.Dump()
713 if options.usrcon:
714 print "USER Connections"
715 try:
716 if nm.Api() == "06":
717 us = cApplet_06(NMIC, "/org/freedesktop/NetworkManagerInfo")
718 else:
719 us = cApplet(USC, '/org/freedesktop/NetworkManagerSettings')
720 us.Dump()
721 except dbus.exceptions.DBusException, e:
722 print e
723 #if e.get_dbus_name() == "org.freedesktop.DBus.Error.ServiceUnknown":
724 print "Applet is not running"
725 fail = True
727 nmo = bus.get_object(NMC, nmp)
728 nmi = dbus.Interface(nmo, NMI)
730 def service_pid(name):
731 DBS = 'org.freedesktop.DBus'
732 DBI = DBS
733 dbo = bus.get_object(DBS, '/')
734 dbi = dbus.Interface(dbo, DBI)
735 owner = dbi.GetNameOwner(name)
736 pid = dbi.GetConnectionUnixProcessID(owner)
737 return pid
739 # TODO UserSettings_06
740 if options.connect != None:
741 if nm.Api() == "06":
742 name = NMIC
743 else:
744 name = USC
745 brn = bus.request_name(name, _dbus_bindings.NAME_FLAG_DO_NOT_QUEUE)
746 if brn == _dbus_bindings.REQUEST_NAME_REPLY_EXISTS:
747 print "Could not provide settings service, another applet is running (pid %s)" % service_pid(name)
748 sys.exit(1)
749 cfg = ConfigParserKNM()
750 if nm.Api() == "06":
751 us = UserSettings_06("/org/freedesktop/NetworkManagerInfo",
752 cfg.ConMaps())
753 else:
754 us = UserSettings("/org/freedesktop/NetworkManagerSettings",
755 cfg.ConMaps())
757 def Connect(wanted_net): # any. or take arg. net is config name or ssid name
758 # ... in general, look for string in all config data. ssid for wifi, whatever for dialup
759 # TODO also respect autoconnect
761 # ActivateConn wants setting device ap; can find device from ap? ap is "specific" for wifi devices
762 #print "Connection wanted to", wanted_net
763 found_con = found_ap = found_dev = None
764 for dev in nm.Devices():
765 for ap in dev.APs():
766 if wanted_net == ap.Ssid():
767 found_ap = ap
768 found_dev = dev
769 break # FIXME both loops
770 found_con = us.GetByNet(wanted_net)
771 if found_ap == None:
772 print "No AP found with SSID", wanted_net
773 return False
774 if found_con == None:
775 print "No settings for net %s, assuming no key is needed" % wanted_net
776 c = mkconmap_wifi(wanted_net)
777 found_con = us.addCon(c)
778 nm.ActivateConnection(found_con, found_dev, found_ap) # TODO async
779 # TODO run loop, exit it when we have serviced the required calls
780 return True
782 if options.connect != None:
783 if options.unprotected:
784 c = mkconmap_wifi(options.connect)
785 us.addCon(c)
786 if options.wep_hex != None:
787 c = mkconmap_wep(options.connect, options.wep_hex)
788 us.addCon(c)
789 if options.wep_pass != None:
790 c = mkconmap_wep_pass(options.connect, options.wep_pass)
791 us.addCon(c)
792 if options.wpa_psk_hex != None:
793 c = mkconmap_psk(options.connect, options.wpa_psk_hex)
794 us.addCon(c)
795 if options.wpa_pass != None:
796 c = mkconmap_psk(options.connect, options.wpa_pass)
797 us.addCon(c)
798 nm.WatchState()
799 if Connect(options.connect):
800 LOOP = True
801 else:
802 fail = True
804 if options.activate_connection != None:
805 (svc, conpath, devpath, appath) = options.activate_connection.split(',')
806 if svc == "" or svc == "user":
807 svc = USC
808 elif svc == "system":
809 svc = SSC
811 if devpath == "":
812 TODO
813 if appath == "":
814 appath = "/"
815 nm.WatchState()
816 nm.nmi.ActivateConnection(svc, conpath, devpath, appath,
817 reply_handler=nm.silent_handler,
818 error_handler=nm.err_handler,
820 LOOP = True
822 if options.monitor:
823 m = Monitor(bus)
825 def loop():
826 loop = gobject.MainLoop()
827 try:
828 loop.run()
829 except:
830 print "Loop exited"
832 if LOOP:
833 loop()
835 if fail:
836 sys.exit(1)