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