summaryrefslogtreecommitdiff
path: root/test/simple-agent
blob: 01c82baf446e885d8de6cf9101f4eb75f13af752 (plain)
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
#!/usr/bin/python

import gobject

import dbus
import dbus.service
import dbus.mainloop.glib
import sys

class Canceled(dbus.DBusException):
	_dbus_error_name = "net.connman.Error.Canceled"

class LaunchBrowser(dbus.DBusException):
	_dbus_error_name = "net.connman.Agent.Error.LaunchBrowser"

class Agent(dbus.service.Object):
	name = None
	ssid = None
	identity = None
	passphrase = None
	wpspin = None
	username = None
	password = None

	@dbus.service.method("net.connman.Agent",
					in_signature='', out_signature='')
	def Release(self):
		print("Release")
		mainloop.quit()

	def input_passphrase(self):
		response = {}

		if not self.identity and not self.passphrase and not self.wpspin:
			print "Service credentials requested, type cancel to cancel"
			args = raw_input('Answer: ')

			for arg in args.split():
				if arg.startswith("cancel"):
					response["Error"] = arg
				if arg.startswith("Identity="):
					identity = arg.replace("Identity=", "", 1)
					response["Identity"] = identity
				if arg.startswith("Passphrase="):
					passphrase = arg.replace("Passphrase=", "", 1)
					response["Passphrase"] = passphrase
				if arg.startswith("WPS="):
					wpspin = arg.replace("WPS=", "", 1)
					response["WPS"] = wpspin
					break
		else:
			if self.identity:
				response["Identity"] = self.identity
			if self.passphrase:
				response["Passphrase"] = self.passphrase
			if self.wpspin:
				response["WPS"] = self.wpspin

		return response

	def input_username(self):
		response = {}

		if not self.username and not self.password:
			print "User login requested, type cancel to cancel"
			print "or browser to login through the browser by yourself."
			args = raw_input('Answer: ')

			for arg in args.split():
				if arg.startswith("cancel") or arg.startswith("browser"):
					response["Error"] = arg
				if arg.startswith("Username="):
					username = arg.replace("Username=", "", 1)
					response["Username"] = username
				if arg.startswith("Password="):
					password = arg.replace("Password=", "", 1)
					response["Password"] = password
		else:
			if self.username:
				response["Username"] = self.username
			if self.password:
				response["Password"] = self.password

		return response

	def input_hidden(self):
		response = {}

		if not self.name and not self.ssid:
			args = raw_input('Answer ')

			for arg in args.split():
				if arg.startswith("Name="):
					name = arg.replace("Name=", "", 1)
					response["Name"] = name
					break
				if arg.startswith("SSID="):
					ssid = arg.replace("SSID", "", 1)
					response["SSID"] = ssid
					break
		else:
			if self.name:
				response["Name"] = self.name
			if self.ssid:
				response["SSID"] = self.ssid

		return response

	@dbus.service.method("net.connman.Agent",
					in_signature='oa{sv}',
					out_signature='a{sv}')
	def RequestInput(self, path, fields):
		print "RequestInput (%s,%s)" % (path, fields)

		response = {}

		if fields.has_key("Name"):
			response.update(self.input_hidden())
		if fields.has_key("Passphrase"):
			response.update(self.input_passphrase())
		if fields.has_key("Username"):
			response.update(self.input_username())

		if response.has_key("Error"):
			if response["Error"] == "cancel":
				raise Canceled("canceled")
				return
			if response["Error"] == "browser":
				raise LaunchBrowser("launch browser")
				return

		print "returning (%s)" % (response)

		return response

	@dbus.service.method("net.connman.Agent",
					in_signature='os',
					out_signature='')
	def RequestBrowser(self, path, url):
		print "RequestBrowser (%s,%s)" % (path, url)

		print "Please login through the given url in a browser"
		print "Then press enter to accept or some text to cancel"

		args = raw_input('> ')

		if len(args) > 0:
			raise Canceled("canceled")

		return

	@dbus.service.method("net.connman.Agent",
					in_signature='os',
					out_signature='')
	def ReportError(self, path, error):
		print "ReportError %s, %s" % (path, error)
		retry = raw_input("Retry service (yes/no): ")
		if (retry == "yes"):
			class Retry(dbus.DBusException):
				_dbus_error_name = "net.connman.Agent.Error.Retry"

			raise Retry("retry service")
		else:
			return


	@dbus.service.method("net.connman.Agent",
					in_signature='', out_signature='')
	def Cancel(self):
		print "Cancel"

class VpnAgent(dbus.service.Object):
	name = None
	host = None
	cookie = None
	username = None
	password = None

	@dbus.service.method("net.connman.vpn.Agent",
					in_signature='', out_signature='')
	def Release(self):
		print("Release VPN agent")

	def input_cookie(self):
		response = {}

		if not self.cookie:
			print "VPN credentials requested, type cancel to cancel"
			args = raw_input('Answer: ')

			for arg in args.split():
				if arg.startswith("cancel"):
					response["Error"] = arg
				if arg.startswith("Cookie="):
					cookie = arg.replace("Cookie=", "", 1)
					response["OpenConnect.Cookie"] = cookie
		else:
			if self.cookie:
				response["OpenConnect.Cookie"] = self.cookie

		return response

	def input_username(self):
		response = {}

		if not self.username and not self.password:
			print "User login requested, type cancel to cancel"
			args = raw_input('Answer: ')

			for arg in args.split():
				if arg.startswith("cancel"):
					response["Error"] = arg
				if arg.startswith("Username="):
					username = arg.replace("Username=", "", 1)
					response["Username"] = username
				if arg.startswith("Password="):
					password = arg.replace("Password=", "", 1)
					response["Password"] = password
		else:
			if self.username:
				response["Username"] = self.username
			if self.password:
				response["Password"] = self.password

		return response

	@dbus.service.method("net.connman.vpn.Agent",
					in_signature='oa{sv}',
					out_signature='a{sv}')
	def RequestInput(self, path, fields):
		print "RequestInput (%s,%s)" % (path, fields)

		response = {}

		if fields.has_key("OpenConnect.Cookie"):
			response.update(self.input_cookie())
		if fields.has_key("Username") or fields.has_key("Password"):
			response.update(self.input_username())

		if response.has_key("Error"):
			if response["Error"] == "cancel":
				raise Canceled("canceled")
				return

		print "returning (%s)" % (response)

		return response

	@dbus.service.method("net.connman.vpn.Agent",
					in_signature='os',
					out_signature='')
	def ReportError(self, path, error):
		print "ReportError %s, %s" % (path, error)
		retry = raw_input("Retry service (yes/no): ")
		if (retry == "yes"):
			class Retry(dbus.DBusException):
				_dbus_error_name = "net.connman.vpn.Agent.Error.Retry"

			raise Retry("retry service")
		else:
			return


	@dbus.service.method("net.connman.vpn.Agent",
					in_signature='', out_signature='')
	def Cancel(self):
		print "Cancel"

def vpnNameOwnerChanged(proxy):
	if proxy:
		print("vpnd is connected to system bus")
		try:
			path = "/test/vpn_agent"
			vpn_manager = dbus.Interface(bus.get_object('net.connman.vpn',
								    "/"),
						     'net.connman.vpn.Manager')
			vpn_manager.RegisterAgent(path)
		except:
			print "vpn agent is not registered"
	else:
		print("vpnd is disconnected from system bus")
		vpn_manager = None

def print_usage():
	print "Usage:"
	print "For hidden service:"
	print "%s Name=<hidden service name> [SSID=<hidden ssid>]" % (sys.argv[0])
	print "For EAP/WPA input:"
	print "%s Identity=<identity> Passphrase=<passphrase> WPS=<wpspin>" % (sys.argv[0])
	print "For WISPr login, L2TP or PPTP input:"
	print "%s Username=<username> Password=<password>" % (sys.argv[0])
	print "For OpenConnect input:"
	print "%s Cookie=<string>" % (sys.argv[0])
	print "Help: %s help" % (sys.argv[0])
	sys.exit(1)

if __name__ == '__main__':
	if len(sys.argv) == 2 and sys.argv[1] == "help":
		print_usage()

	dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

	bus = dbus.SystemBus()
	manager = dbus.Interface(bus.get_object('net.connman', "/"),
					'net.connman.Manager')

	path = "/test/agent"
	object = Agent(bus, path)

	try:
		vpn_manager = dbus.Interface(bus.get_object('net.connman.vpn', "/"),
					    'net.connman.vpn.Manager')
		vpn_path = "/test/vpn_agent"
		vpn_object = VpnAgent(bus, vpn_path)
	except:
		vpn_manager = None
		print "net.connman.vpn is not present"

	if len(sys.argv) >= 2:
		for arg in sys.argv[1:]:
			if arg.startswith("Name="):
				object.name = arg.replace("Name=", "", 1)
			elif arg.startswith("SSID="):
				object.ssid = arg.replace("SSID=", "", 1)
			elif arg.startswith("Identity="):
				object.identity = arg.replace("Identity=", "", 1)
			elif arg.startswith("Passphrase="):
				object.passphrase = arg.replace("Passphrase=", "", 1)
			elif arg.startswith("WPS="):
				object.wpspin = arg.replace("WPS=", "", 1)
			elif arg.startswith("Username="):
				object.username = arg.replace("Username=", "", 1)
				vpn_object.username = arg.replace("Username=", "", 1)
			elif arg.startswith("Password="):
				object.password = arg.replace("Password=", "", 1)
				vpn_object.password = arg.replace("Password=", "", 1)
			elif arg.startswith("Cookie="):
				vpn_object.cookie = arg.replace("Cookie=", "", 1)
			else:
				print_usage()

	try:
		manager.RegisterAgent(path)
	except:
		print "Cannot register connman agent."

	if vpn_manager != None:
		try:
			vpn_manager.RegisterAgent(vpn_path)
			bus.watch_name_owner('net.connman.vpn', vpnNameOwnerChanged)
		except:
			"Cannot register vpn agent"

	mainloop = gobject.MainLoop()
	mainloop.run()

	#manager.UnregisterAgent(path)