Back to index

glibc  2.9
publickey.c
Go to the documentation of this file.
00001 /* Get public or secret key from key server.
00002    Copyright (C) 1996-1999,2002,2007 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <errno.h>
00022 #include <rpc/netdb.h>
00023 #include <rpc/auth_des.h>
00024 
00025 #include "nsswitch.h"
00026 
00027 
00028 /* Type of the lookup function for the public key.  */
00029 typedef int (*public_function) (const char *, char *, int *);
00030 
00031 /* Type of the lookup function for the secret key.  */
00032 typedef int (*secret_function) (const char *, char *, const char *, int *);
00033 
00034 /* The lookup function for the first entry of this service.  */
00035 extern int __nss_publickey_lookup (service_user **nip, const char *name,
00036                                void **fctp) internal_function;
00037 
00038 
00039 int
00040 getpublickey (const char *name, char *key)
00041 {
00042   static service_user *startp;
00043   static public_function start_fct;
00044   service_user *nip;
00045   union
00046   {
00047     public_function f;
00048     void *ptr;
00049   } fct;
00050   enum nss_status status = NSS_STATUS_UNAVAIL;
00051   int no_more;
00052 
00053   if (startp == NULL)
00054     {
00055       no_more = __nss_publickey_lookup (&nip, "getpublickey", &fct.ptr);
00056       if (no_more)
00057        startp = (service_user *) -1;
00058       else
00059        {
00060          startp = nip;
00061          start_fct = fct.f;
00062        }
00063     }
00064   else
00065     {
00066       fct.f = start_fct;
00067       no_more = (nip = startp) == (service_user *) -1;
00068     }
00069 
00070   while (! no_more)
00071     {
00072       status = (*fct.f) (name, key, &errno);
00073 
00074       no_more = __nss_next2 (&nip, "getpublickey", NULL, &fct.ptr, status, 0);
00075     }
00076 
00077   return status == NSS_STATUS_SUCCESS;
00078 }
00079 libc_hidden_def (getpublickey)
00080 
00081 
00082 int
00083 getsecretkey (const char *name, char *key, const char *passwd)
00084 {
00085   static service_user *startp;
00086   static secret_function start_fct;
00087   service_user *nip;
00088   union
00089   {
00090     secret_function f;
00091     void *ptr;
00092   } fct;
00093   enum nss_status status = NSS_STATUS_UNAVAIL;
00094   int no_more;
00095 
00096   if (startp == NULL)
00097     {
00098       no_more = __nss_publickey_lookup (&nip, "getsecretkey", &fct.ptr);
00099       if (no_more)
00100        startp = (service_user *) -1;
00101       else
00102        {
00103          startp = nip;
00104          start_fct = fct.f;
00105        }
00106     }
00107   else
00108     {
00109       fct.f = start_fct;
00110       no_more = (nip = startp) == (service_user *) -1;
00111     }
00112 
00113   while (! no_more)
00114     {
00115       status = (*fct.f) (name, key, passwd, &errno);
00116 
00117       no_more = __nss_next2 (&nip, "getsecretkey", NULL, &fct.ptr, status, 0);
00118     }
00119 
00120   return status == NSS_STATUS_SUCCESS;
00121 }