ioctl.c 22 KB

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