mcserv.c 28 KB

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