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 */
46 # include <sys/param.h>
48 # define NGROUPS_MAX NGROUPS
57 #include <sys/types.h>
63 /* Network include files */
64 #include <sys/socket.h>
65 #include <netinet/in.h>
67 #ifdef HAVE_ARPA_INET_H
68 #include <arpa/inet.h>
72 # include <rpc/pmap_prot.h>
73 # ifdef HAVE_RPC_PMAP_CLNT_H
74 # include <rpc/pmap_clnt.h>
78 /* Authentication include files */
81 # include <security/pam_misc.h>
82 # ifndef PAM_ESTABLISH_CRED
83 # define PAM_ESTABLISH_CRED PAM_CRED_ESTABLISH
93 /* The socket from which we accept commands */
96 /* Requested version number from client */
97 static int clnt_version
;
99 /* If non zero, we accept further commands */
103 char *home_dir
= NULL
;
107 /* Were we started from inetd? */
108 int inetd_started
= 0;
110 /* Are we running as a daemon? */
119 /* port number in which we listen to connections,
120 * if zero, we try to contact the portmapper to get a port, and
121 * if it's not possible, then we use a hardcoded value
125 /* if the server will use rcmd based authentication (hosts.equiv .rhosts) */
128 #define OPENDIR_HANDLES 8
130 #define DO_QUIT_VOID() \
137 /* Only used by get_port_number */
138 #define DO_QUIT_NONVOID(a) \
147 static int quit_server
;
148 static int return_code
;
152 /* {{{ Misc routines */
154 static void send_status (int status
, int errno_number
)
156 rpc_send (msock
, RPC_INT
, status
, RPC_INT
, errno_number
, RPC_END
);
162 /* {{{ File with handle operations */
164 static void do_open (void)
166 int handle
, flags
, mode
;
169 rpc_get (msock
, RPC_STRING
, &arg
, RPC_INT
, &flags
, RPC_INT
, &mode
,RPC_END
);
171 handle
= open (arg
, flags
, mode
);
172 send_status (handle
, errno
);
176 static void do_read (void)
178 int handle
, count
, n
;
181 rpc_get (msock
, RPC_INT
, &handle
, RPC_INT
, &count
, RPC_END
);
182 data
= g_malloc (count
);
184 send_status (-1, ENOMEM
);
187 if (verbose
) printf ("count=%d\n", count
);
188 n
= read (handle
, data
, count
);
189 if (verbose
) printf ("result=%d\n", n
);
191 send_status (-1, errno
);
195 rpc_send (msock
, RPC_BLOCK
, n
, data
, RPC_END
);
200 static void do_write (void)
202 int handle
, count
, status
, written
= 0;
205 rpc_get (msock
, RPC_INT
, &handle
, RPC_INT
, &count
, RPC_END
);
208 int nbytes
= count
> 8192 ? 8192 : count
;
210 rpc_get (msock
, RPC_BLOCK
, nbytes
, buf
, RPC_END
);
211 status
= write (handle
, buf
, nbytes
);
213 send_status (status
, errno
);
216 /* FIXED: amount written must be returned to caller */
218 if (status
< nbytes
) {
219 send_status (written
, errno
);
224 send_status (written
, errno
);
227 static void do_lseek (void)
229 int handle
, offset
, whence
, status
;
234 RPC_INT
, &whence
, RPC_END
);
235 status
= lseek (handle
, offset
, whence
);
236 send_status (status
, errno
);
239 static void do_close (void)
243 rpc_get (msock
, RPC_INT
, &handle
, RPC_END
);
244 status
= close (handle
);
245 send_status (status
, errno
);
250 /* {{{ Stat family routines */
252 static void send_time (int sock
, time_t time
)
254 if (clnt_version
== 1) {
259 ct
[3] = ct
[10] = ct
[13] = ct
[16] = ct
[19] = 0;
261 /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
266 month
= (ct
[6] == 'n') ? 5 : 6;
267 } else if (ct
[4] == 'F'){
269 } else if (ct
[4] == 'M'){
270 month
= (ct
[6] == 'r') ? 2 : 5;
271 } else if (ct
[4] == 'A'){
272 month
= (ct
[5] == 'p') ? 3 : 7;
273 } else if (ct
[4] == 'S'){
275 } else if (ct
[4] == 'O'){
277 } else if (ct
[4] == 'N'){
282 RPC_INT
, atoi (&ct
[17]), /* sec */
283 RPC_INT
, atoi (&ct
[14]), /* min */
284 RPC_INT
, atoi (&ct
[11]), /* hour */
285 RPC_INT
, atoi (&ct
[8]), /* mday */
286 RPC_INT
, atoi (&ct
[20]), /* year */
287 RPC_INT
, month
, /* month */
290 long ltime
= (long) time
;
293 g_snprintf (buf
, sizeof(buf
), "%lx", ltime
);
300 static void send_stat_info (struct stat
*st
)
304 #ifdef HAVE_ST_BLOCKS
311 mylong
= st
->st_rdev
;
315 rpc_send (msock
, RPC_INT
, (long) mylong
,
316 RPC_INT
, (long) st
->st_ino
,
317 RPC_INT
, (long) st
->st_mode
,
318 RPC_INT
, (long) st
->st_nlink
,
319 RPC_INT
, (long) st
->st_uid
,
320 RPC_INT
, (long) st
->st_gid
,
321 RPC_INT
, (long) st
->st_size
,
322 RPC_INT
, (long) blocks
, RPC_END
);
323 send_time (msock
, st
->st_atime
);
324 send_time (msock
, st
->st_mtime
);
325 send_time (msock
, st
->st_ctime
);
328 static void do_lstat (void)
334 rpc_get (msock
, RPC_STRING
, &file
, RPC_END
);
335 n
= lstat (file
, &st
);
336 send_status (n
, errno
);
338 send_stat_info (&st
);
342 static void do_fstat (void)
348 rpc_get (msock
, RPC_INT
, &handle
, RPC_END
);
349 n
= fstat (handle
, &st
);
350 send_status (n
, errno
);
354 send_stat_info (&st
);
357 static void do_stat (void)
363 rpc_get (msock
, RPC_STRING
, &file
, RPC_END
);
365 n
= stat (file
, &st
);
366 send_status (n
, errno
);
368 send_stat_info (&st
);
374 /* {{{ Directory lookup operations */
378 DIR *dirs
[OPENDIR_HANDLES
];
379 char *names
[OPENDIR_HANDLES
];
382 static void close_handle (int handle
)
384 if (mcfs_DIR
.used
> 0) mcfs_DIR
.used
--;
385 if (mcfs_DIR
.dirs
[handle
])
386 closedir (mcfs_DIR
.dirs
[handle
]);
387 if (mcfs_DIR
.names
[handle
])
388 g_free (mcfs_DIR
.names
[handle
]);
389 mcfs_DIR
.dirs
[handle
] = 0;
390 mcfs_DIR
.names
[handle
] = 0;
393 static void do_opendir (void)
399 rpc_get (msock
, RPC_STRING
, &arg
, RPC_END
);
401 if (mcfs_DIR
.used
== OPENDIR_HANDLES
){
402 send_status (-1, ENFILE
); /* Error */
408 for (i
= 0; i
< OPENDIR_HANDLES
; i
++){
409 if (mcfs_DIR
.dirs
[i
] == 0){
416 send_status (-1, EMFILE
);
419 fprintf (stderr
, "OOPS! you have found a bug in mc - do_opendir()!\n");
423 if (verbose
) printf ("handle=%d\n", handle
);
426 mcfs_DIR
.dirs
[handle
] = p
;
427 mcfs_DIR
.names
[handle
] = arg
;
430 /* Because 0 is an error value */
431 rpc_send (msock
, RPC_INT
, handle
+1, RPC_INT
, 0, RPC_END
);
434 send_status (-1, errno
);
439 /* Sends the complete directory listing, as well as the stat information */
440 static void do_readdir (void)
442 struct dirent
*dirent
;
447 rpc_get (msock
, RPC_INT
, &handle
, RPC_END
);
450 rpc_send (msock
, RPC_INT
, 0, RPC_END
);
454 /* We incremented it in opendir */
457 while ((dirent
= readdir (mcfs_DIR
.dirs
[handle
]))){
458 int length
= NLENGTH (dirent
);
460 rpc_send (msock
, RPC_INT
, length
, RPC_END
);
461 rpc_send (msock
, RPC_BLOCK
, length
, dirent
->d_name
, RPC_END
);
462 fname
= g_strconcat (mcfs_DIR
.names
[handle
],
463 PATH_SEP_STR
, dirent
->d_name
, NULL
);
464 n
= lstat (fname
, &st
);
465 send_status (n
, errno
);
468 send_stat_info (&st
);
470 rpc_send (msock
, RPC_INT
, 0, RPC_END
);
473 static void do_closedir (void)
477 rpc_get (msock
, RPC_INT
, &handle
, RPC_END
);
478 close_handle (handle
-1);
483 /* {{{ Operations with one and two file name argument */
485 static void do_chdir (void)
490 rpc_get (msock
, RPC_STRING
, &file
, RPC_END
);
492 status
= chdir (file
);
493 send_status (status
, errno
);
497 static void do_rmdir (void)
502 rpc_get (msock
, RPC_STRING
, &file
, RPC_END
);
504 status
= rmdir (file
);
505 send_status (status
, errno
);
509 static void do_mkdir (void)
514 rpc_get (msock
, RPC_STRING
, &file
, RPC_INT
, &mode
, RPC_END
);
516 status
= mkdir (file
, mode
);
517 send_status (status
, errno
);
521 static void do_mknod (void)
524 int mode
, dev
, status
;
526 rpc_get (msock
, RPC_STRING
, &file
, RPC_INT
, &mode
, RPC_INT
, &dev
, RPC_END
);
528 status
= mknod (file
, mode
, dev
);
529 send_status (status
, errno
);
533 static void do_readlink (void)
539 rpc_get (msock
, RPC_STRING
, &file
, RPC_END
);
540 n
= readlink (file
, buffer
, 2048);
541 send_status (n
, errno
);
544 rpc_send (msock
, RPC_STRING
, buffer
, RPC_END
);
549 static void do_unlink (void)
554 rpc_get (msock
, RPC_STRING
, &file
, RPC_END
);
555 status
= unlink (file
);
556 send_status (status
, errno
);
560 static void do_rename (void)
565 rpc_get (msock
, RPC_STRING
, &f1
, RPC_STRING
, &f2
, RPC_END
);
566 status
= rename (f1
, f2
);
567 send_status (status
, errno
);
568 g_free (f1
); g_free (f2
);
571 static void do_symlink (void)
576 rpc_get (msock
, RPC_STRING
, &f1
, RPC_STRING
, &f2
, RPC_END
);
577 status
= symlink (f1
, f2
);
578 send_status (status
, errno
);
579 g_free (f1
); g_free (f2
);
582 static void do_link (void)
587 rpc_get (msock
, RPC_STRING
, &f1
, RPC_STRING
, &f2
, RPC_END
);
588 status
= link (f1
, f2
);
589 send_status (status
, errno
);
590 g_free (f1
); g_free (f2
);
596 /* {{{ Misc commands */
598 static void do_gethome (void)
600 rpc_send (msock
, RPC_STRING
, (home_dir
) ? home_dir
: "/", RPC_END
);
603 static void do_getupdir (void)
605 rpc_send (msock
, RPC_STRING
, (up_dir
) ? up_dir
: "/", RPC_END
);
608 static void do_chmod (void)
613 rpc_get (msock
, RPC_STRING
, &file
, RPC_INT
, &mode
, RPC_END
);
614 status
= chmod (file
, mode
);
615 send_status (status
, errno
);
619 static void do_chown (void)
622 int owner
, group
, status
;
624 rpc_get (msock
, RPC_STRING
, &file
,RPC_INT
, &owner
, RPC_INT
,&group
,RPC_END
);
625 status
= chown (file
, owner
, group
);
626 send_status (status
, errno
);
630 static void do_utime (void)
638 struct utimbuf times
;
640 rpc_get (msock
, RPC_STRING
, &file
,
644 sscanf (as
, "%lx", &atime
);
645 sscanf (ms
, "%lx", &mtime
);
646 if (verbose
) printf ("Got a = %s, m = %s, comp a = %ld, m = %ld\n",
647 as
, ms
, atime
, mtime
);
650 times
.actime
= (time_t) atime
;
651 times
.modtime
= (time_t) mtime
;
652 status
= utime (file
, ×
);
653 send_status (status
, errno
);
657 static void do_quit (void)
670 mc_pam_conversation (int messages
, const struct pam_message
**msg
,
671 struct pam_response
**resp
, void *appdata_ptr
)
673 struct pam_response
*r
;
674 struct user_pass
*up
= appdata_ptr
;
677 r
= g_new (struct pam_response
, messages
);
682 for (status
= PAM_SUCCESS
; messages
--; msg
++, r
++){
683 switch ((*msg
)->msg_style
){
685 case PAM_PROMPT_ECHO_ON
:
686 r
->resp
= g_strdup (up
->username
);
687 r
->resp_retcode
= PAM_SUCCESS
;
690 case PAM_PROMPT_ECHO_OFF
:
691 r
->resp
= g_strdup (up
->password
);
692 r
->resp_retcode
= PAM_SUCCESS
;
697 r
->resp_retcode
= PAM_SUCCESS
;
702 r
->resp_retcode
= PAM_SUCCESS
;
709 static struct pam_conv conv
= { &mc_pam_conversation
, NULL
};
712 /* Return 0 if authentication failed, 1 otherwise */
714 mc_pam_auth (char *username
, char *password
)
720 up
.username
= username
;
721 up
.password
= password
;
722 conv
.appdata_ptr
= &up
;
724 if ((status
= pam_start("mcserv", username
, &conv
, &pamh
)) != PAM_SUCCESS
)
726 if ((status
= pam_authenticate (pamh
, 0)) != PAM_SUCCESS
)
728 if ((status
= pam_acct_mgmt (pamh
, 0)) != PAM_SUCCESS
)
730 if ((status
= pam_setcred (pamh
, PAM_ESTABLISH_CRED
)) != PAM_SUCCESS
)
732 pam_end (pamh
, status
);
736 pam_end (pamh
, status
);
740 #else /* Code for non-PAM authentication */
742 /* Keep reading until we find a \n */
743 static int next_line (int socket
)
748 if (read (socket
, &c
, 1) <= 0)
755 static int ftp_answer (int sock
, char *text
)
760 socket_read_block (sock
, answer
, 3);
762 if (strcmp (answer
, text
) == 0)
767 static int do_ftp_auth (char *username
, char *password
)
769 struct sockaddr_in local_address
;
770 unsigned long inaddr
;
774 memset ((char *) &local_address
, 0, sizeof (local_address
));
776 local_address
.sin_family
= AF_INET
;
777 /* FIXME: extract the ftp port with the proper function */
778 local_address
.sin_port
= htons (21);
780 /* Convert localhost to usable format */
781 if ((inaddr
= inet_addr ("127.0.0.1")) != -1)
782 memcpy ((char *) &local_address
.sin_addr
, (char *) &inaddr
,
785 if ((my_socket
= socket (AF_INET
, SOCK_STREAM
, 0)) < 0){
786 if (!isDaemon
) fprintf (stderr
, "do_auth: can't create socket\n");
789 if (connect (my_socket
, (struct sockaddr
*) &local_address
,
790 sizeof (local_address
)) < 0){
792 "do_auth: can't connect to ftp daemon for authentication\n");
796 send_string (my_socket
, "user ");
797 send_string (my_socket
, username
);
798 send_string (my_socket
, "\r\n");
799 if (!ftp_answer (my_socket
, "331")){
800 send_string (my_socket
, "quit\r\n");
804 next_line (my_socket
); /* Eat all the line */
805 send_string (my_socket
, "pass ");
806 send_string (my_socket
, password
);
807 send_string (my_socket
, "\r\n");
808 socket_read_block (my_socket
, answer
, 3);
810 send_string (my_socket
, "\r\n");
811 send_string (my_socket
, "quit\r\n");
813 if (strcmp (answer
, "230") == 0)
818 #ifdef NEED_CRYPT_PROTOTYPE
819 extern char *crypt (const char *, const char *);
822 static int do_classic_auth (char *username
, char *password
)
827 if ((this = getpwnam (username
)) == 0)
831 if (strcmp (crypt (password
, this->pw_passwd
), this->pw_passwd
) == 0){
841 #endif /* non-PAM authentication */
843 /* Try to authenticate the user based on:
844 - PAM if the system has it, else it checks:
845 - pwdauth if the system supports it.
846 - conventional auth (check salt on /etc/passwd, crypt, and compare
847 - try to contact the local ftp server and login (if -f flag used)
850 do_auth (char *username
, char *password
)
855 if (strcmp (username
, "anonymous") == 0)
859 if (mc_pam_auth (username
, password
) == 0)
861 #else /* if there is no pam */
863 if (pwdauth (username
, password
) == 0)
867 if (do_classic_auth (username
, password
))
870 auth
= do_ftp_auth (username
, password
);
876 this = getpwnam (username
);
880 if (chdir (this->pw_dir
) == -1)
883 if (this->pw_dir
[strlen (this->pw_dir
) - 1] == '/')
884 home_dir
= g_strdup (this->pw_dir
);
886 home_dir
= g_malloc (strlen (this->pw_dir
) + 2);
888 strcpy (home_dir
, this->pw_dir
);
889 strcat (home_dir
, "/");
895 if (setgid (this->pw_gid
) == -1)
898 #ifdef HAVE_INITGROUPS
900 if (NGROUPS_MAX
> 1 && initgroups (this->pw_name
, this->pw_gid
))
905 #if defined (HAVE_SETUID)
906 if (setuid (this->pw_uid
))
908 #elif defined (HAVE_SETREUID)
909 if (setreuid (this->pw_uid
, this->pw_uid
))
913 /* If the setuid call failed, then deny access */
914 /* This should fix the problem on those machines with strange setups */
915 if (getuid () != this->pw_uid
)
918 if (strcmp (username
, "ftp") == 0)
919 chroot (this->pw_dir
);
926 static int do_rauth (int socket
)
928 struct sockaddr_in from
;
931 if (getpeername(0, (struct sockaddr
*)&from
, &fromlen
) < 0)
933 from
.sin_port
= ntohs ((unsigned short) from
.sin_port
);
935 /* Strange, this should not happend */
936 if (from
.sin_family
!= AF_INET
)
939 hp
= gethostbyaddr((char *)&fromp
.sin_addr
, sizeof (struct in_addr
),
945 static int do_rauth (int msock
)
950 static void login_reply (int logged_in
)
952 rpc_send (msock
, RPC_INT
,
953 logged_in
? MC_LOGINOK
: MC_INVALID_PASS
,
957 /* FIXME: Implement the anonymous login */
958 static void do_login (void)
964 rpc_get (msock
, RPC_LIMITED_STRING
, &up_dir
, RPC_LIMITED_STRING
, &username
, RPC_END
);
965 if (verbose
) printf ("username: %s\n", username
);
968 logged_in
= do_rauth (msock
);
970 login_reply (logged_in
);
974 rpc_send (msock
, RPC_INT
, MC_NEED_PASSWORD
, RPC_END
);
975 rpc_get (msock
, RPC_INT
, &result
, RPC_END
);
976 if (result
== MC_QUIT
)
978 if (result
!= MC_PASS
){
979 if (verbose
) printf ("do_login: Unknown response: %d\n", result
);
982 rpc_get (msock
, RPC_LIMITED_STRING
, &password
, RPC_END
);
983 logged_in
= do_auth (username
, password
);
985 login_reply (logged_in
);
990 /* {{{ Server and dispatching functions */
992 /* This structure must be kept in synch with mcfs.h enums */
994 static struct _command
{
996 void (*callback
)(void);
999 { "close", do_close
},
1000 { "read", do_read
},
1001 { "write", do_write
},
1002 { "opendir", do_opendir
},
1003 { "readdir", do_readdir
},
1004 { "closedir", do_closedir
},
1005 { "stat ", do_stat
},
1006 { "lstat ", do_lstat
},
1007 { "fstat", do_fstat
},
1008 { "chmod", do_chmod
},
1009 { "chown", do_chown
},
1010 { "readlink ", do_readlink
},
1011 { "unlink", do_unlink
},
1012 { "rename", do_rename
},
1013 { "chdir ", do_chdir
},
1014 { "lseek", do_lseek
},
1015 { "rmdir", do_rmdir
},
1016 { "symlink", do_symlink
},
1017 { "mknod", do_mknod
},
1018 { "mkdir", do_mkdir
},
1019 { "link", do_link
},
1020 { "gethome", do_gethome
},
1021 { "getupdir", do_getupdir
},
1022 { "login", do_login
},
1023 { "quit", do_quit
},
1024 { "utime", do_utime
},
1027 static int ncommands
= sizeof(commands
)/sizeof(struct _command
);
1029 static void exec_command (int command
)
1032 command
>= ncommands
||
1033 commands
[command
].command
== 0){
1034 fprintf (stderr
, "Got unknown command: %d\n", command
);
1037 if (verbose
) printf ("Command: %s\n", commands
[command
].command
);
1038 (*commands
[command
].callback
)();
1041 static void check_version (void)
1045 rpc_get (msock
, RPC_INT
, &version
, RPC_END
);
1047 version
<= RPC_PROGVER
)
1048 rpc_send (msock
, RPC_INT
, MC_VERSION_OK
, RPC_END
);
1050 rpc_send (msock
, RPC_INT
, MC_VERSION_MISMATCH
, RPC_END
);
1052 clnt_version
= version
;
1055 /* This routine is called by rpc_get/rpc_send when the connection is closed */
1056 void tcp_invalidate_socket (int sock
)
1058 if (verbose
) printf ("Connection closed\n");
1062 static void server (int sock
)
1071 if (rpc_get (sock
, RPC_INT
, &command
, RPC_END
) &&
1072 (logged_in
|| command
== MC_LOGIN
))
1073 exec_command (command
);
1074 } while (!quit_server
);
1079 /* {{{ Net support code */
1081 static char *get_client (int portnum
)
1083 int sock
, clilen
, newsocket
;
1084 struct sockaddr_in client_address
, server_address
;
1086 char hostname
[255];
1092 if ((sock
= socket (AF_INET
, SOCK_STREAM
, 0)) < 0)
1093 return "Cannot create socket";
1095 /* Use this to debug: */
1096 if (setsockopt (sock
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &yes
, sizeof (yes
)) < 0)
1097 return "setsockopt failed";
1099 gethostname (hostname
, 255);
1100 if (verbose
) printf ("hostname=%s\n", hostname
);
1101 hp
= gethostbyname (hostname
);
1103 if (hp
== 0 && (me
= getenv("HOSTNAME")) && (0 == strcmp(hostname
, me
)))
1104 hp
= gethostbyname ("localhost");
1109 memset ((char *) &server_address
, 0, sizeof (server_address
));
1110 server_address
.sin_family
= hp
->h_addrtype
;
1111 server_address
.sin_addr
.s_addr
= htonl (INADDR_ANY
);
1112 server_address
.sin_port
= htons (portnum
);
1114 if (bind (sock
, (struct sockaddr
*) &server_address
,
1115 sizeof (server_address
)) < 0)
1116 return "Cannot bind";
1123 clilen
= sizeof (client_address
);
1124 newsocket
= accept (sock
, (struct sockaddr
*) &client_address
,
1127 if (isDaemon
&& (child
= fork())) {
1131 waitpid (child
, &status
, 0);
1135 if (isDaemon
&& fork()) exit (0);
1143 #ifdef HAVE_PMAP_SET
1144 static void signal_int_handler (int sig
)
1146 pmap_unset (RPC_PROGNUM
, RPC_PROGVER
);
1150 #ifndef IPPORT_RESERVED
1151 #define IPPORT_RESERVED 1024;
1154 static int get_port_number (void)
1158 #ifdef HAVE_RRESVPORT
1159 int start_port
= IPPORT_RESERVED
;
1161 port
= rresvport (&start_port
);
1163 if (geteuid () == 0){
1164 fprintf (stderr
, "Could not bind the server on a reserved port\n");
1165 DO_QUIT_NONVOID (-1);
1173 port
= mcserver_port
;
1178 static void register_port (int portnum
, int abort_if_fail
)
1180 #ifdef HAVE_PMAP_SET
1181 /* Register our service with the portmapper */
1182 /* protocol: pmap_set (prognum, versnum, protocol, portp) */
1184 if (pmap_set (RPC_PROGNUM
, RPC_PROGVER
, IPPROTO_TCP
, portnum
))
1185 signal (SIGINT
, signal_int_handler
);
1187 fprintf (stderr
, "Could not register service with portmapper\n");
1194 "This system lacks port registration, try using the -p\n"
1195 "flag to force installation at a given port");
1202 int main (int argc
, char *argv
[])
1205 extern char *optarg
;
1208 while ((c
= getopt (argc
, argv
, "fdiqp:v")) != -1){
1228 portnum
= atoi (optarg
);
1240 fprintf (stderr
, "Usage is: mcserv [options] [-p portnum]\n\n"
1242 "-d become a daemon (sets -q)\n"
1244 /* "-r use rhost based authentication\n" */
1246 "-f force ftp authentication\n"
1249 "-p to specify a port number to listen\n");
1255 if (isDaemon
&& fork()) exit (0);
1258 portnum
= get_port_number ();
1260 if (portnum
!= -1) {
1261 register_port (portnum
, 0);
1263 printf ("Using port %d\n", portnum
);
1264 if ((result
= get_client (portnum
)))
1266 #ifdef HAVE_PMAP_SET
1268 pmap_unset (RPC_PROGNUM
, RPC_PROGVER
);
1274 /* FIXME: This function should not be used in mcserv */
1275 void vfs_die( char *m
)