]> git.decadent.org.uk Git - nfs-utils.git/blob - support/export/keys.c
481480871a085972ea54337d32f1a25fb1d30599
[nfs-utils.git] / support / export / keys.c
1 /*
2  * keys.c               Key management for nfsd. Currently, keys
3  *                      are kept in a single file only, but eventually,
4  *                      support for a key server should be added.
5  *
6  * Copyright (C) 1995 Olaf Kirch <okir@monad.swb.de>
7  */
8
9 #include "config.h"
10
11 #include <sys/stat.h>
12 #include "nfslib.h"
13 #include "exportfs.h"
14 #include "xmalloc.h"
15
16 struct keycache {
17         struct keycache *       k_next;
18         struct nfskeyent        k_data;
19 };
20
21 static struct keycache *        keycache = NULL;
22 static time_t                   lastmod = 0;
23
24 static void     key_reload(void);
25
26
27 struct nfskey *
28 key_lookup(char *hname)
29 {
30         struct keycache *kc;
31
32         key_reload();
33
34         for (kc = keycache; kc; kc = kc->k_next) {
35 #if 0
36                 if (matchhostname(kc->k_data.k_hostname, hname))
37 #else
38                 if (!strcmp(kc->k_data.k_hostname, hname))
39 #endif
40                         return &kc->k_data.k_key;
41         }
42
43         return NULL;
44 }
45
46 static void
47 key_reload(void)
48 {
49         struct stat     stb;
50         struct keycache *cp;
51         struct nfskeyent *kp;
52
53         if (stat(_PATH_NFSKEYS, &stb) >= 0 && stb.st_mtime == lastmod)
54                 return;
55
56         while (keycache) {
57                 cp = keycache->k_next;
58                 xfree(keycache);
59                 keycache = cp;
60         }
61
62         setnfskeyent(_PATH_NFSKEYS);
63         while ((kp = getnfskeyent()) != NULL) {
64                 cp = (struct keycache *) xmalloc(sizeof(*cp));
65                 cp->k_data = *kp;
66                 cp->k_next = keycache;
67                 keycache = cp;
68         }
69         endnfskeyent();
70
71         lastmod = stb.st_mtime;
72 }