ioctl.c 22 KB

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