ioctl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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* s_inode = dentry->d_inode;
  342. if (s_inode) {
  343. sr.volNumber = NCP_FINFO(s_inode)->volNumber;
  344. sr.dirEntNum = NCP_FINFO(s_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* s_inode = dentry->d_inode;
  389. if (inode) {
  390. NCP_FINFO(s_inode)->volNumber = vnum;
  391. NCP_FINFO(s_inode)->dirEntNum = de;
  392. NCP_FINFO(s_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. if (copy_from_user(&rqdata, argp, sizeof(rqdata)))
  460. return -EFAULT;
  461. if (rqdata.origin != 0)
  462. return -EINVAL;
  463. /* check for cmd */
  464. switch (rqdata.cmd) {
  465. case NCP_LOCK_EX:
  466. case NCP_LOCK_SH:
  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 = -EIO;
  485. if (!ncp_conn_valid(server))
  486. goto outrel;
  487. result = -EISDIR;
  488. if (!S_ISREG(inode->i_mode))
  489. goto outrel;
  490. if (rqdata.cmd == NCP_LOCK_CLEAR)
  491. {
  492. result = ncp_ClearPhysicalRecord(NCP_SERVER(inode),
  493. NCP_FINFO(inode)->file_handle,
  494. rqdata.offset,
  495. rqdata.length);
  496. if (result > 0) result = 0; /* no such lock */
  497. }
  498. else
  499. {
  500. int lockcmd;
  501. switch (rqdata.cmd)
  502. {
  503. case NCP_LOCK_EX: lockcmd=1; break;
  504. case NCP_LOCK_SH: lockcmd=3; break;
  505. default: lockcmd=0; break;
  506. }
  507. result = ncp_LogPhysicalRecord(NCP_SERVER(inode),
  508. NCP_FINFO(inode)->file_handle,
  509. lockcmd,
  510. rqdata.offset,
  511. rqdata.length,
  512. rqdata.timeout);
  513. if (result > 0) result = -EAGAIN;
  514. }
  515. outrel:
  516. ncp_inode_close(inode);
  517. return result;
  518. }
  519. #endif /* CONFIG_NCPFS_IOCTL_LOCKING */
  520. #ifdef CONFIG_COMPAT
  521. case NCP_IOC_GETOBJECTNAME_32:
  522. if (current->uid != server->m.mounted_uid) {
  523. return -EACCES;
  524. }
  525. {
  526. struct compat_ncp_objectname_ioctl user;
  527. size_t outl;
  528. if (copy_from_user(&user, argp, sizeof(user)))
  529. return -EFAULT;
  530. user.auth_type = server->auth.auth_type;
  531. outl = user.object_name_len;
  532. user.object_name_len = server->auth.object_name_len;
  533. if (outl > user.object_name_len)
  534. outl = user.object_name_len;
  535. if (outl) {
  536. if (copy_to_user(compat_ptr(user.object_name),
  537. server->auth.object_name,
  538. outl)) return -EFAULT;
  539. }
  540. if (copy_to_user(argp, &user, sizeof(user)))
  541. return -EFAULT;
  542. return 0;
  543. }
  544. #endif
  545. case NCP_IOC_GETOBJECTNAME:
  546. if (current->uid != server->m.mounted_uid) {
  547. return -EACCES;
  548. }
  549. {
  550. struct ncp_objectname_ioctl user;
  551. size_t outl;
  552. if (copy_from_user(&user, argp, sizeof(user)))
  553. return -EFAULT;
  554. user.auth_type = server->auth.auth_type;
  555. outl = user.object_name_len;
  556. user.object_name_len = server->auth.object_name_len;
  557. if (outl > user.object_name_len)
  558. outl = user.object_name_len;
  559. if (outl) {
  560. if (copy_to_user(user.object_name,
  561. server->auth.object_name,
  562. outl)) return -EFAULT;
  563. }
  564. if (copy_to_user(argp, &user, sizeof(user)))
  565. return -EFAULT;
  566. return 0;
  567. }
  568. #ifdef CONFIG_COMPAT
  569. case NCP_IOC_SETOBJECTNAME_32:
  570. #endif
  571. case NCP_IOC_SETOBJECTNAME:
  572. if (current->uid != server->m.mounted_uid) {
  573. return -EACCES;
  574. }
  575. {
  576. struct ncp_objectname_ioctl user;
  577. void* newname;
  578. void* oldname;
  579. size_t oldnamelen;
  580. void* oldprivate;
  581. size_t oldprivatelen;
  582. #ifdef CONFIG_COMPAT
  583. if (cmd == NCP_IOC_SETOBJECTNAME_32) {
  584. struct compat_ncp_objectname_ioctl user32;
  585. if (copy_from_user(&user32, argp, sizeof(user32)))
  586. return -EFAULT;
  587. user.auth_type = user32.auth_type;
  588. user.object_name_len = user32.object_name_len;
  589. user.object_name = compat_ptr(user32.object_name);
  590. } else
  591. #endif
  592. if (copy_from_user(&user, argp, sizeof(user)))
  593. return -EFAULT;
  594. if (user.object_name_len > NCP_OBJECT_NAME_MAX_LEN)
  595. return -ENOMEM;
  596. if (user.object_name_len) {
  597. newname = kmalloc(user.object_name_len, GFP_USER);
  598. if (!newname)
  599. return -ENOMEM;
  600. if (copy_from_user(newname, user.object_name, user.object_name_len)) {
  601. kfree(newname);
  602. return -EFAULT;
  603. }
  604. } else {
  605. newname = NULL;
  606. }
  607. /* enter critical section */
  608. /* maybe that kfree can sleep so do that this way */
  609. /* it is at least more SMP friendly (in future...) */
  610. oldname = server->auth.object_name;
  611. oldnamelen = server->auth.object_name_len;
  612. oldprivate = server->priv.data;
  613. oldprivatelen = server->priv.len;
  614. server->auth.auth_type = user.auth_type;
  615. server->auth.object_name_len = user.object_name_len;
  616. server->auth.object_name = newname;
  617. server->priv.len = 0;
  618. server->priv.data = NULL;
  619. /* leave critical section */
  620. kfree(oldprivate);
  621. kfree(oldname);
  622. return 0;
  623. }
  624. #ifdef CONFIG_COMPAT
  625. case NCP_IOC_GETPRIVATEDATA_32:
  626. #endif
  627. case NCP_IOC_GETPRIVATEDATA:
  628. if (current->uid != server->m.mounted_uid) {
  629. return -EACCES;
  630. }
  631. {
  632. struct ncp_privatedata_ioctl user;
  633. size_t outl;
  634. #ifdef CONFIG_COMPAT
  635. if (cmd == NCP_IOC_GETPRIVATEDATA_32) {
  636. struct compat_ncp_privatedata_ioctl user32;
  637. if (copy_from_user(&user32, argp, sizeof(user32)))
  638. return -EFAULT;
  639. user.len = user32.len;
  640. user.data = compat_ptr(user32.data);
  641. } else
  642. #endif
  643. if (copy_from_user(&user, argp, sizeof(user)))
  644. return -EFAULT;
  645. outl = user.len;
  646. user.len = server->priv.len;
  647. if (outl > user.len) outl = user.len;
  648. if (outl) {
  649. if (copy_to_user(user.data,
  650. server->priv.data,
  651. outl)) return -EFAULT;
  652. }
  653. #ifdef CONFIG_COMPAT
  654. if (cmd == NCP_IOC_GETPRIVATEDATA_32) {
  655. struct compat_ncp_privatedata_ioctl user32;
  656. user32.len = user.len;
  657. user32.data = (unsigned long) user.data;
  658. if (copy_to_user(argp, &user32, sizeof(user32)))
  659. return -EFAULT;
  660. } else
  661. #endif
  662. if (copy_to_user(argp, &user, sizeof(user)))
  663. return -EFAULT;
  664. return 0;
  665. }
  666. #ifdef CONFIG_COMPAT
  667. case NCP_IOC_SETPRIVATEDATA_32:
  668. #endif
  669. case NCP_IOC_SETPRIVATEDATA:
  670. if (current->uid != server->m.mounted_uid) {
  671. return -EACCES;
  672. }
  673. {
  674. struct ncp_privatedata_ioctl user;
  675. void* new;
  676. void* old;
  677. size_t oldlen;
  678. #ifdef CONFIG_COMPAT
  679. if (cmd == NCP_IOC_SETPRIVATEDATA_32) {
  680. struct compat_ncp_privatedata_ioctl user32;
  681. if (copy_from_user(&user32, argp, sizeof(user32)))
  682. return -EFAULT;
  683. user.len = user32.len;
  684. user.data = compat_ptr(user32.data);
  685. } else
  686. #endif
  687. if (copy_from_user(&user, argp, sizeof(user)))
  688. return -EFAULT;
  689. if (user.len > NCP_PRIVATE_DATA_MAX_LEN)
  690. return -ENOMEM;
  691. if (user.len) {
  692. new = kmalloc(user.len, GFP_USER);
  693. if (!new)
  694. return -ENOMEM;
  695. if (copy_from_user(new, user.data, user.len)) {
  696. kfree(new);
  697. return -EFAULT;
  698. }
  699. } else {
  700. new = NULL;
  701. }
  702. /* enter critical section */
  703. old = server->priv.data;
  704. oldlen = server->priv.len;
  705. server->priv.len = user.len;
  706. server->priv.data = new;
  707. /* leave critical section */
  708. kfree(old);
  709. return 0;
  710. }
  711. #ifdef CONFIG_NCPFS_NLS
  712. case NCP_IOC_SETCHARSETS:
  713. return ncp_set_charsets(server, argp);
  714. case NCP_IOC_GETCHARSETS:
  715. return ncp_get_charsets(server, argp);
  716. #endif /* CONFIG_NCPFS_NLS */
  717. case NCP_IOC_SETDENTRYTTL:
  718. if ((file_permission(filp, MAY_WRITE) != 0) &&
  719. (current->uid != server->m.mounted_uid))
  720. return -EACCES;
  721. {
  722. u_int32_t user;
  723. if (copy_from_user(&user, argp, sizeof(user)))
  724. return -EFAULT;
  725. /* 20 secs at most... */
  726. if (user > 20000)
  727. return -EINVAL;
  728. user = (user * HZ) / 1000;
  729. server->dentry_ttl = user;
  730. return 0;
  731. }
  732. case NCP_IOC_GETDENTRYTTL:
  733. {
  734. u_int32_t user = (server->dentry_ttl * 1000) / HZ;
  735. if (copy_to_user(argp, &user, sizeof(user)))
  736. return -EFAULT;
  737. return 0;
  738. }
  739. }
  740. return -EINVAL;
  741. }
  742. static int ncp_ioctl_need_write(unsigned int cmd)
  743. {
  744. switch (cmd) {
  745. case NCP_IOC_GET_FS_INFO:
  746. case NCP_IOC_GET_FS_INFO_V2:
  747. case NCP_IOC_NCPREQUEST:
  748. case NCP_IOC_SETDENTRYTTL:
  749. case NCP_IOC_SIGN_INIT:
  750. case NCP_IOC_LOCKUNLOCK:
  751. case NCP_IOC_SET_SIGN_WANTED:
  752. return 1;
  753. case NCP_IOC_GETOBJECTNAME:
  754. case NCP_IOC_SETOBJECTNAME:
  755. case NCP_IOC_GETPRIVATEDATA:
  756. case NCP_IOC_SETPRIVATEDATA:
  757. case NCP_IOC_SETCHARSETS:
  758. case NCP_IOC_GETCHARSETS:
  759. case NCP_IOC_CONN_LOGGED_IN:
  760. case NCP_IOC_GETDENTRYTTL:
  761. case NCP_IOC_GETMOUNTUID2:
  762. case NCP_IOC_SIGN_WANTED:
  763. case NCP_IOC_GETROOT:
  764. case NCP_IOC_SETROOT:
  765. return 0;
  766. default:
  767. /* unkown IOCTL command, assume write */
  768. return 1;
  769. }
  770. }
  771. int ncp_ioctl(struct inode *inode, struct file *filp,
  772. unsigned int cmd, unsigned long arg)
  773. {
  774. int ret;
  775. if (ncp_ioctl_need_write(cmd)) {
  776. /*
  777. * inside the ioctl(), any failures which
  778. * are because of file_permission() are
  779. * -EACCESS, so it seems consistent to keep
  780. * that here.
  781. */
  782. if (mnt_want_write(filp->f_path.mnt))
  783. return -EACCES;
  784. }
  785. ret = __ncp_ioctl(inode, filp, cmd, arg);
  786. if (ncp_ioctl_need_write(cmd))
  787. mnt_drop_write(filp->f_path.mnt);
  788. return ret;
  789. }
  790. #ifdef CONFIG_COMPAT
  791. long ncp_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  792. {
  793. struct inode *inode = file->f_path.dentry->d_inode;
  794. int ret;
  795. lock_kernel();
  796. arg = (unsigned long) compat_ptr(arg);
  797. ret = ncp_ioctl(inode, file, cmd, arg);
  798. unlock_kernel();
  799. return ret;
  800. }
  801. #endif