{"id":33,"date":"2007-11-11T09:51:04","date_gmt":"2007-11-11T09:51:04","guid":{"rendered":"http:\/\/blog.danplanet.com\/wordpress\/?p=33"},"modified":"2011-11-29T16:56:48","modified_gmt":"2011-11-29T16:56:48","slug":"using-dbus-to-take-action-on-a-usb-storage-device","status":"publish","type":"post","link":"https:\/\/www.danplanet.com\/blog\/2007\/11\/11\/using-dbus-to-take-action-on-a-usb-storage-device\/","title":{"rendered":"Using DBUS to take action on a USB storage device"},"content":{"rendered":"<p>I keep my password-encrypted SSH and PGP keys on a small USB key.\u00a0 I do this to keep these sensitive files separate from my laptop, in case I were to lose it.\u00a0 I figure it&#8217;s easier to keep a USB key on me whenever I&#8217;m at work or traveling.\u00a0 I don&#8217;t want to have to keep my USB key inserted all the time to encrypt files and emails or SSH to remote hosts.\u00a0 Even though that would probably be more secure, I think I&#8217;d wear out the flash memory and would probably get annoyed before that even happened.\u00a0 So, I want to cache the sensitive data in memory, so that it&#8217;s lost on powerdown, but available to me while working normally.<\/p>\n<p>Luckily, ssh-agent provides this sort of functionality already, but GPG does not (that I know of).\u00a0 What I want is some way for me to just insert my key and have the PGP files copied to a volatile location and the SSH keys inserted into the agent automatically (prompting for a password first, of course).\u00a0 I could do this with an autorun file on the key itself, but then inserting it into random machines might copy my sensitive files, which would be really bad.\u00a0 Since DBUS seems to be pretty nifty, I figured I should see about writing a little app to respond to the DBUS mount event to run a script for me.\u00a0 I don&#8217;t know much about DBUS, so it seemed like a good exercise.<\/p>\n<p>At first, I saw a <a href=\"http:\/\/www.linuxjournal.com\/article\/9311\">Linux Journal article<\/a> about doing this with udev rules, but that isn&#8217;t as nice because you don&#8217;t have DISPLAY set, and it doesn&#8217;t run as your own user.\u00a0 Still workable, but less nice.\u00a0 So, I googled for examples of using DBUS from Python and came up with the following dispatch app:<\/p>\n<blockquote>\n<p style=\"font-family: Courier New,Courier,mono;\">#!\/usr\/bin\/python<\/p>\n<p>DEVICE_TAG=&#8221;dansmith&#8221;<br \/>\nWORK_SCRIPT=&#8221;\/home\/dan\/bin\/sshkey.sh&#8221;<\/p>\n<p>import dbus<br \/>\nimport gobject<br \/>\nimport dbus.mainloop.glib<br \/>\nimport os<\/p>\n<p>def handler(*args, **kwargs):<br \/>\nif not kwargs[&#8220;member&#8221;] == &#8220;VolumeMountedSignal&#8221;:<br \/>\nreturn<\/p>\n<p>uri = args[0][4].replace(&#8220;file:\/\/&#8221;, &#8220;&#8221<img decoding=\"async\" src=\"https:\/\/www.danplanet.com\/blog\/wp-content\/themes\/wordpress-grey-opaque-master\/images\/smilies\/icon_wink.gif\" alt=\"Smilie: ;)\" title=\"Smilie: ;)\" \/><br \/>\ntag = args[0][6]<\/p>\n<p>if tag == DEVICE_TAG:<br \/>\nos.system(&#8220;%s %s&#8221; % (WORK_SCRIPT, uri))<\/p>\n<p>dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)<br \/>\nbus = dbus.SessionBus()<br \/>\nbus.add_signal_receiver(handler,<br \/>\ninterface_keyword=&#8221;dbus_interface&#8221;,<br \/>\nmember_keyword=&#8221;member&#8221<img decoding=\"async\" src=\"https:\/\/www.danplanet.com\/blog\/wp-content\/themes\/wordpress-grey-opaque-master\/images\/smilies\/icon_wink.gif\" alt=\"Smilie: ;)\" title=\"Smilie: ;)\" \/><\/p>\n<p>loop = gobject.MainLoop()<br \/>\nloop.run()<\/p><\/blockquote>\n<p>I can then exec this as part of my gnome session startup.\u00a0 The app listens for the &#8220;VolumeMountedSignal&#8221; and checks the volume label against what I know my USB key has.\u00a0 If it matches, it runs my little helper script.\u00a0 The script mounts a tmpfs in a subdirectory of $HOME, copies the PGP files into place, and then runs ssh-add on all of the SSH keys.\u00a0 Since DISPLAY is set, I get the GUI ssh-askpass box to unlock each key.<\/p>\n<p>It would be interesting to extend this little dispatch app to have a GUI interface to let you add device labels and associated actions.\u00a0 It might be handy to have a couple things like &#8220;exec script foo&#8221;, &#8220;copy contents to \/path\/bar&#8221;, &#8220;freshen a tarball backup&#8221;, etc.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I keep my password-encrypted SSH and PGP keys on a small USB key.\u00a0 I do this to keep these sensitive files separate from my laptop, in case I were to lose it.\u00a0 I figure it&#8217;s easier to keep a USB &hellip; <a href=\"https:\/\/www.danplanet.com\/blog\/2007\/11\/11\/using-dbus-to-take-action-on-a-usb-storage-device\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,6],"tags":[49,48],"class_list":["post-33","post","type-post","status-publish","format-standard","hentry","category-codemonkeying","category-linux","tag-dbus","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Using DBUS to take action on a USB storage device - Right Angles<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.danplanet.com\/blog\/2007\/11\/11\/using-dbus-to-take-action-on-a-usb-storage-device\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using DBUS to take action on a USB storage device - Right Angles\" \/>\n<meta property=\"og:description\" content=\"I keep my password-encrypted SSH and PGP keys on a small USB key.\u00a0 I do this to keep these sensitive files separate from my laptop, in case I were to lose it.\u00a0 I figure it&#8217;s easier to keep a USB &hellip; Continue reading &rarr;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.danplanet.com\/blog\/2007\/11\/11\/using-dbus-to-take-action-on-a-usb-storage-device\/\" \/>\n<meta property=\"og:site_name\" content=\"Right Angles\" \/>\n<meta property=\"article:published_time\" content=\"2007-11-11T09:51:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2011-11-29T16:56:48+00:00\" \/>\n<meta name=\"author\" content=\"Dan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Dan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.danplanet.com\/blog\/2007\/11\/11\/using-dbus-to-take-action-on-a-usb-storage-device\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.danplanet.com\/blog\/2007\/11\/11\/using-dbus-to-take-action-on-a-usb-storage-device\/\"},\"author\":{\"name\":\"Dan\",\"@id\":\"https:\/\/www.danplanet.com\/blog\/#\/schema\/person\/0f6920aa6d63cae437bf8b122200287c\"},\"headline\":\"Using DBUS to take action on a USB storage device\",\"datePublished\":\"2007-11-11T09:51:04+00:00\",\"dateModified\":\"2011-11-29T16:56:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.danplanet.com\/blog\/2007\/11\/11\/using-dbus-to-take-action-on-a-usb-storage-device\/\"},\"wordCount\":528,\"publisher\":{\"@id\":\"https:\/\/www.danplanet.com\/blog\/#\/schema\/person\/0f6920aa6d63cae437bf8b122200287c\"},\"keywords\":[\"dbus\",\"python\"],\"articleSection\":[\"Codemonkeying\",\"Linux\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.danplanet.com\/blog\/2007\/11\/11\/using-dbus-to-take-action-on-a-usb-storage-device\/\",\"url\":\"https:\/\/www.danplanet.com\/blog\/2007\/11\/11\/using-dbus-to-take-action-on-a-usb-storage-device\/\",\"name\":\"Using DBUS to take action on a USB storage device - Right Angles\",\"isPartOf\":{\"@id\":\"https:\/\/www.danplanet.com\/blog\/#website\"},\"datePublished\":\"2007-11-11T09:51:04+00:00\",\"dateModified\":\"2011-11-29T16:56:48+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.danplanet.com\/blog\/2007\/11\/11\/using-dbus-to-take-action-on-a-usb-storage-device\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.danplanet.com\/blog\/2007\/11\/11\/using-dbus-to-take-action-on-a-usb-storage-device\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.danplanet.com\/blog\/2007\/11\/11\/using-dbus-to-take-action-on-a-usb-storage-device\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.danplanet.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using DBUS to take action on a USB storage device\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.danplanet.com\/blog\/#website\",\"url\":\"https:\/\/www.danplanet.com\/blog\/\",\"name\":\"Right Angles\",\"description\":\"If they&#039;re not right...they&#039;re wrong\",\"publisher\":{\"@id\":\"https:\/\/www.danplanet.com\/blog\/#\/schema\/person\/0f6920aa6d63cae437bf8b122200287c\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.danplanet.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.danplanet.com\/blog\/#\/schema\/person\/0f6920aa6d63cae437bf8b122200287c\",\"name\":\"Dan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.danplanet.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/9b73782704be64dd8c030087af2d1ae0c1dc488cad69093ff0366dbaad2de673?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/9b73782704be64dd8c030087af2d1ae0c1dc488cad69093ff0366dbaad2de673?s=96&d=mm&r=g\",\"caption\":\"Dan\"},\"logo\":{\"@id\":\"https:\/\/www.danplanet.com\/blog\/#\/schema\/person\/image\/\"},\"url\":\"https:\/\/www.danplanet.com\/blog\/author\/dan\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using DBUS to take action on a USB storage device - Right Angles","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.danplanet.com\/blog\/2007\/11\/11\/using-dbus-to-take-action-on-a-usb-storage-device\/","og_locale":"en_US","og_type":"article","og_title":"Using DBUS to take action on a USB storage device - Right Angles","og_description":"I keep my password-encrypted SSH and PGP keys on a small USB key.\u00a0 I do this to keep these sensitive files separate from my laptop, in case I were to lose it.\u00a0 I figure it&#8217;s easier to keep a USB &hellip; Continue reading &rarr;","og_url":"https:\/\/www.danplanet.com\/blog\/2007\/11\/11\/using-dbus-to-take-action-on-a-usb-storage-device\/","og_site_name":"Right Angles","article_published_time":"2007-11-11T09:51:04+00:00","article_modified_time":"2011-11-29T16:56:48+00:00","author":"Dan","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Dan","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.danplanet.com\/blog\/2007\/11\/11\/using-dbus-to-take-action-on-a-usb-storage-device\/#article","isPartOf":{"@id":"https:\/\/www.danplanet.com\/blog\/2007\/11\/11\/using-dbus-to-take-action-on-a-usb-storage-device\/"},"author":{"name":"Dan","@id":"https:\/\/www.danplanet.com\/blog\/#\/schema\/person\/0f6920aa6d63cae437bf8b122200287c"},"headline":"Using DBUS to take action on a USB storage device","datePublished":"2007-11-11T09:51:04+00:00","dateModified":"2011-11-29T16:56:48+00:00","mainEntityOfPage":{"@id":"https:\/\/www.danplanet.com\/blog\/2007\/11\/11\/using-dbus-to-take-action-on-a-usb-storage-device\/"},"wordCount":528,"publisher":{"@id":"https:\/\/www.danplanet.com\/blog\/#\/schema\/person\/0f6920aa6d63cae437bf8b122200287c"},"keywords":["dbus","python"],"articleSection":["Codemonkeying","Linux"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.danplanet.com\/blog\/2007\/11\/11\/using-dbus-to-take-action-on-a-usb-storage-device\/","url":"https:\/\/www.danplanet.com\/blog\/2007\/11\/11\/using-dbus-to-take-action-on-a-usb-storage-device\/","name":"Using DBUS to take action on a USB storage device - Right Angles","isPartOf":{"@id":"https:\/\/www.danplanet.com\/blog\/#website"},"datePublished":"2007-11-11T09:51:04+00:00","dateModified":"2011-11-29T16:56:48+00:00","breadcrumb":{"@id":"https:\/\/www.danplanet.com\/blog\/2007\/11\/11\/using-dbus-to-take-action-on-a-usb-storage-device\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.danplanet.com\/blog\/2007\/11\/11\/using-dbus-to-take-action-on-a-usb-storage-device\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.danplanet.com\/blog\/2007\/11\/11\/using-dbus-to-take-action-on-a-usb-storage-device\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.danplanet.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Using DBUS to take action on a USB storage device"}]},{"@type":"WebSite","@id":"https:\/\/www.danplanet.com\/blog\/#website","url":"https:\/\/www.danplanet.com\/blog\/","name":"Right Angles","description":"If they&#039;re not right...they&#039;re wrong","publisher":{"@id":"https:\/\/www.danplanet.com\/blog\/#\/schema\/person\/0f6920aa6d63cae437bf8b122200287c"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.danplanet.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.danplanet.com\/blog\/#\/schema\/person\/0f6920aa6d63cae437bf8b122200287c","name":"Dan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.danplanet.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/9b73782704be64dd8c030087af2d1ae0c1dc488cad69093ff0366dbaad2de673?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/9b73782704be64dd8c030087af2d1ae0c1dc488cad69093ff0366dbaad2de673?s=96&d=mm&r=g","caption":"Dan"},"logo":{"@id":"https:\/\/www.danplanet.com\/blog\/#\/schema\/person\/image\/"},"url":"https:\/\/www.danplanet.com\/blog\/author\/dan\/"}]}},"_links":{"self":[{"href":"https:\/\/www.danplanet.com\/blog\/wp-json\/wp\/v2\/posts\/33","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.danplanet.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.danplanet.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.danplanet.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.danplanet.com\/blog\/wp-json\/wp\/v2\/comments?post=33"}],"version-history":[{"count":1,"href":"https:\/\/www.danplanet.com\/blog\/wp-json\/wp\/v2\/posts\/33\/revisions"}],"predecessor-version":[{"id":155,"href":"https:\/\/www.danplanet.com\/blog\/wp-json\/wp\/v2\/posts\/33\/revisions\/155"}],"wp:attachment":[{"href":"https:\/\/www.danplanet.com\/blog\/wp-json\/wp\/v2\/media?parent=33"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.danplanet.com\/blog\/wp-json\/wp\/v2\/categories?post=33"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.danplanet.com\/blog\/wp-json\/wp\/v2\/tags?post=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}