ioctl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. /*
  2. * ioctl.c
  3. *
  4. * Copyright (C) 1995, 1996 by Volker Lendecke
  5. * Modified 1997 Peter Waltenberg, Bill Hawes, David Woodhouse for 2.1 dcache
  6. * Modified 1998, 1999 Wolfram Pienkoss for NLS
  7. *
  8. */
  9. #include <linux/capability.h>
  10. #include <linux/compat.h>
  11. #include <linux/errno.h>
  12. #include <linux/fs.h>
  13. #include <linux/ioctl.h>
  14. #include <linux/time.h>
  15. #include <linux/mm.h>
  16. #include <linux/mount.h>
  17. #include <linux/slab.h>
  18. #include <linux/highuid.h>
  19. #include <linux/smp_lock.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/sched.h>
  22. #include <linux/ncp_fs.h>
  23. #include <asm/uaccess.h>
  24. #include "ncplib_kernel.h"
  25. /* maximum limit for ncp_objectname_ioctl */
  26. #define NCP_OBJECT_NAME_MAX_LEN 4096
  27. /* maximum limit for ncp_privatedata_ioctl */
  28. #define NCP_PRIVATE_DATA_MAX_LEN 8192
  29. /* maximum negotiable packet size */
  30. #define NCP_PACKET_SIZE_INTERNAL 65536
  31. static int
  32. ncp_get_fs_info(struct ncp_server * server, struct file *file,
  33. struct ncp_fs_info __user *arg)
  34. {
  35. struct inode *inode = file->f_path.dentry->d_inode;
  36. struct ncp_fs_info info;
  37. if (file_permission(file, MAY_WRITE) != 0
  38. && current_uid() != server->m.mounted_uid)
  39. return -EACCES;
  40. if (copy_from_user(&info, arg, sizeof(info)))
  41. return -EFAULT;
  42. if (info.version != NCP_GET_FS_INFO_VERSION) {
  43. DPRINTK("info.version invalid: %d\n", info.version);
  44. return -EINVAL;
  45. }
  46. /* TODO: info.addr = server->m.serv_addr; */
  47. SET_UID(info.mounted_uid, server->m.mounted_uid);
  48. info.connection = server->connection;
  49. info.buffer_size = server->buffer_size;
  50. info.volume_number = NCP_FINFO(inode)->volNumber;
  51. info.directory_id = NCP_FINFO(inode)->DosDirNum;
  52. if (copy_to_user(arg, &info, sizeof(info)))
  53. return -EFAULT;
  54. return 0;
  55. }
  56. static int
  57. ncp_get_fs_info_v2(struct ncp_server * server, struct file *file,
  58. struct ncp_fs_info_v2 __user * arg)
  59. {
  60. struct inode *inode = file->f_path.dentry->d_inode;
  61. struct ncp_fs_info_v2 info2;
  62. if (file_permission(file, MAY_WRITE) != 0
  63. && current_uid() != server->m.mounted_uid)
  64. return -EACCES;
  65. if (copy_from_user(&info2, arg, sizeof(info2)))
  66. return -EFAULT;
  67. if (info2.version != NCP_GET_FS_INFO_VERSION_V2) {
  68. DPRINTK("info.version invalid: %d\n", info2.version);
  69. return -EINVAL;
  70. }
  71. info2.mounted_uid = server->m.mounted_uid;
  72. info2.connection = server->connection;
  73. info2.buffer_size = server->buffer_size;
  74. info2.volume_number = NCP_FINFO(inode)->volNumber;
  75. info2.directory_id = NCP_FINFO(inode)->DosDirNum;
  76. info2.dummy1 = info2.dummy2 = info2.dummy3 = 0;
  77. if (copy_to_user(arg, &info2, sizeof(info2)))
  78. return -EFAULT;
  79. return 0;
  80. }
  81. #ifdef CONFIG_COMPAT
  82. struct compat_ncp_objectname_ioctl
  83. {
  84. s32 auth_type;
  85. u32 object_name_len;
  86. compat_caddr_t object_name; /* a userspace data, in most cases user name */
  87. };
  88. struct compat_ncp_fs_info_v2 {
  89. s32 version;
  90. u32 mounted_uid;
  91. u32 connection;
  92. u32 buffer_size;
  93. u32 volume_number;
  94. u32 directory_id;
  95. u32 dummy1;
  96. u32 dummy2;
  97. u32 dummy3;
  98. };
  99. struct compat_ncp_ioctl_request {
  100. u32 function;
  101. u32 size;
  102. compat_caddr_t data;
  103. };
  104. struct compat_ncp_privatedata_ioctl
  105. {
  106. u32 len;
  107. compat_caddr_t data; /* ~1000 for NDS */
  108. };
  109. #define NCP_IOC_GET_FS_INFO_V2_32 _IOWR('n', 4, struct compat_ncp_fs_info_v2)
  110. #define NCP_IOC_NCPREQUEST_32 _IOR('n', 1, struct compat_ncp_ioctl_request)
  111. #define NCP_IOC_GETOBJECTNAME_32 _IOWR('n', 9, struct compat_ncp_objectname_ioctl)
  112. #define NCP_IOC_SETOBJECTNAME_32 _IOR('n', 9, struct compat_ncp_objectname_ioctl)
  113. #define NCP_IOC_GETPRIVATEDATA_32 _IOWR('n', 10, struct compat_ncp_privatedata_ioctl)
  114. #define NCP_IOC_SETPRIVATEDATA_32 _IOR('n', 10, struct compat_ncp_privatedata_ioctl)
  115. static int
  116. ncp_get_compat_fs_info_v2(struct ncp_server * server, struct file *file,
  117. struct compat_ncp_fs_info_v2 __user * arg)
  118. {
  119. struct inode *inode = file->f_path.dentry->d_inode;
  120. struct compat_ncp_fs_info_v2 info2;
  121. if (file_permission(file, MAY_WRITE) != 0
  122. && current_uid() != server->m.mounted_uid)
  123. return -EACCES;
  124. if (copy_from_user(&info2, arg, sizeof(info2)))
  125. return -EFAULT;
  126. if (info2.version != NCP_GET_FS_INFO_VERSION_V2) {
  127. DPRINTK("info.version invalid: %d\n", info2.version);
  128. return -EINVAL;
  129. }
  130. info2.mounted_uid = server->m.mounted_uid;
  131. info2.connection = server->connection;
  132. info2.buffer_size = server->buffer_size;
  133. info2.volume_number = NCP_FINFO(inode)->volNumber;
  134. info2.directory_id = NCP_FINFO(inode)->DosDirNum;
  135. info2.dummy1 = info2.dummy2 = info2.dummy3 = 0;
  136. if (copy_to_user(arg, &info2, sizeof(info2)))
  137. return -EFAULT;
  138. return 0;
  139. }
  140. #endif
  141. #define NCP_IOC_GETMOUNTUID16 _IOW('n', 2, u16)
  142. #define NCP_IOC_GETMOUNTUID32 _IOW('n', 2, u32)
  143. #define NCP_IOC_GETMOUNTUID64 _IOW('n', 2, u64)
  144. #ifdef CONFIG_NCPFS_NLS
  145. /* Here we are select the iocharset and the codepage for NLS.
  146. * Thanks Petr Vandrovec for idea and many hints.
  147. */
  148. static int
  149. ncp_set_charsets(struct ncp_server* server, struct ncp_nls_ioctl __user *arg)
  150. {
  151. struct ncp_nls_ioctl user;
  152. struct nls_table *codepage;
  153. struct nls_table *iocharset;
  154. struct nls_table *oldset_io;
  155. struct nls_table *oldset_cp;
  156. if (!capable(CAP_SYS_ADMIN))
  157. return -EACCES;
  158. if (server->root_setuped)
  159. return -EBUSY;
  160. if (copy_from_user(&user, arg, sizeof(user)))
  161. return -EFAULT;
  162. codepage = NULL;
  163. user.codepage[NCP_IOCSNAME_LEN] = 0;
  164. if (!user.codepage[0] || !strcmp(user.codepage, "default"))
  165. codepage = load_nls_default();
  166. else {
  167. codepage = load_nls(user.codepage);
  168. if (!codepage) {
  169. return -EBADRQC;
  170. }
  171. }
  172. iocharset = NULL;
  173. user.iocharset[NCP_IOCSNAME_LEN] = 0;
  174. if (!user.iocharset[0] || !strcmp(user.iocharset, "default")) {
  175. iocharset = load_nls_default();
  176. NCP_CLR_FLAG(server, NCP_FLAG_UTF8);
  177. } else if (!strcmp(user.iocharset, "utf8")) {
  178. iocharset = load_nls_default();
  179. NCP_SET_FLAG(server, NCP_FLAG_UTF8);
  180. } else {
  181. iocharset = load_nls(user.iocharset);
  182. if (!iocharset) {
  183. unload_nls(codepage);
  184. return -EBADRQC;
  185. }
  186. NCP_CLR_FLAG(server, NCP_FLAG_UTF8);
  187. }
  188. oldset_cp = server->nls_vol;
  189. server->nls_vol = codepage;
  190. oldset_io = server->nls_io;
  191. server->nls_io = iocharset;
  192. unload_nls(oldset_cp);
  193. unload_nls(oldset_io);
  194. return 0;
  195. }
  196. static int
  197. ncp_get_charsets(struct ncp_server* server, struct ncp_nls_ioctl __user *arg)
  198. {
  199. struct ncp_nls_ioctl user;
  200. int len;
  201. memset(&user, 0, sizeof(user));
  202. if (server->nls_vol && server->nls_vol->charset) {
  203. len = strlen(server->nls_vol->charset);
  204. if (len > NCP_IOCSNAME_LEN)
  205. len = NCP_IOCSNAME_LEN;
  206. strncpy(user.codepage, server->nls_vol->charset, len);
  207. user.codepage[len] = 0;
  208. }
  209. if (NCP_IS_FLAG(server, NCP_FLAG_UTF8))
  210. strcpy(user.iocharset, "utf8");
  211. else if (server->nls_io && server->nls_io->charset) {
  212. len = strlen(server->nls_io->charset);
  213. if (len > NCP_IOCSNAME_LEN)
  214. len = NCP_IOCSNAME_LEN;
  215. strncpy(user.iocharset, server->nls_io->charset, len);
  216. user.iocharset[len] = 0;
  217. }
  218. if (copy_to_user(arg, &user, sizeof(user)))
  219. return -EFAULT;
  220. return 0;
  221. }
  222. #endif /* CONFIG_NCPFS_NLS */
  223. static long __ncp_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  224. {
  225. struct inode *inode = filp->f_dentry->d_inode;
  226. struct ncp_server *server = NCP_SERVER(inode);
  227. int result;
  228. struct ncp_ioctl_request request;
  229. char* bouncebuffer;
  230. void __user *argp = (void __user *)arg;
  231. uid_t uid = current_uid();
  232. switch (cmd) {
  233. #ifdef CONFIG_COMPAT
  234. case NCP_IOC_NCPREQUEST_32:
  235. #endif
  236. case NCP_IOC_NCPREQUEST:
  237. if (file_permission(filp, MAY_WRITE) != 0
  238. && uid != server->m.mounted_uid)
  239. return -EACCES;
  240. #ifdef CONFIG_COMPAT
  241. if (cmd == NCP_IOC_NCPREQUEST_32) {
  242. struct compat_ncp_ioctl_request request32;
  243. if (copy_from_user(&request32, argp, sizeof(request32)))
  244. return -EFAULT;
  245. request.function = request32.function;
  246. request.size = request32.size;
  247. request.data = compat_ptr(request32.data);
  248. } else
  249. #endif
  250. if (copy_from_user(&request, argp, sizeof(request)))
  251. return -EFAULT;
  252. if ((request.function > 255)
  253. || (request.size >
  254. NCP_PACKET_SIZE - sizeof(struct ncp_request_header))) {
  255. return -EINVAL;
  256. }
  257. bouncebuffer = vmalloc(NCP_PACKET_SIZE_INTERNAL);
  258. if (!bouncebuffer)
  259. return -ENOMEM;
  260. if (copy_from_user(bouncebuffer, request.data, request.size)) {
  261. vfree(bouncebuffer);
  262. return -EFAULT;
  263. }
  264. ncp_lock_server(server);
  265. /* FIXME: We hack around in the server's structures
  266. here to be able to use ncp_request */
  267. server->has_subfunction = 0;
  268. server->current_size = request.size;
  269. memcpy(server->packet, bouncebuffer, request.size);
  270. result = ncp_request2(server, request.function,
  271. bouncebuffer, NCP_PACKET_SIZE_INTERNAL);
  272. if (result < 0)
  273. result = -EIO;
  274. else
  275. result = server->reply_size;
  276. ncp_unlock_server(server);
  277. DPRINTK("ncp_ioctl: copy %d bytes\n",
  278. result);
  279. if (result >= 0)
  280. if (copy_to_user(request.data, bouncebuffer, result))
  281. result = -EFAULT;
  282. vfree(bouncebuffer);
  283. return result;
  284. case NCP_IOC_CONN_LOGGED_IN:
  285. if (!capable(CAP_SYS_ADMIN))
  286. return -EACCES;
  287. if (!(server->m.int_flags & NCP_IMOUNT_LOGGEDIN_POSSIBLE))
  288. return -EINVAL;
  289. if (server->root_setuped)
  290. return -EBUSY;
  291. server->root_setuped = 1;
  292. return ncp_conn_logged_in(inode->i_sb);
  293. case NCP_IOC_GET_FS_INFO:
  294. return ncp_get_fs_info(server, filp, argp);
  295. case NCP_IOC_GET_FS_INFO_V2:
  296. return ncp_get_fs_info_v2(server, filp, argp);
  297. #ifdef CONFIG_COMPAT
  298. case NCP_IOC_GET_FS_INFO_V2_32:
  299. return ncp_get_compat_fs_info_v2(server, filp, argp);
  300. #endif
  301. /* we have too many combinations of CONFIG_COMPAT,
  302. * CONFIG_64BIT and CONFIG_UID16, so just handle
  303. * any of the possible ioctls */
  304. case NCP_IOC_GETMOUNTUID16:
  305. case NCP_IOC_GETMOUNTUID32:
  306. case NCP_IOC_GETMOUNTUID64:
  307. if (file_permission(filp, MAY_READ) != 0
  308. && uid != server->m.mounted_uid)
  309. return -EACCES;
  310. if (cmd == NCP_IOC_GETMOUNTUID16) {
  311. u16 uid;
  312. SET_UID(uid, server->m.mounted_uid);
  313. if (put_user(uid, (u16 __user *)argp))
  314. return -EFAULT;
  315. } else if (cmd == NCP_IOC_GETMOUNTUID32) {
  316. if (put_user(server->m.mounted_uid,
  317. (u32 __user *)argp))
  318. return -EFAULT;
  319. } else {
  320. if (put_user(server->m.mounted_uid,
  321. (u64 __user *)argp))
  322. return -EFAULT;
  323. }
  324. return 0;
  325. case NCP_IOC_GETROOT:
  326. {
  327. struct ncp_setroot_ioctl sr;
  328. if (file_permission(filp, MAY_READ) != 0
  329. && uid != server->m.mounted_uid)
  330. return -EACCES;
  331. if (server->m.mounted_vol[0]) {
  332. struct dentry* dentry = inode->i_sb->s_root;
  333. if (dentry) {
  334. struct inode* s_inode = dentry->d_inode;
  335. if (s_inode) {
  336. sr.volNumber = NCP_FINFO(s_inode)->volNumber;
  337. sr.dirEntNum = NCP_FINFO(s_inode)->dirEntNum;
  338. sr.namespace = server->name_space[sr.volNumber];
  339. } else
  340. DPRINTK("ncpfs: s_root->d_inode==NULL\n");
  341. } else
  342. DPRINTK("ncpfs: s_root==NULL\n");
  343. } else {
  344. sr.volNumber = -1;
  345. sr.namespace = 0;
  346. sr.dirEntNum = 0;
  347. }
  348. if (copy_to_user(argp, &sr, sizeof(sr)))
  349. return -EFAULT;
  350. return 0;
  351. }
  352. case NCP_IOC_SETROOT:
  353. {
  354. struct ncp_setroot_ioctl sr;
  355. __u32 vnum;
  356. __le32 de;
  357. __le32 dosde;
  358. struct dentry* dentry;
  359. if (!capable(CAP_SYS_ADMIN))
  360. {
  361. return -EACCES;
  362. }
  363. if (server->root_setuped) return -EBUSY;
  364. if (copy_from_user(&sr, argp, sizeof(sr)))
  365. return -EFAULT;
  366. if (sr.volNumber < 0) {
  367. server->m.mounted_vol[0] = 0;
  368. vnum = NCP_NUMBER_OF_VOLUMES;
  369. de = 0;
  370. dosde = 0;
  371. } else if (sr.volNumber >= NCP_NUMBER_OF_VOLUMES) {
  372. return -EINVAL;
  373. } else if (ncp_mount_subdir(server, sr.volNumber,
  374. sr.namespace, sr.dirEntNum,
  375. &vnum, &de, &dosde)) {
  376. return -ENOENT;
  377. }
  378. dentry = inode->i_sb->s_root;
  379. server->root_setuped = 1;
  380. if (dentry) {
  381. struct inode* s_inode = dentry->d_inode;
  382. if (s_inode) {
  383. NCP_FINFO(s_inode)->volNumber = vnum;
  384. NCP_FINFO(s_inode)->dirEntNum = de;
  385. NCP_FINFO(s_inode)->DosDirNum = dosde;
  386. } else
  387. DPRINTK("ncpfs: s_root->d_inode==NULL\n");
  388. } else
  389. DPRINTK("ncpfs: s_root==NULL\n");
  390. return 0;
  391. }
  392. #ifdef CONFIG_NCPFS_PACKET_SIGNING
  393. case NCP_IOC_SIGN_INIT:
  394. if (file_permission(filp, MAY_WRITE) != 0
  395. && uid != server->m.mounted_uid)
  396. return -EACCES;
  397. if (argp) {
  398. if (server->sign_wanted)
  399. {
  400. struct ncp_sign_init sign;
  401. if (copy_from_user(&sign, argp, sizeof(sign)))
  402. return -EFAULT;
  403. memcpy(server->sign_root,sign.sign_root,8);
  404. memcpy(server->sign_last,sign.sign_last,16);
  405. server->sign_active = 1;
  406. }
  407. /* ignore when signatures not wanted */
  408. } else {
  409. server->sign_active = 0;
  410. }
  411. return 0;
  412. case NCP_IOC_SIGN_WANTED:
  413. if (file_permission(filp, MAY_READ) != 0
  414. && uid != server->m.mounted_uid)
  415. return -EACCES;
  416. if (put_user(server->sign_wanted, (int __user *)argp))
  417. return -EFAULT;
  418. return 0;
  419. case NCP_IOC_SET_SIGN_WANTED:
  420. {
  421. int newstate;
  422. if (file_permission(filp, MAY_WRITE) != 0
  423. && uid != server->m.mounted_uid)
  424. return -EACCES;
  425. /* get only low 8 bits... */
  426. if (get_user(newstate, (unsigned char __user *)argp))
  427. return -EFAULT;
  428. if (server->sign_active) {
  429. /* cannot turn signatures OFF when active */
  430. if (!newstate) return -EINVAL;
  431. } else {
  432. server->sign_wanted = newstate != 0;
  433. }
  434. return 0;
  435. }
  436. #endif /* CONFIG_NCPFS_PACKET_SIGNING */
  437. #ifdef CONFIG_NCPFS_IOCTL_LOCKING
  438. case NCP_IOC_LOCKUNLOCK:
  439. if (file_permission(filp, MAY_WRITE) != 0
  440. && uid != server->m.mounted_uid)
  441. return -EACCES;
  442. {
  443. struct ncp_lock_ioctl rqdata;
  444. if (copy_from_user(&rqdata, argp, sizeof(rqdata)))
  445. return -EFAULT;
  446. if (rqdata.origin != 0)
  447. return -EINVAL;
  448. /* check for cmd */
  449. switch (rqdata.cmd) {
  450. case NCP_LOCK_EX:
  451. case NCP_LOCK_SH:
  452. if (rqdata.timeout == 0)
  453. rqdata.timeout = NCP_LOCK_DEFAULT_TIMEOUT;
  454. else if (rqdata.timeout > NCP_LOCK_MAX_TIMEOUT)
  455. rqdata.timeout = NCP_LOCK_MAX_TIMEOUT;
  456. break;
  457. case NCP_LOCK_LOG:
  458. rqdata.timeout = NCP_LOCK_DEFAULT_TIMEOUT; /* has no effect */
  459. case NCP_LOCK_CLEAR:
  460. break;
  461. default:
  462. return -EINVAL;
  463. }
  464. /* locking needs both read and write access */
  465. if ((result = ncp_make_open(inode, O_RDWR)) != 0)
  466. {
  467. return result;
  468. }
  469. result = -EIO;
  470. if (!ncp_conn_valid(server))
  471. goto outrel;
  472. result = -EISDIR;
  473. if (!S_ISREG(inode->i_mode))
  474. goto outrel;
  475. if (rqdata.cmd == NCP_LOCK_CLEAR)
  476. {
  477. result = ncp_ClearPhysicalRecord(NCP_SERVER(inode),
  478. NCP_FINFO(inode)->file_handle,
  479. rqdata.offset,
  480. rqdata.length);
  481. if (result > 0) result = 0; /* no such lock */
  482. }
  483. else
  484. {
  485. int lockcmd;
  486. switch (rqdata.cmd)
  487. {
  488. case NCP_LOCK_EX: lockcmd=1; break;
  489. case NCP_LOCK_SH: lockcmd=3; break;
  490. default: lockcmd=0; break;
  491. }
  492. result = ncp_LogPhysicalRecord(NCP_SERVER(inode),
  493. NCP_FINFO(inode)->file_handle,
  494. lockcmd,
  495. rqdata.offset,
  496. rqdata.length,
  497. rqdata.timeout);
  498. if (result > 0) result = -EAGAIN;
  499. }
  500. outrel:
  501. ncp_inode_close(inode);
  502. return result;
  503. }
  504. #endif /* CONFIG_NCPFS_IOCTL_LOCKING */
  505. #ifdef CONFIG_COMPAT
  506. case NCP_IOC_GETOBJECTNAME_32:
  507. if (uid != server->m.mounted_uid)
  508. return -EACCES;
  509. {
  510. struct compat_ncp_objectname_ioctl user;
  511. size_t outl;
  512. if (copy_from_user(&user, argp, sizeof(user)))
  513. return -EFAULT;
  514. user.auth_type = server->auth.auth_type;
  515. outl = user.object_name_len;
  516. user.object_name_len = server->auth.object_name_len;
  517. if (outl > user.object_name_len)
  518. outl = user.object_name_len;
  519. if (outl) {
  520. if (copy_to_user(compat_ptr(user.object_name),
  521. server->auth.object_name,
  522. outl)) return -EFAULT;
  523. }
  524. if (copy_to_user(argp, &user, sizeof(user)))
  525. return -EFAULT;
  526. return 0;
  527. }
  528. #endif
  529. case NCP_IOC_GETOBJECTNAME:
  530. if (uid != server->m.mounted_uid)
  531. return -EACCES;
  532. {
  533. struct ncp_objectname_ioctl user;
  534. size_t outl;
  535. if (copy_from_user(&user, argp, sizeof(user)))
  536. return -EFAULT;
  537. user.auth_type = server->auth.auth_type;
  538. outl = user.object_name_len;
  539. user.object_name_len = server->auth.object_name_len;
  540. if (outl > user.object_name_len)
  541. outl = user.object_name_len;
  542. if (outl) {
  543. if (copy_to_user(user.object_name,
  544. server->auth.object_name,
  545. outl)) return -EFAULT;
  546. }
  547. if (copy_to_user(argp, &user, sizeof(user)))
  548. return -EFAULT;
  549. return 0;
  550. }
  551. #ifdef CONFIG_COMPAT
  552. case NCP_IOC_SETOBJECTNAME_32:
  553. #endif
  554. case NCP_IOC_SETOBJECTNAME:
  555. if (uid != server->m.mounted_uid)
  556. return -EACCES;
  557. {
  558. struct ncp_objectname_ioctl user;
  559. void* newname;
  560. void* oldname;
  561. size_t oldnamelen;
  562. void* oldprivate;
  563. size_t oldprivatelen;
  564. #ifdef CONFIG_COMPAT
  565. if (cmd == NCP_IOC_SETOBJECTNAME_32) {
  566. struct compat_ncp_objectname_ioctl user32;
  567. if (copy_from_user(&user32, argp, sizeof(user32)))
  568. return -EFAULT;
  569. user.auth_type = user32.auth_type;
  570. user.object_name_len = user32.object_name_len;
  571. user.object_name = compat_ptr(user32.object_name);
  572. } else
  573. #endif
  574. if (copy_from_user(&user, argp, sizeof(user)))
  575. return -EFAULT;
  576. if (user.object_name_len > NCP_OBJECT_NAME_MAX_LEN)
  577. return -ENOMEM;
  578. if (user.object_name_len) {
  579. newname = memdup_user(user.object_name,
  580. user.object_name_len);
  581. if (IS_ERR(newname))
  582. return PTR_ERR(newname);
  583. } else {
  584. newname = NULL;
  585. }
  586. /* enter critical section */
  587. /* maybe that kfree can sleep so do that this way */
  588. /* it is at least more SMP friendly (in future...) */
  589. oldname = server->auth.object_name;
  590. oldnamelen = server->auth.object_name_len;
  591. oldprivate = server->priv.data;
  592. oldprivatelen = server->priv.len;
  593. server->auth.auth_type = user.auth_type;
  594. server->auth.object_name_len = user.object_name_len;
  595. server->auth.object_name = newname;
  596. server->priv.len = 0;
  597. server->priv.data = NULL;
  598. /* leave critical section */
  599. kfree(oldprivate);
  600. kfree(oldname);
  601. return 0;
  602. }
  603. #ifdef CONFIG_COMPAT
  604. case NCP_IOC_GETPRIVATEDATA_32:
  605. #endif
  606. case NCP_IOC_GETPRIVATEDATA:
  607. if (uid != server->m.mounted_uid)
  608. return -EACCES;
  609. {
  610. struct ncp_privatedata_ioctl user;
  611. size_t outl;
  612. #ifdef CONFIG_COMPAT
  613. if (cmd == NCP_IOC_GETPRIVATEDATA_32) {
  614. struct compat_ncp_privatedata_ioctl user32;
  615. if (copy_from_user(&user32, argp, sizeof(user32)))
  616. return -EFAULT;
  617. user.len = user32.len;
  618. user.data = compat_ptr(user32.data);
  619. } else
  620. #endif
  621. if (copy_from_user(&user, argp, sizeof(user)))
  622. return -EFAULT;
  623. outl = user.len;
  624. user.len = server->priv.len;
  625. if (outl > user.len) outl = user.len;
  626. if (outl) {
  627. if (copy_to_user(user.data,
  628. server->priv.data,
  629. outl)) return -EFAULT;
  630. }
  631. #ifdef CONFIG_COMPAT
  632. if (cmd == NCP_IOC_GETPRIVATEDATA_32) {
  633. struct compat_ncp_privatedata_ioctl user32;
  634. user32.len = user.len;
  635. user32.data = (unsigned long) user.data;
  636. if (copy_to_user(argp, &user32, sizeof(user32)))
  637. return -EFAULT;
  638. } else
  639. #endif
  640. if (copy_to_user(argp, &user, sizeof(user)))
  641. return -EFAULT;
  642. return 0;
  643. }
  644. #ifdef CONFIG_COMPAT
  645. case NCP_IOC_SETPRIVATEDATA_32:
  646. #endif
  647. case NCP_IOC_SETPRIVATEDATA:
  648. if (uid != server->m.mounted_uid)
  649. return -EACCES;
  650. {
  651. struct ncp_privatedata_ioctl user;
  652. void* new;
  653. void* old;
  654. size_t oldlen;
  655. #ifdef CONFIG_COMPAT
  656. if (cmd == NCP_IOC_SETPRIVATEDATA_32) {
  657. struct compat_ncp_privatedata_ioctl user32;
  658. if (copy_from_user(&user32, argp, sizeof(user32)))
  659. return -EFAULT;
  660. user.len = user32.len;
  661. user.data = compat_ptr(user32.data);
  662. } else
  663. #endif
  664. if (copy_from_user(&user, argp, sizeof(user)))
  665. return -EFAULT;
  666. if (user.len > NCP_PRIVATE_DATA_MAX_LEN)
  667. return -ENOMEM;
  668. if (user.len) {
  669. new = memdup_user(user.data, user.len);
  670. if (IS_ERR(new))
  671. return PTR_ERR(new);
  672. } else {
  673. new = NULL;
  674. }
  675. /* enter critical section */
  676. old = server->priv.data;
  677. oldlen = server->priv.len;
  678. server->priv.len = user.len;
  679. server->priv.data = new;
  680. /* leave critical section */
  681. kfree(old);
  682. return 0;
  683. }
  684. #ifdef CONFIG_NCPFS_NLS
  685. case NCP_IOC_SETCHARSETS:
  686. return ncp_set_charsets(server, argp);
  687. case NCP_IOC_GETCHARSETS:
  688. return ncp_get_charsets(server, argp);
  689. #endif /* CONFIG_NCPFS_NLS */
  690. case NCP_IOC_SETDENTRYTTL:
  691. if (file_permission(filp, MAY_WRITE) != 0 &&
  692. uid != server->m.mounted_uid)
  693. return -EACCES;
  694. {
  695. u_int32_t user;
  696. if (copy_from_user(&user, argp, sizeof(user)))
  697. return -EFAULT;
  698. /* 20 secs at most... */
  699. if (user > 20000)
  700. return -EINVAL;
  701. user = (user * HZ) / 1000;
  702. server->dentry_ttl = user;
  703. return 0;
  704. }
  705. case NCP_IOC_GETDENTRYTTL:
  706. {
  707. u_int32_t user = (server->dentry_ttl * 1000) / HZ;
  708. if (copy_to_user(argp, &user, sizeof(user)))
  709. return -EFAULT;
  710. return 0;
  711. }
  712. }
  713. return -EINVAL;
  714. }
  715. static int ncp_ioctl_need_write(unsigned int cmd)
  716. {
  717. switch (cmd) {
  718. case NCP_IOC_GET_FS_INFO:
  719. case NCP_IOC_GET_FS_INFO_V2:
  720. case NCP_IOC_NCPREQUEST:
  721. case NCP_IOC_SETDENTRYTTL:
  722. case NCP_IOC_SIGN_INIT:
  723. case NCP_IOC_LOCKUNLOCK:
  724. case NCP_IOC_SET_SIGN_WANTED:
  725. return 1;
  726. case NCP_IOC_GETOBJECTNAME:
  727. case NCP_IOC_SETOBJECTNAME:
  728. case NCP_IOC_GETPRIVATEDATA:
  729. case NCP_IOC_SETPRIVATEDATA:
  730. case NCP_IOC_SETCHARSETS:
  731. case NCP_IOC_GETCHARSETS:
  732. case NCP_IOC_CONN_LOGGED_IN:
  733. case NCP_IOC_GETDENTRYTTL:
  734. case NCP_IOC_GETMOUNTUID2:
  735. case NCP_IOC_SIGN_WANTED:
  736. case NCP_IOC_GETROOT:
  737. case NCP_IOC_SETROOT:
  738. return 0;
  739. default:
  740. /* unknown IOCTL command, assume write */
  741. return 1;
  742. }
  743. }
  744. long ncp_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  745. {
  746. long ret;
  747. lock_kernel();
  748. if (ncp_ioctl_need_write(cmd)) {
  749. /*
  750. * inside the ioctl(), any failures which
  751. * are because of file_permission() are
  752. * -EACCESS, so it seems consistent to keep
  753. * that here.
  754. */
  755. if (mnt_want_write(filp->f_path.mnt)) {
  756. ret = -EACCES;
  757. goto out;
  758. }
  759. }
  760. ret = __ncp_ioctl(filp, cmd, arg);
  761. if (ncp_ioctl_need_write(cmd))
  762. mnt_drop_write(filp->f_path.mnt);
  763. out:
  764. unlock_kernel();
  765. return ret;
  766. }
  767. #ifdef CONFIG_COMPAT
  768. long ncp_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  769. {
  770. long ret;
  771. lock_kernel();
  772. arg = (unsigned long) compat_ptr(arg);
  773. ret = ncp_ioctl(file, cmd, arg);
  774. unlock_kernel();
  775. return ret;
  776. }
  777. #endif