ioctl.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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/cred.h>
  22. #include <linux/uaccess.h>
  23. #include "ncp_fs.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. ncp_dbg(1, "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, from_kuid_munged(current_user_ns(), 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. ncp_dbg(1, "info.version invalid: %d\n", info2.version);
  60. return -EINVAL;
  61. }
  62. info2.mounted_uid = from_kuid_munged(current_user_ns(), 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. ncp_dbg(1, "info.version invalid: %d\n", info2.version);
  115. return -EINVAL;
  116. }
  117. info2.mounted_uid = from_kuid_munged(current_user_ns(), 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. ncp_dbg(1, "copy %d bytes\n", result);
  273. if (result >= 0)
  274. if (copy_to_user(request.data, bouncebuffer, result))
  275. result = -EFAULT;
  276. vfree(bouncebuffer);
  277. return result;
  278. case NCP_IOC_CONN_LOGGED_IN:
  279. if (!(server->m.int_flags & NCP_IMOUNT_LOGGEDIN_POSSIBLE))
  280. return -EINVAL;
  281. mutex_lock(&server->root_setup_lock);
  282. if (server->root_setuped)
  283. result = -EBUSY;
  284. else {
  285. result = ncp_conn_logged_in(inode->i_sb);
  286. if (result == 0)
  287. server->root_setuped = 1;
  288. }
  289. mutex_unlock(&server->root_setup_lock);
  290. return result;
  291. case NCP_IOC_GET_FS_INFO:
  292. return ncp_get_fs_info(server, inode, argp);
  293. case NCP_IOC_GET_FS_INFO_V2:
  294. return ncp_get_fs_info_v2(server, inode, argp);
  295. #ifdef CONFIG_COMPAT
  296. case NCP_IOC_GET_FS_INFO_V2_32:
  297. return ncp_get_compat_fs_info_v2(server, inode, argp);
  298. #endif
  299. /* we have too many combinations of CONFIG_COMPAT,
  300. * CONFIG_64BIT and CONFIG_UID16, so just handle
  301. * any of the possible ioctls */
  302. case NCP_IOC_GETMOUNTUID16:
  303. {
  304. u16 uid;
  305. SET_UID(uid, from_kuid_munged(current_user_ns(), server->m.mounted_uid));
  306. if (put_user(uid, (u16 __user *)argp))
  307. return -EFAULT;
  308. return 0;
  309. }
  310. case NCP_IOC_GETMOUNTUID32:
  311. {
  312. uid_t uid = from_kuid_munged(current_user_ns(), server->m.mounted_uid);
  313. if (put_user(uid, (u32 __user *)argp))
  314. return -EFAULT;
  315. return 0;
  316. }
  317. case NCP_IOC_GETMOUNTUID64:
  318. {
  319. uid_t uid = from_kuid_munged(current_user_ns(), server->m.mounted_uid);
  320. if (put_user(uid, (u64 __user *)argp))
  321. return -EFAULT;
  322. return 0;
  323. }
  324. case NCP_IOC_GETROOT:
  325. {
  326. struct ncp_setroot_ioctl sr;
  327. result = -EACCES;
  328. mutex_lock(&server->root_setup_lock);
  329. if (server->m.mounted_vol[0]) {
  330. struct dentry* dentry = inode->i_sb->s_root;
  331. if (dentry) {
  332. struct inode* s_inode = d_inode(dentry);
  333. if (s_inode) {
  334. sr.volNumber = NCP_FINFO(s_inode)->volNumber;
  335. sr.dirEntNum = NCP_FINFO(s_inode)->dirEntNum;
  336. sr.namespace = server->name_space[sr.volNumber];
  337. result = 0;
  338. } else
  339. ncp_dbg(1, "d_inode(s_root)==NULL\n");
  340. } else
  341. ncp_dbg(1, "s_root==NULL\n");
  342. } else {
  343. sr.volNumber = -1;
  344. sr.namespace = 0;
  345. sr.dirEntNum = 0;
  346. result = 0;
  347. }
  348. mutex_unlock(&server->root_setup_lock);
  349. if (!result && copy_to_user(argp, &sr, sizeof(sr)))
  350. result = -EFAULT;
  351. return result;
  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 (copy_from_user(&sr, argp, sizeof(sr)))
  361. return -EFAULT;
  362. mutex_lock(&server->root_setup_lock);
  363. if (server->root_setuped)
  364. result = -EBUSY;
  365. else {
  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. result = 0;
  372. } else if (sr.volNumber >= NCP_NUMBER_OF_VOLUMES) {
  373. result = -EINVAL;
  374. } else if (ncp_mount_subdir(server, sr.volNumber,
  375. sr.namespace, sr.dirEntNum,
  376. &vnum, &de, &dosde)) {
  377. result = -ENOENT;
  378. } else
  379. result = 0;
  380. if (result == 0) {
  381. dentry = inode->i_sb->s_root;
  382. if (dentry) {
  383. struct inode* s_inode = d_inode(dentry);
  384. if (s_inode) {
  385. NCP_FINFO(s_inode)->volNumber = vnum;
  386. NCP_FINFO(s_inode)->dirEntNum = de;
  387. NCP_FINFO(s_inode)->DosDirNum = dosde;
  388. server->root_setuped = 1;
  389. } else {
  390. ncp_dbg(1, "d_inode(s_root)==NULL\n");
  391. result = -EIO;
  392. }
  393. } else {
  394. ncp_dbg(1, "s_root==NULL\n");
  395. result = -EIO;
  396. }
  397. }
  398. }
  399. mutex_unlock(&server->root_setup_lock);
  400. return result;
  401. }
  402. #ifdef CONFIG_NCPFS_PACKET_SIGNING
  403. case NCP_IOC_SIGN_INIT:
  404. {
  405. struct ncp_sign_init sign;
  406. if (argp)
  407. if (copy_from_user(&sign, argp, sizeof(sign)))
  408. return -EFAULT;
  409. ncp_lock_server(server);
  410. mutex_lock(&server->rcv.creq_mutex);
  411. if (argp) {
  412. if (server->sign_wanted) {
  413. memcpy(server->sign_root,sign.sign_root,8);
  414. memcpy(server->sign_last,sign.sign_last,16);
  415. server->sign_active = 1;
  416. }
  417. /* ignore when signatures not wanted */
  418. } else {
  419. server->sign_active = 0;
  420. }
  421. mutex_unlock(&server->rcv.creq_mutex);
  422. ncp_unlock_server(server);
  423. return 0;
  424. }
  425. case NCP_IOC_SIGN_WANTED:
  426. {
  427. int state;
  428. ncp_lock_server(server);
  429. state = server->sign_wanted;
  430. ncp_unlock_server(server);
  431. if (put_user(state, (int __user *)argp))
  432. return -EFAULT;
  433. return 0;
  434. }
  435. case NCP_IOC_SET_SIGN_WANTED:
  436. {
  437. int newstate;
  438. /* get only low 8 bits... */
  439. if (get_user(newstate, (unsigned char __user *)argp))
  440. return -EFAULT;
  441. result = 0;
  442. ncp_lock_server(server);
  443. if (server->sign_active) {
  444. /* cannot turn signatures OFF when active */
  445. if (!newstate)
  446. result = -EINVAL;
  447. } else {
  448. server->sign_wanted = newstate != 0;
  449. }
  450. ncp_unlock_server(server);
  451. return result;
  452. }
  453. #endif /* CONFIG_NCPFS_PACKET_SIGNING */
  454. #ifdef CONFIG_NCPFS_IOCTL_LOCKING
  455. case NCP_IOC_LOCKUNLOCK:
  456. {
  457. struct ncp_lock_ioctl rqdata;
  458. if (copy_from_user(&rqdata, argp, sizeof(rqdata)))
  459. return -EFAULT;
  460. if (rqdata.origin != 0)
  461. return -EINVAL;
  462. /* check for cmd */
  463. switch (rqdata.cmd) {
  464. case NCP_LOCK_EX:
  465. case NCP_LOCK_SH:
  466. if (rqdata.timeout < 0)
  467. return -EINVAL;
  468. if (rqdata.timeout == 0)
  469. rqdata.timeout = NCP_LOCK_DEFAULT_TIMEOUT;
  470. else if (rqdata.timeout > NCP_LOCK_MAX_TIMEOUT)
  471. rqdata.timeout = NCP_LOCK_MAX_TIMEOUT;
  472. break;
  473. case NCP_LOCK_LOG:
  474. rqdata.timeout = NCP_LOCK_DEFAULT_TIMEOUT; /* has no effect */
  475. case NCP_LOCK_CLEAR:
  476. break;
  477. default:
  478. return -EINVAL;
  479. }
  480. /* locking needs both read and write access */
  481. if ((result = ncp_make_open(inode, O_RDWR)) != 0)
  482. {
  483. return result;
  484. }
  485. result = -EISDIR;
  486. if (!S_ISREG(inode->i_mode))
  487. goto outrel;
  488. if (rqdata.cmd == NCP_LOCK_CLEAR)
  489. {
  490. result = ncp_ClearPhysicalRecord(NCP_SERVER(inode),
  491. NCP_FINFO(inode)->file_handle,
  492. rqdata.offset,
  493. rqdata.length);
  494. if (result > 0) result = 0; /* no such lock */
  495. }
  496. else
  497. {
  498. int lockcmd;
  499. switch (rqdata.cmd)
  500. {
  501. case NCP_LOCK_EX: lockcmd=1; break;
  502. case NCP_LOCK_SH: lockcmd=3; break;
  503. default: lockcmd=0; break;
  504. }
  505. result = ncp_LogPhysicalRecord(NCP_SERVER(inode),
  506. NCP_FINFO(inode)->file_handle,
  507. lockcmd,
  508. rqdata.offset,
  509. rqdata.length,
  510. rqdata.timeout);
  511. if (result > 0) result = -EAGAIN;
  512. }
  513. outrel:
  514. ncp_inode_close(inode);
  515. return result;
  516. }
  517. #endif /* CONFIG_NCPFS_IOCTL_LOCKING */
  518. #ifdef CONFIG_COMPAT
  519. case NCP_IOC_GETOBJECTNAME_32:
  520. {
  521. struct compat_ncp_objectname_ioctl user;
  522. size_t outl;
  523. if (copy_from_user(&user, argp, sizeof(user)))
  524. return -EFAULT;
  525. down_read(&server->auth_rwsem);
  526. user.auth_type = server->auth.auth_type;
  527. outl = user.object_name_len;
  528. user.object_name_len = server->auth.object_name_len;
  529. if (outl > user.object_name_len)
  530. outl = user.object_name_len;
  531. result = 0;
  532. if (outl) {
  533. if (copy_to_user(compat_ptr(user.object_name),
  534. server->auth.object_name,
  535. outl))
  536. result = -EFAULT;
  537. }
  538. up_read(&server->auth_rwsem);
  539. if (!result && copy_to_user(argp, &user, sizeof(user)))
  540. result = -EFAULT;
  541. return result;
  542. }
  543. #endif
  544. case NCP_IOC_GETOBJECTNAME:
  545. {
  546. struct ncp_objectname_ioctl user;
  547. size_t outl;
  548. if (copy_from_user(&user, argp, sizeof(user)))
  549. return -EFAULT;
  550. down_read(&server->auth_rwsem);
  551. user.auth_type = server->auth.auth_type;
  552. outl = user.object_name_len;
  553. user.object_name_len = server->auth.object_name_len;
  554. if (outl > user.object_name_len)
  555. outl = user.object_name_len;
  556. result = 0;
  557. if (outl) {
  558. if (copy_to_user(user.object_name,
  559. server->auth.object_name,
  560. outl))
  561. result = -EFAULT;
  562. }
  563. up_read(&server->auth_rwsem);
  564. if (!result && copy_to_user(argp, &user, sizeof(user)))
  565. result = -EFAULT;
  566. return result;
  567. }
  568. #ifdef CONFIG_COMPAT
  569. case NCP_IOC_SETOBJECTNAME_32:
  570. #endif
  571. case NCP_IOC_SETOBJECTNAME:
  572. {
  573. struct ncp_objectname_ioctl user;
  574. void* newname;
  575. void* oldname;
  576. size_t oldnamelen;
  577. void* oldprivate;
  578. size_t oldprivatelen;
  579. #ifdef CONFIG_COMPAT
  580. if (cmd == NCP_IOC_SETOBJECTNAME_32) {
  581. struct compat_ncp_objectname_ioctl user32;
  582. if (copy_from_user(&user32, argp, sizeof(user32)))
  583. return -EFAULT;
  584. user.auth_type = user32.auth_type;
  585. user.object_name_len = user32.object_name_len;
  586. user.object_name = compat_ptr(user32.object_name);
  587. } else
  588. #endif
  589. if (copy_from_user(&user, argp, sizeof(user)))
  590. return -EFAULT;
  591. if (user.object_name_len > NCP_OBJECT_NAME_MAX_LEN)
  592. return -ENOMEM;
  593. if (user.object_name_len) {
  594. newname = memdup_user(user.object_name,
  595. user.object_name_len);
  596. if (IS_ERR(newname))
  597. return PTR_ERR(newname);
  598. } else {
  599. newname = NULL;
  600. }
  601. down_write(&server->auth_rwsem);
  602. oldname = server->auth.object_name;
  603. oldnamelen = server->auth.object_name_len;
  604. oldprivate = server->priv.data;
  605. oldprivatelen = server->priv.len;
  606. server->auth.auth_type = user.auth_type;
  607. server->auth.object_name_len = user.object_name_len;
  608. server->auth.object_name = newname;
  609. server->priv.len = 0;
  610. server->priv.data = NULL;
  611. up_write(&server->auth_rwsem);
  612. kfree(oldprivate);
  613. kfree(oldname);
  614. return 0;
  615. }
  616. #ifdef CONFIG_COMPAT
  617. case NCP_IOC_GETPRIVATEDATA_32:
  618. #endif
  619. case NCP_IOC_GETPRIVATEDATA:
  620. {
  621. struct ncp_privatedata_ioctl user;
  622. size_t outl;
  623. #ifdef CONFIG_COMPAT
  624. if (cmd == NCP_IOC_GETPRIVATEDATA_32) {
  625. struct compat_ncp_privatedata_ioctl user32;
  626. if (copy_from_user(&user32, argp, sizeof(user32)))
  627. return -EFAULT;
  628. user.len = user32.len;
  629. user.data = compat_ptr(user32.data);
  630. } else
  631. #endif
  632. if (copy_from_user(&user, argp, sizeof(user)))
  633. return -EFAULT;
  634. down_read(&server->auth_rwsem);
  635. outl = user.len;
  636. user.len = server->priv.len;
  637. if (outl > user.len) outl = user.len;
  638. result = 0;
  639. if (outl) {
  640. if (copy_to_user(user.data,
  641. server->priv.data,
  642. outl))
  643. result = -EFAULT;
  644. }
  645. up_read(&server->auth_rwsem);
  646. if (result)
  647. return result;
  648. #ifdef CONFIG_COMPAT
  649. if (cmd == NCP_IOC_GETPRIVATEDATA_32) {
  650. struct compat_ncp_privatedata_ioctl user32;
  651. user32.len = user.len;
  652. user32.data = (unsigned long) user.data;
  653. if (copy_to_user(argp, &user32, sizeof(user32)))
  654. return -EFAULT;
  655. } else
  656. #endif
  657. if (copy_to_user(argp, &user, sizeof(user)))
  658. return -EFAULT;
  659. return 0;
  660. }
  661. #ifdef CONFIG_COMPAT
  662. case NCP_IOC_SETPRIVATEDATA_32:
  663. #endif
  664. case NCP_IOC_SETPRIVATEDATA:
  665. {
  666. struct ncp_privatedata_ioctl user;
  667. void* new;
  668. void* old;
  669. size_t oldlen;
  670. #ifdef CONFIG_COMPAT
  671. if (cmd == NCP_IOC_SETPRIVATEDATA_32) {
  672. struct compat_ncp_privatedata_ioctl user32;
  673. if (copy_from_user(&user32, argp, sizeof(user32)))
  674. return -EFAULT;
  675. user.len = user32.len;
  676. user.data = compat_ptr(user32.data);
  677. } else
  678. #endif
  679. if (copy_from_user(&user, argp, sizeof(user)))
  680. return -EFAULT;
  681. if (user.len > NCP_PRIVATE_DATA_MAX_LEN)
  682. return -ENOMEM;
  683. if (user.len) {
  684. new = memdup_user(user.data, user.len);
  685. if (IS_ERR(new))
  686. return PTR_ERR(new);
  687. } else {
  688. new = NULL;
  689. }
  690. down_write(&server->auth_rwsem);
  691. old = server->priv.data;
  692. oldlen = server->priv.len;
  693. server->priv.len = user.len;
  694. server->priv.data = new;
  695. up_write(&server->auth_rwsem);
  696. kfree(old);
  697. return 0;
  698. }
  699. #ifdef CONFIG_NCPFS_NLS
  700. case NCP_IOC_SETCHARSETS:
  701. return ncp_set_charsets(server, argp);
  702. case NCP_IOC_GETCHARSETS:
  703. return ncp_get_charsets(server, argp);
  704. #endif /* CONFIG_NCPFS_NLS */
  705. case NCP_IOC_SETDENTRYTTL:
  706. {
  707. u_int32_t user;
  708. if (copy_from_user(&user, argp, sizeof(user)))
  709. return -EFAULT;
  710. /* 20 secs at most... */
  711. if (user > 20000)
  712. return -EINVAL;
  713. user = (user * HZ) / 1000;
  714. atomic_set(&server->dentry_ttl, user);
  715. return 0;
  716. }
  717. case NCP_IOC_GETDENTRYTTL:
  718. {
  719. u_int32_t user = (atomic_read(&server->dentry_ttl) * 1000) / HZ;
  720. if (copy_to_user(argp, &user, sizeof(user)))
  721. return -EFAULT;
  722. return 0;
  723. }
  724. }
  725. return -EINVAL;
  726. }
  727. long ncp_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  728. {
  729. struct inode *inode = file_inode(filp);
  730. struct ncp_server *server = NCP_SERVER(inode);
  731. kuid_t uid = current_uid();
  732. int need_drop_write = 0;
  733. long ret;
  734. switch (cmd) {
  735. case NCP_IOC_SETCHARSETS:
  736. case NCP_IOC_CONN_LOGGED_IN:
  737. case NCP_IOC_SETROOT:
  738. if (!capable(CAP_SYS_ADMIN)) {
  739. ret = -EPERM;
  740. goto out;
  741. }
  742. break;
  743. }
  744. if (!uid_eq(server->m.mounted_uid, uid)) {
  745. switch (cmd) {
  746. /*
  747. * Only mount owner can issue these ioctls. Information
  748. * necessary to authenticate to other NDS servers are
  749. * stored here.
  750. */
  751. case NCP_IOC_GETOBJECTNAME:
  752. case NCP_IOC_SETOBJECTNAME:
  753. case NCP_IOC_GETPRIVATEDATA:
  754. case NCP_IOC_SETPRIVATEDATA:
  755. #ifdef CONFIG_COMPAT
  756. case NCP_IOC_GETOBJECTNAME_32:
  757. case NCP_IOC_SETOBJECTNAME_32:
  758. case NCP_IOC_GETPRIVATEDATA_32:
  759. case NCP_IOC_SETPRIVATEDATA_32:
  760. #endif
  761. ret = -EACCES;
  762. goto out;
  763. /*
  764. * These require write access on the inode if user id
  765. * does not match. Note that they do not write to the
  766. * file... But old code did mnt_want_write, so I keep
  767. * it as is. Of course not for mountpoint owner, as
  768. * that breaks read-only mounts altogether as ncpmount
  769. * needs working NCP_IOC_NCPREQUEST and
  770. * NCP_IOC_GET_FS_INFO. Some of these codes (setdentryttl,
  771. * signinit, setsignwanted) should be probably restricted
  772. * to owner only, or even more to CAP_SYS_ADMIN).
  773. */
  774. case NCP_IOC_GET_FS_INFO:
  775. case NCP_IOC_GET_FS_INFO_V2:
  776. case NCP_IOC_NCPREQUEST:
  777. case NCP_IOC_SETDENTRYTTL:
  778. case NCP_IOC_SIGN_INIT:
  779. case NCP_IOC_LOCKUNLOCK:
  780. case NCP_IOC_SET_SIGN_WANTED:
  781. #ifdef CONFIG_COMPAT
  782. case NCP_IOC_GET_FS_INFO_V2_32:
  783. case NCP_IOC_NCPREQUEST_32:
  784. #endif
  785. ret = mnt_want_write_file(filp);
  786. if (ret)
  787. goto out;
  788. need_drop_write = 1;
  789. ret = inode_permission(inode, MAY_WRITE);
  790. if (ret)
  791. goto outDropWrite;
  792. break;
  793. /*
  794. * Read access required.
  795. */
  796. case NCP_IOC_GETMOUNTUID16:
  797. case NCP_IOC_GETMOUNTUID32:
  798. case NCP_IOC_GETMOUNTUID64:
  799. case NCP_IOC_GETROOT:
  800. case NCP_IOC_SIGN_WANTED:
  801. ret = inode_permission(inode, MAY_READ);
  802. if (ret)
  803. goto out;
  804. break;
  805. /*
  806. * Anybody can read these.
  807. */
  808. case NCP_IOC_GETCHARSETS:
  809. case NCP_IOC_GETDENTRYTTL:
  810. default:
  811. /* Three codes below are protected by CAP_SYS_ADMIN above. */
  812. case NCP_IOC_SETCHARSETS:
  813. case NCP_IOC_CONN_LOGGED_IN:
  814. case NCP_IOC_SETROOT:
  815. break;
  816. }
  817. }
  818. ret = __ncp_ioctl(inode, cmd, arg);
  819. outDropWrite:
  820. if (need_drop_write)
  821. mnt_drop_write_file(filp);
  822. out:
  823. return ret;
  824. }
  825. #ifdef CONFIG_COMPAT
  826. long ncp_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  827. {
  828. long ret;
  829. arg = (unsigned long) compat_ptr(arg);
  830. ret = ncp_ioctl(file, cmd, arg);
  831. return ret;
  832. }
  833. #endif