mcserv.c 28 KB

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