summaryrefslogtreecommitdiffstats
path: root/utils/mount/parse_dev.c
blob: d64b83d2ed84621e38d228b23080232bcc223915 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * parse_dev.c -- parse device name into hostname and export path
 *
 * Copyright (C) 2008 Oracle.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 0211-1301 USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "xcommon.h"
#include "nls.h"
#include "parse_dev.h"

#ifndef NFS_MAXHOSTNAME
#define NFS_MAXHOSTNAME		(255)
#endif

#ifndef NFS_MAXPATHNAME
#define NFS_MAXPATHNAME		(1024)
#endif

extern char *progname;
extern int verbose;

static int nfs_pdn_no_devname_err(void)
{
	nfs_error(_("%s: no device name was provided"), progname);
	return 0;
}

static int nfs_pdn_hostname_too_long_err(void)
{
	nfs_error(_("%s: server hostname is too long"), progname);
	return 0;
}

static int nfs_pdn_pathname_too_long_err(void)
{
	nfs_error(_("%s: export pathname is too long"), progname);
	return 0;
}

static int nfs_pdn_bad_format_err(void)
{
	nfs_error(_("%s: remote share not in 'host:dir' format"), progname);
	return 0;
}

static int nfs_pdn_nomem_err(void)
{
	nfs_error(_("%s: no memory available to parse devname"), progname);
	return 0;
}

static int nfs_pdn_missing_brace_err(void)
{
	nfs_error(_("%s: closing bracket missing from server address"),
				progname);
	return 0;
}

/*
 * Standard hostname:path format
 */
static int nfs_parse_simple_hostname(const char *dev,
				     char **hostname, char **pathname)
{
	size_t host_len, path_len;
	char *colon, *comma;

	/* Must have a colon */
	colon = strchr(dev, ':');
	if (colon == NULL)
		return nfs_pdn_bad_format_err();
	*colon = '\0';
	host_len = colon - dev;

	if (host_len > NFS_MAXHOSTNAME)
		return nfs_pdn_hostname_too_long_err();

	/* If there's a comma before the colon, take only the
	 * first name in list */
	comma = strchr(dev, ',');
	if (comma != NULL) {
		*comma = '\0';
		host_len = comma - dev;
		nfs_error(_("%s: warning: multiple hostnames not supported"),
				progname);
	} else

	colon++;
	path_len = strlen(colon);
	if (path_len > NFS_MAXPATHNAME)
		return nfs_pdn_pathname_too_long_err();

	if (hostname) {
		*hostname = strndup(dev, host_len);
		if (*hostname == NULL)
			return nfs_pdn_nomem_err();
	}
	if (pathname) {
		*pathname = strndup(colon, path_len);
		if (*pathname == NULL) {
			free(*hostname);
			return nfs_pdn_nomem_err();
		}
	}
	return 1;
}

/*
 * To handle raw IPv6 addresses (which contain colons), the
 * server's address is enclosed in square brackets.  Return
 * what's between the brackets.
 *
 * There could be anything in between the brackets, but we'll
 * let DNS resolution sort it out later.
 */
static int nfs_parse_square_bracket(const char *dev,
				    char **hostname, char **pathname)
{
	size_t host_len, path_len;
	char *cbrace;

	dev++;

	/* Must have a closing square bracket */
	cbrace = strchr(dev, ']');
	if (cbrace == NULL)
		return nfs_pdn_missing_brace_err();
	*cbrace = '\0';
	host_len = cbrace - dev;

	/* Must have a colon just after the closing bracket */
	cbrace++;
	if (*cbrace != ':')
		return nfs_pdn_bad_format_err();

	if (host_len > NFS_MAXHOSTNAME)
		return nfs_pdn_hostname_too_long_err();

	cbrace++;
	path_len = strlen(cbrace);
	if (path_len > NFS_MAXPATHNAME)
		return nfs_pdn_pathname_too_long_err();

	if (hostname) {
		*hostname = strndup(dev, host_len);
		if (*hostname == NULL)
			return nfs_pdn_nomem_err();
	}
	if (pathname) {
		*pathname = strndup(cbrace, path_len);
		if (*pathname == NULL) {
			free(*hostname);
			return nfs_pdn_nomem_err();
		}
	}
	return 1;
}

/*
 * RFC 2224 says an NFS client must grok "public file handles" to
 * support NFS URLs.  Linux doesn't do that yet.  Print a somewhat
 * helpful error message in this case instead of pressing forward
 * with the mount request and failing with a cryptic error message
 * later.
 */
static int nfs_parse_nfs_url(__attribute__((unused)) const char *dev,
			     __attribute__((unused)) char **hostname,
			     __attribute__((unused)) char **pathname)
{
	nfs_error(_("%s: NFS URLs are not supported"), progname);
	return 0;
}

/**
 * nfs_parse_devname - Determine the server's hostname by looking at "devname".
 * @devname: pointer to mounted device name (first argument of mount command)
 * @hostname: OUT: pointer to server's hostname
 * @pathname: OUT: pointer to export path on server
 *
 * Returns 1 if succesful, or zero if some error occurred.  On success,
 * @hostname and @pathname point to dynamically allocated buffers containing
 * the hostname of the server and the export pathname (both '\0'-terminated).
 *
 * @hostname or @pathname may be NULL if caller doesn't want a copy of those
 * parts of @devname.
 *
 * Note that this will not work if @devname is a wide-character string.
 */
int nfs_parse_devname(const char *devname,
		      char **hostname, char **pathname)
{
	char *dev;
	int result;

	if (devname == NULL)
		return nfs_pdn_no_devname_err();

	/* Parser is destructive, so operate on a copy of the device name. */
	dev = strdup(devname);
	if (dev == NULL)
		return nfs_pdn_nomem_err();
	if (*dev == '[')
		result = nfs_parse_square_bracket(dev, hostname, pathname);
	else if (strncmp(dev, "nfs://", 6) == 0)
		result = nfs_parse_nfs_url(dev, hostname, pathname);
	else
		result = nfs_parse_simple_hostname(dev, hostname, pathname);

	free(dev);
	return result;
}