| 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 | Changelog |
|---|
| 26 | 20110129 - First usable version |
|---|
| 27 | |
|---|
| 28 | """ |
|---|
| 29 | |
|---|
| 30 | import os |
|---|
| 31 | import sys |
|---|
| 32 | import pprint |
|---|
| 33 | |
|---|
| 34 | import gobject |
|---|
| 35 | gobject.threads_init() |
|---|
| 36 | |
|---|
| 37 | from subprocess import Popen, PIPE, STDOUT |
|---|
| 38 | |
|---|
| 39 | import dbus |
|---|
| 40 | import dbus.service |
|---|
| 41 | if getattr(dbus, 'version', (0, 0, 0)) >= (0, 41, 0): |
|---|
| 42 | import dbus.glib |
|---|
| 43 | |
|---|
| 44 | PID_FILE="/var/run/multiseat-udisks.pid" |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | class MultiSeatDeviceManager: |
|---|
| 59 | def __init__(self): |
|---|
| 60 | self.pid=os.getpid() |
|---|
| 61 | self.mainloop = gobject.MainLoop() |
|---|
| 62 | self.all_devices=[] |
|---|
| 63 | self.bus = dbus.SystemBus() |
|---|
| 64 | self.proxy = self.bus.get_object("org.freedesktop.UDisks", |
|---|
| 65 | "/org/freedesktop/UDisks") |
|---|
| 66 | self.iface = dbus.Interface(self.proxy, 'org.freedesktop.UDisks') |
|---|
| 67 | self.iface.connect_to_signal('DeviceAdded', self.device_added_callback) |
|---|
| 68 | self.iface.connect_to_signal('DeviceRemoved', self.device_removed_callback) |
|---|
| 69 | |
|---|
| 70 | def device_added_callback(self, dev): |
|---|
| 71 | self.init_load() |
|---|
| 72 | |
|---|
| 73 | def device_removed_callback(self, dev): |
|---|
| 74 | |
|---|
| 75 | for i in range(len(self.all_devices)): |
|---|
| 76 | if not os.path.exists(self.all_devices[i]['device']): |
|---|
| 77 | print "device_removed_callback() NO EXISTS dev=%s"%self.all_devices[i] |
|---|
| 78 | if self.all_devices[i]['ismounted']: |
|---|
| 79 | self.all_devices[i]['ismounted']=False |
|---|
| 80 | self.remove_device(self.all_devices[i]) |
|---|
| 81 | self.umount_same_disk(self.all_devices[i]['device']) |
|---|
| 82 | self.init_load() |
|---|
| 83 | |
|---|
| 84 | def umount_same_disk(self, devname): |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | if len(devname) < 9: |
|---|
| 88 | print "umount_same_disk() devname seems to be disk %s, no umounting..."%devname |
|---|
| 89 | |
|---|
| 90 | return |
|---|
| 91 | dev_disk=devname[0:8] |
|---|
| 92 | for dev in self.all_devices: |
|---|
| 93 | if dev_disk in dev['device']: |
|---|
| 94 | print "umount_same_disk() dev_disk=%s dev=%s"%(dev_disk, dev) |
|---|
| 95 | self.remove_device(dev) |
|---|
| 96 | |
|---|
| 97 | def remove_device(self, dev): |
|---|
| 98 | if self.UmountDevice(dev): |
|---|
| 99 | |
|---|
| 100 | if os.path.isfile(dev['desktopfile']): |
|---|
| 101 | os.unlink(dev['desktopfile']) |
|---|
| 102 | print "remove_device() deleted %s"%dev['desktopfile'] |
|---|
| 103 | |
|---|
| 104 | def get(self, obj, prop): |
|---|
| 105 | return obj.Get("org.freedesktop.DBus.Properties", prop) |
|---|
| 106 | |
|---|
| 107 | def init_load(self): |
|---|
| 108 | self.all_devices=[] |
|---|
| 109 | for sto in self.iface.EnumerateDevices(): |
|---|
| 110 | sto_obj = self.bus.get_object ('org.freedesktop.UDisks', sto) |
|---|
| 111 | storage = dbus.Interface (sto_obj, 'org.freedesktop.DBus.Properties') |
|---|
| 112 | try: |
|---|
| 113 | storage.Get("org.freedesktop.DBus.Properties", 'DeviceIsRemovable') |
|---|
| 114 | except Exception, err: |
|---|
| 115 | print "Exception, perhaps removing device... err=%s"%err |
|---|
| 116 | continue |
|---|
| 117 | if bool(self.get(storage, 'DeviceIsPartition')) and \ |
|---|
| 118 | str(self.get(storage, 'DriveConnectionInterface')) == 'usb': |
|---|
| 119 | fstype=str(self.get(storage, 'IdType')) |
|---|
| 120 | if fstype not in ['vfat', 'iso9660', 'ext2', 'ext3', 'ext4', 'ntfs']: |
|---|
| 121 | continue |
|---|
| 122 | path=str(self.get(storage, 'NativePath')) |
|---|
| 123 | seat_id=self.getSeatID(path) |
|---|
| 124 | serial=str(self.get(storage, 'DriveSerial')) |
|---|
| 125 | partnumber=str(self.get(storage, 'PartitionNumber')) |
|---|
| 126 | label=str(self.get(storage, 'IdLabel')) |
|---|
| 127 | if label == '': |
|---|
| 128 | label=serial |
|---|
| 129 | username=self.getUserfromSeat(seat_id) |
|---|
| 130 | if username != '': |
|---|
| 131 | desktopfile=self.getUserDesktop(username) + "/%s-%s.desktop"%(serial,partnumber) |
|---|
| 132 | useruid=int(self.getUserUID(username)) |
|---|
| 133 | else: |
|---|
| 134 | desktopfile='/dev/null' |
|---|
| 135 | useruid=0 |
|---|
| 136 | dev={ |
|---|
| 137 | "device": str(self.get(storage, 'DeviceFile')), |
|---|
| 138 | "model": str(self.get(storage, 'DriveModel')), |
|---|
| 139 | "vendor": str(self.get(storage, 'DriveVendor')), |
|---|
| 140 | "serial": serial, |
|---|
| 141 | "label": label, |
|---|
| 142 | "fstype": fstype, |
|---|
| 143 | "ismounted": bool(self.get(storage, 'DeviceIsMounted')), |
|---|
| 144 | "partnumber":partnumber, |
|---|
| 145 | "mountpoint": "/media/%s-%s"%(serial,partnumber), |
|---|
| 146 | "desktopfile": desktopfile, |
|---|
| 147 | "path":path, |
|---|
| 148 | "seat_id":seat_id, |
|---|
| 149 | "username":username, |
|---|
| 150 | "useruid":useruid, |
|---|
| 151 | } |
|---|
| 152 | print "init_load() device=%s"%dev |
|---|
| 153 | if not dev['ismounted'] and username != '' and self.MountDevice(dev): |
|---|
| 154 | dev['ismounted']=True |
|---|
| 155 | try: |
|---|
| 156 | self.CreateLauncher(dev) |
|---|
| 157 | except Exception, err: |
|---|
| 158 | print "init_load() Exception creating launcher, err=%s"%err |
|---|
| 159 | self.all_devices.append(dev) |
|---|
| 160 | pprint.pprint(self.all_devices) |
|---|
| 161 | |
|---|
| 162 | def getSeatID(self, path): |
|---|
| 163 | seat_id=0 |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | parent_folder=8 |
|---|
| 174 | if len(os.path.basename(path)) == 3: |
|---|
| 175 | |
|---|
| 176 | parent_folder=7 |
|---|
| 177 | buspath="/".join(path.split('/')[0:len(path.split('/'))-parent_folder]) |
|---|
| 178 | print "getSeatID() buspath=%s"%buspath |
|---|
| 179 | if not os.path.isfile(buspath+'/busnum'): |
|---|
| 180 | print "getSeatID() no busnum file in path=%s, return seat_id=0"%path |
|---|
| 181 | return seat_id |
|---|
| 182 | busnum=int(open(buspath+'/busnum', 'r').readline()) |
|---|
| 183 | devnum=int(open(buspath+'/devnum', 'r').readline()) |
|---|
| 184 | busnum="%03i"%busnum |
|---|
| 185 | devnum="%03i"%devnum |
|---|
| 186 | print "getSeatID() busnum=%s devnum=%s"%(busnum, devnum) |
|---|
| 187 | |
|---|
| 188 | if not os.path.isfile('/dev/seat.db'): |
|---|
| 189 | return seat_id |
|---|
| 190 | f=open('/dev/seat.db', 'r') |
|---|
| 191 | for line in f.readlines(): |
|---|
| 192 | if "%s %s "%(busnum, devnum) in line: |
|---|
| 193 | print "getSeatID() found SEAT_ID in line=%s"%line.strip() |
|---|
| 194 | |
|---|
| 195 | seat_id=line.split()[2] |
|---|
| 196 | f.close() |
|---|
| 197 | return seat_id |
|---|
| 198 | |
|---|
| 199 | def getUserfromSeat(self, seat_id): |
|---|
| 200 | username='' |
|---|
| 201 | cmd="pidof gdm && gdmdynamic -l" |
|---|
| 202 | p=Popen(cmd, shell=True, bufsize=0, stdout=PIPE, stderr=STDOUT, close_fds=True) |
|---|
| 203 | for line in p.stdout.readlines(): |
|---|
| 204 | print "getUserfromSeat() line=%s"%line.strip() |
|---|
| 205 | if ":" in line: |
|---|
| 206 | |
|---|
| 207 | seats=line.strip().split(';') |
|---|
| 208 | for seat in seats: |
|---|
| 209 | if ":%s"%seat_id in seat: |
|---|
| 210 | username=seat.split(',')[1] |
|---|
| 211 | return username |
|---|
| 212 | |
|---|
| 213 | def MountDevice(self, d): |
|---|
| 214 | if d['username'] == '': |
|---|
| 215 | |
|---|
| 216 | return False |
|---|
| 217 | if not self.CreateMountPoint(d): |
|---|
| 218 | |
|---|
| 219 | return False |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | if d['fstype'] == 'iso9660': |
|---|
| 227 | options="-o rw,nosuid,nodev,uhelper=multiseat-udisks,uid=%s,gid=0,iocharset=utf8,mode=0400,dmode=0500"%d['username'] |
|---|
| 228 | elif d['fstype'] == 'vfat': |
|---|
| 229 | options="-o rw,nosuid,nodev,uhelper=multiseat-udisks,uid=%s,gid=0,shortname=mixed,dmask=0077,utf8=1,showexec,flush"%d['username'] |
|---|
| 230 | elif d['fstype'] in ['ext2', 'ext3', 'ext4']: |
|---|
| 231 | options="-o rw,nosuid,nodev,uhelper=multiseat-udisks" |
|---|
| 232 | |
|---|
| 233 | elif d['fstype'] == 'ntfs': |
|---|
| 234 | |
|---|
| 235 | options="-o rw,nosuid,nodev,uhelper=multiseat-udisks,uid=%s,gid=0,dmask=0077,default_permissions"%d['username'] |
|---|
| 236 | else: |
|---|
| 237 | options="-o rw,nosuid,nodev,uhelper=multiseat-udisks" |
|---|
| 238 | |
|---|
| 239 | cmd="mount -t %s %s '%s' %s"%(d['fstype'], d['device'], d['mountpoint'], options) |
|---|
| 240 | print "MountDevice() cmd=%s"%cmd |
|---|
| 241 | p=Popen(cmd, shell=True, bufsize=0, stdout=PIPE, stderr=STDOUT, close_fds=True) |
|---|
| 242 | for line in p.stdout.readlines(): |
|---|
| 243 | print line |
|---|
| 244 | p.communicate() |
|---|
| 245 | if p.returncode == 0: |
|---|
| 246 | os.chown(d['mountpoint'], d['useruid'], 0) |
|---|
| 247 | os.chmod(d['mountpoint'], 0700) |
|---|
| 248 | return True |
|---|
| 249 | return False |
|---|
| 250 | |
|---|
| 251 | def UmountDevice(self, d): |
|---|
| 252 | cmd="umount %s"%(d['device']) |
|---|
| 253 | print "UmountDevice() cmd=%s"%cmd |
|---|
| 254 | p=Popen(cmd, shell=True, bufsize=0, stdout=PIPE, stderr=STDOUT, close_fds=True) |
|---|
| 255 | for line in p.stdout.readlines(): |
|---|
| 256 | print line |
|---|
| 257 | p.communicate() |
|---|
| 258 | if p.returncode == 0: |
|---|
| 259 | self.RemoveMountPoint(d) |
|---|
| 260 | return True |
|---|
| 261 | return False |
|---|
| 262 | |
|---|
| 263 | def CreateMountPoint(self, d): |
|---|
| 264 | if os.path.isdir(d['mountpoint']): |
|---|
| 265 | return True |
|---|
| 266 | try: |
|---|
| 267 | os.mkdir(d['mountpoint']) |
|---|
| 268 | os.chmod(d['mountpoint'], 0700) |
|---|
| 269 | return True |
|---|
| 270 | except Exception, err: |
|---|
| 271 | print "Exception creating mountpoint %s, err=%s"%(d['mountpoint'], err) |
|---|
| 272 | return False |
|---|
| 273 | |
|---|
| 274 | def RemoveMountPoint(self, d): |
|---|
| 275 | try: |
|---|
| 276 | os.rmdir(d['mountpoint']) |
|---|
| 277 | return True |
|---|
| 278 | except Exception, err: |
|---|
| 279 | print "Exception removing mountpoint %s, err=%s"%(d['mountpoint'], err) |
|---|
| 280 | return False |
|---|
| 281 | |
|---|
| 282 | def CreateLauncher(self, d): |
|---|
| 283 | txt="""#!/usr/bin/env xdg-open |
|---|
| 284 | |
|---|
| 285 | [Desktop Entry] |
|---|
| 286 | Version=1.0 |
|---|
| 287 | Type=Link |
|---|
| 288 | Name=%s |
|---|
| 289 | Icon=drive-removable-media |
|---|
| 290 | Dev=%s |
|---|
| 291 | FSType=%s |
|---|
| 292 | MountPoint=%s |
|---|
| 293 | URL=%s |
|---|
| 294 | X-multiseat-desktop=%s |
|---|
| 295 | """%(d['label'], d['device'], d['fstype'], d['mountpoint'], d['mountpoint'], d['seat_id']) |
|---|
| 296 | |
|---|
| 297 | print txt |
|---|
| 298 | f=open(d['desktopfile'], 'w') |
|---|
| 299 | f.write(txt) |
|---|
| 300 | f.close() |
|---|
| 301 | os.chown(d['desktopfile'], d['useruid'], 0) |
|---|
| 302 | os.chmod(d['desktopfile'], 0700) |
|---|
| 303 | |
|---|
| 304 | def getUserDesktop(self, username): |
|---|
| 305 | home="" |
|---|
| 306 | cmd="getent passwd %s"%username |
|---|
| 307 | p=Popen(cmd, shell=True, bufsize=0, stdout=PIPE, stderr=STDOUT, close_fds=True) |
|---|
| 308 | for line in p.stdout.readlines(): |
|---|
| 309 | if "%s:"%username in line: |
|---|
| 310 | home=line.split(':')[5] |
|---|
| 311 | if os.path.isdir(home + "/Escritorio"): |
|---|
| 312 | return home + "/Escritorio" |
|---|
| 313 | elif os.path.isdir(home + "/Desktop"): |
|---|
| 314 | return home + "/Desktop" |
|---|
| 315 | |
|---|
| 316 | return home |
|---|
| 317 | |
|---|
| 318 | def getUserUID(self, username): |
|---|
| 319 | uid=0 |
|---|
| 320 | cmd="getent passwd %s"%username |
|---|
| 321 | p=Popen(cmd, shell=True, bufsize=0, stdout=PIPE, stderr=STDOUT, close_fds=True) |
|---|
| 322 | for line in p.stdout.readlines(): |
|---|
| 323 | if "%s:"%username in line: |
|---|
| 324 | uid=line.split(':')[2] |
|---|
| 325 | return uid |
|---|
| 326 | |
|---|
| 327 | def run (self): |
|---|
| 328 | file(PID_FILE, 'w').write('%d'%self.pid) |
|---|
| 329 | try: |
|---|
| 330 | self.mainloop.run() |
|---|
| 331 | except KeyboardInterrupt: |
|---|
| 332 | self.quit() |
|---|
| 333 | |
|---|
| 334 | def quit(self, *args): |
|---|
| 335 | self.mainloop.quit() |
|---|
| 336 | sys.exit(0) |
|---|
| 337 | |
|---|
| 338 | def umount(dev, uid): |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | if uid == '': |
|---|
| 343 | return "no-uid" |
|---|
| 344 | found=False |
|---|
| 345 | mountline='' |
|---|
| 346 | f=open('/proc/mounts', 'r') |
|---|
| 347 | for line in f.readlines(): |
|---|
| 348 | if line.startswith(dev): |
|---|
| 349 | mountline=line |
|---|
| 350 | found=True |
|---|
| 351 | f.close() |
|---|
| 352 | if not found: |
|---|
| 353 | return "no-mounted" |
|---|
| 354 | |
|---|
| 355 | username='' |
|---|
| 356 | home='' |
|---|
| 357 | cmd="getent passwd %s"%uid |
|---|
| 358 | p=Popen(cmd, shell=True, bufsize=0, stdout=PIPE, stderr=STDOUT, close_fds=True) |
|---|
| 359 | for line in p.stdout.readlines(): |
|---|
| 360 | if ":%s:"%uid in line: |
|---|
| 361 | username=line.split(':')[0] |
|---|
| 362 | home=line.split(':')[5] |
|---|
| 363 | if username == '' or home == '': |
|---|
| 364 | return "invalid-user" |
|---|
| 365 | |
|---|
| 366 | |
|---|
| 367 | |
|---|
| 368 | |
|---|
| 369 | allowed=False |
|---|
| 370 | try: |
|---|
| 371 | if os.stat(mountline.split()[1]).st_uid == int(uid): |
|---|
| 372 | allowed=True |
|---|
| 373 | except Exception: |
|---|
| 374 | pass |
|---|
| 375 | |
|---|
| 376 | if not allowed: |
|---|
| 377 | return "not-yours" |
|---|
| 378 | |
|---|
| 379 | |
|---|
| 380 | |
|---|
| 381 | serial_partnumber=mountline.split()[1].replace("/media/", '') |
|---|
| 382 | if serial_partnumber == "": |
|---|
| 383 | return "no-serial" |
|---|
| 384 | |
|---|
| 385 | |
|---|
| 386 | os.system("sync ; umount %s"%dev) |
|---|
| 387 | |
|---|
| 388 | |
|---|
| 389 | try: |
|---|
| 390 | os.rmdir(mountline.split()[1]) |
|---|
| 391 | except Exception: |
|---|
| 392 | pass |
|---|
| 393 | |
|---|
| 394 | |
|---|
| 395 | desktop='' |
|---|
| 396 | if os.path.isdir(home + "/Escritorio"): |
|---|
| 397 | desktop=home + "/Escritorio" |
|---|
| 398 | elif os.path.isdir(home + "/Desktop"): |
|---|
| 399 | desktop=home + "/Desktop" |
|---|
| 400 | |
|---|
| 401 | try: |
|---|
| 402 | os.unlink(desktop + "/%s.desktop"%serial_partnumber) |
|---|
| 403 | except Exception: |
|---|
| 404 | pass |
|---|
| 405 | |
|---|
| 406 | return "ok" |
|---|
| 407 | |
|---|
| 408 | |
|---|
| 409 | |
|---|
| 410 | if __name__ == '__main__': |
|---|
| 411 | if len(sys.argv) == 3: |
|---|
| 412 | |
|---|
| 413 | |
|---|
| 414 | print umount(sys.argv[1], sys.argv[2]) |
|---|
| 415 | sys.exit(0) |
|---|
| 416 | |
|---|
| 417 | elif "--daemon" in sys.argv: |
|---|
| 418 | |
|---|
| 419 | app = MultiSeatDeviceManager() |
|---|
| 420 | app.run() |
|---|
| 421 | sys.exit(0) |
|---|
| 422 | |
|---|
| 423 | print """no options???? |
|---|
| 424 | |
|---|
| 425 | try: |
|---|
| 426 | to run as inhibitor daemon: |
|---|
| 427 | multiseat-udisks --daemon |
|---|
| 428 | |
|---|
| 429 | to umount a device (this is called by /sbin/umount.multiseat) |
|---|
| 430 | multiseat-udisks /dev/sdc1 1000 |
|---|
| 431 | |
|---|
| 432 | """ |
|---|