ioctl.c 22 KB

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