1 /* Server for the Midnight Commander Virtual File System.
3 Copyright (C) 1995, 1996, 1997 The Free Software Foundation
6 Miguel de Icaza, 1995, 1997,
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.
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
28 We should use syslog to register login/logout.
32 /* {{{ Includes and global variables */
45 # include <sys/param.h>
47 # define NGROUPS_MAX NGROUPS
53 #include <sys/types.h>
59 /* Network include files */
60 #include <sys/socket.h>
61 #include <netinet/in.h>
63 #ifdef HAVE_ARPA_INET_H
64 #include <arpa/inet.h>
68 # include <rpc/pmap_prot.h>
69 # ifdef HAVE_RPC_PMAP_CLNT_H
70 # include <rpc/pmap_clnt.h>
74 /* Authentication include files */
77 # include <security/pam_misc.h>
78 # ifndef PAM_ESTABLISH_CRED
79 # define PAM_ESTABLISH_CRED PAM_CRED_ESTABLISH
85 extern char *crypt (const char *, const char *);
86 #endif /* !HAVE_CRYPT_H */
87 #endif /* !HAVE_PAM */
95 /* The socket from which we accept commands */
98 /* Requested version number from client */
99 static int clnt_version
;
101 /* If non zero, we accept further commands */
105 char *home_dir
= NULL
;
109 /* Were we started from inetd? */
110 int inetd_started
= 0;
112 /* Are we running as a daemon? */
121 /* port number in which we listen to connections,
122 * if zero, we try to contact the portmapper to get a port, and
123 * if it's not possible, then we use a hardcoded value
127 /* if the server will use rcmd based authentication (hosts.equiv .rhosts) */
130 #define OPENDIR_HANDLES 8
132 #define DO_QUIT_VOID() \
139 /* Only used by get_port_number */
140 #define DO_QUIT_NONVOID(a) \
149 static int quit_server
;
150 static int return_code
;
154 /* {{{ Misc routines */
156 static void send_status (int status
, int errno_number
)
158 rpc_send (msock
, RPC_INT
, status
, RPC_INT
, errno_number
, RPC_END
);
164 /* {{{ File with handle operations */
166 static void do_open (void)
168 int handle
, flags
, mode
;
171 rpc_get (msock
, RPC_STRING
, &arg
, RPC_INT
, &flags
, RPC_INT
, &mode
,RPC_END
);
173 handle
= open (arg
, flags
, mode
);
174 send_status (handle
, errno
);
178 static void do_read (void)
180 int handle
, count
, n
;
183 rpc_get (msock
, RPC_INT
, &handle
, RPC_INT
, &count
, RPC_END
);
184 data
= g_malloc (count
);
186 send_status (-1, ENOMEM
);
189 if (verbose
) printf ("count=%d\n", count
);
190 n
= read (handle
, data
, count
);
191 if (verbose
) printf ("result=%d\n", n
);
193 send_status (-1, errno
);
197 rpc_send (msock
, RPC_BLOCK
, n
, data
, RPC_END
);
202 static void do_write (void)
204 int handle
, count
, status
, written
= 0;
207 rpc_get (msock
, RPC_INT
, &handle
, RPC_INT
, &count
, RPC_END
);
210 int nbytes
= count
> 8192 ? 8192 : count
;
212 rpc_get (msock
, RPC_BLOCK
, nbytes
, buf
, RPC_END
);
213 status
= write (handle
, buf
, nbytes
);
215 send_status (status
, errno
);
218 /* FIXED: amount written must be returned to caller */
220 if (status
< nbytes
) {
221 send_status (written
, errno
);
226 send_status (written
, errno
);
229 static void do_lseek (void)
231 int handle
, offset
, whence
, status
;
236 RPC_INT
, &whence
, RPC_END
);
237 status
= lseek (handle
, offset
, whence
);
238 send_status (status
, errno
);
241 static void do_close (void)
245 rpc_get (msock
, RPC_INT
, &handle
, RPC_END
);
246 status
= close (handle
);
247 send_status (status
, errno
);
252 /* {{{ Stat family routines */
254 static void send_time (int sock
, time_t time
)
256 if (clnt_version
== 1) {
261 ct
[3] = ct
[10] = ct
[13] = ct
[16] = ct
[19] = 0;
263 /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
268 month
= (ct
[6] == 'n') ? 5 : 6;
269 } else if (ct
[4] == 'F'){
271 } else if (ct
[4] == 'M'){
272 month
= (ct
[6] == 'r') ? 2 : 5;
273 } else if (ct
[4] == 'A'){
274 month
= (ct
[5] == 'p') ? 3 : 7;
275 } else if (ct
[4] == 'S'){
277 } else if (ct
[4] == 'O'){
279 } else if (ct
[4] == 'N'){
284 RPC_INT
, atoi (&ct
[17]), /* sec */
285 RPC_INT
, atoi (&ct
[14]), /* min */
286 RPC_INT
, atoi (&ct
[11]), /* hour */
287 RPC_INT
, atoi (&ct
[8]), /* mday */
288 RPC_INT
, atoi (&ct
[20]), /* year */
289 RPC_INT
, month
, /* month */
292 long ltime
= (long) time
;
295 g_snprintf (buf
, sizeof(buf
), "%lx", ltime
);
302 static void send_stat_info (struct stat
*st
)
306 #ifdef HAVE_ST_BLOCKS
313 mylong
= st
->st_rdev
;
317 rpc_send (msock
, RPC_INT
, (long) mylong
,
318 RPC_INT
, (long) st
->st_ino
,
319 RPC_INT
, (long) st
->st_mode
,
320 RPC_INT
, (long) st
->st_nlink
,
321 RPC_INT
, (long) st
->st_uid
,
322 RPC_INT
, (long) st
->st_gid
,
323 RPC_INT
, (long) st
->st_size
,
324 RPC_INT
, (long) blocks
, RPC_END
);
325 send_time (msock
, st
->st_atime
);
326 send_time (msock
, st
->st_mtime
);
327 send_time (msock
, st
->st_ctime
);
330 static void do_lstat (void)
336 rpc_get (msock
, RPC_STRING
, &file
, RPC_END
);
337 n
= lstat (file
, &st
);
338 send_status (n
, errno
);
340 send_stat_info (&st
);
344 static void do_fstat (void)
350 rpc_get (msock
, RPC_INT
, &handle
, RPC_END
);
351 n
= fstat (handle
, &st
);
352 send_status (n
, errno
);
356 send_stat_info (&st
);
359 static void do_stat (void)
365 rpc_get (msock
, RPC_STRING
, &file
, RPC_END
);
367 n
= stat (file
, &st
);
368 send_status (n
, errno
);
370 send_stat_info (&st
);
376 /* {{{ Directory lookup operations */
380 DIR *dirs
[OPENDIR_HANDLES
];
381 char *names
[OPENDIR_HANDLES
];
384 static void close_handle (int handle
)
386 if (mcfs_DIR
.used
> 0) mcfs_DIR
.used
--;
387 if (mcfs_DIR
.dirs
[handle
])
388 closedir (mcfs_DIR
.dirs
[handle
]);
389 if (mcfs_DIR
.names
[handle
])
390 g_free (mcfs_DIR
.names
[handle
]);
391 mcfs_DIR
.dirs
[handle
] = 0;
392 mcfs_DIR
.names
[handle
] = 0;
395 static void do_opendir (void)
401 rpc_get (msock
, RPC_STRING
, &arg
, RPC_END
);
403 if (mcfs_DIR
.used
== OPENDIR_HANDLES
){
404 send_status (-1, ENFILE
); /* Error */
410 for (i
= 0; i
< OPENDIR_HANDLES
; i
++){
411 if (mcfs_DIR
.dirs
[i
] == 0){
418 send_status (-1, EMFILE
);
421 fprintf (stderr
, "OOPS! you have found a bug in mc - do_opendir()!\n");
425 if (verbose
) printf ("handle=%d\n", handle
);
428 mcfs_DIR
.dirs
[handle
] = p
;
429 mcfs_DIR
.names
[handle
] = arg
;
432 /* Because 0 is an error value */
433 rpc_send (msock
, RPC_INT
, handle
+1, RPC_INT
, 0, RPC_END
);
436 send_status (-1, errno
);
441 /* Sends the complete directory listing, as well as the stat information */
442 static void do_readdir (void)
444 struct dirent
*dirent
;
449 rpc_get (msock
, RPC_INT
, &handle
, RPC_END
);
452 rpc_send (msock
, RPC_INT
, 0, RPC_END
);
456 /* We incremented it in opendir */
459 while ((dirent
= readdir (mcfs_DIR
.dirs
[handle
]))){
460 int length
= NLENGTH (dirent
);
462 rpc_send (msock
, RPC_INT
, length
, RPC_END
);
463 rpc_send (msock
, RPC_BLOCK
, length
, dirent
->d_name
, RPC_END
);
464 fname
= g_strconcat (mcfs_DIR
.names
[handle
],
465 PATH_SEP_STR
, dirent
->d_name
, NULL
);
466 n
= lstat (fname
, &st
);
467 send_status (n
, errno
);
470 send_stat_info (&st
);
472 rpc_send (msock
, RPC_INT
, 0, RPC_END
);
475 static void do_closedir (void)
479 rpc_get (msock
, RPC_INT
, &handle
, RPC_END
);
480 close_handle (handle
-1);
485 /* {{{ Operations with one and two file name argument */
487 static void do_chdir (void)
492 rpc_get (msock
, RPC_STRING
, &file
, RPC_END
);
494 status
= chdir (file
);
495 send_status (status
, errno
);
499 static void do_rmdir (void)
504 rpc_get (msock
, RPC_STRING
, &file
, RPC_END
);
506 status
= rmdir (file
);
507 send_status (status
, errno
);
511 static void do_mkdir (void)
516 rpc_get (msock
, RPC_STRING
, &file
, RPC_INT
, &mode
, RPC_END
);
518 status
= mkdir (file
, mode
);
519 send_status (status
, errno
);
523 static void do_mknod (void)
526 int mode
, dev
, status
;
528 rpc_get (msock
, RPC_STRING
, &file
, RPC_INT
, &mode
, RPC_INT
, &dev
, RPC_END
);
530 status
= mknod (file
, mode
, dev
);
531 send_status (status
, errno
);
535 static void do_readlink (void)
541 rpc_get (msock
, RPC_STRING
, &file
, RPC_END
);
542 n
= readlink (file
, buffer
, 2048);
543 send_status (n
, errno
);
546 rpc_send (msock
, RPC_STRING
, buffer
, RPC_END
);
551 static void do_unlink (void)
556 rpc_get (msock
, RPC_STRING
, &file
, RPC_END
);
557 status
= unlink (file
);
558 send_status (status
, errno
);
562 static void do_rename (void)
567 rpc_get (msock
, RPC_STRING
, &f1
, RPC_STRING
, &f2
, RPC_END
);
568 status
= rename (f1
, f2
);
569 send_status (status
, errno
);
570 g_free (f1
); g_free (f2
);
573 static void do_symlink (void)
578 rpc_get (msock
, RPC_STRING
, &f1
, RPC_STRING
, &f2
, RPC_END
);
579 status
= symlink (f1
, f2
);
580 send_status (status
, errno
);
581 g_free (f1
); g_free (f2
);
584 static void do_link (void)
589 rpc_get (msock
, RPC_STRING
, &f1
, RPC_STRING
, &f2
, RPC_END
);
590 status
= link (f1
, f2
);
591 send_status (status
, errno
);
592 g_free (f1
); g_free (f2
);
598 /* {{{ Misc commands */
600 static void do_gethome (void)
602 rpc_send (msock
, RPC_STRING
, (home_dir
) ? home_dir
: "/", RPC_END
);
605 static void do_getupdir (void)
607 rpc_send (msock
, RPC_STRING
, (up_dir
) ? up_dir
: "/", RPC_END
);
610 static void do_chmod (void)
615 rpc_get (msock
, RPC_STRING
, &file
, RPC_INT
, &mode
, RPC_END
);
616 status
= chmod (file
, mode
);
617 send_status (status
, errno
);
621 static void do_chown (void)
624 int owner
, group
, status
;
626 rpc_get (msock
, RPC_STRING
, &file
,RPC_INT
, &owner
, RPC_INT
,&group
,RPC_END
);
627 status
= chown (file
, owner
, group
);
628 send_status (status
, errno
);
632 static void do_utime (void)
640 struct utimbuf times
;
642 rpc_get (msock
, RPC_STRING
, &file
,
646 sscanf (as
, "%lx", &atime
);
647 sscanf (ms
, "%lx", &mtime
);
648 if (verbose
) printf ("Got a = %s, m = %s, comp a = %ld, m = %ld\n",
649 as
, ms
, atime
, mtime
);
652 times
.actime
= (time_t) atime
;
653 times
.modtime
= (time_t) mtime
;
654 status
= utime (file
, ×
);
655 send_status (status
, errno
);
659 static void do_quit (void)
672 mc_pam_conversation (int messages
, const struct pam_message
**msg
,
673 struct pam_response
**resp
, void *appdata_ptr
)
675 struct pam_response
*r
;
676 struct user_pass
*up
= appdata_ptr
;
679 r
= g_new (struct pam_response
, messages
);
684 for (status
= PAM_SUCCESS
; messages
--; msg
++, r
++){
685 switch ((*msg
)->msg_style
){
687 case PAM_PROMPT_ECHO_ON
:
688 r
->resp
= g_strdup (up
->username
);
689 r
->resp_retcode
= PAM_SUCCESS
;
692 case PAM_PROMPT_ECHO_OFF
:
693 r
->resp
= g_strdup (up
->password
);
694 r
->resp_retcode
= PAM_SUCCESS
;
699 r
->resp_retcode
= PAM_SUCCESS
;
704 r
->resp_retcode
= PAM_SUCCESS
;
711 static struct pam_conv conv
= { &mc_pam_conversation
, NULL
};
714 /* Return 0 if authentication failed, 1 otherwise */
716 mc_pam_auth (char *username
, char *password
)
722 up
.username
= username
;
723 up
.password
= password
;
724 conv
.appdata_ptr
= &up
;
726 if ((status
= pam_start("mcserv", username
, &conv
, &pamh
)) != PAM_SUCCESS
)
728 if ((status
= pam_authenticate (pamh
, 0)) != PAM_SUCCESS
)
730 if ((status
= pam_acct_mgmt (pamh
, 0)) != PAM_SUCCESS
)
732 if ((status
= pam_setcred (pamh
, PAM_ESTABLISH_CRED
)) != PAM_SUCCESS
)
734 pam_end (pamh
, status
);
738 pam_end (pamh
, status
);
742 #else /* Code for non-PAM authentication */
744 /* Keep reading until we find a \n */
745 static int next_line (int socket
)
750 if (read (socket
, &c
, 1) <= 0)
757 static int ftp_answer (int sock
, char *text
)
762 socket_read_block (sock
, answer
, 3);
764 if (strcmp (answer
, text
) == 0)
769 static int do_ftp_auth (char *username
, char *password
)
771 struct sockaddr_in local_address
;
772 unsigned long inaddr
;
776 memset ((char *) &local_address
, 0, sizeof (local_address
));
778 local_address
.sin_family
= AF_INET
;
779 /* FIXME: extract the ftp port with the proper function */
780 local_address
.sin_port
= htons (21);
782 /* Convert localhost to usable format */
783 if ((inaddr
= inet_addr ("127.0.0.1")) != -1)
784 memcpy ((char *) &local_address
.sin_addr
, (char *) &inaddr
,
787 if ((my_socket
= socket (AF_INET
, SOCK_STREAM
, 0)) < 0){
788 if (!isDaemon
) fprintf (stderr
, "do_auth: can't create socket\n");
791 if (connect (my_socket
, (struct sockaddr
*) &local_address
,
792 sizeof (local_address
)) < 0){
794 "do_auth: can't connect to ftp daemon for authentication\n");
798 send_string (my_socket
, "user ");
799 send_string (my_socket
, username
);
800 send_string (my_socket
, "\r\n");
801 if (!ftp_answer (my_socket
, "331")){
802 send_string (my_socket
, "quit\r\n");
806 next_line (my_socket
); /* Eat all the line */
807 send_string (my_socket
, "pass ");
808 send_string (my_socket
, password
);
809 send_string (my_socket
, "\r\n");
810 socket_read_block (my_socket
, answer
, 3);
812 send_string (my_socket
, "\r\n");
813 send_string (my_socket
, "quit\r\n");
815 if (strcmp (answer
, "230") == 0)
820 static int do_classic_auth (char *username
, char *password
)
825 if ((this = getpwnam (username
)) == 0)
829 if (strcmp (crypt (password
, this->pw_passwd
), this->pw_passwd
) == 0){
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)
848 do_auth (char *username
, char *password
)
853 if (strcmp (username
, "anonymous") == 0)
857 if (mc_pam_auth (username
, password
) == 0)
859 #else /* if there is no pam */
861 if (pwdauth (username
, password
) == 0)
865 if (do_classic_auth (username
, password
))
868 auth
= do_ftp_auth (username
, password
);
874 this = getpwnam (username
);
878 if (chdir (this->pw_dir
) == -1)
881 if (this->pw_dir
[strlen (this->pw_dir
) - 1] == '/')
882 home_dir
= g_strdup (this->pw_dir
);
884 home_dir
= g_malloc (strlen (this->pw_dir
) + 2);
886 strcpy (home_dir
, this->pw_dir
);
887 strcat (home_dir
, "/");
893 if (setgid (this->pw_gid
) == -1)
896 #ifdef HAVE_INITGROUPS
898 if (NGROUPS_MAX
> 1 && initgroups (this->pw_name
, this->pw_gid
))
903 #if defined (HAVE_SETUID)
904 if (setuid (this->pw_uid
))
906 #elif defined (HAVE_SETREUID)
907 if (setreuid (this->pw_uid
, this->pw_uid
))
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
)
916 if (strcmp (username
, "ftp") == 0)
917 chroot (this->pw_dir
);
924 static int do_rauth (int socket
)
926 struct sockaddr_in from
;
929 if (getpeername(0, (struct sockaddr
*)&from
, &fromlen
) < 0)
931 from
.sin_port
= ntohs ((unsigned short) from
.sin_port
);
933 /* Strange, this should not happend */
934 if (from
.sin_family
!= AF_INET
)
937 hp
= gethostbyaddr((char *)&fromp
.sin_addr
, sizeof (struct in_addr
),
943 static int do_rauth (int msock
)
948 static void login_reply (int logged_in
)
950 rpc_send (msock
, RPC_INT
,
951 logged_in
? MC_LOGINOK
: MC_INVALID_PASS
,
955 /* FIXME: Implement the anonymous login */
956 static void do_login (void)
962 rpc_get (msock
, RPC_LIMITED_STRING
, &up_dir
, RPC_LIMITED_STRING
, &username
, RPC_END
);
963 if (verbose
) printf ("username: %s\n", username
);
966 logged_in
= do_rauth (msock
);
968 login_reply (logged_in
);
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
)
976 if (result
!= MC_PASS
){
977 if (verbose
) printf ("do_login: Unknown response: %d\n", result
);
980 rpc_get (msock
, RPC_LIMITED_STRING
, &password
, RPC_END
);
981 logged_in
= do_auth (username
, password
);
983 login_reply (logged_in
);
988 /* {{{ Server and dispatching functions */
990 /* This structure must be kept in synch with mcfs.h enums */
992 static struct _command
{
994 void (*callback
)(void);
997 { "close", do_close
},
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
)
1030 command
>= ncommands
||
1031 commands
[command
].command
== 0){
1032 fprintf (stderr
, "Got unknown command: %d\n", command
);
1035 if (verbose
) printf ("Command: %s\n", commands
[command
].command
);
1036 (*commands
[command
].callback
)();
1039 static void check_version (void)
1043 rpc_get (msock
, RPC_INT
, &version
, RPC_END
);
1045 version
<= RPC_PROGVER
)
1046 rpc_send (msock
, RPC_INT
, MC_VERSION_OK
, RPC_END
);
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");
1060 static void server (int sock
)
1069 if (rpc_get (sock
, RPC_INT
, &command
, RPC_END
) &&
1070 (logged_in
|| command
== MC_LOGIN
))
1071 exec_command (command
);
1072 } while (!quit_server
);
1077 /* {{{ Net support code */
1079 static char *get_client (int portnum
)
1081 int sock
, clilen
, newsocket
;
1082 struct sockaddr_in client_address
, server_address
;
1084 char hostname
[255];
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
);
1101 if (hp
== 0 && (me
= getenv("HOSTNAME")) && (0 == strcmp(hostname
, me
)))
1102 hp
= gethostbyname ("localhost");
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";
1121 clilen
= sizeof (client_address
);
1122 newsocket
= accept (sock
, (struct sockaddr
*) &client_address
,
1125 if (isDaemon
&& (child
= fork())) {
1129 waitpid (child
, &status
, 0);
1133 if (isDaemon
&& fork()) exit (0);
1141 #ifdef HAVE_PMAP_SET
1142 static void signal_int_handler (int sig
)
1144 pmap_unset (RPC_PROGNUM
, RPC_PROGVER
);
1148 #ifndef IPPORT_RESERVED
1149 #define IPPORT_RESERVED 1024;
1152 static int get_port_number (void)
1156 #ifdef HAVE_RRESVPORT
1157 int start_port
= IPPORT_RESERVED
;
1159 port
= rresvport (&start_port
);
1161 if (geteuid () == 0){
1162 fprintf (stderr
, "Could not bind the server on a reserved port\n");
1163 DO_QUIT_NONVOID (-1);
1171 port
= mcserver_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
);
1185 fprintf (stderr
, "Could not register service with portmapper\n");
1192 "This system lacks port registration, try using the -p\n"
1193 "flag to force installation at a given port");
1200 int main (int argc
, char *argv
[])
1203 extern char *optarg
;
1206 while ((c
= getopt (argc
, argv
, "fdiqp:v")) != -1){
1226 portnum
= atoi (optarg
);
1238 fprintf (stderr
, "Usage is: mcserv [options] [-p portnum]\n\n"
1240 "-d become a daemon (sets -q)\n"
1242 /* "-r use rhost based authentication\n" */
1244 "-f force ftp authentication\n"
1247 "-p to specify a port number to listen\n");
1253 if (isDaemon
&& fork()) exit (0);
1256 portnum
= get_port_number ();
1258 if (portnum
!= -1) {
1259 register_port (portnum
, 0);
1261 printf ("Using port %d\n", portnum
);
1262 if ((result
= get_client (portnum
)))
1264 #ifdef HAVE_PMAP_SET
1266 pmap_unset (RPC_PROGNUM
, RPC_PROGVER
);
1272 /* FIXME: This function should not be used in mcserv */
1273 void vfs_die( char *m
)