This file is indexed.

/usr/lib/python2.7/dist-packages/blueman/plugins/applet/NMPANSupport.py is in blueman 1.23-git201403102151-1ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
# Copyright (C) 2009 Valmantas Paliksa <walmis at balticum-tv dot lt>
#
# Licensed under the GNU General Public License Version 3
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty 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, see <http://www.gnu.org/licenses/>.

import gobject
import dbus
from blueman.Sdp import *
from blueman.plugins.AppletPlugin import AppletPlugin
from uuid import uuid1
import gconf
import os
from blueman.main.SignalTracker import SignalTracker
from blueman.main.Device import Device

#class NMApplet

class NMDeviceState:
	UNKNOWN = 0

	#/* Initial state of all devices and the only state for devices not
	# * managed by NetworkManager.
	# *
	# * Allowed next states:
	# *   UNAVAILABLE:  the device is now managed by NetworkManager
	# */
	UNMANAGED = 1

	#/* Indicates the device is not yet ready for use, but is managed by
	# * NetworkManager.  For Ethernet devices, the device may not have an
	# * active carrier.  For WiFi devices, the device may not have it's radio
	# * enabled.
	# *
	# * Allowed next states:
	# *   UNMANAGED:  the device is no longer managed by NetworkManager
	# *   DISCONNECTED:  the device is now ready for use
	# */
	UNAVAILABLE = 2

	#/* Indicates the device does not have an activate connection to anything.
	# *
	# * Allowed next states:
	# *   UNMANAGED:  the device is no longer managed by NetworkManager
	# *   UNAVAILABLE:  the device is no longer ready for use (rfkill, no carrier, etc)
	# *   PREPARE:  the device has started activation
	# */
	DISCONNECTED = 3

	#/* Indicate states in device activation.
	# *
	# * Allowed next states:
	# *   UNMANAGED:  the device is no longer managed by NetworkManager
	# *   UNAVAILABLE:  the device is no longer ready for use (rfkill, no carrier, etc)
	# *   FAILED:  an error ocurred during activation
	# *   NEED_AUTH:  authentication/secrets are needed
	# *   ACTIVATED:  (IP_CONFIG only) activation was successful
	# *   DISCONNECTED:  the device's connection is no longer valid, or NetworkManager went to sleep
	# */
	PREPARE = 4
	CONFIG = 5
	NEED_AUTH = 6
	IP_CONFIG = 7

	#/* Indicates the device is part of an active network connection.
	# *
	# * Allowed next states:
	# *   UNMANAGED:  the device is no longer managed by NetworkManager
	# *   UNAVAILABLE:  the device is no longer ready for use (rfkill, no carrier, etc)
	# *   FAILED:  a DHCP lease was not renewed, or another error
	# *   DISCONNECTED:  the device's connection is no longer valid, or NetworkManager went to sleep
	# */
	ACTIVATED = 8

	#/* Indicates the device's activation failed.
	# *
	# * Allowed next states:
	# *   UNMANAGED:  the device is no longer managed by NetworkManager
	# *   UNAVAILABLE:  the device is no longer ready for use (rfkill, no carrier, etc)
	# *   DISCONNECTED:  the device's connection is ready for activation, or NetworkManager went to sleep
	# */
	FAILED = 9

class NewConnectionBuilder:
	def __init__(self, parent, params, ok_cb, err_cb):
		self.parent = parent
		self.params = params
		self.ok_cb = ok_cb
		self.err_cb = err_cb
		
		self.signals = SignalTracker()
		
		self.device = None
		self.connection = None
		
		self.signals.Handle("dbus", parent.bus, self.on_nm_device_added, "DeviceAdded", "org.freedesktop.NetworkManager")
		self.signals.Handle("dbus", parent.bus, self.on_nma_new_connection, "NewConnection", "org.freedesktop.NetworkManagerSettings")
		
		self.device = self.parent.find_device(params["bluetooth"]["bdaddr"])

		self.connection = self.parent.find_connection(params["bluetooth"]["bdaddr"], "panu")
		if not self.connection:
			parent.add_connection(params)
			gobject.timeout_add(1000, self.signal_wait_timeout)
		else:
			self.init_connection()
			
	def cleanup(self):
		self.signals.DisconnectAll()
			
	def signal_wait_timeout(self):
		if not self.device or not self.connection:
			self.err_cb(dbus.DBusException("Network Manager did not support the connection"))
			if self.connection:
				self.remove_connection()		
			self.cleanup()
		
	def on_nm_device_added(self, path):
		dprint(path)
		self.device = path
		if self.device and self.connection:
			self.init_connection()
	
	def on_nma_new_connection(self, path):
		dprint(path)
		self.connection = path
		if self.device and self.connection:
			self.init_connection()
			
	def init_connection(self):
		self.cleanup()
		dprint("activating", self.connection, self.device)
		if not self.device or not self.connection:
			self.err_cb(dbus.DBusException("Network Manager did not support the connection"))
			if self.connection:
				self.remove_connection()
			self.cleanup()
		else:
			self.signals.Handle("dbus", self.parent.bus, self.on_device_state, "StateChanged", "org.freedesktop.NetworkManager.Device", path=self.device)
		
			self.parent.nm.ActivateConnection("org.freedesktop.NetworkManagerUserSettings", self.connection, self.device, self.connection)
		
	def remove_connection(self):
		self.parent.remove_connection(self.connection)
		
	def on_device_state(self, state, oldstate, reason):
		dprint("state=",state, "oldstate=", oldstate, "reason=", reason)
		if state <= NMDeviceState.DISCONNECTED and NMDeviceState.DISCONNECTED < oldstate <= NMDeviceState.ACTIVATED:
			if self.err_cb:
				self.err_cb(dbus.DBusException("Connection was interrupted"))
			
			self.remove_connection()
			self.cleanup()
		
		elif state == NMDeviceState.FAILED:
			self.err_cb(dbus.DBusException("Network Manager Failed to activate the connection"))
			self.remove_connection()
			self.cleanup()		
		
		elif state == NMDeviceState.ACTIVATED:
			self.ok_cb()
			self.err_cb = None
			self.ok_cb = None
			
	
class NMPANSupport(AppletPlugin):
	__depends__ = ["DBusService"]
	__conflicts__ = ["DhcpClient", "NMIntegration"]
	__icon__ = "network"
	__author__ = "Walmis"
	__description__ = _("Provides support for Personal Area Networking (PAN) introduced in NetworkManager 0.8")
	__priority__ = 2
	
	def on_load(self, applet):
		self.bus = dbus.SystemBus()
		self.nma = None
		self.nm = None
		self.nm_signals = SignalTracker()
		self.nma_signals = SignalTracker()
		
		self.watch1 = self.bus.watch_name_owner("org.freedesktop.NetworkManagerUserSettings", self.on_nma_owner_changed)
		self.watch2 = self.bus.watch_name_owner("org.freedesktop.NetworkManager", self.on_nm_owner_changed)
		
		self.client = gconf.client_get_default ()
		
	def set_gconf(self, key, value):
		func = None
	
		if type(value) == str or type(value) == unicode:
			func = self.client.set_string
		elif type(value) == int:
			func = self.client.set_int
		elif type(value) == bool:
			func = self.client.set_bool
		elif type(value) == float:
			func = self.client.set_float
		elif type(value) == list:
			def x(key, val):
				self.client.set_list(key, gconf.VALUE_STRING, val)
			func = x
			
		elif type(value) == dbus.Array:
			if value.signature == "i":
				def x(key, val):
					self.client.set_list(key, gconf.VALUE_INT, val)
				func = x
			elif value.signature == "s":
				def x(key, val):
					self.client.set_list(key, gconf.VALUE_STRING, val)
				func = x
			else:
				raise AttributeError("Cant set this type in gconf")							
			
		else:
			raise AttributeError("Cant set %s in gconf" % type(value))
			
		func(key, value)
	
	def find_free_gconf_slot(self):
		dirs = list(self.client.all_dirs ("/system/networking/connections"))
		dirs.sort()
		
		i = 1
		for d in dirs:
			try:
				d = int(os.path.basename(d))
			except:
				continue
			if d != i:
				return i
			
			i+=1
			
		return i
		
	def add_connection(self, params):
		slot = self.find_free_gconf_slot()
		
		base_path = "/system/networking/connections/%d" % slot
		
		for group, settings in params.iteritems():
			path = base_path + "/%s" % group
			for k, v in settings.iteritems():
				key = path + "/%s" % k
				self.set_gconf(key, v)
				
	def remove_connection(self, path):
		self.bus.call_blocking("org.freedesktop.NetworkManagerUserSettings", path, "org.freedesktop.NetworkManagerSettings.Connection", "Delete", "", [])	
				
	def format_bdaddr(self, addr):
		return "%02X:%02X:%02X:%02X:%02X:%02X" % (addr[0], addr[1], addr[2], addr[3], addr[4], addr[5])

	
	def find_device(self, bdaddr):
		devices = self.nm.GetDevices()
		for dev in devices:
			try:
				d = self.bus.call_blocking("org.freedesktop.NetworkManager", dev, "org.freedesktop.DBus.Properties", "GetAll", "s", ["org.freedesktop.NetworkManager.Device.Bluetooth"])	
				if d["HwAddress"] == bdaddr:
					dprint(d["HwAddress"])
					return dev
						
			except dbus.DBusException:
				pass	
							
	def find_connection(self, address, t):
		conns = self.nma.ListConnections()
		for conn in conns:
			c = self.bus.call_blocking("org.freedesktop.NetworkManagerUserSettings", conn, "org.freedesktop.NetworkManagerSettings.Connection", "GetSettings", "", [])
			try:
				if (self.format_bdaddr(c["bluetooth"]["bdaddr"]) == address) and c["bluetooth"]["type"] == t:
					return conn
			except:
				pass
				
	def find_active_connection(self, address, type):
		props = self.bus.call_blocking("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager", "org.freedesktop.DBus.Properties", "GetAll", "s", ["org.freedesktop.NetworkManager"])			
		
		nma_connection = self.find_connection(address, type)
		if nma_connection:
			active_conns = props["ActiveConnections"]
			for conn in active_conns:
				conn_props = self.bus.call_blocking("org.freedesktop.NetworkManager", 
								    conn, 
								    "org.freedesktop.DBus.Properties", 
								    "GetAll", 
								    "s", 
								    ["org.freedesktop.NetworkManager.Connection.Active"])	
								    
				if conn_props["Connection"] == nma_connection:
					return conn	
				
	def on_nma_owner_changed(self, owner):
		if owner == "":
			self.nma = None
		else:
			service = self.bus.get_object("org.freedesktop.NetworkManagerUserSettings", "/org/freedesktop/NetworkManagerSettings")
			self.nma = dbus.proxies.Interface(service, "org.freedesktop.NetworkManagerSettings")		
			
	def on_nm_owner_changed(self, owner):
		if owner == "":
			self.nm = None
			self.nm_signals.DisconnectAll()
		else:		
			service = self.bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager")
			self.nm = dbus.proxies.Interface(service, "org.freedesktop.NetworkManager")
			
		
	def on_unload(self):
		self.nm_signals.DisconnectAll()
		self.nma_signals.DisconnectAll()
		
		self.watch1.cancel()
		self.watch2.cancel()
		
	def service_connect_handler(self, interface, object_path, method, args, ok, err):
		if interface == "org.bluez.Network" and method == "Connect":
			uuid = args[0]
			name = uuid16_to_name(uuid128_to_uuid16(uuid))
			d = Device(object_path)
			
			conn = self.find_active_connection(d.Address, "panu")
			if conn:
				err(dbus.DBusException(_("Already connected")))
			else:
				params = {}
				params["bluetooth"] = {"name": "bluetooth", "bdaddr":  str(d.Address), "type" : "panu"}
				params["connection"] = {"autoconnect": False, "id": str("%s on %s") % (name, d.Alias), "uuid" : str(uuid1()), "type": "bluetooth"}
				params['ipv4'] = {'addresses': dbus.Array([], dbus.Signature("i")), 'dns': dbus.Array([], dbus.Signature("i")), "method": "auto", "routes": dbus.Array([], dbus.Signature("i"))}		
			
				NewConnectionBuilder(self, params, ok, err)
				
			return True
			
		elif interface == "org.bluez.Network" and method == "Disconnect":
			d = Device(object_path)
			active_conn_path = self.find_active_connection(d.Address, "panu")
			if active_conn_path:
				self.bus.call_blocking("org.freedesktop.NetworkManager", 
						       "/org/freedesktop/NetworkManager", 
						       "org.freedesktop.NetworkManager", 
						       "DeactivateConnection", 
						       "o", 
						       [active_conn_path])
				ok()		       	
				return True