keyutils historical version 0.1
diff --git a/LICENCE.GPL b/LICENCE.GPL
new file mode 100644
index 0000000..a43ea21
--- /dev/null
+++ b/LICENCE.GPL
@@ -0,0 +1,339 @@
+		    GNU GENERAL PUBLIC LICENSE
+		       Version 2, June 1991
+
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.
+                          675 Mass Ave, Cambridge, MA 02139, USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+			    Preamble
+
+  The licenses for most software are designed to take away your
+freedom to share and change it.  By contrast, the GNU General Public
+License is intended to guarantee your freedom to share and change free
+software--to make sure the software is free for all its users.  This
+General Public License applies to most of the Free Software
+Foundation's software and to any other program whose authors commit to
+using it.  (Some other Free Software Foundation software is covered by
+the GNU Library General Public License instead.)  You can apply it to
+your programs, too.
+
+  When we speak of free software, we are referring to freedom, not
+price.  Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
+
+  To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if you
+distribute copies of the software, or if you modify it.
+
+  For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must give the recipients all the rights that
+you have.  You must make sure that they, too, receive or can get the
+source code.  And you must show them these terms so they know their
+rights.
+
+  We protect your rights with two steps: (1) copyright the software, and
+(2) offer you this license which gives you legal permission to copy,
+distribute and/or modify the software.
+
+  Also, for each author's protection and ours, we want to make certain
+that everyone understands that there is no warranty for this free
+software.  If the software is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original, so
+that any problems introduced by others will not reflect on the original
+authors' reputations.
+
+  Finally, any free program is threatened constantly by software
+patents.  We wish to avoid the danger that redistributors of a free
+program will individually obtain patent licenses, in effect making the
+program proprietary.  To prevent this, we have made it clear that any
+patent must be licensed for everyone's free use or not licensed at all.
+
+  The precise terms and conditions for copying, distribution and
+modification follow.
+
+		    GNU GENERAL PUBLIC LICENSE
+   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+  0. This License applies to any program or other work which contains
+a notice placed by the copyright holder saying it may be distributed
+under the terms of this General Public License.  The "Program", below,
+refers to any such program or work, and a "work based on the Program"
+means either the Program or any derivative work under copyright law:
+that is to say, a work containing the Program or a portion of it,
+either verbatim or with modifications and/or translated into another
+language.  (Hereinafter, translation is included without limitation in
+the term "modification".)  Each licensee is addressed as "you".
+
+Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope.  The act of
+running the Program is not restricted, and the output from the Program
+is covered only if its contents constitute a work based on the
+Program (independent of having been made by running the Program).
+Whether that is true depends on what the Program does.
+
+  1. You may copy and distribute verbatim copies of the Program's
+source code as you receive it, in any medium, provided that you
+conspicuously and appropriately publish on each copy an appropriate
+copyright notice and disclaimer of warranty; keep intact all the
+notices that refer to this License and to the absence of any warranty;
+and give any other recipients of the Program a copy of this License
+along with the Program.
+
+You may charge a fee for the physical act of transferring a copy, and
+you may at your option offer warranty protection in exchange for a fee.
+
+  2. You may modify your copy or copies of the Program or any portion
+of it, thus forming a work based on the Program, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+    a) You must cause the modified files to carry prominent notices
+    stating that you changed the files and the date of any change.
+
+    b) You must cause any work that you distribute or publish, that in
+    whole or in part contains or is derived from the Program or any
+    part thereof, to be licensed as a whole at no charge to all third
+    parties under the terms of this License.
+
+    c) If the modified program normally reads commands interactively
+    when run, you must cause it, when started running for such
+    interactive use in the most ordinary way, to print or display an
+    announcement including an appropriate copyright notice and a
+    notice that there is no warranty (or else, saying that you provide
+    a warranty) and that users may redistribute the program under
+    these conditions, and telling the user how to view a copy of this
+    License.  (Exception: if the Program itself is interactive but
+    does not normally print such an announcement, your work based on
+    the Program is not required to print an announcement.)
+
+These requirements apply to the modified work as a whole.  If
+identifiable sections of that work are not derived from the Program,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works.  But when you
+distribute the same sections as part of a whole which is a work based
+on the Program, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Program.
+
+In addition, mere aggregation of another work not based on the Program
+with the Program (or with a work based on the Program) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+  3. You may copy and distribute the Program (or a work based on it,
+under Section 2) in object code or executable form under the terms of
+Sections 1 and 2 above provided that you also do one of the following:
+
+    a) Accompany it with the complete corresponding machine-readable
+    source code, which must be distributed under the terms of Sections
+    1 and 2 above on a medium customarily used for software interchange; or,
+
+    b) Accompany it with a written offer, valid for at least three
+    years, to give any third party, for a charge no more than your
+    cost of physically performing source distribution, a complete
+    machine-readable copy of the corresponding source code, to be
+    distributed under the terms of Sections 1 and 2 above on a medium
+    customarily used for software interchange; or,
+
+    c) Accompany it with the information you received as to the offer
+    to distribute corresponding source code.  (This alternative is
+    allowed only for noncommercial distribution and only if you
+    received the program in object code or executable form with such
+    an offer, in accord with Subsection b above.)
+
+The source code for a work means the preferred form of the work for
+making modifications to it.  For an executable work, complete source
+code means all the source code for all modules it contains, plus any
+associated interface definition files, plus the scripts used to
+control compilation and installation of the executable.  However, as a
+special exception, the source code distributed need not include
+anything that is normally distributed (in either source or binary
+form) with the major components (compiler, kernel, and so on) of the
+operating system on which the executable runs, unless that component
+itself accompanies the executable.
+
+If distribution of executable or object code is made by offering
+access to copy from a designated place, then offering equivalent
+access to copy the source code from the same place counts as
+distribution of the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+  4. You may not copy, modify, sublicense, or distribute the Program
+except as expressly provided under this License.  Any attempt
+otherwise to copy, modify, sublicense or distribute the Program is
+void, and will automatically terminate your rights under this License.
+However, parties who have received copies, or rights, from you under
+this License will not have their licenses terminated so long as such
+parties remain in full compliance.
+
+  5. You are not required to accept this License, since you have not
+signed it.  However, nothing else grants you permission to modify or
+distribute the Program or its derivative works.  These actions are
+prohibited by law if you do not accept this License.  Therefore, by
+modifying or distributing the Program (or any work based on the
+Program), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Program or works based on it.
+
+  6. Each time you redistribute the Program (or any work based on the
+Program), the recipient automatically receives a license from the
+original licensor to copy, distribute or modify the Program subject to
+these terms and conditions.  You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties to
+this License.
+
+  7. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License.  If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Program at all.  For example, if a patent
+license would not permit royalty-free redistribution of the Program by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Program.
+
+If any portion of this section is held invalid or unenforceable under
+any particular circumstance, the balance of the section is intended to
+apply and the section as a whole is intended to apply in other
+circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system, which is
+implemented by public license practices.  Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+  8. If the distribution and/or use of the Program is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Program under this License
+may add an explicit geographical distribution limitation excluding
+those countries, so that distribution is permitted only in or among
+countries not thus excluded.  In such case, this License incorporates
+the limitation as if written in the body of this License.
+
+  9. The Free Software Foundation may publish revised and/or new versions
+of the General Public License from time to time.  Such new versions will
+be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+Each version is given a distinguishing version number.  If the Program
+specifies a version number of this License which applies to it and "any
+later version", you have the option of following the terms and conditions
+either of that version or of any later version published by the Free
+Software Foundation.  If the Program does not specify a version number of
+this License, you may choose any version ever published by the Free Software
+Foundation.
+
+  10. If you wish to incorporate parts of the Program into other free
+programs whose distribution conditions are different, write to the author
+to ask for permission.  For software which is copyrighted by the Free
+Software Foundation, write to the Free Software Foundation; we sometimes
+make exceptions for this.  Our decision will be guided by the two goals
+of preserving the free status of all derivatives of our free software and
+of promoting the sharing and reuse of software generally.
+
+			    NO WARRANTY
+
+  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
+FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
+OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
+PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
+OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
+TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
+PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
+REPAIR OR CORRECTION.
+
+  12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
+REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
+INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
+OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
+TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
+YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
+PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGES.
+
+		     END OF TERMS AND CONDITIONS
+
+	Appendix: How to Apply These Terms to Your New Programs
+
+  If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+  To do so, attach the following notices to the program.  It is safest
+to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+    <one line to give the program's name and a brief idea of what it does.>
+    Copyright (C) 19yy  <name of author>
+
+    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 2 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, write to the Free Software
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+Also add information on how to contact you by electronic and paper mail.
+
+If the program is interactive, make it output a short notice like this
+when it starts in an interactive mode:
+
+    Gnomovision version 69, Copyright (C) 19yy name of author
+    Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+    This is free software, and you are welcome to redistribute it
+    under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License.  Of course, the commands you use may
+be called something other than `show w' and `show c'; they could even be
+mouse-clicks or menu items--whatever suits your program.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the program, if
+necessary.  Here is a sample; alter the names:
+
+  Yoyodyne, Inc., hereby disclaims all copyright interest in the program
+  `Gnomovision' (which makes passes at compilers) written by James Hacker.
+
+  <signature of Ty Coon>, 1 April 1989
+  Ty Coon, President of Vice
+
+This General Public License does not permit incorporating your program into
+proprietary programs.  If your program is a subroutine library, you may
+consider it more useful to permit linking proprietary applications with the
+library.  If this is what you want to do, use the GNU Library General
+Public License instead of this License.
diff --git a/LICENCE.LGPL b/LICENCE.LGPL
new file mode 100644
index 0000000..8add30a
--- /dev/null
+++ b/LICENCE.LGPL
@@ -0,0 +1,504 @@
+		  GNU LESSER GENERAL PUBLIC LICENSE
+		       Version 2.1, February 1999
+
+ Copyright (C) 1991, 1999 Free Software Foundation, Inc.
+     51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+[This is the first released version of the Lesser GPL.  It also counts
+ as the successor of the GNU Library Public License, version 2, hence
+ the version number 2.1.]
+
+			    Preamble
+
+  The licenses for most software are designed to take away your
+freedom to share and change it.  By contrast, the GNU General Public
+Licenses are intended to guarantee your freedom to share and change
+free software--to make sure the software is free for all its users.
+
+  This license, the Lesser General Public License, applies to some
+specially designated software packages--typically libraries--of the
+Free Software Foundation and other authors who decide to use it.  You
+can use it too, but we suggest you first think carefully about whether
+this license or the ordinary General Public License is the better
+strategy to use in any particular case, based on the explanations below.
+
+  When we speak of free software, we are referring to freedom of use,
+not price.  Our General Public Licenses are designed to make sure that
+you have the freedom to distribute copies of free software (and charge
+for this service if you wish); that you receive source code or can get
+it if you want it; that you can change the software and use pieces of
+it in new free programs; and that you are informed that you can do
+these things.
+
+  To protect your rights, we need to make restrictions that forbid
+distributors to deny you these rights or to ask you to surrender these
+rights.  These restrictions translate to certain responsibilities for
+you if you distribute copies of the library or if you modify it.
+
+  For example, if you distribute copies of the library, whether gratis
+or for a fee, you must give the recipients all the rights that we gave
+you.  You must make sure that they, too, receive or can get the source
+code.  If you link other code with the library, you must provide
+complete object files to the recipients, so that they can relink them
+with the library after making changes to the library and recompiling
+it.  And you must show them these terms so they know their rights.
+
+  We protect your rights with a two-step method: (1) we copyright the
+library, and (2) we offer you this license, which gives you legal
+permission to copy, distribute and/or modify the library.
+
+  To protect each distributor, we want to make it very clear that
+there is no warranty for the free library.  Also, if the library is
+modified by someone else and passed on, the recipients should know
+that what they have is not the original version, so that the original
+author's reputation will not be affected by problems that might be
+introduced by others.
+
+  Finally, software patents pose a constant threat to the existence of
+any free program.  We wish to make sure that a company cannot
+effectively restrict the users of a free program by obtaining a
+restrictive license from a patent holder.  Therefore, we insist that
+any patent license obtained for a version of the library must be
+consistent with the full freedom of use specified in this license.
+
+  Most GNU software, including some libraries, is covered by the
+ordinary GNU General Public License.  This license, the GNU Lesser
+General Public License, applies to certain designated libraries, and
+is quite different from the ordinary General Public License.  We use
+this license for certain libraries in order to permit linking those
+libraries into non-free programs.
+
+  When a program is linked with a library, whether statically or using
+a shared library, the combination of the two is legally speaking a
+combined work, a derivative of the original library.  The ordinary
+General Public License therefore permits such linking only if the
+entire combination fits its criteria of freedom.  The Lesser General
+Public License permits more lax criteria for linking other code with
+the library.
+
+  We call this license the "Lesser" General Public License because it
+does Less to protect the user's freedom than the ordinary General
+Public License.  It also provides other free software developers Less
+of an advantage over competing non-free programs.  These disadvantages
+are the reason we use the ordinary General Public License for many
+libraries.  However, the Lesser license provides advantages in certain
+special circumstances.
+
+  For example, on rare occasions, there may be a special need to
+encourage the widest possible use of a certain library, so that it becomes
+a de-facto standard.  To achieve this, non-free programs must be
+allowed to use the library.  A more frequent case is that a free
+library does the same job as widely used non-free libraries.  In this
+case, there is little to gain by limiting the free library to free
+software only, so we use the Lesser General Public License.
+
+  In other cases, permission to use a particular library in non-free
+programs enables a greater number of people to use a large body of
+free software.  For example, permission to use the GNU C Library in
+non-free programs enables many more people to use the whole GNU
+operating system, as well as its variant, the GNU/Linux operating
+system.
+
+  Although the Lesser General Public License is Less protective of the
+users' freedom, it does ensure that the user of a program that is
+linked with the Library has the freedom and the wherewithal to run
+that program using a modified version of the Library.
+
+  The precise terms and conditions for copying, distribution and
+modification follow.  Pay close attention to the difference between a
+"work based on the library" and a "work that uses the library".  The
+former contains code derived from the library, whereas the latter must
+be combined with the library in order to run.
+
+		  GNU LESSER GENERAL PUBLIC LICENSE
+   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+  0. This License Agreement applies to any software library or other
+program which contains a notice placed by the copyright holder or
+other authorized party saying it may be distributed under the terms of
+this Lesser General Public License (also called "this License").
+Each licensee is addressed as "you".
+
+  A "library" means a collection of software functions and/or data
+prepared so as to be conveniently linked with application programs
+(which use some of those functions and data) to form executables.
+
+  The "Library", below, refers to any such software library or work
+which has been distributed under these terms.  A "work based on the
+Library" means either the Library or any derivative work under
+copyright law: that is to say, a work containing the Library or a
+portion of it, either verbatim or with modifications and/or translated
+straightforwardly into another language.  (Hereinafter, translation is
+included without limitation in the term "modification".)
+
+  "Source code" for a work means the preferred form of the work for
+making modifications to it.  For a library, complete source code means
+all the source code for all modules it contains, plus any associated
+interface definition files, plus the scripts used to control compilation
+and installation of the library.
+
+  Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope.  The act of
+running a program using the Library is not restricted, and output from
+such a program is covered only if its contents constitute a work based
+on the Library (independent of the use of the Library in a tool for
+writing it).  Whether that is true depends on what the Library does
+and what the program that uses the Library does.
+  
+  1. You may copy and distribute verbatim copies of the Library's
+complete source code as you receive it, in any medium, provided that
+you conspicuously and appropriately publish on each copy an
+appropriate copyright notice and disclaimer of warranty; keep intact
+all the notices that refer to this License and to the absence of any
+warranty; and distribute a copy of this License along with the
+Library.
+
+  You may charge a fee for the physical act of transferring a copy,
+and you may at your option offer warranty protection in exchange for a
+fee.
+
+  2. You may modify your copy or copies of the Library or any portion
+of it, thus forming a work based on the Library, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+    a) The modified work must itself be a software library.
+
+    b) You must cause the files modified to carry prominent notices
+    stating that you changed the files and the date of any change.
+
+    c) You must cause the whole of the work to be licensed at no
+    charge to all third parties under the terms of this License.
+
+    d) If a facility in the modified Library refers to a function or a
+    table of data to be supplied by an application program that uses
+    the facility, other than as an argument passed when the facility
+    is invoked, then you must make a good faith effort to ensure that,
+    in the event an application does not supply such function or
+    table, the facility still operates, and performs whatever part of
+    its purpose remains meaningful.
+
+    (For example, a function in a library to compute square roots has
+    a purpose that is entirely well-defined independent of the
+    application.  Therefore, Subsection 2d requires that any
+    application-supplied function or table used by this function must
+    be optional: if the application does not supply it, the square
+    root function must still compute square roots.)
+
+These requirements apply to the modified work as a whole.  If
+identifiable sections of that work are not derived from the Library,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works.  But when you
+distribute the same sections as part of a whole which is a work based
+on the Library, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote
+it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Library.
+
+In addition, mere aggregation of another work not based on the Library
+with the Library (or with a work based on the Library) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+  3. You may opt to apply the terms of the ordinary GNU General Public
+License instead of this License to a given copy of the Library.  To do
+this, you must alter all the notices that refer to this License, so
+that they refer to the ordinary GNU General Public License, version 2,
+instead of to this License.  (If a newer version than version 2 of the
+ordinary GNU General Public License has appeared, then you can specify
+that version instead if you wish.)  Do not make any other change in
+these notices.
+
+  Once this change is made in a given copy, it is irreversible for
+that copy, so the ordinary GNU General Public License applies to all
+subsequent copies and derivative works made from that copy.
+
+  This option is useful when you wish to copy part of the code of
+the Library into a program that is not a library.
+
+  4. You may copy and distribute the Library (or a portion or
+derivative of it, under Section 2) in object code or executable form
+under the terms of Sections 1 and 2 above provided that you accompany
+it with the complete corresponding machine-readable source code, which
+must be distributed under the terms of Sections 1 and 2 above on a
+medium customarily used for software interchange.
+
+  If distribution of object code is made by offering access to copy
+from a designated place, then offering equivalent access to copy the
+source code from the same place satisfies the requirement to
+distribute the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+  5. A program that contains no derivative of any portion of the
+Library, but is designed to work with the Library by being compiled or
+linked with it, is called a "work that uses the Library".  Such a
+work, in isolation, is not a derivative work of the Library, and
+therefore falls outside the scope of this License.
+
+  However, linking a "work that uses the Library" with the Library
+creates an executable that is a derivative of the Library (because it
+contains portions of the Library), rather than a "work that uses the
+library".  The executable is therefore covered by this License.
+Section 6 states terms for distribution of such executables.
+
+  When a "work that uses the Library" uses material from a header file
+that is part of the Library, the object code for the work may be a
+derivative work of the Library even though the source code is not.
+Whether this is true is especially significant if the work can be
+linked without the Library, or if the work is itself a library.  The
+threshold for this to be true is not precisely defined by law.
+
+  If such an object file uses only numerical parameters, data
+structure layouts and accessors, and small macros and small inline
+functions (ten lines or less in length), then the use of the object
+file is unrestricted, regardless of whether it is legally a derivative
+work.  (Executables containing this object code plus portions of the
+Library will still fall under Section 6.)
+
+  Otherwise, if the work is a derivative of the Library, you may
+distribute the object code for the work under the terms of Section 6.
+Any executables containing that work also fall under Section 6,
+whether or not they are linked directly with the Library itself.
+
+  6. As an exception to the Sections above, you may also combine or
+link a "work that uses the Library" with the Library to produce a
+work containing portions of the Library, and distribute that work
+under terms of your choice, provided that the terms permit
+modification of the work for the customer's own use and reverse
+engineering for debugging such modifications.
+
+  You must give prominent notice with each copy of the work that the
+Library is used in it and that the Library and its use are covered by
+this License.  You must supply a copy of this License.  If the work
+during execution displays copyright notices, you must include the
+copyright notice for the Library among them, as well as a reference
+directing the user to the copy of this License.  Also, you must do one
+of these things:
+
+    a) Accompany the work with the complete corresponding
+    machine-readable source code for the Library including whatever
+    changes were used in the work (which must be distributed under
+    Sections 1 and 2 above); and, if the work is an executable linked
+    with the Library, with the complete machine-readable "work that
+    uses the Library", as object code and/or source code, so that the
+    user can modify the Library and then relink to produce a modified
+    executable containing the modified Library.  (It is understood
+    that the user who changes the contents of definitions files in the
+    Library will not necessarily be able to recompile the application
+    to use the modified definitions.)
+
+    b) Use a suitable shared library mechanism for linking with the
+    Library.  A suitable mechanism is one that (1) uses at run time a
+    copy of the library already present on the user's computer system,
+    rather than copying library functions into the executable, and (2)
+    will operate properly with a modified version of the library, if
+    the user installs one, as long as the modified version is
+    interface-compatible with the version that the work was made with.
+
+    c) Accompany the work with a written offer, valid for at
+    least three years, to give the same user the materials
+    specified in Subsection 6a, above, for a charge no more
+    than the cost of performing this distribution.
+
+    d) If distribution of the work is made by offering access to copy
+    from a designated place, offer equivalent access to copy the above
+    specified materials from the same place.
+
+    e) Verify that the user has already received a copy of these
+    materials or that you have already sent this user a copy.
+
+  For an executable, the required form of the "work that uses the
+Library" must include any data and utility programs needed for
+reproducing the executable from it.  However, as a special exception,
+the materials to be distributed need not include anything that is
+normally distributed (in either source or binary form) with the major
+components (compiler, kernel, and so on) of the operating system on
+which the executable runs, unless that component itself accompanies
+the executable.
+
+  It may happen that this requirement contradicts the license
+restrictions of other proprietary libraries that do not normally
+accompany the operating system.  Such a contradiction means you cannot
+use both them and the Library together in an executable that you
+distribute.
+
+  7. You may place library facilities that are a work based on the
+Library side-by-side in a single library together with other library
+facilities not covered by this License, and distribute such a combined
+library, provided that the separate distribution of the work based on
+the Library and of the other library facilities is otherwise
+permitted, and provided that you do these two things:
+
+    a) Accompany the combined library with a copy of the same work
+    based on the Library, uncombined with any other library
+    facilities.  This must be distributed under the terms of the
+    Sections above.
+
+    b) Give prominent notice with the combined library of the fact
+    that part of it is a work based on the Library, and explaining
+    where to find the accompanying uncombined form of the same work.
+
+  8. You may not copy, modify, sublicense, link with, or distribute
+the Library except as expressly provided under this License.  Any
+attempt otherwise to copy, modify, sublicense, link with, or
+distribute the Library is void, and will automatically terminate your
+rights under this License.  However, parties who have received copies,
+or rights, from you under this License will not have their licenses
+terminated so long as such parties remain in full compliance.
+
+  9. You are not required to accept this License, since you have not
+signed it.  However, nothing else grants you permission to modify or
+distribute the Library or its derivative works.  These actions are
+prohibited by law if you do not accept this License.  Therefore, by
+modifying or distributing the Library (or any work based on the
+Library), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Library or works based on it.
+
+  10. Each time you redistribute the Library (or any work based on the
+Library), the recipient automatically receives a license from the
+original licensor to copy, distribute, link with or modify the Library
+subject to these terms and conditions.  You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties with
+this License.
+
+  11. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License.  If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Library at all.  For example, if a patent
+license would not permit royalty-free redistribution of the Library by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Library.
+
+If any portion of this section is held invalid or unenforceable under any
+particular circumstance, the balance of the section is intended to apply,
+and the section as a whole is intended to apply in other circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system which is
+implemented by public license practices.  Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+  12. If the distribution and/or use of the Library is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Library under this License may add
+an explicit geographical distribution limitation excluding those countries,
+so that distribution is permitted only in or among countries not thus
+excluded.  In such case, this License incorporates the limitation as if
+written in the body of this License.
+
+  13. The Free Software Foundation may publish revised and/or new
+versions of the Lesser General Public License from time to time.
+Such new versions will be similar in spirit to the present version,
+but may differ in detail to address new problems or concerns.
+
+Each version is given a distinguishing version number.  If the Library
+specifies a version number of this License which applies to it and
+"any later version", you have the option of following the terms and
+conditions either of that version or of any later version published by
+the Free Software Foundation.  If the Library does not specify a
+license version number, you may choose any version ever published by
+the Free Software Foundation.
+
+  14. If you wish to incorporate parts of the Library into other free
+programs whose distribution conditions are incompatible with these,
+write to the author to ask for permission.  For software which is
+copyrighted by the Free Software Foundation, write to the Free
+Software Foundation; we sometimes make exceptions for this.  Our
+decision will be guided by the two goals of preserving the free status
+of all derivatives of our free software and of promoting the sharing
+and reuse of software generally.
+
+			    NO WARRANTY
+
+  15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
+WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
+EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
+OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
+KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
+LIBRARY IS WITH YOU.  SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
+THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+  16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
+WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
+AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
+FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
+CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
+LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
+RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
+FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
+SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
+DAMAGES.
+
+		     END OF TERMS AND CONDITIONS
+
+           How to Apply These Terms to Your New Libraries
+
+  If you develop a new library, and you want it to be of the greatest
+possible use to the public, we recommend making it free software that
+everyone can redistribute and change.  You can do so by permitting
+redistribution under these terms (or, alternatively, under the terms of the
+ordinary General Public License).
+
+  To apply these terms, attach the following notices to the library.  It is
+safest to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least the
+"copyright" line and a pointer to where the full notice is found.
+
+    <one line to give the library's name and a brief idea of what it does.>
+    Copyright (C) <year>  <name of author>
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library 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
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+Also add information on how to contact you by electronic and paper mail.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the library, if
+necessary.  Here is a sample; alter the names:
+
+  Yoyodyne, Inc., hereby disclaims all copyright interest in the
+  library `Frob' (a library for tweaking knobs) written by James Random Hacker.
+
+  <signature of Ty Coon>, 1 April 1990
+  Ty Coon, President of Vice
+
+That's all there is to it!
+
+
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..c5b8748
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,70 @@
+CFLAGS		:= -g -Wall -O2
+INSTALL		:= install
+DESTDIR		:=
+MAJOR		:= 0
+VERSION		:= $(MAJOR).1
+NO_GLIBC_KEYERR	:= 0
+NO_GLIBC_KEYSYS	:= 0
+BUILDFOR	:= 
+LIBDIR		:= /lib
+
+LNS		:= ln -sf
+
+ifeq ($(NO_GLIBC_KEYERR),1)
+CFLAGS	+= -DNO_GLIBC_KEYERR
+LIBLIBS	:= -ldl -lc
+else
+LIBLIBS	:=
+endif
+
+ifeq ($(NO_GLIBC_KEYSYS),1)
+CFLAGS	+= -DNO_GLIBC_KEYSYS
+endif
+
+ifeq ($(BUILDFOR),32-bit)
+CFLAGS	+= -m32
+LIBDIR	:= /lib
+else
+ifeq ($(BUILDFOR),64-bit)
+CFLAGS	+= -m64
+LIBDIR	:= /lib64
+endif
+endif
+
+all: libkeyutil.so keyctl request-key
+
+
+libkeyutil.so: libkeyutil.so.$(MAJOR)
+	ln -sf $< $@
+
+libkeyutil.so.$(MAJOR): libkeyutil.so.$(VERSION)
+	ln -sf $< $@
+
+libkeyutil.so.$(VERSION): keyutil.c keyutil.h Makefile
+	$(CC) $(CFLAGS) -fPIC $(LDFLAGS) -shared -Wl,-soname,libkeyutil.so.$(MAJOR) -o $@ keyutil.c $(LIBLIBS)
+
+
+keyctl: keyctl.c keyutil.h Makefile
+	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< -L. -lkeyutil -Wl,-rpath,$(LIB)
+
+request-key: request-key.c keyutil.h Makefile
+	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< -L. -lkeyutil -Wl,-rpath,$(LIB)
+
+
+install: all
+	$(INSTALL) -D libkeyutil.so.$(VERSION) $(DESTDIR)/$(LIBDIR)/libkeyutil.so.$(VERSION)
+	$(LNS) libkeyutil.so.$(VERSION) $(DESTDIR)/$(LIBDIR)/libkeyutil.so.$(MAJOR)
+	$(LNS) libkeyutil.so.$(MAJOR) $(DESTDIR)/$(LIBDIR)/libkeyutil.so
+	$(INSTALL) -D keyctl $(DESTDIR)/bin/keyctl
+	$(INSTALL) -D request-key $(DESTDIR)/sbin/request-key
+	$(INSTALL) -D request-key.conf $(DESTDIR)/etc/request-key.conf
+	$(INSTALL) -D request-key-debug.sh $(DESTDIR)/usr/share/keyutils/request-key-debug.sh
+	$(INSTALL) -D keyctl.1 $(DESTDIR)/usr/share/man/man1/keyctl.1
+	$(INSTALL) -D request-key.conf.5 $(DESTDIR)/usr/share/man/man5/request-key.conf.5
+	$(INSTALL) -D request-key.8 $(DESTDIR)/usr/share/man/man8/request-key.8
+	$(INSTALL) -D keyutil.h $(DESTDIR)/usr/include/keyutil.h
+
+clean:
+	$(RM) libkeyutil.so libkeyutil.so.$(MAJOR) libkeyutil.so.$(VERSION)
+	$(RM) keyctl request-key
+	$(RM) *~
diff --git a/README b/README
new file mode 100644
index 0000000..c3e4bc3
--- /dev/null
+++ b/README
@@ -0,0 +1,15 @@
+These tools are used to control the key management system built into the Linux
+kernel.
+
+To build and install the tools and manual pages, run:
+
+	make
+	make install
+
+If your glibc does not contain definitions for the new error codes and system
+calls, then try:
+
+	make NO_GLIBC_KEYERR=1 NO_GLIBC_KEYSYS=1
+
+The tools are licensed under the GPL and the utility library under the LGPL.
+Copies of these are included in this tarball.
diff --git a/keyctl.1 b/keyctl.1
new file mode 100644
index 0000000..1519f97
--- /dev/null
+++ b/keyctl.1
@@ -0,0 +1,529 @@
+.\"
+.\" Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
+.\" Written by David Howells (dhowells@redhat.com)
+.\"
+.\" 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
+.\" 2 of the License, or (at your option) any later version.
+.\"
+.TH KEYCTL 1 "11 July 2005" Linux "Linux Key Management Utilities"
+.SH NAME
+keyctl - Key management facility control
+.SH SYNOPSIS
+\fBkeyctl\fR show
+.br
+\fBkeyctl\fR add <type> <desc> <data> <keyring>
+.br
+\fBkeyctl\fR request <type> <desc> [<dest_keyring>]
+.br
+\fBkeyctl\fR request2 <type> <desc> <info> [<dest_keyring>]
+.br
+\fBkeyctl\fR update <key> <data>
+.br
+\fBkeyctl\fR newring <name> <keyring>
+.br
+\fBkeyctl\fR revoke <key>
+.br
+\fBkeyctl\fR clear <keyring>
+.br
+\fBkeyctl\fR link <key> <keyring>
+.br
+\fBkeyctl\fR unlink <key> <keyring>
+.br
+\fBkeyctl\fR search <keyring> <type> <desc> [<dest_keyring>]
+.br
+\fBkeyctl\fR read <key>
+.br
+\fBkeyctl\fR pipe <key>
+.br
+\fBkeyctl\fR print <key>
+.br
+\fBkeyctl\fR list <keyring>
+.br
+\fBkeyctl\fR rlist <keyring>
+.br
+\fBkeyctl\fR describe <keyring>
+.br
+\fBkeyctl\fR rdescribe <keyring> [sep]
+.br
+\fBkeyctl\fR chown <key> <uid>
+.br
+\fBkeyctl\fR chgrp <key> <gid>
+.br
+\fBkeyctl\fR setperm <key> <mask>
+.br
+\fBkeyctl\fR session
+.br
+\fBkeyctl\fR session - [<prog> <arg1> <arg2> ...]
+.br
+\fBkeyctl\fR session <name> [<prog> <arg1> <arg2> ...]
+.br
+\fBkeyctl\fR instantiate <key> <data> <keyring>
+.br
+\fBkeyctl\fR negate <key> <timeout> <keyring>
+.SH DESCRIPTION
+This program is used to control the key management facility in various ways
+using a variety of subcommands.
+.SH KEY IDENTIFIERS
+.P
+The key identifiers passed to or returned from keyctl are, in general, positive
+integers. There are, however, some special values with special meanings that
+can be passed as arguments:
+.P
+(*) No key: \fB0\fR
+.P
+(*) Thread keyring: \fB@t\fR or \fB-1\fR
+.P
+Each thread may have its own keyring. This is searched first, before all
+others. The thread keyring is replaced by (v)fork, exec and clone.
+.P
+(*) Process keyring: \fB@p\fR or \fB-2\fR
+.P
+Each process (thread group) may have its own keyring. This is shared between
+all members of a group and will be searched after the thread keyring. The
+process keyring is replaced by (v)fork and exec.
+.P
+(*) Session keyring: \fB@s\fR or \fB-3\fR
+.P
+Each process subscribes to a session keyring that is inherited across (v)fork,
+exec and clone. This is searched after the process keyring. Session keyrings
+can be named and an extant keyring can be joined in place of a process's
+current session keyring.
+.P
+(*) User specific keyring: \fB@u\fR or \fB-4\fR
+.P
+This keyring is shared between all the processes owned by a particular user. It
+isn't searched directly, but is normally linked to from the session keyring.
+.P
+(*) User default session keyring: \fB@us\fR or \fB-5\fR
+.P
+This is the default session keyring for a particular user. Login processes that
+change to a particular user will bind to this session until another session is
+set.
+.P
+(*) Group specific keyring: \fB@g\fR or \fB-6\fR
+.P
+This is a place holder for a group specific keyring, but is not actually
+implemented yet in the kernel.
+.SH COMMAND SYNTAX
+Any non-ambiguous shortening of a command name may be used in lieu of the full
+command name. This facility should not be used in scripting as new commands may
+be added in future that then cause ambiguity.
+.P
+(*) \fBShow process keyrings\fR
+.P
+\fBkeyctl show\fR
+.P
+This command recursively shows what keyrings a process is subscribed to and
+what keys and keyrings they contain.
+.P
+(*) \fBAdd a key to a keyring\fR
+.P
+\fBkeyctl add\fR <type> <desc> <data> <keyring>
+.P
+This command creates a key of the specified type and description; instantiates
+it with the given data and attaches it to the specified keyring. It then prints
+the new key's ID on stdout:
+.P
+.RS
+testbox>keyctl add user mykey stuff @u
+.br
+26
+.RE
+.P
+(*) \fBRequest a key\fR
+.P
+\fBkeyctl request\fR <type> <desc> [<dest_keyring>]
+.br
+\fBkeyctl request2\fR <type> <desc> <info> [<dest_keyring>]
+.P
+These two commands request the lookup of a key of the given type and
+description. The process's keyrings will be searched, and if a match is found
+the matching key's ID will be printed to stdout; and if a destination keyring
+is given, the key will be added to that keyring also.
+.P
+If there is no key, the first command will simply return the error ENOKEY and
+fail. The second command will create a partial key with the type and
+description, and call out to \fB/sbin/request-key\fR with that key and the
+extra information supplied above. This will then attempt to instantiate the key
+in some manner, such that a valid key is obtained.
+.P
+If a valid key is obtained, the ID will be printed and the key attached as if
+the original search had succeeded.
+.P
+If there wasn't a valid key obtained, a temporary negative key will be attached
+to the destination keyring if given and the error "Requested key not available"
+will be given.
+.P
+.RS
+testbox>keyctl request2 user debug:hello wibble
+.br
+23
+.br
+testbox>keyctl request user debug:hello
+.br
+23
+.RE
+.P
+(*) \fBUpdate a key\fR
+.P
+\fBkeyctl update\fR <key> <data>
+.P
+This command replaces the data attached to a key with a new set of data. If the
+type of the key doesn't support update then error "Operation not supported"
+will be returned.
+.P
+.RS
+testbox>keyctl update 23 zebra
+.RE
+.P
+(*) \fBCreate a keyring\fR
+.P
+\fBkeyctl newring\fR <name> <keyring>
+.P
+This command creates a new keyring of the specified name and attaches it to the
+specified keyring. The ID of the new keyring will be printed to stdout if
+successful.
+.P
+.RS
+testbox>keyctl newring squelch @us
+.br
+27
+.RE
+.P
+(*) \fBRevoke a key\fR
+.P
+\fBkeyctl revoke\fR <key>
+.P
+This command marks a key as being revoked. Any further operations on that key
+(apart from unlinking it) will return error "Key has been revoked".
+.P
+.RS
+testbox>keyctl revoke 26
+.br
+testbox>keyctl describe 26
+.br
+keyctl_describe: Key has been revoked
+.RE
+.P
+(*) \fBClear a keyring\fR
+.P
+\fBkeyctl clear\fR <keyring>
+.P
+This command unlinks all the keys attached to the specified keyring. Error
+"Not a directory" will be returned if the key specified is not a keyring.
+.P
+.RS
+testbox>keyctl clear 27
+.RE
+.P
+(*) \fBLink a key to a keyring\fR
+.P
+\fBkeyctl link\fR <key> <keyring>
+.P
+This command makes a link from the key to the keyring if there's enough
+capacity to do so. Error "Not a directory" will be returned if the destination
+is not a keyring. Error "Permission denied" will be returned if the key doesn't
+have link permission or the keyring doesn't have write permission. Error "File
+table overflow" will be returned if the keyring is full. Error "Resource
+deadlock avoided" will be returned if an attempt was made to introduce a
+recursive link.
+.P
+.RS
+testbox>keyctl link 23 27
+.br
+testbox>keyctl link 27 27
+.br
+keyctl_link: Resource deadlock avoided
+.RE
+.P
+(*) \fBUnlink a key from a keyring\fR
+.P
+\fBkeyctl unlink\fR <key> <keyring>
+.P
+This command removes a link to the key from the keyring. Error "Not a
+directory" will be returned if the destination is not a keyring. Error
+"Permission denied" will be returned if the keyring doesn't have write
+permission. Error "No such file or directory" will be returned if the key is
+not linked to by the keyring.
+.P
+Note that this only removes one key link from the keyring; any further links to
+the same key are not deleted.
+.P
+.RS
+testbox>keyctl unlink 23 27
+.RE
+.P
+(*) \fBSearch a keyring\fR
+.P
+\fBkeyctl search\fR <keyring> <type> <desc> [<dest_keyring>]
+.P
+This command non-recursively searches a keyring for a key of a particular type
+and description. If found, the ID of the key will be printed on stdout and the
+key will be attached to the destination keyring if present. Error "Requested
+key not available" will be returned if the key is not found.
+.P
+.RS
+testbox>keyctl search @us user debug:hello
+.br
+23
+.br
+pseries>keyctl search @us user debug:bye
+.br
+keyctl_search: Requested key not available
+.RE
+.P
+(*) \fBRead a key\fR
+.P
+\fBkeyctl read\fR <key>
+.br
+\fBkeyctl pipe\fR <key>
+.br
+\fBkeyctl print\fR <key>
+.P
+These commands read the payload of a key. "read" prints it on stdout as a hex
+dump, "pipe" dumps the raw data to stdout and "print" dumps it to stdout
+directly if it's entirely printable or as a hexdump preceded by ":hex:" if not.
+.P
+If the key type does not support reading of the payload, then error "Operation
+not supported" will be returned.
+.P
+.RS
+textbox>keyctl read 26
+.br
+1 bytes of data in key:
+.br
+62
+.br
+testbox>keyctl print 26
+.br
+b
+.br
+testbox>keyctl pipe 26
+.br
+btestbox>
+.RE
+.P
+(*) \fBList a keyring\fR
+.P
+\fBkeyctl list\fR <keyring>
+.br
+\fBkeyctl rlist\fR <keyring>
+.P
+These commands list the contents of a key as a keyring. "list" pretty prints
+the contents and "rlist" just produces a space-separated list of key IDs.
+.P
+No attempt is made to check that the specified keyring is a keyring.
+.P
+.RS
+testbox>keyctl list @us
+.br
+2 keys in keyring:
+.br
+       22: vrwsl----------  4043    -1 keyring: _uid.4043
+.br
+       23: vrwsl----------  4043  4043 user: debug:hello
+.br
+testbox>keyctl rlist @us
+.br
+22 23
+.RE
+.P
+(*) \fBDescribe a key\fR
+.P
+\fBkeyctl describe\fR <keyring>
+.br
+\fBkeyctl rdescribe\fR <keyring> [sep]
+.P
+These commands fetch a description of a keyring. "describe" pretty prints the
+description in the same fashion as the "list" command; "rdescribe" prints the
+raw data returned from the kernel.
+.P
+.RS
+testbox>keyctl describe @us
+       -5: vrwsl----------  4043    -1 keyring: _uid_ses.4043
+testbox>keyctl rdescribe @us
+keyring;4043;-1;1f0000;_uid_ses.4043
+.RE
+.P
+The raw string is "<type>;<uid>;<gid>;<perms>;<description>", where \fIuid\fR
+and \fIgid\fR are the decimal user and group IDs, \fIperms\fR is the
+permissions mask in hex, \fItype\fR and \fIdescription\fR are the type name and
+description strings (neither of which will contain semicolons).
+.P
+(*) \fBChange the access controls on a key\fR
+.P
+\fBkeyctl chown\fR <key> <uid>
+.br
+\fBkeyctl chgrp\fR <key> <gid>
+.P
+These two commands change the UID and GID associated with evaluating a key's
+permissions mask. The UID also governs which quota a key is taken out of.
+.P
+The chown command is not currently supported; attempting it will earn the error
+"Operation not supported" at best.
+.P
+For non-superuser users, the GID may only be set to the process's GID or a GID
+in the process's groups list. The superuser may set any GID it likes.
+.P
+.RS
+testbox>sudo keyctl chown 27 0
+.br
+keyctl_chown: Operation not supported
+.br
+testbox>sudo keyctl chgrp 27 0
+.RE
+.P
+(*) \fBSet the permissions mask on a key\fR
+.P
+\fBkeyctl setperm\fR <key> <mask>
+.P
+This command changes the permission control mask on a key. The mask may be
+specified as a hex number if it begins "0x", an octal number if it begins "0"
+or a decimal number otherwise.
+.P
+The hex numbers are a combination of:
+.P
+.RS
+UID     GID     Other   Permission Granted
+.br
+======  ======  ======  ==================
+.br
+010000  000100  000001  View
+.br
+020000  000200  000002  Read
+.br
+040000  000400  000004  Write
+.br
+080000  000800  000008  Search
+.br
+100000  001000  000010  Link
+.br
+1f0000  001f00  1f0000  All
+.RE
+.P
+\fIView\fR permits the type, description and other parameters of a key to be
+viewed.
+.P
+\fIRead\fR permits the payload (or keyring list) to be read if supported by the
+type.
+.P
+\fIWrite\fR permits the payload (or keyring list) to be modified or updated.
+.P
+\fISearch\fR on a key permits it to be found when a keyring to which it is
+linked is searched.
+.P
+\fILink\fR permits a key to be linked to a keyring.
+.P
+.RS
+testbox>keyctl setperm 27 0x1f1f00
+.RE
+.P
+(*) \fBStart a new session with fresh keyrings\fR
+.P
+\fBkeyctl session\fR
+.br
+\fBkeyctl session\fR - [<prog> <arg1> <arg2> ...]
+.br
+\fBkeyctl session\fR <name> [<prog> <arg1> <arg2> ...]
+.P
+These commands join or create a new keyring and then run a shell or other
+program with that keyring as the session key.
+.P
+The variation with no arguments just creates an anonymous session keyring and
+attaches that as the session keyring; it then exec's $SHELL.
+.P
+The variation with a dash in place of a name creates an anonymous session
+keyring and attaches that as the session keyring; it then exec's the supplied
+command, or $SHELL if one isn't supplied.
+.P
+The variation with a name supplied creates or joins the named keyring and
+attaches that as the session keyring; it then exec's the supplied command, or
+$SHELL if one isn't supplied.
+.P
+.RS
+testbox>keyctl rdescribe @s
+.br
+keyring;4043;-1;1f0000;_uid_ses.4043
+.P
+testbox>keyctl session
+.br
+Joined session keyring: 28
+.br
+testbox>keyctl rdescribe @s
+.br
+keyring;4043;4043;1f0000;_ses.24082
+.P
+testbox>keyctl session -
+.br
+Joined session keyring: 29
+.br
+testbox>keyctl rdescribe @s
+.br
+keyring;4043;4043;1f0000;_ses.24139
+.P
+testbox>keyctl session - keyctl rdescribe @s
+.br
+Joined session keyring: 30
+.br
+keyring;4043;4043;1f0000;_ses.24185
+.P
+testbox>keyctl session fish 
+.br
+Joined session keyring: 34
+.br
+testbox>keyctl rdescribe @s
+.br
+keyring;4043;4043;1f0000;fish
+.P
+testbox>keyctl session fish keyctl rdesc @s
+.br
+Joined session keyring: 35
+.br
+keyring;4043;4043;1f0000;fish
+.RE
+.P
+(*) \fBInstantiate a key\fR
+.P
+\fBkeyctl instantiate\fR <key> <data> <keyring>
+.br
+\fBkeyctl negate\fR <key> <timeout> <keyring>
+.P
+These commands are used to attach data to a partially set up key (as created by
+the kernel and passed to /sbin/request-key). "instantiate" marks a key as being
+valid and attaches the data as the payload. "negate" marks a key as invalid and
+sets a timeout on it so that it'll go away after a while. This prevents a lot
+of quickly sequential requests from slowing the system down overmuch when they
+all fail, as all subsequent requests will then fail with error "Requested key
+not found" until the negative key has expired.
+.P
+The newly instantiated key will be attached to the specified keyring.
+.P
+These commands may only be run from the program run by request-key - a special
+authorisation key is set up by the kernel and attached to the request-key's
+session keyring. This special key is revoked once the key to which it refers
+has been instantiated one way or another.
+.P
+.RS
+keyctl instantiate $1 "Debug $3" $4
+.br
+keyctl negate $1 30 $4
+.RE
+
+.SH ERRORS
+.P
+There are a number of common errors returned by this program:
+.P
+"Not a directory" - a key wasn't a keyring.
+.P
+"Requested key not found" - the looked for key isn't available.
+.P
+"Key has been revoked" - a revoked key was accessed.
+.P
+"Key has expired" - an expired key was accessed.
+.P
+"Permission denied" - permission was denied by a UID/GID/mask combination.
+
+.SH SEE ALSO
+\fBkeyctl\fR(1), \fBrequest-key.conf\fR(5)
diff --git a/keyctl.c b/keyctl.c
new file mode 100644
index 0000000..f3e54bc
--- /dev/null
+++ b/keyctl.c
@@ -0,0 +1,1127 @@
+/* keyctl.c: key control program
+ *
+ * Copyright (C) 2005 Red Hat, Inc. All Rights Reserved.
+ * Written by David Howells (dhowells@redhat.com)
+ *
+ * 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
+ * 2 of the License, or (at your option) any later version.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdarg.h>
+#include <string.h>
+#include <unistd.h>
+#include <ctype.h>
+#include <errno.h>
+#include <asm/unistd.h>
+#include "keyutil.h"
+
+struct command {
+	int (*action)(int argc, char *argv[]);
+	const char	*name;
+	const char	*format;
+};
+
+static int act_keyctl_show(int argc, char *argv[]);
+static int act_keyctl_add(int argc, char *argv[]);
+static int act_keyctl_request(int argc, char *argv[]);
+static int act_keyctl_request2(int argc, char *argv[]);
+static int act_keyctl_update(int argc, char *argv[]);
+static int act_keyctl_newring(int argc, char *argv[]);
+static int act_keyctl_revoke(int argc, char *argv[]);
+static int act_keyctl_clear(int argc, char *argv[]);
+static int act_keyctl_link(int argc, char *argv[]);
+static int act_keyctl_unlink(int argc, char *argv[]);
+static int act_keyctl_search(int argc, char *argv[]);
+static int act_keyctl_read(int argc, char *argv[]);
+static int act_keyctl_pipe(int argc, char *argv[]);
+static int act_keyctl_print(int argc, char *argv[]);
+static int act_keyctl_list(int argc, char *argv[]);
+static int act_keyctl_rlist(int argc, char *argv[]);
+static int act_keyctl_describe(int argc, char *argv[]);
+static int act_keyctl_rdescribe(int argc, char *argv[]);
+static int act_keyctl_chown(int argc, char *argv[]);
+static int act_keyctl_chgrp(int argc, char *argv[]);
+static int act_keyctl_setperm(int argc, char *argv[]);
+static int act_keyctl_session(int argc, char *argv[]);
+static int act_keyctl_instantiate(int argc, char *argv[]);
+static int act_keyctl_negate(int argc, char *argv[]);
+
+const struct command commands[] = {
+	{ act_keyctl_show,	"show",		"" },
+	{ act_keyctl_add,	"add",		"<type> <desc> <data> <keyring>" },
+	{ act_keyctl_request,	"request",	"<type> <desc> [<dest_keyring>]" },
+	{ act_keyctl_request2,	"request2",	"<type> <desc> <info> [<dest_keyring>]" },
+	{ act_keyctl_update,	"update",	"<key> <data>" },
+	{ act_keyctl_newring,	"newring",	"<name> <keyring>" },
+	{ act_keyctl_revoke,	"revoke",	"<key>" },
+	{ act_keyctl_clear,	"clear",	"<keyring>" },
+	{ act_keyctl_link,	"link",		"<key> <keyring>" },
+	{ act_keyctl_unlink,	"unlink",	"<key> <keyring>" },
+	{ act_keyctl_search,	"search",	"<keyring> <type> <desc> [<dest_keyring>]" },
+	{ act_keyctl_read,	"read",		"<key>" },
+	{ act_keyctl_pipe,	"pipe",		"<key>" },
+	{ act_keyctl_print,	"print",	"<key>" },
+	{ act_keyctl_list,	"list",		"<keyring>" },
+	{ act_keyctl_rlist,	"rlist",	"<keyring>" },
+	{ act_keyctl_describe,	"describe",	"<keyring>" },
+	{ act_keyctl_rdescribe,	"rdescribe",	"<keyring> [sep]" },
+	{ act_keyctl_chown,	"chown",	"<key> <uid>" },
+	{ act_keyctl_chgrp,	"chgrp",	"<key> <gid>" },
+	{ act_keyctl_setperm,	"setperm",	"<key> <mask>" },
+	{ act_keyctl_session,	"session",	"" },
+	{ act_keyctl_session,	"session",	"- [<prog> <arg1> <arg2> ...]" },
+	{ act_keyctl_session,	"session",	"<name> [<prog> <arg1> <arg2> ...]" },
+	{ act_keyctl_instantiate, "instantiate","<key> <data> <keyring>" },
+	{ act_keyctl_negate,	"negate",	"<key> <timeout> <keyring>" },
+	{ NULL, NULL, NULL }
+};
+
+static int dump_key_tree(key_serial_t keyring, const char *name);
+static void format(void) __attribute__((noreturn));
+static void error(const char *msg) __attribute__((noreturn));
+static key_serial_t get_key_id(const char *arg);
+
+/*****************************************************************************/
+/*
+ * execute the appropriate subcommand
+ */
+int main(int argc, char *argv[])
+{
+	const struct command *cmd, *best;
+	int n;
+
+	argv++;
+	argc--;
+
+	if (argc == 0)
+		format();
+
+	/* find the best fit command */
+	best = NULL;
+	n = strlen(*argv);
+
+	for (cmd = commands; cmd->action; cmd++) {
+		if (memcmp(cmd->name, *argv, n) != 0)
+			continue;
+
+		if (cmd->name[n] == 0) {
+			/* exact match */
+			best = cmd;
+			break;
+		}
+
+		/* partial match */
+		if (best) {
+			fprintf(stderr, "Ambiguous command\n");
+			exit(2);
+		}
+
+		best = cmd;
+	}
+
+	if (!best) {
+		fprintf(stderr, "Unknown command\n");
+		exit(2);
+	}
+
+	return best->action(argc, argv);
+
+} /* end main() */
+
+/*****************************************************************************/
+/*
+ * display command format information
+ */
+static void format(void)
+{
+	const struct command *cmd;
+
+	fprintf(stderr, "Format:\n");
+
+	for (cmd = commands; cmd->action; cmd++)
+		fprintf(stderr, "  keyctl %s %s\n", cmd->name, cmd->format);
+
+	fprintf(stderr, "\n");
+	fprintf(stderr, "Key/keyring ID:\n");
+	fprintf(stderr, "  <nnn>   numeric keyring ID\n");
+	fprintf(stderr, "  @t      thread keyring\n");
+	fprintf(stderr, "  @p      process keyring\n");
+	fprintf(stderr, "  @s      session keyring\n");
+	fprintf(stderr, "  @u      user keyring\n");
+	fprintf(stderr, "  @us     user default session keyring\n");
+	fprintf(stderr, "  @g      group keyring\n");
+	fprintf(stderr, "\n");
+	fprintf(stderr, "<type> can be \"user\" for a user-defined keyring\n");
+	fprintf(stderr, "If you do this, prefix the description with \"<subtype>:\"\n");
+
+	exit(2);
+
+} /* end format() */
+
+/*****************************************************************************/
+/*
+ * handle an error
+ */
+static inline void error(const char *msg)
+{
+	perror(msg);
+	exit(1);
+
+} /* end error() */
+
+/*****************************************************************************/
+/*
+ * show the parent process's session keyring
+ */
+static int act_keyctl_show(int argc, char *argv[])
+{
+	if (argc != 1)
+		format();
+
+	dump_key_tree(KEY_SPEC_SESSION_KEYRING, "Session Keyring");
+	return 0;
+
+} /* end act_keyctl_show() */
+
+/*****************************************************************************/
+/*
+ * add a key
+ */
+static int act_keyctl_add(int argc, char *argv[])
+{
+	key_serial_t dest;
+	int ret;
+
+	if (argc != 5)
+		format();
+
+	dest = get_key_id(argv[4]);
+
+	ret = add_key(argv[1], argv[2], argv[3], strlen(argv[3]), dest);
+	if (ret < 0)
+		error("add_key");
+
+	/* print the resulting key ID */
+	printf("%d\n", ret);
+	return 0;
+
+} /* end act_keyctl_add() */
+
+/*****************************************************************************/
+/*
+ * request a key
+ */
+static int act_keyctl_request(int argc, char *argv[])
+{
+	key_serial_t dest;
+	int ret;
+
+	if (argc != 3 && argc != 4)
+		format();
+
+	dest = 0;
+	if (argc == 4)
+		dest = get_key_id(argv[3]);
+
+	ret = request_key(argv[1], argv[2], NULL, dest);
+	if (ret < 0)
+		error("request_key");
+
+	/* print the resulting key ID */
+	printf("%d\n", ret);
+	return 0;
+
+} /* end act_keyctl_request() */
+
+/*****************************************************************************/
+/*
+ * request a key, with recourse to /sbin/request-key
+ */
+static int act_keyctl_request2(int argc, char *argv[])
+{
+	key_serial_t dest;
+	int ret;
+
+	if (argc != 4 && argc != 5)
+		format();
+
+	dest = 0;
+	if (argc == 5)
+		dest = get_key_id(argv[4]);
+
+	ret = request_key(argv[1], argv[2], argv[3], dest);
+	if (ret < 0)
+		error("request_key");
+
+	/* print the resulting key ID */
+	printf("%d\n", ret);
+	return 0;
+
+} /* end act_keyctl_request2() */
+
+/*****************************************************************************/
+/*
+ * update a key
+ */
+static int act_keyctl_update(int argc, char *argv[])
+{
+	key_serial_t key;
+
+	if (argc != 3)
+		format();
+
+	key = get_key_id(argv[1]);
+
+	if (keyctl_update(key, argv[2], strlen(argv[2])) < 0)
+		error("keyctl_update");
+
+	return 0;
+
+} /* end act_keyctl_update() */
+
+/*****************************************************************************/
+/*
+ * create a new keyring
+ */
+static int act_keyctl_newring(int argc, char *argv[])
+{
+	key_serial_t dest;
+	int ret;
+
+	if (argc != 3)
+		format();
+
+	dest = get_key_id(argv[2]);
+
+	ret = add_key("keyring", argv[1], NULL, 0, dest);
+	if (ret < 0)
+		error("add_key");
+
+	printf("%d\n", ret);
+	return 0;
+
+} /* end act_keyctl_newring() */
+
+/*****************************************************************************/
+/*
+ * revoke a key
+ */
+static int act_keyctl_revoke(int argc, char *argv[])
+{
+	key_serial_t key;
+
+	if (argc != 2)
+		format();
+
+	key = get_key_id(argv[1]);
+
+	if (keyctl_revoke(key) < 0)
+		error("keyctl_revoke");
+
+	return 0;
+
+} /* end act_keyctl_revoke() */
+
+/*****************************************************************************/
+/*
+ * clear a keyring
+ */
+static int act_keyctl_clear(int argc, char *argv[])
+{
+	key_serial_t keyring;
+
+	if (argc != 2)
+		format();
+
+	keyring = get_key_id(argv[1]);
+
+	if (keyctl_clear(keyring) < 0)
+		error("keyctl_clear");
+
+	return 0;
+
+} /* end act_keyctl_clear() */
+
+/*****************************************************************************/
+/*
+ * link a key to a keyring
+ */
+static int act_keyctl_link(int argc, char *argv[])
+{
+	key_serial_t keyring, key;
+
+	if (argc != 3)
+		format();
+
+	key = get_key_id(argv[1]);
+	keyring = get_key_id(argv[2]);
+
+	if (keyctl_link(key, keyring) < 0)
+		error("keyctl_link");
+
+	return 0;
+
+} /* end act_keyctl_link() */
+
+/*****************************************************************************/
+/*
+ * unlink a key from a keyrign
+ */
+static int act_keyctl_unlink(int argc, char *argv[])
+{
+	key_serial_t keyring, key;
+
+	if (argc != 3)
+		format();
+
+	key = get_key_id(argv[1]);
+	keyring = get_key_id(argv[2]);
+
+	if (keyctl_unlink(key, keyring) < 0)
+		error("keyctl_unlink");
+
+	return 0;
+
+} /* end act_keyctl_unlink() */
+
+/*****************************************************************************/
+/*
+ * search a keyring for a key
+ */
+static int act_keyctl_search(int argc, char *argv[])
+{
+	key_serial_t keyring, dest;
+	int ret;
+
+	if (argc != 4 && argc != 5)
+		format();
+
+	keyring = get_key_id(argv[1]);
+
+	dest = 0;
+	if (argc == 5)
+		dest = get_key_id(argv[4]);
+
+	ret = keyctl_search(keyring, argv[2], argv[3], dest);
+	if (ret < 0)
+		error("keyctl_search");
+
+	/* print the ID of the key we found */
+	printf("%d\n", ret);
+	return 0;
+
+} /* end act_keyctl_search() */
+
+/*****************************************************************************/
+/*
+ * read a key
+ */
+static int act_keyctl_read(int argc, char *argv[])
+{
+	key_serial_t key;
+	void *buffer;
+	char *p;
+	int ret, sep, col;
+
+	if (argc != 2)
+		format();
+
+	key = get_key_id(argv[1]);
+
+	/* read the key payload data */
+	ret = keyctl_read_alloc(key, &buffer);
+	if (ret < 0)
+		error("keyctl_read_alloc");
+
+	if (ret == 0) {
+		printf("No data in key\n");
+		return 0;
+	}
+
+	/* hexdump the contents */
+	printf("%u bytes of data in key:\n", ret);
+
+	sep = 0;
+	col = 0;
+	p = buffer;
+
+	do {
+		if (sep) {
+			putchar(sep);
+			sep = 0;
+		}
+
+		printf("%02hhx", *p);
+		p++;
+
+		col++;
+		if (col % 32 == 0)
+			sep = '\n';
+		else if (col % 4 == 0)
+			sep = ' ';
+
+	} while (--ret > 0);
+
+	printf("\n");
+	return 0;
+
+} /* end act_keyctl_read() */
+
+/*****************************************************************************/
+/*
+ * read a key and dump raw to stdout
+ */
+static int act_keyctl_pipe(int argc, char *argv[])
+{
+	key_serial_t key;
+	void *buffer;
+	int ret;
+
+	if (argc != 2)
+		format();
+
+	key = get_key_id(argv[1]);
+
+	/* read the key payload data */
+	ret = keyctl_read_alloc(key, &buffer);
+	if (ret < 0)
+		error("keyctl_read_alloc");
+
+	if (ret > 0)
+		write(1, buffer, ret);
+	return 0;
+
+} /* end act_keyctl_pipe() */
+
+/*****************************************************************************/
+/*
+ * read a key and dump to stdout in printable form
+ */
+static int act_keyctl_print(int argc, char *argv[])
+{
+	key_serial_t key;
+	void *buffer;
+	char *p;
+	int loop, ret;
+
+	if (argc != 2)
+		format();
+
+	key = get_key_id(argv[1]);
+
+	/* read the key payload data */
+	ret = keyctl_read_alloc(key, &buffer);
+	if (ret < 0)
+		error("keyctl_read_alloc");
+
+	/* see if it's printable */
+	p = buffer;
+	for (loop = ret; loop > 0; loop--, p++)
+		if (!isprint(*p))
+			goto not_printable;
+
+	/* it is */
+	printf("%s\n", (char *) buffer);
+	return 0;
+
+not_printable:
+	/* it isn't */
+	printf(":hex:");
+	p = buffer;
+	for (loop = ret; loop > 0; loop--, p++)
+		printf("%02hhx", *p);
+	printf("\n");
+	return 0;
+
+} /* end act_keyctl_print() */
+
+/*****************************************************************************/
+/*
+ * list a keyring
+ */
+static int act_keyctl_list(int argc, char *argv[])
+{
+	key_serial_t keyring, key, *pk;
+	key_perm_t perm;
+	void *keylist;
+	char *buffer;
+	uid_t uid;
+	gid_t gid;
+	int count, tlen, dpos, dlen, ret;
+
+	if (argc != 2)
+		format();
+
+	keyring = get_key_id(argv[1]);
+
+	/* read the key payload data */
+	count = keyctl_read_alloc(keyring, &keylist);
+	if (count < 0)
+		error("keyctl_read_alloc");
+
+	count /= sizeof(key_serial_t);
+
+	if (count == 0) {
+		printf("keyring is empty\n");
+		return 0;
+	}
+
+	/* list the keys in the keyring */
+	if (count == 1)
+		printf("1 key in keyring:\n");
+	else
+		printf("%u keys in keyring:\n", count);
+
+	pk = keylist;
+	do {
+		key = *pk++;
+
+		ret = keyctl_describe_alloc(key, &buffer);
+		if (ret < 0) {
+			printf("%9d: key inaccessible (%m)\n", key);
+			continue;
+		}
+
+		uid = 0;
+		gid = 0;
+		perm = 0;
+
+		tlen = -1;
+		dpos = -1;
+		dlen = -1;
+
+		sscanf((char *) buffer, "%*[^;]%n;%d;%d;%x;%n%*[^;]%n",
+		       &tlen, &uid, &gid, &perm, &dpos, &dlen);
+		if (dlen == -1) {
+			fprintf(stderr, "Unparseable description obtained for key %d\n", key);
+			exit(3);
+		}
+
+		printf("%9d: %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c %5d %5d %*.*s: %s\n",
+		       key,
+		       perm & KEY_USR_VIEW	? 'v' : '-',
+		       perm & KEY_USR_READ	? 'r' : '-',
+		       perm & KEY_USR_WRITE	? 'w' : '-',
+		       perm & KEY_USR_SEARCH	? 's' : '-',
+		       perm & KEY_USR_LINK	? 'l' : '-',
+		       perm & KEY_GRP_VIEW	? 'v' : '-',
+		       perm & KEY_GRP_READ	? 'r' : '-',
+		       perm & KEY_GRP_WRITE	? 'w' : '-',
+		       perm & KEY_GRP_SEARCH	? 's' : '-',
+		       perm & KEY_GRP_LINK	? 'l' : '-',
+		       perm & KEY_OTH_VIEW	? 'v' : '-',
+		       perm & KEY_OTH_READ	? 'r' : '-',
+		       perm & KEY_OTH_WRITE	? 'w' : '-',
+		       perm & KEY_OTH_SEARCH	? 's' : '-',
+		       perm & KEY_OTH_LINK	? 'l' : '-',
+		       uid, gid,
+		       tlen, tlen, buffer,
+		       buffer + dpos);
+
+		free(buffer);
+
+	} while (--count);
+
+	return 0;
+
+} /* end act_keyctl_list() */
+
+/*****************************************************************************/
+/*
+ * produce a raw list of a keyring
+ */
+static int act_keyctl_rlist(int argc, char *argv[])
+{
+	key_serial_t keyring, key, *pk;
+	void *keylist;
+	int count;
+
+	if (argc != 2)
+		format();
+
+	keyring = get_key_id(argv[1]);
+
+	/* read the key payload data */
+	count = keyctl_read_alloc(keyring, &keylist);
+	if (count < 0)
+		error("keyctl_read_alloc");
+
+	count /= sizeof(key_serial_t);
+
+	/* list the keys in the keyring */
+	pk = keylist;
+	do {
+		key = *pk++;
+		printf("%d%c", key, count == 1 ? '\n' : ' ');
+	} while (--count);
+
+	return 0;
+
+} /* end act_keyctl_rlist() */
+
+/*****************************************************************************/
+/*
+ * describe a key
+ */
+static int act_keyctl_describe(int argc, char *argv[])
+{
+	key_serial_t key;
+	key_perm_t perm;
+	char *buffer;
+	uid_t uid;
+	gid_t gid;
+	int tlen, dpos, dlen, ret;
+
+	if (argc != 2)
+		format();
+
+	key = get_key_id(argv[1]);
+
+	/* get key description */
+	ret = keyctl_describe_alloc(key, &buffer);
+	if (ret < 0)
+		error("keyctl_describe");
+
+	/* parse it */
+	uid = 0;
+	gid = 0;
+	perm = 0;
+
+	tlen = -1;
+	dpos = -1;
+	dlen = -1;
+
+	sscanf(buffer, "%*[^;]%n;%d;%d;%x;%n%*[^;]%n",
+	       &tlen, &uid, &gid, &perm, &dpos, &dlen);
+	if (dlen == -1) {
+		fprintf(stderr, "Unparseable description obtained for key %d\n", key);
+		exit(3);
+	}
+
+	/* display it */
+	printf("%9d: %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c %5d %5d %*.*s: %s\n",
+	       key,
+	       perm & KEY_USR_VIEW	? 'v' : '-',
+	       perm & KEY_USR_READ	? 'r' : '-',
+	       perm & KEY_USR_WRITE	? 'w' : '-',
+	       perm & KEY_USR_SEARCH	? 's' : '-',
+	       perm & KEY_USR_LINK	? 'l' : '-',
+	       perm & KEY_GRP_VIEW	? 'v' : '-',
+	       perm & KEY_GRP_READ	? 'r' : '-',
+	       perm & KEY_GRP_WRITE	? 'w' : '-',
+	       perm & KEY_GRP_SEARCH	? 's' : '-',
+	       perm & KEY_GRP_LINK	? 'l' : '-',
+	       perm & KEY_OTH_VIEW	? 'v' : '-',
+	       perm & KEY_OTH_READ	? 'r' : '-',
+	       perm & KEY_OTH_WRITE	? 'w' : '-',
+	       perm & KEY_OTH_SEARCH	? 's' : '-',
+	       perm & KEY_OTH_LINK	? 'l' : '-',
+	       uid, gid,
+	       tlen, tlen, buffer,
+	       buffer + dpos);
+
+	return 0;
+
+} /* end act_keyctl_describe() */
+
+/*****************************************************************************/
+/*
+ * get raw key description
+ */
+static int act_keyctl_rdescribe(int argc, char *argv[])
+{
+	key_serial_t key;
+	char *buffer, *q;
+	int ret;
+
+	if (argc != 2 && argc != 3)
+		format();
+	if (argc == 3 && !argv[2][0])
+		format();
+
+	key = get_key_id(argv[1]);
+
+	/* get key description */
+	ret = keyctl_describe_alloc(key, &buffer);
+	if (ret < 0)
+		error("keyctl_describe");
+
+	/* replace semicolon separators with requested alternative */
+	if (argc == 3) {
+		for (q = buffer; *q; q++)
+			if (*q == ';')
+				*q = argv[2][0];
+	}
+
+	/* display raw description */
+	printf("%s\n", buffer);
+	return 0;
+
+} /* end act_keyctl_rdescribe() */
+
+/*****************************************************************************/
+/*
+ * change a key's ownership
+ */
+static int act_keyctl_chown(int argc, char *argv[])
+{
+	key_serial_t key;
+	uid_t uid;
+	char *q;
+
+	if (argc != 3)
+		format();
+
+	key = get_key_id(argv[1]);
+
+	uid = strtoul(argv[2], &q, 0);
+	if (*q) {
+		fprintf(stderr, "Unparsable uid: '%s'\n", argv[2]);
+		exit(2);
+	}
+
+	if (keyctl_chown(key, uid, -1) < 0)
+		error("keyctl_chown");
+
+	return 0;
+
+} /* end act_keyctl_chown() */
+
+/*****************************************************************************/
+/*
+ * change a key's group ownership
+ */
+static int act_keyctl_chgrp(int argc, char *argv[])
+{
+	key_serial_t key;
+	gid_t gid;
+	char *q;
+
+	if (argc != 3)
+		format();
+
+	key = get_key_id(argv[1]);
+
+	gid = strtoul(argv[2], &q, 0);
+	if (*q) {
+		fprintf(stderr, "Unparsable gid: '%s'\n", argv[2]);
+		exit(2);
+	}
+
+	if (keyctl_chown(key, -1, gid) < 0)
+		error("keyctl_chown");
+
+	return 0;
+
+} /* end act_keyctl_chgrp() */
+
+/*****************************************************************************/
+/*
+ * set the permissions on a key
+ */
+static int act_keyctl_setperm(int argc, char *argv[])
+{
+	key_serial_t key;
+	key_perm_t perm;
+	char *q;
+
+	if (argc != 3)
+		format();
+
+	key = get_key_id(argv[1]);
+	perm = strtoul(argv[2], &q, 0);
+	if (*q) {
+		fprintf(stderr, "Unparsable permissions: '%s'\n", argv[2]);
+		exit(2);
+	}
+
+	if (keyctl_setperm(key, perm) < 0)
+		error("keyctl_setperm");
+
+	return 0;
+
+} /* end act_keyctl_setperm() */
+
+/*****************************************************************************/
+/*
+ * start a process in a new session
+ */
+static int act_keyctl_session(int argc, char *argv[])
+{
+	char *p, *q;
+	int ret;
+
+	argv++;
+	argc--;
+
+	/* no extra arguments signifies a standard shell in an anonymous
+	 * session */
+	p = NULL;
+	if (argc != 0) {
+		/* a dash signifies an anonymous session */
+		p = *argv;
+		if (strcmp(p, "-") == 0)
+			p = NULL;
+
+		argv++;
+		argc--;
+	}
+
+	/* create a new session keyring */
+	ret = keyctl_join_session_keyring(p);
+	if (ret < 0)
+		error("keyctl_join_session_keyring");
+
+	fprintf(stderr, "Joined session keyring: %d\n", ret);
+
+	/* run the standard shell if no arguments */
+	if (argc == 0) {
+		q = getenv("SHELL");
+		if (!q)
+			q = "/bin/sh";
+		execl(q, q, NULL);
+		error(q);
+	}
+
+	/* run the command specified */
+	execvp(argv[0], argv);
+	error(argv[0]);
+
+} /* end act_keyctl_session() */
+
+/*****************************************************************************/
+/*
+ * instantiate a key that's under construction
+ */
+static int act_keyctl_instantiate(int argc, char *argv[])
+{
+	key_serial_t key, dest;
+
+	if (argc != 4)
+		format();
+
+	key = get_key_id(argv[1]);
+	dest = get_key_id(argv[3]);
+
+	if (keyctl_instantiate(key, argv[2], strlen(argv[2]), dest) < 0)
+		error("keyctl_instantiate");
+
+	return 0;
+
+} /* end act_keyctl_instantiate() */
+
+/*****************************************************************************/
+/*
+ * negate a key that's under construction
+ */
+static int act_keyctl_negate(int argc, char *argv[])
+{
+	unsigned long timeout;
+	key_serial_t key, dest;
+	char *q;
+
+	if (argc != 4)
+		format();
+
+	key = get_key_id(argv[1]);
+
+	timeout = strtoul(argv[2], &q, 10);
+	if (*q) {
+		fprintf(stderr, "Unparsable timeout: '%s'\n", argv[2]);
+		exit(2);
+	}
+
+	dest = get_key_id(argv[3]);
+
+	if (keyctl_negate(key, timeout, dest) < 0)
+		error("keyctl_negate");
+
+	return 0;
+
+} /* end act_keyctl_negate() */
+
+/*****************************************************************************/
+/*
+ * parse a key identifier
+ */
+static key_serial_t get_key_id(const char *arg)
+{
+	key_serial_t id;
+	char *end;
+
+	/* handle a special keyring name */
+	if (arg[0] == '@') {
+		if (strcmp(arg, "@t" ) == 0) return KEY_SPEC_THREAD_KEYRING;
+		if (strcmp(arg, "@p" ) == 0) return KEY_SPEC_PROCESS_KEYRING;
+		if (strcmp(arg, "@s" ) == 0) return KEY_SPEC_SESSION_KEYRING;
+		if (strcmp(arg, "@u" ) == 0) return KEY_SPEC_USER_KEYRING;
+		if (strcmp(arg, "@us") == 0) return KEY_SPEC_USER_SESSION_KEYRING;
+		if (strcmp(arg, "@g" ) == 0) return KEY_SPEC_GROUP_KEYRING;
+
+		fprintf(stderr, "Unknown special key: '%s'\n", arg);
+		exit(2);
+	}
+
+	/* handle a numeric key ID */
+	id = strtoul(arg, &end, 0);
+	if (*end) {
+		fprintf(stderr, "Unparsable key: '%s'\n", arg);
+		exit(2);
+	}
+
+	return id;
+
+} /* end get_key_id() */
+
+/*****************************************************************************/
+/*
+ * recursively display a key/keyring tree
+ */
+static int dump_key_tree_aux(key_serial_t key, int depth, int more)
+{
+	static char dumpindent[64];
+	key_serial_t *pk;
+	key_perm_t perm;
+	size_t ringlen, desclen;
+	void *payload;
+	char *desc, type[255];
+	int uid, gid, ret, n, rdepth, kcount = 0;
+
+	if (depth > 8)
+		return 0;
+
+	/* find out how big this key's description is */
+	ret = keyctl_describe(key, NULL, 0);
+	if (ret < 0) {
+		printf("%d: key inaccessible (%m)\n", key);
+		return 0;
+	}
+	desclen = ret + 1;
+
+	desc = malloc(desclen);
+	if (!desc)
+		error("malloc");
+
+	/* read the description */
+	ret = keyctl_describe(key, desc, desclen);
+	if (ret < 0) {
+		printf("%d: key inaccessible (%m)\n", key);
+		free(desc);
+		return 0;
+	}
+
+	desclen = ret < desclen ? ret : desclen;
+
+	desc[desclen] = 0;
+
+	/* parse */
+	type[0] = 0;
+	uid = 0;
+	gid = 0;
+	perm = 0;
+	sscanf(desc, "%[^;];%d;%d;%x;%n",
+	       type, &uid, &gid, &perm, &n);
+
+	/* and print */
+	printf("%9d"
+	       " %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c %5d %5d "
+	       " %s%s%s: %s\n"
+	       ,
+	       key,
+	       perm & KEY_USR_LINK	? 'l' : '-',
+	       perm & KEY_USR_SEARCH	? 's' : '-',
+	       perm & KEY_USR_WRITE	? 'w' : '-',
+	       perm & KEY_USR_READ	? 'r' : '-',
+	       perm & KEY_USR_VIEW	? 'v' : '-',
+	       perm & KEY_GRP_LINK	? 'l' : '-',
+	       perm & KEY_GRP_SEARCH	? 's' : '-',
+	       perm & KEY_GRP_WRITE	? 'w' : '-',
+	       perm & KEY_GRP_READ	? 'r' : '-',
+	       perm & KEY_GRP_VIEW	? 'v' : '-',
+	       perm & KEY_OTH_LINK	? 'l' : '-',
+	       perm & KEY_OTH_SEARCH	? 's' : '-',
+	       perm & KEY_OTH_WRITE	? 'w' : '-',
+	       perm & KEY_OTH_READ	? 'r' : '-',
+	       perm & KEY_OTH_VIEW	? 'v' : '-',
+	       uid, gid,
+	       dumpindent,
+	       depth > 0 ? "\\_ " : "",
+	       type, desc + n);
+
+	/* if it's a keyring then we're going to want to recursively
+	 * display it if we can */
+	if (strcmp(type, "keyring") == 0) {
+		/* find out how big the keyring is */
+		ret = keyctl_read(key, NULL, 0);
+		if (ret < 0)
+			error("keyctl_read");
+		if (ret == 0)
+			return 0;
+		ringlen = ret;
+
+		/* read its contents */
+		payload = malloc(ringlen);
+		if (!payload)
+			error("malloc");
+
+		ret = keyctl_read(key, payload, ringlen);
+		if (ret < 0)
+			error("keyctl_read");
+
+		ringlen = ret < ringlen ? ret : ringlen;
+		kcount = ringlen / sizeof(key_serial_t);
+
+		/* walk the keyring */
+		pk = payload;
+		do {
+			key = *pk++;
+
+			/* recurse into nexted keyrings */
+			if (strcmp(type, "keyring") == 0) {
+				if (depth == 0) {
+					rdepth = depth;
+					dumpindent[rdepth++] = ' ';
+					dumpindent[rdepth] = 0;
+				}
+				else {
+					rdepth = depth;
+					dumpindent[rdepth++] = ' ';
+					dumpindent[rdepth++] = ' ';
+					dumpindent[rdepth++] = ' ';
+					dumpindent[rdepth++] = ' ';
+					dumpindent[rdepth] = 0;
+				}
+
+				if (more)
+					dumpindent[depth + 0] = '|';
+
+				kcount += dump_key_tree_aux(key,
+							    rdepth,
+							    ringlen - 4 >= sizeof(key_serial_t));
+			}
+
+		} while (ringlen -= 4, ringlen >= sizeof(key_serial_t));
+
+		free(payload);
+	}
+
+	free(desc);
+	return kcount;
+
+} /* end dump_key_tree_aux() */
+
+/*****************************************************************************/
+/*
+ * recursively list a keyring's contents
+ */
+static int dump_key_tree(key_serial_t keyring, const char *name)
+{
+	printf("%s\n", name);
+	return dump_key_tree_aux(keyring, 0, 0);
+
+} /* end dump_key_tree() */
diff --git a/keyutil.c b/keyutil.c
new file mode 100644
index 0000000..4fdd3b6
--- /dev/null
+++ b/keyutil.c
@@ -0,0 +1,386 @@
+/* keyutil.c: key utility library
+ *
+ * Copyright (C) 2005 Red Hat, Inc. All Rights Reserved.
+ * Written by David Howells (dhowells@redhat.com)
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdarg.h>
+#include <string.h>
+#include <unistd.h>
+#include <dlfcn.h>
+#include <errno.h>
+#include <asm/unistd.h>
+#include "keyutil.h"
+
+#ifdef NO_GLIBC_KEYERR
+static int error_inited;
+static int (*libc_xpg_strerror_r)(int errnum, char *buf, size_t n);
+static char *(*libc_strerror_r)(int errnum, char *buf, size_t n);
+static void (*libc_perror)(const char *msg);
+#define RTLD_NEXT      ((void *) -1L)
+#endif
+
+#define __weak __attribute__((weak))
+
+key_serial_t __weak add_key(const char *type,
+			    const char *description,
+			    const void *payload,
+			    size_t plen,
+			    key_serial_t ringid)
+{
+	return syscall(__NR_add_key,
+		       type, description, payload, plen, ringid);
+}
+
+key_serial_t __weak request_key(const char *type,
+				const char *description,
+				const char * callout_info,
+				key_serial_t destringid)
+{
+	return syscall(__NR_request_key,
+		       type, description, callout_info, destringid);
+}
+
+static inline long __keyctl(int cmd,
+			    unsigned long arg2,
+			    unsigned long arg3,
+			    unsigned long arg4,
+			    unsigned long arg5)
+{
+	return syscall(__NR_keyctl,
+		       cmd, arg2, arg3, arg4, arg5);
+}
+
+long __weak keyctl(int cmd, ...)
+{
+	va_list va;
+	unsigned long arg2, arg3, arg4, arg5;
+
+	va_start(va, cmd);
+	arg2 = va_arg(va, unsigned long);
+	arg3 = va_arg(va, unsigned long);
+	arg4 = va_arg(va, unsigned long);
+	arg5 = va_arg(va, unsigned long);
+	va_end(va);
+
+	return __keyctl(cmd, arg2, arg3, arg4, arg5);
+}
+
+key_serial_t keyctl_get_keyring_ID(key_serial_t id, int create)
+{
+	return keyctl(KEYCTL_GET_KEYRING_ID, id, create);
+}
+
+key_serial_t keyctl_join_session_keyring(const char *name)
+{
+	return keyctl(KEYCTL_JOIN_SESSION_KEYRING, name);
+}
+
+long keyctl_update(key_serial_t id, const void *payload, size_t plen)
+{
+	return keyctl(KEYCTL_UPDATE, id, payload, plen);
+}
+
+long keyctl_revoke(key_serial_t id)
+{
+	return keyctl(KEYCTL_REVOKE, id);
+}
+
+long keyctl_chown(key_serial_t id, uid_t uid, gid_t gid)
+{
+	return keyctl(KEYCTL_CHOWN, id, uid, gid);
+}
+
+long keyctl_setperm(key_serial_t id, key_perm_t perm)
+{
+	return keyctl(KEYCTL_SETPERM, id, perm);
+}
+
+long keyctl_describe(key_serial_t id, char *buffer, size_t buflen)
+{
+	return keyctl(KEYCTL_DESCRIBE, id, buffer, buflen);
+}
+
+long keyctl_clear(key_serial_t ringid)
+{
+	return keyctl(KEYCTL_CLEAR, ringid);
+}
+
+long keyctl_link(key_serial_t id, key_serial_t ringid)
+{
+	return keyctl(KEYCTL_LINK, id, ringid);
+}
+
+long keyctl_unlink(key_serial_t id, key_serial_t ringid)
+{
+	return keyctl(KEYCTL_UNLINK, id, ringid);
+}
+
+long keyctl_search(key_serial_t ringid,
+		   const char *type,
+		   const char *description,
+		   key_serial_t destringid)
+{
+	return keyctl(KEYCTL_SEARCH, ringid, type, description, destringid);
+}
+
+long keyctl_read(key_serial_t id, char *buffer, size_t buflen)
+{
+	return keyctl(KEYCTL_READ, id, buffer, buflen);
+}
+
+long keyctl_instantiate(key_serial_t id,
+			const void *payload,
+			size_t plen,
+			key_serial_t ringid)
+{
+	return keyctl(KEYCTL_INSTANTIATE, id, payload, plen, ringid);
+}
+
+long keyctl_negate(key_serial_t id, unsigned timeout, key_serial_t ringid)
+{
+	return keyctl(KEYCTL_NEGATE, id, timeout, ringid);
+}
+
+long keyctl_set_reqkey_keyring(int reqkey_defl)
+{
+	return keyctl(KEYCTL_SET_REQKEY_KEYRING, reqkey_defl);
+}
+
+/*****************************************************************************/
+/*
+ * fetch key description into an allocated buffer
+ * - resulting string is NUL terminated
+ * - returns count not including NUL
+ */
+int keyctl_describe_alloc(key_serial_t id, char **_buffer)
+{
+	char *buf;
+	long buflen, ret;
+
+	ret = keyctl_describe(id, NULL, 0);
+	if (ret < 0)
+		return -1;
+
+	buflen = ret;
+	buf = malloc(buflen);
+	if (!buf)
+		return -1;
+
+	for (;;) {
+		ret = keyctl_describe(id, buf, buflen);
+		if (ret < 0)
+			return -1;
+
+		if (buflen >= ret)
+			break;
+
+		buflen = ret;
+		buf = realloc(buf, buflen);
+		if (!buf)
+			return -1;
+	}
+
+	*_buffer = buf;
+	return buflen - 1;
+
+} /* end keyctl_describe_alloc() */
+
+/*****************************************************************************/
+/*
+ * fetch key contents into an allocated buffer
+ * - resulting buffer has an extra NUL added to the end
+ * - returns count (not including extraneous NUL)
+ */
+int keyctl_read_alloc(key_serial_t id, void **_buffer)
+{
+	void *buf;
+	long buflen, ret;
+
+	ret = keyctl_read(id, NULL, 0);
+	if (ret < 0)
+		return -1;
+
+	buflen = ret;
+	buf = malloc(buflen + 1);
+	if (!buf)
+		return -1;
+
+	for (;;) {
+		ret = keyctl_read(id, buf, buflen);
+		if (ret < 0)
+			return -1;
+
+		if (buflen >= ret)
+			break;
+
+		buflen = ret;
+		buf = realloc(buf, buflen + 1);
+		if (!buf)
+			return -1;
+	}
+
+	((unsigned char *) buf)[buflen] = 0;
+	*_buffer = buf;
+	return buflen;
+
+} /* end keyctl_read_alloc() */
+
+#ifdef NO_GLIBC_KEYERR
+/*****************************************************************************/
+/*
+ * initialise error handling
+ */
+static void error_init(void)
+{
+	libc_xpg_strerror_r = dlsym(RTLD_NEXT,"xpg_strerror_r");
+	if (!libc_xpg_strerror_r)
+		abort();
+	libc_strerror_r = dlsym(RTLD_NEXT,"strerror_r");
+	if (!libc_strerror_r)
+		abort();
+	libc_perror = dlsym(RTLD_NEXT,"perror");
+	if (!libc_perror)
+		abort();
+	error_inited = 1;
+
+} /* end error_init() */
+
+/*****************************************************************************/
+/*
+ * overload glibc's strerror_r() with a version that knows about key errors
+ */
+char *strerror_r(int errnum, char *buf, size_t n)
+{
+	const char *errstr;
+	int len;
+
+	printf("hello\n");
+
+	if (!error_inited)
+		error_init();
+
+	switch (errnum) {
+	case ENOKEY:
+		errstr = "Requested key not available";
+		break;
+
+	case EKEYEXPIRED:
+		errstr = "Key has expired";
+		break;
+
+	case EKEYREVOKED:
+		errstr = "Key has been revoked";
+		break;
+
+	case EKEYREJECTED:
+		errstr = "Key was rejected by service";
+		break;
+
+	default:
+		return libc_strerror_r(errnum, buf, n);
+	}
+
+	len = strlen(errstr) + 1;
+	if (n > len) {
+		errno = ERANGE;
+		if (n > 0) {
+			memcpy(buf, errstr, n - 1);
+			buf[n - 1] = 0;
+		}
+		return NULL;
+	}
+	else {
+		memcpy(buf, errstr, len);
+		return buf;
+	}
+
+} /* end strerror_r() */
+
+/*****************************************************************************/
+/*
+ * overload glibc's strerror_r() with a version that knows about key errors
+ */
+int xpg_strerror_r(int errnum, char *buf, size_t n)
+{
+	const char *errstr;
+	int len;
+
+	if (!error_inited)
+		error_init();
+
+	switch (errnum) {
+	case ENOKEY:
+		errstr = "Requested key not available";
+		break;
+
+	case EKEYEXPIRED:
+		errstr = "Key has expired";
+		break;
+
+	case EKEYREVOKED:
+		errstr = "Key has been revoked";
+		break;
+
+	case EKEYREJECTED:
+		errstr = "Key was rejected by service";
+		break;
+
+	default:
+		return libc_xpg_strerror_r(errnum, buf, n);
+	}
+
+	len = strlen(errstr) + 1;
+	if (n > len) {
+		errno = ERANGE;
+		if (n > 0) {
+			memcpy(buf, errstr, n - 1);
+			buf[n - 1] = 0;
+		}
+		return -1;
+	}
+	else {
+		memcpy(buf, errstr, len);
+		return 0;
+	}
+
+} /* end xpg_strerror_r() */
+
+/*****************************************************************************/
+/*
+ *
+ */
+void perror(const char *msg)
+{
+	switch (errno) {
+	case ENOKEY:
+		fprintf(stderr, "%s: Requested key not available\n", msg);
+		return;
+
+	case EKEYEXPIRED:
+		fprintf(stderr, "%s: Key has expired\n", msg);
+		return;
+
+	case EKEYREVOKED:
+		fprintf(stderr, "%s: Key has been revoked\n", msg);
+		return;
+
+	case EKEYREJECTED:
+		fprintf(stderr, "%s: Key was rejected by service\n", msg);
+		return;
+
+	default:
+		libc_perror(msg);
+		return;
+	}
+
+} /* end perror() */
+#endif
diff --git a/keyutil.h b/keyutil.h
new file mode 100644
index 0000000..610f331
--- /dev/null
+++ b/keyutil.h
@@ -0,0 +1,139 @@
+/* keyutil.h: key utility library interface
+ *
+ * Copyright (C) 2005 Red Hat, Inc. All Rights Reserved.
+ * Written by David Howells (dhowells@redhat.com)
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ */
+
+#ifndef KEYUTIL_H
+#define KEYUTIL_H
+
+
+/* key serial number */
+typedef int32_t key_serial_t;
+
+/* special process keyring shortcut IDs */
+#define KEY_SPEC_THREAD_KEYRING		-1	/* - key ID for thread-specific keyring */
+#define KEY_SPEC_PROCESS_KEYRING	-2	/* - key ID for process-specific keyring */
+#define KEY_SPEC_SESSION_KEYRING	-3	/* - key ID for session-specific keyring */
+#define KEY_SPEC_USER_KEYRING		-4	/* - key ID for UID-specific keyring */
+#define KEY_SPEC_USER_SESSION_KEYRING	-5	/* - key ID for UID-session keyring */
+#define KEY_SPEC_GROUP_KEYRING		-6	/* - key ID for GID-specific keyring */
+
+/* request-key default keyrings */
+#define KEY_REQKEY_DEFL_NO_CHANGE		-1
+#define KEY_REQKEY_DEFL_DEFAULT			0
+#define KEY_REQKEY_DEFL_THREAD_KEYRING		1
+#define KEY_REQKEY_DEFL_PROCESS_KEYRING		2
+#define KEY_REQKEY_DEFL_SESSION_KEYRING		3
+#define KEY_REQKEY_DEFL_USER_KEYRING		4
+#define KEY_REQKEY_DEFL_USER_SESSION_KEYRING	5
+#define KEY_REQKEY_DEFL_GROUP_KEYRING		6
+
+/* key handle permissions mask */
+typedef uint32_t key_perm_t;
+#define KEY_USR_VIEW	0x00010000	/* user can view a key's attributes */
+#define KEY_USR_READ	0x00020000	/* user can read key payload / view keyring */
+#define KEY_USR_WRITE	0x00040000	/* user can update key payload / add link to keyring */
+#define KEY_USR_SEARCH	0x00080000	/* user can find a key in search / search a keyring */
+#define KEY_USR_LINK	0x00100000	/* user can create a link to a key/keyring */
+#define KEY_USR_ALL	0x001f0000
+
+#define KEY_GRP_VIEW	0x00000100	/* group permissions... */
+#define KEY_GRP_READ	0x00000200
+#define KEY_GRP_WRITE	0x00000400
+#define KEY_GRP_SEARCH	0x00000800
+#define KEY_GRP_LINK	0x00001000
+#define KEY_GRP_ALL	0x00001f00
+
+#define KEY_OTH_VIEW	0x00000001	/* third party permissions... */
+#define KEY_OTH_READ	0x00000002
+#define KEY_OTH_WRITE	0x00000004
+#define KEY_OTH_SEARCH	0x00000008
+#define KEY_OTH_LINK	0x00000010
+#define KEY_OTH_ALL	0x0000001f
+
+/* keyctl commands */
+#define KEYCTL_GET_KEYRING_ID		0	/* ask for a keyring's ID */
+#define KEYCTL_JOIN_SESSION_KEYRING	1	/* join or start named session keyring */
+#define KEYCTL_UPDATE			2	/* update a key */
+#define KEYCTL_REVOKE			3	/* revoke a key */
+#define KEYCTL_CHOWN			4	/* set ownership of a key */
+#define KEYCTL_SETPERM			5	/* set perms on a key */
+#define KEYCTL_DESCRIBE			6	/* describe a key */
+#define KEYCTL_CLEAR			7	/* clear contents of a keyring */
+#define KEYCTL_LINK			8	/* link a key into a keyring */
+#define KEYCTL_UNLINK			9	/* unlink a key from a keyring */
+#define KEYCTL_SEARCH			10	/* search for a key in a keyring */
+#define KEYCTL_READ			11	/* read a key or keyring's contents */
+#define KEYCTL_INSTANTIATE		12	/* instantiate a partially constructed key */
+#define KEYCTL_NEGATE			13	/* negate a partially constructed key */
+#define KEYCTL_SET_REQKEY_KEYRING	14	/* set default request-key keyring */
+
+/* key management error codes */
+#ifdef NO_GLIBC_KEYERR
+#if defined(__i386__) || defined(__PPC64__) || defined(__PPC__) || defined(__x86_64__)
+
+#define	ENOKEY		126	/* Required key not available */
+#define	EKEYEXPIRED	127	/* Key has expired */
+#define	EKEYREVOKED	128	/* Key has been revoked */
+#define	EKEYREJECTED	129	/* Key was rejected by service */
+
+#else
+#error key management error codes not supported on this arch
+#endif
+#endif
+
+/*
+ * syscall wrappers
+ */
+extern key_serial_t add_key(const char *type,
+			    const char *description,
+			    const void *payload,
+			    size_t plen,
+			    key_serial_t ringid);
+
+extern key_serial_t request_key(const char *type,
+				const char *description,
+				const char *callout_info,
+				key_serial_t destringid);
+
+extern long keyctl(int cmd, ...);
+
+/*
+ * keyctl function wrappers
+ */
+extern key_serial_t keyctl_get_keyring_ID(key_serial_t id, int create);
+extern key_serial_t keyctl_join_session_keyring(const char *name);
+extern long keyctl_update(key_serial_t id, const void *payload, size_t plen);
+extern long keyctl_revoke(key_serial_t id);
+extern long keyctl_chown(key_serial_t id, uid_t uid, gid_t gid);
+extern long keyctl_setperm(key_serial_t id, key_perm_t perm);
+extern long keyctl_describe(key_serial_t id, char *buffer, size_t buflen);
+extern long keyctl_clear(key_serial_t ringid);
+extern long keyctl_link(key_serial_t id, key_serial_t ringid);
+extern long keyctl_unlink(key_serial_t id, key_serial_t ringid);
+extern long keyctl_search(key_serial_t ringid,
+			  const char *type,
+			  const char *description,
+			  key_serial_t destringid);
+extern long keyctl_read(key_serial_t id, char *buffer, size_t buflen);
+extern long keyctl_instantiate(key_serial_t id,
+			       const void *payload,
+			       size_t plen,
+			       key_serial_t ringid);
+extern long keyctl_negate(key_serial_t id, unsigned timeout, key_serial_t ringid);
+extern long keyctl_set_reqkey_keyring(int reqkey_defl);
+
+/*
+ * utilities
+ */
+extern int keyctl_describe_alloc(key_serial_t id, char **_buffer);
+extern int keyctl_read_alloc(key_serial_t id, void **_buffer);
+
+
+#endif /* KEYUTIL_H */
diff --git a/keyutils.spec b/keyutils.spec
new file mode 100644
index 0000000..58378c2
--- /dev/null
+++ b/keyutils.spec
@@ -0,0 +1,59 @@
+%define vermajor 0
+%define version %{vermajor}.1
+%define _exec_prefix /
+
+Summary: Linux Key Management Utilities
+Name: keyutils
+Version: %{version}
+Release: 1
+License: GPL/LGPL
+Group: System Environment/Base
+ExclusiveOS: Linux
+
+Source0: http://people.redhat.com/~dhowells/keyutils/keyutils-%{version}.tar.bz2
+
+BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-buildroot
+
+%description
+Utilities to control the kernel key management facility and to provide
+a mechanism by which the kernel call back to userspace to get a key
+instantiated.
+
+%package devel
+Summary: Development package for building linux key management utilities
+Group: System Environment/Base
+
+%description devel
+This package provides headers and libraries for building key utilities.
+
+%prep
+%setup -q
+
+%build
+make LIBDIR=%{_libdir}
+
+%install
+rm -rf $RPM_BUILD_ROOT
+make DESTDIR=$RPM_BUILD_ROOT LIBDIR=%{_libdir} install
+
+%clean
+rm -rf $RPM_BUILD_ROOT
+
+%files
+%defattr(-,root,root,-)
+%doc README LICENCE.GPL LICENCE.LGPL
+%{_libdir}/libkeyutil.so.%{version}
+%{_libdir}/libkeyutil.so.%{vermajor}
+/sbin/*
+/bin/*
+/usr/share/keyutils/*
+%{_mandir}/*
+%config(noreplace) /etc/*
+
+%files devel
+%{_libdir}/libkeyutil.so
+%{_includedir}/*
+
+%changelog
+* Mon Jul 12 2005 David Howells <dhowells@redhat.com> - 0.1-1
+- Package creation.
diff --git a/request-key-debug.sh b/request-key-debug.sh
new file mode 100755
index 0000000..ece42a4
--- /dev/null
+++ b/request-key-debug.sh
@@ -0,0 +1,36 @@
+#!/bin/sh
+###############################################################################
+#
+# Copyright (C) 2005 Red Hat, Inc. All Rights Reserved.
+# Written by David Howells (dhowells@redhat.com)
+#
+# 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
+# 2 of the License, or (at your option) any later version.
+#
+###############################################################################
+#
+# Request key debugging
+#
+# Call: request-key-debug.sh <keyid> <desc> <callout> <session-keyring>
+#
+
+{
+    echo RQDebug keyid: $1
+    echo RQDebug desc: $2
+    echo RQDebug callout: $3
+    echo RQDebug session keyring: $4
+
+    if [ "$3" != "neg" ]
+	then
+	keyctl instantiate $1 "Debug $3" $4 || exit 1
+    else
+	cat /proc/keys
+	echo keyctl negate $1 30 $4
+	keyctl negate $1 30 $4
+    fi
+
+} >&/dev/ttyS0
+
+exit 0
diff --git a/request-key.8 b/request-key.8
new file mode 100644
index 0000000..d506da3
--- /dev/null
+++ b/request-key.8
@@ -0,0 +1,29 @@
+.\"
+.\" Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
+.\" Written by David Howells (dhowells@redhat.com)
+.\"
+.\" 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
+.\" 2 of the License, or (at your option) any later version.
+.\"
+.TH REQUEST-KEY 8 "11 July 2005" Linux "Linux Key Management Utilities"
+.SH NAME
+request-key - Handle key instantiation callback requests from the kernel
+.SH SYNOPSIS
+\fB/sbin/request-key \fR<op> <key> <uid> <gid> <threadring> <processring>
+	<sessionring> <info>
+.SH DESCRIPTION
+This program is invoked by the kernel when the kernel is asked for a key that
+it doesn't have immediately available. The kernel creates a partially set up
+key and then calls out to this program to instantiate it. It is not intended
+to be called directly.
+.SH ERRORS
+All errors will be logged to the syslog.
+.SH FILES
+.ul
+/etc/request-key.conf
+.ul 0
+Instantiation handler configuration file.
+.SH SEE ALSO
+\fBkeyctl\fR(1), \fBrequest-key.conf\fR(5)
diff --git a/request-key.c b/request-key.c
new file mode 100644
index 0000000..ef66c05
--- /dev/null
+++ b/request-key.c
@@ -0,0 +1,521 @@
+/* request-key.c: hand a key request off to the appropriate process
+ *
+ * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
+ * Written by David Howells (dhowells@redhat.com)
+ *
+ * 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
+ * 2 of the License, or (at your option) any later version.
+ *
+ * /sbin/request-key <op> <key> <uid> <gid> <threadring> <processring> <sessionring> <info>
+ *
+ * Searches the specified session ring for a key indicating the command to run:
+ *	type:	"user"
+ *	desc:	"request-key:<op>"
+ *	data:	command name, eg: "/home/dhowells/request-key-create.sh"
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <syslog.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <ctype.h>
+#include "keyutil.h"
+
+
+static int xdebug;
+static int xnolog;
+static char *xkey;
+static char *xuid;
+static char *xgid;
+static char *xthread_keyring;
+static char *xprocess_keyring;
+static char *xsession_keyring;
+static int confline;
+
+static void lookup_action(char *op,
+			  key_serial_t key,
+			  char *ktype,
+			  char *kdesc,
+			  char *callout_info)
+	__attribute__((noreturn));
+
+static void execute_program(char *op,
+			    char *ktype,
+			    char *kdesc,
+			    char *callout_info,
+			    char *cmdline)
+	__attribute__((noreturn));
+
+static int match(const char *pattern, int plen, const char *datum, int dlen);
+
+static void debug(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
+static void debug(const char *fmt, ...)
+{
+	va_list va;
+
+	if (xdebug) {
+		va_start(va, fmt);
+		vfprintf(stderr, fmt, va);
+		va_end(va);
+
+		if (!xnolog) {
+			openlog("request-key", 0, LOG_AUTHPRIV);
+
+			va_start(va, fmt);
+			vsyslog(LOG_DEBUG, fmt, va);
+			va_end(va);
+
+			closelog();
+		}
+	}
+}
+
+static void error(const char *fmt, ...) __attribute__((noreturn, format(printf, 1, 2)));
+static void error(const char *fmt, ...)
+{
+	va_list va;
+
+	if (xdebug) {
+		va_start(va, fmt);
+		vfprintf(stderr, fmt, va);
+		va_end(va);
+	}
+
+	if (!xnolog) {
+		openlog("request-key", 0, LOG_AUTHPRIV);
+
+		va_start(va, fmt);
+		vsyslog(LOG_ERR, fmt, va);
+		va_end(va);
+
+		closelog();
+	}
+
+	exit(1);
+}
+
+/*****************************************************************************/
+/*
+ *
+ */
+int main(int argc, char *argv[])
+{
+	key_serial_t key;
+	char *ktype, *kdesc, *buf;
+	int ret, ntype, dpos, dlen;
+
+	for (;;) {
+		if (argc > 1 && strcmp(argv[1], "-d") == 0) {
+			xdebug++;
+			argv++;
+			argc--;
+		}
+		else if (argc > 1 && strcmp(argv[1], "-n") == 0) {
+			xnolog = 1;
+			argv++;
+			argc--;
+		}
+		else
+			break;
+	}
+
+	if (argc != 9)
+		error("Unexpected argument count: %d\n", argc);
+
+	xkey = argv[2];
+	xuid = argv[3];
+	xgid = argv[4];
+	xthread_keyring = argv[5];
+	xprocess_keyring = argv[6];
+	xsession_keyring = argv[7];
+
+	key = atoi(xkey);
+
+	/* ask the kernel to describe the key to us */
+	if (xdebug <= 0) {
+		ret = keyctl_describe_alloc(key, &buf);
+		if (ret < 0)
+			goto inaccessible;
+	}
+	else {
+		buf = strdup("user;0;0;1f0000;debug:1234");
+	}
+
+	/* extract the type and description from the key */
+	debug("Key descriptor: \"%s\"\n", buf);
+	ntype = -1;
+	dpos = -1;
+	dlen = -1;
+
+	sscanf(buf, "%*[^;]%n;%*d;%*d;%*x;%n%*[^;]%n", &ntype, &dpos, &dlen);
+	if (dlen == -1)
+		error("Failed to parse key description\n");
+
+	ktype = buf;
+	ktype[ntype] = 0;
+	kdesc = buf + dpos;
+
+	debug("Key type: %s\n", ktype);
+	debug("Key desc: %s\n", kdesc);
+
+	/* determine the action to perform */
+	lookup_action(argv[1],		/* op */
+		      key,		/* ID of key under construction */
+		      ktype,		/* key type */
+		      kdesc,		/* key description */
+		      argv[8]		/* call out info */
+		      );
+
+inaccessible:
+	error("Key %d is inaccessible (%m)\n", key);
+
+} /* end main() */
+
+/*****************************************************************************/
+/*
+ * determine the action to perform
+ */
+static void lookup_action(char *op,
+			  key_serial_t key,
+			  char *ktype,
+			  char *kdesc,
+			  char *callout_info)
+{
+	char buf[4096 + 2], *p, *q;
+	FILE *conf;
+	int len, oplen, ktlen, kdlen, cilen;
+
+	oplen = strlen(op);
+	ktlen = strlen(ktype);
+	kdlen = strlen(kdesc);
+	cilen = strlen(callout_info);
+
+	/* search the config file for a command to run */
+	conf = fopen(xdebug < 2 ? "/etc/request-key.conf" : "request-key.conf", "r");
+	if (!conf)
+		error("Cannot open /etc/request-key.conf: %m\n");
+
+	for (confline = 1;; confline++) {
+		/* read the file line-by-line */
+		if (!fgets(buf, sizeof(buf), conf)) {
+			if (feof(conf))
+				error("Cannot find command to construct key %d\n", key);
+			error("Error reading /etc/request-key.conf\n");
+		}
+
+		len = strlen(buf);
+		if (len >= sizeof(buf) - 2)
+			error("/etc/request-key.conf:%d: Line too long\n", confline);
+
+		/* ignore blank lines and comments */
+		if (len == 1 || buf[0] == '#' || isspace(buf[0]))
+			continue;
+
+		buf[--len] = 0;
+		p = buf;
+
+		/* attempt to match the op */
+		q = p;
+		while (*p && !isspace(*p)) p++;
+		if (!*p)
+			goto syntax_error;
+		*p = 0;
+
+		if (!match(q, p - q, op, oplen))
+			continue;
+
+		p++;
+
+		/* attempt to match the type */
+		while (isspace(*p)) p++;
+		if (!*p)
+			goto syntax_error;
+
+		q = p;
+		while (*p && !isspace(*p)) p++;
+		if (!*p)
+			goto syntax_error;
+		*p = 0;
+
+		if (!match(q, p - q, ktype, ktlen))
+			continue;
+
+		p++;
+
+		/* attempt to match the description */
+		while (isspace(*p)) p++;
+		if (!*p)
+			goto syntax_error;
+
+		q = p;
+		while (*p && !isspace(*p)) p++;
+		if (!*p)
+			goto syntax_error;
+		*p = 0;
+
+		if (!match(q, p - q, kdesc, kdlen))
+			continue;
+
+		p++;
+
+		/* attempt to match the callout info */
+		while (isspace(*p)) p++;
+		if (!*p)
+			goto syntax_error;
+
+		q = p;
+		while (*p && !isspace(*p)) p++;
+		if (!*p)
+			goto syntax_error;
+		*p = 0;
+
+		if (!match(q, p - q, callout_info, cilen))
+			continue;
+
+		p++;
+
+		debug("Line %d matches\n", confline);
+
+		/* we've got an action */
+		while (isspace(*p)) p++;
+		if (!*p)
+			goto syntax_error;
+
+		execute_program(op, ktype, kdesc, callout_info, p);
+	}
+
+	error("/etc/request-key.conf: No matching action\n");
+
+syntax_error:
+	error("/etc/request-key.conf:%d: Syntax error\n", confline);
+
+} /* end lookup_action() */
+
+/*****************************************************************************/
+/*
+ * attempt to match a datum to a pattern
+ * - one asterisk is allowed anywhere in the pattern to indicate a wildcard
+ * - returns true if matched, false if not
+ */
+static int match(const char *pattern, int plen, const char *datum, int dlen)
+{
+	const char *asterisk;
+	int n;
+
+	debug("match(%*.*s,%*.*s)\n", plen, plen, pattern, dlen, dlen, datum);
+
+	asterisk = memchr(pattern, '*', plen);
+	if (!asterisk) {
+		/* exact match only if no wildcard */
+		if (plen == dlen && memcmp(pattern, datum, dlen) == 0)
+			goto yes;
+		goto no;
+	}
+
+	/* the datum mustn't be shorter than the pattern without the asterisk */
+	if (dlen < plen - 1)
+		goto no;
+
+	n = asterisk - pattern;
+	if (n == 0) {
+		/* wildcard at beginning of pattern */
+		pattern++;
+		if (!*pattern)
+			goto yes; /* "*" matches everything */
+
+		/* match the end of the datum */
+		plen--;
+		if (memcmp(pattern, datum + (dlen - plen), plen) == 0)
+			goto yes;
+		goto no;
+	}
+
+	/* need to match beginning of datum for "abc*" and "abc*def" */
+	if (memcmp(pattern, datum, n) != 0)
+		goto no;
+
+	if (!asterisk[1])
+		goto yes; /* "abc*" matches */
+
+	/* match the end of the datum */
+	asterisk++;
+	n = plen - n - 1;
+	if (memcmp(pattern, datum + (dlen - n), n) == 0)
+		goto yes;
+
+no:
+	debug(" = no\n");
+	return 0;
+
+yes:
+	debug(" = yes\n");
+	return 1;
+
+} /* end match() */
+
+/*****************************************************************************/
+/*
+ * execute a program to deal with a key
+ */
+static void execute_program(char *op,
+			    char *ktype,
+			    char *kdesc,
+			    char *callout_info,
+			    char *cmdline)
+{
+	char *argv[256];
+	char *prog, *p, *q;
+	int argc;
+
+	debug("execute_program('%s')\n", cmdline);
+
+	/* extract the path to the program to run */
+	prog = p = cmdline;
+	while (*p && !isspace(*p)) p++;
+	if (!*p)
+		error("/etc/request-key.conf:%d: No command path\n", confline);
+	*p++ = 0;
+
+	argv[0] = strrchr(prog, '/') + 1;
+
+	/* extract the arguments */
+	for (argc = 1; p; argc++) {
+		while (isspace(*p)) p++;
+		if (!*p)
+			break;
+
+		if (argc >= 254)
+			error("/etc/request-key.conf:%d: Too many arguments\n", confline);
+		argv[argc] = q = p;
+
+		while (*p && !isspace(*p)) p++;
+
+		if (*p)
+			*p++ = 0;
+		else
+			p = NULL;
+
+		debug("argv[%d]: '%s'\n", argc, argv[argc]);
+
+		if (*q != '%')
+			continue;
+
+		/* it's a macro */
+		q++;
+		if (!*q)
+			error("/etc/request-key.conf:%d: Missing macro name\n", confline);
+
+		if (*q == '%') {
+			/* it's actually an anti-macro escape "%%..." -> "%..." */
+			argv[argc]++;
+			continue;
+		}
+
+		/* single character macros */
+		if (!q[1]) {
+			switch (*q) {
+			case 'o': argv[argc] = op;			continue;
+			case 'k': argv[argc] = xkey;			continue;
+			case 't': argv[argc] = ktype;			continue;
+			case 'd': argv[argc] = kdesc;			continue;
+			case 'c': argv[argc] = callout_info;		continue;
+			case 'u': argv[argc] = xuid;			continue;
+			case 'g': argv[argc] = xgid;			continue;
+			case 'T': argv[argc] = xthread_keyring;		continue;
+			case 'P': argv[argc] = xprocess_keyring;	continue;
+			case 'S': argv[argc] = xsession_keyring;	continue;
+			default:
+				error("/etc/request-key.conf:%d: Unsupported macro\n", confline);
+			}
+		}
+
+		/* keysub macro */
+		if (*q == '{') {
+			key_serial_t rqsession, keysub;
+			void *tmp;
+			char *ksdesc, *end, *subdata;
+			int ret, loop;
+
+			/* extract type and description */
+			q++;
+			ksdesc = strchr(q, ':');
+			if (!ksdesc)
+				error("/etc/request-key.conf:%d: Keysub macro lacks ':'\n",
+				      confline);
+			*ksdesc++ = 0;
+			end = strchr(ksdesc, '}');
+			if (!end)
+				error("/etc/request-key.conf:%d: Unterminated keysub macro\n",
+				      confline);
+
+			*end++ = 0;
+			if (*end)
+				error("/etc/request-key.conf:%d:"
+				      " Keysub macro has trailing rubbish\n",
+				      confline);
+
+			debug("Keysub: %s key \"%s\"\n", q, ksdesc);
+
+			if (!q[0])
+				error("/etc/request-key.conf:%d: Keysub type empty\n", confline);
+
+			if (!ksdesc[0])
+				error("/etc/request-key.conf:%d: Keysub description empty\n",
+				      confline);
+
+			/* look up the key in the requestor's session keyring */
+			rqsession = atoi(xsession_keyring);
+
+			keysub = keyctl_search(rqsession, q, ksdesc, 0);
+			if (keysub < 0)
+				error("/etc/request-key.conf:%d:"
+				      " Keysub key not found: %m\n",
+				      confline);
+
+			ret = keyctl_read_alloc(keysub, &tmp);
+			if (ret < 0)
+				error("/etc/request-key.conf:%d:"
+				      " Can't read keysub %d data: %m\n",
+				      confline, keysub);
+			subdata = tmp;
+
+			for (loop = 0; loop < ret; loop++)
+				if (!isprint(subdata[loop]))
+					error("/etc/request-key.conf:%d:"
+					      " keysub %d data not printable ('%02hhx')\n",
+					      confline, keysub, subdata[loop]);
+
+			argv[argc] = subdata;
+			continue;
+		}
+	}
+
+	if (argc == 0)
+		error("/etc/request-key.conf:%d: No arguments\n", confline);
+
+	argv[argc] = NULL;
+
+	/* become the same UID/GID as the key requesting process */
+	//setgid(atoi(xuid));
+	//setuid(atoi(xgid));
+
+	/* attempt to execute the command */
+	if (xdebug) {
+		char **ap;
+
+		debug("Run %s\n", prog);
+		for (ap = argv; *ap; ap++)
+			debug("- argv[%zd] = \"%s\"\n", ap - argv, *ap);
+	}
+
+	execv(prog, argv);
+
+	error("/etc/request-key.conf:%d: Failed to execute '%s': %m\n", confline, prog);
+
+} /* end execute_program() */
diff --git a/request-key.conf b/request-key.conf
new file mode 100644
index 0000000..c1b18a7
--- /dev/null
+++ b/request-key.conf
@@ -0,0 +1,35 @@
+###############################################################################
+#
+# Copyright (C) 2005 Red Hat, Inc. All Rights Reserved.
+# Written by David Howells (dhowells@redhat.com)
+#
+# 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
+# 2 of the License, or (at your option) any later version.
+#
+###############################################################################
+
+
+###############################################################################
+#
+# We can run programs or scripts
+# - Macro substitutions in arguments:
+#	%%...	%...
+#	%o	operation name
+#	%k	ID of key being operated upon
+#	%t	type of key being operated upon
+#	%d	description of key being operated upon
+#	%c	callout info
+#	%u	UID of requestor
+#	%g	GID of requestor
+#	%T	thread keyring of requestor (may be 0)
+#	%P	process keyring of requestor (may be 0)
+#	%S	session keyring of requestor (may be the user's default session)
+#
+################################################################################
+
+#OP	TYPE	DESCRIPTION	CALLOUT INFO	PROGRAM ARG1 ARG2 ARG3 ...
+#======	=======	===============	===============	===============================
+create	user	debug:*		negate		/bin/keyctl %k negate 30 %S
+create	user	debug:*		*		/usr/share/keyutils/request-key-debug.sh %k %d %c %S
diff --git a/request-key.conf.5 b/request-key.conf.5
new file mode 100644
index 0000000..97abbbe
--- /dev/null
+++ b/request-key.conf.5
@@ -0,0 +1,112 @@
+.\"
+.\" Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
+.\" Written by David Howells (dhowells@redhat.com)
+.\"
+.\" 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
+.\" 2 of the License, or (at your option) any later version.
+.\"
+.TH REQUEST-KEY.CONF 5 "11 July 2005" Linux "Linux Key Management Utilities"
+.SH NAME
+request-key.conf - Instantiation handler configuration file
+.SH DESCRIPTION
+.P
+This file is used by the /sbin/request-key program to determine which program
+it should run to instantiate a key.
+.P
+request-key works scans through the file a line at a time until it finds a
+match, which it will then use. If it doesn't find a match, it'll return an
+error and the kernel will automatically negate the key.
+.P
+Any blank line or line beginning with a hash mark '#' is considered to be a
+comment and ignored.
+.P
+All other lines are assumed to be command lines with a number of white space
+separated fields:
+.P
+<op> <type> <description> <callout-info> <prog> <arg1> <arg2> ...
+.P
+The first four fields are used to match the parameters passed to request-key by
+the kernel. \fIop\fR is the operation type; currently the only supported
+operation is "create".
+.P
+\fItype\fR, \fIdescription\fR and \fIcallout-info\fR match the three parameters
+passed to \fBkeyctl request2\fR or the \fBrequest_key()\fR system call. Each of
+these may contain one or more asterisk '*' characters as wildcards anywhere
+within the string.
+.P
+Should a match be made, the program specified by <prog> will be exec'd. This
+must have a fully qualified path name. argv[0] will be set from the part of the
+program name that follows the last slash '/' character.
+.P
+The program arguments can be substituted with various macros. Only complete
+argument substitution is supported - macro substitutions can't be embedded. All
+macros begin with a percent character '%'. An argument beginning with two
+percent characters will have one of them discarded.
+.P
+The following macros are supported:
+.P
+.RS
+%o    Operation type
+.br
+%k    Key ID
+.br
+%t    Key type
+.br
+%d    Key description
+.br
+%c    Callout information
+.br
+%u    Key UID
+.br
+%g    Key GID
+.br
+%T    Requestor's thread keyring
+.br
+%P    Requestor's process keyring
+.br
+%S    Requestor's session keyring
+.RE
+.P
+There's another macro substitution too that permits the interpolation of the
+contents of a key:
+.P
+.RS
+%{<type>:<description>}
+.RE
+.P
+This performs a lookup for a key of the given type and description on the
+requestor's session keyring, and if found, substitutes the contents for the
+macro. If not found an error will be logged and the key under construction will
+be negated.
+.SH EXAMPLE
+.P
+A basic file will be installed in the /etc. This will contain two debugging
+lines that can be used to test the installation:
+.P
+.RS
+create user debug:* negate /bin/keyctl %k negate 30 %S
+.br
+create user debug:* * /usr/share/keyutils/request-key-debug.sh %k %d %c %S
+.RE
+.P
+This is set up so that something like:
+.P
+.RS
+keyctl request2 user debug:xxxx negate
+.RE
+.P
+will create a negative user-defined key, and something like:
+.P
+.RS
+keyctl request2 user debug:yyyy spoon
+.RE
+.P
+will create an instantiated user-defined key with "Debug spoon" as the payload.
+.SH FILES
+.ul
+/etc/request-key.conf
+.ul 0
+.SH SEE ALSO
+\fBkeyctl\fR(1), \fBrequest-key.conf\fR(5)