7 import dbus.mainloop.glib
10 class Canceled(dbus.DBusException):
11 _dbus_error_name = "net.connman.Error.Canceled"
13 class LaunchBrowser(dbus.DBusException):
14 _dbus_error_name = "net.connman.Agent.Error.LaunchBrowser"
16 class Agent(dbus.service.Object):
25 @dbus.service.method("net.connman.Agent",
26 in_signature='', out_signature='')
31 def input_passphrase(self):
34 if not self.identity and not self.passphrase and not self.wpspin:
35 print "Service credentials requested, type cancel to cancel"
36 args = raw_input('Answer: ')
38 for arg in args.split():
39 if arg.startswith("cancel"):
40 response["Error"] = arg
41 if arg.startswith("Identity="):
42 identity = arg.replace("Identity=", "", 1)
43 response["Identity"] = identity
44 if arg.startswith("Passphrase="):
45 passphrase = arg.replace("Passphrase=", "", 1)
46 response["Passphrase"] = passphrase
47 if arg.startswith("WPS="):
48 wpspin = arg.replace("WPS=", "", 1)
49 response["WPS"] = wpspin
53 response["Identity"] = self.identity
55 response["Passphrase"] = self.passphrase
57 response["WPS"] = self.wpspin
61 def input_username(self):
64 if not self.username and not self.password:
65 print "User login requested, type cancel to cancel"
66 print "or browser to login through the browser by yourself."
67 args = raw_input('Answer: ')
69 for arg in args.split():
70 if arg.startswith("cancel") or arg.startswith("browser"):
71 response["Error"] = arg
72 if arg.startswith("Username="):
73 username = arg.replace("Username=", "", 1)
74 response["Username"] = username
75 if arg.startswith("Password="):
76 password = arg.replace("Password=", "", 1)
77 response["Password"] = password
80 response["Username"] = self.username
82 response["Password"] = self.password
86 def input_hidden(self):
89 if not self.name and not self.ssid:
90 args = raw_input('Answer ')
92 for arg in args.split():
93 if arg.startswith("Name="):
94 name = arg.replace("Name=", "", 1)
95 response["Name"] = name
97 if arg.startswith("SSID="):
98 ssid = arg.replace("SSID", "", 1)
99 response["SSID"] = ssid
103 response["Name"] = self.name
105 response["SSID"] = self.ssid
109 @dbus.service.method("net.connman.Agent",
110 in_signature='oa{sv}',
111 out_signature='a{sv}')
112 def RequestInput(self, path, fields):
113 print "RequestInput (%s,%s)" % (path, fields)
117 if fields.has_key("Name"):
118 response.update(self.input_hidden())
119 if fields.has_key("Passphrase"):
120 response.update(self.input_passphrase())
121 if fields.has_key("Username"):
122 response.update(self.input_username())
124 if response.has_key("Error"):
125 if response["Error"] == "cancel":
126 raise Canceled("canceled")
128 if response["Error"] == "browser":
129 raise LaunchBrowser("launch browser")
132 print "returning (%s)" % (response)
136 @dbus.service.method("net.connman.Agent",
139 def RequestBrowser(self, path, url):
140 print "RequestBrowser (%s,%s)" % (path, url)
142 print "Please login through the given url in a browser"
143 print "Then press enter to accept or some text to cancel"
145 args = raw_input('> ')
148 raise Canceled("canceled")
152 @dbus.service.method("net.connman.Agent",
155 def ReportError(self, path, error):
156 print "ReportError %s, %s" % (path, error)
157 retry = raw_input("Retry service (yes/no): ")
159 class Retry(dbus.DBusException):
160 _dbus_error_name = "net.connman.Agent.Error.Retry"
162 raise Retry("retry service")
167 @dbus.service.method("net.connman.Agent",
168 in_signature='', out_signature='')
172 class VpnAgent(dbus.service.Object):
179 @dbus.service.method("net.connman.vpn.Agent",
180 in_signature='', out_signature='')
182 print("Release VPN agent")
184 def input_cookie(self):
188 print "VPN credentials requested, type cancel to cancel"
189 args = raw_input('Answer: ')
191 for arg in args.split():
192 if arg.startswith("cancel"):
193 response["Error"] = arg
194 if arg.startswith("Cookie="):
195 cookie = arg.replace("Cookie=", "", 1)
196 response["OpenConnect.Cookie"] = cookie
199 response["OpenConnect.Cookie"] = self.cookie
203 def input_username(self):
206 if not self.username and not self.password:
207 print "User login requested, type cancel to cancel"
208 args = raw_input('Answer: ')
210 for arg in args.split():
211 if arg.startswith("cancel"):
212 response["Error"] = arg
213 if arg.startswith("Username="):
214 username = arg.replace("Username=", "", 1)
215 response["Username"] = username
216 if arg.startswith("Password="):
217 password = arg.replace("Password=", "", 1)
218 response["Password"] = password
221 response["Username"] = self.username
223 response["Password"] = self.password
227 @dbus.service.method("net.connman.vpn.Agent",
228 in_signature='oa{sv}',
229 out_signature='a{sv}')
230 def RequestInput(self, path, fields):
231 print "RequestInput (%s,%s)" % (path, fields)
235 if fields.has_key("OpenConnect.Cookie"):
236 response.update(self.input_cookie())
237 if fields.has_key("Username") or fields.has_key("Password"):
238 response.update(self.input_username())
240 if response.has_key("Error"):
241 if response["Error"] == "cancel":
242 raise Canceled("canceled")
245 print "returning (%s)" % (response)
249 @dbus.service.method("net.connman.vpn.Agent",
252 def ReportError(self, path, error):
253 print "ReportError %s, %s" % (path, error)
254 retry = raw_input("Retry service (yes/no): ")
256 class Retry(dbus.DBusException):
257 _dbus_error_name = "net.connman.vpn.Agent.Error.Retry"
259 raise Retry("retry service")
264 @dbus.service.method("net.connman.vpn.Agent",
265 in_signature='', out_signature='')
269 def vpnNameOwnerChanged(proxy):
271 print("vpnd is connected to system bus")
273 path = "/test/vpn_agent"
274 vpn_manager = dbus.Interface(bus.get_object('net.connman.vpn',
276 'net.connman.vpn.Manager')
277 vpn_manager.RegisterAgent(path)
279 print "vpn agent is not registered"
281 print("vpnd is disconnected from system bus")
286 print "For hidden service:"
287 print "%s Name=<hidden service name> [SSID=<hidden ssid>]" % (sys.argv[0])
288 print "For EAP/WPA input:"
289 print "%s Identity=<identity> Passphrase=<passphrase> WPS=<wpspin>" % (sys.argv[0])
290 print "For WISPr login, L2TP or PPTP input:"
291 print "%s Username=<username> Password=<password>" % (sys.argv[0])
292 print "For OpenConnect input:"
293 print "%s Cookie=<string>" % (sys.argv[0])
294 print "Help: %s help" % (sys.argv[0])
297 if __name__ == '__main__':
298 if len(sys.argv) == 2 and sys.argv[1] == "help":
301 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
303 bus = dbus.SystemBus()
304 manager = dbus.Interface(bus.get_object('net.connman', "/"),
305 'net.connman.Manager')
308 object = Agent(bus, path)
311 vpn_manager = dbus.Interface(bus.get_object('net.connman.vpn', "/"),
312 'net.connman.vpn.Manager')
313 vpn_path = "/test/vpn_agent"
314 vpn_object = VpnAgent(bus, vpn_path)
317 print "net.connman.vpn is not present"
319 if len(sys.argv) >= 2:
320 for arg in sys.argv[1:]:
321 if arg.startswith("Name="):
322 object.name = arg.replace("Name=", "", 1)
323 elif arg.startswith("SSID="):
324 object.ssid = arg.replace("SSID=", "", 1)
325 elif arg.startswith("Identity="):
326 object.identity = arg.replace("Identity=", "", 1)
327 elif arg.startswith("Passphrase="):
328 object.passphrase = arg.replace("Passphrase=", "", 1)
329 elif arg.startswith("WPS="):
330 object.wpspin = arg.replace("WPS=", "", 1)
331 elif arg.startswith("Username="):
332 object.username = arg.replace("Username=", "", 1)
333 vpn_object.username = arg.replace("Username=", "", 1)
334 elif arg.startswith("Password="):
335 object.password = arg.replace("Password=", "", 1)
336 vpn_object.password = arg.replace("Password=", "", 1)
337 elif arg.startswith("Cookie="):
338 vpn_object.cookie = arg.replace("Cookie=", "", 1)
343 manager.RegisterAgent(path)
345 print "Cannot register connman agent."
347 if vpn_manager != None:
349 vpn_manager.RegisterAgent(vpn_path)
350 bus.watch_name_owner('net.connman.vpn', vpnNameOwnerChanged)
352 "Cannot register vpn agent"
354 mainloop = gobject.MainLoop()
357 #manager.UnregisterAgent(path)