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 #include <arpa/inet.h>
70 # include <rpc/pmap_prot.h>
71 # ifdef HAVE_RPC_PMAP_CLNT_H
72 # include <rpc/pmap_clnt.h>
76 /* Authentication include files */
79 # include <security/pam_misc.h>
80 # ifndef PAM_ESTABLISH_CRED
81 # define PAM_ESTABLISH_CRED PAM_CRED_ESTABLISH
91 /* The socket from which we accept commands */
94 /* Requested version number from client */
95 static int clnt_version
;
97 /* If non zero, we accept further commands */
101 char *home_dir
= NULL
;
105 /* Were we started from inetd? */
106 int inetd_started
= 0;
108 /* Are we running as a daemon? */
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
123 /* if the server will use rcmd based authentication (hosts.equiv .rhosts) */
126 #define OPENDIR_HANDLES 8
128 #define DO_QUIT_VOID() \
135 /* Only used by get_port_number */
136 #define DO_QUIT_NONVOID(a) \
145 static int quit_server
;
146 static int return_code
;
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
);
160 /* {{{ File with handle operations */
162 static void do_open (void)
164 int handle
, flags
, mode
;
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
);
174 static void do_read (void)
176 int handle
, count
, n
;
179 rpc_get (msock
, RPC_INT
, &handle
, RPC_INT
, &count
, RPC_END
);
180 data
= g_malloc (count
);
182 send_status (-1, ENOMEM
);
185 if (verbose
) printf ("count=%d\n", count
);
186 n
= read (handle
, data
, count
);
187 if (verbose
) printf ("result=%d\n", n
);
189 send_status (-1, errno
);
193 rpc_send (msock
, RPC_BLOCK
, n
, data
, RPC_END
);
198 static void do_write (void)
200 int handle
, count
, status
, written
= 0;
203 rpc_get (msock
, RPC_INT
, &handle
, RPC_INT
, &count
, RPC_END
);
206 int nbytes
= count
> 8192 ? 8192 : count
;
208 rpc_get (msock
, RPC_BLOCK
, nbytes
, buf
, RPC_END
);
209 status
= write (handle
, buf
, nbytes
);
211 send_status (status
, errno
);
214 /* FIXED: amount written must be returned to caller */
216 if (status
< nbytes
) {
217 send_status (written
, errno
);
222 send_status (written
, errno
);
225 static void do_lseek (void)
227 int handle
, offset
, whence
, status
;
232 RPC_INT
, &whence
, RPC_END
);
233 status
= lseek (handle
, offset
, whence
);
234 send_status (status
, errno
);
237 static void do_close (void)
241 rpc_get (msock
, RPC_INT
, &handle
, RPC_END
);
242 status
= close (handle
);
243 send_status (status
, errno
);
248 /* {{{ Stat family routines */
250 static void send_time (int sock
, time_t time
)
252 if (clnt_version
== 1) {
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 */
264 month
= (ct
[6] == 'n') ? 5 : 6;
265 } else if (ct
[4] == 'F'){
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'){
273 } else if (ct
[4] == 'O'){
275 } else if (ct
[4] == 'N'){
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 */
288 long ltime
= (long) time
;
291 g_snprintf (buf
, sizeof(buf
), "%lx", ltime
);
298 static void send_stat_info (struct stat
*st
)
302 #ifdef HAVE_ST_BLOCKS
309 mylong
= st
->st_rdev
;
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)
332 rpc_get (msock
, RPC_STRING
, &file
, RPC_END
);
333 n
= lstat (file
, &st
);
334 send_status (n
, errno
);
336 send_stat_info (&st
);
340 static void do_fstat (void)
346 rpc_get (msock
, RPC_INT
, &handle
, RPC_END
);
347 n
= fstat (handle
, &st
);
348 send_status (n
, errno
);
352 send_stat_info (&st
);
355 static void do_stat (void)
361 rpc_get (msock
, RPC_STRING
, &file
, RPC_END
);
363 n
= stat (file
, &st
);
364 send_status (n
, errno
);
366 send_stat_info (&st
);
372 /* {{{ Directory lookup operations */
376 DIR *dirs
[OPENDIR_HANDLES
];
377 char *names
[OPENDIR_HANDLES
];
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)
397 rpc_get (msock
, RPC_STRING
, &arg
, RPC_END
);
399 if (mcfs_DIR
.used
== OPENDIR_HANDLES
){
400 send_status (-1, ENFILE
); /* Error */
406 for (i
= 0; i
< OPENDIR_HANDLES
; i
++){
407 if (mcfs_DIR
.dirs
[i
] == 0){
414 send_status (-1, EMFILE
);
417 fprintf (stderr
, "OOPS! you have found a bug in mc - do_opendir()!\n");
421 if (verbose
) printf ("handle=%d\n", handle
);
424 mcfs_DIR
.dirs
[handle
] = p
;
425 mcfs_DIR
.names
[handle
] = arg
;
428 /* Because 0 is an error value */
429 rpc_send (msock
, RPC_INT
, handle
+1, RPC_INT
, 0, RPC_END
);
432 send_status (-1, errno
);
437 /* Sends the complete directory listing, as well as the stat information */
438 static void do_readdir (void)
440 struct dirent
*dirent
;
445 rpc_get (msock
, RPC_INT
, &handle
, RPC_END
);
448 rpc_send (msock
, RPC_INT
, 0, RPC_END
);
452 /* We incremented it in opendir */
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
);
466 send_stat_info (&st
);
468 rpc_send (msock
, RPC_INT
, 0, RPC_END
);
471 static void do_closedir (void)
475 rpc_get (msock
, RPC_INT
, &handle
, RPC_END
);
476 close_handle (handle
-1);
481 /* {{{ Operations with one and two file name argument */
483 static void do_chdir (void)
488 rpc_get (msock
, RPC_STRING
, &file
, RPC_END
);
490 status
= chdir (file
);
491 send_status (status
, errno
);
495 static void do_rmdir (void)
500 rpc_get (msock
, RPC_STRING
, &file
, RPC_END
);
502 status
= rmdir (file
);
503 send_status (status
, errno
);
507 static void do_mkdir (void)
512 rpc_get (msock
, RPC_STRING
, &file
, RPC_INT
, &mode
, RPC_END
);
514 status
= mkdir (file
, mode
);
515 send_status (status
, errno
);
519 static void do_mknod (void)
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
);
531 static void do_readlink (void)
537 rpc_get (msock
, RPC_STRING
, &file
, RPC_END
);
538 n
= readlink (file
, buffer
, 2048);
539 send_status (n
, errno
);
542 rpc_send (msock
, RPC_STRING
, buffer
, RPC_END
);
547 static void do_unlink (void)
552 rpc_get (msock
, RPC_STRING
, &file
, RPC_END
);
553 status
= unlink (file
);
554 send_status (status
, errno
);
558 static void do_rename (void)
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)
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)
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
);
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)
611 rpc_get (msock
, RPC_STRING
, &file
, RPC_INT
, &mode
, RPC_END
);
612 status
= chmod (file
, mode
);
613 send_status (status
, errno
);
617 static void do_chown (void)
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
);
628 static void do_utime (void)
636 struct utimbuf times
;
638 rpc_get (msock
, RPC_STRING
, &file
,
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
);
648 times
.actime
= (time_t) atime
;
649 times
.modtime
= (time_t) mtime
;
650 status
= utime (file
, ×
);
651 send_status (status
, errno
);
655 static void do_quit (void)
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
;
675 r
= g_new (struct pam_response
, messages
);
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
;
688 case PAM_PROMPT_ECHO_OFF
:
689 r
->resp
= g_strdup (up
->password
);
690 r
->resp_retcode
= PAM_SUCCESS
;
695 r
->resp_retcode
= PAM_SUCCESS
;
700 r
->resp_retcode
= PAM_SUCCESS
;
707 static struct pam_conv conv
= { &mc_pam_conversation
, NULL
};
710 /* Return 0 if authentication failed, 1 otherwise */
712 mc_pam_auth (char *username
, char *password
)
718 up
.username
= username
;
719 up
.password
= password
;
720 conv
.appdata_ptr
= &up
;
722 if ((status
= pam_start("mcserv", username
, &conv
, &pamh
)) != PAM_SUCCESS
)
724 if ((status
= pam_authenticate (pamh
, 0)) != PAM_SUCCESS
)
726 if ((status
= pam_acct_mgmt (pamh
, 0)) != PAM_SUCCESS
)
728 if ((status
= pam_setcred (pamh
, PAM_ESTABLISH_CRED
)) != PAM_SUCCESS
)
730 pam_end (pamh
, status
);
734 pam_end (pamh
, status
);
738 #else /* Code for non-PAM authentication */
740 /* Keep reading until we find a \n */
741 static int next_line (int socket
)
746 if (read (socket
, &c
, 1) <= 0)
753 static int ftp_answer (int sock
, char *text
)
758 socket_read_block (sock
, answer
, 3);
760 if (strcmp (answer
, text
) == 0)
765 static int do_ftp_auth (char *username
, char *password
)
767 struct sockaddr_in local_address
;
768 unsigned long inaddr
;
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
,
783 if ((my_socket
= socket (AF_INET
, SOCK_STREAM
, 0)) < 0){
784 if (!isDaemon
) fprintf (stderr
, "do_auth: can't create socket\n");
787 if (connect (my_socket
, (struct sockaddr
*) &local_address
,
788 sizeof (local_address
)) < 0){
790 "do_auth: can't connect to ftp daemon for authentication\n");
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");
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);
808 send_string (my_socket
, "\r\n");
809 send_string (my_socket
, "quit\r\n");
811 if (strcmp (answer
, "230") == 0)
816 #ifdef NEED_CRYPT_PROTOTYPE
817 extern char *crypt (const char *, const char *);
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
)