ioctl.c 22 KB

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