mcserv.c 28 KB

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