ioctl.c 22 KB

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