diff -r fb73a2046d3e -r c3f54e98b98d modules/keychain/Makefile.in.in
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/keychain/Makefile.in.in	Sat Dec 22 11:18:29 2007 +0100
@@ -0,0 +1,53 @@
+##   Makefile for the OS X keychain module in XEmacs.
+##   Copyright (C) 2002 Jerry James.
+##   Copyright (C) 2007 Eric Knauel
+
+## This file is not part of XEmacs.
+
+### Specialize this part for your module
+MODNAME=keychain
+MODVER=0.0.1
+MODTITLE="OS X Keychain support for XEmacs"
+SRCS=keychain.c
+
+### You should not need to modify anything below this line
+SHELL=/bin/sh
+RM=rm -f
+CFLAGS=@CFLAGS@
+LDFLAGS=@LDFLAGS@ -framework Security -framework Carbon
+
+srcdir=@srcdir@
+VPATH=@srcdir@
+
+SRC_SRCS=$(SRCS:%=$(srcdir)/%)
+OBJS=$(SRCS:.c=.o)
+
+ELLCC=@ELLCC@
+MODARCHDIR=$(shell @ELLCC@ --mod-archdir)
+INSTALLPATH=$(shell @ELLCC@ --mod-site-location)
+INSTALL=@INSTALL@
+INSTALL_PROGRAM=@INSTALL_PROGRAM@
+
+.PHONY:	clean distclean install
+all: $(MODNAME).ell
+
+.c.o:
+	$(ELLCC) $(CFLAGS) --mode=compile --mod-output=$@ \
+	-I$(MODARCHDIR)/include -c $<
+
+$(MODNAME).ell: $(OBJS) $(MODNAME)_i.o
+	$(ELLCC) --mode=link --mod-output=$@ $(OBJS) $(MODNAME)_i.o $(LDFLAGS)
+
+$(MODNAME)_i.c: $(SRCS)
+	ELLMAKEDOC=$(MODARCHDIR)/make-docfile $(ELLCC) --mode=init \
+	--mod-output=$@ --mod-name=$(MODNAME) --mod-version=$(MODVER) \
+	--mod-title=$(MODTITLE) $(SRC_SRCS)
+
+clean:
+	$(RM) $(MODNAME).ell $(OBJS) $(MODNAME)_i.* *~
+
+distclean: clean
+	$(RM) Makefile config.* configure
+
+install: $(MODNAME).ell
+	$(INSTALL_PROGRAM) $< $(INSTALLPATH)
diff -r fb73a2046d3e -r c3f54e98b98d modules/keychain/configure.ac
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/keychain/configure.ac	Sat Dec 22 11:18:29 2007 +0100
@@ -0,0 +1,27 @@
+# Process this file with autoconf to produce a configure script.
+AC_INIT([OS X Keychain support for XEmacs], [1.0], [xemacs-beta@xemacs.org])
+AC_PREREQ(2.53)
+AC_REVISION($Revision: 1.1 $)
+AC_COPYRIGHT([Configuration script for the OS X Keychain module.
+Copyright (C) 2002 Jerry James.
+Copyright (C) 2007 Eric Knauel.
+
+This file is not part of XEmacs.])
+
+AC_CONFIG_SRCDIR([keychain.c])
+AC_PROG_CC
+AC_PROG_INSTALL
+AC_SUBST(CFLAGS)
+AC_SUBST(LDFLAGS)
+
+# Find ELLCC
+AC_ARG_VAR([ELLCC], [The path to the ellcc module compiler])
+AC_PATH_PROG([ELLCC], [ellcc], ["FAIL"])
+AS_IF([test "$ELLCC" = "FAIL"], [AS_ERROR([Cannot find ellcc])])
+AC_SUBST(ELLCC)
+
+# Insert autoconf macros here to find the headers, libraries, other
+# programs, etc. needed by your module.
+
+AC_CONFIG_FILES([Makefile.in Makefile])
+AC_OUTPUT
diff -r fb73a2046d3e -r c3f54e98b98d modules/keychain/install-sh
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/keychain/install-sh	Sat Dec 22 11:18:29 2007 +0100
@@ -0,0 +1,251 @@
+#!/bin/sh
+#
+# install - install a program, script, or datafile
+# This comes from X11R5 (mit/util/scripts/install.sh).
+#
+# Copyright 1991 by the Massachusetts Institute of Technology
+#
+# Permission to use, copy, modify, distribute, and sell this software and its
+# documentation for any purpose is hereby granted without fee, provided that
+# the above copyright notice appear in all copies and that both that
+# copyright notice and this permission notice appear in supporting
+# documentation, and that the name of M.I.T. not be used in advertising or
+# publicity pertaining to distribution of the software without specific,
+# written prior permission.  M.I.T. makes no representations about the
+# suitability of this software for any purpose.  It is provided "as is"
+# without express or implied warranty.
+#
+# Calling this script install-sh is preferred over install.sh, to prevent
+# `make' implicit rules from creating a file called install from it
+# when there is no Makefile.
+#
+# This script is compatible with the BSD install script, but was written
+# from scratch.  It can only install one file at a time, a restriction
+# shared with many OS's install programs.
+
+
+# set DOITPROG to echo to test this script
+
+# Don't use :- since 4.3BSD and earlier shells don't like it.
+doit="${DOITPROG-}"
+
+
+# put in absolute paths if you don't have them in your path; or use env. vars.
+
+mvprog="${MVPROG-mv}"
+cpprog="${CPPROG-cp}"
+chmodprog="${CHMODPROG-chmod}"
+chownprog="${CHOWNPROG-chown}"
+chgrpprog="${CHGRPPROG-chgrp}"
+stripprog="${STRIPPROG-strip}"
+rmprog="${RMPROG-rm}"
+mkdirprog="${MKDIRPROG-mkdir}"
+
+transformbasename=""
+transform_arg=""
+instcmd="$mvprog"
+chmodcmd="$chmodprog 0755"
+chowncmd=""
+chgrpcmd=""
+stripcmd=""
+rmcmd="$rmprog -f"
+mvcmd="$mvprog"
+src=""
+dst=""
+dir_arg=""
+
+while [ x"$1" != x ]; do
+    case $1 in
+	-c) instcmd="$cpprog"
+	    shift
+	    continue;;
+
+	-d) dir_arg=true
+	    shift
+	    continue;;
+
+	-m) chmodcmd="$chmodprog $2"
+	    shift
+	    shift
+	    continue;;
+
+	-o) chowncmd="$chownprog $2"
+	    shift
+	    shift
+	    continue;;
+
+	-g) chgrpcmd="$chgrpprog $2"
+	    shift
+	    shift
+	    continue;;
+
+	-s) stripcmd="$stripprog"
+	    shift
+	    continue;;
+
+	-t=*) transformarg=`echo $1 | sed 's/-t=//'`
+	    shift
+	    continue;;
+
+	-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
+	    shift
+	    continue;;
+
+	*)  if [ x"$src" = x ]
+	    then
+		src=$1
+	    else
+		# this colon is to work around a 386BSD /bin/sh bug
+		:
+		dst=$1
+	    fi
+	    shift
+	    continue;;
+    esac
+done
+
+if [ x"$src" = x ]
+then
+	echo "install:	no input file specified"
+	exit 1
+else
+	:
+fi
+
+if [ x"$dir_arg" != x ]; then
+	dst=$src
+	src=""
+	
+	if [ -d $dst ]; then
+		instcmd=:
+		chmodcmd=""
+	else
+		instcmd=$mkdirprog
+	fi
+else
+
+# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
+# might cause directories to be created, which would be especially bad 
+# if $src (and thus $dsttmp) contains '*'.
+
+	if [ -f "$src" ] || [ -d "$src" ]
+	then
+		:
+	else
+		echo "install:  $src does not exist"
+		exit 1
+	fi
+	
+	if [ x"$dst" = x ]
+	then
+		echo "install:	no destination specified"
+		exit 1
+	else
+		:
+	fi
+
+# If destination is a directory, append the input filename; if your system
+# does not like double slashes in filenames, you may need to add some logic
+
+	if [ -d $dst ]
+	then
+		dst="$dst"/`basename $src`
+	else
+		:
+	fi
+fi
+
+## this sed command emulates the dirname command
+dstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
+
+# Make sure that the destination directory exists.
+#  this part is taken from Noah Friedman's mkinstalldirs script
+
+# Skip lots of stat calls in the usual case.
+if [ ! -d "$dstdir" ]; then
+defaultIFS='
+	'
+IFS="${IFS-${defaultIFS}}"
+
+oIFS="${IFS}"
+# Some sh's can't handle IFS=/ for some reason.
+IFS='%'
+set - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
+IFS="${oIFS}"
+
+pathcomp=''
+
+while [ $# -ne 0 ] ; do
+	pathcomp="${pathcomp}${1}"
+	shift
+
+	if [ ! -d "${pathcomp}" ] ;
+        then
+		$mkdirprog "${pathcomp}"
+	else
+		:
+	fi
+
+	pathcomp="${pathcomp}/"
+done
+fi
+
+if [ x"$dir_arg" != x ]
+then
+	$doit $instcmd $dst &&
+
+	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else : ; fi &&
+	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else : ; fi &&
+	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else : ; fi &&
+	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else : ; fi
+else
+
+# If we're going to rename the final executable, determine the name now.
+
+	if [ x"$transformarg" = x ] 
+	then
+		dstfile=`basename $dst`
+	else
+		dstfile=`basename $dst $transformbasename | 
+			sed $transformarg`$transformbasename
+	fi
+
+# don't allow the sed command to completely eliminate the filename
+
+	if [ x"$dstfile" = x ] 
+	then
+		dstfile=`basename $dst`
+	else
+		:
+	fi
+
+# Make a temp file name in the proper directory.
+
+	dsttmp=$dstdir/#inst.$$#
+
+# Move or copy the file name to the temp name
+
+	$doit $instcmd $src $dsttmp &&
+
+	trap "rm -f ${dsttmp}" 0 &&
+
+# and set any options; do chmod last to preserve setuid bits
+
+# If any of these fail, we abort the whole thing.  If we want to
+# ignore errors from any of these, just make sure not to ignore
+# errors from the above "$doit $instcmd $src $dsttmp" command.
+
+	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else :;fi &&
+	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else :;fi &&
+	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else :;fi &&
+	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else :;fi &&
+
+# Now rename the file to the real destination.
+
+	$doit $rmcmd -f $dstdir/$dstfile &&
+	$doit $mvcmd $dsttmp $dstdir/$dstfile 
+
+fi &&
+
+
+exit 0
diff -r fb73a2046d3e -r c3f54e98b98d modules/keychain/keychain.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/keychain/keychain.c	Sat Dec 22 11:18:29 2007 +0100
@@ -0,0 +1,265 @@
+/*
+ * XEmacs dynamic module for the OS X keychain API.
+ * (C) Copyright 2007 Eric Knauel
+ */
+
+#include <config.h>
+#include "lisp.h"
+
+#include <stdio.h>
+
+#include <CoreFoundation/CoreFoundation.h>
+#include <Security/Security.h>
+#include <CoreServices/CoreServices.h>
+
+#define DEBUG 0
+
+#define DEFAULT_SERVICE_NAME "XEmacs"
+
+/* Given the Lisp string STR this macro extracts the C representation
+   of this string encoded in UTF-8.  All Keychain functions expect
+   string arguments to be encoded with UTF-8. */
+#define extract_utf8_string(str) \
+  (NEW_LISP_STRING_TO_EXTERNAL((str), Qutf_8))
+
+/* Extract the raw C representation of the Lisp string STR */
+#define extract_binary_string(str) \
+  (NEW_LISP_STRING_TO_EXTERNAL((str), Qbinary)) 
+
+/* Signal a Carbon error.  Carbon does not seem to have something like
+   strerror() */
+#define signal_keychain_error(status) \
+  (Fsignal(Qinvalid_state, list2(build_string("Carbon error"),  \
+				 make_int((int) status))))
+
+static Lisp_Object Qkeychain;
+
+/* Open a reference to the keychain specified by the Lisp string ARG
+   and place the reference in REF. If ARG is nil, set REF to NULL
+   which means that REF is a reference to the user's default
+   keychain. */
+void open_keychain(Lisp_Object arg, SecKeychainRef *ref)
+{
+  OSStatus status;
+
+  if (STRINGP(arg))
+    {
+      status = SecKeychainOpen(extract_utf8_string(arg), ref);
+#if DEBUG
+      fprintf(stderr, "SecKeychainOpen() '%s' %d\n",
+	      extract_utf8_string(arg), (int) status);
+#endif /* DEBUG */
+      if (status != noErr)
+	signal_keychain_error(status);
+    }
+  else
+    *ref = NULL;
+}
+
+DEFUN ("keychain-find", Fkeychain_find, 1, 3, 0, /*
+Find and return the password item identified by the string ACCOUNT in
+an OS X keychain. Returns nil if there no such item.  
+
+The optional SERVICE argument may either be a string indicating the
+so-called service name (i. e. the value given in the field labeled
+``where'' in password item dialog of Keychain Access.app).  When the
+value of SERVICE is nil KEYCHAIN-FIND uses the default service name
+``XEmacs''.
+
+The optional KEYCHAIN argument may either be a string with the path to
+keychain file or nil.  If the value is nil, KEYCHAIN-FIND searches the
+user's default keychain.  Specifying an invalid path leads to an
+INVALID-STATE error signal. */
+       (account, service, keychain))
+{
+  OSStatus status;
+  SecKeychainItemRef ref = nil;
+  char *service_name, *account_name;
+  SecKeychainRef keychain_ref;
+  Lisp_Object password_str = Qnil;
+  UInt32 password_data_len;
+  void *password_data;
+
+  CHECK_STRING(account);
+  account_name = extract_utf8_string(account);
+  service_name = 
+    STRINGP(service) ? extract_utf8_string(service) : DEFAULT_SERVICE_NAME;
+  open_keychain(keychain, &keychain_ref);
+
+  status = SecKeychainFindGenericPassword(keychain_ref,
+					  (UInt32) strlen(service_name), 
+					  service_name,
+					  (UInt32) strlen(account_name), 
+					  account_name,
+					  &password_data_len,
+					  &password_data,
+					  &ref);
+#if DEBUG
+  fprintf(stderr, "SecKeychainFindGenericPassword(): '%s' '%s' %d\n",
+	  service_name, account_name, (int) status);
+#endif
+
+  switch (status) {
+
+  case noErr:
+    /* make a copy of password_data and turn it into a lisp string. */
+    password_str = make_ext_string((Extbyte *) password_data,
+				   (EMACS_INT) password_data_len,
+				   Qbinary);
+    SecKeychainItemFreeContent(NULL, password_data);
+    if (keychain_ref != NULL)
+      CFRelease(keychain_ref);
+    CFRelease(ref);
+    return password_str;
+
+  case errSecItemNotFound:
+    return Qnil;
+
+  default:
+    signal_keychain_error(status);
+  }
+
+  return Qnil;
+}
+
+DEFUN ("keychain-add", Fkeychain_add, 2, 4, 0, /* 
+Add a new password string PASSWORD identified with the key ACCOUNT (a
+string) to the OS X Keychain.  Returns t on success or raises an error
+if adding the item fails.
+
+The optional SERVICE argument may either be a string indicating the
+so-called service name (i. e. the value given in the field labeled
+``where'' in password item dialog of Keychain Access.app).  When the
+value of SERVICE is nil KEYCHAIN-FIND uses the default service name
+``XEmacs''.
+
+The optional KEYCHAIN argument may either be a string with the path to
+keychain file or nil.  If the value is nil, KEYCHAIN-ADD adds the item
+to the user's default keychain.  Specifying an invalid path leads to
+an INVALID-STATE error signal. */
+       (account, password, service, keychain))
+{
+  OSStatus status;
+  char *account_name, *password_data, *service_name;
+  SecKeychainItemRef ref = nil;
+  SecKeychainRef keychain_ref;
+
+  CHECK_STRING(account);
+  CHECK_STRING(password);
+
+  account_name = extract_utf8_string(account);
+  password_data = extract_binary_string(password);
+  service_name = 
+    STRINGP(service) ? extract_utf8_string(service) : DEFAULT_SERVICE_NAME;
+  open_keychain(keychain, &keychain_ref);
+
+  status = SecKeychainAddGenericPassword(keychain_ref,
+ 					 (UInt32) strlen(service_name),
+ 					 service_name,
+ 					 (UInt32) strlen(account_name),
+ 					 account_name,
+ 					 (UInt32) strlen(password_data),
+ 					 password_data,
+ 					 &ref);
+#if DEBUG
+  fprintf(stderr, "SecKeychainAddGenericPassword() '%s' '%s' %d\n",
+	  service_name, account_name, (int) status);
+#endif /* DEBUG */
+  switch (status) {
+  case noErr:
+    return Qt;
+
+  case errSecNoDefaultKeychain:
+    Fsignal(Qinvalid_state,
+	    list1(build_string("no default keychain found")));
+
+  case errSecDuplicateItem:
+    Fsignal(Qinvalid_state,
+	    list1(build_string("duplicate keychain item")));
+    
+  case errSecDataTooLarge:
+    Fsignal(Qinvalid_state,
+	    list1(build_string("password too long")));
+
+  default:
+    signal_keychain_error(status);
+  }
+
+  return Qnil;
+}
+
+DEFUN ("keychain-remove", Fkeychain_remove, 1, 3, 0, /*
+
+Remove the password item identified by the string ACCOUNT from an OS X
+keychain.  Returns t if the deletion succeeds.
+
+The optional SERVICE argument may either be a string indicating the
+so-called service name (i. e. the value given in the field labeled
+``where'' in password item dialog of Keychain Access.app).  When the
+value of SERVICE is nil KEYCHAIN-FIND uses the default service name
+``XEmacs''.
+
+The optional KEYCHAIN argument may either be a string with the path to
+keychain file or nil.  If the value is nil, KEYCHAIN-REMOVE uses the
+user's default keychain.  Specifying an invalid path leads to an
+INVALID-STATE error signal. */
+       (account, service, keychain))
+{
+  OSStatus status;
+  SecKeychainItemRef ref = nil;
+  char *service_name, *account_name;
+  SecKeychainRef keychain_ref;
+
+  CHECK_STRING(account);
+  account_name = extract_utf8_string(account);
+  service_name = 
+    STRINGP(service) ? extract_utf8_string(service) : DEFAULT_SERVICE_NAME;
+  open_keychain(keychain, &keychain_ref);
+
+  status = SecKeychainFindGenericPassword(keychain_ref,
+					  (UInt32) strlen(service_name), 
+					  service_name,
+					  (UInt32) strlen(account_name), 
+					  account_name,
+					  NULL, NULL, /* don't fetch password */
+					  &ref);
+#if DEBUG
+  fprintf(stderr, "SecKeychainFindGenericPassword(): '%s' '%s' %d\n",
+	  service_name, account_name, (int) status);
+#endif
+
+  if (status == noErr)
+    {
+      status = SecKeychainItemDelete(ref);
+      if (status == noErr) 
+	{
+	  CFRelease(ref);
+	  return Qt;
+	} else
+	signal_keychain_error(status);
+    } else
+    signal_keychain_error(status);
+
+  return Qnil;
+}
+
+void syms_of_keychain()
+{
+  DEFSYMBOL(Qkeychain);
+  DEFSUBR(Fkeychain_find);
+  DEFSUBR(Fkeychain_add);
+  DEFSUBR(Fkeychain_remove);
+}
+
+void vars_of_keychain()
+{
+  Fprovide(Qkeychain);
+}
+
+#ifdef HAVE_SHLIB
+void
+unload_sample()
+{
+}
+#endif /* HAVE_SHLIB */
+
