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
54 #include <sys/types.h>
60 /* Network include files */
61 #include <sys/socket.h>
62 #include <netinet/in.h>
64 #ifdef HAVE_ARPA_INET_H
65 #include <arpa/inet.h>
69 # include <rpc/pmap_prot.h>
70 # ifdef HAVE_RPC_PMAP_CLNT_H
71 # include <rpc/pmap_clnt.h>
75 /* Authentication include files */
78 # include <security/pam_misc.h>
79 # ifndef PAM_ESTABLISH_CRED
80 # define PAM_ESTABLISH_CRED PAM_CRED_ESTABLISH
86 extern char *crypt (const char *, const char *);
87 #endif /* !HAVE_CRYPT_H */
88 #endif /* !HAVE_PAM */
96 /* The socket from which we accept commands */
99 /* Requested version number from client */
100 static int clnt_version
;
102 /* If non zero, we accept further commands */
106 char *home_dir
= NULL
;
110 /* Were we started from inetd? */
111 int inetd_started
= 0;
113 /* Are we running as a daemon? */
122 /* port number in which we listen to connections,
123 * if zero, we try to contact the portmapper to get a port, and
124 * if it's not possible, then we use a hardcoded value
128 /* if the server will use rcmd based authentication (hosts.equiv .rhosts) */
131 #define OPENDIR_HANDLES 8
133 #define DO_QUIT_VOID() \
140 /* Only used by get_port_number */
141 #define DO_QUIT_NONVOID(a) \
150 static int quit_server
;
151 static int return_code
;
155 /* {{{ Misc routines */
157 static void send_status (int status
, int errno_number
)
159 rpc_send (msock
, RPC_INT
, status
, RPC_INT
, errno_number
, RPC_END
);
165 /* {{{ File with handle operations */
167 static void do_open (void)
169 int handle
, flags
, mode
;
172 rpc_get (msock
, RPC_STRING
, &arg
, RPC_INT
, &flags
, RPC_INT
, &mode
,RPC_END
);
174 handle
= open (arg
, flags
, mode
);
175 send_status (handle
, errno
);
179 static void do_read (void)
181 int handle
, count
, n
;
184 rpc_get (msock
, RPC_INT
, &handle
, RPC_INT
, &count
, RPC_END
);
185 data
= g_malloc (count
);
187 send_status (-1, ENOMEM
);
190 if (verbose
) printf ("count=%d\n", count
);
191 n
= read (handle
, data
, count
);
192 if (verbose
) printf ("result=%d\n", n
);
194 send_status (-1, errno
);
198 rpc_send (msock
, RPC_BLOCK
, n
, data
, RPC_END
);
203 static void do_write (void)
205 int handle
, count
, status
, written
= 0;
208 rpc_get (msock
, RPC_INT
, &handle
, RPC_INT
, &count
, RPC_END
);
211 int nbytes
= count
> 8192 ? 8192 : count
;
213 rpc_get (msock
, RPC_BLOCK
, nbytes
, buf
, RPC_END
);
214 status
= write (handle
, buf
, nbytes
);
216 send_status (status
, errno
);
219 /* FIXED: amount written must be returned to caller */
221 if (status
< nbytes
) {
222 send_status (written
, errno
);
227 send_status (written
, errno
);
230 static void do_lseek (void)
232 int handle
, offset
, whence
, status
;
237 RPC_INT
, &whence
, RPC_END
);
238 status
= lseek (handle
, offset
, whence
);
239 send_status (status
, errno
);
242 static void do_close (void)
246 rpc_get (msock
, RPC_INT
, &handle
, RPC_END
);
247 status
= close (handle
);
248 send_status (status
, errno
);
253 /* {{{ Stat family routines */
255 static void send_time (int sock
, time_t time
)
257 if (clnt_version
== 1) {
262 ct
[3] = ct
[10] = ct
[13] = ct
[16] = ct
[19] = 0;
264 /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
269 month
= (ct
[6] == 'n') ? 5 : 6;
270 } else if (ct
[4] == 'F'){
272 } else if (ct
[4] == 'M'){
273 month
= (ct
[6] == 'r') ? 2 : 5;
274 } else if (ct
[4] == 'A'){
275 month
= (ct
[5] == 'p') ? 3 : 7;
276 } else if (ct
[4] == 'S'){
278 } else if (ct
[4] == 'O'){
280 } else if (ct
[4] == 'N'){
285 RPC_INT
, atoi (&ct
[17]), /* sec */
286 RPC_INT
, atoi (&ct
[14]), /* min */
287 RPC_INT
, atoi (&ct
[11]), /* hour */
288 RPC_INT
, atoi (&ct
[8]), /* mday */
289 RPC_INT
, atoi (&ct
[20]), /* year */
290 RPC_INT
, month
, /* month */
293 long ltime
= (long) time
;
296 g_snprintf (buf
, sizeof(buf
), "%lx", ltime
);
303 static void send_stat_info (struct stat
*st
)
307 #ifdef HAVE_ST_BLOCKS
314 mylong
= st
->st_rdev
;
318 rpc_send (msock
, RPC_INT
, (long) mylong
,
319 RPC_INT
, (long) st
->st_ino
,
320 RPC_INT
, (long) st
->st_mode
,
321 RPC_INT
, (long) st
->st_nlink
,
322 RPC_INT
, (long) st
->st_uid
,
323 RPC_INT
, (long) st
->st_gid
,
324 RPC_INT
, (long) st
->st_size
,
325 RPC_INT
, (long) blocks
, RPC_END
);
326 send_time (msock
, st
->st_atime
);
327 send_time (msock
, st
->st_mtime
);
328 send_time (msock
, st
->st_ctime
);
331 static void do_lstat (void)
337 rpc_get (msock
, RPC_STRING
, &file
, RPC_END
);
338 n
= lstat (file
, &st
);
339 send_status (n
, errno
);
341 send_stat_info (&st
);
345 static void do_fstat (void)
351 rpc_get (msock
, RPC_INT
, &handle
, RPC_END
);
352 n
= fstat (handle
, &st
);
353 send_status (n
, errno
);
357 send_stat_info (&st
);
360 static void do_stat (void)
366 rpc_get (msock
, RPC_STRING
, &file
, RPC_END
);
368 n
= stat (file
, &st
);
369 send_status (n
, errno
);
371 send_stat_info (&st
);
377 /* {{{ Directory lookup operations */
381 DIR *dirs
[OPENDIR_HANDLES
];
382 char *names
[OPENDIR_HANDLES
];
385 static void close_handle (int handle
)
387 if (mcfs_DIR
.used
> 0) mcfs_DIR
.used
--;
388 if (mcfs_DIR
.dirs
[handle
])
389 closedir (mcfs_DIR
.dirs
[handle
]);
390 if (mcfs_DIR
.names
[handle
])
391 g_free (mcfs_DIR
.names
[handle
]);
392 mcfs_DIR
.dirs
[handle
] = 0;
393 mcfs_DIR
.names
[handle
] = 0;
396 static void do_opendir (void)
402 rpc_get (msock
, RPC_STRING
, &arg
, RPC_END
);
404 if (mcfs_DIR
.used
== OPENDIR_HANDLES
){
405 send_status (-1, ENFILE
); /* Error */
411 for (i
= 0; i
< OPENDIR_HANDLES
; i
++){
412 if (mcfs_DIR
.dirs
[i
] == 0){
419 send_status (-1, EMFILE
);
422 fprintf (stderr
, "OOPS! you have found a bug in mc - do_opendir()!\n");
426 if (verbose
) printf ("handle=%d\n", handle
);
429 mcfs_DIR
.dirs
[handle
] = p
;
430 mcfs_DIR
.names
[handle
] = arg
;
433 /* Because 0 is an error value */
434 rpc_send (msock
, RPC_INT
, handle
+1, RPC_INT
, 0, RPC_END
);
437 send_status (-1, errno
);
442 /* Sends the complete directory listing, as well as the stat information */
443 static void do_readdir (void)
445 struct dirent
*dirent
;
450 rpc_get (msock
, RPC_INT
, &handle
, RPC_END
);
453 rpc_send (msock
, RPC_INT
, 0, RPC_END
);
457 /* We incremented it in opendir */
460 while ((dirent
= readdir (mcfs_DIR
.dirs
[handle
]))){
461 int length
= NLENGTH (dirent
);
463 rpc_send (msock
, RPC_INT
, length
, RPC_END
);
464 rpc_send (msock
, RPC_BLOCK
, length
, dirent
->d_name
, RPC_END
);
465 fname
= g_strconcat (mcfs_DIR
.names
[handle
],
466 PATH_SEP_STR
, dirent
->d_name
, NULL
);
467 n
= lstat (fname
, &st
);
468 send_status (n
, errno
);
471 send_stat_info (&st
);
473 rpc_send (msock
, RPC_INT
, 0, RPC_END
);
476 static void do_closedir (void)
480 rpc_get (msock
, RPC_INT
, &handle
, RPC_END
);
481 close_handle (handle
-1);
486 /* {{{ Operations with one and two file name argument */
488 static void do_chdir (void)
493 rpc_get (msock
, RPC_STRING
, &file
, RPC_END
);
495 status
= chdir (file
);
496 send_status (status
, errno
);
500 static void do_rmdir (void)
505 rpc_get (msock
, RPC_STRING
, &file
, RPC_END
);
507 status
= rmdir (file
);
508 send_status (status
, errno
);
512 static void do_mkdir (void)
517 rpc_get (msock
, RPC_STRING
, &file
, RPC_INT
, &mode
, RPC_END
);
519 status
= mkdir (file
, mode
);
520 send_status (status
, errno
);
524 static void do_mknod (void)
527 int mode
, dev
, status
;
529 rpc_get (msock
, RPC_STRING
, &file
, RPC_INT
, &mode
, RPC_INT
, &dev
, RPC_END
);
531 status
= mknod (file
, mode
, dev
);
532 send_status (status
, errno
);
536 static void do_readlink (void)
542 rpc_get (msock
, RPC_STRING
, &file
, RPC_END
);
543 n
= readlink (file
, buffer
, 2048);
544 send_status (n
, errno
);
547 rpc_send (msock
, RPC_STRING
, buffer
, RPC_END
);
552 static void do_unlink (void)
557 rpc_get (msock
, RPC_STRING
, &file
, RPC_END
);
558 status
= unlink (file
);
559 send_status (status
, errno
);
563 static void do_rename (void)
568 rpc_get (msock
, RPC_STRING
, &f1
, RPC_STRING
, &f2
, RPC_END
);
569 status
= rename (f1
, f2
);
570 send_status (status
, errno
);
571 g_free (f1
); g_free (f2
);
574 static void do_symlink (void)
579 rpc_get (msock
, RPC_STRING
, &f1
, RPC_STRING
, &f2
, RPC_END
);
580 status
= symlink (f1
, f2
);
581 send_status (status
, errno
);
582 g_free (f1
); g_free (f2
);
585 static void do_link (void)
590 rpc_get (msock
, RPC_STRING
, &f1
, RPC_STRING
, &f2
, RPC_END
);
591 status
= link (f1
, f2
);
592 send_status (status
, errno
);
593 g_free (f1
); g_free (f2
);
599 /* {{{ Misc commands */
601 static void do_gethome (void)
603 rpc_send (msock
, RPC_STRING
, (home_dir
) ? home_dir
: "/", RPC_END
);
606 static void do_getupdir (void)
608 rpc_send (msock
, RPC_STRING
, (up_dir
) ? up_dir
: "/", RPC_END
);
611 static void do_chmod (void)
616 rpc_get (msock
, RPC_STRING
, &file
, RPC_INT
, &mode
, RPC_END
);
617 status
= chmod (file
, mode
);
618 send_status (status
, errno
);
622 static void do_chown (void)
625 int owner
, group
, status
;
627 rpc_get (msock
, RPC_STRING
, &file
,RPC_INT
, &owner
, RPC_INT
,&group
,RPC_END
);
628 status
= chown (file
, owner
, group
);
629 send_status (status
, errno
);
633 static void do_utime (void)
641 struct utimbuf times
;
643 rpc_get (msock
, RPC_STRING
, &file
,
647 sscanf (as
, "%lx", &atime
);
648 sscanf (ms
, "%lx", &mtime
);
649 if (verbose
) printf ("Got a = %s, m = %s, comp a = %ld, m = %ld\n",
650 as
, ms
, atime
, mtime
);
653 times
.actime
= (time_t) atime
;
654 times
.modtime
= (time_t) mtime
;
655 status
= utime (file
, ×
);
656 send_status (status
, errno
);
660 static void do_quit (void)
673 mc_pam_conversation (int messages
, const struct pam_message
**msg
,
674 struct pam_response
**resp
, void *appdata_ptr
)
676 struct pam_response
*r
;
677 struct user_pass
*up
= appdata_ptr
;
680 r
= g_new (struct pam_response
, messages
);
685 for (status
= PAM_SUCCESS
; messages
--; msg
++, r
++){
686 switch ((*msg
)->msg_style
){
688 case PAM_PROMPT_ECHO_ON
:
689 r
->resp
= g_strdup (up
->username
);
690 r
->resp_retcode
= PAM_SUCCESS
;
693 case PAM_PROMPT_ECHO_OFF
:
694 r
->resp
= g_strdup (up
->password
);
695 r
->resp_retcode
= PAM_SUCCESS
;
700 r
->resp_retcode
= PAM_SUCCESS
;
705 r
->resp_retcode
= PAM_SUCCESS
;
712 static struct pam_conv conv
= { &mc_pam_conversation
, NULL
};
715 /* Return 0 if authentication failed, 1 otherwise */
717 mc_pam_auth (char *username
, char *password
)
723 up
.username
= username
;
724 up
.password
= password
;
725 conv
.appdata_ptr
= &up
;
727 if ((status
= pam_start("mcserv", username
, &conv
, &pamh
)) != PAM_SUCCESS
)
729 if ((status
= pam_authenticate (pamh
, 0)) != PAM_SUCCESS
)
731 if ((status
= pam_acct_mgmt (pamh
, 0)) != PAM_SUCCESS
)
733 if ((status
= pam_setcred (pamh
, PAM_ESTABLISH_CRED
)) != PAM_SUCCESS
)
735 pam_end (pamh
, status
);
739 pam_end (pamh
, status
);
743 #else /* Code for non-PAM authentication */
745 /* Keep reading until we find a \n */
746 static int next_line (int socket
)
751 if (read (socket
, &c
, 1) <= 0)
758 static int ftp_answer (int sock
, char *text
)
763 socket_read_block (sock
, answer
, 3);
765 if (strcmp (answer
, text
) == 0)
770 static int do_ftp_auth (char *username
, char *password
)
772 struct sockaddr_in local_address
;
773 unsigned long inaddr
;
777 memset ((char *) &local_address
, 0, sizeof (local_address
));
779 local_address
.sin_family
= AF_INET
;
780 /* FIXME: extract the ftp port with the proper function */
781 local_address
.sin_port
= htons (21);
783 /* Convert localhost to usable format */
784 if ((inaddr
= inet_addr ("127.0.0.1")) != -1)
785 memcpy ((char *) &local_address
.sin_addr
, (char *) &inaddr
,
788 if ((my_socket
= socket (AF_INET
, SOCK_STREAM
, 0)) < 0){
789 if (!isDaemon
) fprintf (stderr
, "do_auth: can't create socket\n");
792 if (connect (my_socket
, (struct sockaddr
*) &local_address
,
793 sizeof (local_address
)) < 0){
795 "do_auth: can't connect to ftp daemon for authentication\n");
799 send_string (my_socket
, "user ");
800 send_string (my_socket
, username
);
801 send_string (my_socket
, "\r\n");
802 if (!ftp_answer (my_socket
, "331")){
803 send_string (my_socket
, "quit\r\n");
807 next_line (my_socket
); /* Eat all the line */
808 send_string (my_socket
, "pass ");
809 send_string (my_socket
, password
);
810 send_string (my_socket
, "\r\n");
811 socket_read_block (my_socket
, answer
, 3);
813 send_string (my_socket
, "\r\n");
814 send_string (my_socket
, "quit\r\n");
816 if (strcmp (answer
, "230") == 0)
821 static int do_classic_auth (char *username
, char *password
)
826 if ((this = getpwnam (username
)) == 0)
830 if (strcmp (crypt (password
, this->pw_passwd
), this->pw_passwd
) == 0){
840 #endif /* non-PAM authentication */
842 /* Try to authenticate the user based on:
843 - PAM if the system has it, else it checks:
844 - pwdauth if the system supports it.
845 - conventional auth (check salt on /etc/passwd, crypt, and compare
846 - try to contact the local ftp server and login (if -f flag used)
849 do_auth (char *username
, char *password
)
854 if (strcmp (username
, "anonymous") == 0)
858 if (mc_pam_auth (username
, password
) == 0)
860 #else /* if there is no pam */
862 if (pwdauth (username
, password
) == 0)
866 if (do_classic_auth (username
, password
))
869 auth
= do_ftp_auth (username
, password
);
875 this = getpwnam (username
);
879 if (chdir (this->pw_dir
) == -1)
882 if (this->pw_dir
[strlen (this->pw_dir
) - 1] == '/')
883 home_dir
= g_strdup (this->pw_dir
);
885 home_dir
= g_malloc (strlen (this->pw_dir
) + 2);
887 strcpy (home_dir
, this->pw_dir
);
888 strcat (home_dir
, "/");
894 if (setgid (this->pw_gid
) == -1)
897 #ifdef HAVE_INITGROUPS
899 if (NGROUPS_MAX
> 1 && initgroups (this->pw_name
, this->pw_gid
))
904 #if defined (HAVE_SETUID)
905 if (setuid (this->pw_uid
))
907 #elif defined (HAVE_SETREUID)
908 if (setreuid (this->pw_uid
, this->pw_uid
))
912 /* If the setuid call failed, then deny access */
913 /* This should fix the problem on those machines with strange setups */
914 if (getuid () != this->pw_uid
)
917 if (strcmp (username
, "ftp") == 0)
918 chroot (this->pw_dir
);
925 static int do_rauth (int socket
)
927 struct sockaddr_in from
;
930 if (getpeername(0, (struct sockaddr
*)&from
, &fromlen
) < 0)
932 from
.sin_port
= ntohs ((unsigned short) from
.sin_port
);
934 /* Strange, this should not happend */
935 if (from
.sin_family
!= AF_INET
)
938 hp
= gethostbyaddr((char *)&fromp
.sin_addr
, sizeof (struct in_addr
),
944 static int do_rauth (int msock
)
949 static void login_reply (int logged_in
)
951 rpc_send (msock
, RPC_INT
,
952 logged_in
? MC_LOGINOK
: MC_INVALID_PASS
,
956 /* FIXME: Implement the anonymous login */
957 static void do_login (void)
963 rpc_get (msock
, RPC_LIMITED_STRING
, &up_dir
, RPC_LIMITED_STRING
, &username
, RPC_END
);
964 if (verbose
) printf ("username: %s\n", username
);
967 logged_in
= do_rauth (msock
);
969 login_reply (logged_in
);
973 rpc_send (msock
, RPC_INT
, MC_NEED_PASSWORD
, RPC_END
);
974 rpc_get (msock
, RPC_INT
, &result
, RPC_END
);
975 if (result
== MC_QUIT
)
977 if (result
!= MC_PASS
){
978 if (verbose
) printf ("do_login: Unknown response: %d\n", result
);
981 rpc_get (msock
, RPC_LIMITED_STRING
, &password
, RPC_END
);
982 logged_in
= do_auth (username
, password
);
984 login_reply (logged_in
);
989 /* {{{ Server and dispatching functions */
991 /* This structure must be kept in synch with mcfs.h enums */
993 static struct _command
{
995 void (*callback
)(void);
998 { "close", do_close
},
1000 { "write", do_write
},
1001 { "opendir", do_opendir
},
1002 { "readdir", do_readdir
},
1003 { "closedir", do_closedir
},
1004 { "stat ", do_stat
},
1005 { "lstat ", do_lstat
},
1006 { "fstat", do_fstat
},
1007 { "chmod", do_chmod
},
1008 { "chown", do_chown
},
1009 { "readlink ", do_readlink
},
1010 { "unlink", do_unlink
},
1011 { "rename", do_rename
},
1012 { "chdir ", do_chdir
},
1013 { "lseek", do_lseek
},
1014 { "rmdir", do_rmdir
},
1015 { "symlink", do_symlink
},
1016 { "mknod", do_mknod
},
1017 { "mkdir", do_mkdir
},
1018 { "link", do_link
},
1019 { "gethome", do_gethome
},
1020 { "getupdir", do_getupdir
},
1021 { "login", do_login
},
1022 { "quit", do_quit
},
1023 { "utime", do_utime
},
1026 static int ncommands
= sizeof(commands
)/sizeof(struct _command
);
1028 static void exec_command (int command
)
1031 command
>= ncommands
||
1032 commands
[command
].command
== 0){
1033 fprintf (stderr
, "Got unknown command: %d\n", command
);
1036 if (verbose
) printf ("Command: %s\n", commands
[command
].command
);
1037 (*commands
[command
].callback
)();
1040 static void check_version (void)
1044 rpc_get (msock
, RPC_INT
, &version
, RPC_END
);
1046 version
<= RPC_PROGVER
)
1047 rpc_send (msock
, RPC_INT
, MC_VERSION_OK
, RPC_END
);
1049 rpc_send (msock
, RPC_INT
, MC_VERSION_MISMATCH
, RPC_END
);
1051 clnt_version
= version
;
1054 /* This routine is called by rpc_get/rpc_send when the connection is closed */
1055 void tcp_invalidate_socket (int sock
)
1057 if (verbose
) printf ("Connection closed\n");
1061 static void server (int sock
)
1070 if (rpc_get (sock
, RPC_INT
, &command
, RPC_END
) &&
1071 (logged_in
|| command
== MC_LOGIN
))
1072 exec_command (command
);
1073 } while (!quit_server
);
1078 /* {{{ Net support code */
1080 static char *get_client (int portnum
)
1082 int sock
, clilen
, newsocket
;
1083 struct sockaddr_in client_address
, server_address
;
1085 char hostname
[255];
1091 if ((sock
= socket (AF_INET
, SOCK_STREAM
, 0)) < 0)
1092 return "Cannot create socket";
1094 /* Use this to debug: */
1095 if (setsockopt (sock
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &yes
, sizeof (yes
)) < 0)
1096 return "setsockopt failed";
1098 gethostname (hostname
, 255);
1099 if (verbose
) printf ("hostname=%s\n", hostname
);
1100 hp
= gethostbyname (hostname
);
1102 if (hp
== 0 && (me
= getenv("HOSTNAME")) && (0 == strcmp(hostname
, me
)))
1103 hp
= gethostbyname ("localhost");
1108 memset ((char *) &server_address
, 0, sizeof (server_address
));
1109 server_address
.sin_family
= hp
->h_addrtype
;
1110 server_address
.sin_addr
.s_addr
= htonl (INADDR_ANY
);
1111 server_address
.sin_port
= htons (portnum
);
1113 if (bind (sock
, (struct sockaddr
*) &server_address
,
1114 sizeof (server_address
)) < 0)
1115 return "Cannot bind";
1122 clilen
= sizeof (client_address
);
1123 newsocket
= accept (sock
, (struct sockaddr
*) &client_address
,
1126 if (isDaemon
&& (child
= fork())) {
1130 waitpid (child
, &status
, 0);
1134 if (isDaemon
&& fork()) exit (0);
1142 #ifdef HAVE_PMAP_SET
1143 static void signal_int_handler (int sig
)
1145 pmap_unset (RPC_PROGNUM
, RPC_PROGVER
);
1149 #ifndef IPPORT_RESERVED
1150 #define IPPORT_RESERVED 1024;
1153 static int get_port_number (void)
1157 #ifdef HAVE_RRESVPORT
1158 int start_port
= IPPORT_RESERVED
;
1160 port
= rresvport (&start_port
);
1162 if (geteuid () == 0){
1163 fprintf (stderr
, "Could not bind the server on a reserved port\n");
1164 DO_QUIT_NONVOID (-1);
1172 port
= mcserver_port
;
1177 static void register_port (int portnum
, int abort_if_fail
)
1179 #ifdef HAVE_PMAP_SET
1180 /* Register our service with the portmapper */
1181 /* protocol: pmap_set (prognum, versnum, protocol, portp) */
1183 if (pmap_set (RPC_PROGNUM
, RPC_PROGVER
, IPPROTO_TCP
, portnum
))
1184 signal (SIGINT
, signal_int_handler
);
1186 fprintf (stderr
, "Could not register service with portmapper\n");
1193 "This system lacks port registration, try using the -p\n"
1194 "flag to force installation at a given port");
1201 int main (int argc
, char *argv
[])
1204 extern char *optarg
;
1207 while ((c
= getopt (argc
, argv
, "fdiqp:v")) != -1){
1227 portnum
= atoi (optarg
);
1239 fprintf (stderr
, "Usage is: mcserv [options] [-p portnum]\n\n"
1241 "-d become a daemon (sets -q)\n"
1243 /* "-r use rhost based authentication\n" */
1245 "-f force ftp authentication\n"
1248 "-p to specify a port number to listen\n");
1254 if (isDaemon
&& fork()) exit (0);
1257 portnum
= get_port_number ();
1259 if (portnum
!= -1) {
1260 register_port (portnum
, 0);
1262 printf ("Using port %d\n", portnum
);
1263 if ((result
= get_client (portnum
)))
1265 #ifdef HAVE_PMAP_SET
1267 pmap_unset (RPC_PROGNUM
, RPC_PROGVER
);
1273 /* FIXME: This function should not be used in mcserv */
1274 void vfs_die( char *m
)