From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Harald Hanche-Olsen Newsgroups: gmane.emacs.devel Subject: Re: Modifying Emacs to use the Mac OS X Keychain Services Date: Sat, 28 Jul 2012 14:16:53 +0200 (CEST) Message-ID: <20120728.141653.692591133295914672.hanche@math.ntnu.no> References: <87tyc0camo.fsf@lifelogs.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: dough.gmane.org 1343477831 27159 80.91.229.3 (28 Jul 2012 12:17:11 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Sat, 28 Jul 2012 12:17:11 +0000 (UTC) Cc: emacs-devel@gnu.org To: dave@boostpro.com Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sat Jul 28 14:17:12 2012 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Sv5xF-0004QF-Ur for ged-emacs-devel@m.gmane.org; Sat, 28 Jul 2012 14:17:10 +0200 Original-Received: from localhost ([::1]:35592 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Sv5xE-0006DC-TT for ged-emacs-devel@m.gmane.org; Sat, 28 Jul 2012 08:17:08 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:59801) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Sv5x8-000600-QB for emacs-devel@gnu.org; Sat, 28 Jul 2012 08:17:06 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Sv5x4-0004hA-SH for emacs-devel@gnu.org; Sat, 28 Jul 2012 08:17:02 -0400 Original-Received: from hylle01.itea.ntnu.no ([129.241.56.100]:38244) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Sv5x4-0004fh-HA for emacs-devel@gnu.org; Sat, 28 Jul 2012 08:16:58 -0400 Original-Received: from localhost (localhost [127.0.0.1]) by hylle01.itea.ntnu.no (Postfix) with ESMTP id 9EFC039A05A for ; Sat, 28 Jul 2012 14:16:55 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at hylle01.itea.ntnu.no Original-Received: from anne.math.ntnu.no (anne.math.ntnu.no [129.241.15.150]) by hylle01.itea.ntnu.no (Postfix) with SMTP id 1AE7539A056 for ; Sat, 28 Jul 2012 14:16:55 +0200 (CEST) Original-Received: (qmail 28654 invoked from network); 28 Jul 2012 12:16:55 -0000 Original-Received: from gauss.math.ntnu.no (HELO localhost) (hanche@129.241.15.58) by anne.math.ntnu.no with ESMTPA; 28 Jul 2012 12:16:55 -0000 In-Reply-To: X-URL: http://www.math.ntnu.no/~hanche/ X-Mailer: Mew version 6.3.50 on Emacs 24.1.50 / Mule 6.0 (HANACHIRUSATO) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-Received-From: 129.241.56.100 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:151934 Archived-At: (This probably doesn't really belong on the emacs-devel mailing list, but if we keep it short, maybe nobody will object. My apologies if someone does object. I skip the copy to ding@gnus.org, since I am not on that list myself, and don't even know what it's for.) [Dave Abrahams (2012-07-27 15:20:17 UTC)] > I looked a bit at the "secrets" API but could understand it easily > enough to code something up. It's a bit messy, with output clearly meant for human consumption and not for programs. > I just want Emacs to run > > /usr/bin/security --find-internet-password -gs > > to get the password for my mail server. Since this scratches an itch of my own, I cooked up the following. Feel free to adapt, bend and mutilate it to your heart's content. (defun get-keychain-password (service) "Get a generic password from the OS X keychain. The password is associated with the string SERVICE. This corresponds to the Account field in the Keychain Access GUI. BUG: If there is no matching password, or the output of /usr/bin/security has an unexpected format, the function silently returns NIL." (with-temp-buffer ;; /usr/bin/security outputs all kinds of info on stdout ;; oddly, the password itself appears on stderr, in the form ;; password: "..." with a final newline ;; No escape mechanism appears to be used for quotes etc in the password (call-process "/bin/sh" nil (list (current-buffer) t) nil "-c" "/usr/bin/security \"$@\" >/dev/null" "-" "find-generic-password" "-gs" service) (let ((min (point-min)) (max (point-max))) (when (and (> (- max min) 13) (string= (buffer-substring-no-properties min (+ min 11)) "password: \"") (string= (buffer-substring-no-properties (- max 2) max) "\"\n")) (buffer-substring-no-properties (+ min 11) (- max 2)))))) - Harald