ioctl.c 21.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9
/*
 *  ioctl.c
 *
 *  Copyright (C) 1995, 1996 by Volker Lendecke
 *  Modified 1997 Peter Waltenberg, Bill Hawes, David Woodhouse for 2.1 dcache
 *  Modified 1998, 1999 Wolfram Pienkoss for NLS
 *
 */

10
#include <linux/capability.h>
11
#include <linux/compat.h>
L
Linus Torvalds 已提交
12 13 14 15 16
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/ioctl.h>
#include <linux/time.h>
#include <linux/mm.h>
17
#include <linux/mount.h>
L
Linus Torvalds 已提交
18
#include <linux/highuid.h>
19
#include <linux/smp_lock.h>
L
Linus Torvalds 已提交
20
#include <linux/vmalloc.h>
A
Alexey Dobriyan 已提交
21
#include <linux/sched.h>
L
Linus Torvalds 已提交
22 23 24

#include <linux/ncp_fs.h>

25 26
#include <asm/uaccess.h>

L
Linus Torvalds 已提交
27 28 29 30 31 32 33 34 35 36
#include "ncplib_kernel.h"

/* maximum limit for ncp_objectname_ioctl */
#define NCP_OBJECT_NAME_MAX_LEN	4096
/* maximum limit for ncp_privatedata_ioctl */
#define NCP_PRIVATE_DATA_MAX_LEN 8192
/* maximum negotiable packet size */
#define NCP_PACKET_SIZE_INTERNAL 65536

static int
37 38
ncp_get_fs_info(struct ncp_server * server, struct file *file,
		struct ncp_fs_info __user *arg)
L
Linus Torvalds 已提交
39
{
J
Josef Sipek 已提交
40
	struct inode *inode = file->f_path.dentry->d_inode;
L
Linus Torvalds 已提交
41 42
	struct ncp_fs_info info;

43 44
	if (file_permission(file, MAY_WRITE) != 0
	    && current_uid() != server->m.mounted_uid)
L
Linus Torvalds 已提交
45
		return -EACCES;
46

L
Linus Torvalds 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
	if (copy_from_user(&info, arg, sizeof(info)))
		return -EFAULT;

	if (info.version != NCP_GET_FS_INFO_VERSION) {
		DPRINTK("info.version invalid: %d\n", info.version);
		return -EINVAL;
	}
	/* TODO: info.addr = server->m.serv_addr; */
	SET_UID(info.mounted_uid, server->m.mounted_uid);
	info.connection		= server->connection;
	info.buffer_size	= server->buffer_size;
	info.volume_number	= NCP_FINFO(inode)->volNumber;
	info.directory_id	= NCP_FINFO(inode)->DosDirNum;

	if (copy_to_user(arg, &info, sizeof(info)))
		return -EFAULT;
	return 0;
}

static int
67 68
ncp_get_fs_info_v2(struct ncp_server * server, struct file *file,
		   struct ncp_fs_info_v2 __user * arg)
L
Linus Torvalds 已提交
69
{
J
Josef Sipek 已提交
70
	struct inode *inode = file->f_path.dentry->d_inode;
L
Linus Torvalds 已提交
71 72
	struct ncp_fs_info_v2 info2;

73 74
	if (file_permission(file, MAY_WRITE) != 0
	    && current_uid() != server->m.mounted_uid)
L
Linus Torvalds 已提交
75
		return -EACCES;
76

L
Linus Torvalds 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
	if (copy_from_user(&info2, arg, sizeof(info2)))
		return -EFAULT;

	if (info2.version != NCP_GET_FS_INFO_VERSION_V2) {
		DPRINTK("info.version invalid: %d\n", info2.version);
		return -EINVAL;
	}
	info2.mounted_uid   = server->m.mounted_uid;
	info2.connection    = server->connection;
	info2.buffer_size   = server->buffer_size;
	info2.volume_number = NCP_FINFO(inode)->volNumber;
	info2.directory_id  = NCP_FINFO(inode)->DosDirNum;
	info2.dummy1 = info2.dummy2 = info2.dummy3 = 0;

	if (copy_to_user(arg, &info2, sizeof(info2)))
		return -EFAULT;
	return 0;
}

96 97 98 99 100
#ifdef CONFIG_COMPAT
struct compat_ncp_objectname_ioctl
{
	s32		auth_type;
	u32		object_name_len;
101
	compat_caddr_t	object_name;	/* a userspace data, in most cases user name */
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
};

struct compat_ncp_fs_info_v2 {
	s32 version;
	u32 mounted_uid;
	u32 connection;
	u32 buffer_size;

	u32 volume_number;
	u32 directory_id;

	u32 dummy1;
	u32 dummy2;
	u32 dummy3;
};

struct compat_ncp_ioctl_request {
	u32 function;
	u32 size;
	compat_caddr_t data;
};

struct compat_ncp_privatedata_ioctl
{
	u32		len;
	compat_caddr_t	data;		/* ~1000 for NDS */
};

#define NCP_IOC_GET_FS_INFO_V2_32	_IOWR('n', 4, struct compat_ncp_fs_info_v2)
#define NCP_IOC_NCPREQUEST_32		_IOR('n', 1, struct compat_ncp_ioctl_request)
#define NCP_IOC_GETOBJECTNAME_32	_IOWR('n', 9, struct compat_ncp_objectname_ioctl)
#define NCP_IOC_SETOBJECTNAME_32	_IOR('n', 9, struct compat_ncp_objectname_ioctl)
#define NCP_IOC_GETPRIVATEDATA_32	_IOWR('n', 10, struct compat_ncp_privatedata_ioctl)
#define NCP_IOC_SETPRIVATEDATA_32	_IOR('n', 10, struct compat_ncp_privatedata_ioctl)

static int
ncp_get_compat_fs_info_v2(struct ncp_server * server, struct file *file,
		   struct compat_ncp_fs_info_v2 __user * arg)
{
J
Josef Sipek 已提交
141
	struct inode *inode = file->f_path.dentry->d_inode;
142 143
	struct compat_ncp_fs_info_v2 info2;

144 145
	if (file_permission(file, MAY_WRITE) != 0
	    && current_uid() != server->m.mounted_uid)
146
		return -EACCES;
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
	if (copy_from_user(&info2, arg, sizeof(info2)))
		return -EFAULT;

	if (info2.version != NCP_GET_FS_INFO_VERSION_V2) {
		DPRINTK("info.version invalid: %d\n", info2.version);
		return -EINVAL;
	}
	info2.mounted_uid   = server->m.mounted_uid;
	info2.connection    = server->connection;
	info2.buffer_size   = server->buffer_size;
	info2.volume_number = NCP_FINFO(inode)->volNumber;
	info2.directory_id  = NCP_FINFO(inode)->DosDirNum;
	info2.dummy1 = info2.dummy2 = info2.dummy3 = 0;

	if (copy_to_user(arg, &info2, sizeof(info2)))
		return -EFAULT;
	return 0;
}
#endif

#define NCP_IOC_GETMOUNTUID16		_IOW('n', 2, u16)
#define NCP_IOC_GETMOUNTUID32		_IOW('n', 2, u32)
#define NCP_IOC_GETMOUNTUID64		_IOW('n', 2, u64)

L
Linus Torvalds 已提交
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
#ifdef CONFIG_NCPFS_NLS
/* Here we are select the iocharset and the codepage for NLS.
 * Thanks Petr Vandrovec for idea and many hints.
 */
static int
ncp_set_charsets(struct ncp_server* server, struct ncp_nls_ioctl __user *arg)
{
	struct ncp_nls_ioctl user;
	struct nls_table *codepage;
	struct nls_table *iocharset;
	struct nls_table *oldset_io;
	struct nls_table *oldset_cp;

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;
	if (server->root_setuped)
		return -EBUSY;

	if (copy_from_user(&user, arg, sizeof(user)))
		return -EFAULT;

	codepage = NULL;
	user.codepage[NCP_IOCSNAME_LEN] = 0;
	if (!user.codepage[0] || !strcmp(user.codepage, "default"))
		codepage = load_nls_default();
	else {
		codepage = load_nls(user.codepage);
		if (!codepage) {
			return -EBADRQC;
		}
	}

	iocharset = NULL;
	user.iocharset[NCP_IOCSNAME_LEN] = 0;
	if (!user.iocharset[0] || !strcmp(user.iocharset, "default")) {
		iocharset = load_nls_default();
		NCP_CLR_FLAG(server, NCP_FLAG_UTF8);
	} else if (!strcmp(user.iocharset, "utf8")) {
		iocharset = load_nls_default();
		NCP_SET_FLAG(server, NCP_FLAG_UTF8);
	} else {
		iocharset = load_nls(user.iocharset);
		if (!iocharset) {
			unload_nls(codepage);
			return -EBADRQC;
		}
		NCP_CLR_FLAG(server, NCP_FLAG_UTF8);
	}

	oldset_cp = server->nls_vol;
	server->nls_vol = codepage;
	oldset_io = server->nls_io;
	server->nls_io = iocharset;

	if (oldset_cp)
		unload_nls(oldset_cp);
	if (oldset_io)
		unload_nls(oldset_io);

	return 0;
}

static int
ncp_get_charsets(struct ncp_server* server, struct ncp_nls_ioctl __user *arg)
{
	struct ncp_nls_ioctl user;
	int len;

	memset(&user, 0, sizeof(user));
	if (server->nls_vol && server->nls_vol->charset) {
		len = strlen(server->nls_vol->charset);
		if (len > NCP_IOCSNAME_LEN)
			len = NCP_IOCSNAME_LEN;
		strncpy(user.codepage, server->nls_vol->charset, len);
		user.codepage[len] = 0;
	}

	if (NCP_IS_FLAG(server, NCP_FLAG_UTF8))
		strcpy(user.iocharset, "utf8");
	else if (server->nls_io && server->nls_io->charset) {
		len = strlen(server->nls_io->charset);
		if (len > NCP_IOCSNAME_LEN)
			len = NCP_IOCSNAME_LEN;
		strncpy(user.iocharset,	server->nls_io->charset, len);
		user.iocharset[len] = 0;
	}

	if (copy_to_user(arg, &user, sizeof(user)))
		return -EFAULT;
	return 0;
}
#endif /* CONFIG_NCPFS_NLS */

265
static int __ncp_ioctl(struct inode *inode, struct file *filp,
L
Linus Torvalds 已提交
266 267 268 269 270 271 272
	      unsigned int cmd, unsigned long arg)
{
	struct ncp_server *server = NCP_SERVER(inode);
	int result;
	struct ncp_ioctl_request request;
	char* bouncebuffer;
	void __user *argp = (void __user *)arg;
273
	uid_t uid = current_uid();
L
Linus Torvalds 已提交
274 275

	switch (cmd) {
276 277 278
#ifdef CONFIG_COMPAT
	case NCP_IOC_NCPREQUEST_32:
#endif
L
Linus Torvalds 已提交
279
	case NCP_IOC_NCPREQUEST:
280 281
		if (file_permission(filp, MAY_WRITE) != 0
		    && uid != server->m.mounted_uid)
L
Linus Torvalds 已提交
282
			return -EACCES;
283

284 285 286 287 288 289 290 291 292 293
#ifdef CONFIG_COMPAT
		if (cmd == NCP_IOC_NCPREQUEST_32) {
			struct compat_ncp_ioctl_request request32;
			if (copy_from_user(&request32, argp, sizeof(request32)))
				return -EFAULT;
			request.function = request32.function;
			request.size = request32.size;
			request.data = compat_ptr(request32.data);
		} else
#endif
L
Linus Torvalds 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
		if (copy_from_user(&request, argp, sizeof(request)))
			return -EFAULT;

		if ((request.function > 255)
		    || (request.size >
		  NCP_PACKET_SIZE - sizeof(struct ncp_request_header))) {
			return -EINVAL;
		}
		bouncebuffer = vmalloc(NCP_PACKET_SIZE_INTERNAL);
		if (!bouncebuffer)
			return -ENOMEM;
		if (copy_from_user(bouncebuffer, request.data, request.size)) {
			vfree(bouncebuffer);
			return -EFAULT;
		}
		ncp_lock_server(server);

		/* FIXME: We hack around in the server's structures
		   here to be able to use ncp_request */

		server->has_subfunction = 0;
		server->current_size = request.size;
		memcpy(server->packet, bouncebuffer, request.size);

		result = ncp_request2(server, request.function, 
			bouncebuffer, NCP_PACKET_SIZE_INTERNAL);
		if (result < 0)
			result = -EIO;
		else
			result = server->reply_size;
		ncp_unlock_server(server);
		DPRINTK("ncp_ioctl: copy %d bytes\n",
			result);
		if (result >= 0)
			if (copy_to_user(request.data, bouncebuffer, result))
				result = -EFAULT;
		vfree(bouncebuffer);
		return result;

	case NCP_IOC_CONN_LOGGED_IN:

		if (!capable(CAP_SYS_ADMIN))
			return -EACCES;
		if (!(server->m.int_flags & NCP_IMOUNT_LOGGEDIN_POSSIBLE))
			return -EINVAL;
		if (server->root_setuped)
			return -EBUSY;
		server->root_setuped = 1;
		return ncp_conn_logged_in(inode->i_sb);

	case NCP_IOC_GET_FS_INFO:
345
		return ncp_get_fs_info(server, filp, argp);
L
Linus Torvalds 已提交
346 347

	case NCP_IOC_GET_FS_INFO_V2:
348
		return ncp_get_fs_info_v2(server, filp, argp);
L
Linus Torvalds 已提交
349

350 351 352 353 354 355 356 357 358 359
#ifdef CONFIG_COMPAT
	case NCP_IOC_GET_FS_INFO_V2_32:
		return ncp_get_compat_fs_info_v2(server, filp, argp);
#endif
	/* we have too many combinations of CONFIG_COMPAT,
	 * CONFIG_64BIT and CONFIG_UID16, so just handle
	 * any of the possible ioctls */
	case NCP_IOC_GETMOUNTUID16:
	case NCP_IOC_GETMOUNTUID32:
	case NCP_IOC_GETMOUNTUID64:
360 361
		if (file_permission(filp, MAY_READ) != 0
			&& uid != server->m.mounted_uid)
362
			return -EACCES;
363

364 365 366 367 368 369 370 371 372 373 374 375
		if (cmd == NCP_IOC_GETMOUNTUID16) {
			u16 uid;
			SET_UID(uid, server->m.mounted_uid);
			if (put_user(uid, (u16 __user *)argp))
				return -EFAULT;
		} else if (cmd == NCP_IOC_GETMOUNTUID32) {
			if (put_user(server->m.mounted_uid,
						(u32 __user *)argp))
				return -EFAULT;
		} else {
			if (put_user(server->m.mounted_uid,
						(u64 __user *)argp))
L
Linus Torvalds 已提交
376 377
				return -EFAULT;
		}
378
		return 0;
L
Linus Torvalds 已提交
379 380 381 382 383

	case NCP_IOC_GETROOT:
		{
			struct ncp_setroot_ioctl sr;

384 385
			if (file_permission(filp, MAY_READ) != 0
			    && uid != server->m.mounted_uid)
L
Linus Torvalds 已提交
386
				return -EACCES;
387

L
Linus Torvalds 已提交
388 389 390 391
			if (server->m.mounted_vol[0]) {
				struct dentry* dentry = inode->i_sb->s_root;

				if (dentry) {
392
					struct inode* s_inode = dentry->d_inode;
L
Linus Torvalds 已提交
393
				
394 395 396
					if (s_inode) {
						sr.volNumber = NCP_FINFO(s_inode)->volNumber;
						sr.dirEntNum = NCP_FINFO(s_inode)->dirEntNum;
L
Linus Torvalds 已提交
397 398 399 400 401 402 403 404 405 406 407 408 409 410
						sr.namespace = server->name_space[sr.volNumber];
					} else
						DPRINTK("ncpfs: s_root->d_inode==NULL\n");
				} else
					DPRINTK("ncpfs: s_root==NULL\n");
			} else {
				sr.volNumber = -1;
				sr.namespace = 0;
				sr.dirEntNum = 0;
			}
			if (copy_to_user(argp, &sr, sizeof(sr)))
				return -EFAULT;
			return 0;
		}
411

L
Linus Torvalds 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
	case NCP_IOC_SETROOT:
		{
			struct ncp_setroot_ioctl sr;
			__u32 vnum;
			__le32 de;
			__le32 dosde;
			struct dentry* dentry;

			if (!capable(CAP_SYS_ADMIN))
			{
				return -EACCES;
			}
			if (server->root_setuped) return -EBUSY;
			if (copy_from_user(&sr, argp, sizeof(sr)))
				return -EFAULT;
			if (sr.volNumber < 0) {
				server->m.mounted_vol[0] = 0;
				vnum = NCP_NUMBER_OF_VOLUMES;
				de = 0;
				dosde = 0;
			} else if (sr.volNumber >= NCP_NUMBER_OF_VOLUMES) {
				return -EINVAL;
			} else if (ncp_mount_subdir(server, sr.volNumber,
						sr.namespace, sr.dirEntNum,
						&vnum, &de, &dosde)) {
				return -ENOENT;
			}
			
			dentry = inode->i_sb->s_root;
			server->root_setuped = 1;
			if (dentry) {
443
				struct inode* s_inode = dentry->d_inode;
L
Linus Torvalds 已提交
444 445
				
				if (inode) {
446 447 448
					NCP_FINFO(s_inode)->volNumber = vnum;
					NCP_FINFO(s_inode)->dirEntNum = de;
					NCP_FINFO(s_inode)->DosDirNum = dosde;
L
Linus Torvalds 已提交
449 450 451 452 453 454 455 456 457 458
				} else
					DPRINTK("ncpfs: s_root->d_inode==NULL\n");
			} else
				DPRINTK("ncpfs: s_root==NULL\n");

			return 0;
		}

#ifdef CONFIG_NCPFS_PACKET_SIGNING	
	case NCP_IOC_SIGN_INIT:
459 460
		if (file_permission(filp, MAY_WRITE) != 0
		    && uid != server->m.mounted_uid)
L
Linus Torvalds 已提交
461
			return -EACCES;
462

L
Linus Torvalds 已提交
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
		if (argp) {
			if (server->sign_wanted)
			{
				struct ncp_sign_init sign;

				if (copy_from_user(&sign, argp, sizeof(sign)))
					return -EFAULT;
				memcpy(server->sign_root,sign.sign_root,8);
				memcpy(server->sign_last,sign.sign_last,16);
				server->sign_active = 1;
			}
			/* ignore when signatures not wanted */
		} else {
			server->sign_active = 0;
		}
		return 0;		
		
        case NCP_IOC_SIGN_WANTED:
481 482
		if (file_permission(filp, MAY_READ) != 0
		    && uid != server->m.mounted_uid)
L
Linus Torvalds 已提交
483 484 485 486 487
			return -EACCES;
		
                if (put_user(server->sign_wanted, (int __user *)argp))
			return -EFAULT;
                return 0;
488

L
Linus Torvalds 已提交
489 490 491 492
	case NCP_IOC_SET_SIGN_WANTED:
		{
			int newstate;

493 494
			if (file_permission(filp, MAY_WRITE) != 0
			    && uid != server->m.mounted_uid)
L
Linus Torvalds 已提交
495
				return -EACCES;
496

L
Linus Torvalds 已提交
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
			/* get only low 8 bits... */
			if (get_user(newstate, (unsigned char __user *)argp))
				return -EFAULT;
			if (server->sign_active) {
				/* cannot turn signatures OFF when active */
				if (!newstate) return -EINVAL;
			} else {
				server->sign_wanted = newstate != 0;
			}
			return 0;
		}

#endif /* CONFIG_NCPFS_PACKET_SIGNING */

#ifdef CONFIG_NCPFS_IOCTL_LOCKING
	case NCP_IOC_LOCKUNLOCK:
513 514
		if (file_permission(filp, MAY_WRITE) != 0
		    && uid != server->m.mounted_uid)
L
Linus Torvalds 已提交
515
			return -EACCES;
516

L
Linus Torvalds 已提交
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
		{
			struct ncp_lock_ioctl	 rqdata;

			if (copy_from_user(&rqdata, argp, sizeof(rqdata)))
				return -EFAULT;
			if (rqdata.origin != 0)
				return -EINVAL;
			/* check for cmd */
			switch (rqdata.cmd) {
				case NCP_LOCK_EX:
				case NCP_LOCK_SH:
						if (rqdata.timeout == 0)
							rqdata.timeout = NCP_LOCK_DEFAULT_TIMEOUT;
						else if (rqdata.timeout > NCP_LOCK_MAX_TIMEOUT)
							rqdata.timeout = NCP_LOCK_MAX_TIMEOUT;
						break;
				case NCP_LOCK_LOG:
						rqdata.timeout = NCP_LOCK_DEFAULT_TIMEOUT;	/* has no effect */
				case NCP_LOCK_CLEAR:
						break;
				default:
						return -EINVAL;
			}
			/* locking needs both read and write access */
			if ((result = ncp_make_open(inode, O_RDWR)) != 0)
			{
				return result;
			}
			result = -EIO;
			if (!ncp_conn_valid(server))
				goto outrel;
			result = -EISDIR;
			if (!S_ISREG(inode->i_mode))
				goto outrel;
			if (rqdata.cmd == NCP_LOCK_CLEAR)
			{
				result = ncp_ClearPhysicalRecord(NCP_SERVER(inode),
							NCP_FINFO(inode)->file_handle, 
							rqdata.offset,
							rqdata.length);
				if (result > 0) result = 0;	/* no such lock */
			}
			else
			{
				int lockcmd;

				switch (rqdata.cmd)
				{
					case NCP_LOCK_EX:  lockcmd=1; break;
					case NCP_LOCK_SH:  lockcmd=3; break;
					default:	   lockcmd=0; break;
				}
				result = ncp_LogPhysicalRecord(NCP_SERVER(inode),
							NCP_FINFO(inode)->file_handle,
							lockcmd,
							rqdata.offset,
							rqdata.length,
							rqdata.timeout);
				if (result > 0) result = -EAGAIN;
			}
outrel:			
			ncp_inode_close(inode);
			return result;
		}
#endif	/* CONFIG_NCPFS_IOCTL_LOCKING */

583 584
#ifdef CONFIG_COMPAT
	case NCP_IOC_GETOBJECTNAME_32:
585
		if (uid != server->m.mounted_uid)
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
			return -EACCES;
		{
			struct compat_ncp_objectname_ioctl user;
			size_t outl;

			if (copy_from_user(&user, argp, sizeof(user)))
				return -EFAULT;
			user.auth_type = server->auth.auth_type;
			outl = user.object_name_len;
			user.object_name_len = server->auth.object_name_len;
			if (outl > user.object_name_len)
				outl = user.object_name_len;
			if (outl) {
				if (copy_to_user(compat_ptr(user.object_name),
						 server->auth.object_name,
						 outl)) return -EFAULT;
			}
			if (copy_to_user(argp, &user, sizeof(user)))
				return -EFAULT;
			return 0;
		}
#endif
608

L
Linus Torvalds 已提交
609
	case NCP_IOC_GETOBJECTNAME:
610
		if (uid != server->m.mounted_uid)
L
Linus Torvalds 已提交
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
			return -EACCES;
		{
			struct ncp_objectname_ioctl user;
			size_t outl;

			if (copy_from_user(&user, argp, sizeof(user)))
				return -EFAULT;
			user.auth_type = server->auth.auth_type;
			outl = user.object_name_len;
			user.object_name_len = server->auth.object_name_len;
			if (outl > user.object_name_len)
				outl = user.object_name_len;
			if (outl) {
				if (copy_to_user(user.object_name,
						 server->auth.object_name,
						 outl)) return -EFAULT;
			}
			if (copy_to_user(argp, &user, sizeof(user)))
				return -EFAULT;
			return 0;
		}
632

633 634 635
#ifdef CONFIG_COMPAT
	case NCP_IOC_SETOBJECTNAME_32:
#endif
L
Linus Torvalds 已提交
636
	case NCP_IOC_SETOBJECTNAME:
637
		if (uid != server->m.mounted_uid)
L
Linus Torvalds 已提交
638 639 640 641 642 643 644 645 646
			return -EACCES;
		{
			struct ncp_objectname_ioctl user;
			void* newname;
			void* oldname;
			size_t oldnamelen;
			void* oldprivate;
			size_t oldprivatelen;

647 648 649 650 651 652 653 654 655 656
#ifdef CONFIG_COMPAT
			if (cmd == NCP_IOC_SETOBJECTNAME_32) {
				struct compat_ncp_objectname_ioctl user32;
				if (copy_from_user(&user32, argp, sizeof(user32)))
					return -EFAULT;
				user.auth_type = user32.auth_type;
				user.object_name_len = user32.object_name_len;
				user.object_name = compat_ptr(user32.object_name);
			} else
#endif
L
Linus Torvalds 已提交
657 658
			if (copy_from_user(&user, argp, sizeof(user)))
				return -EFAULT;
659

L
Linus Torvalds 已提交
660 661 662
			if (user.object_name_len > NCP_OBJECT_NAME_MAX_LEN)
				return -ENOMEM;
			if (user.object_name_len) {
663 664 665
				newname = kmalloc(user.object_name_len, GFP_USER);
				if (!newname)
					return -ENOMEM;
L
Linus Torvalds 已提交
666
				if (copy_from_user(newname, user.object_name, user.object_name_len)) {
667
					kfree(newname);
L
Linus Torvalds 已提交
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
					return -EFAULT;
				}
			} else {
				newname = NULL;
			}
			/* enter critical section */
			/* maybe that kfree can sleep so do that this way */
			/* it is at least more SMP friendly (in future...) */
			oldname = server->auth.object_name;
			oldnamelen = server->auth.object_name_len;
			oldprivate = server->priv.data;
			oldprivatelen = server->priv.len;
			server->auth.auth_type = user.auth_type;
			server->auth.object_name_len = user.object_name_len;
			server->auth.object_name = newname;
			server->priv.len = 0;
			server->priv.data = NULL;
			/* leave critical section */
686 687
			kfree(oldprivate);
			kfree(oldname);
L
Linus Torvalds 已提交
688 689
			return 0;
		}
690

691 692 693
#ifdef CONFIG_COMPAT
	case NCP_IOC_GETPRIVATEDATA_32:
#endif
L
Linus Torvalds 已提交
694
	case NCP_IOC_GETPRIVATEDATA:
695
		if (uid != server->m.mounted_uid)
L
Linus Torvalds 已提交
696 697 698 699 700
			return -EACCES;
		{
			struct ncp_privatedata_ioctl user;
			size_t outl;

701 702 703 704 705 706 707 708 709
#ifdef CONFIG_COMPAT
			if (cmd == NCP_IOC_GETPRIVATEDATA_32) {
				struct compat_ncp_privatedata_ioctl user32;
				if (copy_from_user(&user32, argp, sizeof(user32)))
					return -EFAULT;
				user.len = user32.len;
				user.data = compat_ptr(user32.data);
			} else
#endif
L
Linus Torvalds 已提交
710 711
			if (copy_from_user(&user, argp, sizeof(user)))
				return -EFAULT;
712

L
Linus Torvalds 已提交
713 714 715 716 717 718 719 720
			outl = user.len;
			user.len = server->priv.len;
			if (outl > user.len) outl = user.len;
			if (outl) {
				if (copy_to_user(user.data,
						 server->priv.data,
						 outl)) return -EFAULT;
			}
721 722 723 724 725
#ifdef CONFIG_COMPAT
			if (cmd == NCP_IOC_GETPRIVATEDATA_32) {
				struct compat_ncp_privatedata_ioctl user32;
				user32.len = user.len;
				user32.data = (unsigned long) user.data;
726
				if (copy_to_user(argp, &user32, sizeof(user32)))
727 728 729
					return -EFAULT;
			} else
#endif
L
Linus Torvalds 已提交
730 731
			if (copy_to_user(argp, &user, sizeof(user)))
				return -EFAULT;
732

L
Linus Torvalds 已提交
733 734
			return 0;
		}
735

736 737 738
#ifdef CONFIG_COMPAT
	case NCP_IOC_SETPRIVATEDATA_32:
#endif
L
Linus Torvalds 已提交
739
	case NCP_IOC_SETPRIVATEDATA:
740
		if (uid != server->m.mounted_uid)
L
Linus Torvalds 已提交
741 742 743 744 745 746 747
			return -EACCES;
		{
			struct ncp_privatedata_ioctl user;
			void* new;
			void* old;
			size_t oldlen;

748 749 750 751 752 753 754 755 756
#ifdef CONFIG_COMPAT
			if (cmd == NCP_IOC_SETPRIVATEDATA_32) {
				struct compat_ncp_privatedata_ioctl user32;
				if (copy_from_user(&user32, argp, sizeof(user32)))
					return -EFAULT;
				user.len = user32.len;
				user.data = compat_ptr(user32.data);
			} else
#endif
L
Linus Torvalds 已提交
757 758
			if (copy_from_user(&user, argp, sizeof(user)))
				return -EFAULT;
759

L
Linus Torvalds 已提交
760 761 762
			if (user.len > NCP_PRIVATE_DATA_MAX_LEN)
				return -ENOMEM;
			if (user.len) {
763 764 765
				new = kmalloc(user.len, GFP_USER);
				if (!new)
					return -ENOMEM;
L
Linus Torvalds 已提交
766
				if (copy_from_user(new, user.data, user.len)) {
767
					kfree(new);
L
Linus Torvalds 已提交
768 769 770 771 772 773 774 775 776 777 778
					return -EFAULT;
				}
			} else {
				new = NULL;
			}
			/* enter critical section */
			old = server->priv.data;
			oldlen = server->priv.len;
			server->priv.len = user.len;
			server->priv.data = new;
			/* leave critical section */
779
			kfree(old);
L
Linus Torvalds 已提交
780 781 782 783 784 785 786 787 788 789 790 791 792
			return 0;
		}

#ifdef CONFIG_NCPFS_NLS
	case NCP_IOC_SETCHARSETS:
		return ncp_set_charsets(server, argp);
		
	case NCP_IOC_GETCHARSETS:
		return ncp_get_charsets(server, argp);

#endif /* CONFIG_NCPFS_NLS */

	case NCP_IOC_SETDENTRYTTL:
793 794
		if (file_permission(filp, MAY_WRITE) != 0 &&
		    uid != server->m.mounted_uid)
L
Linus Torvalds 已提交
795
			return -EACCES;
796

L
Linus Torvalds 已提交
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
		{
			u_int32_t user;

			if (copy_from_user(&user, argp, sizeof(user)))
				return -EFAULT;
			/* 20 secs at most... */
			if (user > 20000)
				return -EINVAL;
			user = (user * HZ) / 1000;
			server->dentry_ttl = user;
			return 0;
		}
		
	case NCP_IOC_GETDENTRYTTL:
		{
			u_int32_t user = (server->dentry_ttl * 1000) / HZ;
			if (copy_to_user(argp, &user, sizeof(user)))
				return -EFAULT;
			return 0;
		}

	}
	return -EINVAL;
}
821

822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
static int ncp_ioctl_need_write(unsigned int cmd)
{
	switch (cmd) {
	case NCP_IOC_GET_FS_INFO:
	case NCP_IOC_GET_FS_INFO_V2:
	case NCP_IOC_NCPREQUEST:
	case NCP_IOC_SETDENTRYTTL:
	case NCP_IOC_SIGN_INIT:
	case NCP_IOC_LOCKUNLOCK:
	case NCP_IOC_SET_SIGN_WANTED:
		return 1;
	case NCP_IOC_GETOBJECTNAME:
	case NCP_IOC_SETOBJECTNAME:
	case NCP_IOC_GETPRIVATEDATA:
	case NCP_IOC_SETPRIVATEDATA:
	case NCP_IOC_SETCHARSETS:
	case NCP_IOC_GETCHARSETS:
	case NCP_IOC_CONN_LOGGED_IN:
	case NCP_IOC_GETDENTRYTTL:
	case NCP_IOC_GETMOUNTUID2:
	case NCP_IOC_SIGN_WANTED:
	case NCP_IOC_GETROOT:
	case NCP_IOC_SETROOT:
		return 0;
	default:
		/* unkown IOCTL command, assume write */
		return 1;
	}
}

int ncp_ioctl(struct inode *inode, struct file *filp,
	      unsigned int cmd, unsigned long arg)
{
	int ret;

	if (ncp_ioctl_need_write(cmd)) {
		/*
		 * inside the ioctl(), any failures which
		 * are because of file_permission() are
		 * -EACCESS, so it seems consistent to keep
		 *  that here.
		 */
		if (mnt_want_write(filp->f_path.mnt))
			return -EACCES;
	}
	ret = __ncp_ioctl(inode, filp, cmd, arg);
	if (ncp_ioctl_need_write(cmd))
		mnt_drop_write(filp->f_path.mnt);
	return ret;
}

873 874 875
#ifdef CONFIG_COMPAT
long ncp_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
J
Josef Sipek 已提交
876
	struct inode *inode = file->f_path.dentry->d_inode;
877 878 879 880 881 882 883 884 885
	int ret;

	lock_kernel();
	arg = (unsigned long) compat_ptr(arg);
	ret = ncp_ioctl(inode, file, cmd, arg);
	unlock_kernel();
	return ret;
}
#endif