update from main archive 961110
[platform/upstream/glibc.git] / sunrpc / publickey.c
1 /* Get public or secret key from key server.
2    Copyright (C) 1996 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Library General Public License for more details.
15
16    You should have received a copy of the GNU Library General Public
17    License along with the GNU C Library; see the file COPYING.LIB.  If not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #include <rpc/netdb.h>
22
23 #include "nsswitch.h"
24
25
26 /* Type of the lookup function for the public key.  */
27 typedef int (*public_function) (const char *, char *);
28
29 /* Type of the lookup function for the secret key.  */
30 typedef int (*secret_function) (const char *, char *, const char *);
31
32 /* The lookup function for the first entry of this service.  */
33 extern int __nss_publickey_lookup (service_user **nip, const char *name,
34                                    void **fctp);
35
36
37 int
38 getpublickey (const char *name, char *key)
39 {
40   static service_user *startp = NULL;
41   static public_function start_fct;
42   service_user *nip;
43   public_function fct;
44   enum nss_status status = NSS_STATUS_UNAVAIL;
45   int no_more;
46
47   if (startp == NULL)
48     {
49       no_more = __nss_publickey_lookup (&nip, "getpublickey", (void **) &fct);
50       if (no_more)
51         startp = (service_user *) -1;
52       else
53         {
54           startp = nip;
55           start_fct = fct;
56         }
57     }
58   else
59     {
60       fct = start_fct;
61       no_more = (nip = startp) == (service_user *) -1;
62     }
63
64   while (! no_more)
65     {
66       status = (*fct) (name, key);
67
68       no_more = __nss_next (&nip, "getpublickey", (void **) &fct, status, 0);
69     }
70
71   return status == NSS_STATUS_SUCCESS;
72 }
73
74
75 int
76 getsecretkey (const char *name, char *key, const char *passwd)
77 {
78   static service_user *startp = NULL;
79   static secret_function start_fct;
80   service_user *nip;
81   secret_function fct;
82   enum nss_status status = NSS_STATUS_UNAVAIL;
83   int no_more;
84
85   if (startp == NULL)
86     {
87       no_more = __nss_publickey_lookup (&nip, "getsecretkey", (void **) &fct);
88       if (no_more)
89         startp = (service_user *) -1;
90       else
91         {
92           startp = nip;
93           start_fct = fct;
94         }
95     }
96   else
97     {
98       fct = start_fct;
99       no_more = (nip = startp) == (service_user *) -1;
100     }
101
102   while (! no_more)
103     {
104       status = (*fct) (name, key, passwd);
105
106       no_more = __nss_next (&nip, "getsecretkey", (void **) &fct, status, 0);
107     }
108
109   return status == NSS_STATUS_SUCCESS;
110 }