mcserv.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  1. /* Server for the Midnight Commander Virtual File System.
  2. Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
  3. 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
  4. Written by:
  5. Miguel de Icaza, 1995, 1997,
  6. Andrej Borsenkow 1996.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. /**
  20. * \file
  21. * \brief Source: server for the Midnight Commander Virtual File System
  22. * \author Miguel de Icaza
  23. * \author Andrej Borsenkow
  24. * \date 1995, 1996, 1997
  25. *
  26. * \todo opendir instead of keeping its table of file handles could return
  27. * the pointer and expect the client to send a proper value back each
  28. * time :-) We should use syslog to register login/logout.
  29. */
  30. /* {{{ Includes and global variables */
  31. #include <config.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #ifdef HAVE_UNISTD_H
  35. # include <unistd.h>
  36. #endif
  37. #include <string.h>
  38. #ifdef HAVE_LIMITS_H
  39. # include <limits.h>
  40. #endif
  41. #ifndef NGROUPS_MAX
  42. # include <sys/param.h>
  43. # ifdef NGROUPS
  44. # define NGROUPS_MAX NGROUPS
  45. # endif
  46. #endif
  47. #ifdef HAVE_GRP_H
  48. # include <grp.h>
  49. #endif
  50. #include <sys/types.h>
  51. #include <sys/stat.h>
  52. #include <sys/wait.h>
  53. #include <errno.h>
  54. #include <signal.h>
  55. #ifdef HAVE_GETOPT_H
  56. # include <getopt.h>
  57. #endif
  58. /* Network include files */
  59. #include <sys/socket.h>
  60. #include <netinet/in.h>
  61. #include <netdb.h>
  62. #ifdef HAVE_ARPA_INET_H
  63. #include <arpa/inet.h>
  64. #endif
  65. #ifdef HAVE_PMAP_SET
  66. # include <rpc/rpc.h>
  67. # include <rpc/pmap_prot.h>
  68. # ifdef HAVE_RPC_PMAP_CLNT_H
  69. # include <rpc/pmap_clnt.h>
  70. # endif
  71. #endif
  72. #if defined(HAVE_PAM)
  73. # if !defined(HAVE_SECURITY_PAM_MISC_H)
  74. # undef HAVE_PAM
  75. # endif
  76. #endif
  77. /* Authentication include files */
  78. #include <pwd.h>
  79. #ifdef HAVE_PAM
  80. # include <security/pam_misc.h>
  81. # ifndef PAM_ESTABLISH_CRED
  82. # define PAM_ESTABLISH_CRED PAM_CRED_ESTABLISH
  83. # endif
  84. #else
  85. #endif /* !HAVE_PAM */
  86. #ifdef HAVE_CRYPT_H
  87. # include <crypt.h>
  88. #endif /* !HAVE_CRYPT_H */
  89. #ifdef HAVE_SHADOW_H
  90. # include <shadow.h>
  91. #else
  92. # ifdef HAVE_SHADOW_SHADOW_H
  93. # include <shadow/shadow.h>
  94. # endif
  95. #endif
  96. /*
  97. * GNU gettext defines printf to libintl_printf on platforms that lack
  98. * a native printf(3) capable of all POSIX features.
  99. */
  100. #undef ENABLE_NLS
  101. #include "../src/global.h"
  102. #include "../src/tty.h" /* enable/disable interrupt key */
  103. #include "../src/wtools.h" /* message() */
  104. #include "../src/main.h" /* print_vfs_message */
  105. #include "utilvfs.h"
  106. #include "vfs.h"
  107. #include "mcfs.h"
  108. #include "mcfsutil.h"
  109. #include "tcputil.h"
  110. #ifndef INADDR_NONE
  111. # define INADDR_NONE (0xffffffffU)
  112. #endif
  113. /* replacement for g_free() from glib */
  114. #undef g_free
  115. #define g_free(x) do {if (x) free (x);} while (0)
  116. /* We don't care about SIGPIPE */
  117. int got_sigpipe = 0;
  118. /* The socket from which we accept commands */
  119. int msock;
  120. /* Requested version number from client */
  121. static int clnt_version;
  122. /* If non zero, we accept further commands */
  123. int logged_in = 0;
  124. /* Home directory */
  125. const char *home_dir = NULL;
  126. char *up_dir = NULL;
  127. /* Were we started from inetd? */
  128. int inetd_started = 0;
  129. /* Are we running as a daemon? */
  130. int isDaemon = 0;
  131. /* guess */
  132. int verbose = 0;
  133. /* ftp auth */
  134. int ftp = 0;
  135. /* port number in which we listen to connections,
  136. * if zero, we try to contact the portmapper to get a port, and
  137. * if it's not possible, then we use a hardcoded value
  138. */
  139. int portnum = 0;
  140. /* if the server will use rcmd based authentication (hosts.equiv .rhosts) */
  141. int r_auth = 0;
  142. #define OPENDIR_HANDLES 8
  143. #define DO_QUIT_VOID() \
  144. do { \
  145. quit_server = 1; \
  146. return_code = 1; \
  147. return; \
  148. } while (0)
  149. /* Only used by get_port_number */
  150. #define DO_QUIT_NONVOID(a) \
  151. do { \
  152. quit_server = 1; \
  153. return_code = 1; \
  154. return (a); \
  155. } while (0)
  156. char buffer[4096];
  157. int debug = 1;
  158. static int quit_server;
  159. static int return_code;
  160. /* }}} */
  161. /* {{{ Misc routines */
  162. static void
  163. send_status (int status, int errno_number)
  164. {
  165. rpc_send (msock, RPC_INT, status, RPC_INT, errno_number, RPC_END);
  166. errno = 0;
  167. }
  168. /* }}} */
  169. /* {{{ File with handle operations */
  170. static void
  171. do_open (void)
  172. {
  173. int handle, flags, mode;
  174. char *arg;
  175. rpc_get (msock, RPC_STRING, &arg, RPC_INT, &flags, RPC_INT, &mode,
  176. RPC_END);
  177. handle = open (arg, flags, mode);
  178. send_status (handle, errno);
  179. g_free (arg);
  180. }
  181. static void
  182. do_read (void)
  183. {
  184. int handle, count, n;
  185. void *data;
  186. rpc_get (msock, RPC_INT, &handle, RPC_INT, &count, RPC_END);
  187. data = malloc (count);
  188. if (!data) {
  189. send_status (-1, ENOMEM);
  190. return;
  191. }
  192. if (verbose)
  193. printf ("count=%d\n", count);
  194. n = read (handle, data, count);
  195. if (verbose)
  196. printf ("result=%d\n", n);
  197. if (n < 0) {
  198. send_status (-1, errno);
  199. return;
  200. }
  201. send_status (n, 0);
  202. rpc_send (msock, RPC_BLOCK, n, data, RPC_END);
  203. g_free (data);
  204. }
  205. static void
  206. do_write (void)
  207. {
  208. int handle, count, status, written = 0;
  209. char buf[8192];
  210. rpc_get (msock, RPC_INT, &handle, RPC_INT, &count, RPC_END);
  211. status = 0;
  212. while (count) {
  213. int nbytes = count > 8192 ? 8192 : count;
  214. rpc_get (msock, RPC_BLOCK, nbytes, buf, RPC_END);
  215. status = write (handle, buf, nbytes);
  216. if (status < 0) {
  217. send_status (status, errno);
  218. return;
  219. }
  220. /* FIXED: amount written must be returned to caller */
  221. written += status;
  222. if (status < nbytes) {
  223. send_status (written, errno);
  224. return;
  225. }
  226. count -= nbytes;
  227. }
  228. send_status (written, errno);
  229. }
  230. static void
  231. do_lseek (void)
  232. {
  233. int handle, offset, whence, status;
  234. rpc_get (msock, RPC_INT, &handle, RPC_INT, &offset, RPC_INT, &whence,
  235. RPC_END);
  236. status = lseek (handle, offset, whence);
  237. send_status (status, errno);
  238. }
  239. static void
  240. do_close (void)
  241. {
  242. int handle, status;
  243. rpc_get (msock, RPC_INT, &handle, RPC_END);
  244. status = close (handle);
  245. send_status (status, errno);
  246. }
  247. /* }}} */
  248. /* {{{ Stat family routines */
  249. static void
  250. send_time (int sock, time_t time)
  251. {
  252. if (clnt_version == 1) {
  253. char *ct;
  254. int month;
  255. ct = ctime (&time);
  256. ct[3] = ct[10] = ct[13] = ct[16] = ct[19] = 0;
  257. /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
  258. if (ct[4] == 'J') {
  259. if (ct[5] == 'a') {
  260. month = 0;
  261. } else
  262. month = (ct[6] == 'n') ? 5 : 6;
  263. } else if (ct[4] == 'F') {
  264. month = 1;
  265. } else if (ct[4] == 'M') {
  266. month = (ct[6] == 'r') ? 2 : 5;
  267. } else if (ct[4] == 'A') {
  268. month = (ct[5] == 'p') ? 3 : 7;
  269. } else if (ct[4] == 'S') {
  270. month = 8;
  271. } else if (ct[4] == 'O') {
  272. month = 9;
  273. } else if (ct[4] == 'N') {
  274. month = 10;
  275. } else
  276. month = 11;
  277. rpc_send (msock, RPC_INT, atoi (&ct[17]), /* sec */
  278. RPC_INT, atoi (&ct[14]), /* min */
  279. RPC_INT, atoi (&ct[11]), /* hour */
  280. RPC_INT, atoi (&ct[8]), /* mday */
  281. RPC_INT, atoi (&ct[20]), /* year */
  282. RPC_INT, month, /* month */
  283. RPC_END);
  284. } else {
  285. long ltime = (long) time;
  286. char buf[BUF_SMALL];
  287. snprintf (buf, sizeof (buf), "%lx", ltime);
  288. rpc_send (msock, RPC_STRING, buf, RPC_END);
  289. }
  290. }
  291. static void
  292. send_stat_info (struct stat *st)
  293. {
  294. long mylong;
  295. int blocks =
  296. #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
  297. st->st_blocks;
  298. #else
  299. st->st_size / 1024;
  300. #endif
  301. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  302. mylong = st->st_rdev;
  303. #else
  304. mylong = 0;
  305. #endif
  306. rpc_send (msock, RPC_INT, (long) mylong, RPC_INT, (long) st->st_ino,
  307. RPC_INT, (long) st->st_mode, RPC_INT, (long) st->st_nlink,
  308. RPC_INT, (long) st->st_uid, RPC_INT, (long) st->st_gid,
  309. RPC_INT, (long) st->st_size, RPC_INT, (long) blocks,
  310. RPC_END);
  311. send_time (msock, st->st_atime);
  312. send_time (msock, st->st_mtime);
  313. send_time (msock, st->st_ctime);
  314. }
  315. static void
  316. do_lstat (void)
  317. {
  318. struct stat st;
  319. char *file;
  320. int n;
  321. rpc_get (msock, RPC_STRING, &file, RPC_END);
  322. n = lstat (file, &st);
  323. send_status (n, errno);
  324. if (n >= 0)
  325. send_stat_info (&st);
  326. g_free (file);
  327. }
  328. static void
  329. do_fstat (void)
  330. {
  331. int handle;
  332. int n;
  333. struct stat st;
  334. rpc_get (msock, RPC_INT, &handle, RPC_END);
  335. n = fstat (handle, &st);
  336. send_status (n, errno);
  337. if (n < 0)
  338. return;
  339. send_stat_info (&st);
  340. }
  341. static void
  342. do_stat (void)
  343. {
  344. struct stat st;
  345. int n;
  346. char *file;
  347. rpc_get (msock, RPC_STRING, &file, RPC_END);
  348. n = stat (file, &st);
  349. send_status (n, errno);
  350. if (n >= 0)
  351. send_stat_info (&st);
  352. g_free (file);
  353. }
  354. /* }}} */
  355. /* {{{ Directory lookup operations */
  356. static struct {
  357. int used;
  358. DIR *dirs[OPENDIR_HANDLES];
  359. char *names[OPENDIR_HANDLES];
  360. } mcfs_DIR;
  361. static void
  362. close_handle (int handle)
  363. {
  364. if (mcfs_DIR.used > 0)
  365. mcfs_DIR.used--;
  366. if (mcfs_DIR.dirs[handle])
  367. closedir (mcfs_DIR.dirs[handle]);
  368. g_free (mcfs_DIR.names[handle]);
  369. mcfs_DIR.dirs[handle] = 0;
  370. mcfs_DIR.names[handle] = 0;
  371. }
  372. static void
  373. do_opendir (void)
  374. {
  375. int handle, i;
  376. char *arg;
  377. DIR *p;
  378. rpc_get (msock, RPC_STRING, &arg, RPC_END);
  379. if (mcfs_DIR.used == OPENDIR_HANDLES) {
  380. send_status (-1, ENFILE); /* Error */
  381. g_free (arg);
  382. return;
  383. }
  384. handle = -1;
  385. for (i = 0; i < OPENDIR_HANDLES; i++) {
  386. if (mcfs_DIR.dirs[i] == 0) {
  387. handle = i;
  388. break;
  389. }
  390. }
  391. if (handle == -1) {
  392. send_status (-1, EMFILE);
  393. g_free (arg);
  394. if (!inetd_started)
  395. fprintf (stderr,
  396. "OOPS! you have found a bug in mc - do_opendir()!\n");
  397. return;
  398. }
  399. if (verbose)
  400. printf ("handle=%d\n", handle);
  401. p = opendir (arg);
  402. if (p) {
  403. mcfs_DIR.dirs[handle] = p;
  404. mcfs_DIR.names[handle] = arg;
  405. mcfs_DIR.used++;
  406. /* Because 0 is an error value */
  407. rpc_send (msock, RPC_INT, handle + 1, RPC_INT, 0, RPC_END);
  408. } else {
  409. send_status (-1, errno);
  410. g_free (arg);
  411. }
  412. }
  413. /* Sends the complete directory listing, as well as the stat information */
  414. static void
  415. do_readdir (void)
  416. {
  417. struct dirent *dirent;
  418. struct stat st;
  419. int handle, n;
  420. rpc_get (msock, RPC_INT, &handle, RPC_END);
  421. if (!handle) {
  422. rpc_send (msock, RPC_INT, 0, RPC_END);
  423. return;
  424. }
  425. /* We incremented it in opendir */
  426. handle--;
  427. while ((dirent = readdir (mcfs_DIR.dirs[handle]))) {
  428. int fname_len;
  429. char *fname;
  430. int length = NLENGTH (dirent);
  431. rpc_send (msock, RPC_INT, length, RPC_END);
  432. rpc_send (msock, RPC_BLOCK, length, dirent->d_name, RPC_END);
  433. fname_len =
  434. strlen (mcfs_DIR.names[handle]) + strlen (dirent->d_name) + 2;
  435. fname = malloc (fname_len);
  436. snprintf (fname, fname_len, "%s/%s", mcfs_DIR.names[handle],
  437. dirent->d_name);
  438. n = lstat (fname, &st);
  439. g_free (fname);
  440. send_status (n, errno);
  441. if (n >= 0)
  442. send_stat_info (&st);
  443. }
  444. rpc_send (msock, RPC_INT, 0, RPC_END);
  445. }
  446. static void
  447. do_closedir (void)
  448. {
  449. int handle;
  450. rpc_get (msock, RPC_INT, &handle, RPC_END);
  451. close_handle (handle - 1);
  452. }
  453. /* }}} */
  454. /* {{{ Operations with one and two file name argument */
  455. static void
  456. do_chdir (void)
  457. {
  458. char *file;
  459. int status;
  460. rpc_get (msock, RPC_STRING, &file, RPC_END);
  461. status = chdir (file);
  462. send_status (status, errno);
  463. g_free (file);
  464. }
  465. static void
  466. do_rmdir (void)
  467. {
  468. char *file;
  469. int status;
  470. rpc_get (msock, RPC_STRING, &file, RPC_END);
  471. status = rmdir (file);
  472. send_status (status, errno);
  473. g_free (file);
  474. }
  475. static void
  476. do_mkdir (void)
  477. {
  478. char *file;
  479. int mode, status;
  480. rpc_get (msock, RPC_STRING, &file, RPC_INT, &mode, RPC_END);
  481. status = mkdir (file, mode);
  482. send_status (status, errno);
  483. g_free (file);
  484. }
  485. static void
  486. do_mknod (void)
  487. {
  488. char *file;
  489. int mode, dev, status;
  490. rpc_get (msock, RPC_STRING, &file, RPC_INT, &mode, RPC_INT, &dev,
  491. RPC_END);
  492. status = mknod (file, mode, dev);
  493. send_status (status, errno);
  494. g_free (file);
  495. }
  496. static void
  497. do_readlink (void)
  498. {
  499. char buffer[2048];
  500. char *file;
  501. int n;
  502. rpc_get (msock, RPC_STRING, &file, RPC_END);
  503. n = readlink (file, buffer, 2048 - 1);
  504. send_status (n, errno);
  505. if (n >= 0) {
  506. buffer[n] = 0;
  507. rpc_send (msock, RPC_STRING, buffer, RPC_END);
  508. }
  509. g_free (file);
  510. }
  511. static void
  512. do_unlink (void)
  513. {
  514. char *file;
  515. int status;
  516. rpc_get (msock, RPC_STRING, &file, RPC_END);
  517. status = unlink (file);
  518. send_status (status, errno);
  519. g_free (file);
  520. }
  521. static void
  522. do_rename (void)
  523. {
  524. char *f1, *f2;
  525. int status;
  526. rpc_get (msock, RPC_STRING, &f1, RPC_STRING, &f2, RPC_END);
  527. status = rename (f1, f2);
  528. send_status (status, errno);
  529. g_free (f1);
  530. g_free (f2);
  531. }
  532. static void
  533. do_symlink (void)
  534. {
  535. char *f1, *f2;
  536. int status;
  537. rpc_get (msock, RPC_STRING, &f1, RPC_STRING, &f2, RPC_END);
  538. status = symlink (f1, f2);
  539. send_status (status, errno);
  540. g_free (f1);
  541. g_free (f2);
  542. }
  543. static void
  544. do_link (void)
  545. {
  546. char *f1, *f2;
  547. int status;
  548. rpc_get (msock, RPC_STRING, &f1, RPC_STRING, &f2, RPC_END);
  549. status = link (f1, f2);
  550. send_status (status, errno);
  551. g_free (f1);
  552. g_free (f2);
  553. }
  554. /* }}} */
  555. /* {{{ Misc commands */
  556. static void
  557. do_gethome (void)
  558. {
  559. rpc_send (msock, RPC_STRING, (home_dir) ? home_dir : "/", RPC_END);
  560. }
  561. static void
  562. do_getupdir (void)
  563. {
  564. rpc_send (msock, RPC_STRING, (up_dir) ? up_dir : "/", RPC_END);
  565. }
  566. static void
  567. do_chmod (void)
  568. {
  569. char *file;
  570. int mode, status;
  571. rpc_get (msock, RPC_STRING, &file, RPC_INT, &mode, RPC_END);
  572. status = chmod (file, mode);
  573. send_status (status, errno);
  574. g_free (file);
  575. }
  576. static void
  577. do_chown (void)
  578. {
  579. char *file;
  580. int owner, group, status;
  581. rpc_get (msock, RPC_STRING, &file, RPC_INT, &owner, RPC_INT, &group,
  582. RPC_END);
  583. status = chown (file, owner, group);
  584. send_status (status, errno);
  585. g_free (file);
  586. }
  587. static void
  588. do_utime (void)
  589. {
  590. char *file;
  591. int status;
  592. long atime;
  593. long mtime;
  594. char *as;
  595. char *ms;
  596. struct utimbuf times;
  597. rpc_get (msock, RPC_STRING, &file, RPC_STRING, &as, RPC_STRING, &ms,
  598. RPC_END);
  599. sscanf (as, "%lx", &atime);
  600. sscanf (ms, "%lx", &mtime);
  601. if (verbose)
  602. printf ("Got a = %s, m = %s, comp a = %ld, m = %ld\n", as, ms,
  603. atime, mtime);
  604. g_free (as);
  605. g_free (ms);
  606. times.actime = (time_t) atime;
  607. times.modtime = (time_t) mtime;
  608. status = utime (file, &times);
  609. send_status (status, errno);
  610. g_free (file);
  611. }
  612. static void
  613. do_quit (void)
  614. {
  615. quit_server = 1;
  616. }
  617. #ifdef HAVE_PAM
  618. struct user_pass {
  619. const char *username;
  620. const char *password;
  621. };
  622. static int
  623. mc_pam_conversation (int messages, const struct pam_message **msg,
  624. struct pam_response **resp, void *appdata_ptr)
  625. {
  626. struct pam_response *r;
  627. struct user_pass *up = appdata_ptr;
  628. int status;
  629. r = (struct pam_response *) malloc (sizeof (struct pam_response) *
  630. messages);
  631. if (!r)
  632. return PAM_CONV_ERR;
  633. *resp = r;
  634. for (status = PAM_SUCCESS; messages--; msg++, r++) {
  635. switch ((*msg)->msg_style) {
  636. case PAM_PROMPT_ECHO_ON:
  637. r->resp = strdup (up->username);
  638. r->resp_retcode = PAM_SUCCESS;
  639. break;
  640. case PAM_PROMPT_ECHO_OFF:
  641. r->resp = strdup (up->password);
  642. r->resp_retcode = PAM_SUCCESS;
  643. break;
  644. case PAM_ERROR_MSG:
  645. r->resp = NULL;
  646. r->resp_retcode = PAM_SUCCESS;
  647. break;
  648. case PAM_TEXT_INFO:
  649. r->resp = NULL;
  650. r->resp_retcode = PAM_SUCCESS;
  651. break;
  652. }
  653. }
  654. return status;
  655. }
  656. static struct pam_conv conv = { &mc_pam_conversation, NULL };
  657. /* Return 0 if authentication failed, 1 otherwise */
  658. static int
  659. mc_pam_auth (const char *username, const char *password)
  660. {
  661. pam_handle_t *pamh;
  662. struct user_pass up;
  663. int status;
  664. up.username = username;
  665. up.password = password;
  666. conv.appdata_ptr = &up;
  667. if ((status =
  668. pam_start ("mcserv", username, &conv, &pamh)) != PAM_SUCCESS)
  669. goto failed_pam;
  670. if ((status = pam_authenticate (pamh, 0)) != PAM_SUCCESS)
  671. goto failed_pam;
  672. if ((status = pam_acct_mgmt (pamh, 0)) != PAM_SUCCESS)
  673. goto failed_pam;
  674. if ((status = pam_setcred (pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS)
  675. goto failed_pam;
  676. pam_end (pamh, status);
  677. return 0;
  678. failed_pam:
  679. pam_end (pamh, status);
  680. return 1;
  681. }
  682. #else /* !HAVE_PAM */
  683. /* Keep reading until we find a \n */
  684. static int
  685. next_line (int socket)
  686. {
  687. char c;
  688. while (1) {
  689. if (read (socket, &c, 1) <= 0)
  690. return 0;
  691. if (c == '\n')
  692. return 1;
  693. }
  694. }
  695. static int
  696. ftp_answer (int sock, const char *text)
  697. {
  698. char answer[4];
  699. next_line (sock);
  700. socket_read_block (sock, answer, 3);
  701. answer[3] = 0;
  702. if (strcmp (answer, text) == 0)
  703. return 1;
  704. return 0;
  705. }
  706. static int
  707. send_string (int sock, const char *string)
  708. {
  709. return socket_write_block (sock, string, strlen (string));
  710. }
  711. static int
  712. do_ftp_auth (const char *username, const char *password)
  713. {
  714. struct sockaddr_in local_address;
  715. unsigned long inaddr;
  716. int my_socket;
  717. char answer[4];
  718. memset ((char *) &local_address, 0, sizeof (local_address));
  719. local_address.sin_family = AF_INET;
  720. /* FIXME: extract the ftp port with the proper function */
  721. local_address.sin_port = htons (21);
  722. /* Convert localhost to usable format */
  723. if ((inaddr = inet_addr ("127.0.0.1")) != INADDR_NONE)
  724. memcpy ((char *) &local_address.sin_addr, (char *) &inaddr,
  725. sizeof (inaddr));
  726. if ((my_socket = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
  727. if (!isDaemon)
  728. fprintf (stderr, "do_auth: can't create socket\n");
  729. return 0;
  730. }
  731. if (connect
  732. (my_socket, (struct sockaddr *) &local_address,
  733. sizeof (local_address)) < 0) {
  734. fprintf (stderr,
  735. "do_auth: can't connect to ftp daemon for authentication\n");
  736. close (my_socket);
  737. return 0;
  738. }
  739. send_string (my_socket, "user ");
  740. send_string (my_socket, username);
  741. send_string (my_socket, "\r\n");
  742. if (!ftp_answer (my_socket, "331")) {
  743. send_string (my_socket, "quit\r\n");
  744. close (my_socket);
  745. return 0;
  746. }
  747. next_line (my_socket); /* Eat all the line */
  748. send_string (my_socket, "pass ");
  749. send_string (my_socket, password);
  750. send_string (my_socket, "\r\n");
  751. socket_read_block (my_socket, answer, 3);
  752. answer[3] = 0;
  753. send_string (my_socket, "\r\n");
  754. send_string (my_socket, "quit\r\n");
  755. close (my_socket);
  756. if (strcmp (answer, "230") == 0)
  757. return 1;
  758. return 0;
  759. }
  760. #ifdef HAVE_CRYPT
  761. static int
  762. do_classic_auth (const char *username, const char *password)
  763. {
  764. int ret = 0;
  765. const char *encr_pwd = NULL;
  766. struct passwd *pw;
  767. #ifdef HAVE_SHADOW
  768. struct spwd *spw;
  769. #endif
  770. if ((pw = getpwnam (username)) == 0)
  771. return 0;
  772. #ifdef HAVE_SHADOW
  773. setspent ();
  774. /* Password expiration is not checked! */
  775. if ((spw = getspnam (username)) == NULL)
  776. encr_pwd = "*";
  777. else
  778. encr_pwd = spw->sp_pwdp;
  779. endspent ();
  780. #else
  781. encr_pwd = pw->pw_passwd;
  782. #endif
  783. if (strcmp (crypt (password, encr_pwd), encr_pwd) == 0)
  784. ret = 1;
  785. endpwent ();
  786. return ret;
  787. }
  788. #endif /* HAVE_CRYPT */
  789. #endif /* !HAVE_PAM */
  790. /* Try to authenticate the user based on:
  791. - PAM if the system has it, else it checks:
  792. - pwdauth if the system supports it.
  793. - conventional auth (check salt on /etc/passwd, crypt, and compare
  794. - try to contact the local ftp server and login (if -f flag used)
  795. */
  796. static int
  797. do_auth (const char *username, const char *password)
  798. {
  799. int auth = 0;
  800. struct passwd *this;
  801. if (strcmp (username, "anonymous") == 0)
  802. username = "ftp";
  803. #ifdef HAVE_PAM
  804. if (mc_pam_auth (username, password) == 0)
  805. auth = 1;
  806. #else /* if there is no pam */
  807. #ifdef HAVE_PWDAUTH
  808. if (pwdauth (username, password) == 0)
  809. auth = 1;
  810. else
  811. #endif
  812. #ifdef HAVE_CRYPT
  813. if (do_classic_auth (username, password))
  814. auth = 1;
  815. else
  816. #endif
  817. if (ftp)
  818. auth = do_ftp_auth (username, password);
  819. #endif /* not pam */
  820. if (!auth)
  821. return 0;
  822. this = getpwnam (username);
  823. if (this == 0)
  824. return 0;
  825. if (chdir (this->pw_dir) == -1)
  826. return 0;
  827. if (this->pw_dir[strlen (this->pw_dir) - 1] == '/')
  828. home_dir = strdup (this->pw_dir);
  829. else {
  830. char *new_home_dir = malloc (strlen (this->pw_dir) + 2);
  831. if (new_home_dir) {
  832. strcpy (new_home_dir, this->pw_dir);
  833. strcat (new_home_dir, "/");
  834. home_dir = new_home_dir;
  835. } else
  836. home_dir = "/";
  837. }
  838. if (setgid (this->pw_gid) == -1)
  839. return 0;
  840. #ifdef HAVE_INITGROUPS
  841. #ifdef NGROUPS_MAX
  842. if (NGROUPS_MAX > 1 && initgroups (this->pw_name, this->pw_gid))
  843. return 0;
  844. #endif
  845. #endif
  846. #if defined (HAVE_SETUID)
  847. if (setuid (this->pw_uid))
  848. return 0;
  849. #elif defined (HAVE_SETREUID)
  850. if (setreuid (this->pw_uid, this->pw_uid))
  851. return 0;
  852. #endif
  853. /* If the setuid call failed, then deny access */
  854. /* This should fix the problem on those machines with strange setups */
  855. if (getuid () != this->pw_uid)
  856. return 0;
  857. if (strcmp (username, "ftp") == 0)
  858. chroot (this->pw_dir);
  859. endpwent ();
  860. return auth;
  861. }
  862. #if 0
  863. static int
  864. do_rauth (int socket)
  865. {
  866. struct sockaddr_in from;
  867. struct hostent *hp;
  868. if (getpeername (0, (struct sockaddr *) &from, &fromlen) < 0)
  869. return 0;
  870. from.sin_port = ntohs ((unsigned short) from.sin_port);
  871. /* Strange, this should not happend */
  872. if (from.sin_family != AF_INET)
  873. return 0;
  874. hp = gethostbyaddr ((char *) &fromp.sin_addr, sizeof (struct in_addr),
  875. fromp.sin_family);
  876. }
  877. #endif
  878. static int
  879. do_rauth (int msock)
  880. {
  881. return 0;
  882. }
  883. static void
  884. login_reply (int logged_in)
  885. {
  886. rpc_send (msock, RPC_INT, logged_in ? MC_LOGINOK : MC_INVALID_PASS,
  887. RPC_END);
  888. }
  889. /* FIXME: Implement the anonymous login */
  890. static void
  891. do_login (void)
  892. {
  893. char *username;
  894. char *password;
  895. int result;
  896. rpc_get (msock, RPC_LIMITED_STRING, &up_dir, RPC_LIMITED_STRING,
  897. &username, RPC_END);
  898. if (verbose)
  899. printf ("username: %s\n", username);
  900. if (r_auth) {
  901. logged_in = do_rauth (msock);
  902. if (logged_in) {
  903. login_reply (logged_in);
  904. return;
  905. }
  906. }
  907. rpc_send (msock, RPC_INT, MC_NEED_PASSWORD, RPC_END);
  908. rpc_get (msock, RPC_INT, &result, RPC_END);
  909. if (result == MC_QUIT)
  910. DO_QUIT_VOID ();
  911. if (result != MC_PASS) {
  912. if (verbose)
  913. printf ("do_login: Unknown response: %d\n", result);
  914. DO_QUIT_VOID ();
  915. }
  916. rpc_get (msock, RPC_LIMITED_STRING, &password, RPC_END);
  917. logged_in = do_auth (username, password);
  918. endpwent ();
  919. login_reply (logged_in);
  920. }
  921. /* }}} */
  922. /* {{{ Server and dispatching functions */
  923. /* This structure must be kept in synch with mcfs.h enums */
  924. static const struct _command {
  925. const char *command;
  926. void (*callback) (void);
  927. } commands[] = {
  928. {
  929. "open", do_open}, {
  930. "close", do_close}, {
  931. "read", do_read}, {
  932. "write", do_write}, {
  933. "opendir", do_opendir}, {
  934. "readdir", do_readdir}, {
  935. "closedir", do_closedir}, {
  936. "stat ", do_stat}, {
  937. "lstat ", do_lstat}, {
  938. "fstat", do_fstat}, {
  939. "chmod", do_chmod}, {
  940. "chown", do_chown}, {
  941. "readlink ", do_readlink}, {
  942. "unlink", do_unlink}, {
  943. "rename", do_rename}, {
  944. "chdir ", do_chdir}, {
  945. "lseek", do_lseek}, {
  946. "rmdir", do_rmdir}, {
  947. "symlink", do_symlink}, {
  948. "mknod", do_mknod}, {
  949. "mkdir", do_mkdir}, {
  950. "link", do_link}, {
  951. "gethome", do_gethome}, {
  952. "getupdir", do_getupdir}, {
  953. "login", do_login}, {
  954. "quit", do_quit}, {
  955. "utime", do_utime}};
  956. static int ncommands = sizeof (commands) / sizeof (struct _command);
  957. static void
  958. exec_command (int command)
  959. {
  960. if (command < 0 || command >= ncommands
  961. || commands[command].command == 0) {
  962. fprintf (stderr, "Got unknown command: %d\n", command);
  963. DO_QUIT_VOID ();
  964. }
  965. if (verbose)
  966. printf ("Command: %s\n", commands[command].command);
  967. (*commands[command].callback) ();
  968. }
  969. static void
  970. check_version (void)
  971. {
  972. int version;
  973. rpc_get (msock, RPC_INT, &version, RPC_END);
  974. if (version >= 1 && version <= RPC_PROGVER)
  975. rpc_send (msock, RPC_INT, MC_VERSION_OK, RPC_END);
  976. else
  977. rpc_send (msock, RPC_INT, MC_VERSION_MISMATCH, RPC_END);
  978. clnt_version = version;
  979. }
  980. /* This routine is called by rpc_get/rpc_send when the connection is closed */
  981. void
  982. tcp_invalidate_socket (int sock)
  983. {
  984. if (verbose)
  985. printf ("Connection closed\n");
  986. DO_QUIT_VOID ();
  987. }
  988. static void
  989. server (int sock)
  990. {
  991. int command;
  992. msock = sock;
  993. quit_server = 0;
  994. check_version ();
  995. do {
  996. if (rpc_get (sock, RPC_INT, &command, RPC_END)
  997. && (logged_in || command == MC_LOGIN))
  998. exec_command (command);
  999. } while (!quit_server);
  1000. }
  1001. /* }}} */
  1002. /* {{{ Net support code */
  1003. static const char *
  1004. get_client (int portnum)
  1005. {
  1006. int sock, newsocket;
  1007. unsigned int clilen;
  1008. struct sockaddr_in client_address, server_address;
  1009. int yes = 1;
  1010. if ((sock = socket (AF_INET, SOCK_STREAM, 0)) < 0)
  1011. return "Cannot create socket";
  1012. /* Use this to debug: */
  1013. if (setsockopt
  1014. (sock, SOL_SOCKET, SO_REUSEADDR, (char *) &yes, sizeof (yes)) < 0)
  1015. return "setsockopt failed";
  1016. memset ((char *) &server_address, 0, sizeof (server_address));
  1017. server_address.sin_family = AF_INET;
  1018. server_address.sin_addr.s_addr = htonl (INADDR_ANY);
  1019. server_address.sin_port = htons (portnum);
  1020. if (bind
  1021. (sock, (struct sockaddr *) &server_address,
  1022. sizeof (server_address)) < 0)
  1023. return "Cannot bind";
  1024. listen (sock, 5);
  1025. for (;;) {
  1026. int child;
  1027. clilen = sizeof (client_address);
  1028. newsocket =
  1029. accept (sock, (struct sockaddr *) &client_address, &clilen);
  1030. if (isDaemon && (child = fork ())) {
  1031. int status;
  1032. close (newsocket);
  1033. waitpid (child, &status, 0);
  1034. continue;
  1035. }
  1036. if (isDaemon && fork ())
  1037. exit (0);
  1038. server (newsocket);
  1039. close (newsocket);
  1040. return 0;
  1041. }
  1042. }
  1043. #ifdef HAVE_PMAP_SET
  1044. static void
  1045. signal_int_handler (int sig)
  1046. {
  1047. (void) sig;
  1048. pmap_unset (RPC_PROGNUM, RPC_PROGVER);
  1049. }
  1050. #endif
  1051. #ifndef IPPORT_RESERVED
  1052. #define IPPORT_RESERVED 1024
  1053. #endif
  1054. static int
  1055. get_port_number (void)
  1056. {
  1057. int port = 0;
  1058. #ifdef HAVE_RRESVPORT
  1059. int start_port = IPPORT_RESERVED;
  1060. port = rresvport (&start_port);
  1061. if (port == -1) {
  1062. if (geteuid () == 0) {
  1063. fprintf (stderr,
  1064. "Cannot bind the server on a reserved port\n");
  1065. DO_QUIT_NONVOID (-1);
  1066. }
  1067. port = 0;
  1068. }
  1069. #endif
  1070. if (port)
  1071. return port;
  1072. port = mcserver_port;
  1073. return port;
  1074. }
  1075. static void
  1076. register_port (int portnum, int abort_if_fail)
  1077. {
  1078. #ifdef HAVE_PMAP_SET
  1079. /* Register our service with the portmapper */
  1080. /* protocol: pmap_set (prognum, versnum, protocol, portp) */
  1081. if (pmap_set (RPC_PROGNUM, RPC_PROGVER, IPPROTO_TCP, portnum))
  1082. signal (SIGINT, signal_int_handler);
  1083. else {
  1084. fprintf (stderr, "Cannot register service with portmapper\n");
  1085. if (abort_if_fail)
  1086. exit (1);
  1087. }
  1088. #else
  1089. if (abort_if_fail) {
  1090. fprintf (stderr,
  1091. "This system lacks port registration, try using the -p\n"
  1092. "flag to force installation at a given port");
  1093. }
  1094. #endif
  1095. }
  1096. /* }}} */
  1097. int
  1098. main (int argc, char *argv[])
  1099. {
  1100. const char *result;
  1101. int c;
  1102. while ((c = getopt (argc, argv, "fdiqp:v")) != -1) {
  1103. switch (c) {
  1104. case 'd':
  1105. isDaemon = 1;
  1106. verbose = 0;
  1107. break;
  1108. case 'v':
  1109. verbose = 1;
  1110. break;
  1111. case 'f':
  1112. ftp = 1;
  1113. break;
  1114. case 'q':
  1115. verbose = 0;
  1116. break;
  1117. case 'p':
  1118. portnum = atoi (optarg);
  1119. break;
  1120. case 'i':
  1121. inetd_started = 1;
  1122. break;
  1123. case 'r':
  1124. r_auth = 1;
  1125. break;
  1126. default:
  1127. fprintf (stderr,
  1128. "Usage is: mcserv [options] [-p portnum]\n\n"
  1129. "options are:\n" "-d become a daemon (sets -q)\n"
  1130. "-q quiet mode\n"
  1131. /* "-r use rhost based authentication\n" */
  1132. #ifndef HAVE_PAM
  1133. "-f force ftp authentication\n"
  1134. #endif
  1135. "-v verbose mode\n"
  1136. "-p to specify a port number to listen\n");
  1137. exit (0);
  1138. }
  1139. }
  1140. if (isDaemon && fork ())
  1141. exit (0);
  1142. if (portnum == 0)
  1143. portnum = get_port_number ();
  1144. if (portnum != -1) {
  1145. register_port (portnum, 0);
  1146. if (verbose)
  1147. printf ("Using port %d\n", portnum);
  1148. if ((result = get_client (portnum)))
  1149. perror (result);
  1150. #ifdef HAVE_PMAP_SET
  1151. if (!isDaemon)
  1152. pmap_unset (RPC_PROGNUM, RPC_PROGVER);
  1153. #endif
  1154. }
  1155. exit (return_code);
  1156. }
  1157. /* FIXME: This function should not be used in mcserv */
  1158. void
  1159. vfs_die (const char *m)
  1160. {
  1161. fputs (m, stderr);
  1162. exit (1);
  1163. }