summaryrefslogtreecommitdiffstats
path: root/fs/nametrans.c
blob: 15c98ed7072940641ea0aecc43c4bcaaa606c953 (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 * $Id: nametrans.c,v 1.2 1997/06/04 23:45:44 davem Exp $
 *
 * linux/fs/nametrans.c - context-dependend filename suffixes.
 * Copyright (C) 1997, Thomas Schoebel-Theuer,
 * <schoebel@informatik.uni-stuttgart.de>.
 *
 * translates names of the form "filename#host=myhost#" to "filename"
 * as if both names were hardlinked to the same file.
 * benefit: diskless clients can mount the / filesystem of the
 * server if /etc/fstab (and other config files) are organized using
 * context suffixes.
 */

#include <linux/config.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/utsname.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <asm/uaccess.h>
#include <linux/nametrans.h>

char  nametrans_txt[MAX_DEFAULT_TRANSLEN] = "";
static struct translations * global_trans = NULL;
static int default_trans = 1;
static const char version[] = "revision: 2.3 <schoebel@informatik.uni-stuttgart.de>";
int translations_dirty = 1;
static char * transl_names[] = {
#ifdef CONFIG_TR_NODENAME
  "host=",        system_utsname.nodename,
#endif
#ifdef CONFIG_TR_KERNNAME
  "kname=",  CONFIG_KERNNAME,
#endif
#ifdef CONFIG_TR_KERNTYPE
  "ktype=",  CONFIG_KERNTYPE,
#endif
#ifdef CONFIG_TR_MACHINE
  "machine=",     system_utsname.machine,
#endif
#ifdef CONFIG_TR_SYSNAME
  "system=",      system_utsname.sysname,
#endif
  0, 0
};

/* Convert and do syntax checking. */
static void convert(char * txt, struct translations * res)
{
	char * tmp = txt;
	char * space = (char*)res + sizeof(struct translations);

	res->count = 0;
	while(*tmp) {
		struct qstr * name = &res->name[res->count];
		struct qstr * c_name = &res->c_name[res->count];
		int len;
		char * p = tmp;

		if(*p++ != '#')
			goto next;
		while(*p && *p != '=' && *p != ':')
			p++;
		if(*p != '=')
			goto next;
		p++;
		len = (unsigned long)p - (unsigned long)tmp;
		c_name->name = space;
		memcpy(space, tmp, len);
		memcpy(space + len, "CREATE#", 8);
		c_name->len = len + 7;
		if(c_name->len >= MAX_TRANS_SUFFIX)
			goto next;
		while(*p && *p != '#' && *p != ':')
			p++;
		if(*p != '#')
			goto next;
		p++;
		if(*p != ':' && *p)
			goto next;
		space += len + 8;
		name->len = len = (unsigned long)p - (unsigned long)tmp;
		if(len >= MAX_TRANS_SUFFIX)
			goto next;
		name->name = space;
		memcpy(space, tmp, len);
		space[len] = '\0';
		space += len + 1;
		res->count++;
		if(res->count >= MAX_TRANSLATIONS ||
		   (unsigned long)space - (unsigned long)res >= PAGE_SIZE-2*MAX_TRANS_SUFFIX)
			return;
	next:
		while(*p && *p++ != ':') ;
		tmp = p;
	}
}

static inline void trans_to_string(struct translations * trans, char * buf, int maxlen)
{
	int i;

	for(i = 0; i < trans->count; i++) {
		int len = trans->name[i].len;
		if(len < maxlen) {
			memcpy(buf, trans->name[i].name, len);
			buf += len;
			maxlen -= len;
			*buf++ = ':';
			maxlen--;
		}
	}
	buf--;
	*buf = '\0';
}

static inline void default_nametrans(char * buf)
{
	char * res = buf;
	char ** entry;
	char * ptr;

	for (entry = transl_names; *entry; entry++) {
		*res++ = '#';
		for(ptr = *entry; *ptr; ptr++)
			*res++ = *ptr;
		entry++;
		for(ptr = *entry; *ptr; ptr++)
			*res++ = *ptr;
		*res++ = '#';
		*res++ = ':';
	}
	res--;
	*res = '\0';
}

void nametrans_setup(char * line)
{
	if(line) {
		default_trans = (!line[0]);
		if(!global_trans) {
			/* This can happen at boot time, and there is no chance
			 * to allocate memory at this early stage.
			 */
			strncpy(nametrans_txt, line, MAX_DEFAULT_TRANSLEN);
		} else {
			if(default_trans) {
				default_nametrans(nametrans_txt);
				line = nametrans_txt;
			}
			convert(line, global_trans);

			/* Show what really was recognized after parsing... */
			trans_to_string(global_trans, nametrans_txt, MAX_DEFAULT_TRANSLEN);
		}
	}
}

/* If the _first_ environment variable is "NAMETRANS", return
 * a pointer to the list of appendices.
 * You can set the first environment variable using
 * 'env - NAMETRANS=... "`env`" command ...'
 */
char* env_transl(void)
{
	char* env;
	int i;

	if(current && current->mm && (env = (char*)current->mm->env_start)
	   && get_ds() != get_fs()
	   && current->mm->env_end>=current->mm->env_start+10
	   && !verify_area(VERIFY_READ,env,10)) {
		for(i=0; i<10; i++) {
			char c;

			get_user(c, env++);
			if(c != "NAMETRANS="[i])
				return 0;
		}
		return env;
	}
	return 0;
}

/* If name has the correct suffix "#keyword=correct_context#",
 * return position of the suffix, else 0.
 */
char *testname(int restricted, char* name)
{
	char * ptr = name;
	char * cut;
	char * env;
	struct translations * trans;
	int i, len;
	char c, tmp;
	
	env = env_transl();
#ifdef CONFIG_TRANS_RESTRICT
	if(!env && restricted)
		goto done;
#else
	(void)restricted; /* inhibit parameter usage warning */
#endif
	if(get_user(c, ptr))
		goto done;
	while(c && c != '#') {
		ptr++;
		__get_user(c, ptr);
	}
	if(!c)
		goto done;
	cut = ptr++;
	if(get_user(c, ptr))
		goto done;
	while (c && c != '#') {
		ptr++;
		get_user(c, ptr);
	}
	if(!c)
		goto done;
	get_user(tmp, ptr);
	if(tmp)
		goto done;
	trans = get_translations(env);
	len = (unsigned long)ptr - (unsigned long)cut;
	for(i = 0; i < trans->count; i++)
		if(trans->name[i].len == len) {
			const char * p1 = cut;
			const char * p2 = trans->name[i].name;
			get_user(c, p1);
			while(c && c == *p2++) {
				p1++;
				get_user(c, p1);
			}
			if(!c)
				return cut;
		}
done:
	return NULL;
}

static inline void check_dirty(void)
{
	if(translations_dirty && default_trans) {
		nametrans_setup("");
		translations_dirty = 0;
	}
}

struct translations * get_translations(char * env)
{
	struct translations * res;

	if(env) {
		char * env_txt = (char*)__get_free_page(GFP_KERNEL);

		strncpy_from_user(env_txt, env, PAGE_SIZE);
		res = (struct translations *)__get_free_page(GFP_KERNEL);
		convert(env_txt, res);
		free_page((unsigned long)env_txt);
	} else {
		check_dirty();
		res = global_trans;
	}
	return res;
}

int nametrans_dostring(ctl_table * table, int write, struct file * filp,
		       void * buffer, size_t * lenp)
{
	int res;
	check_dirty();
	res = proc_dostring(table, write, filp, buffer, lenp);
	if(!res && write)
		nametrans_setup(nametrans_txt);

	return res;
}

int nametrans_string(ctl_table * table, int * name, int nlen,
		     void * oldval, size_t * oldlenp,
		     void * newval, size_t newlen, void ** context)
{
	int res;
	check_dirty();
	res = sysctl_string(table, name, nlen, oldval, oldlenp, newval, newlen, context);
	if(!res && newval && newlen)
		nametrans_setup(nametrans_txt);

	return res;
}

void init_nametrans(void)
{
	if(!global_trans)
		global_trans = (struct translations*)__get_free_page(GFP_KERNEL);
	if(!global_trans) {
		printk("NAMETRANS: No free memory\n");
		return;
	}
	nametrans_setup(nametrans_txt);

	/* Notify user for the default/supplied translations.
	 * Extremely useful for finding translation problems.
	 */
	printk("Nametrans %s\nNametrans %s: %s\n", version,
	       default_trans ? "default translations" : "external parameter",
	       nametrans_txt);
}