summaryrefslogtreecommitdiffstats
path: root/support/export/rmtab.c
blob: 44a0edce728bbed6e87fa25911bfafac682e1476 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 * support/export/rmntab.c
 *
 * Interface to the rmnt file.
 *
 */

#include "config.h"

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "xmalloc.h"
#include "misc.h"
#include "nfslib.h"
#include "exportfs.h"
#include "xio.h"
#include "xlog.h"

int
rmtab_read(void)
{
	struct rmtabent		*rep;
	nfs_export		*exp;

	setrmtabent("r");
	while ((rep = getrmtabent(1)) != NULL) {
		exp = export_lookup(rep->r_client, rep->r_path);
		if (!exp) {
			struct exportent	*xp;
			struct hostent		*hp;
			int			htype;

			htype = client_gettype(rep->r_client);
			if (htype == MCL_FQDN
			    && (hp = gethostbyname (rep->r_client), hp)
			    && (hp = hostent_dup (hp),
				   xp = export_allowed (hp, rep->r_path))) {
				strncpy (xp->e_hostname, rep->r_client,
					 sizeof (xp->e_hostname) - 1);
				xp->e_hostname[sizeof (xp->e_hostname) -1] = '\0';
				exp = export_create(xp);
				free (hp);
			}

			if (!exp)
				continue;
			exp->m_mayexport = 1;
		}
	}
	if (errno == EINVAL) {
		/* Something goes wrong. We need to fix the rmtab
		   file. */
		int	lockid;
		FILE	*fp;
		if ((lockid = xflock(_PATH_RMTAB, "w")) < 0)
			return -1;
		rewindrmtabent();
		if (!(fp = fsetrmtabent(_PATH_RMTABTMP, "w"))) {
			endrmtabent ();
			xfunlock(lockid);
			return -1;
		}
		while ((rep = getrmtabent(0)) != NULL) {
			fputrmtabent(fp, rep);
		}
		if (rename(_PATH_RMTABTMP, _PATH_RMTAB) < 0) {
			xlog(L_ERROR, "couldn't rename %s to %s",
			     _PATH_RMTABTMP, _PATH_RMTAB);
		}
		endrmtabent();
		fendrmtabent(fp);
		xfunlock(lockid);
	}
	else {
		endrmtabent();
	}
	return 0;
}