Sort order transalted...
[midnight-commander.git] / vfs / mcserv.c
blob1cb820df34d46ed5cf85cfa6a1e8ee099ee320a3
1 /* Server for the Midnight Commander Virtual File System.
3 Copyright (C) 1995, 1996, 1997 The Free Software Foundation
5 Written by:
6 Miguel de Icaza, 1995, 1997,
7 Andrej Borsenkow 1996.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 TODO:
24 opendir instead of keeping its table of file handles could return
25 the pointer and expect the client to send a proper value back each
26 time :-)
28 We should use syslog to register login/logout.
32 /* {{{ Includes and global variables */
34 #include <config.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #ifdef HAVE_UNISTD_H
38 # include <unistd.h>
39 #endif
40 #include <fcntl.h>
41 #include <string.h>
42 #ifdef HAVE_LIMITS_H
43 # include <limits.h>
44 #endif
45 #ifndef NGROUPS_MAX
46 # include <sys/param.h>
47 # ifdef NGROUPS
48 # define NGROUPS_MAX NGROUPS
49 # endif
50 #endif
51 #ifdef HAVE_GRP_H
52 # include <grp.h>
53 #endif
54 #ifdef HAVE_CRYPT_H
55 # include <crypt.h>
56 #endif
57 #include <sys/types.h>
58 #include <sys/stat.h>
59 #include <sys/wait.h>
60 #include <errno.h>
61 #include <signal.h>
63 /* Network include files */
64 #include <sys/socket.h>
65 #include <netinet/in.h>
66 #include <netdb.h>
67 #include <arpa/inet.h>
68 #ifdef HAVE_PMAP_SET
69 # include <rpc/rpc.h>
70 # include <rpc/pmap_prot.h>
71 # ifdef HAVE_RPC_PMAP_CLNT_H
72 # include <rpc/pmap_clnt.h>
73 # endif
74 #endif
76 /* Authentication include files */
77 #include <pwd.h>
78 #ifdef HAVE_PAM
79 # include <security/pam_misc.h>
80 # ifndef PAM_ESTABLISH_CRED
81 # define PAM_ESTABLISH_CRED PAM_CRED_ESTABLISH
82 # endif
83 #endif
85 #include "utilvfs.h"
87 #include "vfs.h"
88 #include "mcfs.h"
89 #include "tcputil.h"
91 /* The socket from which we accept commands */
92 int msock;
94 /* Requested version number from client */
95 static int clnt_version;
97 /* If non zero, we accept further commands */
98 int logged_in = 0;
100 /* Home directory */
101 char *home_dir = NULL;
103 char *up_dir = NULL;
105 /* Were we started from inetd? */
106 int inetd_started = 0;
108 /* Are we running as a daemon? */
109 int isDaemon = 0;
111 /* guess */
112 int verbose = 0;
114 /* ftp auth */
115 int ftp = 0;
117 /* port number in which we listen to connections,
118 * if zero, we try to contact the portmapper to get a port, and
119 * if it's not possible, then we use a hardcoded value
121 int portnum = 0;
123 /* if the server will use rcmd based authentication (hosts.equiv .rhosts) */
124 int r_auth = 0;
126 #define OPENDIR_HANDLES 8
128 #define DO_QUIT_VOID() \
129 do { \
130 quit_server = 1; \
131 return_code = 1; \
132 return; \
133 } while (0)
135 /* Only used by get_port_number */
136 #define DO_QUIT_NONVOID(a) \
137 do { \
138 quit_server = 1; \
139 return_code = 1; \
140 return (a); \
141 } while (0)
143 char buffer [4096];
144 int debug = 1;
145 static int quit_server;
146 static int return_code;
148 /* }}} */
150 /* {{{ Misc routines */
152 static void send_status (int status, int errno_number)
154 rpc_send (msock, RPC_INT, status, RPC_INT, errno_number, RPC_END);
155 errno = 0;
158 /* }}} */
160 /* {{{ File with handle operations */
162 static void do_open (void)
164 int handle, flags, mode;
165 char *arg;
167 rpc_get (msock, RPC_STRING, &arg, RPC_INT, &flags, RPC_INT, &mode,RPC_END);
169 handle = open (arg, flags, mode);
170 send_status (handle, errno);
171 g_free (arg);
174 static void do_read (void)
176 int handle, count, n;
177 void *data;
179 rpc_get (msock, RPC_INT, &handle, RPC_INT, &count, RPC_END);
180 data = g_malloc (count);
181 if (!data){
182 send_status (-1, ENOMEM);
183 return;
185 if (verbose) printf ("count=%d\n", count);
186 n = read (handle, data, count);
187 if (verbose) printf ("result=%d\n", n);
188 if (n < 0){
189 send_status (-1, errno);
190 return;
192 send_status (n, 0);
193 rpc_send (msock, RPC_BLOCK, n, data, RPC_END);
195 g_free (data);
198 static void do_write (void)
200 int handle, count, status, written = 0;
201 char buf[8192];
203 rpc_get (msock, RPC_INT, &handle, RPC_INT, &count, RPC_END);
204 status = 0;
205 while (count) {
206 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;
214 /* FIXED: amount written must be returned to caller */
215 written += status;
216 if (status < nbytes) {
217 send_status (written, errno);
218 return;
220 count -= nbytes;
222 send_status (written, errno);
225 static void do_lseek (void)
227 int handle, offset, whence, status;
229 rpc_get (msock,
230 RPC_INT, &handle,
231 RPC_INT, &offset,
232 RPC_INT, &whence, RPC_END);
233 status = lseek (handle, offset, whence);
234 send_status (status, errno);
237 static void do_close (void)
239 int handle, status;
241 rpc_get (msock, RPC_INT, &handle, RPC_END);
242 status = close (handle);
243 send_status (status, errno);
246 /* }}} */
248 /* {{{ Stat family routines */
250 static void send_time (int sock, time_t time)
252 if (clnt_version == 1) {
253 char *ct;
254 int month;
256 ct = ctime (&time);
257 ct [3] = ct [10] = ct [13] = ct [16] = ct [19] = 0;
259 /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
260 if (ct [4] == 'J'){
261 if (ct [5] == 'a'){
262 month = 0;
263 } else
264 month = (ct [6] == 'n') ? 5 : 6;
265 } else if (ct [4] == 'F'){
266 month = 1;
267 } else if (ct [4] == 'M'){
268 month = (ct [6] == 'r') ? 2 : 5;
269 } else if (ct [4] == 'A'){
270 month = (ct [5] == 'p') ? 3 : 7;
271 } else if (ct [4] == 'S'){
272 month = 8;
273 } else if (ct [4] == 'O'){
274 month = 9;
275 } else if (ct [4] == 'N'){
276 month = 10;
277 } else
278 month = 11;
279 rpc_send (msock,
280 RPC_INT, atoi (&ct [17]), /* sec */
281 RPC_INT, atoi (&ct [14]), /* min */
282 RPC_INT, atoi (&ct [11]), /* hour */
283 RPC_INT, atoi (&ct [8]), /* mday */
284 RPC_INT, atoi (&ct [20]), /* year */
285 RPC_INT, month, /* month */
286 RPC_END);
287 } else {
288 long ltime = (long) time;
289 char buf[BUF_SMALL];
291 g_snprintf (buf, sizeof(buf), "%lx", ltime);
292 rpc_send (msock,
293 RPC_STRING, buf,
294 RPC_END);
298 static void send_stat_info (struct stat *st)
300 long mylong;
301 int blocks =
302 #ifdef HAVE_ST_BLOCKS
303 st->st_blocks;
304 #else
305 st->st_size / 1024;
306 #endif
308 #ifdef HAVE_ST_RDEV
309 mylong = st->st_rdev;
310 #else
311 mylong = 0;
312 #endif
313 rpc_send (msock, RPC_INT, (long) mylong,
314 RPC_INT, (long) st->st_ino,
315 RPC_INT, (long) st->st_mode,
316 RPC_INT, (long) st->st_nlink,
317 RPC_INT, (long) st->st_uid,
318 RPC_INT, (long) st->st_gid,
319 RPC_INT, (long) st->st_size,
320 RPC_INT, (long) blocks, RPC_END);
321 send_time (msock, st->st_atime);
322 send_time (msock, st->st_mtime);
323 send_time (msock, st->st_ctime);
326 static void do_lstat (void)
328 struct stat st;
329 char *file;
330 int n;
332 rpc_get (msock, RPC_STRING, &file, RPC_END);
333 n = lstat (file, &st);
334 send_status (n, errno);
335 if (n >= 0)
336 send_stat_info (&st);
337 g_free (file);
340 static void do_fstat (void)
342 int handle;
343 int n;
344 struct stat st;
346 rpc_get (msock, RPC_INT, &handle, RPC_END);
347 n = fstat (handle, &st);
348 send_status (n, errno);
349 if (n < 0)
350 return;
352 send_stat_info (&st);
355 static void do_stat (void)
357 struct stat st;
358 int n;
359 char *file;
361 rpc_get (msock, RPC_STRING, &file, RPC_END);
363 n = stat (file, &st);
364 send_status (n, errno);
365 if (n >= 0)
366 send_stat_info (&st);
367 g_free (file);
370 /* }}} */
372 /* {{{ Directory lookup operations */
374 static struct {
375 int used;
376 DIR *dirs [OPENDIR_HANDLES];
377 char *names [OPENDIR_HANDLES];
378 } mcfs_DIR;
380 static void close_handle (int handle)
382 if (mcfs_DIR.used > 0) mcfs_DIR.used--;
383 if (mcfs_DIR.dirs [handle])
384 closedir (mcfs_DIR.dirs [handle]);
385 if (mcfs_DIR.names [handle])
386 g_free (mcfs_DIR.names [handle]);
387 mcfs_DIR.dirs [handle] = 0;
388 mcfs_DIR.names [handle] = 0;
391 static void do_opendir (void)
393 int handle, i;
394 char *arg;
395 DIR *p;
397 rpc_get (msock, RPC_STRING, &arg, RPC_END);
399 if (mcfs_DIR.used == OPENDIR_HANDLES){
400 send_status (-1, ENFILE); /* Error */
401 g_free (arg);
402 return;
405 handle = -1;
406 for (i = 0; i < OPENDIR_HANDLES; i++){
407 if (mcfs_DIR.dirs [i] == 0){
408 handle = i;
409 break;
413 if (handle == -1){
414 send_status (-1, EMFILE);
415 g_free (arg);
416 if (!inetd_started)
417 fprintf (stderr, "OOPS! you have found a bug in mc - do_opendir()!\n");
418 return;
421 if (verbose) printf ("handle=%d\n", handle);
422 p = opendir (arg);
423 if (p){
424 mcfs_DIR.dirs [handle] = p;
425 mcfs_DIR.names [handle] = arg;
426 mcfs_DIR.used ++;
428 /* Because 0 is an error value */
429 rpc_send (msock, RPC_INT, handle+1, RPC_INT, 0, RPC_END);
431 } else {
432 send_status (-1, errno);
433 g_free (arg);
437 /* Sends the complete directory listing, as well as the stat information */
438 static void do_readdir (void)
440 struct dirent *dirent;
441 struct stat st;
442 int handle, n;
443 char *fname = 0;
445 rpc_get (msock, RPC_INT, &handle, RPC_END);
447 if (!handle){
448 rpc_send (msock, RPC_INT, 0, RPC_END);
449 return;
452 /* We incremented it in opendir */
453 handle --;
455 while ((dirent = readdir (mcfs_DIR.dirs [handle]))){
456 int length = NLENGTH (dirent);
458 rpc_send (msock, RPC_INT, length, RPC_END);
459 rpc_send (msock, RPC_BLOCK, length, dirent->d_name, RPC_END);
460 fname = g_strconcat (mcfs_DIR.names [handle],
461 PATH_SEP_STR, dirent->d_name, NULL);
462 n = lstat (fname, &st);
463 send_status (n, errno);
464 g_free (fname);
465 if (n >= 0)
466 send_stat_info (&st);
468 rpc_send (msock, RPC_INT, 0, RPC_END);
471 static void do_closedir (void)
473 int handle;
475 rpc_get (msock, RPC_INT, &handle, RPC_END);
476 close_handle (handle-1);
479 /* }}} */
481 /* {{{ Operations with one and two file name argument */
483 static void do_chdir (void)
485 char *file;
486 int status;
488 rpc_get (msock, RPC_STRING, &file, RPC_END);
490 status = chdir (file);
491 send_status (status, errno);
492 g_free (file);
495 static void do_rmdir (void)
497 char *file;
498 int status;
500 rpc_get (msock, RPC_STRING, &file, RPC_END);
502 status = rmdir (file);
503 send_status (status, errno);
504 g_free (file);
507 static void do_mkdir (void)
509 char *file;
510 int mode, status;
512 rpc_get (msock, RPC_STRING, &file, RPC_INT, &mode, RPC_END);
514 status = mkdir (file, mode);
515 send_status (status, errno);
516 g_free (file);
519 static void do_mknod (void)
521 char *file;
522 int mode, dev, status;
524 rpc_get (msock, RPC_STRING, &file, RPC_INT, &mode, RPC_INT, &dev, RPC_END);
526 status = mknod (file, mode, dev);
527 send_status (status, errno);
528 g_free (file);
531 static void do_readlink (void)
533 char buffer [2048];
534 char *file;
535 int n;
537 rpc_get (msock, RPC_STRING, &file, RPC_END);
538 n = readlink (file, buffer, 2048);
539 send_status (n, errno);
540 if (n >= 0) {
541 buffer [n] = 0;
542 rpc_send (msock, RPC_STRING, buffer, RPC_END);
544 g_free (file);
547 static void do_unlink (void)
549 char *file;
550 int status;
552 rpc_get (msock, RPC_STRING, &file, RPC_END);
553 status = unlink (file);
554 send_status (status, errno);
555 g_free (file);
558 static void do_rename (void)
560 char *f1, *f2;
561 int status;
563 rpc_get (msock, RPC_STRING, &f1, RPC_STRING, &f2, RPC_END);
564 status = rename (f1, f2);
565 send_status (status, errno);
566 g_free (f1); g_free (f2);
569 static void do_symlink (void)
571 char *f1, *f2;
572 int status;
574 rpc_get (msock, RPC_STRING, &f1, RPC_STRING, &f2, RPC_END);
575 status = symlink (f1, f2);
576 send_status (status, errno);
577 g_free (f1); g_free (f2);
580 static void do_link (void)
582 char *f1, *f2;
583 int status;
585 rpc_get (msock, RPC_STRING, &f1, RPC_STRING, &f2, RPC_END);
586 status = link (f1, f2);
587 send_status (status, errno);
588 g_free (f1); g_free (f2);
592 /* }}} */
594 /* {{{ Misc commands */
596 static void do_gethome (void)
598 rpc_send (msock, RPC_STRING, (home_dir) ? home_dir : "/", RPC_END);
601 static void do_getupdir (void)
603 rpc_send (msock, RPC_STRING, (up_dir) ? up_dir : "/", RPC_END);
606 static void do_chmod (void)
608 char *file;
609 int mode, status;
611 rpc_get (msock, RPC_STRING, &file, RPC_INT, &mode, RPC_END);
612 status = chmod (file, mode);
613 send_status (status, errno);
614 g_free (file);
617 static void do_chown (void)
619 char *file;
620 int owner, group, status;
622 rpc_get (msock, RPC_STRING, &file,RPC_INT, &owner, RPC_INT,&group,RPC_END);
623 status = chown (file, owner, group);
624 send_status (status, errno);
625 g_free (file);
628 static void do_utime (void)
630 char *file;
631 int status;
632 long atime;
633 long mtime;
634 char *as;
635 char *ms;
636 struct utimbuf times;
638 rpc_get (msock, RPC_STRING, &file,
639 RPC_STRING, &as,
640 RPC_STRING, &ms,
641 RPC_END);
642 sscanf (as, "%lx", &atime);
643 sscanf (ms, "%lx", &mtime);
644 if (verbose) printf ("Got a = %s, m = %s, comp a = %ld, m = %ld\n",
645 as, ms, atime, mtime);
646 g_free (as);
647 g_free (ms);
648 times.actime = (time_t) atime;
649 times.modtime = (time_t) mtime;
650 status = utime (file, &times);
651 send_status (status, errno);
652 g_free (file);
655 static void do_quit (void)
657 quit_server = 1;
660 #ifdef HAVE_PAM
662 struct user_pass {
663 char *username;
664 char *password;
667 static int
668 mc_pam_conversation (int messages, const struct pam_message **msg,
669 struct pam_response **resp, void *appdata_ptr)
671 struct pam_response *r;
672 struct user_pass *up = appdata_ptr;
673 int status;
675 r = g_new (struct pam_response, messages);
676 if (!r)
677 return PAM_CONV_ERR;
678 *resp = r;
680 for (status = PAM_SUCCESS; messages--; msg++, r++){
681 switch ((*msg)->msg_style){
683 case PAM_PROMPT_ECHO_ON:
684 r->resp = g_strdup (up->username);
685 r->resp_retcode = PAM_SUCCESS;
686 break;
688 case PAM_PROMPT_ECHO_OFF:
689 r->resp = g_strdup (up->password);
690 r->resp_retcode = PAM_SUCCESS;
691 break;
693 case PAM_ERROR_MSG:
694 r->resp = NULL;
695 r->resp_retcode = PAM_SUCCESS;
696 break;
698 case PAM_TEXT_INFO:
699 r->resp = NULL;
700 r->resp_retcode = PAM_SUCCESS;
701 break;
704 return status;
707 static struct pam_conv conv = { &mc_pam_conversation, NULL };
710 /* Return 0 if authentication failed, 1 otherwise */
711 static int
712 mc_pam_auth (char *username, char *password)
714 pam_handle_t *pamh;
715 struct user_pass up;
716 int status;
718 up.username = username;
719 up.password = password;
720 conv.appdata_ptr = &up;
722 if ((status = pam_start("mcserv", username, &conv, &pamh)) != PAM_SUCCESS)
723 goto failed_pam;
724 if ((status = pam_authenticate (pamh, 0)) != PAM_SUCCESS)
725 goto failed_pam;
726 if ((status = pam_acct_mgmt (pamh, 0)) != PAM_SUCCESS)
727 goto failed_pam;
728 if ((status = pam_setcred (pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS)
729 goto failed_pam;
730 pam_end (pamh, status);
731 return 0;
733 failed_pam:
734 pam_end (pamh, status);
735 return 1;
738 #else /* Code for non-PAM authentication */
740 /* Keep reading until we find a \n */
741 static int next_line (int socket)
743 char c;
745 while (1){
746 if (read (socket, &c, 1) <= 0)
747 return 0;
748 if (c == '\n')
749 return 1;
753 static int ftp_answer (int sock, char *text)
755 char answer [4];
757 next_line (sock);
758 socket_read_block (sock, answer, 3);
759 answer [3] = 0;
760 if (strcmp (answer, text) == 0)
761 return 1;
762 return 0;
765 static int do_ftp_auth (char *username, char *password)
767 struct sockaddr_in local_address;
768 unsigned long inaddr;
769 int my_socket;
770 char answer [4];
772 memset ((char *) &local_address, 0, sizeof (local_address));
774 local_address.sin_family = AF_INET;
775 /* FIXME: extract the ftp port with the proper function */
776 local_address.sin_port = htons (21);
778 /* Convert localhost to usable format */
779 if ((inaddr = inet_addr ("127.0.0.1")) != -1)
780 memcpy ((char *) &local_address.sin_addr, (char *) &inaddr,
781 sizeof (inaddr));
783 if ((my_socket = socket (AF_INET, SOCK_STREAM, 0)) < 0){
784 if (!isDaemon) fprintf (stderr, "do_auth: can't create socket\n");
785 return 0;
787 if (connect (my_socket, (struct sockaddr *) &local_address,
788 sizeof (local_address)) < 0){
789 fprintf (stderr,
790 "do_auth: can't connect to ftp daemon for authentication\n");
791 close (my_socket);
792 return 0;
794 send_string (my_socket, "user ");
795 send_string (my_socket, username);
796 send_string (my_socket, "\r\n");
797 if (!ftp_answer (my_socket, "331")){
798 send_string (my_socket, "quit\r\n");
799 close (my_socket);
800 return 0;
802 next_line (my_socket); /* Eat all the line */
803 send_string (my_socket, "pass ");
804 send_string (my_socket, password);
805 send_string (my_socket, "\r\n");
806 socket_read_block (my_socket, answer, 3);
807 answer [3] = 0;
808 send_string (my_socket, "\r\n");
809 send_string (my_socket, "quit\r\n");
810 close (my_socket);
811 if (strcmp (answer, "230") == 0)
812 return 1;
813 return 0;
816 #ifdef NEED_CRYPT_PROTOTYPE
817 extern char *crypt (const char *, const char *);
818 #endif
820 static int do_classic_auth (char *username, char *password)
822 struct passwd *this;
823 int ret;
825 if ((this = getpwnam (username)) == 0)
826 return 0;
828 #ifdef HAVE_CRYPT
829 if (strcmp (crypt (password, this->pw_passwd), this->pw_passwd) == 0){
830 ret = 1;
831 } else
832 #endif
834 ret = 0;
836 endpwent ();
837 return ret;
839 #endif /* non-PAM authentication */
841 /* Try to authenticate the user based on:
842 - PAM if the system has it, else it checks:
843 - pwdauth if the system supports it.
844 - conventional auth (check salt on /etc/passwd, crypt, and compare
845 - try to contact the local ftp server and login (if -f flag used)
847 static int
848 do_auth (char *username, char *password)
850 int auth = 0;
851 struct passwd *this;
853 if (strcmp (username, "anonymous") == 0)
854 username = "ftp";
856 #ifdef HAVE_PAM
857 if (mc_pam_auth (username, password) == 0)
858 auth = 1;
859 #else /* if there is no pam */
860 #ifdef HAVE_PWDAUTH
861 if (pwdauth (username, password) == 0)
862 auth = 1;
863 else
864 #endif
865 if (do_classic_auth (username, password))
866 auth = 1;
867 else if (ftp)
868 auth = do_ftp_auth (username, password);
869 #endif /* not pam */
871 if (!auth)
872 return 0;
874 this = getpwnam (username);
875 if (this == 0)
876 return 0;
878 if (chdir (this->pw_dir) == -1)
879 return 0;
881 if (this->pw_dir [strlen (this->pw_dir) - 1] == '/')
882 home_dir = g_strdup (this->pw_dir);
883 else {
884 home_dir = g_malloc (strlen (this->pw_dir) + 2);
885 if (home_dir) {
886 strcpy (home_dir, this->pw_dir);
887 strcat (home_dir, "/");
888 } else
889 home_dir = "/";
893 if (setgid (this->pw_gid) == -1)
894 return 0;
896 #ifdef HAVE_INITGROUPS
897 #ifdef NGROUPS_MAX
898 if (NGROUPS_MAX > 1 && initgroups (this->pw_name, this->pw_gid))
899 return 0;
900 #endif
901 #endif
903 #if defined (HAVE_SETUID)
904 if (setuid (this->pw_uid))
905 return 0;
906 #elif defined (HAVE_SETREUID)
907 if (setreuid (this->pw_uid, this->pw_uid))
908 return 0;
909 #endif
911 /* If the setuid call failed, then deny access */
912 /* This should fix the problem on those machines with strange setups */
913 if (getuid () != this->pw_uid)
914 return 0;
916 if (strcmp (username, "ftp") == 0)
917 chroot (this->pw_dir);
919 endpwent ();
920 return auth;
923 #if 0
924 static int do_rauth (int socket)
926 struct sockaddr_in from;
927 struct hostent *hp;
929 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0)
930 return 0;
931 from.sin_port = ntohs ((unsigned short) from.sin_port);
933 /* Strange, this should not happend */
934 if (from.sin_family != AF_INET)
935 return 0;
937 hp = gethostbyaddr((char *)&fromp.sin_addr, sizeof (struct in_addr),
938 fromp.sin_family);
941 #endif
943 static int do_rauth (int msock)
945 return 0;
948 static void login_reply (int logged_in)
950 rpc_send (msock, RPC_INT,
951 logged_in ? MC_LOGINOK : MC_INVALID_PASS,
952 RPC_END);
955 /* FIXME: Implement the anonymous login */
956 static void do_login (void)
958 char *username;
959 char *password;
960 int result;
962 rpc_get (msock, RPC_LIMITED_STRING, &up_dir, RPC_LIMITED_STRING, &username, RPC_END);
963 if (verbose) printf ("username: %s\n", username);
965 if (r_auth){
966 logged_in = do_rauth (msock);
967 if (logged_in){
968 login_reply (logged_in);
969 return;
972 rpc_send (msock, RPC_INT, MC_NEED_PASSWORD, RPC_END);
973 rpc_get (msock, RPC_INT, &result, RPC_END);
974 if (result == MC_QUIT)
975 DO_QUIT_VOID ();
976 if (result != MC_PASS){
977 if (verbose) printf ("do_login: Unknown response: %d\n", result);
978 DO_QUIT_VOID ();
980 rpc_get (msock, RPC_LIMITED_STRING, &password, RPC_END);
981 logged_in = do_auth (username, password);
982 endpwent ();
983 login_reply (logged_in);
986 /* }}} */
988 /* {{{ Server and dispatching functions */
990 /* This structure must be kept in synch with mcfs.h enums */
992 static struct _command {
993 char *command;
994 void (*callback)(void);
995 } commands [] = {
996 { "open", do_open },
997 { "close", do_close },
998 { "read", do_read },
999 { "write", do_write },
1000 { "opendir", do_opendir },
1001 { "readdir", do_readdir },
1002 { "closedir", do_closedir },
1003 { "stat ", do_stat },
1004 { "lstat ", do_lstat },
1005 { "fstat", do_fstat },
1006 { "chmod", do_chmod },
1007 { "chown", do_chown },
1008 { "readlink ", do_readlink },
1009 { "unlink", do_unlink },
1010 { "rename", do_rename },
1011 { "chdir ", do_chdir },
1012 { "lseek", do_lseek },
1013 { "rmdir", do_rmdir },
1014 { "symlink", do_symlink },
1015 { "mknod", do_mknod },
1016 { "mkdir", do_mkdir },
1017 { "link", do_link },
1018 { "gethome", do_gethome },
1019 { "getupdir", do_getupdir },
1020 { "login", do_login },
1021 { "quit", do_quit },
1022 { "utime", do_utime },
1025 static int ncommands = sizeof(commands)/sizeof(struct _command);
1027 static void exec_command (int command)
1029 if (command < 0 ||
1030 command >= ncommands ||
1031 commands [command].command == 0){
1032 fprintf (stderr, "Got unknown command: %d\n", command);
1033 DO_QUIT_VOID ();
1035 if (verbose) printf ("Command: %s\n", commands [command].command);
1036 (*commands [command].callback)();
1039 static void check_version (void)
1041 int version;
1043 rpc_get (msock, RPC_INT, &version, RPC_END);
1044 if (version >= 1 &&
1045 version <= RPC_PROGVER)
1046 rpc_send (msock, RPC_INT, MC_VERSION_OK, RPC_END);
1047 else
1048 rpc_send (msock, RPC_INT, MC_VERSION_MISMATCH, RPC_END);
1050 clnt_version = version;
1053 /* This routine is called by rpc_get/rpc_send when the connection is closed */
1054 void tcp_invalidate_socket (int sock)
1056 if (verbose) printf ("Connection closed\n");
1057 DO_QUIT_VOID();
1060 static void server (int sock)
1062 int command;
1064 msock = sock;
1065 quit_server = 0;
1067 check_version ();
1068 do {
1069 if (rpc_get (sock, RPC_INT, &command, RPC_END) &&
1070 (logged_in || command == MC_LOGIN))
1071 exec_command (command);
1072 } while (!quit_server);
1075 /* }}} */
1077 /* {{{ Net support code */
1079 static char *get_client (int portnum)
1081 int sock, clilen, newsocket;
1082 struct sockaddr_in client_address, server_address;
1083 struct hostent *hp;
1084 char hostname [255];
1085 int yes = 1;
1086 #ifdef __EMX__
1087 char *me;
1088 #endif
1090 if ((sock = socket (AF_INET, SOCK_STREAM, 0)) < 0)
1091 return "Cannot create socket";
1093 /* Use this to debug: */
1094 if (setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, (char *) &yes, sizeof (yes)) < 0)
1095 return "setsockopt failed";
1097 gethostname (hostname, 255);
1098 if (verbose) printf ("hostname=%s\n", hostname);
1099 hp = gethostbyname (hostname);
1100 #ifdef __EMX__
1101 if (hp == 0 && (me = getenv("HOSTNAME")) && (0 == strcmp(hostname, me)))
1102 hp = gethostbyname ("localhost");
1103 #endif
1104 if (hp == 0)
1105 return "hp = 0!";
1107 memset ((char *) &server_address, 0, sizeof (server_address));
1108 server_address.sin_family = hp->h_addrtype;
1109 server_address.sin_addr.s_addr = htonl (INADDR_ANY);
1110 server_address.sin_port = htons (portnum);
1112 if (bind (sock, (struct sockaddr *) &server_address,
1113 sizeof (server_address)) < 0)
1114 return "Cannot bind";
1116 listen (sock, 5);
1118 for (;;){
1119 int child;
1121 clilen = sizeof (client_address);
1122 newsocket = accept (sock, (struct sockaddr *) &client_address,
1123 &clilen);
1125 if (isDaemon && (child = fork())) {
1126 int status;
1128 close (newsocket);
1129 waitpid (child, &status, 0);
1130 continue;
1133 if (isDaemon && fork()) exit (0);
1135 server (newsocket);
1136 close (newsocket);
1137 return 0;
1141 #ifdef HAVE_PMAP_SET
1142 static void signal_int_handler (int sig)
1144 pmap_unset (RPC_PROGNUM, RPC_PROGVER);
1146 #endif
1148 #ifndef IPPORT_RESERVED
1149 #define IPPORT_RESERVED 1024;
1150 #endif
1152 static int get_port_number (void)
1154 int port = 0;
1156 #ifdef HAVE_RRESVPORT
1157 int start_port = IPPORT_RESERVED;
1159 port = rresvport (&start_port);
1160 if (port == -1){
1161 if (geteuid () == 0){
1162 fprintf (stderr, "Could not bind the server on a reserved port\n");
1163 DO_QUIT_NONVOID (-1);
1165 port = 0;
1167 #endif
1168 if (port)
1169 return port;
1171 port = mcserver_port;
1173 return port;
1176 static void register_port (int portnum, int abort_if_fail)
1178 #ifdef HAVE_PMAP_SET
1179 /* Register our service with the portmapper */
1180 /* protocol: pmap_set (prognum, versnum, protocol, portp) */
1182 if (pmap_set (RPC_PROGNUM, RPC_PROGVER, IPPROTO_TCP, portnum))
1183 signal (SIGINT, signal_int_handler);
1184 else {
1185 fprintf (stderr, "Could not register service with portmapper\n");
1186 if (abort_if_fail)
1187 exit (1);
1189 #else
1190 if (abort_if_fail){
1191 fprintf (stderr,
1192 "This system lacks port registration, try using the -p\n"
1193 "flag to force installation at a given port");
1195 #endif
1198 /* }}} */
1200 int main (int argc, char *argv [])
1202 char *result;
1203 extern char *optarg;
1204 int c;
1206 while ((c = getopt (argc, argv, "fdiqp:v")) != -1){
1207 switch (c){
1208 case 'd':
1209 isDaemon = 1;
1210 verbose = 0;
1211 break;
1213 case 'v':
1214 verbose = 1;
1215 break;
1217 case 'f':
1218 ftp = 1;
1219 break;
1221 case 'q':
1222 verbose = 0;
1223 break;
1225 case 'p':
1226 portnum = atoi (optarg);
1227 break;
1229 case 'i':
1230 inetd_started = 1;
1231 break;
1233 case 'r':
1234 r_auth = 1;
1235 break;
1237 default:
1238 fprintf (stderr, "Usage is: mcserv [options] [-p portnum]\n\n"
1239 "options are:\n"
1240 "-d become a daemon (sets -q)\n"
1241 "-q quiet mode\n"
1242 /* "-r use rhost based authentication\n" */
1243 #ifndef HAVE_PAM
1244 "-f force ftp authentication\n"
1245 #endif
1246 "-v verbose mode\n"
1247 "-p to specify a port number to listen\n");
1248 exit (0);
1253 if (isDaemon && fork()) exit (0);
1255 if (portnum == 0)
1256 portnum = get_port_number ();
1258 if (portnum != -1) {
1259 register_port (portnum, 0);
1260 if (verbose)
1261 printf ("Using port %d\n", portnum);
1262 if ((result = get_client (portnum)))
1263 perror (result);
1264 #ifdef HAVE_PMAP_SET
1265 if (!isDaemon)
1266 pmap_unset (RPC_PROGNUM, RPC_PROGVER);
1267 #endif
1269 exit (return_code);
1272 /* FIXME: This function should not be used in mcserv */
1273 void vfs_die( char *m )
1275 fputs (m, stderr);
1276 exit (1);