5f5acccb96ea340ae697eb980ac1ebae406a3630
[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 #include "config.h"
10
11 #include <sys/stat.h>
12 #include <stdlib.h>
13 #include <errno.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include "nfslib.h"
17 #include "exportfs.h"
18 #include "xio.h"
19
20 static int      expsetup(struct nfsctl_export *exparg, nfs_export *exp, int unexport);
21 static int      cltsetup(struct nfsctl_client *cltarg, nfs_client *clp);
22
23 int
24 export_export(nfs_export *exp)
25 {
26         nfs_client *    clp = exp->m_client;
27         struct nfsctl_export    exparg;
28         struct nfsctl_client    cltarg;
29
30         if (!clp->m_exported) {
31                 if (!cltsetup(&cltarg, clp))
32                         return 0;
33                 if (nfsaddclient(&cltarg) < 0)
34                         return 0;
35                 clp->m_exported = 1;
36         }
37         if (!expsetup(&exparg, exp, 0))
38                 return 0;
39         if (nfsexport(&exparg) < 0)
40                 return 0;
41         exp->m_exported = 1;
42         return 1;
43 }
44
45 int
46 export_unexport(nfs_export *exp)
47 {
48         struct nfsctl_export    exparg;
49
50         if (!expsetup(&exparg, exp, 1) || nfsunexport(&exparg) < 0)
51                 return 0;
52         exp->m_exported = 0;
53         return 1;
54 }
55
56 static void
57 str_tolower(char *s)
58 {
59         for ( ; *s; s++)
60                 if (isupper(*s))
61                         *s = tolower(*s);
62 }
63
64 static int
65 cltsetup(struct nfsctl_client *cltarg, nfs_client *clp)
66 {
67         int     i;
68
69         if (clp->m_type != MCL_FQDN) {
70                 xlog(L_ERROR, "internal: can't export non-FQDN host");
71                 return 0;
72         }
73         memset(cltarg, 0, sizeof(*cltarg));
74         strncpy(cltarg->cl_ident, clp->m_hostname,
75                 sizeof (cltarg->cl_ident) - 1);
76         str_tolower(cltarg->cl_ident);
77         cltarg->cl_naddr = clp->m_naddr;
78         for (i = 0; i < cltarg->cl_naddr && i < NFSCLNT_ADDRMAX; i++)
79                 cltarg->cl_addrlist[i] = clp->m_addrlist[i];
80
81         return 1;
82 }
83
84 static int
85 expsetup(struct nfsctl_export *exparg, nfs_export *exp, int unexport)
86 {
87         nfs_client              *clp = exp->m_client;
88         struct stat             stb;
89
90         if (stat(exp->m_export.m_path, &stb) < 0)
91                 return 0;
92
93         if (exp->m_export.e_maptype != CLE_MAP_IDENT) {
94                 xlog(L_ERROR, "%s: unsupported mapping; kernel supports only 'identity' (default)",
95                      exp->m_export.m_path);
96                 errno = EINVAL;
97                 return 0;
98         }
99         memset(exparg, 0, sizeof(*exparg));
100         strncpy(exparg->ex_path, exp->m_export.m_path,
101                 sizeof (exparg->ex_path) - 1);
102         strncpy(exparg->ex_client, clp->m_hostname,
103                 sizeof (exparg->ex_client) - 1);
104         str_tolower(exparg->ex_client);
105         exparg->ex_flags    = exp->m_export.e_flags;
106         exparg->ex_dev      = (!unexport && (exp->m_export.e_flags & NFSEXP_FSID)) ?
107                                 exp->m_export.e_fsid : stb.st_dev;
108         exparg->ex_ino      = stb.st_ino;
109         exparg->ex_anon_uid = exp->m_export.e_anonuid;
110         exparg->ex_anon_gid = exp->m_export.e_anongid;
111
112         return 1;
113 }