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