Initial revision
[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         memset(exparg, 0, sizeof(*exparg));
93         strncpy(exparg->ex_path, exp->m_export.m_path,
94                 sizeof (exparg->ex_path) - 1);
95         strncpy(exparg->ex_client, clp->m_hostname,
96                 sizeof (exparg->ex_client) - 1);
97         str_tolower(exparg->ex_client);
98         exparg->ex_flags    = exp->m_export.e_flags;
99         exparg->ex_dev      = stb.st_dev;
100         exparg->ex_ino      = stb.st_ino;
101         exparg->ex_anon_uid = exp->m_export.e_anonuid;
102         exparg->ex_anon_gid = exp->m_export.e_anongid;
103
104         return 1;
105 }