mcserv.c 27 KB

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