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