]> git.decadent.org.uk Git - nfs-utils.git/blob - support/export/nfsctl.c
nfs-utils: Remove all uses of AI_ADDRCONFIG
[nfs-utils.git] / support / export / nfsctl.c
1 /*
2  * support/export/nfsctl.c
3  *
4  * Communicate export information to knfsd.
5  *
6  * Copyright (C) 1995 Olaf Kirch <okir@monad.swb.de>
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <sys/stat.h>
14 #include <stdlib.h>
15 #include <errno.h>
16 #include <string.h>
17 #include <ctype.h>
18 #include "nfslib.h"
19 #include "exportfs.h"
20 #include "xio.h"
21
22 static int      expsetup(struct nfsctl_export *exparg, nfs_export *exp, int unexport);
23 static int      cltsetup(struct nfsctl_client *cltarg, nfs_client *clp);
24
25 int
26 export_export(nfs_export *exp)
27 {
28         nfs_client *    clp = exp->m_client;
29         struct nfsctl_export    exparg;
30         struct nfsctl_client    cltarg;
31
32         if (!clp->m_exported && (clp->m_type != MCL_GSS)) {
33                 if (!cltsetup(&cltarg, clp))
34                         return 0;
35                 if (nfsaddclient(&cltarg) < 0)
36                         return 0;
37                 clp->m_exported = 1;
38         }
39         if (!expsetup(&exparg, exp, 0))
40                 return 0;
41         if (nfsexport(&exparg) < 0)
42                 return 0;
43         exp->m_exported = 1;
44         return 1;
45 }
46
47 int
48 export_unexport(nfs_export *exp)
49 {
50         struct nfsctl_export    exparg;
51
52         if (!expsetup(&exparg, exp, 1) || nfsunexport(&exparg) < 0)
53                 return 0;
54         exp->m_exported = 0;
55         return 1;
56 }
57
58 static void
59 str_tolower(char *s)
60 {
61         for ( ; *s; s++)
62                 if (isupper(*s))
63                         *s = tolower(*s);
64 }
65
66 static int
67 cltsetup(struct nfsctl_client *cltarg, nfs_client *clp)
68 {
69         int i, j;
70
71         if (clp->m_type != MCL_FQDN) {
72                 xlog(L_ERROR, "internal: can't export non-FQDN host");
73                 return 0;
74         }
75         memset(cltarg, 0, sizeof(*cltarg));
76         strncpy(cltarg->cl_ident, clp->m_hostname,
77                 sizeof (cltarg->cl_ident) - 1);
78         str_tolower(cltarg->cl_ident);
79
80         j = 0;
81         for (i = 0; i < cltarg->cl_naddr && i < NFSCLNT_ADDRMAX; i++) {
82                 const struct sockaddr_in *sin = get_addrlist_in(clp, i);
83                 if (sin->sin_family == AF_INET)
84                         cltarg->cl_addrlist[j++] = sin->sin_addr;
85         }
86         if (j == 0) {
87                 xlog(L_ERROR, "internal: no supported addresses in nfs_client");
88                 return 0;
89         }
90
91         cltarg->cl_naddr = j;
92         return 1;
93 }
94
95 static int
96 expsetup(struct nfsctl_export *exparg, nfs_export *exp, int unexport)
97 {
98         nfs_client              *clp = exp->m_client;
99         struct stat             stb;
100
101         if (stat(exp->m_export.e_path, &stb) < 0)
102                 return 0;
103
104         memset(exparg, 0, sizeof(*exparg));
105         strncpy(exparg->ex_path, exp->m_export.e_path,
106                 sizeof (exparg->ex_path) - 1);
107         strncpy(exparg->ex_client, clp->m_hostname,
108                 sizeof (exparg->ex_client) - 1);
109         str_tolower(exparg->ex_client);
110         exparg->ex_flags    = exp->m_export.e_flags;
111         exparg->ex_dev      = (!unexport && (exp->m_export.e_flags & NFSEXP_FSID)) ?
112                         (__nfsd_dev_t)exp->m_export.e_fsid : stb.st_dev;
113         exparg->ex_ino      = stb.st_ino;
114         exparg->ex_anon_uid = exp->m_export.e_anonuid;
115         exparg->ex_anon_gid = exp->m_export.e_anongid;
116
117         return 1;
118 }